1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/string.h>
18#include <linux/types.h>
19
20#include <media/v4l2-of.h>
21
22static void v4l2_of_parse_csi_bus(const struct device_node *node,
23 struct v4l2_of_endpoint *endpoint)
24{
25 struct v4l2_of_bus_mipi_csi2 *bus = &endpoint->bus.mipi_csi2;
26 u32 data_lanes[ARRAY_SIZE(bus->data_lanes)];
27 struct property *prop;
28 bool have_clk_lane = false;
29 unsigned int flags = 0;
30 u32 v;
31
32 prop = of_find_property(node, "data-lanes", NULL);
33 if (prop) {
34 const __be32 *lane = NULL;
35 int i;
36
37 for (i = 0; i < ARRAY_SIZE(data_lanes); i++) {
38 lane = of_prop_next_u32(prop, lane, &data_lanes[i]);
39 if (!lane)
40 break;
41 }
42 bus->num_data_lanes = i;
43 while (i--)
44 bus->data_lanes[i] = data_lanes[i];
45 }
46
47 if (!of_property_read_u32(node, "clock-lanes", &v)) {
48 bus->clock_lane = v;
49 have_clk_lane = true;
50 }
51
52 if (of_get_property(node, "clock-noncontinuous", &v))
53 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
54 else if (have_clk_lane || bus->num_data_lanes > 0)
55 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
56
57 bus->flags = flags;
58 endpoint->bus_type = V4L2_MBUS_CSI2;
59}
60
61static void v4l2_of_parse_parallel_bus(const struct device_node *node,
62 struct v4l2_of_endpoint *endpoint)
63{
64 struct v4l2_of_bus_parallel *bus = &endpoint->bus.parallel;
65 unsigned int flags = 0;
66 u32 v;
67
68 if (!of_property_read_u32(node, "hsync-active", &v))
69 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
70 V4L2_MBUS_HSYNC_ACTIVE_LOW;
71
72 if (!of_property_read_u32(node, "vsync-active", &v))
73 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
74 V4L2_MBUS_VSYNC_ACTIVE_LOW;
75
76 if (!of_property_read_u32(node, "pclk-sample", &v))
77 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
78 V4L2_MBUS_PCLK_SAMPLE_FALLING;
79
80 if (!of_property_read_u32(node, "field-even-active", &v))
81 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
82 V4L2_MBUS_FIELD_EVEN_LOW;
83 if (flags)
84 endpoint->bus_type = V4L2_MBUS_PARALLEL;
85 else
86 endpoint->bus_type = V4L2_MBUS_BT656;
87
88 if (!of_property_read_u32(node, "data-active", &v))
89 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
90 V4L2_MBUS_DATA_ACTIVE_LOW;
91
92 if (of_get_property(node, "slave-mode", &v))
93 flags |= V4L2_MBUS_SLAVE;
94 else
95 flags |= V4L2_MBUS_MASTER;
96
97 if (!of_property_read_u32(node, "bus-width", &v))
98 bus->bus_width = v;
99
100 if (!of_property_read_u32(node, "data-shift", &v))
101 bus->data_shift = v;
102
103 if (!of_property_read_u32(node, "sync-on-green-active", &v))
104 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
105 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
106
107 bus->flags = flags;
108
109}
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125void v4l2_of_parse_endpoint(const struct device_node *node,
126 struct v4l2_of_endpoint *endpoint)
127{
128 struct device_node *port_node = of_get_parent(node);
129
130 memset(endpoint, 0, offsetof(struct v4l2_of_endpoint, head));
131
132 endpoint->local_node = node;
133
134
135
136
137 of_property_read_u32(port_node, "reg", &endpoint->port);
138 of_property_read_u32(node, "reg", &endpoint->id);
139
140 v4l2_of_parse_csi_bus(node, endpoint);
141
142
143
144
145 if (endpoint->bus.mipi_csi2.flags == 0)
146 v4l2_of_parse_parallel_bus(node, endpoint);
147
148 of_node_put(port_node);
149}
150EXPORT_SYMBOL(v4l2_of_parse_endpoint);
151
152
153
154
155
156
157
158
159
160
161struct device_node *v4l2_of_get_next_endpoint(const struct device_node *parent,
162 struct device_node *prev)
163{
164 struct device_node *endpoint;
165 struct device_node *port = NULL;
166
167 if (!parent)
168 return NULL;
169
170 if (!prev) {
171 struct device_node *node;
172
173
174
175
176 node = of_get_child_by_name(parent, "ports");
177 if (node)
178 parent = node;
179
180 port = of_get_child_by_name(parent, "port");
181
182 if (port) {
183
184 endpoint = of_get_next_child(port, NULL);
185 of_node_put(port);
186 } else {
187 endpoint = NULL;
188 }
189
190 if (!endpoint)
191 pr_err("%s(): no endpoint nodes specified for %s\n",
192 __func__, parent->full_name);
193 of_node_put(node);
194 } else {
195 port = of_get_parent(prev);
196 if (!port)
197
198 return NULL;
199
200
201 of_node_get(prev);
202 endpoint = of_get_next_child(port, prev);
203 if (endpoint) {
204 of_node_put(port);
205 return endpoint;
206 }
207
208
209 do {
210 port = of_get_next_child(parent, port);
211 if (!port)
212 return NULL;
213 } while (of_node_cmp(port->name, "port"));
214
215
216 endpoint = of_get_next_child(port, NULL);
217 of_node_put(port);
218 }
219
220 return endpoint;
221}
222EXPORT_SYMBOL(v4l2_of_get_next_endpoint);
223
224
225
226
227
228
229
230
231struct device_node *v4l2_of_get_remote_port_parent(
232 const struct device_node *node)
233{
234 struct device_node *np;
235 unsigned int depth;
236
237
238 np = of_parse_phandle(node, "remote-endpoint", 0);
239
240
241 for (depth = 3; depth && np; depth--) {
242 np = of_get_next_parent(np);
243 if (depth == 2 && of_node_cmp(np->name, "ports"))
244 break;
245 }
246 return np;
247}
248EXPORT_SYMBOL(v4l2_of_get_remote_port_parent);
249
250
251
252
253
254
255
256
257struct device_node *v4l2_of_get_remote_port(const struct device_node *node)
258{
259 struct device_node *np;
260
261
262 np = of_parse_phandle(node, "remote-endpoint", 0);
263 if (!np)
264 return NULL;
265 return of_get_parent(np);
266}
267EXPORT_SYMBOL(v4l2_of_get_remote_port);
268