xref: /openbmc/linux/drivers/gpu/drm/drm_bridge.c (revision f4284724)
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/err.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 
28 #include <drm/drm_atomic_state_helper.h>
29 #include <drm/drm_bridge.h>
30 #include <drm/drm_encoder.h>
31 #include <drm/drm_of.h>
32 #include <drm/drm_print.h>
33 
34 #include "drm_crtc_internal.h"
35 
36 /**
37  * DOC: overview
38  *
39  * &struct drm_bridge represents a device that hangs on to an encoder. These are
40  * handy when a regular &drm_encoder entity isn't enough to represent the entire
41  * encoder chain.
42  *
43  * A bridge is always attached to a single &drm_encoder at a time, but can be
44  * either connected to it directly, or through a chain of bridges::
45  *
46  *     [ CRTC ---> ] Encoder ---> Bridge A ---> Bridge B
47  *
48  * Here, the output of the encoder feeds to bridge A, and that furthers feeds to
49  * bridge B. Bridge chains can be arbitrarily long, and shall be fully linear:
50  * Chaining multiple bridges to the output of a bridge, or the same bridge to
51  * the output of different bridges, is not supported.
52  *
53  * &drm_bridge, like &drm_panel, aren't &drm_mode_object entities like planes,
54  * CRTCs, encoders or connectors and hence are not visible to userspace. They
55  * just provide additional hooks to get the desired output at the end of the
56  * encoder chain.
57  */
58 
59 /**
60  * DOC:	display driver integration
61  *
62  * Display drivers are responsible for linking encoders with the first bridge
63  * in the chains. This is done by acquiring the appropriate bridge with
64  * devm_drm_of_get_bridge(). Once acquired, the bridge shall be attached to the
65  * encoder with a call to drm_bridge_attach().
66  *
67  * Bridges are responsible for linking themselves with the next bridge in the
68  * chain, if any. This is done the same way as for encoders, with the call to
69  * drm_bridge_attach() occurring in the &drm_bridge_funcs.attach operation.
70  *
71  * Once these links are created, the bridges can participate along with encoder
72  * functions to perform mode validation and fixup (through
73  * drm_bridge_chain_mode_valid() and drm_atomic_bridge_chain_check()), mode
74  * setting (through drm_bridge_chain_mode_set()), enable (through
75  * drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_enable())
76  * and disable (through drm_atomic_bridge_chain_disable() and
77  * drm_atomic_bridge_chain_post_disable()). Those functions call the
78  * corresponding operations provided in &drm_bridge_funcs in sequence for all
79  * bridges in the chain.
80  *
81  * For display drivers that use the atomic helpers
82  * drm_atomic_helper_check_modeset(),
83  * drm_atomic_helper_commit_modeset_enables() and
84  * drm_atomic_helper_commit_modeset_disables() (either directly in hand-rolled
85  * commit check and commit tail handlers, or through the higher-level
86  * drm_atomic_helper_check() and drm_atomic_helper_commit_tail() or
87  * drm_atomic_helper_commit_tail_rpm() helpers), this is done transparently and
88  * requires no intervention from the driver. For other drivers, the relevant
89  * DRM bridge chain functions shall be called manually.
90  *
91  * Bridges also participate in implementing the &drm_connector at the end of
92  * the bridge chain. Display drivers may use the drm_bridge_connector_init()
93  * helper to create the &drm_connector, or implement it manually on top of the
94  * connector-related operations exposed by the bridge (see the overview
95  * documentation of bridge operations for more details).
96  */
97 
98 /**
99  * DOC: special care dsi
100  *
101  * The interaction between the bridges and other frameworks involved in
102  * the probing of the upstream driver and the bridge driver can be
103  * challenging. Indeed, there's multiple cases that needs to be
104  * considered:
105  *
106  * - The upstream driver doesn't use the component framework and isn't a
107  *   MIPI-DSI host. In this case, the bridge driver will probe at some
108  *   point and the upstream driver should try to probe again by returning
109  *   EPROBE_DEFER as long as the bridge driver hasn't probed.
110  *
111  * - The upstream driver doesn't use the component framework, but is a
112  *   MIPI-DSI host. The bridge device uses the MIPI-DCS commands to be
113  *   controlled. In this case, the bridge device is a child of the
114  *   display device and when it will probe it's assured that the display
115  *   device (and MIPI-DSI host) is present. The upstream driver will be
116  *   assured that the bridge driver is connected between the
117  *   &mipi_dsi_host_ops.attach and &mipi_dsi_host_ops.detach operations.
118  *   Therefore, it must run mipi_dsi_host_register() in its probe
119  *   function, and then run drm_bridge_attach() in its
120  *   &mipi_dsi_host_ops.attach hook.
121  *
122  * - The upstream driver uses the component framework and is a MIPI-DSI
123  *   host. The bridge device uses the MIPI-DCS commands to be
124  *   controlled. This is the same situation than above, and can run
125  *   mipi_dsi_host_register() in either its probe or bind hooks.
126  *
127  * - The upstream driver uses the component framework and is a MIPI-DSI
128  *   host. The bridge device uses a separate bus (such as I2C) to be
129  *   controlled. In this case, there's no correlation between the probe
130  *   of the bridge and upstream drivers, so care must be taken to avoid
131  *   an endless EPROBE_DEFER loop, with each driver waiting for the
132  *   other to probe.
133  *
134  * The ideal pattern to cover the last item (and all the others in the
135  * MIPI-DSI host driver case) is to split the operations like this:
136  *
137  * - The MIPI-DSI host driver must run mipi_dsi_host_register() in its
138  *   probe hook. It will make sure that the MIPI-DSI host sticks around,
139  *   and that the driver's bind can be called.
140  *
141  * - In its probe hook, the bridge driver must try to find its MIPI-DSI
142  *   host, register as a MIPI-DSI device and attach the MIPI-DSI device
143  *   to its host. The bridge driver is now functional.
144  *
145  * - In its &struct mipi_dsi_host_ops.attach hook, the MIPI-DSI host can
146  *   now add its component. Its bind hook will now be called and since
147  *   the bridge driver is attached and registered, we can now look for
148  *   and attach it.
149  *
150  * At this point, we're now certain that both the upstream driver and
151  * the bridge driver are functional and we can't have a deadlock-like
152  * situation when probing.
153  */
154 
155 static DEFINE_MUTEX(bridge_lock);
156 static LIST_HEAD(bridge_list);
157 
158 /**
159  * drm_bridge_add - add the given bridge to the global bridge list
160  *
161  * @bridge: bridge control structure
162  */
163 void drm_bridge_add(struct drm_bridge *bridge)
164 {
165 	mutex_init(&bridge->hpd_mutex);
166 
167 	mutex_lock(&bridge_lock);
168 	list_add_tail(&bridge->list, &bridge_list);
169 	mutex_unlock(&bridge_lock);
170 }
171 EXPORT_SYMBOL(drm_bridge_add);
172 
173 static void drm_bridge_remove_void(void *bridge)
174 {
175 	drm_bridge_remove(bridge);
176 }
177 
178 /**
179  * devm_drm_bridge_add - devm managed version of drm_bridge_add()
180  *
181  * @dev: device to tie the bridge lifetime to
182  * @bridge: bridge control structure
183  *
184  * This is the managed version of drm_bridge_add() which automatically
185  * calls drm_bridge_remove() when @dev is unbound.
186  *
187  * Return: 0 if no error or negative error code.
188  */
189 int devm_drm_bridge_add(struct device *dev, struct drm_bridge *bridge)
190 {
191 	drm_bridge_add(bridge);
192 	return devm_add_action_or_reset(dev, drm_bridge_remove_void, bridge);
193 }
194 EXPORT_SYMBOL(devm_drm_bridge_add);
195 
196 /**
197  * drm_bridge_remove - remove the given bridge from the global bridge list
198  *
199  * @bridge: bridge control structure
200  */
201 void drm_bridge_remove(struct drm_bridge *bridge)
202 {
203 	mutex_lock(&bridge_lock);
204 	list_del_init(&bridge->list);
205 	mutex_unlock(&bridge_lock);
206 
207 	mutex_destroy(&bridge->hpd_mutex);
208 }
209 EXPORT_SYMBOL(drm_bridge_remove);
210 
211 static struct drm_private_state *
212 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)
213 {
214 	struct drm_bridge *bridge = drm_priv_to_bridge(obj);
215 	struct drm_bridge_state *state;
216 
217 	state = bridge->funcs->atomic_duplicate_state(bridge);
218 	return state ? &state->base : NULL;
219 }
220 
221 static void
222 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,
223 				     struct drm_private_state *s)
224 {
225 	struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
226 	struct drm_bridge *bridge = drm_priv_to_bridge(obj);
227 
228 	bridge->funcs->atomic_destroy_state(bridge, state);
229 }
230 
231 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
232 	.atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
233 	.atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
234 };
235 
236 /**
237  * drm_bridge_attach - attach the bridge to an encoder's chain
238  *
239  * @encoder: DRM encoder
240  * @bridge: bridge to attach
241  * @previous: previous bridge in the chain (optional)
242  * @flags: DRM_BRIDGE_ATTACH_* flags
243  *
244  * Called by a kms driver to link the bridge to an encoder's chain. The previous
245  * argument specifies the previous bridge in the chain. If NULL, the bridge is
246  * linked directly at the encoder's output. Otherwise it is linked at the
247  * previous bridge's output.
248  *
249  * If non-NULL the previous bridge must be already attached by a call to this
250  * function.
251  *
252  * Note that bridges attached to encoders are auto-detached during encoder
253  * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
254  * *not* be balanced with a drm_bridge_detach() in driver code.
255  *
256  * RETURNS:
257  * Zero on success, error code on failure
258  */
259 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
260 		      struct drm_bridge *previous,
261 		      enum drm_bridge_attach_flags flags)
262 {
263 	int ret;
264 
265 	if (!encoder || !bridge)
266 		return -EINVAL;
267 
268 	if (previous && (!previous->dev || previous->encoder != encoder))
269 		return -EINVAL;
270 
271 	if (bridge->dev)
272 		return -EBUSY;
273 
274 	bridge->dev = encoder->dev;
275 	bridge->encoder = encoder;
276 
277 	if (previous)
278 		list_add(&bridge->chain_node, &previous->chain_node);
279 	else
280 		list_add(&bridge->chain_node, &encoder->bridge_chain);
281 
282 	if (bridge->funcs->attach) {
283 		ret = bridge->funcs->attach(bridge, flags);
284 		if (ret < 0)
285 			goto err_reset_bridge;
286 	}
287 
288 	if (bridge->funcs->atomic_reset) {
289 		struct drm_bridge_state *state;
290 
291 		state = bridge->funcs->atomic_reset(bridge);
292 		if (IS_ERR(state)) {
293 			ret = PTR_ERR(state);
294 			goto err_detach_bridge;
295 		}
296 
297 		drm_atomic_private_obj_init(bridge->dev, &bridge->base,
298 					    &state->base,
299 					    &drm_bridge_priv_state_funcs);
300 	}
301 
302 	return 0;
303 
304 err_detach_bridge:
305 	if (bridge->funcs->detach)
306 		bridge->funcs->detach(bridge);
307 
308 err_reset_bridge:
309 	bridge->dev = NULL;
310 	bridge->encoder = NULL;
311 	list_del(&bridge->chain_node);
312 
313 #ifdef CONFIG_OF
314 	DRM_ERROR("failed to attach bridge %pOF to encoder %s: %d\n",
315 		  bridge->of_node, encoder->name, ret);
316 #else
317 	DRM_ERROR("failed to attach bridge to encoder %s: %d\n",
318 		  encoder->name, ret);
319 #endif
320 
321 	return ret;
322 }
323 EXPORT_SYMBOL(drm_bridge_attach);
324 
325 void drm_bridge_detach(struct drm_bridge *bridge)
326 {
327 	if (WARN_ON(!bridge))
328 		return;
329 
330 	if (WARN_ON(!bridge->dev))
331 		return;
332 
333 	if (bridge->funcs->atomic_reset)
334 		drm_atomic_private_obj_fini(&bridge->base);
335 
336 	if (bridge->funcs->detach)
337 		bridge->funcs->detach(bridge);
338 
339 	list_del(&bridge->chain_node);
340 	bridge->dev = NULL;
341 }
342 
343 /**
344  * DOC: bridge operations
345  *
346  * Bridge drivers expose operations through the &drm_bridge_funcs structure.
347  * The DRM internals (atomic and CRTC helpers) use the helpers defined in
348  * drm_bridge.c to call bridge operations. Those operations are divided in
349  * three big categories to support different parts of the bridge usage.
350  *
351  * - The encoder-related operations support control of the bridges in the
352  *   chain, and are roughly counterparts to the &drm_encoder_helper_funcs
353  *   operations. They are used by the legacy CRTC and the atomic modeset
354  *   helpers to perform mode validation, fixup and setting, and enable and
355  *   disable the bridge automatically.
356  *
357  *   The enable and disable operations are split in
358  *   &drm_bridge_funcs.pre_enable, &drm_bridge_funcs.enable,
359  *   &drm_bridge_funcs.disable and &drm_bridge_funcs.post_disable to provide
360  *   finer-grained control.
361  *
362  *   Bridge drivers may implement the legacy version of those operations, or
363  *   the atomic version (prefixed with atomic\_), in which case they shall also
364  *   implement the atomic state bookkeeping operations
365  *   (&drm_bridge_funcs.atomic_duplicate_state,
366  *   &drm_bridge_funcs.atomic_destroy_state and &drm_bridge_funcs.reset).
367  *   Mixing atomic and non-atomic versions of the operations is not supported.
368  *
369  * - The bus format negotiation operations
370  *   &drm_bridge_funcs.atomic_get_output_bus_fmts and
371  *   &drm_bridge_funcs.atomic_get_input_bus_fmts allow bridge drivers to
372  *   negotiate the formats transmitted between bridges in the chain when
373  *   multiple formats are supported. Negotiation for formats is performed
374  *   transparently for display drivers by the atomic modeset helpers. Only
375  *   atomic versions of those operations exist, bridge drivers that need to
376  *   implement them shall thus also implement the atomic version of the
377  *   encoder-related operations. This feature is not supported by the legacy
378  *   CRTC helpers.
379  *
380  * - The connector-related operations support implementing a &drm_connector
381  *   based on a chain of bridges. DRM bridges traditionally create a
382  *   &drm_connector for bridges meant to be used at the end of the chain. This
383  *   puts additional burden on bridge drivers, especially for bridges that may
384  *   be used in the middle of a chain or at the end of it. Furthermore, it
385  *   requires all operations of the &drm_connector to be handled by a single
386  *   bridge, which doesn't always match the hardware architecture.
387  *
388  *   To simplify bridge drivers and make the connector implementation more
389  *   flexible, a new model allows bridges to unconditionally skip creation of
390  *   &drm_connector and instead expose &drm_bridge_funcs operations to support
391  *   an externally-implemented &drm_connector. Those operations are
392  *   &drm_bridge_funcs.detect, &drm_bridge_funcs.get_modes,
393  *   &drm_bridge_funcs.get_edid, &drm_bridge_funcs.hpd_notify,
394  *   &drm_bridge_funcs.hpd_enable and &drm_bridge_funcs.hpd_disable. When
395  *   implemented, display drivers shall create a &drm_connector instance for
396  *   each chain of bridges, and implement those connector instances based on
397  *   the bridge connector operations.
398  *
399  *   Bridge drivers shall implement the connector-related operations for all
400  *   the features that the bridge hardware support. For instance, if a bridge
401  *   supports reading EDID, the &drm_bridge_funcs.get_edid shall be
402  *   implemented. This however doesn't mean that the DDC lines are wired to the
403  *   bridge on a particular platform, as they could also be connected to an I2C
404  *   controller of the SoC. Support for the connector-related operations on the
405  *   running platform is reported through the &drm_bridge.ops flags. Bridge
406  *   drivers shall detect which operations they can support on the platform
407  *   (usually this information is provided by ACPI or DT), and set the
408  *   &drm_bridge.ops flags for all supported operations. A flag shall only be
409  *   set if the corresponding &drm_bridge_funcs operation is implemented, but
410  *   an implemented operation doesn't necessarily imply that the corresponding
411  *   flag will be set. Display drivers shall use the &drm_bridge.ops flags to
412  *   decide which bridge to delegate a connector operation to. This mechanism
413  *   allows providing a single static const &drm_bridge_funcs instance in
414  *   bridge drivers, improving security by storing function pointers in
415  *   read-only memory.
416  *
417  *   In order to ease transition, bridge drivers may support both the old and
418  *   new models by making connector creation optional and implementing the
419  *   connected-related bridge operations. Connector creation is then controlled
420  *   by the flags argument to the drm_bridge_attach() function. Display drivers
421  *   that support the new model and create connectors themselves shall set the
422  *   %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag, and bridge drivers shall then skip
423  *   connector creation. For intermediate bridges in the chain, the flag shall
424  *   be passed to the drm_bridge_attach() call for the downstream bridge.
425  *   Bridge drivers that implement the new model only shall return an error
426  *   from their &drm_bridge_funcs.attach handler when the
427  *   %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag is not set. New display drivers
428  *   should use the new model, and convert the bridge drivers they use if
429  *   needed, in order to gradually transition to the new model.
430  */
431 
432 /**
433  * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
434  *				 encoder chain
435  * @bridge: bridge control structure
436  * @mode: desired mode to be set for the bridge
437  * @adjusted_mode: updated mode that works for this bridge
438  *
439  * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
440  * encoder chain, starting from the first bridge to the last.
441  *
442  * Note: the bridge passed should be the one closest to the encoder
443  *
444  * RETURNS:
445  * true on success, false on failure
446  */
447 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
448 				 const struct drm_display_mode *mode,
449 				 struct drm_display_mode *adjusted_mode)
450 {
451 	struct drm_encoder *encoder;
452 
453 	if (!bridge)
454 		return true;
455 
456 	encoder = bridge->encoder;
457 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
458 		if (!bridge->funcs->mode_fixup)
459 			continue;
460 
461 		if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
462 			return false;
463 	}
464 
465 	return true;
466 }
467 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
468 
469 /**
470  * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
471  *				 encoder chain.
472  * @bridge: bridge control structure
473  * @info: display info against which the mode shall be validated
474  * @mode: desired mode to be validated
475  *
476  * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
477  * chain, starting from the first bridge to the last. If at least one bridge
478  * does not accept the mode the function returns the error code.
479  *
480  * Note: the bridge passed should be the one closest to the encoder.
481  *
482  * RETURNS:
483  * MODE_OK on success, drm_mode_status Enum error code on failure
484  */
485 enum drm_mode_status
486 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
487 			    const struct drm_display_info *info,
488 			    const struct drm_display_mode *mode)
489 {
490 	struct drm_encoder *encoder;
491 
492 	if (!bridge)
493 		return MODE_OK;
494 
495 	encoder = bridge->encoder;
496 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
497 		enum drm_mode_status ret;
498 
499 		if (!bridge->funcs->mode_valid)
500 			continue;
501 
502 		ret = bridge->funcs->mode_valid(bridge, info, mode);
503 		if (ret != MODE_OK)
504 			return ret;
505 	}
506 
507 	return MODE_OK;
508 }
509 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
510 
511 /**
512  * drm_bridge_chain_disable - disables all bridges in the encoder chain
513  * @bridge: bridge control structure
514  *
515  * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
516  * chain, starting from the last bridge to the first. These are called before
517  * calling the encoder's prepare op.
518  *
519  * Note: the bridge passed should be the one closest to the encoder
520  */
521 void drm_bridge_chain_disable(struct drm_bridge *bridge)
522 {
523 	struct drm_encoder *encoder;
524 	struct drm_bridge *iter;
525 
526 	if (!bridge)
527 		return;
528 
529 	encoder = bridge->encoder;
530 	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
531 		if (iter->funcs->disable)
532 			iter->funcs->disable(iter);
533 
534 		if (iter == bridge)
535 			break;
536 	}
537 }
538 EXPORT_SYMBOL(drm_bridge_chain_disable);
539 
540 /**
541  * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the
542  *				   encoder chain
543  * @bridge: bridge control structure
544  *
545  * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
546  * encoder chain, starting from the first bridge to the last. These are called
547  * after completing the encoder's prepare op.
548  *
549  * Note: the bridge passed should be the one closest to the encoder
550  */
551 void drm_bridge_chain_post_disable(struct drm_bridge *bridge)
552 {
553 	struct drm_encoder *encoder;
554 
555 	if (!bridge)
556 		return;
557 
558 	encoder = bridge->encoder;
559 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
560 		if (bridge->funcs->post_disable)
561 			bridge->funcs->post_disable(bridge);
562 	}
563 }
564 EXPORT_SYMBOL(drm_bridge_chain_post_disable);
565 
566 /**
567  * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
568  *			       encoder chain
569  * @bridge: bridge control structure
570  * @mode: desired mode to be set for the encoder chain
571  * @adjusted_mode: updated mode that works for this encoder chain
572  *
573  * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
574  * encoder chain, starting from the first bridge to the last.
575  *
576  * Note: the bridge passed should be the one closest to the encoder
577  */
578 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
579 			       const struct drm_display_mode *mode,
580 			       const struct drm_display_mode *adjusted_mode)
581 {
582 	struct drm_encoder *encoder;
583 
584 	if (!bridge)
585 		return;
586 
587 	encoder = bridge->encoder;
588 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
589 		if (bridge->funcs->mode_set)
590 			bridge->funcs->mode_set(bridge, mode, adjusted_mode);
591 	}
592 }
593 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
594 
595 /**
596  * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the
597  *				 encoder chain
598  * @bridge: bridge control structure
599  *
600  * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
601  * chain, starting from the last bridge to the first. These are called
602  * before calling the encoder's commit op.
603  *
604  * Note: the bridge passed should be the one closest to the encoder
605  */
606 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge)
607 {
608 	struct drm_encoder *encoder;
609 	struct drm_bridge *iter;
610 
611 	if (!bridge)
612 		return;
613 
614 	encoder = bridge->encoder;
615 	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
616 		if (iter->funcs->pre_enable)
617 			iter->funcs->pre_enable(iter);
618 
619 		if (iter == bridge)
620 			break;
621 	}
622 }
623 EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
624 
625 /**
626  * drm_bridge_chain_enable - enables all bridges in the encoder chain
627  * @bridge: bridge control structure
628  *
629  * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
630  * chain, starting from the first bridge to the last. These are called
631  * after completing the encoder's commit op.
632  *
633  * Note that the bridge passed should be the one closest to the encoder
634  */
635 void drm_bridge_chain_enable(struct drm_bridge *bridge)
636 {
637 	struct drm_encoder *encoder;
638 
639 	if (!bridge)
640 		return;
641 
642 	encoder = bridge->encoder;
643 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
644 		if (bridge->funcs->enable)
645 			bridge->funcs->enable(bridge);
646 	}
647 }
648 EXPORT_SYMBOL(drm_bridge_chain_enable);
649 
650 /**
651  * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
652  * @bridge: bridge control structure
653  * @old_state: old atomic state
654  *
655  * Calls &drm_bridge_funcs.atomic_disable (falls back on
656  * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
657  * starting from the last bridge to the first. These are called before calling
658  * &drm_encoder_helper_funcs.atomic_disable
659  *
660  * Note: the bridge passed should be the one closest to the encoder
661  */
662 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
663 				     struct drm_atomic_state *old_state)
664 {
665 	struct drm_encoder *encoder;
666 	struct drm_bridge *iter;
667 
668 	if (!bridge)
669 		return;
670 
671 	encoder = bridge->encoder;
672 	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
673 		if (iter->funcs->atomic_disable) {
674 			struct drm_bridge_state *old_bridge_state;
675 
676 			old_bridge_state =
677 				drm_atomic_get_old_bridge_state(old_state,
678 								iter);
679 			if (WARN_ON(!old_bridge_state))
680 				return;
681 
682 			iter->funcs->atomic_disable(iter, old_bridge_state);
683 		} else if (iter->funcs->disable) {
684 			iter->funcs->disable(iter);
685 		}
686 
687 		if (iter == bridge)
688 			break;
689 	}
690 }
691 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
692 
693 /**
694  * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
695  *					  in the encoder chain
696  * @bridge: bridge control structure
697  * @old_state: old atomic state
698  *
699  * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
700  * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
701  * starting from the first bridge to the last. These are called after completing
702  * &drm_encoder_helper_funcs.atomic_disable
703  *
704  * Note: the bridge passed should be the one closest to the encoder
705  */
706 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
707 					  struct drm_atomic_state *old_state)
708 {
709 	struct drm_encoder *encoder;
710 
711 	if (!bridge)
712 		return;
713 
714 	encoder = bridge->encoder;
715 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
716 		if (bridge->funcs->atomic_post_disable) {
717 			struct drm_bridge_state *old_bridge_state;
718 
719 			old_bridge_state =
720 				drm_atomic_get_old_bridge_state(old_state,
721 								bridge);
722 			if (WARN_ON(!old_bridge_state))
723 				return;
724 
725 			bridge->funcs->atomic_post_disable(bridge,
726 							   old_bridge_state);
727 		} else if (bridge->funcs->post_disable) {
728 			bridge->funcs->post_disable(bridge);
729 		}
730 	}
731 }
732 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
733 
734 /**
735  * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
736  *					the encoder chain
737  * @bridge: bridge control structure
738  * @old_state: old atomic state
739  *
740  * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
741  * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
742  * starting from the last bridge to the first. These are called before calling
743  * &drm_encoder_helper_funcs.atomic_enable
744  *
745  * Note: the bridge passed should be the one closest to the encoder
746  */
747 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
748 					struct drm_atomic_state *old_state)
749 {
750 	struct drm_encoder *encoder;
751 	struct drm_bridge *iter;
752 
753 	if (!bridge)
754 		return;
755 
756 	encoder = bridge->encoder;
757 	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
758 		if (iter->funcs->atomic_pre_enable) {
759 			struct drm_bridge_state *old_bridge_state;
760 
761 			old_bridge_state =
762 				drm_atomic_get_old_bridge_state(old_state,
763 								iter);
764 			if (WARN_ON(!old_bridge_state))
765 				return;
766 
767 			iter->funcs->atomic_pre_enable(iter, old_bridge_state);
768 		} else if (iter->funcs->pre_enable) {
769 			iter->funcs->pre_enable(iter);
770 		}
771 
772 		if (iter == bridge)
773 			break;
774 	}
775 }
776 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
777 
778 /**
779  * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
780  * @bridge: bridge control structure
781  * @old_state: old atomic state
782  *
783  * Calls &drm_bridge_funcs.atomic_enable (falls back on
784  * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
785  * starting from the first bridge to the last. These are called after completing
786  * &drm_encoder_helper_funcs.atomic_enable
787  *
788  * Note: the bridge passed should be the one closest to the encoder
789  */
790 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
791 				    struct drm_atomic_state *old_state)
792 {
793 	struct drm_encoder *encoder;
794 
795 	if (!bridge)
796 		return;
797 
798 	encoder = bridge->encoder;
799 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
800 		if (bridge->funcs->atomic_enable) {
801 			struct drm_bridge_state *old_bridge_state;
802 
803 			old_bridge_state =
804 				drm_atomic_get_old_bridge_state(old_state,
805 								bridge);
806 			if (WARN_ON(!old_bridge_state))
807 				return;
808 
809 			bridge->funcs->atomic_enable(bridge, old_bridge_state);
810 		} else if (bridge->funcs->enable) {
811 			bridge->funcs->enable(bridge);
812 		}
813 	}
814 }
815 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
816 
817 static int drm_atomic_bridge_check(struct drm_bridge *bridge,
818 				   struct drm_crtc_state *crtc_state,
819 				   struct drm_connector_state *conn_state)
820 {
821 	if (bridge->funcs->atomic_check) {
822 		struct drm_bridge_state *bridge_state;
823 		int ret;
824 
825 		bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
826 							       bridge);
827 		if (WARN_ON(!bridge_state))
828 			return -EINVAL;
829 
830 		ret = bridge->funcs->atomic_check(bridge, bridge_state,
831 						  crtc_state, conn_state);
832 		if (ret)
833 			return ret;
834 	} else if (bridge->funcs->mode_fixup) {
835 		if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode,
836 					       &crtc_state->adjusted_mode))
837 			return -EINVAL;
838 	}
839 
840 	return 0;
841 }
842 
843 static int select_bus_fmt_recursive(struct drm_bridge *first_bridge,
844 				    struct drm_bridge *cur_bridge,
845 				    struct drm_crtc_state *crtc_state,
846 				    struct drm_connector_state *conn_state,
847 				    u32 out_bus_fmt)
848 {
849 	struct drm_bridge_state *cur_state;
850 	unsigned int num_in_bus_fmts, i;
851 	struct drm_bridge *prev_bridge;
852 	u32 *in_bus_fmts;
853 	int ret;
854 
855 	prev_bridge = drm_bridge_get_prev_bridge(cur_bridge);
856 	cur_state = drm_atomic_get_new_bridge_state(crtc_state->state,
857 						    cur_bridge);
858 
859 	/*
860 	 * If bus format negotiation is not supported by this bridge, let's
861 	 * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and
862 	 * hope that it can handle this situation gracefully (by providing
863 	 * appropriate default values).
864 	 */
865 	if (!cur_bridge->funcs->atomic_get_input_bus_fmts) {
866 		if (cur_bridge != first_bridge) {
867 			ret = select_bus_fmt_recursive(first_bridge,
868 						       prev_bridge, crtc_state,
869 						       conn_state,
870 						       MEDIA_BUS_FMT_FIXED);
871 			if (ret)
872 				return ret;
873 		}
874 
875 		/*
876 		 * Driver does not implement the atomic state hooks, but that's
877 		 * fine, as long as it does not access the bridge state.
878 		 */
879 		if (cur_state) {
880 			cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
881 			cur_state->output_bus_cfg.format = out_bus_fmt;
882 		}
883 
884 		return 0;
885 	}
886 
887 	/*
888 	 * If the driver implements ->atomic_get_input_bus_fmts() it
889 	 * should also implement the atomic state hooks.
890 	 */
891 	if (WARN_ON(!cur_state))
892 		return -EINVAL;
893 
894 	in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge,
895 							cur_state,
896 							crtc_state,
897 							conn_state,
898 							out_bus_fmt,
899 							&num_in_bus_fmts);
900 	if (!num_in_bus_fmts)
901 		return -ENOTSUPP;
902 	else if (!in_bus_fmts)
903 		return -ENOMEM;
904 
905 	if (first_bridge == cur_bridge) {
906 		cur_state->input_bus_cfg.format = in_bus_fmts[0];
907 		cur_state->output_bus_cfg.format = out_bus_fmt;
908 		kfree(in_bus_fmts);
909 		return 0;
910 	}
911 
912 	for (i = 0; i < num_in_bus_fmts; i++) {
913 		ret = select_bus_fmt_recursive(first_bridge, prev_bridge,
914 					       crtc_state, conn_state,
915 					       in_bus_fmts[i]);
916 		if (ret != -ENOTSUPP)
917 			break;
918 	}
919 
920 	if (!ret) {
921 		cur_state->input_bus_cfg.format = in_bus_fmts[i];
922 		cur_state->output_bus_cfg.format = out_bus_fmt;
923 	}
924 
925 	kfree(in_bus_fmts);
926 	return ret;
927 }
928 
929 /*
930  * This function is called by &drm_atomic_bridge_chain_check() just before
931  * calling &drm_bridge_funcs.atomic_check() on all elements of the chain.
932  * It performs bus format negotiation between bridge elements. The negotiation
933  * happens in reverse order, starting from the last element in the chain up to
934  * @bridge.
935  *
936  * Negotiation starts by retrieving supported output bus formats on the last
937  * bridge element and testing them one by one. The test is recursive, meaning
938  * that for each tested output format, the whole chain will be walked backward,
939  * and each element will have to choose an input bus format that can be
940  * transcoded to the requested output format. When a bridge element does not
941  * support transcoding into a specific output format -ENOTSUPP is returned and
942  * the next bridge element will have to try a different format. If none of the
943  * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail.
944  *
945  * This implementation is relying on
946  * &drm_bridge_funcs.atomic_get_output_bus_fmts() and
947  * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported
948  * input/output formats.
949  *
950  * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by
951  * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts()
952  * tries a single format: &drm_connector.display_info.bus_formats[0] if
953  * available, MEDIA_BUS_FMT_FIXED otherwise.
954  *
955  * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented,
956  * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the
957  * bridge element that lacks this hook and asks the previous element in the
958  * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what
959  * to do in that case (fail if they want to enforce bus format negotiation, or
960  * provide a reasonable default if they need to support pipelines where not
961  * all elements support bus format negotiation).
962  */
963 static int
964 drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge,
965 					struct drm_crtc_state *crtc_state,
966 					struct drm_connector_state *conn_state)
967 {
968 	struct drm_connector *conn = conn_state->connector;
969 	struct drm_encoder *encoder = bridge->encoder;
970 	struct drm_bridge_state *last_bridge_state;
971 	unsigned int i, num_out_bus_fmts;
972 	struct drm_bridge *last_bridge;
973 	u32 *out_bus_fmts;
974 	int ret = 0;
975 
976 	last_bridge = list_last_entry(&encoder->bridge_chain,
977 				      struct drm_bridge, chain_node);
978 	last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
979 							    last_bridge);
980 
981 	if (last_bridge->funcs->atomic_get_output_bus_fmts) {
982 		const struct drm_bridge_funcs *funcs = last_bridge->funcs;
983 
984 		/*
985 		 * If the driver implements ->atomic_get_output_bus_fmts() it
986 		 * should also implement the atomic state hooks.
987 		 */
988 		if (WARN_ON(!last_bridge_state))
989 			return -EINVAL;
990 
991 		out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge,
992 							last_bridge_state,
993 							crtc_state,
994 							conn_state,
995 							&num_out_bus_fmts);
996 		if (!num_out_bus_fmts)
997 			return -ENOTSUPP;
998 		else if (!out_bus_fmts)
999 			return -ENOMEM;
1000 	} else {
1001 		num_out_bus_fmts = 1;
1002 		out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL);
1003 		if (!out_bus_fmts)
1004 			return -ENOMEM;
1005 
1006 		if (conn->display_info.num_bus_formats &&
1007 		    conn->display_info.bus_formats)
1008 			out_bus_fmts[0] = conn->display_info.bus_formats[0];
1009 		else
1010 			out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
1011 	}
1012 
1013 	for (i = 0; i < num_out_bus_fmts; i++) {
1014 		ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state,
1015 					       conn_state, out_bus_fmts[i]);
1016 		if (ret != -ENOTSUPP)
1017 			break;
1018 	}
1019 
1020 	kfree(out_bus_fmts);
1021 
1022 	return ret;
1023 }
1024 
1025 static void
1026 drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge,
1027 				      struct drm_connector *conn,
1028 				      struct drm_atomic_state *state)
1029 {
1030 	struct drm_bridge_state *bridge_state, *next_bridge_state;
1031 	struct drm_bridge *next_bridge;
1032 	u32 output_flags = 0;
1033 
1034 	bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
1035 
1036 	/* No bridge state attached to this bridge => nothing to propagate. */
1037 	if (!bridge_state)
1038 		return;
1039 
1040 	next_bridge = drm_bridge_get_next_bridge(bridge);
1041 
1042 	/*
1043 	 * Let's try to apply the most common case here, that is, propagate
1044 	 * display_info flags for the last bridge, and propagate the input
1045 	 * flags of the next bridge element to the output end of the current
1046 	 * bridge when the bridge is not the last one.
1047 	 * There are exceptions to this rule, like when signal inversion is
1048 	 * happening at the board level, but that's something drivers can deal
1049 	 * with from their &drm_bridge_funcs.atomic_check() implementation by
1050 	 * simply overriding the flags value we've set here.
1051 	 */
1052 	if (!next_bridge) {
1053 		output_flags = conn->display_info.bus_flags;
1054 	} else {
1055 		next_bridge_state = drm_atomic_get_new_bridge_state(state,
1056 								next_bridge);
1057 		/*
1058 		 * No bridge state attached to the next bridge, just leave the
1059 		 * flags to 0.
1060 		 */
1061 		if (next_bridge_state)
1062 			output_flags = next_bridge_state->input_bus_cfg.flags;
1063 	}
1064 
1065 	bridge_state->output_bus_cfg.flags = output_flags;
1066 
1067 	/*
1068 	 * Propagate the output flags to the input end of the bridge. Again, it's
1069 	 * not necessarily what all bridges want, but that's what most of them
1070 	 * do, and by doing that by default we avoid forcing drivers to
1071 	 * duplicate the "dummy propagation" logic.
1072 	 */
1073 	bridge_state->input_bus_cfg.flags = output_flags;
1074 }
1075 
1076 /**
1077  * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain
1078  * @bridge: bridge control structure
1079  * @crtc_state: new CRTC state
1080  * @conn_state: new connector state
1081  *
1082  * First trigger a bus format negotiation before calling
1083  * &drm_bridge_funcs.atomic_check() (falls back on
1084  * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain,
1085  * starting from the last bridge to the first. These are called before calling
1086  * &drm_encoder_helper_funcs.atomic_check()
1087  *
1088  * RETURNS:
1089  * 0 on success, a negative error code on failure
1090  */
1091 int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,
1092 				  struct drm_crtc_state *crtc_state,
1093 				  struct drm_connector_state *conn_state)
1094 {
1095 	struct drm_connector *conn = conn_state->connector;
1096 	struct drm_encoder *encoder;
1097 	struct drm_bridge *iter;
1098 	int ret;
1099 
1100 	if (!bridge)
1101 		return 0;
1102 
1103 	ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state,
1104 						      conn_state);
1105 	if (ret)
1106 		return ret;
1107 
1108 	encoder = bridge->encoder;
1109 	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
1110 		int ret;
1111 
1112 		/*
1113 		 * Bus flags are propagated by default. If a bridge needs to
1114 		 * tweak the input bus flags for any reason, it should happen
1115 		 * in its &drm_bridge_funcs.atomic_check() implementation such
1116 		 * that preceding bridges in the chain can propagate the new
1117 		 * bus flags.
1118 		 */
1119 		drm_atomic_bridge_propagate_bus_flags(iter, conn,
1120 						      crtc_state->state);
1121 
1122 		ret = drm_atomic_bridge_check(iter, crtc_state, conn_state);
1123 		if (ret)
1124 			return ret;
1125 
1126 		if (iter == bridge)
1127 			break;
1128 	}
1129 
1130 	return 0;
1131 }
1132 EXPORT_SYMBOL(drm_atomic_bridge_chain_check);
1133 
1134 /**
1135  * drm_bridge_detect - check if anything is attached to the bridge output
1136  * @bridge: bridge control structure
1137  *
1138  * If the bridge supports output detection, as reported by the
1139  * DRM_BRIDGE_OP_DETECT bridge ops flag, call &drm_bridge_funcs.detect for the
1140  * bridge and return the connection status. Otherwise return
1141  * connector_status_unknown.
1142  *
1143  * RETURNS:
1144  * The detection status on success, or connector_status_unknown if the bridge
1145  * doesn't support output detection.
1146  */
1147 enum drm_connector_status drm_bridge_detect(struct drm_bridge *bridge)
1148 {
1149 	if (!(bridge->ops & DRM_BRIDGE_OP_DETECT))
1150 		return connector_status_unknown;
1151 
1152 	return bridge->funcs->detect(bridge);
1153 }
1154 EXPORT_SYMBOL_GPL(drm_bridge_detect);
1155 
1156 /**
1157  * drm_bridge_get_modes - fill all modes currently valid for the sink into the
1158  * @connector
1159  * @bridge: bridge control structure
1160  * @connector: the connector to fill with modes
1161  *
1162  * If the bridge supports output modes retrieval, as reported by the
1163  * DRM_BRIDGE_OP_MODES bridge ops flag, call &drm_bridge_funcs.get_modes to
1164  * fill the connector with all valid modes and return the number of modes
1165  * added. Otherwise return 0.
1166  *
1167  * RETURNS:
1168  * The number of modes added to the connector.
1169  */
1170 int drm_bridge_get_modes(struct drm_bridge *bridge,
1171 			 struct drm_connector *connector)
1172 {
1173 	if (!(bridge->ops & DRM_BRIDGE_OP_MODES))
1174 		return 0;
1175 
1176 	return bridge->funcs->get_modes(bridge, connector);
1177 }
1178 EXPORT_SYMBOL_GPL(drm_bridge_get_modes);
1179 
1180 /**
1181  * drm_bridge_get_edid - get the EDID data of the connected display
1182  * @bridge: bridge control structure
1183  * @connector: the connector to read EDID for
1184  *
1185  * If the bridge supports output EDID retrieval, as reported by the
1186  * DRM_BRIDGE_OP_EDID bridge ops flag, call &drm_bridge_funcs.get_edid to
1187  * get the EDID and return it. Otherwise return NULL.
1188  *
1189  * RETURNS:
1190  * The retrieved EDID on success, or NULL otherwise.
1191  */
1192 struct edid *drm_bridge_get_edid(struct drm_bridge *bridge,
1193 				 struct drm_connector *connector)
1194 {
1195 	if (!(bridge->ops & DRM_BRIDGE_OP_EDID))
1196 		return NULL;
1197 
1198 	return bridge->funcs->get_edid(bridge, connector);
1199 }
1200 EXPORT_SYMBOL_GPL(drm_bridge_get_edid);
1201 
1202 /**
1203  * drm_bridge_hpd_enable - enable hot plug detection for the bridge
1204  * @bridge: bridge control structure
1205  * @cb: hot-plug detection callback
1206  * @data: data to be passed to the hot-plug detection callback
1207  *
1208  * Call &drm_bridge_funcs.hpd_enable if implemented and register the given @cb
1209  * and @data as hot plug notification callback. From now on the @cb will be
1210  * called with @data when an output status change is detected by the bridge,
1211  * until hot plug notification gets disabled with drm_bridge_hpd_disable().
1212  *
1213  * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1214  * bridge->ops. This function shall not be called when the flag is not set.
1215  *
1216  * Only one hot plug detection callback can be registered at a time, it is an
1217  * error to call this function when hot plug detection is already enabled for
1218  * the bridge.
1219  */
1220 void drm_bridge_hpd_enable(struct drm_bridge *bridge,
1221 			   void (*cb)(void *data,
1222 				      enum drm_connector_status status),
1223 			   void *data)
1224 {
1225 	if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1226 		return;
1227 
1228 	mutex_lock(&bridge->hpd_mutex);
1229 
1230 	if (WARN(bridge->hpd_cb, "Hot plug detection already enabled\n"))
1231 		goto unlock;
1232 
1233 	bridge->hpd_cb = cb;
1234 	bridge->hpd_data = data;
1235 
1236 	if (bridge->funcs->hpd_enable)
1237 		bridge->funcs->hpd_enable(bridge);
1238 
1239 unlock:
1240 	mutex_unlock(&bridge->hpd_mutex);
1241 }
1242 EXPORT_SYMBOL_GPL(drm_bridge_hpd_enable);
1243 
1244 /**
1245  * drm_bridge_hpd_disable - disable hot plug detection for the bridge
1246  * @bridge: bridge control structure
1247  *
1248  * Call &drm_bridge_funcs.hpd_disable if implemented and unregister the hot
1249  * plug detection callback previously registered with drm_bridge_hpd_enable().
1250  * Once this function returns the callback will not be called by the bridge
1251  * when an output status change occurs.
1252  *
1253  * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1254  * bridge->ops. This function shall not be called when the flag is not set.
1255  */
1256 void drm_bridge_hpd_disable(struct drm_bridge *bridge)
1257 {
1258 	if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1259 		return;
1260 
1261 	mutex_lock(&bridge->hpd_mutex);
1262 	if (bridge->funcs->hpd_disable)
1263 		bridge->funcs->hpd_disable(bridge);
1264 
1265 	bridge->hpd_cb = NULL;
1266 	bridge->hpd_data = NULL;
1267 	mutex_unlock(&bridge->hpd_mutex);
1268 }
1269 EXPORT_SYMBOL_GPL(drm_bridge_hpd_disable);
1270 
1271 /**
1272  * drm_bridge_hpd_notify - notify hot plug detection events
1273  * @bridge: bridge control structure
1274  * @status: output connection status
1275  *
1276  * Bridge drivers shall call this function to report hot plug events when they
1277  * detect a change in the output status, when hot plug detection has been
1278  * enabled by drm_bridge_hpd_enable().
1279  *
1280  * This function shall be called in a context that can sleep.
1281  */
1282 void drm_bridge_hpd_notify(struct drm_bridge *bridge,
1283 			   enum drm_connector_status status)
1284 {
1285 	mutex_lock(&bridge->hpd_mutex);
1286 	if (bridge->hpd_cb)
1287 		bridge->hpd_cb(bridge->hpd_data, status);
1288 	mutex_unlock(&bridge->hpd_mutex);
1289 }
1290 EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify);
1291 
1292 #ifdef CONFIG_OF
1293 /**
1294  * of_drm_find_bridge - find the bridge corresponding to the device node in
1295  *			the global bridge list
1296  *
1297  * @np: device node
1298  *
1299  * RETURNS:
1300  * drm_bridge control struct on success, NULL on failure
1301  */
1302 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
1303 {
1304 	struct drm_bridge *bridge;
1305 
1306 	mutex_lock(&bridge_lock);
1307 
1308 	list_for_each_entry(bridge, &bridge_list, list) {
1309 		if (bridge->of_node == np) {
1310 			mutex_unlock(&bridge_lock);
1311 			return bridge;
1312 		}
1313 	}
1314 
1315 	mutex_unlock(&bridge_lock);
1316 	return NULL;
1317 }
1318 EXPORT_SYMBOL(of_drm_find_bridge);
1319 #endif
1320 
1321 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
1322 MODULE_DESCRIPTION("DRM bridge infrastructure");
1323 MODULE_LICENSE("GPL and additional rights");
1324