xref: /openbmc/linux/drivers/gpu/drm/drm_bridge.c (revision 8622a0e5)
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 #include <linux/mutex.h>
27 
28 #include <drm/drm_bridge.h>
29 #include <drm/drm_encoder.h>
30 
31 #include "drm_crtc_internal.h"
32 
33 /**
34  * DOC: overview
35  *
36  * &struct drm_bridge represents a device that hangs on to an encoder. These are
37  * handy when a regular &drm_encoder entity isn't enough to represent the entire
38  * encoder chain.
39  *
40  * A bridge is always attached to a single &drm_encoder at a time, but can be
41  * either connected to it directly, or through an intermediate bridge::
42  *
43  *     encoder ---> bridge B ---> bridge A
44  *
45  * Here, the output of the encoder feeds to bridge B, and that furthers feeds to
46  * bridge A.
47  *
48  * The driver using the bridge is responsible to make the associations between
49  * the encoder and bridges. Once these links are made, the bridges will
50  * participate along with encoder functions to perform mode_set/enable/disable
51  * through the ops provided in &drm_bridge_funcs.
52  *
53  * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes,
54  * CRTCs, encoders or connectors and hence are not visible to userspace. They
55  * just provide additional hooks to get the desired output at the end of the
56  * encoder chain.
57  *
58  * Bridges can also be chained up using the &drm_bridge.chain_node field.
59  *
60  * Both legacy CRTC helpers and the new atomic modeset helpers support bridges.
61  */
62 
63 static DEFINE_MUTEX(bridge_lock);
64 static LIST_HEAD(bridge_list);
65 
66 /**
67  * drm_bridge_add - add the given bridge to the global bridge list
68  *
69  * @bridge: bridge control structure
70  */
71 void 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 EXPORT_SYMBOL(drm_bridge_add);
78 
79 /**
80  * drm_bridge_remove - remove the given bridge from the global bridge list
81  *
82  * @bridge: bridge control structure
83  */
84 void drm_bridge_remove(struct drm_bridge *bridge)
85 {
86 	mutex_lock(&bridge_lock);
87 	list_del_init(&bridge->list);
88 	mutex_unlock(&bridge_lock);
89 }
90 EXPORT_SYMBOL(drm_bridge_remove);
91 
92 /**
93  * drm_bridge_attach - attach the bridge to an encoder's chain
94  *
95  * @encoder: DRM encoder
96  * @bridge: bridge to attach
97  * @previous: previous bridge in the chain (optional)
98  *
99  * Called by a kms driver to link the bridge to an encoder's chain. The previous
100  * argument specifies the previous bridge in the chain. If NULL, the bridge is
101  * linked directly at the encoder's output. Otherwise it is linked at the
102  * previous bridge's output.
103  *
104  * If non-NULL the previous bridge must be already attached by a call to this
105  * function.
106  *
107  * Note that bridges attached to encoders are auto-detached during encoder
108  * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
109  * *not* be balanced with a drm_bridge_detach() in driver code.
110  *
111  * RETURNS:
112  * Zero on success, error code on failure
113  */
114 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
115 		      struct drm_bridge *previous)
116 {
117 	int ret;
118 
119 	if (!encoder || !bridge)
120 		return -EINVAL;
121 
122 	if (previous && (!previous->dev || previous->encoder != encoder))
123 		return -EINVAL;
124 
125 	if (bridge->dev)
126 		return -EBUSY;
127 
128 	bridge->dev = encoder->dev;
129 	bridge->encoder = encoder;
130 
131 	if (previous)
132 		list_add(&bridge->chain_node, &previous->chain_node);
133 	else
134 		list_add(&bridge->chain_node, &encoder->bridge_chain);
135 
136 	if (bridge->funcs->attach) {
137 		ret = bridge->funcs->attach(bridge);
138 		if (ret < 0) {
139 			list_del(&bridge->chain_node);
140 			bridge->dev = NULL;
141 			bridge->encoder = NULL;
142 			return ret;
143 		}
144 	}
145 
146 	return 0;
147 }
148 EXPORT_SYMBOL(drm_bridge_attach);
149 
150 void drm_bridge_detach(struct drm_bridge *bridge)
151 {
152 	if (WARN_ON(!bridge))
153 		return;
154 
155 	if (WARN_ON(!bridge->dev))
156 		return;
157 
158 	if (bridge->funcs->detach)
159 		bridge->funcs->detach(bridge);
160 
161 	list_del(&bridge->chain_node);
162 	bridge->dev = NULL;
163 }
164 
165 /**
166  * DOC: bridge callbacks
167  *
168  * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM
169  * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c
170  * These helpers call a specific &drm_bridge_funcs op for all the bridges
171  * during encoder configuration.
172  *
173  * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
174  */
175 
176 /**
177  * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
178  *				 encoder chain
179  * @bridge: bridge control structure
180  * @mode: desired mode to be set for the bridge
181  * @adjusted_mode: updated mode that works for this bridge
182  *
183  * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
184  * encoder chain, starting from the first bridge to the last.
185  *
186  * Note: the bridge passed should be the one closest to the encoder
187  *
188  * RETURNS:
189  * true on success, false on failure
190  */
191 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
192 				 const struct drm_display_mode *mode,
193 				 struct drm_display_mode *adjusted_mode)
194 {
195 	struct drm_encoder *encoder;
196 
197 	if (!bridge)
198 		return true;
199 
200 	encoder = bridge->encoder;
201 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
202 		if (!bridge->funcs->mode_fixup)
203 			continue;
204 
205 		if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
206 			return false;
207 	}
208 
209 	return true;
210 }
211 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
212 
213 /**
214  * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
215  *				 encoder chain.
216  * @bridge: bridge control structure
217  * @mode: desired mode to be validated
218  *
219  * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
220  * chain, starting from the first bridge to the last. If at least one bridge
221  * does not accept the mode the function returns the error code.
222  *
223  * Note: the bridge passed should be the one closest to the encoder.
224  *
225  * RETURNS:
226  * MODE_OK on success, drm_mode_status Enum error code on failure
227  */
228 enum drm_mode_status
229 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
230 			    const struct drm_display_mode *mode)
231 {
232 	struct drm_encoder *encoder;
233 
234 	if (!bridge)
235 		return MODE_OK;
236 
237 	encoder = bridge->encoder;
238 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
239 		enum drm_mode_status ret;
240 
241 		if (!bridge->funcs->mode_valid)
242 			continue;
243 
244 		ret = bridge->funcs->mode_valid(bridge, mode);
245 		if (ret != MODE_OK)
246 			return ret;
247 	}
248 
249 	return MODE_OK;
250 }
251 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
252 
253 /**
254  * drm_bridge_chain_disable - disables all bridges in the encoder chain
255  * @bridge: bridge control structure
256  *
257  * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
258  * chain, starting from the last bridge to the first. These are called before
259  * calling the encoder's prepare op.
260  *
261  * Note: the bridge passed should be the one closest to the encoder
262  */
263 void drm_bridge_chain_disable(struct drm_bridge *bridge)
264 {
265 	struct drm_encoder *encoder;
266 	struct drm_bridge *iter;
267 
268 	if (!bridge)
269 		return;
270 
271 	encoder = bridge->encoder;
272 	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
273 		if (iter->funcs->disable)
274 			iter->funcs->disable(iter);
275 
276 		if (iter == bridge)
277 			break;
278 	}
279 }
280 EXPORT_SYMBOL(drm_bridge_chain_disable);
281 
282 /**
283  * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the
284  *				   encoder chain
285  * @bridge: bridge control structure
286  *
287  * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
288  * encoder chain, starting from the first bridge to the last. These are called
289  * after completing the encoder's prepare op.
290  *
291  * Note: the bridge passed should be the one closest to the encoder
292  */
293 void drm_bridge_chain_post_disable(struct drm_bridge *bridge)
294 {
295 	struct drm_encoder *encoder;
296 
297 	if (!bridge)
298 		return;
299 
300 	encoder = bridge->encoder;
301 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
302 		if (bridge->funcs->post_disable)
303 			bridge->funcs->post_disable(bridge);
304 	}
305 }
306 EXPORT_SYMBOL(drm_bridge_chain_post_disable);
307 
308 /**
309  * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
310  *			       encoder chain
311  * @bridge: bridge control structure
312  * @mode: desired mode to be set for the encoder chain
313  * @adjusted_mode: updated mode that works for this encoder chain
314  *
315  * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
316  * encoder chain, starting from the first bridge to the last.
317  *
318  * Note: the bridge passed should be the one closest to the encoder
319  */
320 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
321 			       const struct drm_display_mode *mode,
322 			       const struct drm_display_mode *adjusted_mode)
323 {
324 	struct drm_encoder *encoder;
325 
326 	if (!bridge)
327 		return;
328 
329 	encoder = bridge->encoder;
330 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
331 		if (bridge->funcs->mode_set)
332 			bridge->funcs->mode_set(bridge, mode, adjusted_mode);
333 	}
334 }
335 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
336 
337 /**
338  * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the
339  *				 encoder chain
340  * @bridge: bridge control structure
341  *
342  * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
343  * chain, starting from the last bridge to the first. These are called
344  * before calling the encoder's commit op.
345  *
346  * Note: the bridge passed should be the one closest to the encoder
347  */
348 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge)
349 {
350 	struct drm_encoder *encoder;
351 	struct drm_bridge *iter;
352 
353 	if (!bridge)
354 		return;
355 
356 	encoder = bridge->encoder;
357 	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
358 		if (iter->funcs->pre_enable)
359 			iter->funcs->pre_enable(iter);
360 	}
361 }
362 EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
363 
364 /**
365  * drm_bridge_chain_enable - enables all bridges in the encoder chain
366  * @bridge: bridge control structure
367  *
368  * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
369  * chain, starting from the first bridge to the last. These are called
370  * after completing the encoder's commit op.
371  *
372  * Note that the bridge passed should be the one closest to the encoder
373  */
374 void drm_bridge_chain_enable(struct drm_bridge *bridge)
375 {
376 	struct drm_encoder *encoder;
377 
378 	if (!bridge)
379 		return;
380 
381 	encoder = bridge->encoder;
382 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
383 		if (bridge->funcs->enable)
384 			bridge->funcs->enable(bridge);
385 	}
386 }
387 EXPORT_SYMBOL(drm_bridge_chain_enable);
388 
389 /**
390  * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
391  * @bridge: bridge control structure
392  * @old_state: old atomic state
393  *
394  * Calls &drm_bridge_funcs.atomic_disable (falls back on
395  * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
396  * starting from the last bridge to the first. These are called before calling
397  * &drm_encoder_helper_funcs.atomic_disable
398  *
399  * Note: the bridge passed should be the one closest to the encoder
400  */
401 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
402 				     struct drm_atomic_state *old_state)
403 {
404 	struct drm_encoder *encoder;
405 	struct drm_bridge *iter;
406 
407 	if (!bridge)
408 		return;
409 
410 	encoder = bridge->encoder;
411 	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
412 		if (iter->funcs->atomic_disable)
413 			iter->funcs->atomic_disable(iter, old_state);
414 		else if (iter->funcs->disable)
415 			iter->funcs->disable(iter);
416 
417 		if (iter == bridge)
418 			break;
419 	}
420 }
421 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
422 
423 /**
424  * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
425  *					  in the encoder chain
426  * @bridge: bridge control structure
427  * @old_state: old atomic state
428  *
429  * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
430  * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
431  * starting from the first bridge to the last. These are called after completing
432  * &drm_encoder_helper_funcs.atomic_disable
433  *
434  * Note: the bridge passed should be the one closest to the encoder
435  */
436 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
437 					  struct drm_atomic_state *old_state)
438 {
439 	struct drm_encoder *encoder;
440 
441 	if (!bridge)
442 		return;
443 
444 	encoder = bridge->encoder;
445 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
446 		if (bridge->funcs->atomic_post_disable)
447 			bridge->funcs->atomic_post_disable(bridge, old_state);
448 		else if (bridge->funcs->post_disable)
449 			bridge->funcs->post_disable(bridge);
450 	}
451 }
452 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
453 
454 /**
455  * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
456  *					the encoder chain
457  * @bridge: bridge control structure
458  * @old_state: old atomic state
459  *
460  * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
461  * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
462  * starting from the last bridge to the first. These are called before calling
463  * &drm_encoder_helper_funcs.atomic_enable
464  *
465  * Note: the bridge passed should be the one closest to the encoder
466  */
467 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
468 					struct drm_atomic_state *old_state)
469 {
470 	struct drm_encoder *encoder;
471 	struct drm_bridge *iter;
472 
473 	if (!bridge)
474 		return;
475 
476 	encoder = bridge->encoder;
477 	list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
478 		if (iter->funcs->atomic_pre_enable)
479 			iter->funcs->atomic_pre_enable(iter, old_state);
480 		else if (iter->funcs->pre_enable)
481 			iter->funcs->pre_enable(iter);
482 
483 		if (iter == bridge)
484 			break;
485 	}
486 }
487 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
488 
489 /**
490  * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
491  * @bridge: bridge control structure
492  * @old_state: old atomic state
493  *
494  * Calls &drm_bridge_funcs.atomic_enable (falls back on
495  * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
496  * starting from the first bridge to the last. These are called after completing
497  * &drm_encoder_helper_funcs.atomic_enable
498  *
499  * Note: the bridge passed should be the one closest to the encoder
500  */
501 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
502 				    struct drm_atomic_state *old_state)
503 {
504 	struct drm_encoder *encoder;
505 
506 	if (!bridge)
507 		return;
508 
509 	encoder = bridge->encoder;
510 	list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
511 		if (bridge->funcs->atomic_enable)
512 			bridge->funcs->atomic_enable(bridge, old_state);
513 		else if (bridge->funcs->enable)
514 			bridge->funcs->enable(bridge);
515 	}
516 }
517 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
518 
519 #ifdef CONFIG_OF
520 /**
521  * of_drm_find_bridge - find the bridge corresponding to the device node in
522  *			the global bridge list
523  *
524  * @np: device node
525  *
526  * RETURNS:
527  * drm_bridge control struct on success, NULL on failure
528  */
529 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
530 {
531 	struct drm_bridge *bridge;
532 
533 	mutex_lock(&bridge_lock);
534 
535 	list_for_each_entry(bridge, &bridge_list, list) {
536 		if (bridge->of_node == np) {
537 			mutex_unlock(&bridge_lock);
538 			return bridge;
539 		}
540 	}
541 
542 	mutex_unlock(&bridge_lock);
543 	return NULL;
544 }
545 EXPORT_SYMBOL(of_drm_find_bridge);
546 #endif
547 
548 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
549 MODULE_DESCRIPTION("DRM bridge infrastructure");
550 MODULE_LICENSE("GPL and additional rights");
551