Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6-block.git] / drivers / gpu / drm / drm_panel.c
CommitLineData
aead40ea
TR
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
30static DEFINE_MUTEX(panel_lock);
31static LIST_HEAD(panel_list);
32
83127f67
TR
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
41/**
42 * drm_panel_init - initialize a panel
43 * @panel: DRM panel
44 *
45 * Sets up internal fields of the panel so that it can subsequently be added
46 * to the registry.
47 */
aead40ea
TR
48void drm_panel_init(struct drm_panel *panel)
49{
50 INIT_LIST_HEAD(&panel->list);
51}
52EXPORT_SYMBOL(drm_panel_init);
53
83127f67
TR
54/**
55 * drm_panel_add - add a panel to the global registry
56 * @panel: panel to add
57 *
58 * Add a panel to the global registry so that it can be looked up by display
59 * drivers.
60 *
61 * Return: 0 on success or a negative error code on failure.
62 */
aead40ea
TR
63int drm_panel_add(struct drm_panel *panel)
64{
65 mutex_lock(&panel_lock);
66 list_add_tail(&panel->list, &panel_list);
67 mutex_unlock(&panel_lock);
68
69 return 0;
70}
71EXPORT_SYMBOL(drm_panel_add);
72
83127f67
TR
73/**
74 * drm_panel_remove - remove a panel from the global registry
75 * @panel: DRM panel
76 *
77 * Removes a panel from the global registry.
78 */
aead40ea
TR
79void drm_panel_remove(struct drm_panel *panel)
80{
81 mutex_lock(&panel_lock);
82 list_del_init(&panel->list);
83 mutex_unlock(&panel_lock);
84}
85EXPORT_SYMBOL(drm_panel_remove);
86
83127f67
TR
87/**
88 * drm_panel_attach - attach a panel to a connector
89 * @panel: DRM panel
90 * @connector: DRM connector
91 *
92 * After obtaining a pointer to a DRM panel a display driver calls this
93 * function to attach a panel to a connector.
94 *
95 * An error is returned if the panel is already attached to another connector.
96 *
38992c57
JS
97 * When unloading, the driver should detach from the panel by calling
98 * drm_panel_detach().
99 *
83127f67
TR
100 * Return: 0 on success or a negative error code on failure.
101 */
aead40ea
TR
102int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
103{
104 if (panel->connector)
105 return -EBUSY;
106
107 panel->connector = connector;
108 panel->drm = connector->dev;
109
110 return 0;
111}
112EXPORT_SYMBOL(drm_panel_attach);
113
83127f67
TR
114/**
115 * drm_panel_detach - detach a panel from a connector
116 * @panel: DRM panel
117 *
118 * Detaches a panel from the connector it is attached to. If a panel is not
119 * attached to any connector this is effectively a no-op.
120 *
38992c57
JS
121 * This function should not be called by the panel device itself. It
122 * is only for the drm device that called drm_panel_attach().
123 *
83127f67
TR
124 * Return: 0 on success or a negative error code on failure.
125 */
aead40ea
TR
126int drm_panel_detach(struct drm_panel *panel)
127{
128 panel->connector = NULL;
129 panel->drm = NULL;
130
131 return 0;
132}
133EXPORT_SYMBOL(drm_panel_detach);
134
135#ifdef CONFIG_OF
83127f67
TR
136/**
137 * of_drm_find_panel - look up a panel using a device tree node
138 * @np: device tree node of the panel
139 *
140 * Searches the set of registered panels for one that matches the given device
141 * tree node. If a matching panel is found, return a pointer to it.
142 *
143 * Return: A pointer to the panel registered for the specified device tree
5fa8e4a2 144 * node or an ERR_PTR() if no panel matching the device tree node can be found.
3eb3cd04 145 *
c59eb3cf 146 * Possible error codes returned by this function:
3eb3cd04 147 *
c59eb3cf
BB
148 * - EPROBE_DEFER: the panel device has not been probed yet, and the caller
149 * should retry later
150 * - ENODEV: the device is not available (status != "okay" or "ok")
83127f67 151 */
327bc443 152struct drm_panel *of_drm_find_panel(const struct device_node *np)
aead40ea
TR
153{
154 struct drm_panel *panel;
155
c59eb3cf
BB
156 if (!of_device_is_available(np))
157 return ERR_PTR(-ENODEV);
158
aead40ea
TR
159 mutex_lock(&panel_lock);
160
161 list_for_each_entry(panel, &panel_list, list) {
162 if (panel->dev->of_node == np) {
163 mutex_unlock(&panel_lock);
164 return panel;
165 }
166 }
167
168 mutex_unlock(&panel_lock);
5fa8e4a2 169 return ERR_PTR(-EPROBE_DEFER);
aead40ea
TR
170}
171EXPORT_SYMBOL(of_drm_find_panel);
172#endif
173
174MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
175MODULE_DESCRIPTION("DRM panel infrastructure");
176MODULE_LICENSE("GPL and additional rights");