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