blob: ca755a4832fc01de0404e0c5623b3cc7e6c3765b [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
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500157void v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
158 struct v4l2_fwnode_endpoint *vep,
159 u32 bus_type)
160{
161 struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
162 u32 v;
163
164 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v))
165 bus->clock_inv = v;
166
167 if (!fwnode_property_read_u32(fwnode, "strobe", &v))
168 bus->strobe = v;
169
170 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v))
171 bus->data_lane = v;
172
173 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v))
174 bus->clock_lane = v;
175
176 if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2)
177 vep->bus_type = V4L2_MBUS_CCP2;
178 else
179 vep->bus_type = V4L2_MBUS_CSI1;
180}
181
Sakari Ailusca50c192016-08-12 08:05:51 -0300182/**
183 * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties
184 * @fwnode: pointer to the endpoint's fwnode handle
185 * @vep: pointer to the V4L2 fwnode data structure
186 *
187 * All properties are optional. If none are found, we don't set any flags. This
188 * means the port has a static configuration and no properties have to be
189 * specified explicitly. If any properties that identify the bus as parallel
190 * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if
191 * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we
192 * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a
193 * reference to @fwnode.
194 *
195 * NOTE: This function does not parse properties the size of which is variable
196 * without a low fixed limit. Please use v4l2_fwnode_endpoint_alloc_parse() in
197 * new drivers instead.
198 *
199 * Return: 0 on success or a negative error code on failure.
200 */
201int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
202 struct v4l2_fwnode_endpoint *vep)
203{
Sakari Ailuse07a41f2015-02-25 14:51:01 -0500204 u32 bus_type = 0;
Sakari Ailusca50c192016-08-12 08:05:51 -0300205 int rval;
206
207 fwnode_graph_parse_endpoint(fwnode, &vep->base);
208
209 /* Zero fields from bus_type to until the end */
210 memset(&vep->bus_type, 0, sizeof(*vep) -
211 offsetof(typeof(*vep), bus_type));
212
Sakari Ailuse07a41f2015-02-25 14:51:01 -0500213 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
214
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500215 switch (bus_type) {
216 case V4L2_FWNODE_BUS_TYPE_GUESS:
217 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep);
218 if (rval)
219 return rval;
220 /*
221 * Parse the parallel video bus properties only if none
222 * of the MIPI CSI-2 specific properties were found.
223 */
224 if (vep->bus.mipi_csi2.flags == 0)
225 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep);
Sakari Ailusca50c192016-08-12 08:05:51 -0300226
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500227 return 0;
228 case V4L2_FWNODE_BUS_TYPE_CCP2:
229 case V4L2_FWNODE_BUS_TYPE_CSI1:
230 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type);
231
232 return 0;
233 default:
234 pr_warn("unsupported bus type %u\n", bus_type);
235 return -EINVAL;
236 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300237}
238EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
239
240/*
241 * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by
242 * v4l2_fwnode_endpoint_alloc_parse()
243 * @vep - the V4L2 fwnode the resources of which are to be released
244 *
245 * It is safe to call this function with NULL argument or on a V4L2 fwnode the
246 * parsing of which failed.
247 */
248void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
249{
250 if (IS_ERR_OR_NULL(vep))
251 return;
252
253 kfree(vep->link_frequencies);
254 kfree(vep);
255}
256EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
257
258/**
259 * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties
260 * @fwnode: pointer to the endpoint's fwnode handle
261 *
262 * All properties are optional. If none are found, we don't set any flags. This
263 * means the port has a static configuration and no properties have to be
264 * specified explicitly. If any properties that identify the bus as parallel
265 * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if
266 * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we
267 * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a
268 * reference to @fwnode.
269 *
270 * v4l2_fwnode_endpoint_alloc_parse() has two important differences to
271 * v4l2_fwnode_endpoint_parse():
272 *
273 * 1. It also parses variable size data.
274 *
275 * 2. The memory it has allocated to store the variable size data must be freed
276 * using v4l2_fwnode_endpoint_free() when no longer needed.
277 *
278 * Return: Pointer to v4l2_fwnode_endpoint if successful, on an error pointer
279 * on error.
280 */
281struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse(
282 struct fwnode_handle *fwnode)
283{
284 struct v4l2_fwnode_endpoint *vep;
285 int rval;
286
287 vep = kzalloc(sizeof(*vep), GFP_KERNEL);
288 if (!vep)
289 return ERR_PTR(-ENOMEM);
290
291 rval = v4l2_fwnode_endpoint_parse(fwnode, vep);
292 if (rval < 0)
293 goto out_err;
294
295 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
296 NULL, 0);
297 if (rval < 0)
298 goto out_err;
299
300 vep->link_frequencies =
301 kmalloc_array(rval, sizeof(*vep->link_frequencies), GFP_KERNEL);
302 if (!vep->link_frequencies) {
303 rval = -ENOMEM;
304 goto out_err;
305 }
306
307 vep->nr_of_link_frequencies = rval;
308
309 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
310 vep->link_frequencies,
311 vep->nr_of_link_frequencies);
312 if (rval < 0)
313 goto out_err;
314
315 return vep;
316
317out_err:
318 v4l2_fwnode_endpoint_free(vep);
319 return ERR_PTR(rval);
320}
321EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
322
323/**
324 * v4l2_fwnode_endpoint_parse_link() - parse a link between two endpoints
325 * @__fwnode: pointer to the endpoint's fwnode at the local end of the link
326 * @link: pointer to the V4L2 fwnode link data structure
327 *
328 * Fill the link structure with the local and remote nodes and port numbers.
329 * The local_node and remote_node fields are set to point to the local and
330 * remote port's parent nodes respectively (the port parent node being the
331 * parent node of the port node if that node isn't a 'ports' node, or the
332 * grand-parent node of the port node otherwise).
333 *
334 * A reference is taken to both the local and remote nodes, the caller must use
335 * v4l2_fwnode_endpoint_put_link() to drop the references when done with the
336 * link.
337 *
338 * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be
339 * found.
340 */
341int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
342 struct v4l2_fwnode_link *link)
343{
344 const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
345 struct fwnode_handle *fwnode;
346
347 memset(link, 0, sizeof(*link));
348
349 fwnode = fwnode_get_parent(__fwnode);
350 fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
351 fwnode = fwnode_get_next_parent(fwnode);
352 if (is_of_node(fwnode) &&
353 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
354 fwnode = fwnode_get_next_parent(fwnode);
355 link->local_node = fwnode;
356
357 fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
358 if (!fwnode) {
359 fwnode_handle_put(fwnode);
360 return -ENOLINK;
361 }
362
363 fwnode = fwnode_get_parent(fwnode);
364 fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
365 fwnode = fwnode_get_next_parent(fwnode);
366 if (is_of_node(fwnode) &&
367 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
368 fwnode = fwnode_get_next_parent(fwnode);
369 link->remote_node = fwnode;
370
371 return 0;
372}
373EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
374
375/**
376 * v4l2_fwnode_put_link() - drop references to nodes in a link
377 * @link: pointer to the V4L2 fwnode link data structure
378 *
379 * Drop references to the local and remote nodes in the link. This function
380 * must be called on every link parsed with v4l2_fwnode_parse_link().
381 */
382void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
383{
384 fwnode_handle_put(link->local_node);
385 fwnode_handle_put(link->remote_node);
386}
387EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
388
389MODULE_LICENSE("GPL");
390MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
391MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
392MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");