blob: c96847fc0ebcbaffa76b2a63064a440f690db550 [file] [log] [blame]
Ajay Kumar3d3f8b12015-01-20 22:08:44 +05301/*
2 * Copyright (c) 2014 Samsung Electronics Co., Ltd
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include <linux/err.h>
25#include <linux/module.h>
Daniel Vetter199e4e92016-08-31 18:09:05 +020026#include <linux/mutex.h>
Ajay Kumar3d3f8b12015-01-20 22:08:44 +053027
Boris Brezillon75146592020-01-28 14:55:03 +010028#include <drm/drm_atomic_state_helper.h>
Daniel Vetter199e4e92016-08-31 18:09:05 +020029#include <drm/drm_bridge.h>
Laurent Pinchart3bb80f22016-11-28 17:59:08 +020030#include <drm/drm_encoder.h>
Maxime Ripard87ea9582021-09-10 15:09:39 +020031#include <drm/drm_of.h>
Laurent Pinchartfb8d6172021-03-23 23:50:08 +020032#include <drm/drm_print.h>
Ajay Kumar3d3f8b12015-01-20 22:08:44 +053033
Laurent Pinchart4a878c02016-11-28 18:32:05 +020034#include "drm_crtc_internal.h"
35
Archit Taneja2331b4e2015-05-21 11:03:17 +053036/**
37 * DOC: overview
38 *
Daniel Vetterea0dd852016-12-29 21:48:26 +010039 * &struct drm_bridge represents a device that hangs on to an encoder. These are
Daniel Vetterda024fe2015-12-04 09:45:47 +010040 * handy when a regular &drm_encoder entity isn't enough to represent the entire
Archit Taneja2331b4e2015-05-21 11:03:17 +053041 * encoder chain.
42 *
Daniel Vetterda024fe2015-12-04 09:45:47 +010043 * A bridge is always attached to a single &drm_encoder at a time, but can be
Laurent Pinchart04513692020-02-26 13:24:26 +020044 * either connected to it directly, or through a chain of bridges::
Archit Taneja2331b4e2015-05-21 11:03:17 +053045 *
Laurent Pinchart04513692020-02-26 13:24:26 +020046 * [ CRTC ---> ] Encoder ---> Bridge A ---> Bridge B
Archit Taneja2331b4e2015-05-21 11:03:17 +053047 *
Laurent Pinchart04513692020-02-26 13:24:26 +020048 * Here, the output of the encoder feeds to bridge A, and that furthers feeds to
49 * bridge B. Bridge chains can be arbitrarily long, and shall be fully linear:
50 * Chaining multiple bridges to the output of a bridge, or the same bridge to
51 * the output of different bridges, is not supported.
Archit Taneja2331b4e2015-05-21 11:03:17 +053052 *
Maxime Ripard88868152021-09-10 12:11:55 +020053 * &drm_bridge, like &drm_panel, aren't &drm_mode_object entities like planes,
54 * CRTCs, encoders or connectors and hence are not visible to userspace. They
55 * just provide additional hooks to get the desired output at the end of the
56 * encoder chain.
57 */
58
59/**
60 * DOC: display driver integration
61 *
Laurent Pinchart04513692020-02-26 13:24:26 +020062 * Display drivers are responsible for linking encoders with the first bridge
63 * in the chains. This is done by acquiring the appropriate bridge with
Maxime Ripard87ea9582021-09-10 15:09:39 +020064 * devm_drm_of_get_bridge(). Once acquired, the bridge shall be attached to the
65 * encoder with a call to drm_bridge_attach().
Archit Taneja2331b4e2015-05-21 11:03:17 +053066 *
Laurent Pinchart04513692020-02-26 13:24:26 +020067 * Bridges are responsible for linking themselves with the next bridge in the
68 * chain, if any. This is done the same way as for encoders, with the call to
69 * drm_bridge_attach() occurring in the &drm_bridge_funcs.attach operation.
70 *
71 * Once these links are created, the bridges can participate along with encoder
72 * functions to perform mode validation and fixup (through
73 * drm_bridge_chain_mode_valid() and drm_atomic_bridge_chain_check()), mode
74 * setting (through drm_bridge_chain_mode_set()), enable (through
75 * drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_enable())
76 * and disable (through drm_atomic_bridge_chain_disable() and
77 * drm_atomic_bridge_chain_post_disable()). Those functions call the
78 * corresponding operations provided in &drm_bridge_funcs in sequence for all
79 * bridges in the chain.
80 *
81 * For display drivers that use the atomic helpers
82 * drm_atomic_helper_check_modeset(),
83 * drm_atomic_helper_commit_modeset_enables() and
84 * drm_atomic_helper_commit_modeset_disables() (either directly in hand-rolled
85 * commit check and commit tail handlers, or through the higher-level
86 * drm_atomic_helper_check() and drm_atomic_helper_commit_tail() or
87 * drm_atomic_helper_commit_tail_rpm() helpers), this is done transparently and
88 * requires no intervention from the driver. For other drivers, the relevant
89 * DRM bridge chain functions shall be called manually.
90 *
Laurent Pinchart5e20bdf2020-02-26 13:24:40 +020091 * Bridges also participate in implementing the &drm_connector at the end of
92 * the bridge chain. Display drivers may use the drm_bridge_connector_init()
93 * helper to create the &drm_connector, or implement it manually on top of the
94 * connector-related operations exposed by the bridge (see the overview
95 * documentation of bridge operations for more details).
Archit Taneja2331b4e2015-05-21 11:03:17 +053096 */
97
Maxime Ripard209264a2021-09-10 12:11:56 +020098/**
99 * DOC: special care dsi
100 *
101 * The interaction between the bridges and other frameworks involved in
102 * the probing of the upstream driver and the bridge driver can be
103 * challenging. Indeed, there's multiple cases that needs to be
104 * considered:
105 *
106 * - The upstream driver doesn't use the component framework and isn't a
107 * MIPI-DSI host. In this case, the bridge driver will probe at some
108 * point and the upstream driver should try to probe again by returning
109 * EPROBE_DEFER as long as the bridge driver hasn't probed.
110 *
111 * - The upstream driver doesn't use the component framework, but is a
112 * MIPI-DSI host. The bridge device uses the MIPI-DCS commands to be
113 * controlled. In this case, the bridge device is a child of the
114 * display device and when it will probe it's assured that the display
115 * device (and MIPI-DSI host) is present. The upstream driver will be
116 * assured that the bridge driver is connected between the
117 * &mipi_dsi_host_ops.attach and &mipi_dsi_host_ops.detach operations.
118 * Therefore, it must run mipi_dsi_host_register() in its probe
119 * function, and then run drm_bridge_attach() in its
120 * &mipi_dsi_host_ops.attach hook.
121 *
122 * - The upstream driver uses the component framework and is a MIPI-DSI
123 * host. The bridge device uses the MIPI-DCS commands to be
124 * controlled. This is the same situation than above, and can run
125 * mipi_dsi_host_register() in either its probe or bind hooks.
126 *
127 * - The upstream driver uses the component framework and is a MIPI-DSI
128 * host. The bridge device uses a separate bus (such as I2C) to be
129 * controlled. In this case, there's no correlation between the probe
130 * of the bridge and upstream drivers, so care must be taken to avoid
131 * an endless EPROBE_DEFER loop, with each driver waiting for the
132 * other to probe.
133 *
134 * The ideal pattern to cover the last item (and all the others in the
135 * MIPI-DSI host driver case) is to split the operations like this:
136 *
137 * - The MIPI-DSI host driver must run mipi_dsi_host_register() in its
138 * probe hook. It will make sure that the MIPI-DSI host sticks around,
139 * and that the driver's bind can be called.
140 *
141 * - In its probe hook, the bridge driver must try to find its MIPI-DSI
142 * host, register as a MIPI-DSI device and attach the MIPI-DSI device
143 * to its host. The bridge driver is now functional.
144 *
145 * - In its &struct mipi_dsi_host_ops.attach hook, the MIPI-DSI host can
146 * now add its component. Its bind hook will now be called and since
147 * the bridge driver is attached and registered, we can now look for
148 * and attach it.
149 *
150 * At this point, we're now certain that both the upstream driver and
151 * the bridge driver are functional and we can't have a deadlock-like
152 * situation when probing.
153 */
154
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530155static DEFINE_MUTEX(bridge_lock);
156static LIST_HEAD(bridge_list);
157
Archit Taneja2331b4e2015-05-21 11:03:17 +0530158/**
159 * drm_bridge_add - add the given bridge to the global bridge list
160 *
161 * @bridge: bridge control structure
Archit Taneja2331b4e2015-05-21 11:03:17 +0530162 */
Inki Dae992868842017-07-03 17:42:17 +0900163void drm_bridge_add(struct drm_bridge *bridge)
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530164{
Laurent Pinchart11f6c4b2020-02-26 13:24:27 +0200165 mutex_init(&bridge->hpd_mutex);
166
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530167 mutex_lock(&bridge_lock);
168 list_add_tail(&bridge->list, &bridge_list);
169 mutex_unlock(&bridge_lock);
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530170}
171EXPORT_SYMBOL(drm_bridge_add);
172
Archit Taneja2331b4e2015-05-21 11:03:17 +0530173/**
174 * drm_bridge_remove - remove the given bridge from the global bridge list
175 *
176 * @bridge: bridge control structure
177 */
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530178void drm_bridge_remove(struct drm_bridge *bridge)
179{
180 mutex_lock(&bridge_lock);
181 list_del_init(&bridge->list);
182 mutex_unlock(&bridge_lock);
Laurent Pinchart11f6c4b2020-02-26 13:24:27 +0200183
184 mutex_destroy(&bridge->hpd_mutex);
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530185}
186EXPORT_SYMBOL(drm_bridge_remove);
187
Boris Brezillon75146592020-01-28 14:55:03 +0100188static struct drm_private_state *
189drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)
190{
191 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
192 struct drm_bridge_state *state;
193
194 state = bridge->funcs->atomic_duplicate_state(bridge);
195 return state ? &state->base : NULL;
196}
197
198static void
199drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,
200 struct drm_private_state *s)
201{
202 struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
203 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
204
205 bridge->funcs->atomic_destroy_state(bridge, state);
206}
207
208static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
209 .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
210 .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
211};
212
Archit Taneja2331b4e2015-05-21 11:03:17 +0530213/**
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200214 * drm_bridge_attach - attach the bridge to an encoder's chain
Archit Taneja2331b4e2015-05-21 11:03:17 +0530215 *
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200216 * @encoder: DRM encoder
217 * @bridge: bridge to attach
218 * @previous: previous bridge in the chain (optional)
Laurent Pincharta25b9882020-02-26 13:24:29 +0200219 * @flags: DRM_BRIDGE_ATTACH_* flags
Archit Taneja2331b4e2015-05-21 11:03:17 +0530220 *
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200221 * Called by a kms driver to link the bridge to an encoder's chain. The previous
222 * argument specifies the previous bridge in the chain. If NULL, the bridge is
223 * linked directly at the encoder's output. Otherwise it is linked at the
224 * previous bridge's output.
Archit Taneja2331b4e2015-05-21 11:03:17 +0530225 *
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200226 * If non-NULL the previous bridge must be already attached by a call to this
227 * function.
Archit Taneja2331b4e2015-05-21 11:03:17 +0530228 *
Peter Rosin169cc4c2018-08-06 08:19:10 +0200229 * Note that bridges attached to encoders are auto-detached during encoder
230 * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
231 * *not* be balanced with a drm_bridge_detach() in driver code.
232 *
Archit Taneja2331b4e2015-05-21 11:03:17 +0530233 * RETURNS:
234 * Zero on success, error code on failure
235 */
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200236int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
Laurent Pincharta25b9882020-02-26 13:24:29 +0200237 struct drm_bridge *previous,
238 enum drm_bridge_attach_flags flags)
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530239{
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200240 int ret;
241
242 if (!encoder || !bridge)
243 return -EINVAL;
244
245 if (previous && (!previous->dev || previous->encoder != encoder))
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530246 return -EINVAL;
247
248 if (bridge->dev)
249 return -EBUSY;
250
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200251 bridge->dev = encoder->dev;
252 bridge->encoder = encoder;
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530253
Boris Brezillon05193dc2019-12-03 15:15:08 +0100254 if (previous)
255 list_add(&bridge->chain_node, &previous->chain_node);
256 else
257 list_add(&bridge->chain_node, &encoder->bridge_chain);
258
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200259 if (bridge->funcs->attach) {
Laurent Pincharta25b9882020-02-26 13:24:29 +0200260 ret = bridge->funcs->attach(bridge, flags);
Boris Brezillon75146592020-01-28 14:55:03 +0100261 if (ret < 0)
262 goto err_reset_bridge;
263 }
264
265 if (bridge->funcs->atomic_reset) {
266 struct drm_bridge_state *state;
267
268 state = bridge->funcs->atomic_reset(bridge);
269 if (IS_ERR(state)) {
270 ret = PTR_ERR(state);
271 goto err_detach_bridge;
Boris Brezillon09912632020-01-07 19:58:07 +0100272 }
Boris Brezillon75146592020-01-28 14:55:03 +0100273
274 drm_atomic_private_obj_init(bridge->dev, &bridge->base,
275 &state->base,
276 &drm_bridge_priv_state_funcs);
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200277 }
278
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530279 return 0;
Boris Brezillon75146592020-01-28 14:55:03 +0100280
281err_detach_bridge:
282 if (bridge->funcs->detach)
283 bridge->funcs->detach(bridge);
284
285err_reset_bridge:
286 bridge->dev = NULL;
287 bridge->encoder = NULL;
288 list_del(&bridge->chain_node);
Laurent Pinchartfb8d6172021-03-23 23:50:08 +0200289
290#ifdef CONFIG_OF
291 DRM_ERROR("failed to attach bridge %pOF to encoder %s: %d\n",
292 bridge->of_node, encoder->name, ret);
293#else
294 DRM_ERROR("failed to attach bridge to encoder %s: %d\n",
295 encoder->name, ret);
296#endif
297
Boris Brezillon75146592020-01-28 14:55:03 +0100298 return ret;
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530299}
300EXPORT_SYMBOL(drm_bridge_attach);
301
Andrea Merellocf3bef92016-08-25 11:04:32 +0200302void drm_bridge_detach(struct drm_bridge *bridge)
303{
304 if (WARN_ON(!bridge))
305 return;
306
307 if (WARN_ON(!bridge->dev))
308 return;
309
Boris Brezillon75146592020-01-28 14:55:03 +0100310 if (bridge->funcs->atomic_reset)
311 drm_atomic_private_obj_fini(&bridge->base);
312
Andrea Merellocf3bef92016-08-25 11:04:32 +0200313 if (bridge->funcs->detach)
314 bridge->funcs->detach(bridge);
315
Boris Brezillon05193dc2019-12-03 15:15:08 +0100316 list_del(&bridge->chain_node);
Andrea Merellocf3bef92016-08-25 11:04:32 +0200317 bridge->dev = NULL;
318}
Andrea Merellocf3bef92016-08-25 11:04:32 +0200319
320/**
Laurent Pinchart04513692020-02-26 13:24:26 +0200321 * DOC: bridge operations
Archit Taneja2331b4e2015-05-21 11:03:17 +0530322 *
Laurent Pinchart04513692020-02-26 13:24:26 +0200323 * Bridge drivers expose operations through the &drm_bridge_funcs structure.
324 * The DRM internals (atomic and CRTC helpers) use the helpers defined in
325 * drm_bridge.c to call bridge operations. Those operations are divided in
Laurent Pinchart11f6c4b2020-02-26 13:24:27 +0200326 * three big categories to support different parts of the bridge usage.
Archit Taneja2331b4e2015-05-21 11:03:17 +0530327 *
Laurent Pinchart04513692020-02-26 13:24:26 +0200328 * - The encoder-related operations support control of the bridges in the
329 * chain, and are roughly counterparts to the &drm_encoder_helper_funcs
330 * operations. They are used by the legacy CRTC and the atomic modeset
331 * helpers to perform mode validation, fixup and setting, and enable and
332 * disable the bridge automatically.
333 *
334 * The enable and disable operations are split in
335 * &drm_bridge_funcs.pre_enable, &drm_bridge_funcs.enable,
336 * &drm_bridge_funcs.disable and &drm_bridge_funcs.post_disable to provide
337 * finer-grained control.
338 *
339 * Bridge drivers may implement the legacy version of those operations, or
340 * the atomic version (prefixed with atomic\_), in which case they shall also
341 * implement the atomic state bookkeeping operations
342 * (&drm_bridge_funcs.atomic_duplicate_state,
343 * &drm_bridge_funcs.atomic_destroy_state and &drm_bridge_funcs.reset).
344 * Mixing atomic and non-atomic versions of the operations is not supported.
345 *
346 * - The bus format negotiation operations
347 * &drm_bridge_funcs.atomic_get_output_bus_fmts and
348 * &drm_bridge_funcs.atomic_get_input_bus_fmts allow bridge drivers to
349 * negotiate the formats transmitted between bridges in the chain when
350 * multiple formats are supported. Negotiation for formats is performed
351 * transparently for display drivers by the atomic modeset helpers. Only
352 * atomic versions of those operations exist, bridge drivers that need to
353 * implement them shall thus also implement the atomic version of the
354 * encoder-related operations. This feature is not supported by the legacy
355 * CRTC helpers.
Laurent Pinchart11f6c4b2020-02-26 13:24:27 +0200356 *
357 * - The connector-related operations support implementing a &drm_connector
358 * based on a chain of bridges. DRM bridges traditionally create a
359 * &drm_connector for bridges meant to be used at the end of the chain. This
360 * puts additional burden on bridge drivers, especially for bridges that may
361 * be used in the middle of a chain or at the end of it. Furthermore, it
362 * requires all operations of the &drm_connector to be handled by a single
363 * bridge, which doesn't always match the hardware architecture.
364 *
365 * To simplify bridge drivers and make the connector implementation more
366 * flexible, a new model allows bridges to unconditionally skip creation of
367 * &drm_connector and instead expose &drm_bridge_funcs operations to support
368 * an externally-implemented &drm_connector. Those operations are
369 * &drm_bridge_funcs.detect, &drm_bridge_funcs.get_modes,
370 * &drm_bridge_funcs.get_edid, &drm_bridge_funcs.hpd_notify,
371 * &drm_bridge_funcs.hpd_enable and &drm_bridge_funcs.hpd_disable. When
372 * implemented, display drivers shall create a &drm_connector instance for
373 * each chain of bridges, and implement those connector instances based on
374 * the bridge connector operations.
375 *
376 * Bridge drivers shall implement the connector-related operations for all
377 * the features that the bridge hardware support. For instance, if a bridge
378 * supports reading EDID, the &drm_bridge_funcs.get_edid shall be
379 * implemented. This however doesn't mean that the DDC lines are wired to the
380 * bridge on a particular platform, as they could also be connected to an I2C
381 * controller of the SoC. Support for the connector-related operations on the
382 * running platform is reported through the &drm_bridge.ops flags. Bridge
383 * drivers shall detect which operations they can support on the platform
384 * (usually this information is provided by ACPI or DT), and set the
385 * &drm_bridge.ops flags for all supported operations. A flag shall only be
386 * set if the corresponding &drm_bridge_funcs operation is implemented, but
387 * an implemented operation doesn't necessarily imply that the corresponding
388 * flag will be set. Display drivers shall use the &drm_bridge.ops flags to
389 * decide which bridge to delegate a connector operation to. This mechanism
390 * allows providing a single static const &drm_bridge_funcs instance in
391 * bridge drivers, improving security by storing function pointers in
392 * read-only memory.
Laurent Pincharta25b9882020-02-26 13:24:29 +0200393 *
394 * In order to ease transition, bridge drivers may support both the old and
395 * new models by making connector creation optional and implementing the
396 * connected-related bridge operations. Connector creation is then controlled
397 * by the flags argument to the drm_bridge_attach() function. Display drivers
398 * that support the new model and create connectors themselves shall set the
399 * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag, and bridge drivers shall then skip
400 * connector creation. For intermediate bridges in the chain, the flag shall
401 * be passed to the drm_bridge_attach() call for the downstream bridge.
402 * Bridge drivers that implement the new model only shall return an error
403 * from their &drm_bridge_funcs.attach handler when the
404 * %DRM_BRIDGE_ATTACH_NO_CONNECTOR flag is not set. New display drivers
405 * should use the new model, and convert the bridge drivers they use if
406 * needed, in order to gradually transition to the new model.
Archit Taneja2331b4e2015-05-21 11:03:17 +0530407 */
408
409/**
Boris Brezillonea099ad2019-12-03 15:15:05 +0100410 * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
411 * encoder chain
Archit Taneja862e6862015-05-21 11:03:16 +0530412 * @bridge: bridge control structure
413 * @mode: desired mode to be set for the bridge
414 * @adjusted_mode: updated mode that works for this bridge
415 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100416 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
Archit Taneja862e6862015-05-21 11:03:16 +0530417 * encoder chain, starting from the first bridge to the last.
418 *
419 * Note: the bridge passed should be the one closest to the encoder
420 *
421 * RETURNS:
422 * true on success, false on failure
423 */
Boris Brezillonea099ad2019-12-03 15:15:05 +0100424bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
425 const struct drm_display_mode *mode,
426 struct drm_display_mode *adjusted_mode)
Archit Taneja862e6862015-05-21 11:03:16 +0530427{
Boris Brezillon05193dc2019-12-03 15:15:08 +0100428 struct drm_encoder *encoder;
Archit Taneja862e6862015-05-21 11:03:16 +0530429
430 if (!bridge)
431 return true;
432
Boris Brezillon05193dc2019-12-03 15:15:08 +0100433 encoder = bridge->encoder;
434 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
435 if (!bridge->funcs->mode_fixup)
436 continue;
Archit Taneja862e6862015-05-21 11:03:16 +0530437
Boris Brezillon05193dc2019-12-03 15:15:08 +0100438 if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
439 return false;
440 }
Archit Taneja862e6862015-05-21 11:03:16 +0530441
Boris Brezillon05193dc2019-12-03 15:15:08 +0100442 return true;
Archit Taneja862e6862015-05-21 11:03:16 +0530443}
Boris Brezillonea099ad2019-12-03 15:15:05 +0100444EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
Archit Taneja862e6862015-05-21 11:03:16 +0530445
446/**
Boris Brezillonea099ad2019-12-03 15:15:05 +0100447 * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
448 * encoder chain.
Jose Abreub1240f82017-05-25 15:19:14 +0100449 * @bridge: bridge control structure
Laurent Pinchart12c683e2020-05-26 04:14:48 +0300450 * @info: display info against which the mode shall be validated
Jose Abreub1240f82017-05-25 15:19:14 +0100451 * @mode: desired mode to be validated
452 *
453 * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
454 * chain, starting from the first bridge to the last. If at least one bridge
455 * does not accept the mode the function returns the error code.
456 *
457 * Note: the bridge passed should be the one closest to the encoder.
458 *
459 * RETURNS:
460 * MODE_OK on success, drm_mode_status Enum error code on failure
461 */
Boris Brezillonea099ad2019-12-03 15:15:05 +0100462enum drm_mode_status
463drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
Laurent Pinchart12c683e2020-05-26 04:14:48 +0300464 const struct drm_display_info *info,
Boris Brezillonea099ad2019-12-03 15:15:05 +0100465 const struct drm_display_mode *mode)
Jose Abreub1240f82017-05-25 15:19:14 +0100466{
Boris Brezillon05193dc2019-12-03 15:15:08 +0100467 struct drm_encoder *encoder;
Jose Abreub1240f82017-05-25 15:19:14 +0100468
469 if (!bridge)
Boris Brezillon05193dc2019-12-03 15:15:08 +0100470 return MODE_OK;
Jose Abreub1240f82017-05-25 15:19:14 +0100471
Boris Brezillon05193dc2019-12-03 15:15:08 +0100472 encoder = bridge->encoder;
473 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
474 enum drm_mode_status ret;
475
476 if (!bridge->funcs->mode_valid)
477 continue;
478
Laurent Pinchart12c683e2020-05-26 04:14:48 +0300479 ret = bridge->funcs->mode_valid(bridge, info, mode);
Boris Brezillon05193dc2019-12-03 15:15:08 +0100480 if (ret != MODE_OK)
481 return ret;
482 }
Jose Abreub1240f82017-05-25 15:19:14 +0100483
Boris Brezillon05193dc2019-12-03 15:15:08 +0100484 return MODE_OK;
Jose Abreub1240f82017-05-25 15:19:14 +0100485}
Boris Brezillonea099ad2019-12-03 15:15:05 +0100486EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
Jose Abreub1240f82017-05-25 15:19:14 +0100487
488/**
Boris Brezillonea099ad2019-12-03 15:15:05 +0100489 * drm_bridge_chain_disable - disables all bridges in the encoder chain
Archit Taneja862e6862015-05-21 11:03:16 +0530490 * @bridge: bridge control structure
491 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100492 * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
Archit Taneja862e6862015-05-21 11:03:16 +0530493 * chain, starting from the last bridge to the first. These are called before
494 * calling the encoder's prepare op.
495 *
496 * Note: the bridge passed should be the one closest to the encoder
497 */
Boris Brezillonea099ad2019-12-03 15:15:05 +0100498void drm_bridge_chain_disable(struct drm_bridge *bridge)
Archit Taneja862e6862015-05-21 11:03:16 +0530499{
Boris Brezillon05193dc2019-12-03 15:15:08 +0100500 struct drm_encoder *encoder;
501 struct drm_bridge *iter;
502
Archit Taneja862e6862015-05-21 11:03:16 +0530503 if (!bridge)
504 return;
505
Boris Brezillon05193dc2019-12-03 15:15:08 +0100506 encoder = bridge->encoder;
507 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
508 if (iter->funcs->disable)
509 iter->funcs->disable(iter);
Archit Taneja862e6862015-05-21 11:03:16 +0530510
Boris Brezillon05193dc2019-12-03 15:15:08 +0100511 if (iter == bridge)
512 break;
513 }
Archit Taneja862e6862015-05-21 11:03:16 +0530514}
Boris Brezillonea099ad2019-12-03 15:15:05 +0100515EXPORT_SYMBOL(drm_bridge_chain_disable);
Archit Taneja862e6862015-05-21 11:03:16 +0530516
517/**
Boris Brezillonea099ad2019-12-03 15:15:05 +0100518 * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the
519 * encoder chain
Archit Taneja862e6862015-05-21 11:03:16 +0530520 * @bridge: bridge control structure
521 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100522 * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
Archit Taneja862e6862015-05-21 11:03:16 +0530523 * encoder chain, starting from the first bridge to the last. These are called
524 * after completing the encoder's prepare op.
525 *
526 * Note: the bridge passed should be the one closest to the encoder
527 */
Boris Brezillonea099ad2019-12-03 15:15:05 +0100528void drm_bridge_chain_post_disable(struct drm_bridge *bridge)
Archit Taneja862e6862015-05-21 11:03:16 +0530529{
Boris Brezillon05193dc2019-12-03 15:15:08 +0100530 struct drm_encoder *encoder;
531
Archit Taneja862e6862015-05-21 11:03:16 +0530532 if (!bridge)
533 return;
534
Boris Brezillon05193dc2019-12-03 15:15:08 +0100535 encoder = bridge->encoder;
536 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
537 if (bridge->funcs->post_disable)
538 bridge->funcs->post_disable(bridge);
539 }
Archit Taneja862e6862015-05-21 11:03:16 +0530540}
Boris Brezillonea099ad2019-12-03 15:15:05 +0100541EXPORT_SYMBOL(drm_bridge_chain_post_disable);
Archit Taneja862e6862015-05-21 11:03:16 +0530542
543/**
Boris Brezillonea099ad2019-12-03 15:15:05 +0100544 * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
545 * encoder chain
Archit Taneja862e6862015-05-21 11:03:16 +0530546 * @bridge: bridge control structure
Boris Brezillonea099ad2019-12-03 15:15:05 +0100547 * @mode: desired mode to be set for the encoder chain
548 * @adjusted_mode: updated mode that works for this encoder chain
Archit Taneja862e6862015-05-21 11:03:16 +0530549 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100550 * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
Archit Taneja862e6862015-05-21 11:03:16 +0530551 * encoder chain, starting from the first bridge to the last.
552 *
553 * Note: the bridge passed should be the one closest to the encoder
554 */
Boris Brezillonea099ad2019-12-03 15:15:05 +0100555void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
556 const struct drm_display_mode *mode,
557 const struct drm_display_mode *adjusted_mode)
Archit Taneja862e6862015-05-21 11:03:16 +0530558{
Boris Brezillon05193dc2019-12-03 15:15:08 +0100559 struct drm_encoder *encoder;
560
Archit Taneja862e6862015-05-21 11:03:16 +0530561 if (!bridge)
562 return;
563
Boris Brezillon05193dc2019-12-03 15:15:08 +0100564 encoder = bridge->encoder;
565 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
566 if (bridge->funcs->mode_set)
567 bridge->funcs->mode_set(bridge, mode, adjusted_mode);
568 }
Archit Taneja862e6862015-05-21 11:03:16 +0530569}
Boris Brezillonea099ad2019-12-03 15:15:05 +0100570EXPORT_SYMBOL(drm_bridge_chain_mode_set);
Archit Taneja862e6862015-05-21 11:03:16 +0530571
572/**
Boris Brezillonea099ad2019-12-03 15:15:05 +0100573 * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the
574 * encoder chain
Archit Taneja862e6862015-05-21 11:03:16 +0530575 * @bridge: bridge control structure
576 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100577 * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
Archit Taneja862e6862015-05-21 11:03:16 +0530578 * chain, starting from the last bridge to the first. These are called
579 * before calling the encoder's commit op.
580 *
581 * Note: the bridge passed should be the one closest to the encoder
582 */
Boris Brezillonea099ad2019-12-03 15:15:05 +0100583void drm_bridge_chain_pre_enable(struct drm_bridge *bridge)
Archit Taneja862e6862015-05-21 11:03:16 +0530584{
Boris Brezillon05193dc2019-12-03 15:15:08 +0100585 struct drm_encoder *encoder;
586 struct drm_bridge *iter;
587
Archit Taneja862e6862015-05-21 11:03:16 +0530588 if (!bridge)
589 return;
590
Boris Brezillon05193dc2019-12-03 15:15:08 +0100591 encoder = bridge->encoder;
592 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
593 if (iter->funcs->pre_enable)
594 iter->funcs->pre_enable(iter);
Douglas Andersonbab5cca2021-04-16 15:39:24 -0700595
596 if (iter == bridge)
597 break;
Boris Brezillon05193dc2019-12-03 15:15:08 +0100598 }
Archit Taneja862e6862015-05-21 11:03:16 +0530599}
Boris Brezillonea099ad2019-12-03 15:15:05 +0100600EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
Archit Taneja862e6862015-05-21 11:03:16 +0530601
602/**
Boris Brezillonea099ad2019-12-03 15:15:05 +0100603 * drm_bridge_chain_enable - enables all bridges in the encoder chain
Archit Taneja862e6862015-05-21 11:03:16 +0530604 * @bridge: bridge control structure
605 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100606 * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
Archit Taneja862e6862015-05-21 11:03:16 +0530607 * chain, starting from the first bridge to the last. These are called
608 * after completing the encoder's commit op.
609 *
610 * Note that the bridge passed should be the one closest to the encoder
611 */
Boris Brezillonea099ad2019-12-03 15:15:05 +0100612void drm_bridge_chain_enable(struct drm_bridge *bridge)
Archit Taneja862e6862015-05-21 11:03:16 +0530613{
Boris Brezillon05193dc2019-12-03 15:15:08 +0100614 struct drm_encoder *encoder;
615
Archit Taneja862e6862015-05-21 11:03:16 +0530616 if (!bridge)
617 return;
618
Boris Brezillon05193dc2019-12-03 15:15:08 +0100619 encoder = bridge->encoder;
620 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
621 if (bridge->funcs->enable)
622 bridge->funcs->enable(bridge);
623 }
Archit Taneja862e6862015-05-21 11:03:16 +0530624}
Boris Brezillonea099ad2019-12-03 15:15:05 +0100625EXPORT_SYMBOL(drm_bridge_chain_enable);
Archit Taneja862e6862015-05-21 11:03:16 +0530626
Sean Paul5ade0712019-06-11 12:08:17 -0400627/**
Boris Brezillonea099ad2019-12-03 15:15:05 +0100628 * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
Sean Paul5ade0712019-06-11 12:08:17 -0400629 * @bridge: bridge control structure
Boris Brezillonf3fdbc72019-12-03 15:15:11 +0100630 * @old_state: old atomic state
Sean Paul5ade0712019-06-11 12:08:17 -0400631 *
632 * Calls &drm_bridge_funcs.atomic_disable (falls back on
633 * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
634 * starting from the last bridge to the first. These are called before calling
635 * &drm_encoder_helper_funcs.atomic_disable
636 *
637 * Note: the bridge passed should be the one closest to the encoder
638 */
Boris Brezillonea099ad2019-12-03 15:15:05 +0100639void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
Boris Brezillonf3fdbc72019-12-03 15:15:11 +0100640 struct drm_atomic_state *old_state)
Sean Paul5ade0712019-06-11 12:08:17 -0400641{
Boris Brezillon05193dc2019-12-03 15:15:08 +0100642 struct drm_encoder *encoder;
643 struct drm_bridge *iter;
644
Sean Paul5ade0712019-06-11 12:08:17 -0400645 if (!bridge)
646 return;
647
Boris Brezillon05193dc2019-12-03 15:15:08 +0100648 encoder = bridge->encoder;
649 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
Boris Brezillon41cf5712020-01-28 14:55:06 +0100650 if (iter->funcs->atomic_disable) {
651 struct drm_bridge_state *old_bridge_state;
652
653 old_bridge_state =
654 drm_atomic_get_old_bridge_state(old_state,
655 iter);
656 if (WARN_ON(!old_bridge_state))
657 return;
658
659 iter->funcs->atomic_disable(iter, old_bridge_state);
660 } else if (iter->funcs->disable) {
Boris Brezillon05193dc2019-12-03 15:15:08 +0100661 iter->funcs->disable(iter);
Boris Brezillon41cf5712020-01-28 14:55:06 +0100662 }
Sean Paul5ade0712019-06-11 12:08:17 -0400663
Boris Brezillon05193dc2019-12-03 15:15:08 +0100664 if (iter == bridge)
665 break;
666 }
Sean Paul5ade0712019-06-11 12:08:17 -0400667}
Boris Brezillonea099ad2019-12-03 15:15:05 +0100668EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
Sean Paul5ade0712019-06-11 12:08:17 -0400669
670/**
Boris Brezillonea099ad2019-12-03 15:15:05 +0100671 * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
672 * in the encoder chain
Sean Paul5ade0712019-06-11 12:08:17 -0400673 * @bridge: bridge control structure
Boris Brezillonf3fdbc72019-12-03 15:15:11 +0100674 * @old_state: old atomic state
Sean Paul5ade0712019-06-11 12:08:17 -0400675 *
676 * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
677 * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
678 * starting from the first bridge to the last. These are called after completing
679 * &drm_encoder_helper_funcs.atomic_disable
680 *
681 * Note: the bridge passed should be the one closest to the encoder
682 */
Boris Brezillonea099ad2019-12-03 15:15:05 +0100683void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
Boris Brezillonf3fdbc72019-12-03 15:15:11 +0100684 struct drm_atomic_state *old_state)
Sean Paul5ade0712019-06-11 12:08:17 -0400685{
Boris Brezillon05193dc2019-12-03 15:15:08 +0100686 struct drm_encoder *encoder;
687
Sean Paul5ade0712019-06-11 12:08:17 -0400688 if (!bridge)
689 return;
690
Boris Brezillon05193dc2019-12-03 15:15:08 +0100691 encoder = bridge->encoder;
692 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
Boris Brezillon41cf5712020-01-28 14:55:06 +0100693 if (bridge->funcs->atomic_post_disable) {
694 struct drm_bridge_state *old_bridge_state;
695
696 old_bridge_state =
697 drm_atomic_get_old_bridge_state(old_state,
698 bridge);
699 if (WARN_ON(!old_bridge_state))
700 return;
701
702 bridge->funcs->atomic_post_disable(bridge,
703 old_bridge_state);
704 } else if (bridge->funcs->post_disable) {
Boris Brezillon05193dc2019-12-03 15:15:08 +0100705 bridge->funcs->post_disable(bridge);
Boris Brezillon41cf5712020-01-28 14:55:06 +0100706 }
Boris Brezillon05193dc2019-12-03 15:15:08 +0100707 }
Sean Paul5ade0712019-06-11 12:08:17 -0400708}
Boris Brezillonea099ad2019-12-03 15:15:05 +0100709EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
Sean Paul5ade0712019-06-11 12:08:17 -0400710
711/**
Boris Brezillonea099ad2019-12-03 15:15:05 +0100712 * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
713 * the encoder chain
Sean Paul5ade0712019-06-11 12:08:17 -0400714 * @bridge: bridge control structure
Boris Brezillonf3fdbc72019-12-03 15:15:11 +0100715 * @old_state: old atomic state
Sean Paul5ade0712019-06-11 12:08:17 -0400716 *
717 * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
718 * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
719 * starting from the last bridge to the first. These are called before calling
720 * &drm_encoder_helper_funcs.atomic_enable
721 *
722 * Note: the bridge passed should be the one closest to the encoder
723 */
Boris Brezillonea099ad2019-12-03 15:15:05 +0100724void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
Boris Brezillonf3fdbc72019-12-03 15:15:11 +0100725 struct drm_atomic_state *old_state)
Sean Paul5ade0712019-06-11 12:08:17 -0400726{
Boris Brezillon05193dc2019-12-03 15:15:08 +0100727 struct drm_encoder *encoder;
728 struct drm_bridge *iter;
729
Sean Paul5ade0712019-06-11 12:08:17 -0400730 if (!bridge)
731 return;
732
Boris Brezillon05193dc2019-12-03 15:15:08 +0100733 encoder = bridge->encoder;
734 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
Boris Brezillon41cf5712020-01-28 14:55:06 +0100735 if (iter->funcs->atomic_pre_enable) {
736 struct drm_bridge_state *old_bridge_state;
737
738 old_bridge_state =
739 drm_atomic_get_old_bridge_state(old_state,
740 iter);
741 if (WARN_ON(!old_bridge_state))
742 return;
743
744 iter->funcs->atomic_pre_enable(iter, old_bridge_state);
745 } else if (iter->funcs->pre_enable) {
Boris Brezillon05193dc2019-12-03 15:15:08 +0100746 iter->funcs->pre_enable(iter);
Boris Brezillon41cf5712020-01-28 14:55:06 +0100747 }
Sean Paul5ade0712019-06-11 12:08:17 -0400748
Boris Brezillon05193dc2019-12-03 15:15:08 +0100749 if (iter == bridge)
750 break;
751 }
Sean Paul5ade0712019-06-11 12:08:17 -0400752}
Boris Brezillonea099ad2019-12-03 15:15:05 +0100753EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
Sean Paul5ade0712019-06-11 12:08:17 -0400754
755/**
Boris Brezillonea099ad2019-12-03 15:15:05 +0100756 * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
Sean Paul5ade0712019-06-11 12:08:17 -0400757 * @bridge: bridge control structure
Boris Brezillonf3fdbc72019-12-03 15:15:11 +0100758 * @old_state: old atomic state
Sean Paul5ade0712019-06-11 12:08:17 -0400759 *
760 * Calls &drm_bridge_funcs.atomic_enable (falls back on
761 * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
762 * starting from the first bridge to the last. These are called after completing
763 * &drm_encoder_helper_funcs.atomic_enable
764 *
765 * Note: the bridge passed should be the one closest to the encoder
766 */
Boris Brezillonea099ad2019-12-03 15:15:05 +0100767void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
Boris Brezillonf3fdbc72019-12-03 15:15:11 +0100768 struct drm_atomic_state *old_state)
Sean Paul5ade0712019-06-11 12:08:17 -0400769{
Boris Brezillon05193dc2019-12-03 15:15:08 +0100770 struct drm_encoder *encoder;
771
Sean Paul5ade0712019-06-11 12:08:17 -0400772 if (!bridge)
773 return;
774
Boris Brezillon05193dc2019-12-03 15:15:08 +0100775 encoder = bridge->encoder;
776 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
Boris Brezillon41cf5712020-01-28 14:55:06 +0100777 if (bridge->funcs->atomic_enable) {
778 struct drm_bridge_state *old_bridge_state;
779
780 old_bridge_state =
781 drm_atomic_get_old_bridge_state(old_state,
782 bridge);
783 if (WARN_ON(!old_bridge_state))
784 return;
785
786 bridge->funcs->atomic_enable(bridge, old_bridge_state);
787 } else if (bridge->funcs->enable) {
Boris Brezillon05193dc2019-12-03 15:15:08 +0100788 bridge->funcs->enable(bridge);
Boris Brezillon41cf5712020-01-28 14:55:06 +0100789 }
Boris Brezillon05193dc2019-12-03 15:15:08 +0100790 }
Sean Paul5ade0712019-06-11 12:08:17 -0400791}
Boris Brezillonea099ad2019-12-03 15:15:05 +0100792EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
Sean Paul5ade0712019-06-11 12:08:17 -0400793
Boris Brezillon5061b8a2020-01-28 14:55:07 +0100794static int drm_atomic_bridge_check(struct drm_bridge *bridge,
795 struct drm_crtc_state *crtc_state,
796 struct drm_connector_state *conn_state)
797{
798 if (bridge->funcs->atomic_check) {
799 struct drm_bridge_state *bridge_state;
800 int ret;
801
802 bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
803 bridge);
804 if (WARN_ON(!bridge_state))
805 return -EINVAL;
806
807 ret = bridge->funcs->atomic_check(bridge, bridge_state,
808 crtc_state, conn_state);
809 if (ret)
810 return ret;
811 } else if (bridge->funcs->mode_fixup) {
812 if (!bridge->funcs->mode_fixup(bridge, &crtc_state->mode,
813 &crtc_state->adjusted_mode))
814 return -EINVAL;
815 }
816
817 return 0;
818}
819
Boris Brezillonf32df582020-01-28 14:55:08 +0100820static int select_bus_fmt_recursive(struct drm_bridge *first_bridge,
821 struct drm_bridge *cur_bridge,
822 struct drm_crtc_state *crtc_state,
823 struct drm_connector_state *conn_state,
824 u32 out_bus_fmt)
825{
826 struct drm_bridge_state *cur_state;
827 unsigned int num_in_bus_fmts, i;
828 struct drm_bridge *prev_bridge;
829 u32 *in_bus_fmts;
830 int ret;
831
832 prev_bridge = drm_bridge_get_prev_bridge(cur_bridge);
833 cur_state = drm_atomic_get_new_bridge_state(crtc_state->state,
834 cur_bridge);
835
836 /*
837 * If bus format negotiation is not supported by this bridge, let's
838 * pass MEDIA_BUS_FMT_FIXED to the previous bridge in the chain and
839 * hope that it can handle this situation gracefully (by providing
840 * appropriate default values).
841 */
842 if (!cur_bridge->funcs->atomic_get_input_bus_fmts) {
843 if (cur_bridge != first_bridge) {
844 ret = select_bus_fmt_recursive(first_bridge,
845 prev_bridge, crtc_state,
846 conn_state,
847 MEDIA_BUS_FMT_FIXED);
848 if (ret)
849 return ret;
850 }
851
852 /*
853 * Driver does not implement the atomic state hooks, but that's
854 * fine, as long as it does not access the bridge state.
855 */
856 if (cur_state) {
857 cur_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
858 cur_state->output_bus_cfg.format = out_bus_fmt;
859 }
860
861 return 0;
862 }
863
864 /*
865 * If the driver implements ->atomic_get_input_bus_fmts() it
866 * should also implement the atomic state hooks.
867 */
868 if (WARN_ON(!cur_state))
869 return -EINVAL;
870
871 in_bus_fmts = cur_bridge->funcs->atomic_get_input_bus_fmts(cur_bridge,
872 cur_state,
873 crtc_state,
874 conn_state,
875 out_bus_fmt,
876 &num_in_bus_fmts);
877 if (!num_in_bus_fmts)
878 return -ENOTSUPP;
879 else if (!in_bus_fmts)
880 return -ENOMEM;
881
882 if (first_bridge == cur_bridge) {
883 cur_state->input_bus_cfg.format = in_bus_fmts[0];
884 cur_state->output_bus_cfg.format = out_bus_fmt;
885 kfree(in_bus_fmts);
886 return 0;
887 }
888
889 for (i = 0; i < num_in_bus_fmts; i++) {
890 ret = select_bus_fmt_recursive(first_bridge, prev_bridge,
891 crtc_state, conn_state,
892 in_bus_fmts[i]);
893 if (ret != -ENOTSUPP)
894 break;
895 }
896
897 if (!ret) {
898 cur_state->input_bus_cfg.format = in_bus_fmts[i];
899 cur_state->output_bus_cfg.format = out_bus_fmt;
900 }
901
902 kfree(in_bus_fmts);
903 return ret;
904}
905
906/*
907 * This function is called by &drm_atomic_bridge_chain_check() just before
908 * calling &drm_bridge_funcs.atomic_check() on all elements of the chain.
909 * It performs bus format negotiation between bridge elements. The negotiation
910 * happens in reverse order, starting from the last element in the chain up to
911 * @bridge.
912 *
913 * Negotiation starts by retrieving supported output bus formats on the last
914 * bridge element and testing them one by one. The test is recursive, meaning
915 * that for each tested output format, the whole chain will be walked backward,
916 * and each element will have to choose an input bus format that can be
917 * transcoded to the requested output format. When a bridge element does not
918 * support transcoding into a specific output format -ENOTSUPP is returned and
919 * the next bridge element will have to try a different format. If none of the
920 * combinations worked, -ENOTSUPP is returned and the atomic modeset will fail.
921 *
922 * This implementation is relying on
923 * &drm_bridge_funcs.atomic_get_output_bus_fmts() and
924 * &drm_bridge_funcs.atomic_get_input_bus_fmts() to gather supported
925 * input/output formats.
926 *
927 * When &drm_bridge_funcs.atomic_get_output_bus_fmts() is not implemented by
928 * the last element of the chain, &drm_atomic_bridge_chain_select_bus_fmts()
929 * tries a single format: &drm_connector.display_info.bus_formats[0] if
930 * available, MEDIA_BUS_FMT_FIXED otherwise.
931 *
932 * When &drm_bridge_funcs.atomic_get_input_bus_fmts() is not implemented,
933 * &drm_atomic_bridge_chain_select_bus_fmts() skips the negotiation on the
934 * bridge element that lacks this hook and asks the previous element in the
935 * chain to try MEDIA_BUS_FMT_FIXED. It's up to bridge drivers to decide what
936 * to do in that case (fail if they want to enforce bus format negotiation, or
937 * provide a reasonable default if they need to support pipelines where not
938 * all elements support bus format negotiation).
939 */
940static int
941drm_atomic_bridge_chain_select_bus_fmts(struct drm_bridge *bridge,
942 struct drm_crtc_state *crtc_state,
943 struct drm_connector_state *conn_state)
944{
945 struct drm_connector *conn = conn_state->connector;
946 struct drm_encoder *encoder = bridge->encoder;
947 struct drm_bridge_state *last_bridge_state;
948 unsigned int i, num_out_bus_fmts;
949 struct drm_bridge *last_bridge;
950 u32 *out_bus_fmts;
951 int ret = 0;
952
953 last_bridge = list_last_entry(&encoder->bridge_chain,
954 struct drm_bridge, chain_node);
955 last_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
956 last_bridge);
957
958 if (last_bridge->funcs->atomic_get_output_bus_fmts) {
959 const struct drm_bridge_funcs *funcs = last_bridge->funcs;
960
961 /*
962 * If the driver implements ->atomic_get_output_bus_fmts() it
963 * should also implement the atomic state hooks.
964 */
965 if (WARN_ON(!last_bridge_state))
966 return -EINVAL;
967
968 out_bus_fmts = funcs->atomic_get_output_bus_fmts(last_bridge,
969 last_bridge_state,
970 crtc_state,
971 conn_state,
972 &num_out_bus_fmts);
973 if (!num_out_bus_fmts)
974 return -ENOTSUPP;
975 else if (!out_bus_fmts)
976 return -ENOMEM;
977 } else {
978 num_out_bus_fmts = 1;
979 out_bus_fmts = kmalloc(sizeof(*out_bus_fmts), GFP_KERNEL);
980 if (!out_bus_fmts)
981 return -ENOMEM;
982
983 if (conn->display_info.num_bus_formats &&
984 conn->display_info.bus_formats)
985 out_bus_fmts[0] = conn->display_info.bus_formats[0];
986 else
987 out_bus_fmts[0] = MEDIA_BUS_FMT_FIXED;
988 }
989
990 for (i = 0; i < num_out_bus_fmts; i++) {
991 ret = select_bus_fmt_recursive(bridge, last_bridge, crtc_state,
992 conn_state, out_bus_fmts[i]);
993 if (ret != -ENOTSUPP)
994 break;
995 }
996
997 kfree(out_bus_fmts);
998
999 return ret;
1000}
1001
1002static void
1003drm_atomic_bridge_propagate_bus_flags(struct drm_bridge *bridge,
1004 struct drm_connector *conn,
1005 struct drm_atomic_state *state)
1006{
1007 struct drm_bridge_state *bridge_state, *next_bridge_state;
1008 struct drm_bridge *next_bridge;
1009 u32 output_flags = 0;
1010
1011 bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
1012
1013 /* No bridge state attached to this bridge => nothing to propagate. */
1014 if (!bridge_state)
1015 return;
1016
1017 next_bridge = drm_bridge_get_next_bridge(bridge);
1018
1019 /*
1020 * Let's try to apply the most common case here, that is, propagate
1021 * display_info flags for the last bridge, and propagate the input
1022 * flags of the next bridge element to the output end of the current
1023 * bridge when the bridge is not the last one.
1024 * There are exceptions to this rule, like when signal inversion is
1025 * happening at the board level, but that's something drivers can deal
1026 * with from their &drm_bridge_funcs.atomic_check() implementation by
1027 * simply overriding the flags value we've set here.
1028 */
1029 if (!next_bridge) {
1030 output_flags = conn->display_info.bus_flags;
1031 } else {
1032 next_bridge_state = drm_atomic_get_new_bridge_state(state,
1033 next_bridge);
1034 /*
1035 * No bridge state attached to the next bridge, just leave the
1036 * flags to 0.
1037 */
1038 if (next_bridge_state)
1039 output_flags = next_bridge_state->input_bus_cfg.flags;
1040 }
1041
1042 bridge_state->output_bus_cfg.flags = output_flags;
1043
1044 /*
Cai Huoqing0ae865e2021-07-30 21:27:29 +08001045 * Propagate the output flags to the input end of the bridge. Again, it's
Boris Brezillonf32df582020-01-28 14:55:08 +01001046 * not necessarily what all bridges want, but that's what most of them
1047 * do, and by doing that by default we avoid forcing drivers to
1048 * duplicate the "dummy propagation" logic.
1049 */
1050 bridge_state->input_bus_cfg.flags = output_flags;
1051}
1052
Boris Brezillon5061b8a2020-01-28 14:55:07 +01001053/**
1054 * drm_atomic_bridge_chain_check() - Do an atomic check on the bridge chain
1055 * @bridge: bridge control structure
1056 * @crtc_state: new CRTC state
1057 * @conn_state: new connector state
1058 *
Boris Brezillonf32df582020-01-28 14:55:08 +01001059 * First trigger a bus format negotiation before calling
1060 * &drm_bridge_funcs.atomic_check() (falls back on
Boris Brezillon5061b8a2020-01-28 14:55:07 +01001061 * &drm_bridge_funcs.mode_fixup()) op for all the bridges in the encoder chain,
1062 * starting from the last bridge to the first. These are called before calling
1063 * &drm_encoder_helper_funcs.atomic_check()
1064 *
1065 * RETURNS:
1066 * 0 on success, a negative error code on failure
1067 */
1068int drm_atomic_bridge_chain_check(struct drm_bridge *bridge,
1069 struct drm_crtc_state *crtc_state,
1070 struct drm_connector_state *conn_state)
1071{
Boris Brezillonf32df582020-01-28 14:55:08 +01001072 struct drm_connector *conn = conn_state->connector;
Boris Brezillon5061b8a2020-01-28 14:55:07 +01001073 struct drm_encoder *encoder;
1074 struct drm_bridge *iter;
Boris Brezillonf32df582020-01-28 14:55:08 +01001075 int ret;
Boris Brezillon5061b8a2020-01-28 14:55:07 +01001076
1077 if (!bridge)
1078 return 0;
1079
Boris Brezillonf32df582020-01-28 14:55:08 +01001080 ret = drm_atomic_bridge_chain_select_bus_fmts(bridge, crtc_state,
1081 conn_state);
1082 if (ret)
1083 return ret;
1084
Boris Brezillon5061b8a2020-01-28 14:55:07 +01001085 encoder = bridge->encoder;
1086 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
1087 int ret;
1088
Boris Brezillonf32df582020-01-28 14:55:08 +01001089 /*
1090 * Bus flags are propagated by default. If a bridge needs to
1091 * tweak the input bus flags for any reason, it should happen
1092 * in its &drm_bridge_funcs.atomic_check() implementation such
1093 * that preceding bridges in the chain can propagate the new
1094 * bus flags.
1095 */
1096 drm_atomic_bridge_propagate_bus_flags(iter, conn,
1097 crtc_state->state);
1098
Boris Brezillon5061b8a2020-01-28 14:55:07 +01001099 ret = drm_atomic_bridge_check(iter, crtc_state, conn_state);
1100 if (ret)
1101 return ret;
1102
1103 if (iter == bridge)
1104 break;
1105 }
1106
1107 return 0;
1108}
1109EXPORT_SYMBOL(drm_atomic_bridge_chain_check);
1110
Laurent Pinchart11f6c4b2020-02-26 13:24:27 +02001111/**
1112 * drm_bridge_detect - check if anything is attached to the bridge output
1113 * @bridge: bridge control structure
1114 *
1115 * If the bridge supports output detection, as reported by the
1116 * DRM_BRIDGE_OP_DETECT bridge ops flag, call &drm_bridge_funcs.detect for the
1117 * bridge and return the connection status. Otherwise return
1118 * connector_status_unknown.
1119 *
1120 * RETURNS:
1121 * The detection status on success, or connector_status_unknown if the bridge
1122 * doesn't support output detection.
1123 */
1124enum drm_connector_status drm_bridge_detect(struct drm_bridge *bridge)
1125{
1126 if (!(bridge->ops & DRM_BRIDGE_OP_DETECT))
1127 return connector_status_unknown;
1128
1129 return bridge->funcs->detect(bridge);
1130}
1131EXPORT_SYMBOL_GPL(drm_bridge_detect);
1132
1133/**
1134 * drm_bridge_get_modes - fill all modes currently valid for the sink into the
1135 * @connector
1136 * @bridge: bridge control structure
1137 * @connector: the connector to fill with modes
1138 *
1139 * If the bridge supports output modes retrieval, as reported by the
1140 * DRM_BRIDGE_OP_MODES bridge ops flag, call &drm_bridge_funcs.get_modes to
1141 * fill the connector with all valid modes and return the number of modes
1142 * added. Otherwise return 0.
1143 *
1144 * RETURNS:
1145 * The number of modes added to the connector.
1146 */
1147int drm_bridge_get_modes(struct drm_bridge *bridge,
1148 struct drm_connector *connector)
1149{
1150 if (!(bridge->ops & DRM_BRIDGE_OP_MODES))
1151 return 0;
1152
1153 return bridge->funcs->get_modes(bridge, connector);
1154}
1155EXPORT_SYMBOL_GPL(drm_bridge_get_modes);
1156
1157/**
1158 * drm_bridge_get_edid - get the EDID data of the connected display
1159 * @bridge: bridge control structure
1160 * @connector: the connector to read EDID for
1161 *
1162 * If the bridge supports output EDID retrieval, as reported by the
1163 * DRM_BRIDGE_OP_EDID bridge ops flag, call &drm_bridge_funcs.get_edid to
Laurent Pinchart11d3cf82020-05-26 04:14:43 +03001164 * get the EDID and return it. Otherwise return NULL.
Laurent Pinchart11f6c4b2020-02-26 13:24:27 +02001165 *
1166 * RETURNS:
Laurent Pinchart11d3cf82020-05-26 04:14:43 +03001167 * The retrieved EDID on success, or NULL otherwise.
Laurent Pinchart11f6c4b2020-02-26 13:24:27 +02001168 */
1169struct edid *drm_bridge_get_edid(struct drm_bridge *bridge,
1170 struct drm_connector *connector)
1171{
1172 if (!(bridge->ops & DRM_BRIDGE_OP_EDID))
Laurent Pinchart11d3cf82020-05-26 04:14:43 +03001173 return NULL;
Laurent Pinchart11f6c4b2020-02-26 13:24:27 +02001174
1175 return bridge->funcs->get_edid(bridge, connector);
1176}
1177EXPORT_SYMBOL_GPL(drm_bridge_get_edid);
1178
1179/**
1180 * drm_bridge_hpd_enable - enable hot plug detection for the bridge
1181 * @bridge: bridge control structure
1182 * @cb: hot-plug detection callback
1183 * @data: data to be passed to the hot-plug detection callback
1184 *
1185 * Call &drm_bridge_funcs.hpd_enable if implemented and register the given @cb
1186 * and @data as hot plug notification callback. From now on the @cb will be
1187 * called with @data when an output status change is detected by the bridge,
1188 * until hot plug notification gets disabled with drm_bridge_hpd_disable().
1189 *
1190 * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1191 * bridge->ops. This function shall not be called when the flag is not set.
1192 *
1193 * Only one hot plug detection callback can be registered at a time, it is an
1194 * error to call this function when hot plug detection is already enabled for
1195 * the bridge.
1196 */
1197void drm_bridge_hpd_enable(struct drm_bridge *bridge,
1198 void (*cb)(void *data,
1199 enum drm_connector_status status),
1200 void *data)
1201{
1202 if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1203 return;
1204
1205 mutex_lock(&bridge->hpd_mutex);
1206
1207 if (WARN(bridge->hpd_cb, "Hot plug detection already enabled\n"))
1208 goto unlock;
1209
1210 bridge->hpd_cb = cb;
1211 bridge->hpd_data = data;
1212
1213 if (bridge->funcs->hpd_enable)
1214 bridge->funcs->hpd_enable(bridge);
1215
1216unlock:
1217 mutex_unlock(&bridge->hpd_mutex);
1218}
1219EXPORT_SYMBOL_GPL(drm_bridge_hpd_enable);
1220
1221/**
1222 * drm_bridge_hpd_disable - disable hot plug detection for the bridge
1223 * @bridge: bridge control structure
1224 *
1225 * Call &drm_bridge_funcs.hpd_disable if implemented and unregister the hot
1226 * plug detection callback previously registered with drm_bridge_hpd_enable().
1227 * Once this function returns the callback will not be called by the bridge
1228 * when an output status change occurs.
1229 *
1230 * Hot plug detection is supported only if the DRM_BRIDGE_OP_HPD flag is set in
1231 * bridge->ops. This function shall not be called when the flag is not set.
1232 */
1233void drm_bridge_hpd_disable(struct drm_bridge *bridge)
1234{
1235 if (!(bridge->ops & DRM_BRIDGE_OP_HPD))
1236 return;
1237
1238 mutex_lock(&bridge->hpd_mutex);
1239 if (bridge->funcs->hpd_disable)
1240 bridge->funcs->hpd_disable(bridge);
1241
1242 bridge->hpd_cb = NULL;
1243 bridge->hpd_data = NULL;
1244 mutex_unlock(&bridge->hpd_mutex);
1245}
1246EXPORT_SYMBOL_GPL(drm_bridge_hpd_disable);
1247
1248/**
1249 * drm_bridge_hpd_notify - notify hot plug detection events
1250 * @bridge: bridge control structure
1251 * @status: output connection status
1252 *
1253 * Bridge drivers shall call this function to report hot plug events when they
1254 * detect a change in the output status, when hot plug detection has been
1255 * enabled by drm_bridge_hpd_enable().
1256 *
1257 * This function shall be called in a context that can sleep.
1258 */
1259void drm_bridge_hpd_notify(struct drm_bridge *bridge,
1260 enum drm_connector_status status)
1261{
1262 mutex_lock(&bridge->hpd_mutex);
1263 if (bridge->hpd_cb)
1264 bridge->hpd_cb(bridge->hpd_data, status);
1265 mutex_unlock(&bridge->hpd_mutex);
1266}
1267EXPORT_SYMBOL_GPL(drm_bridge_hpd_notify);
1268
Ajay Kumar3d3f8b12015-01-20 22:08:44 +05301269#ifdef CONFIG_OF
Archit Taneja2331b4e2015-05-21 11:03:17 +05301270/**
1271 * of_drm_find_bridge - find the bridge corresponding to the device node in
1272 * the global bridge list
1273 *
1274 * @np: device node
1275 *
1276 * RETURNS:
1277 * drm_bridge control struct on success, NULL on failure
1278 */
Ajay Kumar3d3f8b12015-01-20 22:08:44 +05301279struct drm_bridge *of_drm_find_bridge(struct device_node *np)
1280{
1281 struct drm_bridge *bridge;
1282
1283 mutex_lock(&bridge_lock);
1284
1285 list_for_each_entry(bridge, &bridge_list, list) {
1286 if (bridge->of_node == np) {
1287 mutex_unlock(&bridge_lock);
1288 return bridge;
1289 }
1290 }
1291
1292 mutex_unlock(&bridge_lock);
1293 return NULL;
1294}
1295EXPORT_SYMBOL(of_drm_find_bridge);
Ajay Kumar3d3f8b12015-01-20 22:08:44 +05301296#endif
1297
1298MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
1299MODULE_DESCRIPTION("DRM bridge infrastructure");
1300MODULE_LICENSE("GPL and additional rights");