blob: f2f71d71494a276411ff4d29e4884bbd8b558a99 [file] [log] [blame]
Liviu Dudaudf785aa2015-10-20 10:23:12 +01001#include <linux/component.h>
Russell King7e435aa2014-06-15 11:07:12 +01002#include <linux/export.h>
3#include <linux/list.h>
4#include <linux/of_graph.h>
5#include <drm/drmP.h>
Rob Herring1f2db302017-03-22 08:26:05 -05006#include <drm/drm_bridge.h>
Russell King7e435aa2014-06-15 11:07:12 +01007#include <drm/drm_crtc.h>
Laurent Pinchart93382032016-11-28 20:51:09 +02008#include <drm/drm_encoder.h>
Rob Herring1f2db302017-03-22 08:26:05 -05009#include <drm/drm_panel.h>
Russell King7e435aa2014-06-15 11:07:12 +010010#include <drm/drm_of.h>
11
Daniel Vetter7f9e7ec2018-07-09 10:40:14 +020012/**
13 * DOC: overview
14 *
15 * A set of helper functions to aid DRM drivers in parsing standard DT
16 * properties.
17 */
18
Russell King97ac0e42016-10-19 11:28:27 +010019static void drm_release_of(struct device *dev, void *data)
20{
21 of_node_put(data);
22}
23
Russell King7e435aa2014-06-15 11:07:12 +010024/**
Jernej Skrabec8b5f7a62018-06-25 14:03:01 +020025 * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node
Russell King7e435aa2014-06-15 11:07:12 +010026 * @dev: DRM device
27 * @port: port OF node
28 *
29 * Given a port OF node, return the possible mask of the corresponding
30 * CRTC within a device's list of CRTCs. Returns zero if not found.
31 */
Jernej Skrabec8b5f7a62018-06-25 14:03:01 +020032uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
33 struct device_node *port)
Russell King7e435aa2014-06-15 11:07:12 +010034{
35 unsigned int index = 0;
36 struct drm_crtc *tmp;
37
Daniel Vetter6295d602015-07-09 23:44:25 +020038 drm_for_each_crtc(tmp, dev) {
Russell King7e435aa2014-06-15 11:07:12 +010039 if (tmp->port == port)
40 return 1 << index;
41
42 index++;
43 }
44
45 return 0;
46}
Jernej Skrabec8b5f7a62018-06-25 14:03:01 +020047EXPORT_SYMBOL(drm_of_crtc_port_mask);
Russell King7e435aa2014-06-15 11:07:12 +010048
49/**
50 * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
51 * @dev: DRM device
52 * @port: encoder port to scan for endpoints
53 *
54 * Scan all endpoints attached to a port, locate their attached CRTCs,
55 * and generate the DRM mask of CRTCs which may be attached to this
56 * encoder.
57 *
58 * See Documentation/devicetree/bindings/graph.txt for the bindings.
59 */
60uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
61 struct device_node *port)
62{
Philipp Zabel7416f4e2014-07-03 22:59:51 +020063 struct device_node *remote_port, *ep;
Russell King7e435aa2014-06-15 11:07:12 +010064 uint32_t possible_crtcs = 0;
65
Philipp Zabel7416f4e2014-07-03 22:59:51 +020066 for_each_endpoint_of_node(port, ep) {
Russell King7e435aa2014-06-15 11:07:12 +010067 remote_port = of_graph_get_remote_port(ep);
68 if (!remote_port) {
69 of_node_put(ep);
70 return 0;
71 }
72
Jernej Skrabec8b5f7a62018-06-25 14:03:01 +020073 possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
Russell King7e435aa2014-06-15 11:07:12 +010074
75 of_node_put(remote_port);
Philipp Zabel7416f4e2014-07-03 22:59:51 +020076 }
Russell King7e435aa2014-06-15 11:07:12 +010077
78 return possible_crtcs;
79}
80EXPORT_SYMBOL(drm_of_find_possible_crtcs);
Liviu Dudaudf785aa2015-10-20 10:23:12 +010081
82/**
Russell King97ac0e42016-10-19 11:28:27 +010083 * drm_of_component_match_add - Add a component helper OF node match rule
84 * @master: master device
85 * @matchptr: component match pointer
86 * @compare: compare function used for matching component
87 * @node: of_node
88 */
89void drm_of_component_match_add(struct device *master,
90 struct component_match **matchptr,
91 int (*compare)(struct device *, void *),
92 struct device_node *node)
93{
94 of_node_get(node);
95 component_match_add_release(master, matchptr, drm_release_of,
96 compare, node);
97}
98EXPORT_SYMBOL_GPL(drm_of_component_match_add);
99
100/**
Liviu Dudaudf785aa2015-10-20 10:23:12 +0100101 * drm_of_component_probe - Generic probe function for a component based master
102 * @dev: master device containing the OF node
103 * @compare_of: compare function used for matching components
Daniel Vetter7f9e7ec2018-07-09 10:40:14 +0200104 * @m_ops: component master ops to be used
Liviu Dudaudf785aa2015-10-20 10:23:12 +0100105 *
106 * Parse the platform device OF node and bind all the components associated
107 * with the master. Interface ports are added before the encoders in order to
108 * satisfy their .bind requirements
109 * See Documentation/devicetree/bindings/graph.txt for the bindings.
110 *
111 * Returns zero if successful, or one of the standard error codes if it fails.
112 */
113int drm_of_component_probe(struct device *dev,
114 int (*compare_of)(struct device *, void *),
115 const struct component_master_ops *m_ops)
116{
117 struct device_node *ep, *port, *remote;
118 struct component_match *match = NULL;
119 int i;
120
121 if (!dev->of_node)
122 return -EINVAL;
123
124 /*
125 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
126 * called from encoder's .bind callbacks works as expected
127 */
128 for (i = 0; ; i++) {
129 port = of_parse_phandle(dev->of_node, "ports", i);
130 if (!port)
131 break;
132
Baruch Siach2efa8392018-02-22 21:22:50 +0200133 if (of_device_is_available(port->parent))
134 drm_of_component_match_add(dev, &match, compare_of,
135 port);
Liviu Dudaudf785aa2015-10-20 10:23:12 +0100136
Liviu Dudaudf785aa2015-10-20 10:23:12 +0100137 of_node_put(port);
138 }
139
140 if (i == 0) {
141 dev_err(dev, "missing 'ports' property\n");
142 return -ENODEV;
143 }
144
145 if (!match) {
146 dev_err(dev, "no available port\n");
147 return -ENODEV;
148 }
149
150 /*
151 * For bound crtcs, bind the encoders attached to their remote endpoint
152 */
153 for (i = 0; ; i++) {
154 port = of_parse_phandle(dev->of_node, "ports", i);
155 if (!port)
156 break;
157
158 if (!of_device_is_available(port->parent)) {
159 of_node_put(port);
160 continue;
161 }
162
163 for_each_child_of_node(port, ep) {
164 remote = of_graph_get_remote_port_parent(ep);
165 if (!remote || !of_device_is_available(remote)) {
166 of_node_put(remote);
167 continue;
168 } else if (!of_device_is_available(remote->parent)) {
Rob Herring4bf99142017-07-18 16:43:04 -0500169 dev_warn(dev, "parent device of %pOF is not available\n",
170 remote);
Liviu Dudaudf785aa2015-10-20 10:23:12 +0100171 of_node_put(remote);
172 continue;
173 }
174
Russell King97ac0e42016-10-19 11:28:27 +0100175 drm_of_component_match_add(dev, &match, compare_of,
176 remote);
Liviu Dudaudf785aa2015-10-20 10:23:12 +0100177 of_node_put(remote);
178 }
179 of_node_put(port);
180 }
181
182 return component_master_add_with_match(dev, m_ops, match);
183}
184EXPORT_SYMBOL(drm_of_component_probe);
Philipp Zabel4cacf912015-02-24 11:34:01 +0100185
186/*
187 * drm_of_encoder_active_endpoint - return the active encoder endpoint
188 * @node: device tree node containing encoder input ports
189 * @encoder: drm_encoder
190 *
191 * Given an encoder device node and a drm_encoder with a connected crtc,
192 * parse the encoder endpoint connecting to the crtc port.
193 */
194int drm_of_encoder_active_endpoint(struct device_node *node,
195 struct drm_encoder *encoder,
196 struct of_endpoint *endpoint)
197{
198 struct device_node *ep;
199 struct drm_crtc *crtc = encoder->crtc;
200 struct device_node *port;
201 int ret;
202
203 if (!node || !crtc)
204 return -EINVAL;
205
206 for_each_endpoint_of_node(node, ep) {
207 port = of_graph_get_remote_port(ep);
208 of_node_put(port);
209 if (port == crtc->port) {
210 ret = of_graph_parse_endpoint(ep, endpoint);
211 of_node_put(ep);
212 return ret;
213 }
214 }
215
216 return -EINVAL;
217}
218EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
Rob Herring1f2db302017-03-22 08:26:05 -0500219
Daniel Vetter3fbdfe92019-01-11 17:40:45 +0100220/**
Rob Herring1f2db302017-03-22 08:26:05 -0500221 * drm_of_find_panel_or_bridge - return connected panel or bridge device
222 * @np: device tree node containing encoder output ports
Daniel Vetter3fbdfe92019-01-11 17:40:45 +0100223 * @port: port in the device tree node
224 * @endpoint: endpoint in the device tree node
Rob Herring1f2db302017-03-22 08:26:05 -0500225 * @panel: pointer to hold returned drm_panel
226 * @bridge: pointer to hold returned drm_bridge
227 *
228 * Given a DT node's port and endpoint number, find the connected node and
229 * return either the associated struct drm_panel or drm_bridge device. Either
230 * @panel or @bridge must not be NULL.
231 *
232 * Returns zero if successful, or one of the standard error codes if it fails.
233 */
234int drm_of_find_panel_or_bridge(const struct device_node *np,
235 int port, int endpoint,
236 struct drm_panel **panel,
237 struct drm_bridge **bridge)
238{
239 int ret = -EPROBE_DEFER;
240 struct device_node *remote;
241
242 if (!panel && !bridge)
243 return -EINVAL;
Dan Carpenter320e4212017-09-25 13:30:38 +0300244 if (panel)
245 *panel = NULL;
Rob Herring1f2db302017-03-22 08:26:05 -0500246
247 remote = of_graph_get_remote_node(np, port, endpoint);
248 if (!remote)
249 return -ENODEV;
250
Boris Brezillon2e64a172018-05-09 15:00:41 +0200251 if (!of_device_is_available(remote)) {
252 of_node_put(remote);
253 return -ENODEV;
254 }
255
Rob Herring1f2db302017-03-22 08:26:05 -0500256 if (panel) {
257 *panel = of_drm_find_panel(remote);
Boris Brezillon5fa8e4a2018-05-09 15:00:39 +0200258 if (!IS_ERR(*panel))
Rob Herring1f2db302017-03-22 08:26:05 -0500259 ret = 0;
Boris Brezillon5fa8e4a2018-05-09 15:00:39 +0200260 else
261 *panel = NULL;
Rob Herring1f2db302017-03-22 08:26:05 -0500262 }
263
264 /* No panel found yet, check for a bridge next. */
265 if (bridge) {
266 if (ret) {
267 *bridge = of_drm_find_bridge(remote);
268 if (*bridge)
269 ret = 0;
270 } else {
271 *bridge = NULL;
272 }
273
274 }
275
276 of_node_put(remote);
277 return ret;
278}
279EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);