xref: /linux/drivers/gpu/drm/drm_of.c (revision 3fbdfe99f7523a6e2a177f5c969234f2023b11bc)
1df785aa8SLiviu Dudau #include <linux/component.h>
27e435aadSRussell King #include <linux/export.h>
37e435aadSRussell King #include <linux/list.h>
47e435aadSRussell King #include <linux/of_graph.h>
57e435aadSRussell King #include <drm/drmP.h>
61f2db303SRob Herring #include <drm/drm_bridge.h>
77e435aadSRussell King #include <drm/drm_crtc.h>
89338203cSLaurent Pinchart #include <drm/drm_encoder.h>
91f2db303SRob Herring #include <drm/drm_panel.h>
107e435aadSRussell King #include <drm/drm_of.h>
117e435aadSRussell King 
127f9e7ec9SDaniel Vetter /**
137f9e7ec9SDaniel Vetter  * DOC: overview
147f9e7ec9SDaniel Vetter  *
157f9e7ec9SDaniel Vetter  * A set of helper functions to aid DRM drivers in parsing standard DT
167f9e7ec9SDaniel Vetter  * properties.
177f9e7ec9SDaniel Vetter  */
187f9e7ec9SDaniel Vetter 
1997ac0e47SRussell King static void drm_release_of(struct device *dev, void *data)
2097ac0e47SRussell King {
2197ac0e47SRussell King 	of_node_put(data);
2297ac0e47SRussell King }
2397ac0e47SRussell King 
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  */
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  */
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  */
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);
9597ac0e47SRussell King 	component_match_add_release(master, matchptr, drm_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  */
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  */
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 
220*3fbdfe99SDaniel 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
223*3fbdfe99SDaniel Vetter  * @port: port in the device tree node
224*3fbdfe99SDaniel 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  *
2321f2db303SRob Herring  * Returns zero if successful, or one of the standard error codes if it fails.
2331f2db303SRob Herring  */
2341f2db303SRob Herring int drm_of_find_panel_or_bridge(const struct device_node *np,
2351f2db303SRob Herring 				int port, int endpoint,
2361f2db303SRob Herring 				struct drm_panel **panel,
2371f2db303SRob Herring 				struct drm_bridge **bridge)
2381f2db303SRob Herring {
2391f2db303SRob Herring 	int ret = -EPROBE_DEFER;
2401f2db303SRob Herring 	struct device_node *remote;
2411f2db303SRob Herring 
2421f2db303SRob Herring 	if (!panel && !bridge)
2431f2db303SRob Herring 		return -EINVAL;
244320e421eSDan Carpenter 	if (panel)
245320e421eSDan Carpenter 		*panel = NULL;
2461f2db303SRob Herring 
2471f2db303SRob Herring 	remote = of_graph_get_remote_node(np, port, endpoint);
2481f2db303SRob Herring 	if (!remote)
2491f2db303SRob Herring 		return -ENODEV;
2501f2db303SRob Herring 
2512e64a174SBoris Brezillon 	if (!of_device_is_available(remote)) {
2522e64a174SBoris Brezillon 		of_node_put(remote);
2532e64a174SBoris Brezillon 		return -ENODEV;
2542e64a174SBoris Brezillon 	}
2552e64a174SBoris Brezillon 
2561f2db303SRob Herring 	if (panel) {
2571f2db303SRob Herring 		*panel = of_drm_find_panel(remote);
2585fa8e4a2SBoris Brezillon 		if (!IS_ERR(*panel))
2591f2db303SRob Herring 			ret = 0;
2605fa8e4a2SBoris Brezillon 		else
2615fa8e4a2SBoris Brezillon 			*panel = NULL;
2621f2db303SRob Herring 	}
2631f2db303SRob Herring 
2641f2db303SRob Herring 	/* No panel found yet, check for a bridge next. */
2651f2db303SRob Herring 	if (bridge) {
2661f2db303SRob Herring 		if (ret) {
2671f2db303SRob Herring 			*bridge = of_drm_find_bridge(remote);
2681f2db303SRob Herring 			if (*bridge)
2691f2db303SRob Herring 				ret = 0;
2701f2db303SRob Herring 		} else {
2711f2db303SRob Herring 			*bridge = NULL;
2721f2db303SRob Herring 		}
2731f2db303SRob Herring 
2741f2db303SRob Herring 	}
2751f2db303SRob Herring 
2761f2db303SRob Herring 	of_node_put(remote);
2771f2db303SRob Herring 	return ret;
2781f2db303SRob Herring }
2791f2db303SRob Herring EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);
280