xref: /openbmc/linux/include/media/v4l2-fwnode.h (revision 5b394b2d)
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: union with bus configuration data structure
85  * @bus.parallel: embedded &struct v4l2_fwnode_bus_parallel.
86  *		  Used if the bus is parallel.
87  * @bus.mipi_csi1: embedded &struct v4l2_fwnode_bus_mipi_csi1.
88  *		   Used if the bus is MIPI Alliance's Camera Serial
89  *		   Interface version 1 (MIPI CSI1) or Standard
90  *		   Mobile Imaging Architecture's Compact Camera Port 2
91  *		   (SMIA CCP2).
92  * @bus.mipi_csi2: embedded &struct v4l2_fwnode_bus_mipi_csi2.
93  *		   Used if the bus is MIPI Alliance's Camera Serial
94  *		   Interface version 2 (MIPI CSI2).
95  * @link_frequencies: array of supported link frequencies
96  * @nr_of_link_frequencies: number of elements in link_frequenccies array
97  */
98 struct v4l2_fwnode_endpoint {
99 	struct fwnode_endpoint base;
100 	/*
101 	 * Fields below this line will be zeroed by
102 	 * v4l2_fwnode_endpoint_parse()
103 	 */
104 	enum v4l2_mbus_type bus_type;
105 	union {
106 		struct v4l2_fwnode_bus_parallel parallel;
107 		struct v4l2_fwnode_bus_mipi_csi1 mipi_csi1;
108 		struct v4l2_fwnode_bus_mipi_csi2 mipi_csi2;
109 	} bus;
110 	u64 *link_frequencies;
111 	unsigned int nr_of_link_frequencies;
112 };
113 
114 /**
115  * struct v4l2_fwnode_link - a link between two endpoints
116  * @local_node: pointer to device_node of this endpoint
117  * @local_port: identifier of the port this endpoint belongs to
118  * @remote_node: pointer to device_node of the remote endpoint
119  * @remote_port: identifier of the port the remote endpoint belongs to
120  */
121 struct v4l2_fwnode_link {
122 	struct fwnode_handle *local_node;
123 	unsigned int local_port;
124 	struct fwnode_handle *remote_node;
125 	unsigned int remote_port;
126 };
127 
128 /**
129  * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties
130  * @fwnode: pointer to the endpoint's fwnode handle
131  * @vep: pointer to the V4L2 fwnode data structure
132  *
133  * All properties are optional. If none are found, we don't set any flags. This
134  * means the port has a static configuration and no properties have to be
135  * specified explicitly. If any properties that identify the bus as parallel
136  * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if
137  * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we
138  * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a
139  * reference to @fwnode.
140  *
141  * NOTE: This function does not parse properties the size of which is variable
142  * without a low fixed limit. Please use v4l2_fwnode_endpoint_alloc_parse() in
143  * new drivers instead.
144  *
145  * Return: 0 on success or a negative error code on failure.
146  */
147 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
148 			       struct v4l2_fwnode_endpoint *vep);
149 
150 /**
151  * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by
152  * v4l2_fwnode_endpoint_alloc_parse()
153  * @vep: the V4L2 fwnode the resources of which are to be released
154  *
155  * It is safe to call this function with NULL argument or on a V4L2 fwnode the
156  * parsing of which failed.
157  */
158 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep);
159 
160 /**
161  * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties
162  * @fwnode: pointer to the endpoint's fwnode handle
163  *
164  * All properties are optional. If none are found, we don't set any flags. This
165  * means the port has a static configuration and no properties have to be
166  * specified explicitly. If any properties that identify the bus as parallel
167  * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if
168  * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we
169  * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a
170  * reference to @fwnode.
171  *
172  * v4l2_fwnode_endpoint_alloc_parse() has two important differences to
173  * v4l2_fwnode_endpoint_parse():
174  *
175  * 1. It also parses variable size data.
176  *
177  * 2. The memory it has allocated to store the variable size data must be freed
178  *    using v4l2_fwnode_endpoint_free() when no longer needed.
179  *
180  * Return: Pointer to v4l2_fwnode_endpoint if successful, on an error pointer
181  * on error.
182  */
183 struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse(
184 	struct fwnode_handle *fwnode);
185 
186 /**
187  * v4l2_fwnode_parse_link() - parse a link between two endpoints
188  * @fwnode: pointer to the endpoint's fwnode at the local end of the link
189  * @link: pointer to the V4L2 fwnode link data structure
190  *
191  * Fill the link structure with the local and remote nodes and port numbers.
192  * The local_node and remote_node fields are set to point to the local and
193  * remote port's parent nodes respectively (the port parent node being the
194  * parent node of the port node if that node isn't a 'ports' node, or the
195  * grand-parent node of the port node otherwise).
196  *
197  * A reference is taken to both the local and remote nodes, the caller must use
198  * v4l2_fwnode_put_link() to drop the references when done with the
199  * link.
200  *
201  * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be
202  * found.
203  */
204 int v4l2_fwnode_parse_link(struct fwnode_handle *fwnode,
205 			   struct v4l2_fwnode_link *link);
206 
207 /**
208  * v4l2_fwnode_put_link() - drop references to nodes in a link
209  * @link: pointer to the V4L2 fwnode link data structure
210  *
211  * Drop references to the local and remote nodes in the link. This function
212  * must be called on every link parsed with v4l2_fwnode_parse_link().
213  */
214 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link);
215 
216 
217 /**
218  * typedef parse_endpoint_func - Driver's callback function to be called on
219  *	each V4L2 fwnode endpoint.
220  *
221  * @dev: pointer to &struct device
222  * @vep: pointer to &struct v4l2_fwnode_endpoint
223  * @asd: pointer to &struct v4l2_async_subdev
224  *
225  * Return:
226  * * %0 on success
227  * * %-ENOTCONN if the endpoint is to be skipped but this
228  *   should not be considered as an error
229  * * %-EINVAL if the endpoint configuration is invalid
230  */
231 typedef int (*parse_endpoint_func)(struct device *dev,
232 				  struct v4l2_fwnode_endpoint *vep,
233 				  struct v4l2_async_subdev *asd);
234 
235 
236 /**
237  * v4l2_async_notifier_parse_fwnode_endpoints - Parse V4L2 fwnode endpoints in a
238  *						device node
239  * @dev: the device the endpoints of which are to be parsed
240  * @notifier: notifier for @dev
241  * @asd_struct_size: size of the driver's async sub-device struct, including
242  *		     sizeof(struct v4l2_async_subdev). The &struct
243  *		     v4l2_async_subdev shall be the first member of
244  *		     the driver's async sub-device struct, i.e. both
245  *		     begin at the same memory address.
246  * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
247  *		    endpoint. Optional.
248  *
249  * Parse the fwnode endpoints of the @dev device and populate the async sub-
250  * devices array of the notifier. The @parse_endpoint callback function is
251  * called for each endpoint with the corresponding async sub-device pointer to
252  * let the caller initialize the driver-specific part of the async sub-device
253  * structure.
254  *
255  * The notifier memory shall be zeroed before this function is called on the
256  * notifier.
257  *
258  * This function may not be called on a registered notifier and may be called on
259  * a notifier only once.
260  *
261  * Do not change the notifier's subdevs array, take references to the subdevs
262  * array itself or change the notifier's num_subdevs field. This is because this
263  * function allocates and reallocates the subdevs array based on parsing
264  * endpoints.
265  *
266  * The &struct v4l2_fwnode_endpoint passed to the callback function
267  * @parse_endpoint is released once the function is finished. If there is a need
268  * to retain that configuration, the user needs to allocate memory for it.
269  *
270  * Any notifier populated using this function must be released with a call to
271  * v4l2_async_notifier_cleanup() after it has been unregistered and the async
272  * sub-devices are no longer in use, even if the function returned an error.
273  *
274  * Return: %0 on success, including when no async sub-devices are found
275  *	   %-ENOMEM if memory allocation failed
276  *	   %-EINVAL if graph or endpoint parsing failed
277  *	   Other error codes as returned by @parse_endpoint
278  */
279 int v4l2_async_notifier_parse_fwnode_endpoints(
280 	struct device *dev, struct v4l2_async_notifier *notifier,
281 	size_t asd_struct_size,
282 	parse_endpoint_func parse_endpoint);
283 
284 /**
285  * v4l2_async_notifier_parse_fwnode_endpoints_by_port - Parse V4L2 fwnode
286  *							endpoints of a port in a
287  *							device node
288  * @dev: the device the endpoints of which are to be parsed
289  * @notifier: notifier for @dev
290  * @asd_struct_size: size of the driver's async sub-device struct, including
291  *		     sizeof(struct v4l2_async_subdev). The &struct
292  *		     v4l2_async_subdev shall be the first member of
293  *		     the driver's async sub-device struct, i.e. both
294  *		     begin at the same memory address.
295  * @port: port number where endpoints are to be parsed
296  * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
297  *		    endpoint. Optional.
298  *
299  * This function is just like v4l2_async_notifier_parse_fwnode_endpoints() with
300  * the exception that it only parses endpoints in a given port. This is useful
301  * on devices that have both sinks and sources: the async sub-devices connected
302  * to sources have already been configured by another driver (on capture
303  * devices). In this case the driver must know which ports to parse.
304  *
305  * Parse the fwnode endpoints of the @dev device on a given @port and populate
306  * the async sub-devices array of the notifier. The @parse_endpoint callback
307  * function is called for each endpoint with the corresponding async sub-device
308  * pointer to let the caller initialize the driver-specific part of the async
309  * sub-device structure.
310  *
311  * The notifier memory shall be zeroed before this function is called on the
312  * notifier the first time.
313  *
314  * This function may not be called on a registered notifier and may be called on
315  * a notifier only once per port.
316  *
317  * Do not change the notifier's subdevs array, take references to the subdevs
318  * array itself or change the notifier's num_subdevs field. This is because this
319  * function allocates and reallocates the subdevs array based on parsing
320  * endpoints.
321  *
322  * The &struct v4l2_fwnode_endpoint passed to the callback function
323  * @parse_endpoint is released once the function is finished. If there is a need
324  * to retain that configuration, the user needs to allocate memory for it.
325  *
326  * Any notifier populated using this function must be released with a call to
327  * v4l2_async_notifier_cleanup() after it has been unregistered and the async
328  * sub-devices are no longer in use, even if the function returned an error.
329  *
330  * Return: %0 on success, including when no async sub-devices are found
331  *	   %-ENOMEM if memory allocation failed
332  *	   %-EINVAL if graph or endpoint parsing failed
333  *	   Other error codes as returned by @parse_endpoint
334  */
335 int v4l2_async_notifier_parse_fwnode_endpoints_by_port(
336 	struct device *dev, struct v4l2_async_notifier *notifier,
337 	size_t asd_struct_size, unsigned int port,
338 	parse_endpoint_func parse_endpoint);
339 
340 /**
341  * v4l2_fwnode_reference_parse_sensor_common - parse common references on
342  *					       sensors for async sub-devices
343  * @dev: the device node the properties of which are parsed for references
344  * @notifier: the async notifier where the async subdevs will be added
345  *
346  * Parse common sensor properties for remote devices related to the
347  * sensor and set up async sub-devices for them.
348  *
349  * Any notifier populated using this function must be released with a call to
350  * v4l2_async_notifier_release() after it has been unregistered and the async
351  * sub-devices are no longer in use, even in the case the function returned an
352  * error.
353  *
354  * Return: 0 on success
355  *	   -ENOMEM if memory allocation failed
356  *	   -EINVAL if property parsing failed
357  */
358 int v4l2_async_notifier_parse_fwnode_sensor_common(
359 	struct device *dev, struct v4l2_async_notifier *notifier);
360 
361 #endif /* _V4L2_FWNODE_H */
362