treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / drm_bridge.c
blobc2cf0c90fa265d5a369194b75bd161dbe272b378
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_bridge.h>
29 #include <drm/drm_encoder.h>
31 #include "drm_crtc_internal.h"
33 /**
34 * DOC: overview
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.
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::
43 * encoder ---> bridge B ---> bridge A
45 * Here, the output of the encoder feeds to bridge B, and that furthers feeds to
46 * bridge A.
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.
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.
58 * Bridges can also be chained up using the &drm_bridge.chain_node field.
60 * Both legacy CRTC helpers and the new atomic modeset helpers support bridges.
63 static DEFINE_MUTEX(bridge_lock);
64 static LIST_HEAD(bridge_list);
66 /**
67 * drm_bridge_add - add the given bridge to the global bridge list
69 * @bridge: bridge control structure
71 void drm_bridge_add(struct drm_bridge *bridge)
73 mutex_lock(&bridge_lock);
74 list_add_tail(&bridge->list, &bridge_list);
75 mutex_unlock(&bridge_lock);
77 EXPORT_SYMBOL(drm_bridge_add);
79 /**
80 * drm_bridge_remove - remove the given bridge from the global bridge list
82 * @bridge: bridge control structure
84 void drm_bridge_remove(struct drm_bridge *bridge)
86 mutex_lock(&bridge_lock);
87 list_del_init(&bridge->list);
88 mutex_unlock(&bridge_lock);
90 EXPORT_SYMBOL(drm_bridge_remove);
92 /**
93 * drm_bridge_attach - attach the bridge to an encoder's chain
95 * @encoder: DRM encoder
96 * @bridge: bridge to attach
97 * @previous: previous bridge in the chain (optional)
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.
104 * If non-NULL the previous bridge must be already attached by a call to this
105 * function.
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.
111 * RETURNS:
112 * Zero on success, error code on failure
114 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
115 struct drm_bridge *previous)
117 int ret;
119 if (!encoder || !bridge)
120 return -EINVAL;
122 if (previous && (!previous->dev || previous->encoder != encoder))
123 return -EINVAL;
125 if (bridge->dev)
126 return -EBUSY;
128 bridge->dev = encoder->dev;
129 bridge->encoder = encoder;
131 if (previous)
132 list_add(&bridge->chain_node, &previous->chain_node);
133 else
134 list_add(&bridge->chain_node, &encoder->bridge_chain);
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;
146 return 0;
148 EXPORT_SYMBOL(drm_bridge_attach);
150 void drm_bridge_detach(struct drm_bridge *bridge)
152 if (WARN_ON(!bridge))
153 return;
155 if (WARN_ON(!bridge->dev))
156 return;
158 if (bridge->funcs->detach)
159 bridge->funcs->detach(bridge);
161 list_del(&bridge->chain_node);
162 bridge->dev = NULL;
166 * DOC: bridge callbacks
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.
173 * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
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
183 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
184 * encoder chain, starting from the first bridge to the last.
186 * Note: the bridge passed should be the one closest to the encoder
188 * RETURNS:
189 * true on success, false on failure
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)
195 struct drm_encoder *encoder;
197 if (!bridge)
198 return true;
200 encoder = bridge->encoder;
201 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
202 if (!bridge->funcs->mode_fixup)
203 continue;
205 if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
206 return false;
209 return true;
211 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
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
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.
223 * Note: the bridge passed should be the one closest to the encoder.
225 * RETURNS:
226 * MODE_OK on success, drm_mode_status Enum error code on failure
228 enum drm_mode_status
229 drm_bridge_chain_mode_valid(struct drm_bridge *bridge,
230 const struct drm_display_mode *mode)
232 struct drm_encoder *encoder;
234 if (!bridge)
235 return MODE_OK;
237 encoder = bridge->encoder;
238 list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) {
239 enum drm_mode_status ret;
241 if (!bridge->funcs->mode_valid)
242 continue;
244 ret = bridge->funcs->mode_valid(bridge, mode);
245 if (ret != MODE_OK)
246 return ret;
249 return MODE_OK;
251 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
254 * drm_bridge_chain_disable - disables all bridges in the encoder chain
255 * @bridge: bridge control structure
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.
261 * Note: the bridge passed should be the one closest to the encoder
263 void drm_bridge_chain_disable(struct drm_bridge *bridge)
265 struct drm_encoder *encoder;
266 struct drm_bridge *iter;
268 if (!bridge)
269 return;
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);
276 if (iter == bridge)
277 break;
280 EXPORT_SYMBOL(drm_bridge_chain_disable);
283 * drm_bridge_chain_post_disable - cleans up after disabling all bridges in the
284 * encoder chain
285 * @bridge: bridge control structure
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.
291 * Note: the bridge passed should be the one closest to the encoder
293 void drm_bridge_chain_post_disable(struct drm_bridge *bridge)
295 struct drm_encoder *encoder;
297 if (!bridge)
298 return;
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);
306 EXPORT_SYMBOL(drm_bridge_chain_post_disable);
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
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.
318 * Note: the bridge passed should be the one closest to the encoder
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)
324 struct drm_encoder *encoder;
326 if (!bridge)
327 return;
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);
335 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
338 * drm_bridge_chain_pre_enable - prepares for enabling all bridges in the
339 * encoder chain
340 * @bridge: bridge control structure
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.
346 * Note: the bridge passed should be the one closest to the encoder
348 void drm_bridge_chain_pre_enable(struct drm_bridge *bridge)
350 struct drm_encoder *encoder;
351 struct drm_bridge *iter;
353 if (!bridge)
354 return;
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);
362 EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
365 * drm_bridge_chain_enable - enables all bridges in the encoder chain
366 * @bridge: bridge control structure
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.
372 * Note that the bridge passed should be the one closest to the encoder
374 void drm_bridge_chain_enable(struct drm_bridge *bridge)
376 struct drm_encoder *encoder;
378 if (!bridge)
379 return;
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);
387 EXPORT_SYMBOL(drm_bridge_chain_enable);
390 * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
391 * @bridge: bridge control structure
392 * @old_state: old atomic state
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
399 * Note: the bridge passed should be the one closest to the encoder
401 void drm_atomic_bridge_chain_disable(struct drm_bridge *bridge,
402 struct drm_atomic_state *old_state)
404 struct drm_encoder *encoder;
405 struct drm_bridge *iter;
407 if (!bridge)
408 return;
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);
417 if (iter == bridge)
418 break;
421 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
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
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
434 * Note: the bridge passed should be the one closest to the encoder
436 void drm_atomic_bridge_chain_post_disable(struct drm_bridge *bridge,
437 struct drm_atomic_state *old_state)
439 struct drm_encoder *encoder;
441 if (!bridge)
442 return;
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);
452 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
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
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
465 * Note: the bridge passed should be the one closest to the encoder
467 void drm_atomic_bridge_chain_pre_enable(struct drm_bridge *bridge,
468 struct drm_atomic_state *old_state)
470 struct drm_encoder *encoder;
471 struct drm_bridge *iter;
473 if (!bridge)
474 return;
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);
483 if (iter == bridge)
484 break;
487 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
490 * drm_atomic_bridge_chain_enable - enables all bridges in the encoder chain
491 * @bridge: bridge control structure
492 * @old_state: old atomic state
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
499 * Note: the bridge passed should be the one closest to the encoder
501 void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge,
502 struct drm_atomic_state *old_state)
504 struct drm_encoder *encoder;
506 if (!bridge)
507 return;
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);
517 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
519 #ifdef CONFIG_OF
521 * of_drm_find_bridge - find the bridge corresponding to the device node in
522 * the global bridge list
524 * @np: device node
526 * RETURNS:
527 * drm_bridge control struct on success, NULL on failure
529 struct drm_bridge *of_drm_find_bridge(struct device_node *np)
531 struct drm_bridge *bridge;
533 mutex_lock(&bridge_lock);
535 list_for_each_entry(bridge, &bridge_list, list) {
536 if (bridge->of_node == np) {
537 mutex_unlock(&bridge_lock);
538 return bridge;
542 mutex_unlock(&bridge_lock);
543 return NULL;
545 EXPORT_SYMBOL(of_drm_find_bridge);
546 #endif
548 MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
549 MODULE_DESCRIPTION("DRM bridge infrastructure");
550 MODULE_LICENSE("GPL and additional rights");