]>
Commit | Line | Data |
---|---|---|
ca50c197 SA |
1 | /* |
2 | * V4L2 fwnode binding parsing library | |
3 | * | |
4 | * The origins of the V4L2 fwnode library are in V4L2 OF library that | |
5 | * formerly was located in v4l2-of.c. | |
6 | * | |
7 | * Copyright (c) 2016 Intel Corporation. | |
8 | * Author: Sakari Ailus <sakari.ailus@linux.intel.com> | |
9 | * | |
10 | * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd. | |
11 | * Author: Sylwester Nawrocki <s.nawrocki@samsung.com> | |
12 | * | |
13 | * Copyright (C) 2012 Renesas Electronics Corp. | |
14 | * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de> | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or modify | |
17 | * it under the terms of version 2 of the GNU General Public License as | |
18 | * published by the Free Software Foundation. | |
19 | */ | |
20 | #include <linux/acpi.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/of.h> | |
24 | #include <linux/property.h> | |
25 | #include <linux/slab.h> | |
26 | #include <linux/string.h> | |
27 | #include <linux/types.h> | |
28 | ||
29 | #include <media/v4l2-fwnode.h> | |
30 | ||
e07a41f9 SA |
31 | enum v4l2_fwnode_bus_type { |
32 | V4L2_FWNODE_BUS_TYPE_GUESS = 0, | |
33 | V4L2_FWNODE_BUS_TYPE_CSI2_CPHY, | |
34 | V4L2_FWNODE_BUS_TYPE_CSI1, | |
35 | V4L2_FWNODE_BUS_TYPE_CCP2, | |
36 | NR_OF_V4L2_FWNODE_BUS_TYPE, | |
37 | }; | |
38 | ||
f3112735 SA |
39 | static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode, |
40 | struct v4l2_fwnode_endpoint *vep) | |
ca50c197 SA |
41 | { |
42 | struct v4l2_fwnode_bus_mipi_csi2 *bus = &vep->bus.mipi_csi2; | |
43 | bool have_clk_lane = false; | |
44 | unsigned int flags = 0, lanes_used = 0; | |
45 | unsigned int i; | |
46 | u32 v; | |
47 | int rval; | |
48 | ||
49 | rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0); | |
50 | if (rval > 0) { | |
ad3cdf3e | 51 | u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES]; |
ca50c197 | 52 | |
ad3cdf3e SA |
53 | bus->num_data_lanes = |
54 | min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval); | |
ca50c197 SA |
55 | |
56 | fwnode_property_read_u32_array(fwnode, "data-lanes", array, | |
57 | bus->num_data_lanes); | |
58 | ||
59 | for (i = 0; i < bus->num_data_lanes; i++) { | |
60 | if (lanes_used & BIT(array[i])) | |
61 | pr_warn("duplicated lane %u in data-lanes\n", | |
62 | array[i]); | |
63 | lanes_used |= BIT(array[i]); | |
64 | ||
65 | bus->data_lanes[i] = array[i]; | |
66 | } | |
ca50c197 | 67 | |
4ee23621 | 68 | rval = fwnode_property_read_u32_array(fwnode, |
b24f0215 SA |
69 | "lane-polarities", NULL, |
70 | 0); | |
4ee23621 | 71 | if (rval > 0) { |
b24f0215 | 72 | if (rval != 1 + bus->num_data_lanes /* clock+data */) { |
4ee23621 MCC |
73 | pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n", |
74 | 1 + bus->num_data_lanes, rval); | |
75 | return -EINVAL; | |
76 | } | |
ca50c197 | 77 | |
b24f0215 SA |
78 | fwnode_property_read_u32_array(fwnode, |
79 | "lane-polarities", array, | |
80 | 1 + bus->num_data_lanes); | |
ca50c197 | 81 | |
4ee23621 MCC |
82 | for (i = 0; i < 1 + bus->num_data_lanes; i++) |
83 | bus->lane_polarities[i] = array[i]; | |
84 | } | |
b24f0215 | 85 | |
ca50c197 SA |
86 | } |
87 | ||
88 | if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) { | |
89 | if (lanes_used & BIT(v)) | |
90 | pr_warn("duplicated lane %u in clock-lanes\n", v); | |
91 | lanes_used |= BIT(v); | |
92 | ||
93 | bus->clock_lane = v; | |
94 | have_clk_lane = true; | |
95 | } | |
96 | ||
97 | if (fwnode_property_present(fwnode, "clock-noncontinuous")) | |
98 | flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK; | |
99 | else if (have_clk_lane || bus->num_data_lanes > 0) | |
100 | flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK; | |
101 | ||
102 | bus->flags = flags; | |
103 | vep->bus_type = V4L2_MBUS_CSI2; | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | static void v4l2_fwnode_endpoint_parse_parallel_bus( | |
109 | struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep) | |
110 | { | |
111 | struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel; | |
112 | unsigned int flags = 0; | |
113 | u32 v; | |
114 | ||
115 | if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) | |
116 | flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH : | |
117 | V4L2_MBUS_HSYNC_ACTIVE_LOW; | |
118 | ||
119 | if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) | |
120 | flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH : | |
121 | V4L2_MBUS_VSYNC_ACTIVE_LOW; | |
122 | ||
123 | if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) | |
124 | flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH : | |
125 | V4L2_MBUS_FIELD_EVEN_LOW; | |
126 | if (flags) | |
127 | vep->bus_type = V4L2_MBUS_PARALLEL; | |
128 | else | |
129 | vep->bus_type = V4L2_MBUS_BT656; | |
130 | ||
131 | if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) | |
132 | flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING : | |
133 | V4L2_MBUS_PCLK_SAMPLE_FALLING; | |
134 | ||
135 | if (!fwnode_property_read_u32(fwnode, "data-active", &v)) | |
136 | flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH : | |
137 | V4L2_MBUS_DATA_ACTIVE_LOW; | |
138 | ||
139 | if (fwnode_property_present(fwnode, "slave-mode")) | |
140 | flags |= V4L2_MBUS_SLAVE; | |
141 | else | |
142 | flags |= V4L2_MBUS_MASTER; | |
143 | ||
144 | if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) | |
145 | bus->bus_width = v; | |
146 | ||
147 | if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) | |
148 | bus->data_shift = v; | |
149 | ||
150 | if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) | |
151 | flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH : | |
152 | V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW; | |
153 | ||
154 | bus->flags = flags; | |
155 | ||
156 | } | |
157 | ||
abc5b2cb MCC |
158 | static void |
159 | v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode, | |
160 | struct v4l2_fwnode_endpoint *vep, | |
161 | u32 bus_type) | |
97bbdf02 SA |
162 | { |
163 | struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1; | |
164 | u32 v; | |
165 | ||
166 | if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) | |
167 | bus->clock_inv = v; | |
168 | ||
169 | if (!fwnode_property_read_u32(fwnode, "strobe", &v)) | |
170 | bus->strobe = v; | |
171 | ||
172 | if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) | |
173 | bus->data_lane = v; | |
174 | ||
175 | if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) | |
176 | bus->clock_lane = v; | |
177 | ||
178 | if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2) | |
179 | vep->bus_type = V4L2_MBUS_CCP2; | |
180 | else | |
181 | vep->bus_type = V4L2_MBUS_CSI1; | |
182 | } | |
183 | ||
ca50c197 SA |
184 | /** |
185 | * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties | |
186 | * @fwnode: pointer to the endpoint's fwnode handle | |
187 | * @vep: pointer to the V4L2 fwnode data structure | |
188 | * | |
189 | * All properties are optional. If none are found, we don't set any flags. This | |
190 | * means the port has a static configuration and no properties have to be | |
191 | * specified explicitly. If any properties that identify the bus as parallel | |
192 | * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if | |
193 | * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we | |
194 | * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a | |
195 | * reference to @fwnode. | |
196 | * | |
197 | * NOTE: This function does not parse properties the size of which is variable | |
198 | * without a low fixed limit. Please use v4l2_fwnode_endpoint_alloc_parse() in | |
199 | * new drivers instead. | |
200 | * | |
201 | * Return: 0 on success or a negative error code on failure. | |
202 | */ | |
203 | int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode, | |
204 | struct v4l2_fwnode_endpoint *vep) | |
205 | { | |
e07a41f9 | 206 | u32 bus_type = 0; |
ca50c197 SA |
207 | int rval; |
208 | ||
209 | fwnode_graph_parse_endpoint(fwnode, &vep->base); | |
210 | ||
211 | /* Zero fields from bus_type to until the end */ | |
212 | memset(&vep->bus_type, 0, sizeof(*vep) - | |
213 | offsetof(typeof(*vep), bus_type)); | |
214 | ||
e07a41f9 SA |
215 | fwnode_property_read_u32(fwnode, "bus-type", &bus_type); |
216 | ||
97bbdf02 SA |
217 | switch (bus_type) { |
218 | case V4L2_FWNODE_BUS_TYPE_GUESS: | |
219 | rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep); | |
220 | if (rval) | |
221 | return rval; | |
222 | /* | |
223 | * Parse the parallel video bus properties only if none | |
224 | * of the MIPI CSI-2 specific properties were found. | |
225 | */ | |
226 | if (vep->bus.mipi_csi2.flags == 0) | |
227 | v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep); | |
228 | ||
229 | return 0; | |
230 | case V4L2_FWNODE_BUS_TYPE_CCP2: | |
231 | case V4L2_FWNODE_BUS_TYPE_CSI1: | |
232 | v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type); | |
233 | ||
234 | return 0; | |
235 | default: | |
236 | pr_warn("unsupported bus type %u\n", bus_type); | |
237 | return -EINVAL; | |
238 | } | |
ca50c197 SA |
239 | } |
240 | EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse); | |
241 | ||
242 | /* | |
243 | * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by | |
244 | * v4l2_fwnode_endpoint_alloc_parse() | |
245 | * @vep - the V4L2 fwnode the resources of which are to be released | |
246 | * | |
247 | * It is safe to call this function with NULL argument or on a V4L2 fwnode the | |
248 | * parsing of which failed. | |
249 | */ | |
250 | void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep) | |
251 | { | |
252 | if (IS_ERR_OR_NULL(vep)) | |
253 | return; | |
254 | ||
255 | kfree(vep->link_frequencies); | |
256 | kfree(vep); | |
257 | } | |
258 | EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free); | |
259 | ||
260 | /** | |
261 | * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties | |
262 | * @fwnode: pointer to the endpoint's fwnode handle | |
263 | * | |
264 | * All properties are optional. If none are found, we don't set any flags. This | |
265 | * means the port has a static configuration and no properties have to be | |
266 | * specified explicitly. If any properties that identify the bus as parallel | |
267 | * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if | |
268 | * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we | |
269 | * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a | |
270 | * reference to @fwnode. | |
271 | * | |
272 | * v4l2_fwnode_endpoint_alloc_parse() has two important differences to | |
273 | * v4l2_fwnode_endpoint_parse(): | |
274 | * | |
275 | * 1. It also parses variable size data. | |
276 | * | |
277 | * 2. The memory it has allocated to store the variable size data must be freed | |
278 | * using v4l2_fwnode_endpoint_free() when no longer needed. | |
279 | * | |
280 | * Return: Pointer to v4l2_fwnode_endpoint if successful, on an error pointer | |
281 | * on error. | |
282 | */ | |
283 | struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse( | |
284 | struct fwnode_handle *fwnode) | |
285 | { | |
286 | struct v4l2_fwnode_endpoint *vep; | |
287 | int rval; | |
288 | ||
289 | vep = kzalloc(sizeof(*vep), GFP_KERNEL); | |
290 | if (!vep) | |
291 | return ERR_PTR(-ENOMEM); | |
292 | ||
293 | rval = v4l2_fwnode_endpoint_parse(fwnode, vep); | |
294 | if (rval < 0) | |
295 | goto out_err; | |
296 | ||
297 | rval = fwnode_property_read_u64_array(fwnode, "link-frequencies", | |
298 | NULL, 0); | |
06f81520 SA |
299 | if (rval > 0) { |
300 | vep->link_frequencies = | |
301 | kmalloc_array(rval, sizeof(*vep->link_frequencies), | |
302 | GFP_KERNEL); | |
303 | if (!vep->link_frequencies) { | |
304 | rval = -ENOMEM; | |
305 | goto out_err; | |
306 | } | |
ca50c197 | 307 | |
06f81520 | 308 | vep->nr_of_link_frequencies = rval; |
ca50c197 | 309 | |
06f81520 SA |
310 | rval = fwnode_property_read_u64_array( |
311 | fwnode, "link-frequencies", vep->link_frequencies, | |
312 | vep->nr_of_link_frequencies); | |
313 | if (rval < 0) | |
314 | goto out_err; | |
315 | } | |
ca50c197 SA |
316 | |
317 | return vep; | |
318 | ||
319 | out_err: | |
320 | v4l2_fwnode_endpoint_free(vep); | |
321 | return ERR_PTR(rval); | |
322 | } | |
323 | EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse); | |
324 | ||
325 | /** | |
326 | * v4l2_fwnode_endpoint_parse_link() - parse a link between two endpoints | |
327 | * @__fwnode: pointer to the endpoint's fwnode at the local end of the link | |
328 | * @link: pointer to the V4L2 fwnode link data structure | |
329 | * | |
330 | * Fill the link structure with the local and remote nodes and port numbers. | |
331 | * The local_node and remote_node fields are set to point to the local and | |
332 | * remote port's parent nodes respectively (the port parent node being the | |
333 | * parent node of the port node if that node isn't a 'ports' node, or the | |
334 | * grand-parent node of the port node otherwise). | |
335 | * | |
336 | * A reference is taken to both the local and remote nodes, the caller must use | |
337 | * v4l2_fwnode_endpoint_put_link() to drop the references when done with the | |
338 | * link. | |
339 | * | |
340 | * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be | |
341 | * found. | |
342 | */ | |
343 | int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode, | |
344 | struct v4l2_fwnode_link *link) | |
345 | { | |
346 | const char *port_prop = is_of_node(__fwnode) ? "reg" : "port"; | |
347 | struct fwnode_handle *fwnode; | |
348 | ||
349 | memset(link, 0, sizeof(*link)); | |
350 | ||
351 | fwnode = fwnode_get_parent(__fwnode); | |
352 | fwnode_property_read_u32(fwnode, port_prop, &link->local_port); | |
353 | fwnode = fwnode_get_next_parent(fwnode); | |
354 | if (is_of_node(fwnode) && | |
355 | of_node_cmp(to_of_node(fwnode)->name, "ports") == 0) | |
356 | fwnode = fwnode_get_next_parent(fwnode); | |
357 | link->local_node = fwnode; | |
358 | ||
359 | fwnode = fwnode_graph_get_remote_endpoint(__fwnode); | |
360 | if (!fwnode) { | |
361 | fwnode_handle_put(fwnode); | |
362 | return -ENOLINK; | |
363 | } | |
364 | ||
365 | fwnode = fwnode_get_parent(fwnode); | |
366 | fwnode_property_read_u32(fwnode, port_prop, &link->remote_port); | |
367 | fwnode = fwnode_get_next_parent(fwnode); | |
368 | if (is_of_node(fwnode) && | |
369 | of_node_cmp(to_of_node(fwnode)->name, "ports") == 0) | |
370 | fwnode = fwnode_get_next_parent(fwnode); | |
371 | link->remote_node = fwnode; | |
372 | ||
373 | return 0; | |
374 | } | |
375 | EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link); | |
376 | ||
377 | /** | |
378 | * v4l2_fwnode_put_link() - drop references to nodes in a link | |
379 | * @link: pointer to the V4L2 fwnode link data structure | |
380 | * | |
381 | * Drop references to the local and remote nodes in the link. This function | |
382 | * must be called on every link parsed with v4l2_fwnode_parse_link(). | |
383 | */ | |
384 | void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link) | |
385 | { | |
386 | fwnode_handle_put(link->local_node); | |
387 | fwnode_handle_put(link->remote_node); | |
388 | } | |
389 | EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link); | |
390 | ||
391 | MODULE_LICENSE("GPL"); | |
392 | MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>"); | |
393 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); | |
394 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); |