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 #include <linux/backlight.h>
25 #include <linux/err.h>
26 #include <linux/module.h>
27
28 #include <drm/drm_crtc.h>
29 #include <drm/drm_panel.h>
30 #include <drm/drm_print.h>
31
32 static DEFINE_MUTEX(panel_lock);
33 static LIST_HEAD(panel_list);
34
35 /**
36 * DOC: drm panel
37 *
38 * The DRM panel helpers allow drivers to register panel objects with a
39 * central registry and provide functions to retrieve those panels in display
40 * drivers.
41 *
42 * For easy integration into drivers using the &drm_bridge infrastructure please
43 * take look at drm_panel_bridge_add() and devm_drm_panel_bridge_add().
44 */
45
46 /**
47 * drm_panel_init - initialize a panel
48 * @panel: DRM panel
49 * @dev: parent device of the panel
50 * @funcs: panel operations
51 * @connector_type: the connector type (DRM_MODE_CONNECTOR_*) corresponding to
52 * the panel interface (must NOT be DRM_MODE_CONNECTOR_Unknown)
53 *
54 * Initialize the panel structure for subsequent registration with
55 * drm_panel_add().
56 */
drm_panel_init(struct drm_panel * panel,struct device * dev,const struct drm_panel_funcs * funcs,int connector_type)57 void drm_panel_init(struct drm_panel *panel, struct device *dev,
58 const struct drm_panel_funcs *funcs, int connector_type)
59 {
60 if (connector_type == DRM_MODE_CONNECTOR_Unknown)
61 DRM_WARN("%s: %s: a valid connector type is required!\n", __func__, dev_name(dev));
62
63 INIT_LIST_HEAD(&panel->list);
64 INIT_LIST_HEAD(&panel->followers);
65 mutex_init(&panel->follower_lock);
66 panel->dev = dev;
67 panel->funcs = funcs;
68 panel->connector_type = connector_type;
69 }
70 EXPORT_SYMBOL(drm_panel_init);
71
72 /**
73 * drm_panel_add - add a panel to the global registry
74 * @panel: panel to add
75 *
76 * Add a panel to the global registry so that it can be looked up by display
77 * drivers.
78 */
drm_panel_add(struct drm_panel * panel)79 void drm_panel_add(struct drm_panel *panel)
80 {
81 mutex_lock(&panel_lock);
82 list_add_tail(&panel->list, &panel_list);
83 mutex_unlock(&panel_lock);
84 }
85 EXPORT_SYMBOL(drm_panel_add);
86
87 /**
88 * drm_panel_remove - remove a panel from the global registry
89 * @panel: DRM panel
90 *
91 * Removes a panel from the global registry.
92 */
drm_panel_remove(struct drm_panel * panel)93 void drm_panel_remove(struct drm_panel *panel)
94 {
95 mutex_lock(&panel_lock);
96 list_del_init(&panel->list);
97 mutex_unlock(&panel_lock);
98 }
99 EXPORT_SYMBOL(drm_panel_remove);
100
101 /**
102 * drm_panel_prepare - power on a panel
103 * @panel: DRM panel
104 *
105 * Calling this function will enable power and deassert any reset signals to
106 * the panel. After this has completed it is possible to communicate with any
107 * integrated circuitry via a command bus.
108 *
109 * Return: 0 on success or a negative error code on failure.
110 */
drm_panel_prepare(struct drm_panel * panel)111 int drm_panel_prepare(struct drm_panel *panel)
112 {
113 struct drm_panel_follower *follower;
114 int ret;
115
116 if (!panel)
117 return -EINVAL;
118
119 if (panel->prepared) {
120 dev_warn(panel->dev, "Skipping prepare of already prepared panel\n");
121 return 0;
122 }
123
124 mutex_lock(&panel->follower_lock);
125
126 if (panel->funcs && panel->funcs->prepare) {
127 ret = panel->funcs->prepare(panel);
128 if (ret < 0)
129 goto exit;
130 }
131 panel->prepared = true;
132
133 list_for_each_entry(follower, &panel->followers, list) {
134 ret = follower->funcs->panel_prepared(follower);
135 if (ret < 0)
136 dev_info(panel->dev, "%ps failed: %d\n",
137 follower->funcs->panel_prepared, ret);
138 }
139
140 ret = 0;
141 exit:
142 mutex_unlock(&panel->follower_lock);
143
144 return ret;
145 }
146 EXPORT_SYMBOL(drm_panel_prepare);
147
148 /**
149 * drm_panel_unprepare - power off a panel
150 * @panel: DRM panel
151 *
152 * Calling this function will completely power off a panel (assert the panel's
153 * reset, turn off power supplies, ...). After this function has completed, it
154 * is usually no longer possible to communicate with the panel until another
155 * call to drm_panel_prepare().
156 *
157 * Return: 0 on success or a negative error code on failure.
158 */
drm_panel_unprepare(struct drm_panel * panel)159 int drm_panel_unprepare(struct drm_panel *panel)
160 {
161 struct drm_panel_follower *follower;
162 int ret;
163
164 if (!panel)
165 return -EINVAL;
166
167 if (!panel->prepared) {
168 dev_warn(panel->dev, "Skipping unprepare of already unprepared panel\n");
169 return 0;
170 }
171
172 mutex_lock(&panel->follower_lock);
173
174 list_for_each_entry(follower, &panel->followers, list) {
175 ret = follower->funcs->panel_unpreparing(follower);
176 if (ret < 0)
177 dev_info(panel->dev, "%ps failed: %d\n",
178 follower->funcs->panel_unpreparing, ret);
179 }
180
181 if (panel->funcs && panel->funcs->unprepare) {
182 ret = panel->funcs->unprepare(panel);
183 if (ret < 0)
184 goto exit;
185 }
186 panel->prepared = false;
187
188 ret = 0;
189 exit:
190 mutex_unlock(&panel->follower_lock);
191
192 return ret;
193 }
194 EXPORT_SYMBOL(drm_panel_unprepare);
195
196 /**
197 * drm_panel_enable - enable a panel
198 * @panel: DRM panel
199 *
200 * Calling this function will cause the panel display drivers to be turned on
201 * and the backlight to be enabled. Content will be visible on screen after
202 * this call completes.
203 *
204 * Return: 0 on success or a negative error code on failure.
205 */
drm_panel_enable(struct drm_panel * panel)206 int drm_panel_enable(struct drm_panel *panel)
207 {
208 int ret;
209
210 if (!panel)
211 return -EINVAL;
212
213 if (panel->enabled) {
214 dev_warn(panel->dev, "Skipping enable of already enabled panel\n");
215 return 0;
216 }
217
218 if (panel->funcs && panel->funcs->enable) {
219 ret = panel->funcs->enable(panel);
220 if (ret < 0)
221 return ret;
222 }
223 panel->enabled = true;
224
225 ret = backlight_enable(panel->backlight);
226 if (ret < 0)
227 DRM_DEV_INFO(panel->dev, "failed to enable backlight: %d\n",
228 ret);
229
230 return 0;
231 }
232 EXPORT_SYMBOL(drm_panel_enable);
233
234 /**
235 * drm_panel_disable - disable a panel
236 * @panel: DRM panel
237 *
238 * This will typically turn off the panel's backlight or disable the display
239 * drivers. For smart panels it should still be possible to communicate with
240 * the integrated circuitry via any command bus after this call.
241 *
242 * Return: 0 on success or a negative error code on failure.
243 */
drm_panel_disable(struct drm_panel * panel)244 int drm_panel_disable(struct drm_panel *panel)
245 {
246 int ret;
247
248 if (!panel)
249 return -EINVAL;
250
251 if (!panel->enabled) {
252 dev_warn(panel->dev, "Skipping disable of already disabled panel\n");
253 return 0;
254 }
255
256 ret = backlight_disable(panel->backlight);
257 if (ret < 0)
258 DRM_DEV_INFO(panel->dev, "failed to disable backlight: %d\n",
259 ret);
260
261 if (panel->funcs && panel->funcs->disable) {
262 ret = panel->funcs->disable(panel);
263 if (ret < 0)
264 return ret;
265 }
266 panel->enabled = false;
267
268 return 0;
269 }
270 EXPORT_SYMBOL(drm_panel_disable);
271
272 /**
273 * drm_panel_get_modes - probe the available display modes of a panel
274 * @panel: DRM panel
275 * @connector: DRM connector
276 *
277 * The modes probed from the panel are automatically added to the connector
278 * that the panel is attached to.
279 *
280 * Return: The number of modes available from the panel on success, or 0 on
281 * failure (no modes).
282 */
drm_panel_get_modes(struct drm_panel * panel,struct drm_connector * connector)283 int drm_panel_get_modes(struct drm_panel *panel,
284 struct drm_connector *connector)
285 {
286 if (!panel)
287 return 0;
288
289 if (panel->funcs && panel->funcs->get_modes) {
290 int num;
291
292 num = panel->funcs->get_modes(panel, connector);
293 if (num > 0)
294 return num;
295 }
296
297 return 0;
298 }
299 EXPORT_SYMBOL(drm_panel_get_modes);
300
301 #ifdef CONFIG_OF
302 /**
303 * of_drm_find_panel - look up a panel using a device tree node
304 * @np: device tree node of the panel
305 *
306 * Searches the set of registered panels for one that matches the given device
307 * tree node. If a matching panel is found, return a pointer to it.
308 *
309 * Return: A pointer to the panel registered for the specified device tree
310 * node or an ERR_PTR() if no panel matching the device tree node can be found.
311 *
312 * Possible error codes returned by this function:
313 *
314 * - EPROBE_DEFER: the panel device has not been probed yet, and the caller
315 * should retry later
316 * - ENODEV: the device is not available (status != "okay" or "ok")
317 */
of_drm_find_panel(const struct device_node * np)318 struct drm_panel *of_drm_find_panel(const struct device_node *np)
319 {
320 struct drm_panel *panel;
321
322 if (!of_device_is_available(np))
323 return ERR_PTR(-ENODEV);
324
325 mutex_lock(&panel_lock);
326
327 list_for_each_entry(panel, &panel_list, list) {
328 if (panel->dev->of_node == np) {
329 mutex_unlock(&panel_lock);
330 return panel;
331 }
332 }
333
334 mutex_unlock(&panel_lock);
335 return ERR_PTR(-EPROBE_DEFER);
336 }
337 EXPORT_SYMBOL(of_drm_find_panel);
338
339 /**
340 * of_drm_get_panel_orientation - look up the orientation of the panel through
341 * the "rotation" binding from a device tree node
342 * @np: device tree node of the panel
343 * @orientation: orientation enum to be filled in
344 *
345 * Looks up the rotation of a panel in the device tree. The orientation of the
346 * panel is expressed as a property name "rotation" in the device tree. The
347 * rotation in the device tree is counter clockwise.
348 *
349 * Return: 0 when a valid rotation value (0, 90, 180, or 270) is read or the
350 * rotation property doesn't exist. Return a negative error code on failure.
351 */
of_drm_get_panel_orientation(const struct device_node * np,enum drm_panel_orientation * orientation)352 int of_drm_get_panel_orientation(const struct device_node *np,
353 enum drm_panel_orientation *orientation)
354 {
355 int rotation, ret;
356
357 ret = of_property_read_u32(np, "rotation", &rotation);
358 if (ret == -EINVAL) {
359 /* Don't return an error if there's no rotation property. */
360 *orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
361 return 0;
362 }
363
364 if (ret < 0)
365 return ret;
366
367 if (rotation == 0)
368 *orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
369 else if (rotation == 90)
370 *orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
371 else if (rotation == 180)
372 *orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
373 else if (rotation == 270)
374 *orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
375 else
376 return -EINVAL;
377
378 return 0;
379 }
380 EXPORT_SYMBOL(of_drm_get_panel_orientation);
381 #endif
382
383 /**
384 * drm_is_panel_follower() - Check if the device is a panel follower
385 * @dev: The 'struct device' to check
386 *
387 * This checks to see if a device needs to be power sequenced together with
388 * a panel using the panel follower API.
389 * At the moment panels can only be followed on device tree enabled systems.
390 * The "panel" property of the follower points to the panel to be followed.
391 *
392 * Return: true if we should be power sequenced with a panel; false otherwise.
393 */
drm_is_panel_follower(struct device * dev)394 bool drm_is_panel_follower(struct device *dev)
395 {
396 /*
397 * The "panel" property is actually a phandle, but for simplicity we
398 * don't bother trying to parse it here. We just need to know if the
399 * property is there.
400 */
401 return of_property_read_bool(dev->of_node, "panel");
402 }
403 EXPORT_SYMBOL(drm_is_panel_follower);
404
405 /**
406 * drm_panel_add_follower() - Register something to follow panel state.
407 * @follower_dev: The 'struct device' for the follower.
408 * @follower: The panel follower descriptor for the follower.
409 *
410 * A panel follower is called right after preparing the panel and right before
411 * unpreparing the panel. It's primary intention is to power on an associated
412 * touchscreen, though it could be used for any similar devices. Multiple
413 * devices are allowed the follow the same panel.
414 *
415 * If a follower is added to a panel that's already been turned on, the
416 * follower's prepare callback is called right away.
417 *
418 * At the moment panels can only be followed on device tree enabled systems.
419 * The "panel" property of the follower points to the panel to be followed.
420 *
421 * Return: 0 or an error code. Note that -ENODEV means that we detected that
422 * follower_dev is not actually following a panel. The caller may
423 * choose to ignore this return value if following a panel is optional.
424 */
drm_panel_add_follower(struct device * follower_dev,struct drm_panel_follower * follower)425 int drm_panel_add_follower(struct device *follower_dev,
426 struct drm_panel_follower *follower)
427 {
428 struct device_node *panel_np;
429 struct drm_panel *panel;
430 int ret;
431
432 panel_np = of_parse_phandle(follower_dev->of_node, "panel", 0);
433 if (!panel_np)
434 return -ENODEV;
435
436 panel = of_drm_find_panel(panel_np);
437 of_node_put(panel_np);
438 if (IS_ERR(panel))
439 return PTR_ERR(panel);
440
441 get_device(panel->dev);
442 follower->panel = panel;
443
444 mutex_lock(&panel->follower_lock);
445
446 list_add_tail(&follower->list, &panel->followers);
447 if (panel->prepared) {
448 ret = follower->funcs->panel_prepared(follower);
449 if (ret < 0)
450 dev_info(panel->dev, "%ps failed: %d\n",
451 follower->funcs->panel_prepared, ret);
452 }
453
454 mutex_unlock(&panel->follower_lock);
455
456 return 0;
457 }
458 EXPORT_SYMBOL(drm_panel_add_follower);
459
460 /**
461 * drm_panel_remove_follower() - Reverse drm_panel_add_follower().
462 * @follower: The panel follower descriptor for the follower.
463 *
464 * Undo drm_panel_add_follower(). This includes calling the follower's
465 * unprepare function if we're removed from a panel that's currently prepared.
466 *
467 * Return: 0 or an error code.
468 */
drm_panel_remove_follower(struct drm_panel_follower * follower)469 void drm_panel_remove_follower(struct drm_panel_follower *follower)
470 {
471 struct drm_panel *panel = follower->panel;
472 int ret;
473
474 mutex_lock(&panel->follower_lock);
475
476 if (panel->prepared) {
477 ret = follower->funcs->panel_unpreparing(follower);
478 if (ret < 0)
479 dev_info(panel->dev, "%ps failed: %d\n",
480 follower->funcs->panel_unpreparing, ret);
481 }
482 list_del_init(&follower->list);
483
484 mutex_unlock(&panel->follower_lock);
485
486 put_device(panel->dev);
487 }
488 EXPORT_SYMBOL(drm_panel_remove_follower);
489
drm_panel_remove_follower_void(void * follower)490 static void drm_panel_remove_follower_void(void *follower)
491 {
492 drm_panel_remove_follower(follower);
493 }
494
495 /**
496 * devm_drm_panel_add_follower() - devm version of drm_panel_add_follower()
497 * @follower_dev: The 'struct device' for the follower.
498 * @follower: The panel follower descriptor for the follower.
499 *
500 * Handles calling drm_panel_remove_follower() using devm on the follower_dev.
501 *
502 * Return: 0 or an error code.
503 */
devm_drm_panel_add_follower(struct device * follower_dev,struct drm_panel_follower * follower)504 int devm_drm_panel_add_follower(struct device *follower_dev,
505 struct drm_panel_follower *follower)
506 {
507 int ret;
508
509 ret = drm_panel_add_follower(follower_dev, follower);
510 if (ret)
511 return ret;
512
513 return devm_add_action_or_reset(follower_dev,
514 drm_panel_remove_follower_void, follower);
515 }
516 EXPORT_SYMBOL(devm_drm_panel_add_follower);
517
518 #if IS_REACHABLE(CONFIG_BACKLIGHT_CLASS_DEVICE)
519 /**
520 * drm_panel_of_backlight - use backlight device node for backlight
521 * @panel: DRM panel
522 *
523 * Use this function to enable backlight handling if your panel
524 * uses device tree and has a backlight phandle.
525 *
526 * When the panel is enabled backlight will be enabled after a
527 * successful call to &drm_panel_funcs.enable()
528 *
529 * When the panel is disabled backlight will be disabled before the
530 * call to &drm_panel_funcs.disable().
531 *
532 * A typical implementation for a panel driver supporting device tree
533 * will call this function at probe time. Backlight will then be handled
534 * transparently without requiring any intervention from the driver.
535 * drm_panel_of_backlight() must be called after the call to drm_panel_init().
536 *
537 * Return: 0 on success or a negative error code on failure.
538 */
drm_panel_of_backlight(struct drm_panel * panel)539 int drm_panel_of_backlight(struct drm_panel *panel)
540 {
541 struct backlight_device *backlight;
542
543 if (!panel || !panel->dev)
544 return -EINVAL;
545
546 backlight = devm_of_find_backlight(panel->dev);
547
548 if (IS_ERR(backlight))
549 return PTR_ERR(backlight);
550
551 panel->backlight = backlight;
552 return 0;
553 }
554 EXPORT_SYMBOL(drm_panel_of_backlight);
555 #endif
556
557 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
558 MODULE_DESCRIPTION("DRM panel infrastructure");
559 MODULE_LICENSE("GPL and additional rights");
560