Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 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 | |
Sam Ravnborg | 152dbde | 2019-12-07 15:03:30 +0100 | [diff] [blame] | 24 | #include <linux/backlight.h> |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 25 | #include <linux/err.h> |
| 26 | #include <linux/module.h> |
| 27 | |
| 28 | #include <drm/drm_crtc.h> |
| 29 | #include <drm/drm_panel.h> |
Sam Ravnborg | 152dbde | 2019-12-07 15:03:30 +0100 | [diff] [blame] | 30 | #include <drm/drm_print.h> |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 31 | |
| 32 | static DEFINE_MUTEX(panel_lock); |
| 33 | static LIST_HEAD(panel_list); |
| 34 | |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 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. |
Daniel Vetter | 0aa5eb3 | 2019-01-11 17:40:46 +0100 | [diff] [blame] | 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(). |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 44 | */ |
| 45 | |
| 46 | /** |
| 47 | * drm_panel_init - initialize a panel |
| 48 | * @panel: DRM panel |
Laurent Pinchart | 6dbe0c4 | 2019-08-23 22:32:43 +0300 | [diff] [blame] | 49 | * @dev: parent device of the panel |
| 50 | * @funcs: panel operations |
Laurent Pinchart | 9a2654c | 2019-09-04 16:28:03 +0300 | [diff] [blame] | 51 | * @connector_type: the connector type (DRM_MODE_CONNECTOR_*) corresponding to |
| 52 | * the panel interface |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 53 | * |
Laurent Pinchart | 6dbe0c4 | 2019-08-23 22:32:43 +0300 | [diff] [blame] | 54 | * Initialize the panel structure for subsequent registration with |
| 55 | * drm_panel_add(). |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 56 | */ |
Laurent Pinchart | 6dbe0c4 | 2019-08-23 22:32:43 +0300 | [diff] [blame] | 57 | void drm_panel_init(struct drm_panel *panel, struct device *dev, |
Laurent Pinchart | 9a2654c | 2019-09-04 16:28:03 +0300 | [diff] [blame] | 58 | const struct drm_panel_funcs *funcs, int connector_type) |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 59 | { |
| 60 | INIT_LIST_HEAD(&panel->list); |
Laurent Pinchart | 6dbe0c4 | 2019-08-23 22:32:43 +0300 | [diff] [blame] | 61 | panel->dev = dev; |
| 62 | panel->funcs = funcs; |
Laurent Pinchart | 9a2654c | 2019-09-04 16:28:03 +0300 | [diff] [blame] | 63 | panel->connector_type = connector_type; |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 64 | } |
| 65 | EXPORT_SYMBOL(drm_panel_init); |
| 66 | |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 67 | /** |
| 68 | * drm_panel_add - add a panel to the global registry |
| 69 | * @panel: panel to add |
| 70 | * |
| 71 | * Add a panel to the global registry so that it can be looked up by display |
| 72 | * drivers. |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 73 | */ |
Bernard Zhao | c3ee8c6 | 2020-08-01 20:02:13 +0800 | [diff] [blame] | 74 | void drm_panel_add(struct drm_panel *panel) |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 75 | { |
| 76 | mutex_lock(&panel_lock); |
| 77 | list_add_tail(&panel->list, &panel_list); |
| 78 | mutex_unlock(&panel_lock); |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 79 | } |
| 80 | EXPORT_SYMBOL(drm_panel_add); |
| 81 | |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 82 | /** |
| 83 | * drm_panel_remove - remove a panel from the global registry |
| 84 | * @panel: DRM panel |
| 85 | * |
| 86 | * Removes a panel from the global registry. |
| 87 | */ |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 88 | void drm_panel_remove(struct drm_panel *panel) |
| 89 | { |
| 90 | mutex_lock(&panel_lock); |
| 91 | list_del_init(&panel->list); |
| 92 | mutex_unlock(&panel_lock); |
| 93 | } |
| 94 | EXPORT_SYMBOL(drm_panel_remove); |
| 95 | |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 96 | /** |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 97 | * drm_panel_prepare - power on a panel |
| 98 | * @panel: DRM panel |
| 99 | * |
| 100 | * Calling this function will enable power and deassert any reset signals to |
| 101 | * the panel. After this has completed it is possible to communicate with any |
| 102 | * integrated circuitry via a command bus. |
| 103 | * |
| 104 | * Return: 0 on success or a negative error code on failure. |
| 105 | */ |
| 106 | int drm_panel_prepare(struct drm_panel *panel) |
| 107 | { |
Sam Ravnborg | 5dce87a | 2019-12-07 15:03:29 +0100 | [diff] [blame] | 108 | if (!panel) |
| 109 | return -EINVAL; |
| 110 | |
| 111 | if (panel->funcs && panel->funcs->prepare) |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 112 | return panel->funcs->prepare(panel); |
| 113 | |
Sam Ravnborg | 5dce87a | 2019-12-07 15:03:29 +0100 | [diff] [blame] | 114 | return 0; |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 115 | } |
| 116 | EXPORT_SYMBOL(drm_panel_prepare); |
| 117 | |
| 118 | /** |
| 119 | * drm_panel_unprepare - power off a panel |
| 120 | * @panel: DRM panel |
| 121 | * |
| 122 | * Calling this function will completely power off a panel (assert the panel's |
| 123 | * reset, turn off power supplies, ...). After this function has completed, it |
| 124 | * is usually no longer possible to communicate with the panel until another |
| 125 | * call to drm_panel_prepare(). |
| 126 | * |
| 127 | * Return: 0 on success or a negative error code on failure. |
| 128 | */ |
| 129 | int drm_panel_unprepare(struct drm_panel *panel) |
| 130 | { |
Sam Ravnborg | 5dce87a | 2019-12-07 15:03:29 +0100 | [diff] [blame] | 131 | if (!panel) |
| 132 | return -EINVAL; |
| 133 | |
| 134 | if (panel->funcs && panel->funcs->unprepare) |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 135 | return panel->funcs->unprepare(panel); |
| 136 | |
Sam Ravnborg | 5dce87a | 2019-12-07 15:03:29 +0100 | [diff] [blame] | 137 | return 0; |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 138 | } |
| 139 | EXPORT_SYMBOL(drm_panel_unprepare); |
| 140 | |
| 141 | /** |
| 142 | * drm_panel_enable - enable a panel |
| 143 | * @panel: DRM panel |
| 144 | * |
| 145 | * Calling this function will cause the panel display drivers to be turned on |
| 146 | * and the backlight to be enabled. Content will be visible on screen after |
| 147 | * this call completes. |
| 148 | * |
| 149 | * Return: 0 on success or a negative error code on failure. |
| 150 | */ |
| 151 | int drm_panel_enable(struct drm_panel *panel) |
| 152 | { |
Sam Ravnborg | 152dbde | 2019-12-07 15:03:30 +0100 | [diff] [blame] | 153 | int ret; |
| 154 | |
Sam Ravnborg | 5dce87a | 2019-12-07 15:03:29 +0100 | [diff] [blame] | 155 | if (!panel) |
| 156 | return -EINVAL; |
| 157 | |
Sam Ravnborg | 152dbde | 2019-12-07 15:03:30 +0100 | [diff] [blame] | 158 | if (panel->funcs && panel->funcs->enable) { |
| 159 | ret = panel->funcs->enable(panel); |
| 160 | if (ret < 0) |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | ret = backlight_enable(panel->backlight); |
| 165 | if (ret < 0) |
| 166 | DRM_DEV_INFO(panel->dev, "failed to enable backlight: %d\n", |
| 167 | ret); |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 168 | |
Sam Ravnborg | 5dce87a | 2019-12-07 15:03:29 +0100 | [diff] [blame] | 169 | return 0; |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 170 | } |
| 171 | EXPORT_SYMBOL(drm_panel_enable); |
| 172 | |
| 173 | /** |
| 174 | * drm_panel_disable - disable a panel |
| 175 | * @panel: DRM panel |
| 176 | * |
| 177 | * This will typically turn off the panel's backlight or disable the display |
| 178 | * drivers. For smart panels it should still be possible to communicate with |
| 179 | * the integrated circuitry via any command bus after this call. |
| 180 | * |
| 181 | * Return: 0 on success or a negative error code on failure. |
| 182 | */ |
| 183 | int drm_panel_disable(struct drm_panel *panel) |
| 184 | { |
Sam Ravnborg | 152dbde | 2019-12-07 15:03:30 +0100 | [diff] [blame] | 185 | int ret; |
| 186 | |
Sam Ravnborg | 5dce87a | 2019-12-07 15:03:29 +0100 | [diff] [blame] | 187 | if (!panel) |
| 188 | return -EINVAL; |
| 189 | |
Sam Ravnborg | 152dbde | 2019-12-07 15:03:30 +0100 | [diff] [blame] | 190 | ret = backlight_disable(panel->backlight); |
| 191 | if (ret < 0) |
| 192 | DRM_DEV_INFO(panel->dev, "failed to disable backlight: %d\n", |
| 193 | ret); |
| 194 | |
Sam Ravnborg | 5dce87a | 2019-12-07 15:03:29 +0100 | [diff] [blame] | 195 | if (panel->funcs && panel->funcs->disable) |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 196 | return panel->funcs->disable(panel); |
| 197 | |
Sam Ravnborg | 5dce87a | 2019-12-07 15:03:29 +0100 | [diff] [blame] | 198 | return 0; |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 199 | } |
| 200 | EXPORT_SYMBOL(drm_panel_disable); |
| 201 | |
| 202 | /** |
| 203 | * drm_panel_get_modes - probe the available display modes of a panel |
| 204 | * @panel: DRM panel |
Sam Ravnborg | 06c4a9c | 2019-12-07 15:03:34 +0100 | [diff] [blame] | 205 | * @connector: DRM connector |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 206 | * |
| 207 | * The modes probed from the panel are automatically added to the connector |
| 208 | * that the panel is attached to. |
| 209 | * |
| 210 | * Return: The number of modes available from the panel on success or a |
| 211 | * negative error code on failure. |
| 212 | */ |
Sam Ravnborg | 06c4a9c | 2019-12-07 15:03:34 +0100 | [diff] [blame] | 213 | int drm_panel_get_modes(struct drm_panel *panel, |
| 214 | struct drm_connector *connector) |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 215 | { |
Sam Ravnborg | 5dce87a | 2019-12-07 15:03:29 +0100 | [diff] [blame] | 216 | if (!panel) |
| 217 | return -EINVAL; |
| 218 | |
| 219 | if (panel->funcs && panel->funcs->get_modes) |
Sam Ravnborg | 06c4a9c | 2019-12-07 15:03:34 +0100 | [diff] [blame] | 220 | return panel->funcs->get_modes(panel, connector); |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 221 | |
Sam Ravnborg | 5dce87a | 2019-12-07 15:03:29 +0100 | [diff] [blame] | 222 | return -EOPNOTSUPP; |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame] | 223 | } |
| 224 | EXPORT_SYMBOL(drm_panel_get_modes); |
| 225 | |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 226 | #ifdef CONFIG_OF |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 227 | /** |
| 228 | * of_drm_find_panel - look up a panel using a device tree node |
| 229 | * @np: device tree node of the panel |
| 230 | * |
| 231 | * Searches the set of registered panels for one that matches the given device |
| 232 | * tree node. If a matching panel is found, return a pointer to it. |
| 233 | * |
| 234 | * Return: A pointer to the panel registered for the specified device tree |
Boris Brezillon | 5fa8e4a | 2018-05-09 15:00:39 +0200 | [diff] [blame] | 235 | * node or an ERR_PTR() if no panel matching the device tree node can be found. |
Sean Paul | 3eb3cd0 | 2018-08-15 16:38:28 -0400 | [diff] [blame] | 236 | * |
Boris Brezillon | c59eb3c | 2018-05-09 15:00:40 +0200 | [diff] [blame] | 237 | * Possible error codes returned by this function: |
Sean Paul | 3eb3cd0 | 2018-08-15 16:38:28 -0400 | [diff] [blame] | 238 | * |
Boris Brezillon | c59eb3c | 2018-05-09 15:00:40 +0200 | [diff] [blame] | 239 | * - EPROBE_DEFER: the panel device has not been probed yet, and the caller |
| 240 | * should retry later |
| 241 | * - ENODEV: the device is not available (status != "okay" or "ok") |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 242 | */ |
Laurent Pinchart | 327bc44 | 2016-11-19 05:28:05 +0200 | [diff] [blame] | 243 | struct drm_panel *of_drm_find_panel(const struct device_node *np) |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 244 | { |
| 245 | struct drm_panel *panel; |
| 246 | |
Boris Brezillon | c59eb3c | 2018-05-09 15:00:40 +0200 | [diff] [blame] | 247 | if (!of_device_is_available(np)) |
| 248 | return ERR_PTR(-ENODEV); |
| 249 | |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 250 | mutex_lock(&panel_lock); |
| 251 | |
| 252 | list_for_each_entry(panel, &panel_list, list) { |
| 253 | if (panel->dev->of_node == np) { |
| 254 | mutex_unlock(&panel_lock); |
| 255 | return panel; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | mutex_unlock(&panel_lock); |
Boris Brezillon | 5fa8e4a | 2018-05-09 15:00:39 +0200 | [diff] [blame] | 260 | return ERR_PTR(-EPROBE_DEFER); |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 261 | } |
| 262 | EXPORT_SYMBOL(of_drm_find_panel); |
Derek Basehore | 5f3e750 | 2020-08-14 00:56:06 +0300 | [diff] [blame] | 263 | |
| 264 | /** |
| 265 | * of_drm_get_panel_orientation - look up the orientation of the panel through |
| 266 | * the "rotation" binding from a device tree node |
| 267 | * @np: device tree node of the panel |
| 268 | * @orientation: orientation enum to be filled in |
| 269 | * |
| 270 | * Looks up the rotation of a panel in the device tree. The orientation of the |
| 271 | * panel is expressed as a property name "rotation" in the device tree. The |
| 272 | * rotation in the device tree is counter clockwise. |
| 273 | * |
| 274 | * Return: 0 when a valid rotation value (0, 90, 180, or 270) is read or the |
| 275 | * rotation property doesn't exist. Return a negative error code on failure. |
| 276 | */ |
| 277 | int of_drm_get_panel_orientation(const struct device_node *np, |
| 278 | enum drm_panel_orientation *orientation) |
| 279 | { |
| 280 | int rotation, ret; |
| 281 | |
| 282 | ret = of_property_read_u32(np, "rotation", &rotation); |
| 283 | if (ret == -EINVAL) { |
| 284 | /* Don't return an error if there's no rotation property. */ |
| 285 | *orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN; |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | if (ret < 0) |
| 290 | return ret; |
| 291 | |
| 292 | if (rotation == 0) |
| 293 | *orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL; |
| 294 | else if (rotation == 90) |
| 295 | *orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP; |
| 296 | else if (rotation == 180) |
| 297 | *orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP; |
| 298 | else if (rotation == 270) |
| 299 | *orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP; |
| 300 | else |
| 301 | return -EINVAL; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | EXPORT_SYMBOL(of_drm_get_panel_orientation); |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 306 | #endif |
| 307 | |
Andy Shevchenko | 4a34a9d | 2019-12-17 16:07:21 +0200 | [diff] [blame] | 308 | #if IS_REACHABLE(CONFIG_BACKLIGHT_CLASS_DEVICE) |
Sam Ravnborg | 152dbde | 2019-12-07 15:03:30 +0100 | [diff] [blame] | 309 | /** |
| 310 | * drm_panel_of_backlight - use backlight device node for backlight |
| 311 | * @panel: DRM panel |
| 312 | * |
| 313 | * Use this function to enable backlight handling if your panel |
| 314 | * uses device tree and has a backlight phandle. |
| 315 | * |
| 316 | * When the panel is enabled backlight will be enabled after a |
| 317 | * successful call to &drm_panel_funcs.enable() |
| 318 | * |
| 319 | * When the panel is disabled backlight will be disabled before the |
| 320 | * call to &drm_panel_funcs.disable(). |
| 321 | * |
| 322 | * A typical implementation for a panel driver supporting device tree |
| 323 | * will call this function at probe time. Backlight will then be handled |
| 324 | * transparently without requiring any intervention from the driver. |
| 325 | * drm_panel_of_backlight() must be called after the call to drm_panel_init(). |
| 326 | * |
| 327 | * Return: 0 on success or a negative error code on failure. |
| 328 | */ |
| 329 | int drm_panel_of_backlight(struct drm_panel *panel) |
| 330 | { |
| 331 | struct backlight_device *backlight; |
| 332 | |
| 333 | if (!panel || !panel->dev) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | backlight = devm_of_find_backlight(panel->dev); |
| 337 | |
| 338 | if (IS_ERR(backlight)) |
| 339 | return PTR_ERR(backlight); |
| 340 | |
| 341 | panel->backlight = backlight; |
| 342 | return 0; |
| 343 | } |
| 344 | EXPORT_SYMBOL(drm_panel_of_backlight); |
| 345 | #endif |
| 346 | |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 347 | MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>"); |
| 348 | MODULE_DESCRIPTION("DRM panel infrastructure"); |
| 349 | MODULE_LICENSE("GPL and additional rights"); |