1 #include <linux/component.h>
2 #include <linux/export.h>
3 #include <linux/list.h>
4 #include <linux/of_graph.h>
6 #include <drm/drm_crtc.h>
7 #include <drm/drm_encoder.h>
8 #include <drm/drm_of.h>
10 static void drm_release_of(struct device *dev, void *data)
16 * drm_crtc_port_mask - find the mask of a registered CRTC by port OF node
20 * Given a port OF node, return the possible mask of the corresponding
21 * CRTC within a device's list of CRTCs. Returns zero if not found.
23 static uint32_t drm_crtc_port_mask(struct drm_device *dev,
24 struct device_node *port)
26 unsigned int index = 0;
29 drm_for_each_crtc(tmp, dev) {
30 if (tmp->port == port)
40 * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
42 * @port: encoder port to scan for endpoints
44 * Scan all endpoints attached to a port, locate their attached CRTCs,
45 * and generate the DRM mask of CRTCs which may be attached to this
48 * See Documentation/devicetree/bindings/graph.txt for the bindings.
50 uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
51 struct device_node *port)
53 struct device_node *remote_port, *ep;
54 uint32_t possible_crtcs = 0;
56 for_each_endpoint_of_node(port, ep) {
57 remote_port = of_graph_get_remote_port(ep);
63 possible_crtcs |= drm_crtc_port_mask(dev, remote_port);
65 of_node_put(remote_port);
68 return possible_crtcs;
70 EXPORT_SYMBOL(drm_of_find_possible_crtcs);
73 * drm_of_component_match_add - Add a component helper OF node match rule
74 * @master: master device
75 * @matchptr: component match pointer
76 * @compare: compare function used for matching component
79 void drm_of_component_match_add(struct device *master,
80 struct component_match **matchptr,
81 int (*compare)(struct device *, void *),
82 struct device_node *node)
85 component_match_add_release(master, matchptr, drm_release_of,
88 EXPORT_SYMBOL_GPL(drm_of_component_match_add);
91 * drm_of_component_probe - Generic probe function for a component based master
92 * @dev: master device containing the OF node
93 * @compare_of: compare function used for matching components
94 * @master_ops: component master ops to be used
96 * Parse the platform device OF node and bind all the components associated
97 * with the master. Interface ports are added before the encoders in order to
98 * satisfy their .bind requirements
99 * See Documentation/devicetree/bindings/graph.txt for the bindings.
101 * Returns zero if successful, or one of the standard error codes if it fails.
103 int drm_of_component_probe(struct device *dev,
104 int (*compare_of)(struct device *, void *),
105 const struct component_master_ops *m_ops)
107 struct device_node *ep, *port, *remote;
108 struct component_match *match = NULL;
115 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
116 * called from encoder's .bind callbacks works as expected
119 port = of_parse_phandle(dev->of_node, "ports", i);
123 if (!of_device_is_available(port->parent)) {
128 drm_of_component_match_add(dev, &match, compare_of, port);
133 dev_err(dev, "missing 'ports' property\n");
138 dev_err(dev, "no available port\n");
143 * For bound crtcs, bind the encoders attached to their remote endpoint
146 port = of_parse_phandle(dev->of_node, "ports", i);
150 if (!of_device_is_available(port->parent)) {
155 for_each_child_of_node(port, ep) {
156 remote = of_graph_get_remote_port_parent(ep);
157 if (!remote || !of_device_is_available(remote)) {
160 } else if (!of_device_is_available(remote->parent)) {
161 dev_warn(dev, "parent device of %s is not available\n",
167 drm_of_component_match_add(dev, &match, compare_of,
174 return component_master_add_with_match(dev, m_ops, match);
176 EXPORT_SYMBOL(drm_of_component_probe);
179 * drm_of_encoder_active_endpoint - return the active encoder endpoint
180 * @node: device tree node containing encoder input ports
181 * @encoder: drm_encoder
183 * Given an encoder device node and a drm_encoder with a connected crtc,
184 * parse the encoder endpoint connecting to the crtc port.
186 int drm_of_encoder_active_endpoint(struct device_node *node,
187 struct drm_encoder *encoder,
188 struct of_endpoint *endpoint)
190 struct device_node *ep;
191 struct drm_crtc *crtc = encoder->crtc;
192 struct device_node *port;
198 for_each_endpoint_of_node(node, ep) {
199 port = of_graph_get_remote_port(ep);
201 if (port == crtc->port) {
202 ret = of_graph_parse_endpoint(ep, endpoint);
210 EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);