drm_bridge.c 53 KB

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