xref: /openbmc/linux/include/drm/drm_bridge.h (revision e721eb06)
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/ctype.h>
27 #include <linux/list.h>
28 #include <linux/mutex.h>
29 
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_encoder.h>
32 #include <drm/drm_mode_object.h>
33 #include <drm/drm_modes.h>
34 
35 struct drm_bridge;
36 struct drm_bridge_timings;
37 struct drm_connector;
38 struct drm_panel;
39 struct edid;
40 struct i2c_adapter;
41 
42 /**
43  * enum drm_bridge_attach_flags - Flags for &drm_bridge_funcs.attach
44  */
45 enum drm_bridge_attach_flags {
46 	/**
47 	 * @DRM_BRIDGE_ATTACH_NO_CONNECTOR: When this flag is set the bridge
48 	 * shall not create a drm_connector.
49 	 */
50 	DRM_BRIDGE_ATTACH_NO_CONNECTOR = BIT(0),
51 };
52 
53 /**
54  * struct drm_bridge_funcs - drm_bridge control functions
55  */
56 struct drm_bridge_funcs {
57 	/**
58 	 * @attach:
59 	 *
60 	 * This callback is invoked whenever our bridge is being attached to a
61 	 * &drm_encoder. The flags argument tunes the behaviour of the attach
62 	 * operation (see DRM_BRIDGE_ATTACH_*).
63 	 *
64 	 * The @attach callback is optional.
65 	 *
66 	 * RETURNS:
67 	 *
68 	 * Zero on success, error code on failure.
69 	 */
70 	int (*attach)(struct drm_bridge *bridge,
71 		      enum drm_bridge_attach_flags flags);
72 
73 	/**
74 	 * @detach:
75 	 *
76 	 * This callback is invoked whenever our bridge is being detached from a
77 	 * &drm_encoder.
78 	 *
79 	 * The @detach callback is optional.
80 	 */
81 	void (*detach)(struct drm_bridge *bridge);
82 
83 	/**
84 	 * @mode_valid:
85 	 *
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
92 	 * can be displayed.
93 	 *
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().
98 	 *
99 	 * The @mode_valid callback is optional.
100 	 *
101 	 * NOTE:
102 	 *
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
108 	 * @mode_fixup.
109 	 *
110 	 * RETURNS:
111 	 *
112 	 * drm_mode_status Enum
113 	 */
114 	enum drm_mode_status (*mode_valid)(struct drm_bridge *bridge,
115 					   const struct drm_display_mode *mode);
116 
117 	/**
118 	 * @mode_fixup:
119 	 *
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.
126 	 *
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
129 	 * configuration.
130 	 *
131 	 * The mode_fixup callback is optional. &drm_bridge_funcs.mode_fixup()
132 	 * is not called when &drm_bridge_funcs.atomic_check() is implemented,
133 	 * so only one of them should be provided.
134 	 *
135 	 * NOTE:
136 	 *
137 	 * This function is called in the check phase of atomic modesets, which
138 	 * can be aborted for any reason (including on userspace's request to
139 	 * just check whether a configuration would be possible). Drivers MUST
140 	 * NOT touch any persistent state (hardware or software) or data
141 	 * structures except the passed in @state parameter.
142 	 *
143 	 * Also beware that userspace can request its own custom modes, neither
144 	 * core nor helpers filter modes to the list of probe modes reported by
145 	 * the GETCONNECTOR IOCTL and stored in &drm_connector.modes. To ensure
146 	 * that modes are filtered consistently put any bridge constraints and
147 	 * limits checks into @mode_valid.
148 	 *
149 	 * RETURNS:
150 	 *
151 	 * True if an acceptable configuration is possible, false if the modeset
152 	 * operation should be rejected.
153 	 */
154 	bool (*mode_fixup)(struct drm_bridge *bridge,
155 			   const struct drm_display_mode *mode,
156 			   struct drm_display_mode *adjusted_mode);
157 	/**
158 	 * @disable:
159 	 *
160 	 * This callback should disable the bridge. It is called right before
161 	 * the preceding element in the display pipe is disabled. If the
162 	 * preceding element is a bridge this means it's called before that
163 	 * bridge's @disable vfunc. If the preceding element is a &drm_encoder
164 	 * it's called right before the &drm_encoder_helper_funcs.disable,
165 	 * &drm_encoder_helper_funcs.prepare or &drm_encoder_helper_funcs.dpms
166 	 * hook.
167 	 *
168 	 * The bridge can assume that the display pipe (i.e. clocks and timing
169 	 * signals) feeding it is still running when this callback is called.
170 	 *
171 	 * The @disable callback is optional.
172 	 */
173 	void (*disable)(struct drm_bridge *bridge);
174 
175 	/**
176 	 * @post_disable:
177 	 *
178 	 * This callback should disable the bridge. It is called right after the
179 	 * preceding element in the display pipe is disabled. If the preceding
180 	 * element is a bridge this means it's called after that bridge's
181 	 * @post_disable function. If the preceding element is a &drm_encoder
182 	 * it's called right after the encoder's
183 	 * &drm_encoder_helper_funcs.disable, &drm_encoder_helper_funcs.prepare
184 	 * or &drm_encoder_helper_funcs.dpms hook.
185 	 *
186 	 * The bridge must assume that the display pipe (i.e. clocks and timing
187 	 * singals) feeding it is no longer running when this callback is
188 	 * called.
189 	 *
190 	 * The @post_disable callback is optional.
191 	 */
192 	void (*post_disable)(struct drm_bridge *bridge);
193 
194 	/**
195 	 * @mode_set:
196 	 *
197 	 * This callback should set the given mode on the bridge. It is called
198 	 * after the @mode_set callback for the preceding element in the display
199 	 * pipeline has been called already. If the bridge is the first element
200 	 * then this would be &drm_encoder_helper_funcs.mode_set. The display
201 	 * pipe (i.e.  clocks and timing signals) is off when this function is
202 	 * called.
203 	 *
204 	 * The adjusted_mode parameter is the mode output by the CRTC for the
205 	 * first bridge in the chain. It can be different from the mode
206 	 * parameter that contains the desired mode for the connector at the end
207 	 * of the bridges chain, for instance when the first bridge in the chain
208 	 * performs scaling. The adjusted mode is mostly useful for the first
209 	 * bridge in the chain and is likely irrelevant for the other bridges.
210 	 *
211 	 * For atomic drivers the adjusted_mode is the mode stored in
212 	 * &drm_crtc_state.adjusted_mode.
213 	 *
214 	 * NOTE:
215 	 *
216 	 * If a need arises to store and access modes adjusted for other
217 	 * locations than the connection between the CRTC and the first bridge,
218 	 * the DRM framework will have to be extended with DRM bridge states.
219 	 */
220 	void (*mode_set)(struct drm_bridge *bridge,
221 			 const struct drm_display_mode *mode,
222 			 const struct drm_display_mode *adjusted_mode);
223 	/**
224 	 * @pre_enable:
225 	 *
226 	 * This callback should enable the bridge. It is called right before
227 	 * the preceding element in the display pipe is enabled. If the
228 	 * preceding element is a bridge this means it's called before that
229 	 * bridge's @pre_enable function. If the preceding element is a
230 	 * &drm_encoder it's called right before the encoder's
231 	 * &drm_encoder_helper_funcs.enable, &drm_encoder_helper_funcs.commit or
232 	 * &drm_encoder_helper_funcs.dpms hook.
233 	 *
234 	 * The display pipe (i.e. clocks and timing signals) feeding this bridge
235 	 * will not yet be running when this callback is called. The bridge must
236 	 * not enable the display link feeding the next bridge in the chain (if
237 	 * there is one) when this callback is called.
238 	 *
239 	 * The @pre_enable callback is optional.
240 	 */
241 	void (*pre_enable)(struct drm_bridge *bridge);
242 
243 	/**
244 	 * @enable:
245 	 *
246 	 * This callback should enable the bridge. It is called right after
247 	 * the preceding element in the display pipe is enabled. If the
248 	 * preceding element is a bridge this means it's called after that
249 	 * bridge's @enable function. If the preceding element is a
250 	 * &drm_encoder it's called right after the encoder's
251 	 * &drm_encoder_helper_funcs.enable, &drm_encoder_helper_funcs.commit or
252 	 * &drm_encoder_helper_funcs.dpms hook.
253 	 *
254 	 * The bridge can assume that the display pipe (i.e. clocks and timing
255 	 * signals) feeding it is running when this callback is called. This
256 	 * callback must enable the display link feeding the next bridge in the
257 	 * chain if there is one.
258 	 *
259 	 * The @enable callback is optional.
260 	 */
261 	void (*enable)(struct drm_bridge *bridge);
262 
263 	/**
264 	 * @atomic_pre_enable:
265 	 *
266 	 * This callback should enable the bridge. It is called right before
267 	 * the preceding element in the display pipe is enabled. If the
268 	 * preceding element is a bridge this means it's called before that
269 	 * bridge's @atomic_pre_enable or @pre_enable function. If the preceding
270 	 * element is a &drm_encoder it's called right before the encoder's
271 	 * &drm_encoder_helper_funcs.atomic_enable hook.
272 	 *
273 	 * The display pipe (i.e. clocks and timing signals) feeding this bridge
274 	 * will not yet be running when this callback is called. The bridge must
275 	 * not enable the display link feeding the next bridge in the chain (if
276 	 * there is one) when this callback is called.
277 	 *
278 	 * Note that this function will only be invoked in the context of an
279 	 * atomic commit. It will not be invoked from
280 	 * &drm_bridge_chain_pre_enable. It would be prudent to also provide an
281 	 * implementation of @pre_enable if you are expecting driver calls into
282 	 * &drm_bridge_chain_pre_enable.
283 	 *
284 	 * The @atomic_pre_enable callback is optional.
285 	 */
286 	void (*atomic_pre_enable)(struct drm_bridge *bridge,
287 				  struct drm_bridge_state *old_bridge_state);
288 
289 	/**
290 	 * @atomic_enable:
291 	 *
292 	 * This callback should enable the bridge. It is called right after
293 	 * the preceding element in the display pipe is enabled. If the
294 	 * preceding element is a bridge this means it's called after that
295 	 * bridge's @atomic_enable or @enable function. If the preceding element
296 	 * is a &drm_encoder it's called right after the encoder's
297 	 * &drm_encoder_helper_funcs.atomic_enable hook.
298 	 *
299 	 * The bridge can assume that the display pipe (i.e. clocks and timing
300 	 * signals) feeding it is running when this callback is called. This
301 	 * callback must enable the display link feeding the next bridge in the
302 	 * chain if there is one.
303 	 *
304 	 * Note that this function will only be invoked in the context of an
305 	 * atomic commit. It will not be invoked from &drm_bridge_chain_enable.
306 	 * It would be prudent to also provide an implementation of @enable if
307 	 * you are expecting driver calls into &drm_bridge_chain_enable.
308 	 *
309 	 * The @atomic_enable callback is optional.
310 	 */
311 	void (*atomic_enable)(struct drm_bridge *bridge,
312 			      struct drm_bridge_state *old_bridge_state);
313 	/**
314 	 * @atomic_disable:
315 	 *
316 	 * This callback should disable the bridge. It is called right before
317 	 * the preceding element in the display pipe is disabled. If the
318 	 * preceding element is a bridge this means it's called before that
319 	 * bridge's @atomic_disable or @disable vfunc. If the preceding element
320 	 * is a &drm_encoder it's called right before the
321 	 * &drm_encoder_helper_funcs.atomic_disable hook.
322 	 *
323 	 * The bridge can assume that the display pipe (i.e. clocks and timing
324 	 * signals) feeding it is still running when this callback is called.
325 	 *
326 	 * Note that this function will only be invoked in the context of an
327 	 * atomic commit. It will not be invoked from
328 	 * &drm_bridge_chain_disable. It would be prudent to also provide an
329 	 * implementation of @disable if you are expecting driver calls into
330 	 * &drm_bridge_chain_disable.
331 	 *
332 	 * The @atomic_disable callback is optional.
333 	 */
334 	void (*atomic_disable)(struct drm_bridge *bridge,
335 			       struct drm_bridge_state *old_bridge_state);
336 
337 	/**
338 	 * @atomic_post_disable:
339 	 *
340 	 * This callback should disable the bridge. It is called right after the
341 	 * preceding element in the display pipe is disabled. If the preceding
342 	 * element is a bridge this means it's called after that bridge's
343 	 * @atomic_post_disable or @post_disable function. If the preceding
344 	 * element is a &drm_encoder it's called right after the encoder's
345 	 * &drm_encoder_helper_funcs.atomic_disable hook.
346 	 *
347 	 * The bridge must assume that the display pipe (i.e. clocks and timing
348 	 * signals) feeding it is no longer running when this callback is
349 	 * called.
350 	 *
351 	 * Note that this function will only be invoked in the context of an
352 	 * atomic commit. It will not be invoked from
353 	 * &drm_bridge_chain_post_disable.
354 	 * It would be prudent to also provide an implementation of
355 	 * @post_disable if you are expecting driver calls into
356 	 * &drm_bridge_chain_post_disable.
357 	 *
358 	 * The @atomic_post_disable callback is optional.
359 	 */
360 	void (*atomic_post_disable)(struct drm_bridge *bridge,
361 				    struct drm_bridge_state *old_bridge_state);
362 
363 	/**
364 	 * @atomic_duplicate_state:
365 	 *
366 	 * Duplicate the current bridge state object (which is guaranteed to be
367 	 * non-NULL).
368 	 *
369 	 * The atomic_duplicate_state hook is mandatory if the bridge
370 	 * implements any of the atomic hooks, and should be left unassigned
371 	 * otherwise. For bridges that don't subclass &drm_bridge_state, the
372 	 * drm_atomic_helper_bridge_duplicate_state() helper function shall be
373 	 * used to implement this hook.
374 	 *
375 	 * RETURNS:
376 	 * A valid drm_bridge_state object or NULL if the allocation fails.
377 	 */
378 	struct drm_bridge_state *(*atomic_duplicate_state)(struct drm_bridge *bridge);
379 
380 	/**
381 	 * @atomic_destroy_state:
382 	 *
383 	 * Destroy a bridge state object previously allocated by
384 	 * &drm_bridge_funcs.atomic_duplicate_state().
385 	 *
386 	 * The atomic_destroy_state hook is mandatory if the bridge implements
387 	 * any of the atomic hooks, and should be left unassigned otherwise.
388 	 * For bridges that don't subclass &drm_bridge_state, the
389 	 * drm_atomic_helper_bridge_destroy_state() helper function shall be
390 	 * used to implement this hook.
391 	 */
392 	void (*atomic_destroy_state)(struct drm_bridge *bridge,
393 				     struct drm_bridge_state *state);
394 
395 	/**
396 	 * @atomic_get_output_bus_fmts:
397 	 *
398 	 * Return the supported bus formats on the output end of a bridge.
399 	 * The returned array must be allocated with kmalloc() and will be
400 	 * freed by the caller. If the allocation fails, NULL should be
401 	 * returned. num_output_fmts must be set to the returned array size.
402 	 * Formats listed in the returned array should be listed in decreasing
403 	 * preference order (the core will try all formats until it finds one
404 	 * that works).
405 	 *
406 	 * This method is only called on the last element of the bridge chain
407 	 * as part of the bus format negotiation process that happens in
408 	 * &drm_atomic_bridge_chain_select_bus_fmts().
409 	 * This method is optional. When not implemented, the core will
410 	 * fall back to &drm_connector.display_info.bus_formats[0] if
411 	 * &drm_connector.display_info.num_bus_formats > 0,
412 	 * or to MEDIA_BUS_FMT_FIXED otherwise.
413 	 */
414 	u32 *(*atomic_get_output_bus_fmts)(struct drm_bridge *bridge,
415 					   struct drm_bridge_state *bridge_state,
416 					   struct drm_crtc_state *crtc_state,
417 					   struct drm_connector_state *conn_state,
418 					   unsigned int *num_output_fmts);
419 
420 	/**
421 	 * @atomic_get_input_bus_fmts:
422 	 *
423 	 * Return the supported bus formats on the input end of a bridge for
424 	 * a specific output bus format.
425 	 *
426 	 * The returned array must be allocated with kmalloc() and will be
427 	 * freed by the caller. If the allocation fails, NULL should be
428 	 * returned. num_output_fmts must be set to the returned array size.
429 	 * Formats listed in the returned array should be listed in decreasing
430 	 * preference order (the core will try all formats until it finds one
431 	 * that works). When the format is not supported NULL should be
432 	 * returned and num_output_fmts should be set to 0.
433 	 *
434 	 * This method is called on all elements of the bridge chain as part of
435 	 * the bus format negotiation process that happens in
436 	 * drm_atomic_bridge_chain_select_bus_fmts().
437 	 * This method is optional. When not implemented, the core will bypass
438 	 * bus format negotiation on this element of the bridge without
439 	 * failing, and the previous element in the chain will be passed
440 	 * MEDIA_BUS_FMT_FIXED as its output bus format.
441 	 *
442 	 * Bridge drivers that need to support being linked to bridges that are
443 	 * not supporting bus format negotiation should handle the
444 	 * output_fmt == MEDIA_BUS_FMT_FIXED case appropriately, by selecting a
445 	 * sensible default value or extracting this information from somewhere
446 	 * else (FW property, &drm_display_mode, &drm_display_info, ...)
447 	 *
448 	 * Note: Even if input format selection on the first bridge has no
449 	 * impact on the negotiation process (bus format negotiation stops once
450 	 * we reach the first element of the chain), drivers are expected to
451 	 * return accurate input formats as the input format may be used to
452 	 * configure the CRTC output appropriately.
453 	 */
454 	u32 *(*atomic_get_input_bus_fmts)(struct drm_bridge *bridge,
455 					  struct drm_bridge_state *bridge_state,
456 					  struct drm_crtc_state *crtc_state,
457 					  struct drm_connector_state *conn_state,
458 					  u32 output_fmt,
459 					  unsigned int *num_input_fmts);
460 
461 	/**
462 	 * @atomic_check:
463 	 *
464 	 * This method is responsible for checking bridge state correctness.
465 	 * It can also check the state of the surrounding components in chain
466 	 * to make sure the whole pipeline can work properly.
467 	 *
468 	 * &drm_bridge_funcs.atomic_check() hooks are called in reverse
469 	 * order (from the last to the first bridge).
470 	 *
471 	 * This method is optional. &drm_bridge_funcs.mode_fixup() is not
472 	 * called when &drm_bridge_funcs.atomic_check() is implemented, so only
473 	 * one of them should be provided.
474 	 *
475 	 * If drivers need to tweak &drm_bridge_state.input_bus_cfg.flags or
476 	 * &drm_bridge_state.output_bus_cfg.flags it should should happen in
477 	 * this function. By default the &drm_bridge_state.output_bus_cfg.flags
478 	 * field is set to the next bridge
479 	 * &drm_bridge_state.input_bus_cfg.flags value or
480 	 * &drm_connector.display_info.bus_flags if the bridge is the last
481 	 * element in the chain.
482 	 *
483 	 * RETURNS:
484 	 * zero if the check passed, a negative error code otherwise.
485 	 */
486 	int (*atomic_check)(struct drm_bridge *bridge,
487 			    struct drm_bridge_state *bridge_state,
488 			    struct drm_crtc_state *crtc_state,
489 			    struct drm_connector_state *conn_state);
490 
491 	/**
492 	 * @atomic_reset:
493 	 *
494 	 * Reset the bridge to a predefined state (or retrieve its current
495 	 * state) and return a &drm_bridge_state object matching this state.
496 	 * This function is called at attach time.
497 	 *
498 	 * The atomic_reset hook is mandatory if the bridge implements any of
499 	 * the atomic hooks, and should be left unassigned otherwise. For
500 	 * bridges that don't subclass &drm_bridge_state, the
501 	 * drm_atomic_helper_bridge_reset() helper function shall be used to
502 	 * implement this hook.
503 	 *
504 	 * Note that the atomic_reset() semantics is not exactly matching the
505 	 * reset() semantics found on other components (connector, plane, ...).
506 	 *
507 	 * 1. The reset operation happens when the bridge is attached, not when
508 	 *    drm_mode_config_reset() is called
509 	 * 2. It's meant to be used exclusively on bridges that have been
510 	 *    converted to the ATOMIC API
511 	 *
512 	 * RETURNS:
513 	 * A valid drm_bridge_state object in case of success, an ERR_PTR()
514 	 * giving the reason of the failure otherwise.
515 	 */
516 	struct drm_bridge_state *(*atomic_reset)(struct drm_bridge *bridge);
517 
518 	/**
519 	 * @detect:
520 	 *
521 	 * Check if anything is attached to the bridge output.
522 	 *
523 	 * This callback is optional, if not implemented the bridge will be
524 	 * considered as always having a component attached to its output.
525 	 * Bridges that implement this callback shall set the
526 	 * DRM_BRIDGE_OP_DETECT flag in their &drm_bridge->ops.
527 	 *
528 	 * RETURNS:
529 	 *
530 	 * drm_connector_status indicating the bridge output status.
531 	 */
532 	enum drm_connector_status (*detect)(struct drm_bridge *bridge);
533 
534 	/**
535 	 * @get_modes:
536 	 *
537 	 * Fill all modes currently valid for the sink into the &drm_connector
538 	 * with drm_mode_probed_add().
539 	 *
540 	 * The @get_modes callback is mostly intended to support non-probeable
541 	 * displays such as many fixed panels. Bridges that support reading
542 	 * EDID shall leave @get_modes unimplemented and implement the
543 	 * &drm_bridge_funcs->get_edid callback instead.
544 	 *
545 	 * This callback is optional. Bridges that implement it shall set the
546 	 * DRM_BRIDGE_OP_MODES flag in their &drm_bridge->ops.
547 	 *
548 	 * The connector parameter shall be used for the sole purpose of
549 	 * filling modes, and shall not be stored internally by bridge drivers
550 	 * for future usage.
551 	 *
552 	 * RETURNS:
553 	 *
554 	 * The number of modes added by calling drm_mode_probed_add().
555 	 */
556 	int (*get_modes)(struct drm_bridge *bridge,
557 			 struct drm_connector *connector);
558 
559 	/**
560 	 * @get_edid:
561 	 *
562 	 * Read and parse the EDID data of the connected display.
563 	 *
564 	 * The @get_edid callback is the preferred way of reporting mode
565 	 * information for a display connected to the bridge output. Bridges
566 	 * that support reading EDID shall implement this callback and leave
567 	 * the @get_modes callback unimplemented.
568 	 *
569 	 * The caller of this operation shall first verify the output
570 	 * connection status and refrain from reading EDID from a disconnected
571 	 * output.
572 	 *
573 	 * This callback is optional. Bridges that implement it shall set the
574 	 * DRM_BRIDGE_OP_EDID flag in their &drm_bridge->ops.
575 	 *
576 	 * The connector parameter shall be used for the sole purpose of EDID
577 	 * retrieval and parsing, and shall not be stored internally by bridge
578 	 * drivers for future usage.
579 	 *
580 	 * RETURNS:
581 	 *
582 	 * An edid structure newly allocated with kmalloc() (or similar) on
583 	 * success, or NULL otherwise. The caller is responsible for freeing
584 	 * the returned edid structure with kfree().
585 	 */
586 	struct edid *(*get_edid)(struct drm_bridge *bridge,
587 				 struct drm_connector *connector);
588 
589 	/**
590 	 * @hpd_notify:
591 	 *
592 	 * Notify the bridge of hot plug detection.
593 	 *
594 	 * This callback is optional, it may be implemented by bridges that
595 	 * need to be notified of display connection or disconnection for
596 	 * internal reasons. One use case is to reset the internal state of CEC
597 	 * controllers for HDMI bridges.
598 	 */
599 	void (*hpd_notify)(struct drm_bridge *bridge,
600 			   enum drm_connector_status status);
601 
602 	/**
603 	 * @hpd_enable:
604 	 *
605 	 * Enable hot plug detection. From now on the bridge shall call
606 	 * drm_bridge_hpd_notify() each time a change is detected in the output
607 	 * connection status, until hot plug detection gets disabled with
608 	 * @hpd_disable.
609 	 *
610 	 * This callback is optional and shall only be implemented by bridges
611 	 * that support hot-plug notification without polling. Bridges that
612 	 * implement it shall also implement the @hpd_disable callback and set
613 	 * the DRM_BRIDGE_OP_HPD flag in their &drm_bridge->ops.
614 	 */
615 	void (*hpd_enable)(struct drm_bridge *bridge);
616 
617 	/**
618 	 * @hpd_disable:
619 	 *
620 	 * Disable hot plug detection. Once this function returns the bridge
621 	 * shall not call drm_bridge_hpd_notify() when a change in the output
622 	 * connection status occurs.
623 	 *
624 	 * This callback is optional and shall only be implemented by bridges
625 	 * that support hot-plug notification without polling. Bridges that
626 	 * implement it shall also implement the @hpd_enable callback and set
627 	 * the DRM_BRIDGE_OP_HPD flag in their &drm_bridge->ops.
628 	 */
629 	void (*hpd_disable)(struct drm_bridge *bridge);
630 };
631 
632 /**
633  * struct drm_bridge_timings - timing information for the bridge
634  */
635 struct drm_bridge_timings {
636 	/**
637 	 * @input_bus_flags:
638 	 *
639 	 * Tells what additional settings for the pixel data on the bus
640 	 * this bridge requires (like pixel signal polarity). See also
641 	 * &drm_display_info->bus_flags.
642 	 */
643 	u32 input_bus_flags;
644 	/**
645 	 * @setup_time_ps:
646 	 *
647 	 * Defines the time in picoseconds the input data lines must be
648 	 * stable before the clock edge.
649 	 */
650 	u32 setup_time_ps;
651 	/**
652 	 * @hold_time_ps:
653 	 *
654 	 * Defines the time in picoseconds taken for the bridge to sample the
655 	 * input signal after the clock edge.
656 	 */
657 	u32 hold_time_ps;
658 	/**
659 	 * @dual_link:
660 	 *
661 	 * True if the bus operates in dual-link mode. The exact meaning is
662 	 * dependent on the bus type. For LVDS buses, this indicates that even-
663 	 * and odd-numbered pixels are received on separate links.
664 	 */
665 	bool dual_link;
666 };
667 
668 /**
669  * enum drm_bridge_ops - Bitmask of operations supported by the bridge
670  */
671 enum drm_bridge_ops {
672 	/**
673 	 * @DRM_BRIDGE_OP_DETECT: The bridge can detect displays connected to
674 	 * its output. Bridges that set this flag shall implement the
675 	 * &drm_bridge_funcs->detect callback.
676 	 */
677 	DRM_BRIDGE_OP_DETECT = BIT(0),
678 	/**
679 	 * @DRM_BRIDGE_OP_EDID: The bridge can retrieve the EDID of the display
680 	 * connected to its output. Bridges that set this flag shall implement
681 	 * the &drm_bridge_funcs->get_edid callback.
682 	 */
683 	DRM_BRIDGE_OP_EDID = BIT(1),
684 	/**
685 	 * @DRM_BRIDGE_OP_HPD: The bridge can detect hot-plug and hot-unplug
686 	 * without requiring polling. Bridges that set this flag shall
687 	 * implement the &drm_bridge_funcs->hpd_enable and
688 	 * &drm_bridge_funcs->hpd_disable callbacks if they support enabling
689 	 * and disabling hot-plug detection dynamically.
690 	 */
691 	DRM_BRIDGE_OP_HPD = BIT(2),
692 	/**
693 	 * @DRM_BRIDGE_OP_MODES: The bridge can retrieve the modes supported
694 	 * by the display at its output. This does not include reading EDID
695 	 * which is separately covered by @DRM_BRIDGE_OP_EDID. Bridges that set
696 	 * this flag shall implement the &drm_bridge_funcs->get_modes callback.
697 	 */
698 	DRM_BRIDGE_OP_MODES = BIT(3),
699 };
700 
701 /**
702  * struct drm_bridge - central DRM bridge control structure
703  */
704 struct drm_bridge {
705 	/** @base: inherit from &drm_private_object */
706 	struct drm_private_obj base;
707 	/** @dev: DRM device this bridge belongs to */
708 	struct drm_device *dev;
709 	/** @encoder: encoder to which this bridge is connected */
710 	struct drm_encoder *encoder;
711 	/** @chain_node: used to form a bridge chain */
712 	struct list_head chain_node;
713 #ifdef CONFIG_OF
714 	/** @of_node: device node pointer to the bridge */
715 	struct device_node *of_node;
716 #endif
717 	/** @list: to keep track of all added bridges */
718 	struct list_head list;
719 	/**
720 	 * @timings:
721 	 *
722 	 * the timing specification for the bridge, if any (may be NULL)
723 	 */
724 	const struct drm_bridge_timings *timings;
725 	/** @funcs: control functions */
726 	const struct drm_bridge_funcs *funcs;
727 	/** @driver_private: pointer to the bridge driver's internal context */
728 	void *driver_private;
729 	/** @ops: bitmask of operations supported by the bridge */
730 	enum drm_bridge_ops ops;
731 	/**
732 	 * @type: Type of the connection at the bridge output
733 	 * (DRM_MODE_CONNECTOR_*). For bridges at the end of this chain this
734 	 * identifies the type of connected display.
735 	 */
736 	int type;
737 	/**
738 	 * @interlace_allowed: Indicate that the bridge can handle interlaced
739 	 * modes.
740 	 */
741 	bool interlace_allowed;
742 	/**
743 	 * @ddc: Associated I2C adapter for DDC access, if any.
744 	 */
745 	struct i2c_adapter *ddc;
746 	/** private: */
747 	/**
748 	 * @hpd_mutex: Protects the @hpd_cb and @hpd_data fields.
749 	 */
750 	struct mutex hpd_mutex;
751 	/**
752 	 * @hpd_cb: Hot plug detection callback, registered with
753 	 * drm_bridge_hpd_enable().
754 	 */
755 	void (*hpd_cb)(void *data, enum drm_connector_status status);
756 	/**
757 	 * @hpd_data: Private data passed to the Hot plug detection callback
758 	 * @hpd_cb.
759 	 */
760 	void *hpd_data;
761 };
762 
763 static inline struct drm_bridge *
764 drm_priv_to_bridge(struct drm_private_obj *priv)
765 {
766 	return container_of(priv, struct drm_bridge, base);
767 }
768 
769 void drm_bridge_add(struct drm_bridge *bridge);
770 void drm_bridge_remove(struct drm_bridge *bridge);
771 struct drm_bridge *of_drm_find_bridge(struct device_node *np);
772 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
773 		      struct drm_bridge *previous,
774 		      enum drm_bridge_attach_flags flags);
775 
776 /**
777  * drm_bridge_get_next_bridge() - Get the next bridge in the chain
778  * @bridge: bridge object
779  *
780  * RETURNS:
781  * the next bridge in the chain after @bridge, or NULL if @bridge is the last.
782  */
783 static inline struct drm_bridge *
784 drm_bridge_get_next_bridge(struct drm_bridge *bridge)
785 {
786 	if (list_is_last(&bridge->chain_node, &bridge->encoder->bridge_chain))
787 		return NULL;
788 
789 	return list_next_entry(bridge, chain_node);
790 }
791 
792 /**
793  * drm_bridge_get_prev_bridge() - Get the previous bridge in the chain
794  * @bridge: bridge object
795  *
796  * RETURNS:
797  * the previous bridge in the chain, or NULL if @bridge is the first.
798  */
799 static inline struct drm_bridge *
800 drm_bridge_get_prev_bridge(struct drm_bridge *bridge)
801 {
802 	if (list_is_first(&bridge->chain_node, &bridge->encoder->bridge_chain))
803 		return NULL;
804 
805 	return list_prev_entry(bridge, chain_node);
806 }
807 
808 /**
809  * drm_bridge_chain_get_first_bridge() - Get the first bridge in the chain
810  * @encoder: encoder object
811  *
812  * RETURNS:
813  * the first bridge in the chain, or NULL if @encoder has no bridge attached
814  * to it.
815  */
816 static inline struct drm_bridge *
817 drm_bridge_chain_get_first_bridge(struct drm_encoder *encoder)
818 {
819 	return list_first_entry_or_null(&encoder->bridge_chain,
820 					struct drm_bridge, chain_node);
821 }
822 
823 /**
824  * drm_for_each_bridge_in_chain() - Iterate over all bridges present in a chain
825  * @encoder: the encoder to iterate bridges on
826  * @bridge: a bridge pointer updated to point to the current bridge at each
827  *	    iteration
828  *
829  * Iterate over all bridges present in the bridge chain attached to @encoder.
830  */
831 #define drm_for_each_bridge_in_chain(encoder, bridge)			\
832 	list_for_each_entry(bridge, &(encoder)->bridge_chain, chain_node)
833 
834 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
835 				 const struct drm_display_mode *mode,
836 				 struct drm_display_mode *adjusted_mode);
837 enum drm_mode_status
838 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
839 			    const struct drm_display_mode *mode);
840 void drm_bridge_chain_disable(struct drm_bridge *bridge);
841 void drm_bridge_chain_post_disable(struct drm_bridge *bridge);
842 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
843 			       const struct drm_display_mode *mode,
844 			       const struct drm_display_mode *adjusted_mode);
845 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge);
846 void drm_bridge_chain_enable(struct drm_bridge *bridge);
847 
848 int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,
849 				  struct drm_crtc_state *crtc_state,
850 				  struct drm_connector_state *conn_state);
851 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
852 				     struct drm_atomic_state *state);
853 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
854 					  struct drm_atomic_state *state);
855 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
856 					struct drm_atomic_state *state);
857 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
858 				    struct drm_atomic_state *state);
859 
860 u32 *
861 drm_atomic_helper_bridge_propagate_bus_fmt(struct drm_bridge *bridge,
862 					struct drm_bridge_state *bridge_state,
863 					struct drm_crtc_state *crtc_state,
864 					struct drm_connector_state *conn_state,
865 					u32 output_fmt,
866 					unsigned int *num_input_fmts);
867 
868 enum drm_connector_status drm_bridge_detect(struct drm_bridge *bridge);
869 int drm_bridge_get_modes(struct drm_bridge *bridge,
870 			 struct drm_connector *connector);
871 struct edid *drm_bridge_get_edid(struct drm_bridge *bridge,
872 				 struct drm_connector *connector);
873 void drm_bridge_hpd_enable(struct drm_bridge *bridge,
874 			   void (*cb)(void *data,
875 				      enum drm_connector_status status),
876 			   void *data);
877 void drm_bridge_hpd_disable(struct drm_bridge *bridge);
878 void drm_bridge_hpd_notify(struct drm_bridge *bridge,
879 			   enum drm_connector_status status);
880 
881 #ifdef CONFIG_DRM_PANEL_BRIDGE
882 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel);
883 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
884 					      u32 connector_type);
885 void drm_panel_bridge_remove(struct drm_bridge *bridge);
886 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
887 					     struct drm_panel *panel);
888 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
889 						   struct drm_panel *panel,
890 						   u32 connector_type);
891 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge);
892 #endif
893 
894 #endif
895