drm_bridge.c revision 1.3 1 /* $NetBSD: drm_bridge.c,v 1.3 2018/08/27 06:43:47 riastradh 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.3 2018/08/27 06:43:47 riastradh 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 /**
70 * drm_bridge_add - add the given bridge to the global bridge list
71 *
72 * @bridge: bridge control structure
73 *
74 * RETURNS:
75 * Unconditionally returns Zero.
76 */
77 int drm_bridge_add(struct drm_bridge *bridge)
78 {
79 mutex_lock(&bridge_lock);
80 list_add_tail(&bridge->list, &bridge_list);
81 mutex_unlock(&bridge_lock);
82
83 return 0;
84 }
85 EXPORT_SYMBOL(drm_bridge_add);
86
87 /**
88 * drm_bridge_remove - remove the given bridge from the global bridge list
89 *
90 * @bridge: bridge control structure
91 */
92 void drm_bridge_remove(struct drm_bridge *bridge)
93 {
94 mutex_lock(&bridge_lock);
95 list_del_init(&bridge->list);
96 mutex_unlock(&bridge_lock);
97 }
98 EXPORT_SYMBOL(drm_bridge_remove);
99
100 /**
101 * drm_bridge_attach - associate given bridge to our DRM device
102 *
103 * @dev: DRM device
104 * @bridge: bridge control structure
105 *
106 * called by a kms driver to link one of our encoder/bridge to the given
107 * bridge.
108 *
109 * Note that setting up links between the bridge and our encoder/bridge
110 * objects needs to be handled by the kms driver itself
111 *
112 * RETURNS:
113 * Zero on success, error code on failure
114 */
115 int drm_bridge_attach(struct drm_device *dev, struct drm_bridge *bridge)
116 {
117 if (!dev || !bridge)
118 return -EINVAL;
119
120 if (bridge->dev)
121 return -EBUSY;
122
123 bridge->dev = dev;
124
125 if (bridge->funcs->attach)
126 return bridge->funcs->attach(bridge);
127
128 return 0;
129 }
130 EXPORT_SYMBOL(drm_bridge_attach);
131
132 /**
133 * DOC: bridge callbacks
134 *
135 * The drm_bridge_funcs ops are populated by the bridge driver. The drm
136 * internals(atomic and crtc helpers) use the helpers defined in drm_bridge.c
137 * These helpers call a specific drm_bridge_funcs op for all the bridges
138 * during encoder configuration.
139 *
140 * When creating a bridge driver, one can implement drm_bridge_funcs op with
141 * the help of these rough rules:
142 *
143 * pre_enable: this contains things needed to be done for the bridge before
144 * its clock and timings are enabled by its source. For a bridge, its source
145 * is generally the encoder or bridge just before it in the encoder chain.
146 *
147 * enable: this contains things needed to be done for the bridge once its
148 * source is enabled. In other words, enable is called once the source is
149 * ready with clock and timing needed by the bridge.
150 *
151 * disable: this contains things needed to be done for the bridge assuming
152 * that its source is still enabled, i.e. clock and timings are still on.
153 *
154 * post_disable: this contains things needed to be done for the bridge once
155 * its source is disabled, i.e. once clocks and timings are off.
156 *
157 * mode_fixup: this should fixup the given mode for the bridge. It is called
158 * after the encoder's mode fixup. mode_fixup can also reject a mode completely
159 * if it's unsuitable for the hardware.
160 *
161 * mode_set: this sets up the mode for the bridge. It assumes that its source
162 * (an encoder or a bridge) has set the mode too.
163 */
164
165 /**
166 * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the
167 * encoder chain
168 * @bridge: bridge control structure
169 * @mode: desired mode to be set for the bridge
170 * @adjusted_mode: updated mode that works for this bridge
171 *
172 * Calls 'mode_fixup' drm_bridge_funcs op for all the bridges in the
173 * encoder chain, starting from the first bridge to the last.
174 *
175 * Note: the bridge passed should be the one closest to the encoder
176 *
177 * RETURNS:
178 * true on success, false on failure
179 */
180 bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
181 const struct drm_display_mode *mode,
182 struct drm_display_mode *adjusted_mode)
183 {
184 bool ret = true;
185
186 if (!bridge)
187 return true;
188
189 if (bridge->funcs->mode_fixup)
190 ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode);
191
192 ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode);
193
194 return ret;
195 }
196 EXPORT_SYMBOL(drm_bridge_mode_fixup);
197
198 /**
199 * drm_bridge_disable - calls 'disable' drm_bridge_funcs op for all
200 * bridges in the encoder chain.
201 * @bridge: bridge control structure
202 *
203 * Calls 'disable' drm_bridge_funcs op for all the bridges in the encoder
204 * chain, starting from the last bridge to the first. These are called before
205 * calling the encoder's prepare op.
206 *
207 * Note: the bridge passed should be the one closest to the encoder
208 */
209 void drm_bridge_disable(struct drm_bridge *bridge)
210 {
211 if (!bridge)
212 return;
213
214 drm_bridge_disable(bridge->next);
215
216 bridge->funcs->disable(bridge);
217 }
218 EXPORT_SYMBOL(drm_bridge_disable);
219
220 /**
221 * drm_bridge_post_disable - calls 'post_disable' drm_bridge_funcs op for
222 * all bridges in the encoder chain.
223 * @bridge: bridge control structure
224 *
225 * Calls 'post_disable' drm_bridge_funcs op for all the bridges in the
226 * encoder chain, starting from the first bridge to the last. These are called
227 * after completing the encoder's prepare op.
228 *
229 * Note: the bridge passed should be the one closest to the encoder
230 */
231 void drm_bridge_post_disable(struct drm_bridge *bridge)
232 {
233 if (!bridge)
234 return;
235
236 bridge->funcs->post_disable(bridge);
237
238 drm_bridge_post_disable(bridge->next);
239 }
240 EXPORT_SYMBOL(drm_bridge_post_disable);
241
242 /**
243 * drm_bridge_mode_set - set proposed mode for all bridges in the
244 * encoder chain
245 * @bridge: bridge control structure
246 * @mode: desired mode to be set for the bridge
247 * @adjusted_mode: updated mode that works for this bridge
248 *
249 * Calls 'mode_set' drm_bridge_funcs op for all the bridges in the
250 * encoder chain, starting from the first bridge to the last.
251 *
252 * Note: the bridge passed should be the one closest to the encoder
253 */
254 void drm_bridge_mode_set(struct drm_bridge *bridge,
255 struct drm_display_mode *mode,
256 struct drm_display_mode *adjusted_mode)
257 {
258 if (!bridge)
259 return;
260
261 if (bridge->funcs->mode_set)
262 bridge->funcs->mode_set(bridge, mode, adjusted_mode);
263
264 drm_bridge_mode_set(bridge->next, mode, adjusted_mode);
265 }
266 EXPORT_SYMBOL(drm_bridge_mode_set);
267
268 /**
269 * drm_bridge_pre_enable - calls 'pre_enable' drm_bridge_funcs op for all
270 * bridges in the encoder chain.
271 * @bridge: bridge control structure
272 *
273 * Calls 'pre_enable' drm_bridge_funcs op for all the bridges in the encoder
274 * chain, starting from the last bridge to the first. These are called
275 * before calling the encoder's commit op.
276 *
277 * Note: the bridge passed should be the one closest to the encoder
278 */
279 void drm_bridge_pre_enable(struct drm_bridge *bridge)
280 {
281 if (!bridge)
282 return;
283
284 drm_bridge_pre_enable(bridge->next);
285
286 bridge->funcs->pre_enable(bridge);
287 }
288 EXPORT_SYMBOL(drm_bridge_pre_enable);
289
290 /**
291 * drm_bridge_enable - calls 'enable' drm_bridge_funcs op for all bridges
292 * in the encoder chain.
293 * @bridge: bridge control structure
294 *
295 * Calls 'enable' drm_bridge_funcs op for all the bridges in the encoder
296 * chain, starting from the first bridge to the last. These are called
297 * after completing the encoder's commit op.
298 *
299 * Note that the bridge passed should be the one closest to the encoder
300 */
301 void drm_bridge_enable(struct drm_bridge *bridge)
302 {
303 if (!bridge)
304 return;
305
306 bridge->funcs->enable(bridge);
307
308 drm_bridge_enable(bridge->next);
309 }
310 EXPORT_SYMBOL(drm_bridge_enable);
311
312 #ifdef CONFIG_OF
313 /**
314 * of_drm_find_bridge - find the bridge corresponding to the device node in
315 * the global bridge list
316 *
317 * @np: device node
318 *
319 * RETURNS:
320 * drm_bridge control struct on success, NULL on failure
321 */
322 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
323 {
324 struct drm_bridge *bridge;
325
326 mutex_lock(&bridge_lock);
327
328 list_for_each_entry(bridge, &bridge_list, list) {
329 if (bridge->of_node == np) {
330 mutex_unlock(&bridge_lock);
331 return bridge;
332 }
333 }
334
335 mutex_unlock(&bridge_lock);
336 return NULL;
337 }
338 EXPORT_SYMBOL(of_drm_find_bridge);
339 #endif
340
341 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs (at) samsung.com>");
342 MODULE_DESCRIPTION("DRM bridge infrastructure");
343 MODULE_LICENSE("GPL and additional rights");
344