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
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.
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().
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 */
drm_panel_init(struct drm_panel * panel)51 void drm_panel_init(struct drm_panel *panel)
52 {
53 INIT_LIST_HEAD(&panel->list);
54 BLOCKING_INIT_NOTIFIER_HEAD(&panel->nh);
55 }
56 EXPORT_SYMBOL(drm_panel_init);
57
58 /**
59 * drm_panel_add - add a panel to the global registry
60 * @panel: panel to add
61 *
62 * Add a panel to the global registry so that it can be looked up by display
63 * drivers.
64 *
65 * Return: 0 on success or a negative error code on failure.
66 */
drm_panel_add(struct drm_panel * panel)67 int drm_panel_add(struct drm_panel *panel)
68 {
69 mutex_lock(&panel_lock);
70 list_add_tail(&panel->list, &panel_list);
71 mutex_unlock(&panel_lock);
72
73 return 0;
74 }
75 EXPORT_SYMBOL(drm_panel_add);
76
77 /**
78 * drm_panel_remove - remove a panel from the global registry
79 * @panel: DRM panel
80 *
81 * Removes a panel from the global registry.
82 */
drm_panel_remove(struct drm_panel * panel)83 void drm_panel_remove(struct drm_panel *panel)
84 {
85 mutex_lock(&panel_lock);
86 list_del_init(&panel->list);
87 mutex_unlock(&panel_lock);
88 }
89 EXPORT_SYMBOL(drm_panel_remove);
90
91 /**
92 * drm_panel_attach - attach a panel to a connector
93 * @panel: DRM panel
94 * @connector: DRM connector
95 *
96 * After obtaining a pointer to a DRM panel a display driver calls this
97 * function to attach a panel to a connector.
98 *
99 * An error is returned if the panel is already attached to another connector.
100 *
101 * When unloading, the driver should detach from the panel by calling
102 * drm_panel_detach().
103 *
104 * Return: 0 on success or a negative error code on failure.
105 */
drm_panel_attach(struct drm_panel * panel,struct drm_connector * connector)106 int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
107 {
108 if (panel->connector)
109 return -EBUSY;
110
111 panel->connector = connector;
112 panel->drm = connector->dev;
113
114 return 0;
115 }
116 EXPORT_SYMBOL(drm_panel_attach);
117
118 /**
119 * drm_panel_detach - detach a panel from a connector
120 * @panel: DRM panel
121 *
122 * Detaches a panel from the connector it is attached to. If a panel is not
123 * attached to any connector this is effectively a no-op.
124 *
125 * This function should not be called by the panel device itself. It
126 * is only for the drm device that called drm_panel_attach().
127 */
drm_panel_detach(struct drm_panel * panel)128 void drm_panel_detach(struct drm_panel *panel)
129 {
130 panel->connector = NULL;
131 panel->drm = NULL;
132 }
133 EXPORT_SYMBOL(drm_panel_detach);
134
135 /**
136 * drm_panel_prepare - power on a panel
137 * @panel: DRM panel
138 *
139 * Calling this function will enable power and deassert any reset signals to
140 * the panel. After this has completed it is possible to communicate with any
141 * integrated circuitry via a command bus.
142 *
143 * Return: 0 on success or a negative error code on failure.
144 */
drm_panel_prepare(struct drm_panel * panel)145 int drm_panel_prepare(struct drm_panel *panel)
146 {
147 if (panel && panel->funcs && panel->funcs->prepare)
148 return panel->funcs->prepare(panel);
149
150 return panel ? -ENOSYS : -EINVAL;
151 }
152 EXPORT_SYMBOL(drm_panel_prepare);
153
154 /**
155 * drm_panel_unprepare - power off a panel
156 * @panel: DRM panel
157 *
158 * Calling this function will completely power off a panel (assert the panel's
159 * reset, turn off power supplies, ...). After this function has completed, it
160 * is usually no longer possible to communicate with the panel until another
161 * call to drm_panel_prepare().
162 *
163 * Return: 0 on success or a negative error code on failure.
164 */
drm_panel_unprepare(struct drm_panel * panel)165 int drm_panel_unprepare(struct drm_panel *panel)
166 {
167 if (panel && panel->funcs && panel->funcs->unprepare)
168 return panel->funcs->unprepare(panel);
169
170 return panel ? -ENOSYS : -EINVAL;
171 }
172 EXPORT_SYMBOL(drm_panel_unprepare);
173
174 /**
175 * drm_panel_enable - enable a panel
176 * @panel: DRM panel
177 *
178 * Calling this function will cause the panel display drivers to be turned on
179 * and the backlight to be enabled. Content will be visible on screen after
180 * this call completes.
181 *
182 * Return: 0 on success or a negative error code on failure.
183 */
drm_panel_enable(struct drm_panel * panel)184 int drm_panel_enable(struct drm_panel *panel)
185 {
186 if (panel && panel->funcs && panel->funcs->enable)
187 return panel->funcs->enable(panel);
188
189 return panel ? -ENOSYS : -EINVAL;
190 }
191 EXPORT_SYMBOL(drm_panel_enable);
192
193 /**
194 * drm_panel_disable - disable a panel
195 * @panel: DRM panel
196 *
197 * This will typically turn off the panel's backlight or disable the display
198 * drivers. For smart panels it should still be possible to communicate with
199 * the integrated circuitry via any command bus after this call.
200 *
201 * Return: 0 on success or a negative error code on failure.
202 */
drm_panel_disable(struct drm_panel * panel)203 int drm_panel_disable(struct drm_panel *panel)
204 {
205 if (panel && panel->funcs && panel->funcs->disable)
206 return panel->funcs->disable(panel);
207
208 return panel ? -ENOSYS : -EINVAL;
209 }
210 EXPORT_SYMBOL(drm_panel_disable);
211
212 /**
213 * drm_panel_get_modes - probe the available display modes of a panel
214 * @panel: DRM panel
215 *
216 * The modes probed from the panel are automatically added to the connector
217 * that the panel is attached to.
218 *
219 * Return: The number of modes available from the panel on success or a
220 * negative error code on failure.
221 */
drm_panel_get_modes(struct drm_panel * panel)222 int drm_panel_get_modes(struct drm_panel *panel)
223 {
224 if (panel && panel->funcs && panel->funcs->get_modes)
225 return panel->funcs->get_modes(panel);
226
227 return panel ? -ENOSYS : -EINVAL;
228 }
229 EXPORT_SYMBOL(drm_panel_get_modes);
230
231 #ifdef CONFIG_OF
232 /**
233 * of_drm_find_panel - look up a panel using a device tree node
234 * @np: device tree node of the panel
235 *
236 * Searches the set of registered panels for one that matches the given device
237 * tree node. If a matching panel is found, return a pointer to it.
238 *
239 * Return: A pointer to the panel registered for the specified device tree
240 * node or an ERR_PTR() if no panel matching the device tree node can be found.
241 *
242 * Possible error codes returned by this function:
243 *
244 * - EPROBE_DEFER: the panel device has not been probed yet, and the caller
245 * should retry later
246 * - ENODEV: the device is not available (status != "okay" or "ok")
247 */
of_drm_find_panel(const struct device_node * np)248 struct drm_panel *of_drm_find_panel(const struct device_node *np)
249 {
250 struct drm_panel *panel;
251
252 if (!of_device_is_available(np))
253 return ERR_PTR(-ENODEV);
254
255 mutex_lock(&panel_lock);
256
257 list_for_each_entry(panel, &panel_list, list) {
258 if (panel->dev->of_node == np) {
259 mutex_unlock(&panel_lock);
260 return panel;
261 }
262 }
263
264 mutex_unlock(&panel_lock);
265 return ERR_PTR(-EPROBE_DEFER);
266 }
267 EXPORT_SYMBOL(of_drm_find_panel);
268 #endif
269
drm_panel_notifier_register(struct drm_panel * panel,struct notifier_block * nb)270 int drm_panel_notifier_register(struct drm_panel *panel,
271 struct notifier_block *nb)
272 {
273 return blocking_notifier_chain_register(&panel->nh, nb);
274 }
275 EXPORT_SYMBOL(drm_panel_notifier_register);
276
drm_panel_notifier_unregister(struct drm_panel * panel,struct notifier_block * nb)277 int drm_panel_notifier_unregister(struct drm_panel *panel,
278 struct notifier_block *nb)
279 {
280 return blocking_notifier_chain_unregister(&panel->nh, nb);
281 }
282 EXPORT_SYMBOL(drm_panel_notifier_unregister);
283
drm_panel_notifier_call_chain(struct drm_panel * panel,unsigned long val,void * v)284 int drm_panel_notifier_call_chain(struct drm_panel *panel,
285 unsigned long val, void *v)
286 {
287 return blocking_notifier_call_chain(&panel->nh, val, v);
288 }
289 EXPORT_SYMBOL(drm_panel_notifier_call_chain);
290
291 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
292 MODULE_DESCRIPTION("DRM panel infrastructure");
293 MODULE_LICENSE("GPL and additional rights");
294