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 | |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/module.h> |
| 26 | |
| 27 | #include <drm/drm_crtc.h> |
| 28 | #include <drm/drm_panel.h> |
| 29 | |
| 30 | static DEFINE_MUTEX(panel_lock); |
| 31 | static LIST_HEAD(panel_list); |
| 32 | |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 33 | /** |
| 34 | * DOC: drm panel |
| 35 | * |
| 36 | * The DRM panel helpers allow drivers to register panel objects with a |
| 37 | * central registry and provide functions to retrieve those panels in display |
| 38 | * drivers. |
Daniel Vetter | 0aa5eb3 | 2019-01-11 17:40:46 +0100 | [diff] [blame] | 39 | * |
| 40 | * For easy integration into drivers using the &drm_bridge infrastructure please |
| 41 | * 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] | 42 | */ |
| 43 | |
| 44 | /** |
| 45 | * drm_panel_init - initialize a panel |
| 46 | * @panel: DRM panel |
| 47 | * |
| 48 | * Sets up internal fields of the panel so that it can subsequently be added |
| 49 | * to the registry. |
| 50 | */ |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 51 | void drm_panel_init(struct drm_panel *panel) |
| 52 | { |
| 53 | INIT_LIST_HEAD(&panel->list); |
| 54 | } |
| 55 | EXPORT_SYMBOL(drm_panel_init); |
| 56 | |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 57 | /** |
| 58 | * drm_panel_add - add a panel to the global registry |
| 59 | * @panel: panel to add |
| 60 | * |
| 61 | * Add a panel to the global registry so that it can be looked up by display |
| 62 | * drivers. |
| 63 | * |
| 64 | * Return: 0 on success or a negative error code on failure. |
| 65 | */ |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 66 | int drm_panel_add(struct drm_panel *panel) |
| 67 | { |
| 68 | mutex_lock(&panel_lock); |
| 69 | list_add_tail(&panel->list, &panel_list); |
| 70 | mutex_unlock(&panel_lock); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | EXPORT_SYMBOL(drm_panel_add); |
| 75 | |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 76 | /** |
| 77 | * drm_panel_remove - remove a panel from the global registry |
| 78 | * @panel: DRM panel |
| 79 | * |
| 80 | * Removes a panel from the global registry. |
| 81 | */ |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 82 | void drm_panel_remove(struct drm_panel *panel) |
| 83 | { |
| 84 | mutex_lock(&panel_lock); |
| 85 | list_del_init(&panel->list); |
| 86 | mutex_unlock(&panel_lock); |
| 87 | } |
| 88 | EXPORT_SYMBOL(drm_panel_remove); |
| 89 | |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 90 | /** |
| 91 | * drm_panel_attach - attach a panel to a connector |
| 92 | * @panel: DRM panel |
| 93 | * @connector: DRM connector |
| 94 | * |
| 95 | * After obtaining a pointer to a DRM panel a display driver calls this |
| 96 | * function to attach a panel to a connector. |
| 97 | * |
| 98 | * An error is returned if the panel is already attached to another connector. |
| 99 | * |
Jyri Sarha | 38992c5 | 2018-04-26 11:06:59 +0300 | [diff] [blame] | 100 | * When unloading, the driver should detach from the panel by calling |
| 101 | * drm_panel_detach(). |
| 102 | * |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 103 | * Return: 0 on success or a negative error code on failure. |
| 104 | */ |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 105 | int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector) |
| 106 | { |
| 107 | if (panel->connector) |
| 108 | return -EBUSY; |
| 109 | |
| 110 | panel->connector = connector; |
| 111 | panel->drm = connector->dev; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | EXPORT_SYMBOL(drm_panel_attach); |
| 116 | |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 117 | /** |
| 118 | * drm_panel_detach - detach a panel from a connector |
| 119 | * @panel: DRM panel |
| 120 | * |
| 121 | * Detaches a panel from the connector it is attached to. If a panel is not |
| 122 | * attached to any connector this is effectively a no-op. |
| 123 | * |
Jyri Sarha | 38992c5 | 2018-04-26 11:06:59 +0300 | [diff] [blame] | 124 | * This function should not be called by the panel device itself. It |
| 125 | * is only for the drm device that called drm_panel_attach(). |
| 126 | * |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 127 | * Return: 0 on success or a negative error code on failure. |
| 128 | */ |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 129 | int drm_panel_detach(struct drm_panel *panel) |
| 130 | { |
| 131 | panel->connector = NULL; |
| 132 | panel->drm = NULL; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | EXPORT_SYMBOL(drm_panel_detach); |
| 137 | |
Sam Ravnborg | 7a833d3 | 2019-08-04 22:16:32 +0200 | [diff] [blame^] | 138 | /** |
| 139 | * drm_panel_prepare - power on a panel |
| 140 | * @panel: DRM panel |
| 141 | * |
| 142 | * Calling this function will enable power and deassert any reset signals to |
| 143 | * the panel. After this has completed it is possible to communicate with any |
| 144 | * integrated circuitry via a command bus. |
| 145 | * |
| 146 | * Return: 0 on success or a negative error code on failure. |
| 147 | */ |
| 148 | int drm_panel_prepare(struct drm_panel *panel) |
| 149 | { |
| 150 | if (panel && panel->funcs && panel->funcs->prepare) |
| 151 | return panel->funcs->prepare(panel); |
| 152 | |
| 153 | return panel ? -ENOSYS : -EINVAL; |
| 154 | } |
| 155 | EXPORT_SYMBOL(drm_panel_prepare); |
| 156 | |
| 157 | /** |
| 158 | * drm_panel_unprepare - power off a panel |
| 159 | * @panel: DRM panel |
| 160 | * |
| 161 | * Calling this function will completely power off a panel (assert the panel's |
| 162 | * reset, turn off power supplies, ...). After this function has completed, it |
| 163 | * is usually no longer possible to communicate with the panel until another |
| 164 | * call to drm_panel_prepare(). |
| 165 | * |
| 166 | * Return: 0 on success or a negative error code on failure. |
| 167 | */ |
| 168 | int drm_panel_unprepare(struct drm_panel *panel) |
| 169 | { |
| 170 | if (panel && panel->funcs && panel->funcs->unprepare) |
| 171 | return panel->funcs->unprepare(panel); |
| 172 | |
| 173 | return panel ? -ENOSYS : -EINVAL; |
| 174 | } |
| 175 | EXPORT_SYMBOL(drm_panel_unprepare); |
| 176 | |
| 177 | /** |
| 178 | * drm_panel_enable - enable a panel |
| 179 | * @panel: DRM panel |
| 180 | * |
| 181 | * Calling this function will cause the panel display drivers to be turned on |
| 182 | * and the backlight to be enabled. Content will be visible on screen after |
| 183 | * this call completes. |
| 184 | * |
| 185 | * Return: 0 on success or a negative error code on failure. |
| 186 | */ |
| 187 | int drm_panel_enable(struct drm_panel *panel) |
| 188 | { |
| 189 | if (panel && panel->funcs && panel->funcs->enable) |
| 190 | return panel->funcs->enable(panel); |
| 191 | |
| 192 | return panel ? -ENOSYS : -EINVAL; |
| 193 | } |
| 194 | EXPORT_SYMBOL(drm_panel_enable); |
| 195 | |
| 196 | /** |
| 197 | * drm_panel_disable - disable a panel |
| 198 | * @panel: DRM panel |
| 199 | * |
| 200 | * This will typically turn off the panel's backlight or disable the display |
| 201 | * drivers. For smart panels it should still be possible to communicate with |
| 202 | * the integrated circuitry via any command bus after this call. |
| 203 | * |
| 204 | * Return: 0 on success or a negative error code on failure. |
| 205 | */ |
| 206 | int drm_panel_disable(struct drm_panel *panel) |
| 207 | { |
| 208 | if (panel && panel->funcs && panel->funcs->disable) |
| 209 | return panel->funcs->disable(panel); |
| 210 | |
| 211 | return panel ? -ENOSYS : -EINVAL; |
| 212 | } |
| 213 | EXPORT_SYMBOL(drm_panel_disable); |
| 214 | |
| 215 | /** |
| 216 | * drm_panel_get_modes - probe the available display modes of a panel |
| 217 | * @panel: DRM panel |
| 218 | * |
| 219 | * The modes probed from the panel are automatically added to the connector |
| 220 | * that the panel is attached to. |
| 221 | * |
| 222 | * Return: The number of modes available from the panel on success or a |
| 223 | * negative error code on failure. |
| 224 | */ |
| 225 | int drm_panel_get_modes(struct drm_panel *panel) |
| 226 | { |
| 227 | if (panel && panel->funcs && panel->funcs->get_modes) |
| 228 | return panel->funcs->get_modes(panel); |
| 229 | |
| 230 | return panel ? -ENOSYS : -EINVAL; |
| 231 | } |
| 232 | EXPORT_SYMBOL(drm_panel_get_modes); |
| 233 | |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 234 | #ifdef CONFIG_OF |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 235 | /** |
| 236 | * of_drm_find_panel - look up a panel using a device tree node |
| 237 | * @np: device tree node of the panel |
| 238 | * |
| 239 | * Searches the set of registered panels for one that matches the given device |
| 240 | * tree node. If a matching panel is found, return a pointer to it. |
| 241 | * |
| 242 | * Return: A pointer to the panel registered for the specified device tree |
Boris Brezillon | 5fa8e4a | 2018-05-09 15:00:39 +0200 | [diff] [blame] | 243 | * 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] | 244 | * |
Boris Brezillon | c59eb3c | 2018-05-09 15:00:40 +0200 | [diff] [blame] | 245 | * Possible error codes returned by this function: |
Sean Paul | 3eb3cd0 | 2018-08-15 16:38:28 -0400 | [diff] [blame] | 246 | * |
Boris Brezillon | c59eb3c | 2018-05-09 15:00:40 +0200 | [diff] [blame] | 247 | * - EPROBE_DEFER: the panel device has not been probed yet, and the caller |
| 248 | * should retry later |
| 249 | * - ENODEV: the device is not available (status != "okay" or "ok") |
Thierry Reding | 83127f6 | 2016-05-06 16:01:37 +0200 | [diff] [blame] | 250 | */ |
Laurent Pinchart | 327bc44 | 2016-11-19 05:28:05 +0200 | [diff] [blame] | 251 | struct drm_panel *of_drm_find_panel(const struct device_node *np) |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 252 | { |
| 253 | struct drm_panel *panel; |
| 254 | |
Boris Brezillon | c59eb3c | 2018-05-09 15:00:40 +0200 | [diff] [blame] | 255 | if (!of_device_is_available(np)) |
| 256 | return ERR_PTR(-ENODEV); |
| 257 | |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 258 | mutex_lock(&panel_lock); |
| 259 | |
| 260 | list_for_each_entry(panel, &panel_list, list) { |
| 261 | if (panel->dev->of_node == np) { |
| 262 | mutex_unlock(&panel_lock); |
| 263 | return panel; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | mutex_unlock(&panel_lock); |
Boris Brezillon | 5fa8e4a | 2018-05-09 15:00:39 +0200 | [diff] [blame] | 268 | return ERR_PTR(-EPROBE_DEFER); |
Thierry Reding | aead40e | 2013-08-30 13:36:43 +0200 | [diff] [blame] | 269 | } |
| 270 | EXPORT_SYMBOL(of_drm_find_panel); |
| 271 | #endif |
| 272 | |
| 273 | MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>"); |
| 274 | MODULE_DESCRIPTION("DRM panel infrastructure"); |
| 275 | MODULE_LICENSE("GPL and additional rights"); |