2 * Copyright (c) 2014 Samsung Electronics Co., Ltd
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:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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.
24 #include <linux/err.h>
25 #include <linux/media-bus-format.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
29 #include <drm/drm_atomic_state_helper.h>
30 #include <drm/drm_bridge.h>
31 #include <drm/drm_debugfs.h>
32 #include <drm/drm_edid.h>
33 #include <drm/drm_encoder.h>
34 #include <drm/drm_file.h>
35 #include <drm/drm_of.h>
36 #include <drm/drm_print.h>
38 #include "drm_crtc_internal.h"
43 * &struct drm_bridge represents a device that hangs on to an encoder. These are
44 * handy when a regular &drm_encoder entity isn't enough to represent the entire
47 * A bridge is always attached to a single &drm_encoder at a time, but can be
48 * either connected to it directly, or through a chain of bridges::
50 * [ CRTC ---> ] Encoder ---> Bridge A ---> Bridge B
52 * Here, the output of the encoder feeds to bridge A, and that furthers feeds to
53 * bridge B. Bridge chains can be arbitrarily long, and shall be fully linear:
54 * Chaining multiple bridges to the output of a bridge, or the same bridge to
55 * the output of different bridges, is not supported.
57 * &drm_bridge, like &drm_panel, aren't &drm_mode_object entities like planes,
58 * CRTCs, encoders or connectors and hence are not visible to userspace. They
59 * just provide additional hooks to get the desired output at the end of the
64 * DOC: display driver integration
66 * Display drivers are responsible for linking encoders with the first bridge
67 * in the chains. This is done by acquiring the appropriate bridge with
68 * devm_drm_of_get_bridge(). Once acquired, the bridge shall be attached to the
69 * encoder with a call to drm_bridge_attach().
71 * Bridges are responsible for linking themselves with the next bridge in the
72 * chain, if any. This is done the same way as for encoders, with the call to
73 * drm_bridge_attach() occurring in the &drm_bridge_funcs.attach operation.
75 * Once these links are created, the bridges can participate along with encoder
76 * functions to perform mode validation and fixup (through
77 * drm_bridge_chain_mode_valid() and drm_atomic_bridge_chain_check()), mode
78 * setting (through drm_bridge_chain_mode_set()), enable (through
79 * drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_enable())
80 * and disable (through drm_atomic_bridge_chain_disable() and
81 * drm_atomic_bridge_chain_post_disable()). Those functions call the
82 * corresponding operations provided in &drm_bridge_funcs in sequence for all
83 * bridges in the chain.
85 * For display drivers that use the atomic helpers
86 * drm_atomic_helper_check_modeset(),
87 * drm_atomic_helper_commit_modeset_enables() and
88 * drm_atomic_helper_commit_modeset_disables() (either directly in hand-rolled
89 * commit check and commit tail handlers, or through the higher-level
90 * drm_atomic_helper_check() and drm_atomic_helper_commit_tail() or
91 * drm_atomic_helper_commit_tail_rpm() helpers), this is done transparently and
92 * requires no intervention from the driver. For other drivers, the relevant
93 * DRM bridge chain functions shall be called manually.
95 * Bridges also participate in implementing the &drm_connector at the end of
96 * the bridge chain. Display drivers may use the drm_bridge_connector_init()
97 * helper to create the &drm_connector, or implement it manually on top of the
98 * connector-related operations exposed by the bridge (see the overview
99 * documentation of bridge operations for more details).
103 * DOC: special care dsi
105 * The interaction between the bridges and other frameworks involved in
106 * the probing of the upstream driver and the bridge driver can be
107 * challenging. Indeed, there's multiple cases that needs to be
110 * - The upstream driver doesn't use the component framework and isn't a
111 * MIPI-DSI host. In this case, the bridge driver will probe at some
112 * point and the upstream driver should try to probe again by returning
113 * EPROBE_DEFER as long as the bridge driver hasn't probed.
115 * - The upstream driver doesn't use the component framework, but is a
116 * MIPI-DSI host. The bridge device uses the MIPI-DCS commands to be
117 * controlled. In this case, the bridge device is a child of the
118 * display device and when it will probe it's assured that the display
119 * device (and MIPI-DSI host) is present. The upstream driver will be
120 * assured that the bridge driver is connected between the
121 * &mipi_dsi_host_ops.attach and &mipi_dsi_host_ops.detach operations.
122 * Therefore, it must run mipi_dsi_host_register() in its probe
123 * function, and then run drm_bridge_attach() in its
124 * &mipi_dsi_host_ops.attach hook.
126 * - The upstream driver uses the component framework and is a MIPI-DSI
127 * host. The bridge device uses the MIPI-DCS commands to be
128 * controlled. This is the same situation than above, and can run
129 * mipi_dsi_host_register() in either its probe or bind hooks.
131 * - The upstream driver uses the component framework and is a MIPI-DSI
132 * host. The bridge device uses a separate bus (such as I2C) to be
133 * controlled. In this case, there's no correlation between the probe
134 * of the bridge and upstream drivers, so care must be taken to avoid
135 * an endless EPROBE_DEFER loop, with each driver waiting for the
138 * The ideal pattern to cover the last item (and all the others in the
139 * MIPI-DSI host driver case) is to split the operations like this:
141 * - The MIPI-DSI host driver must run mipi_dsi_host_register() in its
142 * probe hook. It will make sure that the MIPI-DSI host sticks around,
143 * and that the driver's bind can be called.
145 * - In its probe hook, the bridge driver must try to find its MIPI-DSI
146 * host, register as a MIPI-DSI device and attach the MIPI-DSI device
147 * to its host. The bridge driver is now functional.
149 * - In its &struct mipi_dsi_host_ops.attach hook, the MIPI-DSI host can
150 * now add its component. Its bind hook will now be called and since
151 * the bridge driver is attached and registered, we can now look for
154 * At this point, we're now certain that both the upstream driver and
155 * the bridge driver are functional and we can't have a deadlock-like
156 * situation when probing.
160 * DOC: dsi bridge operations
162 * DSI host interfaces are expected to be implemented as bridges rather than
163 * encoders, however there are a few aspects of their operation that need to
164 * be defined in order to provide a consistent interface.
166 * A DSI host should keep the PHY powered down until the pre_enable operation is
167 * called. All lanes are in an undefined idle state up to this point, and it
168 * must not be assumed that it is LP-11.
169 * pre_enable should initialise the PHY, set the data lanes to LP-11, and the
170 * clock lane to either LP-11 or HS depending on the mode_flag
171 * %MIPI_DSI_CLOCK_NON_CONTINUOUS.
173 * Ordinarily the downstream bridge DSI peripheral pre_enable will have been
174 * called before the DSI host. If the DSI peripheral requires LP-11 and/or
175 * the clock lane to be in HS mode prior to pre_enable, then it can set the
176 * &pre_enable_prev_first flag to request the pre_enable (and
177 * post_disable) order to be altered to enable the DSI host first.
179 * Either the CRTC being enabled, or the DSI host enable operation should switch
180 * the host to actively transmitting video on the data lanes.
182 * The reverse also applies. The DSI host disable operation or stopping the CRTC
183 * should stop transmitting video, and the data lanes should return to the LP-11
184 * state. The DSI host &post_disable operation should disable the PHY.
185 * If the &pre_enable_prev_first flag is set, then the DSI peripheral's
186 * bridge &post_disable will be called before the DSI host's post_disable.
188 * Whilst it is valid to call &host_transfer prior to pre_enable or after
189 * post_disable, the exact state of the lanes is undefined at this point. The
190 * DSI host should initialise the interface, transmit the data, and then disable
191 * the interface again.
193 * Ultra Low Power State (ULPS) is not explicitly supported by DRM. If
194 * implemented, it therefore needs to be handled entirely within the DSI Host
198 static DEFINE_MUTEX(bridge_lock);
199 static LIST_HEAD(bridge_list);
202 * drm_bridge_add - add the given bridge to the global bridge list
204 * @bridge: bridge control structure
206 void drm_bridge_add(struct drm_bridge *bridge)
208 mutex_init(&bridge->hpd_mutex);
210 mutex_lock(&bridge_lock);
211 list_add_tail(&bridge->list, &bridge_list);
212 mutex_unlock(&bridge_lock);
214 EXPORT_SYMBOL(drm_bridge_add);
216 static void drm_bridge_remove_void(void *bridge)
218 drm_bridge_remove(bridge);
222 * devm_drm_bridge_add - devm managed version of drm_bridge_add()
224 * @dev: device to tie the bridge lifetime to
225 * @bridge: bridge control structure
227 * This is the managed version of drm_bridge_add() which automatically
228 * calls drm_bridge_remove() when @dev is unbound.
230 * Return: 0 if no error or negative error code.
232 int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge)
234 drm_bridge_add(bridge);
235 return devm_add_action_or_reset(dev, drm_bridge_remove_void, bridge);
237 EXPORT_SYMBOL(devm_drm_bridge_add);
240 * drm_bridge_remove - remove the given bridge from the global bridge list
242 * @bridge: bridge control structure
244 void drm_bridge_remove(struct drm_bridge *bridge)
246 mutex_lock(&bridge_lock);
247 list_del_init(&bridge->list);
248 mutex_unlock(&bridge_lock);
250 mutex_destroy(&bridge->hpd_mutex);
252 EXPORT_SYMBOL(drm_bridge_remove);
254 static struct drm_private_state *
255 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)
257 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
258 struct drm_bridge_state *state;
260 state = bridge->funcs->atomic_duplicate_state(bridge);
261 return state ? &state->base : NULL;
265 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,
266 struct drm_private_state *s)
268 struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
269 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
271 bridge->funcs->atomic_destroy_state(bridge, state);
274 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
275 .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
276 .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
280 * drm_bridge_attach - attach the bridge to an encoder's chain
282 * @encoder: DRM encoder
283 * @bridge: bridge to attach
284 * @previous: previous bridge in the chain (optional)
285 * @flags: DRM_BRIDGE_ATTACH_* flags
287 * Called by a kms driver to link the bridge to an encoder's chain. The previous
288 * argument specifies the previous bridge in the chain. If NULL, the bridge is
289 * linked directly at the encoder's output. Otherwise it is linked at the
290 * previous bridge's output.
292 * If non-NULL the previous bridge must be already attached by a call to this
295 * Note that bridges attached to encoders are auto-detached during encoder
296 * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
297 * *not* be balanced with a drm_bridge_detach() in driver code.
300 * Zero on success, error code on failure
302 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
303 struct drm_bridge *previous,
304 enum drm_bridge_attach_flags flags)
308 if (!encoder || !bridge)
311 if (previous && (!previous->dev || previous->encoder != encoder))
317 bridge->dev = encoder->dev;
318 bridge->encoder = encoder;
321 list_add(&bridge->chain_node, &previous->chain_node);
323 list_add(&bridge->chain_node, &encoder->bridge_chain);
325 if (bridge->funcs->attach) {
326 ret = bridge->funcs->attach(bridge, flags);
328 goto err_reset_bridge;
331 if (bridge->funcs->atomic_reset) {
332 struct drm_bridge_state *state;
334 state = bridge->funcs->atomic_reset(bridge);
336 ret = PTR_ERR(state);
337 goto err_detach_bridge;
340 drm_atomic_private_obj_init(bridge->dev, &bridge->base,
342 &drm_bridge_priv_state_funcs);
348 if (bridge->funcs->detach)
349 bridge->funcs->detach(bridge);
353 bridge->encoder = NULL;
354 list_del(&bridge->chain_node);
357 DRM_ERROR("failed to attach bridge %pOF to encoder %s: %d\n",
358 bridge->of_node, encoder->name, ret);
360 DRM_ERROR("failed to attach bridge to encoder %s: %d\n",
366 EXPORT_SYMBOL(drm_bridge_attach);
368 void drm_bridge_detach(struct drm_bridge *bridge)
370 if (WARN_ON(!bridge))
373 if (WARN_ON(!bridge->dev))
376 if (bridge->funcs->atomic_reset)
377 drm_atomic_private_obj_fini(&bridge->base);
379 if (bridge->funcs->detach)
380 bridge->funcs->detach(bridge);
382 list_del(&bridge->chain_node);
387 * DOC: bridge operations
389 * Bridge drivers expose operations through the &drm_bridge_funcs structure.
390 * The DRM internals (atomic and CRTC helpers) use the helpers defined in
391 * drm_bridge.c to call bridge operations. Those operations are divided in
392 * three big categories to support different parts of the bridge usage.
394 * - The encoder-related operations support control of the bridges in the
395 * chain, and are roughly counterparts to the &drm_encoder_helper_funcs
396 * operations. They are used by the legacy CRTC and the atomic modeset
397 * helpers to perform mode validation, fixup and setting, and enable and
398 * disable the bridge automatically.
400 * The enable and disable operations are split in
401 * &drm_bridge_funcs.pre_enable, &drm_bridge_funcs.enable,
402 * &drm_bridge_funcs.disable and &drm_bridge_funcs.post_disable to provide
403 * finer-grained control.
405 * Bridge drivers may implement the legacy version of those operations, or
406 * the atomic version (prefixed with atomic\_), in which case they shall also
407 * implement the atomic state bookkeeping operations
408 * (&drm_bridge_funcs.atomic_duplicate_state,
409 * &drm_bridge_funcs.atomic_destroy_state and &drm_bridge_funcs.reset).
410 * Mixing atomic and non-atomic versions of the operations is not supported.
412 * - The bus format negotiation operations
413 * &drm_bridge_funcs.atomic_get_output_bus_fmts and
414 * &drm_bridge_funcs.atomic_get_input_bus_fmts allow bridge drivers to
415 * negotiate the formats transmitted between bridges in the chain when
416 * multiple formats are supported. Negotiation for formats is performed
417 * transparently for display drivers by the atomic modeset helpers. Only
418 * atomic versions of those operations exist, bridge drivers that need to
419 * implement them shall thus also implement the atomic version of the
420 * encoder-related operations. This feature is not supported by the legacy
423 * - The connector-related operations support implementing a &drm_connector
424 * based on a chain of bridges. DRM bridges traditionally create a
425 * &drm_connector for bridges meant to be used at the end of the chain. This
426 * puts additional burden on bridge drivers, especially for bridges that may
427 * be used in the middle of a chain or at the end of it. Furthermore, it
428 * requires all operations of the &drm_connector to be handled by a single
429 * bridge, which doesn't always match the hardware architecture.
431 * To simplify bridge drivers and make the connector implementation more
432 * flexible, a new model allows bridges to unconditionally skip creation of
433 * &drm_connector and instead expose &drm_bridge_funcs operations to support
434 * an externally-implemented &drm_connector. Those operations are
435 * &drm_bridge_funcs.detect, &drm_bridge_funcs.get_modes,
436 * &drm_bridge_funcs.get_edid, &drm_bridge_funcs.hpd_notify,
437 * &drm_bridge_funcs.hpd_enable and &drm_bridge_funcs.hpd_disable. When
438 * implemented, display drivers shall create a &drm_connector instance for
439 * each chain of bridges, and implement those connector instances based on
440 * the bridge connector operations.
442 * Bridge drivers shall implement the connector-related operations for all
443 * the features that the bridge hardware support. For instance, if a bridge
444 * supports reading EDID, the &drm_bridge_funcs.get_edid shall be
445 * implemented. This however doesn't mean that the DDC lines are wired to the
446 * bridge on a particular platform, as they could also be connected to an I2C
447 * controller of the SoC. Support for the connector-related operations on the
448 * running platform is reported through the &drm_bridge.ops flags. Bridge
449 * drivers shall detect which operations they can support on the platform
450 * (usually this information is provided by ACPI or DT), and set the
451 * &drm_bridge.ops flags for all supported operations. A flag shall only be
452 * set if the corresponding &drm_bridge_funcs operation is implemented, but
453 * an implemented operation doesn't necessarily imply that the corresponding
454 * flag will be set. Display drivers shall use the &drm_bridge.ops flags to
455 * decide which bridge to delegate a connector operation to. This mechanism
456 * allows providing a single static const &drm_bridge_funcs instance in
457 * bridge drivers, improving security by storing function pointers in
460 * In order to ease transition, bridge drivers may support both the old and
461 * new models by making connector creation optional and implementing the
462 * connected-related bridge operations. Connector creation is then controlled
463 * by the flags argument to the drm_bridge_attach() function. Display drivers
464 * that support the new model and create connectors themselves shall set the
465 * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag, and bridge drivers shall then skip
466 * connector creation. For intermediate bridges in the chain, the flag shall
467 * be passed to the drm_bridge_attach() call for the downstream bridge.
468 * Bridge drivers that implement the new model only shall return an error
469 * from their &drm_bridge_funcs.attach handler when the
470 * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag is not set. New display drivers
471 * should use the new model, and convert the bridge drivers they use if
472 * needed, in order to gradually transition to the new model.
476 * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
478 * @bridge: bridge control structure
479 * @mode: desired mode to be set for the bridge
480 * @adjusted_mode: updated mode that works for this bridge
482 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
483 * encoder chain, starting from the first bridge to the last.
485 * Note: the bridge passed should be the one closest to the encoder
488 * true on success, false on failure
490 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
491 const struct drm_display_mode *mode,
492 struct drm_display_mode *adjusted_mode)
494 struct drm_encoder *encoder;
499 encoder = bridge->encoder;
500 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
501 if (!bridge->funcs->mode_fixup)
504 if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
510 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
513 * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
515 * @bridge: bridge control structure
516 * @info: display info against which the mode shall be validated
517 * @mode: desired mode to be validated
519 * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
520 * chain, starting from the first bridge to the last. If at least one bridge
521 * does not accept the mode the function returns the error code.
523 * Note: the bridge passed should be the one closest to the encoder.
526 * MODE_OK on success, drm_mode_status Enum error code on failure
529 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
530 const struct drm_display_info *info,
531 const struct drm_display_mode *mode)
533 struct drm_encoder *encoder;
538 encoder = bridge->encoder;
539 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
540 enum drm_mode_status ret;
542 if (!bridge->funcs->mode_valid)
545 ret = bridge->funcs->mode_valid(bridge, info, mode);
552 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
555 * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
557 * @bridge: bridge control structure
558 * @mode: desired mode to be set for the encoder chain
559 * @adjusted_mode: updated mode that works for this encoder chain
561 * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
562 * encoder chain, starting from the first bridge to the last.
564 * Note: the bridge passed should be the one closest to the encoder
566 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
567 const struct drm_display_mode *mode,
568 const struct drm_display_mode *adjusted_mode)
570 struct drm_encoder *encoder;
575 encoder = bridge->encoder;
576 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
577 if (bridge->funcs->mode_set)
578 bridge->funcs->mode_set(bridge, mode, adjusted_mode);
581 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
584 * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
585 * @bridge: bridge control structure
586 * @old_state: old atomic state
588 * Calls &drm_bridge_funcs.atomic_disable (falls back on
589 * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
590 * starting from the last bridge to the first. These are called before calling
591 * &drm_encoder_helper_funcs.atomic_disable
593 * Note: the bridge passed should be the one closest to the encoder
595 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
596 struct drm_atomic_state *old_state)
598 struct drm_encoder *encoder;
599 struct drm_bridge *iter;
604 encoder = bridge->encoder;
605 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
606 if (iter->funcs->atomic_disable) {
607 struct drm_bridge_state *old_bridge_state;
610 drm_atomic_get_old_bridge_state(old_state,
612 if (WARN_ON(!old_bridge_state))
615 iter->funcs->atomic_disable(iter, old_bridge_state);
616 } else if (iter->funcs->disable) {
617 iter->funcs->disable(iter);
624 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
626 static void drm_atomic_bridge_call_post_disable(struct drm_bridge *bridge,
627 struct drm_atomic_state *old_state)
629 if (old_state && bridge->funcs->atomic_post_disable) {
630 struct drm_bridge_state *old_bridge_state;
633 drm_atomic_get_old_bridge_state(old_state,
635 if (WARN_ON(!old_bridge_state))
638 bridge->funcs->atomic_post_disable(bridge,
640 } else if (bridge->funcs->post_disable) {
641 bridge->funcs->post_disable(bridge);
646 * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
647 * in the encoder chain
648 * @bridge: bridge control structure
649 * @old_state: old atomic state
651 * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
652 * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
653 * starting from the first bridge to the last. These are called after completing
654 * &drm_encoder_helper_funcs.atomic_disable
656 * If a bridge sets @pre_enable_prev_first, then the @post_disable for that
657 * bridge will be called before the previous one to reverse the @pre_enable
660 * Note: the bridge passed should be the one closest to the encoder
662 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
663 struct drm_atomic_state *old_state)
665 struct drm_encoder *encoder;
666 struct drm_bridge *next, *limit;
671 encoder = bridge->encoder;
673 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
676 if (!list_is_last(&bridge->chain_node, &encoder->bridge_chain)) {
677 next = list_next_entry(bridge, chain_node);
679 if (next->pre_enable_prev_first) {
680 /* next bridge had requested that prev
681 * was enabled first, so disabled last
685 /* Find the next bridge that has NOT requested
686 * prev to be enabled first / disabled last
688 list_for_each_entry_from(next, &encoder->bridge_chain,
690 if (next->pre_enable_prev_first) {
691 next = list_prev_entry(next, chain_node);
697 /* Call these bridges in reverse order */
698 list_for_each_entry_from_reverse(next, &encoder->bridge_chain,
703 drm_atomic_bridge_call_post_disable(next,
709 drm_atomic_bridge_call_post_disable(bridge, old_state);
712 /* Jump all bridges that we have already post_disabled */
716 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
718 static void drm_atomic_bridge_call_pre_enable(struct drm_bridge *bridge,
719 struct drm_atomic_state *old_state)
721 if (old_state && bridge->funcs->atomic_pre_enable) {
722 struct drm_bridge_state *old_bridge_state;
725 drm_atomic_get_old_bridge_state(old_state,
727 if (WARN_ON(!old_bridge_state))
730 bridge->funcs->atomic_pre_enable(bridge, old_bridge_state);
731 } else if (bridge->funcs->pre_enable) {
732 bridge->funcs->pre_enable(bridge);
737 * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
739 * @bridge: bridge control structure
740 * @old_state: old atomic state
742 * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
743 * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
744 * starting from the last bridge to the first. These are called before calling
745 * &drm_encoder_helper_funcs.atomic_enable
747 * If a bridge sets @pre_enable_prev_first, then the pre_enable for the
748 * prev bridge will be called before pre_enable of this bridge.
750 * Note: the bridge passed should be the one closest to the encoder
752 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
753 struct drm_atomic_state *old_state)
755 struct drm_encoder *encoder;
756 struct drm_bridge *iter, *next, *limit;
761 encoder = bridge->encoder;
763 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
764 if (iter->pre_enable_prev_first) {
767 list_for_each_entry_from_reverse(next,
768 &encoder->bridge_chain,
773 if (!next->pre_enable_prev_first) {
774 /* Found first bridge that does NOT
775 * request prev to be enabled first
777 limit = list_prev_entry(next, chain_node);
782 list_for_each_entry_from(next, &encoder->bridge_chain, chain_node) {
783 /* Call requested prev bridge pre_enable
787 /* At the first bridge to request prev
788 * bridges called first.
792 drm_atomic_bridge_call_pre_enable(next, old_state);
796 drm_atomic_bridge_call_pre_enable(iter, old_state);
798 if (iter->pre_enable_prev_first)
799 /* Jump all bridges that we have already pre_enabled */
806 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
809 * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
810 * @bridge: bridge control structure
811 * @old_state: old atomic state
813 * Calls &drm_bridge_funcs.atomic_enable (falls back on
814 * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
815 * starting from the first bridge to the last. These are called after completing
816 * &drm_encoder_helper_funcs.atomic_enable
818 * Note: the bridge passed should be the one closest to the encoder
820 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
821 struct drm_atomic_state *old_state)
823 struct drm_encoder *encoder;
828 encoder = bridge->encoder;
829 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
830 if (bridge->funcs->atomic_enable) {
831 struct drm_bridge_state *old_bridge_state;
834 drm_atomic_get_old_bridge_state(old_state,
836 if (WARN_ON(!old_bridge_state))
839 bridge->funcs->atomic_enable(bridge, old_bridge_state);
840 } else if (bridge->funcs->enable) {
841 bridge->funcs->enable(bridge);
845 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
847 static int drm_atomic_bridge_check(struct drm_bridge *bridge,
848 struct drm_crtc_state *crtc_state,
849 struct drm_connector_state *conn_state)
851 if (bridge->funcs->atomic_check) {
852 struct drm_bridge_state *bridge_state;
855 bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
857 if (WARN_ON(!bridge_state))
860 ret = bridge->funcs->atomic_check(bridge, bridge_state,
861 crtc_state, conn_state);
864 } else if (bridge->funcs->mode_fixup) {
865 if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode,
866 &crtc_state->adjusted_mode))
873 static int select_bus_fmt_recursive(struct drm_bridge *first_bridge,
874 struct drm_bridge *cur_bridge,
875 struct drm_crtc_state *crtc_state,
876 struct drm_connector_state *conn_state,
879 unsigned int i, num_in_bus_fmts = 0;
880 struct drm_bridge_state *cur_state;
881 struct drm_bridge *prev_bridge;
885 prev_bridge = drm_bridge_get_prev_bridge(cur_bridge);
886 cur_state = drm_atomic_get_new_bridge_state(crtc_state->state,
890 * If bus format negotiation is not supported by this bridge, let's
891 * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and
892 * hope that it can handle this situation gracefully (by providing
893 * appropriate default values).
895 if (!cur_bridge->funcs->atomic_get_input_bus_fmts) {
896 if (cur_bridge != first_bridge) {
897 ret = select_bus_fmt_recursive(first_bridge,
898 prev_bridge, crtc_state,
900 MEDIA_BUS_FMT_FIXED);
906 * Driver does not implement the atomic state hooks, but that's
907 * fine, as long as it does not access the bridge state.
910 cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
911 cur_state->output_bus_cfg.format = out_bus_fmt;
918 * If the driver implements ->atomic_get_input_bus_fmts() it
919 * should also implement the atomic state hooks.
921 if (WARN_ON(!cur_state))
924 in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge,
930 if (!num_in_bus_fmts)
932 else if (!in_bus_fmts)
935 if (first_bridge == cur_bridge) {
936 cur_state->input_bus_cfg.format = in_bus_fmts[0];
937 cur_state->output_bus_cfg.format = out_bus_fmt;
942 for (i = 0; i < num_in_bus_fmts; i++) {
943 ret = select_bus_fmt_recursive(first_bridge, prev_bridge,
944 crtc_state, conn_state,
946 if (ret != -ENOTSUPP)
951 cur_state->input_bus_cfg.format = in_bus_fmts[i];
952 cur_state->output_bus_cfg.format = out_bus_fmt;
960 * This function is called by &drm_atomic_bridge_chain_check() just before
961 * calling &drm_bridge_funcs.atomic_check() on all elements of the chain.
962 * It performs bus format negotiation between bridge elements. The negotiation
963 * happens in reverse order, starting from the last element in the chain up to
966 * Negotiation starts by retrieving supported output bus formats on the last
967 * bridge element and testing them one by one. The test is recursive, meaning
968 * that for each tested output format, the whole chain will be walked backward,
969 * and each element will have to choose an input bus format that can be
970 * transcoded to the requested output format. When a bridge element does not
971 * support transcoding into a specific output format -ENOTSUPP is returned and
972 * the next bridge element will have to try a different format. If none of the
973 * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail.
975 * This implementation is relying on
976 * &drm_bridge_funcs.atomic_get_output_bus_fmts() and
977 * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported
978 * input/output formats.
980 * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by
981 * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts()
982 * tries a single format: &drm_connector.display_info.bus_formats[0] if
983 * available, MEDIA_BUS_FMT_FIXED otherwise.
985 * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented,
986 * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the
987 * bridge element that lacks this hook and asks the previous element in the
988 * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what
989 * to do in that case (fail if they want to enforce bus format negotiation, or
990 * provide a reasonable default if they need to support pipelines where not
991 * all elements support bus format negotiation).
994 drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge,
995 struct drm_crtc_state *crtc_state,
996 struct drm_connector_state *conn_state)
998 struct drm_connector *conn = conn_state->connector;
999 struct drm_encoder *encoder = bridge->encoder;
1000 struct drm_bridge_state *last_bridge_state;
1001 unsigned int i, num_out_bus_fmts = 0;
1002 struct drm_bridge *last_bridge;
1006 last_bridge = list_last_entry(&encoder->bridge_chain,
1007 struct drm_bridge, chain_node);
1008 last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
1011 if (last_bridge->funcs->atomic_get_output_bus_fmts) {
1012 const struct drm_bridge_funcs *funcs = last_bridge->funcs;
1015 * If the driver implements ->atomic_get_output_bus_fmts() it
1016 * should also implement the atomic state hooks.
1018 if (WARN_ON(!last_bridge_state))
1021 out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge,
1026 if (!num_out_bus_fmts)
1028 else if (!out_bus_fmts)
1031 num_out_bus_fmts = 1;
1032 out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL);
1036 if (conn->display_info.num_bus_formats &&
1037 conn->display_info.bus_formats)
1038 out_bus_fmts[0] = conn->display_info.bus_formats[0];
1040 out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
1043 for (i = 0; i < num_out_bus_fmts; i++) {
1044 ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state,
1045 conn_state, out_bus_fmts[i]);
1046 if (ret != -ENOTSUPP)
1050 kfree(out_bus_fmts);
1056 drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge,
1057 struct drm_connector *conn,
1058 struct drm_atomic_state *state)
1060 struct drm_bridge_state *bridge_state, *next_bridge_state;
1061 struct drm_bridge *next_bridge;
1062 u32 output_flags = 0;
1064 bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
1066 /* No bridge state attached to this bridge => nothing to propagate. */
1070 next_bridge = drm_bridge_get_next_bridge(bridge);
1073 * Let's try to apply the most common case here, that is, propagate
1074 * display_info flags for the last bridge, and propagate the input
1075 * flags of the next bridge element to the output end of the current
1076 * bridge when the bridge is not the last one.
1077 * There are exceptions to this rule, like when signal inversion is
1078 * happening at the board level, but that's something drivers can deal
1079 * with from their &drm_bridge_funcs.atomic_check() implementation by
1080 * simply overriding the flags value we've set here.
1083 output_flags = conn->display_info.bus_flags;
1085 next_bridge_state = drm_atomic_get_new_bridge_state(state,
1088 * No bridge state attached to the next bridge, just leave the
1091 if (next_bridge_state)
1092 output_flags = next_bridge_state->input_bus_cfg.flags;
1095 bridge_state->output_bus_cfg.flags = output_flags;
1098 * Propagate the output flags to the input end of the bridge. Again, it's
1099 * not necessarily what all bridges want, but that's what most of them
1100 * do, and by doing that by default we avoid forcing drivers to
1101 * duplicate the "dummy propagation" logic.
1103 bridge_state->input_bus_cfg.flags = output_flags;
1107 * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain
1108 * @bridge: bridge control structure
1109 * @crtc_state: new CRTC state
1110 * @conn_state: new connector state
1112 * First trigger a bus format negotiation before calling
1113 * &drm_bridge_funcs.atomic_check() (falls back on
1114 * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain,
1115 * starting from the last bridge to the first. These are called before calling
1116 * &drm_encoder_helper_funcs.atomic_check()
1119 * 0 on success, a negative error code on failure
1121 int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,
1122 struct drm_crtc_state *crtc_state,
1123 struct drm_connector_state *conn_state)
1125 struct drm_connector *conn = conn_state->connector;
1126 struct drm_encoder *encoder;
1127 struct drm_bridge *iter;
1133 ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state,
1138 encoder = bridge->encoder;
1139 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
1143 * Bus flags are propagated by default. If a bridge needs to
1144 * tweak the input bus flags for any reason, it should happen
1145 * in its &drm_bridge_funcs.atomic_check() implementation such
1146 * that preceding bridges in the chain can propagate the new
1149 drm_atomic_bridge_propagate_bus_flags(iter, conn,
1152 ret = drm_atomic_bridge_check(iter, crtc_state, conn_state);
1162 EXPORT_SYMBOL(drm_atomic_bridge_chain_check);
1165 * drm_bridge_detect - check if anything is attached to the bridge output
1166 * @bridge: bridge control structure
1168 * If the bridge supports output detection, as reported by the
1169 * DRM_BRIDGE_OP_DETECT bridge ops flag, call &drm_bridge_funcs.detect for the
1170 * bridge and return the connection status. Otherwise return
1171 * connector_status_unknown.
1174 * The detection status on success, or connector_status_unknown if the bridge
1175 * doesn't support output detection.
1177 enum drm_connector_status drm_bridge_detect(struct drm_bridge *bridge)
1179 if (!(bridge->ops & DRM_BRIDGE_OP_DETECT))
1180 return connector_status_unknown;
1182 return bridge->funcs->detect(bridge);
1184 EXPORT_SYMBOL_GPL(drm_bridge_detect);
1187 * drm_bridge_get_modes - fill all modes currently valid for the sink into the
1189 * @bridge: bridge control structure
1190 * @connector: the connector to fill with modes
1192 * If the bridge supports output modes retrieval, as reported by the
1193 * DRM_BRIDGE_OP_MODES bridge ops flag, call &drm_bridge_funcs.get_modes to
1194 * fill the connector with all valid modes and return the number of modes
1195 * added. Otherwise return 0.
1198 * The number of modes added to the connector.
1200 int drm_bridge_get_modes(struct drm_bridge *bridge,
1201 struct drm_connector *connector)
1203 if (!(bridge->ops & DRM_BRIDGE_OP_MODES))
1206 return bridge->funcs->get_modes(bridge, connector);
1208 EXPORT_SYMBOL_GPL(drm_bridge_get_modes);
1211 * drm_bridge_edid_read - read the EDID data of the connected display
1212 * @bridge: bridge control structure
1213 * @connector: the connector to read EDID for
1215 * If the bridge supports output EDID retrieval, as reported by the
1216 * DRM_BRIDGE_OP_EDID bridge ops flag, call &drm_bridge_funcs.edid_read to get
1217 * the EDID and return it. Otherwise return NULL.
1220 * The retrieved EDID on success, or NULL otherwise.
1222 const struct drm_edid *drm_bridge_edid_read(struct drm_bridge *bridge,
1223 struct drm_connector *connector)
1225 if (!(bridge->ops & DRM_BRIDGE_OP_EDID))
1228 return bridge->funcs->edid_read(bridge, connector);
1230 EXPORT_SYMBOL_GPL(drm_bridge_edid_read);
1233 * drm_bridge_hpd_enable - enable hot plug detection for the bridge
1234 * @bridge: bridge control structure
1235 * @cb: hot-plug detection callback
1236 * @data: data to be passed to the hot-plug detection callback
1238 * Call &drm_bridge_funcs.hpd_enable if implemented and register the given @cb
1239 * and @data as hot plug notification callback. From now on the @cb will be
1240 * called with @data when an output status change is detected by the bridge,
1241 * until hot plug notification gets disabled with drm_bridge_hpd_disable().
1243 * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1244 * bridge->ops. This function shall not be called when the flag is not set.
1246 * Only one hot plug detection callback can be registered at a time, it is an
1247 * error to call this function when hot plug detection is already enabled for
1250 void drm_bridge_hpd_enable(struct drm_bridge *bridge,
1251 void (*cb)(void *data,
1252 enum drm_connector_status status),
1255 if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1258 mutex_lock(&bridge->hpd_mutex);
1260 if (WARN(bridge->hpd_cb, "Hot plug detection already enabled\n"))
1263 bridge->hpd_cb = cb;
1264 bridge->hpd_data = data;
1266 if (bridge->funcs->hpd_enable)
1267 bridge->funcs->hpd_enable(bridge);
1270 mutex_unlock(&bridge->hpd_mutex);
1272 EXPORT_SYMBOL_GPL(drm_bridge_hpd_enable);
1275 * drm_bridge_hpd_disable - disable hot plug detection for the bridge
1276 * @bridge: bridge control structure
1278 * Call &drm_bridge_funcs.hpd_disable if implemented and unregister the hot
1279 * plug detection callback previously registered with drm_bridge_hpd_enable().
1280 * Once this function returns the callback will not be called by the bridge
1281 * when an output status change occurs.
1283 * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1284 * bridge->ops. This function shall not be called when the flag is not set.
1286 void drm_bridge_hpd_disable(struct drm_bridge *bridge)
1288 if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1291 mutex_lock(&bridge->hpd_mutex);
1292 if (bridge->funcs->hpd_disable)
1293 bridge->funcs->hpd_disable(bridge);
1295 bridge->hpd_cb = NULL;
1296 bridge->hpd_data = NULL;
1297 mutex_unlock(&bridge->hpd_mutex);
1299 EXPORT_SYMBOL_GPL(drm_bridge_hpd_disable);
1302 * drm_bridge_hpd_notify - notify hot plug detection events
1303 * @bridge: bridge control structure
1304 * @status: output connection status
1306 * Bridge drivers shall call this function to report hot plug events when they
1307 * detect a change in the output status, when hot plug detection has been
1308 * enabled by drm_bridge_hpd_enable().
1310 * This function shall be called in a context that can sleep.
1312 void drm_bridge_hpd_notify(struct drm_bridge *bridge,
1313 enum drm_connector_status status)
1315 mutex_lock(&bridge->hpd_mutex);
1317 bridge->hpd_cb(bridge->hpd_data, status);
1318 mutex_unlock(&bridge->hpd_mutex);
1320 EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify);
1324 * of_drm_find_bridge - find the bridge corresponding to the device node in
1325 * the global bridge list
1330 * drm_bridge control struct on success, NULL on failure
1332 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
1334 struct drm_bridge *bridge;
1336 mutex_lock(&bridge_lock);
1338 list_for_each_entry(bridge, &bridge_list, list) {
1339 if (bridge->of_node == np) {
1340 mutex_unlock(&bridge_lock);
1345 mutex_unlock(&bridge_lock);
1348 EXPORT_SYMBOL(of_drm_find_bridge);
1352 MODULE_DESCRIPTION("DRM bridge infrastructure");
1353 MODULE_LICENSE("GPL and additional rights");