blob: d71dd3913cd96fddd6e4398e4222fb5a4722e293 [file] [log] [blame]
Sakari Ailusca50c192016-08-12 08:05:51 -03001/*
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
Sakari Ailuse07a41f2015-02-25 14:51:01 -050031enum 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
Sakari Ailusf3112732017-02-20 05:42:09 -050039static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
40 struct v4l2_fwnode_endpoint *vep)
Sakari Ailusca50c192016-08-12 08:05:51 -030041{
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) {
51 u32 array[ARRAY_SIZE(bus->data_lanes)];
52
53 bus->num_data_lanes =
54 min_t(int, ARRAY_SIZE(bus->data_lanes), rval);
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 }
67 }
68
69 rval = fwnode_property_read_u32_array(fwnode, "lane-polarities", NULL,
70 0);
71 if (rval > 0) {
72 u32 array[ARRAY_SIZE(bus->lane_polarities)];
73
74 if (rval < 1 + bus->num_data_lanes /* clock + data */) {
75 pr_warn("too few lane-polarities entries (need %u, got %u)\n",
76 1 + bus->num_data_lanes, rval);
77 return -EINVAL;
78 }
79
80 fwnode_property_read_u32_array(fwnode, "lane-polarities", array,
81 1 + bus->num_data_lanes);
82
83 for (i = 0; i < 1 + bus->num_data_lanes; i++)
84 bus->lane_polarities[i] = array[i];
85 }
86
87 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
88 if (lanes_used & BIT(v))
89 pr_warn("duplicated lane %u in clock-lanes\n", v);
90 lanes_used |= BIT(v);
91
92 bus->clock_lane = v;
93 have_clk_lane = true;
94 }
95
96 if (fwnode_property_present(fwnode, "clock-noncontinuous"))
97 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
98 else if (have_clk_lane || bus->num_data_lanes > 0)
99 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
100
101 bus->flags = flags;
102 vep->bus_type = V4L2_MBUS_CSI2;
103
104 return 0;
105}
106
107static void v4l2_fwnode_endpoint_parse_parallel_bus(
108 struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep)
109{
110 struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
111 unsigned int flags = 0;
112 u32 v;
113
114 if (!fwnode_property_read_u32(fwnode, "hsync-active", &v))
115 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
116 V4L2_MBUS_HSYNC_ACTIVE_LOW;
117
118 if (!fwnode_property_read_u32(fwnode, "vsync-active", &v))
119 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
120 V4L2_MBUS_VSYNC_ACTIVE_LOW;
121
122 if (!fwnode_property_read_u32(fwnode, "field-even-active", &v))
123 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
124 V4L2_MBUS_FIELD_EVEN_LOW;
125 if (flags)
126 vep->bus_type = V4L2_MBUS_PARALLEL;
127 else
128 vep->bus_type = V4L2_MBUS_BT656;
129
130 if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v))
131 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
132 V4L2_MBUS_PCLK_SAMPLE_FALLING;
133
134 if (!fwnode_property_read_u32(fwnode, "data-active", &v))
135 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
136 V4L2_MBUS_DATA_ACTIVE_LOW;
137
138 if (fwnode_property_present(fwnode, "slave-mode"))
139 flags |= V4L2_MBUS_SLAVE;
140 else
141 flags |= V4L2_MBUS_MASTER;
142
143 if (!fwnode_property_read_u32(fwnode, "bus-width", &v))
144 bus->bus_width = v;
145
146 if (!fwnode_property_read_u32(fwnode, "data-shift", &v))
147 bus->data_shift = v;
148
149 if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v))
150 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
151 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
152
153 bus->flags = flags;
154
155}
156
157/**
158 * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties
159 * @fwnode: pointer to the endpoint's fwnode handle
160 * @vep: pointer to the V4L2 fwnode data structure
161 *
162 * All properties are optional. If none are found, we don't set any flags. This
163 * means the port has a static configuration and no properties have to be
164 * specified explicitly. If any properties that identify the bus as parallel
165 * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if
166 * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we
167 * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a
168 * reference to @fwnode.
169 *
170 * NOTE: This function does not parse properties the size of which is variable
171 * without a low fixed limit. Please use v4l2_fwnode_endpoint_alloc_parse() in
172 * new drivers instead.
173 *
174 * Return: 0 on success or a negative error code on failure.
175 */
176int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
177 struct v4l2_fwnode_endpoint *vep)
178{
Sakari Ailuse07a41f2015-02-25 14:51:01 -0500179 u32 bus_type = 0;
Sakari Ailusca50c192016-08-12 08:05:51 -0300180 int rval;
181
182 fwnode_graph_parse_endpoint(fwnode, &vep->base);
183
184 /* Zero fields from bus_type to until the end */
185 memset(&vep->bus_type, 0, sizeof(*vep) -
186 offsetof(typeof(*vep), bus_type));
187
Sakari Ailuse07a41f2015-02-25 14:51:01 -0500188 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
189
Sakari Ailusf3112732017-02-20 05:42:09 -0500190 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep);
Sakari Ailusca50c192016-08-12 08:05:51 -0300191 if (rval)
192 return rval;
193 /*
194 * Parse the parallel video bus properties only if none
195 * of the MIPI CSI-2 specific properties were found.
196 */
197 if (vep->bus.mipi_csi2.flags == 0)
198 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep);
199
200 return 0;
201}
202EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
203
204/*
205 * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by
206 * v4l2_fwnode_endpoint_alloc_parse()
207 * @vep - the V4L2 fwnode the resources of which are to be released
208 *
209 * It is safe to call this function with NULL argument or on a V4L2 fwnode the
210 * parsing of which failed.
211 */
212void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
213{
214 if (IS_ERR_OR_NULL(vep))
215 return;
216
217 kfree(vep->link_frequencies);
218 kfree(vep);
219}
220EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
221
222/**
223 * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties
224 * @fwnode: pointer to the endpoint's fwnode handle
225 *
226 * All properties are optional. If none are found, we don't set any flags. This
227 * means the port has a static configuration and no properties have to be
228 * specified explicitly. If any properties that identify the bus as parallel
229 * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if
230 * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we
231 * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a
232 * reference to @fwnode.
233 *
234 * v4l2_fwnode_endpoint_alloc_parse() has two important differences to
235 * v4l2_fwnode_endpoint_parse():
236 *
237 * 1. It also parses variable size data.
238 *
239 * 2. The memory it has allocated to store the variable size data must be freed
240 * using v4l2_fwnode_endpoint_free() when no longer needed.
241 *
242 * Return: Pointer to v4l2_fwnode_endpoint if successful, on an error pointer
243 * on error.
244 */
245struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse(
246 struct fwnode_handle *fwnode)
247{
248 struct v4l2_fwnode_endpoint *vep;
249 int rval;
250
251 vep = kzalloc(sizeof(*vep), GFP_KERNEL);
252 if (!vep)
253 return ERR_PTR(-ENOMEM);
254
255 rval = v4l2_fwnode_endpoint_parse(fwnode, vep);
256 if (rval < 0)
257 goto out_err;
258
259 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
260 NULL, 0);
261 if (rval < 0)
262 goto out_err;
263
264 vep->link_frequencies =
265 kmalloc_array(rval, sizeof(*vep->link_frequencies), GFP_KERNEL);
266 if (!vep->link_frequencies) {
267 rval = -ENOMEM;
268 goto out_err;
269 }
270
271 vep->nr_of_link_frequencies = rval;
272
273 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
274 vep->link_frequencies,
275 vep->nr_of_link_frequencies);
276 if (rval < 0)
277 goto out_err;
278
279 return vep;
280
281out_err:
282 v4l2_fwnode_endpoint_free(vep);
283 return ERR_PTR(rval);
284}
285EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
286
287/**
288 * v4l2_fwnode_endpoint_parse_link() - parse a link between two endpoints
289 * @__fwnode: pointer to the endpoint's fwnode at the local end of the link
290 * @link: pointer to the V4L2 fwnode link data structure
291 *
292 * Fill the link structure with the local and remote nodes and port numbers.
293 * The local_node and remote_node fields are set to point to the local and
294 * remote port's parent nodes respectively (the port parent node being the
295 * parent node of the port node if that node isn't a 'ports' node, or the
296 * grand-parent node of the port node otherwise).
297 *
298 * A reference is taken to both the local and remote nodes, the caller must use
299 * v4l2_fwnode_endpoint_put_link() to drop the references when done with the
300 * link.
301 *
302 * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be
303 * found.
304 */
305int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
306 struct v4l2_fwnode_link *link)
307{
308 const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
309 struct fwnode_handle *fwnode;
310
311 memset(link, 0, sizeof(*link));
312
313 fwnode = fwnode_get_parent(__fwnode);
314 fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
315 fwnode = fwnode_get_next_parent(fwnode);
316 if (is_of_node(fwnode) &&
317 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
318 fwnode = fwnode_get_next_parent(fwnode);
319 link->local_node = fwnode;
320
321 fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
322 if (!fwnode) {
323 fwnode_handle_put(fwnode);
324 return -ENOLINK;
325 }
326
327 fwnode = fwnode_get_parent(fwnode);
328 fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
329 fwnode = fwnode_get_next_parent(fwnode);
330 if (is_of_node(fwnode) &&
331 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
332 fwnode = fwnode_get_next_parent(fwnode);
333 link->remote_node = fwnode;
334
335 return 0;
336}
337EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
338
339/**
340 * v4l2_fwnode_put_link() - drop references to nodes in a link
341 * @link: pointer to the V4L2 fwnode link data structure
342 *
343 * Drop references to the local and remote nodes in the link. This function
344 * must be called on every link parsed with v4l2_fwnode_parse_link().
345 */
346void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
347{
348 fwnode_handle_put(link->local_node);
349 fwnode_handle_put(link->remote_node);
350}
351EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
352
353MODULE_LICENSE("GPL");
354MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
355MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
356MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");