blob: ae5e57ad718c13641629aafff43bb278f41b3208 [file] [log] [blame]
Ajay Kumar3d3f8b12015-01-20 22:08:44 +05301/*
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>
Daniel Vetter199e4e92016-08-31 18:09:05 +020026#include <linux/mutex.h>
Ajay Kumar3d3f8b12015-01-20 22:08:44 +053027
Daniel Vetter199e4e92016-08-31 18:09:05 +020028#include <drm/drm_bridge.h>
Laurent Pinchart3bb80f22016-11-28 17:59:08 +020029#include <drm/drm_encoder.h>
Ajay Kumar3d3f8b12015-01-20 22:08:44 +053030
Laurent Pinchart4a878c02016-11-28 18:32:05 +020031#include "drm_crtc_internal.h"
32
Archit Taneja2331b4e2015-05-21 11:03:17 +053033/**
34 * DOC: overview
35 *
Daniel Vetterea0dd852016-12-29 21:48:26 +010036 * &struct drm_bridge represents a device that hangs on to an encoder. These are
Daniel Vetterda024fe2015-12-04 09:45:47 +010037 * handy when a regular &drm_encoder entity isn't enough to represent the entire
Archit Taneja2331b4e2015-05-21 11:03:17 +053038 * encoder chain.
39 *
Daniel Vetterda024fe2015-12-04 09:45:47 +010040 * A bridge is always attached to a single &drm_encoder at a time, but can be
Daniel Vetterda5335b2016-05-31 22:55:13 +020041 * either connected to it directly, or through an intermediate bridge::
Archit Taneja2331b4e2015-05-21 11:03:17 +053042 *
Daniel Vetterda024fe2015-12-04 09:45:47 +010043 * encoder ---> bridge B ---> bridge A
Archit Taneja2331b4e2015-05-21 11:03:17 +053044 *
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
Daniel Vetterda024fe2015-12-04 09:45:47 +010051 * through the ops provided in &drm_bridge_funcs.
Archit Taneja2331b4e2015-05-21 11:03:17 +053052 *
53 * drm_bridge, like drm_panel, aren't drm_mode_object entities like planes,
Daniel Vetterda024fe2015-12-04 09:45:47 +010054 * 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 *
Daniel Vetterea0dd852016-12-29 21:48:26 +010058 * Bridges can also be chained up using the next pointer in &struct drm_bridge.
Daniel Vetterda024fe2015-12-04 09:45:47 +010059 *
60 * Both legacy CRTC helpers and the new atomic modeset helpers support bridges.
Archit Taneja2331b4e2015-05-21 11:03:17 +053061 */
62
Ajay Kumar3d3f8b12015-01-20 22:08:44 +053063static DEFINE_MUTEX(bridge_lock);
64static LIST_HEAD(bridge_list);
65
Archit Taneja2331b4e2015-05-21 11:03:17 +053066/**
67 * drm_bridge_add - add the given bridge to the global bridge list
68 *
69 * @bridge: bridge control structure
70 *
71 * RETURNS:
72 * Unconditionally returns Zero.
73 */
Ajay Kumar3d3f8b12015-01-20 22:08:44 +053074int drm_bridge_add(struct drm_bridge *bridge)
75{
76 mutex_lock(&bridge_lock);
77 list_add_tail(&bridge->list, &bridge_list);
78 mutex_unlock(&bridge_lock);
79
80 return 0;
81}
82EXPORT_SYMBOL(drm_bridge_add);
83
Archit Taneja2331b4e2015-05-21 11:03:17 +053084/**
85 * drm_bridge_remove - remove the given bridge from the global bridge list
86 *
87 * @bridge: bridge control structure
88 */
Ajay Kumar3d3f8b12015-01-20 22:08:44 +053089void drm_bridge_remove(struct drm_bridge *bridge)
90{
91 mutex_lock(&bridge_lock);
92 list_del_init(&bridge->list);
93 mutex_unlock(&bridge_lock);
94}
95EXPORT_SYMBOL(drm_bridge_remove);
96
Archit Taneja2331b4e2015-05-21 11:03:17 +053097/**
Laurent Pinchart3bb80f22016-11-28 17:59:08 +020098 * drm_bridge_attach - attach the bridge to an encoder's chain
Archit Taneja2331b4e2015-05-21 11:03:17 +053099 *
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200100 * @encoder: DRM encoder
101 * @bridge: bridge to attach
102 * @previous: previous bridge in the chain (optional)
Archit Taneja2331b4e2015-05-21 11:03:17 +0530103 *
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200104 * Called by a kms driver to link the bridge to an encoder's chain. The previous
105 * argument specifies the previous bridge in the chain. If NULL, the bridge is
106 * linked directly at the encoder's output. Otherwise it is linked at the
107 * previous bridge's output.
Archit Taneja2331b4e2015-05-21 11:03:17 +0530108 *
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200109 * If non-NULL the previous bridge must be already attached by a call to this
110 * function.
Archit Taneja2331b4e2015-05-21 11:03:17 +0530111 *
112 * RETURNS:
113 * Zero on success, error code on failure
114 */
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200115int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
116 struct drm_bridge *previous)
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530117{
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200118 int ret;
119
120 if (!encoder || !bridge)
121 return -EINVAL;
122
123 if (previous && (!previous->dev || previous->encoder != encoder))
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530124 return -EINVAL;
125
126 if (bridge->dev)
127 return -EBUSY;
128
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200129 bridge->dev = encoder->dev;
130 bridge->encoder = encoder;
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530131
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200132 if (bridge->funcs->attach) {
133 ret = bridge->funcs->attach(bridge);
134 if (ret < 0) {
135 bridge->dev = NULL;
136 bridge->encoder = NULL;
137 return ret;
138 }
139 }
140
141 if (previous)
142 previous->next = bridge;
143 else
144 encoder->bridge = bridge;
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530145
146 return 0;
147}
148EXPORT_SYMBOL(drm_bridge_attach);
149
Andrea Merellocf3bef92016-08-25 11:04:32 +0200150void 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 bridge->dev = NULL;
162}
Andrea Merellocf3bef92016-08-25 11:04:32 +0200163
164/**
Archit Taneja2331b4e2015-05-21 11:03:17 +0530165 * DOC: bridge callbacks
166 *
Daniel Vetterda024fe2015-12-04 09:45:47 +0100167 * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM
168 * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c
169 * These helpers call a specific &drm_bridge_funcs op for all the bridges
Archit Taneja2331b4e2015-05-21 11:03:17 +0530170 * during encoder configuration.
171 *
Daniel Vetterda024fe2015-12-04 09:45:47 +0100172 * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
Archit Taneja2331b4e2015-05-21 11:03:17 +0530173 */
174
175/**
Archit Taneja862e6862015-05-21 11:03:16 +0530176 * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the
177 * encoder chain
178 * @bridge: bridge control structure
179 * @mode: desired mode to be set for the bridge
180 * @adjusted_mode: updated mode that works for this bridge
181 *
Daniel Vetterda024fe2015-12-04 09:45:47 +0100182 * Calls ->mode_fixup() &drm_bridge_funcs op for all the bridges in the
Archit Taneja862e6862015-05-21 11:03:16 +0530183 * encoder chain, starting from the first bridge to the last.
184 *
185 * Note: the bridge passed should be the one closest to the encoder
186 *
187 * RETURNS:
188 * true on success, false on failure
189 */
190bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
191 const struct drm_display_mode *mode,
192 struct drm_display_mode *adjusted_mode)
193{
194 bool ret = true;
195
196 if (!bridge)
197 return true;
198
199 if (bridge->funcs->mode_fixup)
200 ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode);
201
202 ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode);
203
204 return ret;
205}
206EXPORT_SYMBOL(drm_bridge_mode_fixup);
207
208/**
Daniel Vetterda024fe2015-12-04 09:45:47 +0100209 * drm_bridge_disable - calls ->disable() &drm_bridge_funcs op for all
Archit Taneja862e6862015-05-21 11:03:16 +0530210 * bridges in the encoder chain.
211 * @bridge: bridge control structure
212 *
Daniel Vetterda024fe2015-12-04 09:45:47 +0100213 * Calls ->disable() &drm_bridge_funcs op for all the bridges in the encoder
Archit Taneja862e6862015-05-21 11:03:16 +0530214 * chain, starting from the last bridge to the first. These are called before
215 * calling the encoder's prepare op.
216 *
217 * Note: the bridge passed should be the one closest to the encoder
218 */
219void drm_bridge_disable(struct drm_bridge *bridge)
220{
221 if (!bridge)
222 return;
223
224 drm_bridge_disable(bridge->next);
225
Laurent Pinchartc8a3b2a2016-02-26 11:51:06 +0200226 if (bridge->funcs->disable)
227 bridge->funcs->disable(bridge);
Archit Taneja862e6862015-05-21 11:03:16 +0530228}
229EXPORT_SYMBOL(drm_bridge_disable);
230
231/**
Daniel Vetterda024fe2015-12-04 09:45:47 +0100232 * drm_bridge_post_disable - calls ->post_disable() &drm_bridge_funcs op for
Archit Taneja862e6862015-05-21 11:03:16 +0530233 * all bridges in the encoder chain.
234 * @bridge: bridge control structure
235 *
Daniel Vetterda024fe2015-12-04 09:45:47 +0100236 * Calls ->post_disable() &drm_bridge_funcs op for all the bridges in the
Archit Taneja862e6862015-05-21 11:03:16 +0530237 * encoder chain, starting from the first bridge to the last. These are called
238 * after completing the encoder's prepare op.
239 *
240 * Note: the bridge passed should be the one closest to the encoder
241 */
242void drm_bridge_post_disable(struct drm_bridge *bridge)
243{
244 if (!bridge)
245 return;
246
Laurent Pinchartc8a3b2a2016-02-26 11:51:06 +0200247 if (bridge->funcs->post_disable)
248 bridge->funcs->post_disable(bridge);
Archit Taneja862e6862015-05-21 11:03:16 +0530249
250 drm_bridge_post_disable(bridge->next);
251}
252EXPORT_SYMBOL(drm_bridge_post_disable);
253
254/**
255 * drm_bridge_mode_set - set proposed mode for all bridges in the
256 * encoder chain
257 * @bridge: bridge control structure
258 * @mode: desired mode to be set for the bridge
259 * @adjusted_mode: updated mode that works for this bridge
260 *
Daniel Vetterda024fe2015-12-04 09:45:47 +0100261 * Calls ->mode_set() &drm_bridge_funcs op for all the bridges in the
Archit Taneja862e6862015-05-21 11:03:16 +0530262 * encoder chain, starting from the first bridge to the last.
263 *
264 * Note: the bridge passed should be the one closest to the encoder
265 */
266void drm_bridge_mode_set(struct drm_bridge *bridge,
267 struct drm_display_mode *mode,
268 struct drm_display_mode *adjusted_mode)
269{
270 if (!bridge)
271 return;
272
273 if (bridge->funcs->mode_set)
274 bridge->funcs->mode_set(bridge, mode, adjusted_mode);
275
276 drm_bridge_mode_set(bridge->next, mode, adjusted_mode);
277}
278EXPORT_SYMBOL(drm_bridge_mode_set);
279
280/**
Daniel Vetterda024fe2015-12-04 09:45:47 +0100281 * drm_bridge_pre_enable - calls ->pre_enable() &drm_bridge_funcs op for all
Archit Taneja862e6862015-05-21 11:03:16 +0530282 * bridges in the encoder chain.
283 * @bridge: bridge control structure
284 *
Daniel Vetterda024fe2015-12-04 09:45:47 +0100285 * Calls ->pre_enable() &drm_bridge_funcs op for all the bridges in the encoder
Archit Taneja862e6862015-05-21 11:03:16 +0530286 * chain, starting from the last bridge to the first. These are called
287 * before calling the encoder's commit op.
288 *
289 * Note: the bridge passed should be the one closest to the encoder
290 */
291void drm_bridge_pre_enable(struct drm_bridge *bridge)
292{
293 if (!bridge)
294 return;
295
296 drm_bridge_pre_enable(bridge->next);
297
Laurent Pinchartc8a3b2a2016-02-26 11:51:06 +0200298 if (bridge->funcs->pre_enable)
299 bridge->funcs->pre_enable(bridge);
Archit Taneja862e6862015-05-21 11:03:16 +0530300}
301EXPORT_SYMBOL(drm_bridge_pre_enable);
302
303/**
Daniel Vetterda024fe2015-12-04 09:45:47 +0100304 * drm_bridge_enable - calls ->enable() &drm_bridge_funcs op for all bridges
Archit Taneja862e6862015-05-21 11:03:16 +0530305 * in the encoder chain.
306 * @bridge: bridge control structure
307 *
Daniel Vetterda024fe2015-12-04 09:45:47 +0100308 * Calls ->enable() &drm_bridge_funcs op for all the bridges in the encoder
Archit Taneja862e6862015-05-21 11:03:16 +0530309 * chain, starting from the first bridge to the last. These are called
310 * after completing the encoder's commit op.
311 *
312 * Note that the bridge passed should be the one closest to the encoder
313 */
314void drm_bridge_enable(struct drm_bridge *bridge)
315{
316 if (!bridge)
317 return;
318
Laurent Pinchartc8a3b2a2016-02-26 11:51:06 +0200319 if (bridge->funcs->enable)
320 bridge->funcs->enable(bridge);
Archit Taneja862e6862015-05-21 11:03:16 +0530321
322 drm_bridge_enable(bridge->next);
323}
324EXPORT_SYMBOL(drm_bridge_enable);
325
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530326#ifdef CONFIG_OF
Archit Taneja2331b4e2015-05-21 11:03:17 +0530327/**
328 * of_drm_find_bridge - find the bridge corresponding to the device node in
329 * the global bridge list
330 *
331 * @np: device node
332 *
333 * RETURNS:
334 * drm_bridge control struct on success, NULL on failure
335 */
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530336struct drm_bridge *of_drm_find_bridge(struct device_node *np)
337{
338 struct drm_bridge *bridge;
339
340 mutex_lock(&bridge_lock);
341
342 list_for_each_entry(bridge, &bridge_list, list) {
343 if (bridge->of_node == np) {
344 mutex_unlock(&bridge_lock);
345 return bridge;
346 }
347 }
348
349 mutex_unlock(&bridge_lock);
350 return NULL;
351}
352EXPORT_SYMBOL(of_drm_find_bridge);
353#endif
354
355MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
356MODULE_DESCRIPTION("DRM bridge infrastructure");
357MODULE_LICENSE("GPL and additional rights");