drm_bridge.c 9.7 KB

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