drm_bridge.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #include <drm/drm_encoder.h>
  28. /**
  29. * DOC: overview
  30. *
  31. * struct &drm_bridge represents a device that hangs on to an encoder. These are
  32. * handy when a regular &drm_encoder entity isn't enough to represent the entire
  33. * encoder chain.
  34. *
  35. * A bridge is always attached to a single &drm_encoder at a time, but can be
  36. * either connected to it directly, or through an intermediate bridge::
  37. *
  38. * encoder ---> bridge B ---> bridge A
  39. *
  40. * Here, the output of the encoder feeds to bridge B, and that furthers feeds to
  41. * bridge A.
  42. *
  43. * The driver using the bridge is responsible to make the associations between
  44. * the encoder and bridges. Once these links are made, the bridges will
  45. * participate along with encoder functions to perform mode_set/enable/disable
  46. * through the ops provided in &drm_bridge_funcs.
  47. *
  48. * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes,
  49. * CRTCs, encoders or connectors and hence are not visible to userspace. They
  50. * just provide additional hooks to get the desired output at the end of the
  51. * encoder chain.
  52. *
  53. * Bridges can also be chained up using the next pointer in struct &drm_bridge.
  54. *
  55. * Both legacy CRTC helpers and the new atomic modeset helpers support bridges.
  56. */
  57. static DEFINE_MUTEX(bridge_lock);
  58. static LIST_HEAD(bridge_list);
  59. /**
  60. * drm_bridge_add - add the given bridge to the global bridge list
  61. *
  62. * @bridge: bridge control structure
  63. *
  64. * RETURNS:
  65. * Unconditionally returns Zero.
  66. */
  67. int drm_bridge_add(struct drm_bridge *bridge)
  68. {
  69. mutex_lock(&bridge_lock);
  70. list_add_tail(&bridge->list, &bridge_list);
  71. mutex_unlock(&bridge_lock);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(drm_bridge_add);
  75. /**
  76. * drm_bridge_remove - remove the given bridge from the global bridge list
  77. *
  78. * @bridge: bridge control structure
  79. */
  80. void drm_bridge_remove(struct drm_bridge *bridge)
  81. {
  82. mutex_lock(&bridge_lock);
  83. list_del_init(&bridge->list);
  84. mutex_unlock(&bridge_lock);
  85. }
  86. EXPORT_SYMBOL(drm_bridge_remove);
  87. /**
  88. * drm_bridge_attach - attach the bridge to an encoder's chain
  89. *
  90. * @encoder: DRM encoder
  91. * @bridge: bridge to attach
  92. * @previous: previous bridge in the chain (optional)
  93. *
  94. * Called by a kms driver to link the bridge to an encoder's chain. The previous
  95. * argument specifies the previous bridge in the chain. If NULL, the bridge is
  96. * linked directly at the encoder's output. Otherwise it is linked at the
  97. * previous bridge's output.
  98. *
  99. * If non-NULL the previous bridge must be already attached by a call to this
  100. * function.
  101. *
  102. * RETURNS:
  103. * Zero on success, error code on failure
  104. */
  105. int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
  106. struct drm_bridge *previous)
  107. {
  108. int ret;
  109. if (!encoder || !bridge)
  110. return -EINVAL;
  111. if (previous && (!previous->dev || previous->encoder != encoder))
  112. return -EINVAL;
  113. if (bridge->dev)
  114. return -EBUSY;
  115. bridge->dev = encoder->dev;
  116. bridge->encoder = encoder;
  117. if (bridge->funcs->attach) {
  118. ret = bridge->funcs->attach(bridge);
  119. if (ret < 0) {
  120. bridge->dev = NULL;
  121. bridge->encoder = NULL;
  122. return ret;
  123. }
  124. }
  125. if (previous)
  126. previous->next = bridge;
  127. else
  128. encoder->bridge = bridge;
  129. return 0;
  130. }
  131. EXPORT_SYMBOL(drm_bridge_attach);
  132. /**
  133. * drm_bridge_detach - deassociate given bridge from its DRM device
  134. *
  135. * @bridge: bridge control structure
  136. *
  137. * Called by a kms driver to unlink the given bridge from its DRM device.
  138. *
  139. * Note that tearing down links between the bridge and our encoder/bridge
  140. * objects needs to be handled by the kms driver itself.
  141. */
  142. void drm_bridge_detach(struct drm_bridge *bridge)
  143. {
  144. if (WARN_ON(!bridge))
  145. return;
  146. if (WARN_ON(!bridge->dev))
  147. return;
  148. if (bridge->funcs->detach)
  149. bridge->funcs->detach(bridge);
  150. bridge->dev = NULL;
  151. }
  152. EXPORT_SYMBOL(drm_bridge_detach);
  153. /**
  154. * DOC: bridge callbacks
  155. *
  156. * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM
  157. * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c
  158. * These helpers call a specific &drm_bridge_funcs op for all the bridges
  159. * during encoder configuration.
  160. *
  161. * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
  162. */
  163. /**
  164. * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the
  165. * encoder chain
  166. * @bridge: bridge control structure
  167. * @mode: desired mode to be set for the bridge
  168. * @adjusted_mode: updated mode that works for this bridge
  169. *
  170. * Calls ->mode_fixup() &drm_bridge_funcs op for all the bridges in the
  171. * encoder chain, starting from the first bridge to the last.
  172. *
  173. * Note: the bridge passed should be the one closest to the encoder
  174. *
  175. * RETURNS:
  176. * true on success, false on failure
  177. */
  178. bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
  179. const struct drm_display_mode *mode,
  180. struct drm_display_mode *adjusted_mode)
  181. {
  182. bool ret = true;
  183. if (!bridge)
  184. return true;
  185. if (bridge->funcs->mode_fixup)
  186. ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode);
  187. ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode);
  188. return ret;
  189. }
  190. EXPORT_SYMBOL(drm_bridge_mode_fixup);
  191. /**
  192. * drm_bridge_disable - calls ->disable() &drm_bridge_funcs op for all
  193. * bridges in the encoder chain.
  194. * @bridge: bridge control structure
  195. *
  196. * Calls ->disable() &drm_bridge_funcs op for all the bridges in the encoder
  197. * chain, starting from the last bridge to the first. These are called before
  198. * calling the encoder's prepare op.
  199. *
  200. * Note: the bridge passed should be the one closest to the encoder
  201. */
  202. void drm_bridge_disable(struct drm_bridge *bridge)
  203. {
  204. if (!bridge)
  205. return;
  206. drm_bridge_disable(bridge->next);
  207. if (bridge->funcs->disable)
  208. bridge->funcs->disable(bridge);
  209. }
  210. EXPORT_SYMBOL(drm_bridge_disable);
  211. /**
  212. * drm_bridge_post_disable - calls ->post_disable() &drm_bridge_funcs op for
  213. * all bridges in the encoder chain.
  214. * @bridge: bridge control structure
  215. *
  216. * Calls ->post_disable() &drm_bridge_funcs op for all the bridges in the
  217. * encoder chain, starting from the first bridge to the last. These are called
  218. * after completing the encoder's prepare op.
  219. *
  220. * Note: the bridge passed should be the one closest to the encoder
  221. */
  222. void drm_bridge_post_disable(struct drm_bridge *bridge)
  223. {
  224. if (!bridge)
  225. return;
  226. if (bridge->funcs->post_disable)
  227. bridge->funcs->post_disable(bridge);
  228. drm_bridge_post_disable(bridge->next);
  229. }
  230. EXPORT_SYMBOL(drm_bridge_post_disable);
  231. /**
  232. * drm_bridge_mode_set - set proposed mode for all bridges in the
  233. * encoder chain
  234. * @bridge: bridge control structure
  235. * @mode: desired mode to be set for the bridge
  236. * @adjusted_mode: updated mode that works for this bridge
  237. *
  238. * Calls ->mode_set() &drm_bridge_funcs op for all the bridges in the
  239. * encoder chain, starting from the first bridge to the last.
  240. *
  241. * Note: the bridge passed should be the one closest to the encoder
  242. */
  243. void drm_bridge_mode_set(struct drm_bridge *bridge,
  244. struct drm_display_mode *mode,
  245. struct drm_display_mode *adjusted_mode)
  246. {
  247. if (!bridge)
  248. return;
  249. if (bridge->funcs->mode_set)
  250. bridge->funcs->mode_set(bridge, mode, adjusted_mode);
  251. drm_bridge_mode_set(bridge->next, mode, adjusted_mode);
  252. }
  253. EXPORT_SYMBOL(drm_bridge_mode_set);
  254. /**
  255. * drm_bridge_pre_enable - calls ->pre_enable() &drm_bridge_funcs op for all
  256. * bridges in the encoder chain.
  257. * @bridge: bridge control structure
  258. *
  259. * Calls ->pre_enable() &drm_bridge_funcs op for all the bridges in the encoder
  260. * chain, starting from the last bridge to the first. These are called
  261. * before calling the encoder's commit op.
  262. *
  263. * Note: the bridge passed should be the one closest to the encoder
  264. */
  265. void drm_bridge_pre_enable(struct drm_bridge *bridge)
  266. {
  267. if (!bridge)
  268. return;
  269. drm_bridge_pre_enable(bridge->next);
  270. if (bridge->funcs->pre_enable)
  271. bridge->funcs->pre_enable(bridge);
  272. }
  273. EXPORT_SYMBOL(drm_bridge_pre_enable);
  274. /**
  275. * drm_bridge_enable - calls ->enable() &drm_bridge_funcs op for all bridges
  276. * in the encoder chain.
  277. * @bridge: bridge control structure
  278. *
  279. * Calls ->enable() &drm_bridge_funcs op for all the bridges in the encoder
  280. * chain, starting from the first bridge to the last. These are called
  281. * after completing the encoder's commit op.
  282. *
  283. * Note that the bridge passed should be the one closest to the encoder
  284. */
  285. void drm_bridge_enable(struct drm_bridge *bridge)
  286. {
  287. if (!bridge)
  288. return;
  289. if (bridge->funcs->enable)
  290. bridge->funcs->enable(bridge);
  291. drm_bridge_enable(bridge->next);
  292. }
  293. EXPORT_SYMBOL(drm_bridge_enable);
  294. #ifdef CONFIG_OF
  295. /**
  296. * of_drm_find_bridge - find the bridge corresponding to the device node in
  297. * the global bridge list
  298. *
  299. * @np: device node
  300. *
  301. * RETURNS:
  302. * drm_bridge control struct on success, NULL on failure
  303. */
  304. struct drm_bridge *of_drm_find_bridge(struct device_node *np)
  305. {
  306. struct drm_bridge *bridge;
  307. mutex_lock(&bridge_lock);
  308. list_for_each_entry(bridge, &bridge_list, list) {
  309. if (bridge->of_node == np) {
  310. mutex_unlock(&bridge_lock);
  311. return bridge;
  312. }
  313. }
  314. mutex_unlock(&bridge_lock);
  315. return NULL;
  316. }
  317. EXPORT_SYMBOL(of_drm_find_bridge);
  318. #endif
  319. MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
  320. MODULE_DESCRIPTION("DRM bridge infrastructure");
  321. MODULE_LICENSE("GPL and additional rights");