1#include <linux/component.h>
2#include <linux/export.h>
3#include <linux/list.h>
4#include <linux/of_graph.h>
5#include <drm/drmP.h>
6#include <drm/drm_bridge.h>
7#include <drm/drm_crtc.h>
8#include <drm/drm_encoder.h>
9#include <drm/drm_panel.h>
10#include <drm/drm_of.h>
11
12
13
14
15
16
17
18
19static void drm_release_of(struct device *dev, void *data)
20{
21 of_node_put(data);
22}
23
24
25
26
27
28
29
30
31
32uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
33 struct device_node *port)
34{
35 unsigned int index = 0;
36 struct drm_crtc *tmp;
37
38 drm_for_each_crtc(tmp, dev) {
39 if (tmp->port == port)
40 return 1 << index;
41
42 index++;
43 }
44
45 return 0;
46}
47EXPORT_SYMBOL(drm_of_crtc_port_mask);
48
49
50
51
52
53
54
55
56
57
58
59
60uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
61 struct device_node *port)
62{
63 struct device_node *remote_port, *ep;
64 uint32_t possible_crtcs = 0;
65
66 for_each_endpoint_of_node(port, ep) {
67 remote_port = of_graph_get_remote_port(ep);
68 if (!remote_port) {
69 of_node_put(ep);
70 return 0;
71 }
72
73 possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
74
75 of_node_put(remote_port);
76 }
77
78 return possible_crtcs;
79}
80EXPORT_SYMBOL(drm_of_find_possible_crtcs);
81
82
83
84
85
86
87
88
89void drm_of_component_match_add(struct device *master,
90 struct component_match **matchptr,
91 int (*compare)(struct device *, void *),
92 struct device_node *node)
93{
94 of_node_get(node);
95 component_match_add_release(master, matchptr, drm_release_of,
96 compare, node);
97}
98EXPORT_SYMBOL_GPL(drm_of_component_match_add);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113int drm_of_component_probe(struct device *dev,
114 int (*compare_of)(struct device *, void *),
115 const struct component_master_ops *m_ops)
116{
117 struct device_node *ep, *port, *remote;
118 struct component_match *match = NULL;
119 int i;
120
121 if (!dev->of_node)
122 return -EINVAL;
123
124
125
126
127
128 for (i = 0; ; i++) {
129 port = of_parse_phandle(dev->of_node, "ports", i);
130 if (!port)
131 break;
132
133 if (of_device_is_available(port->parent))
134 drm_of_component_match_add(dev, &match, compare_of,
135 port);
136
137 of_node_put(port);
138 }
139
140 if (i == 0) {
141 dev_err(dev, "missing 'ports' property\n");
142 return -ENODEV;
143 }
144
145 if (!match) {
146 dev_err(dev, "no available port\n");
147 return -ENODEV;
148 }
149
150
151
152
153 for (i = 0; ; i++) {
154 port = of_parse_phandle(dev->of_node, "ports", i);
155 if (!port)
156 break;
157
158 if (!of_device_is_available(port->parent)) {
159 of_node_put(port);
160 continue;
161 }
162
163 for_each_child_of_node(port, ep) {
164 remote = of_graph_get_remote_port_parent(ep);
165 if (!remote || !of_device_is_available(remote)) {
166 of_node_put(remote);
167 continue;
168 } else if (!of_device_is_available(remote->parent)) {
169 dev_warn(dev, "parent device of %pOF is not available\n",
170 remote);
171 of_node_put(remote);
172 continue;
173 }
174
175 drm_of_component_match_add(dev, &match, compare_of,
176 remote);
177 of_node_put(remote);
178 }
179 of_node_put(port);
180 }
181
182 return component_master_add_with_match(dev, m_ops, match);
183}
184EXPORT_SYMBOL(drm_of_component_probe);
185
186
187
188
189
190
191
192
193
194int drm_of_encoder_active_endpoint(struct device_node *node,
195 struct drm_encoder *encoder,
196 struct of_endpoint *endpoint)
197{
198 struct device_node *ep;
199 struct drm_crtc *crtc = encoder->crtc;
200 struct device_node *port;
201 int ret;
202
203 if (!node || !crtc)
204 return -EINVAL;
205
206 for_each_endpoint_of_node(node, ep) {
207 port = of_graph_get_remote_port(ep);
208 of_node_put(port);
209 if (port == crtc->port) {
210 ret = of_graph_parse_endpoint(ep, endpoint);
211 of_node_put(ep);
212 return ret;
213 }
214 }
215
216 return -EINVAL;
217}
218EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234int drm_of_find_panel_or_bridge(const struct device_node *np,
235 int port, int endpoint,
236 struct drm_panel **panel,
237 struct drm_bridge **bridge)
238{
239 int ret = -EPROBE_DEFER;
240 struct device_node *remote;
241
242 if (!panel && !bridge)
243 return -EINVAL;
244 if (panel)
245 *panel = NULL;
246
247 remote = of_graph_get_remote_node(np, port, endpoint);
248 if (!remote)
249 return -ENODEV;
250
251 if (!of_device_is_available(remote)) {
252 of_node_put(remote);
253 return -ENODEV;
254 }
255
256 if (panel) {
257 *panel = of_drm_find_panel(remote);
258 if (!IS_ERR(*panel))
259 ret = 0;
260 else
261 *panel = NULL;
262 }
263
264
265 if (bridge) {
266 if (ret) {
267 *bridge = of_drm_find_bridge(remote);
268 if (*bridge)
269 ret = 0;
270 } else {
271 *bridge = NULL;
272 }
273
274 }
275
276 of_node_put(remote);
277 return ret;
278}
279EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);
280