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