xref: /openbmc/linux/include/media/v4l2-fwnode.h (revision 15e3ae36)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * V4L2 fwnode binding parsing library
4  *
5  * Copyright (c) 2016 Intel Corporation.
6  * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
7  *
8  * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
9  * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
10  *
11  * Copyright (C) 2012 Renesas Electronics Corp.
12  * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
13  */
14 #ifndef _V4L2_FWNODE_H
15 #define _V4L2_FWNODE_H
16 
17 #include <linux/errno.h>
18 #include <linux/fwnode.h>
19 #include <linux/list.h>
20 #include <linux/types.h>
21 
22 #include <media/v4l2-mediabus.h>
23 #include <media/v4l2-subdev.h>
24 
25 struct fwnode_handle;
26 struct v4l2_async_notifier;
27 struct v4l2_async_subdev;
28 
29 #define V4L2_FWNODE_CSI2_MAX_DATA_LANES	4
30 
31 /**
32  * struct v4l2_fwnode_bus_mipi_csi2 - MIPI CSI-2 bus data structure
33  * @flags: media bus (V4L2_MBUS_*) flags
34  * @data_lanes: an array of physical data lane indexes
35  * @clock_lane: physical lane index of the clock lane
36  * @num_data_lanes: number of data lanes
37  * @lane_polarities: polarity of the lanes. The order is the same of
38  *		   the physical lanes.
39  */
40 struct v4l2_fwnode_bus_mipi_csi2 {
41 	unsigned int flags;
42 	unsigned char data_lanes[V4L2_FWNODE_CSI2_MAX_DATA_LANES];
43 	unsigned char clock_lane;
44 	unsigned short num_data_lanes;
45 	bool lane_polarities[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
46 };
47 
48 /**
49  * struct v4l2_fwnode_bus_parallel - parallel data bus data structure
50  * @flags: media bus (V4L2_MBUS_*) flags
51  * @bus_width: bus width in bits
52  * @data_shift: data shift in bits
53  */
54 struct v4l2_fwnode_bus_parallel {
55 	unsigned int flags;
56 	unsigned char bus_width;
57 	unsigned char data_shift;
58 };
59 
60 /**
61  * struct v4l2_fwnode_bus_mipi_csi1 - CSI-1/CCP2 data bus structure
62  * @clock_inv: polarity of clock/strobe signal
63  *	       false - not inverted, true - inverted
64  * @strobe: false - data/clock, true - data/strobe
65  * @lane_polarity: the polarities of the clock (index 0) and data lanes
66  *		   index (1)
67  * @data_lane: the number of the data lane
68  * @clock_lane: the number of the clock lane
69  */
70 struct v4l2_fwnode_bus_mipi_csi1 {
71 	unsigned char clock_inv:1;
72 	unsigned char strobe:1;
73 	bool lane_polarity[2];
74 	unsigned char data_lane;
75 	unsigned char clock_lane;
76 };
77 
78 /**
79  * struct v4l2_fwnode_endpoint - the endpoint data structure
80  * @base: fwnode endpoint of the v4l2_fwnode
81  * @bus_type: bus type
82  * @bus: union with bus configuration data structure
83  * @bus.parallel: embedded &struct v4l2_fwnode_bus_parallel.
84  *		  Used if the bus is parallel.
85  * @bus.mipi_csi1: embedded &struct v4l2_fwnode_bus_mipi_csi1.
86  *		   Used if the bus is MIPI Alliance's Camera Serial
87  *		   Interface version 1 (MIPI CSI1) or Standard
88  *		   Mobile Imaging Architecture's Compact Camera Port 2
89  *		   (SMIA CCP2).
90  * @bus.mipi_csi2: embedded &struct v4l2_fwnode_bus_mipi_csi2.
91  *		   Used if the bus is MIPI Alliance's Camera Serial
92  *		   Interface version 2 (MIPI CSI2).
93  * @link_frequencies: array of supported link frequencies
94  * @nr_of_link_frequencies: number of elements in link_frequenccies array
95  */
96 struct v4l2_fwnode_endpoint {
97 	struct fwnode_endpoint base;
98 	/*
99 	 * Fields below this line will be zeroed by
100 	 * v4l2_fwnode_endpoint_parse()
101 	 */
102 	enum v4l2_mbus_type bus_type;
103 	union {
104 		struct v4l2_fwnode_bus_parallel parallel;
105 		struct v4l2_fwnode_bus_mipi_csi1 mipi_csi1;
106 		struct v4l2_fwnode_bus_mipi_csi2 mipi_csi2;
107 	} bus;
108 	u64 *link_frequencies;
109 	unsigned int nr_of_link_frequencies;
110 };
111 
112 /**
113  * struct v4l2_fwnode_link - a link between two endpoints
114  * @local_node: pointer to device_node of this endpoint
115  * @local_port: identifier of the port this endpoint belongs to
116  * @local_id: identifier of the id this endpoint belongs to
117  * @remote_node: pointer to device_node of the remote endpoint
118  * @remote_port: identifier of the port the remote endpoint belongs to
119  * @remote_id: identifier of the id the remote endpoint belongs to
120  */
121 struct v4l2_fwnode_link {
122 	struct fwnode_handle *local_node;
123 	unsigned int local_port;
124 	unsigned int local_id;
125 	struct fwnode_handle *remote_node;
126 	unsigned int remote_port;
127 	unsigned int remote_id;
128 };
129 
130 /**
131  * enum v4l2_connector_type - connector type
132  * @V4L2_CONN_UNKNOWN:   unknown connector type, no V4L2 connector configuration
133  * @V4L2_CONN_COMPOSITE: analog composite connector
134  * @V4L2_CONN_SVIDEO:    analog svideo connector
135  */
136 enum v4l2_connector_type {
137 	V4L2_CONN_UNKNOWN,
138 	V4L2_CONN_COMPOSITE,
139 	V4L2_CONN_SVIDEO,
140 };
141 
142 /**
143  * struct v4l2_connector_link - connector link data structure
144  * @head: structure to be used to add the link to the
145  *        &struct v4l2_fwnode_connector
146  * @fwnode_link: &struct v4l2_fwnode_link link between the connector and the
147  *               device the connector belongs to.
148  */
149 struct v4l2_connector_link {
150 	struct list_head head;
151 	struct v4l2_fwnode_link fwnode_link;
152 };
153 
154 /**
155  * struct v4l2_fwnode_connector_analog - analog connector data structure
156  * @sdtv_stds: sdtv standards this connector supports, set to V4L2_STD_ALL
157  *             if no restrictions are specified.
158  */
159 struct v4l2_fwnode_connector_analog {
160 	v4l2_std_id sdtv_stds;
161 };
162 
163 /**
164  * struct v4l2_fwnode_connector - the connector data structure
165  * @name: the connector device name
166  * @label: optional connector label
167  * @type: connector type
168  * @links: list of all connector &struct v4l2_connector_link links
169  * @nr_of_links: total number of links
170  * @connector: connector configuration
171  * @connector.analog: analog connector configuration
172  *                    &struct v4l2_fwnode_connector_analog
173  */
174 struct v4l2_fwnode_connector {
175 	const char *name;
176 	const char *label;
177 	enum v4l2_connector_type type;
178 	struct list_head links;
179 	unsigned int nr_of_links;
180 
181 	union {
182 		struct v4l2_fwnode_connector_analog analog;
183 		/* future connectors */
184 	} connector;
185 };
186 
187 /**
188  * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties
189  * @fwnode: pointer to the endpoint's fwnode handle
190  * @vep: pointer to the V4L2 fwnode data structure
191  *
192  * This function parses the V4L2 fwnode endpoint specific parameters from the
193  * firmware. The caller is responsible for assigning @vep.bus_type to a valid
194  * media bus type. The caller may also set the default configuration for the
195  * endpoint --- a configuration that shall be in line with the DT binding
196  * documentation. Should a device support multiple bus types, the caller may
197  * call this function once the correct type is found --- with a default
198  * configuration valid for that type.
199  *
200  * As a compatibility means guessing the bus type is also supported by setting
201  * @vep.bus_type to V4L2_MBUS_UNKNOWN. The caller may not provide a default
202  * configuration in this case as the defaults are specific to a given bus type.
203  * This functionality is deprecated and should not be used in new drivers and it
204  * is only supported for CSI-2 D-PHY, parallel and Bt.656 buses.
205  *
206  * The function does not change the V4L2 fwnode endpoint state if it fails.
207  *
208  * NOTE: This function does not parse properties the size of which is variable
209  * without a low fixed limit. Please use v4l2_fwnode_endpoint_alloc_parse() in
210  * new drivers instead.
211  *
212  * Return: %0 on success or a negative error code on failure:
213  *	   %-ENOMEM on memory allocation failure
214  *	   %-EINVAL on parsing failure
215  *	   %-ENXIO on mismatching bus types
216  */
217 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
218 			       struct v4l2_fwnode_endpoint *vep);
219 
220 /**
221  * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by
222  * v4l2_fwnode_endpoint_alloc_parse()
223  * @vep: the V4L2 fwnode the resources of which are to be released
224  *
225  * It is safe to call this function with NULL argument or on a V4L2 fwnode the
226  * parsing of which failed.
227  */
228 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep);
229 
230 /**
231  * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties
232  * @fwnode: pointer to the endpoint's fwnode handle
233  * @vep: pointer to the V4L2 fwnode data structure
234  *
235  * This function parses the V4L2 fwnode endpoint specific parameters from the
236  * firmware. The caller is responsible for assigning @vep.bus_type to a valid
237  * media bus type. The caller may also set the default configuration for the
238  * endpoint --- a configuration that shall be in line with the DT binding
239  * documentation. Should a device support multiple bus types, the caller may
240  * call this function once the correct type is found --- with a default
241  * configuration valid for that type.
242  *
243  * As a compatibility means guessing the bus type is also supported by setting
244  * @vep.bus_type to V4L2_MBUS_UNKNOWN. The caller may not provide a default
245  * configuration in this case as the defaults are specific to a given bus type.
246  * This functionality is deprecated and should not be used in new drivers and it
247  * is only supported for CSI-2 D-PHY, parallel and Bt.656 buses.
248  *
249  * The function does not change the V4L2 fwnode endpoint state if it fails.
250  *
251  * v4l2_fwnode_endpoint_alloc_parse() has two important differences to
252  * v4l2_fwnode_endpoint_parse():
253  *
254  * 1. It also parses variable size data.
255  *
256  * 2. The memory it has allocated to store the variable size data must be freed
257  *    using v4l2_fwnode_endpoint_free() when no longer needed.
258  *
259  * Return: %0 on success or a negative error code on failure:
260  *	   %-ENOMEM on memory allocation failure
261  *	   %-EINVAL on parsing failure
262  *	   %-ENXIO on mismatching bus types
263  */
264 int v4l2_fwnode_endpoint_alloc_parse(struct fwnode_handle *fwnode,
265 				     struct v4l2_fwnode_endpoint *vep);
266 
267 /**
268  * v4l2_fwnode_parse_link() - parse a link between two endpoints
269  * @fwnode: pointer to the endpoint's fwnode at the local end of the link
270  * @link: pointer to the V4L2 fwnode link data structure
271  *
272  * Fill the link structure with the local and remote nodes and port numbers.
273  * The local_node and remote_node fields are set to point to the local and
274  * remote port's parent nodes respectively (the port parent node being the
275  * parent node of the port node if that node isn't a 'ports' node, or the
276  * grand-parent node of the port node otherwise).
277  *
278  * A reference is taken to both the local and remote nodes, the caller must use
279  * v4l2_fwnode_put_link() to drop the references when done with the
280  * link.
281  *
282  * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be
283  * found.
284  */
285 int v4l2_fwnode_parse_link(struct fwnode_handle *fwnode,
286 			   struct v4l2_fwnode_link *link);
287 
288 /**
289  * v4l2_fwnode_put_link() - drop references to nodes in a link
290  * @link: pointer to the V4L2 fwnode link data structure
291  *
292  * Drop references to the local and remote nodes in the link. This function
293  * must be called on every link parsed with v4l2_fwnode_parse_link().
294  */
295 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link);
296 
297 /**
298  * v4l2_fwnode_connector_free() - free the V4L2 connector acquired memory
299  * @connector: the V4L2 connector resources of which are to be released
300  *
301  * Free all allocated memory and put all links acquired by
302  * v4l2_fwnode_connector_parse() and v4l2_fwnode_connector_add_link().
303  *
304  * It is safe to call this function with NULL argument or on a V4L2 connector
305  * the parsing of which failed.
306  */
307 void v4l2_fwnode_connector_free(struct v4l2_fwnode_connector *connector);
308 
309 /**
310  * v4l2_fwnode_connector_parse() - initialize the 'struct v4l2_fwnode_connector'
311  * @fwnode: pointer to the subdev endpoint's fwnode handle where the connector
312  *	    is connected to or to the connector endpoint fwnode handle.
313  * @connector: pointer to the V4L2 fwnode connector data structure
314  *
315  * Fill the &struct v4l2_fwnode_connector with the connector type, label and
316  * all &enum v4l2_connector_type specific connector data. The label is optional
317  * so it is set to %NULL if no one was found. The function initialize the links
318  * to zero. Adding links to the connector is done by calling
319  * v4l2_fwnode_connector_add_link().
320  *
321  * The memory allocated for the label must be freed when no longer needed.
322  * Freeing the memory is done by v4l2_fwnode_connector_free().
323  *
324  * Return:
325  * * %0 on success or a negative error code on failure:
326  * * %-EINVAL if @fwnode is invalid
327  * * %-ENOTCONN if connector type is unknown or connector device can't be found
328  */
329 int v4l2_fwnode_connector_parse(struct fwnode_handle *fwnode,
330 				struct v4l2_fwnode_connector *connector);
331 
332 /**
333  * v4l2_fwnode_connector_add_link - add a link between a connector node and
334  *				    a v4l2-subdev node.
335  * @fwnode: pointer to the subdev endpoint's fwnode handle where the connector
336  *          is connected to
337  * @connector: pointer to the V4L2 fwnode connector data structure
338  *
339  * Add a new &struct v4l2_connector_link link to the
340  * &struct v4l2_fwnode_connector connector links list. The link local_node
341  * points to the connector node, the remote_node to the host v4l2 (sub)dev.
342  *
343  * The taken references to remote_node and local_node must be dropped and the
344  * allocated memory must be freed when no longer needed. Both is done by calling
345  * v4l2_fwnode_connector_free().
346  *
347  * Return:
348  * * %0 on success or a negative error code on failure:
349  * * %-EINVAL if @fwnode or @connector is invalid or @connector type is unknown
350  * * %-ENOMEM on link memory allocation failure
351  * * %-ENOTCONN if remote connector device can't be found
352  * * %-ENOLINK if link parsing between v4l2 (sub)dev and connector fails
353  */
354 int v4l2_fwnode_connector_add_link(struct fwnode_handle *fwnode,
355 				   struct v4l2_fwnode_connector *connector);
356 
357 /**
358  * typedef parse_endpoint_func - Driver's callback function to be called on
359  *	each V4L2 fwnode endpoint.
360  *
361  * @dev: pointer to &struct device
362  * @vep: pointer to &struct v4l2_fwnode_endpoint
363  * @asd: pointer to &struct v4l2_async_subdev
364  *
365  * Return:
366  * * %0 on success
367  * * %-ENOTCONN if the endpoint is to be skipped but this
368  *   should not be considered as an error
369  * * %-EINVAL if the endpoint configuration is invalid
370  */
371 typedef int (*parse_endpoint_func)(struct device *dev,
372 				  struct v4l2_fwnode_endpoint *vep,
373 				  struct v4l2_async_subdev *asd);
374 
375 /**
376  * v4l2_async_notifier_parse_fwnode_endpoints - Parse V4L2 fwnode endpoints in a
377  *						device node
378  * @dev: the device the endpoints of which are to be parsed
379  * @notifier: notifier for @dev
380  * @asd_struct_size: size of the driver's async sub-device struct, including
381  *		     sizeof(struct v4l2_async_subdev). The &struct
382  *		     v4l2_async_subdev shall be the first member of
383  *		     the driver's async sub-device struct, i.e. both
384  *		     begin at the same memory address.
385  * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
386  *		    endpoint. Optional.
387  *
388  * Parse the fwnode endpoints of the @dev device and populate the async sub-
389  * devices list in the notifier. The @parse_endpoint callback function is
390  * called for each endpoint with the corresponding async sub-device pointer to
391  * let the caller initialize the driver-specific part of the async sub-device
392  * structure.
393  *
394  * The notifier memory shall be zeroed before this function is called on the
395  * notifier.
396  *
397  * This function may not be called on a registered notifier and may be called on
398  * a notifier only once.
399  *
400  * The &struct v4l2_fwnode_endpoint passed to the callback function
401  * @parse_endpoint is released once the function is finished. If there is a need
402  * to retain that configuration, the user needs to allocate memory for it.
403  *
404  * Any notifier populated using this function must be released with a call to
405  * v4l2_async_notifier_cleanup() after it has been unregistered and the async
406  * sub-devices are no longer in use, even if the function returned an error.
407  *
408  * Return: %0 on success, including when no async sub-devices are found
409  *	   %-ENOMEM if memory allocation failed
410  *	   %-EINVAL if graph or endpoint parsing failed
411  *	   Other error codes as returned by @parse_endpoint
412  */
413 int
414 v4l2_async_notifier_parse_fwnode_endpoints(struct device *dev,
415 					   struct v4l2_async_notifier *notifier,
416 					   size_t asd_struct_size,
417 					   parse_endpoint_func parse_endpoint);
418 
419 /**
420  * v4l2_async_notifier_parse_fwnode_endpoints_by_port - Parse V4L2 fwnode
421  *							endpoints of a port in a
422  *							device node
423  * @dev: the device the endpoints of which are to be parsed
424  * @notifier: notifier for @dev
425  * @asd_struct_size: size of the driver's async sub-device struct, including
426  *		     sizeof(struct v4l2_async_subdev). The &struct
427  *		     v4l2_async_subdev shall be the first member of
428  *		     the driver's async sub-device struct, i.e. both
429  *		     begin at the same memory address.
430  * @port: port number where endpoints are to be parsed
431  * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
432  *		    endpoint. Optional.
433  *
434  * This function is just like v4l2_async_notifier_parse_fwnode_endpoints() with
435  * the exception that it only parses endpoints in a given port. This is useful
436  * on devices that have both sinks and sources: the async sub-devices connected
437  * to sources have already been configured by another driver (on capture
438  * devices). In this case the driver must know which ports to parse.
439  *
440  * Parse the fwnode endpoints of the @dev device on a given @port and populate
441  * the async sub-devices list of the notifier. The @parse_endpoint callback
442  * function is called for each endpoint with the corresponding async sub-device
443  * pointer to let the caller initialize the driver-specific part of the async
444  * sub-device structure.
445  *
446  * The notifier memory shall be zeroed before this function is called on the
447  * notifier the first time.
448  *
449  * This function may not be called on a registered notifier and may be called on
450  * a notifier only once per port.
451  *
452  * The &struct v4l2_fwnode_endpoint passed to the callback function
453  * @parse_endpoint is released once the function is finished. If there is a need
454  * to retain that configuration, the user needs to allocate memory for it.
455  *
456  * Any notifier populated using this function must be released with a call to
457  * v4l2_async_notifier_cleanup() after it has been unregistered and the async
458  * sub-devices are no longer in use, even if the function returned an error.
459  *
460  * Return: %0 on success, including when no async sub-devices are found
461  *	   %-ENOMEM if memory allocation failed
462  *	   %-EINVAL if graph or endpoint parsing failed
463  *	   Other error codes as returned by @parse_endpoint
464  */
465 int
466 v4l2_async_notifier_parse_fwnode_endpoints_by_port(struct device *dev,
467 						   struct v4l2_async_notifier *notifier,
468 						   size_t asd_struct_size,
469 						   unsigned int port,
470 						   parse_endpoint_func parse_endpoint);
471 
472 /**
473  * v4l2_fwnode_reference_parse_sensor_common - parse common references on
474  *					       sensors for async sub-devices
475  * @dev: the device node the properties of which are parsed for references
476  * @notifier: the async notifier where the async subdevs will be added
477  *
478  * Parse common sensor properties for remote devices related to the
479  * sensor and set up async sub-devices for them.
480  *
481  * Any notifier populated using this function must be released with a call to
482  * v4l2_async_notifier_release() after it has been unregistered and the async
483  * sub-devices are no longer in use, even in the case the function returned an
484  * error.
485  *
486  * Return: 0 on success
487  *	   -ENOMEM if memory allocation failed
488  *	   -EINVAL if property parsing failed
489  */
490 int v4l2_async_notifier_parse_fwnode_sensor_common(struct device *dev,
491 						   struct v4l2_async_notifier *notifier);
492 
493 /**
494  * v4l2_async_register_fwnode_subdev - registers a sub-device to the
495  *					asynchronous sub-device framework
496  *					and parses fwnode endpoints
497  *
498  * @sd: pointer to struct &v4l2_subdev
499  * @asd_struct_size: size of the driver's async sub-device struct, including
500  *		     sizeof(struct v4l2_async_subdev). The &struct
501  *		     v4l2_async_subdev shall be the first member of
502  *		     the driver's async sub-device struct, i.e. both
503  *		     begin at the same memory address.
504  * @ports: array of port id's to parse for fwnode endpoints. If NULL, will
505  *	   parse all ports owned by the sub-device.
506  * @num_ports: number of ports in @ports array. Ignored if @ports is NULL.
507  * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
508  *		    endpoint. Optional.
509  *
510  * This function is just like v4l2_async_register_subdev() with the
511  * exception that calling it will also allocate a notifier for the
512  * sub-device, parse the sub-device's firmware node endpoints using
513  * v4l2_async_notifier_parse_fwnode_endpoints() or
514  * v4l2_async_notifier_parse_fwnode_endpoints_by_port(), and
515  * registers the sub-device notifier. The sub-device is similarly
516  * unregistered by calling v4l2_async_unregister_subdev().
517  *
518  * While registered, the subdev module is marked as in-use.
519  *
520  * An error is returned if the module is no longer loaded on any attempts
521  * to register it.
522  */
523 int
524 v4l2_async_register_fwnode_subdev(struct v4l2_subdev *sd,
525 				  size_t asd_struct_size,
526 				  unsigned int *ports,
527 				  unsigned int num_ports,
528 				  parse_endpoint_func parse_endpoint);
529 
530 /* Helper macros to access the connector links. */
531 
532 /** v4l2_connector_last_link - Helper macro to get the first
533  *                             &struct v4l2_fwnode_connector link
534  * @v4l2c: &struct v4l2_fwnode_connector owning the connector links
535  *
536  * This marco returns the first added &struct v4l2_connector_link connector
537  * link or @NULL if the connector has no links.
538  */
539 #define v4l2_connector_first_link(v4l2c)				       \
540 	list_first_entry_or_null(&(v4l2c)->links,			       \
541 				 struct v4l2_connector_link, head)
542 
543 /** v4l2_connector_last_link - Helper macro to get the last
544  *                             &struct v4l2_fwnode_connector link
545  * @v4l2c: &struct v4l2_fwnode_connector owning the connector links
546  *
547  * This marco returns the last &struct v4l2_connector_link added connector link.
548  */
549 #define v4l2_connector_last_link(v4l2c)					       \
550 	list_last_entry(&(v4l2c)->links, struct v4l2_connector_link, head)
551 
552 #endif /* _V4L2_FWNODE_H */
553