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