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