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