drm_bridge.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. * drm_bridge represents a device that hangs on to an encoder. These are handy
  31. * when a regular drm_encoder entity isn't enough to represent the entire
  32. * encoder chain.
  33. *
  34. * A bridge is always associated 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. They just provide additional hooks to get the
  49. * desired output at the end of the encoder chain.
  50. */
  51. static DEFINE_MUTEX(bridge_lock);
  52. static LIST_HEAD(bridge_list);
  53. /**
  54. * drm_bridge_add - add the given bridge to the global bridge list
  55. *
  56. * @bridge: bridge control structure
  57. *
  58. * RETURNS:
  59. * Unconditionally returns Zero.
  60. */
  61. int drm_bridge_add(struct drm_bridge *bridge)
  62. {
  63. mutex_lock(&bridge_lock);
  64. list_add_tail(&bridge->list, &bridge_list);
  65. mutex_unlock(&bridge_lock);
  66. return 0;
  67. }
  68. EXPORT_SYMBOL(drm_bridge_add);
  69. /**
  70. * drm_bridge_remove - remove the given bridge from the global bridge list
  71. *
  72. * @bridge: bridge control structure
  73. */
  74. void drm_bridge_remove(struct drm_bridge *bridge)
  75. {
  76. mutex_lock(&bridge_lock);
  77. list_del_init(&bridge->list);
  78. mutex_unlock(&bridge_lock);
  79. }
  80. EXPORT_SYMBOL(drm_bridge_remove);
  81. /**
  82. * drm_bridge_attach - associate given bridge to our DRM device
  83. *
  84. * @dev: DRM device
  85. * @bridge: bridge control structure
  86. *
  87. * called by a kms driver to link one of our encoder/bridge to the given
  88. * bridge.
  89. *
  90. * Note that setting up links between the bridge and our encoder/bridge
  91. * objects needs to be handled by the kms driver itself
  92. *
  93. * RETURNS:
  94. * Zero on success, error code on failure
  95. */
  96. int drm_bridge_attach(struct drm_device *dev, struct drm_bridge *bridge)
  97. {
  98. if (!dev || !bridge)
  99. return -EINVAL;
  100. if (bridge->dev)
  101. return -EBUSY;
  102. bridge->dev = dev;
  103. if (bridge->funcs->attach)
  104. return bridge->funcs->attach(bridge);
  105. return 0;
  106. }
  107. EXPORT_SYMBOL(drm_bridge_attach);
  108. /**
  109. * DOC: bridge callbacks
  110. *
  111. * The drm_bridge_funcs ops are populated by the bridge driver. The drm
  112. * internals(atomic and crtc helpers) use the helpers defined in drm_bridge.c
  113. * These helpers call a specific drm_bridge_funcs op for all the bridges
  114. * during encoder configuration.
  115. *
  116. * When creating a bridge driver, one can implement drm_bridge_funcs op with
  117. * the help of these rough rules:
  118. *
  119. * pre_enable: this contains things needed to be done for the bridge before
  120. * its clock and timings are enabled by its source. For a bridge, its source
  121. * is generally the encoder or bridge just before it in the encoder chain.
  122. *
  123. * enable: this contains things needed to be done for the bridge once its
  124. * source is enabled. In other words, enable is called once the source is
  125. * ready with clock and timing needed by the bridge.
  126. *
  127. * disable: this contains things needed to be done for the bridge assuming
  128. * that its source is still enabled, i.e. clock and timings are still on.
  129. *
  130. * post_disable: this contains things needed to be done for the bridge once
  131. * its source is disabled, i.e. once clocks and timings are off.
  132. *
  133. * mode_fixup: this should fixup the given mode for the bridge. It is called
  134. * after the encoder's mode fixup. mode_fixup can also reject a mode completely
  135. * if it's unsuitable for the hardware.
  136. *
  137. * mode_set: this sets up the mode for the bridge. It assumes that its source
  138. * (an encoder or a bridge) has set the mode too.
  139. */
  140. /**
  141. * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the
  142. * encoder chain
  143. * @bridge: bridge control structure
  144. * @mode: desired mode to be set for the bridge
  145. * @adjusted_mode: updated mode that works for this bridge
  146. *
  147. * Calls 'mode_fixup' drm_bridge_funcs op for all the bridges in the
  148. * encoder chain, starting from the first bridge to the last.
  149. *
  150. * Note: the bridge passed should be the one closest to the encoder
  151. *
  152. * RETURNS:
  153. * true on success, false on failure
  154. */
  155. bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
  156. const struct drm_display_mode *mode,
  157. struct drm_display_mode *adjusted_mode)
  158. {
  159. bool ret = true;
  160. if (!bridge)
  161. return true;
  162. if (bridge->funcs->mode_fixup)
  163. ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode);
  164. ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode);
  165. return ret;
  166. }
  167. EXPORT_SYMBOL(drm_bridge_mode_fixup);
  168. /**
  169. * drm_bridge_disable - calls 'disable' drm_bridge_funcs op for all
  170. * bridges in the encoder chain.
  171. * @bridge: bridge control structure
  172. *
  173. * Calls 'disable' drm_bridge_funcs op for all the bridges in the encoder
  174. * chain, starting from the last bridge to the first. These are called before
  175. * calling the encoder's prepare op.
  176. *
  177. * Note: the bridge passed should be the one closest to the encoder
  178. */
  179. void drm_bridge_disable(struct drm_bridge *bridge)
  180. {
  181. if (!bridge)
  182. return;
  183. drm_bridge_disable(bridge->next);
  184. bridge->funcs->disable(bridge);
  185. }
  186. EXPORT_SYMBOL(drm_bridge_disable);
  187. /**
  188. * drm_bridge_post_disable - calls 'post_disable' drm_bridge_funcs op for
  189. * all bridges in the encoder chain.
  190. * @bridge: bridge control structure
  191. *
  192. * Calls 'post_disable' drm_bridge_funcs op for all the bridges in the
  193. * encoder chain, starting from the first bridge to the last. These are called
  194. * after completing the encoder's prepare op.
  195. *
  196. * Note: the bridge passed should be the one closest to the encoder
  197. */
  198. void drm_bridge_post_disable(struct drm_bridge *bridge)
  199. {
  200. if (!bridge)
  201. return;
  202. bridge->funcs->post_disable(bridge);
  203. drm_bridge_post_disable(bridge->next);
  204. }
  205. EXPORT_SYMBOL(drm_bridge_post_disable);
  206. /**
  207. * drm_bridge_mode_set - set proposed mode for all bridges in the
  208. * encoder chain
  209. * @bridge: bridge control structure
  210. * @mode: desired mode to be set for the bridge
  211. * @adjusted_mode: updated mode that works for this bridge
  212. *
  213. * Calls 'mode_set' drm_bridge_funcs op for all the bridges in the
  214. * encoder chain, starting from the first bridge to the last.
  215. *
  216. * Note: the bridge passed should be the one closest to the encoder
  217. */
  218. void drm_bridge_mode_set(struct drm_bridge *bridge,
  219. struct drm_display_mode *mode,
  220. struct drm_display_mode *adjusted_mode)
  221. {
  222. if (!bridge)
  223. return;
  224. if (bridge->funcs->mode_set)
  225. bridge->funcs->mode_set(bridge, mode, adjusted_mode);
  226. drm_bridge_mode_set(bridge->next, mode, adjusted_mode);
  227. }
  228. EXPORT_SYMBOL(drm_bridge_mode_set);
  229. /**
  230. * drm_bridge_pre_enable - calls 'pre_enable' drm_bridge_funcs op for all
  231. * bridges in the encoder chain.
  232. * @bridge: bridge control structure
  233. *
  234. * Calls 'pre_enable' drm_bridge_funcs op for all the bridges in the encoder
  235. * chain, starting from the last bridge to the first. These are called
  236. * before calling the encoder's commit op.
  237. *
  238. * Note: the bridge passed should be the one closest to the encoder
  239. */
  240. void drm_bridge_pre_enable(struct drm_bridge *bridge)
  241. {
  242. if (!bridge)
  243. return;
  244. drm_bridge_pre_enable(bridge->next);
  245. bridge->funcs->pre_enable(bridge);
  246. }
  247. EXPORT_SYMBOL(drm_bridge_pre_enable);
  248. /**
  249. * drm_bridge_enable - calls 'enable' drm_bridge_funcs op for all bridges
  250. * in the encoder chain.
  251. * @bridge: bridge control structure
  252. *
  253. * Calls 'enable' drm_bridge_funcs op for all the bridges in the encoder
  254. * chain, starting from the first bridge to the last. These are called
  255. * after completing the encoder's commit op.
  256. *
  257. * Note that the bridge passed should be the one closest to the encoder
  258. */
  259. void drm_bridge_enable(struct drm_bridge *bridge)
  260. {
  261. if (!bridge)
  262. return;
  263. bridge->funcs->enable(bridge);
  264. drm_bridge_enable(bridge->next);
  265. }
  266. EXPORT_SYMBOL(drm_bridge_enable);
  267. #ifdef CONFIG_OF
  268. /**
  269. * of_drm_find_bridge - find the bridge corresponding to the device node in
  270. * the global bridge list
  271. *
  272. * @np: device node
  273. *
  274. * RETURNS:
  275. * drm_bridge control struct on success, NULL on failure
  276. */
  277. struct drm_bridge *of_drm_find_bridge(struct device_node *np)
  278. {
  279. struct drm_bridge *bridge;
  280. mutex_lock(&bridge_lock);
  281. list_for_each_entry(bridge, &bridge_list, list) {
  282. if (bridge->of_node == np) {
  283. mutex_unlock(&bridge_lock);
  284. return bridge;
  285. }
  286. }
  287. mutex_unlock(&bridge_lock);
  288. return NULL;
  289. }
  290. EXPORT_SYMBOL(of_drm_find_bridge);
  291. #endif
  292. MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
  293. MODULE_DESCRIPTION("DRM bridge infrastructure");
  294. MODULE_LICENSE("GPL and additional rights");