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