drm_bridge.c 44 KB

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