[media] v4l: of: Instead of zeroing bus_type and bus field separately, unify this
[deliverable/linux.git] / drivers / media / v4l2-core / v4l2-of.c
CommitLineData
99fd133f
GL
1/*
2 * V4L2 OF binding parsing library
3 *
4 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
c6e8d96d 5 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
99fd133f
GL
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/string.h>
18#include <linux/types.h>
19
20#include <media/v4l2-of.h>
21
b6eec1c4
SA
22static int v4l2_of_parse_csi_bus(const struct device_node *node,
23 struct v4l2_of_endpoint *endpoint)
99fd133f
GL
24{
25 struct v4l2_of_bus_mipi_csi2 *bus = &endpoint->bus.mipi_csi2;
99fd133f
GL
26 struct property *prop;
27 bool have_clk_lane = false;
28 unsigned int flags = 0;
29 u32 v;
30
31 prop = of_find_property(node, "data-lanes", NULL);
32 if (prop) {
33 const __be32 *lane = NULL;
b6eec1c4 34 unsigned int i;
99fd133f 35
b6eec1c4
SA
36 for (i = 0; i < ARRAY_SIZE(bus->data_lanes); i++) {
37 lane = of_prop_next_u32(prop, lane, &v);
99fd133f
GL
38 if (!lane)
39 break;
b6eec1c4 40 bus->data_lanes[i] = v;
99fd133f
GL
41 }
42 bus->num_data_lanes = i;
b6eec1c4
SA
43 }
44
45 prop = of_find_property(node, "lane-polarities", NULL);
46 if (prop) {
47 const __be32 *polarity = NULL;
48 unsigned int i;
49
50 for (i = 0; i < ARRAY_SIZE(bus->lane_polarities); i++) {
51 polarity = of_prop_next_u32(prop, polarity, &v);
52 if (!polarity)
53 break;
54 bus->lane_polarities[i] = v;
55 }
56
57 if (i < 1 + bus->num_data_lanes /* clock + data */) {
58 pr_warn("%s: too few lane-polarities entries (need %u, got %u)\n",
59 node->full_name, 1 + bus->num_data_lanes, i);
60 return -EINVAL;
61 }
99fd133f
GL
62 }
63
64 if (!of_property_read_u32(node, "clock-lanes", &v)) {
65 bus->clock_lane = v;
66 have_clk_lane = true;
67 }
68
69 if (of_get_property(node, "clock-noncontinuous", &v))
70 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
71 else if (have_clk_lane || bus->num_data_lanes > 0)
72 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
73
74 bus->flags = flags;
75 endpoint->bus_type = V4L2_MBUS_CSI2;
b6eec1c4
SA
76
77 return 0;
99fd133f
GL
78}
79
80static void v4l2_of_parse_parallel_bus(const struct device_node *node,
81 struct v4l2_of_endpoint *endpoint)
82{
83 struct v4l2_of_bus_parallel *bus = &endpoint->bus.parallel;
84 unsigned int flags = 0;
85 u32 v;
86
87 if (!of_property_read_u32(node, "hsync-active", &v))
88 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
89 V4L2_MBUS_HSYNC_ACTIVE_LOW;
90
91 if (!of_property_read_u32(node, "vsync-active", &v))
92 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
93 V4L2_MBUS_VSYNC_ACTIVE_LOW;
94
95 if (!of_property_read_u32(node, "pclk-sample", &v))
96 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
97 V4L2_MBUS_PCLK_SAMPLE_FALLING;
98
99 if (!of_property_read_u32(node, "field-even-active", &v))
100 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
101 V4L2_MBUS_FIELD_EVEN_LOW;
102 if (flags)
103 endpoint->bus_type = V4L2_MBUS_PARALLEL;
104 else
105 endpoint->bus_type = V4L2_MBUS_BT656;
106
107 if (!of_property_read_u32(node, "data-active", &v))
108 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
109 V4L2_MBUS_DATA_ACTIVE_LOW;
110
111 if (of_get_property(node, "slave-mode", &v))
112 flags |= V4L2_MBUS_SLAVE;
113 else
114 flags |= V4L2_MBUS_MASTER;
115
116 if (!of_property_read_u32(node, "bus-width", &v))
117 bus->bus_width = v;
118
119 if (!of_property_read_u32(node, "data-shift", &v))
120 bus->data_shift = v;
121
d1d70aa6
LP
122 if (!of_property_read_u32(node, "sync-on-green-active", &v))
123 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
124 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
125
99fd133f
GL
126 bus->flags = flags;
127
128}
99fd133f
GL
129
130/**
131 * v4l2_of_parse_endpoint() - parse all endpoint node properties
132 * @node: pointer to endpoint device_node
133 * @endpoint: pointer to the V4L2 OF endpoint data structure
134 *
135 * All properties are optional. If none are found, we don't set any flags.
136 * This means the port has a static configuration and no properties have
137 * to be specified explicitly.
138 * If any properties that identify the bus as parallel are found and
139 * slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if we recognise
140 * the bus as serial CSI-2 and clock-noncontinuous isn't set, we set the
141 * V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag.
142 * The caller should hold a reference to @node.
9ff889b6
LP
143 *
144 * Return: 0.
99fd133f 145 */
9ff889b6
LP
146int v4l2_of_parse_endpoint(const struct device_node *node,
147 struct v4l2_of_endpoint *endpoint)
99fd133f 148{
b6eec1c4
SA
149 int rval;
150
f2a575f6 151 of_graph_parse_endpoint(node, &endpoint->base);
161aadae
SA
152 /* Zero fields from bus_type to until the end */
153 memset(&endpoint->bus_type, 0, sizeof(*endpoint) -
154 offsetof(typeof(*endpoint), bus_type));
99fd133f 155
b6eec1c4
SA
156 rval = v4l2_of_parse_csi_bus(node, endpoint);
157 if (rval)
158 return rval;
99fd133f
GL
159 /*
160 * Parse the parallel video bus properties only if none
161 * of the MIPI CSI-2 specific properties were found.
162 */
163 if (endpoint->bus.mipi_csi2.flags == 0)
164 v4l2_of_parse_parallel_bus(node, endpoint);
165
9ff889b6 166 return 0;
99fd133f
GL
167}
168EXPORT_SYMBOL(v4l2_of_parse_endpoint);
c9bca8b3
LP
169
170/**
171 * v4l2_of_parse_link() - parse a link between two endpoints
172 * @node: pointer to the endpoint at the local end of the link
173 * @link: pointer to the V4L2 OF link data structure
174 *
175 * Fill the link structure with the local and remote nodes and port numbers.
176 * The local_node and remote_node fields are set to point to the local and
177 * remote port's parent nodes respectively (the port parent node being the
178 * parent node of the port node if that node isn't a 'ports' node, or the
179 * grand-parent node of the port node otherwise).
180 *
181 * A reference is taken to both the local and remote nodes, the caller must use
182 * v4l2_of_put_link() to drop the references when done with the link.
183 *
184 * Return: 0 on success, or -ENOLINK if the remote endpoint can't be found.
185 */
186int v4l2_of_parse_link(const struct device_node *node,
187 struct v4l2_of_link *link)
188{
189 struct device_node *np;
190
191 memset(link, 0, sizeof(*link));
192
193 np = of_get_parent(node);
194 of_property_read_u32(np, "reg", &link->local_port);
195 np = of_get_next_parent(np);
196 if (of_node_cmp(np->name, "ports") == 0)
197 np = of_get_next_parent(np);
198 link->local_node = np;
199
200 np = of_parse_phandle(node, "remote-endpoint", 0);
201 if (!np) {
202 of_node_put(link->local_node);
203 return -ENOLINK;
204 }
205
206 np = of_get_parent(np);
207 of_property_read_u32(np, "reg", &link->remote_port);
208 np = of_get_next_parent(np);
209 if (of_node_cmp(np->name, "ports") == 0)
210 np = of_get_next_parent(np);
211 link->remote_node = np;
212
213 return 0;
214}
215EXPORT_SYMBOL(v4l2_of_parse_link);
216
217/**
218 * v4l2_of_put_link() - drop references to nodes in a link
219 * @link: pointer to the V4L2 OF link data structure
220 *
221 * Drop references to the local and remote nodes in the link. This function must
222 * be called on every link parsed with v4l2_of_parse_link().
223 */
224void v4l2_of_put_link(struct v4l2_of_link *link)
225{
226 of_node_put(link->local_node);
227 of_node_put(link->remote_node);
228}
229EXPORT_SYMBOL(v4l2_of_put_link);
This page took 0.213321 seconds and 5 git commands to generate.