drm_bridge.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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/media-bus-format.h>
  25. #include <linux/module.h>
  26. #include <linux/mutex.h>
  27. #include <drm/drm_atomic_state_helper.h>
  28. #include <drm/drm_bridge.h>
  29. #include <drm/drm_encoder.h>
  30. #include <drm/drm_of.h>
  31. #include <drm/drm_print.h>
  32. #include "drm_crtc_internal.h"
  33. /**
  34. * DOC: overview
  35. *
  36. * &struct drm_bridge represents a device that hangs on to an encoder. These are
  37. * handy when a regular &drm_encoder entity isn't enough to represent the entire
  38. * encoder chain.
  39. *
  40. * A bridge is always attached to a single &drm_encoder at a time, but can be
  41. * either connected to it directly, or through a chain of bridges::
  42. *
  43. * [ CRTC ---> ] Encoder ---> Bridge A ---> Bridge B
  44. *
  45. * Here, the output of the encoder feeds to bridge A, and that furthers feeds to
  46. * bridge B. Bridge chains can be arbitrarily long, and shall be fully linear:
  47. * Chaining multiple bridges to the output of a bridge, or the same bridge to
  48. * the output of different bridges, is not supported.
  49. *
  50. * &drm_bridge, like &drm_panel, aren't &drm_mode_object entities like planes,
  51. * CRTCs, encoders or connectors and hence are not visible to userspace. They
  52. * just provide additional hooks to get the desired output at the end of the
  53. * encoder chain.
  54. */
  55. /**
  56. * DOC: display driver integration
  57. *
  58. * Display drivers are responsible for linking encoders with the first bridge
  59. * in the chains. This is done by acquiring the appropriate bridge with
  60. * devm_drm_of_get_bridge(). Once acquired, the bridge shall be attached to the
  61. * encoder with a call to drm_bridge_attach().
  62. *
  63. * Bridges are responsible for linking themselves with the next bridge in the
  64. * chain, if any. This is done the same way as for encoders, with the call to
  65. * drm_bridge_attach() occurring in the &drm_bridge_funcs.attach operation.
  66. *
  67. * Once these links are created, the bridges can participate along with encoder
  68. * functions to perform mode validation and fixup (through
  69. * drm_bridge_chain_mode_valid() and drm_atomic_bridge_chain_check()), mode
  70. * setting (through drm_bridge_chain_mode_set()), enable (through
  71. * drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_enable())
  72. * and disable (through drm_atomic_bridge_chain_disable() and
  73. * drm_atomic_bridge_chain_post_disable()). Those functions call the
  74. * corresponding operations provided in &drm_bridge_funcs in sequence for all
  75. * bridges in the chain.
  76. *
  77. * For display drivers that use the atomic helpers
  78. * drm_atomic_helper_check_modeset(),
  79. * drm_atomic_helper_commit_modeset_enables() and
  80. * drm_atomic_helper_commit_modeset_disables() (either directly in hand-rolled
  81. * commit check and commit tail handlers, or through the higher-level
  82. * drm_atomic_helper_check() and drm_atomic_helper_commit_tail() or
  83. * drm_atomic_helper_commit_tail_rpm() helpers), this is done transparently and
  84. * requires no intervention from the driver. For other drivers, the relevant
  85. * DRM bridge chain functions shall be called manually.
  86. *
  87. * Bridges also participate in implementing the &drm_connector at the end of
  88. * the bridge chain. Display drivers may use the drm_bridge_connector_init()
  89. * helper to create the &drm_connector, or implement it manually on top of the
  90. * connector-related operations exposed by the bridge (see the overview
  91. * documentation of bridge operations for more details).
  92. */
  93. /**
  94. * DOC: special care dsi
  95. *
  96. * The interaction between the bridges and other frameworks involved in
  97. * the probing of the upstream driver and the bridge driver can be
  98. * challenging. Indeed, there's multiple cases that needs to be
  99. * considered:
  100. *
  101. * - The upstream driver doesn't use the component framework and isn't a
  102. * MIPI-DSI host. In this case, the bridge driver will probe at some
  103. * point and the upstream driver should try to probe again by returning
  104. * EPROBE_DEFER as long as the bridge driver hasn't probed.
  105. *
  106. * - The upstream driver doesn't use the component framework, but is a
  107. * MIPI-DSI host. The bridge device uses the MIPI-DCS commands to be
  108. * controlled. In this case, the bridge device is a child of the
  109. * display device and when it will probe it's assured that the display
  110. * device (and MIPI-DSI host) is present. The upstream driver will be
  111. * assured that the bridge driver is connected between the
  112. * &mipi_dsi_host_ops.attach and &mipi_dsi_host_ops.detach operations.
  113. * Therefore, it must run mipi_dsi_host_register() in its probe
  114. * function, and then run drm_bridge_attach() in its
  115. * &mipi_dsi_host_ops.attach hook.
  116. *
  117. * - The upstream driver uses the component framework and is a MIPI-DSI
  118. * host. The bridge device uses the MIPI-DCS commands to be
  119. * controlled. This is the same situation than above, and can run
  120. * mipi_dsi_host_register() in either its probe or bind hooks.
  121. *
  122. * - The upstream driver uses the component framework and is a MIPI-DSI
  123. * host. The bridge device uses a separate bus (such as I2C) to be
  124. * controlled. In this case, there's no correlation between the probe
  125. * of the bridge and upstream drivers, so care must be taken to avoid
  126. * an endless EPROBE_DEFER loop, with each driver waiting for the
  127. * other to probe.
  128. *
  129. * The ideal pattern to cover the last item (and all the others in the
  130. * MIPI-DSI host driver case) is to split the operations like this:
  131. *
  132. * - The MIPI-DSI host driver must run mipi_dsi_host_register() in its
  133. * probe hook. It will make sure that the MIPI-DSI host sticks around,
  134. * and that the driver's bind can be called.
  135. *
  136. * - In its probe hook, the bridge driver must try to find its MIPI-DSI
  137. * host, register as a MIPI-DSI device and attach the MIPI-DSI device
  138. * to its host. The bridge driver is now functional.
  139. *
  140. * - In its &struct mipi_dsi_host_ops.attach hook, the MIPI-DSI host can
  141. * now add its component. Its bind hook will now be called and since
  142. * the bridge driver is attached and registered, we can now look for
  143. * and attach it.
  144. *
  145. * At this point, we're now certain that both the upstream driver and
  146. * the bridge driver are functional and we can't have a deadlock-like
  147. * situation when probing.
  148. */
  149. static DEFINE_MUTEX(bridge_lock);
  150. static LIST_HEAD(bridge_list);
  151. /**
  152. * drm_bridge_add - add the given bridge to the global bridge list
  153. *
  154. * @bridge: bridge control structure
  155. */
  156. void drm_bridge_add(struct drm_bridge *bridge)
  157. {
  158. mutex_init(&bridge->hpd_mutex);
  159. mutex_lock(&bridge_lock);
  160. list_add_tail(&bridge->list, &bridge_list);
  161. mutex_unlock(&bridge_lock);
  162. }
  163. EXPORT_SYMBOL(drm_bridge_add);
  164. static void drm_bridge_remove_void(void *bridge)
  165. {
  166. drm_bridge_remove(bridge);
  167. }
  168. /**
  169. * devm_drm_bridge_add - devm managed version of drm_bridge_add()
  170. *
  171. * @dev: device to tie the bridge lifetime to
  172. * @bridge: bridge control structure
  173. *
  174. * This is the managed version of drm_bridge_add() which automatically
  175. * calls drm_bridge_remove() when @dev is unbound.
  176. *
  177. * Return: 0 if no error or negative error code.
  178. */
  179. int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge)
  180. {
  181. drm_bridge_add(bridge);
  182. return devm_add_action_or_reset(dev, drm_bridge_remove_void, bridge);
  183. }
  184. EXPORT_SYMBOL(devm_drm_bridge_add);
  185. /**
  186. * drm_bridge_remove - remove the given bridge from the global bridge list
  187. *
  188. * @bridge: bridge control structure
  189. */
  190. void drm_bridge_remove(struct drm_bridge *bridge)
  191. {
  192. mutex_lock(&bridge_lock);
  193. list_del_init(&bridge->list);
  194. mutex_unlock(&bridge_lock);
  195. mutex_destroy(&bridge->hpd_mutex);
  196. }
  197. EXPORT_SYMBOL(drm_bridge_remove);
  198. static struct drm_private_state *
  199. drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)
  200. {
  201. struct drm_bridge *bridge = drm_priv_to_bridge(obj);
  202. struct drm_bridge_state *state;
  203. state = bridge->funcs->atomic_duplicate_state(bridge);
  204. return state ? &state->base : NULL;
  205. }
  206. static void
  207. drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,
  208. struct drm_private_state *s)
  209. {
  210. struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
  211. struct drm_bridge *bridge = drm_priv_to_bridge(obj);
  212. bridge->funcs->atomic_destroy_state(bridge, state);
  213. }
  214. static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
  215. .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
  216. .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
  217. };
  218. /**
  219. * drm_bridge_attach - attach the bridge to an encoder's chain
  220. *
  221. * @encoder: DRM encoder
  222. * @bridge: bridge to attach
  223. * @previous: previous bridge in the chain (optional)
  224. * @flags: DRM_BRIDGE_ATTACH_* flags
  225. *
  226. * Called by a kms driver to link the bridge to an encoder's chain. The previous
  227. * argument specifies the previous bridge in the chain. If NULL, the bridge is
  228. * linked directly at the encoder's output. Otherwise it is linked at the
  229. * previous bridge's output.
  230. *
  231. * If non-NULL the previous bridge must be already attached by a call to this
  232. * function.
  233. *
  234. * Note that bridges attached to encoders are auto-detached during encoder
  235. * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
  236. * *not* be balanced with a drm_bridge_detach() in driver code.
  237. *
  238. * RETURNS:
  239. * Zero on success, error code on failure
  240. */
  241. int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
  242. struct drm_bridge *previous,
  243. enum drm_bridge_attach_flags flags)
  244. {
  245. int ret;
  246. if (!encoder || !bridge)
  247. return -EINVAL;
  248. if (previous && (!previous->dev || previous->encoder != encoder))
  249. return -EINVAL;
  250. if (bridge->dev)
  251. return -EBUSY;
  252. bridge->dev = encoder->dev;
  253. bridge->encoder = encoder;
  254. if (previous)
  255. list_add(&bridge->chain_node, &previous->chain_node);
  256. else
  257. list_add(&bridge->chain_node, &encoder->bridge_chain);
  258. if (bridge->funcs->attach) {
  259. ret = bridge->funcs->attach(bridge, flags);
  260. if (ret < 0)
  261. goto err_reset_bridge;
  262. }
  263. if (bridge->funcs->atomic_reset) {
  264. struct drm_bridge_state *state;
  265. state = bridge->funcs->atomic_reset(bridge);
  266. if (IS_ERR(state)) {
  267. ret = PTR_ERR(state);
  268. goto err_detach_bridge;
  269. }
  270. drm_atomic_private_obj_init(bridge->dev, &bridge->base,
  271. &state->base,
  272. &drm_bridge_priv_state_funcs);
  273. }
  274. return 0;
  275. err_detach_bridge:
  276. if (bridge->funcs->detach)
  277. bridge->funcs->detach(bridge);
  278. err_reset_bridge:
  279. bridge->dev = NULL;
  280. bridge->encoder = NULL;
  281. list_del(&bridge->chain_node);
  282. #ifdef CONFIG_OF
  283. DRM_ERROR("failed to attach bridge %pOF to encoder %s: %d\n",
  284. bridge->of_node, encoder->name, ret);
  285. #else
  286. DRM_ERROR("failed to attach bridge to encoder %s: %d\n",
  287. encoder->name, ret);
  288. #endif
  289. return ret;
  290. }
  291. EXPORT_SYMBOL(drm_bridge_attach);
  292. void drm_bridge_detach(struct drm_bridge *bridge)
  293. {
  294. if (WARN_ON(!bridge))
  295. return;
  296. if (WARN_ON(!bridge->dev))
  297. return;
  298. if (bridge->funcs->atomic_reset)
  299. drm_atomic_private_obj_fini(&bridge->base);
  300. if (bridge->funcs->detach)
  301. bridge->funcs->detach(bridge);
  302. list_del(&bridge->chain_node);
  303. bridge->dev = NULL;
  304. }
  305. /**
  306. * DOC: bridge operations
  307. *
  308. * Bridge drivers expose operations through the &drm_bridge_funcs structure.
  309. * The DRM internals (atomic and CRTC helpers) use the helpers defined in
  310. * drm_bridge.c to call bridge operations. Those operations are divided in
  311. * three big categories to support different parts of the bridge usage.
  312. *
  313. * - The encoder-related operations support control of the bridges in the
  314. * chain, and are roughly counterparts to the &drm_encoder_helper_funcs
  315. * operations. They are used by the legacy CRTC and the atomic modeset
  316. * helpers to perform mode validation, fixup and setting, and enable and
  317. * disable the bridge automatically.
  318. *
  319. * The enable and disable operations are split in
  320. * &drm_bridge_funcs.pre_enable, &drm_bridge_funcs.enable,
  321. * &drm_bridge_funcs.disable and &drm_bridge_funcs.post_disable to provide
  322. * finer-grained control.
  323. *
  324. * Bridge drivers may implement the legacy version of those operations, or
  325. * the atomic version (prefixed with atomic\_), in which case they shall also
  326. * implement the atomic state bookkeeping operations
  327. * (&drm_bridge_funcs.atomic_duplicate_state,
  328. * &drm_bridge_funcs.atomic_destroy_state and &drm_bridge_funcs.reset).
  329. * Mixing atomic and non-atomic versions of the operations is not supported.
  330. *
  331. * - The bus format negotiation operations
  332. * &drm_bridge_funcs.atomic_get_output_bus_fmts and
  333. * &drm_bridge_funcs.atomic_get_input_bus_fmts allow bridge drivers to
  334. * negotiate the formats transmitted between bridges in the chain when
  335. * multiple formats are supported. Negotiation for formats is performed
  336. * transparently for display drivers by the atomic modeset helpers. Only
  337. * atomic versions of those operations exist, bridge drivers that need to
  338. * implement them shall thus also implement the atomic version of the
  339. * encoder-related operations. This feature is not supported by the legacy
  340. * CRTC helpers.
  341. *
  342. * - The connector-related operations support implementing a &drm_connector
  343. * based on a chain of bridges. DRM bridges traditionally create a
  344. * &drm_connector for bridges meant to be used at the end of the chain. This
  345. * puts additional burden on bridge drivers, especially for bridges that may
  346. * be used in the middle of a chain or at the end of it. Furthermore, it
  347. * requires all operations of the &drm_connector to be handled by a single
  348. * bridge, which doesn't always match the hardware architecture.
  349. *
  350. * To simplify bridge drivers and make the connector implementation more
  351. * flexible, a new model allows bridges to unconditionally skip creation of
  352. * &drm_connector and instead expose &drm_bridge_funcs operations to support
  353. * an externally-implemented &drm_connector. Those operations are
  354. * &drm_bridge_funcs.detect, &drm_bridge_funcs.get_modes,
  355. * &drm_bridge_funcs.get_edid, &drm_bridge_funcs.hpd_notify,
  356. * &drm_bridge_funcs.hpd_enable and &drm_bridge_funcs.hpd_disable. When
  357. * implemented, display drivers shall create a &drm_connector instance for
  358. * each chain of bridges, and implement those connector instances based on
  359. * the bridge connector operations.
  360. *
  361. * Bridge drivers shall implement the connector-related operations for all
  362. * the features that the bridge hardware support. For instance, if a bridge
  363. * supports reading EDID, the &drm_bridge_funcs.get_edid shall be
  364. * implemented. This however doesn't mean that the DDC lines are wired to the
  365. * bridge on a particular platform, as they could also be connected to an I2C
  366. * controller of the SoC. Support for the connector-related operations on the
  367. * running platform is reported through the &drm_bridge.ops flags. Bridge
  368. * drivers shall detect which operations they can support on the platform
  369. * (usually this information is provided by ACPI or DT), and set the
  370. * &drm_bridge.ops flags for all supported operations. A flag shall only be
  371. * set if the corresponding &drm_bridge_funcs operation is implemented, but
  372. * an implemented operation doesn't necessarily imply that the corresponding
  373. * flag will be set. Display drivers shall use the &drm_bridge.ops flags to
  374. * decide which bridge to delegate a connector operation to. This mechanism
  375. * allows providing a single static const &drm_bridge_funcs instance in
  376. * bridge drivers, improving security by storing function pointers in
  377. * read-only memory.
  378. *
  379. * In order to ease transition, bridge drivers may support both the old and
  380. * new models by making connector creation optional and implementing the
  381. * connected-related bridge operations. Connector creation is then controlled
  382. * by the flags argument to the drm_bridge_attach() function. Display drivers
  383. * that support the new model and create connectors themselves shall set the
  384. * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag, and bridge drivers shall then skip
  385. * connector creation. For intermediate bridges in the chain, the flag shall
  386. * be passed to the drm_bridge_attach() call for the downstream bridge.
  387. * Bridge drivers that implement the new model only shall return an error
  388. * from their &drm_bridge_funcs.attach handler when the
  389. * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag is not set. New display drivers
  390. * should use the new model, and convert the bridge drivers they use if
  391. * needed, in order to gradually transition to the new model.
  392. */
  393. /**
  394. * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
  395. * encoder chain
  396. * @bridge: bridge control structure
  397. * @mode: desired mode to be set for the bridge
  398. * @adjusted_mode: updated mode that works for this bridge
  399. *
  400. * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
  401. * encoder chain, starting from the first bridge to the last.
  402. *
  403. * Note: the bridge passed should be the one closest to the encoder
  404. *
  405. * RETURNS:
  406. * true on success, false on failure
  407. */
  408. bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
  409. const struct drm_display_mode *mode,
  410. struct drm_display_mode *adjusted_mode)
  411. {
  412. struct drm_encoder *encoder;
  413. if (!bridge)
  414. return true;
  415. encoder = bridge->encoder;
  416. list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
  417. if (!bridge->funcs->mode_fixup)
  418. continue;
  419. if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
  420. return false;
  421. }
  422. return true;
  423. }
  424. EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
  425. /**
  426. * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
  427. * encoder chain.
  428. * @bridge: bridge control structure
  429. * @info: display info against which the mode shall be validated
  430. * @mode: desired mode to be validated
  431. *
  432. * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
  433. * chain, starting from the first bridge to the last. If at least one bridge
  434. * does not accept the mode the function returns the error code.
  435. *
  436. * Note: the bridge passed should be the one closest to the encoder.
  437. *
  438. * RETURNS:
  439. * MODE_OK on success, drm_mode_status Enum error code on failure
  440. */
  441. enum drm_mode_status
  442. drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
  443. const struct drm_display_info *info,
  444. const struct drm_display_mode *mode)
  445. {
  446. struct drm_encoder *encoder;
  447. if (!bridge)
  448. return MODE_OK;
  449. encoder = bridge->encoder;
  450. list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
  451. enum drm_mode_status ret;
  452. if (!bridge->funcs->mode_valid)
  453. continue;
  454. ret = bridge->funcs->mode_valid(bridge, info, mode);
  455. if (ret != MODE_OK)
  456. return ret;
  457. }
  458. return MODE_OK;
  459. }
  460. EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
  461. /**
  462. * drm_bridge_chain_disable - disables all bridges in the encoder chain
  463. * @bridge: bridge control structure
  464. *
  465. * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
  466. * chain, starting from the last bridge to the first. These are called before
  467. * calling the encoder's prepare op.
  468. *
  469. * Note: the bridge passed should be the one closest to the encoder
  470. */
  471. void drm_bridge_chain_disable(struct drm_bridge *bridge)
  472. {
  473. struct drm_encoder *encoder;
  474. struct drm_bridge *iter;
  475. if (!bridge)
  476. return;
  477. encoder = bridge->encoder;
  478. list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
  479. if (iter->funcs->disable)
  480. iter->funcs->disable(iter);
  481. if (iter == bridge)
  482. break;
  483. }
  484. }
  485. EXPORT_SYMBOL(drm_bridge_chain_disable);
  486. /**
  487. * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the
  488. * encoder chain
  489. * @bridge: bridge control structure
  490. *
  491. * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
  492. * encoder chain, starting from the first bridge to the last. These are called
  493. * after completing the encoder's prepare op.
  494. *
  495. * Note: the bridge passed should be the one closest to the encoder
  496. */
  497. void drm_bridge_chain_post_disable(struct drm_bridge *bridge)
  498. {
  499. struct drm_encoder *encoder;
  500. if (!bridge)
  501. return;
  502. encoder = bridge->encoder;
  503. list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
  504. if (bridge->funcs->post_disable)
  505. bridge->funcs->post_disable(bridge);
  506. }
  507. }
  508. EXPORT_SYMBOL(drm_bridge_chain_post_disable);
  509. /**
  510. * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
  511. * encoder chain
  512. * @bridge: bridge control structure
  513. * @mode: desired mode to be set for the encoder chain
  514. * @adjusted_mode: updated mode that works for this encoder chain
  515. *
  516. * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
  517. * encoder chain, starting from the first bridge to the last.
  518. *
  519. * Note: the bridge passed should be the one closest to the encoder
  520. */
  521. void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
  522. const struct drm_display_mode *mode,
  523. const struct drm_display_mode *adjusted_mode)
  524. {
  525. struct drm_encoder *encoder;
  526. if (!bridge)
  527. return;
  528. encoder = bridge->encoder;
  529. list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
  530. if (bridge->funcs->mode_set)
  531. bridge->funcs->mode_set(bridge, mode, adjusted_mode);
  532. }
  533. }
  534. EXPORT_SYMBOL(drm_bridge_chain_mode_set);
  535. /**
  536. * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the
  537. * encoder chain
  538. * @bridge: bridge control structure
  539. *
  540. * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
  541. * chain, starting from the last bridge to the first. These are called
  542. * before calling the encoder's commit op.
  543. *
  544. * Note: the bridge passed should be the one closest to the encoder
  545. */
  546. void drm_bridge_chain_pre_enable(struct drm_bridge *bridge)
  547. {
  548. struct drm_encoder *encoder;
  549. struct drm_bridge *iter;
  550. if (!bridge)
  551. return;
  552. encoder = bridge->encoder;
  553. list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
  554. if (iter->funcs->pre_enable)
  555. iter->funcs->pre_enable(iter);
  556. if (iter == bridge)
  557. break;
  558. }
  559. }
  560. EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
  561. /**
  562. * drm_bridge_chain_enable - enables all bridges in the encoder chain
  563. * @bridge: bridge control structure
  564. *
  565. * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
  566. * chain, starting from the first bridge to the last. These are called
  567. * after completing the encoder's commit op.
  568. *
  569. * Note that the bridge passed should be the one closest to the encoder
  570. */
  571. void drm_bridge_chain_enable(struct drm_bridge *bridge)
  572. {
  573. struct drm_encoder *encoder;
  574. if (!bridge)
  575. return;
  576. encoder = bridge->encoder;
  577. list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
  578. if (bridge->funcs->enable)
  579. bridge->funcs->enable(bridge);
  580. }
  581. }
  582. EXPORT_SYMBOL(drm_bridge_chain_enable);
  583. /**
  584. * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
  585. * @bridge: bridge control structure
  586. * @old_state: old atomic state
  587. *
  588. * Calls &drm_bridge_funcs.atomic_disable (falls back on
  589. * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
  590. * starting from the last bridge to the first. These are called before calling
  591. * &drm_encoder_helper_funcs.atomic_disable
  592. *
  593. * Note: the bridge passed should be the one closest to the encoder
  594. */
  595. void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
  596. struct drm_atomic_state *old_state)
  597. {
  598. struct drm_encoder *encoder;
  599. struct drm_bridge *iter;
  600. if (!bridge)
  601. return;
  602. encoder = bridge->encoder;
  603. list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
  604. if (iter->funcs->atomic_disable) {
  605. struct drm_bridge_state *old_bridge_state;
  606. old_bridge_state =
  607. drm_atomic_get_old_bridge_state(old_state,
  608. iter);
  609. if (WARN_ON(!old_bridge_state))
  610. return;
  611. iter->funcs->atomic_disable(iter, old_bridge_state);
  612. } else if (iter->funcs->disable) {
  613. iter->funcs->disable(iter);
  614. }
  615. if (iter == bridge)
  616. break;
  617. }
  618. }
  619. EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
  620. /**
  621. * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
  622. * in the encoder chain
  623. * @bridge: bridge control structure
  624. * @old_state: old atomic state
  625. *
  626. * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
  627. * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
  628. * starting from the first bridge to the last. These are called after completing
  629. * &drm_encoder_helper_funcs.atomic_disable
  630. *
  631. * Note: the bridge passed should be the one closest to the encoder
  632. */
  633. void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
  634. struct drm_atomic_state *old_state)
  635. {
  636. struct drm_encoder *encoder;
  637. if (!bridge)
  638. return;
  639. encoder = bridge->encoder;
  640. list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
  641. if (bridge->funcs->atomic_post_disable) {
  642. struct drm_bridge_state *old_bridge_state;
  643. old_bridge_state =
  644. drm_atomic_get_old_bridge_state(old_state,
  645. bridge);
  646. if (WARN_ON(!old_bridge_state))
  647. return;
  648. bridge->funcs->atomic_post_disable(bridge,
  649. old_bridge_state);
  650. } else if (bridge->funcs->post_disable) {
  651. bridge->funcs->post_disable(bridge);
  652. }
  653. }
  654. }
  655. EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
  656. /**
  657. * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
  658. * the encoder chain
  659. * @bridge: bridge control structure
  660. * @old_state: old atomic state
  661. *
  662. * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
  663. * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
  664. * starting from the last bridge to the first. These are called before calling
  665. * &drm_encoder_helper_funcs.atomic_enable
  666. *
  667. * Note: the bridge passed should be the one closest to the encoder
  668. */
  669. void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
  670. struct drm_atomic_state *old_state)
  671. {
  672. struct drm_encoder *encoder;
  673. struct drm_bridge *iter;
  674. if (!bridge)
  675. return;
  676. encoder = bridge->encoder;
  677. list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
  678. if (iter->funcs->atomic_pre_enable) {
  679. struct drm_bridge_state *old_bridge_state;
  680. old_bridge_state =
  681. drm_atomic_get_old_bridge_state(old_state,
  682. iter);
  683. if (WARN_ON(!old_bridge_state))
  684. return;
  685. iter->funcs->atomic_pre_enable(iter, old_bridge_state);
  686. } else if (iter->funcs->pre_enable) {
  687. iter->funcs->pre_enable(iter);
  688. }
  689. if (iter == bridge)
  690. break;
  691. }
  692. }
  693. EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
  694. /**
  695. * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
  696. * @bridge: bridge control structure
  697. * @old_state: old atomic state
  698. *
  699. * Calls &drm_bridge_funcs.atomic_enable (falls back on
  700. * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
  701. * starting from the first bridge to the last. These are called after completing
  702. * &drm_encoder_helper_funcs.atomic_enable
  703. *
  704. * Note: the bridge passed should be the one closest to the encoder
  705. */
  706. void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
  707. struct drm_atomic_state *old_state)
  708. {
  709. struct drm_encoder *encoder;
  710. if (!bridge)
  711. return;
  712. encoder = bridge->encoder;
  713. list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
  714. if (bridge->funcs->atomic_enable) {
  715. struct drm_bridge_state *old_bridge_state;
  716. old_bridge_state =
  717. drm_atomic_get_old_bridge_state(old_state,
  718. bridge);
  719. if (WARN_ON(!old_bridge_state))
  720. return;
  721. bridge->funcs->atomic_enable(bridge, old_bridge_state);
  722. } else if (bridge->funcs->enable) {
  723. bridge->funcs->enable(bridge);
  724. }
  725. }
  726. }
  727. EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
  728. static int drm_atomic_bridge_check(struct drm_bridge *bridge,
  729. struct drm_crtc_state *crtc_state,
  730. struct drm_connector_state *conn_state)
  731. {
  732. if (bridge->funcs->atomic_check) {
  733. struct drm_bridge_state *bridge_state;
  734. int ret;
  735. bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
  736. bridge);
  737. if (WARN_ON(!bridge_state))
  738. return -EINVAL;
  739. ret = bridge->funcs->atomic_check(bridge, bridge_state,
  740. crtc_state, conn_state);
  741. if (ret)
  742. return ret;
  743. } else if (bridge->funcs->mode_fixup) {
  744. if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode,
  745. &crtc_state->adjusted_mode))
  746. return -EINVAL;
  747. }
  748. return 0;
  749. }
  750. static int select_bus_fmt_recursive(struct drm_bridge *first_bridge,
  751. struct drm_bridge *cur_bridge,
  752. struct drm_crtc_state *crtc_state,
  753. struct drm_connector_state *conn_state,
  754. u32 out_bus_fmt)
  755. {
  756. unsigned int i, num_in_bus_fmts = 0;
  757. struct drm_bridge_state *cur_state;
  758. struct drm_bridge *prev_bridge;
  759. u32 *in_bus_fmts;
  760. int ret;
  761. prev_bridge = drm_bridge_get_prev_bridge(cur_bridge);
  762. cur_state = drm_atomic_get_new_bridge_state(crtc_state->state,
  763. cur_bridge);
  764. /*
  765. * If bus format negotiation is not supported by this bridge, let's
  766. * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and
  767. * hope that it can handle this situation gracefully (by providing
  768. * appropriate default values).
  769. */
  770. if (!cur_bridge->funcs->atomic_get_input_bus_fmts) {
  771. if (cur_bridge != first_bridge) {
  772. ret = select_bus_fmt_recursive(first_bridge,
  773. prev_bridge, crtc_state,
  774. conn_state,
  775. MEDIA_BUS_FMT_FIXED);
  776. if (ret)
  777. return ret;
  778. }
  779. /*
  780. * Driver does not implement the atomic state hooks, but that's
  781. * fine, as long as it does not access the bridge state.
  782. */
  783. if (cur_state) {
  784. cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
  785. cur_state->output_bus_cfg.format = out_bus_fmt;
  786. }
  787. return 0;
  788. }
  789. /*
  790. * If the driver implements ->atomic_get_input_bus_fmts() it
  791. * should also implement the atomic state hooks.
  792. */
  793. if (WARN_ON(!cur_state))
  794. return -EINVAL;
  795. in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge,
  796. cur_state,
  797. crtc_state,
  798. conn_state,
  799. out_bus_fmt,
  800. &num_in_bus_fmts);
  801. if (!num_in_bus_fmts)
  802. return -ENOTSUPP;
  803. else if (!in_bus_fmts)
  804. return -ENOMEM;
  805. if (first_bridge == cur_bridge) {
  806. cur_state->input_bus_cfg.format = in_bus_fmts[0];
  807. cur_state->output_bus_cfg.format = out_bus_fmt;
  808. kfree(in_bus_fmts);
  809. return 0;
  810. }
  811. for (i = 0; i < num_in_bus_fmts; i++) {
  812. ret = select_bus_fmt_recursive(first_bridge, prev_bridge,
  813. crtc_state, conn_state,
  814. in_bus_fmts[i]);
  815. if (ret != -ENOTSUPP)
  816. break;
  817. }
  818. if (!ret) {
  819. cur_state->input_bus_cfg.format = in_bus_fmts[i];
  820. cur_state->output_bus_cfg.format = out_bus_fmt;
  821. }
  822. kfree(in_bus_fmts);
  823. return ret;
  824. }
  825. /*
  826. * This function is called by &drm_atomic_bridge_chain_check() just before
  827. * calling &drm_bridge_funcs.atomic_check() on all elements of the chain.
  828. * It performs bus format negotiation between bridge elements. The negotiation
  829. * happens in reverse order, starting from the last element in the chain up to
  830. * @bridge.
  831. *
  832. * Negotiation starts by retrieving supported output bus formats on the last
  833. * bridge element and testing them one by one. The test is recursive, meaning
  834. * that for each tested output format, the whole chain will be walked backward,
  835. * and each element will have to choose an input bus format that can be
  836. * transcoded to the requested output format. When a bridge element does not
  837. * support transcoding into a specific output format -ENOTSUPP is returned and
  838. * the next bridge element will have to try a different format. If none of the
  839. * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail.
  840. *
  841. * This implementation is relying on
  842. * &drm_bridge_funcs.atomic_get_output_bus_fmts() and
  843. * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported
  844. * input/output formats.
  845. *
  846. * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by
  847. * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts()
  848. * tries a single format: &drm_connector.display_info.bus_formats[0] if
  849. * available, MEDIA_BUS_FMT_FIXED otherwise.
  850. *
  851. * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented,
  852. * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the
  853. * bridge element that lacks this hook and asks the previous element in the
  854. * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what
  855. * to do in that case (fail if they want to enforce bus format negotiation, or
  856. * provide a reasonable default if they need to support pipelines where not
  857. * all elements support bus format negotiation).
  858. */
  859. static int
  860. drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge,
  861. struct drm_crtc_state *crtc_state,
  862. struct drm_connector_state *conn_state)
  863. {
  864. struct drm_connector *conn = conn_state->connector;
  865. struct drm_encoder *encoder = bridge->encoder;
  866. struct drm_bridge_state *last_bridge_state;
  867. unsigned int i, num_out_bus_fmts = 0;
  868. struct drm_bridge *last_bridge;
  869. u32 *out_bus_fmts;
  870. int ret = 0;
  871. last_bridge = list_last_entry(&encoder->bridge_chain,
  872. struct drm_bridge, chain_node);
  873. last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
  874. last_bridge);
  875. if (last_bridge->funcs->atomic_get_output_bus_fmts) {
  876. const struct drm_bridge_funcs *funcs = last_bridge->funcs;
  877. /*
  878. * If the driver implements ->atomic_get_output_bus_fmts() it
  879. * should also implement the atomic state hooks.
  880. */
  881. if (WARN_ON(!last_bridge_state))
  882. return -EINVAL;
  883. out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge,
  884. last_bridge_state,
  885. crtc_state,
  886. conn_state,
  887. &num_out_bus_fmts);
  888. if (!num_out_bus_fmts)
  889. return -ENOTSUPP;
  890. else if (!out_bus_fmts)
  891. return -ENOMEM;
  892. } else {
  893. num_out_bus_fmts = 1;
  894. out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL);
  895. if (!out_bus_fmts)
  896. return -ENOMEM;
  897. if (conn->display_info.num_bus_formats &&
  898. conn->display_info.bus_formats)
  899. out_bus_fmts[0] = conn->display_info.bus_formats[0];
  900. else
  901. out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
  902. }
  903. for (i = 0; i < num_out_bus_fmts; i++) {
  904. ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state,
  905. conn_state, out_bus_fmts[i]);
  906. if (ret != -ENOTSUPP)
  907. break;
  908. }
  909. kfree(out_bus_fmts);
  910. return ret;
  911. }
  912. static void
  913. drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge,
  914. struct drm_connector *conn,
  915. struct drm_atomic_state *state)
  916. {
  917. struct drm_bridge_state *bridge_state, *next_bridge_state;
  918. struct drm_bridge *next_bridge;
  919. u32 output_flags = 0;
  920. bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
  921. /* No bridge state attached to this bridge => nothing to propagate. */
  922. if (!bridge_state)
  923. return;
  924. next_bridge = drm_bridge_get_next_bridge(bridge);
  925. /*
  926. * Let's try to apply the most common case here, that is, propagate
  927. * display_info flags for the last bridge, and propagate the input
  928. * flags of the next bridge element to the output end of the current
  929. * bridge when the bridge is not the last one.
  930. * There are exceptions to this rule, like when signal inversion is
  931. * happening at the board level, but that's something drivers can deal
  932. * with from their &drm_bridge_funcs.atomic_check() implementation by
  933. * simply overriding the flags value we've set here.
  934. */
  935. if (!next_bridge) {
  936. output_flags = conn->display_info.bus_flags;
  937. } else {
  938. next_bridge_state = drm_atomic_get_new_bridge_state(state,
  939. next_bridge);
  940. /*
  941. * No bridge state attached to the next bridge, just leave the
  942. * flags to 0.
  943. */
  944. if (next_bridge_state)
  945. output_flags = next_bridge_state->input_bus_cfg.flags;
  946. }
  947. bridge_state->output_bus_cfg.flags = output_flags;
  948. /*
  949. * Propagate the output flags to the input end of the bridge. Again, it's
  950. * not necessarily what all bridges want, but that's what most of them
  951. * do, and by doing that by default we avoid forcing drivers to
  952. * duplicate the "dummy propagation" logic.
  953. */
  954. bridge_state->input_bus_cfg.flags = output_flags;
  955. }
  956. /**
  957. * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain
  958. * @bridge: bridge control structure
  959. * @crtc_state: new CRTC state
  960. * @conn_state: new connector state
  961. *
  962. * First trigger a bus format negotiation before calling
  963. * &drm_bridge_funcs.atomic_check() (falls back on
  964. * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain,
  965. * starting from the last bridge to the first. These are called before calling
  966. * &drm_encoder_helper_funcs.atomic_check()
  967. *
  968. * RETURNS:
  969. * 0 on success, a negative error code on failure
  970. */
  971. int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,
  972. struct drm_crtc_state *crtc_state,
  973. struct drm_connector_state *conn_state)
  974. {
  975. struct drm_connector *conn = conn_state->connector;
  976. struct drm_encoder *encoder;
  977. struct drm_bridge *iter;
  978. int ret;
  979. if (!bridge)
  980. return 0;
  981. ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state,
  982. conn_state);
  983. if (ret)
  984. return ret;
  985. encoder = bridge->encoder;
  986. list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
  987. int ret;
  988. /*
  989. * Bus flags are propagated by default. If a bridge needs to
  990. * tweak the input bus flags for any reason, it should happen
  991. * in its &drm_bridge_funcs.atomic_check() implementation such
  992. * that preceding bridges in the chain can propagate the new
  993. * bus flags.
  994. */
  995. drm_atomic_bridge_propagate_bus_flags(iter, conn,
  996. crtc_state->state);
  997. ret = drm_atomic_bridge_check(iter, crtc_state, conn_state);
  998. if (ret)
  999. return ret;
  1000. if (iter == bridge)
  1001. break;
  1002. }
  1003. return 0;
  1004. }
  1005. EXPORT_SYMBOL(drm_atomic_bridge_chain_check);
  1006. /**
  1007. * drm_bridge_detect - check if anything is attached to the bridge output
  1008. * @bridge: bridge control structure
  1009. *
  1010. * If the bridge supports output detection, as reported by the
  1011. * DRM_BRIDGE_OP_DETECT bridge ops flag, call &drm_bridge_funcs.detect for the
  1012. * bridge and return the connection status. Otherwise return
  1013. * connector_status_unknown.
  1014. *
  1015. * RETURNS:
  1016. * The detection status on success, or connector_status_unknown if the bridge
  1017. * doesn't support output detection.
  1018. */
  1019. enum drm_connector_status drm_bridge_detect(struct drm_bridge *bridge)
  1020. {
  1021. if (!(bridge->ops & DRM_BRIDGE_OP_DETECT))
  1022. return connector_status_unknown;
  1023. return bridge->funcs->detect(bridge);
  1024. }
  1025. EXPORT_SYMBOL_GPL(drm_bridge_detect);
  1026. /**
  1027. * drm_bridge_get_modes - fill all modes currently valid for the sink into the
  1028. * @connector
  1029. * @bridge: bridge control structure
  1030. * @connector: the connector to fill with modes
  1031. *
  1032. * If the bridge supports output modes retrieval, as reported by the
  1033. * DRM_BRIDGE_OP_MODES bridge ops flag, call &drm_bridge_funcs.get_modes to
  1034. * fill the connector with all valid modes and return the number of modes
  1035. * added. Otherwise return 0.
  1036. *
  1037. * RETURNS:
  1038. * The number of modes added to the connector.
  1039. */
  1040. int drm_bridge_get_modes(struct drm_bridge *bridge,
  1041. struct drm_connector *connector)
  1042. {
  1043. if (!(bridge->ops & DRM_BRIDGE_OP_MODES))
  1044. return 0;
  1045. return bridge->funcs->get_modes(bridge, connector);
  1046. }
  1047. EXPORT_SYMBOL_GPL(drm_bridge_get_modes);
  1048. /**
  1049. * drm_bridge_get_edid - get the EDID data of the connected display
  1050. * @bridge: bridge control structure
  1051. * @connector: the connector to read EDID for
  1052. *
  1053. * If the bridge supports output EDID retrieval, as reported by the
  1054. * DRM_BRIDGE_OP_EDID bridge ops flag, call &drm_bridge_funcs.get_edid to
  1055. * get the EDID and return it. Otherwise return NULL.
  1056. *
  1057. * RETURNS:
  1058. * The retrieved EDID on success, or NULL otherwise.
  1059. */
  1060. struct edid *drm_bridge_get_edid(struct drm_bridge *bridge,
  1061. struct drm_connector *connector)
  1062. {
  1063. if (!(bridge->ops & DRM_BRIDGE_OP_EDID))
  1064. return NULL;
  1065. return bridge->funcs->get_edid(bridge, connector);
  1066. }
  1067. EXPORT_SYMBOL_GPL(drm_bridge_get_edid);
  1068. /**
  1069. * drm_bridge_hpd_enable - enable hot plug detection for the bridge
  1070. * @bridge: bridge control structure
  1071. * @cb: hot-plug detection callback
  1072. * @data: data to be passed to the hot-plug detection callback
  1073. *
  1074. * Call &drm_bridge_funcs.hpd_enable if implemented and register the given @cb
  1075. * and @data as hot plug notification callback. From now on the @cb will be
  1076. * called with @data when an output status change is detected by the bridge,
  1077. * until hot plug notification gets disabled with drm_bridge_hpd_disable().
  1078. *
  1079. * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
  1080. * bridge->ops. This function shall not be called when the flag is not set.
  1081. *
  1082. * Only one hot plug detection callback can be registered at a time, it is an
  1083. * error to call this function when hot plug detection is already enabled for
  1084. * the bridge.
  1085. */
  1086. void drm_bridge_hpd_enable(struct drm_bridge *bridge,
  1087. void (*cb)(void *data,
  1088. enum drm_connector_status status),
  1089. void *data)
  1090. {
  1091. if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
  1092. return;
  1093. mutex_lock(&bridge->hpd_mutex);
  1094. if (WARN(bridge->hpd_cb, "Hot plug detection already enabled\n"))
  1095. goto unlock;
  1096. bridge->hpd_cb = cb;
  1097. bridge->hpd_data = data;
  1098. if (bridge->funcs->hpd_enable)
  1099. bridge->funcs->hpd_enable(bridge);
  1100. unlock:
  1101. mutex_unlock(&bridge->hpd_mutex);
  1102. }
  1103. EXPORT_SYMBOL_GPL(drm_bridge_hpd_enable);
  1104. /**
  1105. * drm_bridge_hpd_disable - disable hot plug detection for the bridge
  1106. * @bridge: bridge control structure
  1107. *
  1108. * Call &drm_bridge_funcs.hpd_disable if implemented and unregister the hot
  1109. * plug detection callback previously registered with drm_bridge_hpd_enable().
  1110. * Once this function returns the callback will not be called by the bridge
  1111. * when an output status change occurs.
  1112. *
  1113. * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
  1114. * bridge->ops. This function shall not be called when the flag is not set.
  1115. */
  1116. void drm_bridge_hpd_disable(struct drm_bridge *bridge)
  1117. {
  1118. if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
  1119. return;
  1120. mutex_lock(&bridge->hpd_mutex);
  1121. if (bridge->funcs->hpd_disable)
  1122. bridge->funcs->hpd_disable(bridge);
  1123. bridge->hpd_cb = NULL;
  1124. bridge->hpd_data = NULL;
  1125. mutex_unlock(&bridge->hpd_mutex);
  1126. }
  1127. EXPORT_SYMBOL_GPL(drm_bridge_hpd_disable);
  1128. /**
  1129. * drm_bridge_hpd_notify - notify hot plug detection events
  1130. * @bridge: bridge control structure
  1131. * @status: output connection status
  1132. *
  1133. * Bridge drivers shall call this function to report hot plug events when they
  1134. * detect a change in the output status, when hot plug detection has been
  1135. * enabled by drm_bridge_hpd_enable().
  1136. *
  1137. * This function shall be called in a context that can sleep.
  1138. */
  1139. void drm_bridge_hpd_notify(struct drm_bridge *bridge,
  1140. enum drm_connector_status status)
  1141. {
  1142. mutex_lock(&bridge->hpd_mutex);
  1143. if (bridge->hpd_cb)
  1144. bridge->hpd_cb(bridge->hpd_data, status);
  1145. mutex_unlock(&bridge->hpd_mutex);
  1146. }
  1147. EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify);
  1148. #ifdef CONFIG_OF
  1149. /**
  1150. * of_drm_find_bridge - find the bridge corresponding to the device node in
  1151. * the global bridge list
  1152. *
  1153. * @np: device node
  1154. *
  1155. * RETURNS:
  1156. * drm_bridge control struct on success, NULL on failure
  1157. */
  1158. struct drm_bridge *of_drm_find_bridge(struct device_node *np)
  1159. {
  1160. struct drm_bridge *bridge;
  1161. mutex_lock(&bridge_lock);
  1162. list_for_each_entry(bridge, &bridge_list, list) {
  1163. if (bridge->of_node == np) {
  1164. mutex_unlock(&bridge_lock);
  1165. return bridge;
  1166. }
  1167. }
  1168. mutex_unlock(&bridge_lock);
  1169. return NULL;
  1170. }
  1171. EXPORT_SYMBOL(of_drm_find_bridge);
  1172. #endif
  1173. MODULE_AUTHOR("Ajay Kumar <[email protected]>");
  1174. MODULE_DESCRIPTION("DRM bridge infrastructure");
  1175. MODULE_LICENSE("GPL and additional rights");