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