xref: /linux/drivers/gpu/drm/drm_bridge.c (revision b6f466c509ad2f390b3fc91cd0de4783554f5f98)
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sub license,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the
12  * next paragraph) shall be included in all copies or substantial portions
13  * of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <linux/debugfs.h>
25 #include <linux/err.h>
26 #include <linux/export.h>
27 #include <linux/media-bus-format.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/srcu.h>
31 
32 #include <drm/drm_atomic_state_helper.h>
33 #include <drm/drm_bridge.h>
34 #include <drm/drm_debugfs.h>
35 #include <drm/drm_edid.h>
36 #include <drm/drm_encoder.h>
37 #include <drm/drm_file.h>
38 #include <drm/drm_of.h>
39 #include <drm/drm_print.h>
40 
41 #include "drm_crtc_internal.h"
42 
43 /**
44  * DOC: overview
45  *
46  * &struct drm_bridge represents a device that hangs on to an encoder. These are
47  * handy when a regular &drm_encoder entity isn't enough to represent the entire
48  * encoder chain.
49  *
50  * A bridge is always attached to a single &drm_encoder at a time, but can be
51  * either connected to it directly, or through a chain of bridges::
52  *
53  *     [ CRTC ---> ] Encoder ---> Bridge A ---> Bridge B
54  *
55  * Here, the output of the encoder feeds to bridge A, and that furthers feeds to
56  * bridge B. Bridge chains can be arbitrarily long, and shall be fully linear:
57  * Chaining multiple bridges to the output of a bridge, or the same bridge to
58  * the output of different bridges, is not supported.
59  *
60  * &drm_bridge, like &drm_panel, aren't &drm_mode_object entities like planes,
61  * CRTCs, encoders or connectors and hence are not visible to userspace. They
62  * just provide additional hooks to get the desired output at the end of the
63  * encoder chain.
64  */
65 
66 /**
67  * DOC:	display driver integration
68  *
69  * Display drivers are responsible for linking encoders with the first bridge
70  * in the chains. This is done by acquiring the appropriate bridge with
71  * devm_drm_of_get_bridge(). Once acquired, the bridge shall be attached to the
72  * encoder with a call to drm_bridge_attach().
73  *
74  * Bridges are responsible for linking themselves with the next bridge in the
75  * chain, if any. This is done the same way as for encoders, with the call to
76  * drm_bridge_attach() occurring in the &drm_bridge_funcs.attach operation.
77  *
78  * Once these links are created, the bridges can participate along with encoder
79  * functions to perform mode validation and fixup (through
80  * drm_bridge_chain_mode_valid() and drm_atomic_bridge_chain_check()), mode
81  * setting (through drm_bridge_chain_mode_set()), enable (through
82  * drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_enable())
83  * and disable (through drm_atomic_bridge_chain_disable() and
84  * drm_atomic_bridge_chain_post_disable()). Those functions call the
85  * corresponding operations provided in &drm_bridge_funcs in sequence for all
86  * bridges in the chain.
87  *
88  * For display drivers that use the atomic helpers
89  * drm_atomic_helper_check_modeset(),
90  * drm_atomic_helper_commit_modeset_enables() and
91  * drm_atomic_helper_commit_modeset_disables() (either directly in hand-rolled
92  * commit check and commit tail handlers, or through the higher-level
93  * drm_atomic_helper_check() and drm_atomic_helper_commit_tail() or
94  * drm_atomic_helper_commit_tail_rpm() helpers), this is done transparently and
95  * requires no intervention from the driver. For other drivers, the relevant
96  * DRM bridge chain functions shall be called manually.
97  *
98  * Bridges also participate in implementing the &drm_connector at the end of
99  * the bridge chain. Display drivers may use the drm_bridge_connector_init()
100  * helper to create the &drm_connector, or implement it manually on top of the
101  * connector-related operations exposed by the bridge (see the overview
102  * documentation of bridge operations for more details).
103  */
104 
105 /**
106  * DOC: special care dsi
107  *
108  * The interaction between the bridges and other frameworks involved in
109  * the probing of the upstream driver and the bridge driver can be
110  * challenging. Indeed, there's multiple cases that needs to be
111  * considered:
112  *
113  * - The upstream driver doesn't use the component framework and isn't a
114  *   MIPI-DSI host. In this case, the bridge driver will probe at some
115  *   point and the upstream driver should try to probe again by returning
116  *   EPROBE_DEFER as long as the bridge driver hasn't probed.
117  *
118  * - The upstream driver doesn't use the component framework, but is a
119  *   MIPI-DSI host. The bridge device uses the MIPI-DCS commands to be
120  *   controlled. In this case, the bridge device is a child of the
121  *   display device and when it will probe it's assured that the display
122  *   device (and MIPI-DSI host) is present. The upstream driver will be
123  *   assured that the bridge driver is connected between the
124  *   &mipi_dsi_host_ops.attach and &mipi_dsi_host_ops.detach operations.
125  *   Therefore, it must run mipi_dsi_host_register() in its probe
126  *   function, and then run drm_bridge_attach() in its
127  *   &mipi_dsi_host_ops.attach hook.
128  *
129  * - The upstream driver uses the component framework and is a MIPI-DSI
130  *   host. The bridge device uses the MIPI-DCS commands to be
131  *   controlled. This is the same situation than above, and can run
132  *   mipi_dsi_host_register() in either its probe or bind hooks.
133  *
134  * - The upstream driver uses the component framework and is a MIPI-DSI
135  *   host. The bridge device uses a separate bus (such as I2C) to be
136  *   controlled. In this case, there's no correlation between the probe
137  *   of the bridge and upstream drivers, so care must be taken to avoid
138  *   an endless EPROBE_DEFER loop, with each driver waiting for the
139  *   other to probe.
140  *
141  * The ideal pattern to cover the last item (and all the others in the
142  * MIPI-DSI host driver case) is to split the operations like this:
143  *
144  * - The MIPI-DSI host driver must run mipi_dsi_host_register() in its
145  *   probe hook. It will make sure that the MIPI-DSI host sticks around,
146  *   and that the driver's bind can be called.
147  *
148  * - In its probe hook, the bridge driver must try to find its MIPI-DSI
149  *   host, register as a MIPI-DSI device and attach the MIPI-DSI device
150  *   to its host. The bridge driver is now functional.
151  *
152  * - In its &struct mipi_dsi_host_ops.attach hook, the MIPI-DSI host can
153  *   now add its component. Its bind hook will now be called and since
154  *   the bridge driver is attached and registered, we can now look for
155  *   and attach it.
156  *
157  * At this point, we're now certain that both the upstream driver and
158  * the bridge driver are functional and we can't have a deadlock-like
159  * situation when probing.
160  */
161 
162 /**
163  * DOC: dsi bridge operations
164  *
165  * DSI host interfaces are expected to be implemented as bridges rather than
166  * encoders, however there are a few aspects of their operation that need to
167  * be defined in order to provide a consistent interface.
168  *
169  * A DSI host should keep the PHY powered down until the pre_enable operation is
170  * called. All lanes are in an undefined idle state up to this point, and it
171  * must not be assumed that it is LP-11.
172  * pre_enable should initialise the PHY, set the data lanes to LP-11, and the
173  * clock lane to either LP-11 or HS depending on the mode_flag
174  * %MIPI_DSI_CLOCK_NON_CONTINUOUS.
175  *
176  * Ordinarily the downstream bridge DSI peripheral pre_enable will have been
177  * called before the DSI host. If the DSI peripheral requires LP-11 and/or
178  * the clock lane to be in HS mode prior to pre_enable, then it can set the
179  * &pre_enable_prev_first flag to request the pre_enable (and
180  * post_disable) order to be altered to enable the DSI host first.
181  *
182  * Either the CRTC being enabled, or the DSI host enable operation should switch
183  * the host to actively transmitting video on the data lanes.
184  *
185  * The reverse also applies. The DSI host disable operation or stopping the CRTC
186  * should stop transmitting video, and the data lanes should return to the LP-11
187  * state. The DSI host &post_disable operation should disable the PHY.
188  * If the &pre_enable_prev_first flag is set, then the DSI peripheral's
189  * bridge &post_disable will be called before the DSI host's post_disable.
190  *
191  * Whilst it is valid to call &host_transfer prior to pre_enable or after
192  * post_disable, the exact state of the lanes is undefined at this point. The
193  * DSI host should initialise the interface, transmit the data, and then disable
194  * the interface again.
195  *
196  * Ultra Low Power State (ULPS) is not explicitly supported by DRM. If
197  * implemented, it therefore needs to be handled entirely within the DSI Host
198  * driver.
199  */
200 
201 /**
202  * DOC: bridge chain format selection
203  *
204  * A bridge chain, from display output processor to connector, may contain
205  * bridges capable of converting between bus formats on their inputs, and
206  * output formats on their outputs. For example, a bridge may be able to convert
207  * from RGB to YCbCr 4:4:4, and pass through YCbCr 4:2:0 as-is, but not convert
208  * from RGB to YCbCr 4:2:0. This means not all input formats map to all output
209  * formats.
210  *
211  * Further adding to this, a desired output color format, as specified with the
212  * "color format" DRM property, might not correspond 1:1 to what the display
213  * driver should set at its output. The bridge chain it feeds into may only be
214  * able to reach the desired output format, if a conversion from a different
215  * starting format is performed.
216  *
217  * To deal with this complexity, the recursive bridge chain bus format selection
218  * logic starts with the last bridge in the chain, usually the connector, and
219  * then recursively walks the chain of bridges backwards to the first bridge,
220  * trying to find a path.
221  *
222  * For a display driver to work in such a scenario, it should read the first
223  * bridge's bridge state to figure out which bus format the chain resolved to.
224  * If the first bridge's input format resolved to %MEDIA_BUS_FMT_FIXED, then its
225  * output format should be used.
226  *
227  * Special handling is done for HDMI as it relates to format selection. Instead
228  * of directly using the "color format" DRM property for bridge chains that end
229  * in HDMI bridges, the bridge chain format selection logic will trust the logic
230  * that set the HDMI output format. For the common HDMI state helper
231  * functionality, this means that %DRM_CONNECTOR_COLOR_FORMAT_AUTO will allow
232  * fallbacks to YCBCr 4:2:0 if the bandwidth requirements would otherwise be too
233  * high but the mode and connector allow it.
234  *
235  * For bridge chains that do not end in an HDMI bridge,
236  * %DRM_CONNECTOR_COLOR_FORMAT_AUTO will be satisfied with the first output
237  * format on the last bridge for which it can find a path back to the first
238  * bridge.
239  */
240 
241 /* Protect bridge_list and bridge_lingering_list */
242 static DEFINE_MUTEX(bridge_lock);
243 static LIST_HEAD(bridge_list);
244 static LIST_HEAD(bridge_lingering_list);
245 
246 DEFINE_STATIC_SRCU(drm_bridge_unplug_srcu);
247 
248 /**
249  * drm_bridge_enter - Enter DRM bridge critical section
250  * @bridge: DRM bridge
251  * @idx: Pointer to index that will be passed to the matching drm_bridge_exit()
252  *
253  * This function marks and protects the beginning of a section that should not
254  * be entered after the bridge has been unplugged. The section end is marked
255  * with drm_bridge_exit(). Calls to this function can be nested.
256  *
257  * Returns:
258  * True if it is OK to enter the section, false otherwise.
259  */
260 bool drm_bridge_enter(struct drm_bridge *bridge, int *idx)
261 {
262 	*idx = srcu_read_lock(&drm_bridge_unplug_srcu);
263 
264 	if (bridge->unplugged) {
265 		srcu_read_unlock(&drm_bridge_unplug_srcu, *idx);
266 		return false;
267 	}
268 
269 	return true;
270 }
271 EXPORT_SYMBOL(drm_bridge_enter);
272 
273 /**
274  * drm_bridge_exit - Exit DRM bridge critical section
275  * @idx: index returned by drm_bridge_enter()
276  *
277  * This function marks the end of a section that should not be entered after
278  * the bridge has been unplugged.
279  */
280 void drm_bridge_exit(int idx)
281 {
282 	srcu_read_unlock(&drm_bridge_unplug_srcu, idx);
283 }
284 EXPORT_SYMBOL(drm_bridge_exit);
285 
286 /**
287  * drm_bridge_unplug - declare a DRM bridge was unplugged and remove it
288  * @bridge: DRM bridge
289  *
290  * This tells the bridge has been physically unplugged and no operations on
291  * device resources must be done anymore. Entry-points can use
292  * drm_bridge_enter() and drm_bridge_exit() to protect device resources in
293  * a race free manner.
294  *
295  * Also unregisters the bridge.
296  */
297 void drm_bridge_unplug(struct drm_bridge *bridge)
298 {
299 	bridge->unplugged = true;
300 
301 	synchronize_srcu(&drm_bridge_unplug_srcu);
302 
303 	drm_bridge_remove(bridge);
304 }
305 EXPORT_SYMBOL(drm_bridge_unplug);
306 
307 static void __drm_bridge_free(struct kref *kref)
308 {
309 	struct drm_bridge *bridge = container_of(kref, struct drm_bridge, refcount);
310 
311 	mutex_lock(&bridge_lock);
312 	list_del(&bridge->list);
313 	mutex_unlock(&bridge_lock);
314 
315 	if (bridge->funcs->destroy)
316 		bridge->funcs->destroy(bridge);
317 
318 	drm_bridge_put(bridge->next_bridge);
319 
320 	kfree(bridge->container);
321 }
322 
323 /**
324  * drm_bridge_get - Acquire a bridge reference
325  * @bridge: DRM bridge; if NULL this function does nothing
326  *
327  * This function increments the bridge's refcount.
328  *
329  * Returns:
330  * Pointer to @bridge.
331  */
332 struct drm_bridge *drm_bridge_get(struct drm_bridge *bridge)
333 {
334 	if (bridge)
335 		kref_get(&bridge->refcount);
336 
337 	return bridge;
338 }
339 EXPORT_SYMBOL(drm_bridge_get);
340 
341 /**
342  * drm_bridge_put - Release a bridge reference
343  * @bridge: DRM bridge; if NULL or an ERR_PTR this function does nothing
344  *
345  * This function decrements the bridge's reference count and frees the
346  * object if the reference count drops to zero.
347  *
348  * See also drm_bridge_clear_and_put() if you also need to set the pointer
349  * to NULL
350  */
351 void drm_bridge_put(struct drm_bridge *bridge)
352 {
353 	if (!IS_ERR_OR_NULL(bridge))
354 		kref_put(&bridge->refcount, __drm_bridge_free);
355 }
356 EXPORT_SYMBOL(drm_bridge_put);
357 
358 /**
359  * drm_bridge_clear_and_put - Given a bridge pointer, clear the pointer
360  *                            then put the bridge
361  * @bridge_pp: pointer to pointer to a struct drm_bridge; ``bridge_pp``
362  *             must be non-NULL; if ``*bridge_pp`` is NULL this function
363  *             does nothing
364  *
365  * Helper to put a DRM bridge, but only after setting its pointer to
366  * NULL. Useful when a struct drm_bridge reference must be dropped without
367  * leaving a use-after-free window where the pointed bridge might have been
368  * freed while still holding a pointer to it.
369  *
370  * For struct ``drm_bridge *some_bridge``, this code::
371  *
372  *     drm_bridge_clear_and_put(&some_bridge);
373  *
374  * is equivalent to the more complex::
375  *
376  *     struct drm_bridge *temp = some_bridge;
377  *     some_bridge = NULL;
378  *     drm_bridge_put(temp);
379  */
380 void drm_bridge_clear_and_put(struct drm_bridge **bridge_pp)
381 {
382 	struct drm_bridge *bridge = *bridge_pp;
383 
384 	*bridge_pp = NULL;
385 	drm_bridge_put(bridge);
386 }
387 EXPORT_SYMBOL(drm_bridge_clear_and_put);
388 
389 /**
390  * drm_bridge_put_void - wrapper to drm_bridge_put() taking a void pointer
391  *
392  * @data: pointer to @struct drm_bridge, cast to a void pointer
393  *
394  * Wrapper of drm_bridge_put() to be used when a function taking a void
395  * pointer is needed, for example as a devm action.
396  */
397 static void drm_bridge_put_void(void *data)
398 {
399 	struct drm_bridge *bridge = (struct drm_bridge *)data;
400 
401 	drm_bridge_put(bridge);
402 }
403 
404 void *__devm_drm_bridge_alloc(struct device *dev, size_t size, size_t offset,
405 			      const struct drm_bridge_funcs *funcs)
406 {
407 	void *container;
408 	struct drm_bridge *bridge;
409 	int err;
410 
411 	if (!funcs) {
412 		dev_warn(dev, "Missing funcs pointer\n");
413 		return ERR_PTR(-EINVAL);
414 	}
415 
416 	container = kzalloc(size, GFP_KERNEL);
417 	if (!container)
418 		return ERR_PTR(-ENOMEM);
419 
420 	bridge = container + offset;
421 	INIT_LIST_HEAD(&bridge->list);
422 	bridge->container = container;
423 	bridge->funcs = funcs;
424 	kref_init(&bridge->refcount);
425 
426 	err = devm_add_action_or_reset(dev, drm_bridge_put_void, bridge);
427 	if (err)
428 		return ERR_PTR(err);
429 
430 	return container;
431 }
432 EXPORT_SYMBOL(__devm_drm_bridge_alloc);
433 
434 /**
435  * drm_bridge_add - register a bridge
436  *
437  * @bridge: bridge control structure
438  *
439  * Add the given bridge to the global list of bridges, where they can be
440  * found by users via of_drm_find_and_get_bridge().
441  *
442  * The bridge to be added must have been allocated by
443  * devm_drm_bridge_alloc().
444  */
445 void drm_bridge_add(struct drm_bridge *bridge)
446 {
447 	if (!bridge->container)
448 		DRM_WARN("DRM bridge corrupted or not allocated by devm_drm_bridge_alloc()\n");
449 
450 	drm_bridge_get(bridge);
451 
452 	/*
453 	 * If the bridge was previously added and then removed, it is now
454 	 * in bridge_lingering_list. Remove it or bridge_lingering_list will be
455 	 * corrupted when adding this bridge to bridge_list below.
456 	 */
457 	if (!list_empty(&bridge->list))
458 		list_del_init(&bridge->list);
459 
460 	mutex_init(&bridge->hpd_state_mutex);
461 	mutex_init(&bridge->hpd_mutex);
462 
463 	if (bridge->ops & DRM_BRIDGE_OP_HDMI)
464 		bridge->ycbcr_420_allowed = !!(bridge->supported_formats &
465 					       BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR420));
466 
467 	mutex_lock(&bridge_lock);
468 	list_add_tail(&bridge->list, &bridge_list);
469 	mutex_unlock(&bridge_lock);
470 }
471 EXPORT_SYMBOL(drm_bridge_add);
472 
473 static void drm_bridge_remove_void(void *bridge)
474 {
475 	drm_bridge_remove(bridge);
476 }
477 
478 /**
479  * devm_drm_bridge_add - devm managed version of drm_bridge_add()
480  *
481  * @dev: device to tie the bridge lifetime to
482  * @bridge: bridge control structure
483  *
484  * This is the managed version of drm_bridge_add() which automatically
485  * calls drm_bridge_remove() when @dev is unbound.
486  *
487  * Return: 0 if no error or negative error code.
488  */
489 int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge)
490 {
491 	drm_bridge_add(bridge);
492 	return devm_add_action_or_reset(dev, drm_bridge_remove_void, bridge);
493 }
494 EXPORT_SYMBOL(devm_drm_bridge_add);
495 
496 /**
497  * drm_bridge_remove - unregister a bridge
498  *
499  * @bridge: bridge control structure
500  *
501  * Remove the given bridge from the global list of registered bridges, so
502  * it won't be found by users via of_drm_find_and_get_bridge(), and add it
503  * to the lingering bridge list, to keep track of it until its allocated
504  * memory is eventually freed.
505  */
506 void drm_bridge_remove(struct drm_bridge *bridge)
507 {
508 	mutex_lock(&bridge_lock);
509 	list_move_tail(&bridge->list, &bridge_lingering_list);
510 	mutex_unlock(&bridge_lock);
511 
512 	mutex_destroy(&bridge->hpd_mutex);
513 	mutex_destroy(&bridge->hpd_state_mutex);
514 
515 	drm_bridge_put(bridge);
516 }
517 EXPORT_SYMBOL(drm_bridge_remove);
518 
519 static struct drm_private_state *
520 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)
521 {
522 	struct drm_bridge *bridge = drm_priv_to_bridge(obj);
523 	struct drm_bridge_state *state;
524 
525 	state = bridge->funcs->atomic_duplicate_state(bridge);
526 	return state ? &state->base : NULL;
527 }
528 
529 static void
530 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,
531 				     struct drm_private_state *s)
532 {
533 	struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
534 	struct drm_bridge *bridge = drm_priv_to_bridge(obj);
535 
536 	bridge->funcs->atomic_destroy_state(bridge, state);
537 }
538 
539 static struct drm_private_state *
540 drm_bridge_atomic_create_priv_state(struct drm_private_obj *obj)
541 {
542 	struct drm_bridge *bridge = drm_priv_to_bridge(obj);
543 	struct drm_bridge_state *state;
544 
545 	state = bridge->funcs->atomic_create_state(bridge);
546 	if (IS_ERR(state))
547 		return ERR_CAST(state);
548 
549 	return &state->base;
550 }
551 
552 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
553 	.atomic_create_state = drm_bridge_atomic_create_priv_state,
554 	.atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
555 	.atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
556 };
557 
558 static bool drm_bridge_is_atomic(struct drm_bridge *bridge)
559 {
560 	return bridge->funcs->atomic_create_state != NULL;
561 }
562 
563 /**
564  * drm_bridge_attach - attach the bridge to an encoder's chain
565  *
566  * @encoder: DRM encoder
567  * @bridge: bridge to attach
568  * @previous: previous bridge in the chain (optional)
569  * @flags: DRM_BRIDGE_ATTACH_* flags
570  *
571  * Called by a kms driver to link the bridge to an encoder's chain. The previous
572  * argument specifies the previous bridge in the chain. If NULL, the bridge is
573  * linked directly at the encoder's output. Otherwise it is linked at the
574  * previous bridge's output.
575  *
576  * If non-NULL the previous bridge must be already attached by a call to this
577  * function.
578  *
579  * The bridge to be attached must have been previously added by
580  * drm_bridge_add().
581  *
582  * Note that bridges attached to encoders are auto-detached during encoder
583  * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
584  * *not* be balanced with a drm_bridge_detach() in driver code.
585  *
586  * RETURNS:
587  * Zero on success, error code on failure
588  */
589 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
590 		      struct drm_bridge *previous,
591 		      enum drm_bridge_attach_flags flags)
592 {
593 	int ret;
594 
595 	if (!encoder || !bridge)
596 		return -EINVAL;
597 
598 	if (!bridge->container)
599 		DRM_WARN("DRM bridge corrupted or not allocated by devm_drm_bridge_alloc()\n");
600 
601 	if (list_empty(&bridge->list))
602 		DRM_WARN("Missing drm_bridge_add() before attach\n");
603 
604 	drm_bridge_get(bridge);
605 
606 	if (previous && (!previous->dev || previous->encoder != encoder)) {
607 		ret = -EINVAL;
608 		goto err_put_bridge;
609 	}
610 
611 	if (bridge->dev) {
612 		ret = -EBUSY;
613 		goto err_put_bridge;
614 	}
615 
616 	bridge->dev = encoder->dev;
617 	bridge->encoder = encoder;
618 
619 	mutex_lock(&encoder->bridge_chain_mutex);
620 	if (previous)
621 		list_add(&bridge->chain_node, &previous->chain_node);
622 	else
623 		list_add(&bridge->chain_node, &encoder->bridge_chain);
624 	mutex_unlock(&encoder->bridge_chain_mutex);
625 
626 	if (bridge->funcs->attach) {
627 		ret = bridge->funcs->attach(bridge, encoder, flags);
628 		if (ret < 0)
629 			goto err_reset_bridge;
630 	}
631 
632 	if (drm_bridge_is_atomic(bridge))
633 		drm_atomic_private_obj_init(bridge->dev, &bridge->base,
634 					    &drm_bridge_priv_state_funcs);
635 
636 	return 0;
637 
638 err_reset_bridge:
639 	bridge->dev = NULL;
640 	bridge->encoder = NULL;
641 	mutex_lock(&encoder->bridge_chain_mutex);
642 	list_del(&bridge->chain_node);
643 	mutex_unlock(&encoder->bridge_chain_mutex);
644 
645 	if (ret != -EPROBE_DEFER)
646 		DRM_ERROR("failed to attach bridge %pOF to encoder %s: %d\n",
647 			  bridge->of_node, encoder->name, ret);
648 	else
649 		dev_err_probe(encoder->dev->dev, -EPROBE_DEFER,
650 			      "failed to attach bridge %pOF to encoder %s\n",
651 			      bridge->of_node, encoder->name);
652 
653 err_put_bridge:
654 	drm_bridge_put(bridge);
655 	return ret;
656 }
657 EXPORT_SYMBOL(drm_bridge_attach);
658 
659 void drm_bridge_detach(struct drm_bridge *bridge)
660 {
661 	if (WARN_ON(!bridge))
662 		return;
663 
664 	if (WARN_ON(!bridge->dev))
665 		return;
666 
667 	if (drm_bridge_is_atomic(bridge))
668 		drm_atomic_private_obj_fini(&bridge->base);
669 
670 	if (bridge->funcs->detach)
671 		bridge->funcs->detach(bridge);
672 
673 	list_del(&bridge->chain_node);
674 	bridge->dev = NULL;
675 	drm_bridge_put(bridge);
676 }
677 
678 /**
679  * DOC: bridge operations
680  *
681  * Bridge drivers expose operations through the &drm_bridge_funcs structure.
682  * The DRM internals (atomic and CRTC helpers) use the helpers defined in
683  * drm_bridge.c to call bridge operations. Those operations are divided in
684  * three big categories to support different parts of the bridge usage.
685  *
686  * - The encoder-related operations support control of the bridges in the
687  *   chain, and are roughly counterparts to the &drm_encoder_helper_funcs
688  *   operations. They are used by the legacy CRTC and the atomic modeset
689  *   helpers to perform mode validation, fixup and setting, and enable and
690  *   disable the bridge automatically.
691  *
692  *   The enable and disable operations are split in
693  *   &drm_bridge_funcs.pre_enable, &drm_bridge_funcs.enable,
694  *   &drm_bridge_funcs.disable and &drm_bridge_funcs.post_disable to provide
695  *   finer-grained control.
696  *
697  *   Bridge drivers may implement the legacy version of those operations, or
698  *   the atomic version (prefixed with atomic\_), in which case they shall also
699  *   implement the atomic state bookkeeping operations
700  *   (&drm_bridge_funcs.atomic_duplicate_state,
701  *   &drm_bridge_funcs.atomic_destroy_state and &drm_bridge_funcs.reset).
702  *   Mixing atomic and non-atomic versions of the operations is not supported.
703  *
704  * - The bus format negotiation operations
705  *   &drm_bridge_funcs.atomic_get_output_bus_fmts and
706  *   &drm_bridge_funcs.atomic_get_input_bus_fmts allow bridge drivers to
707  *   negotiate the formats transmitted between bridges in the chain when
708  *   multiple formats are supported. Negotiation for formats is performed
709  *   transparently for display drivers by the atomic modeset helpers. Only
710  *   atomic versions of those operations exist, bridge drivers that need to
711  *   implement them shall thus also implement the atomic version of the
712  *   encoder-related operations. This feature is not supported by the legacy
713  *   CRTC helpers.
714  *
715  * - The connector-related operations support implementing a &drm_connector
716  *   based on a chain of bridges. DRM bridges traditionally create a
717  *   &drm_connector for bridges meant to be used at the end of the chain. This
718  *   puts additional burden on bridge drivers, especially for bridges that may
719  *   be used in the middle of a chain or at the end of it. Furthermore, it
720  *   requires all operations of the &drm_connector to be handled by a single
721  *   bridge, which doesn't always match the hardware architecture.
722  *
723  *   To simplify bridge drivers and make the connector implementation more
724  *   flexible, a new model allows bridges to unconditionally skip creation of
725  *   &drm_connector and instead expose &drm_bridge_funcs operations to support
726  *   an externally-implemented &drm_connector. Those operations are
727  *   &drm_bridge_funcs.detect, &drm_bridge_funcs.get_modes,
728  *   &drm_bridge_funcs.get_edid, &drm_bridge_funcs.hpd_notify,
729  *   &drm_bridge_funcs.hpd_enable and &drm_bridge_funcs.hpd_disable. When
730  *   implemented, display drivers shall create a &drm_connector instance for
731  *   each chain of bridges, and implement those connector instances based on
732  *   the bridge connector operations.
733  *
734  *   Bridge drivers shall implement the connector-related operations for all
735  *   the features that the bridge hardware support. For instance, if a bridge
736  *   supports reading EDID, the &drm_bridge_funcs.get_edid shall be
737  *   implemented. This however doesn't mean that the DDC lines are wired to the
738  *   bridge on a particular platform, as they could also be connected to an I2C
739  *   controller of the SoC. Support for the connector-related operations on the
740  *   running platform is reported through the &drm_bridge.ops flags. Bridge
741  *   drivers shall detect which operations they can support on the platform
742  *   (usually this information is provided by ACPI or DT), and set the
743  *   &drm_bridge.ops flags for all supported operations. A flag shall only be
744  *   set if the corresponding &drm_bridge_funcs operation is implemented, but
745  *   an implemented operation doesn't necessarily imply that the corresponding
746  *   flag will be set. Display drivers shall use the &drm_bridge.ops flags to
747  *   decide which bridge to delegate a connector operation to. This mechanism
748  *   allows providing a single static const &drm_bridge_funcs instance in
749  *   bridge drivers, improving security by storing function pointers in
750  *   read-only memory.
751  *
752  *   In order to ease transition, bridge drivers may support both the old and
753  *   new models by making connector creation optional and implementing the
754  *   connected-related bridge operations. Connector creation is then controlled
755  *   by the flags argument to the drm_bridge_attach() function. Display drivers
756  *   that support the new model and create connectors themselves shall set the
757  *   %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag, and bridge drivers shall then skip
758  *   connector creation. For intermediate bridges in the chain, the flag shall
759  *   be passed to the drm_bridge_attach() call for the downstream bridge.
760  *   Bridge drivers that implement the new model only shall return an error
761  *   from their &drm_bridge_funcs.attach handler when the
762  *   %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag is not set. New display drivers
763  *   should use the new model, and convert the bridge drivers they use if
764  *   needed, in order to gradually transition to the new model.
765  */
766 
767 /**
768  * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
769  *				 encoder chain.
770  * @first_bridge: bridge control structure
771  * @info: display info against which the mode shall be validated
772  * @mode: desired mode to be validated
773  *
774  * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
775  * chain, starting from the first bridge to the last. If at least one bridge
776  * does not accept the mode the function returns the error code.
777  *
778  * Note: the bridge passed should be the one closest to the encoder.
779  *
780  * RETURNS:
781  * MODE_OK on success, drm_mode_status Enum error code on failure
782  */
783 enum drm_mode_status
784 drm_bridge_chain_mode_valid(struct drm_bridge *first_bridge,
785 			    const struct drm_display_info *info,
786 			    const struct drm_display_mode *mode)
787 {
788 	if (!first_bridge)
789 		return MODE_OK;
790 
791 	drm_for_each_bridge_in_chain_from(first_bridge, bridge) {
792 		enum drm_mode_status ret;
793 
794 		if (!bridge->funcs->mode_valid)
795 			continue;
796 
797 		ret = bridge->funcs->mode_valid(bridge, info, mode);
798 		if (ret != MODE_OK)
799 			return ret;
800 	}
801 
802 	return MODE_OK;
803 }
804 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
805 
806 /**
807  * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
808  *			       encoder chain
809  * @first_bridge: bridge control structure
810  * @mode: desired mode to be set for the encoder chain
811  * @adjusted_mode: updated mode that works for this encoder chain
812  *
813  * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
814  * encoder chain, starting from the first bridge to the last.
815  *
816  * Note: the bridge passed should be the one closest to the encoder
817  */
818 void drm_bridge_chain_mode_set(struct drm_bridge *first_bridge,
819 			       const struct drm_display_mode *mode,
820 			       const struct drm_display_mode *adjusted_mode)
821 {
822 	if (!first_bridge)
823 		return;
824 
825 	drm_for_each_bridge_in_chain_from(first_bridge, bridge)
826 		if (bridge->funcs->mode_set)
827 			bridge->funcs->mode_set(bridge, mode, adjusted_mode);
828 }
829 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
830 
831 /**
832  * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
833  * @bridge: bridge control structure
834  * @state: atomic state being committed
835  *
836  * Calls &drm_bridge_funcs.atomic_disable (falls back on
837  * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
838  * starting from the last bridge to the first. These are called before calling
839  * &drm_encoder_helper_funcs.atomic_disable
840  *
841  * Note: the bridge passed should be the one closest to the encoder
842  */
843 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
844 				     struct drm_atomic_commit *state)
845 {
846 	struct drm_encoder *encoder;
847 	struct drm_bridge *iter;
848 
849 	if (!bridge)
850 		return;
851 
852 	encoder = bridge->encoder;
853 	mutex_lock(&encoder->bridge_chain_mutex);
854 	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
855 		if (iter->funcs->atomic_disable) {
856 			iter->funcs->atomic_disable(iter, state);
857 		} else if (iter->funcs->disable) {
858 			iter->funcs->disable(iter);
859 		}
860 
861 		if (iter == bridge)
862 			break;
863 	}
864 	mutex_unlock(&encoder->bridge_chain_mutex);
865 }
866 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
867 
868 static void drm_atomic_bridge_call_post_disable(struct drm_bridge *bridge,
869 						struct drm_atomic_commit *state)
870 {
871 	if (state && bridge->funcs->atomic_post_disable)
872 		bridge->funcs->atomic_post_disable(bridge, state);
873 	else if (bridge->funcs->post_disable)
874 		bridge->funcs->post_disable(bridge);
875 }
876 
877 /**
878  * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
879  *					  in the encoder chain
880  * @bridge: bridge control structure
881  * @state: atomic state being committed
882  *
883  * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
884  * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
885  * starting from the first bridge to the last. These are called after completing
886  * &drm_encoder_helper_funcs.atomic_disable
887  *
888  * If a bridge sets @pre_enable_prev_first, then the @post_disable for that
889  * bridge will be called before the previous one to reverse the @pre_enable
890  * calling direction.
891  *
892  * Example:
893  * Bridge A ---> Bridge B ---> Bridge C ---> Bridge D ---> Bridge E
894  *
895  * With pre_enable_prev_first flag enable in Bridge B, D, E then the resulting
896  * @post_disable order would be,
897  * Bridge B, Bridge A, Bridge E, Bridge D, Bridge C.
898  *
899  * Note: the bridge passed should be the one closest to the encoder
900  */
901 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
902 					  struct drm_atomic_commit *state)
903 {
904 	struct drm_encoder *encoder;
905 	struct drm_bridge *next, *limit;
906 
907 	if (!bridge)
908 		return;
909 
910 	encoder = bridge->encoder;
911 
912 	mutex_lock(&encoder->bridge_chain_mutex);
913 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
914 		limit = NULL;
915 
916 		if (!list_is_last(&bridge->chain_node, &encoder->bridge_chain)) {
917 			next = list_next_entry(bridge, chain_node);
918 
919 			if (next->pre_enable_prev_first) {
920 				/* next bridge had requested that prev
921 				 * was enabled first, so disabled last
922 				 */
923 				limit = next;
924 
925 				/* Find the next bridge that has NOT requested
926 				 * prev to be enabled first / disabled last
927 				 */
928 				list_for_each_entry_from(next, &encoder->bridge_chain,
929 							 chain_node) {
930 					if (!next->pre_enable_prev_first) {
931 						next = list_prev_entry(next, chain_node);
932 						limit = next;
933 						break;
934 					}
935 
936 					if (list_is_last(&next->chain_node,
937 							 &encoder->bridge_chain)) {
938 						limit = next;
939 						break;
940 					}
941 				}
942 
943 				/* Call these bridges in reverse order */
944 				list_for_each_entry_from_reverse(next, &encoder->bridge_chain,
945 								 chain_node) {
946 					if (next == bridge)
947 						break;
948 
949 					drm_atomic_bridge_call_post_disable(next,
950 									    state);
951 				}
952 			}
953 		}
954 
955 		drm_atomic_bridge_call_post_disable(bridge, state);
956 
957 		if (limit)
958 			/* Jump all bridges that we have already post_disabled */
959 			bridge = limit;
960 	}
961 	mutex_unlock(&encoder->bridge_chain_mutex);
962 }
963 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
964 
965 static void drm_atomic_bridge_call_pre_enable(struct drm_bridge *bridge,
966 					      struct drm_atomic_commit *state)
967 {
968 	if (state && bridge->funcs->atomic_pre_enable)
969 		bridge->funcs->atomic_pre_enable(bridge, state);
970 	else if (bridge->funcs->pre_enable)
971 		bridge->funcs->pre_enable(bridge);
972 }
973 
974 /**
975  * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
976  *					the encoder chain
977  * @bridge: bridge control structure
978  * @state: atomic state being committed
979  *
980  * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
981  * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
982  * starting from the last bridge to the first. These are called before calling
983  * &drm_encoder_helper_funcs.atomic_enable
984  *
985  * If a bridge sets @pre_enable_prev_first, then the pre_enable for the
986  * prev bridge will be called before pre_enable of this bridge.
987  *
988  * Example:
989  * Bridge A ---> Bridge B ---> Bridge C ---> Bridge D ---> Bridge E
990  *
991  * With pre_enable_prev_first flag enable in Bridge B, D, E then the resulting
992  * @pre_enable order would be,
993  * Bridge C, Bridge D, Bridge E, Bridge A, Bridge B.
994  *
995  * Note: the bridge passed should be the one closest to the encoder
996  */
997 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
998 					struct drm_atomic_commit *state)
999 {
1000 	struct drm_encoder *encoder;
1001 	struct drm_bridge *iter, *next, *limit;
1002 
1003 	if (!bridge)
1004 		return;
1005 
1006 	encoder = bridge->encoder;
1007 
1008 	mutex_lock(&encoder->bridge_chain_mutex);
1009 	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
1010 		if (iter->pre_enable_prev_first) {
1011 			next = iter;
1012 			limit = bridge;
1013 			list_for_each_entry_from_reverse(next,
1014 							 &encoder->bridge_chain,
1015 							 chain_node) {
1016 				if (next == bridge)
1017 					break;
1018 
1019 				if (!next->pre_enable_prev_first) {
1020 					/* Found first bridge that does NOT
1021 					 * request prev to be enabled first
1022 					 */
1023 					limit = next;
1024 					break;
1025 				}
1026 			}
1027 
1028 			list_for_each_entry_from(next, &encoder->bridge_chain, chain_node) {
1029 				/* Call requested prev bridge pre_enable
1030 				 * in order.
1031 				 */
1032 				if (next == iter)
1033 					/* At the first bridge to request prev
1034 					 * bridges called first.
1035 					 */
1036 					break;
1037 
1038 				drm_atomic_bridge_call_pre_enable(next, state);
1039 			}
1040 		}
1041 
1042 		drm_atomic_bridge_call_pre_enable(iter, state);
1043 
1044 		if (iter->pre_enable_prev_first)
1045 			/* Jump all bridges that we have already pre_enabled */
1046 			iter = limit;
1047 
1048 		if (iter == bridge)
1049 			break;
1050 	}
1051 	mutex_unlock(&encoder->bridge_chain_mutex);
1052 }
1053 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
1054 
1055 /**
1056  * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
1057  * @first_bridge: bridge control structure
1058  * @state: atomic state being committed
1059  *
1060  * Calls &drm_bridge_funcs.atomic_enable (falls back on
1061  * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
1062  * starting from the first bridge to the last. These are called after completing
1063  * &drm_encoder_helper_funcs.atomic_enable
1064  *
1065  * Note: the bridge passed should be the one closest to the encoder
1066  */
1067 void drm_atomic_bridge_chain_enable(struct drm_bridge *first_bridge,
1068 				    struct drm_atomic_commit *state)
1069 {
1070 	if (!first_bridge)
1071 		return;
1072 
1073 	drm_for_each_bridge_in_chain_from(first_bridge, bridge)
1074 		if (bridge->funcs->atomic_enable) {
1075 			bridge->funcs->atomic_enable(bridge, state);
1076 		} else if (bridge->funcs->enable) {
1077 			bridge->funcs->enable(bridge);
1078 		}
1079 }
1080 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
1081 
1082 static int drm_atomic_bridge_check(struct drm_bridge *bridge,
1083 				   struct drm_crtc_state *crtc_state,
1084 				   struct drm_connector_state *conn_state)
1085 {
1086 	if (bridge->funcs->atomic_check) {
1087 		struct drm_bridge_state *bridge_state;
1088 		int ret;
1089 
1090 		bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
1091 							       bridge);
1092 		if (WARN_ON(!bridge_state))
1093 			return -EINVAL;
1094 
1095 		ret = bridge->funcs->atomic_check(bridge, bridge_state,
1096 						  crtc_state, conn_state);
1097 		if (ret)
1098 			return ret;
1099 	} else if (bridge->funcs->mode_fixup) {
1100 		if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode,
1101 					       &crtc_state->adjusted_mode))
1102 			return -EINVAL;
1103 	}
1104 
1105 	return 0;
1106 }
1107 
1108 static int select_bus_fmt_recursive(struct drm_bridge *first_bridge,
1109 				    struct drm_bridge *cur_bridge,
1110 				    struct drm_crtc_state *crtc_state,
1111 				    struct drm_connector_state *conn_state,
1112 				    u32 out_bus_fmt)
1113 {
1114 	unsigned int i, num_in_bus_fmts = 0;
1115 	struct drm_bridge_state *cur_state;
1116 	struct drm_bridge *prev_bridge __free(drm_bridge_put) =
1117 		drm_bridge_get_prev_bridge(cur_bridge);
1118 	u32 *in_bus_fmts;
1119 	int ret;
1120 
1121 	cur_state = drm_atomic_get_new_bridge_state(crtc_state->state,
1122 						    cur_bridge);
1123 
1124 	/*
1125 	 * If bus format negotiation is not supported by this bridge, let's
1126 	 * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and
1127 	 * hope that it can handle this situation gracefully (by providing
1128 	 * appropriate default values).
1129 	 */
1130 	if (!cur_bridge->funcs->atomic_get_input_bus_fmts) {
1131 		if (cur_bridge != first_bridge) {
1132 			ret = select_bus_fmt_recursive(first_bridge,
1133 						       prev_bridge, crtc_state,
1134 						       conn_state,
1135 						       MEDIA_BUS_FMT_FIXED);
1136 			if (ret)
1137 				return ret;
1138 		}
1139 
1140 		/*
1141 		 * Driver does not implement the atomic state hooks, but that's
1142 		 * fine, as long as it does not access the bridge state.
1143 		 */
1144 		if (cur_state) {
1145 			cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
1146 			cur_state->output_bus_cfg.format = out_bus_fmt;
1147 		}
1148 
1149 		return 0;
1150 	}
1151 
1152 	/*
1153 	 * If the driver implements ->atomic_get_input_bus_fmts() it
1154 	 * should also implement the atomic state hooks.
1155 	 */
1156 	if (WARN_ON(!cur_state))
1157 		return -EINVAL;
1158 
1159 	in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge,
1160 							cur_state,
1161 							crtc_state,
1162 							conn_state,
1163 							out_bus_fmt,
1164 							&num_in_bus_fmts);
1165 	if (!num_in_bus_fmts)
1166 		return -ENOTSUPP;
1167 	else if (!in_bus_fmts)
1168 		return -ENOMEM;
1169 
1170 	if (first_bridge == cur_bridge) {
1171 		cur_state->input_bus_cfg.format = in_bus_fmts[0];
1172 		cur_state->output_bus_cfg.format = out_bus_fmt;
1173 		kfree(in_bus_fmts);
1174 		return 0;
1175 	}
1176 
1177 	for (i = 0; i < num_in_bus_fmts; i++) {
1178 		ret = select_bus_fmt_recursive(first_bridge, prev_bridge,
1179 					       crtc_state, conn_state,
1180 					       in_bus_fmts[i]);
1181 		if (ret != -ENOTSUPP)
1182 			break;
1183 	}
1184 
1185 	if (!ret) {
1186 		cur_state->input_bus_cfg.format = in_bus_fmts[i];
1187 		cur_state->output_bus_cfg.format = out_bus_fmt;
1188 	}
1189 
1190 	kfree(in_bus_fmts);
1191 	return ret;
1192 }
1193 
1194 static bool __pure bus_format_is_color_fmt(u32 bus_fmt, enum drm_connector_color_format fmt)
1195 {
1196 	if (fmt == DRM_CONNECTOR_COLOR_FORMAT_AUTO)
1197 		return true;
1198 
1199 	switch (bus_fmt) {
1200 	case MEDIA_BUS_FMT_FIXED:
1201 		return true;
1202 	case MEDIA_BUS_FMT_RGB888_1X24:
1203 	case MEDIA_BUS_FMT_RGB101010_1X30:
1204 	case MEDIA_BUS_FMT_RGB121212_1X36:
1205 	case MEDIA_BUS_FMT_RGB161616_1X48:
1206 		return fmt == DRM_CONNECTOR_COLOR_FORMAT_RGB444;
1207 	case MEDIA_BUS_FMT_YUV8_1X24:
1208 	case MEDIA_BUS_FMT_YUV10_1X30:
1209 	case MEDIA_BUS_FMT_YUV12_1X36:
1210 	case MEDIA_BUS_FMT_YUV16_1X48:
1211 		return fmt == DRM_CONNECTOR_COLOR_FORMAT_YCBCR444;
1212 	case MEDIA_BUS_FMT_UYVY8_1X16:
1213 	case MEDIA_BUS_FMT_VYUY8_1X16:
1214 	case MEDIA_BUS_FMT_YUYV8_1X16:
1215 	case MEDIA_BUS_FMT_YVYU8_1X16:
1216 	case MEDIA_BUS_FMT_UYVY10_1X20:
1217 	case MEDIA_BUS_FMT_YUYV10_1X20:
1218 	case MEDIA_BUS_FMT_VYUY10_1X20:
1219 	case MEDIA_BUS_FMT_YVYU10_1X20:
1220 	case MEDIA_BUS_FMT_UYVY12_1X24:
1221 	case MEDIA_BUS_FMT_VYUY12_1X24:
1222 	case MEDIA_BUS_FMT_YUYV12_1X24:
1223 	case MEDIA_BUS_FMT_YVYU12_1X24:
1224 		return fmt == DRM_CONNECTOR_COLOR_FORMAT_YCBCR422;
1225 	case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
1226 	case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
1227 	case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
1228 	case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
1229 		return fmt == DRM_CONNECTOR_COLOR_FORMAT_YCBCR420;
1230 	default:
1231 		return false;
1232 	}
1233 }
1234 
1235 /*
1236  * This function is called by &drm_atomic_bridge_chain_check() just before
1237  * calling &drm_bridge_funcs.atomic_check() on all elements of the chain.
1238  * It performs bus format negotiation between bridge elements. The negotiation
1239  * happens in reverse order, starting from the last element in the chain up to
1240  * @bridge.
1241  *
1242  * Negotiation starts by retrieving supported output bus formats on the last
1243  * bridge element and testing them one by one. The test is recursive, meaning
1244  * that for each tested output format, the whole chain will be walked backward,
1245  * and each element will have to choose an input bus format that can be
1246  * transcoded to the requested output format. When a bridge element does not
1247  * support transcoding into a specific output format -ENOTSUPP is returned and
1248  * the next bridge element will have to try a different format. If none of the
1249  * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail.
1250  *
1251  * This implementation is relying on
1252  * &drm_bridge_funcs.atomic_get_output_bus_fmts() and
1253  * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported
1254  * input/output formats.
1255  *
1256  * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by
1257  * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts()
1258  * tries a single format: &drm_connector.display_info.bus_formats[0] if
1259  * available, MEDIA_BUS_FMT_FIXED otherwise.
1260  *
1261  * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented,
1262  * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the
1263  * bridge element that lacks this hook and asks the previous element in the
1264  * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what
1265  * to do in that case (fail if they want to enforce bus format negotiation, or
1266  * provide a reasonable default if they need to support pipelines where not
1267  * all elements support bus format negotiation).
1268  */
1269 static int
1270 drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge,
1271 					struct drm_crtc_state *crtc_state,
1272 					struct drm_connector_state *conn_state)
1273 {
1274 	struct drm_connector *conn = conn_state->connector;
1275 	struct drm_encoder *encoder = bridge->encoder;
1276 	struct drm_bridge_state *last_bridge_state;
1277 	unsigned int i, num_out_bus_fmts = 0;
1278 	enum drm_connector_color_format fmt;
1279 	u32 *out_bus_fmts;
1280 	int ret = 0;
1281 
1282 	struct drm_bridge *last_bridge __free(drm_bridge_put) =
1283 		drm_bridge_get(list_last_entry(&encoder->bridge_chain,
1284 					       struct drm_bridge, chain_node));
1285 	last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
1286 							    last_bridge);
1287 
1288 	if (last_bridge->funcs->atomic_get_output_bus_fmts) {
1289 		const struct drm_bridge_funcs *funcs = last_bridge->funcs;
1290 
1291 		/*
1292 		 * If the driver implements ->atomic_get_output_bus_fmts() it
1293 		 * should also implement the atomic state hooks.
1294 		 */
1295 		if (WARN_ON(!last_bridge_state))
1296 			return -EINVAL;
1297 
1298 		out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge,
1299 							last_bridge_state,
1300 							crtc_state,
1301 							conn_state,
1302 							&num_out_bus_fmts);
1303 		if (!num_out_bus_fmts)
1304 			return -ENOTSUPP;
1305 		else if (!out_bus_fmts)
1306 			return -ENOMEM;
1307 	} else {
1308 		num_out_bus_fmts = 1;
1309 		out_bus_fmts = kmalloc_obj(*out_bus_fmts);
1310 		if (!out_bus_fmts)
1311 			return -ENOMEM;
1312 
1313 		if (conn->display_info.num_bus_formats &&
1314 		    conn->display_info.bus_formats)
1315 			out_bus_fmts[0] = conn->display_info.bus_formats[0];
1316 		else
1317 			out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
1318 	}
1319 
1320 	/*
1321 	 * Instead of directly accessing conn_state.color_format, call into a
1322 	 * connector function that allows connector implementations (e.g. for
1323 	 * bridge connectors including HDMI bridges, where the HDMI helpers will
1324 	 * have already chosen an appropriate output format) to override the
1325 	 * selected format.
1326 	 */
1327 	fmt = drm_connector_get_color_format(conn_state);
1328 
1329 	for (i = 0; i < num_out_bus_fmts; i++) {
1330 		if (!bus_format_is_color_fmt(out_bus_fmts[i], fmt)) {
1331 			drm_dbg_kms(last_bridge->dev,
1332 				    "Skipping bus format 0x%04x as it doesn't match format %d\n",
1333 				    out_bus_fmts[i], fmt);
1334 			ret = -ENOTSUPP;
1335 			continue;
1336 		}
1337 		ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state,
1338 					       conn_state, out_bus_fmts[i]);
1339 		if (ret != -ENOTSUPP) {
1340 			drm_dbg_kms(last_bridge->dev,
1341 				    "Found bridge chain ending with bus format 0x%04x\n",
1342 				    out_bus_fmts[i]);
1343 			break;
1344 		}
1345 	}
1346 
1347 	kfree(out_bus_fmts);
1348 
1349 	return ret;
1350 }
1351 
1352 static void
1353 drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge,
1354 				      struct drm_connector *conn,
1355 				      struct drm_atomic_commit *state)
1356 {
1357 	struct drm_bridge_state *bridge_state, *next_bridge_state;
1358 	u32 output_flags = 0;
1359 
1360 	bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
1361 
1362 	/* No bridge state attached to this bridge => nothing to propagate. */
1363 	if (!bridge_state)
1364 		return;
1365 
1366 	struct drm_bridge *next_bridge __free(drm_bridge_put) = drm_bridge_get_next_bridge(bridge);
1367 
1368 	/*
1369 	 * Let's try to apply the most common case here, that is, propagate
1370 	 * display_info flags for the last bridge, and propagate the input
1371 	 * flags of the next bridge element to the output end of the current
1372 	 * bridge when the bridge is not the last one.
1373 	 * There are exceptions to this rule, like when signal inversion is
1374 	 * happening at the board level, but that's something drivers can deal
1375 	 * with from their &drm_bridge_funcs.atomic_check() implementation by
1376 	 * simply overriding the flags value we've set here.
1377 	 */
1378 	if (!next_bridge) {
1379 		output_flags = conn->display_info.bus_flags;
1380 	} else {
1381 		next_bridge_state = drm_atomic_get_new_bridge_state(state,
1382 								next_bridge);
1383 		/*
1384 		 * No bridge state attached to the next bridge, just leave the
1385 		 * flags to 0.
1386 		 */
1387 		if (next_bridge_state)
1388 			output_flags = next_bridge_state->input_bus_cfg.flags;
1389 	}
1390 
1391 	bridge_state->output_bus_cfg.flags = output_flags;
1392 
1393 	/*
1394 	 * Propagate the output flags to the input end of the bridge. Again, it's
1395 	 * not necessarily what all bridges want, but that's what most of them
1396 	 * do, and by doing that by default we avoid forcing drivers to
1397 	 * duplicate the "dummy propagation" logic.
1398 	 */
1399 	bridge_state->input_bus_cfg.flags = output_flags;
1400 }
1401 
1402 /**
1403  * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain
1404  * @bridge: bridge control structure
1405  * @crtc_state: new CRTC state
1406  * @conn_state: new connector state
1407  *
1408  * First trigger a bus format negotiation before calling
1409  * &drm_bridge_funcs.atomic_check() (falls back on
1410  * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain,
1411  * starting from the last bridge to the first. These are called before calling
1412  * &drm_encoder_helper_funcs.atomic_check()
1413  *
1414  * RETURNS:
1415  * 0 on success, a negative error code on failure
1416  */
1417 int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,
1418 				  struct drm_crtc_state *crtc_state,
1419 				  struct drm_connector_state *conn_state)
1420 {
1421 	struct drm_connector *conn = conn_state->connector;
1422 	struct drm_encoder *encoder;
1423 	struct drm_bridge *iter;
1424 	int ret;
1425 
1426 	if (!bridge)
1427 		return 0;
1428 
1429 	ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state,
1430 						      conn_state);
1431 	if (ret)
1432 		return ret;
1433 
1434 	encoder = bridge->encoder;
1435 	scoped_guard(mutex, &encoder->bridge_chain_mutex) {
1436 		list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
1437 			int ret;
1438 
1439 			/*
1440 			 * Bus flags are propagated by default. If a bridge needs to
1441 			 * tweak the input bus flags for any reason, it should happen
1442 			 * in its &drm_bridge_funcs.atomic_check() implementation such
1443 			 * that preceding bridges in the chain can propagate the new
1444 			 * bus flags.
1445 			 */
1446 			drm_atomic_bridge_propagate_bus_flags(iter, conn,
1447 							      crtc_state->state);
1448 
1449 			ret = drm_atomic_bridge_check(iter, crtc_state, conn_state);
1450 			if (ret)
1451 				return ret;
1452 
1453 			if (iter == bridge)
1454 				break;
1455 		}
1456 	}
1457 
1458 	return 0;
1459 }
1460 EXPORT_SYMBOL(drm_atomic_bridge_chain_check);
1461 
1462 /**
1463  * drm_bridge_detect - check if anything is attached to the bridge output
1464  * @bridge: bridge control structure
1465  * @connector: attached connector
1466  *
1467  * If the bridge supports output detection, as reported by the
1468  * DRM_BRIDGE_OP_DETECT bridge ops flag, call &drm_bridge_funcs.detect for the
1469  * bridge and return the connection status. Otherwise return
1470  * connector_status_unknown.
1471  *
1472  * RETURNS:
1473  * The detection status on success, or connector_status_unknown if the bridge
1474  * doesn't support output detection.
1475  */
1476 enum drm_connector_status
1477 drm_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
1478 {
1479 	if (!(bridge->ops & DRM_BRIDGE_OP_DETECT))
1480 		return connector_status_unknown;
1481 
1482 	return bridge->funcs->detect(bridge, connector);
1483 }
1484 EXPORT_SYMBOL_GPL(drm_bridge_detect);
1485 
1486 /**
1487  * drm_bridge_get_modes - fill all modes currently valid for the sink into the
1488  * @connector
1489  * @bridge: bridge control structure
1490  * @connector: the connector to fill with modes
1491  *
1492  * If the bridge supports output modes retrieval, as reported by the
1493  * DRM_BRIDGE_OP_MODES bridge ops flag, call &drm_bridge_funcs.get_modes to
1494  * fill the connector with all valid modes and return the number of modes
1495  * added. Otherwise return 0.
1496  *
1497  * RETURNS:
1498  * The number of modes added to the connector.
1499  */
1500 int drm_bridge_get_modes(struct drm_bridge *bridge,
1501 			 struct drm_connector *connector)
1502 {
1503 	if (!(bridge->ops & DRM_BRIDGE_OP_MODES))
1504 		return 0;
1505 
1506 	return bridge->funcs->get_modes(bridge, connector);
1507 }
1508 EXPORT_SYMBOL_GPL(drm_bridge_get_modes);
1509 
1510 /**
1511  * drm_bridge_edid_read - read the EDID data of the connected display
1512  * @bridge: bridge control structure
1513  * @connector: the connector to read EDID for
1514  *
1515  * If the bridge supports output EDID retrieval, as reported by the
1516  * DRM_BRIDGE_OP_EDID bridge ops flag, call &drm_bridge_funcs.edid_read to get
1517  * the EDID and return it. Otherwise return NULL.
1518  *
1519  * RETURNS:
1520  * The retrieved EDID on success, or NULL otherwise.
1521  */
1522 const struct drm_edid *drm_bridge_edid_read(struct drm_bridge *bridge,
1523 					    struct drm_connector *connector)
1524 {
1525 	if (!(bridge->ops & DRM_BRIDGE_OP_EDID))
1526 		return NULL;
1527 
1528 	return bridge->funcs->edid_read(bridge, connector);
1529 }
1530 EXPORT_SYMBOL_GPL(drm_bridge_edid_read);
1531 
1532 /**
1533  * drm_bridge_hpd_enable - enable hot plug detection for the bridge
1534  * @bridge: bridge control structure
1535  * @cb: hot-plug detection callback
1536  * @data: data to be passed to the hot-plug detection callback
1537  *
1538  * Call &drm_bridge_funcs.hpd_enable if implemented and register the given @cb
1539  * and @data as hot plug notification callback. From now on the @cb will be
1540  * called with @data when an output status change is detected by the bridge,
1541  * until hot plug notification gets disabled with drm_bridge_hpd_disable().
1542  *
1543  * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1544  * bridge->ops. This function shall not be called when the flag is not set.
1545  *
1546  * Only one hot plug detection callback can be registered at a time, it is an
1547  * error to call this function when hot plug detection is already enabled for
1548  * the bridge.
1549  */
1550 void drm_bridge_hpd_enable(struct drm_bridge *bridge,
1551 			   void (*cb)(void *data,
1552 				      enum drm_connector_status status),
1553 			   void *data)
1554 {
1555 	if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1556 		return;
1557 
1558 	mutex_lock(&bridge->hpd_state_mutex);
1559 
1560 	mutex_lock(&bridge->hpd_mutex);
1561 
1562 	if (WARN(bridge->hpd_cb, "Hot plug detection already enabled\n")) {
1563 		mutex_unlock(&bridge->hpd_mutex);
1564 		goto unlock;
1565 	}
1566 
1567 	bridge->hpd_cb = cb;
1568 	bridge->hpd_data = data;
1569 
1570 	mutex_unlock(&bridge->hpd_mutex);
1571 
1572 	if (bridge->funcs->hpd_enable)
1573 		bridge->funcs->hpd_enable(bridge);
1574 
1575 unlock:
1576 	mutex_unlock(&bridge->hpd_state_mutex);
1577 }
1578 EXPORT_SYMBOL_GPL(drm_bridge_hpd_enable);
1579 
1580 /**
1581  * drm_bridge_hpd_disable - disable hot plug detection for the bridge
1582  * @bridge: bridge control structure
1583  *
1584  * Call &drm_bridge_funcs.hpd_disable if implemented and unregister the hot
1585  * plug detection callback previously registered with drm_bridge_hpd_enable().
1586  * Once this function returns the callback will not be called by the bridge
1587  * when an output status change occurs.
1588  *
1589  * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1590  * bridge->ops. This function shall not be called when the flag is not set.
1591  */
1592 void drm_bridge_hpd_disable(struct drm_bridge *bridge)
1593 {
1594 	if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1595 		return;
1596 
1597 	mutex_lock(&bridge->hpd_state_mutex);
1598 	if (bridge->funcs->hpd_disable)
1599 		bridge->funcs->hpd_disable(bridge);
1600 
1601 	mutex_lock(&bridge->hpd_mutex);
1602 	bridge->hpd_cb = NULL;
1603 	bridge->hpd_data = NULL;
1604 	mutex_unlock(&bridge->hpd_mutex);
1605 	mutex_unlock(&bridge->hpd_state_mutex);
1606 }
1607 EXPORT_SYMBOL_GPL(drm_bridge_hpd_disable);
1608 
1609 /**
1610  * drm_bridge_hpd_notify - notify hot plug detection events
1611  * @bridge: bridge control structure
1612  * @status: output connection status
1613  *
1614  * Bridge drivers shall call this function to report hot plug events when they
1615  * detect a change in the output status, when hot plug detection has been
1616  * enabled by drm_bridge_hpd_enable().
1617  *
1618  * This function shall be called in a context that can sleep.
1619  */
1620 void drm_bridge_hpd_notify(struct drm_bridge *bridge,
1621 			   enum drm_connector_status status)
1622 {
1623 	mutex_lock(&bridge->hpd_mutex);
1624 	if (bridge->hpd_cb)
1625 		bridge->hpd_cb(bridge->hpd_data, status);
1626 	mutex_unlock(&bridge->hpd_mutex);
1627 }
1628 EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify);
1629 
1630 #ifdef CONFIG_OF
1631 /**
1632  * of_drm_find_and_get_bridge - find the bridge corresponding to the device
1633  *                              node in the global bridge list
1634  * @np: device node
1635  *
1636  * The refcount of the returned bridge is incremented. Use drm_bridge_put()
1637  * when done with it.
1638  *
1639  * RETURNS:
1640  * drm_bridge control struct on success, NULL on failure
1641  */
1642 struct drm_bridge *of_drm_find_and_get_bridge(struct device_node *np)
1643 {
1644 	struct drm_bridge *bridge;
1645 
1646 	scoped_guard(mutex, &bridge_lock) {
1647 		list_for_each_entry(bridge, &bridge_list, list)
1648 			if (bridge->of_node == np)
1649 				return drm_bridge_get(bridge);
1650 	}
1651 
1652 	return NULL;
1653 }
1654 EXPORT_SYMBOL(of_drm_find_and_get_bridge);
1655 
1656 /**
1657  * of_drm_find_bridge - find the bridge corresponding to the device node in
1658  *			the global bridge list
1659  *
1660  * @np: device node
1661  *
1662  * This function is deprecated. Convert to of_drm_find_and_get_bridge()
1663  * instead for proper refcounting.
1664  *
1665  * The bridge returned by this function is not refcounted. This is
1666  * dangerous because the bridge might be deallocated even before the caller
1667  * has a chance to use it. To use this function you have to do one of:
1668  *
1669  * - get a reference with drm_bridge_get() as soon as possible to
1670  *   minimize the race window, and then drm_bridge_put() when no longer
1671  *   using the pointer
1672  *
1673  * - not call drm_bridge_get() or drm_bridge_put() at all, which used to
1674  *   be the correct practice before dynamic bridge lifetime was introduced
1675  *
1676  * - again, convert to of_drm_find_and_get_bridge(), which is the only safe
1677  *   thing to do
1678  *
1679  * RETURNS:
1680  * drm_bridge control struct on success, NULL on failure
1681  */
1682 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
1683 {
1684 	struct drm_bridge *bridge = of_drm_find_and_get_bridge(np);
1685 
1686 	/*
1687 	 * We need to emulate the original semantics of
1688 	 * of_drm_find_bridge(), which was not getting any bridge
1689 	 * reference. Being now based on of_drm_find_and_get_bridge() which
1690 	 * gets a reference, put it before returning.
1691 	 */
1692 	drm_bridge_put(bridge);
1693 
1694 	return bridge;
1695 }
1696 EXPORT_SYMBOL(of_drm_find_bridge);
1697 
1698 /**
1699  * of_drm_get_bridge_by_endpoint - return DRM bridge connected to a port/endpoint
1700  * @np: device tree node containing output ports
1701  * @port: port in the device tree node, or -1 for the first port found
1702  * @endpoint: endpoint in the device tree node, or -1 for the first endpoint found
1703  *
1704  * Given a DT node's port and endpoint number, find the connected node and
1705  * return the associated drm_bridge device.
1706  *
1707  * The refcount of the returned bridge is incremented. Use drm_bridge_put()
1708  * when done with it.
1709  *
1710  * Returns a pointer to the connected drm_bridge, or a negative error on failure
1711  */
1712 struct drm_bridge *of_drm_get_bridge_by_endpoint(const struct device_node *np,
1713 						 int port, int endpoint)
1714 {
1715 	struct drm_bridge *bridge;
1716 
1717 	/*
1718 	 * of_graph_get_remote_node() produces a noisy error message if port
1719 	 * node isn't found and the absence of the port is a legit case here,
1720 	 * so at first we silently check whether graph is present in the
1721 	 * device-tree node.
1722 	 */
1723 	if (!of_graph_is_present(np))
1724 		return ERR_PTR(-ENODEV);
1725 
1726 	struct device_node *remote __free(device_node) =
1727 		of_graph_get_remote_node(np, port, endpoint);
1728 	if (!remote)
1729 		return ERR_PTR(-ENODEV);
1730 
1731 	bridge = of_drm_find_and_get_bridge(remote);
1732 	if (!bridge)
1733 		return ERR_PTR(-EPROBE_DEFER);
1734 
1735 	return bridge;
1736 }
1737 EXPORT_SYMBOL_GPL(of_drm_get_bridge_by_endpoint);
1738 #endif
1739 
1740 /**
1741  * devm_drm_put_bridge - Release a bridge reference obtained via devm
1742  * @dev: device that got the bridge via devm
1743  * @bridge: pointer to a struct drm_bridge obtained via devm
1744  *
1745  * Same as drm_bridge_put() for bridge pointers obtained via devm functions
1746  * such as devm_drm_bridge_alloc().
1747  *
1748  * This function is a temporary workaround and MUST NOT be used. Manual
1749  * handling of bridge lifetime is inherently unsafe.
1750  */
1751 void devm_drm_put_bridge(struct device *dev, struct drm_bridge *bridge)
1752 {
1753 	devm_release_action(dev, drm_bridge_put_void, bridge);
1754 }
1755 EXPORT_SYMBOL(devm_drm_put_bridge);
1756 
1757 static void drm_bridge_debugfs_show_bridge(struct drm_printer *p,
1758 					   struct drm_bridge *bridge,
1759 					   unsigned int idx,
1760 					   bool lingering,
1761 					   bool scoped)
1762 {
1763 	unsigned int refcount = kref_read(&bridge->refcount);
1764 
1765 	if (scoped)
1766 		refcount--;
1767 
1768 	drm_printf(p, "bridge[%u]: %ps\n", idx, bridge->funcs);
1769 
1770 	drm_printf_indent(p, 1, "refcount: %u%s\n", refcount,
1771 			  lingering ? " [lingering]" : "");
1772 
1773 	drm_printf_indent(p, 1, "type: [%d] %s\n", bridge->type,
1774 			  drm_get_connector_type_name(bridge->type));
1775 
1776 	/* The OF node could be freed after drm_bridge_remove() */
1777 	if (bridge->of_node && !lingering)
1778 		drm_printf_indent(p, 1, "OF: %pOFfc\n", bridge->of_node);
1779 
1780 	drm_printf_indent(p, 1, "ops: [0x%x]", bridge->ops);
1781 	if (bridge->ops & DRM_BRIDGE_OP_DETECT)
1782 		drm_puts(p, " detect");
1783 	if (bridge->ops & DRM_BRIDGE_OP_EDID)
1784 		drm_puts(p, " edid");
1785 	if (bridge->ops & DRM_BRIDGE_OP_HPD)
1786 		drm_puts(p, " hpd");
1787 	if (bridge->ops & DRM_BRIDGE_OP_MODES)
1788 		drm_puts(p, " modes");
1789 	if (bridge->ops & DRM_BRIDGE_OP_HDMI)
1790 		drm_puts(p, " hdmi");
1791 	drm_puts(p, "\n");
1792 }
1793 
1794 static int allbridges_show(struct seq_file *m, void *data)
1795 {
1796 	struct drm_printer p = drm_seq_file_printer(m);
1797 	struct drm_bridge *bridge;
1798 	unsigned int idx = 0;
1799 
1800 	mutex_lock(&bridge_lock);
1801 
1802 	list_for_each_entry(bridge, &bridge_list, list)
1803 		drm_bridge_debugfs_show_bridge(&p, bridge, idx++, false, false);
1804 
1805 	list_for_each_entry(bridge, &bridge_lingering_list, list)
1806 		drm_bridge_debugfs_show_bridge(&p, bridge, idx++, true, false);
1807 
1808 	mutex_unlock(&bridge_lock);
1809 
1810 	return 0;
1811 }
1812 DEFINE_SHOW_ATTRIBUTE(allbridges);
1813 
1814 static int encoder_bridges_show(struct seq_file *m, void *data)
1815 {
1816 	struct drm_encoder *encoder = m->private;
1817 	struct drm_printer p = drm_seq_file_printer(m);
1818 	unsigned int idx = 0;
1819 
1820 	drm_for_each_bridge_in_chain_scoped(encoder, bridge)
1821 		drm_bridge_debugfs_show_bridge(&p, bridge, idx++, false, true);
1822 
1823 	return 0;
1824 }
1825 DEFINE_SHOW_ATTRIBUTE(encoder_bridges);
1826 
1827 void drm_bridge_debugfs_params(struct dentry *root)
1828 {
1829 	debugfs_create_file("bridges", 0444, root, NULL, &allbridges_fops);
1830 }
1831 
1832 void drm_bridge_debugfs_encoder_params(struct dentry *root,
1833 				       struct drm_encoder *encoder)
1834 {
1835 	/* bridges list */
1836 	debugfs_create_file("bridges", 0444, root, encoder, &encoder_bridges_fops);
1837 }
1838 
1839 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
1840 MODULE_DESCRIPTION("DRM bridge infrastructure");
1841 MODULE_LICENSE("GPL and additional rights");
1842