treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / drm_panel.c
blob8c7bac85a793779bf93476a33b7423142e42c1ad
1 /*
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
13 * of the Software.
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);
35 /**
36 * DOC: drm panel
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.
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().
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
54 * Initialize the panel structure for subsequent registration with
55 * drm_panel_add().
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 panel->dev = dev;
62 panel->funcs = funcs;
63 panel->connector_type = connector_type;
65 EXPORT_SYMBOL(drm_panel_init);
67 /**
68 * drm_panel_add - add a panel to the global registry
69 * @panel: panel to add
71 * Add a panel to the global registry so that it can be looked up by display
72 * drivers.
74 * Return: 0 on success or a negative error code on failure.
76 int 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 return 0;
84 EXPORT_SYMBOL(drm_panel_add);
86 /**
87 * drm_panel_remove - remove a panel from the global registry
88 * @panel: DRM panel
90 * Removes a panel from the global registry.
92 void drm_panel_remove(struct drm_panel *panel)
94 mutex_lock(&panel_lock);
95 list_del_init(&panel->list);
96 mutex_unlock(&panel_lock);
98 EXPORT_SYMBOL(drm_panel_remove);
101 * drm_panel_attach - attach a panel to a connector
102 * @panel: DRM panel
103 * @connector: DRM connector
105 * After obtaining a pointer to a DRM panel a display driver calls this
106 * function to attach a panel to a connector.
108 * An error is returned if the panel is already attached to another connector.
110 * When unloading, the driver should detach from the panel by calling
111 * drm_panel_detach().
113 * Return: 0 on success or a negative error code on failure.
115 int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
117 return 0;
119 EXPORT_SYMBOL(drm_panel_attach);
122 * drm_panel_detach - detach a panel from a connector
123 * @panel: DRM panel
125 * Detaches a panel from the connector it is attached to. If a panel is not
126 * attached to any connector this is effectively a no-op.
128 * This function should not be called by the panel device itself. It
129 * is only for the drm device that called drm_panel_attach().
131 void drm_panel_detach(struct drm_panel *panel)
134 EXPORT_SYMBOL(drm_panel_detach);
137 * drm_panel_prepare - power on a panel
138 * @panel: DRM panel
140 * Calling this function will enable power and deassert any reset signals to
141 * the panel. After this has completed it is possible to communicate with any
142 * integrated circuitry via a command bus.
144 * Return: 0 on success or a negative error code on failure.
146 int drm_panel_prepare(struct drm_panel *panel)
148 if (!panel)
149 return -EINVAL;
151 if (panel->funcs && panel->funcs->prepare)
152 return panel->funcs->prepare(panel);
154 return 0;
156 EXPORT_SYMBOL(drm_panel_prepare);
159 * drm_panel_unprepare - power off a panel
160 * @panel: DRM panel
162 * Calling this function will completely power off a panel (assert the panel's
163 * reset, turn off power supplies, ...). After this function has completed, it
164 * is usually no longer possible to communicate with the panel until another
165 * call to drm_panel_prepare().
167 * Return: 0 on success or a negative error code on failure.
169 int drm_panel_unprepare(struct drm_panel *panel)
171 if (!panel)
172 return -EINVAL;
174 if (panel->funcs && panel->funcs->unprepare)
175 return panel->funcs->unprepare(panel);
177 return 0;
179 EXPORT_SYMBOL(drm_panel_unprepare);
182 * drm_panel_enable - enable a panel
183 * @panel: DRM panel
185 * Calling this function will cause the panel display drivers to be turned on
186 * and the backlight to be enabled. Content will be visible on screen after
187 * this call completes.
189 * Return: 0 on success or a negative error code on failure.
191 int drm_panel_enable(struct drm_panel *panel)
193 int ret;
195 if (!panel)
196 return -EINVAL;
198 if (panel->funcs && panel->funcs->enable) {
199 ret = panel->funcs->enable(panel);
200 if (ret < 0)
201 return ret;
204 ret = backlight_enable(panel->backlight);
205 if (ret < 0)
206 DRM_DEV_INFO(panel->dev, "failed to enable backlight: %d\n",
207 ret);
209 return 0;
211 EXPORT_SYMBOL(drm_panel_enable);
214 * drm_panel_disable - disable a panel
215 * @panel: DRM panel
217 * This will typically turn off the panel's backlight or disable the display
218 * drivers. For smart panels it should still be possible to communicate with
219 * the integrated circuitry via any command bus after this call.
221 * Return: 0 on success or a negative error code on failure.
223 int drm_panel_disable(struct drm_panel *panel)
225 int ret;
227 if (!panel)
228 return -EINVAL;
230 ret = backlight_disable(panel->backlight);
231 if (ret < 0)
232 DRM_DEV_INFO(panel->dev, "failed to disable backlight: %d\n",
233 ret);
235 if (panel->funcs && panel->funcs->disable)
236 return panel->funcs->disable(panel);
238 return 0;
240 EXPORT_SYMBOL(drm_panel_disable);
243 * drm_panel_get_modes - probe the available display modes of a panel
244 * @panel: DRM panel
245 * @connector: DRM connector
247 * The modes probed from the panel are automatically added to the connector
248 * that the panel is attached to.
250 * Return: The number of modes available from the panel on success or a
251 * negative error code on failure.
253 int drm_panel_get_modes(struct drm_panel *panel,
254 struct drm_connector *connector)
256 if (!panel)
257 return -EINVAL;
259 if (panel->funcs && panel->funcs->get_modes)
260 return panel->funcs->get_modes(panel, connector);
262 return -EOPNOTSUPP;
264 EXPORT_SYMBOL(drm_panel_get_modes);
266 #ifdef CONFIG_OF
268 * of_drm_find_panel - look up a panel using a device tree node
269 * @np: device tree node of the panel
271 * Searches the set of registered panels for one that matches the given device
272 * tree node. If a matching panel is found, return a pointer to it.
274 * Return: A pointer to the panel registered for the specified device tree
275 * node or an ERR_PTR() if no panel matching the device tree node can be found.
277 * Possible error codes returned by this function:
279 * - EPROBE_DEFER: the panel device has not been probed yet, and the caller
280 * should retry later
281 * - ENODEV: the device is not available (status != "okay" or "ok")
283 struct drm_panel *of_drm_find_panel(const struct device_node *np)
285 struct drm_panel *panel;
287 if (!of_device_is_available(np))
288 return ERR_PTR(-ENODEV);
290 mutex_lock(&panel_lock);
292 list_for_each_entry(panel, &panel_list, list) {
293 if (panel->dev->of_node == np) {
294 mutex_unlock(&panel_lock);
295 return panel;
299 mutex_unlock(&panel_lock);
300 return ERR_PTR(-EPROBE_DEFER);
302 EXPORT_SYMBOL(of_drm_find_panel);
303 #endif
305 #if IS_REACHABLE(CONFIG_BACKLIGHT_CLASS_DEVICE)
307 * drm_panel_of_backlight - use backlight device node for backlight
308 * @panel: DRM panel
310 * Use this function to enable backlight handling if your panel
311 * uses device tree and has a backlight phandle.
313 * When the panel is enabled backlight will be enabled after a
314 * successful call to &drm_panel_funcs.enable()
316 * When the panel is disabled backlight will be disabled before the
317 * call to &drm_panel_funcs.disable().
319 * A typical implementation for a panel driver supporting device tree
320 * will call this function at probe time. Backlight will then be handled
321 * transparently without requiring any intervention from the driver.
322 * drm_panel_of_backlight() must be called after the call to drm_panel_init().
324 * Return: 0 on success or a negative error code on failure.
326 int drm_panel_of_backlight(struct drm_panel *panel)
328 struct backlight_device *backlight;
330 if (!panel || !panel->dev)
331 return -EINVAL;
333 backlight = devm_of_find_backlight(panel->dev);
335 if (IS_ERR(backlight))
336 return PTR_ERR(backlight);
338 panel->backlight = backlight;
339 return 0;
341 EXPORT_SYMBOL(drm_panel_of_backlight);
342 #endif
344 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
345 MODULE_DESCRIPTION("DRM panel infrastructure");
346 MODULE_LICENSE("GPL and additional rights");