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