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