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