blob: b5f2f0fece99887a6467058f8a2e18b87fdbe58a [file] [log] [blame]
Liviu Dudaudf785aa2015-10-20 10:23:12 +01001#include <linux/component.h>
Russell King7e435aa2014-06-15 11:07:12 +01002#include <linux/export.h>
3#include <linux/list.h>
4#include <linux/of_graph.h>
5#include <drm/drmP.h>
6#include <drm/drm_crtc.h>
Laurent Pinchart93382032016-11-28 20:51:09 +02007#include <drm/drm_encoder.h>
Russell King7e435aa2014-06-15 11:07:12 +01008#include <drm/drm_of.h>
9
Russell King97ac0e42016-10-19 11:28:27 +010010static void drm_release_of(struct device *dev, void *data)
11{
12 of_node_put(data);
13}
14
Russell King7e435aa2014-06-15 11:07:12 +010015/**
16 * drm_crtc_port_mask - find the mask of a registered CRTC by port OF node
17 * @dev: DRM device
18 * @port: port OF node
19 *
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.
22 */
23static uint32_t drm_crtc_port_mask(struct drm_device *dev,
24 struct device_node *port)
25{
26 unsigned int index = 0;
27 struct drm_crtc *tmp;
28
Daniel Vetter6295d602015-07-09 23:44:25 +020029 drm_for_each_crtc(tmp, dev) {
Russell King7e435aa2014-06-15 11:07:12 +010030 if (tmp->port == port)
31 return 1 << index;
32
33 index++;
34 }
35
36 return 0;
37}
38
39/**
40 * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
41 * @dev: DRM device
42 * @port: encoder port to scan for endpoints
43 *
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
46 * encoder.
47 *
48 * See Documentation/devicetree/bindings/graph.txt for the bindings.
49 */
50uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
51 struct device_node *port)
52{
Philipp Zabel7416f4e2014-07-03 22:59:51 +020053 struct device_node *remote_port, *ep;
Russell King7e435aa2014-06-15 11:07:12 +010054 uint32_t possible_crtcs = 0;
55
Philipp Zabel7416f4e2014-07-03 22:59:51 +020056 for_each_endpoint_of_node(port, ep) {
Russell King7e435aa2014-06-15 11:07:12 +010057 remote_port = of_graph_get_remote_port(ep);
58 if (!remote_port) {
59 of_node_put(ep);
60 return 0;
61 }
62
63 possible_crtcs |= drm_crtc_port_mask(dev, remote_port);
64
65 of_node_put(remote_port);
Philipp Zabel7416f4e2014-07-03 22:59:51 +020066 }
Russell King7e435aa2014-06-15 11:07:12 +010067
68 return possible_crtcs;
69}
70EXPORT_SYMBOL(drm_of_find_possible_crtcs);
Liviu Dudaudf785aa2015-10-20 10:23:12 +010071
72/**
Russell King97ac0e42016-10-19 11:28:27 +010073 * 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
77 * @node: of_node
78 */
79void drm_of_component_match_add(struct device *master,
80 struct component_match **matchptr,
81 int (*compare)(struct device *, void *),
82 struct device_node *node)
83{
84 of_node_get(node);
85 component_match_add_release(master, matchptr, drm_release_of,
86 compare, node);
87}
88EXPORT_SYMBOL_GPL(drm_of_component_match_add);
89
90/**
Liviu Dudaudf785aa2015-10-20 10:23:12 +010091 * 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
95 *
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.
100 *
101 * Returns zero if successful, or one of the standard error codes if it fails.
102 */
103int drm_of_component_probe(struct device *dev,
104 int (*compare_of)(struct device *, void *),
105 const struct component_master_ops *m_ops)
106{
107 struct device_node *ep, *port, *remote;
108 struct component_match *match = NULL;
109 int i;
110
111 if (!dev->of_node)
112 return -EINVAL;
113
114 /*
115 * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
116 * called from encoder's .bind callbacks works as expected
117 */
118 for (i = 0; ; i++) {
119 port = of_parse_phandle(dev->of_node, "ports", i);
120 if (!port)
121 break;
122
123 if (!of_device_is_available(port->parent)) {
124 of_node_put(port);
125 continue;
126 }
127
Russell King97ac0e42016-10-19 11:28:27 +0100128 drm_of_component_match_add(dev, &match, compare_of, port);
Liviu Dudaudf785aa2015-10-20 10:23:12 +0100129 of_node_put(port);
130 }
131
132 if (i == 0) {
133 dev_err(dev, "missing 'ports' property\n");
134 return -ENODEV;
135 }
136
137 if (!match) {
138 dev_err(dev, "no available port\n");
139 return -ENODEV;
140 }
141
142 /*
143 * For bound crtcs, bind the encoders attached to their remote endpoint
144 */
145 for (i = 0; ; i++) {
146 port = of_parse_phandle(dev->of_node, "ports", i);
147 if (!port)
148 break;
149
150 if (!of_device_is_available(port->parent)) {
151 of_node_put(port);
152 continue;
153 }
154
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)) {
158 of_node_put(remote);
159 continue;
160 } else if (!of_device_is_available(remote->parent)) {
161 dev_warn(dev, "parent device of %s is not available\n",
162 remote->full_name);
163 of_node_put(remote);
164 continue;
165 }
166
Russell King97ac0e42016-10-19 11:28:27 +0100167 drm_of_component_match_add(dev, &match, compare_of,
168 remote);
Liviu Dudaudf785aa2015-10-20 10:23:12 +0100169 of_node_put(remote);
170 }
171 of_node_put(port);
172 }
173
174 return component_master_add_with_match(dev, m_ops, match);
175}
176EXPORT_SYMBOL(drm_of_component_probe);
Philipp Zabel4cacf912015-02-24 11:34:01 +0100177
178/*
179 * drm_of_encoder_active_endpoint - return the active encoder endpoint
180 * @node: device tree node containing encoder input ports
181 * @encoder: drm_encoder
182 *
183 * Given an encoder device node and a drm_encoder with a connected crtc,
184 * parse the encoder endpoint connecting to the crtc port.
185 */
186int drm_of_encoder_active_endpoint(struct device_node *node,
187 struct drm_encoder *encoder,
188 struct of_endpoint *endpoint)
189{
190 struct device_node *ep;
191 struct drm_crtc *crtc = encoder->crtc;
192 struct device_node *port;
193 int ret;
194
195 if (!node || !crtc)
196 return -EINVAL;
197
198 for_each_endpoint_of_node(node, ep) {
199 port = of_graph_get_remote_port(ep);
200 of_node_put(port);
201 if (port == crtc->port) {
202 ret = of_graph_parse_endpoint(ep, endpoint);
203 of_node_put(ep);
204 return ret;
205 }
206 }
207
208 return -EINVAL;
209}
210EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);