2 * Copyright (c) 2014 Samsung Electronics Co., Ltd
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:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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.
24 #include <linux/err.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
28 #include <drm/drm_bridge.h>
29 #include <drm/drm_encoder.h>
31 #include "drm_crtc_internal.h"
36 * &struct drm_bridge represents a device that hangs on to an encoder. These are
37 * handy when a regular &drm_encoder entity isn't enough to represent the entire
40 * A bridge is always attached to a single &drm_encoder at a time, but can be
41 * either connected to it directly, or through an intermediate bridge::
43 * encoder ---> bridge B ---> bridge A
45 * Here, the output of the encoder feeds to bridge B, and that furthers feeds to
48 * The driver using the bridge is responsible to make the associations between
49 * the encoder and bridges. Once these links are made, the bridges will
50 * participate along with encoder functions to perform mode_set/enable/disable
51 * through the ops provided in &drm_bridge_funcs.
53 * 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
58 * Bridges can also be chained up using the &drm_bridge.next pointer.
60 * Both legacy CRTC helpers and the new atomic modeset helpers support bridges.
63 static DEFINE_MUTEX(bridge_lock
);
64 static LIST_HEAD(bridge_list
);
67 * drm_bridge_add - add the given bridge to the global bridge list
69 * @bridge: bridge control structure
71 void drm_bridge_add(struct drm_bridge
*bridge
)
73 mutex_lock(&bridge_lock
);
74 list_add_tail(&bridge
->list
, &bridge_list
);
75 mutex_unlock(&bridge_lock
);
77 EXPORT_SYMBOL(drm_bridge_add
);
80 * drm_bridge_remove - remove the given bridge from the global bridge list
82 * @bridge: bridge control structure
84 void drm_bridge_remove(struct drm_bridge
*bridge
)
86 mutex_lock(&bridge_lock
);
87 list_del_init(&bridge
->list
);
88 mutex_unlock(&bridge_lock
);
90 EXPORT_SYMBOL(drm_bridge_remove
);
93 * drm_bridge_attach - attach the bridge to an encoder's chain
95 * @encoder: DRM encoder
96 * @bridge: bridge to attach
97 * @previous: previous bridge in the chain (optional)
99 * Called by a kms driver to link the bridge to an encoder's chain. The previous
100 * argument specifies the previous bridge in the chain. If NULL, the bridge is
101 * linked directly at the encoder's output. Otherwise it is linked at the
102 * previous bridge's output.
104 * If non-NULL the previous bridge must be already attached by a call to this
107 * Note that bridges attached to encoders are auto-detached during encoder
108 * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
109 * *not* be balanced with a drm_bridge_detach() in driver code.
112 * Zero on success, error code on failure
114 int drm_bridge_attach(struct drm_encoder
*encoder
, struct drm_bridge
*bridge
,
115 struct drm_bridge
*previous
)
119 if (!encoder
|| !bridge
)
122 if (previous
&& (!previous
->dev
|| previous
->encoder
!= encoder
))
128 bridge
->dev
= encoder
->dev
;
129 bridge
->encoder
= encoder
;
131 if (bridge
->funcs
->attach
) {
132 ret
= bridge
->funcs
->attach(bridge
);
135 bridge
->encoder
= NULL
;
141 previous
->next
= bridge
;
143 encoder
->bridge
= bridge
;
147 EXPORT_SYMBOL(drm_bridge_attach
);
149 void drm_bridge_detach(struct drm_bridge
*bridge
)
151 if (WARN_ON(!bridge
))
154 if (WARN_ON(!bridge
->dev
))
157 if (bridge
->funcs
->detach
)
158 bridge
->funcs
->detach(bridge
);
164 * DOC: bridge callbacks
166 * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM
167 * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c
168 * These helpers call a specific &drm_bridge_funcs op for all the bridges
169 * during encoder configuration.
171 * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
175 * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the
177 * @bridge: bridge control structure
178 * @mode: desired mode to be set for the bridge
179 * @adjusted_mode: updated mode that works for this bridge
181 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
182 * encoder chain, starting from the first bridge to the last.
184 * Note: the bridge passed should be the one closest to the encoder
187 * true on success, false on failure
189 bool drm_bridge_mode_fixup(struct drm_bridge
*bridge
,
190 const struct drm_display_mode
*mode
,
191 struct drm_display_mode
*adjusted_mode
)
198 if (bridge
->funcs
->mode_fixup
)
199 ret
= bridge
->funcs
->mode_fixup(bridge
, mode
, adjusted_mode
);
201 ret
= ret
&& drm_bridge_mode_fixup(bridge
->next
, mode
, adjusted_mode
);
205 EXPORT_SYMBOL(drm_bridge_mode_fixup
);
208 * drm_bridge_mode_valid - validate the mode against all bridges in the
210 * @bridge: bridge control structure
211 * @mode: desired mode to be validated
213 * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
214 * chain, starting from the first bridge to the last. If at least one bridge
215 * does not accept the mode the function returns the error code.
217 * Note: the bridge passed should be the one closest to the encoder.
220 * MODE_OK on success, drm_mode_status Enum error code on failure
222 enum drm_mode_status
drm_bridge_mode_valid(struct drm_bridge
*bridge
,
223 const struct drm_display_mode
*mode
)
225 enum drm_mode_status ret
= MODE_OK
;
230 if (bridge
->funcs
->mode_valid
)
231 ret
= bridge
->funcs
->mode_valid(bridge
, mode
);
236 return drm_bridge_mode_valid(bridge
->next
, mode
);
238 EXPORT_SYMBOL(drm_bridge_mode_valid
);
241 * drm_bridge_disable - disables all bridges in the encoder chain
242 * @bridge: bridge control structure
244 * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
245 * chain, starting from the last bridge to the first. These are called before
246 * calling the encoder's prepare op.
248 * Note: the bridge passed should be the one closest to the encoder
250 void drm_bridge_disable(struct drm_bridge
*bridge
)
255 drm_bridge_disable(bridge
->next
);
257 if (bridge
->funcs
->disable
)
258 bridge
->funcs
->disable(bridge
);
260 EXPORT_SYMBOL(drm_bridge_disable
);
263 * drm_bridge_post_disable - cleans up after disabling all bridges in the encoder chain
264 * @bridge: bridge control structure
266 * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
267 * encoder chain, starting from the first bridge to the last. These are called
268 * after completing the encoder's prepare op.
270 * Note: the bridge passed should be the one closest to the encoder
272 void drm_bridge_post_disable(struct drm_bridge
*bridge
)
277 if (bridge
->funcs
->post_disable
)
278 bridge
->funcs
->post_disable(bridge
);
280 drm_bridge_post_disable(bridge
->next
);
282 EXPORT_SYMBOL(drm_bridge_post_disable
);
285 * drm_bridge_mode_set - set proposed mode for all bridges in the
287 * @bridge: bridge control structure
288 * @mode: desired mode to be set for the bridge
289 * @adjusted_mode: updated mode that works for this bridge
291 * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
292 * encoder chain, starting from the first bridge to the last.
294 * Note: the bridge passed should be the one closest to the encoder
296 void drm_bridge_mode_set(struct drm_bridge
*bridge
,
297 const struct drm_display_mode
*mode
,
298 const struct drm_display_mode
*adjusted_mode
)
303 if (bridge
->funcs
->mode_set
)
304 bridge
->funcs
->mode_set(bridge
, mode
, adjusted_mode
);
306 drm_bridge_mode_set(bridge
->next
, mode
, adjusted_mode
);
308 EXPORT_SYMBOL(drm_bridge_mode_set
);
311 * drm_bridge_pre_enable - prepares for enabling all
312 * bridges in the encoder chain
313 * @bridge: bridge control structure
315 * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
316 * chain, starting from the last bridge to the first. These are called
317 * before calling the encoder's commit op.
319 * Note: the bridge passed should be the one closest to the encoder
321 void drm_bridge_pre_enable(struct drm_bridge
*bridge
)
326 drm_bridge_pre_enable(bridge
->next
);
328 if (bridge
->funcs
->pre_enable
)
329 bridge
->funcs
->pre_enable(bridge
);
331 EXPORT_SYMBOL(drm_bridge_pre_enable
);
334 * drm_bridge_enable - enables all bridges in the encoder chain
335 * @bridge: bridge control structure
337 * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
338 * chain, starting from the first bridge to the last. These are called
339 * after completing the encoder's commit op.
341 * Note that the bridge passed should be the one closest to the encoder
343 void drm_bridge_enable(struct drm_bridge
*bridge
)
348 if (bridge
->funcs
->enable
)
349 bridge
->funcs
->enable(bridge
);
351 drm_bridge_enable(bridge
->next
);
353 EXPORT_SYMBOL(drm_bridge_enable
);
357 * of_drm_find_bridge - find the bridge corresponding to the device node in
358 * the global bridge list
363 * drm_bridge control struct on success, NULL on failure
365 struct drm_bridge
*of_drm_find_bridge(struct device_node
*np
)
367 struct drm_bridge
*bridge
;
369 mutex_lock(&bridge_lock
);
371 list_for_each_entry(bridge
, &bridge_list
, list
) {
372 if (bridge
->of_node
== np
) {
373 mutex_unlock(&bridge_lock
);
378 mutex_unlock(&bridge_lock
);
381 EXPORT_SYMBOL(of_drm_find_bridge
);
384 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
385 MODULE_DESCRIPTION("DRM bridge infrastructure");
386 MODULE_LICENSE("GPL and additional rights");