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