2 * V4L2 OF binding parsing library
4 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
5 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
7 * Copyright (C) 2012 Renesas Electronics Corp.
8 * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
21 #include <media/v4l2-of.h>
23 static int v4l2_of_parse_csi_bus(const struct device_node *node,
24 struct v4l2_of_endpoint *endpoint)
26 struct v4l2_of_bus_mipi_csi2 *bus = &endpoint->bus.mipi_csi2;
27 struct property *prop;
28 bool have_clk_lane = false;
29 unsigned int flags = 0, lanes_used = 0;
32 prop = of_find_property(node, "data-lanes", NULL);
34 const __be32 *lane = NULL;
37 for (i = 0; i < ARRAY_SIZE(bus->data_lanes); i++) {
38 lane = of_prop_next_u32(prop, lane, &v);
42 if (lanes_used & BIT(v))
43 pr_warn("%s: duplicated lane %u in data-lanes\n",
47 bus->data_lanes[i] = v;
49 bus->num_data_lanes = i;
52 prop = of_find_property(node, "lane-polarities", NULL);
54 const __be32 *polarity = NULL;
57 for (i = 0; i < ARRAY_SIZE(bus->lane_polarities); i++) {
58 polarity = of_prop_next_u32(prop, polarity, &v);
61 bus->lane_polarities[i] = v;
64 if (i < 1 + bus->num_data_lanes /* clock + data */) {
65 pr_warn("%s: too few lane-polarities entries (need %u, got %u)\n",
66 node->full_name, 1 + bus->num_data_lanes, i);
71 if (!of_property_read_u32(node, "clock-lanes", &v)) {
72 if (lanes_used & BIT(v))
73 pr_warn("%s: duplicated lane %u in clock-lanes\n",
81 if (of_get_property(node, "clock-noncontinuous", &v))
82 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
83 else if (have_clk_lane || bus->num_data_lanes > 0)
84 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
87 endpoint->bus_type = V4L2_MBUS_CSI2;
92 static void v4l2_of_parse_parallel_bus(const struct device_node *node,
93 struct v4l2_of_endpoint *endpoint)
95 struct v4l2_of_bus_parallel *bus = &endpoint->bus.parallel;
96 unsigned int flags = 0;
99 if (!of_property_read_u32(node, "hsync-active", &v))
100 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
101 V4L2_MBUS_HSYNC_ACTIVE_LOW;
103 if (!of_property_read_u32(node, "vsync-active", &v))
104 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
105 V4L2_MBUS_VSYNC_ACTIVE_LOW;
107 if (!of_property_read_u32(node, "field-even-active", &v))
108 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
109 V4L2_MBUS_FIELD_EVEN_LOW;
111 endpoint->bus_type = V4L2_MBUS_PARALLEL;
113 endpoint->bus_type = V4L2_MBUS_BT656;
115 if (!of_property_read_u32(node, "pclk-sample", &v))
116 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
117 V4L2_MBUS_PCLK_SAMPLE_FALLING;
119 if (!of_property_read_u32(node, "data-active", &v))
120 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
121 V4L2_MBUS_DATA_ACTIVE_LOW;
123 if (of_get_property(node, "slave-mode", &v))
124 flags |= V4L2_MBUS_SLAVE;
126 flags |= V4L2_MBUS_MASTER;
128 if (!of_property_read_u32(node, "bus-width", &v))
131 if (!of_property_read_u32(node, "data-shift", &v))
134 if (!of_property_read_u32(node, "sync-on-green-active", &v))
135 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
136 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
143 * v4l2_of_parse_endpoint() - parse all endpoint node properties
144 * @node: pointer to endpoint device_node
145 * @endpoint: pointer to the V4L2 OF endpoint data structure
147 * All properties are optional. If none are found, we don't set any flags.
148 * This means the port has a static configuration and no properties have
149 * to be specified explicitly.
150 * If any properties that identify the bus as parallel are found and
151 * slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if we recognise
152 * the bus as serial CSI-2 and clock-noncontinuous isn't set, we set the
153 * V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag.
154 * The caller should hold a reference to @node.
156 * NOTE: This function does not parse properties the size of which is
157 * variable without a low fixed limit. Please use
158 * v4l2_of_alloc_parse_endpoint() in new drivers instead.
160 * Return: 0 on success or a negative error code on failure.
162 int v4l2_of_parse_endpoint(const struct device_node *node,
163 struct v4l2_of_endpoint *endpoint)
167 of_graph_parse_endpoint(node, &endpoint->base);
168 /* Zero fields from bus_type to until the end */
169 memset(&endpoint->bus_type, 0, sizeof(*endpoint) -
170 offsetof(typeof(*endpoint), bus_type));
172 rval = v4l2_of_parse_csi_bus(node, endpoint);
176 * Parse the parallel video bus properties only if none
177 * of the MIPI CSI-2 specific properties were found.
179 if (endpoint->bus.mipi_csi2.flags == 0)
180 v4l2_of_parse_parallel_bus(node, endpoint);
184 EXPORT_SYMBOL(v4l2_of_parse_endpoint);
187 * v4l2_of_free_endpoint() - free the endpoint acquired by
188 * v4l2_of_alloc_parse_endpoint()
189 * @endpoint - the endpoint the resources of which are to be released
191 * It is safe to call this function with NULL argument or on an
192 * endpoint the parsing of which failed.
194 void v4l2_of_free_endpoint(struct v4l2_of_endpoint *endpoint)
196 if (IS_ERR_OR_NULL(endpoint))
199 kfree(endpoint->link_frequencies);
202 EXPORT_SYMBOL(v4l2_of_free_endpoint);
205 * v4l2_of_alloc_parse_endpoint() - parse all endpoint node properties
206 * @node: pointer to endpoint device_node
208 * All properties are optional. If none are found, we don't set any flags.
209 * This means the port has a static configuration and no properties have
210 * to be specified explicitly.
211 * If any properties that identify the bus as parallel are found and
212 * slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if we recognise
213 * the bus as serial CSI-2 and clock-noncontinuous isn't set, we set the
214 * V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag.
215 * The caller should hold a reference to @node.
217 * v4l2_of_alloc_parse_endpoint() has two important differences to
218 * v4l2_of_parse_endpoint():
220 * 1. It also parses variable size data and
222 * 2. The memory it has allocated to store the variable size data must
223 * be freed using v4l2_of_free_endpoint() when no longer needed.
225 * Return: Pointer to v4l2_of_endpoint if successful, on error a
226 * negative error code.
228 struct v4l2_of_endpoint *v4l2_of_alloc_parse_endpoint(
229 const struct device_node *node)
231 struct v4l2_of_endpoint *endpoint;
235 endpoint = kzalloc(sizeof(*endpoint), GFP_KERNEL);
237 return ERR_PTR(-ENOMEM);
239 rval = v4l2_of_parse_endpoint(node, endpoint);
243 if (of_get_property(node, "link-frequencies", &len)) {
244 endpoint->link_frequencies = kmalloc(len, GFP_KERNEL);
245 if (!endpoint->link_frequencies) {
250 endpoint->nr_of_link_frequencies =
251 len / sizeof(*endpoint->link_frequencies);
253 rval = of_property_read_u64_array(
254 node, "link-frequencies", endpoint->link_frequencies,
255 endpoint->nr_of_link_frequencies);
263 v4l2_of_free_endpoint(endpoint);
264 return ERR_PTR(rval);
266 EXPORT_SYMBOL(v4l2_of_alloc_parse_endpoint);
269 * v4l2_of_parse_link() - parse a link between two endpoints
270 * @node: pointer to the endpoint at the local end of the link
271 * @link: pointer to the V4L2 OF link data structure
273 * Fill the link structure with the local and remote nodes and port numbers.
274 * The local_node and remote_node fields are set to point to the local and
275 * remote port's parent nodes respectively (the port parent node being the
276 * parent node of the port node if that node isn't a 'ports' node, or the
277 * grand-parent node of the port node otherwise).
279 * A reference is taken to both the local and remote nodes, the caller must use
280 * v4l2_of_put_link() to drop the references when done with the link.
282 * Return: 0 on success, or -ENOLINK if the remote endpoint can't be found.
284 int v4l2_of_parse_link(const struct device_node *node,
285 struct v4l2_of_link *link)
287 struct device_node *np;
289 memset(link, 0, sizeof(*link));
291 np = of_get_parent(node);
292 of_property_read_u32(np, "reg", &link->local_port);
293 np = of_get_next_parent(np);
294 if (of_node_cmp(np->name, "ports") == 0)
295 np = of_get_next_parent(np);
296 link->local_node = np;
298 np = of_parse_phandle(node, "remote-endpoint", 0);
300 of_node_put(link->local_node);
304 np = of_get_parent(np);
305 of_property_read_u32(np, "reg", &link->remote_port);
306 np = of_get_next_parent(np);
307 if (of_node_cmp(np->name, "ports") == 0)
308 np = of_get_next_parent(np);
309 link->remote_node = np;
313 EXPORT_SYMBOL(v4l2_of_parse_link);
316 * v4l2_of_put_link() - drop references to nodes in a link
317 * @link: pointer to the V4L2 OF link data structure
319 * Drop references to the local and remote nodes in the link. This function must
320 * be called on every link parsed with v4l2_of_parse_link().
322 void v4l2_of_put_link(struct v4l2_of_link *link)
324 of_node_put(link->local_node);
325 of_node_put(link->remote_node);
327 EXPORT_SYMBOL(v4l2_of_put_link);