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