xref: /linux/include/drm/drm_panel.h (revision aa8a8c7e4641ebcef02dd6fa387ca334857367f4)
1 /*
2  * Copyright (C) 2013, NVIDIA Corporation.  All rights reserved.
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 #ifndef __DRM_PANEL_H__
25 #define __DRM_PANEL_H__
26 
27 #include <linux/err.h>
28 #include <linux/errno.h>
29 #include <linux/list.h>
30 #include <linux/mutex.h>
31 #include <linux/kref.h>
32 
33 struct backlight_device;
34 struct dentry;
35 struct device_node;
36 struct drm_bridge;
37 struct drm_connector;
38 struct drm_device;
39 struct drm_panel_follower;
40 struct drm_panel;
41 struct display_timing;
42 
43 enum drm_panel_orientation;
44 
45 /**
46  * struct drm_panel_funcs - perform operations on a given panel
47  *
48  * The .prepare() function is typically called before the display controller
49  * starts to transmit video data. Panel drivers can use this to turn the panel
50  * on and wait for it to become ready. If additional configuration is required
51  * (via a control bus such as I2C, SPI or DSI for example) this is a good time
52  * to do that.
53  *
54  * After the display controller has started transmitting video data, it's safe
55  * to call the .enable() function. This will typically enable the backlight to
56  * make the image on screen visible. Some panels require a certain amount of
57  * time or frames before the image is displayed. This function is responsible
58  * for taking this into account before enabling the backlight to avoid visual
59  * glitches.
60  *
61  * Before stopping video transmission from the display controller it can be
62  * necessary to turn off the panel to avoid visual glitches. This is done in
63  * the .disable() function. Analogously to .enable() this typically involves
64  * turning off the backlight and waiting for some time to make sure no image
65  * is visible on the panel. It is then safe for the display controller to
66  * cease transmission of video data.
67  *
68  * To save power when no video data is transmitted, a driver can power down
69  * the panel. This is the job of the .unprepare() function.
70  *
71  * Backlight can be handled automatically if configured using
72  * drm_panel_of_backlight() or drm_panel_dp_aux_backlight(). Then the driver
73  * does not need to implement the functionality to enable/disable backlight.
74  */
75 struct drm_panel_funcs {
76 	/**
77 	 * @prepare:
78 	 *
79 	 * Turn on panel and perform set up.
80 	 *
81 	 * This function is optional.
82 	 */
83 	int (*prepare)(struct drm_panel *panel);
84 
85 	/**
86 	 * @enable:
87 	 *
88 	 * Enable panel (turn on back light, etc.).
89 	 *
90 	 * This function is optional.
91 	 */
92 	int (*enable)(struct drm_panel *panel);
93 
94 	/**
95 	 * @disable:
96 	 *
97 	 * Disable panel (turn off back light, etc.).
98 	 *
99 	 * This function is optional.
100 	 */
101 	int (*disable)(struct drm_panel *panel);
102 
103 	/**
104 	 * @unprepare:
105 	 *
106 	 * Turn off panel.
107 	 *
108 	 * This function is optional.
109 	 */
110 	int (*unprepare)(struct drm_panel *panel);
111 
112 	/**
113 	 * @get_modes:
114 	 *
115 	 * Add modes to the connector that the panel is attached to
116 	 * and returns the number of modes added.
117 	 *
118 	 * This function is mandatory.
119 	 */
120 	int (*get_modes)(struct drm_panel *panel,
121 			 struct drm_connector *connector);
122 
123 	/**
124 	 * @get_orientation:
125 	 *
126 	 * Return the panel orientation set by device tree or EDID.
127 	 *
128 	 * This function is optional.
129 	 */
130 	enum drm_panel_orientation (*get_orientation)(struct drm_panel *panel);
131 
132 	/**
133 	 * @get_timings:
134 	 *
135 	 * Copy display timings into the provided array and return
136 	 * the number of display timings available.
137 	 *
138 	 * This function is optional.
139 	 */
140 	int (*get_timings)(struct drm_panel *panel, unsigned int num_timings,
141 			   struct display_timing *timings);
142 
143 	/**
144 	 * @debugfs_init:
145 	 *
146 	 * Allows panels to create panels-specific debugfs files.
147 	 */
148 	void (*debugfs_init)(struct drm_panel *panel, struct dentry *root);
149 };
150 
151 struct drm_panel_follower_funcs {
152 	/**
153 	 * @panel_prepared:
154 	 *
155 	 * Called after the panel has been powered on.
156 	 */
157 	int (*panel_prepared)(struct drm_panel_follower *follower);
158 
159 	/**
160 	 * @panel_unpreparing:
161 	 *
162 	 * Called before the panel is powered off.
163 	 */
164 	int (*panel_unpreparing)(struct drm_panel_follower *follower);
165 
166 	/**
167 	 * @panel_enabled:
168 	 *
169 	 * Called after the panel and the backlight have been enabled.
170 	 */
171 	int (*panel_enabled)(struct drm_panel_follower *follower);
172 
173 	/**
174 	 * @panel_disabling:
175 	 *
176 	 * Called before the panel and the backlight are disabled.
177 	 */
178 	int (*panel_disabling)(struct drm_panel_follower *follower);
179 };
180 
181 struct drm_panel_follower {
182 	/**
183 	 * @funcs:
184 	 *
185 	 * Dependent device callbacks; should be initted by the caller.
186 	 */
187 	const struct drm_panel_follower_funcs *funcs;
188 
189 	/**
190 	 * @list
191 	 *
192 	 * Used for linking into panel's list; set by drm_panel_add_follower().
193 	 */
194 	struct list_head list;
195 
196 	/**
197 	 * @panel
198 	 *
199 	 * The panel we're dependent on; set by drm_panel_add_follower().
200 	 */
201 	struct drm_panel *panel;
202 };
203 
204 /**
205  * struct drm_panel - DRM panel object
206  */
207 struct drm_panel {
208 	/**
209 	 * @dev:
210 	 *
211 	 * Parent device of the panel.
212 	 */
213 	struct device *dev;
214 
215 	/**
216 	 * @backlight:
217 	 *
218 	 * Backlight device, used to turn on backlight after the call
219 	 * to enable(), and to turn off backlight before the call to
220 	 * disable().
221 	 * backlight is set by drm_panel_of_backlight() or
222 	 * drm_panel_dp_aux_backlight() and drivers shall not assign it.
223 	 */
224 	struct backlight_device *backlight;
225 
226 	/**
227 	 * @funcs:
228 	 *
229 	 * Operations that can be performed on the panel.
230 	 */
231 	const struct drm_panel_funcs *funcs;
232 
233 	/**
234 	 * @connector_type:
235 	 *
236 	 * Type of the panel as a DRM_MODE_CONNECTOR_* value. This is used to
237 	 * initialise the drm_connector corresponding to the panel with the
238 	 * correct connector type.
239 	 */
240 	int connector_type;
241 
242 	/**
243 	 * @list:
244 	 *
245 	 * Panel entry in registry.
246 	 */
247 	struct list_head list;
248 
249 	/**
250 	 * @followers:
251 	 *
252 	 * A list of struct drm_panel_follower dependent on this panel.
253 	 */
254 	struct list_head followers;
255 
256 	/**
257 	 * @follower_lock:
258 	 *
259 	 * Lock for followers list.
260 	 */
261 	struct mutex follower_lock;
262 
263 	/**
264 	 * @prepare_prev_first:
265 	 *
266 	 * The previous controller should be prepared first, before the prepare
267 	 * for the panel is called. This is largely required for DSI panels
268 	 * where the DSI host controller should be initialised to LP-11 before
269 	 * the panel is powered up.
270 	 */
271 	bool prepare_prev_first;
272 
273 	/**
274 	 * @prepared:
275 	 *
276 	 * If true then the panel has been prepared.
277 	 */
278 	bool prepared;
279 
280 	/**
281 	 * @enabled:
282 	 *
283 	 * If true then the panel has been enabled.
284 	 */
285 	bool enabled;
286 
287 	/**
288 	 * @container: Pointer to the private driver struct embedding this
289 	 * @struct drm_panel.
290 	 */
291 	void *container;
292 
293 	/**
294 	 * @refcount: reference count of users referencing this panel.
295 	 */
296 	struct kref refcount;
297 };
298 
299 void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t offset,
300 			     const struct drm_panel_funcs *funcs,
301 			     int connector_type);
302 
303 /**
304  * devm_drm_panel_alloc - Allocate and initialize a refcounted panel.
305  *
306  * @dev: struct device of the panel device
307  * @type: the type of the struct which contains struct &drm_panel
308  * @member: the name of the &drm_panel within @type
309  * @funcs: callbacks for this panel
310  * @connector_type: the connector type (DRM_MODE_CONNECTOR_*) corresponding to
311  * the panel interface
312  *
313  * The reference count of the returned panel is initialized to 1. This
314  * reference will be automatically dropped via devm (by calling
315  * drm_panel_put()) when @dev is removed.
316  *
317  * Returns:
318  * Pointer to container structure embedding the panel, ERR_PTR on failure.
319  */
320 #define devm_drm_panel_alloc(dev, type, member, funcs, connector_type) \
321 	((type *)__devm_drm_panel_alloc(dev, sizeof(type), \
322 					offsetof(type, member), funcs, \
323 					connector_type))
324 
325 struct drm_panel *drm_panel_get(struct drm_panel *panel);
326 void drm_panel_put(struct drm_panel *panel);
327 
328 void drm_panel_add(struct drm_panel *panel);
329 void drm_panel_remove(struct drm_panel *panel);
330 int devm_drm_panel_add(struct device *dev, struct drm_panel *panel);
331 
332 void drm_panel_prepare(struct drm_panel *panel);
333 void drm_panel_unprepare(struct drm_panel *panel);
334 
335 void drm_panel_enable(struct drm_panel *panel);
336 void drm_panel_disable(struct drm_panel *panel);
337 
338 int drm_panel_get_modes(struct drm_panel *panel, struct drm_connector *connector);
339 
340 #if defined(CONFIG_OF) && IS_ENABLED(CONFIG_DRM_PANEL)
341 struct drm_panel *of_drm_find_panel(const struct device_node *np);
342 int drm_of_find_panel_or_bridge(const struct device_node *np,
343 				int port, int endpoint,
344 				struct drm_panel **panel,
345 				struct drm_bridge **bridge);
346 #else
347 static inline struct drm_panel *of_drm_find_panel(const struct device_node *np)
348 {
349 	return ERR_PTR(-ENODEV);
350 }
351 
352 static inline int drm_of_find_panel_or_bridge(const struct device_node *np,
353 					      int port, int endpoint,
354 					      struct drm_panel **panel,
355 					      struct drm_bridge **bridge)
356 {
357 	return -EINVAL;
358 }
359 #endif
360 
361 #if IS_ENABLED(CONFIG_DRM_PANEL)
362 bool drm_is_panel_follower(struct device *dev);
363 int drm_panel_add_follower(struct device *follower_dev,
364 			   struct drm_panel_follower *follower);
365 void drm_panel_remove_follower(struct drm_panel_follower *follower);
366 int devm_drm_panel_add_follower(struct device *follower_dev,
367 				struct drm_panel_follower *follower);
368 #else
369 static inline bool drm_is_panel_follower(struct device *dev)
370 {
371 	return false;
372 }
373 
374 static inline int drm_panel_add_follower(struct device *follower_dev,
375 					 struct drm_panel_follower *follower)
376 {
377 	return -ENODEV;
378 }
379 
380 static inline void drm_panel_remove_follower(struct drm_panel_follower *follower) { }
381 static inline int devm_drm_panel_add_follower(struct device *follower_dev,
382 					      struct drm_panel_follower *follower)
383 {
384 	return -ENODEV;
385 }
386 #endif
387 
388 #if IS_ENABLED(CONFIG_DRM_PANEL) && (IS_BUILTIN(CONFIG_BACKLIGHT_CLASS_DEVICE) || \
389 	(IS_MODULE(CONFIG_DRM) && IS_MODULE(CONFIG_BACKLIGHT_CLASS_DEVICE)))
390 int drm_panel_of_backlight(struct drm_panel *panel);
391 #else
392 static inline int drm_panel_of_backlight(struct drm_panel *panel)
393 {
394 	return 0;
395 }
396 #endif
397 
398 #ifdef CONFIG_DRM_PANEL_BRIDGE
399 bool drm_bridge_is_panel(const struct drm_bridge *bridge);
400 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel);
401 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
402 					      u32 connector_type);
403 void drm_panel_bridge_remove(struct drm_bridge *bridge);
404 int drm_panel_bridge_set_orientation(struct drm_connector *connector,
405 				     struct drm_bridge *bridge);
406 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
407 					     struct drm_panel *panel);
408 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
409 						   struct drm_panel *panel,
410 						   u32 connector_type);
411 struct drm_bridge *drmm_panel_bridge_add(struct drm_device *drm,
412 					 struct drm_panel *panel);
413 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge);
414 #else
415 static inline bool drm_bridge_is_panel(const struct drm_bridge *bridge)
416 {
417 	return false;
418 }
419 
420 static inline int drm_panel_bridge_set_orientation(struct drm_connector *connector,
421 						   struct drm_bridge *bridge)
422 {
423 	return -EINVAL;
424 }
425 #endif
426 
427 #if defined(CONFIG_OF) && defined(CONFIG_DRM_PANEL_BRIDGE)
428 struct drm_bridge *devm_drm_of_get_bridge(struct device *dev, struct device_node *node,
429 					  u32 port, u32 endpoint);
430 struct drm_bridge *drmm_of_get_bridge(struct drm_device *drm, struct device_node *node,
431 				      u32 port, u32 endpoint);
432 #else
433 static inline struct drm_bridge *devm_drm_of_get_bridge(struct device *dev,
434 							struct device_node *node,
435 							u32 port,
436 							u32 endpoint)
437 {
438 	return ERR_PTR(-ENODEV);
439 }
440 
441 static inline struct drm_bridge *drmm_of_get_bridge(struct drm_device *drm,
442 						    struct device_node *node,
443 						    u32 port,
444 						    u32 endpoint)
445 {
446 	return ERR_PTR(-ENODEV);
447 }
448 #endif
449 
450 #endif
451