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>
32 #include "drm_crtc_internal.h"
37 * &struct drm_bridge represents a device that hangs on to an encoder. These are
38 * handy when a regular &drm_encoder entity isn't enough to represent the entire
41 * A bridge is always attached to a single &drm_encoder at a time, but can be
42 * either connected to it directly, or through an intermediate bridge::
44 * encoder ---> bridge B ---> bridge A
46 * Here, the output of the encoder feeds to bridge B, and that furthers feeds to
49 * The driver using the bridge is responsible to make the associations between
50 * the encoder and bridges. Once these links are made, the bridges will
51 * participate along with encoder functions to perform mode_set/enable/disable
52 * through the ops provided in &drm_bridge_funcs.
54 * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes,
55 * CRTCs, encoders or connectors and hence are not visible to userspace. They
56 * just provide additional hooks to get the desired output at the end of the
59 * Bridges can also be chained up using the &drm_bridge.chain_node field.
61 * Both legacy CRTC helpers and the new atomic modeset helpers support bridges.
64 static DEFINE_MUTEX(bridge_lock);
65 static LIST_HEAD(bridge_list);
68 * drm_bridge_add - add the given bridge to the global bridge list
70 * @bridge: bridge control structure
72 void drm_bridge_add(struct drm_bridge *bridge)
74 mutex_lock(&bridge_lock);
75 list_add_tail(&bridge->list, &bridge_list);
76 mutex_unlock(&bridge_lock);
78 EXPORT_SYMBOL(drm_bridge_add);
81 * drm_bridge_remove - remove the given bridge from the global bridge list
83 * @bridge: bridge control structure
85 void drm_bridge_remove(struct drm_bridge *bridge)
87 mutex_lock(&bridge_lock);
88 list_del_init(&bridge->list);
89 mutex_unlock(&bridge_lock);
91 EXPORT_SYMBOL(drm_bridge_remove);
93 static struct drm_bridge_state *
94 drm_atomic_default_bridge_duplicate_state(struct drm_bridge *bridge)
96 struct drm_bridge_state *new;
98 if (WARN_ON(!bridge->base.state))
101 new = kzalloc(sizeof(*new), GFP_KERNEL);
103 __drm_atomic_helper_bridge_duplicate_state(bridge, new);
108 static struct drm_private_state *
109 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)
111 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
112 struct drm_bridge_state *state;
114 if (bridge->funcs->atomic_duplicate_state)
115 state = bridge->funcs->atomic_duplicate_state(bridge);
117 state = drm_atomic_default_bridge_duplicate_state(bridge);
119 return state ? &state->base : NULL;
123 drm_atomic_default_bridge_destroy_state(struct drm_bridge *bridge,
124 struct drm_bridge_state *state)
126 /* Just a simple kfree() for now */
131 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,
132 struct drm_private_state *s)
134 struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
135 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
137 if (bridge->funcs->atomic_destroy_state)
138 bridge->funcs->atomic_destroy_state(bridge, state);
140 drm_atomic_default_bridge_destroy_state(bridge, state);
143 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
144 .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
145 .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
148 static struct drm_bridge_state *
149 drm_atomic_default_bridge_reset(struct drm_bridge *bridge)
151 struct drm_bridge_state *bridge_state;
153 bridge_state = kzalloc(sizeof(*bridge_state), GFP_KERNEL);
155 return ERR_PTR(-ENOMEM);
157 __drm_atomic_helper_bridge_reset(bridge, bridge_state);
162 * drm_bridge_attach - attach the bridge to an encoder's chain
164 * @encoder: DRM encoder
165 * @bridge: bridge to attach
166 * @previous: previous bridge in the chain (optional)
168 * Called by a kms driver to link the bridge to an encoder's chain. The previous
169 * argument specifies the previous bridge in the chain. If NULL, the bridge is
170 * linked directly at the encoder's output. Otherwise it is linked at the
171 * previous bridge's output.
173 * If non-NULL the previous bridge must be already attached by a call to this
176 * Note that bridges attached to encoders are auto-detached during encoder
177 * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
178 * *not* be balanced with a drm_bridge_detach() in driver code.
181 * Zero on success, error code on failure
183 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
184 struct drm_bridge *previous)
186 struct drm_bridge_state *state;
189 if (!encoder || !bridge)
192 if (previous && (!previous->dev || previous->encoder != encoder))
198 bridge->dev = encoder->dev;
199 bridge->encoder = encoder;
202 list_add(&bridge->chain_node, &previous->chain_node);
204 list_add(&bridge->chain_node, &encoder->bridge_chain);
206 if (bridge->funcs->attach) {
207 ret = bridge->funcs->attach(bridge);
209 goto err_reset_bridge;
212 if (bridge->funcs->atomic_reset)
213 state = bridge->funcs->atomic_reset(bridge);
215 state = drm_atomic_default_bridge_reset(bridge);
218 ret = PTR_ERR(state);
219 goto err_detach_bridge;
222 drm_atomic_private_obj_init(bridge->dev, &bridge->base,
224 &drm_bridge_priv_state_funcs);
229 if (bridge->funcs->detach)
230 bridge->funcs->detach(bridge);
234 bridge->encoder = NULL;
235 list_del(&bridge->chain_node);
238 EXPORT_SYMBOL(drm_bridge_attach);
240 void drm_bridge_detach(struct drm_bridge *bridge)
242 if (WARN_ON(!bridge))
245 if (WARN_ON(!bridge->dev))
248 drm_atomic_private_obj_fini(&bridge->base);
250 if (bridge->funcs->detach)
251 bridge->funcs->detach(bridge);
253 list_del(&bridge->chain_node);
258 * DOC: bridge callbacks
260 * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM
261 * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c
262 * These helpers call a specific &drm_bridge_funcs op for all the bridges
263 * during encoder configuration.
265 * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
269 * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
271 * @bridge: bridge control structure
272 * @mode: desired mode to be set for the bridge
273 * @adjusted_mode: updated mode that works for this bridge
275 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
276 * encoder chain, starting from the first bridge to the last.
278 * Note: the bridge passed should be the one closest to the encoder
281 * true on success, false on failure
283 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
284 const struct drm_display_mode *mode,
285 struct drm_display_mode *adjusted_mode)
287 struct drm_encoder *encoder;
292 encoder = bridge->encoder;
293 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
294 if (!bridge->funcs->mode_fixup)
297 if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
303 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
306 * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
308 * @bridge: bridge control structure
309 * @mode: desired mode to be validated
311 * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
312 * chain, starting from the first bridge to the last. If at least one bridge
313 * does not accept the mode the function returns the error code.
315 * Note: the bridge passed should be the one closest to the encoder.
318 * MODE_OK on success, drm_mode_status Enum error code on failure
321 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
322 const struct drm_display_mode *mode)
324 struct drm_encoder *encoder;
329 encoder = bridge->encoder;
330 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
331 enum drm_mode_status ret;
333 if (!bridge->funcs->mode_valid)
336 ret = bridge->funcs->mode_valid(bridge, mode);
343 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
346 * drm_bridge_chain_disable - disables all bridges in the encoder chain
347 * @bridge: bridge control structure
349 * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
350 * chain, starting from the last bridge to the first. These are called before
351 * calling the encoder's prepare op.
353 * Note: the bridge passed should be the one closest to the encoder
355 void drm_bridge_chain_disable(struct drm_bridge *bridge)
357 struct drm_encoder *encoder;
358 struct drm_bridge *iter;
363 encoder = bridge->encoder;
364 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
365 if (iter->funcs->disable)
366 iter->funcs->disable(iter);
372 EXPORT_SYMBOL(drm_bridge_chain_disable);
375 * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the
377 * @bridge: bridge control structure
379 * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
380 * encoder chain, starting from the first bridge to the last. These are called
381 * after completing the encoder's prepare op.
383 * Note: the bridge passed should be the one closest to the encoder
385 void drm_bridge_chain_post_disable(struct drm_bridge *bridge)
387 struct drm_encoder *encoder;
392 encoder = bridge->encoder;
393 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
394 if (bridge->funcs->post_disable)
395 bridge->funcs->post_disable(bridge);
398 EXPORT_SYMBOL(drm_bridge_chain_post_disable);
401 * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
403 * @bridge: bridge control structure
404 * @mode: desired mode to be set for the encoder chain
405 * @adjusted_mode: updated mode that works for this encoder chain
407 * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
408 * encoder chain, starting from the first bridge to the last.
410 * Note: the bridge passed should be the one closest to the encoder
412 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
413 const struct drm_display_mode *mode,
414 const struct drm_display_mode *adjusted_mode)
416 struct drm_encoder *encoder;
421 encoder = bridge->encoder;
422 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
423 if (bridge->funcs->mode_set)
424 bridge->funcs->mode_set(bridge, mode, adjusted_mode);
427 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
430 * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the
432 * @bridge: bridge control structure
434 * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
435 * chain, starting from the last bridge to the first. These are called
436 * before calling the encoder's commit op.
438 * Note: the bridge passed should be the one closest to the encoder
440 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge)
442 struct drm_encoder *encoder;
443 struct drm_bridge *iter;
448 encoder = bridge->encoder;
449 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
450 if (iter->funcs->pre_enable)
451 iter->funcs->pre_enable(iter);
454 EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
457 * drm_bridge_chain_enable - enables all bridges in the encoder chain
458 * @bridge: bridge control structure
460 * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
461 * chain, starting from the first bridge to the last. These are called
462 * after completing the encoder's commit op.
464 * Note that the bridge passed should be the one closest to the encoder
466 void drm_bridge_chain_enable(struct drm_bridge *bridge)
468 struct drm_encoder *encoder;
473 encoder = bridge->encoder;
474 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
475 if (bridge->funcs->enable)
476 bridge->funcs->enable(bridge);
479 EXPORT_SYMBOL(drm_bridge_chain_enable);
482 * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
483 * @bridge: bridge control structure
484 * @old_state: old atomic state
486 * Calls &drm_bridge_funcs.atomic_disable (falls back on
487 * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
488 * starting from the last bridge to the first. These are called before calling
489 * &drm_encoder_helper_funcs.atomic_disable
491 * Note: the bridge passed should be the one closest to the encoder
493 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
494 struct drm_atomic_state *old_state)
496 struct drm_encoder *encoder;
497 struct drm_bridge *iter;
502 encoder = bridge->encoder;
503 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
504 if (iter->funcs->atomic_disable) {
505 struct drm_bridge_state *old_bridge_state;
508 drm_atomic_get_old_bridge_state(old_state,
510 if (WARN_ON(!old_bridge_state))
513 iter->funcs->atomic_disable(iter, old_bridge_state);
514 } else if (iter->funcs->disable) {
515 iter->funcs->disable(iter);
522 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
525 * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
526 * in the encoder chain
527 * @bridge: bridge control structure
528 * @old_state: old atomic state
530 * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
531 * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
532 * starting from the first bridge to the last. These are called after completing
533 * &drm_encoder_helper_funcs.atomic_disable
535 * Note: the bridge passed should be the one closest to the encoder
537 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
538 struct drm_atomic_state *old_state)
540 struct drm_encoder *encoder;
545 encoder = bridge->encoder;
546 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
547 if (bridge->funcs->atomic_post_disable) {
548 struct drm_bridge_state *old_bridge_state;
551 drm_atomic_get_old_bridge_state(old_state,
553 if (WARN_ON(!old_bridge_state))
556 bridge->funcs->atomic_post_disable(bridge,
558 } else if (bridge->funcs->post_disable) {
559 bridge->funcs->post_disable(bridge);
563 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
566 * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
568 * @bridge: bridge control structure
569 * @old_state: old atomic state
571 * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
572 * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
573 * starting from the last bridge to the first. These are called before calling
574 * &drm_encoder_helper_funcs.atomic_enable
576 * Note: the bridge passed should be the one closest to the encoder
578 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
579 struct drm_atomic_state *old_state)
581 struct drm_encoder *encoder;
582 struct drm_bridge *iter;
587 encoder = bridge->encoder;
588 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
589 if (iter->funcs->atomic_pre_enable) {
590 struct drm_bridge_state *old_bridge_state;
593 drm_atomic_get_old_bridge_state(old_state,
595 if (WARN_ON(!old_bridge_state))
598 iter->funcs->atomic_pre_enable(iter, old_bridge_state);
599 } else if (iter->funcs->pre_enable) {
600 iter->funcs->pre_enable(iter);
607 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
610 * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
611 * @bridge: bridge control structure
612 * @old_state: old atomic state
614 * Calls &drm_bridge_funcs.atomic_enable (falls back on
615 * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
616 * starting from the first bridge to the last. These are called after completing
617 * &drm_encoder_helper_funcs.atomic_enable
619 * Note: the bridge passed should be the one closest to the encoder
621 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
622 struct drm_atomic_state *old_state)
624 struct drm_encoder *encoder;
629 encoder = bridge->encoder;
630 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
631 if (bridge->funcs->atomic_enable) {
632 struct drm_bridge_state *old_bridge_state;
635 drm_atomic_get_old_bridge_state(old_state,
637 if (WARN_ON(!old_bridge_state))
640 bridge->funcs->atomic_enable(bridge, old_bridge_state);
641 } else if (bridge->funcs->enable) {
642 bridge->funcs->enable(bridge);
646 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
648 static int drm_atomic_bridge_check(struct drm_bridge *bridge,
649 struct drm_crtc_state *crtc_state,
650 struct drm_connector_state *conn_state)
652 if (bridge->funcs->atomic_check) {
653 struct drm_bridge_state *bridge_state;
656 bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
658 if (WARN_ON(!bridge_state))
661 ret = bridge->funcs->atomic_check(bridge, bridge_state,
662 crtc_state, conn_state);
665 } else if (bridge->funcs->mode_fixup) {
666 if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode,
667 &crtc_state->adjusted_mode))
675 * drm_atomic_helper_bridge_propagate_bus_fmt() - Propagate output format to
676 * the input end of a bridge
677 * @bridge: bridge control structure
678 * @bridge_state: new bridge state
679 * @crtc_state: new CRTC state
680 * @conn_state: new connector state
681 * @output_fmt: tested output bus format
682 * @num_input_fmts: will contain the size of the returned array
684 * This helper is a pluggable implementation of the
685 * &drm_bridge_funcs.atomic_get_input_bus_fmts operation for bridges that don't
686 * modify the bus configuration between their input and their output. It
687 * returns an array of input formats with a single element set to @output_fmt.
690 * a valid format array of size @num_input_fmts, or NULL if the allocation
694 drm_atomic_helper_bridge_propagate_bus_fmt(struct drm_bridge *bridge,
695 struct drm_bridge_state *bridge_state,
696 struct drm_crtc_state *crtc_state,
697 struct drm_connector_state *conn_state,
699 unsigned int *num_input_fmts)
703 input_fmts = kzalloc(sizeof(*input_fmts), GFP_KERNEL);
710 input_fmts[0] = output_fmt;
713 EXPORT_SYMBOL(drm_atomic_helper_bridge_propagate_bus_fmt);
715 static int select_bus_fmt_recursive(struct drm_bridge *first_bridge,
716 struct drm_bridge *cur_bridge,
717 struct drm_crtc_state *crtc_state,
718 struct drm_connector_state *conn_state,
721 struct drm_bridge_state *cur_state;
722 unsigned int num_in_bus_fmts, i;
723 struct drm_bridge *prev_bridge;
727 prev_bridge = drm_bridge_get_prev_bridge(cur_bridge);
728 cur_state = drm_atomic_get_new_bridge_state(crtc_state->state,
730 if (WARN_ON(!cur_state))
734 * If bus format negotiation is not supported by this bridge, let's
735 * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and
736 * hope that it can handle this situation gracefully (by providing
737 * appropriate default values).
739 if (!cur_bridge->funcs->atomic_get_input_bus_fmts) {
740 if (cur_bridge != first_bridge) {
741 ret = select_bus_fmt_recursive(first_bridge,
742 prev_bridge, crtc_state,
744 MEDIA_BUS_FMT_FIXED);
749 cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
750 cur_state->output_bus_cfg.format = out_bus_fmt;
754 in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge,
760 if (!num_in_bus_fmts)
762 else if (!in_bus_fmts)
765 if (first_bridge == cur_bridge) {
766 cur_state->input_bus_cfg.format = in_bus_fmts[0];
767 cur_state->output_bus_cfg.format = out_bus_fmt;
772 for (i = 0; i < num_in_bus_fmts; i++) {
773 ret = select_bus_fmt_recursive(first_bridge, prev_bridge,
774 crtc_state, conn_state,
776 if (ret != -ENOTSUPP)
781 cur_state->input_bus_cfg.format = in_bus_fmts[i];
782 cur_state->output_bus_cfg.format = out_bus_fmt;
790 * This function is called by &drm_atomic_bridge_chain_check() just before
791 * calling &drm_bridge_funcs.atomic_check() on all elements of the chain.
792 * It performs bus format negotiation between bridge elements. The negotiation
793 * happens in reverse order, starting from the last element in the chain up to
796 * Negotiation starts by retrieving supported output bus formats on the last
797 * bridge element and testing them one by one. The test is recursive, meaning
798 * that for each tested output format, the whole chain will be walked backward,
799 * and each element will have to choose an input bus format that can be
800 * transcoded to the requested output format. When a bridge element does not
801 * support transcoding into a specific output format -ENOTSUPP is returned and
802 * the next bridge element will have to try a different format. If none of the
803 * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail.
805 * This implementation is relying on
806 * &drm_bridge_funcs.atomic_get_output_bus_fmts() and
807 * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported
808 * input/output formats.
810 * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by
811 * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts()
812 * tries a single format: &drm_connector.display_info.bus_formats[0] if
813 * available, MEDIA_BUS_FMT_FIXED otherwise.
815 * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented,
816 * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the
817 * bridge element that lacks this hook and asks the previous element in the
818 * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what
819 * to do in that case (fail if they want to enforce bus format negotiation, or
820 * provide a reasonable default if they need to support pipelines where not
821 * all elements support bus format negotiation).
824 drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge,
825 struct drm_crtc_state *crtc_state,
826 struct drm_connector_state *conn_state)
828 struct drm_connector *conn = conn_state->connector;
829 struct drm_encoder *encoder = bridge->encoder;
830 struct drm_bridge_state *last_bridge_state;
831 unsigned int i, num_out_bus_fmts;
832 struct drm_bridge *last_bridge;
836 last_bridge = list_last_entry(&encoder->bridge_chain,
837 struct drm_bridge, chain_node);
838 last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
840 if (WARN_ON(!last_bridge_state))
843 if (last_bridge->funcs->atomic_get_output_bus_fmts) {
844 const struct drm_bridge_funcs *funcs = last_bridge->funcs;
846 out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge,
851 if (!num_out_bus_fmts)
853 else if (!out_bus_fmts)
856 num_out_bus_fmts = 1;
857 out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL);
861 if (conn->display_info.num_bus_formats &&
862 conn->display_info.bus_formats)
863 out_bus_fmts[0] = conn->display_info.bus_formats[0];
865 out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
868 for (i = 0; i < num_out_bus_fmts; i++) {
869 ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state,
870 conn_state, out_bus_fmts[i]);
871 if (ret != -ENOTSUPP)
881 drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge,
882 struct drm_connector *conn,
883 struct drm_atomic_state *state)
885 struct drm_bridge_state *bridge_state, *next_bridge_state;
886 struct drm_bridge *next_bridge;
889 bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
890 next_bridge = drm_bridge_get_next_bridge(bridge);
893 * Let's try to apply the most common case here, that is, propagate
894 * display_info flags for the last bridge, and propagate the input
895 * flags of the next bridge element to the output end of the current
896 * bridge when the bridge is not the last one.
897 * There are exceptions to this rule, like when signal inversion is
898 * happening at the board level, but that's something drivers can deal
899 * with from their &drm_bridge_funcs.atomic_check() implementation by
900 * simply overriding the flags value we've set here.
903 output_flags = conn->display_info.bus_flags;
905 next_bridge_state = drm_atomic_get_new_bridge_state(state,
907 output_flags = next_bridge_state->input_bus_cfg.flags;
910 bridge_state->output_bus_cfg.flags = output_flags;
913 * Propage the output flags to the input end of the bridge. Again, it's
914 * not necessarily what all bridges want, but that's what most of them
915 * do, and by doing that by default we avoid forcing drivers to
916 * duplicate the "dummy propagation" logic.
918 bridge_state->input_bus_cfg.flags = output_flags;
922 * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain
923 * @bridge: bridge control structure
924 * @crtc_state: new CRTC state
925 * @conn_state: new connector state
927 * First trigger a bus format negotiation before calling
928 * &drm_bridge_funcs.atomic_check() (falls back on
929 * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain,
930 * starting from the last bridge to the first. These are called before calling
931 * &drm_encoder_helper_funcs.atomic_check()
934 * 0 on success, a negative error code on failure
936 int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,
937 struct drm_crtc_state *crtc_state,
938 struct drm_connector_state *conn_state)
940 struct drm_connector *conn = conn_state->connector;
941 struct drm_encoder *encoder = bridge->encoder;
942 struct drm_bridge *iter;
945 ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state,
950 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
954 * Bus flags are propagated by default. If a bridge needs to
955 * tweak the input bus flags for any reason, it should happen
956 * in its &drm_bridge_funcs.atomic_check() implementation such
957 * that preceding bridges in the chain can propagate the new
960 drm_atomic_bridge_propagate_bus_flags(iter, conn,
963 ret = drm_atomic_bridge_check(iter, crtc_state, conn_state);
973 EXPORT_SYMBOL(drm_atomic_bridge_chain_check);
976 * __drm_atomic_helper_bridge_reset() - Initialize a bridge state to its
978 * @bridge: the bridge this state is refers to
979 * @state: bridge state to initialize
981 * Initialize the bridge state to default values. This is meant to be* called
982 * by the bridge &drm_plane_funcs.reset hook for bridges that subclass the
985 void __drm_atomic_helper_bridge_reset(struct drm_bridge *bridge,
986 struct drm_bridge_state *state)
988 memset(state, 0, sizeof(*state));
989 state->bridge = bridge;
991 EXPORT_SYMBOL(__drm_atomic_helper_bridge_reset);
994 * __drm_atomic_helper_bridge_duplicate_state() - Copy atomic bridge state
995 * @bridge: bridge object
996 * @state: atomic bridge state
998 * Copies atomic state from a bridge's current state and resets inferred values.
999 * This is useful for drivers that subclass the bridge state.
1001 void __drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge,
1002 struct drm_bridge_state *state)
1004 __drm_atomic_helper_private_obj_duplicate_state(&bridge->base,
1006 state->bridge = bridge;
1008 EXPORT_SYMBOL(__drm_atomic_helper_bridge_duplicate_state);
1012 * of_drm_find_bridge - find the bridge corresponding to the device node in
1013 * the global bridge list
1018 * drm_bridge control struct on success, NULL on failure
1020 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
1022 struct drm_bridge *bridge;
1024 mutex_lock(&bridge_lock);
1026 list_for_each_entry(bridge, &bridge_list, list) {
1027 if (bridge->of_node == np) {
1028 mutex_unlock(&bridge_lock);
1033 mutex_unlock(&bridge_lock);
1036 EXPORT_SYMBOL(of_drm_find_bridge);
1039 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
1040 MODULE_DESCRIPTION("DRM bridge infrastructure");
1041 MODULE_LICENSE("GPL and additional rights");