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/module.h>
26 #include <linux/mutex.h>
28 #include <drm/drm_atomic_state_helper.h>
29 #include <drm/drm_bridge.h>
30 #include <drm/drm_encoder.h>
31 #include <drm/drm_print.h>
33 #include "drm_crtc_internal.h"
38 * &struct drm_bridge represents a device that hangs on to an encoder. These are
39 * handy when a regular &drm_encoder entity isn't enough to represent the entire
42 * A bridge is always attached to a single &drm_encoder at a time, but can be
43 * either connected to it directly, or through a chain of bridges::
45 * [ CRTC ---> ] Encoder ---> Bridge A ---> Bridge B
47 * Here, the output of the encoder feeds to bridge A, and that furthers feeds to
48 * bridge B. Bridge chains can be arbitrarily long, and shall be fully linear:
49 * Chaining multiple bridges to the output of a bridge, or the same bridge to
50 * the output of different bridges, is not supported.
52 * Display drivers are responsible for linking encoders with the first bridge
53 * in the chains. This is done by acquiring the appropriate bridge with
54 * of_drm_find_bridge() or drm_of_find_panel_or_bridge(), or creating it for a
55 * panel with drm_panel_bridge_add_typed() (or the managed version
56 * devm_drm_panel_bridge_add_typed()). Once acquired, the bridge shall be
57 * attached to the encoder with a call to drm_bridge_attach().
59 * Bridges are responsible for linking themselves with the next bridge in the
60 * chain, if any. This is done the same way as for encoders, with the call to
61 * drm_bridge_attach() occurring in the &drm_bridge_funcs.attach operation.
63 * Once these links are created, the bridges can participate along with encoder
64 * functions to perform mode validation and fixup (through
65 * drm_bridge_chain_mode_valid() and drm_atomic_bridge_chain_check()), mode
66 * setting (through drm_bridge_chain_mode_set()), enable (through
67 * drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_enable())
68 * and disable (through drm_atomic_bridge_chain_disable() and
69 * drm_atomic_bridge_chain_post_disable()). Those functions call the
70 * corresponding operations provided in &drm_bridge_funcs in sequence for all
71 * bridges in the chain.
73 * For display drivers that use the atomic helpers
74 * drm_atomic_helper_check_modeset(),
75 * drm_atomic_helper_commit_modeset_enables() and
76 * drm_atomic_helper_commit_modeset_disables() (either directly in hand-rolled
77 * commit check and commit tail handlers, or through the higher-level
78 * drm_atomic_helper_check() and drm_atomic_helper_commit_tail() or
79 * drm_atomic_helper_commit_tail_rpm() helpers), this is done transparently and
80 * requires no intervention from the driver. For other drivers, the relevant
81 * DRM bridge chain functions shall be called manually.
83 * Bridges also participate in implementing the &drm_connector at the end of
84 * the bridge chain. Display drivers may use the drm_bridge_connector_init()
85 * helper to create the &drm_connector, or implement it manually on top of the
86 * connector-related operations exposed by the bridge (see the overview
87 * documentation of bridge operations for more details).
89 * &drm_bridge, like &drm_panel, aren't &drm_mode_object entities like planes,
90 * CRTCs, encoders or connectors and hence are not visible to userspace. They
91 * just provide additional hooks to get the desired output at the end of the
95 static DEFINE_MUTEX(bridge_lock);
96 static LIST_HEAD(bridge_list);
99 * drm_bridge_add - add the given bridge to the global bridge list
101 * @bridge: bridge control structure
103 void drm_bridge_add(struct drm_bridge *bridge)
105 mutex_init(&bridge->hpd_mutex);
107 mutex_lock(&bridge_lock);
108 list_add_tail(&bridge->list, &bridge_list);
109 mutex_unlock(&bridge_lock);
111 EXPORT_SYMBOL(drm_bridge_add);
114 * drm_bridge_remove - remove the given bridge from the global bridge list
116 * @bridge: bridge control structure
118 void drm_bridge_remove(struct drm_bridge *bridge)
120 mutex_lock(&bridge_lock);
121 list_del_init(&bridge->list);
122 mutex_unlock(&bridge_lock);
124 mutex_destroy(&bridge->hpd_mutex);
126 EXPORT_SYMBOL(drm_bridge_remove);
128 static struct drm_private_state *
129 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)
131 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
132 struct drm_bridge_state *state;
134 state = bridge->funcs->atomic_duplicate_state(bridge);
135 return state ? &state->base : NULL;
139 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,
140 struct drm_private_state *s)
142 struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
143 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
145 bridge->funcs->atomic_destroy_state(bridge, state);
148 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
149 .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
150 .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
154 * drm_bridge_attach - attach the bridge to an encoder's chain
156 * @encoder: DRM encoder
157 * @bridge: bridge to attach
158 * @previous: previous bridge in the chain (optional)
159 * @flags: DRM_BRIDGE_ATTACH_* flags
161 * Called by a kms driver to link the bridge to an encoder's chain. The previous
162 * argument specifies the previous bridge in the chain. If NULL, the bridge is
163 * linked directly at the encoder's output. Otherwise it is linked at the
164 * previous bridge's output.
166 * If non-NULL the previous bridge must be already attached by a call to this
169 * Note that bridges attached to encoders are auto-detached during encoder
170 * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
171 * *not* be balanced with a drm_bridge_detach() in driver code.
174 * Zero on success, error code on failure
176 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
177 struct drm_bridge *previous,
178 enum drm_bridge_attach_flags flags)
182 if (!encoder || !bridge)
185 if (previous && (!previous->dev || previous->encoder != encoder))
191 bridge->dev = encoder->dev;
192 bridge->encoder = encoder;
195 list_add(&bridge->chain_node, &previous->chain_node);
197 list_add(&bridge->chain_node, &encoder->bridge_chain);
199 if (bridge->funcs->attach) {
200 ret = bridge->funcs->attach(bridge, flags);
202 goto err_reset_bridge;
205 if (bridge->funcs->atomic_reset) {
206 struct drm_bridge_state *state;
208 state = bridge->funcs->atomic_reset(bridge);
210 ret = PTR_ERR(state);
211 goto err_detach_bridge;
214 drm_atomic_private_obj_init(bridge->dev, &bridge->base,
216 &drm_bridge_priv_state_funcs);
222 if (bridge->funcs->detach)
223 bridge->funcs->detach(bridge);
227 bridge->encoder = NULL;
228 list_del(&bridge->chain_node);
231 DRM_ERROR("failed to attach bridge %pOF to encoder %s: %d\n",
232 bridge->of_node, encoder->name, ret);
234 DRM_ERROR("failed to attach bridge to encoder %s: %d\n",
240 EXPORT_SYMBOL(drm_bridge_attach);
242 void drm_bridge_detach(struct drm_bridge *bridge)
244 if (WARN_ON(!bridge))
247 if (WARN_ON(!bridge->dev))
250 if (bridge->funcs->atomic_reset)
251 drm_atomic_private_obj_fini(&bridge->base);
253 if (bridge->funcs->detach)
254 bridge->funcs->detach(bridge);
256 list_del(&bridge->chain_node);
261 * DOC: bridge operations
263 * Bridge drivers expose operations through the &drm_bridge_funcs structure.
264 * The DRM internals (atomic and CRTC helpers) use the helpers defined in
265 * drm_bridge.c to call bridge operations. Those operations are divided in
266 * three big categories to support different parts of the bridge usage.
268 * - The encoder-related operations support control of the bridges in the
269 * chain, and are roughly counterparts to the &drm_encoder_helper_funcs
270 * operations. They are used by the legacy CRTC and the atomic modeset
271 * helpers to perform mode validation, fixup and setting, and enable and
272 * disable the bridge automatically.
274 * The enable and disable operations are split in
275 * &drm_bridge_funcs.pre_enable, &drm_bridge_funcs.enable,
276 * &drm_bridge_funcs.disable and &drm_bridge_funcs.post_disable to provide
277 * finer-grained control.
279 * Bridge drivers may implement the legacy version of those operations, or
280 * the atomic version (prefixed with atomic\_), in which case they shall also
281 * implement the atomic state bookkeeping operations
282 * (&drm_bridge_funcs.atomic_duplicate_state,
283 * &drm_bridge_funcs.atomic_destroy_state and &drm_bridge_funcs.reset).
284 * Mixing atomic and non-atomic versions of the operations is not supported.
286 * - The bus format negotiation operations
287 * &drm_bridge_funcs.atomic_get_output_bus_fmts and
288 * &drm_bridge_funcs.atomic_get_input_bus_fmts allow bridge drivers to
289 * negotiate the formats transmitted between bridges in the chain when
290 * multiple formats are supported. Negotiation for formats is performed
291 * transparently for display drivers by the atomic modeset helpers. Only
292 * atomic versions of those operations exist, bridge drivers that need to
293 * implement them shall thus also implement the atomic version of the
294 * encoder-related operations. This feature is not supported by the legacy
297 * - The connector-related operations support implementing a &drm_connector
298 * based on a chain of bridges. DRM bridges traditionally create a
299 * &drm_connector for bridges meant to be used at the end of the chain. This
300 * puts additional burden on bridge drivers, especially for bridges that may
301 * be used in the middle of a chain or at the end of it. Furthermore, it
302 * requires all operations of the &drm_connector to be handled by a single
303 * bridge, which doesn't always match the hardware architecture.
305 * To simplify bridge drivers and make the connector implementation more
306 * flexible, a new model allows bridges to unconditionally skip creation of
307 * &drm_connector and instead expose &drm_bridge_funcs operations to support
308 * an externally-implemented &drm_connector. Those operations are
309 * &drm_bridge_funcs.detect, &drm_bridge_funcs.get_modes,
310 * &drm_bridge_funcs.get_edid, &drm_bridge_funcs.hpd_notify,
311 * &drm_bridge_funcs.hpd_enable and &drm_bridge_funcs.hpd_disable. When
312 * implemented, display drivers shall create a &drm_connector instance for
313 * each chain of bridges, and implement those connector instances based on
314 * the bridge connector operations.
316 * Bridge drivers shall implement the connector-related operations for all
317 * the features that the bridge hardware support. For instance, if a bridge
318 * supports reading EDID, the &drm_bridge_funcs.get_edid shall be
319 * implemented. This however doesn't mean that the DDC lines are wired to the
320 * bridge on a particular platform, as they could also be connected to an I2C
321 * controller of the SoC. Support for the connector-related operations on the
322 * running platform is reported through the &drm_bridge.ops flags. Bridge
323 * drivers shall detect which operations they can support on the platform
324 * (usually this information is provided by ACPI or DT), and set the
325 * &drm_bridge.ops flags for all supported operations. A flag shall only be
326 * set if the corresponding &drm_bridge_funcs operation is implemented, but
327 * an implemented operation doesn't necessarily imply that the corresponding
328 * flag will be set. Display drivers shall use the &drm_bridge.ops flags to
329 * decide which bridge to delegate a connector operation to. This mechanism
330 * allows providing a single static const &drm_bridge_funcs instance in
331 * bridge drivers, improving security by storing function pointers in
334 * In order to ease transition, bridge drivers may support both the old and
335 * new models by making connector creation optional and implementing the
336 * connected-related bridge operations. Connector creation is then controlled
337 * by the flags argument to the drm_bridge_attach() function. Display drivers
338 * that support the new model and create connectors themselves shall set the
339 * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag, and bridge drivers shall then skip
340 * connector creation. For intermediate bridges in the chain, the flag shall
341 * be passed to the drm_bridge_attach() call for the downstream bridge.
342 * Bridge drivers that implement the new model only shall return an error
343 * from their &drm_bridge_funcs.attach handler when the
344 * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag is not set. New display drivers
345 * should use the new model, and convert the bridge drivers they use if
346 * needed, in order to gradually transition to the new model.
350 * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
352 * @bridge: bridge control structure
353 * @mode: desired mode to be set for the bridge
354 * @adjusted_mode: updated mode that works for this bridge
356 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
357 * encoder chain, starting from the first bridge to the last.
359 * Note: the bridge passed should be the one closest to the encoder
362 * true on success, false on failure
364 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
365 const struct drm_display_mode *mode,
366 struct drm_display_mode *adjusted_mode)
368 struct drm_encoder *encoder;
373 encoder = bridge->encoder;
374 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
375 if (!bridge->funcs->mode_fixup)
378 if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
384 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
387 * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
389 * @bridge: bridge control structure
390 * @info: display info against which the mode shall be validated
391 * @mode: desired mode to be validated
393 * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
394 * chain, starting from the first bridge to the last. If at least one bridge
395 * does not accept the mode the function returns the error code.
397 * Note: the bridge passed should be the one closest to the encoder.
400 * MODE_OK on success, drm_mode_status Enum error code on failure
403 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
404 const struct drm_display_info *info,
405 const struct drm_display_mode *mode)
407 struct drm_encoder *encoder;
412 encoder = bridge->encoder;
413 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
414 enum drm_mode_status ret;
416 if (!bridge->funcs->mode_valid)
419 ret = bridge->funcs->mode_valid(bridge, info, mode);
426 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
429 * drm_bridge_chain_disable - disables all bridges in the encoder chain
430 * @bridge: bridge control structure
432 * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
433 * chain, starting from the last bridge to the first. These are called before
434 * calling the encoder's prepare op.
436 * Note: the bridge passed should be the one closest to the encoder
438 void drm_bridge_chain_disable(struct drm_bridge *bridge)
440 struct drm_encoder *encoder;
441 struct drm_bridge *iter;
446 encoder = bridge->encoder;
447 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
448 if (iter->funcs->disable)
449 iter->funcs->disable(iter);
455 EXPORT_SYMBOL(drm_bridge_chain_disable);
458 * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the
460 * @bridge: bridge control structure
462 * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
463 * encoder chain, starting from the first bridge to the last. These are called
464 * after completing the encoder's prepare op.
466 * If a bridge sets @pre_enable_upstream_first, then the @post_disable for that
467 * bridge will be called before the previous one to reverse the @pre_enable
470 * Note: the bridge passed should be the one closest to the encoder
472 void drm_bridge_chain_post_disable(struct drm_bridge *bridge)
474 drm_atomic_bridge_chain_post_disable(bridge, NULL);
476 EXPORT_SYMBOL(drm_bridge_chain_post_disable);
479 * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
481 * @bridge: bridge control structure
482 * @mode: desired mode to be set for the encoder chain
483 * @adjusted_mode: updated mode that works for this encoder chain
485 * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
486 * encoder chain, starting from the first bridge to the last.
488 * Note: the bridge passed should be the one closest to the encoder
490 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
491 const struct drm_display_mode *mode,
492 const 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_set)
502 bridge->funcs->mode_set(bridge, mode, adjusted_mode);
505 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
508 * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the
510 * @bridge: bridge control structure
512 * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
513 * chain, starting from the last bridge to the first. These are called
514 * before calling the encoder's commit op.
516 * If a bridge sets @pre_enable_upstream_first, then the @pre_enable for the
517 * previous bridge will be called before @pre_enable of this bridge.
519 * Note: the bridge passed should be the one closest to the encoder
521 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge)
523 drm_atomic_bridge_chain_pre_enable(bridge, NULL);
525 EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
528 * drm_bridge_chain_enable - enables all bridges in the encoder chain
529 * @bridge: bridge control structure
531 * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
532 * chain, starting from the first bridge to the last. These are called
533 * after completing the encoder's commit op.
535 * Note that the bridge passed should be the one closest to the encoder
537 void drm_bridge_chain_enable(struct drm_bridge *bridge)
539 struct drm_encoder *encoder;
544 encoder = bridge->encoder;
545 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
546 if (bridge->funcs->enable)
547 bridge->funcs->enable(bridge);
550 EXPORT_SYMBOL(drm_bridge_chain_enable);
553 * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
554 * @bridge: bridge control structure
555 * @old_state: old atomic state
557 * Calls &drm_bridge_funcs.atomic_disable (falls back on
558 * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
559 * starting from the last bridge to the first. These are called before calling
560 * &drm_encoder_helper_funcs.atomic_disable
562 * Note: the bridge passed should be the one closest to the encoder
564 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
565 struct drm_atomic_state *old_state)
567 struct drm_encoder *encoder;
568 struct drm_bridge *iter;
573 encoder = bridge->encoder;
574 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
575 if (iter->funcs->atomic_disable) {
576 struct drm_bridge_state *old_bridge_state;
579 drm_atomic_get_old_bridge_state(old_state,
581 if (WARN_ON(!old_bridge_state))
584 iter->funcs->atomic_disable(iter, old_bridge_state);
585 } else if (iter->funcs->disable) {
586 iter->funcs->disable(iter);
593 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
595 static void drm_atomic_bridge_call_post_disable(struct drm_bridge *bridge,
596 struct drm_atomic_state *old_state)
598 if (old_state && bridge->funcs->atomic_post_disable) {
599 struct drm_bridge_state *old_bridge_state;
602 drm_atomic_get_old_bridge_state(old_state,
604 if (WARN_ON(!old_bridge_state))
607 bridge->funcs->atomic_post_disable(bridge,
609 } else if (bridge->funcs->post_disable) {
610 bridge->funcs->post_disable(bridge);
615 * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
616 * in the encoder chain
617 * @bridge: bridge control structure
618 * @old_state: old atomic state
620 * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
621 * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
622 * starting from the first bridge to the last. These are called after completing
623 * &drm_encoder_helper_funcs.atomic_disable
624 * If a bridge sets @pre_enable_upstream_first, then the @post_disable for that
625 * bridge will be called before the previous one to reverse the @pre_enable
628 * Note: the bridge passed should be the one closest to the encoder
630 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
631 struct drm_atomic_state *old_state)
633 struct drm_encoder *encoder;
634 struct drm_bridge *next, *limit;
639 encoder = bridge->encoder;
641 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
644 if (!list_is_last(&bridge->chain_node, &encoder->bridge_chain)) {
645 next = list_next_entry(bridge, chain_node);
647 if (next->pre_enable_upstream_first) {
648 /* Downstream bridge had requested that upstream
649 * was enabled first, so disabled last
653 /* Find the next bridge that has NOT requested
654 * upstream to be enabled first / disabled last
656 list_for_each_entry_from(next, &encoder->bridge_chain,
658 if (next->pre_enable_upstream_first) {
659 next = list_prev_entry(next, chain_node);
665 /* Call these bridges in reverse order */
666 list_for_each_entry_from_reverse(next, &encoder->bridge_chain,
671 drm_atomic_bridge_call_post_disable(next,
677 drm_atomic_bridge_call_post_disable(bridge, old_state);
683 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
685 static void drm_atomic_bridge_call_pre_enable(struct drm_bridge *bridge,
686 struct drm_atomic_state *old_state)
688 if (old_state && bridge->funcs->atomic_pre_enable) {
689 struct drm_bridge_state *old_bridge_state;
692 drm_atomic_get_old_bridge_state(old_state,
694 if (WARN_ON(!old_bridge_state))
697 bridge->funcs->atomic_pre_enable(bridge, old_bridge_state);
698 } else if (bridge->funcs->pre_enable) {
699 bridge->funcs->pre_enable(bridge);
704 * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
706 * @bridge: bridge control structure
707 * @old_state: old atomic state
709 * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
710 * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
711 * starting from the last bridge to the first. These are called before calling
712 * &drm_encoder_helper_funcs.atomic_enable
714 * If a bridge sets @pre_enable_upstream_first, then the pre_enable for the
715 * upstream bridge will be called before pre_enable of this bridge.
717 * Note: the bridge passed should be the one closest to the encoder
719 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
720 struct drm_atomic_state *old_state)
722 struct drm_encoder *encoder;
723 struct drm_bridge *iter, *next, *limit;
728 encoder = bridge->encoder;
730 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
731 if (iter->pre_enable_upstream_first) {
734 list_for_each_entry_from_reverse(next,
735 &encoder->bridge_chain,
740 if (!next->pre_enable_upstream_first) {
741 /* Found first bridge that does NOT
742 * request upstream to be enabled first
744 limit = list_prev_entry(next, chain_node);
749 list_for_each_entry_from(next, &encoder->bridge_chain, chain_node) {
750 /* Call requested upstream bridge pre_enable
754 /* At the first bridgge to request upstream
755 * bridges called first.
759 drm_atomic_bridge_call_pre_enable(next, old_state);
763 drm_atomic_bridge_call_pre_enable(iter, old_state);
765 if (iter->pre_enable_upstream_first)
766 /* Jump all bridges that we have already pre_enabled
774 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
777 * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
778 * @bridge: bridge control structure
779 * @old_state: old atomic state
781 * Calls &drm_bridge_funcs.atomic_enable (falls back on
782 * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
783 * starting from the first bridge to the last. These are called after completing
784 * &drm_encoder_helper_funcs.atomic_enable
786 * Note: the bridge passed should be the one closest to the encoder
788 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
789 struct drm_atomic_state *old_state)
791 struct drm_encoder *encoder;
796 encoder = bridge->encoder;
797 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
798 if (bridge->funcs->atomic_enable) {
799 struct drm_bridge_state *old_bridge_state;
802 drm_atomic_get_old_bridge_state(old_state,
804 if (WARN_ON(!old_bridge_state))
807 bridge->funcs->atomic_enable(bridge, old_bridge_state);
808 } else if (bridge->funcs->enable) {
809 bridge->funcs->enable(bridge);
813 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
815 static int drm_atomic_bridge_check(struct drm_bridge *bridge,
816 struct drm_crtc_state *crtc_state,
817 struct drm_connector_state *conn_state)
819 if (bridge->funcs->atomic_check) {
820 struct drm_bridge_state *bridge_state;
823 bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
825 if (WARN_ON(!bridge_state))
828 ret = bridge->funcs->atomic_check(bridge, bridge_state,
829 crtc_state, conn_state);
832 } else if (bridge->funcs->mode_fixup) {
833 if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode,
834 &crtc_state->adjusted_mode))
841 static int select_bus_fmt_recursive(struct drm_bridge *first_bridge,
842 struct drm_bridge *cur_bridge,
843 struct drm_crtc_state *crtc_state,
844 struct drm_connector_state *conn_state,
847 struct drm_bridge_state *cur_state;
848 unsigned int num_in_bus_fmts, i;
849 struct drm_bridge *prev_bridge;
853 prev_bridge = drm_bridge_get_prev_bridge(cur_bridge);
854 cur_state = drm_atomic_get_new_bridge_state(crtc_state->state,
858 * If bus format negotiation is not supported by this bridge, let's
859 * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and
860 * hope that it can handle this situation gracefully (by providing
861 * appropriate default values).
863 if (!cur_bridge->funcs->atomic_get_input_bus_fmts) {
864 if (cur_bridge != first_bridge) {
865 ret = select_bus_fmt_recursive(first_bridge,
866 prev_bridge, crtc_state,
868 MEDIA_BUS_FMT_FIXED);
874 * Driver does not implement the atomic state hooks, but that's
875 * fine, as long as it does not access the bridge state.
878 cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
879 cur_state->output_bus_cfg.format = out_bus_fmt;
886 * If the driver implements ->atomic_get_input_bus_fmts() it
887 * should also implement the atomic state hooks.
889 if (WARN_ON(!cur_state))
892 in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge,
898 if (!num_in_bus_fmts)
900 else if (!in_bus_fmts)
903 if (first_bridge == cur_bridge) {
904 cur_state->input_bus_cfg.format = in_bus_fmts[0];
905 cur_state->output_bus_cfg.format = out_bus_fmt;
910 for (i = 0; i < num_in_bus_fmts; i++) {
911 ret = select_bus_fmt_recursive(first_bridge, prev_bridge,
912 crtc_state, conn_state,
914 if (ret != -ENOTSUPP)
919 cur_state->input_bus_cfg.format = in_bus_fmts[i];
920 cur_state->output_bus_cfg.format = out_bus_fmt;
928 * This function is called by &drm_atomic_bridge_chain_check() just before
929 * calling &drm_bridge_funcs.atomic_check() on all elements of the chain.
930 * It performs bus format negotiation between bridge elements. The negotiation
931 * happens in reverse order, starting from the last element in the chain up to
934 * Negotiation starts by retrieving supported output bus formats on the last
935 * bridge element and testing them one by one. The test is recursive, meaning
936 * that for each tested output format, the whole chain will be walked backward,
937 * and each element will have to choose an input bus format that can be
938 * transcoded to the requested output format. When a bridge element does not
939 * support transcoding into a specific output format -ENOTSUPP is returned and
940 * the next bridge element will have to try a different format. If none of the
941 * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail.
943 * This implementation is relying on
944 * &drm_bridge_funcs.atomic_get_output_bus_fmts() and
945 * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported
946 * input/output formats.
948 * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by
949 * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts()
950 * tries a single format: &drm_connector.display_info.bus_formats[0] if
951 * available, MEDIA_BUS_FMT_FIXED otherwise.
953 * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented,
954 * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the
955 * bridge element that lacks this hook and asks the previous element in the
956 * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what
957 * to do in that case (fail if they want to enforce bus format negotiation, or
958 * provide a reasonable default if they need to support pipelines where not
959 * all elements support bus format negotiation).
962 drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge,
963 struct drm_crtc_state *crtc_state,
964 struct drm_connector_state *conn_state)
966 struct drm_connector *conn = conn_state->connector;
967 struct drm_encoder *encoder = bridge->encoder;
968 struct drm_bridge_state *last_bridge_state;
969 unsigned int i, num_out_bus_fmts;
970 struct drm_bridge *last_bridge;
974 last_bridge = list_last_entry(&encoder->bridge_chain,
975 struct drm_bridge, chain_node);
976 last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
979 if (last_bridge->funcs->atomic_get_output_bus_fmts) {
980 const struct drm_bridge_funcs *funcs = last_bridge->funcs;
983 * If the driver implements ->atomic_get_output_bus_fmts() it
984 * should also implement the atomic state hooks.
986 if (WARN_ON(!last_bridge_state))
989 out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge,
994 if (!num_out_bus_fmts)
996 else if (!out_bus_fmts)
999 num_out_bus_fmts = 1;
1000 out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL);
1004 if (conn->display_info.num_bus_formats &&
1005 conn->display_info.bus_formats)
1006 out_bus_fmts[0] = conn->display_info.bus_formats[0];
1008 out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
1011 for (i = 0; i < num_out_bus_fmts; i++) {
1012 ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state,
1013 conn_state, out_bus_fmts[i]);
1014 if (ret != -ENOTSUPP)
1018 kfree(out_bus_fmts);
1024 drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge,
1025 struct drm_connector *conn,
1026 struct drm_atomic_state *state)
1028 struct drm_bridge_state *bridge_state, *next_bridge_state;
1029 struct drm_bridge *next_bridge;
1030 u32 output_flags = 0;
1032 bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
1034 /* No bridge state attached to this bridge => nothing to propagate. */
1038 next_bridge = drm_bridge_get_next_bridge(bridge);
1041 * Let's try to apply the most common case here, that is, propagate
1042 * display_info flags for the last bridge, and propagate the input
1043 * flags of the next bridge element to the output end of the current
1044 * bridge when the bridge is not the last one.
1045 * There are exceptions to this rule, like when signal inversion is
1046 * happening at the board level, but that's something drivers can deal
1047 * with from their &drm_bridge_funcs.atomic_check() implementation by
1048 * simply overriding the flags value we've set here.
1051 output_flags = conn->display_info.bus_flags;
1053 next_bridge_state = drm_atomic_get_new_bridge_state(state,
1056 * No bridge state attached to the next bridge, just leave the
1059 if (next_bridge_state)
1060 output_flags = next_bridge_state->input_bus_cfg.flags;
1063 bridge_state->output_bus_cfg.flags = output_flags;
1066 * Propagate the output flags to the input end of the bridge. Again, it's
1067 * not necessarily what all bridges want, but that's what most of them
1068 * do, and by doing that by default we avoid forcing drivers to
1069 * duplicate the "dummy propagation" logic.
1071 bridge_state->input_bus_cfg.flags = output_flags;
1075 * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain
1076 * @bridge: bridge control structure
1077 * @crtc_state: new CRTC state
1078 * @conn_state: new connector state
1080 * First trigger a bus format negotiation before calling
1081 * &drm_bridge_funcs.atomic_check() (falls back on
1082 * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain,
1083 * starting from the last bridge to the first. These are called before calling
1084 * &drm_encoder_helper_funcs.atomic_check()
1087 * 0 on success, a negative error code on failure
1089 int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,
1090 struct drm_crtc_state *crtc_state,
1091 struct drm_connector_state *conn_state)
1093 struct drm_connector *conn = conn_state->connector;
1094 struct drm_encoder *encoder;
1095 struct drm_bridge *iter;
1101 ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state,
1106 encoder = bridge->encoder;
1107 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
1111 * Bus flags are propagated by default. If a bridge needs to
1112 * tweak the input bus flags for any reason, it should happen
1113 * in its &drm_bridge_funcs.atomic_check() implementation such
1114 * that preceding bridges in the chain can propagate the new
1117 drm_atomic_bridge_propagate_bus_flags(iter, conn,
1120 ret = drm_atomic_bridge_check(iter, crtc_state, conn_state);
1130 EXPORT_SYMBOL(drm_atomic_bridge_chain_check);
1133 * drm_bridge_detect - check if anything is attached to the bridge output
1134 * @bridge: bridge control structure
1136 * If the bridge supports output detection, as reported by the
1137 * DRM_BRIDGE_OP_DETECT bridge ops flag, call &drm_bridge_funcs.detect for the
1138 * bridge and return the connection status. Otherwise return
1139 * connector_status_unknown.
1142 * The detection status on success, or connector_status_unknown if the bridge
1143 * doesn't support output detection.
1145 enum drm_connector_status drm_bridge_detect(struct drm_bridge *bridge)
1147 if (!(bridge->ops & DRM_BRIDGE_OP_DETECT))
1148 return connector_status_unknown;
1150 return bridge->funcs->detect(bridge);
1152 EXPORT_SYMBOL_GPL(drm_bridge_detect);
1155 * drm_bridge_get_modes - fill all modes currently valid for the sink into the
1157 * @bridge: bridge control structure
1158 * @connector: the connector to fill with modes
1160 * If the bridge supports output modes retrieval, as reported by the
1161 * DRM_BRIDGE_OP_MODES bridge ops flag, call &drm_bridge_funcs.get_modes to
1162 * fill the connector with all valid modes and return the number of modes
1163 * added. Otherwise return 0.
1166 * The number of modes added to the connector.
1168 int drm_bridge_get_modes(struct drm_bridge *bridge,
1169 struct drm_connector *connector)
1171 if (!(bridge->ops & DRM_BRIDGE_OP_MODES))
1174 return bridge->funcs->get_modes(bridge, connector);
1176 EXPORT_SYMBOL_GPL(drm_bridge_get_modes);
1179 * drm_bridge_get_edid - get the EDID data of the connected display
1180 * @bridge: bridge control structure
1181 * @connector: the connector to read EDID for
1183 * If the bridge supports output EDID retrieval, as reported by the
1184 * DRM_BRIDGE_OP_EDID bridge ops flag, call &drm_bridge_funcs.get_edid to
1185 * get the EDID and return it. Otherwise return NULL.
1188 * The retrieved EDID on success, or NULL otherwise.
1190 struct edid *drm_bridge_get_edid(struct drm_bridge *bridge,
1191 struct drm_connector *connector)
1193 if (!(bridge->ops & DRM_BRIDGE_OP_EDID))
1196 return bridge->funcs->get_edid(bridge, connector);
1198 EXPORT_SYMBOL_GPL(drm_bridge_get_edid);
1201 * drm_bridge_hpd_enable - enable hot plug detection for the bridge
1202 * @bridge: bridge control structure
1203 * @cb: hot-plug detection callback
1204 * @data: data to be passed to the hot-plug detection callback
1206 * Call &drm_bridge_funcs.hpd_enable if implemented and register the given @cb
1207 * and @data as hot plug notification callback. From now on the @cb will be
1208 * called with @data when an output status change is detected by the bridge,
1209 * until hot plug notification gets disabled with drm_bridge_hpd_disable().
1211 * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1212 * bridge->ops. This function shall not be called when the flag is not set.
1214 * Only one hot plug detection callback can be registered at a time, it is an
1215 * error to call this function when hot plug detection is already enabled for
1218 void drm_bridge_hpd_enable(struct drm_bridge *bridge,
1219 void (*cb)(void *data,
1220 enum drm_connector_status status),
1223 if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1226 mutex_lock(&bridge->hpd_mutex);
1228 if (WARN(bridge->hpd_cb, "Hot plug detection already enabled\n"))
1231 bridge->hpd_cb = cb;
1232 bridge->hpd_data = data;
1234 if (bridge->funcs->hpd_enable)
1235 bridge->funcs->hpd_enable(bridge);
1238 mutex_unlock(&bridge->hpd_mutex);
1240 EXPORT_SYMBOL_GPL(drm_bridge_hpd_enable);
1243 * drm_bridge_hpd_disable - disable hot plug detection for the bridge
1244 * @bridge: bridge control structure
1246 * Call &drm_bridge_funcs.hpd_disable if implemented and unregister the hot
1247 * plug detection callback previously registered with drm_bridge_hpd_enable().
1248 * Once this function returns the callback will not be called by the bridge
1249 * when an output status change occurs.
1251 * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1252 * bridge->ops. This function shall not be called when the flag is not set.
1254 void drm_bridge_hpd_disable(struct drm_bridge *bridge)
1256 if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1259 mutex_lock(&bridge->hpd_mutex);
1260 if (bridge->funcs->hpd_disable)
1261 bridge->funcs->hpd_disable(bridge);
1263 bridge->hpd_cb = NULL;
1264 bridge->hpd_data = NULL;
1265 mutex_unlock(&bridge->hpd_mutex);
1267 EXPORT_SYMBOL_GPL(drm_bridge_hpd_disable);
1270 * drm_bridge_hpd_notify - notify hot plug detection events
1271 * @bridge: bridge control structure
1272 * @status: output connection status
1274 * Bridge drivers shall call this function to report hot plug events when they
1275 * detect a change in the output status, when hot plug detection has been
1276 * enabled by drm_bridge_hpd_enable().
1278 * This function shall be called in a context that can sleep.
1280 void drm_bridge_hpd_notify(struct drm_bridge *bridge,
1281 enum drm_connector_status status)
1283 mutex_lock(&bridge->hpd_mutex);
1285 bridge->hpd_cb(bridge->hpd_data, status);
1286 mutex_unlock(&bridge->hpd_mutex);
1288 EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify);
1292 * of_drm_find_bridge - find the bridge corresponding to the device node in
1293 * the global bridge list
1298 * drm_bridge control struct on success, NULL on failure
1300 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
1302 struct drm_bridge *bridge;
1304 mutex_lock(&bridge_lock);
1306 list_for_each_entry(bridge, &bridge_list, list) {
1307 if (bridge->of_node == np) {
1308 mutex_unlock(&bridge_lock);
1313 mutex_unlock(&bridge_lock);
1316 EXPORT_SYMBOL(of_drm_find_bridge);
1319 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
1320 MODULE_DESCRIPTION("DRM bridge infrastructure");
1321 MODULE_LICENSE("GPL and additional rights");