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>
29 #include <drm/drm_crtc.h>
30 #include <drm/drm_panel.h>
31 #include <drm/drm_print.h>
33 static DEFINE_MUTEX(panel_lock);
34 static LIST_HEAD(panel_list);
39 * The DRM panel helpers allow drivers to register panel objects with a
40 * central registry and provide functions to retrieve those panels in display
43 * For easy integration into drivers using the &drm_bridge infrastructure please
44 * take look at drm_panel_bridge_add() and devm_drm_panel_bridge_add().
48 * drm_panel_init - initialize a panel
50 * @dev: parent device of the panel
51 * @funcs: panel operations
52 * @connector_type: the connector type (DRM_MODE_CONNECTOR_*) corresponding to
55 * Initialize the panel structure for subsequent registration with
58 void drm_panel_init(struct drm_panel *panel, struct device *dev,
59 const struct drm_panel_funcs *funcs, int connector_type)
61 INIT_LIST_HEAD(&panel->list);
62 INIT_LIST_HEAD(&panel->followers);
63 mutex_init(&panel->follower_lock);
66 panel->connector_type = connector_type;
68 EXPORT_SYMBOL(drm_panel_init);
71 * drm_panel_add - add a panel to the global registry
72 * @panel: panel to add
74 * Add a panel to the global registry so that it can be looked up by display
77 void drm_panel_add(struct drm_panel *panel)
79 mutex_lock(&panel_lock);
80 list_add_tail(&panel->list, &panel_list);
81 mutex_unlock(&panel_lock);
83 EXPORT_SYMBOL(drm_panel_add);
86 * drm_panel_remove - remove a panel from the global registry
89 * Removes a panel from the global registry.
91 void drm_panel_remove(struct drm_panel *panel)
93 mutex_lock(&panel_lock);
94 list_del_init(&panel->list);
95 mutex_unlock(&panel_lock);
97 EXPORT_SYMBOL(drm_panel_remove);
100 * drm_panel_prepare - power on a panel
103 * Calling this function will enable power and deassert any reset signals to
104 * the panel. After this has completed it is possible to communicate with any
105 * integrated circuitry via a command bus.
107 * Return: 0 on success or a negative error code on failure.
109 int drm_panel_prepare(struct drm_panel *panel)
111 struct drm_panel_follower *follower;
117 if (panel->prepared) {
118 dev_warn(panel->dev, "Skipping prepare of already prepared panel\n");
122 mutex_lock(&panel->follower_lock);
124 if (panel->funcs && panel->funcs->prepare) {
125 ret = panel->funcs->prepare(panel);
129 panel->prepared = true;
131 list_for_each_entry(follower, &panel->followers, list) {
132 ret = follower->funcs->panel_prepared(follower);
134 dev_info(panel->dev, "%ps failed: %d\n",
135 follower->funcs->panel_prepared, ret);
140 mutex_unlock(&panel->follower_lock);
144 EXPORT_SYMBOL(drm_panel_prepare);
147 * drm_panel_unprepare - power off a panel
150 * Calling this function will completely power off a panel (assert the panel's
151 * reset, turn off power supplies, ...). After this function has completed, it
152 * is usually no longer possible to communicate with the panel until another
153 * call to drm_panel_prepare().
155 * Return: 0 on success or a negative error code on failure.
157 int drm_panel_unprepare(struct drm_panel *panel)
159 struct drm_panel_follower *follower;
166 * If you are seeing the warning below it likely means one of two things:
167 * - Your panel driver incorrectly calls drm_panel_unprepare() in its
168 * shutdown routine. You should delete this.
169 * - You are using panel-edp or panel-simple and your DRM modeset
170 * driver's shutdown() callback happened after the panel's shutdown().
171 * In this case the warning is harmless though ideally you should
172 * figure out how to reverse the order of the shutdown() callbacks.
174 if (!panel->prepared) {
175 dev_warn(panel->dev, "Skipping unprepare of already unprepared panel\n");
179 mutex_lock(&panel->follower_lock);
181 list_for_each_entry(follower, &panel->followers, list) {
182 ret = follower->funcs->panel_unpreparing(follower);
184 dev_info(panel->dev, "%ps failed: %d\n",
185 follower->funcs->panel_unpreparing, ret);
188 if (panel->funcs && panel->funcs->unprepare) {
189 ret = panel->funcs->unprepare(panel);
193 panel->prepared = false;
197 mutex_unlock(&panel->follower_lock);
201 EXPORT_SYMBOL(drm_panel_unprepare);
204 * drm_panel_enable - enable a panel
207 * Calling this function will cause the panel display drivers to be turned on
208 * and the backlight to be enabled. Content will be visible on screen after
209 * this call completes.
211 * Return: 0 on success or a negative error code on failure.
213 int drm_panel_enable(struct drm_panel *panel)
220 if (panel->enabled) {
221 dev_warn(panel->dev, "Skipping enable of already enabled panel\n");
225 if (panel->funcs && panel->funcs->enable) {
226 ret = panel->funcs->enable(panel);
230 panel->enabled = true;
232 ret = backlight_enable(panel->backlight);
234 DRM_DEV_INFO(panel->dev, "failed to enable backlight: %d\n",
239 EXPORT_SYMBOL(drm_panel_enable);
242 * drm_panel_disable - disable a panel
245 * This will typically turn off the panel's backlight or disable the display
246 * drivers. For smart panels it should still be possible to communicate with
247 * the integrated circuitry via any command bus after this call.
249 * Return: 0 on success or a negative error code on failure.
251 int drm_panel_disable(struct drm_panel *panel)
259 * If you are seeing the warning below it likely means one of two things:
260 * - Your panel driver incorrectly calls drm_panel_disable() in its
261 * shutdown routine. You should delete this.
262 * - You are using panel-edp or panel-simple and your DRM modeset
263 * driver's shutdown() callback happened after the panel's shutdown().
264 * In this case the warning is harmless though ideally you should
265 * figure out how to reverse the order of the shutdown() callbacks.
267 if (!panel->enabled) {
268 dev_warn(panel->dev, "Skipping disable of already disabled panel\n");
272 ret = backlight_disable(panel->backlight);
274 DRM_DEV_INFO(panel->dev, "failed to disable backlight: %d\n",
277 if (panel->funcs && panel->funcs->disable) {
278 ret = panel->funcs->disable(panel);
282 panel->enabled = false;
286 EXPORT_SYMBOL(drm_panel_disable);
289 * drm_panel_get_modes - probe the available display modes of a panel
291 * @connector: DRM connector
293 * The modes probed from the panel are automatically added to the connector
294 * that the panel is attached to.
296 * Return: The number of modes available from the panel on success, or 0 on
297 * failure (no modes).
299 int drm_panel_get_modes(struct drm_panel *panel,
300 struct drm_connector *connector)
305 if (panel->funcs && panel->funcs->get_modes) {
308 num = panel->funcs->get_modes(panel, connector);
315 EXPORT_SYMBOL(drm_panel_get_modes);
319 * of_drm_find_panel - look up a panel using a device tree node
320 * @np: device tree node of the panel
322 * Searches the set of registered panels for one that matches the given device
323 * tree node. If a matching panel is found, return a pointer to it.
325 * Return: A pointer to the panel registered for the specified device tree
326 * node or an ERR_PTR() if no panel matching the device tree node can be found.
328 * Possible error codes returned by this function:
330 * - EPROBE_DEFER: the panel device has not been probed yet, and the caller
332 * - ENODEV: the device is not available (status != "okay" or "ok")
334 struct drm_panel *of_drm_find_panel(const struct device_node *np)
336 struct drm_panel *panel;
338 if (!of_device_is_available(np))
339 return ERR_PTR(-ENODEV);
341 mutex_lock(&panel_lock);
343 list_for_each_entry(panel, &panel_list, list) {
344 if (panel->dev->of_node == np) {
345 mutex_unlock(&panel_lock);
350 mutex_unlock(&panel_lock);
351 return ERR_PTR(-EPROBE_DEFER);
353 EXPORT_SYMBOL(of_drm_find_panel);
356 * of_drm_get_panel_orientation - look up the orientation of the panel through
357 * the "rotation" binding from a device tree node
358 * @np: device tree node of the panel
359 * @orientation: orientation enum to be filled in
361 * Looks up the rotation of a panel in the device tree. The orientation of the
362 * panel is expressed as a property name "rotation" in the device tree. The
363 * rotation in the device tree is counter clockwise.
365 * Return: 0 when a valid rotation value (0, 90, 180, or 270) is read or the
366 * rotation property doesn't exist. Return a negative error code on failure.
368 int of_drm_get_panel_orientation(const struct device_node *np,
369 enum drm_panel_orientation *orientation)
373 ret = of_property_read_u32(np, "rotation", &rotation);
374 if (ret == -EINVAL) {
375 /* Don't return an error if there's no rotation property. */
376 *orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
384 *orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
385 else if (rotation == 90)
386 *orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
387 else if (rotation == 180)
388 *orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
389 else if (rotation == 270)
390 *orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
396 EXPORT_SYMBOL(of_drm_get_panel_orientation);
400 * drm_is_panel_follower() - Check if the device is a panel follower
401 * @dev: The 'struct device' to check
403 * This checks to see if a device needs to be power sequenced together with
404 * a panel using the panel follower API.
405 * At the moment panels can only be followed on device tree enabled systems.
406 * The "panel" property of the follower points to the panel to be followed.
408 * Return: true if we should be power sequenced with a panel; false otherwise.
410 bool drm_is_panel_follower(struct device *dev)
413 * The "panel" property is actually a phandle, but for simplicity we
414 * don't bother trying to parse it here. We just need to know if the
417 return of_property_present(dev->of_node, "panel");
419 EXPORT_SYMBOL(drm_is_panel_follower);
422 * drm_panel_add_follower() - Register something to follow panel state.
423 * @follower_dev: The 'struct device' for the follower.
424 * @follower: The panel follower descriptor for the follower.
426 * A panel follower is called right after preparing the panel and right before
427 * unpreparing the panel. It's primary intention is to power on an associated
428 * touchscreen, though it could be used for any similar devices. Multiple
429 * devices are allowed the follow the same panel.
431 * If a follower is added to a panel that's already been turned on, the
432 * follower's prepare callback is called right away.
434 * At the moment panels can only be followed on device tree enabled systems.
435 * The "panel" property of the follower points to the panel to be followed.
437 * Return: 0 or an error code. Note that -ENODEV means that we detected that
438 * follower_dev is not actually following a panel. The caller may
439 * choose to ignore this return value if following a panel is optional.
441 int drm_panel_add_follower(struct device *follower_dev,
442 struct drm_panel_follower *follower)
444 struct device_node *panel_np;
445 struct drm_panel *panel;
448 panel_np = of_parse_phandle(follower_dev->of_node, "panel", 0);
452 panel = of_drm_find_panel(panel_np);
453 of_node_put(panel_np);
455 return PTR_ERR(panel);
457 get_device(panel->dev);
458 follower->panel = panel;
460 mutex_lock(&panel->follower_lock);
462 list_add_tail(&follower->list, &panel->followers);
463 if (panel->prepared) {
464 ret = follower->funcs->panel_prepared(follower);
466 dev_info(panel->dev, "%ps failed: %d\n",
467 follower->funcs->panel_prepared, ret);
470 mutex_unlock(&panel->follower_lock);
474 EXPORT_SYMBOL(drm_panel_add_follower);
477 * drm_panel_remove_follower() - Reverse drm_panel_add_follower().
478 * @follower: The panel follower descriptor for the follower.
480 * Undo drm_panel_add_follower(). This includes calling the follower's
481 * unprepare function if we're removed from a panel that's currently prepared.
483 * Return: 0 or an error code.
485 void drm_panel_remove_follower(struct drm_panel_follower *follower)
487 struct drm_panel *panel = follower->panel;
490 mutex_lock(&panel->follower_lock);
492 if (panel->prepared) {
493 ret = follower->funcs->panel_unpreparing(follower);
495 dev_info(panel->dev, "%ps failed: %d\n",
496 follower->funcs->panel_unpreparing, ret);
498 list_del_init(&follower->list);
500 mutex_unlock(&panel->follower_lock);
502 put_device(panel->dev);
504 EXPORT_SYMBOL(drm_panel_remove_follower);
506 static void drm_panel_remove_follower_void(void *follower)
508 drm_panel_remove_follower(follower);
512 * devm_drm_panel_add_follower() - devm version of drm_panel_add_follower()
513 * @follower_dev: The 'struct device' for the follower.
514 * @follower: The panel follower descriptor for the follower.
516 * Handles calling drm_panel_remove_follower() using devm on the follower_dev.
518 * Return: 0 or an error code.
520 int devm_drm_panel_add_follower(struct device *follower_dev,
521 struct drm_panel_follower *follower)
525 ret = drm_panel_add_follower(follower_dev, follower);
529 return devm_add_action_or_reset(follower_dev,
530 drm_panel_remove_follower_void, follower);
532 EXPORT_SYMBOL(devm_drm_panel_add_follower);
534 #if IS_REACHABLE(CONFIG_BACKLIGHT_CLASS_DEVICE)
536 * drm_panel_of_backlight - use backlight device node for backlight
539 * Use this function to enable backlight handling if your panel
540 * uses device tree and has a backlight phandle.
542 * When the panel is enabled backlight will be enabled after a
543 * successful call to &drm_panel_funcs.enable()
545 * When the panel is disabled backlight will be disabled before the
546 * call to &drm_panel_funcs.disable().
548 * A typical implementation for a panel driver supporting device tree
549 * will call this function at probe time. Backlight will then be handled
550 * transparently without requiring any intervention from the driver.
551 * drm_panel_of_backlight() must be called after the call to drm_panel_init().
553 * Return: 0 on success or a negative error code on failure.
555 int drm_panel_of_backlight(struct drm_panel *panel)
557 struct backlight_device *backlight;
559 if (!panel || !panel->dev)
562 backlight = devm_of_find_backlight(panel->dev);
564 if (IS_ERR(backlight))
565 return PTR_ERR(backlight);
567 panel->backlight = backlight;
570 EXPORT_SYMBOL(drm_panel_of_backlight);
574 MODULE_DESCRIPTION("DRM panel infrastructure");
575 MODULE_LICENSE("GPL and additional rights");