xref: /linux/drivers/gpu/drm/drm_of.c (revision 73289afe03619bac585b69f563d0bb9a52e67722)
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>
5*73289afeSVille Syrjälä #include <linux/of.h>
67e435aadSRussell King #include <linux/of_graph.h>
70500c04eSSam Ravnborg 
81f2db303SRob Herring #include <drm/drm_bridge.h>
97e435aadSRussell King #include <drm/drm_crtc.h>
100500c04eSSam Ravnborg #include <drm/drm_device.h>
119338203cSLaurent Pinchart #include <drm/drm_encoder.h>
127e435aadSRussell King #include <drm/drm_of.h>
130500c04eSSam Ravnborg #include <drm/drm_panel.h>
147e435aadSRussell King 
157f9e7ec9SDaniel Vetter /**
167f9e7ec9SDaniel Vetter  * DOC: overview
177f9e7ec9SDaniel Vetter  *
187f9e7ec9SDaniel Vetter  * A set of helper functions to aid DRM drivers in parsing standard DT
197f9e7ec9SDaniel Vetter  * properties.
207f9e7ec9SDaniel Vetter  */
217f9e7ec9SDaniel Vetter 
227e435aadSRussell King /**
238b5f7a62SJernej Skrabec  * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node
247e435aadSRussell King  * @dev: DRM device
257e435aadSRussell King  * @port: port OF node
267e435aadSRussell King  *
277e435aadSRussell King  * Given a port OF node, return the possible mask of the corresponding
287e435aadSRussell King  * CRTC within a device's list of CRTCs.  Returns zero if not found.
297e435aadSRussell King  */
308b5f7a62SJernej Skrabec uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
317e435aadSRussell King 			    struct device_node *port)
327e435aadSRussell King {
337e435aadSRussell King 	unsigned int index = 0;
347e435aadSRussell King 	struct drm_crtc *tmp;
357e435aadSRussell King 
366295d607SDaniel Vetter 	drm_for_each_crtc(tmp, dev) {
377e435aadSRussell King 		if (tmp->port == port)
387e435aadSRussell King 			return 1 << index;
397e435aadSRussell King 
407e435aadSRussell King 		index++;
417e435aadSRussell King 	}
427e435aadSRussell King 
437e435aadSRussell King 	return 0;
447e435aadSRussell King }
458b5f7a62SJernej Skrabec EXPORT_SYMBOL(drm_of_crtc_port_mask);
467e435aadSRussell King 
477e435aadSRussell King /**
487e435aadSRussell King  * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
497e435aadSRussell King  * @dev: DRM device
507e435aadSRussell King  * @port: encoder port to scan for endpoints
517e435aadSRussell King  *
527e435aadSRussell King  * Scan all endpoints attached to a port, locate their attached CRTCs,
537e435aadSRussell King  * and generate the DRM mask of CRTCs which may be attached to this
547e435aadSRussell King  * encoder.
557e435aadSRussell King  *
567e435aadSRussell King  * See Documentation/devicetree/bindings/graph.txt for the bindings.
577e435aadSRussell King  */
587e435aadSRussell King uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
597e435aadSRussell King 				    struct device_node *port)
607e435aadSRussell King {
617416f4e3SPhilipp Zabel 	struct device_node *remote_port, *ep;
627e435aadSRussell King 	uint32_t possible_crtcs = 0;
637e435aadSRussell King 
647416f4e3SPhilipp Zabel 	for_each_endpoint_of_node(port, ep) {
657e435aadSRussell King 		remote_port = of_graph_get_remote_port(ep);
667e435aadSRussell King 		if (!remote_port) {
677e435aadSRussell King 			of_node_put(ep);
687e435aadSRussell King 			return 0;
697e435aadSRussell King 		}
707e435aadSRussell King 
718b5f7a62SJernej Skrabec 		possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
727e435aadSRussell King 
737e435aadSRussell King 		of_node_put(remote_port);
747416f4e3SPhilipp Zabel 	}
757e435aadSRussell King 
767e435aadSRussell King 	return possible_crtcs;
777e435aadSRussell King }
787e435aadSRussell King EXPORT_SYMBOL(drm_of_find_possible_crtcs);
79df785aa8SLiviu Dudau 
80df785aa8SLiviu Dudau /**
8197ac0e47SRussell King  * drm_of_component_match_add - Add a component helper OF node match rule
8297ac0e47SRussell King  * @master: master device
8397ac0e47SRussell King  * @matchptr: component match pointer
8497ac0e47SRussell King  * @compare: compare function used for matching component
8597ac0e47SRussell King  * @node: of_node
8697ac0e47SRussell King  */
8797ac0e47SRussell King void drm_of_component_match_add(struct device *master,
8897ac0e47SRussell King 				struct component_match **matchptr,
8997ac0e47SRussell King 				int (*compare)(struct device *, void *),
9097ac0e47SRussell King 				struct device_node *node)
9197ac0e47SRussell King {
9297ac0e47SRussell King 	of_node_get(node);
93ab011ab6SYong Wu 	component_match_add_release(master, matchptr, component_release_of,
9497ac0e47SRussell King 				    compare, node);
9597ac0e47SRussell King }
9697ac0e47SRussell King EXPORT_SYMBOL_GPL(drm_of_component_match_add);
9797ac0e47SRussell King 
9897ac0e47SRussell King /**
99df785aa8SLiviu Dudau  * drm_of_component_probe - Generic probe function for a component based master
100df785aa8SLiviu Dudau  * @dev: master device containing the OF node
101df785aa8SLiviu Dudau  * @compare_of: compare function used for matching components
1027f9e7ec9SDaniel Vetter  * @m_ops: component master ops to be used
103df785aa8SLiviu Dudau  *
104df785aa8SLiviu Dudau  * Parse the platform device OF node and bind all the components associated
105df785aa8SLiviu Dudau  * with the master. Interface ports are added before the encoders in order to
106df785aa8SLiviu Dudau  * satisfy their .bind requirements
107df785aa8SLiviu Dudau  * See Documentation/devicetree/bindings/graph.txt for the bindings.
108df785aa8SLiviu Dudau  *
109df785aa8SLiviu Dudau  * Returns zero if successful, or one of the standard error codes if it fails.
110df785aa8SLiviu Dudau  */
111df785aa8SLiviu Dudau int drm_of_component_probe(struct device *dev,
112df785aa8SLiviu Dudau 			   int (*compare_of)(struct device *, void *),
113df785aa8SLiviu Dudau 			   const struct component_master_ops *m_ops)
114df785aa8SLiviu Dudau {
115df785aa8SLiviu Dudau 	struct device_node *ep, *port, *remote;
116df785aa8SLiviu Dudau 	struct component_match *match = NULL;
117df785aa8SLiviu Dudau 	int i;
118df785aa8SLiviu Dudau 
119df785aa8SLiviu Dudau 	if (!dev->of_node)
120df785aa8SLiviu Dudau 		return -EINVAL;
121df785aa8SLiviu Dudau 
122df785aa8SLiviu Dudau 	/*
123df785aa8SLiviu Dudau 	 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
124df785aa8SLiviu Dudau 	 * called from encoder's .bind callbacks works as expected
125df785aa8SLiviu Dudau 	 */
126df785aa8SLiviu Dudau 	for (i = 0; ; i++) {
127df785aa8SLiviu Dudau 		port = of_parse_phandle(dev->of_node, "ports", i);
128df785aa8SLiviu Dudau 		if (!port)
129df785aa8SLiviu Dudau 			break;
130df785aa8SLiviu Dudau 
1312efa8392SBaruch Siach 		if (of_device_is_available(port->parent))
1322efa8392SBaruch Siach 			drm_of_component_match_add(dev, &match, compare_of,
1332efa8392SBaruch Siach 						   port);
134df785aa8SLiviu Dudau 
135df785aa8SLiviu Dudau 		of_node_put(port);
136df785aa8SLiviu Dudau 	}
137df785aa8SLiviu Dudau 
138df785aa8SLiviu Dudau 	if (i == 0) {
139df785aa8SLiviu Dudau 		dev_err(dev, "missing 'ports' property\n");
140df785aa8SLiviu Dudau 		return -ENODEV;
141df785aa8SLiviu Dudau 	}
142df785aa8SLiviu Dudau 
143df785aa8SLiviu Dudau 	if (!match) {
144df785aa8SLiviu Dudau 		dev_err(dev, "no available port\n");
145df785aa8SLiviu Dudau 		return -ENODEV;
146df785aa8SLiviu Dudau 	}
147df785aa8SLiviu Dudau 
148df785aa8SLiviu Dudau 	/*
149df785aa8SLiviu Dudau 	 * For bound crtcs, bind the encoders attached to their remote endpoint
150df785aa8SLiviu Dudau 	 */
151df785aa8SLiviu Dudau 	for (i = 0; ; i++) {
152df785aa8SLiviu Dudau 		port = of_parse_phandle(dev->of_node, "ports", i);
153df785aa8SLiviu Dudau 		if (!port)
154df785aa8SLiviu Dudau 			break;
155df785aa8SLiviu Dudau 
156df785aa8SLiviu Dudau 		if (!of_device_is_available(port->parent)) {
157df785aa8SLiviu Dudau 			of_node_put(port);
158df785aa8SLiviu Dudau 			continue;
159df785aa8SLiviu Dudau 		}
160df785aa8SLiviu Dudau 
161df785aa8SLiviu Dudau 		for_each_child_of_node(port, ep) {
162df785aa8SLiviu Dudau 			remote = of_graph_get_remote_port_parent(ep);
163df785aa8SLiviu Dudau 			if (!remote || !of_device_is_available(remote)) {
164df785aa8SLiviu Dudau 				of_node_put(remote);
165df785aa8SLiviu Dudau 				continue;
166df785aa8SLiviu Dudau 			} else if (!of_device_is_available(remote->parent)) {
1674bf99144SRob Herring 				dev_warn(dev, "parent device of %pOF is not available\n",
1684bf99144SRob Herring 					 remote);
169df785aa8SLiviu Dudau 				of_node_put(remote);
170df785aa8SLiviu Dudau 				continue;
171df785aa8SLiviu Dudau 			}
172df785aa8SLiviu Dudau 
17397ac0e47SRussell King 			drm_of_component_match_add(dev, &match, compare_of,
17497ac0e47SRussell King 						   remote);
175df785aa8SLiviu Dudau 			of_node_put(remote);
176df785aa8SLiviu Dudau 		}
177df785aa8SLiviu Dudau 		of_node_put(port);
178df785aa8SLiviu Dudau 	}
179df785aa8SLiviu Dudau 
180df785aa8SLiviu Dudau 	return component_master_add_with_match(dev, m_ops, match);
181df785aa8SLiviu Dudau }
182df785aa8SLiviu Dudau EXPORT_SYMBOL(drm_of_component_probe);
1834cacf91fSPhilipp Zabel 
1844cacf91fSPhilipp Zabel /*
1854cacf91fSPhilipp Zabel  * drm_of_encoder_active_endpoint - return the active encoder endpoint
1864cacf91fSPhilipp Zabel  * @node: device tree node containing encoder input ports
1874cacf91fSPhilipp Zabel  * @encoder: drm_encoder
1884cacf91fSPhilipp Zabel  *
1894cacf91fSPhilipp Zabel  * Given an encoder device node and a drm_encoder with a connected crtc,
1904cacf91fSPhilipp Zabel  * parse the encoder endpoint connecting to the crtc port.
1914cacf91fSPhilipp Zabel  */
1924cacf91fSPhilipp Zabel int drm_of_encoder_active_endpoint(struct device_node *node,
1934cacf91fSPhilipp Zabel 				   struct drm_encoder *encoder,
1944cacf91fSPhilipp Zabel 				   struct of_endpoint *endpoint)
1954cacf91fSPhilipp Zabel {
1964cacf91fSPhilipp Zabel 	struct device_node *ep;
1974cacf91fSPhilipp Zabel 	struct drm_crtc *crtc = encoder->crtc;
1984cacf91fSPhilipp Zabel 	struct device_node *port;
1994cacf91fSPhilipp Zabel 	int ret;
2004cacf91fSPhilipp Zabel 
2014cacf91fSPhilipp Zabel 	if (!node || !crtc)
2024cacf91fSPhilipp Zabel 		return -EINVAL;
2034cacf91fSPhilipp Zabel 
2044cacf91fSPhilipp Zabel 	for_each_endpoint_of_node(node, ep) {
2054cacf91fSPhilipp Zabel 		port = of_graph_get_remote_port(ep);
2064cacf91fSPhilipp Zabel 		of_node_put(port);
2074cacf91fSPhilipp Zabel 		if (port == crtc->port) {
2084cacf91fSPhilipp Zabel 			ret = of_graph_parse_endpoint(ep, endpoint);
2094cacf91fSPhilipp Zabel 			of_node_put(ep);
2104cacf91fSPhilipp Zabel 			return ret;
2114cacf91fSPhilipp Zabel 		}
2124cacf91fSPhilipp Zabel 	}
2134cacf91fSPhilipp Zabel 
2144cacf91fSPhilipp Zabel 	return -EINVAL;
2154cacf91fSPhilipp Zabel }
2164cacf91fSPhilipp Zabel EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
2171f2db303SRob Herring 
2183fbdfe99SDaniel Vetter /**
2191f2db303SRob Herring  * drm_of_find_panel_or_bridge - return connected panel or bridge device
2201f2db303SRob Herring  * @np: device tree node containing encoder output ports
2213fbdfe99SDaniel Vetter  * @port: port in the device tree node
2223fbdfe99SDaniel Vetter  * @endpoint: endpoint in the device tree node
2231f2db303SRob Herring  * @panel: pointer to hold returned drm_panel
2241f2db303SRob Herring  * @bridge: pointer to hold returned drm_bridge
2251f2db303SRob Herring  *
2261f2db303SRob Herring  * Given a DT node's port and endpoint number, find the connected node and
2271f2db303SRob Herring  * return either the associated struct drm_panel or drm_bridge device. Either
2281f2db303SRob Herring  * @panel or @bridge must not be NULL.
2291f2db303SRob Herring  *
23087ea9580SMaxime Ripard  * This function is deprecated and should not be used in new drivers. Use
23187ea9580SMaxime Ripard  * devm_drm_of_get_bridge() instead.
23287ea9580SMaxime Ripard  *
2331f2db303SRob Herring  * Returns zero if successful, or one of the standard error codes if it fails.
2341f2db303SRob Herring  */
2351f2db303SRob Herring int drm_of_find_panel_or_bridge(const struct device_node *np,
2361f2db303SRob Herring 				int port, int endpoint,
2371f2db303SRob Herring 				struct drm_panel **panel,
2381f2db303SRob Herring 				struct drm_bridge **bridge)
2391f2db303SRob Herring {
240169466d4SBjorn Andersson 	int ret = -EPROBE_DEFER;
241169466d4SBjorn Andersson 	struct device_node *remote;
2421f2db303SRob Herring 
2431f2db303SRob Herring 	if (!panel && !bridge)
2441f2db303SRob Herring 		return -EINVAL;
245320e421eSDan Carpenter 	if (panel)
246320e421eSDan Carpenter 		*panel = NULL;
2471f2db303SRob Herring 
248169466d4SBjorn Andersson 	/*
249169466d4SBjorn Andersson 	 * of_graph_get_remote_node() produces a noisy error message if port
250169466d4SBjorn Andersson 	 * node isn't found and the absence of the port is a legit case here,
251169466d4SBjorn Andersson 	 * so at first we silently check whether graph presents in the
252169466d4SBjorn Andersson 	 * device-tree node.
253169466d4SBjorn Andersson 	 */
254169466d4SBjorn Andersson 	if (!of_graph_is_present(np))
255169466d4SBjorn Andersson 		return -ENODEV;
256169466d4SBjorn Andersson 
257169466d4SBjorn Andersson 	remote = of_graph_get_remote_node(np, port, endpoint);
258169466d4SBjorn Andersson 	if (!remote)
259169466d4SBjorn Andersson 		return -ENODEV;
260169466d4SBjorn Andersson 
261169466d4SBjorn Andersson 	if (panel) {
262169466d4SBjorn Andersson 		*panel = of_drm_find_panel(remote);
263169466d4SBjorn Andersson 		if (!IS_ERR(*panel))
264169466d4SBjorn Andersson 			ret = 0;
265169466d4SBjorn Andersson 		else
266169466d4SBjorn Andersson 			*panel = NULL;
267169466d4SBjorn Andersson 	}
268169466d4SBjorn Andersson 
269169466d4SBjorn Andersson 	/* No panel found yet, check for a bridge next. */
270169466d4SBjorn Andersson 	if (bridge) {
271169466d4SBjorn Andersson 		if (ret) {
272169466d4SBjorn Andersson 			*bridge = of_drm_find_bridge(remote);
273169466d4SBjorn Andersson 			if (*bridge)
274169466d4SBjorn Andersson 				ret = 0;
275169466d4SBjorn Andersson 		} else {
276169466d4SBjorn Andersson 			*bridge = NULL;
277169466d4SBjorn Andersson 		}
278169466d4SBjorn Andersson 
279169466d4SBjorn Andersson 	}
280169466d4SBjorn Andersson 
281169466d4SBjorn Andersson 	of_node_put(remote);
282169466d4SBjorn Andersson 	return ret;
2831f2db303SRob Herring }
2841f2db303SRob Herring EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);
28565290075SFabrizio Castro 
28665290075SFabrizio Castro enum drm_of_lvds_pixels {
28765290075SFabrizio Castro 	DRM_OF_LVDS_EVEN = BIT(0),
28865290075SFabrizio Castro 	DRM_OF_LVDS_ODD = BIT(1),
28965290075SFabrizio Castro };
29065290075SFabrizio Castro 
29165290075SFabrizio Castro static int drm_of_lvds_get_port_pixels_type(struct device_node *port_node)
29265290075SFabrizio Castro {
29365290075SFabrizio Castro 	bool even_pixels =
29465290075SFabrizio Castro 		of_property_read_bool(port_node, "dual-lvds-even-pixels");
29565290075SFabrizio Castro 	bool odd_pixels =
29665290075SFabrizio Castro 		of_property_read_bool(port_node, "dual-lvds-odd-pixels");
29765290075SFabrizio Castro 
29865290075SFabrizio Castro 	return (even_pixels ? DRM_OF_LVDS_EVEN : 0) |
29965290075SFabrizio Castro 	       (odd_pixels ? DRM_OF_LVDS_ODD : 0);
30065290075SFabrizio Castro }
30165290075SFabrizio Castro 
30265290075SFabrizio Castro static int drm_of_lvds_get_remote_pixels_type(
30365290075SFabrizio Castro 			const struct device_node *port_node)
30465290075SFabrizio Castro {
30565290075SFabrizio Castro 	struct device_node *endpoint = NULL;
30665290075SFabrizio Castro 	int pixels_type = -EPIPE;
30765290075SFabrizio Castro 
30865290075SFabrizio Castro 	for_each_child_of_node(port_node, endpoint) {
30965290075SFabrizio Castro 		struct device_node *remote_port;
31065290075SFabrizio Castro 		int current_pt;
31165290075SFabrizio Castro 
31265290075SFabrizio Castro 		if (!of_node_name_eq(endpoint, "endpoint"))
31365290075SFabrizio Castro 			continue;
31465290075SFabrizio Castro 
31565290075SFabrizio Castro 		remote_port = of_graph_get_remote_port(endpoint);
31665290075SFabrizio Castro 		if (!remote_port) {
317b557a5f8SJulia Lawall 			of_node_put(endpoint);
31865290075SFabrizio Castro 			return -EPIPE;
31965290075SFabrizio Castro 		}
32065290075SFabrizio Castro 
32165290075SFabrizio Castro 		current_pt = drm_of_lvds_get_port_pixels_type(remote_port);
32265290075SFabrizio Castro 		of_node_put(remote_port);
32365290075SFabrizio Castro 		if (pixels_type < 0)
32465290075SFabrizio Castro 			pixels_type = current_pt;
32565290075SFabrizio Castro 
32665290075SFabrizio Castro 		/*
32765290075SFabrizio Castro 		 * Sanity check, ensure that all remote endpoints have the same
32865290075SFabrizio Castro 		 * pixel type. We may lift this restriction later if we need to
32965290075SFabrizio Castro 		 * support multiple sinks with different dual-link
33065290075SFabrizio Castro 		 * configurations by passing the endpoints explicitly to
33165290075SFabrizio Castro 		 * drm_of_lvds_get_dual_link_pixel_order().
33265290075SFabrizio Castro 		 */
3336f9223a5SSteven Price 		if (!current_pt || pixels_type != current_pt) {
3346f9223a5SSteven Price 			of_node_put(endpoint);
33565290075SFabrizio Castro 			return -EINVAL;
33665290075SFabrizio Castro 		}
3376f9223a5SSteven Price 	}
33865290075SFabrizio Castro 
33965290075SFabrizio Castro 	return pixels_type;
34065290075SFabrizio Castro }
34165290075SFabrizio Castro 
34265290075SFabrizio Castro /**
34365290075SFabrizio Castro  * drm_of_lvds_get_dual_link_pixel_order - Get LVDS dual-link pixel order
34465290075SFabrizio Castro  * @port1: First DT port node of the Dual-link LVDS source
34565290075SFabrizio Castro  * @port2: Second DT port node of the Dual-link LVDS source
34665290075SFabrizio Castro  *
34765290075SFabrizio Castro  * An LVDS dual-link connection is made of two links, with even pixels
34865290075SFabrizio Castro  * transitting on one link, and odd pixels on the other link. This function
34965290075SFabrizio Castro  * returns, for two ports of an LVDS dual-link source, which port shall transmit
35065290075SFabrizio Castro  * the even and odd pixels, based on the requirements of the connected sink.
35165290075SFabrizio Castro  *
35265290075SFabrizio Castro  * The pixel order is determined from the dual-lvds-even-pixels and
35365290075SFabrizio Castro  * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
35465290075SFabrizio Castro  * properties are not present, or if their usage is not valid, this function
35565290075SFabrizio Castro  * returns -EINVAL.
35665290075SFabrizio Castro  *
35765290075SFabrizio Castro  * If either port is not connected, this function returns -EPIPE.
35865290075SFabrizio Castro  *
35965290075SFabrizio Castro  * @port1 and @port2 are typically DT sibling nodes, but may have different
36065290075SFabrizio Castro  * parents when, for instance, two separate LVDS encoders carry the even and odd
36165290075SFabrizio Castro  * pixels.
36265290075SFabrizio Castro  *
36365290075SFabrizio Castro  * Return:
36465290075SFabrizio Castro  * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 carries even pixels and @port2
36565290075SFabrizio Castro  *   carries odd pixels
36665290075SFabrizio Castro  * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 carries odd pixels and @port2
36765290075SFabrizio Castro  *   carries even pixels
36865290075SFabrizio Castro  * * -EINVAL - @port1 and @port2 are not connected to a dual-link LVDS sink, or
36965290075SFabrizio Castro  *   the sink configuration is invalid
37065290075SFabrizio Castro  * * -EPIPE - when @port1 or @port2 are not connected
37165290075SFabrizio Castro  */
37265290075SFabrizio Castro int drm_of_lvds_get_dual_link_pixel_order(const struct device_node *port1,
37365290075SFabrizio Castro 					  const struct device_node *port2)
37465290075SFabrizio Castro {
37565290075SFabrizio Castro 	int remote_p1_pt, remote_p2_pt;
37665290075SFabrizio Castro 
37765290075SFabrizio Castro 	if (!port1 || !port2)
37865290075SFabrizio Castro 		return -EINVAL;
37965290075SFabrizio Castro 
38065290075SFabrizio Castro 	remote_p1_pt = drm_of_lvds_get_remote_pixels_type(port1);
38165290075SFabrizio Castro 	if (remote_p1_pt < 0)
38265290075SFabrizio Castro 		return remote_p1_pt;
38365290075SFabrizio Castro 
38465290075SFabrizio Castro 	remote_p2_pt = drm_of_lvds_get_remote_pixels_type(port2);
38565290075SFabrizio Castro 	if (remote_p2_pt < 0)
38665290075SFabrizio Castro 		return remote_p2_pt;
38765290075SFabrizio Castro 
38865290075SFabrizio Castro 	/*
38965290075SFabrizio Castro 	 * A valid dual-lVDS bus is found when one remote port is marked with
39065290075SFabrizio Castro 	 * "dual-lvds-even-pixels", and the other remote port is marked with
39165290075SFabrizio Castro 	 * "dual-lvds-odd-pixels", bail out if the markers are not right.
39265290075SFabrizio Castro 	 */
39365290075SFabrizio Castro 	if (remote_p1_pt + remote_p2_pt != DRM_OF_LVDS_EVEN + DRM_OF_LVDS_ODD)
39465290075SFabrizio Castro 		return -EINVAL;
39565290075SFabrizio Castro 
39665290075SFabrizio Castro 	return remote_p1_pt == DRM_OF_LVDS_EVEN ?
39765290075SFabrizio Castro 		DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS :
39865290075SFabrizio Castro 		DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS;
39965290075SFabrizio Castro }
40065290075SFabrizio Castro EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order);
4017c4dd0a2SMarek Vasut 
4027c4dd0a2SMarek Vasut /**
4037c4dd0a2SMarek Vasut  * drm_of_lvds_get_data_mapping - Get LVDS data mapping
4047c4dd0a2SMarek Vasut  * @port: DT port node of the LVDS source or sink
4057c4dd0a2SMarek Vasut  *
4067c4dd0a2SMarek Vasut  * Convert DT "data-mapping" property string value into media bus format value.
4077c4dd0a2SMarek Vasut  *
4087c4dd0a2SMarek Vasut  * Return:
4097c4dd0a2SMarek Vasut  * * MEDIA_BUS_FMT_RGB666_1X7X3_SPWG - data-mapping is "jeida-18"
4107c4dd0a2SMarek Vasut  * * MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA - data-mapping is "jeida-24"
4117c4dd0a2SMarek Vasut  * * MEDIA_BUS_FMT_RGB888_1X7X4_SPWG - data-mapping is "vesa-24"
4127c4dd0a2SMarek Vasut  * * -EINVAL - the "data-mapping" property is unsupported
4137c4dd0a2SMarek Vasut  * * -ENODEV - the "data-mapping" property is missing
4147c4dd0a2SMarek Vasut  */
4157c4dd0a2SMarek Vasut int drm_of_lvds_get_data_mapping(const struct device_node *port)
4167c4dd0a2SMarek Vasut {
4177c4dd0a2SMarek Vasut 	const char *mapping;
4187c4dd0a2SMarek Vasut 	int ret;
4197c4dd0a2SMarek Vasut 
4207c4dd0a2SMarek Vasut 	ret = of_property_read_string(port, "data-mapping", &mapping);
4217c4dd0a2SMarek Vasut 	if (ret < 0)
4227c4dd0a2SMarek Vasut 		return -ENODEV;
4237c4dd0a2SMarek Vasut 
4247c4dd0a2SMarek Vasut 	if (!strcmp(mapping, "jeida-18"))
4257c4dd0a2SMarek Vasut 		return MEDIA_BUS_FMT_RGB666_1X7X3_SPWG;
4267c4dd0a2SMarek Vasut 	if (!strcmp(mapping, "jeida-24"))
4277c4dd0a2SMarek Vasut 		return MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA;
4287c4dd0a2SMarek Vasut 	if (!strcmp(mapping, "vesa-24"))
4297c4dd0a2SMarek Vasut 		return MEDIA_BUS_FMT_RGB888_1X7X4_SPWG;
4307c4dd0a2SMarek Vasut 
4317c4dd0a2SMarek Vasut 	return -EINVAL;
4327c4dd0a2SMarek Vasut }
4337c4dd0a2SMarek Vasut EXPORT_SYMBOL_GPL(drm_of_lvds_get_data_mapping);
434fc801750SMarek Vasut 
435fc801750SMarek Vasut /**
436fc801750SMarek Vasut  * drm_of_get_data_lanes_count - Get DSI/(e)DP data lane count
437fc801750SMarek Vasut  * @endpoint: DT endpoint node of the DSI/(e)DP source or sink
438fc801750SMarek Vasut  * @min: minimum supported number of data lanes
439fc801750SMarek Vasut  * @max: maximum supported number of data lanes
440fc801750SMarek Vasut  *
441fc801750SMarek Vasut  * Count DT "data-lanes" property elements and check for validity.
442fc801750SMarek Vasut  *
443fc801750SMarek Vasut  * Return:
444fc801750SMarek Vasut  * * min..max - positive integer count of "data-lanes" elements
445fc801750SMarek Vasut  * * -ve - the "data-lanes" property is missing or invalid
446fc801750SMarek Vasut  * * -EINVAL - the "data-lanes" property is unsupported
447fc801750SMarek Vasut  */
448fc801750SMarek Vasut int drm_of_get_data_lanes_count(const struct device_node *endpoint,
449fc801750SMarek Vasut 				const unsigned int min, const unsigned int max)
450fc801750SMarek Vasut {
451fc801750SMarek Vasut 	int ret;
452fc801750SMarek Vasut 
453fc801750SMarek Vasut 	ret = of_property_count_u32_elems(endpoint, "data-lanes");
454fc801750SMarek Vasut 	if (ret < 0)
455fc801750SMarek Vasut 		return ret;
456fc801750SMarek Vasut 
457fc801750SMarek Vasut 	if (ret < min || ret > max)
458fc801750SMarek Vasut 		return -EINVAL;
459fc801750SMarek Vasut 
460fc801750SMarek Vasut 	return ret;
461fc801750SMarek Vasut }
462fc801750SMarek Vasut EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count);
463fc801750SMarek Vasut 
464fc801750SMarek Vasut /**
465fc801750SMarek Vasut  * drm_of_get_data_lanes_count_ep - Get DSI/(e)DP data lane count by endpoint
466fc801750SMarek Vasut  * @port: DT port node of the DSI/(e)DP source or sink
467fc801750SMarek Vasut  * @port_reg: identifier (value of reg property) of the parent port node
468fc801750SMarek Vasut  * @reg: identifier (value of reg property) of the endpoint node
469fc801750SMarek Vasut  * @min: minimum supported number of data lanes
470fc801750SMarek Vasut  * @max: maximum supported number of data lanes
471fc801750SMarek Vasut  *
472fc801750SMarek Vasut  * Count DT "data-lanes" property elements and check for validity.
473fc801750SMarek Vasut  * This variant uses endpoint specifier.
474fc801750SMarek Vasut  *
475fc801750SMarek Vasut  * Return:
476fc801750SMarek Vasut  * * min..max - positive integer count of "data-lanes" elements
477fc801750SMarek Vasut  * * -EINVAL - the "data-mapping" property is unsupported
478fc801750SMarek Vasut  * * -ENODEV - the "data-mapping" property is missing
479fc801750SMarek Vasut  */
480fc801750SMarek Vasut int drm_of_get_data_lanes_count_ep(const struct device_node *port,
481fc801750SMarek Vasut 				   int port_reg, int reg,
482fc801750SMarek Vasut 				   const unsigned int min,
483fc801750SMarek Vasut 				   const unsigned int max)
484fc801750SMarek Vasut {
485fc801750SMarek Vasut 	struct device_node *endpoint;
486fc801750SMarek Vasut 	int ret;
487fc801750SMarek Vasut 
488fc801750SMarek Vasut 	endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg);
489fc801750SMarek Vasut 	ret = drm_of_get_data_lanes_count(endpoint, min, max);
490fc801750SMarek Vasut 	of_node_put(endpoint);
491fc801750SMarek Vasut 
492fc801750SMarek Vasut 	return ret;
493fc801750SMarek Vasut }
494fc801750SMarek Vasut EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_ep);
495