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