2 * V4L2 fwnode binding parsing library
4 * The origins of the V4L2 fwnode library are in V4L2 OF library that
5 * formerly was located in v4l2-of.c.
7 * Copyright (c) 2016 Intel Corporation.
8 * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
10 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
11 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
13 * Copyright (C) 2012 Renesas Electronics Corp.
14 * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
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.
20 #include <linux/acpi.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
24 #include <linux/property.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
29 #include <media/v4l2-fwnode.h>
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,
39 static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
40 struct v4l2_fwnode_endpoint *vep)
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;
49 rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0);
51 u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
54 min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval);
56 fwnode_property_read_u32_array(fwnode, "data-lanes", array,
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",
63 lanes_used |= BIT(array[i]);
65 bus->data_lanes[i] = array[i];
68 rval = fwnode_property_read_u32_array(fwnode,
69 "lane-polarities", NULL,
72 if (rval != 1 + bus->num_data_lanes /* clock+data */) {
73 pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
74 1 + bus->num_data_lanes, rval);
78 fwnode_property_read_u32_array(fwnode,
79 "lane-polarities", array,
80 1 + bus->num_data_lanes);
82 for (i = 0; i < 1 + bus->num_data_lanes; i++)
83 bus->lane_polarities[i] = array[i];
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);
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;
103 vep->bus_type = V4L2_MBUS_CSI2;
108 static void v4l2_fwnode_endpoint_parse_parallel_bus(
109 struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep)
111 struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
112 unsigned int flags = 0;
115 if (!fwnode_property_read_u32(fwnode, "hsync-active", &v))
116 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
117 V4L2_MBUS_HSYNC_ACTIVE_LOW;
119 if (!fwnode_property_read_u32(fwnode, "vsync-active", &v))
120 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
121 V4L2_MBUS_VSYNC_ACTIVE_LOW;
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;
127 vep->bus_type = V4L2_MBUS_PARALLEL;
129 vep->bus_type = V4L2_MBUS_BT656;
131 if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v))
132 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
133 V4L2_MBUS_PCLK_SAMPLE_FALLING;
135 if (!fwnode_property_read_u32(fwnode, "data-active", &v))
136 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
137 V4L2_MBUS_DATA_ACTIVE_LOW;
139 if (fwnode_property_present(fwnode, "slave-mode"))
140 flags |= V4L2_MBUS_SLAVE;
142 flags |= V4L2_MBUS_MASTER;
144 if (!fwnode_property_read_u32(fwnode, "bus-width", &v))
147 if (!fwnode_property_read_u32(fwnode, "data-shift", &v))
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;
159 v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
160 struct v4l2_fwnode_endpoint *vep,
163 struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
166 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v))
169 if (!fwnode_property_read_u32(fwnode, "strobe", &v))
172 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v))
175 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v))
178 if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2)
179 vep->bus_type = V4L2_MBUS_CCP2;
181 vep->bus_type = V4L2_MBUS_CSI1;
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
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.
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.
201 * Return: 0 on success or a negative error code on failure.
203 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
204 struct v4l2_fwnode_endpoint *vep)
209 fwnode_graph_parse_endpoint(fwnode, &vep->base);
211 /* Zero fields from bus_type to until the end */
212 memset(&vep->bus_type, 0, sizeof(*vep) -
213 offsetof(typeof(*vep), bus_type));
215 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
218 case V4L2_FWNODE_BUS_TYPE_GUESS:
219 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep);
223 * Parse the parallel video bus properties only if none
224 * of the MIPI CSI-2 specific properties were found.
226 if (vep->bus.mipi_csi2.flags == 0)
227 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep);
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);
236 pr_warn("unsupported bus type %u\n", bus_type);
240 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
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
247 * It is safe to call this function with NULL argument or on a V4L2 fwnode the
248 * parsing of which failed.
250 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
252 if (IS_ERR_OR_NULL(vep))
255 kfree(vep->link_frequencies);
258 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
261 * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties
262 * @fwnode: pointer to the endpoint's fwnode handle
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.
272 * v4l2_fwnode_endpoint_alloc_parse() has two important differences to
273 * v4l2_fwnode_endpoint_parse():
275 * 1. It also parses variable size data.
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.
280 * Return: Pointer to v4l2_fwnode_endpoint if successful, on an error pointer
283 struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse(
284 struct fwnode_handle *fwnode)
286 struct v4l2_fwnode_endpoint *vep;
289 vep = kzalloc(sizeof(*vep), GFP_KERNEL);
291 return ERR_PTR(-ENOMEM);
293 rval = v4l2_fwnode_endpoint_parse(fwnode, vep);
297 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
300 vep->link_frequencies =
301 kmalloc_array(rval, sizeof(*vep->link_frequencies),
303 if (!vep->link_frequencies) {
308 vep->nr_of_link_frequencies = rval;
310 rval = fwnode_property_read_u64_array(
311 fwnode, "link-frequencies", vep->link_frequencies,
312 vep->nr_of_link_frequencies);
320 v4l2_fwnode_endpoint_free(vep);
321 return ERR_PTR(rval);
323 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
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
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).
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
340 * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be
343 int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
344 struct v4l2_fwnode_link *link)
346 const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
347 struct fwnode_handle *fwnode;
349 memset(link, 0, sizeof(*link));
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;
359 fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
361 fwnode_handle_put(fwnode);
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;
375 EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
378 * v4l2_fwnode_put_link() - drop references to nodes in a link
379 * @link: pointer to the V4L2 fwnode link data structure
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().
384 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
386 fwnode_handle_put(link->local_node);
387 fwnode_handle_put(link->remote_node);
389 EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
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>");