8883dcfe63cb98f4eb049205b412b6b3f80477c8
[linux-2.6-block.git] / include / drm / drm_bridge.h
1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #ifndef __DRM_BRIDGE_H__
24 #define __DRM_BRIDGE_H__
25
26 #include <linux/list.h>
27 #include <linux/ctype.h>
28
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_encoder.h>
31 #include <drm/drm_mode_object.h>
32 #include <drm/drm_modes.h>
33
34 struct drm_bridge;
35 struct drm_bridge_timings;
36 struct drm_panel;
37
38 /**
39  * struct drm_bridge_funcs - drm_bridge control functions
40  */
41 struct drm_bridge_funcs {
42         /**
43          * @attach:
44          *
45          * This callback is invoked whenever our bridge is being attached to a
46          * &drm_encoder.
47          *
48          * The @attach callback is optional.
49          *
50          * RETURNS:
51          *
52          * Zero on success, error code on failure.
53          */
54         int (*attach)(struct drm_bridge *bridge);
55
56         /**
57          * @detach:
58          *
59          * This callback is invoked whenever our bridge is being detached from a
60          * &drm_encoder.
61          *
62          * The @detach callback is optional.
63          */
64         void (*detach)(struct drm_bridge *bridge);
65
66         /**
67          * @mode_valid:
68          *
69          * This callback is used to check if a specific mode is valid in this
70          * bridge. This should be implemented if the bridge has some sort of
71          * restriction in the modes it can display. For example, a given bridge
72          * may be responsible to set a clock value. If the clock can not
73          * produce all the values for the available modes then this callback
74          * can be used to restrict the number of modes to only the ones that
75          * can be displayed.
76          *
77          * This hook is used by the probe helpers to filter the mode list in
78          * drm_helper_probe_single_connector_modes(), and it is used by the
79          * atomic helpers to validate modes supplied by userspace in
80          * drm_atomic_helper_check_modeset().
81          *
82          * The @mode_valid callback is optional.
83          *
84          * NOTE:
85          *
86          * Since this function is both called from the check phase of an atomic
87          * commit, and the mode validation in the probe paths it is not allowed
88          * to look at anything else but the passed-in mode, and validate it
89          * against configuration-invariant hardward constraints. Any further
90          * limits which depend upon the configuration can only be checked in
91          * @mode_fixup.
92          *
93          * RETURNS:
94          *
95          * drm_mode_status Enum
96          */
97         enum drm_mode_status (*mode_valid)(struct drm_bridge *bridge,
98                                            const struct drm_display_mode *mode);
99
100         /**
101          * @mode_fixup:
102          *
103          * This callback is used to validate and adjust a mode. The parameter
104          * mode is the display mode that should be fed to the next element in
105          * the display chain, either the final &drm_connector or the next
106          * &drm_bridge. The parameter adjusted_mode is the input mode the bridge
107          * requires. It can be modified by this callback and does not need to
108          * match mode. See also &drm_crtc_state.adjusted_mode for more details.
109          *
110          * This is the only hook that allows a bridge to reject a modeset. If
111          * this function passes all other callbacks must succeed for this
112          * configuration.
113          *
114          * The @mode_fixup callback is optional.
115          *
116          * NOTE:
117          *
118          * This function is called in the check phase of atomic modesets, which
119          * can be aborted for any reason (including on userspace's request to
120          * just check whether a configuration would be possible). Drivers MUST
121          * NOT touch any persistent state (hardware or software) or data
122          * structures except the passed in @state parameter.
123          *
124          * Also beware that userspace can request its own custom modes, neither
125          * core nor helpers filter modes to the list of probe modes reported by
126          * the GETCONNECTOR IOCTL and stored in &drm_connector.modes. To ensure
127          * that modes are filtered consistently put any bridge constraints and
128          * limits checks into @mode_valid.
129          *
130          * RETURNS:
131          *
132          * True if an acceptable configuration is possible, false if the modeset
133          * operation should be rejected.
134          */
135         bool (*mode_fixup)(struct drm_bridge *bridge,
136                            const struct drm_display_mode *mode,
137                            struct drm_display_mode *adjusted_mode);
138         /**
139          * @disable:
140          *
141          * This callback should disable the bridge. It is called right before
142          * the preceding element in the display pipe is disabled. If the
143          * preceding element is a bridge this means it's called before that
144          * bridge's @disable vfunc. If the preceding element is a &drm_encoder
145          * it's called right before the &drm_encoder_helper_funcs.disable,
146          * &drm_encoder_helper_funcs.prepare or &drm_encoder_helper_funcs.dpms
147          * hook.
148          *
149          * The bridge can assume that the display pipe (i.e. clocks and timing
150          * signals) feeding it is still running when this callback is called.
151          *
152          * The @disable callback is optional.
153          */
154         void (*disable)(struct drm_bridge *bridge);
155
156         /**
157          * @post_disable:
158          *
159          * This callback should disable the bridge. It is called right after the
160          * preceding element in the display pipe is disabled. If the preceding
161          * element is a bridge this means it's called after that bridge's
162          * @post_disable function. If the preceding element is a &drm_encoder
163          * it's called right after the encoder's
164          * &drm_encoder_helper_funcs.disable, &drm_encoder_helper_funcs.prepare
165          * or &drm_encoder_helper_funcs.dpms hook.
166          *
167          * The bridge must assume that the display pipe (i.e. clocks and timing
168          * singals) feeding it is no longer running when this callback is
169          * called.
170          *
171          * The @post_disable callback is optional.
172          */
173         void (*post_disable)(struct drm_bridge *bridge);
174
175         /**
176          * @mode_set:
177          *
178          * This callback should set the given mode on the bridge. It is called
179          * after the @mode_set callback for the preceding element in the display
180          * pipeline has been called already. If the bridge is the first element
181          * then this would be &drm_encoder_helper_funcs.mode_set. The display
182          * pipe (i.e.  clocks and timing signals) is off when this function is
183          * called.
184          *
185          * The adjusted_mode parameter is the mode output by the CRTC for the
186          * first bridge in the chain. It can be different from the mode
187          * parameter that contains the desired mode for the connector at the end
188          * of the bridges chain, for instance when the first bridge in the chain
189          * performs scaling. The adjusted mode is mostly useful for the first
190          * bridge in the chain and is likely irrelevant for the other bridges.
191          *
192          * For atomic drivers the adjusted_mode is the mode stored in
193          * &drm_crtc_state.adjusted_mode.
194          *
195          * NOTE:
196          *
197          * If a need arises to store and access modes adjusted for other
198          * locations than the connection between the CRTC and the first bridge,
199          * the DRM framework will have to be extended with DRM bridge states.
200          */
201         void (*mode_set)(struct drm_bridge *bridge,
202                          const struct drm_display_mode *mode,
203                          const struct drm_display_mode *adjusted_mode);
204         /**
205          * @pre_enable:
206          *
207          * This callback should enable the bridge. It is called right before
208          * the preceding element in the display pipe is enabled. If the
209          * preceding element is a bridge this means it's called before that
210          * bridge's @pre_enable function. If the preceding element is a
211          * &drm_encoder it's called right before the encoder's
212          * &drm_encoder_helper_funcs.enable, &drm_encoder_helper_funcs.commit or
213          * &drm_encoder_helper_funcs.dpms hook.
214          *
215          * The display pipe (i.e. clocks and timing signals) feeding this bridge
216          * will not yet be running when this callback is called. The bridge must
217          * not enable the display link feeding the next bridge in the chain (if
218          * there is one) when this callback is called.
219          *
220          * The @pre_enable callback is optional.
221          */
222         void (*pre_enable)(struct drm_bridge *bridge);
223
224         /**
225          * @enable:
226          *
227          * This callback should enable the bridge. It is called right after
228          * the preceding element in the display pipe is enabled. If the
229          * preceding element is a bridge this means it's called after that
230          * bridge's @enable function. If the preceding element is a
231          * &drm_encoder it's called right after the encoder's
232          * &drm_encoder_helper_funcs.enable, &drm_encoder_helper_funcs.commit or
233          * &drm_encoder_helper_funcs.dpms hook.
234          *
235          * The bridge can assume that the display pipe (i.e. clocks and timing
236          * signals) feeding it is running when this callback is called. This
237          * callback must enable the display link feeding the next bridge in the
238          * chain if there is one.
239          *
240          * The @enable callback is optional.
241          */
242         void (*enable)(struct drm_bridge *bridge);
243
244         /**
245          * @atomic_pre_enable:
246          *
247          * This callback should enable the bridge. It is called right before
248          * the preceding element in the display pipe is enabled. If the
249          * preceding element is a bridge this means it's called before that
250          * bridge's @atomic_pre_enable or @pre_enable function. If the preceding
251          * element is a &drm_encoder it's called right before the encoder's
252          * &drm_encoder_helper_funcs.atomic_enable hook.
253          *
254          * The display pipe (i.e. clocks and timing signals) feeding this bridge
255          * will not yet be running when this callback is called. The bridge must
256          * not enable the display link feeding the next bridge in the chain (if
257          * there is one) when this callback is called.
258          *
259          * Note that this function will only be invoked in the context of an
260          * atomic commit. It will not be invoked from
261          * &drm_bridge_chain_pre_enable. It would be prudent to also provide an
262          * implementation of @pre_enable if you are expecting driver calls into
263          * &drm_bridge_chain_pre_enable.
264          *
265          * The @atomic_pre_enable callback is optional.
266          */
267         void (*atomic_pre_enable)(struct drm_bridge *bridge,
268                                   struct drm_atomic_state *old_state);
269
270         /**
271          * @atomic_enable:
272          *
273          * This callback should enable the bridge. It is called right after
274          * the preceding element in the display pipe is enabled. If the
275          * preceding element is a bridge this means it's called after that
276          * bridge's @atomic_enable or @enable function. If the preceding element
277          * is a &drm_encoder it's called right after the encoder's
278          * &drm_encoder_helper_funcs.atomic_enable hook.
279          *
280          * The bridge can assume that the display pipe (i.e. clocks and timing
281          * signals) feeding it is running when this callback is called. This
282          * callback must enable the display link feeding the next bridge in the
283          * chain if there is one.
284          *
285          * Note that this function will only be invoked in the context of an
286          * atomic commit. It will not be invoked from &drm_bridge_chain_enable.
287          * It would be prudent to also provide an implementation of @enable if
288          * you are expecting driver calls into &drm_bridge_chain_enable.
289          *
290          * The @atomic_enable callback is optional.
291          */
292         void (*atomic_enable)(struct drm_bridge *bridge,
293                               struct drm_atomic_state *old_state);
294         /**
295          * @atomic_disable:
296          *
297          * This callback should disable the bridge. It is called right before
298          * the preceding element in the display pipe is disabled. If the
299          * preceding element is a bridge this means it's called before that
300          * bridge's @atomic_disable or @disable vfunc. If the preceding element
301          * is a &drm_encoder it's called right before the
302          * &drm_encoder_helper_funcs.atomic_disable hook.
303          *
304          * The bridge can assume that the display pipe (i.e. clocks and timing
305          * signals) feeding it is still running when this callback is called.
306          *
307          * Note that this function will only be invoked in the context of an
308          * atomic commit. It will not be invoked from
309          * &drm_bridge_chain_disable. It would be prudent to also provide an
310          * implementation of @disable if you are expecting driver calls into
311          * &drm_bridge_chain_disable.
312          *
313          * The @atomic_disable callback is optional.
314          */
315         void (*atomic_disable)(struct drm_bridge *bridge,
316                                struct drm_atomic_state *old_state);
317
318         /**
319          * @atomic_post_disable:
320          *
321          * This callback should disable the bridge. It is called right after the
322          * preceding element in the display pipe is disabled. If the preceding
323          * element is a bridge this means it's called after that bridge's
324          * @atomic_post_disable or @post_disable function. If the preceding
325          * element is a &drm_encoder it's called right after the encoder's
326          * &drm_encoder_helper_funcs.atomic_disable hook.
327          *
328          * The bridge must assume that the display pipe (i.e. clocks and timing
329          * signals) feeding it is no longer running when this callback is
330          * called.
331          *
332          * Note that this function will only be invoked in the context of an
333          * atomic commit. It will not be invoked from
334          * &drm_bridge_chain_post_disable.
335          * It would be prudent to also provide an implementation of
336          * @post_disable if you are expecting driver calls into
337          * &drm_bridge_chain_post_disable.
338          *
339          * The @atomic_post_disable callback is optional.
340          */
341         void (*atomic_post_disable)(struct drm_bridge *bridge,
342                                     struct drm_atomic_state *old_state);
343
344         /**
345          * @atomic_duplicate_state:
346          *
347          * Duplicate the current bridge state object (which is guaranteed to be
348          * non-NULL).
349          *
350          * The atomic_duplicate_state() is optional. When not implemented the
351          * core allocates a drm_bridge_state object and calls
352          * &__drm_atomic_helper_bridge_duplicate_state() to initialize it.
353          *
354          * RETURNS:
355          * A valid drm_bridge_state object or NULL if the allocation fails.
356          */
357         struct drm_bridge_state *(*atomic_duplicate_state)(struct drm_bridge *bridge);
358
359         /**
360          * @atomic_destroy_state:
361          *
362          * Destroy a bridge state object previously allocated by
363          * &drm_bridge_funcs.atomic_duplicate_state().
364          *
365          * The atomic_destroy_state hook is optional. When not implemented the
366          * core calls kfree() on the state.
367          */
368         void (*atomic_destroy_state)(struct drm_bridge *bridge,
369                                      struct drm_bridge_state *state);
370
371         /**
372          * @atomic_reset:
373          *
374          * Reset the bridge to a predefined state (or retrieve its current
375          * state) and return a &drm_bridge_state object matching this state.
376          * This function is called at attach time.
377          *
378          * The atomic_reset hook is mandatory if the bridge implements any of
379          * the atomic hooks, and should be left unassigned otherwise.
380          *
381          * Note that the atomic_reset() semantics is not exactly matching the
382          * reset() semantics found on other components (connector, plane, ...).
383          * 1/ The reset operation happens when the bridge is attached, not when
384          *    drm_mode_config_reset() is called
385          * 2/ It's meant to be used exclusively on bridges that have been
386          *    converted to the ATOMIC API
387          *
388          * RETURNS:
389          * A valid drm_bridge_state object in case of success, an ERR_PTR()
390          * giving the reason of the failure otherwise.
391          */
392         struct drm_bridge_state *(*atomic_reset)(struct drm_bridge *bridge);
393 };
394
395 /**
396  * struct drm_bridge_timings - timing information for the bridge
397  */
398 struct drm_bridge_timings {
399         /**
400          * @input_bus_flags:
401          *
402          * Tells what additional settings for the pixel data on the bus
403          * this bridge requires (like pixel signal polarity). See also
404          * &drm_display_info->bus_flags.
405          */
406         u32 input_bus_flags;
407         /**
408          * @setup_time_ps:
409          *
410          * Defines the time in picoseconds the input data lines must be
411          * stable before the clock edge.
412          */
413         u32 setup_time_ps;
414         /**
415          * @hold_time_ps:
416          *
417          * Defines the time in picoseconds taken for the bridge to sample the
418          * input signal after the clock edge.
419          */
420         u32 hold_time_ps;
421         /**
422          * @dual_link:
423          *
424          * True if the bus operates in dual-link mode. The exact meaning is
425          * dependent on the bus type. For LVDS buses, this indicates that even-
426          * and odd-numbered pixels are received on separate links.
427          */
428         bool dual_link;
429 };
430
431 /**
432  * struct drm_bridge - central DRM bridge control structure
433  */
434 struct drm_bridge {
435         /** @base: inherit from &drm_private_object */
436         struct drm_private_obj base;
437         /** @dev: DRM device this bridge belongs to */
438         struct drm_device *dev;
439         /** @encoder: encoder to which this bridge is connected */
440         struct drm_encoder *encoder;
441         /** @chain_node: used to form a bridge chain */
442         struct list_head chain_node;
443 #ifdef CONFIG_OF
444         /** @of_node: device node pointer to the bridge */
445         struct device_node *of_node;
446 #endif
447         /** @list: to keep track of all added bridges */
448         struct list_head list;
449         /**
450          * @timings:
451          *
452          * the timing specification for the bridge, if any (may be NULL)
453          */
454         const struct drm_bridge_timings *timings;
455         /** @funcs: control functions */
456         const struct drm_bridge_funcs *funcs;
457         /** @driver_private: pointer to the bridge driver's internal context */
458         void *driver_private;
459 };
460
461 static inline struct drm_bridge *
462 drm_priv_to_bridge(struct drm_private_obj *priv)
463 {
464         return container_of(priv, struct drm_bridge, base);
465 }
466
467 void drm_bridge_add(struct drm_bridge *bridge);
468 void drm_bridge_remove(struct drm_bridge *bridge);
469 struct drm_bridge *of_drm_find_bridge(struct device_node *np);
470 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
471                       struct drm_bridge *previous);
472
473 /**
474  * drm_bridge_get_next_bridge() - Get the next bridge in the chain
475  * @bridge: bridge object
476  *
477  * RETURNS:
478  * the next bridge in the chain after @bridge, or NULL if @bridge is the last.
479  */
480 static inline struct drm_bridge *
481 drm_bridge_get_next_bridge(struct drm_bridge *bridge)
482 {
483         if (list_is_last(&bridge->chain_node, &bridge->encoder->bridge_chain))
484                 return NULL;
485
486         return list_next_entry(bridge, chain_node);
487 }
488
489 /**
490  * drm_bridge_get_prev_bridge() - Get the previous bridge in the chain
491  * @bridge: bridge object
492  *
493  * RETURNS:
494  * the previous bridge in the chain, or NULL if @bridge is the first.
495  */
496 static inline struct drm_bridge *
497 drm_bridge_get_prev_bridge(struct drm_bridge *bridge)
498 {
499         if (list_is_first(&bridge->chain_node, &bridge->encoder->bridge_chain))
500                 return NULL;
501
502         return list_prev_entry(bridge, chain_node);
503 }
504
505 /**
506  * drm_bridge_chain_get_first_bridge() - Get the first bridge in the chain
507  * @encoder: encoder object
508  *
509  * RETURNS:
510  * the first bridge in the chain, or NULL if @encoder has no bridge attached
511  * to it.
512  */
513 static inline struct drm_bridge *
514 drm_bridge_chain_get_first_bridge(struct drm_encoder *encoder)
515 {
516         return list_first_entry_or_null(&encoder->bridge_chain,
517                                         struct drm_bridge, chain_node);
518 }
519
520 /**
521  * drm_for_each_bridge_in_chain() - Iterate over all bridges present in a chain
522  * @encoder: the encoder to iterate bridges on
523  * @bridge: a bridge pointer updated to point to the current bridge at each
524  *          iteration
525  *
526  * Iterate over all bridges present in the bridge chain attached to @encoder.
527  */
528 #define drm_for_each_bridge_in_chain(encoder, bridge)                   \
529         list_for_each_entry(bridge, &(encoder)->bridge_chain, chain_node)
530
531 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
532                                  const struct drm_display_mode *mode,
533                                  struct drm_display_mode *adjusted_mode);
534 enum drm_mode_status
535 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
536                             const struct drm_display_mode *mode);
537 void drm_bridge_chain_disable(struct drm_bridge *bridge);
538 void drm_bridge_chain_post_disable(struct drm_bridge *bridge);
539 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
540                                const struct drm_display_mode *mode,
541                                const struct drm_display_mode *adjusted_mode);
542 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge);
543 void drm_bridge_chain_enable(struct drm_bridge *bridge);
544
545 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
546                                      struct drm_atomic_state *state);
547 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
548                                           struct drm_atomic_state *state);
549 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
550                                         struct drm_atomic_state *state);
551 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
552                                     struct drm_atomic_state *state);
553
554 #ifdef CONFIG_DRM_PANEL_BRIDGE
555 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel);
556 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
557                                               u32 connector_type);
558 void drm_panel_bridge_remove(struct drm_bridge *bridge);
559 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
560                                              struct drm_panel *panel);
561 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
562                                                    struct drm_panel *panel,
563                                                    u32 connector_type);
564 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge);
565 #endif
566
567 #endif