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