drm/bridge: Patch atomic hooks to take a drm_bridge_state
[linux/fpc-iii.git] / drivers / gpu / drm / drm_bridge.c
blob872e159fcb42c328c03e9336b0158fa09a255e40
1 /*
2 * Copyright (c) 2014 Samsung Electronics Co., Ltd
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:
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.
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.
24 #include <linux/err.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
28 #include <drm/drm_atomic_state_helper.h>
29 #include <drm/drm_bridge.h>
30 #include <drm/drm_encoder.h>
32 #include "drm_crtc_internal.h"
34 /**
35 * DOC: overview
37 * &struct drm_bridge represents a device that hangs on to an encoder. These are
38 * handy when a regular &drm_encoder entity isn't enough to represent the entire
39 * encoder chain.
41 * A bridge is always attached to a single &drm_encoder at a time, but can be
42 * either connected to it directly, or through an intermediate bridge::
44 * encoder ---> bridge B ---> bridge A
46 * Here, the output of the encoder feeds to bridge B, and that furthers feeds to
47 * bridge A.
49 * The driver using the bridge is responsible to make the associations between
50 * the encoder and bridges. Once these links are made, the bridges will
51 * participate along with encoder functions to perform mode_set/enable/disable
52 * through the ops provided in &drm_bridge_funcs.
54 * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes,
55 * CRTCs, encoders or connectors and hence are not visible to userspace. They
56 * just provide additional hooks to get the desired output at the end of the
57 * encoder chain.
59 * Bridges can also be chained up using the &drm_bridge.chain_node field.
61 * Both legacy CRTC helpers and the new atomic modeset helpers support bridges.
64 static DEFINE_MUTEX(bridge_lock);
65 static LIST_HEAD(bridge_list);
67 /**
68 * drm_bridge_add - add the given bridge to the global bridge list
70 * @bridge: bridge control structure
72 void drm_bridge_add(struct drm_bridge *bridge)
74 mutex_lock(&bridge_lock);
75 list_add_tail(&bridge->list, &bridge_list);
76 mutex_unlock(&bridge_lock);
78 EXPORT_SYMBOL(drm_bridge_add);
80 /**
81 * drm_bridge_remove - remove the given bridge from the global bridge list
83 * @bridge: bridge control structure
85 void drm_bridge_remove(struct drm_bridge *bridge)
87 mutex_lock(&bridge_lock);
88 list_del_init(&bridge->list);
89 mutex_unlock(&bridge_lock);
91 EXPORT_SYMBOL(drm_bridge_remove);
93 static struct drm_bridge_state *
94 drm_atomic_default_bridge_duplicate_state(struct drm_bridge *bridge)
96 struct drm_bridge_state *new;
98 if (WARN_ON(!bridge->base.state))
99 return NULL;
101 new = kzalloc(sizeof(*new), GFP_KERNEL);
102 if (new)
103 __drm_atomic_helper_bridge_duplicate_state(bridge, new);
105 return new;
108 static struct drm_private_state *
109 drm_bridge_atomic_duplicate_priv_state(struct drm_private_obj *obj)
111 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
112 struct drm_bridge_state *state;
114 if (bridge->funcs->atomic_duplicate_state)
115 state = bridge->funcs->atomic_duplicate_state(bridge);
116 else
117 state = drm_atomic_default_bridge_duplicate_state(bridge);
119 return state ? &state->base : NULL;
122 static void
123 drm_atomic_default_bridge_destroy_state(struct drm_bridge *bridge,
124 struct drm_bridge_state *state)
126 /* Just a simple kfree() for now */
127 kfree(state);
130 static void
131 drm_bridge_atomic_destroy_priv_state(struct drm_private_obj *obj,
132 struct drm_private_state *s)
134 struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
135 struct drm_bridge *bridge = drm_priv_to_bridge(obj);
137 if (bridge->funcs->atomic_destroy_state)
138 bridge->funcs->atomic_destroy_state(bridge, state);
139 else
140 drm_atomic_default_bridge_destroy_state(bridge, state);
143 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
144 .atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
145 .atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
148 static struct drm_bridge_state *
149 drm_atomic_default_bridge_reset(struct drm_bridge *bridge)
151 struct drm_bridge_state *bridge_state;
153 bridge_state = kzalloc(sizeof(*bridge_state), GFP_KERNEL);
154 if (!bridge_state)
155 return ERR_PTR(-ENOMEM);
157 __drm_atomic_helper_bridge_reset(bridge, bridge_state);
158 return bridge_state;
162 * drm_bridge_attach - attach the bridge to an encoder's chain
164 * @encoder: DRM encoder
165 * @bridge: bridge to attach
166 * @previous: previous bridge in the chain (optional)
168 * Called by a kms driver to link the bridge to an encoder's chain. The previous
169 * argument specifies the previous bridge in the chain. If NULL, the bridge is
170 * linked directly at the encoder's output. Otherwise it is linked at the
171 * previous bridge's output.
173 * If non-NULL the previous bridge must be already attached by a call to this
174 * function.
176 * Note that bridges attached to encoders are auto-detached during encoder
177 * cleanup in drm_encoder_cleanup(), so drm_bridge_attach() should generally
178 * *not* be balanced with a drm_bridge_detach() in driver code.
180 * RETURNS:
181 * Zero on success, error code on failure
183 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
184 struct drm_bridge *previous)
186 struct drm_bridge_state *state;
187 int ret;
189 if (!encoder || !bridge)
190 return -EINVAL;
192 if (previous && (!previous->dev || previous->encoder != encoder))
193 return -EINVAL;
195 if (bridge->dev)
196 return -EBUSY;
198 bridge->dev = encoder->dev;
199 bridge->encoder = encoder;
201 if (previous)
202 list_add(&bridge->chain_node, &previous->chain_node);
203 else
204 list_add(&bridge->chain_node, &encoder->bridge_chain);
206 if (bridge->funcs->attach) {
207 ret = bridge->funcs->attach(bridge);
208 if (ret < 0)
209 goto err_reset_bridge;
212 if (bridge->funcs->atomic_reset)
213 state = bridge->funcs->atomic_reset(bridge);
214 else
215 state = drm_atomic_default_bridge_reset(bridge);
217 if (IS_ERR(state)) {
218 ret = PTR_ERR(state);
219 goto err_detach_bridge;
222 drm_atomic_private_obj_init(bridge->dev, &bridge->base,
223 &state->base,
224 &drm_bridge_priv_state_funcs);
226 return 0;
228 err_detach_bridge:
229 if (bridge->funcs->detach)
230 bridge->funcs->detach(bridge);
232 err_reset_bridge:
233 bridge->dev = NULL;
234 bridge->encoder = NULL;
235 list_del(&bridge->chain_node);
236 return ret;
238 EXPORT_SYMBOL(drm_bridge_attach);
240 void drm_bridge_detach(struct drm_bridge *bridge)
242 if (WARN_ON(!bridge))
243 return;
245 if (WARN_ON(!bridge->dev))
246 return;
248 drm_atomic_private_obj_fini(&bridge->base);
250 if (bridge->funcs->detach)
251 bridge->funcs->detach(bridge);
253 list_del(&bridge->chain_node);
254 bridge->dev = NULL;
258 * DOC: bridge callbacks
260 * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM
261 * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c
262 * These helpers call a specific &drm_bridge_funcs op for all the bridges
263 * during encoder configuration.
265 * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
269 * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
270 * encoder chain
271 * @bridge: bridge control structure
272 * @mode: desired mode to be set for the bridge
273 * @adjusted_mode: updated mode that works for this bridge
275 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
276 * encoder chain, starting from the first bridge to the last.
278 * Note: the bridge passed should be the one closest to the encoder
280 * RETURNS:
281 * true on success, false on failure
283 bool drm_bridge_chain_mode_fixup(struct drm_bridge *bridge,
284 const struct drm_display_mode *mode,
285 struct drm_display_mode *adjusted_mode)
287 struct drm_encoder *encoder;
289 if (!bridge)
290 return true;
292 encoder = bridge->encoder;
293 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
294 if (!bridge->funcs->mode_fixup)
295 continue;
297 if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
298 return false;
301 return true;
303 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
306 * drm_bridge_chain_mode_valid - validate the mode against all bridges in the
307 * encoder chain.
308 * @bridge: bridge control structure
309 * @mode: desired mode to be validated
311 * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
312 * chain, starting from the first bridge to the last. If at least one bridge
313 * does not accept the mode the function returns the error code.
315 * Note: the bridge passed should be the one closest to the encoder.
317 * RETURNS:
318 * MODE_OK on success, drm_mode_status Enum error code on failure
320 enum drm_mode_status
321 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
322 const struct drm_display_mode *mode)
324 struct drm_encoder *encoder;
326 if (!bridge)
327 return MODE_OK;
329 encoder = bridge->encoder;
330 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
331 enum drm_mode_status ret;
333 if (!bridge->funcs->mode_valid)
334 continue;
336 ret = bridge->funcs->mode_valid(bridge, mode);
337 if (ret != MODE_OK)
338 return ret;
341 return MODE_OK;
343 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
346 * drm_bridge_chain_disable - disables all bridges in the encoder chain
347 * @bridge: bridge control structure
349 * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
350 * chain, starting from the last bridge to the first. These are called before
351 * calling the encoder's prepare op.
353 * Note: the bridge passed should be the one closest to the encoder
355 void drm_bridge_chain_disable(struct drm_bridge *bridge)
357 struct drm_encoder *encoder;
358 struct drm_bridge *iter;
360 if (!bridge)
361 return;
363 encoder = bridge->encoder;
364 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
365 if (iter->funcs->disable)
366 iter->funcs->disable(iter);
368 if (iter == bridge)
369 break;
372 EXPORT_SYMBOL(drm_bridge_chain_disable);
375 * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the
376 * encoder chain
377 * @bridge: bridge control structure
379 * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
380 * encoder chain, starting from the first bridge to the last. These are called
381 * after completing the encoder's prepare op.
383 * Note: the bridge passed should be the one closest to the encoder
385 void drm_bridge_chain_post_disable(struct drm_bridge *bridge)
387 struct drm_encoder *encoder;
389 if (!bridge)
390 return;
392 encoder = bridge->encoder;
393 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
394 if (bridge->funcs->post_disable)
395 bridge->funcs->post_disable(bridge);
398 EXPORT_SYMBOL(drm_bridge_chain_post_disable);
401 * drm_bridge_chain_mode_set - set proposed mode for all bridges in the
402 * encoder chain
403 * @bridge: bridge control structure
404 * @mode: desired mode to be set for the encoder chain
405 * @adjusted_mode: updated mode that works for this encoder chain
407 * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
408 * encoder chain, starting from the first bridge to the last.
410 * Note: the bridge passed should be the one closest to the encoder
412 void drm_bridge_chain_mode_set(struct drm_bridge *bridge,
413 const struct drm_display_mode *mode,
414 const struct drm_display_mode *adjusted_mode)
416 struct drm_encoder *encoder;
418 if (!bridge)
419 return;
421 encoder = bridge->encoder;
422 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
423 if (bridge->funcs->mode_set)
424 bridge->funcs->mode_set(bridge, mode, adjusted_mode);
427 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
430 * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the
431 * encoder chain
432 * @bridge: bridge control structure
434 * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
435 * chain, starting from the last bridge to the first. These are called
436 * before calling the encoder's commit op.
438 * Note: the bridge passed should be the one closest to the encoder
440 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge)
442 struct drm_encoder *encoder;
443 struct drm_bridge *iter;
445 if (!bridge)
446 return;
448 encoder = bridge->encoder;
449 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
450 if (iter->funcs->pre_enable)
451 iter->funcs->pre_enable(iter);
454 EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
457 * drm_bridge_chain_enable - enables all bridges in the encoder chain
458 * @bridge: bridge control structure
460 * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
461 * chain, starting from the first bridge to the last. These are called
462 * after completing the encoder's commit op.
464 * Note that the bridge passed should be the one closest to the encoder
466 void drm_bridge_chain_enable(struct drm_bridge *bridge)
468 struct drm_encoder *encoder;
470 if (!bridge)
471 return;
473 encoder = bridge->encoder;
474 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
475 if (bridge->funcs->enable)
476 bridge->funcs->enable(bridge);
479 EXPORT_SYMBOL(drm_bridge_chain_enable);
482 * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
483 * @bridge: bridge control structure
484 * @old_state: old atomic state
486 * Calls &drm_bridge_funcs.atomic_disable (falls back on
487 * &drm_bridge_funcs.disable) op for all the bridges in the encoder chain,
488 * starting from the last bridge to the first. These are called before calling
489 * &drm_encoder_helper_funcs.atomic_disable
491 * Note: the bridge passed should be the one closest to the encoder
493 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
494 struct drm_atomic_state *old_state)
496 struct drm_encoder *encoder;
497 struct drm_bridge *iter;
499 if (!bridge)
500 return;
502 encoder = bridge->encoder;
503 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
504 if (iter->funcs->atomic_disable) {
505 struct drm_bridge_state *old_bridge_state;
507 old_bridge_state =
508 drm_atomic_get_old_bridge_state(old_state,
509 iter);
510 if (WARN_ON(!old_bridge_state))
511 return;
513 iter->funcs->atomic_disable(iter, old_bridge_state);
514 } else if (iter->funcs->disable) {
515 iter->funcs->disable(iter);
518 if (iter == bridge)
519 break;
522 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
525 * drm_atomic_bridge_chain_post_disable - cleans up after disabling all bridges
526 * in the encoder chain
527 * @bridge: bridge control structure
528 * @old_state: old atomic state
530 * Calls &drm_bridge_funcs.atomic_post_disable (falls back on
531 * &drm_bridge_funcs.post_disable) op for all the bridges in the encoder chain,
532 * starting from the first bridge to the last. These are called after completing
533 * &drm_encoder_helper_funcs.atomic_disable
535 * Note: the bridge passed should be the one closest to the encoder
537 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
538 struct drm_atomic_state *old_state)
540 struct drm_encoder *encoder;
542 if (!bridge)
543 return;
545 encoder = bridge->encoder;
546 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
547 if (bridge->funcs->atomic_post_disable) {
548 struct drm_bridge_state *old_bridge_state;
550 old_bridge_state =
551 drm_atomic_get_old_bridge_state(old_state,
552 bridge);
553 if (WARN_ON(!old_bridge_state))
554 return;
556 bridge->funcs->atomic_post_disable(bridge,
557 old_bridge_state);
558 } else if (bridge->funcs->post_disable) {
559 bridge->funcs->post_disable(bridge);
563 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
566 * drm_atomic_bridge_chain_pre_enable - prepares for enabling all bridges in
567 * the encoder chain
568 * @bridge: bridge control structure
569 * @old_state: old atomic state
571 * Calls &drm_bridge_funcs.atomic_pre_enable (falls back on
572 * &drm_bridge_funcs.pre_enable) op for all the bridges in the encoder chain,
573 * starting from the last bridge to the first. These are called before calling
574 * &drm_encoder_helper_funcs.atomic_enable
576 * Note: the bridge passed should be the one closest to the encoder
578 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
579 struct drm_atomic_state *old_state)
581 struct drm_encoder *encoder;
582 struct drm_bridge *iter;
584 if (!bridge)
585 return;
587 encoder = bridge->encoder;
588 list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) {
589 if (iter->funcs->atomic_pre_enable) {
590 struct drm_bridge_state *old_bridge_state;
592 old_bridge_state =
593 drm_atomic_get_old_bridge_state(old_state,
594 iter);
595 if (WARN_ON(!old_bridge_state))
596 return;
598 iter->funcs->atomic_pre_enable(iter, old_bridge_state);
599 } else if (iter->funcs->pre_enable) {
600 iter->funcs->pre_enable(iter);
603 if (iter == bridge)
604 break;
607 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
610 * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
611 * @bridge: bridge control structure
612 * @old_state: old atomic state
614 * Calls &drm_bridge_funcs.atomic_enable (falls back on
615 * &drm_bridge_funcs.enable) op for all the bridges in the encoder chain,
616 * starting from the first bridge to the last. These are called after completing
617 * &drm_encoder_helper_funcs.atomic_enable
619 * Note: the bridge passed should be the one closest to the encoder
621 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
622 struct drm_atomic_state *old_state)
624 struct drm_encoder *encoder;
626 if (!bridge)
627 return;
629 encoder = bridge->encoder;
630 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
631 if (bridge->funcs->atomic_enable) {
632 struct drm_bridge_state *old_bridge_state;
634 old_bridge_state =
635 drm_atomic_get_old_bridge_state(old_state,
636 bridge);
637 if (WARN_ON(!old_bridge_state))
638 return;
640 bridge->funcs->atomic_enable(bridge, old_bridge_state);
641 } else if (bridge->funcs->enable) {
642 bridge->funcs->enable(bridge);
646 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
649 * __drm_atomic_helper_bridge_reset() - Initialize a bridge state to its
650 * default
651 * @bridge: the bridge this state is refers to
652 * @state: bridge state to initialize
654 * Initialize the bridge state to default values. This is meant to be* called
655 * by the bridge &drm_plane_funcs.reset hook for bridges that subclass the
656 * bridge state.
658 void __drm_atomic_helper_bridge_reset(struct drm_bridge *bridge,
659 struct drm_bridge_state *state)
661 memset(state, 0, sizeof(*state));
662 state->bridge = bridge;
664 EXPORT_SYMBOL(__drm_atomic_helper_bridge_reset);
667 * __drm_atomic_helper_bridge_duplicate_state() - Copy atomic bridge state
668 * @bridge: bridge object
669 * @state: atomic bridge state
671 * Copies atomic state from a bridge's current state and resets inferred values.
672 * This is useful for drivers that subclass the bridge state.
674 void __drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge,
675 struct drm_bridge_state *state)
677 __drm_atomic_helper_private_obj_duplicate_state(&bridge->base,
678 &state->base);
679 state->bridge = bridge;
681 EXPORT_SYMBOL(__drm_atomic_helper_bridge_duplicate_state);
683 #ifdef CONFIG_OF
685 * of_drm_find_bridge - find the bridge corresponding to the device node in
686 * the global bridge list
688 * @np: device node
690 * RETURNS:
691 * drm_bridge control struct on success, NULL on failure
693 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
695 struct drm_bridge *bridge;
697 mutex_lock(&bridge_lock);
699 list_for_each_entry(bridge, &bridge_list, list) {
700 if (bridge->of_node == np) {
701 mutex_unlock(&bridge_lock);
702 return bridge;
706 mutex_unlock(&bridge_lock);
707 return NULL;
709 EXPORT_SYMBOL(of_drm_find_bridge);
710 #endif
712 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
713 MODULE_DESCRIPTION("DRM bridge infrastructure");
714 MODULE_LICENSE("GPL and additional rights");