xref: /linux/drivers/gpu/drm/drm_of.c (revision 5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f)
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/media-bus-format.h>
6 #include <linux/of.h>
7 #include <linux/of_graph.h>
8 
9 #include <drm/drm_bridge.h>
10 #include <drm/drm_connector.h>
11 #include <drm/drm_crtc.h>
12 #include <drm/drm_device.h>
13 #include <drm/drm_encoder.h>
14 #include <drm/drm_mipi_dsi.h>
15 #include <drm/drm_of.h>
16 #include <drm/drm_panel.h>
17 
18 /**
19  * DOC: overview
20  *
21  * A set of helper functions to aid DRM drivers in parsing standard DT
22  * properties.
23  */
24 
25 /**
26  * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node
27  * @dev: DRM device
28  * @port: port OF node
29  *
30  * Given a port OF node, return the possible mask of the corresponding
31  * CRTC within a device's list of CRTCs.  Returns zero if not found.
32  */
33 uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
34 			    struct device_node *port)
35 {
36 	unsigned int index = 0;
37 	struct drm_crtc *tmp;
38 
39 	drm_for_each_crtc(tmp, dev) {
40 		if (tmp->port == port)
41 			return 1 << index;
42 
43 		index++;
44 	}
45 
46 	return 0;
47 }
48 EXPORT_SYMBOL(drm_of_crtc_port_mask);
49 
50 /**
51  * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
52  * @dev: DRM device
53  * @port: encoder port to scan for endpoints
54  *
55  * Scan all endpoints attached to a port, locate their attached CRTCs,
56  * and generate the DRM mask of CRTCs which may be attached to this
57  * encoder.
58  *
59  * See https://github.com/devicetree-org/dt-schema/blob/main/dtschema/schemas/graph.yaml
60  * for the bindings.
61  */
62 uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
63 				    struct device_node *port)
64 {
65 	struct device_node *remote_port, *ep;
66 	uint32_t possible_crtcs = 0;
67 
68 	for_each_endpoint_of_node(port, ep) {
69 		remote_port = of_graph_get_remote_port(ep);
70 		if (!remote_port) {
71 			of_node_put(ep);
72 			return 0;
73 		}
74 
75 		possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
76 
77 		of_node_put(remote_port);
78 	}
79 
80 	return possible_crtcs;
81 }
82 EXPORT_SYMBOL(drm_of_find_possible_crtcs);
83 
84 /**
85  * drm_of_component_match_add - Add a component helper OF node match rule
86  * @master: master device
87  * @matchptr: component match pointer
88  * @compare: compare function used for matching component
89  * @node: of_node
90  */
91 void drm_of_component_match_add(struct device *master,
92 				struct component_match **matchptr,
93 				int (*compare)(struct device *, void *),
94 				struct device_node *node)
95 {
96 	of_node_get(node);
97 	component_match_add_release(master, matchptr, component_release_of,
98 				    compare, node);
99 }
100 EXPORT_SYMBOL_GPL(drm_of_component_match_add);
101 
102 /**
103  * drm_of_component_probe - Generic probe function for a component based master
104  * @dev: master device containing the OF node
105  * @compare_of: compare function used for matching components
106  * @m_ops: component master ops to be used
107  *
108  * Parse the platform device OF node and bind all the components associated
109  * with the master. Interface ports are added before the encoders in order to
110  * satisfy their .bind requirements
111  *
112  * See https://github.com/devicetree-org/dt-schema/blob/main/dtschema/schemas/graph.yaml
113  * for the bindings.
114  *
115  * Returns zero if successful, or one of the standard error codes if it fails.
116  */
117 int drm_of_component_probe(struct device *dev,
118 			   int (*compare_of)(struct device *, void *),
119 			   const struct component_master_ops *m_ops)
120 {
121 	struct device_node *ep, *port, *remote;
122 	struct component_match *match = NULL;
123 	int i;
124 
125 	if (!dev->of_node)
126 		return -EINVAL;
127 
128 	/*
129 	 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
130 	 * called from encoder's .bind callbacks works as expected
131 	 */
132 	for (i = 0; ; i++) {
133 		port = of_parse_phandle(dev->of_node, "ports", i);
134 		if (!port)
135 			break;
136 
137 		if (of_device_is_available(port->parent))
138 			drm_of_component_match_add(dev, &match, compare_of,
139 						   port);
140 
141 		of_node_put(port);
142 	}
143 
144 	if (i == 0) {
145 		dev_err(dev, "missing 'ports' property\n");
146 		return -ENODEV;
147 	}
148 
149 	if (!match) {
150 		dev_err(dev, "no available port\n");
151 		return -ENODEV;
152 	}
153 
154 	/*
155 	 * For bound crtcs, bind the encoders attached to their remote endpoint
156 	 */
157 	for (i = 0; ; i++) {
158 		port = of_parse_phandle(dev->of_node, "ports", i);
159 		if (!port)
160 			break;
161 
162 		if (!of_device_is_available(port->parent)) {
163 			of_node_put(port);
164 			continue;
165 		}
166 
167 		for_each_child_of_node(port, ep) {
168 			remote = of_graph_get_remote_port_parent(ep);
169 			if (!remote || !of_device_is_available(remote)) {
170 				of_node_put(remote);
171 				continue;
172 			} else if (!of_device_is_available(remote->parent)) {
173 				dev_warn(dev, "parent device of %pOF is not available\n",
174 					 remote);
175 				of_node_put(remote);
176 				continue;
177 			}
178 
179 			drm_of_component_match_add(dev, &match, compare_of,
180 						   remote);
181 			of_node_put(remote);
182 		}
183 		of_node_put(port);
184 	}
185 
186 	return component_master_add_with_match(dev, m_ops, match);
187 }
188 EXPORT_SYMBOL(drm_of_component_probe);
189 
190 /*
191  * drm_of_encoder_active_endpoint - return the active encoder endpoint
192  * @node: device tree node containing encoder input ports
193  * @encoder: drm_encoder
194  *
195  * Given an encoder device node and a drm_encoder with a connected crtc,
196  * parse the encoder endpoint connecting to the crtc port.
197  */
198 int drm_of_encoder_active_endpoint(struct device_node *node,
199 				   struct drm_encoder *encoder,
200 				   struct of_endpoint *endpoint)
201 {
202 	struct device_node *ep;
203 	struct drm_crtc *crtc = encoder->crtc;
204 	struct device_node *port;
205 	int ret;
206 
207 	if (!node || !crtc)
208 		return -EINVAL;
209 
210 	for_each_endpoint_of_node(node, ep) {
211 		port = of_graph_get_remote_port(ep);
212 		of_node_put(port);
213 		if (port == crtc->port) {
214 			ret = of_graph_parse_endpoint(ep, endpoint);
215 			of_node_put(ep);
216 			return ret;
217 		}
218 	}
219 
220 	return -EINVAL;
221 }
222 EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
223 
224 /**
225  * drm_of_get_panel_orientation - look up the orientation of the panel through
226  * the "rotation" binding from a device tree node
227  * @np: device tree node of the panel
228  * @orientation: orientation enum to be filled in
229  *
230  * Looks up the rotation of a panel in the device tree. The orientation of the
231  * panel is expressed as a property name "rotation" in the device tree. The
232  * rotation in the device tree is counter clockwise.
233  *
234  * Return: 0 when a valid rotation value (0, 90, 180, or 270) is read or the
235  * rotation property doesn't exist. Return a negative error code on failure.
236  */
237 int drm_of_get_panel_orientation(const struct device_node *np,
238 				 enum drm_panel_orientation *orientation)
239 {
240 	int rotation, ret;
241 
242 	ret = of_property_read_u32(np, "rotation", &rotation);
243 	if (ret == -EINVAL) {
244 		/* Don't return an error if there's no rotation property. */
245 		*orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
246 		return 0;
247 	}
248 
249 	if (ret < 0)
250 		return ret;
251 
252 	if (rotation == 0)
253 		*orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
254 	else if (rotation == 90)
255 		*orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
256 	else if (rotation == 180)
257 		*orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
258 	else if (rotation == 270)
259 		*orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
260 	else
261 		return -EINVAL;
262 
263 	return 0;
264 }
265 EXPORT_SYMBOL_GPL(drm_of_get_panel_orientation);
266 
267 enum drm_of_lvds_pixels {
268 	DRM_OF_LVDS_EVEN = BIT(0),
269 	DRM_OF_LVDS_ODD = BIT(1),
270 };
271 
272 static int drm_of_lvds_get_port_pixels_type(struct device_node *port_node)
273 {
274 	bool even_pixels =
275 		of_property_read_bool(port_node, "dual-lvds-even-pixels");
276 	bool odd_pixels =
277 		of_property_read_bool(port_node, "dual-lvds-odd-pixels");
278 
279 	return (even_pixels ? DRM_OF_LVDS_EVEN : 0) |
280 	       (odd_pixels ? DRM_OF_LVDS_ODD : 0);
281 }
282 
283 static int drm_of_lvds_get_remote_pixels_type(
284 			const struct device_node *port_node)
285 {
286 	struct device_node *endpoint = NULL;
287 	int pixels_type = -EPIPE;
288 
289 	for_each_child_of_node(port_node, endpoint) {
290 		struct device_node *remote_port;
291 		int current_pt;
292 
293 		if (!of_node_name_eq(endpoint, "endpoint"))
294 			continue;
295 
296 		remote_port = of_graph_get_remote_port(endpoint);
297 		if (!remote_port) {
298 			of_node_put(endpoint);
299 			return -EPIPE;
300 		}
301 
302 		current_pt = drm_of_lvds_get_port_pixels_type(remote_port);
303 		of_node_put(remote_port);
304 		if (pixels_type < 0)
305 			pixels_type = current_pt;
306 
307 		/*
308 		 * Sanity check, ensure that all remote endpoints have the same
309 		 * pixel type. We may lift this restriction later if we need to
310 		 * support multiple sinks with different dual-link
311 		 * configurations by passing the endpoints explicitly to
312 		 * drm_of_lvds_get_dual_link_pixel_order().
313 		 */
314 		if (!current_pt || pixels_type != current_pt) {
315 			of_node_put(endpoint);
316 			return -EINVAL;
317 		}
318 	}
319 
320 	return pixels_type;
321 }
322 
323 static int __drm_of_lvds_get_dual_link_pixel_order(int p1_pt, int p2_pt)
324 {
325 	/*
326 	 * A valid dual-lVDS bus is found when one port is marked with
327 	 * "dual-lvds-even-pixels", and the other port is marked with
328 	 * "dual-lvds-odd-pixels", bail out if the markers are not right.
329 	 */
330 	if (p1_pt + p2_pt != DRM_OF_LVDS_EVEN + DRM_OF_LVDS_ODD)
331 		return -EINVAL;
332 
333 	return p1_pt == DRM_OF_LVDS_EVEN ?
334 		DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS :
335 		DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS;
336 }
337 
338 /**
339  * drm_of_lvds_get_dual_link_pixel_order - Get LVDS dual-link source pixel order
340  * @port1: First DT port node of the Dual-link LVDS source
341  * @port2: Second DT port node of the Dual-link LVDS source
342  *
343  * An LVDS dual-link connection is made of two links, with even pixels
344  * transitting on one link, and odd pixels on the other link. This function
345  * returns, for two ports of an LVDS dual-link source, which port shall transmit
346  * the even and odd pixels, based on the requirements of the connected sink.
347  *
348  * The pixel order is determined from the dual-lvds-even-pixels and
349  * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
350  * properties are not present, or if their usage is not valid, this function
351  * returns -EINVAL.
352  *
353  * If either port is not connected, this function returns -EPIPE.
354  *
355  * @port1 and @port2 are typically DT sibling nodes, but may have different
356  * parents when, for instance, two separate LVDS encoders carry the even and odd
357  * pixels.
358  *
359  * Return:
360  * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 carries even pixels and @port2
361  *   carries odd pixels
362  * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 carries odd pixels and @port2
363  *   carries even pixels
364  * * -EINVAL - @port1 and @port2 are not connected to a dual-link LVDS sink, or
365  *   the sink configuration is invalid
366  * * -EPIPE - when @port1 or @port2 are not connected
367  */
368 int drm_of_lvds_get_dual_link_pixel_order(const struct device_node *port1,
369 					  const struct device_node *port2)
370 {
371 	int remote_p1_pt, remote_p2_pt;
372 
373 	if (!port1 || !port2)
374 		return -EINVAL;
375 
376 	remote_p1_pt = drm_of_lvds_get_remote_pixels_type(port1);
377 	if (remote_p1_pt < 0)
378 		return remote_p1_pt;
379 
380 	remote_p2_pt = drm_of_lvds_get_remote_pixels_type(port2);
381 	if (remote_p2_pt < 0)
382 		return remote_p2_pt;
383 
384 	return __drm_of_lvds_get_dual_link_pixel_order(remote_p1_pt, remote_p2_pt);
385 }
386 EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order);
387 
388 /**
389  * drm_of_lvds_get_dual_link_pixel_order_sink - Get LVDS dual-link sink pixel order
390  * @port1: First DT port node of the Dual-link LVDS sink
391  * @port2: Second DT port node of the Dual-link LVDS sink
392  *
393  * An LVDS dual-link connection is made of two links, with even pixels
394  * transitting on one link, and odd pixels on the other link. This function
395  * returns, for two ports of an LVDS dual-link sink, which port shall transmit
396  * the even and odd pixels, based on the requirements of the sink.
397  *
398  * The pixel order is determined from the dual-lvds-even-pixels and
399  * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
400  * properties are not present, or if their usage is not valid, this function
401  * returns -EINVAL.
402  *
403  * If either port is not connected, this function returns -EPIPE.
404  *
405  * @port1 and @port2 are typically DT sibling nodes, but may have different
406  * parents when, for instance, two separate LVDS decoders receive the even and
407  * odd pixels.
408  *
409  * Return:
410  * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 receives even pixels and @port2
411  *   receives odd pixels
412  * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 receives odd pixels and @port2
413  *   receives even pixels
414  * * -EINVAL - @port1 or @port2 are NULL
415  * * -EPIPE - when @port1 or @port2 are not connected
416  */
417 int drm_of_lvds_get_dual_link_pixel_order_sink(struct device_node *port1,
418 					       struct device_node *port2)
419 {
420 	int sink_p1_pt, sink_p2_pt;
421 
422 	if (!port1 || !port2)
423 		return -EINVAL;
424 
425 	sink_p1_pt = drm_of_lvds_get_port_pixels_type(port1);
426 	if (!sink_p1_pt)
427 		return -EPIPE;
428 
429 	sink_p2_pt = drm_of_lvds_get_port_pixels_type(port2);
430 	if (!sink_p2_pt)
431 		return -EPIPE;
432 
433 	return __drm_of_lvds_get_dual_link_pixel_order(sink_p1_pt, sink_p2_pt);
434 }
435 EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order_sink);
436 
437 /**
438  * drm_of_lvds_get_data_mapping - Get LVDS data mapping
439  * @port: DT port node of the LVDS source or sink
440  *
441  * Convert DT "data-mapping" property string value into media bus format value.
442  *
443  * Return:
444  * * MEDIA_BUS_FMT_RGB666_1X7X3_SPWG - data-mapping is "jeida-18"
445  * * MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA - data-mapping is "jeida-24"
446  * * MEDIA_BUS_FMT_RGB101010_1X7X5_JEIDA - data-mapping is "jeida-30"
447  * * MEDIA_BUS_FMT_RGB888_1X7X4_SPWG - data-mapping is "vesa-24"
448  * * MEDIA_BUS_FMT_RGB101010_1X7X5_SPWG - data-mapping is "vesa-30"
449  * * -EINVAL - the "data-mapping" property is unsupported
450  * * -ENODEV - the "data-mapping" property is missing
451  */
452 int drm_of_lvds_get_data_mapping(const struct device_node *port)
453 {
454 	const char *mapping;
455 	int ret;
456 
457 	ret = of_property_read_string(port, "data-mapping", &mapping);
458 	if (ret < 0)
459 		return -ENODEV;
460 
461 	if (!strcmp(mapping, "jeida-18"))
462 		return MEDIA_BUS_FMT_RGB666_1X7X3_SPWG;
463 	if (!strcmp(mapping, "jeida-24"))
464 		return MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA;
465 	if (!strcmp(mapping, "jeida-30"))
466 		return MEDIA_BUS_FMT_RGB101010_1X7X5_JEIDA;
467 	if (!strcmp(mapping, "vesa-24"))
468 		return MEDIA_BUS_FMT_RGB888_1X7X4_SPWG;
469 	if (!strcmp(mapping, "vesa-30"))
470 		return MEDIA_BUS_FMT_RGB101010_1X7X5_SPWG;
471 
472 	return -EINVAL;
473 }
474 EXPORT_SYMBOL_GPL(drm_of_lvds_get_data_mapping);
475 
476 /**
477  * drm_of_get_data_lanes_count - Get DSI/(e)DP data lane count
478  * @endpoint: DT endpoint node of the DSI/(e)DP source or sink
479  * @min: minimum supported number of data lanes
480  * @max: maximum supported number of data lanes
481  *
482  * Count DT "data-lanes" property elements and check for validity.
483  *
484  * Return:
485  * * min..max - positive integer count of "data-lanes" elements
486  * * -ENODATA - the property does not have a value.
487  * * -EINVAL - the "data-lanes" property is missing or invalid
488  */
489 int drm_of_get_data_lanes_count(const struct device_node *endpoint,
490 				const unsigned int min, const unsigned int max)
491 {
492 	int ret;
493 
494 	ret = of_property_count_u32_elems(endpoint, "data-lanes");
495 	if (ret < 0)
496 		return ret;
497 
498 	if (ret < min || ret > max)
499 		return -EINVAL;
500 
501 	return ret;
502 }
503 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count);
504 
505 /**
506  * drm_of_get_data_lanes_count_ep - Get DSI/(e)DP data lane count by endpoint
507  * @port: DT port node of the DSI/(e)DP source or sink
508  * @port_reg: identifier (value of reg property) of the parent port node
509  * @reg: identifier (value of reg property) of the endpoint node
510  * @min: minimum supported number of data lanes
511  * @max: maximum supported number of data lanes
512  *
513  * Count DT "data-lanes" property elements and check for validity.
514  * This variant uses endpoint specifier.
515  *
516  * Return:
517  * * min..max - positive integer count of "data-lanes" elements
518  * * -ENODATA - the property does not have a value.
519  * * -EINVAL - the "data-lanes" property is missing or invalid
520  */
521 int drm_of_get_data_lanes_count_ep(const struct device_node *port,
522 				   int port_reg, int reg,
523 				   const unsigned int min,
524 				   const unsigned int max)
525 {
526 	struct device_node *endpoint;
527 	int ret;
528 
529 	endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg);
530 	ret = drm_of_get_data_lanes_count(endpoint, min, max);
531 	of_node_put(endpoint);
532 
533 	return ret;
534 }
535 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_ep);
536 
537 /**
538  * drm_of_get_data_lanes_count_remote - Get DSI/(e)DP data lane count by endpoint
539  * @port: DT port node of the DSI/(e)DP source or sink
540  * @port_reg: identifier (value of reg property) of the parent port node
541  * @reg: identifier (value of reg property) of the endpoint node
542  * @min: minimum supported number of data lanes
543  * @max: maximum supported number of data lanes
544  *
545  * Count DT "data-lanes" property elements in the remote endpoint and check for
546  * validity.  This variant uses endpoint specifier.
547  *
548  * Return:
549  * * min..max - positive integer count of "data-lanes" elements
550  * * -EINVAL - the "data-lanes" property is unsupported
551  * * -ENODEV - the "data-lanes" property is missing
552  */
553 int drm_of_get_data_lanes_count_remote(const struct device_node *port,
554 				       int port_reg, int reg,
555 				       const unsigned int min,
556 				       const unsigned int max)
557 {
558 	struct device_node *endpoint, *remote;
559 	int ret;
560 
561 	endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg);
562 	remote = of_graph_get_remote_endpoint(endpoint);
563 	of_node_put(endpoint);
564 	ret = drm_of_get_data_lanes_count(remote, min, max);
565 	of_node_put(remote);
566 
567 	return ret;
568 }
569 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_remote);
570 
571 #if IS_ENABLED(CONFIG_DRM_MIPI_DSI)
572 
573 /**
574  * drm_of_get_dsi_bus - find the DSI bus for a given device
575  * @dev: parent device of display (SPI, I2C)
576  *
577  * Gets parent DSI bus for a DSI device controlled through a bus other
578  * than MIPI-DCS (SPI, I2C, etc.) using the Device Tree.
579  *
580  * This function assumes that the device's port@0 is the DSI input.
581  *
582  * Returns pointer to mipi_dsi_host if successful, -EINVAL if the
583  * request is unsupported, -EPROBE_DEFER if the DSI host is found but
584  * not available, or -ENODEV otherwise.
585  */
586 struct mipi_dsi_host *drm_of_get_dsi_bus(struct device *dev)
587 {
588 	struct mipi_dsi_host *dsi_host;
589 	struct device_node *endpoint, *dsi_host_node;
590 
591 	/*
592 	 * Get first endpoint child from device.
593 	 */
594 	endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
595 	if (!endpoint)
596 		return ERR_PTR(-ENODEV);
597 
598 	/*
599 	 * Follow the first endpoint to get the DSI host node and then
600 	 * release the endpoint since we no longer need it.
601 	 */
602 	dsi_host_node = of_graph_get_remote_port_parent(endpoint);
603 	of_node_put(endpoint);
604 	if (!dsi_host_node)
605 		return ERR_PTR(-ENODEV);
606 
607 	/*
608 	 * Get the DSI host from the DSI host node. If we get an error
609 	 * or the return is null assume we're not ready to probe just
610 	 * yet. Release the DSI host node since we're done with it.
611 	 */
612 	dsi_host = of_find_mipi_dsi_host_by_node(dsi_host_node);
613 	of_node_put(dsi_host_node);
614 	if (IS_ERR_OR_NULL(dsi_host))
615 		return ERR_PTR(-EPROBE_DEFER);
616 
617 	return dsi_host;
618 }
619 EXPORT_SYMBOL_GPL(drm_of_get_dsi_bus);
620 
621 #endif /* CONFIG_DRM_MIPI_DSI */
622