drm_bridge.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2014 Samsung Electronics Co., Ltd
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the
  12. * next paragraph) shall be included in all copies or substantial portions
  13. * of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <linux/err.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <drm/drm_bridge.h>
  27. /**
  28. * DOC: overview
  29. *
  30. * struct &drm_bridge represents a device that hangs on to an encoder. These are
  31. * handy when a regular &drm_encoder entity isn't enough to represent the entire
  32. * encoder chain.
  33. *
  34. * A bridge is always attached to a single &drm_encoder at a time, but can be
  35. * either connected to it directly, or through an intermediate bridge::
  36. *
  37. * encoder ---> bridge B ---> bridge A
  38. *
  39. * Here, the output of the encoder feeds to bridge B, and that furthers feeds to
  40. * bridge A.
  41. *
  42. * The driver using the bridge is responsible to make the associations between
  43. * the encoder and bridges. Once these links are made, the bridges will
  44. * participate along with encoder functions to perform mode_set/enable/disable
  45. * through the ops provided in &drm_bridge_funcs.
  46. *
  47. * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes,
  48. * CRTCs, encoders or connectors and hence are not visible to userspace. They
  49. * just provide additional hooks to get the desired output at the end of the
  50. * encoder chain.
  51. *
  52. * Bridges can also be chained up using the next pointer in struct &drm_bridge.
  53. *
  54. * Both legacy CRTC helpers and the new atomic modeset helpers support bridges.
  55. */
  56. static DEFINE_MUTEX(bridge_lock);
  57. static LIST_HEAD(bridge_list);
  58. /**
  59. * drm_bridge_add - add the given bridge to the global bridge list
  60. *
  61. * @bridge: bridge control structure
  62. *
  63. * RETURNS:
  64. * Unconditionally returns Zero.
  65. */
  66. int drm_bridge_add(struct drm_bridge *bridge)
  67. {
  68. mutex_lock(&bridge_lock);
  69. list_add_tail(&bridge->list, &bridge_list);
  70. mutex_unlock(&bridge_lock);
  71. return 0;
  72. }
  73. EXPORT_SYMBOL(drm_bridge_add);
  74. /**
  75. * drm_bridge_remove - remove the given bridge from the global bridge list
  76. *
  77. * @bridge: bridge control structure
  78. */
  79. void drm_bridge_remove(struct drm_bridge *bridge)
  80. {
  81. mutex_lock(&bridge_lock);
  82. list_del_init(&bridge->list);
  83. mutex_unlock(&bridge_lock);
  84. }
  85. EXPORT_SYMBOL(drm_bridge_remove);
  86. /**
  87. * drm_bridge_attach - associate given bridge to our DRM device
  88. *
  89. * @dev: DRM device
  90. * @bridge: bridge control structure
  91. *
  92. * Called by a kms driver to link one of our encoder/bridge to the given
  93. * bridge.
  94. *
  95. * Note that setting up links between the bridge and our encoder/bridge
  96. * objects needs to be handled by the kms driver itself.
  97. *
  98. * RETURNS:
  99. * Zero on success, error code on failure
  100. */
  101. int drm_bridge_attach(struct drm_device *dev, struct drm_bridge *bridge)
  102. {
  103. if (!dev || !bridge)
  104. return -EINVAL;
  105. if (bridge->dev)
  106. return -EBUSY;
  107. bridge->dev = dev;
  108. if (bridge->funcs->attach)
  109. return bridge->funcs->attach(bridge);
  110. return 0;
  111. }
  112. EXPORT_SYMBOL(drm_bridge_attach);
  113. /**
  114. * drm_bridge_detach - deassociate given bridge from its DRM device
  115. *
  116. * @bridge: bridge control structure
  117. *
  118. * Called by a kms driver to unlink the given bridge from its DRM device.
  119. *
  120. * Note that tearing down links between the bridge and our encoder/bridge
  121. * objects needs to be handled by the kms driver itself.
  122. */
  123. void drm_bridge_detach(struct drm_bridge *bridge)
  124. {
  125. if (WARN_ON(!bridge))
  126. return;
  127. if (WARN_ON(!bridge->dev))
  128. return;
  129. if (bridge->funcs->detach)
  130. bridge->funcs->detach(bridge);
  131. bridge->dev = NULL;
  132. }
  133. EXPORT_SYMBOL(drm_bridge_detach);
  134. /**
  135. * DOC: bridge callbacks
  136. *
  137. * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM
  138. * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c
  139. * These helpers call a specific &drm_bridge_funcs op for all the bridges
  140. * during encoder configuration.
  141. *
  142. * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
  143. */
  144. /**
  145. * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the
  146. * encoder chain
  147. * @bridge: bridge control structure
  148. * @mode: desired mode to be set for the bridge
  149. * @adjusted_mode: updated mode that works for this bridge
  150. *
  151. * Calls ->mode_fixup() &drm_bridge_funcs op for all the bridges in the
  152. * encoder chain, starting from the first bridge to the last.
  153. *
  154. * Note: the bridge passed should be the one closest to the encoder
  155. *
  156. * RETURNS:
  157. * true on success, false on failure
  158. */
  159. bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
  160. const struct drm_display_mode *mode,
  161. struct drm_display_mode *adjusted_mode)
  162. {
  163. bool ret = true;
  164. if (!bridge)
  165. return true;
  166. if (bridge->funcs->mode_fixup)
  167. ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode);
  168. ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode);
  169. return ret;
  170. }
  171. EXPORT_SYMBOL(drm_bridge_mode_fixup);
  172. /**
  173. * drm_bridge_disable - calls ->disable() &drm_bridge_funcs op for all
  174. * bridges in the encoder chain.
  175. * @bridge: bridge control structure
  176. *
  177. * Calls ->disable() &drm_bridge_funcs op for all the bridges in the encoder
  178. * chain, starting from the last bridge to the first. These are called before
  179. * calling the encoder's prepare op.
  180. *
  181. * Note: the bridge passed should be the one closest to the encoder
  182. */
  183. void drm_bridge_disable(struct drm_bridge *bridge)
  184. {
  185. if (!bridge)
  186. return;
  187. drm_bridge_disable(bridge->next);
  188. if (bridge->funcs->disable)
  189. bridge->funcs->disable(bridge);
  190. }
  191. EXPORT_SYMBOL(drm_bridge_disable);
  192. /**
  193. * drm_bridge_post_disable - calls ->post_disable() &drm_bridge_funcs op for
  194. * all bridges in the encoder chain.
  195. * @bridge: bridge control structure
  196. *
  197. * Calls ->post_disable() &drm_bridge_funcs op for all the bridges in the
  198. * encoder chain, starting from the first bridge to the last. These are called
  199. * after completing the encoder's prepare op.
  200. *
  201. * Note: the bridge passed should be the one closest to the encoder
  202. */
  203. void drm_bridge_post_disable(struct drm_bridge *bridge)
  204. {
  205. if (!bridge)
  206. return;
  207. if (bridge->funcs->post_disable)
  208. bridge->funcs->post_disable(bridge);
  209. drm_bridge_post_disable(bridge->next);
  210. }
  211. EXPORT_SYMBOL(drm_bridge_post_disable);
  212. /**
  213. * drm_bridge_mode_set - set proposed mode for all bridges in the
  214. * encoder chain
  215. * @bridge: bridge control structure
  216. * @mode: desired mode to be set for the bridge
  217. * @adjusted_mode: updated mode that works for this bridge
  218. *
  219. * Calls ->mode_set() &drm_bridge_funcs op for all the bridges in the
  220. * encoder chain, starting from the first bridge to the last.
  221. *
  222. * Note: the bridge passed should be the one closest to the encoder
  223. */
  224. void drm_bridge_mode_set(struct drm_bridge *bridge,
  225. struct drm_display_mode *mode,
  226. struct drm_display_mode *adjusted_mode)
  227. {
  228. if (!bridge)
  229. return;
  230. if (bridge->funcs->mode_set)
  231. bridge->funcs->mode_set(bridge, mode, adjusted_mode);
  232. drm_bridge_mode_set(bridge->next, mode, adjusted_mode);
  233. }
  234. EXPORT_SYMBOL(drm_bridge_mode_set);
  235. /**
  236. * drm_bridge_pre_enable - calls ->pre_enable() &drm_bridge_funcs op for all
  237. * bridges in the encoder chain.
  238. * @bridge: bridge control structure
  239. *
  240. * Calls ->pre_enable() &drm_bridge_funcs op for all the bridges in the encoder
  241. * chain, starting from the last bridge to the first. These are called
  242. * before calling the encoder's commit op.
  243. *
  244. * Note: the bridge passed should be the one closest to the encoder
  245. */
  246. void drm_bridge_pre_enable(struct drm_bridge *bridge)
  247. {
  248. if (!bridge)
  249. return;
  250. drm_bridge_pre_enable(bridge->next);
  251. if (bridge->funcs->pre_enable)
  252. bridge->funcs->pre_enable(bridge);
  253. }
  254. EXPORT_SYMBOL(drm_bridge_pre_enable);
  255. /**
  256. * drm_bridge_enable - calls ->enable() &drm_bridge_funcs op for all bridges
  257. * in the encoder chain.
  258. * @bridge: bridge control structure
  259. *
  260. * Calls ->enable() &drm_bridge_funcs op for all the bridges in the encoder
  261. * chain, starting from the first bridge to the last. These are called
  262. * after completing the encoder's commit op.
  263. *
  264. * Note that the bridge passed should be the one closest to the encoder
  265. */
  266. void drm_bridge_enable(struct drm_bridge *bridge)
  267. {
  268. if (!bridge)
  269. return;
  270. if (bridge->funcs->enable)
  271. bridge->funcs->enable(bridge);
  272. drm_bridge_enable(bridge->next);
  273. }
  274. EXPORT_SYMBOL(drm_bridge_enable);
  275. #ifdef CONFIG_OF
  276. /**
  277. * of_drm_find_bridge - find the bridge corresponding to the device node in
  278. * the global bridge list
  279. *
  280. * @np: device node
  281. *
  282. * RETURNS:
  283. * drm_bridge control struct on success, NULL on failure
  284. */
  285. struct drm_bridge *of_drm_find_bridge(struct device_node *np)
  286. {
  287. struct drm_bridge *bridge;
  288. mutex_lock(&bridge_lock);
  289. list_for_each_entry(bridge, &bridge_list, list) {
  290. if (bridge->of_node == np) {
  291. mutex_unlock(&bridge_lock);
  292. return bridge;
  293. }
  294. }
  295. mutex_unlock(&bridge_lock);
  296. return NULL;
  297. }
  298. EXPORT_SYMBOL(of_drm_find_bridge);
  299. #endif
  300. MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
  301. MODULE_DESCRIPTION("DRM bridge infrastructure");
  302. MODULE_LICENSE("GPL and additional rights");