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