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_of.h>
32 #include <drm/drm_print.h>
34 #include "drm_crtc_internal.h"
39 * &struct drm_bridge represents a device that hangs on to an encoder. These are
40 * handy when a regular &drm_encoder entity isn't enough to represent the entire
43 * A bridge is always attached to a single &drm_encoder at a time, but can be
44 * either connected to it directly, or through a chain of bridges::
46 * [ CRTC ---> ] Encoder ---> Bridge A ---> Bridge B
48 * Here, the output of the encoder feeds to bridge A, and that furthers feeds to
49 * bridge B. Bridge chains can be arbitrarily long, and shall be fully linear:
50 * Chaining multiple bridges to the output of a bridge, or the same bridge to
51 * the output of different bridges, is not supported.
53 * &drm_bridge, like &drm_panel, aren't &drm_mode_object entities like planes,
54 * CRTCs, encoders or connectors and hence are not visible to userspace. They
55 * just provide additional hooks to get the desired output at the end of the
60 * DOC: display driver integration
62 * Display drivers are responsible for linking encoders with the first bridge
63 * in the chains. This is done by acquiring the appropriate bridge with
64 * devm_drm_of_get_bridge(). Once acquired, the bridge shall be attached to the
65 * encoder with a call to drm_bridge_attach().
67 * Bridges are responsible for linking themselves with the next bridge in the
68 * chain, if any. This is done the same way as for encoders, with the call to
69 * drm_bridge_attach() occurring in the &drm_bridge_funcs.attach operation.
71 * Once these links are created, the bridges can participate along with encoder
72 * functions to perform mode validation and fixup (through
73 * drm_bridge_chain_mode_valid() and drm_atomic_bridge_chain_check()), mode
74 * setting (through drm_bridge_chain_mode_set()), enable (through
75 * drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_enable())
76 * and disable (through drm_atomic_bridge_chain_disable() and
77 * drm_atomic_bridge_chain_post_disable()). Those functions call the
78 * corresponding operations provided in &drm_bridge_funcs in sequence for all
79 * bridges in the chain.
81 * For display drivers that use the atomic helpers
82 * drm_atomic_helper_check_modeset(),
83 * drm_atomic_helper_commit_modeset_enables() and
84 * drm_atomic_helper_commit_modeset_disables() (either directly in hand-rolled
85 * commit check and commit tail handlers, or through the higher-level
86 * drm_atomic_helper_check() and drm_atomic_helper_commit_tail() or
87 * drm_atomic_helper_commit_tail_rpm() helpers), this is done transparently and
88 * requires no intervention from the driver. For other drivers, the relevant
89 * DRM bridge chain functions shall be called manually.
91 * Bridges also participate in implementing the &drm_connector at the end of
92 * the bridge chain. Display drivers may use the drm_bridge_connector_init()
93 * helper to create the &drm_connector, or implement it manually on top of the
94 * connector-related operations exposed by the bridge (see the overview
95 * documentation of bridge operations for more details).
99 * DOC: special care dsi
101 * The interaction between the bridges and other frameworks involved in
102 * the probing of the upstream driver and the bridge driver can be
103 * challenging. Indeed, there's multiple cases that needs to be
106 * - The upstream driver doesn't use the component framework and isn't a
107 * MIPI-DSI host. In this case, the bridge driver will probe at some
108 * point and the upstream driver should try to probe again by returning
109 * EPROBE_DEFER as long as the bridge driver hasn't probed.
111 * - The upstream driver doesn't use the component framework, but is a
112 * MIPI-DSI host. The bridge device uses the MIPI-DCS commands to be
113 * controlled. In this case, the bridge device is a child of the
114 * display device and when it will probe it's assured that the display
115 * device (and MIPI-DSI host) is present. The upstream driver will be
116 * assured that the bridge driver is connected between the
117 * &mipi_dsi_host_ops.attach and &mipi_dsi_host_ops.detach operations.
118 * Therefore, it must run mipi_dsi_host_register() in its probe
119 * function, and then run drm_bridge_attach() in its
120 * &mipi_dsi_host_ops.attach hook.
122 * - The upstream driver uses the component framework and is a MIPI-DSI
123 * host. The bridge device uses the MIPI-DCS commands to be
124 * controlled. This is the same situation than above, and can run
125 * mipi_dsi_host_register() in either its probe or bind hooks.
127 * - The upstream driver uses the component framework and is a MIPI-DSI
128 * host. The bridge device uses a separate bus (such as I2C) to be
129 * controlled. In this case, there's no correlation between the probe
130 * of the bridge and upstream drivers, so care must be taken to avoid
131 * an endless EPROBE_DEFER loop, with each driver waiting for the
134 * The ideal pattern to cover the last item (and all the others in the
135 * MIPI-DSI host driver case) is to split the operations like this:
137 * - The MIPI-DSI host driver must run mipi_dsi_host_register() in its
138 * probe hook. It will make sure that the MIPI-DSI host sticks around,
139 * and that the driver's bind can be called.
141 * - In its probe hook, the bridge driver must try to find its MIPI-DSI
142 * host, register as a MIPI-DSI device and attach the MIPI-DSI device
143 * to its host. The bridge driver is now functional.
145 * - In its &struct mipi_dsi_host_ops.attach hook, the MIPI-DSI host can
146 * now add its component. Its bind hook will now be called and since
147 * the bridge driver is attached and registered, we can now look for
150 * At this point, we're now certain that both the upstream driver and
151 * the bridge driver are functional and we can't have a deadlock-like
152 * situation when probing.
155 static DEFINE_MUTEX(bridge_lock);
156 static LIST_HEAD(bridge_list);
159 * drm_bridge_add - add the given bridge to the global bridge list
161 * @bridge: bridge control structure
163 void drm_bridge_add(struct drm_bridge *bridge)
165 mutex_init(&bridge->hpd_mutex);
167 mutex_lock(&bridge_lock);
168 list_add_tail(&bridge->list, &bridge_list);
169 mutex_unlock(&bridge_lock);
171 EXPORT_SYMBOL(drm_bridge_add);
173 static void drm_bridge_remove_void(void *bridge)
175 drm_bridge_remove(bridge);
179 * devm_drm_bridge_add - devm managed version of drm_bridge_add()
181 * @dev: device to tie the bridge lifetime to
182 * @bridge: bridge control structure
184 * This is the managed version of drm_bridge_add() which automatically
185 * calls drm_bridge_remove() when @dev is unbound.
187 * Return: 0 if no error or negative error code.
189 int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge)
191 drm_bridge_add(bridge);
192 return devm_add_action_or_reset(dev, drm_bridge_remove_void, bridge);
194 EXPORT_SYMBOL(devm_drm_bridge_add);
197 * drm_bridge_remove - remove the given bridge from the global bridge list
199 * @bridge: bridge control structure
201 void drm_bridge_remove(struct drm_bridge *bridge)
203 mutex_lock(&bridge_lock);
204 list_del_init(&bridge->list);
205 mutex_unlock(&bridge_lock);
207 mutex_destroy(&bridge->hpd_mutex);
209 EXPORT_SYMBOL(drm_bridge_remove);
211 static struct drm_private_state *
212 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)
214 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
215 struct drm_bridge_state *state;
217 state = bridge->funcs->atomic_duplicate_state(bridge);
218 return state ? &state->base : NULL;
222 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,
223 struct drm_private_state *s)
225 struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
226 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
228 bridge->funcs->atomic_destroy_state(bridge, state);
231 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
232 .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
233 .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
237 * drm_bridge_attach - attach the bridge to an encoder's chain
239 * @encoder: DRM encoder
240 * @bridge: bridge to attach
241 * @previous: previous bridge in the chain (optional)
242 * @flags: DRM_BRIDGE_ATTACH_* flags
244 * Called by a kms driver to link the bridge to an encoder's chain. The previous
245 * argument specifies the previous bridge in the chain. If NULL, the bridge is
246 * linked directly at the encoder's output. Otherwise it is linked at the
247 * previous bridge's output.
249 * If non-NULL the previous bridge must be already attached by a call to this
252 * Note that bridges attached to encoders are auto-detached during encoder
253 * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
254 * *not* be balanced with a drm_bridge_detach() in driver code.
257 * Zero on success, error code on failure
259 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
260 struct drm_bridge *previous,
261 enum drm_bridge_attach_flags flags)
265 if (!encoder || !bridge)
268 if (previous && (!previous->dev || previous->encoder != encoder))
274 bridge->dev = encoder->dev;
275 bridge->encoder = encoder;
278 list_add(&bridge->chain_node, &previous->chain_node);
280 list_add(&bridge->chain_node, &encoder->bridge_chain);
282 if (bridge->funcs->attach) {
283 ret = bridge->funcs->attach(bridge, flags);
285 goto err_reset_bridge;
288 if (bridge->funcs->atomic_reset) {
289 struct drm_bridge_state *state;
291 state = bridge->funcs->atomic_reset(bridge);
293 ret = PTR_ERR(state);
294 goto err_detach_bridge;
297 drm_atomic_private_obj_init(bridge->dev, &bridge->base,
299 &drm_bridge_priv_state_funcs);
305 if (bridge->funcs->detach)
306 bridge->funcs->detach(bridge);
310 bridge->encoder = NULL;
311 list_del(&bridge->chain_node);
314 DRM_ERROR("failed to attach bridge %pOF to encoder %s: %d\n",
315 bridge->of_node, encoder->name, ret);
317 DRM_ERROR("failed to attach bridge to encoder %s: %d\n",
323 EXPORT_SYMBOL(drm_bridge_attach);
325 void drm_bridge_detach(struct drm_bridge *bridge)
327 if (WARN_ON(!bridge))
330 if (WARN_ON(!bridge->dev))
333 if (bridge->funcs->atomic_reset)
334 drm_atomic_private_obj_fini(&bridge->base);
336 if (bridge->funcs->detach)
337 bridge->funcs->detach(bridge);
339 list_del(&bridge->chain_node);
344 * DOC: bridge operations
346 * Bridge drivers expose operations through the &drm_bridge_funcs structure.
347 * The DRM internals (atomic and CRTC helpers) use the helpers defined in
348 * drm_bridge.c to call bridge operations. Those operations are divided in
349 * three big categories to support different parts of the bridge usage.
351 * - The encoder-related operations support control of the bridges in the
352 * chain, and are roughly counterparts to the &drm_encoder_helper_funcs
353 * operations. They are used by the legacy CRTC and the atomic modeset
354 * helpers to perform mode validation, fixup and setting, and enable and
355 * disable the bridge automatically.
357 * The enable and disable operations are split in
358 * &drm_bridge_funcs.pre_enable, &drm_bridge_funcs.enable,
359 * &drm_bridge_funcs.disable and &drm_bridge_funcs.post_disable to provide
360 * finer-grained control.
362 * Bridge drivers may implement the legacy version of those operations, or
363 * the atomic version (prefixed with atomic\_), in which case they shall also
364 * implement the atomic state bookkeeping operations
365 * (&drm_bridge_funcs.atomic_duplicate_state,
366 * &drm_bridge_funcs.atomic_destroy_state and &drm_bridge_funcs.reset).
367 * Mixing atomic and non-atomic versions of the operations is not supported.
369 * - The bus format negotiation operations
370 * &drm_bridge_funcs.atomic_get_output_bus_fmts and
371 * &drm_bridge_funcs.atomic_get_input_bus_fmts allow bridge drivers to
372 * negotiate the formats transmitted between bridges in the chain when
373 * multiple formats are supported. Negotiation for formats is performed
374 * transparently for display drivers by the atomic modeset helpers. Only
375 * atomic versions of those operations exist, bridge drivers that need to
376 * implement them shall thus also implement the atomic version of the
377 * encoder-related operations. This feature is not supported by the legacy
380 * - The connector-related operations support implementing a &drm_connector
381 * based on a chain of bridges. DRM bridges traditionally create a
382 * &drm_connector for bridges meant to be used at the end of the chain. This
383 * puts additional burden on bridge drivers, especially for bridges that may
384 * be used in the middle of a chain or at the end of it. Furthermore, it
385 * requires all operations of the &drm_connector to be handled by a single
386 * bridge, which doesn't always match the hardware architecture.
388 * To simplify bridge drivers and make the connector implementation more
389 * flexible, a new model allows bridges to unconditionally skip creation of
390 * &drm_connector and instead expose &drm_bridge_funcs operations to support
391 * an externally-implemented &drm_connector. Those operations are
392 * &drm_bridge_funcs.detect, &drm_bridge_funcs.get_modes,
393 * &drm_bridge_funcs.get_edid, &drm_bridge_funcs.hpd_notify,
394 * &drm_bridge_funcs.hpd_enable and &drm_bridge_funcs.hpd_disable. When
395 * implemented, display drivers shall create a &drm_connector instance for
396 * each chain of bridges, and implement those connector instances based on
397 * the bridge connector operations.
399 * Bridge drivers shall implement the connector-related operations for all
400 * the features that the bridge hardware support. For instance, if a bridge
401 * supports reading EDID, the &drm_bridge_funcs.get_edid shall be
402 * implemented. This however doesn't mean that the DDC lines are wired to the
403 * bridge on a particular platform, as they could also be connected to an I2C
404 * controller of the SoC. Support for the connector-related operations on the
405 * running platform is reported through the &drm_bridge.ops flags. Bridge
406 * drivers shall detect which operations they can support on the platform
407 * (usually this information is provided by ACPI or DT), and set the
408 * &drm_bridge.ops flags for all supported operations. A flag shall only be
409 * set if the corresponding &drm_bridge_funcs operation is implemented, but
410 * an implemented operation doesn't necessarily imply that the corresponding
411 * flag will be set. Display drivers shall use the &drm_bridge.ops flags to
412 * decide which bridge to delegate a connector operation to. This mechanism
413 * allows providing a single static const &drm_bridge_funcs instance in
414 * bridge drivers, improving security by storing function pointers in
417 * In order to ease transition, bridge drivers may support both the old and
418 * new models by making connector creation optional and implementing the
419 * connected-related bridge operations. Connector creation is then controlled
420 * by the flags argument to the drm_bridge_attach() function. Display drivers
421 * that support the new model and create connectors themselves shall set the
422 * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag, and bridge drivers shall then skip
423 * connector creation. For intermediate bridges in the chain, the flag shall
424 * be passed to the drm_bridge_attach() call for the downstream bridge.
425 * Bridge drivers that implement the new model only shall return an error
426 * from their &drm_bridge_funcs.attach handler when the
427 * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag is not set. New display drivers
428 * should use the new model, and convert the bridge drivers they use if
429 * needed, in order to gradually transition to the new model.
433 * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
435 * @bridge: bridge control structure
436 * @mode: desired mode to be set for the bridge
437 * @adjusted_mode: updated mode that works for this bridge
439 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
440 * encoder chain, starting from the first bridge to the last.
442 * Note: the bridge passed should be the one closest to the encoder
445 * true on success, false on failure
447 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
448 const struct drm_display_mode *mode,
449 struct drm_display_mode *adjusted_mode)
451 struct drm_encoder *encoder;
456 encoder = bridge->encoder;
457 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
458 if (!bridge->funcs->mode_fixup)
461 if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
467 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
470 * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
472 * @bridge: bridge control structure
473 * @info: display info against which the mode shall be validated
474 * @mode: desired mode to be validated
476 * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
477 * chain, starting from the first bridge to the last. If at least one bridge
478 * does not accept the mode the function returns the error code.
480 * Note: the bridge passed should be the one closest to the encoder.
483 * MODE_OK on success, drm_mode_status Enum error code on failure
486 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
487 const struct drm_display_info *info,
488 const struct drm_display_mode *mode)
490 struct drm_encoder *encoder;
495 encoder = bridge->encoder;
496 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
497 enum drm_mode_status ret;
499 if (!bridge->funcs->mode_valid)
502 ret = bridge->funcs->mode_valid(bridge, info, mode);
509 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
512 * drm_bridge_chain_disable - disables all bridges in the encoder chain
513 * @bridge: bridge control structure
515 * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
516 * chain, starting from the last bridge to the first. These are called before
517 * calling the encoder's prepare op.
519 * Note: the bridge passed should be the one closest to the encoder
521 void drm_bridge_chain_disable(struct drm_bridge *bridge)
523 struct drm_encoder *encoder;
524 struct drm_bridge *iter;
529 encoder = bridge->encoder;
530 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
531 if (iter->funcs->disable)
532 iter->funcs->disable(iter);
538 EXPORT_SYMBOL(drm_bridge_chain_disable);
541 * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the
543 * @bridge: bridge control structure
545 * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
546 * encoder chain, starting from the first bridge to the last. These are called
547 * after completing the encoder's prepare op.
549 * Note: the bridge passed should be the one closest to the encoder
551 void drm_bridge_chain_post_disable(struct drm_bridge *bridge)
553 struct drm_encoder *encoder;
558 encoder = bridge->encoder;
559 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
560 if (bridge->funcs->post_disable)
561 bridge->funcs->post_disable(bridge);
564 EXPORT_SYMBOL(drm_bridge_chain_post_disable);
567 * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
569 * @bridge: bridge control structure
570 * @mode: desired mode to be set for the encoder chain
571 * @adjusted_mode: updated mode that works for this encoder chain
573 * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
574 * encoder chain, starting from the first bridge to the last.
576 * Note: the bridge passed should be the one closest to the encoder
578 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
579 const struct drm_display_mode *mode,
580 const struct drm_display_mode *adjusted_mode)
582 struct drm_encoder *encoder;
587 encoder = bridge->encoder;
588 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
589 if (bridge->funcs->mode_set)
590 bridge->funcs->mode_set(bridge, mode, adjusted_mode);
593 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
596 * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the
598 * @bridge: bridge control structure
600 * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
601 * chain, starting from the last bridge to the first. These are called
602 * before calling the encoder's commit op.
604 * Note: the bridge passed should be the one closest to the encoder
606 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge)
608 struct drm_encoder *encoder;
609 struct drm_bridge *iter;
614 encoder = bridge->encoder;
615 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
616 if (iter->funcs->pre_enable)
617 iter->funcs->pre_enable(iter);
623 EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
626 * drm_bridge_chain_enable - enables all bridges in the encoder chain
627 * @bridge: bridge control structure
629 * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
630 * chain, starting from the first bridge to the last. These are called
631 * after completing the encoder's commit op.
633 * Note that the bridge passed should be the one closest to the encoder
635 void drm_bridge_chain_enable(struct drm_bridge *bridge)
637 struct drm_encoder *encoder;
642 encoder = bridge->encoder;
643 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
644 if (bridge->funcs->enable)
645 bridge->funcs->enable(bridge);
648 EXPORT_SYMBOL(drm_bridge_chain_enable);
651 * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
652 * @bridge: bridge control structure
653 * @old_state: old atomic state
655 * Calls &drm_bridge_funcs.atomic_disable (falls back on
656 * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
657 * starting from the last bridge to the first. These are called before calling
658 * &drm_encoder_helper_funcs.atomic_disable
660 * Note: the bridge passed should be the one closest to the encoder
662 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
663 struct drm_atomic_state *old_state)
665 struct drm_encoder *encoder;
666 struct drm_bridge *iter;
671 encoder = bridge->encoder;
672 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
673 if (iter->funcs->atomic_disable) {
674 struct drm_bridge_state *old_bridge_state;
677 drm_atomic_get_old_bridge_state(old_state,
679 if (WARN_ON(!old_bridge_state))
682 iter->funcs->atomic_disable(iter, old_bridge_state);
683 } else if (iter->funcs->disable) {
684 iter->funcs->disable(iter);
691 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
694 * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
695 * in the encoder chain
696 * @bridge: bridge control structure
697 * @old_state: old atomic state
699 * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
700 * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
701 * starting from the first bridge to the last. These are called after completing
702 * &drm_encoder_helper_funcs.atomic_disable
704 * Note: the bridge passed should be the one closest to the encoder
706 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
707 struct drm_atomic_state *old_state)
709 struct drm_encoder *encoder;
714 encoder = bridge->encoder;
715 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
716 if (bridge->funcs->atomic_post_disable) {
717 struct drm_bridge_state *old_bridge_state;
720 drm_atomic_get_old_bridge_state(old_state,
722 if (WARN_ON(!old_bridge_state))
725 bridge->funcs->atomic_post_disable(bridge,
727 } else if (bridge->funcs->post_disable) {
728 bridge->funcs->post_disable(bridge);
732 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
735 * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
737 * @bridge: bridge control structure
738 * @old_state: old atomic state
740 * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
741 * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
742 * starting from the last bridge to the first. These are called before calling
743 * &drm_encoder_helper_funcs.atomic_enable
745 * Note: the bridge passed should be the one closest to the encoder
747 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
748 struct drm_atomic_state *old_state)
750 struct drm_encoder *encoder;
751 struct drm_bridge *iter;
756 encoder = bridge->encoder;
757 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
758 if (iter->funcs->atomic_pre_enable) {
759 struct drm_bridge_state *old_bridge_state;
762 drm_atomic_get_old_bridge_state(old_state,
764 if (WARN_ON(!old_bridge_state))
767 iter->funcs->atomic_pre_enable(iter, old_bridge_state);
768 } else if (iter->funcs->pre_enable) {
769 iter->funcs->pre_enable(iter);
776 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
779 * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
780 * @bridge: bridge control structure
781 * @old_state: old atomic state
783 * Calls &drm_bridge_funcs.atomic_enable (falls back on
784 * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
785 * starting from the first bridge to the last. These are called after completing
786 * &drm_encoder_helper_funcs.atomic_enable
788 * Note: the bridge passed should be the one closest to the encoder
790 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
791 struct drm_atomic_state *old_state)
793 struct drm_encoder *encoder;
798 encoder = bridge->encoder;
799 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
800 if (bridge->funcs->atomic_enable) {
801 struct drm_bridge_state *old_bridge_state;
804 drm_atomic_get_old_bridge_state(old_state,
806 if (WARN_ON(!old_bridge_state))
809 bridge->funcs->atomic_enable(bridge, old_bridge_state);
810 } else if (bridge->funcs->enable) {
811 bridge->funcs->enable(bridge);
815 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
817 static int drm_atomic_bridge_check(struct drm_bridge *bridge,
818 struct drm_crtc_state *crtc_state,
819 struct drm_connector_state *conn_state)
821 if (bridge->funcs->atomic_check) {
822 struct drm_bridge_state *bridge_state;
825 bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
827 if (WARN_ON(!bridge_state))
830 ret = bridge->funcs->atomic_check(bridge, bridge_state,
831 crtc_state, conn_state);
834 } else if (bridge->funcs->mode_fixup) {
835 if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode,
836 &crtc_state->adjusted_mode))
843 static int select_bus_fmt_recursive(struct drm_bridge *first_bridge,
844 struct drm_bridge *cur_bridge,
845 struct drm_crtc_state *crtc_state,
846 struct drm_connector_state *conn_state,
849 struct drm_bridge_state *cur_state;
850 unsigned int num_in_bus_fmts, i;
851 struct drm_bridge *prev_bridge;
855 prev_bridge = drm_bridge_get_prev_bridge(cur_bridge);
856 cur_state = drm_atomic_get_new_bridge_state(crtc_state->state,
860 * If bus format negotiation is not supported by this bridge, let's
861 * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and
862 * hope that it can handle this situation gracefully (by providing
863 * appropriate default values).
865 if (!cur_bridge->funcs->atomic_get_input_bus_fmts) {
866 if (cur_bridge != first_bridge) {
867 ret = select_bus_fmt_recursive(first_bridge,
868 prev_bridge, crtc_state,
870 MEDIA_BUS_FMT_FIXED);
876 * Driver does not implement the atomic state hooks, but that's
877 * fine, as long as it does not access the bridge state.
880 cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
881 cur_state->output_bus_cfg.format = out_bus_fmt;
888 * If the driver implements ->atomic_get_input_bus_fmts() it
889 * should also implement the atomic state hooks.
891 if (WARN_ON(!cur_state))
894 in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge,
900 if (!num_in_bus_fmts)
902 else if (!in_bus_fmts)
905 if (first_bridge == cur_bridge) {
906 cur_state->input_bus_cfg.format = in_bus_fmts[0];
907 cur_state->output_bus_cfg.format = out_bus_fmt;
912 for (i = 0; i < num_in_bus_fmts; i++) {
913 ret = select_bus_fmt_recursive(first_bridge, prev_bridge,
914 crtc_state, conn_state,
916 if (ret != -ENOTSUPP)
921 cur_state->input_bus_cfg.format = in_bus_fmts[i];
922 cur_state->output_bus_cfg.format = out_bus_fmt;
930 * This function is called by &drm_atomic_bridge_chain_check() just before
931 * calling &drm_bridge_funcs.atomic_check() on all elements of the chain.
932 * It performs bus format negotiation between bridge elements. The negotiation
933 * happens in reverse order, starting from the last element in the chain up to
936 * Negotiation starts by retrieving supported output bus formats on the last
937 * bridge element and testing them one by one. The test is recursive, meaning
938 * that for each tested output format, the whole chain will be walked backward,
939 * and each element will have to choose an input bus format that can be
940 * transcoded to the requested output format. When a bridge element does not
941 * support transcoding into a specific output format -ENOTSUPP is returned and
942 * the next bridge element will have to try a different format. If none of the
943 * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail.
945 * This implementation is relying on
946 * &drm_bridge_funcs.atomic_get_output_bus_fmts() and
947 * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported
948 * input/output formats.
950 * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by
951 * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts()
952 * tries a single format: &drm_connector.display_info.bus_formats[0] if
953 * available, MEDIA_BUS_FMT_FIXED otherwise.
955 * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented,
956 * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the
957 * bridge element that lacks this hook and asks the previous element in the
958 * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what
959 * to do in that case (fail if they want to enforce bus format negotiation, or
960 * provide a reasonable default if they need to support pipelines where not
961 * all elements support bus format negotiation).
964 drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge,
965 struct drm_crtc_state *crtc_state,
966 struct drm_connector_state *conn_state)
968 struct drm_connector *conn = conn_state->connector;
969 struct drm_encoder *encoder = bridge->encoder;
970 struct drm_bridge_state *last_bridge_state;
971 unsigned int i, num_out_bus_fmts;
972 struct drm_bridge *last_bridge;
976 last_bridge = list_last_entry(&encoder->bridge_chain,
977 struct drm_bridge, chain_node);
978 last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
981 if (last_bridge->funcs->atomic_get_output_bus_fmts) {
982 const struct drm_bridge_funcs *funcs = last_bridge->funcs;
985 * If the driver implements ->atomic_get_output_bus_fmts() it
986 * should also implement the atomic state hooks.
988 if (WARN_ON(!last_bridge_state))
991 out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge,
996 if (!num_out_bus_fmts)
998 else if (!out_bus_fmts)
1001 num_out_bus_fmts = 1;
1002 out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL);
1006 if (conn->display_info.num_bus_formats &&
1007 conn->display_info.bus_formats)
1008 out_bus_fmts[0] = conn->display_info.bus_formats[0];
1010 out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
1013 for (i = 0; i < num_out_bus_fmts; i++) {
1014 ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state,
1015 conn_state, out_bus_fmts[i]);
1016 if (ret != -ENOTSUPP)
1020 kfree(out_bus_fmts);
1026 drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge,
1027 struct drm_connector *conn,
1028 struct drm_atomic_state *state)
1030 struct drm_bridge_state *bridge_state, *next_bridge_state;
1031 struct drm_bridge *next_bridge;
1032 u32 output_flags = 0;
1034 bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
1036 /* No bridge state attached to this bridge => nothing to propagate. */
1040 next_bridge = drm_bridge_get_next_bridge(bridge);
1043 * Let's try to apply the most common case here, that is, propagate
1044 * display_info flags for the last bridge, and propagate the input
1045 * flags of the next bridge element to the output end of the current
1046 * bridge when the bridge is not the last one.
1047 * There are exceptions to this rule, like when signal inversion is
1048 * happening at the board level, but that's something drivers can deal
1049 * with from their &drm_bridge_funcs.atomic_check() implementation by
1050 * simply overriding the flags value we've set here.
1053 output_flags = conn->display_info.bus_flags;
1055 next_bridge_state = drm_atomic_get_new_bridge_state(state,
1058 * No bridge state attached to the next bridge, just leave the
1061 if (next_bridge_state)
1062 output_flags = next_bridge_state->input_bus_cfg.flags;
1065 bridge_state->output_bus_cfg.flags = output_flags;
1068 * Propagate the output flags to the input end of the bridge. Again, it's
1069 * not necessarily what all bridges want, but that's what most of them
1070 * do, and by doing that by default we avoid forcing drivers to
1071 * duplicate the "dummy propagation" logic.
1073 bridge_state->input_bus_cfg.flags = output_flags;
1077 * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain
1078 * @bridge: bridge control structure
1079 * @crtc_state: new CRTC state
1080 * @conn_state: new connector state
1082 * First trigger a bus format negotiation before calling
1083 * &drm_bridge_funcs.atomic_check() (falls back on
1084 * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain,
1085 * starting from the last bridge to the first. These are called before calling
1086 * &drm_encoder_helper_funcs.atomic_check()
1089 * 0 on success, a negative error code on failure
1091 int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,
1092 struct drm_crtc_state *crtc_state,
1093 struct drm_connector_state *conn_state)
1095 struct drm_connector *conn = conn_state->connector;
1096 struct drm_encoder *encoder;
1097 struct drm_bridge *iter;
1103 ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state,
1108 encoder = bridge->encoder;
1109 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
1113 * Bus flags are propagated by default. If a bridge needs to
1114 * tweak the input bus flags for any reason, it should happen
1115 * in its &drm_bridge_funcs.atomic_check() implementation such
1116 * that preceding bridges in the chain can propagate the new
1119 drm_atomic_bridge_propagate_bus_flags(iter, conn,
1122 ret = drm_atomic_bridge_check(iter, crtc_state, conn_state);
1132 EXPORT_SYMBOL(drm_atomic_bridge_chain_check);
1135 * drm_bridge_detect - check if anything is attached to the bridge output
1136 * @bridge: bridge control structure
1138 * If the bridge supports output detection, as reported by the
1139 * DRM_BRIDGE_OP_DETECT bridge ops flag, call &drm_bridge_funcs.detect for the
1140 * bridge and return the connection status. Otherwise return
1141 * connector_status_unknown.
1144 * The detection status on success, or connector_status_unknown if the bridge
1145 * doesn't support output detection.
1147 enum drm_connector_status drm_bridge_detect(struct drm_bridge *bridge)
1149 if (!(bridge->ops & DRM_BRIDGE_OP_DETECT))
1150 return connector_status_unknown;
1152 return bridge->funcs->detect(bridge);
1154 EXPORT_SYMBOL_GPL(drm_bridge_detect);
1157 * drm_bridge_get_modes - fill all modes currently valid for the sink into the
1159 * @bridge: bridge control structure
1160 * @connector: the connector to fill with modes
1162 * If the bridge supports output modes retrieval, as reported by the
1163 * DRM_BRIDGE_OP_MODES bridge ops flag, call &drm_bridge_funcs.get_modes to
1164 * fill the connector with all valid modes and return the number of modes
1165 * added. Otherwise return 0.
1168 * The number of modes added to the connector.
1170 int drm_bridge_get_modes(struct drm_bridge *bridge,
1171 struct drm_connector *connector)
1173 if (!(bridge->ops & DRM_BRIDGE_OP_MODES))
1176 return bridge->funcs->get_modes(bridge, connector);
1178 EXPORT_SYMBOL_GPL(drm_bridge_get_modes);
1181 * drm_bridge_get_edid - get the EDID data of the connected display
1182 * @bridge: bridge control structure
1183 * @connector: the connector to read EDID for
1185 * If the bridge supports output EDID retrieval, as reported by the
1186 * DRM_BRIDGE_OP_EDID bridge ops flag, call &drm_bridge_funcs.get_edid to
1187 * get the EDID and return it. Otherwise return NULL.
1190 * The retrieved EDID on success, or NULL otherwise.
1192 struct edid *drm_bridge_get_edid(struct drm_bridge *bridge,
1193 struct drm_connector *connector)
1195 if (!(bridge->ops & DRM_BRIDGE_OP_EDID))
1198 return bridge->funcs->get_edid(bridge, connector);
1200 EXPORT_SYMBOL_GPL(drm_bridge_get_edid);
1203 * drm_bridge_hpd_enable - enable hot plug detection for the bridge
1204 * @bridge: bridge control structure
1205 * @cb: hot-plug detection callback
1206 * @data: data to be passed to the hot-plug detection callback
1208 * Call &drm_bridge_funcs.hpd_enable if implemented and register the given @cb
1209 * and @data as hot plug notification callback. From now on the @cb will be
1210 * called with @data when an output status change is detected by the bridge,
1211 * until hot plug notification gets disabled with drm_bridge_hpd_disable().
1213 * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1214 * bridge->ops. This function shall not be called when the flag is not set.
1216 * Only one hot plug detection callback can be registered at a time, it is an
1217 * error to call this function when hot plug detection is already enabled for
1220 void drm_bridge_hpd_enable(struct drm_bridge *bridge,
1221 void (*cb)(void *data,
1222 enum drm_connector_status status),
1225 if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1228 mutex_lock(&bridge->hpd_mutex);
1230 if (WARN(bridge->hpd_cb, "Hot plug detection already enabled\n"))
1233 bridge->hpd_cb = cb;
1234 bridge->hpd_data = data;
1236 if (bridge->funcs->hpd_enable)
1237 bridge->funcs->hpd_enable(bridge);
1240 mutex_unlock(&bridge->hpd_mutex);
1242 EXPORT_SYMBOL_GPL(drm_bridge_hpd_enable);
1245 * drm_bridge_hpd_disable - disable hot plug detection for the bridge
1246 * @bridge: bridge control structure
1248 * Call &drm_bridge_funcs.hpd_disable if implemented and unregister the hot
1249 * plug detection callback previously registered with drm_bridge_hpd_enable().
1250 * Once this function returns the callback will not be called by the bridge
1251 * when an output status change occurs.
1253 * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1254 * bridge->ops. This function shall not be called when the flag is not set.
1256 void drm_bridge_hpd_disable(struct drm_bridge *bridge)
1258 if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1261 mutex_lock(&bridge->hpd_mutex);
1262 if (bridge->funcs->hpd_disable)
1263 bridge->funcs->hpd_disable(bridge);
1265 bridge->hpd_cb = NULL;
1266 bridge->hpd_data = NULL;
1267 mutex_unlock(&bridge->hpd_mutex);
1269 EXPORT_SYMBOL_GPL(drm_bridge_hpd_disable);
1272 * drm_bridge_hpd_notify - notify hot plug detection events
1273 * @bridge: bridge control structure
1274 * @status: output connection status
1276 * Bridge drivers shall call this function to report hot plug events when they
1277 * detect a change in the output status, when hot plug detection has been
1278 * enabled by drm_bridge_hpd_enable().
1280 * This function shall be called in a context that can sleep.
1282 void drm_bridge_hpd_notify(struct drm_bridge *bridge,
1283 enum drm_connector_status status)
1285 mutex_lock(&bridge->hpd_mutex);
1287 bridge->hpd_cb(bridge->hpd_data, status);
1288 mutex_unlock(&bridge->hpd_mutex);
1290 EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify);
1294 * of_drm_find_bridge - find the bridge corresponding to the device node in
1295 * the global bridge list
1300 * drm_bridge control struct on success, NULL on failure
1302 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
1304 struct drm_bridge *bridge;
1306 mutex_lock(&bridge_lock);
1308 list_for_each_entry(bridge, &bridge_list, list) {
1309 if (bridge->of_node == np) {
1310 mutex_unlock(&bridge_lock);
1315 mutex_unlock(&bridge_lock);
1318 EXPORT_SYMBOL(of_drm_find_bridge);
1322 MODULE_DESCRIPTION("DRM bridge infrastructure");
1323 MODULE_LICENSE("GPL and additional rights");