blob: 1638bfe9627ce21c08ccb3e17b868d13c466bb16 [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 Vetter4541d312017-01-02 09:17:26 +010058 * Bridges can also be chained up using the &drm_bridge.next pointer.
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
Archit Taneja2331b4e2015-05-21 11:03:17 +053070 */
Inki Dae992868842017-07-03 17:42:17 +090071void drm_bridge_add(struct drm_bridge *bridge)
Ajay Kumar3d3f8b12015-01-20 22:08:44 +053072{
73 mutex_lock(&bridge_lock);
74 list_add_tail(&bridge->list, &bridge_list);
75 mutex_unlock(&bridge_lock);
Ajay Kumar3d3f8b12015-01-20 22:08:44 +053076}
77EXPORT_SYMBOL(drm_bridge_add);
78
Archit Taneja2331b4e2015-05-21 11:03:17 +053079/**
80 * drm_bridge_remove - remove the given bridge from the global bridge list
81 *
82 * @bridge: bridge control structure
83 */
Ajay Kumar3d3f8b12015-01-20 22:08:44 +053084void 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}
90EXPORT_SYMBOL(drm_bridge_remove);
91
Archit Taneja2331b4e2015-05-21 11:03:17 +053092/**
Laurent Pinchart3bb80f22016-11-28 17:59:08 +020093 * drm_bridge_attach - attach the bridge to an encoder's chain
Archit Taneja2331b4e2015-05-21 11:03:17 +053094 *
Laurent Pinchart3bb80f22016-11-28 17:59:08 +020095 * @encoder: DRM encoder
96 * @bridge: bridge to attach
97 * @previous: previous bridge in the chain (optional)
Archit Taneja2331b4e2015-05-21 11:03:17 +053098 *
Laurent Pinchart3bb80f22016-11-28 17:59:08 +020099 * 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.
Archit Taneja2331b4e2015-05-21 11:03:17 +0530103 *
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200104 * If non-NULL the previous bridge must be already attached by a call to this
105 * function.
Archit Taneja2331b4e2015-05-21 11:03:17 +0530106 *
107 * RETURNS:
108 * Zero on success, error code on failure
109 */
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200110int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
111 struct drm_bridge *previous)
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530112{
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200113 int ret;
114
115 if (!encoder || !bridge)
116 return -EINVAL;
117
118 if (previous && (!previous->dev || previous->encoder != encoder))
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530119 return -EINVAL;
120
121 if (bridge->dev)
122 return -EBUSY;
123
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200124 bridge->dev = encoder->dev;
125 bridge->encoder = encoder;
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530126
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200127 if (bridge->funcs->attach) {
128 ret = bridge->funcs->attach(bridge);
129 if (ret < 0) {
130 bridge->dev = NULL;
131 bridge->encoder = NULL;
132 return ret;
133 }
134 }
135
136 if (previous)
137 previous->next = bridge;
138 else
139 encoder->bridge = bridge;
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530140
141 return 0;
142}
143EXPORT_SYMBOL(drm_bridge_attach);
144
Andrea Merellocf3bef92016-08-25 11:04:32 +0200145void drm_bridge_detach(struct drm_bridge *bridge)
146{
147 if (WARN_ON(!bridge))
148 return;
149
150 if (WARN_ON(!bridge->dev))
151 return;
152
153 if (bridge->funcs->detach)
154 bridge->funcs->detach(bridge);
155
156 bridge->dev = NULL;
157}
Andrea Merellocf3bef92016-08-25 11:04:32 +0200158
159/**
Archit Taneja2331b4e2015-05-21 11:03:17 +0530160 * DOC: bridge callbacks
161 *
Daniel Vetterda024fe2015-12-04 09:45:47 +0100162 * The &drm_bridge_funcs ops are populated by the bridge driver. The DRM
163 * internals (atomic and CRTC helpers) use the helpers defined in drm_bridge.c
164 * These helpers call a specific &drm_bridge_funcs op for all the bridges
Archit Taneja2331b4e2015-05-21 11:03:17 +0530165 * during encoder configuration.
166 *
Daniel Vetterda024fe2015-12-04 09:45:47 +0100167 * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
Archit Taneja2331b4e2015-05-21 11:03:17 +0530168 */
169
170/**
Archit Taneja862e6862015-05-21 11:03:16 +0530171 * drm_bridge_mode_fixup - fixup proposed mode for all bridges in the
172 * encoder chain
173 * @bridge: bridge control structure
174 * @mode: desired mode to be set for the bridge
175 * @adjusted_mode: updated mode that works for this bridge
176 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100177 * Calls &drm_bridge_funcs.mode_fixup for all the bridges in the
Archit Taneja862e6862015-05-21 11:03:16 +0530178 * encoder chain, starting from the first bridge to the last.
179 *
180 * Note: the bridge passed should be the one closest to the encoder
181 *
182 * RETURNS:
183 * true on success, false on failure
184 */
185bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
186 const struct drm_display_mode *mode,
187 struct drm_display_mode *adjusted_mode)
188{
189 bool ret = true;
190
191 if (!bridge)
192 return true;
193
194 if (bridge->funcs->mode_fixup)
195 ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode);
196
197 ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode);
198
199 return ret;
200}
201EXPORT_SYMBOL(drm_bridge_mode_fixup);
202
203/**
Jose Abreub1240f82017-05-25 15:19:14 +0100204 * drm_bridge_mode_valid - validate the mode against all bridges in the
205 * encoder chain.
206 * @bridge: bridge control structure
207 * @mode: desired mode to be validated
208 *
209 * Calls &drm_bridge_funcs.mode_valid for all the bridges in the encoder
210 * chain, starting from the first bridge to the last. If at least one bridge
211 * does not accept the mode the function returns the error code.
212 *
213 * Note: the bridge passed should be the one closest to the encoder.
214 *
215 * RETURNS:
216 * MODE_OK on success, drm_mode_status Enum error code on failure
217 */
218enum drm_mode_status drm_bridge_mode_valid(struct drm_bridge *bridge,
219 const struct drm_display_mode *mode)
220{
221 enum drm_mode_status ret = MODE_OK;
222
223 if (!bridge)
224 return ret;
225
226 if (bridge->funcs->mode_valid)
227 ret = bridge->funcs->mode_valid(bridge, mode);
228
229 if (ret != MODE_OK)
230 return ret;
231
232 return drm_bridge_mode_valid(bridge->next, mode);
233}
234EXPORT_SYMBOL(drm_bridge_mode_valid);
235
236/**
Daniel Vetter4541d312017-01-02 09:17:26 +0100237 * drm_bridge_disable - disables all bridges in the encoder chain
Archit Taneja862e6862015-05-21 11:03:16 +0530238 * @bridge: bridge control structure
239 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100240 * Calls &drm_bridge_funcs.disable op for all the bridges in the encoder
Archit Taneja862e6862015-05-21 11:03:16 +0530241 * chain, starting from the last bridge to the first. These are called before
242 * calling the encoder's prepare op.
243 *
244 * Note: the bridge passed should be the one closest to the encoder
245 */
246void drm_bridge_disable(struct drm_bridge *bridge)
247{
248 if (!bridge)
249 return;
250
251 drm_bridge_disable(bridge->next);
252
Laurent Pinchartc8a3b2a2016-02-26 11:51:06 +0200253 if (bridge->funcs->disable)
254 bridge->funcs->disable(bridge);
Archit Taneja862e6862015-05-21 11:03:16 +0530255}
256EXPORT_SYMBOL(drm_bridge_disable);
257
258/**
Daniel Vetter4541d312017-01-02 09:17:26 +0100259 * drm_bridge_post_disable - cleans up after disabling all bridges in the encoder chain
Archit Taneja862e6862015-05-21 11:03:16 +0530260 * @bridge: bridge control structure
261 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100262 * Calls &drm_bridge_funcs.post_disable op for all the bridges in the
Archit Taneja862e6862015-05-21 11:03:16 +0530263 * encoder chain, starting from the first bridge to the last. These are called
264 * after completing the encoder's prepare op.
265 *
266 * Note: the bridge passed should be the one closest to the encoder
267 */
268void drm_bridge_post_disable(struct drm_bridge *bridge)
269{
270 if (!bridge)
271 return;
272
Laurent Pinchartc8a3b2a2016-02-26 11:51:06 +0200273 if (bridge->funcs->post_disable)
274 bridge->funcs->post_disable(bridge);
Archit Taneja862e6862015-05-21 11:03:16 +0530275
276 drm_bridge_post_disable(bridge->next);
277}
278EXPORT_SYMBOL(drm_bridge_post_disable);
279
280/**
281 * drm_bridge_mode_set - set proposed mode for all bridges in the
282 * encoder chain
283 * @bridge: bridge control structure
284 * @mode: desired mode to be set for the bridge
285 * @adjusted_mode: updated mode that works for this bridge
286 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100287 * Calls &drm_bridge_funcs.mode_set op for all the bridges in the
Archit Taneja862e6862015-05-21 11:03:16 +0530288 * encoder chain, starting from the first bridge to the last.
289 *
290 * Note: the bridge passed should be the one closest to the encoder
291 */
292void drm_bridge_mode_set(struct drm_bridge *bridge,
293 struct drm_display_mode *mode,
294 struct drm_display_mode *adjusted_mode)
295{
296 if (!bridge)
297 return;
298
299 if (bridge->funcs->mode_set)
300 bridge->funcs->mode_set(bridge, mode, adjusted_mode);
301
302 drm_bridge_mode_set(bridge->next, mode, adjusted_mode);
303}
304EXPORT_SYMBOL(drm_bridge_mode_set);
305
306/**
Daniel Vetter4541d312017-01-02 09:17:26 +0100307 * drm_bridge_pre_enable - prepares for enabling all
308 * bridges in the encoder chain
Archit Taneja862e6862015-05-21 11:03:16 +0530309 * @bridge: bridge control structure
310 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100311 * Calls &drm_bridge_funcs.pre_enable op for all the bridges in the encoder
Archit Taneja862e6862015-05-21 11:03:16 +0530312 * chain, starting from the last bridge to the first. These are called
313 * before calling the encoder's commit op.
314 *
315 * Note: the bridge passed should be the one closest to the encoder
316 */
317void drm_bridge_pre_enable(struct drm_bridge *bridge)
318{
319 if (!bridge)
320 return;
321
322 drm_bridge_pre_enable(bridge->next);
323
Laurent Pinchartc8a3b2a2016-02-26 11:51:06 +0200324 if (bridge->funcs->pre_enable)
325 bridge->funcs->pre_enable(bridge);
Archit Taneja862e6862015-05-21 11:03:16 +0530326}
327EXPORT_SYMBOL(drm_bridge_pre_enable);
328
329/**
Daniel Vetter4541d312017-01-02 09:17:26 +0100330 * drm_bridge_enable - enables all bridges in the encoder chain
Archit Taneja862e6862015-05-21 11:03:16 +0530331 * @bridge: bridge control structure
332 *
Daniel Vetter4541d312017-01-02 09:17:26 +0100333 * Calls &drm_bridge_funcs.enable op for all the bridges in the encoder
Archit Taneja862e6862015-05-21 11:03:16 +0530334 * chain, starting from the first bridge to the last. These are called
335 * after completing the encoder's commit op.
336 *
337 * Note that the bridge passed should be the one closest to the encoder
338 */
339void drm_bridge_enable(struct drm_bridge *bridge)
340{
341 if (!bridge)
342 return;
343
Laurent Pinchartc8a3b2a2016-02-26 11:51:06 +0200344 if (bridge->funcs->enable)
345 bridge->funcs->enable(bridge);
Archit Taneja862e6862015-05-21 11:03:16 +0530346
347 drm_bridge_enable(bridge->next);
348}
349EXPORT_SYMBOL(drm_bridge_enable);
350
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530351#ifdef CONFIG_OF
Archit Taneja2331b4e2015-05-21 11:03:17 +0530352/**
353 * of_drm_find_bridge - find the bridge corresponding to the device node in
354 * the global bridge list
355 *
356 * @np: device node
357 *
358 * RETURNS:
359 * drm_bridge control struct on success, NULL on failure
360 */
Ajay Kumar3d3f8b12015-01-20 22:08:44 +0530361struct drm_bridge *of_drm_find_bridge(struct device_node *np)
362{
363 struct drm_bridge *bridge;
364
365 mutex_lock(&bridge_lock);
366
367 list_for_each_entry(bridge, &bridge_list, list) {
368 if (bridge->of_node == np) {
369 mutex_unlock(&bridge_lock);
370 return bridge;
371 }
372 }
373
374 mutex_unlock(&bridge_lock);
375 return NULL;
376}
377EXPORT_SYMBOL(of_drm_find_bridge);
378#endif
379
380MODULE_AUTHOR("Ajay Kumar <ajaykumar.rs@samsung.com>");
381MODULE_DESCRIPTION("DRM bridge infrastructure");
382MODULE_LICENSE("GPL and additional rights");