drm_bridge.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. void drm_bridge_add(struct drm_bridge *bridge)
  66. {
  67. mutex_lock(&bridge_lock);
  68. list_add_tail(&bridge->list, &bridge_list);
  69. mutex_unlock(&bridge_lock);
  70. }
  71. EXPORT_SYMBOL(drm_bridge_add);
  72. /**
  73. * drm_bridge_remove - remove the given bridge from the global bridge list
  74. *
  75. * @bridge: bridge control structure
  76. */
  77. void drm_bridge_remove(struct drm_bridge *bridge)
  78. {
  79. mutex_lock(&bridge_lock);
  80. list_del_init(&bridge->list);
  81. mutex_unlock(&bridge_lock);
  82. }
  83. EXPORT_SYMBOL(drm_bridge_remove);
  84. /**
  85. * drm_bridge_attach - attach the bridge to an encoder's chain
  86. *
  87. * @encoder: DRM encoder
  88. * @bridge: bridge to attach
  89. * @previous: previous bridge in the chain (optional)
  90. *
  91. * Called by a kms driver to link the bridge to an encoder's chain. The previous
  92. * argument specifies the previous bridge in the chain. If NULL, the bridge is
  93. * linked directly at the encoder's output. Otherwise it is linked at the
  94. * previous bridge's output.
  95. *
  96. * If non-NULL the previous bridge must be already attached by a call to this
  97. * function.
  98. *
  99. * RETURNS:
  100. * Zero on success, error code on failure
  101. */
  102. int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
  103. struct drm_bridge *previous)
  104. {
  105. int ret;
  106. if (!encoder || !bridge)
  107. return -EINVAL;
  108. if (previous && (!previous->dev || previous->encoder != encoder))
  109. return -EINVAL;
  110. if (bridge->dev)
  111. return -EBUSY;
  112. bridge->dev = encoder->dev;
  113. bridge->encoder = encoder;
  114. if (bridge->funcs->attach) {
  115. ret = bridge->funcs->attach(bridge);
  116. if (ret < 0) {
  117. bridge->dev = NULL;
  118. bridge->encoder = NULL;
  119. return ret;
  120. }
  121. }
  122. if (previous)
  123. previous->next = bridge;
  124. else
  125. encoder->bridge = bridge;
  126. return 0;
  127. }
  128. EXPORT_SYMBOL(drm_bridge_attach);
  129. void drm_bridge_detach(struct drm_bridge *bridge)
  130. {
  131. if (WARN_ON(!bridge))
  132. return;
  133. if (WARN_ON(!bridge->dev))
  134. return;
  135. if (bridge->funcs->detach)
  136. bridge->funcs->detach(bridge);
  137. bridge->dev = NULL;
  138. }
  139. /**
  140. * DOC: bridge callbacks
  141. *
  142. * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM
  143. * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c
  144. * These helpers call a specific &drm_bridge_funcs op for all the bridges
  145. * during encoder configuration.
  146. *
  147. * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
  148. */
  149. /**
  150. * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the
  151. * encoder chain
  152. * @bridge: bridge control structure
  153. * @mode: desired mode to be set for the bridge
  154. * @adjusted_mode: updated mode that works for this bridge
  155. *
  156. * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
  157. * encoder chain, starting from the first bridge to the last.
  158. *
  159. * Note: the bridge passed should be the one closest to the encoder
  160. *
  161. * RETURNS:
  162. * true on success, false on failure
  163. */
  164. bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
  165. const struct drm_display_mode *mode,
  166. struct drm_display_mode *adjusted_mode)
  167. {
  168. bool ret = true;
  169. if (!bridge)
  170. return true;
  171. if (bridge->funcs->mode_fixup)
  172. ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode);
  173. ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode);
  174. return ret;
  175. }
  176. EXPORT_SYMBOL(drm_bridge_mode_fixup);
  177. /**
  178. * drm_bridge_mode_valid - validate the mode against all bridges in the
  179. * encoder chain.
  180. * @bridge: bridge control structure
  181. * @mode: desired mode to be validated
  182. *
  183. * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
  184. * chain, starting from the first bridge to the last. If at least one bridge
  185. * does not accept the mode the function returns the error code.
  186. *
  187. * Note: the bridge passed should be the one closest to the encoder.
  188. *
  189. * RETURNS:
  190. * MODE_OK on success, drm_mode_status Enum error code on failure
  191. */
  192. enum drm_mode_status drm_bridge_mode_valid(struct drm_bridge *bridge,
  193. const struct drm_display_mode *mode)
  194. {
  195. enum drm_mode_status ret = MODE_OK;
  196. if (!bridge)
  197. return ret;
  198. if (bridge->funcs->mode_valid)
  199. ret = bridge->funcs->mode_valid(bridge, mode);
  200. if (ret != MODE_OK)
  201. return ret;
  202. return drm_bridge_mode_valid(bridge->next, mode);
  203. }
  204. EXPORT_SYMBOL(drm_bridge_mode_valid);
  205. /**
  206. * drm_bridge_disable - disables all bridges in the encoder chain
  207. * @bridge: bridge control structure
  208. *
  209. * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
  210. * chain, starting from the last bridge to the first. These are called before
  211. * calling the encoder's prepare op.
  212. *
  213. * Note: the bridge passed should be the one closest to the encoder
  214. */
  215. void drm_bridge_disable(struct drm_bridge *bridge)
  216. {
  217. if (!bridge)
  218. return;
  219. drm_bridge_disable(bridge->next);
  220. if (bridge->funcs->disable)
  221. bridge->funcs->disable(bridge);
  222. }
  223. EXPORT_SYMBOL(drm_bridge_disable);
  224. /**
  225. * drm_bridge_post_disable - cleans up after disabling all bridges in the encoder chain
  226. * @bridge: bridge control structure
  227. *
  228. * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
  229. * encoder chain, starting from the first bridge to the last. These are called
  230. * after completing the encoder's prepare op.
  231. *
  232. * Note: the bridge passed should be the one closest to the encoder
  233. */
  234. void drm_bridge_post_disable(struct drm_bridge *bridge)
  235. {
  236. if (!bridge)
  237. return;
  238. if (bridge->funcs->post_disable)
  239. bridge->funcs->post_disable(bridge);
  240. drm_bridge_post_disable(bridge->next);
  241. }
  242. EXPORT_SYMBOL(drm_bridge_post_disable);
  243. /**
  244. * drm_bridge_mode_set - set proposed mode for all bridges in the
  245. * encoder chain
  246. * @bridge: bridge control structure
  247. * @mode: desired mode to be set for the bridge
  248. * @adjusted_mode: updated mode that works for this bridge
  249. *
  250. * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
  251. * encoder chain, starting from the first bridge to the last.
  252. *
  253. * Note: the bridge passed should be the one closest to the encoder
  254. */
  255. void drm_bridge_mode_set(struct drm_bridge *bridge,
  256. struct drm_display_mode *mode,
  257. struct drm_display_mode *adjusted_mode)
  258. {
  259. if (!bridge)
  260. return;
  261. if (bridge->funcs->mode_set)
  262. bridge->funcs->mode_set(bridge, mode, adjusted_mode);
  263. drm_bridge_mode_set(bridge->next, mode, adjusted_mode);
  264. }
  265. EXPORT_SYMBOL(drm_bridge_mode_set);
  266. /**
  267. * drm_bridge_pre_enable - prepares for enabling all
  268. * bridges in the encoder chain
  269. * @bridge: bridge control structure
  270. *
  271. * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
  272. * chain, starting from the last bridge to the first. These are called
  273. * before calling the encoder's commit op.
  274. *
  275. * Note: the bridge passed should be the one closest to the encoder
  276. */
  277. void drm_bridge_pre_enable(struct drm_bridge *bridge)
  278. {
  279. if (!bridge)
  280. return;
  281. drm_bridge_pre_enable(bridge->next);
  282. if (bridge->funcs->pre_enable)
  283. bridge->funcs->pre_enable(bridge);
  284. }
  285. EXPORT_SYMBOL(drm_bridge_pre_enable);
  286. /**
  287. * drm_bridge_enable - enables all bridges in the encoder chain
  288. * @bridge: bridge control structure
  289. *
  290. * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
  291. * chain, starting from the first bridge to the last. These are called
  292. * after completing the encoder's commit op.
  293. *
  294. * Note that the bridge passed should be the one closest to the encoder
  295. */
  296. void drm_bridge_enable(struct drm_bridge *bridge)
  297. {
  298. if (!bridge)
  299. return;
  300. if (bridge->funcs->enable)
  301. bridge->funcs->enable(bridge);
  302. drm_bridge_enable(bridge->next);
  303. }
  304. EXPORT_SYMBOL(drm_bridge_enable);
  305. #ifdef CONFIG_OF
  306. /**
  307. * of_drm_find_bridge - find the bridge corresponding to the device node in
  308. * the global bridge list
  309. *
  310. * @np: device node
  311. *
  312. * RETURNS:
  313. * drm_bridge control struct on success, NULL on failure
  314. */
  315. struct drm_bridge *of_drm_find_bridge(struct device_node *np)
  316. {
  317. struct drm_bridge *bridge;
  318. mutex_lock(&bridge_lock);
  319. list_for_each_entry(bridge, &bridge_list, list) {
  320. if (bridge->of_node == np) {
  321. mutex_unlock(&bridge_lock);
  322. return bridge;
  323. }
  324. }
  325. mutex_unlock(&bridge_lock);
  326. return NULL;
  327. }
  328. EXPORT_SYMBOL(of_drm_find_bridge);
  329. #endif
  330. MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
  331. MODULE_DESCRIPTION("DRM bridge infrastructure");
  332. MODULE_LICENSE("GPL and additional rights");