drm_bridge.c revision 1.4 1 1.4 jmcneill /* $NetBSD: drm_bridge.c,v 1.4 2020/01/03 21:01:16 jmcneill Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Copyright (c) 2014 Samsung Electronics Co., Ltd
5 1.1 riastrad *
6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
7 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
8 1.1 riastrad * to deal in the Software without restriction, including without limitation
9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sub license,
10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
11 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
12 1.1 riastrad *
13 1.1 riastrad * The above copyright notice and this permission notice (including the
14 1.1 riastrad * next paragraph) shall be included in all copies or substantial portions
15 1.1 riastrad * of the Software.
16 1.1 riastrad *
17 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 1.1 riastrad * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 1.1 riastrad * DEALINGS IN THE SOFTWARE.
24 1.1 riastrad */
25 1.1 riastrad
26 1.1 riastrad #include <sys/cdefs.h>
27 1.4 jmcneill __KERNEL_RCSID(0, "$NetBSD: drm_bridge.c,v 1.4 2020/01/03 21:01:16 jmcneill Exp $");
28 1.1 riastrad
29 1.1 riastrad #include <linux/err.h>
30 1.1 riastrad #include <linux/module.h>
31 1.1 riastrad
32 1.1 riastrad #include <drm/drm_crtc.h>
33 1.1 riastrad
34 1.1 riastrad #include "drm/drmP.h"
35 1.1 riastrad
36 1.1 riastrad /**
37 1.1 riastrad * DOC: overview
38 1.1 riastrad *
39 1.1 riastrad * drm_bridge represents a device that hangs on to an encoder. These are handy
40 1.1 riastrad * when a regular drm_encoder entity isn't enough to represent the entire
41 1.1 riastrad * encoder chain.
42 1.1 riastrad *
43 1.1 riastrad * A bridge is always associated to a single drm_encoder at a time, but can be
44 1.1 riastrad * either connected to it directly, or through an intermediate bridge:
45 1.1 riastrad *
46 1.1 riastrad * encoder ---> bridge B ---> bridge A
47 1.1 riastrad *
48 1.1 riastrad * Here, the output of the encoder feeds to bridge B, and that furthers feeds to
49 1.1 riastrad * bridge A.
50 1.1 riastrad *
51 1.1 riastrad * The driver using the bridge is responsible to make the associations between
52 1.1 riastrad * the encoder and bridges. Once these links are made, the bridges will
53 1.1 riastrad * participate along with encoder functions to perform mode_set/enable/disable
54 1.1 riastrad * through the ops provided in drm_bridge_funcs.
55 1.1 riastrad *
56 1.1 riastrad * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes,
57 1.1 riastrad * crtcs, encoders or connectors. They just provide additional hooks to get the
58 1.1 riastrad * desired output at the end of the encoder chain.
59 1.1 riastrad */
60 1.1 riastrad
61 1.3 riastrad #ifdef __NetBSD__
62 1.3 riastrad static struct mutex bridge_lock;
63 1.3 riastrad static struct list_head bridge_list = LIST_HEAD_INIT(bridge_list);
64 1.3 riastrad #else
65 1.1 riastrad static DEFINE_MUTEX(bridge_lock);
66 1.1 riastrad static LIST_HEAD(bridge_list);
67 1.3 riastrad #endif
68 1.1 riastrad
69 1.4 jmcneill #ifdef __NetBSD__
70 1.4 jmcneill void drm_bridge_init_lock(void)
71 1.4 jmcneill {
72 1.4 jmcneill linux_mutex_init(&bridge_lock);
73 1.4 jmcneill }
74 1.4 jmcneill void drm_bridge_fini_lock(void)
75 1.4 jmcneill {
76 1.4 jmcneill linux_mutex_destroy(&bridge_lock);
77 1.4 jmcneill }
78 1.4 jmcneill #endif
79 1.4 jmcneill
80 1.1 riastrad /**
81 1.1 riastrad * drm_bridge_add - add the given bridge to the global bridge list
82 1.1 riastrad *
83 1.1 riastrad * @bridge: bridge control structure
84 1.1 riastrad *
85 1.1 riastrad * RETURNS:
86 1.1 riastrad * Unconditionally returns Zero.
87 1.1 riastrad */
88 1.1 riastrad int drm_bridge_add(struct drm_bridge *bridge)
89 1.1 riastrad {
90 1.1 riastrad mutex_lock(&bridge_lock);
91 1.1 riastrad list_add_tail(&bridge->list, &bridge_list);
92 1.1 riastrad mutex_unlock(&bridge_lock);
93 1.1 riastrad
94 1.1 riastrad return 0;
95 1.1 riastrad }
96 1.1 riastrad EXPORT_SYMBOL(drm_bridge_add);
97 1.1 riastrad
98 1.1 riastrad /**
99 1.1 riastrad * drm_bridge_remove - remove the given bridge from the global bridge list
100 1.1 riastrad *
101 1.1 riastrad * @bridge: bridge control structure
102 1.1 riastrad */
103 1.1 riastrad void drm_bridge_remove(struct drm_bridge *bridge)
104 1.1 riastrad {
105 1.1 riastrad mutex_lock(&bridge_lock);
106 1.1 riastrad list_del_init(&bridge->list);
107 1.1 riastrad mutex_unlock(&bridge_lock);
108 1.1 riastrad }
109 1.1 riastrad EXPORT_SYMBOL(drm_bridge_remove);
110 1.1 riastrad
111 1.1 riastrad /**
112 1.1 riastrad * drm_bridge_attach - associate given bridge to our DRM device
113 1.1 riastrad *
114 1.1 riastrad * @dev: DRM device
115 1.1 riastrad * @bridge: bridge control structure
116 1.1 riastrad *
117 1.1 riastrad * called by a kms driver to link one of our encoder/bridge to the given
118 1.1 riastrad * bridge.
119 1.1 riastrad *
120 1.1 riastrad * Note that setting up links between the bridge and our encoder/bridge
121 1.1 riastrad * objects needs to be handled by the kms driver itself
122 1.1 riastrad *
123 1.1 riastrad * RETURNS:
124 1.1 riastrad * Zero on success, error code on failure
125 1.1 riastrad */
126 1.1 riastrad int drm_bridge_attach(struct drm_device *dev, struct drm_bridge *bridge)
127 1.1 riastrad {
128 1.1 riastrad if (!dev || !bridge)
129 1.1 riastrad return -EINVAL;
130 1.1 riastrad
131 1.1 riastrad if (bridge->dev)
132 1.1 riastrad return -EBUSY;
133 1.1 riastrad
134 1.1 riastrad bridge->dev = dev;
135 1.1 riastrad
136 1.1 riastrad if (bridge->funcs->attach)
137 1.1 riastrad return bridge->funcs->attach(bridge);
138 1.1 riastrad
139 1.1 riastrad return 0;
140 1.1 riastrad }
141 1.1 riastrad EXPORT_SYMBOL(drm_bridge_attach);
142 1.1 riastrad
143 1.1 riastrad /**
144 1.1 riastrad * DOC: bridge callbacks
145 1.1 riastrad *
146 1.1 riastrad * The drm_bridge_funcs ops are populated by the bridge driver. The drm
147 1.1 riastrad * internals(atomic and crtc helpers) use the helpers defined in drm_bridge.c
148 1.1 riastrad * These helpers call a specific drm_bridge_funcs op for all the bridges
149 1.1 riastrad * during encoder configuration.
150 1.1 riastrad *
151 1.1 riastrad * When creating a bridge driver, one can implement drm_bridge_funcs op with
152 1.1 riastrad * the help of these rough rules:
153 1.1 riastrad *
154 1.1 riastrad * pre_enable: this contains things needed to be done for the bridge before
155 1.1 riastrad * its clock and timings are enabled by its source. For a bridge, its source
156 1.1 riastrad * is generally the encoder or bridge just before it in the encoder chain.
157 1.1 riastrad *
158 1.1 riastrad * enable: this contains things needed to be done for the bridge once its
159 1.1 riastrad * source is enabled. In other words, enable is called once the source is
160 1.1 riastrad * ready with clock and timing needed by the bridge.
161 1.1 riastrad *
162 1.1 riastrad * disable: this contains things needed to be done for the bridge assuming
163 1.1 riastrad * that its source is still enabled, i.e. clock and timings are still on.
164 1.1 riastrad *
165 1.1 riastrad * post_disable: this contains things needed to be done for the bridge once
166 1.1 riastrad * its source is disabled, i.e. once clocks and timings are off.
167 1.1 riastrad *
168 1.1 riastrad * mode_fixup: this should fixup the given mode for the bridge. It is called
169 1.1 riastrad * after the encoder's mode fixup. mode_fixup can also reject a mode completely
170 1.1 riastrad * if it's unsuitable for the hardware.
171 1.1 riastrad *
172 1.1 riastrad * mode_set: this sets up the mode for the bridge. It assumes that its source
173 1.1 riastrad * (an encoder or a bridge) has set the mode too.
174 1.1 riastrad */
175 1.1 riastrad
176 1.1 riastrad /**
177 1.1 riastrad * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the
178 1.1 riastrad * encoder chain
179 1.1 riastrad * @bridge: bridge control structure
180 1.1 riastrad * @mode: desired mode to be set for the bridge
181 1.1 riastrad * @adjusted_mode: updated mode that works for this bridge
182 1.1 riastrad *
183 1.1 riastrad * Calls 'mode_fixup' drm_bridge_funcs op for all the bridges in the
184 1.1 riastrad * encoder chain, starting from the first bridge to the last.
185 1.1 riastrad *
186 1.1 riastrad * Note: the bridge passed should be the one closest to the encoder
187 1.1 riastrad *
188 1.1 riastrad * RETURNS:
189 1.1 riastrad * true on success, false on failure
190 1.1 riastrad */
191 1.1 riastrad bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
192 1.1 riastrad const struct drm_display_mode *mode,
193 1.1 riastrad struct drm_display_mode *adjusted_mode)
194 1.1 riastrad {
195 1.1 riastrad bool ret = true;
196 1.1 riastrad
197 1.1 riastrad if (!bridge)
198 1.1 riastrad return true;
199 1.1 riastrad
200 1.1 riastrad if (bridge->funcs->mode_fixup)
201 1.1 riastrad ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode);
202 1.1 riastrad
203 1.1 riastrad ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode);
204 1.1 riastrad
205 1.1 riastrad return ret;
206 1.1 riastrad }
207 1.1 riastrad EXPORT_SYMBOL(drm_bridge_mode_fixup);
208 1.1 riastrad
209 1.1 riastrad /**
210 1.1 riastrad * drm_bridge_disable - calls 'disable' drm_bridge_funcs op for all
211 1.1 riastrad * bridges in the encoder chain.
212 1.1 riastrad * @bridge: bridge control structure
213 1.1 riastrad *
214 1.1 riastrad * Calls 'disable' drm_bridge_funcs op for all the bridges in the encoder
215 1.1 riastrad * chain, starting from the last bridge to the first. These are called before
216 1.1 riastrad * calling the encoder's prepare op.
217 1.1 riastrad *
218 1.1 riastrad * Note: the bridge passed should be the one closest to the encoder
219 1.1 riastrad */
220 1.1 riastrad void drm_bridge_disable(struct drm_bridge *bridge)
221 1.1 riastrad {
222 1.1 riastrad if (!bridge)
223 1.1 riastrad return;
224 1.1 riastrad
225 1.1 riastrad drm_bridge_disable(bridge->next);
226 1.1 riastrad
227 1.1 riastrad bridge->funcs->disable(bridge);
228 1.1 riastrad }
229 1.1 riastrad EXPORT_SYMBOL(drm_bridge_disable);
230 1.1 riastrad
231 1.1 riastrad /**
232 1.1 riastrad * drm_bridge_post_disable - calls 'post_disable' drm_bridge_funcs op for
233 1.1 riastrad * all bridges in the encoder chain.
234 1.1 riastrad * @bridge: bridge control structure
235 1.1 riastrad *
236 1.1 riastrad * Calls 'post_disable' drm_bridge_funcs op for all the bridges in the
237 1.1 riastrad * encoder chain, starting from the first bridge to the last. These are called
238 1.1 riastrad * after completing the encoder's prepare op.
239 1.1 riastrad *
240 1.1 riastrad * Note: the bridge passed should be the one closest to the encoder
241 1.1 riastrad */
242 1.1 riastrad void drm_bridge_post_disable(struct drm_bridge *bridge)
243 1.1 riastrad {
244 1.1 riastrad if (!bridge)
245 1.1 riastrad return;
246 1.1 riastrad
247 1.1 riastrad bridge->funcs->post_disable(bridge);
248 1.1 riastrad
249 1.1 riastrad drm_bridge_post_disable(bridge->next);
250 1.1 riastrad }
251 1.1 riastrad EXPORT_SYMBOL(drm_bridge_post_disable);
252 1.1 riastrad
253 1.1 riastrad /**
254 1.1 riastrad * drm_bridge_mode_set - set proposed mode for all bridges in the
255 1.1 riastrad * encoder chain
256 1.1 riastrad * @bridge: bridge control structure
257 1.1 riastrad * @mode: desired mode to be set for the bridge
258 1.1 riastrad * @adjusted_mode: updated mode that works for this bridge
259 1.1 riastrad *
260 1.1 riastrad * Calls 'mode_set' drm_bridge_funcs op for all the bridges in the
261 1.1 riastrad * encoder chain, starting from the first bridge to the last.
262 1.1 riastrad *
263 1.1 riastrad * Note: the bridge passed should be the one closest to the encoder
264 1.1 riastrad */
265 1.1 riastrad void drm_bridge_mode_set(struct drm_bridge *bridge,
266 1.1 riastrad struct drm_display_mode *mode,
267 1.1 riastrad struct drm_display_mode *adjusted_mode)
268 1.1 riastrad {
269 1.1 riastrad if (!bridge)
270 1.1 riastrad return;
271 1.1 riastrad
272 1.1 riastrad if (bridge->funcs->mode_set)
273 1.1 riastrad bridge->funcs->mode_set(bridge, mode, adjusted_mode);
274 1.1 riastrad
275 1.1 riastrad drm_bridge_mode_set(bridge->next, mode, adjusted_mode);
276 1.1 riastrad }
277 1.1 riastrad EXPORT_SYMBOL(drm_bridge_mode_set);
278 1.1 riastrad
279 1.1 riastrad /**
280 1.1 riastrad * drm_bridge_pre_enable - calls 'pre_enable' drm_bridge_funcs op for all
281 1.1 riastrad * bridges in the encoder chain.
282 1.1 riastrad * @bridge: bridge control structure
283 1.1 riastrad *
284 1.1 riastrad * Calls 'pre_enable' drm_bridge_funcs op for all the bridges in the encoder
285 1.1 riastrad * chain, starting from the last bridge to the first. These are called
286 1.1 riastrad * before calling the encoder's commit op.
287 1.1 riastrad *
288 1.1 riastrad * Note: the bridge passed should be the one closest to the encoder
289 1.1 riastrad */
290 1.1 riastrad void drm_bridge_pre_enable(struct drm_bridge *bridge)
291 1.1 riastrad {
292 1.1 riastrad if (!bridge)
293 1.1 riastrad return;
294 1.1 riastrad
295 1.1 riastrad drm_bridge_pre_enable(bridge->next);
296 1.1 riastrad
297 1.1 riastrad bridge->funcs->pre_enable(bridge);
298 1.1 riastrad }
299 1.1 riastrad EXPORT_SYMBOL(drm_bridge_pre_enable);
300 1.1 riastrad
301 1.1 riastrad /**
302 1.1 riastrad * drm_bridge_enable - calls 'enable' drm_bridge_funcs op for all bridges
303 1.1 riastrad * in the encoder chain.
304 1.1 riastrad * @bridge: bridge control structure
305 1.1 riastrad *
306 1.1 riastrad * Calls 'enable' drm_bridge_funcs op for all the bridges in the encoder
307 1.1 riastrad * chain, starting from the first bridge to the last. These are called
308 1.1 riastrad * after completing the encoder's commit op.
309 1.1 riastrad *
310 1.1 riastrad * Note that the bridge passed should be the one closest to the encoder
311 1.1 riastrad */
312 1.1 riastrad void drm_bridge_enable(struct drm_bridge *bridge)
313 1.1 riastrad {
314 1.1 riastrad if (!bridge)
315 1.1 riastrad return;
316 1.1 riastrad
317 1.1 riastrad bridge->funcs->enable(bridge);
318 1.1 riastrad
319 1.1 riastrad drm_bridge_enable(bridge->next);
320 1.1 riastrad }
321 1.1 riastrad EXPORT_SYMBOL(drm_bridge_enable);
322 1.1 riastrad
323 1.1 riastrad #ifdef CONFIG_OF
324 1.1 riastrad /**
325 1.1 riastrad * of_drm_find_bridge - find the bridge corresponding to the device node in
326 1.1 riastrad * the global bridge list
327 1.1 riastrad *
328 1.1 riastrad * @np: device node
329 1.1 riastrad *
330 1.1 riastrad * RETURNS:
331 1.1 riastrad * drm_bridge control struct on success, NULL on failure
332 1.1 riastrad */
333 1.1 riastrad struct drm_bridge *of_drm_find_bridge(struct device_node *np)
334 1.1 riastrad {
335 1.1 riastrad struct drm_bridge *bridge;
336 1.1 riastrad
337 1.1 riastrad mutex_lock(&bridge_lock);
338 1.1 riastrad
339 1.1 riastrad list_for_each_entry(bridge, &bridge_list, list) {
340 1.1 riastrad if (bridge->of_node == np) {
341 1.1 riastrad mutex_unlock(&bridge_lock);
342 1.1 riastrad return bridge;
343 1.1 riastrad }
344 1.1 riastrad }
345 1.1 riastrad
346 1.1 riastrad mutex_unlock(&bridge_lock);
347 1.1 riastrad return NULL;
348 1.1 riastrad }
349 1.1 riastrad EXPORT_SYMBOL(of_drm_find_bridge);
350 1.1 riastrad #endif
351 1.1 riastrad
352 1.1 riastrad MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs (at) samsung.com>");
353 1.1 riastrad MODULE_DESCRIPTION("DRM bridge infrastructure");
354 1.1 riastrad MODULE_LICENSE("GPL and additional rights");
355