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