2 * Copyright (C) 2013, NVIDIA Corporation. All rights reserved.
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:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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.
24 #include <linux/backlight.h>
25 #include <linux/err.h>
26 #include <linux/module.h>
28 #include <drm/drm_crtc.h>
29 #include <drm/drm_panel.h>
30 #include <drm/drm_print.h>
32 static DEFINE_MUTEX(panel_lock);
33 static LIST_HEAD(panel_list);
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
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().
47 * drm_panel_init - initialize a panel
49 * @dev: parent device of the panel
50 * @funcs: panel operations
51 * @connector_type: the connector type (DRM_MODE_CONNECTOR_*) corresponding to
54 * Initialize the panel structure for subsequent registration with
57 void drm_panel_init(struct drm_panel *panel, struct device *dev,
58 const struct drm_panel_funcs *funcs, int connector_type)
60 INIT_LIST_HEAD(&panel->list);
61 INIT_LIST_HEAD(&panel->followers);
62 mutex_init(&panel->follower_lock);
65 panel->connector_type = connector_type;
67 EXPORT_SYMBOL(drm_panel_init);
70 * drm_panel_add - add a panel to the global registry
71 * @panel: panel to add
73 * Add a panel to the global registry so that it can be looked up by display
76 void drm_panel_add(struct drm_panel *panel)
78 mutex_lock(&panel_lock);
79 list_add_tail(&panel->list, &panel_list);
80 mutex_unlock(&panel_lock);
82 EXPORT_SYMBOL(drm_panel_add);
85 * drm_panel_remove - remove a panel from the global registry
88 * Removes a panel from the global registry.
90 void drm_panel_remove(struct drm_panel *panel)
92 mutex_lock(&panel_lock);
93 list_del_init(&panel->list);
94 mutex_unlock(&panel_lock);
96 EXPORT_SYMBOL(drm_panel_remove);
99 * drm_panel_prepare - power on a panel
102 * Calling this function will enable power and deassert any reset signals to
103 * the panel. After this has completed it is possible to communicate with any
104 * integrated circuitry via a command bus.
106 * Return: 0 on success or a negative error code on failure.
108 int drm_panel_prepare(struct drm_panel *panel)
110 struct drm_panel_follower *follower;
116 if (panel->prepared) {
117 dev_warn(panel->dev, "Skipping prepare of already prepared panel\n");
121 mutex_lock(&panel->follower_lock);
123 if (panel->funcs && panel->funcs->prepare) {
124 ret = panel->funcs->prepare(panel);
128 panel->prepared = true;
130 list_for_each_entry(follower, &panel->followers, list) {
131 ret = follower->funcs->panel_prepared(follower);
133 dev_info(panel->dev, "%ps failed: %d\n",
134 follower->funcs->panel_prepared, ret);
139 mutex_unlock(&panel->follower_lock);
143 EXPORT_SYMBOL(drm_panel_prepare);
146 * drm_panel_unprepare - power off a panel
149 * Calling this function will completely power off a panel (assert the panel's
150 * reset, turn off power supplies, ...). After this function has completed, it
151 * is usually no longer possible to communicate with the panel until another
152 * call to drm_panel_prepare().
154 * Return: 0 on success or a negative error code on failure.
156 int drm_panel_unprepare(struct drm_panel *panel)
158 struct drm_panel_follower *follower;
164 if (!panel->prepared) {
165 dev_warn(panel->dev, "Skipping unprepare of already unprepared panel\n");
169 mutex_lock(&panel->follower_lock);
171 list_for_each_entry(follower, &panel->followers, list) {
172 ret = follower->funcs->panel_unpreparing(follower);
174 dev_info(panel->dev, "%ps failed: %d\n",
175 follower->funcs->panel_unpreparing, ret);
178 if (panel->funcs && panel->funcs->unprepare) {
179 ret = panel->funcs->unprepare(panel);
183 panel->prepared = false;
187 mutex_unlock(&panel->follower_lock);
191 EXPORT_SYMBOL(drm_panel_unprepare);
194 * drm_panel_enable - enable a panel
197 * Calling this function will cause the panel display drivers to be turned on
198 * and the backlight to be enabled. Content will be visible on screen after
199 * this call completes.
201 * Return: 0 on success or a negative error code on failure.
203 int drm_panel_enable(struct drm_panel *panel)
210 if (panel->enabled) {
211 dev_warn(panel->dev, "Skipping enable of already enabled panel\n");
215 if (panel->funcs && panel->funcs->enable) {
216 ret = panel->funcs->enable(panel);
220 panel->enabled = true;
222 ret = backlight_enable(panel->backlight);
224 DRM_DEV_INFO(panel->dev, "failed to enable backlight: %d\n",
229 EXPORT_SYMBOL(drm_panel_enable);
232 * drm_panel_disable - disable a panel
235 * This will typically turn off the panel's backlight or disable the display
236 * drivers. For smart panels it should still be possible to communicate with
237 * the integrated circuitry via any command bus after this call.
239 * Return: 0 on success or a negative error code on failure.
241 int drm_panel_disable(struct drm_panel *panel)
248 if (!panel->enabled) {
249 dev_warn(panel->dev, "Skipping disable of already disabled panel\n");
253 ret = backlight_disable(panel->backlight);
255 DRM_DEV_INFO(panel->dev, "failed to disable backlight: %d\n",
258 if (panel->funcs && panel->funcs->disable) {
259 ret = panel->funcs->disable(panel);
263 panel->enabled = false;
267 EXPORT_SYMBOL(drm_panel_disable);
270 * drm_panel_get_modes - probe the available display modes of a panel
272 * @connector: DRM connector
274 * The modes probed from the panel are automatically added to the connector
275 * that the panel is attached to.
277 * Return: The number of modes available from the panel on success or a
278 * negative error code on failure.
280 int drm_panel_get_modes(struct drm_panel *panel,
281 struct drm_connector *connector)
286 if (panel->funcs && panel->funcs->get_modes)
287 return panel->funcs->get_modes(panel, connector);
291 EXPORT_SYMBOL(drm_panel_get_modes);
295 * of_drm_find_panel - look up a panel using a device tree node
296 * @np: device tree node of the panel
298 * Searches the set of registered panels for one that matches the given device
299 * tree node. If a matching panel is found, return a pointer to it.
301 * Return: A pointer to the panel registered for the specified device tree
302 * node or an ERR_PTR() if no panel matching the device tree node can be found.
304 * Possible error codes returned by this function:
306 * - EPROBE_DEFER: the panel device has not been probed yet, and the caller
308 * - ENODEV: the device is not available (status != "okay" or "ok")
310 struct drm_panel *of_drm_find_panel(const struct device_node *np)
312 struct drm_panel *panel;
314 if (!of_device_is_available(np))
315 return ERR_PTR(-ENODEV);
317 mutex_lock(&panel_lock);
319 list_for_each_entry(panel, &panel_list, list) {
320 if (panel->dev->of_node == np) {
321 mutex_unlock(&panel_lock);
326 mutex_unlock(&panel_lock);
327 return ERR_PTR(-EPROBE_DEFER);
329 EXPORT_SYMBOL(of_drm_find_panel);
332 * of_drm_get_panel_orientation - look up the orientation of the panel through
333 * the "rotation" binding from a device tree node
334 * @np: device tree node of the panel
335 * @orientation: orientation enum to be filled in
337 * Looks up the rotation of a panel in the device tree. The orientation of the
338 * panel is expressed as a property name "rotation" in the device tree. The
339 * rotation in the device tree is counter clockwise.
341 * Return: 0 when a valid rotation value (0, 90, 180, or 270) is read or the
342 * rotation property doesn't exist. Return a negative error code on failure.
344 int of_drm_get_panel_orientation(const struct device_node *np,
345 enum drm_panel_orientation *orientation)
349 ret = of_property_read_u32(np, "rotation", &rotation);
350 if (ret == -EINVAL) {
351 /* Don't return an error if there's no rotation property. */
352 *orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
360 *orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
361 else if (rotation == 90)
362 *orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
363 else if (rotation == 180)
364 *orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
365 else if (rotation == 270)
366 *orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
372 EXPORT_SYMBOL(of_drm_get_panel_orientation);
376 * drm_is_panel_follower() - Check if the device is a panel follower
377 * @dev: The 'struct device' to check
379 * This checks to see if a device needs to be power sequenced together with
380 * a panel using the panel follower API.
381 * At the moment panels can only be followed on device tree enabled systems.
382 * The "panel" property of the follower points to the panel to be followed.
384 * Return: true if we should be power sequenced with a panel; false otherwise.
386 bool drm_is_panel_follower(struct device *dev)
389 * The "panel" property is actually a phandle, but for simplicity we
390 * don't bother trying to parse it here. We just need to know if the
393 return of_property_read_bool(dev->of_node, "panel");
395 EXPORT_SYMBOL(drm_is_panel_follower);
398 * drm_panel_add_follower() - Register something to follow panel state.
399 * @follower_dev: The 'struct device' for the follower.
400 * @follower: The panel follower descriptor for the follower.
402 * A panel follower is called right after preparing the panel and right before
403 * unpreparing the panel. It's primary intention is to power on an associated
404 * touchscreen, though it could be used for any similar devices. Multiple
405 * devices are allowed the follow the same panel.
407 * If a follower is added to a panel that's already been turned on, the
408 * follower's prepare callback is called right away.
410 * At the moment panels can only be followed on device tree enabled systems.
411 * The "panel" property of the follower points to the panel to be followed.
413 * Return: 0 or an error code. Note that -ENODEV means that we detected that
414 * follower_dev is not actually following a panel. The caller may
415 * choose to ignore this return value if following a panel is optional.
417 int drm_panel_add_follower(struct device *follower_dev,
418 struct drm_panel_follower *follower)
420 struct device_node *panel_np;
421 struct drm_panel *panel;
424 panel_np = of_parse_phandle(follower_dev->of_node, "panel", 0);
428 panel = of_drm_find_panel(panel_np);
429 of_node_put(panel_np);
431 return PTR_ERR(panel);
433 get_device(panel->dev);
434 follower->panel = panel;
436 mutex_lock(&panel->follower_lock);
438 list_add_tail(&follower->list, &panel->followers);
439 if (panel->prepared) {
440 ret = follower->funcs->panel_prepared(follower);
442 dev_info(panel->dev, "%ps failed: %d\n",
443 follower->funcs->panel_prepared, ret);
446 mutex_unlock(&panel->follower_lock);
450 EXPORT_SYMBOL(drm_panel_add_follower);
453 * drm_panel_remove_follower() - Reverse drm_panel_add_follower().
454 * @follower: The panel follower descriptor for the follower.
456 * Undo drm_panel_add_follower(). This includes calling the follower's
457 * unprepare function if we're removed from a panel that's currently prepared.
459 * Return: 0 or an error code.
461 void drm_panel_remove_follower(struct drm_panel_follower *follower)
463 struct drm_panel *panel = follower->panel;
466 mutex_lock(&panel->follower_lock);
468 if (panel->prepared) {
469 ret = follower->funcs->panel_unpreparing(follower);
471 dev_info(panel->dev, "%ps failed: %d\n",
472 follower->funcs->panel_unpreparing, ret);
474 list_del_init(&follower->list);
476 mutex_unlock(&panel->follower_lock);
478 put_device(panel->dev);
480 EXPORT_SYMBOL(drm_panel_remove_follower);
482 static void drm_panel_remove_follower_void(void *follower)
484 drm_panel_remove_follower(follower);
488 * devm_drm_panel_add_follower() - devm version of drm_panel_add_follower()
489 * @follower_dev: The 'struct device' for the follower.
490 * @follower: The panel follower descriptor for the follower.
492 * Handles calling drm_panel_remove_follower() using devm on the follower_dev.
494 * Return: 0 or an error code.
496 int devm_drm_panel_add_follower(struct device *follower_dev,
497 struct drm_panel_follower *follower)
501 ret = drm_panel_add_follower(follower_dev, follower);
505 return devm_add_action_or_reset(follower_dev,
506 drm_panel_remove_follower_void, follower);
508 EXPORT_SYMBOL(devm_drm_panel_add_follower);
510 #if IS_REACHABLE(CONFIG_BACKLIGHT_CLASS_DEVICE)
512 * drm_panel_of_backlight - use backlight device node for backlight
515 * Use this function to enable backlight handling if your panel
516 * uses device tree and has a backlight phandle.
518 * When the panel is enabled backlight will be enabled after a
519 * successful call to &drm_panel_funcs.enable()
521 * When the panel is disabled backlight will be disabled before the
522 * call to &drm_panel_funcs.disable().
524 * A typical implementation for a panel driver supporting device tree
525 * will call this function at probe time. Backlight will then be handled
526 * transparently without requiring any intervention from the driver.
527 * drm_panel_of_backlight() must be called after the call to drm_panel_init().
529 * Return: 0 on success or a negative error code on failure.
531 int drm_panel_of_backlight(struct drm_panel *panel)
533 struct backlight_device *backlight;
535 if (!panel || !panel->dev)
538 backlight = devm_of_find_backlight(panel->dev);
540 if (IS_ERR(backlight))
541 return PTR_ERR(backlight);
543 panel->backlight = backlight;
546 EXPORT_SYMBOL(drm_panel_of_backlight);
550 MODULE_DESCRIPTION("DRM panel infrastructure");
551 MODULE_LICENSE("GPL and additional rights");