xref: /linux/drivers/gpu/drm/drm_of.c (revision 6bfca938471b1804c4a63774ca2fd30cb3596215)
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 /**
268  * drm_of_find_panel_or_bridge - return connected panel or bridge device
269  * @np: device tree node containing encoder output ports
270  * @port: port in the device tree node
271  * @endpoint: endpoint in the device tree node
272  * @panel: pointer to hold returned drm_panel, must not be NULL
273  * @bridge: pointer to hold returned drm_bridge
274  *
275  * Given a DT node's port and endpoint number, find the connected node and
276  * return either the associated struct drm_panel or drm_bridge device.
277  *
278  * This function is deprecated and should not be used in new drivers. Use
279  * of_drm_get_bridge_by_endpoint() instead when not looking for a panel, or
280  * devm_drm_of_get_bridge() otherwise.
281  *
282  * Returns zero if successful, or one of the standard error codes if it fails.
283  */
284 int drm_of_find_panel_or_bridge(const struct device_node *np,
285 				int port, int endpoint,
286 				struct drm_panel **panel,
287 				struct drm_bridge **bridge)
288 {
289 	int ret = -EPROBE_DEFER;
290 	struct device_node *remote;
291 
292 	if (WARN_ON(!panel))
293 		return -EINVAL;
294 
295 	*panel = NULL;
296 
297 	/*
298 	 * of_graph_get_remote_node() produces a noisy error message if port
299 	 * node isn't found and the absence of the port is a legit case here,
300 	 * so at first we silently check whether graph presents in the
301 	 * device-tree node.
302 	 */
303 	if (!of_graph_is_present(np))
304 		return -ENODEV;
305 
306 	remote = of_graph_get_remote_node(np, port, endpoint);
307 	if (!remote)
308 		return -ENODEV;
309 
310 	*panel = of_drm_find_panel(remote);
311 	if (!IS_ERR(*panel))
312 		ret = 0;
313 	else
314 		*panel = NULL;
315 
316 	if (bridge) {
317 		if (ret) {
318 			/* No panel found yet, check for a bridge next. */
319 			*bridge = of_drm_find_bridge(remote);
320 			if (*bridge)
321 				ret = 0;
322 		} else {
323 			*bridge = NULL;
324 		}
325 
326 	}
327 
328 	of_node_put(remote);
329 	return ret;
330 }
331 EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);
332 
333 enum drm_of_lvds_pixels {
334 	DRM_OF_LVDS_EVEN = BIT(0),
335 	DRM_OF_LVDS_ODD = BIT(1),
336 };
337 
338 static int drm_of_lvds_get_port_pixels_type(struct device_node *port_node)
339 {
340 	bool even_pixels =
341 		of_property_read_bool(port_node, "dual-lvds-even-pixels");
342 	bool odd_pixels =
343 		of_property_read_bool(port_node, "dual-lvds-odd-pixels");
344 
345 	return (even_pixels ? DRM_OF_LVDS_EVEN : 0) |
346 	       (odd_pixels ? DRM_OF_LVDS_ODD : 0);
347 }
348 
349 static int drm_of_lvds_get_remote_pixels_type(
350 			const struct device_node *port_node)
351 {
352 	struct device_node *endpoint = NULL;
353 	int pixels_type = -EPIPE;
354 
355 	for_each_child_of_node(port_node, endpoint) {
356 		struct device_node *remote_port;
357 		int current_pt;
358 
359 		if (!of_node_name_eq(endpoint, "endpoint"))
360 			continue;
361 
362 		remote_port = of_graph_get_remote_port(endpoint);
363 		if (!remote_port) {
364 			of_node_put(endpoint);
365 			return -EPIPE;
366 		}
367 
368 		current_pt = drm_of_lvds_get_port_pixels_type(remote_port);
369 		of_node_put(remote_port);
370 		if (pixels_type < 0)
371 			pixels_type = current_pt;
372 
373 		/*
374 		 * Sanity check, ensure that all remote endpoints have the same
375 		 * pixel type. We may lift this restriction later if we need to
376 		 * support multiple sinks with different dual-link
377 		 * configurations by passing the endpoints explicitly to
378 		 * drm_of_lvds_get_dual_link_pixel_order().
379 		 */
380 		if (!current_pt || pixels_type != current_pt) {
381 			of_node_put(endpoint);
382 			return -EINVAL;
383 		}
384 	}
385 
386 	return pixels_type;
387 }
388 
389 static int __drm_of_lvds_get_dual_link_pixel_order(int p1_pt, int p2_pt)
390 {
391 	/*
392 	 * A valid dual-lVDS bus is found when one port is marked with
393 	 * "dual-lvds-even-pixels", and the other port is marked with
394 	 * "dual-lvds-odd-pixels", bail out if the markers are not right.
395 	 */
396 	if (p1_pt + p2_pt != DRM_OF_LVDS_EVEN + DRM_OF_LVDS_ODD)
397 		return -EINVAL;
398 
399 	return p1_pt == DRM_OF_LVDS_EVEN ?
400 		DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS :
401 		DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS;
402 }
403 
404 /**
405  * drm_of_lvds_get_dual_link_pixel_order - Get LVDS dual-link source pixel order
406  * @port1: First DT port node of the Dual-link LVDS source
407  * @port2: Second DT port node of the Dual-link LVDS source
408  *
409  * An LVDS dual-link connection is made of two links, with even pixels
410  * transitting on one link, and odd pixels on the other link. This function
411  * returns, for two ports of an LVDS dual-link source, which port shall transmit
412  * the even and odd pixels, based on the requirements of the connected sink.
413  *
414  * The pixel order is determined from the dual-lvds-even-pixels and
415  * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
416  * properties are not present, or if their usage is not valid, this function
417  * returns -EINVAL.
418  *
419  * If either port is not connected, this function returns -EPIPE.
420  *
421  * @port1 and @port2 are typically DT sibling nodes, but may have different
422  * parents when, for instance, two separate LVDS encoders carry the even and odd
423  * pixels.
424  *
425  * Return:
426  * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 carries even pixels and @port2
427  *   carries odd pixels
428  * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 carries odd pixels and @port2
429  *   carries even pixels
430  * * -EINVAL - @port1 and @port2 are not connected to a dual-link LVDS sink, or
431  *   the sink configuration is invalid
432  * * -EPIPE - when @port1 or @port2 are not connected
433  */
434 int drm_of_lvds_get_dual_link_pixel_order(const struct device_node *port1,
435 					  const struct device_node *port2)
436 {
437 	int remote_p1_pt, remote_p2_pt;
438 
439 	if (!port1 || !port2)
440 		return -EINVAL;
441 
442 	remote_p1_pt = drm_of_lvds_get_remote_pixels_type(port1);
443 	if (remote_p1_pt < 0)
444 		return remote_p1_pt;
445 
446 	remote_p2_pt = drm_of_lvds_get_remote_pixels_type(port2);
447 	if (remote_p2_pt < 0)
448 		return remote_p2_pt;
449 
450 	return __drm_of_lvds_get_dual_link_pixel_order(remote_p1_pt, remote_p2_pt);
451 }
452 EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order);
453 
454 /**
455  * drm_of_lvds_get_dual_link_pixel_order_sink - Get LVDS dual-link sink pixel order
456  * @port1: First DT port node of the Dual-link LVDS sink
457  * @port2: Second DT port node of the Dual-link LVDS sink
458  *
459  * An LVDS dual-link connection is made of two links, with even pixels
460  * transitting on one link, and odd pixels on the other link. This function
461  * returns, for two ports of an LVDS dual-link sink, which port shall transmit
462  * the even and odd pixels, based on the requirements of the sink.
463  *
464  * The pixel order is determined from the dual-lvds-even-pixels and
465  * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
466  * properties are not present, or if their usage is not valid, this function
467  * returns -EINVAL.
468  *
469  * If either port is not connected, this function returns -EPIPE.
470  *
471  * @port1 and @port2 are typically DT sibling nodes, but may have different
472  * parents when, for instance, two separate LVDS decoders receive the even and
473  * odd pixels.
474  *
475  * Return:
476  * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 receives even pixels and @port2
477  *   receives odd pixels
478  * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 receives odd pixels and @port2
479  *   receives even pixels
480  * * -EINVAL - @port1 or @port2 are NULL
481  * * -EPIPE - when @port1 or @port2 are not connected
482  */
483 int drm_of_lvds_get_dual_link_pixel_order_sink(struct device_node *port1,
484 					       struct device_node *port2)
485 {
486 	int sink_p1_pt, sink_p2_pt;
487 
488 	if (!port1 || !port2)
489 		return -EINVAL;
490 
491 	sink_p1_pt = drm_of_lvds_get_port_pixels_type(port1);
492 	if (!sink_p1_pt)
493 		return -EPIPE;
494 
495 	sink_p2_pt = drm_of_lvds_get_port_pixels_type(port2);
496 	if (!sink_p2_pt)
497 		return -EPIPE;
498 
499 	return __drm_of_lvds_get_dual_link_pixel_order(sink_p1_pt, sink_p2_pt);
500 }
501 EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order_sink);
502 
503 /**
504  * drm_of_lvds_get_data_mapping - Get LVDS data mapping
505  * @port: DT port node of the LVDS source or sink
506  *
507  * Convert DT "data-mapping" property string value into media bus format value.
508  *
509  * Return:
510  * * MEDIA_BUS_FMT_RGB666_1X7X3_SPWG - data-mapping is "jeida-18"
511  * * MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA - data-mapping is "jeida-24"
512  * * MEDIA_BUS_FMT_RGB101010_1X7X5_JEIDA - data-mapping is "jeida-30"
513  * * MEDIA_BUS_FMT_RGB888_1X7X4_SPWG - data-mapping is "vesa-24"
514  * * MEDIA_BUS_FMT_RGB101010_1X7X5_SPWG - data-mapping is "vesa-30"
515  * * -EINVAL - the "data-mapping" property is unsupported
516  * * -ENODEV - the "data-mapping" property is missing
517  */
518 int drm_of_lvds_get_data_mapping(const struct device_node *port)
519 {
520 	const char *mapping;
521 	int ret;
522 
523 	ret = of_property_read_string(port, "data-mapping", &mapping);
524 	if (ret < 0)
525 		return -ENODEV;
526 
527 	if (!strcmp(mapping, "jeida-18"))
528 		return MEDIA_BUS_FMT_RGB666_1X7X3_SPWG;
529 	if (!strcmp(mapping, "jeida-24"))
530 		return MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA;
531 	if (!strcmp(mapping, "jeida-30"))
532 		return MEDIA_BUS_FMT_RGB101010_1X7X5_JEIDA;
533 	if (!strcmp(mapping, "vesa-24"))
534 		return MEDIA_BUS_FMT_RGB888_1X7X4_SPWG;
535 	if (!strcmp(mapping, "vesa-30"))
536 		return MEDIA_BUS_FMT_RGB101010_1X7X5_SPWG;
537 
538 	return -EINVAL;
539 }
540 EXPORT_SYMBOL_GPL(drm_of_lvds_get_data_mapping);
541 
542 /**
543  * drm_of_get_data_lanes_count - Get DSI/(e)DP data lane count
544  * @endpoint: DT endpoint node of the DSI/(e)DP source or sink
545  * @min: minimum supported number of data lanes
546  * @max: maximum supported number of data lanes
547  *
548  * Count DT "data-lanes" property elements and check for validity.
549  *
550  * Return:
551  * * min..max - positive integer count of "data-lanes" elements
552  * * -ve - the "data-lanes" property is missing or invalid
553  * * -EINVAL - the "data-lanes" property is unsupported
554  */
555 int drm_of_get_data_lanes_count(const struct device_node *endpoint,
556 				const unsigned int min, const unsigned int max)
557 {
558 	int ret;
559 
560 	ret = of_property_count_u32_elems(endpoint, "data-lanes");
561 	if (ret < 0)
562 		return ret;
563 
564 	if (ret < min || ret > max)
565 		return -EINVAL;
566 
567 	return ret;
568 }
569 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count);
570 
571 /**
572  * drm_of_get_data_lanes_count_ep - Get DSI/(e)DP data lane count by endpoint
573  * @port: DT port node of the DSI/(e)DP source or sink
574  * @port_reg: identifier (value of reg property) of the parent port node
575  * @reg: identifier (value of reg property) of the endpoint node
576  * @min: minimum supported number of data lanes
577  * @max: maximum supported number of data lanes
578  *
579  * Count DT "data-lanes" property elements and check for validity.
580  * This variant uses endpoint specifier.
581  *
582  * Return:
583  * * min..max - positive integer count of "data-lanes" elements
584  * * -EINVAL - the "data-mapping" property is unsupported
585  * * -ENODEV - the "data-mapping" property is missing
586  */
587 int drm_of_get_data_lanes_count_ep(const struct device_node *port,
588 				   int port_reg, int reg,
589 				   const unsigned int min,
590 				   const unsigned int max)
591 {
592 	struct device_node *endpoint;
593 	int ret;
594 
595 	endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg);
596 	ret = drm_of_get_data_lanes_count(endpoint, min, max);
597 	of_node_put(endpoint);
598 
599 	return ret;
600 }
601 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_ep);
602 
603 /**
604  * drm_of_get_data_lanes_count_remote - Get DSI/(e)DP data lane count by endpoint
605  * @port: DT port node of the DSI/(e)DP source or sink
606  * @port_reg: identifier (value of reg property) of the parent port node
607  * @reg: identifier (value of reg property) of the endpoint node
608  * @min: minimum supported number of data lanes
609  * @max: maximum supported number of data lanes
610  *
611  * Count DT "data-lanes" property elements in the remote endpoint and check for
612  * validity.  This variant uses endpoint specifier.
613  *
614  * Return:
615  * * min..max - positive integer count of "data-lanes" elements
616  * * -EINVAL - the "data-lanes" property is unsupported
617  * * -ENODEV - the "data-lanes" property is missing
618  */
619 int drm_of_get_data_lanes_count_remote(const struct device_node *port,
620 				       int port_reg, int reg,
621 				       const unsigned int min,
622 				       const unsigned int max)
623 {
624 	struct device_node *endpoint, *remote;
625 	int ret;
626 
627 	endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg);
628 	remote = of_graph_get_remote_endpoint(endpoint);
629 	of_node_put(endpoint);
630 	ret = drm_of_get_data_lanes_count(remote, min, max);
631 	of_node_put(remote);
632 
633 	return ret;
634 }
635 EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_remote);
636 
637 #if IS_ENABLED(CONFIG_DRM_MIPI_DSI)
638 
639 /**
640  * drm_of_get_dsi_bus - find the DSI bus for a given device
641  * @dev: parent device of display (SPI, I2C)
642  *
643  * Gets parent DSI bus for a DSI device controlled through a bus other
644  * than MIPI-DCS (SPI, I2C, etc.) using the Device Tree.
645  *
646  * This function assumes that the device's port@0 is the DSI input.
647  *
648  * Returns pointer to mipi_dsi_host if successful, -EINVAL if the
649  * request is unsupported, -EPROBE_DEFER if the DSI host is found but
650  * not available, or -ENODEV otherwise.
651  */
652 struct mipi_dsi_host *drm_of_get_dsi_bus(struct device *dev)
653 {
654 	struct mipi_dsi_host *dsi_host;
655 	struct device_node *endpoint, *dsi_host_node;
656 
657 	/*
658 	 * Get first endpoint child from device.
659 	 */
660 	endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
661 	if (!endpoint)
662 		return ERR_PTR(-ENODEV);
663 
664 	/*
665 	 * Follow the first endpoint to get the DSI host node and then
666 	 * release the endpoint since we no longer need it.
667 	 */
668 	dsi_host_node = of_graph_get_remote_port_parent(endpoint);
669 	of_node_put(endpoint);
670 	if (!dsi_host_node)
671 		return ERR_PTR(-ENODEV);
672 
673 	/*
674 	 * Get the DSI host from the DSI host node. If we get an error
675 	 * or the return is null assume we're not ready to probe just
676 	 * yet. Release the DSI host node since we're done with it.
677 	 */
678 	dsi_host = of_find_mipi_dsi_host_by_node(dsi_host_node);
679 	of_node_put(dsi_host_node);
680 	if (IS_ERR_OR_NULL(dsi_host))
681 		return ERR_PTR(-EPROBE_DEFER);
682 
683 	return dsi_host;
684 }
685 EXPORT_SYMBOL_GPL(drm_of_get_dsi_bus);
686 
687 #endif /* CONFIG_DRM_MIPI_DSI */
688