drm_bridge.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 <drm/drm_crtc.h>
  26. #include "drm/drmP.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. * DOC: bridge callbacks
  115. *
  116. * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM
  117. * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c
  118. * These helpers call a specific &drm_bridge_funcs op for all the bridges
  119. * during encoder configuration.
  120. *
  121. * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
  122. */
  123. /**
  124. * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the
  125. * encoder chain
  126. * @bridge: bridge control structure
  127. * @mode: desired mode to be set for the bridge
  128. * @adjusted_mode: updated mode that works for this bridge
  129. *
  130. * Calls ->mode_fixup() &drm_bridge_funcs op for all the bridges in the
  131. * encoder chain, starting from the first bridge to the last.
  132. *
  133. * Note: the bridge passed should be the one closest to the encoder
  134. *
  135. * RETURNS:
  136. * true on success, false on failure
  137. */
  138. bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
  139. const struct drm_display_mode *mode,
  140. struct drm_display_mode *adjusted_mode)
  141. {
  142. bool ret = true;
  143. if (!bridge)
  144. return true;
  145. if (bridge->funcs->mode_fixup)
  146. ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode);
  147. ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode);
  148. return ret;
  149. }
  150. EXPORT_SYMBOL(drm_bridge_mode_fixup);
  151. /**
  152. * drm_bridge_disable - calls ->disable() &drm_bridge_funcs op for all
  153. * bridges in the encoder chain.
  154. * @bridge: bridge control structure
  155. *
  156. * Calls ->disable() &drm_bridge_funcs op for all the bridges in the encoder
  157. * chain, starting from the last bridge to the first. These are called before
  158. * calling the encoder's prepare op.
  159. *
  160. * Note: the bridge passed should be the one closest to the encoder
  161. */
  162. void drm_bridge_disable(struct drm_bridge *bridge)
  163. {
  164. if (!bridge)
  165. return;
  166. drm_bridge_disable(bridge->next);
  167. if (bridge->funcs->disable)
  168. bridge->funcs->disable(bridge);
  169. }
  170. EXPORT_SYMBOL(drm_bridge_disable);
  171. /**
  172. * drm_bridge_post_disable - calls ->post_disable() &drm_bridge_funcs op for
  173. * all bridges in the encoder chain.
  174. * @bridge: bridge control structure
  175. *
  176. * Calls ->post_disable() &drm_bridge_funcs op for all the bridges in the
  177. * encoder chain, starting from the first bridge to the last. These are called
  178. * after completing the encoder's prepare op.
  179. *
  180. * Note: the bridge passed should be the one closest to the encoder
  181. */
  182. void drm_bridge_post_disable(struct drm_bridge *bridge)
  183. {
  184. if (!bridge)
  185. return;
  186. if (bridge->funcs->post_disable)
  187. bridge->funcs->post_disable(bridge);
  188. drm_bridge_post_disable(bridge->next);
  189. }
  190. EXPORT_SYMBOL(drm_bridge_post_disable);
  191. /**
  192. * drm_bridge_mode_set - set proposed mode for all bridges in the
  193. * encoder chain
  194. * @bridge: bridge control structure
  195. * @mode: desired mode to be set for the bridge
  196. * @adjusted_mode: updated mode that works for this bridge
  197. *
  198. * Calls ->mode_set() &drm_bridge_funcs op for all the bridges in the
  199. * encoder chain, starting from the first bridge to the last.
  200. *
  201. * Note: the bridge passed should be the one closest to the encoder
  202. */
  203. void drm_bridge_mode_set(struct drm_bridge *bridge,
  204. struct drm_display_mode *mode,
  205. struct drm_display_mode *adjusted_mode)
  206. {
  207. if (!bridge)
  208. return;
  209. if (bridge->funcs->mode_set)
  210. bridge->funcs->mode_set(bridge, mode, adjusted_mode);
  211. drm_bridge_mode_set(bridge->next, mode, adjusted_mode);
  212. }
  213. EXPORT_SYMBOL(drm_bridge_mode_set);
  214. /**
  215. * drm_bridge_pre_enable - calls ->pre_enable() &drm_bridge_funcs op for all
  216. * bridges in the encoder chain.
  217. * @bridge: bridge control structure
  218. *
  219. * Calls ->pre_enable() &drm_bridge_funcs op for all the bridges in the encoder
  220. * chain, starting from the last bridge to the first. These are called
  221. * before calling the encoder's commit op.
  222. *
  223. * Note: the bridge passed should be the one closest to the encoder
  224. */
  225. void drm_bridge_pre_enable(struct drm_bridge *bridge)
  226. {
  227. if (!bridge)
  228. return;
  229. drm_bridge_pre_enable(bridge->next);
  230. if (bridge->funcs->pre_enable)
  231. bridge->funcs->pre_enable(bridge);
  232. }
  233. EXPORT_SYMBOL(drm_bridge_pre_enable);
  234. /**
  235. * drm_bridge_enable - calls ->enable() &drm_bridge_funcs op for all bridges
  236. * in the encoder chain.
  237. * @bridge: bridge control structure
  238. *
  239. * Calls ->enable() &drm_bridge_funcs op for all the bridges in the encoder
  240. * chain, starting from the first bridge to the last. These are called
  241. * after completing the encoder's commit op.
  242. *
  243. * Note that the bridge passed should be the one closest to the encoder
  244. */
  245. void drm_bridge_enable(struct drm_bridge *bridge)
  246. {
  247. if (!bridge)
  248. return;
  249. if (bridge->funcs->enable)
  250. bridge->funcs->enable(bridge);
  251. drm_bridge_enable(bridge->next);
  252. }
  253. EXPORT_SYMBOL(drm_bridge_enable);
  254. #ifdef CONFIG_OF
  255. /**
  256. * of_drm_find_bridge - find the bridge corresponding to the device node in
  257. * the global bridge list
  258. *
  259. * @np: device node
  260. *
  261. * RETURNS:
  262. * drm_bridge control struct on success, NULL on failure
  263. */
  264. struct drm_bridge *of_drm_find_bridge(struct device_node *np)
  265. {
  266. struct drm_bridge *bridge;
  267. mutex_lock(&bridge_lock);
  268. list_for_each_entry(bridge, &bridge_list, list) {
  269. if (bridge->of_node == np) {
  270. mutex_unlock(&bridge_lock);
  271. return bridge;
  272. }
  273. }
  274. mutex_unlock(&bridge_lock);
  275. return NULL;
  276. }
  277. EXPORT_SYMBOL(of_drm_find_bridge);
  278. #endif
  279. MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
  280. MODULE_DESCRIPTION("DRM bridge infrastructure");
  281. MODULE_LICENSE("GPL and additional rights");