]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/drm_bridge.c
37400607e9b739d302d0bb18edc0808487b4963b
[linux.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
32 #include "drm_crtc_internal.h"
33
34 /**
35  * DOC: overview
36  *
37  * &struct drm_bridge represents a device that hangs on to an encoder. These are
38  * handy when a regular &drm_encoder entity isn't enough to represent the entire
39  * encoder chain.
40  *
41  * A bridge is always attached to a single &drm_encoder at a time, but can be
42  * either connected to it directly, or through an intermediate bridge::
43  *
44  *     encoder ---> bridge B ---> bridge A
45  *
46  * Here, the output of the encoder feeds to bridge B, and that furthers feeds to
47  * bridge A.
48  *
49  * The driver using the bridge is responsible to make the associations between
50  * the encoder and bridges. Once these links are made, the bridges will
51  * participate along with encoder functions to perform mode_set/enable/disable
52  * through the ops provided in &drm_bridge_funcs.
53  *
54  * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes,
55  * CRTCs, encoders or connectors and hence are not visible to userspace. They
56  * just provide additional hooks to get the desired output at the end of the
57  * encoder chain.
58  *
59  * Bridges can also be chained up using the &drm_bridge.chain_node field.
60  *
61  * Both legacy CRTC helpers and the new atomic modeset helpers support bridges.
62  */
63
64 static DEFINE_MUTEX(bridge_lock);
65 static LIST_HEAD(bridge_list);
66
67 /**
68  * drm_bridge_add - add the given bridge to the global bridge list
69  *
70  * @bridge: bridge control structure
71  */
72 void drm_bridge_add(struct drm_bridge *bridge)
73 {
74         mutex_lock(&bridge_lock);
75         list_add_tail(&bridge->list, &bridge_list);
76         mutex_unlock(&bridge_lock);
77 }
78 EXPORT_SYMBOL(drm_bridge_add);
79
80 /**
81  * drm_bridge_remove - remove the given bridge from the global bridge list
82  *
83  * @bridge: bridge control structure
84  */
85 void drm_bridge_remove(struct drm_bridge *bridge)
86 {
87         mutex_lock(&bridge_lock);
88         list_del_init(&bridge->list);
89         mutex_unlock(&bridge_lock);
90 }
91 EXPORT_SYMBOL(drm_bridge_remove);
92
93 static struct drm_bridge_state *
94 drm_atomic_default_bridge_duplicate_state(struct drm_bridge *bridge)
95 {
96         struct drm_bridge_state *new;
97
98         if (WARN_ON(!bridge->base.state))
99                 return NULL;
100
101         new = kzalloc(sizeof(*new), GFP_KERNEL);
102         if (new)
103                 __drm_atomic_helper_bridge_duplicate_state(bridge, new);
104
105         return new;
106 }
107
108 static struct drm_private_state *
109 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)
110 {
111         struct drm_bridge *bridge = drm_priv_to_bridge(obj);
112         struct drm_bridge_state *state;
113
114         if (bridge->funcs->atomic_duplicate_state)
115                 state = bridge->funcs->atomic_duplicate_state(bridge);
116         else
117                 state = drm_atomic_default_bridge_duplicate_state(bridge);
118
119         return state ? &state->base : NULL;
120 }
121
122 static void
123 drm_atomic_default_bridge_destroy_state(struct drm_bridge *bridge,
124                                         struct drm_bridge_state *state)
125 {
126         /* Just a simple kfree() for now */
127         kfree(state);
128 }
129
130 static void
131 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,
132                                      struct drm_private_state *s)
133 {
134         struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
135         struct drm_bridge *bridge = drm_priv_to_bridge(obj);
136
137         if (bridge->funcs->atomic_destroy_state)
138                 bridge->funcs->atomic_destroy_state(bridge, state);
139         else
140                 drm_atomic_default_bridge_destroy_state(bridge, state);
141 }
142
143 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
144         .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
145         .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
146 };
147
148 static struct drm_bridge_state *
149 drm_atomic_default_bridge_reset(struct drm_bridge *bridge)
150 {
151         struct drm_bridge_state *bridge_state;
152
153         bridge_state = kzalloc(sizeof(*bridge_state), GFP_KERNEL);
154         if (!bridge_state)
155                 return ERR_PTR(-ENOMEM);
156
157         __drm_atomic_helper_bridge_reset(bridge, bridge_state);
158         return bridge_state;
159 }
160
161 /**
162  * drm_bridge_attach - attach the bridge to an encoder's chain
163  *
164  * @encoder: DRM encoder
165  * @bridge: bridge to attach
166  * @previous: previous bridge in the chain (optional)
167  *
168  * Called by a kms driver to link the bridge to an encoder's chain. The previous
169  * argument specifies the previous bridge in the chain. If NULL, the bridge is
170  * linked directly at the encoder's output. Otherwise it is linked at the
171  * previous bridge's output.
172  *
173  * If non-NULL the previous bridge must be already attached by a call to this
174  * function.
175  *
176  * Note that bridges attached to encoders are auto-detached during encoder
177  * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
178  * *not* be balanced with a drm_bridge_detach() in driver code.
179  *
180  * RETURNS:
181  * Zero on success, error code on failure
182  */
183 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
184                       struct drm_bridge *previous)
185 {
186         struct drm_bridge_state *state;
187         int ret;
188
189         if (!encoder || !bridge)
190                 return -EINVAL;
191
192         if (previous && (!previous->dev || previous->encoder != encoder))
193                 return -EINVAL;
194
195         if (bridge->dev)
196                 return -EBUSY;
197
198         bridge->dev = encoder->dev;
199         bridge->encoder = encoder;
200
201         if (previous)
202                 list_add(&bridge->chain_node, &previous->chain_node);
203         else
204                 list_add(&bridge->chain_node, &encoder->bridge_chain);
205
206         if (bridge->funcs->attach) {
207                 ret = bridge->funcs->attach(bridge);
208                 if (ret < 0)
209                         goto err_reset_bridge;
210         }
211
212         if (bridge->funcs->atomic_reset)
213                 state = bridge->funcs->atomic_reset(bridge);
214         else
215                 state = drm_atomic_default_bridge_reset(bridge);
216
217         if (IS_ERR(state)) {
218                 ret = PTR_ERR(state);
219                 goto err_detach_bridge;
220         }
221
222         drm_atomic_private_obj_init(bridge->dev, &bridge->base,
223                                     &state->base,
224                                     &drm_bridge_priv_state_funcs);
225
226         return 0;
227
228 err_detach_bridge:
229         if (bridge->funcs->detach)
230                 bridge->funcs->detach(bridge);
231
232 err_reset_bridge:
233         bridge->dev = NULL;
234         bridge->encoder = NULL;
235         list_del(&bridge->chain_node);
236         return ret;
237 }
238 EXPORT_SYMBOL(drm_bridge_attach);
239
240 void drm_bridge_detach(struct drm_bridge *bridge)
241 {
242         if (WARN_ON(!bridge))
243                 return;
244
245         if (WARN_ON(!bridge->dev))
246                 return;
247
248         drm_atomic_private_obj_fini(&bridge->base);
249
250         if (bridge->funcs->detach)
251                 bridge->funcs->detach(bridge);
252
253         list_del(&bridge->chain_node);
254         bridge->dev = NULL;
255 }
256
257 /**
258  * DOC: bridge callbacks
259  *
260  * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM
261  * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c
262  * These helpers call a specific &drm_bridge_funcs op for all the bridges
263  * during encoder configuration.
264  *
265  * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
266  */
267
268 /**
269  * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
270  *                               encoder chain
271  * @bridge: bridge control structure
272  * @mode: desired mode to be set for the bridge
273  * @adjusted_mode: updated mode that works for this bridge
274  *
275  * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
276  * encoder chain, starting from the first bridge to the last.
277  *
278  * Note: the bridge passed should be the one closest to the encoder
279  *
280  * RETURNS:
281  * true on success, false on failure
282  */
283 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
284                                  const struct drm_display_mode *mode,
285                                  struct drm_display_mode *adjusted_mode)
286 {
287         struct drm_encoder *encoder;
288
289         if (!bridge)
290                 return true;
291
292         encoder = bridge->encoder;
293         list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
294                 if (!bridge->funcs->mode_fixup)
295                         continue;
296
297                 if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
298                         return false;
299         }
300
301         return true;
302 }
303 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
304
305 /**
306  * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
307  *                               encoder chain.
308  * @bridge: bridge control structure
309  * @mode: desired mode to be validated
310  *
311  * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
312  * chain, starting from the first bridge to the last. If at least one bridge
313  * does not accept the mode the function returns the error code.
314  *
315  * Note: the bridge passed should be the one closest to the encoder.
316  *
317  * RETURNS:
318  * MODE_OK on success, drm_mode_status Enum error code on failure
319  */
320 enum drm_mode_status
321 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
322                             const struct drm_display_mode *mode)
323 {
324         struct drm_encoder *encoder;
325
326         if (!bridge)
327                 return MODE_OK;
328
329         encoder = bridge->encoder;
330         list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
331                 enum drm_mode_status ret;
332
333                 if (!bridge->funcs->mode_valid)
334                         continue;
335
336                 ret = bridge->funcs->mode_valid(bridge, mode);
337                 if (ret != MODE_OK)
338                         return ret;
339         }
340
341         return MODE_OK;
342 }
343 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
344
345 /**
346  * drm_bridge_chain_disable - disables all bridges in the encoder chain
347  * @bridge: bridge control structure
348  *
349  * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
350  * chain, starting from the last bridge to the first. These are called before
351  * calling the encoder's prepare op.
352  *
353  * Note: the bridge passed should be the one closest to the encoder
354  */
355 void drm_bridge_chain_disable(struct drm_bridge *bridge)
356 {
357         struct drm_encoder *encoder;
358         struct drm_bridge *iter;
359
360         if (!bridge)
361                 return;
362
363         encoder = bridge->encoder;
364         list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
365                 if (iter->funcs->disable)
366                         iter->funcs->disable(iter);
367
368                 if (iter == bridge)
369                         break;
370         }
371 }
372 EXPORT_SYMBOL(drm_bridge_chain_disable);
373
374 /**
375  * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the
376  *                                 encoder chain
377  * @bridge: bridge control structure
378  *
379  * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
380  * encoder chain, starting from the first bridge to the last. These are called
381  * after completing the encoder's prepare op.
382  *
383  * Note: the bridge passed should be the one closest to the encoder
384  */
385 void drm_bridge_chain_post_disable(struct drm_bridge *bridge)
386 {
387         struct drm_encoder *encoder;
388
389         if (!bridge)
390                 return;
391
392         encoder = bridge->encoder;
393         list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
394                 if (bridge->funcs->post_disable)
395                         bridge->funcs->post_disable(bridge);
396         }
397 }
398 EXPORT_SYMBOL(drm_bridge_chain_post_disable);
399
400 /**
401  * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
402  *                             encoder chain
403  * @bridge: bridge control structure
404  * @mode: desired mode to be set for the encoder chain
405  * @adjusted_mode: updated mode that works for this encoder chain
406  *
407  * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
408  * encoder chain, starting from the first bridge to the last.
409  *
410  * Note: the bridge passed should be the one closest to the encoder
411  */
412 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
413                                const struct drm_display_mode *mode,
414                                const struct drm_display_mode *adjusted_mode)
415 {
416         struct drm_encoder *encoder;
417
418         if (!bridge)
419                 return;
420
421         encoder = bridge->encoder;
422         list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
423                 if (bridge->funcs->mode_set)
424                         bridge->funcs->mode_set(bridge, mode, adjusted_mode);
425         }
426 }
427 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
428
429 /**
430  * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the
431  *                               encoder chain
432  * @bridge: bridge control structure
433  *
434  * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
435  * chain, starting from the last bridge to the first. These are called
436  * before calling the encoder's commit op.
437  *
438  * Note: the bridge passed should be the one closest to the encoder
439  */
440 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge)
441 {
442         struct drm_encoder *encoder;
443         struct drm_bridge *iter;
444
445         if (!bridge)
446                 return;
447
448         encoder = bridge->encoder;
449         list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
450                 if (iter->funcs->pre_enable)
451                         iter->funcs->pre_enable(iter);
452         }
453 }
454 EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
455
456 /**
457  * drm_bridge_chain_enable - enables all bridges in the encoder chain
458  * @bridge: bridge control structure
459  *
460  * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
461  * chain, starting from the first bridge to the last. These are called
462  * after completing the encoder's commit op.
463  *
464  * Note that the bridge passed should be the one closest to the encoder
465  */
466 void drm_bridge_chain_enable(struct drm_bridge *bridge)
467 {
468         struct drm_encoder *encoder;
469
470         if (!bridge)
471                 return;
472
473         encoder = bridge->encoder;
474         list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
475                 if (bridge->funcs->enable)
476                         bridge->funcs->enable(bridge);
477         }
478 }
479 EXPORT_SYMBOL(drm_bridge_chain_enable);
480
481 /**
482  * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
483  * @bridge: bridge control structure
484  * @old_state: old atomic state
485  *
486  * Calls &drm_bridge_funcs.atomic_disable (falls back on
487  * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
488  * starting from the last bridge to the first. These are called before calling
489  * &drm_encoder_helper_funcs.atomic_disable
490  *
491  * Note: the bridge passed should be the one closest to the encoder
492  */
493 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
494                                      struct drm_atomic_state *old_state)
495 {
496         struct drm_encoder *encoder;
497         struct drm_bridge *iter;
498
499         if (!bridge)
500                 return;
501
502         encoder = bridge->encoder;
503         list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
504                 if (iter->funcs->atomic_disable) {
505                         struct drm_bridge_state *old_bridge_state;
506
507                         old_bridge_state =
508                                 drm_atomic_get_old_bridge_state(old_state,
509                                                                 iter);
510                         if (WARN_ON(!old_bridge_state))
511                                 return;
512
513                         iter->funcs->atomic_disable(iter, old_bridge_state);
514                 } else if (iter->funcs->disable) {
515                         iter->funcs->disable(iter);
516                 }
517
518                 if (iter == bridge)
519                         break;
520         }
521 }
522 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
523
524 /**
525  * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
526  *                                        in the encoder chain
527  * @bridge: bridge control structure
528  * @old_state: old atomic state
529  *
530  * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
531  * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
532  * starting from the first bridge to the last. These are called after completing
533  * &drm_encoder_helper_funcs.atomic_disable
534  *
535  * Note: the bridge passed should be the one closest to the encoder
536  */
537 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
538                                           struct drm_atomic_state *old_state)
539 {
540         struct drm_encoder *encoder;
541
542         if (!bridge)
543                 return;
544
545         encoder = bridge->encoder;
546         list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
547                 if (bridge->funcs->atomic_post_disable) {
548                         struct drm_bridge_state *old_bridge_state;
549
550                         old_bridge_state =
551                                 drm_atomic_get_old_bridge_state(old_state,
552                                                                 bridge);
553                         if (WARN_ON(!old_bridge_state))
554                                 return;
555
556                         bridge->funcs->atomic_post_disable(bridge,
557                                                            old_bridge_state);
558                 } else if (bridge->funcs->post_disable) {
559                         bridge->funcs->post_disable(bridge);
560                 }
561         }
562 }
563 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
564
565 /**
566  * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
567  *                                      the encoder chain
568  * @bridge: bridge control structure
569  * @old_state: old atomic state
570  *
571  * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
572  * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
573  * starting from the last bridge to the first. These are called before calling
574  * &drm_encoder_helper_funcs.atomic_enable
575  *
576  * Note: the bridge passed should be the one closest to the encoder
577  */
578 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
579                                         struct drm_atomic_state *old_state)
580 {
581         struct drm_encoder *encoder;
582         struct drm_bridge *iter;
583
584         if (!bridge)
585                 return;
586
587         encoder = bridge->encoder;
588         list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
589                 if (iter->funcs->atomic_pre_enable) {
590                         struct drm_bridge_state *old_bridge_state;
591
592                         old_bridge_state =
593                                 drm_atomic_get_old_bridge_state(old_state,
594                                                                 iter);
595                         if (WARN_ON(!old_bridge_state))
596                                 return;
597
598                         iter->funcs->atomic_pre_enable(iter, old_bridge_state);
599                 } else if (iter->funcs->pre_enable) {
600                         iter->funcs->pre_enable(iter);
601                 }
602
603                 if (iter == bridge)
604                         break;
605         }
606 }
607 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
608
609 /**
610  * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
611  * @bridge: bridge control structure
612  * @old_state: old atomic state
613  *
614  * Calls &drm_bridge_funcs.atomic_enable (falls back on
615  * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
616  * starting from the first bridge to the last. These are called after completing
617  * &drm_encoder_helper_funcs.atomic_enable
618  *
619  * Note: the bridge passed should be the one closest to the encoder
620  */
621 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
622                                     struct drm_atomic_state *old_state)
623 {
624         struct drm_encoder *encoder;
625
626         if (!bridge)
627                 return;
628
629         encoder = bridge->encoder;
630         list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
631                 if (bridge->funcs->atomic_enable) {
632                         struct drm_bridge_state *old_bridge_state;
633
634                         old_bridge_state =
635                                 drm_atomic_get_old_bridge_state(old_state,
636                                                                 bridge);
637                         if (WARN_ON(!old_bridge_state))
638                                 return;
639
640                         bridge->funcs->atomic_enable(bridge, old_bridge_state);
641                 } else if (bridge->funcs->enable) {
642                         bridge->funcs->enable(bridge);
643                 }
644         }
645 }
646 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
647
648 static int drm_atomic_bridge_check(struct drm_bridge *bridge,
649                                    struct drm_crtc_state *crtc_state,
650                                    struct drm_connector_state *conn_state)
651 {
652         if (bridge->funcs->atomic_check) {
653                 struct drm_bridge_state *bridge_state;
654                 int ret;
655
656                 bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
657                                                                bridge);
658                 if (WARN_ON(!bridge_state))
659                         return -EINVAL;
660
661                 ret = bridge->funcs->atomic_check(bridge, bridge_state,
662                                                   crtc_state, conn_state);
663                 if (ret)
664                         return ret;
665         } else if (bridge->funcs->mode_fixup) {
666                 if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode,
667                                                &crtc_state->adjusted_mode))
668                         return -EINVAL;
669         }
670
671         return 0;
672 }
673
674 /**
675  * drm_atomic_helper_bridge_propagate_bus_fmt() - Propagate output format to
676  *                                                the input end of a bridge
677  * @bridge: bridge control structure
678  * @bridge_state: new bridge state
679  * @crtc_state: new CRTC state
680  * @conn_state: new connector state
681  * @output_fmt: tested output bus format
682  * @num_input_fmts: will contain the size of the returned array
683  *
684  * This helper is a pluggable implementation of the
685  * &drm_bridge_funcs.atomic_get_input_bus_fmts operation for bridges that don't
686  * modify the bus configuration between their input and their output. It
687  * returns an array of input formats with a single element set to @output_fmt.
688  *
689  * RETURNS:
690  * a valid format array of size @num_input_fmts, or NULL if the allocation
691  * failed
692  */
693 u32 *
694 drm_atomic_helper_bridge_propagate_bus_fmt(struct drm_bridge *bridge,
695                                         struct drm_bridge_state *bridge_state,
696                                         struct drm_crtc_state *crtc_state,
697                                         struct drm_connector_state *conn_state,
698                                         u32 output_fmt,
699                                         unsigned int *num_input_fmts)
700 {
701         u32 *input_fmts;
702
703         input_fmts = kzalloc(sizeof(*input_fmts), GFP_KERNEL);
704         if (!input_fmts) {
705                 *num_input_fmts = 0;
706                 return NULL;
707         }
708
709         *num_input_fmts = 1;
710         input_fmts[0] = output_fmt;
711         return input_fmts;
712 }
713 EXPORT_SYMBOL(drm_atomic_helper_bridge_propagate_bus_fmt);
714
715 static int select_bus_fmt_recursive(struct drm_bridge *first_bridge,
716                                     struct drm_bridge *cur_bridge,
717                                     struct drm_crtc_state *crtc_state,
718                                     struct drm_connector_state *conn_state,
719                                     u32 out_bus_fmt)
720 {
721         struct drm_bridge_state *cur_state;
722         unsigned int num_in_bus_fmts, i;
723         struct drm_bridge *prev_bridge;
724         u32 *in_bus_fmts;
725         int ret;
726
727         prev_bridge = drm_bridge_get_prev_bridge(cur_bridge);
728         cur_state = drm_atomic_get_new_bridge_state(crtc_state->state,
729                                                     cur_bridge);
730         if (WARN_ON(!cur_state))
731                 return -EINVAL;
732
733         /*
734          * If bus format negotiation is not supported by this bridge, let's
735          * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and
736          * hope that it can handle this situation gracefully (by providing
737          * appropriate default values).
738          */
739         if (!cur_bridge->funcs->atomic_get_input_bus_fmts) {
740                 if (cur_bridge != first_bridge) {
741                         ret = select_bus_fmt_recursive(first_bridge,
742                                                        prev_bridge, crtc_state,
743                                                        conn_state,
744                                                        MEDIA_BUS_FMT_FIXED);
745                         if (ret)
746                                 return ret;
747                 }
748
749                 cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
750                 cur_state->output_bus_cfg.format = out_bus_fmt;
751                 return 0;
752         }
753
754         in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge,
755                                                         cur_state,
756                                                         crtc_state,
757                                                         conn_state,
758                                                         out_bus_fmt,
759                                                         &num_in_bus_fmts);
760         if (!num_in_bus_fmts)
761                 return -ENOTSUPP;
762         else if (!in_bus_fmts)
763                 return -ENOMEM;
764
765         if (first_bridge == cur_bridge) {
766                 cur_state->input_bus_cfg.format = in_bus_fmts[0];
767                 cur_state->output_bus_cfg.format = out_bus_fmt;
768                 kfree(in_bus_fmts);
769                 return 0;
770         }
771
772         for (i = 0; i < num_in_bus_fmts; i++) {
773                 ret = select_bus_fmt_recursive(first_bridge, prev_bridge,
774                                                crtc_state, conn_state,
775                                                in_bus_fmts[i]);
776                 if (ret != -ENOTSUPP)
777                         break;
778         }
779
780         if (!ret) {
781                 cur_state->input_bus_cfg.format = in_bus_fmts[i];
782                 cur_state->output_bus_cfg.format = out_bus_fmt;
783         }
784
785         kfree(in_bus_fmts);
786         return ret;
787 }
788
789 /*
790  * This function is called by &drm_atomic_bridge_chain_check() just before
791  * calling &drm_bridge_funcs.atomic_check() on all elements of the chain.
792  * It performs bus format negotiation between bridge elements. The negotiation
793  * happens in reverse order, starting from the last element in the chain up to
794  * @bridge.
795  *
796  * Negotiation starts by retrieving supported output bus formats on the last
797  * bridge element and testing them one by one. The test is recursive, meaning
798  * that for each tested output format, the whole chain will be walked backward,
799  * and each element will have to choose an input bus format that can be
800  * transcoded to the requested output format. When a bridge element does not
801  * support transcoding into a specific output format -ENOTSUPP is returned and
802  * the next bridge element will have to try a different format. If none of the
803  * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail.
804  *
805  * This implementation is relying on
806  * &drm_bridge_funcs.atomic_get_output_bus_fmts() and
807  * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported
808  * input/output formats.
809  *
810  * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by
811  * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts()
812  * tries a single format: &drm_connector.display_info.bus_formats[0] if
813  * available, MEDIA_BUS_FMT_FIXED otherwise.
814  *
815  * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented,
816  * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the
817  * bridge element that lacks this hook and asks the previous element in the
818  * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what
819  * to do in that case (fail if they want to enforce bus format negotiation, or
820  * provide a reasonable default if they need to support pipelines where not
821  * all elements support bus format negotiation).
822  */
823 static int
824 drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge,
825                                         struct drm_crtc_state *crtc_state,
826                                         struct drm_connector_state *conn_state)
827 {
828         struct drm_connector *conn = conn_state->connector;
829         struct drm_encoder *encoder = bridge->encoder;
830         struct drm_bridge_state *last_bridge_state;
831         unsigned int i, num_out_bus_fmts;
832         struct drm_bridge *last_bridge;
833         u32 *out_bus_fmts;
834         int ret = 0;
835
836         last_bridge = list_last_entry(&encoder->bridge_chain,
837                                       struct drm_bridge, chain_node);
838         last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
839                                                             last_bridge);
840         if (WARN_ON(!last_bridge_state))
841                 return -EINVAL;
842
843         if (last_bridge->funcs->atomic_get_output_bus_fmts) {
844                 const struct drm_bridge_funcs *funcs = last_bridge->funcs;
845
846                 out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge,
847                                                         last_bridge_state,
848                                                         crtc_state,
849                                                         conn_state,
850                                                         &num_out_bus_fmts);
851                 if (!num_out_bus_fmts)
852                         return -ENOTSUPP;
853                 else if (!out_bus_fmts)
854                         return -ENOMEM;
855         } else {
856                 num_out_bus_fmts = 1;
857                 out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL);
858                 if (!out_bus_fmts)
859                         return -ENOMEM;
860
861                 if (conn->display_info.num_bus_formats &&
862                     conn->display_info.bus_formats)
863                         out_bus_fmts[0] = conn->display_info.bus_formats[0];
864                 else
865                         out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
866         }
867
868         for (i = 0; i < num_out_bus_fmts; i++) {
869                 ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state,
870                                                conn_state, out_bus_fmts[i]);
871                 if (ret != -ENOTSUPP)
872                         break;
873         }
874
875         kfree(out_bus_fmts);
876
877         return ret;
878 }
879
880 static void
881 drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge,
882                                       struct drm_connector *conn,
883                                       struct drm_atomic_state *state)
884 {
885         struct drm_bridge_state *bridge_state, *next_bridge_state;
886         struct drm_bridge *next_bridge;
887         u32 output_flags;
888
889         bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
890         next_bridge = drm_bridge_get_next_bridge(bridge);
891
892         /*
893          * Let's try to apply the most common case here, that is, propagate
894          * display_info flags for the last bridge, and propagate the input
895          * flags of the next bridge element to the output end of the current
896          * bridge when the bridge is not the last one.
897          * There are exceptions to this rule, like when signal inversion is
898          * happening at the board level, but that's something drivers can deal
899          * with from their &drm_bridge_funcs.atomic_check() implementation by
900          * simply overriding the flags value we've set here.
901          */
902         if (!next_bridge) {
903                 output_flags = conn->display_info.bus_flags;
904         } else {
905                 next_bridge_state = drm_atomic_get_new_bridge_state(state,
906                                                                 next_bridge);
907                 output_flags = next_bridge_state->input_bus_cfg.flags;
908         }
909
910         bridge_state->output_bus_cfg.flags = output_flags;
911
912         /*
913          * Propage the output flags to the input end of the bridge. Again, it's
914          * not necessarily what all bridges want, but that's what most of them
915          * do, and by doing that by default we avoid forcing drivers to
916          * duplicate the "dummy propagation" logic.
917          */
918         bridge_state->input_bus_cfg.flags = output_flags;
919 }
920
921 /**
922  * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain
923  * @bridge: bridge control structure
924  * @crtc_state: new CRTC state
925  * @conn_state: new connector state
926  *
927  * First trigger a bus format negotiation before calling
928  * &drm_bridge_funcs.atomic_check() (falls back on
929  * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain,
930  * starting from the last bridge to the first. These are called before calling
931  * &drm_encoder_helper_funcs.atomic_check()
932  *
933  * RETURNS:
934  * 0 on success, a negative error code on failure
935  */
936 int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,
937                                   struct drm_crtc_state *crtc_state,
938                                   struct drm_connector_state *conn_state)
939 {
940         struct drm_connector *conn = conn_state->connector;
941         struct drm_encoder *encoder = bridge->encoder;
942         struct drm_bridge *iter;
943         int ret;
944
945         ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state,
946                                                       conn_state);
947         if (ret)
948                 return ret;
949
950         list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
951                 int ret;
952
953                 /*
954                  * Bus flags are propagated by default. If a bridge needs to
955                  * tweak the input bus flags for any reason, it should happen
956                  * in its &drm_bridge_funcs.atomic_check() implementation such
957                  * that preceding bridges in the chain can propagate the new
958                  * bus flags.
959                  */
960                 drm_atomic_bridge_propagate_bus_flags(iter, conn,
961                                                       crtc_state->state);
962
963                 ret = drm_atomic_bridge_check(iter, crtc_state, conn_state);
964                 if (ret)
965                         return ret;
966
967                 if (iter == bridge)
968                         break;
969         }
970
971         return 0;
972 }
973 EXPORT_SYMBOL(drm_atomic_bridge_chain_check);
974
975 /**
976  * __drm_atomic_helper_bridge_reset() - Initialize a bridge state to its
977  *                                      default
978  * @bridge: the bridge this state is refers to
979  * @state: bridge state to initialize
980  *
981  * Initialize the bridge state to default values. This is meant to be* called
982  * by the bridge &drm_plane_funcs.reset hook for bridges that subclass the
983  * bridge state.
984  */
985 void __drm_atomic_helper_bridge_reset(struct drm_bridge *bridge,
986                                       struct drm_bridge_state *state)
987 {
988         memset(state, 0, sizeof(*state));
989         state->bridge = bridge;
990 }
991 EXPORT_SYMBOL(__drm_atomic_helper_bridge_reset);
992
993 /**
994  * __drm_atomic_helper_bridge_duplicate_state() - Copy atomic bridge state
995  * @bridge: bridge object
996  * @state: atomic bridge state
997  *
998  * Copies atomic state from a bridge's current state and resets inferred values.
999  * This is useful for drivers that subclass the bridge state.
1000  */
1001 void __drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge,
1002                                                 struct drm_bridge_state *state)
1003 {
1004         __drm_atomic_helper_private_obj_duplicate_state(&bridge->base,
1005                                                         &state->base);
1006         state->bridge = bridge;
1007 }
1008 EXPORT_SYMBOL(__drm_atomic_helper_bridge_duplicate_state);
1009
1010 #ifdef CONFIG_OF
1011 /**
1012  * of_drm_find_bridge - find the bridge corresponding to the device node in
1013  *                      the global bridge list
1014  *
1015  * @np: device node
1016  *
1017  * RETURNS:
1018  * drm_bridge control struct on success, NULL on failure
1019  */
1020 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
1021 {
1022         struct drm_bridge *bridge;
1023
1024         mutex_lock(&bridge_lock);
1025
1026         list_for_each_entry(bridge, &bridge_list, list) {
1027                 if (bridge->of_node == np) {
1028                         mutex_unlock(&bridge_lock);
1029                         return bridge;
1030                 }
1031         }
1032
1033         mutex_unlock(&bridge_lock);
1034         return NULL;
1035 }
1036 EXPORT_SYMBOL(of_drm_find_bridge);
1037 #endif
1038
1039 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
1040 MODULE_DESCRIPTION("DRM bridge infrastructure");
1041 MODULE_LICENSE("GPL and additional rights");