2 * Copyright (c) 2016 Intel Corporation
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.
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
23 #ifndef __DRM_BRIDGE_H__
24 #define __DRM_BRIDGE_H__
26 #include <linux/list.h>
27 #include <linux/ctype.h>
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>
35 struct drm_bridge_timings;
39 * struct drm_bridge_state - Atomic bridge state object
40 * @base: inherit from &drm_private_state
41 * @bridge: the bridge this state refers to
43 struct drm_bridge_state {
44 struct drm_private_state base;
46 struct drm_bridge *bridge;
49 static inline struct drm_bridge_state *
50 drm_priv_to_bridge_state(struct drm_private_state *priv)
52 return container_of(priv, struct drm_bridge_state, base);
56 * struct drm_bridge_funcs - drm_bridge control functions
58 struct drm_bridge_funcs {
62 * This callback is invoked whenever our bridge is being attached to a
65 * The @attach callback is optional.
69 * Zero on success, error code on failure.
71 int (*attach)(struct drm_bridge *bridge);
76 * This callback is invoked whenever our bridge is being detached from a
79 * The @detach callback is optional.
81 void (*detach)(struct drm_bridge *bridge);
86 * This callback is used to check if a specific mode is valid in this
87 * bridge. This should be implemented if the bridge has some sort of
88 * restriction in the modes it can display. For example, a given bridge
89 * may be responsible to set a clock value. If the clock can not
90 * produce all the values for the available modes then this callback
91 * can be used to restrict the number of modes to only the ones that
94 * This hook is used by the probe helpers to filter the mode list in
95 * drm_helper_probe_single_connector_modes(), and it is used by the
96 * atomic helpers to validate modes supplied by userspace in
97 * drm_atomic_helper_check_modeset().
99 * The @mode_valid callback is optional.
103 * Since this function is both called from the check phase of an atomic
104 * commit, and the mode validation in the probe paths it is not allowed
105 * to look at anything else but the passed-in mode, and validate it
106 * against configuration-invariant hardward constraints. Any further
107 * limits which depend upon the configuration can only be checked in
112 * drm_mode_status Enum
114 enum drm_mode_status (*mode_valid)(struct drm_bridge *bridge,
115 const struct drm_display_mode *mode);
120 * This callback is used to validate and adjust a mode. The parameter
121 * mode is the display mode that should be fed to the next element in
122 * the display chain, either the final &drm_connector or the next
123 * &drm_bridge. The parameter adjusted_mode is the input mode the bridge
124 * requires. It can be modified by this callback and does not need to
125 * match mode. See also &drm_crtc_state.adjusted_mode for more details.
127 * This is the only hook that allows a bridge to reject a modeset. If
128 * this function passes all other callbacks must succeed for this
131 * The @mode_fixup callback is optional.
135 * This function is called in the check phase of atomic modesets, which
136 * can be aborted for any reason (including on userspace's request to
137 * just check whether a configuration would be possible). Drivers MUST
138 * NOT touch any persistent state (hardware or software) or data
139 * structures except the passed in @state parameter.
141 * Also beware that userspace can request its own custom modes, neither
142 * core nor helpers filter modes to the list of probe modes reported by
143 * the GETCONNECTOR IOCTL and stored in &drm_connector.modes. To ensure
144 * that modes are filtered consistently put any bridge constraints and
145 * limits checks into @mode_valid.
149 * True if an acceptable configuration is possible, false if the modeset
150 * operation should be rejected.
152 bool (*mode_fixup)(struct drm_bridge *bridge,
153 const struct drm_display_mode *mode,
154 struct drm_display_mode *adjusted_mode);
158 * This callback should disable the bridge. It is called right before
159 * the preceding element in the display pipe is disabled. If the
160 * preceding element is a bridge this means it's called before that
161 * bridge's @disable vfunc. If the preceding element is a &drm_encoder
162 * it's called right before the &drm_encoder_helper_funcs.disable,
163 * &drm_encoder_helper_funcs.prepare or &drm_encoder_helper_funcs.dpms
166 * The bridge can assume that the display pipe (i.e. clocks and timing
167 * signals) feeding it is still running when this callback is called.
169 * The @disable callback is optional.
171 void (*disable)(struct drm_bridge *bridge);
176 * This callback should disable the bridge. It is called right after the
177 * preceding element in the display pipe is disabled. If the preceding
178 * element is a bridge this means it's called after that bridge's
179 * @post_disable function. If the preceding element is a &drm_encoder
180 * it's called right after the encoder's
181 * &drm_encoder_helper_funcs.disable, &drm_encoder_helper_funcs.prepare
182 * or &drm_encoder_helper_funcs.dpms hook.
184 * The bridge must assume that the display pipe (i.e. clocks and timing
185 * singals) feeding it is no longer running when this callback is
188 * The @post_disable callback is optional.
190 void (*post_disable)(struct drm_bridge *bridge);
195 * This callback should set the given mode on the bridge. It is called
196 * after the @mode_set callback for the preceding element in the display
197 * pipeline has been called already. If the bridge is the first element
198 * then this would be &drm_encoder_helper_funcs.mode_set. The display
199 * pipe (i.e. clocks and timing signals) is off when this function is
202 * The adjusted_mode parameter is the mode output by the CRTC for the
203 * first bridge in the chain. It can be different from the mode
204 * parameter that contains the desired mode for the connector at the end
205 * of the bridges chain, for instance when the first bridge in the chain
206 * performs scaling. The adjusted mode is mostly useful for the first
207 * bridge in the chain and is likely irrelevant for the other bridges.
209 * For atomic drivers the adjusted_mode is the mode stored in
210 * &drm_crtc_state.adjusted_mode.
214 * If a need arises to store and access modes adjusted for other
215 * locations than the connection between the CRTC and the first bridge,
216 * the DRM framework will have to be extended with DRM bridge states.
218 void (*mode_set)(struct drm_bridge *bridge,
219 const struct drm_display_mode *mode,
220 const struct drm_display_mode *adjusted_mode);
224 * This callback should enable the bridge. It is called right before
225 * the preceding element in the display pipe is enabled. If the
226 * preceding element is a bridge this means it's called before that
227 * bridge's @pre_enable function. If the preceding element is a
228 * &drm_encoder it's called right before the encoder's
229 * &drm_encoder_helper_funcs.enable, &drm_encoder_helper_funcs.commit or
230 * &drm_encoder_helper_funcs.dpms hook.
232 * The display pipe (i.e. clocks and timing signals) feeding this bridge
233 * will not yet be running when this callback is called. The bridge must
234 * not enable the display link feeding the next bridge in the chain (if
235 * there is one) when this callback is called.
237 * The @pre_enable callback is optional.
239 void (*pre_enable)(struct drm_bridge *bridge);
244 * This callback should enable the bridge. It is called right after
245 * the preceding element in the display pipe is enabled. If the
246 * preceding element is a bridge this means it's called after that
247 * bridge's @enable function. If the preceding element is a
248 * &drm_encoder it's called right after the encoder's
249 * &drm_encoder_helper_funcs.enable, &drm_encoder_helper_funcs.commit or
250 * &drm_encoder_helper_funcs.dpms hook.
252 * The bridge can assume that the display pipe (i.e. clocks and timing
253 * signals) feeding it is running when this callback is called. This
254 * callback must enable the display link feeding the next bridge in the
255 * chain if there is one.
257 * The @enable callback is optional.
259 void (*enable)(struct drm_bridge *bridge);
262 * @atomic_pre_enable:
264 * This callback should enable the bridge. It is called right before
265 * the preceding element in the display pipe is enabled. If the
266 * preceding element is a bridge this means it's called before that
267 * bridge's @atomic_pre_enable or @pre_enable function. If the preceding
268 * element is a &drm_encoder it's called right before the encoder's
269 * &drm_encoder_helper_funcs.atomic_enable hook.
271 * The display pipe (i.e. clocks and timing signals) feeding this bridge
272 * will not yet be running when this callback is called. The bridge must
273 * not enable the display link feeding the next bridge in the chain (if
274 * there is one) when this callback is called.
276 * Note that this function will only be invoked in the context of an
277 * atomic commit. It will not be invoked from
278 * &drm_bridge_chain_pre_enable. It would be prudent to also provide an
279 * implementation of @pre_enable if you are expecting driver calls into
280 * &drm_bridge_chain_pre_enable.
282 * The @atomic_pre_enable callback is optional.
284 void (*atomic_pre_enable)(struct drm_bridge *bridge,
285 struct drm_bridge_state *old_bridge_state);
290 * This callback should enable the bridge. It is called right after
291 * the preceding element in the display pipe is enabled. If the
292 * preceding element is a bridge this means it's called after that
293 * bridge's @atomic_enable or @enable function. If the preceding element
294 * is a &drm_encoder it's called right after the encoder's
295 * &drm_encoder_helper_funcs.atomic_enable hook.
297 * The bridge can assume that the display pipe (i.e. clocks and timing
298 * signals) feeding it is running when this callback is called. This
299 * callback must enable the display link feeding the next bridge in the
300 * chain if there is one.
302 * Note that this function will only be invoked in the context of an
303 * atomic commit. It will not be invoked from &drm_bridge_chain_enable.
304 * It would be prudent to also provide an implementation of @enable if
305 * you are expecting driver calls into &drm_bridge_chain_enable.
307 * The @atomic_enable callback is optional.
309 void (*atomic_enable)(struct drm_bridge *bridge,
310 struct drm_bridge_state *old_bridge_state);
314 * This callback should disable the bridge. It is called right before
315 * the preceding element in the display pipe is disabled. If the
316 * preceding element is a bridge this means it's called before that
317 * bridge's @atomic_disable or @disable vfunc. If the preceding element
318 * is a &drm_encoder it's called right before the
319 * &drm_encoder_helper_funcs.atomic_disable hook.
321 * The bridge can assume that the display pipe (i.e. clocks and timing
322 * signals) feeding it is still running when this callback is called.
324 * Note that this function will only be invoked in the context of an
325 * atomic commit. It will not be invoked from
326 * &drm_bridge_chain_disable. It would be prudent to also provide an
327 * implementation of @disable if you are expecting driver calls into
328 * &drm_bridge_chain_disable.
330 * The @atomic_disable callback is optional.
332 void (*atomic_disable)(struct drm_bridge *bridge,
333 struct drm_bridge_state *old_bridge_state);
336 * @atomic_post_disable:
338 * This callback should disable the bridge. It is called right after the
339 * preceding element in the display pipe is disabled. If the preceding
340 * element is a bridge this means it's called after that bridge's
341 * @atomic_post_disable or @post_disable function. If the preceding
342 * element is a &drm_encoder it's called right after the encoder's
343 * &drm_encoder_helper_funcs.atomic_disable hook.
345 * The bridge must assume that the display pipe (i.e. clocks and timing
346 * signals) feeding it is no longer running when this callback is
349 * Note that this function will only be invoked in the context of an
350 * atomic commit. It will not be invoked from
351 * &drm_bridge_chain_post_disable.
352 * It would be prudent to also provide an implementation of
353 * @post_disable if you are expecting driver calls into
354 * &drm_bridge_chain_post_disable.
356 * The @atomic_post_disable callback is optional.
358 void (*atomic_post_disable)(struct drm_bridge *bridge,
359 struct drm_bridge_state *old_bridge_state);
362 * @atomic_duplicate_state:
364 * Duplicate the current bridge state object (which is guaranteed to be
367 * The atomic_duplicate_state() is optional. When not implemented the
368 * core allocates a drm_bridge_state object and calls
369 * &__drm_atomic_helper_bridge_duplicate_state() to initialize it.
372 * A valid drm_bridge_state object or NULL if the allocation fails.
374 struct drm_bridge_state *(*atomic_duplicate_state)(struct drm_bridge *bridge);
377 * @atomic_destroy_state:
379 * Destroy a bridge state object previously allocated by
380 * &drm_bridge_funcs.atomic_duplicate_state().
382 * The atomic_destroy_state hook is optional. When not implemented the
383 * core calls kfree() on the state.
385 void (*atomic_destroy_state)(struct drm_bridge *bridge,
386 struct drm_bridge_state *state);
391 * Reset the bridge to a predefined state (or retrieve its current
392 * state) and return a &drm_bridge_state object matching this state.
393 * This function is called at attach time.
395 * The atomic_reset hook is optional. When not implemented the core
396 * allocates a new state and calls &__drm_atomic_helper_bridge_reset().
399 * A valid drm_bridge_state object in case of success, an ERR_PTR()
400 * giving the reason of the failure otherwise.
402 struct drm_bridge_state *(*atomic_reset)(struct drm_bridge *bridge);
406 * struct drm_bridge_timings - timing information for the bridge
408 struct drm_bridge_timings {
412 * Tells what additional settings for the pixel data on the bus
413 * this bridge requires (like pixel signal polarity). See also
414 * &drm_display_info->bus_flags.
420 * Defines the time in picoseconds the input data lines must be
421 * stable before the clock edge.
427 * Defines the time in picoseconds taken for the bridge to sample the
428 * input signal after the clock edge.
434 * True if the bus operates in dual-link mode. The exact meaning is
435 * dependent on the bus type. For LVDS buses, this indicates that even-
436 * and odd-numbered pixels are received on separate links.
442 * struct drm_bridge - central DRM bridge control structure
445 /** @base: inherit from &drm_private_object */
446 struct drm_private_obj base;
447 /** @dev: DRM device this bridge belongs to */
448 struct drm_device *dev;
449 /** @encoder: encoder to which this bridge is connected */
450 struct drm_encoder *encoder;
451 /** @chain_node: used to form a bridge chain */
452 struct list_head chain_node;
454 /** @of_node: device node pointer to the bridge */
455 struct device_node *of_node;
457 /** @list: to keep track of all added bridges */
458 struct list_head list;
462 * the timing specification for the bridge, if any (may be NULL)
464 const struct drm_bridge_timings *timings;
465 /** @funcs: control functions */
466 const struct drm_bridge_funcs *funcs;
467 /** @driver_private: pointer to the bridge driver's internal context */
468 void *driver_private;
471 static inline struct drm_bridge *
472 drm_priv_to_bridge(struct drm_private_obj *priv)
474 return container_of(priv, struct drm_bridge, base);
477 void drm_bridge_add(struct drm_bridge *bridge);
478 void drm_bridge_remove(struct drm_bridge *bridge);
479 struct drm_bridge *of_drm_find_bridge(struct device_node *np);
480 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
481 struct drm_bridge *previous);
484 * drm_bridge_get_next_bridge() - Get the next bridge in the chain
485 * @bridge: bridge object
488 * the next bridge in the chain after @bridge, or NULL if @bridge is the last.
490 static inline struct drm_bridge *
491 drm_bridge_get_next_bridge(struct drm_bridge *bridge)
493 if (list_is_last(&bridge->chain_node, &bridge->encoder->bridge_chain))
496 return list_next_entry(bridge, chain_node);
500 * drm_bridge_get_prev_bridge() - Get the previous bridge in the chain
501 * @bridge: bridge object
504 * the previous bridge in the chain, or NULL if @bridge is the first.
506 static inline struct drm_bridge *
507 drm_bridge_get_prev_bridge(struct drm_bridge *bridge)
509 if (list_is_first(&bridge->chain_node, &bridge->encoder->bridge_chain))
512 return list_prev_entry(bridge, chain_node);
516 * drm_bridge_chain_get_first_bridge() - Get the first bridge in the chain
517 * @encoder: encoder object
520 * the first bridge in the chain, or NULL if @encoder has no bridge attached
523 static inline struct drm_bridge *
524 drm_bridge_chain_get_first_bridge(struct drm_encoder *encoder)
526 return list_first_entry_or_null(&encoder->bridge_chain,
527 struct drm_bridge, chain_node);
531 * drm_for_each_bridge_in_chain() - Iterate over all bridges present in a chain
532 * @encoder: the encoder to iterate bridges on
533 * @bridge: a bridge pointer updated to point to the current bridge at each
536 * Iterate over all bridges present in the bridge chain attached to @encoder.
538 #define drm_for_each_bridge_in_chain(encoder, bridge) \
539 list_for_each_entry(bridge, &(encoder)->bridge_chain, chain_node)
541 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
542 const struct drm_display_mode *mode,
543 struct drm_display_mode *adjusted_mode);
545 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
546 const struct drm_display_mode *mode);
547 void drm_bridge_chain_disable(struct drm_bridge *bridge);
548 void drm_bridge_chain_post_disable(struct drm_bridge *bridge);
549 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
550 const struct drm_display_mode *mode,
551 const struct drm_display_mode *adjusted_mode);
552 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge);
553 void drm_bridge_chain_enable(struct drm_bridge *bridge);
555 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
556 struct drm_atomic_state *state);
557 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
558 struct drm_atomic_state *state);
559 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
560 struct drm_atomic_state *state);
561 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
562 struct drm_atomic_state *state);
564 void __drm_atomic_helper_bridge_reset(struct drm_bridge *bridge,
565 struct drm_bridge_state *state);
566 void __drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge,
567 struct drm_bridge_state *new);
569 static inline struct drm_bridge_state *
570 drm_atomic_get_bridge_state(struct drm_atomic_state *state,
571 struct drm_bridge *bridge)
573 struct drm_private_state *obj_state;
575 obj_state = drm_atomic_get_private_obj_state(state, &bridge->base);
576 if (IS_ERR(obj_state))
577 return ERR_CAST(obj_state);
579 return drm_priv_to_bridge_state(obj_state);
582 static inline struct drm_bridge_state *
583 drm_atomic_get_old_bridge_state(struct drm_atomic_state *state,
584 struct drm_bridge *bridge)
586 struct drm_private_state *obj_state;
588 obj_state = drm_atomic_get_old_private_obj_state(state, &bridge->base);
592 return drm_priv_to_bridge_state(obj_state);
595 static inline struct drm_bridge_state *
596 drm_atomic_get_new_bridge_state(struct drm_atomic_state *state,
597 struct drm_bridge *bridge)
599 struct drm_private_state *obj_state;
601 obj_state = drm_atomic_get_new_private_obj_state(state, &bridge->base);
605 return drm_priv_to_bridge_state(obj_state);
608 #ifdef CONFIG_DRM_PANEL_BRIDGE
609 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel);
610 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
612 void drm_panel_bridge_remove(struct drm_bridge *bridge);
613 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
614 struct drm_panel *panel);
615 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
616 struct drm_panel *panel,
618 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge);