xref: /openbmc/linux/include/media/v4l2-common.h (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
11a59d1b8SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2b2f0648fSHans Verkuil /*
3b2f0648fSHans Verkuil     v4l2 common internal API header
4b2f0648fSHans Verkuil 
5b2f0648fSHans Verkuil     This header contains internal shared ioctl definitions for use by the
6b2f0648fSHans Verkuil     internal low-level v4l2 drivers.
7b2f0648fSHans Verkuil     Each ioctl begins with VIDIOC_INT_ to clearly mark that it is an internal
8b2f0648fSHans Verkuil     define,
9b2f0648fSHans Verkuil 
10b2f0648fSHans Verkuil     Copyright (C) 2005  Hans Verkuil <hverkuil@xs4all.nl>
11b2f0648fSHans Verkuil 
12b2f0648fSHans Verkuil  */
13b2f0648fSHans Verkuil 
14b2f0648fSHans Verkuil #ifndef V4L2_COMMON_H_
15b2f0648fSHans Verkuil #define V4L2_COMMON_H_
16b2f0648fSHans Verkuil 
1777cdffcbSArnd Bergmann #include <linux/time.h>
18401998faSMauro Carvalho Chehab #include <media/v4l2-dev.h>
19401998faSMauro Carvalho Chehab 
2076a59fe7SMauro Carvalho Chehab /* Common printk constructs for v4l-i2c drivers. These macros create a unique
217e8b09eaSHans Verkuil    prefix consisting of the driver name, the adapter number and the i2c
227e8b09eaSHans Verkuil    address. */
237e8b09eaSHans Verkuil #define v4l_printk(level, name, adapter, addr, fmt, arg...) \
247e8b09eaSHans Verkuil 	printk(level "%s %d-%04x: " fmt, name, i2c_adapter_id(adapter), addr , ## arg)
257e8b09eaSHans Verkuil 
267e8b09eaSHans Verkuil #define v4l_client_printk(level, client, fmt, arg...)			    \
27f9d32f25SLars-Peter Clausen 	v4l_printk(level, (client)->dev.driver->name, (client)->adapter, \
287e8b09eaSHans Verkuil 		   (client)->addr, fmt , ## arg)
297e8b09eaSHans Verkuil 
307e8b09eaSHans Verkuil #define v4l_err(client, fmt, arg...) \
317e8b09eaSHans Verkuil 	v4l_client_printk(KERN_ERR, client, fmt , ## arg)
327e8b09eaSHans Verkuil 
337e8b09eaSHans Verkuil #define v4l_warn(client, fmt, arg...) \
347e8b09eaSHans Verkuil 	v4l_client_printk(KERN_WARNING, client, fmt , ## arg)
357e8b09eaSHans Verkuil 
367e8b09eaSHans Verkuil #define v4l_info(client, fmt, arg...) \
377e8b09eaSHans Verkuil 	v4l_client_printk(KERN_INFO, client, fmt , ## arg)
387e8b09eaSHans Verkuil 
397e8b09eaSHans Verkuil /* These three macros assume that the debug level is set with a module
407e8b09eaSHans Verkuil    parameter called 'debug'. */
41f167cb4eSMauro Carvalho Chehab #define v4l_dbg(level, debug, client, fmt, arg...)			     \
427e8b09eaSHans Verkuil 	do {								     \
437e8b09eaSHans Verkuil 		if (debug >= (level))					     \
447e8b09eaSHans Verkuil 			v4l_client_printk(KERN_DEBUG, client, fmt , ## arg); \
457e8b09eaSHans Verkuil 	} while (0)
467e8b09eaSHans Verkuil 
47a41231d5SMauro Carvalho Chehab /* Add a version of v4l_dbg to be used on drivers using dev_foo() macros */
48a41231d5SMauro Carvalho Chehab #define dev_dbg_lvl(__dev, __level, __debug, __fmt, __arg...)		\
49a41231d5SMauro Carvalho Chehab 	do {								\
50a41231d5SMauro Carvalho Chehab 		if (__debug >= (__level))				\
51a41231d5SMauro Carvalho Chehab 			dev_printk(KERN_DEBUG, __dev, __fmt, ##__arg);	\
52a41231d5SMauro Carvalho Chehab 	} while (0)
53a41231d5SMauro Carvalho Chehab 
547e8b09eaSHans Verkuil /* ------------------------------------------------------------------------- */
557e8b09eaSHans Verkuil 
56dd99120cSHans Verkuil /* These printk constructs can be used with v4l2_device and v4l2_subdev */
57dd99120cSHans Verkuil #define v4l2_printk(level, dev, fmt, arg...) \
58dd99120cSHans Verkuil 	printk(level "%s: " fmt, (dev)->name , ## arg)
59dd99120cSHans Verkuil 
60dd99120cSHans Verkuil #define v4l2_err(dev, fmt, arg...) \
61dd99120cSHans Verkuil 	v4l2_printk(KERN_ERR, dev, fmt , ## arg)
62dd99120cSHans Verkuil 
63dd99120cSHans Verkuil #define v4l2_warn(dev, fmt, arg...) \
64dd99120cSHans Verkuil 	v4l2_printk(KERN_WARNING, dev, fmt , ## arg)
65dd99120cSHans Verkuil 
66dd99120cSHans Verkuil #define v4l2_info(dev, fmt, arg...) \
67dd99120cSHans Verkuil 	v4l2_printk(KERN_INFO, dev, fmt , ## arg)
68dd99120cSHans Verkuil 
69dd99120cSHans Verkuil /* These three macros assume that the debug level is set with a module
70dd99120cSHans Verkuil    parameter called 'debug'. */
71dd99120cSHans Verkuil #define v4l2_dbg(level, debug, dev, fmt, arg...)			\
72dd99120cSHans Verkuil 	do {								\
73dd99120cSHans Verkuil 		if (debug >= (level))					\
74dd99120cSHans Verkuil 			v4l2_printk(KERN_DEBUG, dev, fmt , ## arg);	\
75dd99120cSHans Verkuil 	} while (0)
76dd99120cSHans Verkuil 
77b8719158SMauro Carvalho Chehab /**
78b8719158SMauro Carvalho Chehab  * v4l2_ctrl_query_fill- Fill in a struct v4l2_queryctrl
79b8719158SMauro Carvalho Chehab  *
80b8719158SMauro Carvalho Chehab  * @qctrl: pointer to the &struct v4l2_queryctrl to be filled
81b8719158SMauro Carvalho Chehab  * @min: minimum value for the control
82b8719158SMauro Carvalho Chehab  * @max: maximum value for the control
83b8719158SMauro Carvalho Chehab  * @step: control step
84b8719158SMauro Carvalho Chehab  * @def: default value for the control
85b8719158SMauro Carvalho Chehab  *
86b8719158SMauro Carvalho Chehab  * Fills the &struct v4l2_queryctrl fields for the query control.
87b8719158SMauro Carvalho Chehab  *
88b8719158SMauro Carvalho Chehab  * .. note::
89b8719158SMauro Carvalho Chehab  *
90b8719158SMauro Carvalho Chehab  *    This function assumes that the @qctrl->id field is filled.
91b8719158SMauro Carvalho Chehab  *
92b8719158SMauro Carvalho Chehab  * Returns -EINVAL if the control is not known by the V4L2 core, 0 on success.
93b8719158SMauro Carvalho Chehab  */
94dd99120cSHans Verkuil 
95b8719158SMauro Carvalho Chehab int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl,
96b8719158SMauro Carvalho Chehab 			 s32 min, s32 max, s32 step, s32 def);
979cb2318bSHans Verkuil 
989cb2318bSHans Verkuil /* ------------------------------------------------------------------------- */
999cb2318bSHans Verkuil 
100dd99120cSHans Verkuil struct v4l2_device;
101dd99120cSHans Verkuil struct v4l2_subdev;
102dd99120cSHans Verkuil struct v4l2_subdev_ops;
1038ffbc655SHans Verkuil 
10402283b98SEzequiel Garcia /* I2C Helper functions */
10502283b98SEzequiel Garcia #include <linux/i2c.h>
10602283b98SEzequiel Garcia 
10702283b98SEzequiel Garcia /**
10802283b98SEzequiel Garcia  * enum v4l2_i2c_tuner_type - specifies the range of tuner address that
10902283b98SEzequiel Garcia  *	should be used when seeking for I2C devices.
11002283b98SEzequiel Garcia  *
11102283b98SEzequiel Garcia  * @ADDRS_RADIO:		Radio tuner addresses.
11202283b98SEzequiel Garcia  *				Represent the following I2C addresses:
11302283b98SEzequiel Garcia  *				0x10 (if compiled with tea5761 support)
11402283b98SEzequiel Garcia  *				and 0x60.
11502283b98SEzequiel Garcia  * @ADDRS_DEMOD:		Demod tuner addresses.
11602283b98SEzequiel Garcia  *				Represent the following I2C addresses:
11702283b98SEzequiel Garcia  *				0x42, 0x43, 0x4a and 0x4b.
11802283b98SEzequiel Garcia  * @ADDRS_TV:			TV tuner addresses.
11902283b98SEzequiel Garcia  *				Represent the following I2C addresses:
12002283b98SEzequiel Garcia  *				0x42, 0x43, 0x4a, 0x4b, 0x60, 0x61, 0x62,
12102283b98SEzequiel Garcia  *				0x63 and 0x64.
12202283b98SEzequiel Garcia  * @ADDRS_TV_WITH_DEMOD:	TV tuner addresses if demod is present, this
12302283b98SEzequiel Garcia  *				excludes addresses used by the demodulator
12402283b98SEzequiel Garcia  *				from the list of candidates.
12502283b98SEzequiel Garcia  *				Represent the following I2C addresses:
12602283b98SEzequiel Garcia  *				0x60, 0x61, 0x62, 0x63 and 0x64.
12702283b98SEzequiel Garcia  *
12802283b98SEzequiel Garcia  * NOTE: All I2C addresses above use the 7-bit notation.
12902283b98SEzequiel Garcia  */
13002283b98SEzequiel Garcia enum v4l2_i2c_tuner_type {
13102283b98SEzequiel Garcia 	ADDRS_RADIO,
13202283b98SEzequiel Garcia 	ADDRS_DEMOD,
13302283b98SEzequiel Garcia 	ADDRS_TV,
13402283b98SEzequiel Garcia 	ADDRS_TV_WITH_DEMOD,
13502283b98SEzequiel Garcia };
13602283b98SEzequiel Garcia 
13702283b98SEzequiel Garcia #if defined(CONFIG_VIDEO_V4L2_I2C)
13802283b98SEzequiel Garcia 
139a39c57f8SMauro Carvalho Chehab /**
140a39c57f8SMauro Carvalho Chehab  * v4l2_i2c_new_subdev - Load an i2c module and return an initialized
141a39c57f8SMauro Carvalho Chehab  *	&struct v4l2_subdev.
142a39c57f8SMauro Carvalho Chehab  *
143a39c57f8SMauro Carvalho Chehab  * @v4l2_dev: pointer to &struct v4l2_device
144a39c57f8SMauro Carvalho Chehab  * @adapter: pointer to struct i2c_adapter
145a39c57f8SMauro Carvalho Chehab  * @client_type:  name of the chip that's on the adapter.
146a39c57f8SMauro Carvalho Chehab  * @addr: I2C address. If zero, it will use @probe_addrs
147a39c57f8SMauro Carvalho Chehab  * @probe_addrs: array with a list of address. The last entry at such
148a39c57f8SMauro Carvalho Chehab  *	array should be %I2C_CLIENT_END.
149a39c57f8SMauro Carvalho Chehab  *
150a39c57f8SMauro Carvalho Chehab  * returns a &struct v4l2_subdev pointer.
151a39c57f8SMauro Carvalho Chehab  */
1523c7c9370SHans Verkuil struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
1539a1f8b34SLaurent Pinchart 		struct i2c_adapter *adapter, const char *client_type,
154f0222c7dSHans Verkuil 		u8 addr, const unsigned short *probe_addrs);
155f0222c7dSHans Verkuil 
156a39c57f8SMauro Carvalho Chehab /**
157a39c57f8SMauro Carvalho Chehab  * v4l2_i2c_new_subdev_board - Load an i2c module and return an initialized
158a39c57f8SMauro Carvalho Chehab  *	&struct v4l2_subdev.
159a39c57f8SMauro Carvalho Chehab  *
160a39c57f8SMauro Carvalho Chehab  * @v4l2_dev: pointer to &struct v4l2_device
161a39c57f8SMauro Carvalho Chehab  * @adapter: pointer to struct i2c_adapter
162a39c57f8SMauro Carvalho Chehab  * @info: pointer to struct i2c_board_info used to replace the irq,
163a39c57f8SMauro Carvalho Chehab  *	 platform_data and addr arguments.
164a39c57f8SMauro Carvalho Chehab  * @probe_addrs: array with a list of address. The last entry at such
165a39c57f8SMauro Carvalho Chehab  *	array should be %I2C_CLIENT_END.
166a39c57f8SMauro Carvalho Chehab  *
167a39c57f8SMauro Carvalho Chehab  * returns a &struct v4l2_subdev pointer.
168a39c57f8SMauro Carvalho Chehab  */
169f0222c7dSHans Verkuil struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
1709a1f8b34SLaurent Pinchart 		struct i2c_adapter *adapter, struct i2c_board_info *info,
1719a1f8b34SLaurent Pinchart 		const unsigned short *probe_addrs);
172f0222c7dSHans Verkuil 
173a39c57f8SMauro Carvalho Chehab /**
17406582930SSakari Ailus  * v4l2_i2c_subdev_set_name - Set name for an I²C sub-device
17506582930SSakari Ailus  *
17606582930SSakari Ailus  * @sd: pointer to &struct v4l2_subdev
17706582930SSakari Ailus  * @client: pointer to struct i2c_client
178bb9ea2c3SAlexander Stein  * @devname: the name of the device; if NULL, the I²C device drivers's name
179bb9ea2c3SAlexander Stein  *           will be used
18006582930SSakari Ailus  * @postfix: sub-device specific string to put right after the I²C device name;
18106582930SSakari Ailus  *	     may be NULL
18206582930SSakari Ailus  */
18306582930SSakari Ailus void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
18406582930SSakari Ailus 			      const char *devname, const char *postfix);
18506582930SSakari Ailus 
18606582930SSakari Ailus /**
187a39c57f8SMauro Carvalho Chehab  * v4l2_i2c_subdev_init - Initializes a &struct v4l2_subdev with data from
188a39c57f8SMauro Carvalho Chehab  *	an i2c_client struct.
189a39c57f8SMauro Carvalho Chehab  *
190a39c57f8SMauro Carvalho Chehab  * @sd: pointer to &struct v4l2_subdev
191a39c57f8SMauro Carvalho Chehab  * @client: pointer to struct i2c_client
192a39c57f8SMauro Carvalho Chehab  * @ops: pointer to &struct v4l2_subdev_ops
193a39c57f8SMauro Carvalho Chehab  */
194dd99120cSHans Verkuil void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
195dd99120cSHans Verkuil 		const struct v4l2_subdev_ops *ops);
196a39c57f8SMauro Carvalho Chehab 
197a39c57f8SMauro Carvalho Chehab /**
198a39c57f8SMauro Carvalho Chehab  * v4l2_i2c_subdev_addr - returns i2c client address of &struct v4l2_subdev.
199a39c57f8SMauro Carvalho Chehab  *
200a39c57f8SMauro Carvalho Chehab  * @sd: pointer to &struct v4l2_subdev
201a39c57f8SMauro Carvalho Chehab  *
202a39c57f8SMauro Carvalho Chehab  * Returns the address of an I2C sub-device
203a39c57f8SMauro Carvalho Chehab  */
204ab373190SHans Verkuil unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd);
205dd99120cSHans Verkuil 
20676a59fe7SMauro Carvalho Chehab /**
20776a59fe7SMauro Carvalho Chehab  * v4l2_i2c_tuner_addrs - Return a list of I2C tuner addresses to probe.
20876a59fe7SMauro Carvalho Chehab  *
20976a59fe7SMauro Carvalho Chehab  * @type: type of the tuner to seek, as defined by
21076a59fe7SMauro Carvalho Chehab  *	  &enum v4l2_i2c_tuner_type.
21176a59fe7SMauro Carvalho Chehab  *
21276a59fe7SMauro Carvalho Chehab  * NOTE: Use only if the tuner addresses are unknown.
21376a59fe7SMauro Carvalho Chehab  */
214c7d29e2fSHans Verkuil const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type);
215c7d29e2fSHans Verkuil 
21651ff392cSEzequiel Garcia /**
21751ff392cSEzequiel Garcia  * v4l2_i2c_subdev_unregister - Unregister a v4l2_subdev
21851ff392cSEzequiel Garcia  *
21951ff392cSEzequiel Garcia  * @sd: pointer to &struct v4l2_subdev
22051ff392cSEzequiel Garcia  */
22151ff392cSEzequiel Garcia void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd);
22251ff392cSEzequiel Garcia 
22302283b98SEzequiel Garcia #else
22402283b98SEzequiel Garcia 
22502283b98SEzequiel Garcia static inline struct v4l2_subdev *
v4l2_i2c_new_subdev(struct v4l2_device * v4l2_dev,struct i2c_adapter * adapter,const char * client_type,u8 addr,const unsigned short * probe_addrs)22602283b98SEzequiel Garcia v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
22702283b98SEzequiel Garcia 		    struct i2c_adapter *adapter, const char *client_type,
22802283b98SEzequiel Garcia 		    u8 addr, const unsigned short *probe_addrs)
22902283b98SEzequiel Garcia {
23002283b98SEzequiel Garcia 	return NULL;
23102283b98SEzequiel Garcia }
23202283b98SEzequiel Garcia 
23302283b98SEzequiel Garcia static inline struct v4l2_subdev *
v4l2_i2c_new_subdev_board(struct v4l2_device * v4l2_dev,struct i2c_adapter * adapter,struct i2c_board_info * info,const unsigned short * probe_addrs)23402283b98SEzequiel Garcia v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
23502283b98SEzequiel Garcia 			  struct i2c_adapter *adapter, struct i2c_board_info *info,
23602283b98SEzequiel Garcia 			  const unsigned short *probe_addrs)
23702283b98SEzequiel Garcia {
23802283b98SEzequiel Garcia 	return NULL;
23902283b98SEzequiel Garcia }
24002283b98SEzequiel Garcia 
24102283b98SEzequiel Garcia static inline void
v4l2_i2c_subdev_set_name(struct v4l2_subdev * sd,struct i2c_client * client,const char * devname,const char * postfix)24202283b98SEzequiel Garcia v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
24302283b98SEzequiel Garcia 			 const char *devname, const char *postfix)
24402283b98SEzequiel Garcia {}
24502283b98SEzequiel Garcia 
24602283b98SEzequiel Garcia static inline void
v4l2_i2c_subdev_init(struct v4l2_subdev * sd,struct i2c_client * client,const struct v4l2_subdev_ops * ops)24702283b98SEzequiel Garcia v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
24802283b98SEzequiel Garcia 		     const struct v4l2_subdev_ops *ops)
24902283b98SEzequiel Garcia {}
25002283b98SEzequiel Garcia 
v4l2_i2c_subdev_addr(struct v4l2_subdev * sd)25102283b98SEzequiel Garcia static inline unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
25202283b98SEzequiel Garcia {
25302283b98SEzequiel Garcia 	return I2C_CLIENT_END;
25402283b98SEzequiel Garcia }
25502283b98SEzequiel Garcia 
25602283b98SEzequiel Garcia static inline const unsigned short *
v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)25702283b98SEzequiel Garcia v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
25802283b98SEzequiel Garcia {
25902283b98SEzequiel Garcia 	return NULL;
26002283b98SEzequiel Garcia }
26102283b98SEzequiel Garcia 
v4l2_i2c_subdev_unregister(struct v4l2_subdev * sd)26251ff392cSEzequiel Garcia static inline void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd)
26351ff392cSEzequiel Garcia {}
26451ff392cSEzequiel Garcia 
26502283b98SEzequiel Garcia #endif
26602283b98SEzequiel Garcia 
2678ffbc655SHans Verkuil /* ------------------------------------------------------------------------- */
2688ffbc655SHans Verkuil 
26985e09219SDmitri Belimov /* SPI Helper functions */
27085e09219SDmitri Belimov 
27185e09219SDmitri Belimov #include <linux/spi/spi.h>
27285e09219SDmitri Belimov 
2737c795df5SEzequiel Garcia #if defined(CONFIG_SPI)
27485e09219SDmitri Belimov 
275b8719158SMauro Carvalho Chehab /**
276b8719158SMauro Carvalho Chehab  *  v4l2_spi_new_subdev - Load an spi module and return an initialized
277b8719158SMauro Carvalho Chehab  *	&struct v4l2_subdev.
278b8719158SMauro Carvalho Chehab  *
279b8719158SMauro Carvalho Chehab  *
280b8719158SMauro Carvalho Chehab  * @v4l2_dev: pointer to &struct v4l2_device.
281b8719158SMauro Carvalho Chehab  * @master: pointer to struct spi_master.
282b8719158SMauro Carvalho Chehab  * @info: pointer to struct spi_board_info.
283b8719158SMauro Carvalho Chehab  *
284b8719158SMauro Carvalho Chehab  * returns a &struct v4l2_subdev pointer.
285b8719158SMauro Carvalho Chehab  */
28685e09219SDmitri Belimov struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
28785e09219SDmitri Belimov 		struct spi_master *master, struct spi_board_info *info);
28885e09219SDmitri Belimov 
289b8719158SMauro Carvalho Chehab /**
290b8719158SMauro Carvalho Chehab  * v4l2_spi_subdev_init - Initialize a v4l2_subdev with data from an
291b8719158SMauro Carvalho Chehab  *	spi_device struct.
292b8719158SMauro Carvalho Chehab  *
293b8719158SMauro Carvalho Chehab  * @sd: pointer to &struct v4l2_subdev
294b8719158SMauro Carvalho Chehab  * @spi: pointer to struct spi_device.
295b8719158SMauro Carvalho Chehab  * @ops: pointer to &struct v4l2_subdev_ops
296b8719158SMauro Carvalho Chehab  */
29785e09219SDmitri Belimov void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
29885e09219SDmitri Belimov 		const struct v4l2_subdev_ops *ops);
2997c795df5SEzequiel Garcia 
300a9cff393SEzequiel Garcia /**
301a9cff393SEzequiel Garcia  * v4l2_spi_subdev_unregister - Unregister a v4l2_subdev
302a9cff393SEzequiel Garcia  *
303a9cff393SEzequiel Garcia  * @sd: pointer to &struct v4l2_subdev
304a9cff393SEzequiel Garcia  */
305a9cff393SEzequiel Garcia void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd);
306a9cff393SEzequiel Garcia 
3077c795df5SEzequiel Garcia #else
3087c795df5SEzequiel Garcia 
3097c795df5SEzequiel Garcia static inline struct v4l2_subdev *
v4l2_spi_new_subdev(struct v4l2_device * v4l2_dev,struct spi_master * master,struct spi_board_info * info)3107c795df5SEzequiel Garcia v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
3117c795df5SEzequiel Garcia 		    struct spi_master *master, struct spi_board_info *info)
3127c795df5SEzequiel Garcia {
3137c795df5SEzequiel Garcia 	return NULL;
3147c795df5SEzequiel Garcia }
3157c795df5SEzequiel Garcia 
3167c795df5SEzequiel Garcia static inline void
v4l2_spi_subdev_init(struct v4l2_subdev * sd,struct spi_device * spi,const struct v4l2_subdev_ops * ops)3177c795df5SEzequiel Garcia v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
3187c795df5SEzequiel Garcia 		     const struct v4l2_subdev_ops *ops)
3197c795df5SEzequiel Garcia {}
3207c795df5SEzequiel Garcia 
v4l2_spi_subdev_unregister(struct v4l2_subdev * sd)321a9cff393SEzequiel Garcia static inline void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd)
322a9cff393SEzequiel Garcia {}
32385e09219SDmitri Belimov #endif
32485e09219SDmitri Belimov 
32585e09219SDmitri Belimov /* ------------------------------------------------------------------------- */
32685e09219SDmitri Belimov 
32701154ef5SMauro Carvalho Chehab /*
32801154ef5SMauro Carvalho Chehab  * FIXME: these remaining ioctls/structs should be removed as well, but they
32901154ef5SMauro Carvalho Chehab  * are still used in tuner-simple.c (TUNER_SET_CONFIG) and cx18/ivtv (RESET).
33001154ef5SMauro Carvalho Chehab  * To remove these ioctls some more cleanup is needed in those modules.
33176a59fe7SMauro Carvalho Chehab  *
33276a59fe7SMauro Carvalho Chehab  * It doesn't make much sense on documenting them, as what we really want is
33376a59fe7SMauro Carvalho Chehab  * to get rid of them.
33401154ef5SMauro Carvalho Chehab  */
3357e8b09eaSHans Verkuil 
33678a3b4dbSHans Verkuil /* s_config */
3377f171123SMauro Carvalho Chehab struct v4l2_priv_tun_config {
3387f171123SMauro Carvalho Chehab 	int tuner;
3397f171123SMauro Carvalho Chehab 	void *priv;
3407f171123SMauro Carvalho Chehab };
3417f171123SMauro Carvalho Chehab #define TUNER_SET_CONFIG           _IOW('d', 92, struct v4l2_priv_tun_config)
3425e453dc7SMichael Krufky 
3436c31e598SHans Verkuil #define VIDIOC_INT_RESET		_IOW ('d', 102, u32)
3446c31e598SHans Verkuil 
345b0d3159bSTrent Piepho /* ------------------------------------------------------------------------- */
346b0d3159bSTrent Piepho 
347b0d3159bSTrent Piepho /* Miscellaneous helper functions */
348b0d3159bSTrent Piepho 
34976a59fe7SMauro Carvalho Chehab /**
35076a59fe7SMauro Carvalho Chehab  * v4l_bound_align_image - adjust video dimensions according to
35176a59fe7SMauro Carvalho Chehab  *	a given constraints.
35276a59fe7SMauro Carvalho Chehab  *
35376a59fe7SMauro Carvalho Chehab  * @width:	pointer to width that will be adjusted if needed.
35476a59fe7SMauro Carvalho Chehab  * @wmin:	minimum width.
35576a59fe7SMauro Carvalho Chehab  * @wmax:	maximum width.
35676a59fe7SMauro Carvalho Chehab  * @walign:	least significant bit on width.
35776a59fe7SMauro Carvalho Chehab  * @height:	pointer to height that will be adjusted if needed.
35876a59fe7SMauro Carvalho Chehab  * @hmin:	minimum height.
35976a59fe7SMauro Carvalho Chehab  * @hmax:	maximum height.
360ae5a8ca8SNiklas Söderlund  * @halign:	least significant bit on height.
36176a59fe7SMauro Carvalho Chehab  * @salign:	least significant bit for the image size (e. g.
36276a59fe7SMauro Carvalho Chehab  *		:math:`width * height`).
36376a59fe7SMauro Carvalho Chehab  *
36476a59fe7SMauro Carvalho Chehab  * Clip an image to have @width between @wmin and @wmax, and @height between
36576a59fe7SMauro Carvalho Chehab  * @hmin and @hmax, inclusive.
36676a59fe7SMauro Carvalho Chehab  *
36776a59fe7SMauro Carvalho Chehab  * Additionally, the @width will be a multiple of :math:`2^{walign}`,
36876a59fe7SMauro Carvalho Chehab  * the @height will be a multiple of :math:`2^{halign}`, and the overall
36976a59fe7SMauro Carvalho Chehab  * size :math:`width * height` will be a multiple of :math:`2^{salign}`.
37076a59fe7SMauro Carvalho Chehab  *
37176a59fe7SMauro Carvalho Chehab  * .. note::
37276a59fe7SMauro Carvalho Chehab  *
37376a59fe7SMauro Carvalho Chehab  *    #. The clipping rectangle may be shrunk or enlarged to fit the alignment
37476a59fe7SMauro Carvalho Chehab  *       constraints.
37576a59fe7SMauro Carvalho Chehab  *    #. @wmax must not be smaller than @wmin.
37676a59fe7SMauro Carvalho Chehab  *    #. @hmax must not be smaller than @hmin.
37776a59fe7SMauro Carvalho Chehab  *    #. The alignments must not be so high there are no possible image
37876a59fe7SMauro Carvalho Chehab  *       sizes within the allowed bounds.
37976a59fe7SMauro Carvalho Chehab  *    #. @wmin and @hmin must be at least 1 (don't use 0).
38076a59fe7SMauro Carvalho Chehab  *    #. For @walign, @halign and @salign, if you don't care about a certain
38176a59fe7SMauro Carvalho Chehab  *       alignment, specify ``0``, as :math:`2^0 = 1` and one byte alignment
38276a59fe7SMauro Carvalho Chehab  *       is equivalent to no alignment.
38376a59fe7SMauro Carvalho Chehab  *    #. If you only want to adjust downward, specify a maximum that's the
38476a59fe7SMauro Carvalho Chehab  *       same as the initial value.
38576a59fe7SMauro Carvalho Chehab  */
38676a59fe7SMauro Carvalho Chehab void v4l_bound_align_image(unsigned int *width, unsigned int wmin,
387b0d3159bSTrent Piepho 			   unsigned int wmax, unsigned int walign,
38876a59fe7SMauro Carvalho Chehab 			   unsigned int *height, unsigned int hmin,
389b0d3159bSTrent Piepho 			   unsigned int hmax, unsigned int halign,
390b0d3159bSTrent Piepho 			   unsigned int salign);
3913fd8e647SHans Verkuil 
39276a59fe7SMauro Carvalho Chehab /**
39395ce9c28SSakari Ailus  * v4l2_find_nearest_size - Find the nearest size among a discrete
39495ce9c28SSakari Ailus  *	set of resolutions contained in an array of a driver specific struct.
39595ce9c28SSakari Ailus  *
39695ce9c28SSakari Ailus  * @array: a driver specific array of image sizes
397d2dc57b1SSakari Ailus  * @array_size: the length of the driver specific array of image sizes
39895ce9c28SSakari Ailus  * @width_field: the name of the width field in the driver specific struct
39995ce9c28SSakari Ailus  * @height_field: the name of the height field in the driver specific struct
40095ce9c28SSakari Ailus  * @width: desired width.
40195ce9c28SSakari Ailus  * @height: desired height.
40295ce9c28SSakari Ailus  *
40395ce9c28SSakari Ailus  * Finds the closest resolution to minimize the width and height differences
40495ce9c28SSakari Ailus  * between what requested and the supported resolutions. The size of the width
40595ce9c28SSakari Ailus  * and height fields in the driver specific must equal to that of u32, i.e. four
40695ce9c28SSakari Ailus  * bytes.
40795ce9c28SSakari Ailus  *
40895ce9c28SSakari Ailus  * Returns the best match or NULL if the length of the array is zero.
40995ce9c28SSakari Ailus  */
410d2dc57b1SSakari Ailus #define v4l2_find_nearest_size(array, array_size, width_field, height_field, \
41195ce9c28SSakari Ailus 			       width, height)				\
41295ce9c28SSakari Ailus 	({								\
41395ce9c28SSakari Ailus 		BUILD_BUG_ON(sizeof((array)->width_field) != sizeof(u32) || \
41495ce9c28SSakari Ailus 			     sizeof((array)->height_field) != sizeof(u32)); \
4152bbc46e8SSakari Ailus 		(typeof(&(array)[0]))__v4l2_find_nearest_size(		\
416d2dc57b1SSakari Ailus 			(array), array_size, sizeof(*(array)),		\
41795ce9c28SSakari Ailus 			offsetof(typeof(*(array)), width_field),	\
41895ce9c28SSakari Ailus 			offsetof(typeof(*(array)), height_field),	\
41995ce9c28SSakari Ailus 			width, height);					\
42095ce9c28SSakari Ailus 	})
42195ce9c28SSakari Ailus const void *
42295ce9c28SSakari Ailus __v4l2_find_nearest_size(const void *array, size_t array_size,
42395ce9c28SSakari Ailus 			 size_t entry_size, size_t width_offset,
42495ce9c28SSakari Ailus 			 size_t height_offset, s32 width, s32 height);
42595ce9c28SSakari Ailus 
42695ce9c28SSakari Ailus /**
427672de9a7SHans Verkuil  * v4l2_g_parm_cap - helper routine for vidioc_g_parm to fill this in by
428672de9a7SHans Verkuil  *      calling the g_frame_interval op of the given subdev. It only works
429672de9a7SHans Verkuil  *      for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
430672de9a7SHans Verkuil  *      function name.
431672de9a7SHans Verkuil  *
432672de9a7SHans Verkuil  * @vdev: the struct video_device pointer. Used to determine the device caps.
433672de9a7SHans Verkuil  * @sd: the sub-device pointer.
434672de9a7SHans Verkuil  * @a: the VIDIOC_G_PARM argument.
435672de9a7SHans Verkuil  */
436672de9a7SHans Verkuil int v4l2_g_parm_cap(struct video_device *vdev,
437672de9a7SHans Verkuil 		    struct v4l2_subdev *sd, struct v4l2_streamparm *a);
438672de9a7SHans Verkuil 
439672de9a7SHans Verkuil /**
440672de9a7SHans Verkuil  * v4l2_s_parm_cap - helper routine for vidioc_s_parm to fill this in by
441672de9a7SHans Verkuil  *      calling the s_frame_interval op of the given subdev. It only works
442672de9a7SHans Verkuil  *      for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
443672de9a7SHans Verkuil  *      function name.
444672de9a7SHans Verkuil  *
445672de9a7SHans Verkuil  * @vdev: the struct video_device pointer. Used to determine the device caps.
446672de9a7SHans Verkuil  * @sd: the sub-device pointer.
447672de9a7SHans Verkuil  * @a: the VIDIOC_S_PARM argument.
448672de9a7SHans Verkuil  */
449672de9a7SHans Verkuil int v4l2_s_parm_cap(struct video_device *vdev,
450672de9a7SHans Verkuil 		    struct v4l2_subdev *sd, struct v4l2_streamparm *a);
451672de9a7SHans Verkuil 
45285de5e06SAkinobu Mita /* Compare two v4l2_fract structs */
45385de5e06SAkinobu Mita #define V4L2_FRACT_COMPARE(a, OP, b)			\
45485de5e06SAkinobu Mita 	((u64)(a).numerator * (b).denominator OP	\
45585de5e06SAkinobu Mita 	(u64)(b).numerator * (a).denominator)
45685de5e06SAkinobu Mita 
457f44b969aSEzequiel Garcia /* ------------------------------------------------------------------------- */
458f44b969aSEzequiel Garcia 
459f44b969aSEzequiel Garcia /* Pixel format and FourCC helpers */
460f44b969aSEzequiel Garcia 
461f44b969aSEzequiel Garcia /**
462d5a897c8SBenoit Parrot  * enum v4l2_pixel_encoding - specifies the pixel encoding value
463d5a897c8SBenoit Parrot  *
464d5a897c8SBenoit Parrot  * @V4L2_PIXEL_ENC_UNKNOWN:	Pixel encoding is unknown/un-initialized
465d5a897c8SBenoit Parrot  * @V4L2_PIXEL_ENC_YUV:		Pixel encoding is YUV
466d5a897c8SBenoit Parrot  * @V4L2_PIXEL_ENC_RGB:		Pixel encoding is RGB
467d5a897c8SBenoit Parrot  * @V4L2_PIXEL_ENC_BAYER:	Pixel encoding is Bayer
468d5a897c8SBenoit Parrot  */
469d5a897c8SBenoit Parrot enum v4l2_pixel_encoding {
470d5a897c8SBenoit Parrot 	V4L2_PIXEL_ENC_UNKNOWN = 0,
471d5a897c8SBenoit Parrot 	V4L2_PIXEL_ENC_YUV = 1,
472d5a897c8SBenoit Parrot 	V4L2_PIXEL_ENC_RGB = 2,
473d5a897c8SBenoit Parrot 	V4L2_PIXEL_ENC_BAYER = 3,
474d5a897c8SBenoit Parrot };
475d5a897c8SBenoit Parrot 
476d5a897c8SBenoit Parrot /**
477f44b969aSEzequiel Garcia  * struct v4l2_format_info - information about a V4L2 format
478f44b969aSEzequiel Garcia  * @format: 4CC format identifier (V4L2_PIX_FMT_*)
479d5a897c8SBenoit Parrot  * @pixel_enc: Pixel encoding (see enum v4l2_pixel_encoding above)
480f44b969aSEzequiel Garcia  * @mem_planes: Number of memory planes, which includes the alpha plane (1 to 4).
481f44b969aSEzequiel Garcia  * @comp_planes: Number of component planes, which includes the alpha plane (1 to 4).
482f44b969aSEzequiel Garcia  * @bpp: Array of per-plane bytes per pixel
483*86c256beSNicolas Dufresne  * @bpp_div: Array of per-plane bytes per pixel divisors to support fractional pixel sizes.
484f44b969aSEzequiel Garcia  * @hdiv: Horizontal chroma subsampling factor
485f44b969aSEzequiel Garcia  * @vdiv: Vertical chroma subsampling factor
486f44b969aSEzequiel Garcia  * @block_w: Per-plane macroblock pixel width (optional)
487f44b969aSEzequiel Garcia  * @block_h: Per-plane macroblock pixel height (optional)
488f44b969aSEzequiel Garcia  */
489f44b969aSEzequiel Garcia struct v4l2_format_info {
490f44b969aSEzequiel Garcia 	u32 format;
491d5a897c8SBenoit Parrot 	u8 pixel_enc;
492f44b969aSEzequiel Garcia 	u8 mem_planes;
493f44b969aSEzequiel Garcia 	u8 comp_planes;
494f44b969aSEzequiel Garcia 	u8 bpp[4];
495*86c256beSNicolas Dufresne 	u8 bpp_div[4];
496f44b969aSEzequiel Garcia 	u8 hdiv;
497f44b969aSEzequiel Garcia 	u8 vdiv;
498f44b969aSEzequiel Garcia 	u8 block_w[4];
499f44b969aSEzequiel Garcia 	u8 block_h[4];
500f44b969aSEzequiel Garcia };
501f44b969aSEzequiel Garcia 
v4l2_is_format_rgb(const struct v4l2_format_info * f)502d5a897c8SBenoit Parrot static inline bool v4l2_is_format_rgb(const struct v4l2_format_info *f)
503d5a897c8SBenoit Parrot {
504d5a897c8SBenoit Parrot 	return f && f->pixel_enc == V4L2_PIXEL_ENC_RGB;
505d5a897c8SBenoit Parrot }
506f44b969aSEzequiel Garcia 
v4l2_is_format_yuv(const struct v4l2_format_info * f)507d5a897c8SBenoit Parrot static inline bool v4l2_is_format_yuv(const struct v4l2_format_info *f)
508d5a897c8SBenoit Parrot {
509d5a897c8SBenoit Parrot 	return f && f->pixel_enc == V4L2_PIXEL_ENC_YUV;
510d5a897c8SBenoit Parrot }
511d5a897c8SBenoit Parrot 
v4l2_is_format_bayer(const struct v4l2_format_info * f)512d5a897c8SBenoit Parrot static inline bool v4l2_is_format_bayer(const struct v4l2_format_info *f)
513d5a897c8SBenoit Parrot {
514d5a897c8SBenoit Parrot 	return f && f->pixel_enc == V4L2_PIXEL_ENC_BAYER;
515d5a897c8SBenoit Parrot }
516d5a897c8SBenoit Parrot 
517d5a897c8SBenoit Parrot const struct v4l2_format_info *v4l2_format_info(u32 format);
51832cddf9cSBoris Brezillon void v4l2_apply_frmsize_constraints(u32 *width, u32 *height,
51932cddf9cSBoris Brezillon 				    const struct v4l2_frmsize_stepwise *frmsize);
520ce57a82fSBoris Brezillon int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat,
521ce57a82fSBoris Brezillon 		     u32 width, u32 height);
522ce57a82fSBoris Brezillon int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
523ce57a82fSBoris Brezillon 			u32 width, u32 height);
524f44b969aSEzequiel Garcia 
5251b888b3cSSakari Ailus /**
5262984a99fSSakari Ailus  * v4l2_get_link_freq - Get link rate from transmitter
5271b888b3cSSakari Ailus  *
5281b888b3cSSakari Ailus  * @handler: The transmitter's control handler
5291b888b3cSSakari Ailus  * @mul: The multiplier between pixel rate and link frequency. Bits per pixel on
5301b888b3cSSakari Ailus  *	 D-PHY, samples per clock on parallel. 0 otherwise.
5311b888b3cSSakari Ailus  * @div: The divisor between pixel rate and link frequency. Number of data lanes
5321b888b3cSSakari Ailus  *	 times two on D-PHY, 1 on parallel. 0 otherwise.
5331b888b3cSSakari Ailus  *
5341b888b3cSSakari Ailus  * This function is intended for obtaining the link frequency from the
5351b888b3cSSakari Ailus  * transmitter sub-devices. It returns the link rate, either from the
5361b888b3cSSakari Ailus  * V4L2_CID_LINK_FREQ control implemented by the transmitter, or value
5371b888b3cSSakari Ailus  * calculated based on the V4L2_CID_PIXEL_RATE implemented by the transmitter.
5381b888b3cSSakari Ailus  *
5391b888b3cSSakari Ailus  * Returns link frequency on success, otherwise a negative error code:
5401b888b3cSSakari Ailus  *	-ENOENT: Link frequency or pixel rate control not found
5411b888b3cSSakari Ailus  *	-EINVAL: Invalid link frequency value
5421b888b3cSSakari Ailus  */
5432984a99fSSakari Ailus s64 v4l2_get_link_freq(struct v4l2_ctrl_handler *handler, unsigned int mul,
5441b888b3cSSakari Ailus 		       unsigned int div);
5451b888b3cSSakari Ailus 
5466ba8b8d4SMichael Grzeschik void v4l2_simplify_fraction(u32 *numerator, u32 *denominator,
5476ba8b8d4SMichael Grzeschik 		unsigned int n_terms, unsigned int threshold);
5486ba8b8d4SMichael Grzeschik u32 v4l2_fraction_to_interval(u32 numerator, u32 denominator);
5496ba8b8d4SMichael Grzeschik 
v4l2_buffer_get_timestamp(const struct v4l2_buffer * buf)55077cdffcbSArnd Bergmann static inline u64 v4l2_buffer_get_timestamp(const struct v4l2_buffer *buf)
55177cdffcbSArnd Bergmann {
55277cdffcbSArnd Bergmann 	/*
55377cdffcbSArnd Bergmann 	 * When the timestamp comes from 32-bit user space, there may be
55477cdffcbSArnd Bergmann 	 * uninitialized data in tv_usec, so cast it to u32.
55577cdffcbSArnd Bergmann 	 * Otherwise allow invalid input for backwards compatibility.
55677cdffcbSArnd Bergmann 	 */
55777cdffcbSArnd Bergmann 	return buf->timestamp.tv_sec * NSEC_PER_SEC +
55877cdffcbSArnd Bergmann 		(u32)buf->timestamp.tv_usec * NSEC_PER_USEC;
55977cdffcbSArnd Bergmann }
56077cdffcbSArnd Bergmann 
v4l2_buffer_set_timestamp(struct v4l2_buffer * buf,u64 timestamp)56177cdffcbSArnd Bergmann static inline void v4l2_buffer_set_timestamp(struct v4l2_buffer *buf,
56277cdffcbSArnd Bergmann 					     u64 timestamp)
56377cdffcbSArnd Bergmann {
56477cdffcbSArnd Bergmann 	struct timespec64 ts = ns_to_timespec64(timestamp);
56577cdffcbSArnd Bergmann 
56677cdffcbSArnd Bergmann 	buf->timestamp.tv_sec  = ts.tv_sec;
56777cdffcbSArnd Bergmann 	buf->timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
56877cdffcbSArnd Bergmann }
56977cdffcbSArnd Bergmann 
v4l2_is_colorspace_valid(__u32 colorspace)5702f491463SDafna Hirschfeld static inline bool v4l2_is_colorspace_valid(__u32 colorspace)
5712f491463SDafna Hirschfeld {
5722f491463SDafna Hirschfeld 	return colorspace > V4L2_COLORSPACE_DEFAULT &&
573718d2153SLaurent Pinchart 	       colorspace < V4L2_COLORSPACE_LAST;
5742f491463SDafna Hirschfeld }
5752f491463SDafna Hirschfeld 
v4l2_is_xfer_func_valid(__u32 xfer_func)5762f491463SDafna Hirschfeld static inline bool v4l2_is_xfer_func_valid(__u32 xfer_func)
5772f491463SDafna Hirschfeld {
5782f491463SDafna Hirschfeld 	return xfer_func > V4L2_XFER_FUNC_DEFAULT &&
579718d2153SLaurent Pinchart 	       xfer_func < V4L2_XFER_FUNC_LAST;
5802f491463SDafna Hirschfeld }
5812f491463SDafna Hirschfeld 
v4l2_is_ycbcr_enc_valid(__u8 ycbcr_enc)5822f491463SDafna Hirschfeld static inline bool v4l2_is_ycbcr_enc_valid(__u8 ycbcr_enc)
5832f491463SDafna Hirschfeld {
5842f491463SDafna Hirschfeld 	return ycbcr_enc > V4L2_YCBCR_ENC_DEFAULT &&
585718d2153SLaurent Pinchart 	       ycbcr_enc < V4L2_YCBCR_ENC_LAST;
5862f491463SDafna Hirschfeld }
5872f491463SDafna Hirschfeld 
v4l2_is_hsv_enc_valid(__u8 hsv_enc)5882f491463SDafna Hirschfeld static inline bool v4l2_is_hsv_enc_valid(__u8 hsv_enc)
5892f491463SDafna Hirschfeld {
5902f491463SDafna Hirschfeld 	return hsv_enc == V4L2_HSV_ENC_180 || hsv_enc == V4L2_HSV_ENC_256;
5912f491463SDafna Hirschfeld }
5922f491463SDafna Hirschfeld 
v4l2_is_quant_valid(__u8 quantization)5932f491463SDafna Hirschfeld static inline bool v4l2_is_quant_valid(__u8 quantization)
5942f491463SDafna Hirschfeld {
5952f491463SDafna Hirschfeld 	return quantization == V4L2_QUANTIZATION_FULL_RANGE ||
5962f491463SDafna Hirschfeld 	       quantization == V4L2_QUANTIZATION_LIM_RANGE;
5972f491463SDafna Hirschfeld }
5982f491463SDafna Hirschfeld 
599b2f0648fSHans Verkuil #endif /* V4L2_COMMON_H_ */
600