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