blob: b902361dee6e1db300c10ce5798de7bada296b65 [file] [log] [blame]
Thierry Redingaead40e2013-08-30 13:36:43 +02001/*
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
Jyri Sarha0c087542018-04-26 11:07:00 +030027#include <drm/drm_device.h>
Thierry Redingaead40e2013-08-30 13:36:43 +020028#include <drm/drm_crtc.h>
29#include <drm/drm_panel.h>
30
31static DEFINE_MUTEX(panel_lock);
32static LIST_HEAD(panel_list);
33
Thierry Reding83127f62016-05-06 16:01:37 +020034/**
35 * DOC: drm panel
36 *
37 * The DRM panel helpers allow drivers to register panel objects with a
38 * central registry and provide functions to retrieve those panels in display
39 * drivers.
40 */
41
42/**
43 * drm_panel_init - initialize a panel
44 * @panel: DRM panel
45 *
46 * Sets up internal fields of the panel so that it can subsequently be added
47 * to the registry.
48 */
Thierry Redingaead40e2013-08-30 13:36:43 +020049void drm_panel_init(struct drm_panel *panel)
50{
51 INIT_LIST_HEAD(&panel->list);
52}
53EXPORT_SYMBOL(drm_panel_init);
54
Thierry Reding83127f62016-05-06 16:01:37 +020055/**
56 * drm_panel_add - add a panel to the global registry
57 * @panel: panel to add
58 *
59 * Add a panel to the global registry so that it can be looked up by display
60 * drivers.
61 *
62 * Return: 0 on success or a negative error code on failure.
63 */
Thierry Redingaead40e2013-08-30 13:36:43 +020064int drm_panel_add(struct drm_panel *panel)
65{
66 mutex_lock(&panel_lock);
67 list_add_tail(&panel->list, &panel_list);
68 mutex_unlock(&panel_lock);
69
70 return 0;
71}
72EXPORT_SYMBOL(drm_panel_add);
73
Thierry Reding83127f62016-05-06 16:01:37 +020074/**
75 * drm_panel_remove - remove a panel from the global registry
76 * @panel: DRM panel
77 *
78 * Removes a panel from the global registry.
79 */
Thierry Redingaead40e2013-08-30 13:36:43 +020080void drm_panel_remove(struct drm_panel *panel)
81{
82 mutex_lock(&panel_lock);
83 list_del_init(&panel->list);
84 mutex_unlock(&panel_lock);
85}
86EXPORT_SYMBOL(drm_panel_remove);
87
Thierry Reding83127f62016-05-06 16:01:37 +020088/**
89 * drm_panel_attach - attach a panel to a connector
90 * @panel: DRM panel
91 * @connector: DRM connector
92 *
93 * After obtaining a pointer to a DRM panel a display driver calls this
94 * function to attach a panel to a connector.
95 *
96 * An error is returned if the panel is already attached to another connector.
97 *
Jyri Sarha38992c52018-04-26 11:06:59 +030098 * When unloading, the driver should detach from the panel by calling
99 * drm_panel_detach().
100 *
Thierry Reding83127f62016-05-06 16:01:37 +0200101 * Return: 0 on success or a negative error code on failure.
102 */
Thierry Redingaead40e2013-08-30 13:36:43 +0200103int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
104{
105 if (panel->connector)
106 return -EBUSY;
107
Jyri Sarha0c087542018-04-26 11:07:00 +0300108 panel->link = device_link_add(connector->dev->dev, panel->dev, 0);
109 if (!panel->link) {
110 dev_err(panel->dev, "failed to link panel to %s\n",
111 dev_name(connector->dev->dev));
112 return -EINVAL;
113 }
114
Thierry Redingaead40e2013-08-30 13:36:43 +0200115 panel->connector = connector;
116 panel->drm = connector->dev;
117
118 return 0;
119}
120EXPORT_SYMBOL(drm_panel_attach);
121
Thierry Reding83127f62016-05-06 16:01:37 +0200122/**
123 * drm_panel_detach - detach a panel from a connector
124 * @panel: DRM panel
125 *
126 * Detaches a panel from the connector it is attached to. If a panel is not
127 * attached to any connector this is effectively a no-op.
128 *
Jyri Sarha38992c52018-04-26 11:06:59 +0300129 * This function should not be called by the panel device itself. It
130 * is only for the drm device that called drm_panel_attach().
131 *
Thierry Reding83127f62016-05-06 16:01:37 +0200132 * Return: 0 on success or a negative error code on failure.
133 */
Thierry Redingaead40e2013-08-30 13:36:43 +0200134int drm_panel_detach(struct drm_panel *panel)
135{
Jyri Sarha0c087542018-04-26 11:07:00 +0300136 device_link_del(panel->link);
137
Thierry Redingaead40e2013-08-30 13:36:43 +0200138 panel->connector = NULL;
139 panel->drm = NULL;
140
141 return 0;
142}
143EXPORT_SYMBOL(drm_panel_detach);
144
145#ifdef CONFIG_OF
Thierry Reding83127f62016-05-06 16:01:37 +0200146/**
147 * of_drm_find_panel - look up a panel using a device tree node
148 * @np: device tree node of the panel
149 *
150 * Searches the set of registered panels for one that matches the given device
151 * tree node. If a matching panel is found, return a pointer to it.
152 *
153 * Return: A pointer to the panel registered for the specified device tree
Boris Brezillon5fa8e4a2018-05-09 15:00:39 +0200154 * node or an ERR_PTR() if no panel matching the device tree node can be found.
Boris Brezillonc59eb3c2018-05-09 15:00:40 +0200155 * Possible error codes returned by this function:
156 * - EPROBE_DEFER: the panel device has not been probed yet, and the caller
157 * should retry later
158 * - ENODEV: the device is not available (status != "okay" or "ok")
Thierry Reding83127f62016-05-06 16:01:37 +0200159 */
Laurent Pinchart327bc442016-11-19 05:28:05 +0200160struct drm_panel *of_drm_find_panel(const struct device_node *np)
Thierry Redingaead40e2013-08-30 13:36:43 +0200161{
162 struct drm_panel *panel;
163
Boris Brezillonc59eb3c2018-05-09 15:00:40 +0200164 if (!of_device_is_available(np))
165 return ERR_PTR(-ENODEV);
166
Thierry Redingaead40e2013-08-30 13:36:43 +0200167 mutex_lock(&panel_lock);
168
169 list_for_each_entry(panel, &panel_list, list) {
170 if (panel->dev->of_node == np) {
171 mutex_unlock(&panel_lock);
172 return panel;
173 }
174 }
175
176 mutex_unlock(&panel_lock);
Boris Brezillon5fa8e4a2018-05-09 15:00:39 +0200177 return ERR_PTR(-EPROBE_DEFER);
Thierry Redingaead40e2013-08-30 13:36:43 +0200178}
179EXPORT_SYMBOL(of_drm_find_panel);
180#endif
181
182MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
183MODULE_DESCRIPTION("DRM panel infrastructure");
184MODULE_LICENSE("GPL and additional rights");