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