xref: /linux/drivers/gpu/drm/drm_of.c (revision 4cacf91fcb1d7118e93caf9cb6651d7f7b56e58d)
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 
97e435aadSRussell King /**
107e435aadSRussell King  * drm_crtc_port_mask - find the mask of a registered CRTC by port OF node
117e435aadSRussell King  * @dev: DRM device
127e435aadSRussell King  * @port: port OF node
137e435aadSRussell King  *
147e435aadSRussell King  * Given a port OF node, return the possible mask of the corresponding
157e435aadSRussell King  * CRTC within a device's list of CRTCs.  Returns zero if not found.
167e435aadSRussell King  */
177e435aadSRussell King static uint32_t drm_crtc_port_mask(struct drm_device *dev,
187e435aadSRussell King 				   struct device_node *port)
197e435aadSRussell King {
207e435aadSRussell King 	unsigned int index = 0;
217e435aadSRussell King 	struct drm_crtc *tmp;
227e435aadSRussell King 
236295d607SDaniel Vetter 	drm_for_each_crtc(tmp, dev) {
247e435aadSRussell King 		if (tmp->port == port)
257e435aadSRussell King 			return 1 << index;
267e435aadSRussell King 
277e435aadSRussell King 		index++;
287e435aadSRussell King 	}
297e435aadSRussell King 
307e435aadSRussell King 	return 0;
317e435aadSRussell King }
327e435aadSRussell King 
337e435aadSRussell King /**
347e435aadSRussell King  * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
357e435aadSRussell King  * @dev: DRM device
367e435aadSRussell King  * @port: encoder port to scan for endpoints
377e435aadSRussell King  *
387e435aadSRussell King  * Scan all endpoints attached to a port, locate their attached CRTCs,
397e435aadSRussell King  * and generate the DRM mask of CRTCs which may be attached to this
407e435aadSRussell King  * encoder.
417e435aadSRussell King  *
427e435aadSRussell King  * See Documentation/devicetree/bindings/graph.txt for the bindings.
437e435aadSRussell King  */
447e435aadSRussell King uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
457e435aadSRussell King 				    struct device_node *port)
467e435aadSRussell King {
477416f4e3SPhilipp Zabel 	struct device_node *remote_port, *ep;
487e435aadSRussell King 	uint32_t possible_crtcs = 0;
497e435aadSRussell King 
507416f4e3SPhilipp Zabel 	for_each_endpoint_of_node(port, ep) {
517e435aadSRussell King 		remote_port = of_graph_get_remote_port(ep);
527e435aadSRussell King 		if (!remote_port) {
537e435aadSRussell King 			of_node_put(ep);
547e435aadSRussell King 			return 0;
557e435aadSRussell King 		}
567e435aadSRussell King 
577e435aadSRussell King 		possible_crtcs |= drm_crtc_port_mask(dev, remote_port);
587e435aadSRussell King 
597e435aadSRussell King 		of_node_put(remote_port);
607416f4e3SPhilipp Zabel 	}
617e435aadSRussell King 
627e435aadSRussell King 	return possible_crtcs;
637e435aadSRussell King }
647e435aadSRussell King EXPORT_SYMBOL(drm_of_find_possible_crtcs);
65df785aa8SLiviu Dudau 
66df785aa8SLiviu Dudau /**
67df785aa8SLiviu Dudau  * drm_of_component_probe - Generic probe function for a component based master
68df785aa8SLiviu Dudau  * @dev: master device containing the OF node
69df785aa8SLiviu Dudau  * @compare_of: compare function used for matching components
70df785aa8SLiviu Dudau  * @master_ops: component master ops to be used
71df785aa8SLiviu Dudau  *
72df785aa8SLiviu Dudau  * Parse the platform device OF node and bind all the components associated
73df785aa8SLiviu Dudau  * with the master. Interface ports are added before the encoders in order to
74df785aa8SLiviu Dudau  * satisfy their .bind requirements
75df785aa8SLiviu Dudau  * See Documentation/devicetree/bindings/graph.txt for the bindings.
76df785aa8SLiviu Dudau  *
77df785aa8SLiviu Dudau  * Returns zero if successful, or one of the standard error codes if it fails.
78df785aa8SLiviu Dudau  */
79df785aa8SLiviu Dudau int drm_of_component_probe(struct device *dev,
80df785aa8SLiviu Dudau 			   int (*compare_of)(struct device *, void *),
81df785aa8SLiviu Dudau 			   const struct component_master_ops *m_ops)
82df785aa8SLiviu Dudau {
83df785aa8SLiviu Dudau 	struct device_node *ep, *port, *remote;
84df785aa8SLiviu Dudau 	struct component_match *match = NULL;
85df785aa8SLiviu Dudau 	int i;
86df785aa8SLiviu Dudau 
87df785aa8SLiviu Dudau 	if (!dev->of_node)
88df785aa8SLiviu Dudau 		return -EINVAL;
89df785aa8SLiviu Dudau 
90df785aa8SLiviu Dudau 	/*
91df785aa8SLiviu Dudau 	 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
92df785aa8SLiviu Dudau 	 * called from encoder's .bind callbacks works as expected
93df785aa8SLiviu Dudau 	 */
94df785aa8SLiviu Dudau 	for (i = 0; ; i++) {
95df785aa8SLiviu Dudau 		port = of_parse_phandle(dev->of_node, "ports", i);
96df785aa8SLiviu Dudau 		if (!port)
97df785aa8SLiviu Dudau 			break;
98df785aa8SLiviu Dudau 
99df785aa8SLiviu Dudau 		if (!of_device_is_available(port->parent)) {
100df785aa8SLiviu Dudau 			of_node_put(port);
101df785aa8SLiviu Dudau 			continue;
102df785aa8SLiviu Dudau 		}
103df785aa8SLiviu Dudau 
104df785aa8SLiviu Dudau 		component_match_add(dev, &match, compare_of, port);
105df785aa8SLiviu Dudau 		of_node_put(port);
106df785aa8SLiviu Dudau 	}
107df785aa8SLiviu Dudau 
108df785aa8SLiviu Dudau 	if (i == 0) {
109df785aa8SLiviu Dudau 		dev_err(dev, "missing 'ports' property\n");
110df785aa8SLiviu Dudau 		return -ENODEV;
111df785aa8SLiviu Dudau 	}
112df785aa8SLiviu Dudau 
113df785aa8SLiviu Dudau 	if (!match) {
114df785aa8SLiviu Dudau 		dev_err(dev, "no available port\n");
115df785aa8SLiviu Dudau 		return -ENODEV;
116df785aa8SLiviu Dudau 	}
117df785aa8SLiviu Dudau 
118df785aa8SLiviu Dudau 	/*
119df785aa8SLiviu Dudau 	 * For bound crtcs, bind the encoders attached to their remote endpoint
120df785aa8SLiviu Dudau 	 */
121df785aa8SLiviu Dudau 	for (i = 0; ; i++) {
122df785aa8SLiviu Dudau 		port = of_parse_phandle(dev->of_node, "ports", i);
123df785aa8SLiviu Dudau 		if (!port)
124df785aa8SLiviu Dudau 			break;
125df785aa8SLiviu Dudau 
126df785aa8SLiviu Dudau 		if (!of_device_is_available(port->parent)) {
127df785aa8SLiviu Dudau 			of_node_put(port);
128df785aa8SLiviu Dudau 			continue;
129df785aa8SLiviu Dudau 		}
130df785aa8SLiviu Dudau 
131df785aa8SLiviu Dudau 		for_each_child_of_node(port, ep) {
132df785aa8SLiviu Dudau 			remote = of_graph_get_remote_port_parent(ep);
133df785aa8SLiviu Dudau 			if (!remote || !of_device_is_available(remote)) {
134df785aa8SLiviu Dudau 				of_node_put(remote);
135df785aa8SLiviu Dudau 				continue;
136df785aa8SLiviu Dudau 			} else if (!of_device_is_available(remote->parent)) {
137df785aa8SLiviu Dudau 				dev_warn(dev, "parent device of %s is not available\n",
138df785aa8SLiviu Dudau 					 remote->full_name);
139df785aa8SLiviu Dudau 				of_node_put(remote);
140df785aa8SLiviu Dudau 				continue;
141df785aa8SLiviu Dudau 			}
142df785aa8SLiviu Dudau 
143df785aa8SLiviu Dudau 			component_match_add(dev, &match, compare_of, remote);
144df785aa8SLiviu Dudau 			of_node_put(remote);
145df785aa8SLiviu Dudau 		}
146df785aa8SLiviu Dudau 		of_node_put(port);
147df785aa8SLiviu Dudau 	}
148df785aa8SLiviu Dudau 
149df785aa8SLiviu Dudau 	return component_master_add_with_match(dev, m_ops, match);
150df785aa8SLiviu Dudau }
151df785aa8SLiviu Dudau EXPORT_SYMBOL(drm_of_component_probe);
152*4cacf91fSPhilipp Zabel 
153*4cacf91fSPhilipp Zabel /*
154*4cacf91fSPhilipp Zabel  * drm_of_encoder_active_endpoint - return the active encoder endpoint
155*4cacf91fSPhilipp Zabel  * @node: device tree node containing encoder input ports
156*4cacf91fSPhilipp Zabel  * @encoder: drm_encoder
157*4cacf91fSPhilipp Zabel  *
158*4cacf91fSPhilipp Zabel  * Given an encoder device node and a drm_encoder with a connected crtc,
159*4cacf91fSPhilipp Zabel  * parse the encoder endpoint connecting to the crtc port.
160*4cacf91fSPhilipp Zabel  */
161*4cacf91fSPhilipp Zabel int drm_of_encoder_active_endpoint(struct device_node *node,
162*4cacf91fSPhilipp Zabel 				   struct drm_encoder *encoder,
163*4cacf91fSPhilipp Zabel 				   struct of_endpoint *endpoint)
164*4cacf91fSPhilipp Zabel {
165*4cacf91fSPhilipp Zabel 	struct device_node *ep;
166*4cacf91fSPhilipp Zabel 	struct drm_crtc *crtc = encoder->crtc;
167*4cacf91fSPhilipp Zabel 	struct device_node *port;
168*4cacf91fSPhilipp Zabel 	int ret;
169*4cacf91fSPhilipp Zabel 
170*4cacf91fSPhilipp Zabel 	if (!node || !crtc)
171*4cacf91fSPhilipp Zabel 		return -EINVAL;
172*4cacf91fSPhilipp Zabel 
173*4cacf91fSPhilipp Zabel 	for_each_endpoint_of_node(node, ep) {
174*4cacf91fSPhilipp Zabel 		port = of_graph_get_remote_port(ep);
175*4cacf91fSPhilipp Zabel 		of_node_put(port);
176*4cacf91fSPhilipp Zabel 		if (port == crtc->port) {
177*4cacf91fSPhilipp Zabel 			ret = of_graph_parse_endpoint(ep, endpoint);
178*4cacf91fSPhilipp Zabel 			of_node_put(ep);
179*4cacf91fSPhilipp Zabel 			return ret;
180*4cacf91fSPhilipp Zabel 		}
181*4cacf91fSPhilipp Zabel 	}
182*4cacf91fSPhilipp Zabel 
183*4cacf91fSPhilipp Zabel 	return -EINVAL;
184*4cacf91fSPhilipp Zabel }
185*4cacf91fSPhilipp Zabel EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
186