xref: /linux/include/media/v4l2-fwnode.h (revision 4fd18fc38757217c746aa063ba9e4729814dc737)
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. There are two ways to use this function, either by letting it
223  * obtain the type of the bus (by setting the @vep.bus_type field to
224  * V4L2_MBUS_UNKNOWN) or specifying the bus type explicitly to one of the &enum
225  * v4l2_mbus_type types.
226  *
227  * When @vep.bus_type is V4L2_MBUS_UNKNOWN, the function will use the "bus-type"
228  * property to determine the type when it is available. The caller is
229  * responsible for validating the contents of @vep.bus_type field after the call
230  * returns.
231  *
232  * As a deprecated functionality to support older DT bindings without "bus-type"
233  * property for devices that support multiple types, if the "bus-type" property
234  * does not exist, the function will attempt to guess the type based on the
235  * endpoint properties available. NEVER RELY ON GUESSING THE BUS TYPE IN NEW
236  * DRIVERS OR BINDINGS.
237  *
238  * It is also possible to set @vep.bus_type corresponding to an actual bus. In
239  * this case the function will only attempt to parse properties related to this
240  * bus, and it will return an error if the value of the "bus-type" property
241  * corresponds to a different bus.
242  *
243  * The caller is required to initialise all fields of @vep, either with
244  * explicitly values, or by zeroing them.
245  *
246  * The function does not change the V4L2 fwnode endpoint state if it fails.
247  *
248  * NOTE: This function does not parse "link-frequencies" property as its size is
249  * not known in advance. Please use v4l2_fwnode_endpoint_alloc_parse() if you
250  * need properties of variable size.
251  *
252  * Return: %0 on success or a negative error code on failure:
253  *	   %-ENOMEM on memory allocation failure
254  *	   %-EINVAL on parsing failure
255  *	   %-ENXIO on mismatching bus types
256  */
257 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
258 			       struct v4l2_fwnode_endpoint *vep);
259 
260 /**
261  * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by
262  * v4l2_fwnode_endpoint_alloc_parse()
263  * @vep: the V4L2 fwnode the resources of which are to be released
264  *
265  * It is safe to call this function with NULL argument or on a V4L2 fwnode the
266  * parsing of which failed.
267  */
268 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep);
269 
270 /**
271  * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties
272  * @fwnode: pointer to the endpoint's fwnode handle
273  * @vep: pointer to the V4L2 fwnode data structure
274  *
275  * This function parses the V4L2 fwnode endpoint specific parameters from the
276  * firmware. There are two ways to use this function, either by letting it
277  * obtain the type of the bus (by setting the @vep.bus_type field to
278  * V4L2_MBUS_UNKNOWN) or specifying the bus type explicitly to one of the &enum
279  * v4l2_mbus_type types.
280  *
281  * When @vep.bus_type is V4L2_MBUS_UNKNOWN, the function will use the "bus-type"
282  * property to determine the type when it is available. The caller is
283  * responsible for validating the contents of @vep.bus_type field after the call
284  * returns.
285  *
286  * As a deprecated functionality to support older DT bindings without "bus-type"
287  * property for devices that support multiple types, if the "bus-type" property
288  * does not exist, the function will attempt to guess the type based on the
289  * endpoint properties available. NEVER RELY ON GUESSING THE BUS TYPE IN NEW
290  * DRIVERS OR BINDINGS.
291  *
292  * It is also possible to set @vep.bus_type corresponding to an actual bus. In
293  * this case the function will only attempt to parse properties related to this
294  * bus, and it will return an error if the value of the "bus-type" property
295  * corresponds to a different bus.
296  *
297  * The caller is required to initialise all fields of @vep, either with
298  * explicitly values, or by zeroing them.
299  *
300  * The function does not change the V4L2 fwnode endpoint state if it fails.
301  *
302  * v4l2_fwnode_endpoint_alloc_parse() has two important differences to
303  * v4l2_fwnode_endpoint_parse():
304  *
305  * 1. It also parses variable size data.
306  *
307  * 2. The memory it has allocated to store the variable size data must be freed
308  *    using v4l2_fwnode_endpoint_free() when no longer needed.
309  *
310  * Return: %0 on success or a negative error code on failure:
311  *	   %-ENOMEM on memory allocation failure
312  *	   %-EINVAL on parsing failure
313  *	   %-ENXIO on mismatching bus types
314  */
315 int v4l2_fwnode_endpoint_alloc_parse(struct fwnode_handle *fwnode,
316 				     struct v4l2_fwnode_endpoint *vep);
317 
318 /**
319  * v4l2_fwnode_parse_link() - parse a link between two endpoints
320  * @fwnode: pointer to the endpoint's fwnode at the local end of the link
321  * @link: pointer to the V4L2 fwnode link data structure
322  *
323  * Fill the link structure with the local and remote nodes and port numbers.
324  * The local_node and remote_node fields are set to point to the local and
325  * remote port's parent nodes respectively (the port parent node being the
326  * parent node of the port node if that node isn't a 'ports' node, or the
327  * grand-parent node of the port node otherwise).
328  *
329  * A reference is taken to both the local and remote nodes, the caller must use
330  * v4l2_fwnode_put_link() to drop the references when done with the
331  * link.
332  *
333  * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be
334  * found.
335  */
336 int v4l2_fwnode_parse_link(struct fwnode_handle *fwnode,
337 			   struct v4l2_fwnode_link *link);
338 
339 /**
340  * v4l2_fwnode_put_link() - drop references to nodes in a link
341  * @link: pointer to the V4L2 fwnode link data structure
342  *
343  * Drop references to the local and remote nodes in the link. This function
344  * must be called on every link parsed with v4l2_fwnode_parse_link().
345  */
346 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link);
347 
348 /**
349  * v4l2_fwnode_connector_free() - free the V4L2 connector acquired memory
350  * @connector: the V4L2 connector resources of which are to be released
351  *
352  * Free all allocated memory and put all links acquired by
353  * v4l2_fwnode_connector_parse() and v4l2_fwnode_connector_add_link().
354  *
355  * It is safe to call this function with NULL argument or on a V4L2 connector
356  * the parsing of which failed.
357  */
358 void v4l2_fwnode_connector_free(struct v4l2_fwnode_connector *connector);
359 
360 /**
361  * v4l2_fwnode_connector_parse() - initialize the 'struct v4l2_fwnode_connector'
362  * @fwnode: pointer to the subdev endpoint's fwnode handle where the connector
363  *	    is connected to or to the connector endpoint fwnode handle.
364  * @connector: pointer to the V4L2 fwnode connector data structure
365  *
366  * Fill the &struct v4l2_fwnode_connector with the connector type, label and
367  * all &enum v4l2_connector_type specific connector data. The label is optional
368  * so it is set to %NULL if no one was found. The function initialize the links
369  * to zero. Adding links to the connector is done by calling
370  * v4l2_fwnode_connector_add_link().
371  *
372  * The memory allocated for the label must be freed when no longer needed.
373  * Freeing the memory is done by v4l2_fwnode_connector_free().
374  *
375  * Return:
376  * * %0 on success or a negative error code on failure:
377  * * %-EINVAL if @fwnode is invalid
378  * * %-ENOTCONN if connector type is unknown or connector device can't be found
379  */
380 int v4l2_fwnode_connector_parse(struct fwnode_handle *fwnode,
381 				struct v4l2_fwnode_connector *connector);
382 
383 /**
384  * v4l2_fwnode_connector_add_link - add a link between a connector node and
385  *				    a v4l2-subdev node.
386  * @fwnode: pointer to the subdev endpoint's fwnode handle where the connector
387  *          is connected to
388  * @connector: pointer to the V4L2 fwnode connector data structure
389  *
390  * Add a new &struct v4l2_connector_link link to the
391  * &struct v4l2_fwnode_connector connector links list. The link local_node
392  * points to the connector node, the remote_node to the host v4l2 (sub)dev.
393  *
394  * The taken references to remote_node and local_node must be dropped and the
395  * allocated memory must be freed when no longer needed. Both is done by calling
396  * v4l2_fwnode_connector_free().
397  *
398  * Return:
399  * * %0 on success or a negative error code on failure:
400  * * %-EINVAL if @fwnode or @connector is invalid or @connector type is unknown
401  * * %-ENOMEM on link memory allocation failure
402  * * %-ENOTCONN if remote connector device can't be found
403  * * %-ENOLINK if link parsing between v4l2 (sub)dev and connector fails
404  */
405 int v4l2_fwnode_connector_add_link(struct fwnode_handle *fwnode,
406 				   struct v4l2_fwnode_connector *connector);
407 
408 /**
409  * v4l2_fwnode_device_parse() - parse fwnode device properties
410  * @dev: pointer to &struct device
411  * @props: pointer to &struct v4l2_fwnode_device_properties where to store the
412  *	   parsed properties values
413  *
414  * This function parses and validates the V4L2 fwnode device properties from the
415  * firmware interface, and fills the @struct v4l2_fwnode_device_properties
416  * provided by the caller.
417  *
418  * Return:
419  *	% 0 on success
420  *	%-EINVAL if a parsed property value is not valid
421  */
422 int v4l2_fwnode_device_parse(struct device *dev,
423 			     struct v4l2_fwnode_device_properties *props);
424 
425 /**
426  * typedef parse_endpoint_func - Driver's callback function to be called on
427  *	each V4L2 fwnode endpoint.
428  *
429  * @dev: pointer to &struct device
430  * @vep: pointer to &struct v4l2_fwnode_endpoint
431  * @asd: pointer to &struct v4l2_async_subdev
432  *
433  * Return:
434  * * %0 on success
435  * * %-ENOTCONN if the endpoint is to be skipped but this
436  *   should not be considered as an error
437  * * %-EINVAL if the endpoint configuration is invalid
438  */
439 typedef int (*parse_endpoint_func)(struct device *dev,
440 				  struct v4l2_fwnode_endpoint *vep,
441 				  struct v4l2_async_subdev *asd);
442 
443 /**
444  * v4l2_async_notifier_parse_fwnode_endpoints - Parse V4L2 fwnode endpoints in a
445  *						device node
446  * @dev: the device the endpoints of which are to be parsed
447  * @notifier: notifier for @dev
448  * @asd_struct_size: size of the driver's async sub-device struct, including
449  *		     sizeof(struct v4l2_async_subdev). The &struct
450  *		     v4l2_async_subdev shall be the first member of
451  *		     the driver's async sub-device struct, i.e. both
452  *		     begin at the same memory address.
453  * @parse_endpoint: Driver's callback function called on each V4L2 fwnode
454  *		    endpoint. Optional.
455  *
456  * Parse the fwnode endpoints of the @dev device and populate the async sub-
457  * devices list in the notifier. The @parse_endpoint callback function is
458  * called for each endpoint with the corresponding async sub-device pointer to
459  * let the caller initialize the driver-specific part of the async sub-device
460  * structure.
461  *
462  * The notifier memory shall be zeroed before this function is called on the
463  * notifier.
464  *
465  * This function may not be called on a registered notifier and may be called on
466  * a notifier only once.
467  *
468  * The &struct v4l2_fwnode_endpoint passed to the callback function
469  * @parse_endpoint is released once the function is finished. If there is a need
470  * to retain that configuration, the user needs to allocate memory for it.
471  *
472  * Any notifier populated using this function must be released with a call to
473  * v4l2_async_notifier_cleanup() after it has been unregistered and the async
474  * sub-devices are no longer in use, even if the function returned an error.
475  *
476  * Return: %0 on success, including when no async sub-devices are found
477  *	   %-ENOMEM if memory allocation failed
478  *	   %-EINVAL if graph or endpoint parsing failed
479  *	   Other error codes as returned by @parse_endpoint
480  */
481 int
482 v4l2_async_notifier_parse_fwnode_endpoints(struct device *dev,
483 					   struct v4l2_async_notifier *notifier,
484 					   size_t asd_struct_size,
485 					   parse_endpoint_func parse_endpoint);
486 
487 /**
488  * v4l2_async_notifier_parse_fwnode_sensor_common - parse common references on
489  *					       sensors for async sub-devices
490  * @dev: the device node the properties of which are parsed for references
491  * @notifier: the async notifier where the async subdevs will be added
492  *
493  * Parse common sensor properties for remote devices related to the
494  * sensor and set up async sub-devices for them.
495  *
496  * Any notifier populated using this function must be released with a call to
497  * v4l2_async_notifier_release() after it has been unregistered and the async
498  * sub-devices are no longer in use, even in the case the function returned an
499  * error.
500  *
501  * Return: 0 on success
502  *	   -ENOMEM if memory allocation failed
503  *	   -EINVAL if property parsing failed
504  */
505 int v4l2_async_notifier_parse_fwnode_sensor_common(struct device *dev,
506 						   struct v4l2_async_notifier *notifier);
507 
508 /* Helper macros to access the connector links. */
509 
510 /** v4l2_connector_last_link - Helper macro to get the first
511  *                             &struct v4l2_fwnode_connector link
512  * @v4l2c: &struct v4l2_fwnode_connector owning the connector links
513  *
514  * This marco returns the first added &struct v4l2_connector_link connector
515  * link or @NULL if the connector has no links.
516  */
517 #define v4l2_connector_first_link(v4l2c)				       \
518 	list_first_entry_or_null(&(v4l2c)->links,			       \
519 				 struct v4l2_connector_link, head)
520 
521 /** v4l2_connector_last_link - Helper macro to get the last
522  *                             &struct v4l2_fwnode_connector link
523  * @v4l2c: &struct v4l2_fwnode_connector owning the connector links
524  *
525  * This marco returns the last &struct v4l2_connector_link added connector link.
526  */
527 #define v4l2_connector_last_link(v4l2c)					       \
528 	list_last_entry(&(v4l2c)->links, struct v4l2_connector_link, head)
529 
530 #endif /* _V4L2_FWNODE_H */
531