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