xref: /linux/drivers/gpu/drm/drm_of.c (revision 73289afe03619bac585b69f563d0bb9a52e67722)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/component.h>
3 #include <linux/export.h>
4 #include <linux/list.h>
5 #include <linux/of.h>
6 #include <linux/of_graph.h>
7 
8 #include <drm/drm_bridge.h>
9 #include <drm/drm_crtc.h>
10 #include <drm/drm_device.h>
11 #include <drm/drm_encoder.h>
12 #include <drm/drm_of.h>
13 #include <drm/drm_panel.h>
14 
15 /**
16  * DOC: overview
17  *
18  * A set of helper functions to aid DRM drivers in parsing standard DT
19  * properties.
20  */
21 
22 /**
23  * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node
24  * @dev: DRM device
25  * @port: port OF node
26  *
27  * Given a port OF node, return the possible mask of the corresponding
28  * CRTC within a device's list of CRTCs.  Returns zero if not found.
29  */
30 uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
31 			    struct device_node *port)
32 {
33 	unsigned int index = 0;
34 	struct drm_crtc *tmp;
35 
36 	drm_for_each_crtc(tmp, dev) {
37 		if (tmp->port == port)
38 			return 1 << index;
39 
40 		index++;
41 	}
42 
43 	return 0;
44 }
45 EXPORT_SYMBOL(drm_of_crtc_port_mask);
46 
47 /**
48  * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
49  * @dev: DRM device
50  * @port: encoder port to scan for endpoints
51  *
52  * Scan all endpoints attached to a port, locate their attached CRTCs,
53  * and generate the DRM mask of CRTCs which may be attached to this
54  * encoder.
55  *
56  * See Documentation/devicetree/bindings/graph.txt for the bindings.
57  */
58 uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
59 				    struct device_node *port)
60 {
61 	struct device_node *remote_port, *ep;
62 	uint32_t possible_crtcs = 0;
63 
64 	for_each_endpoint_of_node(port, ep) {
65 		remote_port = of_graph_get_remote_port(ep);
66 		if (!remote_port) {
67 			of_node_put(ep);
68 			return 0;
69 		}
70 
71 		possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
72 
73 		of_node_put(remote_port);
74 	}
75 
76 	return possible_crtcs;
77 }
78 EXPORT_SYMBOL(drm_of_find_possible_crtcs);
79 
80 /**
81  * drm_of_component_match_add - Add a component helper OF node match rule
82  * @master: master device
83  * @matchptr: component match pointer
84  * @compare: compare function used for matching component
85  * @node: of_node
86  */
87 void drm_of_component_match_add(struct device *master,
88 				struct component_match **matchptr,
89 				int (*compare)(struct device *, void *),
90 				struct device_node *node)
91 {
92 	of_node_get(node);
93 	component_match_add_release(master, matchptr, component_release_of,
94 				    compare, node);
95 }
96 EXPORT_SYMBOL_GPL(drm_of_component_match_add);
97 
98 /**
99  * drm_of_component_probe - Generic probe function for a component based master
100  * @dev: master device containing the OF node
101  * @compare_of: compare function used for matching components
102  * @m_ops: component master ops to be used
103  *
104  * Parse the platform device OF node and bind all the components associated
105  * with the master. Interface ports are added before the encoders in order to
106  * satisfy their .bind requirements
107  * See Documentation/devicetree/bindings/graph.txt for the bindings.
108  *
109  * Returns zero if successful, or one of the standard error codes if it fails.
110  */
111 int drm_of_component_probe(struct device *dev,
112 			   int (*compare_of)(struct device *, void *),
113 			   const struct component_master_ops *m_ops)
114 {
115 	struct device_node *ep, *port, *remote;
116 	struct component_match *match = NULL;
117 	int i;
118 
119 	if (!dev->of_node)
120 		return -EINVAL;
121 
122 	/*
123 	 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
124 	 * called from encoder's .bind callbacks works as expected
125 	 */
126 	for (i = 0; ; i++) {
127 		port = of_parse_phandle(dev->of_node, "ports", i);
128 		if (!port)
129 			break;
130 
131 		if (of_device_is_available(port->parent))
132 			drm_of_component_match_add(dev, &match, compare_of,
133 						   port);
134 
135 		of_node_put(port);
136 	}
137 
138 	if (i == 0) {
139 		dev_err(dev, "missing 'ports' property\n");
140 		return -ENODEV;
141 	}
142 
143 	if (!match) {
144 		dev_err(dev, "no available port\n");
145 		return -ENODEV;
146 	}
147 
148 	/*
149 	 * For bound crtcs, bind the encoders attached to their remote endpoint
150 	 */
151 	for (i = 0; ; i++) {
152 		port = of_parse_phandle(dev->of_node, "ports", i);
153 		if (!port)
154 			break;
155 
156 		if (!of_device_is_available(port->parent)) {
157 			of_node_put(port);
158 			continue;
159 		}
160 
161 		for_each_child_of_node(port, ep) {
162 			remote = of_graph_get_remote_port_parent(ep);
163 			if (!remote || !of_device_is_available(remote)) {
164 				of_node_put(remote);
165 				continue;
166 			} else if (!of_device_is_available(remote->parent)) {
167 				dev_warn(dev, "parent device of %pOF is not available\n",
168 					 remote);
169 				of_node_put(remote);
170 				continue;
171 			}
172 
173 			drm_of_component_match_add(dev, &match, compare_of,
174 						   remote);
175 			of_node_put(remote);
176 		}
177 		of_node_put(port);
178 	}
179 
180 	return component_master_add_with_match(dev, m_ops, match);
181 }
182 EXPORT_SYMBOL(drm_of_component_probe);
183 
184 /*
185  * drm_of_encoder_active_endpoint - return the active encoder endpoint
186  * @node: device tree node containing encoder input ports
187  * @encoder: drm_encoder
188  *
189  * Given an encoder device node and a drm_encoder with a connected crtc,
190  * parse the encoder endpoint connecting to the crtc port.
191  */
192 int drm_of_encoder_active_endpoint(struct device_node *node,
193 				   struct drm_encoder *encoder,
194 				   struct of_endpoint *endpoint)
195 {
196 	struct device_node *ep;
197 	struct drm_crtc *crtc = encoder->crtc;
198 	struct device_node *port;
199 	int ret;
200 
201 	if (!node || !crtc)
202 		return -EINVAL;
203 
204 	for_each_endpoint_of_node(node, ep) {
205 		port = of_graph_get_remote_port(ep);
206 		of_node_put(port);
207 		if (port == crtc->port) {
208 			ret = of_graph_parse_endpoint(ep, endpoint);
209 			of_node_put(ep);
210 			return ret;
211 		}
212 	}
213 
214 	return -EINVAL;
215 }
216 EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
217 
218 /**
219  * drm_of_find_panel_or_bridge - return connected panel or bridge device
220  * @np: device tree node containing encoder output ports
221  * @port: port in the device tree node
222  * @endpoint: endpoint in the device tree node
223  * @panel: pointer to hold returned drm_panel
224  * @bridge: pointer to hold returned drm_bridge
225  *
226  * Given a DT node's port and endpoint number, find the connected node and
227  * return either the associated struct drm_panel or drm_bridge device. Either
228  * @panel or @bridge must not be NULL.
229  *
230  * This function is deprecated and should not be used in new drivers. Use
231  * devm_drm_of_get_bridge() instead.
232  *
233  * Returns zero if successful, or one of the standard error codes if it fails.
234  */
235 int drm_of_find_panel_or_bridge(const struct device_node *np,
236 				int port, int endpoint,
237 				struct drm_panel **panel,
238 				struct drm_bridge **bridge)
239 {
240 	int ret = -EPROBE_DEFER;
241 	struct device_node *remote;
242 
243 	if (!panel && !bridge)
244 		return -EINVAL;
245 	if (panel)
246 		*panel = NULL;
247 
248 	/*
249 	 * of_graph_get_remote_node() produces a noisy error message if port
250 	 * node isn't found and the absence of the port is a legit case here,
251 	 * so at first we silently check whether graph presents in the
252 	 * device-tree node.
253 	 */
254 	if (!of_graph_is_present(np))
255 		return -ENODEV;
256 
257 	remote = of_graph_get_remote_node(np, port, endpoint);
258 	if (!remote)
259 		return -ENODEV;
260 
261 	if (panel) {
262 		*panel = of_drm_find_panel(remote);
263 		if (!IS_ERR(*panel))
264 			ret = 0;
265 		else
266 			*panel = NULL;
267 	}
268 
269 	/* No panel found yet, check for a bridge next. */
270 	if (bridge) {
271 		if (ret) {
272 			*bridge = of_drm_find_bridge(remote);
273 			if (*bridge)
274 				ret = 0;
275 		} else {
276 			*bridge = NULL;
277 		}
278 
279 	}
280 
281 	of_node_put(remote);
282 	return ret;
283 }
284 EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);
285 
286 enum drm_of_lvds_pixels {
287 	DRM_OF_LVDS_EVEN = BIT(0),
288 	DRM_OF_LVDS_ODD = BIT(1),
289 };
290 
291 static int drm_of_lvds_get_port_pixels_type(struct device_node *port_node)
292 {
293 	bool even_pixels =
294 		of_property_read_bool(port_node, "dual-lvds-even-pixels");
295 	bool odd_pixels =
296 		of_property_read_bool(port_node, "dual-lvds-odd-pixels");
297 
298 	return (even_pixels ? DRM_OF_LVDS_EVEN : 0) |
299 	       (odd_pixels ? DRM_OF_LVDS_ODD : 0);
300 }
301 
302 static int drm_of_lvds_get_remote_pixels_type(
303 			const struct device_node *port_node)
304 {
305 	struct device_node *endpoint = NULL;
306 	int pixels_type = -EPIPE;
307 
308 	for_each_child_of_node(port_node, endpoint) {
309 		struct device_node *remote_port;
310 		int current_pt;
311 
312 		if (!of_node_name_eq(endpoint, "endpoint"))
313 			continue;
314 
315 		remote_port = of_graph_get_remote_port(endpoint);
316 		if (!remote_port) {
317 			of_node_put(endpoint);
318 			return -EPIPE;
319 		}
320 
321 		current_pt = drm_of_lvds_get_port_pixels_type(remote_port);
322 		of_node_put(remote_port);
323 		if (pixels_type < 0)
324 			pixels_type = current_pt;
325 
326 		/*
327 		 * Sanity check, ensure that all remote endpoints have the same
328 		 * pixel type. We may lift this restriction later if we need to
329 		 * support multiple sinks with different dual-link
330 		 * configurations by passing the endpoints explicitly to
331 		 * drm_of_lvds_get_dual_link_pixel_order().
332 		 */
333 		if (!current_pt || pixels_type != current_pt) {
334 			of_node_put(endpoint);
335 			return -EINVAL;
336 		}
337 	}
338 
339 	return pixels_type;
340 }
341 
342 /**
343  * drm_of_lvds_get_dual_link_pixel_order - Get LVDS dual-link pixel order
344  * @port1: First DT port node of the Dual-link LVDS source
345  * @port2: Second DT port node of the Dual-link LVDS source
346  *
347  * An LVDS dual-link connection is made of two links, with even pixels
348  * transitting on one link, and odd pixels on the other link. This function
349  * returns, for two ports of an LVDS dual-link source, which port shall transmit
350  * the even and odd pixels, based on the requirements of the connected sink.
351  *
352  * The pixel order is determined from the dual-lvds-even-pixels and
353  * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
354  * properties are not present, or if their usage is not valid, this function
355  * returns -EINVAL.
356  *
357  * If either port is not connected, this function returns -EPIPE.
358  *
359  * @port1 and @port2 are typically DT sibling nodes, but may have different
360  * parents when, for instance, two separate LVDS encoders carry the even and odd
361  * pixels.
362  *
363  * Return:
364  * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 carries even pixels and @port2
365  *   carries odd pixels
366  * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 carries odd pixels and @port2
367  *   carries even pixels
368  * * -EINVAL - @port1 and @port2 are not connected to a dual-link LVDS sink, or
369  *   the sink configuration is invalid
370  * * -EPIPE - when @port1 or @port2 are not connected
371  */
372 int drm_of_lvds_get_dual_link_pixel_order(const struct device_node *port1,
373 					  const struct device_node *port2)
374 {
375 	int remote_p1_pt, remote_p2_pt;
376 
377 	if (!port1 || !port2)
378 		return -EINVAL;
379 
380 	remote_p1_pt = drm_of_lvds_get_remote_pixels_type(port1);
381 	if (remote_p1_pt < 0)
382 		return remote_p1_pt;
383 
384 	remote_p2_pt = drm_of_lvds_get_remote_pixels_type(port2);
385 	if (remote_p2_pt < 0)
386 		return remote_p2_pt;
387 
388 	/*
389 	 * A valid dual-lVDS bus is found when one remote port is marked with
390 	 * "dual-lvds-even-pixels", and the other remote port is marked with
391 	 * "dual-lvds-odd-pixels", bail out if the markers are not right.
392 	 */
393 	if (remote_p1_pt + remote_p2_pt != DRM_OF_LVDS_EVEN + DRM_OF_LVDS_ODD)
394 		return -EINVAL;
395 
396 	return remote_p1_pt == DRM_OF_LVDS_EVEN ?
397 		DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS :
398 		DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS;
399 }
400 EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order);
401 
402 /**
403  * drm_of_lvds_get_data_mapping - Get LVDS data mapping
404  * @port: DT port node of the LVDS source or sink
405  *
406  * Convert DT "data-mapping" property string value into media bus format value.
407  *
408  * Return:
409  * * MEDIA_BUS_FMT_RGB666_1X7X3_SPWG - data-mapping is "jeida-18"
410  * * MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA - data-mapping is "jeida-24"
411  * * MEDIA_BUS_FMT_RGB888_1X7X4_SPWG - data-mapping is "vesa-24"
412  * * -EINVAL - the "data-mapping" property is unsupported
413  * * -ENODEV - the "data-mapping" property is missing
414  */
415 int drm_of_lvds_get_data_mapping(const struct device_node *port)
416 {
417 	const char *mapping;
418 	int ret;
419 
420 	ret = of_property_read_string(port, "data-mapping", &mapping);
421 	if (ret < 0)
422 		return -ENODEV;
423 
424 	if (!strcmp(mapping, "jeida-18"))
425 		return MEDIA_BUS_FMT_RGB666_1X7X3_SPWG;
426 	if (!strcmp(mapping, "jeida-24"))
427 		return MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA;
428 	if (!strcmp(mapping, "vesa-24"))
429 		return MEDIA_BUS_FMT_RGB888_1X7X4_SPWG;
430 
431 	return -EINVAL;
432 }
433 EXPORT_SYMBOL_GPL(drm_of_lvds_get_data_mapping);
434 
435 /**
436  * drm_of_get_data_lanes_count - Get DSI/(e)DP data lane count
437  * @endpoint: DT endpoint node of the DSI/(e)DP source or sink
438  * @min: minimum supported number of data lanes
439  * @max: maximum supported number of data lanes
440  *
441  * Count DT "data-lanes" property elements and check for validity.
442  *
443  * Return:
444  * * min..max - positive integer count of "data-lanes" elements
445  * * -ve - the "data-lanes" property is missing or invalid
446  * * -EINVAL - the "data-lanes" property is unsupported
447  */
448 int drm_of_get_data_lanes_count(const struct device_node *endpoint,
449 				const unsigned int min, const unsigned int max)
450 {
451 	int ret;
452 
453 	ret = of_property_count_u32_elems(endpoint, "data-lanes");
454 	if (ret < 0)
455 		return ret;
456 
457 	if (ret < min || ret > max)
458 		return -EINVAL;
459 
460 	return ret;
461 }
462 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count);
463 
464 /**
465  * drm_of_get_data_lanes_count_ep - Get DSI/(e)DP data lane count by endpoint
466  * @port: DT port node of the DSI/(e)DP source or sink
467  * @port_reg: identifier (value of reg property) of the parent port node
468  * @reg: identifier (value of reg property) of the endpoint node
469  * @min: minimum supported number of data lanes
470  * @max: maximum supported number of data lanes
471  *
472  * Count DT "data-lanes" property elements and check for validity.
473  * This variant uses endpoint specifier.
474  *
475  * Return:
476  * * min..max - positive integer count of "data-lanes" elements
477  * * -EINVAL - the "data-mapping" property is unsupported
478  * * -ENODEV - the "data-mapping" property is missing
479  */
480 int drm_of_get_data_lanes_count_ep(const struct device_node *port,
481 				   int port_reg, int reg,
482 				   const unsigned int min,
483 				   const unsigned int max)
484 {
485 	struct device_node *endpoint;
486 	int ret;
487 
488 	endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg);
489 	ret = drm_of_get_data_lanes_count(endpoint, min, max);
490 	of_node_put(endpoint);
491 
492 	return ret;
493 }
494 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_ep);
495