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