xref: /openbmc/linux/drivers/gpu/drm/drm_of.c (revision 19ff997f)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2df785aa8SLiviu Dudau #include <linux/component.h>
37e435aadSRussell King #include <linux/export.h>
47e435aadSRussell King #include <linux/list.h>
572bd9ea3SVille Syrjälä #include <linux/media-bus-format.h>
673289afeSVille Syrjälä #include <linux/of.h>
77e435aadSRussell King #include <linux/of_graph.h>
80500c04eSSam Ravnborg 
91f2db303SRob Herring #include <drm/drm_bridge.h>
107e435aadSRussell King #include <drm/drm_crtc.h>
110500c04eSSam Ravnborg #include <drm/drm_device.h>
129338203cSLaurent Pinchart #include <drm/drm_encoder.h>
13*19ff997fSChris Morgan #include <drm/drm_mipi_dsi.h>
147e435aadSRussell King #include <drm/drm_of.h>
150500c04eSSam Ravnborg #include <drm/drm_panel.h>
167e435aadSRussell King 
177f9e7ec9SDaniel Vetter /**
187f9e7ec9SDaniel Vetter  * DOC: overview
197f9e7ec9SDaniel Vetter  *
207f9e7ec9SDaniel Vetter  * A set of helper functions to aid DRM drivers in parsing standard DT
217f9e7ec9SDaniel Vetter  * properties.
227f9e7ec9SDaniel Vetter  */
237f9e7ec9SDaniel Vetter 
247e435aadSRussell King /**
258b5f7a62SJernej Skrabec  * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node
267e435aadSRussell King  * @dev: DRM device
277e435aadSRussell King  * @port: port OF node
287e435aadSRussell King  *
297e435aadSRussell King  * Given a port OF node, return the possible mask of the corresponding
307e435aadSRussell King  * CRTC within a device's list of CRTCs.  Returns zero if not found.
317e435aadSRussell King  */
drm_of_crtc_port_mask(struct drm_device * dev,struct device_node * port)328b5f7a62SJernej Skrabec uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
337e435aadSRussell King 			    struct device_node *port)
347e435aadSRussell King {
357e435aadSRussell King 	unsigned int index = 0;
367e435aadSRussell King 	struct drm_crtc *tmp;
377e435aadSRussell King 
386295d607SDaniel Vetter 	drm_for_each_crtc(tmp, dev) {
397e435aadSRussell King 		if (tmp->port == port)
407e435aadSRussell King 			return 1 << index;
417e435aadSRussell King 
427e435aadSRussell King 		index++;
437e435aadSRussell King 	}
447e435aadSRussell King 
457e435aadSRussell King 	return 0;
467e435aadSRussell King }
478b5f7a62SJernej Skrabec EXPORT_SYMBOL(drm_of_crtc_port_mask);
487e435aadSRussell King 
497e435aadSRussell King /**
507e435aadSRussell King  * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
517e435aadSRussell King  * @dev: DRM device
527e435aadSRussell King  * @port: encoder port to scan for endpoints
537e435aadSRussell King  *
547e435aadSRussell King  * Scan all endpoints attached to a port, locate their attached CRTCs,
557e435aadSRussell King  * and generate the DRM mask of CRTCs which may be attached to this
567e435aadSRussell King  * encoder.
577e435aadSRussell King  *
587e435aadSRussell King  * See Documentation/devicetree/bindings/graph.txt for the bindings.
597e435aadSRussell King  */
drm_of_find_possible_crtcs(struct drm_device * dev,struct device_node * port)607e435aadSRussell King uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
617e435aadSRussell King 				    struct device_node *port)
627e435aadSRussell King {
637416f4e3SPhilipp Zabel 	struct device_node *remote_port, *ep;
647e435aadSRussell King 	uint32_t possible_crtcs = 0;
657e435aadSRussell King 
667416f4e3SPhilipp Zabel 	for_each_endpoint_of_node(port, ep) {
677e435aadSRussell King 		remote_port = of_graph_get_remote_port(ep);
687e435aadSRussell King 		if (!remote_port) {
697e435aadSRussell King 			of_node_put(ep);
707e435aadSRussell King 			return 0;
717e435aadSRussell King 		}
727e435aadSRussell King 
738b5f7a62SJernej Skrabec 		possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
747e435aadSRussell King 
757e435aadSRussell King 		of_node_put(remote_port);
767416f4e3SPhilipp Zabel 	}
777e435aadSRussell King 
787e435aadSRussell King 	return possible_crtcs;
797e435aadSRussell King }
807e435aadSRussell King EXPORT_SYMBOL(drm_of_find_possible_crtcs);
81df785aa8SLiviu Dudau 
82df785aa8SLiviu Dudau /**
8397ac0e47SRussell King  * drm_of_component_match_add - Add a component helper OF node match rule
8497ac0e47SRussell King  * @master: master device
8597ac0e47SRussell King  * @matchptr: component match pointer
8697ac0e47SRussell King  * @compare: compare function used for matching component
8797ac0e47SRussell King  * @node: of_node
8897ac0e47SRussell King  */
drm_of_component_match_add(struct device * master,struct component_match ** matchptr,int (* compare)(struct device *,void *),struct device_node * node)8997ac0e47SRussell King void drm_of_component_match_add(struct device *master,
9097ac0e47SRussell King 				struct component_match **matchptr,
9197ac0e47SRussell King 				int (*compare)(struct device *, void *),
9297ac0e47SRussell King 				struct device_node *node)
9397ac0e47SRussell King {
9497ac0e47SRussell King 	of_node_get(node);
95ab011ab6SYong Wu 	component_match_add_release(master, matchptr, component_release_of,
9697ac0e47SRussell King 				    compare, node);
9797ac0e47SRussell King }
9897ac0e47SRussell King EXPORT_SYMBOL_GPL(drm_of_component_match_add);
9997ac0e47SRussell King 
10097ac0e47SRussell King /**
101df785aa8SLiviu Dudau  * drm_of_component_probe - Generic probe function for a component based master
102df785aa8SLiviu Dudau  * @dev: master device containing the OF node
103df785aa8SLiviu Dudau  * @compare_of: compare function used for matching components
1047f9e7ec9SDaniel Vetter  * @m_ops: component master ops to be used
105df785aa8SLiviu Dudau  *
106df785aa8SLiviu Dudau  * Parse the platform device OF node and bind all the components associated
107df785aa8SLiviu Dudau  * with the master. Interface ports are added before the encoders in order to
108df785aa8SLiviu Dudau  * satisfy their .bind requirements
109df785aa8SLiviu Dudau  * See Documentation/devicetree/bindings/graph.txt for the bindings.
110df785aa8SLiviu Dudau  *
111df785aa8SLiviu Dudau  * Returns zero if successful, or one of the standard error codes if it fails.
112df785aa8SLiviu Dudau  */
drm_of_component_probe(struct device * dev,int (* compare_of)(struct device *,void *),const struct component_master_ops * m_ops)113df785aa8SLiviu Dudau int drm_of_component_probe(struct device *dev,
114df785aa8SLiviu Dudau 			   int (*compare_of)(struct device *, void *),
115df785aa8SLiviu Dudau 			   const struct component_master_ops *m_ops)
116df785aa8SLiviu Dudau {
117df785aa8SLiviu Dudau 	struct device_node *ep, *port, *remote;
118df785aa8SLiviu Dudau 	struct component_match *match = NULL;
119df785aa8SLiviu Dudau 	int i;
120df785aa8SLiviu Dudau 
121df785aa8SLiviu Dudau 	if (!dev->of_node)
122df785aa8SLiviu Dudau 		return -EINVAL;
123df785aa8SLiviu Dudau 
124df785aa8SLiviu Dudau 	/*
125df785aa8SLiviu Dudau 	 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
126df785aa8SLiviu Dudau 	 * called from encoder's .bind callbacks works as expected
127df785aa8SLiviu Dudau 	 */
128df785aa8SLiviu Dudau 	for (i = 0; ; i++) {
129df785aa8SLiviu Dudau 		port = of_parse_phandle(dev->of_node, "ports", i);
130df785aa8SLiviu Dudau 		if (!port)
131df785aa8SLiviu Dudau 			break;
132df785aa8SLiviu Dudau 
1332efa8392SBaruch Siach 		if (of_device_is_available(port->parent))
1342efa8392SBaruch Siach 			drm_of_component_match_add(dev, &match, compare_of,
1352efa8392SBaruch Siach 						   port);
136df785aa8SLiviu Dudau 
137df785aa8SLiviu Dudau 		of_node_put(port);
138df785aa8SLiviu Dudau 	}
139df785aa8SLiviu Dudau 
140df785aa8SLiviu Dudau 	if (i == 0) {
141df785aa8SLiviu Dudau 		dev_err(dev, "missing 'ports' property\n");
142df785aa8SLiviu Dudau 		return -ENODEV;
143df785aa8SLiviu Dudau 	}
144df785aa8SLiviu Dudau 
145df785aa8SLiviu Dudau 	if (!match) {
146df785aa8SLiviu Dudau 		dev_err(dev, "no available port\n");
147df785aa8SLiviu Dudau 		return -ENODEV;
148df785aa8SLiviu Dudau 	}
149df785aa8SLiviu Dudau 
150df785aa8SLiviu Dudau 	/*
151df785aa8SLiviu Dudau 	 * For bound crtcs, bind the encoders attached to their remote endpoint
152df785aa8SLiviu Dudau 	 */
153df785aa8SLiviu Dudau 	for (i = 0; ; i++) {
154df785aa8SLiviu Dudau 		port = of_parse_phandle(dev->of_node, "ports", i);
155df785aa8SLiviu Dudau 		if (!port)
156df785aa8SLiviu Dudau 			break;
157df785aa8SLiviu Dudau 
158df785aa8SLiviu Dudau 		if (!of_device_is_available(port->parent)) {
159df785aa8SLiviu Dudau 			of_node_put(port);
160df785aa8SLiviu Dudau 			continue;
161df785aa8SLiviu Dudau 		}
162df785aa8SLiviu Dudau 
163df785aa8SLiviu Dudau 		for_each_child_of_node(port, ep) {
164df785aa8SLiviu Dudau 			remote = of_graph_get_remote_port_parent(ep);
165df785aa8SLiviu Dudau 			if (!remote || !of_device_is_available(remote)) {
166df785aa8SLiviu Dudau 				of_node_put(remote);
167df785aa8SLiviu Dudau 				continue;
168df785aa8SLiviu Dudau 			} else if (!of_device_is_available(remote->parent)) {
1694bf99144SRob Herring 				dev_warn(dev, "parent device of %pOF is not available\n",
1704bf99144SRob Herring 					 remote);
171df785aa8SLiviu Dudau 				of_node_put(remote);
172df785aa8SLiviu Dudau 				continue;
173df785aa8SLiviu Dudau 			}
174df785aa8SLiviu Dudau 
17597ac0e47SRussell King 			drm_of_component_match_add(dev, &match, compare_of,
17697ac0e47SRussell King 						   remote);
177df785aa8SLiviu Dudau 			of_node_put(remote);
178df785aa8SLiviu Dudau 		}
179df785aa8SLiviu Dudau 		of_node_put(port);
180df785aa8SLiviu Dudau 	}
181df785aa8SLiviu Dudau 
182df785aa8SLiviu Dudau 	return component_master_add_with_match(dev, m_ops, match);
183df785aa8SLiviu Dudau }
184df785aa8SLiviu Dudau EXPORT_SYMBOL(drm_of_component_probe);
1854cacf91fSPhilipp Zabel 
1864cacf91fSPhilipp Zabel /*
1874cacf91fSPhilipp Zabel  * drm_of_encoder_active_endpoint - return the active encoder endpoint
1884cacf91fSPhilipp Zabel  * @node: device tree node containing encoder input ports
1894cacf91fSPhilipp Zabel  * @encoder: drm_encoder
1904cacf91fSPhilipp Zabel  *
1914cacf91fSPhilipp Zabel  * Given an encoder device node and a drm_encoder with a connected crtc,
1924cacf91fSPhilipp Zabel  * parse the encoder endpoint connecting to the crtc port.
1934cacf91fSPhilipp Zabel  */
drm_of_encoder_active_endpoint(struct device_node * node,struct drm_encoder * encoder,struct of_endpoint * endpoint)1944cacf91fSPhilipp Zabel int drm_of_encoder_active_endpoint(struct device_node *node,
1954cacf91fSPhilipp Zabel 				   struct drm_encoder *encoder,
1964cacf91fSPhilipp Zabel 				   struct of_endpoint *endpoint)
1974cacf91fSPhilipp Zabel {
1984cacf91fSPhilipp Zabel 	struct device_node *ep;
1994cacf91fSPhilipp Zabel 	struct drm_crtc *crtc = encoder->crtc;
2004cacf91fSPhilipp Zabel 	struct device_node *port;
2014cacf91fSPhilipp Zabel 	int ret;
2024cacf91fSPhilipp Zabel 
2034cacf91fSPhilipp Zabel 	if (!node || !crtc)
2044cacf91fSPhilipp Zabel 		return -EINVAL;
2054cacf91fSPhilipp Zabel 
2064cacf91fSPhilipp Zabel 	for_each_endpoint_of_node(node, ep) {
2074cacf91fSPhilipp Zabel 		port = of_graph_get_remote_port(ep);
2084cacf91fSPhilipp Zabel 		of_node_put(port);
2094cacf91fSPhilipp Zabel 		if (port == crtc->port) {
2104cacf91fSPhilipp Zabel 			ret = of_graph_parse_endpoint(ep, endpoint);
2114cacf91fSPhilipp Zabel 			of_node_put(ep);
2124cacf91fSPhilipp Zabel 			return ret;
2134cacf91fSPhilipp Zabel 		}
2144cacf91fSPhilipp Zabel 	}
2154cacf91fSPhilipp Zabel 
2164cacf91fSPhilipp Zabel 	return -EINVAL;
2174cacf91fSPhilipp Zabel }
2184cacf91fSPhilipp Zabel EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
2191f2db303SRob Herring 
2203fbdfe99SDaniel Vetter /**
2211f2db303SRob Herring  * drm_of_find_panel_or_bridge - return connected panel or bridge device
2221f2db303SRob Herring  * @np: device tree node containing encoder output ports
2233fbdfe99SDaniel Vetter  * @port: port in the device tree node
2243fbdfe99SDaniel Vetter  * @endpoint: endpoint in the device tree node
2251f2db303SRob Herring  * @panel: pointer to hold returned drm_panel
2261f2db303SRob Herring  * @bridge: pointer to hold returned drm_bridge
2271f2db303SRob Herring  *
2281f2db303SRob Herring  * Given a DT node's port and endpoint number, find the connected node and
2291f2db303SRob Herring  * return either the associated struct drm_panel or drm_bridge device. Either
2301f2db303SRob Herring  * @panel or @bridge must not be NULL.
2311f2db303SRob Herring  *
23287ea9580SMaxime Ripard  * This function is deprecated and should not be used in new drivers. Use
23387ea9580SMaxime Ripard  * devm_drm_of_get_bridge() instead.
23487ea9580SMaxime Ripard  *
2351f2db303SRob Herring  * Returns zero if successful, or one of the standard error codes if it fails.
2361f2db303SRob Herring  */
drm_of_find_panel_or_bridge(const struct device_node * np,int port,int endpoint,struct drm_panel ** panel,struct drm_bridge ** bridge)2371f2db303SRob Herring int drm_of_find_panel_or_bridge(const struct device_node *np,
2381f2db303SRob Herring 				int port, int endpoint,
2391f2db303SRob Herring 				struct drm_panel **panel,
2401f2db303SRob Herring 				struct drm_bridge **bridge)
2411f2db303SRob Herring {
242169466d4SBjorn Andersson 	int ret = -EPROBE_DEFER;
243169466d4SBjorn Andersson 	struct device_node *remote;
2441f2db303SRob Herring 
2451f2db303SRob Herring 	if (!panel && !bridge)
2461f2db303SRob Herring 		return -EINVAL;
247320e421eSDan Carpenter 	if (panel)
248320e421eSDan Carpenter 		*panel = NULL;
2491f2db303SRob Herring 
250169466d4SBjorn Andersson 	/*
251169466d4SBjorn Andersson 	 * of_graph_get_remote_node() produces a noisy error message if port
252169466d4SBjorn Andersson 	 * node isn't found and the absence of the port is a legit case here,
253169466d4SBjorn Andersson 	 * so at first we silently check whether graph presents in the
254169466d4SBjorn Andersson 	 * device-tree node.
255169466d4SBjorn Andersson 	 */
256169466d4SBjorn Andersson 	if (!of_graph_is_present(np))
257169466d4SBjorn Andersson 		return -ENODEV;
258169466d4SBjorn Andersson 
259169466d4SBjorn Andersson 	remote = of_graph_get_remote_node(np, port, endpoint);
260169466d4SBjorn Andersson 	if (!remote)
261169466d4SBjorn Andersson 		return -ENODEV;
262169466d4SBjorn Andersson 
263169466d4SBjorn Andersson 	if (panel) {
264169466d4SBjorn Andersson 		*panel = of_drm_find_panel(remote);
265169466d4SBjorn Andersson 		if (!IS_ERR(*panel))
266169466d4SBjorn Andersson 			ret = 0;
267169466d4SBjorn Andersson 		else
268169466d4SBjorn Andersson 			*panel = NULL;
269169466d4SBjorn Andersson 	}
270169466d4SBjorn Andersson 
271169466d4SBjorn Andersson 	/* No panel found yet, check for a bridge next. */
272169466d4SBjorn Andersson 	if (bridge) {
273169466d4SBjorn Andersson 		if (ret) {
274169466d4SBjorn Andersson 			*bridge = of_drm_find_bridge(remote);
275169466d4SBjorn Andersson 			if (*bridge)
276169466d4SBjorn Andersson 				ret = 0;
277169466d4SBjorn Andersson 		} else {
278169466d4SBjorn Andersson 			*bridge = NULL;
279169466d4SBjorn Andersson 		}
280169466d4SBjorn Andersson 
281169466d4SBjorn Andersson 	}
282169466d4SBjorn Andersson 
283169466d4SBjorn Andersson 	of_node_put(remote);
284169466d4SBjorn Andersson 	return ret;
2851f2db303SRob Herring }
2861f2db303SRob Herring EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);
28765290075SFabrizio Castro 
28865290075SFabrizio Castro enum drm_of_lvds_pixels {
28965290075SFabrizio Castro 	DRM_OF_LVDS_EVEN = BIT(0),
29065290075SFabrizio Castro 	DRM_OF_LVDS_ODD = BIT(1),
29165290075SFabrizio Castro };
29265290075SFabrizio Castro 
drm_of_lvds_get_port_pixels_type(struct device_node * port_node)29365290075SFabrizio Castro static int drm_of_lvds_get_port_pixels_type(struct device_node *port_node)
29465290075SFabrizio Castro {
29565290075SFabrizio Castro 	bool even_pixels =
29665290075SFabrizio Castro 		of_property_read_bool(port_node, "dual-lvds-even-pixels");
29765290075SFabrizio Castro 	bool odd_pixels =
29865290075SFabrizio Castro 		of_property_read_bool(port_node, "dual-lvds-odd-pixels");
29965290075SFabrizio Castro 
30065290075SFabrizio Castro 	return (even_pixels ? DRM_OF_LVDS_EVEN : 0) |
30165290075SFabrizio Castro 	       (odd_pixels ? DRM_OF_LVDS_ODD : 0);
30265290075SFabrizio Castro }
30365290075SFabrizio Castro 
drm_of_lvds_get_remote_pixels_type(const struct device_node * port_node)30465290075SFabrizio Castro static int drm_of_lvds_get_remote_pixels_type(
30565290075SFabrizio Castro 			const struct device_node *port_node)
30665290075SFabrizio Castro {
30765290075SFabrizio Castro 	struct device_node *endpoint = NULL;
30865290075SFabrizio Castro 	int pixels_type = -EPIPE;
30965290075SFabrizio Castro 
31065290075SFabrizio Castro 	for_each_child_of_node(port_node, endpoint) {
31165290075SFabrizio Castro 		struct device_node *remote_port;
31265290075SFabrizio Castro 		int current_pt;
31365290075SFabrizio Castro 
31465290075SFabrizio Castro 		if (!of_node_name_eq(endpoint, "endpoint"))
31565290075SFabrizio Castro 			continue;
31665290075SFabrizio Castro 
31765290075SFabrizio Castro 		remote_port = of_graph_get_remote_port(endpoint);
31865290075SFabrizio Castro 		if (!remote_port) {
319b557a5f8SJulia Lawall 			of_node_put(endpoint);
32065290075SFabrizio Castro 			return -EPIPE;
32165290075SFabrizio Castro 		}
32265290075SFabrizio Castro 
32365290075SFabrizio Castro 		current_pt = drm_of_lvds_get_port_pixels_type(remote_port);
32465290075SFabrizio Castro 		of_node_put(remote_port);
32565290075SFabrizio Castro 		if (pixels_type < 0)
32665290075SFabrizio Castro 			pixels_type = current_pt;
32765290075SFabrizio Castro 
32865290075SFabrizio Castro 		/*
32965290075SFabrizio Castro 		 * Sanity check, ensure that all remote endpoints have the same
33065290075SFabrizio Castro 		 * pixel type. We may lift this restriction later if we need to
33165290075SFabrizio Castro 		 * support multiple sinks with different dual-link
33265290075SFabrizio Castro 		 * configurations by passing the endpoints explicitly to
33365290075SFabrizio Castro 		 * drm_of_lvds_get_dual_link_pixel_order().
33465290075SFabrizio Castro 		 */
3356f9223a5SSteven Price 		if (!current_pt || pixels_type != current_pt) {
3366f9223a5SSteven Price 			of_node_put(endpoint);
33765290075SFabrizio Castro 			return -EINVAL;
33865290075SFabrizio Castro 		}
3396f9223a5SSteven Price 	}
34065290075SFabrizio Castro 
34165290075SFabrizio Castro 	return pixels_type;
34265290075SFabrizio Castro }
34365290075SFabrizio Castro 
34465290075SFabrizio Castro /**
34565290075SFabrizio Castro  * drm_of_lvds_get_dual_link_pixel_order - Get LVDS dual-link pixel order
34665290075SFabrizio Castro  * @port1: First DT port node of the Dual-link LVDS source
34765290075SFabrizio Castro  * @port2: Second DT port node of the Dual-link LVDS source
34865290075SFabrizio Castro  *
34965290075SFabrizio Castro  * An LVDS dual-link connection is made of two links, with even pixels
35065290075SFabrizio Castro  * transitting on one link, and odd pixels on the other link. This function
35165290075SFabrizio Castro  * returns, for two ports of an LVDS dual-link source, which port shall transmit
35265290075SFabrizio Castro  * the even and odd pixels, based on the requirements of the connected sink.
35365290075SFabrizio Castro  *
35465290075SFabrizio Castro  * The pixel order is determined from the dual-lvds-even-pixels and
35565290075SFabrizio Castro  * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
35665290075SFabrizio Castro  * properties are not present, or if their usage is not valid, this function
35765290075SFabrizio Castro  * returns -EINVAL.
35865290075SFabrizio Castro  *
35965290075SFabrizio Castro  * If either port is not connected, this function returns -EPIPE.
36065290075SFabrizio Castro  *
36165290075SFabrizio Castro  * @port1 and @port2 are typically DT sibling nodes, but may have different
36265290075SFabrizio Castro  * parents when, for instance, two separate LVDS encoders carry the even and odd
36365290075SFabrizio Castro  * pixels.
36465290075SFabrizio Castro  *
36565290075SFabrizio Castro  * Return:
36665290075SFabrizio Castro  * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 carries even pixels and @port2
36765290075SFabrizio Castro  *   carries odd pixels
36865290075SFabrizio Castro  * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 carries odd pixels and @port2
36965290075SFabrizio Castro  *   carries even pixels
37065290075SFabrizio Castro  * * -EINVAL - @port1 and @port2 are not connected to a dual-link LVDS sink, or
37165290075SFabrizio Castro  *   the sink configuration is invalid
37265290075SFabrizio Castro  * * -EPIPE - when @port1 or @port2 are not connected
37365290075SFabrizio Castro  */
drm_of_lvds_get_dual_link_pixel_order(const struct device_node * port1,const struct device_node * port2)37465290075SFabrizio Castro int drm_of_lvds_get_dual_link_pixel_order(const struct device_node *port1,
37565290075SFabrizio Castro 					  const struct device_node *port2)
37665290075SFabrizio Castro {
37765290075SFabrizio Castro 	int remote_p1_pt, remote_p2_pt;
37865290075SFabrizio Castro 
37965290075SFabrizio Castro 	if (!port1 || !port2)
38065290075SFabrizio Castro 		return -EINVAL;
38165290075SFabrizio Castro 
38265290075SFabrizio Castro 	remote_p1_pt = drm_of_lvds_get_remote_pixels_type(port1);
38365290075SFabrizio Castro 	if (remote_p1_pt < 0)
38465290075SFabrizio Castro 		return remote_p1_pt;
38565290075SFabrizio Castro 
38665290075SFabrizio Castro 	remote_p2_pt = drm_of_lvds_get_remote_pixels_type(port2);
38765290075SFabrizio Castro 	if (remote_p2_pt < 0)
38865290075SFabrizio Castro 		return remote_p2_pt;
38965290075SFabrizio Castro 
39065290075SFabrizio Castro 	/*
39165290075SFabrizio Castro 	 * A valid dual-lVDS bus is found when one remote port is marked with
39265290075SFabrizio Castro 	 * "dual-lvds-even-pixels", and the other remote port is marked with
39365290075SFabrizio Castro 	 * "dual-lvds-odd-pixels", bail out if the markers are not right.
39465290075SFabrizio Castro 	 */
39565290075SFabrizio Castro 	if (remote_p1_pt + remote_p2_pt != DRM_OF_LVDS_EVEN + DRM_OF_LVDS_ODD)
39665290075SFabrizio Castro 		return -EINVAL;
39765290075SFabrizio Castro 
39865290075SFabrizio Castro 	return remote_p1_pt == DRM_OF_LVDS_EVEN ?
39965290075SFabrizio Castro 		DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS :
40065290075SFabrizio Castro 		DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS;
40165290075SFabrizio Castro }
40265290075SFabrizio Castro EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order);
4037c4dd0a2SMarek Vasut 
4047c4dd0a2SMarek Vasut /**
4057c4dd0a2SMarek Vasut  * drm_of_lvds_get_data_mapping - Get LVDS data mapping
4067c4dd0a2SMarek Vasut  * @port: DT port node of the LVDS source or sink
4077c4dd0a2SMarek Vasut  *
4087c4dd0a2SMarek Vasut  * Convert DT "data-mapping" property string value into media bus format value.
4097c4dd0a2SMarek Vasut  *
4107c4dd0a2SMarek Vasut  * Return:
4117c4dd0a2SMarek Vasut  * * MEDIA_BUS_FMT_RGB666_1X7X3_SPWG - data-mapping is "jeida-18"
4127c4dd0a2SMarek Vasut  * * MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA - data-mapping is "jeida-24"
4137c4dd0a2SMarek Vasut  * * MEDIA_BUS_FMT_RGB888_1X7X4_SPWG - data-mapping is "vesa-24"
4147c4dd0a2SMarek Vasut  * * -EINVAL - the "data-mapping" property is unsupported
4157c4dd0a2SMarek Vasut  * * -ENODEV - the "data-mapping" property is missing
4167c4dd0a2SMarek Vasut  */
drm_of_lvds_get_data_mapping(const struct device_node * port)4177c4dd0a2SMarek Vasut int drm_of_lvds_get_data_mapping(const struct device_node *port)
4187c4dd0a2SMarek Vasut {
4197c4dd0a2SMarek Vasut 	const char *mapping;
4207c4dd0a2SMarek Vasut 	int ret;
4217c4dd0a2SMarek Vasut 
4227c4dd0a2SMarek Vasut 	ret = of_property_read_string(port, "data-mapping", &mapping);
4237c4dd0a2SMarek Vasut 	if (ret < 0)
4247c4dd0a2SMarek Vasut 		return -ENODEV;
4257c4dd0a2SMarek Vasut 
4267c4dd0a2SMarek Vasut 	if (!strcmp(mapping, "jeida-18"))
4277c4dd0a2SMarek Vasut 		return MEDIA_BUS_FMT_RGB666_1X7X3_SPWG;
4287c4dd0a2SMarek Vasut 	if (!strcmp(mapping, "jeida-24"))
4297c4dd0a2SMarek Vasut 		return MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA;
4307c4dd0a2SMarek Vasut 	if (!strcmp(mapping, "vesa-24"))
4317c4dd0a2SMarek Vasut 		return MEDIA_BUS_FMT_RGB888_1X7X4_SPWG;
4327c4dd0a2SMarek Vasut 
4337c4dd0a2SMarek Vasut 	return -EINVAL;
4347c4dd0a2SMarek Vasut }
4357c4dd0a2SMarek Vasut EXPORT_SYMBOL_GPL(drm_of_lvds_get_data_mapping);
436fc801750SMarek Vasut 
437fc801750SMarek Vasut /**
438fc801750SMarek Vasut  * drm_of_get_data_lanes_count - Get DSI/(e)DP data lane count
439fc801750SMarek Vasut  * @endpoint: DT endpoint node of the DSI/(e)DP source or sink
440fc801750SMarek Vasut  * @min: minimum supported number of data lanes
441fc801750SMarek Vasut  * @max: maximum supported number of data lanes
442fc801750SMarek Vasut  *
443fc801750SMarek Vasut  * Count DT "data-lanes" property elements and check for validity.
444fc801750SMarek Vasut  *
445fc801750SMarek Vasut  * Return:
446fc801750SMarek Vasut  * * min..max - positive integer count of "data-lanes" elements
447fc801750SMarek Vasut  * * -ve - the "data-lanes" property is missing or invalid
448fc801750SMarek Vasut  * * -EINVAL - the "data-lanes" property is unsupported
449fc801750SMarek Vasut  */
drm_of_get_data_lanes_count(const struct device_node * endpoint,const unsigned int min,const unsigned int max)450fc801750SMarek Vasut int drm_of_get_data_lanes_count(const struct device_node *endpoint,
451fc801750SMarek Vasut 				const unsigned int min, const unsigned int max)
452fc801750SMarek Vasut {
453fc801750SMarek Vasut 	int ret;
454fc801750SMarek Vasut 
455fc801750SMarek Vasut 	ret = of_property_count_u32_elems(endpoint, "data-lanes");
456fc801750SMarek Vasut 	if (ret < 0)
457fc801750SMarek Vasut 		return ret;
458fc801750SMarek Vasut 
459fc801750SMarek Vasut 	if (ret < min || ret > max)
460fc801750SMarek Vasut 		return -EINVAL;
461fc801750SMarek Vasut 
462fc801750SMarek Vasut 	return ret;
463fc801750SMarek Vasut }
464fc801750SMarek Vasut EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count);
465fc801750SMarek Vasut 
466fc801750SMarek Vasut /**
467fc801750SMarek Vasut  * drm_of_get_data_lanes_count_ep - Get DSI/(e)DP data lane count by endpoint
468fc801750SMarek Vasut  * @port: DT port node of the DSI/(e)DP source or sink
469fc801750SMarek Vasut  * @port_reg: identifier (value of reg property) of the parent port node
470fc801750SMarek Vasut  * @reg: identifier (value of reg property) of the endpoint node
471fc801750SMarek Vasut  * @min: minimum supported number of data lanes
472fc801750SMarek Vasut  * @max: maximum supported number of data lanes
473fc801750SMarek Vasut  *
474fc801750SMarek Vasut  * Count DT "data-lanes" property elements and check for validity.
475fc801750SMarek Vasut  * This variant uses endpoint specifier.
476fc801750SMarek Vasut  *
477fc801750SMarek Vasut  * Return:
478fc801750SMarek Vasut  * * min..max - positive integer count of "data-lanes" elements
479fc801750SMarek Vasut  * * -EINVAL - the "data-mapping" property is unsupported
480fc801750SMarek Vasut  * * -ENODEV - the "data-mapping" property is missing
481fc801750SMarek Vasut  */
drm_of_get_data_lanes_count_ep(const struct device_node * port,int port_reg,int reg,const unsigned int min,const unsigned int max)482fc801750SMarek Vasut int drm_of_get_data_lanes_count_ep(const struct device_node *port,
483fc801750SMarek Vasut 				   int port_reg, int reg,
484fc801750SMarek Vasut 				   const unsigned int min,
485fc801750SMarek Vasut 				   const unsigned int max)
486fc801750SMarek Vasut {
487fc801750SMarek Vasut 	struct device_node *endpoint;
488fc801750SMarek Vasut 	int ret;
489fc801750SMarek Vasut 
490fc801750SMarek Vasut 	endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg);
491fc801750SMarek Vasut 	ret = drm_of_get_data_lanes_count(endpoint, min, max);
492fc801750SMarek Vasut 	of_node_put(endpoint);
493fc801750SMarek Vasut 
494fc801750SMarek Vasut 	return ret;
495fc801750SMarek Vasut }
496fc801750SMarek Vasut EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_ep);
497*19ff997fSChris Morgan 
498*19ff997fSChris Morgan #if IS_ENABLED(CONFIG_DRM_MIPI_DSI)
499*19ff997fSChris Morgan 
500*19ff997fSChris Morgan /**
501*19ff997fSChris Morgan  * drm_of_get_dsi_bus - find the DSI bus for a given device
502*19ff997fSChris Morgan  * @dev: parent device of display (SPI, I2C)
503*19ff997fSChris Morgan  *
504*19ff997fSChris Morgan  * Gets parent DSI bus for a DSI device controlled through a bus other
505*19ff997fSChris Morgan  * than MIPI-DCS (SPI, I2C, etc.) using the Device Tree.
506*19ff997fSChris Morgan  *
507*19ff997fSChris Morgan  * Returns pointer to mipi_dsi_host if successful, -EINVAL if the
508*19ff997fSChris Morgan  * request is unsupported, -EPROBE_DEFER if the DSI host is found but
509*19ff997fSChris Morgan  * not available, or -ENODEV otherwise.
510*19ff997fSChris Morgan  */
drm_of_get_dsi_bus(struct device * dev)511*19ff997fSChris Morgan struct mipi_dsi_host *drm_of_get_dsi_bus(struct device *dev)
512*19ff997fSChris Morgan {
513*19ff997fSChris Morgan 	struct mipi_dsi_host *dsi_host;
514*19ff997fSChris Morgan 	struct device_node *endpoint, *dsi_host_node;
515*19ff997fSChris Morgan 
516*19ff997fSChris Morgan 	/*
517*19ff997fSChris Morgan 	 * Get first endpoint child from device.
518*19ff997fSChris Morgan 	 */
519*19ff997fSChris Morgan 	endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
520*19ff997fSChris Morgan 	if (!endpoint)
521*19ff997fSChris Morgan 		return ERR_PTR(-ENODEV);
522*19ff997fSChris Morgan 
523*19ff997fSChris Morgan 	/*
524*19ff997fSChris Morgan 	 * Follow the first endpoint to get the DSI host node and then
525*19ff997fSChris Morgan 	 * release the endpoint since we no longer need it.
526*19ff997fSChris Morgan 	 */
527*19ff997fSChris Morgan 	dsi_host_node = of_graph_get_remote_port_parent(endpoint);
528*19ff997fSChris Morgan 	of_node_put(endpoint);
529*19ff997fSChris Morgan 	if (!dsi_host_node)
530*19ff997fSChris Morgan 		return ERR_PTR(-ENODEV);
531*19ff997fSChris Morgan 
532*19ff997fSChris Morgan 	/*
533*19ff997fSChris Morgan 	 * Get the DSI host from the DSI host node. If we get an error
534*19ff997fSChris Morgan 	 * or the return is null assume we're not ready to probe just
535*19ff997fSChris Morgan 	 * yet. Release the DSI host node since we're done with it.
536*19ff997fSChris Morgan 	 */
537*19ff997fSChris Morgan 	dsi_host = of_find_mipi_dsi_host_by_node(dsi_host_node);
538*19ff997fSChris Morgan 	of_node_put(dsi_host_node);
539*19ff997fSChris Morgan 	if (IS_ERR_OR_NULL(dsi_host))
540*19ff997fSChris Morgan 		return ERR_PTR(-EPROBE_DEFER);
541*19ff997fSChris Morgan 
542*19ff997fSChris Morgan 	return dsi_host;
543*19ff997fSChris Morgan }
544*19ff997fSChris Morgan EXPORT_SYMBOL_GPL(drm_of_get_dsi_bus);
545*19ff997fSChris Morgan 
546*19ff997fSChris Morgan #endif /* CONFIG_DRM_MIPI_DSI */
547