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