Merge branch 'turbostat' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux
[linux-2.6-block.git] / drivers / media / v4l2-core / v4l2-of.c
1 /*
2  * V4L2 OF binding parsing library
3  *
4  * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
5  * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
6  *
7  * Copyright (C) 2012 Renesas Electronics Corp.
8  * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
9  *
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.
13  */
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20
21 #include <media/v4l2-of.h>
22
23 static int v4l2_of_parse_csi_bus(const struct device_node *node,
24                                  struct v4l2_of_endpoint *endpoint)
25 {
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;
30         u32 v;
31
32         prop = of_find_property(node, "data-lanes", NULL);
33         if (prop) {
34                 const __be32 *lane = NULL;
35                 unsigned int i;
36
37                 for (i = 0; i < ARRAY_SIZE(bus->data_lanes); i++) {
38                         lane = of_prop_next_u32(prop, lane, &v);
39                         if (!lane)
40                                 break;
41
42                         if (lanes_used & BIT(v))
43                                 pr_warn("%s: duplicated lane %u in data-lanes\n",
44                                         node->full_name, v);
45                         lanes_used |= BIT(v);
46
47                         bus->data_lanes[i] = v;
48                 }
49                 bus->num_data_lanes = i;
50         }
51
52         prop = of_find_property(node, "lane-polarities", NULL);
53         if (prop) {
54                 const __be32 *polarity = NULL;
55                 unsigned int i;
56
57                 for (i = 0; i < ARRAY_SIZE(bus->lane_polarities); i++) {
58                         polarity = of_prop_next_u32(prop, polarity, &v);
59                         if (!polarity)
60                                 break;
61                         bus->lane_polarities[i] = v;
62                 }
63
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);
67                         return -EINVAL;
68                 }
69         }
70
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",
74                                 node->full_name, v);
75                 lanes_used |= BIT(v);
76
77                 bus->clock_lane = v;
78                 have_clk_lane = true;
79         }
80
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;
85
86         bus->flags = flags;
87         endpoint->bus_type = V4L2_MBUS_CSI2;
88
89         return 0;
90 }
91
92 static void v4l2_of_parse_parallel_bus(const struct device_node *node,
93                                        struct v4l2_of_endpoint *endpoint)
94 {
95         struct v4l2_of_bus_parallel *bus = &endpoint->bus.parallel;
96         unsigned int flags = 0;
97         u32 v;
98
99         if (!of_property_read_u32(node, "hsync-active", &v))
100                 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
101                         V4L2_MBUS_HSYNC_ACTIVE_LOW;
102
103         if (!of_property_read_u32(node, "vsync-active", &v))
104                 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
105                         V4L2_MBUS_VSYNC_ACTIVE_LOW;
106
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;
110         if (flags)
111                 endpoint->bus_type = V4L2_MBUS_PARALLEL;
112         else
113                 endpoint->bus_type = V4L2_MBUS_BT656;
114
115         if (!of_property_read_u32(node, "pclk-sample", &v))
116                 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
117                         V4L2_MBUS_PCLK_SAMPLE_FALLING;
118
119         if (!of_property_read_u32(node, "data-active", &v))
120                 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
121                         V4L2_MBUS_DATA_ACTIVE_LOW;
122
123         if (of_get_property(node, "slave-mode", &v))
124                 flags |= V4L2_MBUS_SLAVE;
125         else
126                 flags |= V4L2_MBUS_MASTER;
127
128         if (!of_property_read_u32(node, "bus-width", &v))
129                 bus->bus_width = v;
130
131         if (!of_property_read_u32(node, "data-shift", &v))
132                 bus->data_shift = v;
133
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;
137
138         bus->flags = flags;
139
140 }
141
142 /**
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
146  *
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.
155  *
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.
159  *
160  * Return: 0 on success or a negative error code on failure.
161  */
162 int v4l2_of_parse_endpoint(const struct device_node *node,
163                            struct v4l2_of_endpoint *endpoint)
164 {
165         int rval;
166
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));
171
172         rval = v4l2_of_parse_csi_bus(node, endpoint);
173         if (rval)
174                 return rval;
175         /*
176          * Parse the parallel video bus properties only if none
177          * of the MIPI CSI-2 specific properties were found.
178          */
179         if (endpoint->bus.mipi_csi2.flags == 0)
180                 v4l2_of_parse_parallel_bus(node, endpoint);
181
182         return 0;
183 }
184 EXPORT_SYMBOL(v4l2_of_parse_endpoint);
185
186 /*
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
190  *
191  * It is safe to call this function with NULL argument or on an
192  * endpoint the parsing of which failed.
193  */
194 void v4l2_of_free_endpoint(struct v4l2_of_endpoint *endpoint)
195 {
196         if (IS_ERR_OR_NULL(endpoint))
197                 return;
198
199         kfree(endpoint->link_frequencies);
200         kfree(endpoint);
201 }
202 EXPORT_SYMBOL(v4l2_of_free_endpoint);
203
204 /**
205  * v4l2_of_alloc_parse_endpoint() - parse all endpoint node properties
206  * @node: pointer to endpoint device_node
207  *
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.
216  *
217  * v4l2_of_alloc_parse_endpoint() has two important differences to
218  * v4l2_of_parse_endpoint():
219  *
220  * 1. It also parses variable size data and
221  *
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.
224  *
225  * Return: Pointer to v4l2_of_endpoint if successful, on error a
226  * negative error code.
227  */
228 struct v4l2_of_endpoint *v4l2_of_alloc_parse_endpoint(
229         const struct device_node *node)
230 {
231         struct v4l2_of_endpoint *endpoint;
232         int len;
233         int rval;
234
235         endpoint = kzalloc(sizeof(*endpoint), GFP_KERNEL);
236         if (!endpoint)
237                 return ERR_PTR(-ENOMEM);
238
239         rval = v4l2_of_parse_endpoint(node, endpoint);
240         if (rval < 0)
241                 goto out_err;
242
243         if (of_get_property(node, "link-frequencies", &len)) {
244                 endpoint->link_frequencies = kmalloc(len, GFP_KERNEL);
245                 if (!endpoint->link_frequencies) {
246                         rval = -ENOMEM;
247                         goto out_err;
248                 }
249
250                 endpoint->nr_of_link_frequencies =
251                         len / sizeof(*endpoint->link_frequencies);
252
253                 rval = of_property_read_u64_array(
254                         node, "link-frequencies", endpoint->link_frequencies,
255                         endpoint->nr_of_link_frequencies);
256                 if (rval < 0)
257                         goto out_err;
258         }
259
260         return endpoint;
261
262 out_err:
263         v4l2_of_free_endpoint(endpoint);
264         return ERR_PTR(rval);
265 }
266 EXPORT_SYMBOL(v4l2_of_alloc_parse_endpoint);
267
268 /**
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
272  *
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).
278  *
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.
281  *
282  * Return: 0 on success, or -ENOLINK if the remote endpoint can't be found.
283  */
284 int v4l2_of_parse_link(const struct device_node *node,
285                        struct v4l2_of_link *link)
286 {
287         struct device_node *np;
288
289         memset(link, 0, sizeof(*link));
290
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;
297
298         np = of_parse_phandle(node, "remote-endpoint", 0);
299         if (!np) {
300                 of_node_put(link->local_node);
301                 return -ENOLINK;
302         }
303
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;
310
311         return 0;
312 }
313 EXPORT_SYMBOL(v4l2_of_parse_link);
314
315 /**
316  * v4l2_of_put_link() - drop references to nodes in a link
317  * @link: pointer to the V4L2 OF link data structure
318  *
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().
321  */
322 void v4l2_of_put_link(struct v4l2_of_link *link)
323 {
324         of_node_put(link->local_node);
325         of_node_put(link->remote_node);
326 }
327 EXPORT_SYMBOL(v4l2_of_put_link);