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