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