xref: /openbmc/linux/include/media/v4l2-fwnode.h (revision 2f828fb2)
1 /*
2  * V4L2 fwnode binding parsing library
3  *
4  * Copyright (c) 2016 Intel Corporation.
5  * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
6  *
7  * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
8  * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
9  *
10  * Copyright (C) 2012 Renesas Electronics Corp.
11  * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of version 2 of the GNU General Public License as
15  * published by the Free Software Foundation.
16  */
17 #ifndef _V4L2_FWNODE_H
18 #define _V4L2_FWNODE_H
19 
20 #include <linux/errno.h>
21 #include <linux/fwnode.h>
22 #include <linux/list.h>
23 #include <linux/types.h>
24 
25 #include <media/v4l2-mediabus.h>
26 
27 struct fwnode_handle;
28 struct v4l2_async_notifier;
29 struct v4l2_async_subdev;
30 
31 #define V4L2_FWNODE_CSI2_MAX_DATA_LANES	4
32 
33 /**
34  * struct v4l2_fwnode_bus_mipi_csi2 - MIPI CSI-2 bus data structure
35  * @flags: media bus (V4L2_MBUS_*) flags
36  * @data_lanes: an array of physical data lane indexes
37  * @clock_lane: physical lane index of the clock lane
38  * @num_data_lanes: number of data lanes
39  * @lane_polarities: polarity of the lanes. The order is the same of
40  *		   the physical lanes.
41  */
42 struct v4l2_fwnode_bus_mipi_csi2 {
43 	unsigned int flags;
44 	unsigned char data_lanes[V4L2_FWNODE_CSI2_MAX_DATA_LANES];
45 	unsigned char clock_lane;
46 	unsigned short num_data_lanes;
47 	bool lane_polarities[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
48 };
49 
50 /**
51  * struct v4l2_fwnode_bus_parallel - parallel data bus data structure
52  * @flags: media bus (V4L2_MBUS_*) flags
53  * @bus_width: bus width in bits
54  * @data_shift: data shift in bits
55  */
56 struct v4l2_fwnode_bus_parallel {
57 	unsigned int flags;
58 	unsigned char bus_width;
59 	unsigned char data_shift;
60 };
61 
62 /**
63  * struct v4l2_fwnode_bus_mipi_csi1 - CSI-1/CCP2 data bus structure
64  * @clock_inv: polarity of clock/strobe signal
65  *	       false - not inverted, true - inverted
66  * @strobe: false - data/clock, true - data/strobe
67  * @lane_polarity: the polarities of the clock (index 0) and data lanes
68  *		   index (1)
69  * @data_lane: the number of the data lane
70  * @clock_lane: the number of the clock lane
71  */
72 struct v4l2_fwnode_bus_mipi_csi1 {
73 	bool clock_inv;
74 	bool strobe;
75 	bool lane_polarity[2];
76 	unsigned char data_lane;
77 	unsigned char clock_lane;
78 };
79 
80 /**
81  * struct v4l2_fwnode_endpoint - the endpoint data structure
82  * @base: fwnode endpoint of the v4l2_fwnode
83  * @bus_type: bus type
84  * @bus: bus configuration data structure
85  * @link_frequencies: array of supported link frequencies
86  * @nr_of_link_frequencies: number of elements in link_frequenccies array
87  */
88 struct v4l2_fwnode_endpoint {
89 	struct fwnode_endpoint base;
90 	/*
91 	 * Fields below this line will be zeroed by
92 	 * v4l2_fwnode_parse_endpoint()
93 	 */
94 	enum v4l2_mbus_type bus_type;
95 	union {
96 		struct v4l2_fwnode_bus_parallel parallel;
97 		struct v4l2_fwnode_bus_mipi_csi1 mipi_csi1;
98 		struct v4l2_fwnode_bus_mipi_csi2 mipi_csi2;
99 	} bus;
100 	u64 *link_frequencies;
101 	unsigned int nr_of_link_frequencies;
102 };
103 
104 /**
105  * struct v4l2_fwnode_link - a link between two endpoints
106  * @local_node: pointer to device_node of this endpoint
107  * @local_port: identifier of the port this endpoint belongs to
108  * @remote_node: pointer to device_node of the remote endpoint
109  * @remote_port: identifier of the port the remote endpoint belongs to
110  */
111 struct v4l2_fwnode_link {
112 	struct fwnode_handle *local_node;
113 	unsigned int local_port;
114 	struct fwnode_handle *remote_node;
115 	unsigned int remote_port;
116 };
117 
118 /**
119  * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties
120  * @fwnode: pointer to the endpoint's fwnode handle
121  * @vep: pointer to the V4L2 fwnode data structure
122  *
123  * All properties are optional. If none are found, we don't set any flags. This
124  * means the port has a static configuration and no properties have to be
125  * specified explicitly. If any properties that identify the bus as parallel
126  * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if
127  * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we
128  * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a
129  * reference to @fwnode.
130  *
131  * NOTE: This function does not parse properties the size of which is variable
132  * without a low fixed limit. Please use v4l2_fwnode_endpoint_alloc_parse() in
133  * new drivers instead.
134  *
135  * Return: 0 on success or a negative error code on failure.
136  */
137 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
138 			       struct v4l2_fwnode_endpoint *vep);
139 
140 /**
141  * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by
142  * v4l2_fwnode_endpoint_alloc_parse()
143  * @vep: the V4L2 fwnode the resources of which are to be released
144  *
145  * It is safe to call this function with NULL argument or on a V4L2 fwnode the
146  * parsing of which failed.
147  */
148 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep);
149 
150 /**
151  * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties
152  * @fwnode: pointer to the endpoint's fwnode handle
153  *
154  * All properties are optional. If none are found, we don't set any flags. This
155  * means the port has a static configuration and no properties have to be
156  * specified explicitly. If any properties that identify the bus as parallel
157  * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if
158  * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we
159  * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a
160  * reference to @fwnode.
161  *
162  * v4l2_fwnode_endpoint_alloc_parse() has two important differences to
163  * v4l2_fwnode_endpoint_parse():
164  *
165  * 1. It also parses variable size data.
166  *
167  * 2. The memory it has allocated to store the variable size data must be freed
168  *    using v4l2_fwnode_endpoint_free() when no longer needed.
169  *
170  * Return: Pointer to v4l2_fwnode_endpoint if successful, on an error pointer
171  * on error.
172  */
173 struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse(
174 	struct fwnode_handle *fwnode);
175 
176 /**
177  * v4l2_fwnode_parse_link() - parse a link between two endpoints
178  * @fwnode: pointer to the endpoint's fwnode at the local end of the link
179  * @link: pointer to the V4L2 fwnode link data structure
180  *
181  * Fill the link structure with the local and remote nodes and port numbers.
182  * The local_node and remote_node fields are set to point to the local and
183  * remote port's parent nodes respectively (the port parent node being the
184  * parent node of the port node if that node isn't a 'ports' node, or the
185  * grand-parent node of the port node otherwise).
186  *
187  * A reference is taken to both the local and remote nodes, the caller must use
188  * v4l2_fwnode_put_link() to drop the references when done with the
189  * link.
190  *
191  * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be
192  * found.
193  */
194 int v4l2_fwnode_parse_link(struct fwnode_handle *fwnode,
195 			   struct v4l2_fwnode_link *link);
196 
197 /**
198  * v4l2_fwnode_put_link() - drop references to nodes in a link
199  * @link: pointer to the V4L2 fwnode link data structure
200  *
201  * Drop references to the local and remote nodes in the link. This function
202  * must be called on every link parsed with v4l2_fwnode_parse_link().
203  */
204 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link);
205 
206 
207 /**
208  * typedef parse_endpoint_func - Driver's callback function to be called on
209  *	each V4L2 fwnode endpoint.
210  *
211  * @dev: pointer to &struct device
212  * @vep: pointer to &struct v4l2_fwnode_endpoint
213  * @asd: pointer to &struct v4l2_async_subdev
214  *
215  * Return:
216  * * %0 on success
217  * * %-ENOTCONN if the endpoint is to be skipped but this
218  *   should not be considered as an error
219  * * %-EINVAL if the endpoint configuration is invalid
220  */
221 typedef int (*parse_endpoint_func)(struct device *dev,
222 				  struct v4l2_fwnode_endpoint *vep,
223 				  struct v4l2_async_subdev *asd);
224 
225 
226 /**
227  * v4l2_async_notifier_parse_fwnode_endpoints - Parse V4L2 fwnode endpoints in a
228  *						device node
229  * @dev: the device the endpoints of which are to be parsed
230  * @notifier: notifier for @dev
231  * @asd_struct_size: size of the driver's async sub-device struct, including
232  *		     sizeof(struct v4l2_async_subdev). The &struct
233  *		     v4l2_async_subdev shall be the first member of
234  *		     the driver's async sub-device struct, i.e. both
235  *		     begin at the same memory address.
236  * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
237  *		    endpoint. Optional.
238  *
239  * Parse the fwnode endpoints of the @dev device and populate the async sub-
240  * devices array of the notifier. The @parse_endpoint callback function is
241  * called for each endpoint with the corresponding async sub-device pointer to
242  * let the caller initialize the driver-specific part of the async sub-device
243  * structure.
244  *
245  * The notifier memory shall be zeroed before this function is called on the
246  * notifier.
247  *
248  * This function may not be called on a registered notifier and may be called on
249  * a notifier only once.
250  *
251  * Do not change the notifier's subdevs array, take references to the subdevs
252  * array itself or change the notifier's num_subdevs field. This is because this
253  * function allocates and reallocates the subdevs array based on parsing
254  * endpoints.
255  *
256  * The &struct v4l2_fwnode_endpoint passed to the callback function
257  * @parse_endpoint is released once the function is finished. If there is a need
258  * to retain that configuration, the user needs to allocate memory for it.
259  *
260  * Any notifier populated using this function must be released with a call to
261  * v4l2_async_notifier_cleanup() after it has been unregistered and the async
262  * sub-devices are no longer in use, even if the function returned an error.
263  *
264  * Return: %0 on success, including when no async sub-devices are found
265  *	   %-ENOMEM if memory allocation failed
266  *	   %-EINVAL if graph or endpoint parsing failed
267  *	   Other error codes as returned by @parse_endpoint
268  */
269 int v4l2_async_notifier_parse_fwnode_endpoints(
270 	struct device *dev, struct v4l2_async_notifier *notifier,
271 	size_t asd_struct_size,
272 	parse_endpoint_func parse_endpoint);
273 
274 /**
275  * v4l2_async_notifier_parse_fwnode_endpoints_by_port - Parse V4L2 fwnode
276  *							endpoints of a port in a
277  *							device node
278  * @dev: the device the endpoints of which are to be parsed
279  * @notifier: notifier for @dev
280  * @asd_struct_size: size of the driver's async sub-device struct, including
281  *		     sizeof(struct v4l2_async_subdev). The &struct
282  *		     v4l2_async_subdev shall be the first member of
283  *		     the driver's async sub-device struct, i.e. both
284  *		     begin at the same memory address.
285  * @port: port number where endpoints are to be parsed
286  * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
287  *		    endpoint. Optional.
288  *
289  * This function is just like v4l2_async_notifier_parse_fwnode_endpoints() with
290  * the exception that it only parses endpoints in a given port. This is useful
291  * on devices that have both sinks and sources: the async sub-devices connected
292  * to sources have already been configured by another driver (on capture
293  * devices). In this case the driver must know which ports to parse.
294  *
295  * Parse the fwnode endpoints of the @dev device on a given @port and populate
296  * the async sub-devices array of the notifier. The @parse_endpoint callback
297  * function is called for each endpoint with the corresponding async sub-device
298  * pointer to let the caller initialize the driver-specific part of the async
299  * sub-device structure.
300  *
301  * The notifier memory shall be zeroed before this function is called on the
302  * notifier the first time.
303  *
304  * This function may not be called on a registered notifier and may be called on
305  * a notifier only once per port.
306  *
307  * Do not change the notifier's subdevs array, take references to the subdevs
308  * array itself or change the notifier's num_subdevs field. This is because this
309  * function allocates and reallocates the subdevs array based on parsing
310  * endpoints.
311  *
312  * The &struct v4l2_fwnode_endpoint passed to the callback function
313  * @parse_endpoint is released once the function is finished. If there is a need
314  * to retain that configuration, the user needs to allocate memory for it.
315  *
316  * Any notifier populated using this function must be released with a call to
317  * v4l2_async_notifier_cleanup() after it has been unregistered and the async
318  * sub-devices are no longer in use, even if the function returned an error.
319  *
320  * Return: %0 on success, including when no async sub-devices are found
321  *	   %-ENOMEM if memory allocation failed
322  *	   %-EINVAL if graph or endpoint parsing failed
323  *	   Other error codes as returned by @parse_endpoint
324  */
325 int v4l2_async_notifier_parse_fwnode_endpoints_by_port(
326 	struct device *dev, struct v4l2_async_notifier *notifier,
327 	size_t asd_struct_size, unsigned int port,
328 	parse_endpoint_func parse_endpoint);
329 
330 /**
331  * v4l2_fwnode_reference_parse_sensor_common - parse common references on
332  *					       sensors for async sub-devices
333  * @dev: the device node the properties of which are parsed for references
334  * @notifier: the async notifier where the async subdevs will be added
335  *
336  * Parse common sensor properties for remote devices related to the
337  * sensor and set up async sub-devices for them.
338  *
339  * Any notifier populated using this function must be released with a call to
340  * v4l2_async_notifier_release() after it has been unregistered and the async
341  * sub-devices are no longer in use, even in the case the function returned an
342  * error.
343  *
344  * Return: 0 on success
345  *	   -ENOMEM if memory allocation failed
346  *	   -EINVAL if property parsing failed
347  */
348 int v4l2_async_notifier_parse_fwnode_sensor_common(
349 	struct device *dev, struct v4l2_async_notifier *notifier);
350 
351 #endif /* _V4L2_FWNODE_H */
352