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