xref: /linux/drivers/gpu/drm/drm_of.c (revision 97ac0e47aed5f635893b0e2df634c64b38ca7541)
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>
67e435aadSRussell King #include <drm/drm_crtc.h>
77e435aadSRussell King #include <drm/drm_of.h>
87e435aadSRussell King 
9*97ac0e47SRussell King static void drm_release_of(struct device *dev, void *data)
10*97ac0e47SRussell King {
11*97ac0e47SRussell King 	of_node_put(data);
12*97ac0e47SRussell King }
13*97ac0e47SRussell King 
147e435aadSRussell King /**
157e435aadSRussell King  * drm_crtc_port_mask - find the mask of a registered CRTC by port OF node
167e435aadSRussell King  * @dev: DRM device
177e435aadSRussell King  * @port: port OF node
187e435aadSRussell King  *
197e435aadSRussell King  * Given a port OF node, return the possible mask of the corresponding
207e435aadSRussell King  * CRTC within a device's list of CRTCs.  Returns zero if not found.
217e435aadSRussell King  */
227e435aadSRussell King static uint32_t drm_crtc_port_mask(struct drm_device *dev,
237e435aadSRussell King 				   struct device_node *port)
247e435aadSRussell King {
257e435aadSRussell King 	unsigned int index = 0;
267e435aadSRussell King 	struct drm_crtc *tmp;
277e435aadSRussell King 
286295d607SDaniel Vetter 	drm_for_each_crtc(tmp, dev) {
297e435aadSRussell King 		if (tmp->port == port)
307e435aadSRussell King 			return 1 << index;
317e435aadSRussell King 
327e435aadSRussell King 		index++;
337e435aadSRussell King 	}
347e435aadSRussell King 
357e435aadSRussell King 	return 0;
367e435aadSRussell King }
377e435aadSRussell King 
387e435aadSRussell King /**
397e435aadSRussell King  * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
407e435aadSRussell King  * @dev: DRM device
417e435aadSRussell King  * @port: encoder port to scan for endpoints
427e435aadSRussell King  *
437e435aadSRussell King  * Scan all endpoints attached to a port, locate their attached CRTCs,
447e435aadSRussell King  * and generate the DRM mask of CRTCs which may be attached to this
457e435aadSRussell King  * encoder.
467e435aadSRussell King  *
477e435aadSRussell King  * See Documentation/devicetree/bindings/graph.txt for the bindings.
487e435aadSRussell King  */
497e435aadSRussell King uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
507e435aadSRussell King 				    struct device_node *port)
517e435aadSRussell King {
527416f4e3SPhilipp Zabel 	struct device_node *remote_port, *ep;
537e435aadSRussell King 	uint32_t possible_crtcs = 0;
547e435aadSRussell King 
557416f4e3SPhilipp Zabel 	for_each_endpoint_of_node(port, ep) {
567e435aadSRussell King 		remote_port = of_graph_get_remote_port(ep);
577e435aadSRussell King 		if (!remote_port) {
587e435aadSRussell King 			of_node_put(ep);
597e435aadSRussell King 			return 0;
607e435aadSRussell King 		}
617e435aadSRussell King 
627e435aadSRussell King 		possible_crtcs |= drm_crtc_port_mask(dev, remote_port);
637e435aadSRussell King 
647e435aadSRussell King 		of_node_put(remote_port);
657416f4e3SPhilipp Zabel 	}
667e435aadSRussell King 
677e435aadSRussell King 	return possible_crtcs;
687e435aadSRussell King }
697e435aadSRussell King EXPORT_SYMBOL(drm_of_find_possible_crtcs);
70df785aa8SLiviu Dudau 
71df785aa8SLiviu Dudau /**
72*97ac0e47SRussell King  * drm_of_component_match_add - Add a component helper OF node match rule
73*97ac0e47SRussell King  * @master: master device
74*97ac0e47SRussell King  * @matchptr: component match pointer
75*97ac0e47SRussell King  * @compare: compare function used for matching component
76*97ac0e47SRussell King  * @node: of_node
77*97ac0e47SRussell King  */
78*97ac0e47SRussell King void drm_of_component_match_add(struct device *master,
79*97ac0e47SRussell King 				struct component_match **matchptr,
80*97ac0e47SRussell King 				int (*compare)(struct device *, void *),
81*97ac0e47SRussell King 				struct device_node *node)
82*97ac0e47SRussell King {
83*97ac0e47SRussell King 	of_node_get(node);
84*97ac0e47SRussell King 	component_match_add_release(master, matchptr, drm_release_of,
85*97ac0e47SRussell King 				    compare, node);
86*97ac0e47SRussell King }
87*97ac0e47SRussell King EXPORT_SYMBOL_GPL(drm_of_component_match_add);
88*97ac0e47SRussell King 
89*97ac0e47SRussell King /**
90df785aa8SLiviu Dudau  * drm_of_component_probe - Generic probe function for a component based master
91df785aa8SLiviu Dudau  * @dev: master device containing the OF node
92df785aa8SLiviu Dudau  * @compare_of: compare function used for matching components
93df785aa8SLiviu Dudau  * @master_ops: component master ops to be used
94df785aa8SLiviu Dudau  *
95df785aa8SLiviu Dudau  * Parse the platform device OF node and bind all the components associated
96df785aa8SLiviu Dudau  * with the master. Interface ports are added before the encoders in order to
97df785aa8SLiviu Dudau  * satisfy their .bind requirements
98df785aa8SLiviu Dudau  * See Documentation/devicetree/bindings/graph.txt for the bindings.
99df785aa8SLiviu Dudau  *
100df785aa8SLiviu Dudau  * Returns zero if successful, or one of the standard error codes if it fails.
101df785aa8SLiviu Dudau  */
102df785aa8SLiviu Dudau int drm_of_component_probe(struct device *dev,
103df785aa8SLiviu Dudau 			   int (*compare_of)(struct device *, void *),
104df785aa8SLiviu Dudau 			   const struct component_master_ops *m_ops)
105df785aa8SLiviu Dudau {
106df785aa8SLiviu Dudau 	struct device_node *ep, *port, *remote;
107df785aa8SLiviu Dudau 	struct component_match *match = NULL;
108df785aa8SLiviu Dudau 	int i;
109df785aa8SLiviu Dudau 
110df785aa8SLiviu Dudau 	if (!dev->of_node)
111df785aa8SLiviu Dudau 		return -EINVAL;
112df785aa8SLiviu Dudau 
113df785aa8SLiviu Dudau 	/*
114df785aa8SLiviu Dudau 	 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
115df785aa8SLiviu Dudau 	 * called from encoder's .bind callbacks works as expected
116df785aa8SLiviu Dudau 	 */
117df785aa8SLiviu Dudau 	for (i = 0; ; i++) {
118df785aa8SLiviu Dudau 		port = of_parse_phandle(dev->of_node, "ports", i);
119df785aa8SLiviu Dudau 		if (!port)
120df785aa8SLiviu Dudau 			break;
121df785aa8SLiviu Dudau 
122df785aa8SLiviu Dudau 		if (!of_device_is_available(port->parent)) {
123df785aa8SLiviu Dudau 			of_node_put(port);
124df785aa8SLiviu Dudau 			continue;
125df785aa8SLiviu Dudau 		}
126df785aa8SLiviu Dudau 
127*97ac0e47SRussell King 		drm_of_component_match_add(dev, &match, compare_of, port);
128df785aa8SLiviu Dudau 		of_node_put(port);
129df785aa8SLiviu Dudau 	}
130df785aa8SLiviu Dudau 
131df785aa8SLiviu Dudau 	if (i == 0) {
132df785aa8SLiviu Dudau 		dev_err(dev, "missing 'ports' property\n");
133df785aa8SLiviu Dudau 		return -ENODEV;
134df785aa8SLiviu Dudau 	}
135df785aa8SLiviu Dudau 
136df785aa8SLiviu Dudau 	if (!match) {
137df785aa8SLiviu Dudau 		dev_err(dev, "no available port\n");
138df785aa8SLiviu Dudau 		return -ENODEV;
139df785aa8SLiviu Dudau 	}
140df785aa8SLiviu Dudau 
141df785aa8SLiviu Dudau 	/*
142df785aa8SLiviu Dudau 	 * For bound crtcs, bind the encoders attached to their remote endpoint
143df785aa8SLiviu Dudau 	 */
144df785aa8SLiviu Dudau 	for (i = 0; ; i++) {
145df785aa8SLiviu Dudau 		port = of_parse_phandle(dev->of_node, "ports", i);
146df785aa8SLiviu Dudau 		if (!port)
147df785aa8SLiviu Dudau 			break;
148df785aa8SLiviu Dudau 
149df785aa8SLiviu Dudau 		if (!of_device_is_available(port->parent)) {
150df785aa8SLiviu Dudau 			of_node_put(port);
151df785aa8SLiviu Dudau 			continue;
152df785aa8SLiviu Dudau 		}
153df785aa8SLiviu Dudau 
154df785aa8SLiviu Dudau 		for_each_child_of_node(port, ep) {
155df785aa8SLiviu Dudau 			remote = of_graph_get_remote_port_parent(ep);
156df785aa8SLiviu Dudau 			if (!remote || !of_device_is_available(remote)) {
157df785aa8SLiviu Dudau 				of_node_put(remote);
158df785aa8SLiviu Dudau 				continue;
159df785aa8SLiviu Dudau 			} else if (!of_device_is_available(remote->parent)) {
160df785aa8SLiviu Dudau 				dev_warn(dev, "parent device of %s is not available\n",
161df785aa8SLiviu Dudau 					 remote->full_name);
162df785aa8SLiviu Dudau 				of_node_put(remote);
163df785aa8SLiviu Dudau 				continue;
164df785aa8SLiviu Dudau 			}
165df785aa8SLiviu Dudau 
166*97ac0e47SRussell King 			drm_of_component_match_add(dev, &match, compare_of,
167*97ac0e47SRussell King 						   remote);
168df785aa8SLiviu Dudau 			of_node_put(remote);
169df785aa8SLiviu Dudau 		}
170df785aa8SLiviu Dudau 		of_node_put(port);
171df785aa8SLiviu Dudau 	}
172df785aa8SLiviu Dudau 
173df785aa8SLiviu Dudau 	return component_master_add_with_match(dev, m_ops, match);
174df785aa8SLiviu Dudau }
175df785aa8SLiviu Dudau EXPORT_SYMBOL(drm_of_component_probe);
1764cacf91fSPhilipp Zabel 
1774cacf91fSPhilipp Zabel /*
1784cacf91fSPhilipp Zabel  * drm_of_encoder_active_endpoint - return the active encoder endpoint
1794cacf91fSPhilipp Zabel  * @node: device tree node containing encoder input ports
1804cacf91fSPhilipp Zabel  * @encoder: drm_encoder
1814cacf91fSPhilipp Zabel  *
1824cacf91fSPhilipp Zabel  * Given an encoder device node and a drm_encoder with a connected crtc,
1834cacf91fSPhilipp Zabel  * parse the encoder endpoint connecting to the crtc port.
1844cacf91fSPhilipp Zabel  */
1854cacf91fSPhilipp Zabel int drm_of_encoder_active_endpoint(struct device_node *node,
1864cacf91fSPhilipp Zabel 				   struct drm_encoder *encoder,
1874cacf91fSPhilipp Zabel 				   struct of_endpoint *endpoint)
1884cacf91fSPhilipp Zabel {
1894cacf91fSPhilipp Zabel 	struct device_node *ep;
1904cacf91fSPhilipp Zabel 	struct drm_crtc *crtc = encoder->crtc;
1914cacf91fSPhilipp Zabel 	struct device_node *port;
1924cacf91fSPhilipp Zabel 	int ret;
1934cacf91fSPhilipp Zabel 
1944cacf91fSPhilipp Zabel 	if (!node || !crtc)
1954cacf91fSPhilipp Zabel 		return -EINVAL;
1964cacf91fSPhilipp Zabel 
1974cacf91fSPhilipp Zabel 	for_each_endpoint_of_node(node, ep) {
1984cacf91fSPhilipp Zabel 		port = of_graph_get_remote_port(ep);
1994cacf91fSPhilipp Zabel 		of_node_put(port);
2004cacf91fSPhilipp Zabel 		if (port == crtc->port) {
2014cacf91fSPhilipp Zabel 			ret = of_graph_parse_endpoint(ep, endpoint);
2024cacf91fSPhilipp Zabel 			of_node_put(ep);
2034cacf91fSPhilipp Zabel 			return ret;
2044cacf91fSPhilipp Zabel 		}
2054cacf91fSPhilipp Zabel 	}
2064cacf91fSPhilipp Zabel 
2074cacf91fSPhilipp Zabel 	return -EINVAL;
2084cacf91fSPhilipp Zabel }
2094cacf91fSPhilipp Zabel EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
210