xref: /linux/include/media/v4l2-common.h (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3     v4l2 common internal API header
4 
5     This header contains internal shared ioctl definitions for use by the
6     internal low-level v4l2 drivers.
7     Each ioctl begins with VIDIOC_INT_ to clearly mark that it is an internal
8     define,
9 
10     Copyright (C) 2005  Hans Verkuil <hverkuil@kernel.org>
11 
12  */
13 
14 #ifndef V4L2_COMMON_H_
15 #define V4L2_COMMON_H_
16 
17 #include <linux/time.h>
18 #include <media/v4l2-dev.h>
19 
20 /* Common printk constructs for v4l-i2c drivers. These macros create a unique
21    prefix consisting of the driver name, the adapter number and the i2c
22    address. */
23 #define v4l_printk(level, name, adapter, addr, fmt, arg...) \
24 	printk(level "%s %d-%04x: " fmt, name, i2c_adapter_id(adapter), addr , ## arg)
25 
26 #define v4l_client_printk(level, client, fmt, arg...)			    \
27 	v4l_printk(level, (client)->dev.driver->name, (client)->adapter, \
28 		   (client)->addr, fmt , ## arg)
29 
30 #define v4l_err(client, fmt, arg...) \
31 	v4l_client_printk(KERN_ERR, client, fmt , ## arg)
32 
33 #define v4l_warn(client, fmt, arg...) \
34 	v4l_client_printk(KERN_WARNING, client, fmt , ## arg)
35 
36 #define v4l_info(client, fmt, arg...) \
37 	v4l_client_printk(KERN_INFO, client, fmt , ## arg)
38 
39 /* These three macros assume that the debug level is set with a module
40    parameter called 'debug'. */
41 #define v4l_dbg(level, debug, client, fmt, arg...)			     \
42 	do {								     \
43 		if (debug >= (level))					     \
44 			v4l_client_printk(KERN_DEBUG, client, fmt , ## arg); \
45 	} while (0)
46 
47 /* Add a version of v4l_dbg to be used on drivers using dev_foo() macros */
48 #define dev_dbg_lvl(__dev, __level, __debug, __fmt, __arg...)		\
49 	do {								\
50 		if (__debug >= (__level))				\
51 			dev_printk(KERN_DEBUG, __dev, __fmt, ##__arg);	\
52 	} while (0)
53 
54 /* ------------------------------------------------------------------------- */
55 
56 /* These printk constructs can be used with v4l2_device and v4l2_subdev */
57 #define v4l2_printk(level, dev, fmt, arg...) \
58 	printk(level "%s: " fmt, (dev)->name , ## arg)
59 
60 #define v4l2_err(dev, fmt, arg...) \
61 	v4l2_printk(KERN_ERR, dev, fmt , ## arg)
62 
63 #define v4l2_warn(dev, fmt, arg...) \
64 	v4l2_printk(KERN_WARNING, dev, fmt , ## arg)
65 
66 #define v4l2_info(dev, fmt, arg...) \
67 	v4l2_printk(KERN_INFO, dev, fmt , ## arg)
68 
69 /* These three macros assume that the debug level is set with a module
70    parameter called 'debug'. */
71 #define v4l2_dbg(level, debug, dev, fmt, arg...)			\
72 	do {								\
73 		if (debug >= (level))					\
74 			v4l2_printk(KERN_DEBUG, dev, fmt , ## arg);	\
75 	} while (0)
76 
77 /**
78  * v4l2_ctrl_query_fill- Fill in a struct v4l2_queryctrl
79  *
80  * @qctrl: pointer to the &struct v4l2_queryctrl to be filled
81  * @min: minimum value for the control
82  * @max: maximum value for the control
83  * @step: control step
84  * @def: default value for the control
85  *
86  * Fills the &struct v4l2_queryctrl fields for the query control.
87  *
88  * .. note::
89  *
90  *    This function assumes that the @qctrl->id field is filled.
91  *
92  * Returns -EINVAL if the control is not known by the V4L2 core, 0 on success.
93  */
94 
95 int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl,
96 			 s32 min, s32 max, s32 step, s32 def);
97 
98 /* ------------------------------------------------------------------------- */
99 
100 struct clk;
101 struct v4l2_device;
102 struct v4l2_subdev;
103 struct v4l2_subdev_ops;
104 
105 /* I2C Helper functions */
106 #include <linux/i2c.h>
107 
108 /**
109  * enum v4l2_i2c_tuner_type - specifies the range of tuner address that
110  *	should be used when seeking for I2C devices.
111  *
112  * @ADDRS_RADIO:		Radio tuner addresses.
113  *				Represent the following I2C addresses:
114  *				0x10 (if compiled with tea5761 support)
115  *				and 0x60.
116  * @ADDRS_DEMOD:		Demod tuner addresses.
117  *				Represent the following I2C addresses:
118  *				0x42, 0x43, 0x4a and 0x4b.
119  * @ADDRS_TV:			TV tuner addresses.
120  *				Represent the following I2C addresses:
121  *				0x42, 0x43, 0x4a, 0x4b, 0x60, 0x61, 0x62,
122  *				0x63 and 0x64.
123  * @ADDRS_TV_WITH_DEMOD:	TV tuner addresses if demod is present, this
124  *				excludes addresses used by the demodulator
125  *				from the list of candidates.
126  *				Represent the following I2C addresses:
127  *				0x60, 0x61, 0x62, 0x63 and 0x64.
128  *
129  * NOTE: All I2C addresses above use the 7-bit notation.
130  */
131 enum v4l2_i2c_tuner_type {
132 	ADDRS_RADIO,
133 	ADDRS_DEMOD,
134 	ADDRS_TV,
135 	ADDRS_TV_WITH_DEMOD,
136 };
137 
138 #if defined(CONFIG_VIDEO_V4L2_I2C)
139 
140 /**
141  * v4l2_i2c_new_subdev - Load an i2c module and return an initialized
142  *	&struct v4l2_subdev.
143  *
144  * @v4l2_dev: pointer to &struct v4l2_device
145  * @adapter: pointer to struct i2c_adapter
146  * @client_type:  name of the chip that's on the adapter.
147  * @addr: I2C address. If zero, it will use @probe_addrs
148  * @probe_addrs: array with a list of address. The last entry at such
149  *	array should be %I2C_CLIENT_END.
150  *
151  * returns a &struct v4l2_subdev pointer.
152  */
153 struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
154 		struct i2c_adapter *adapter, const char *client_type,
155 		u8 addr, const unsigned short *probe_addrs);
156 
157 /**
158  * v4l2_i2c_new_subdev_board - Load an i2c module and return an initialized
159  *	&struct v4l2_subdev.
160  *
161  * @v4l2_dev: pointer to &struct v4l2_device
162  * @adapter: pointer to struct i2c_adapter
163  * @info: pointer to struct i2c_board_info used to replace the irq,
164  *	 platform_data and addr arguments.
165  * @probe_addrs: array with a list of address. The last entry at such
166  *	array should be %I2C_CLIENT_END.
167  *
168  * returns a &struct v4l2_subdev pointer.
169  */
170 struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
171 		struct i2c_adapter *adapter, struct i2c_board_info *info,
172 		const unsigned short *probe_addrs);
173 
174 /**
175  * v4l2_i2c_subdev_set_name - Set name for an I²C sub-device
176  *
177  * @sd: pointer to &struct v4l2_subdev
178  * @client: pointer to struct i2c_client
179  * @devname: the name of the device; if NULL, the I²C device drivers's name
180  *           will be used
181  * @postfix: sub-device specific string to put right after the I²C device name;
182  *	     may be NULL
183  */
184 void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
185 			      const char *devname, const char *postfix);
186 
187 /**
188  * v4l2_i2c_subdev_init - Initializes a &struct v4l2_subdev with data from
189  *	an i2c_client struct.
190  *
191  * @sd: pointer to &struct v4l2_subdev
192  * @client: pointer to struct i2c_client
193  * @ops: pointer to &struct v4l2_subdev_ops
194  */
195 void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
196 		const struct v4l2_subdev_ops *ops);
197 
198 /**
199  * v4l2_i2c_subdev_addr - returns i2c client address of &struct v4l2_subdev.
200  *
201  * @sd: pointer to &struct v4l2_subdev
202  *
203  * Returns the address of an I2C sub-device
204  */
205 unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd);
206 
207 /**
208  * v4l2_i2c_tuner_addrs - Return a list of I2C tuner addresses to probe.
209  *
210  * @type: type of the tuner to seek, as defined by
211  *	  &enum v4l2_i2c_tuner_type.
212  *
213  * NOTE: Use only if the tuner addresses are unknown.
214  */
215 const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type);
216 
217 /**
218  * v4l2_i2c_subdev_unregister - Unregister a v4l2_subdev
219  *
220  * @sd: pointer to &struct v4l2_subdev
221  */
222 void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd);
223 
224 #else
225 
226 static inline struct v4l2_subdev *
227 v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
228 		    struct i2c_adapter *adapter, const char *client_type,
229 		    u8 addr, const unsigned short *probe_addrs)
230 {
231 	return NULL;
232 }
233 
234 static inline struct v4l2_subdev *
235 v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
236 			  struct i2c_adapter *adapter, struct i2c_board_info *info,
237 			  const unsigned short *probe_addrs)
238 {
239 	return NULL;
240 }
241 
242 static inline void
243 v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client,
244 			 const char *devname, const char *postfix)
245 {}
246 
247 static inline void
248 v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
249 		     const struct v4l2_subdev_ops *ops)
250 {}
251 
252 static inline unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
253 {
254 	return I2C_CLIENT_END;
255 }
256 
257 static inline const unsigned short *
258 v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
259 {
260 	return NULL;
261 }
262 
263 static inline void v4l2_i2c_subdev_unregister(struct v4l2_subdev *sd)
264 {}
265 
266 #endif
267 
268 /* ------------------------------------------------------------------------- */
269 
270 /* SPI Helper functions */
271 
272 #include <linux/spi/spi.h>
273 
274 #if defined(CONFIG_SPI)
275 
276 /**
277  *  v4l2_spi_new_subdev - Load an spi module and return an initialized
278  *	&struct v4l2_subdev.
279  *
280  *
281  * @v4l2_dev: pointer to &struct v4l2_device.
282  * @ctlr: pointer to struct spi_controller.
283  * @info: pointer to struct spi_board_info.
284  *
285  * returns a &struct v4l2_subdev pointer.
286  */
287 struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
288 		struct spi_controller *ctlr, struct spi_board_info *info);
289 
290 /**
291  * v4l2_spi_subdev_init - Initialize a v4l2_subdev with data from an
292  *	spi_device struct.
293  *
294  * @sd: pointer to &struct v4l2_subdev
295  * @spi: pointer to struct spi_device.
296  * @ops: pointer to &struct v4l2_subdev_ops
297  */
298 void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
299 		const struct v4l2_subdev_ops *ops);
300 
301 /**
302  * v4l2_spi_subdev_unregister - Unregister a v4l2_subdev
303  *
304  * @sd: pointer to &struct v4l2_subdev
305  */
306 void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd);
307 
308 #else
309 
310 static inline struct v4l2_subdev *
311 v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
312 		    struct spi_controller *ctlr, struct spi_board_info *info)
313 {
314 	return NULL;
315 }
316 
317 static inline void
318 v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
319 		     const struct v4l2_subdev_ops *ops)
320 {}
321 
322 static inline void v4l2_spi_subdev_unregister(struct v4l2_subdev *sd)
323 {}
324 #endif
325 
326 /* ------------------------------------------------------------------------- */
327 
328 /*
329  * FIXME: these remaining ioctls/structs should be removed as well, but they
330  * are still used in tuner-simple.c (TUNER_SET_CONFIG) and cx18/ivtv (RESET).
331  * To remove these ioctls some more cleanup is needed in those modules.
332  *
333  * It doesn't make much sense on documenting them, as what we really want is
334  * to get rid of them.
335  */
336 
337 /* s_config */
338 struct v4l2_priv_tun_config {
339 	int tuner;
340 	void *priv;
341 };
342 #define TUNER_SET_CONFIG           _IOW('d', 92, struct v4l2_priv_tun_config)
343 
344 #define VIDIOC_INT_RESET		_IOW ('d', 102, u32)
345 
346 /* ------------------------------------------------------------------------- */
347 
348 /* Miscellaneous helper functions */
349 
350 /**
351  * v4l_bound_align_image - adjust video dimensions according to
352  *	a given constraints.
353  *
354  * @width:	pointer to width that will be adjusted if needed.
355  * @wmin:	minimum width.
356  * @wmax:	maximum width.
357  * @walign:	least significant bit on width.
358  * @height:	pointer to height that will be adjusted if needed.
359  * @hmin:	minimum height.
360  * @hmax:	maximum height.
361  * @halign:	least significant bit on height.
362  * @salign:	least significant bit for the image size (e. g.
363  *		:math:`width * height`).
364  *
365  * Clip an image to have @width between @wmin and @wmax, and @height between
366  * @hmin and @hmax, inclusive.
367  *
368  * Additionally, the @width will be a multiple of :math:`2^{walign}`,
369  * the @height will be a multiple of :math:`2^{halign}`, and the overall
370  * size :math:`width * height` will be a multiple of :math:`2^{salign}`.
371  *
372  * .. note::
373  *
374  *    #. The clipping rectangle may be shrunk or enlarged to fit the alignment
375  *       constraints.
376  *    #. @wmax must not be smaller than @wmin.
377  *    #. @hmax must not be smaller than @hmin.
378  *    #. The alignments must not be so high there are no possible image
379  *       sizes within the allowed bounds.
380  *    #. @wmin and @hmin must be at least 1 (don't use 0).
381  *    #. For @walign, @halign and @salign, if you don't care about a certain
382  *       alignment, specify ``0``, as :math:`2^0 = 1` and one byte alignment
383  *       is equivalent to no alignment.
384  *    #. If you only want to adjust downward, specify a maximum that's the
385  *       same as the initial value.
386  */
387 void v4l_bound_align_image(unsigned int *width, unsigned int wmin,
388 			   unsigned int wmax, unsigned int walign,
389 			   unsigned int *height, unsigned int hmin,
390 			   unsigned int hmax, unsigned int halign,
391 			   unsigned int salign);
392 
393 /**
394  * v4l2_find_nearest_size_conditional - Find the nearest size among a discrete
395  *	set of resolutions contained in an array of a driver specific struct,
396  *	with conditionally exlusion of certain modes
397  *
398  * @array: a driver specific array of image sizes
399  * @array_size: the length of the driver specific array of image sizes
400  * @width_field: the name of the width field in the driver specific struct
401  * @height_field: the name of the height field in the driver specific struct
402  * @width: desired width
403  * @height: desired height
404  * @func: ignores mode if returns false
405  * @context: context for the function
406  *
407  * Finds the closest resolution to minimize the width and height differences
408  * between what requested and the supported resolutions. The size of the width
409  * and height fields in the driver specific must equal to that of u32, i.e. four
410  * bytes. @func is called for each mode considered, a mode is ignored if @func
411  * returns false for it.
412  *
413  * Returns the best match or NULL if the length of the array is zero.
414  */
415 #define v4l2_find_nearest_size_conditional(array, array_size, width_field, \
416 					   height_field, width, height, \
417 					   func, context) \
418 	({								\
419 		BUILD_BUG_ON(sizeof((array)->width_field) != sizeof(u32) || \
420 			     sizeof((array)->height_field) != sizeof(u32)); \
421 		(typeof(&(array)[0]))__v4l2_find_nearest_size_conditional( \
422 			(array), array_size, sizeof(*(array)),		\
423 			offsetof(typeof(*(array)), width_field),	\
424 			offsetof(typeof(*(array)), height_field),	\
425 			width, height, func, context);			\
426 	})
427 const void *
428 __v4l2_find_nearest_size_conditional(const void *array, size_t array_size,
429 				     size_t entry_size, size_t width_offset,
430 				     size_t height_offset, s32 width,
431 				     s32 height,
432 				     bool (*func)(const void *array,
433 						  size_t index,
434 						  const void *context),
435 				     const void *context);
436 
437 /**
438  * v4l2_find_nearest_size - Find the nearest size among a discrete set of
439  *	resolutions contained in an array of a driver specific struct
440  *
441  * @array: a driver specific array of image sizes
442  * @array_size: the length of the driver specific array of image sizes
443  * @width_field: the name of the width field in the driver specific struct
444  * @height_field: the name of the height field in the driver specific struct
445  * @width: desired width
446  * @height: desired height
447  *
448  * Finds the closest resolution to minimize the width and height differences
449  * between what requested and the supported resolutions. The size of the width
450  * and height fields in the driver specific must equal to that of u32, i.e. four
451  * bytes.
452  *
453  * Returns the best match or NULL if the length of the array is zero.
454  */
455 #define v4l2_find_nearest_size(array, array_size, width_field,		\
456 			       height_field, width, height)		\
457 	v4l2_find_nearest_size_conditional(array, array_size, width_field, \
458 					   height_field, width, height, NULL, \
459 					   NULL)
460 
461 /**
462  * v4l2_g_parm_cap - helper routine for vidioc_g_parm to fill this in by
463  *      calling the get_frame_interval op of the given subdev. It only works
464  *      for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
465  *      function name.
466  *
467  * @vdev: the struct video_device pointer. Used to determine the device caps.
468  * @sd: the sub-device pointer.
469  * @a: the VIDIOC_G_PARM argument.
470  */
471 int v4l2_g_parm_cap(struct video_device *vdev,
472 		    struct v4l2_subdev *sd, struct v4l2_streamparm *a);
473 
474 /**
475  * v4l2_s_parm_cap - helper routine for vidioc_s_parm to fill this in by
476  *      calling the set_frame_interval op of the given subdev. It only works
477  *      for V4L2_BUF_TYPE_VIDEO_CAPTURE(_MPLANE), hence the _cap in the
478  *      function name.
479  *
480  * @vdev: the struct video_device pointer. Used to determine the device caps.
481  * @sd: the sub-device pointer.
482  * @a: the VIDIOC_S_PARM argument.
483  */
484 int v4l2_s_parm_cap(struct video_device *vdev,
485 		    struct v4l2_subdev *sd, struct v4l2_streamparm *a);
486 
487 /* Compare two v4l2_fract structs */
488 #define V4L2_FRACT_COMPARE(a, OP, b)			\
489 	((u64)(a).numerator * (b).denominator OP	\
490 	(u64)(b).numerator * (a).denominator)
491 
492 /* ------------------------------------------------------------------------- */
493 
494 /* Pixel format and FourCC helpers */
495 
496 /**
497  * enum v4l2_pixel_encoding - specifies the pixel encoding value
498  *
499  * @V4L2_PIXEL_ENC_UNKNOWN:	Pixel encoding is unknown/un-initialized
500  * @V4L2_PIXEL_ENC_YUV:		Pixel encoding is YUV
501  * @V4L2_PIXEL_ENC_RGB:		Pixel encoding is RGB
502  * @V4L2_PIXEL_ENC_BAYER:	Pixel encoding is Bayer
503  */
504 enum v4l2_pixel_encoding {
505 	V4L2_PIXEL_ENC_UNKNOWN = 0,
506 	V4L2_PIXEL_ENC_YUV = 1,
507 	V4L2_PIXEL_ENC_RGB = 2,
508 	V4L2_PIXEL_ENC_BAYER = 3,
509 };
510 
511 /**
512  * struct v4l2_format_info - information about a V4L2 format
513  * @format: 4CC format identifier (V4L2_PIX_FMT_*)
514  * @pixel_enc: Pixel encoding (see enum v4l2_pixel_encoding above)
515  * @mem_planes: Number of memory planes, which includes the alpha plane (1 to 4).
516  * @comp_planes: Number of component planes, which includes the alpha plane (1 to 4).
517  * @bpp: Array of per-plane bytes per pixel
518  * @bpp_div: Array of per-plane bytes per pixel divisors to support fractional pixel sizes.
519  * @hdiv: Horizontal chroma subsampling factor
520  * @vdiv: Vertical chroma subsampling factor
521  * @block_w: Per-plane macroblock pixel width (optional)
522  * @block_h: Per-plane macroblock pixel height (optional)
523  */
524 struct v4l2_format_info {
525 	u32 format;
526 	u8 pixel_enc;
527 	u8 mem_planes;
528 	u8 comp_planes;
529 	u8 bpp[4];
530 	u8 bpp_div[4];
531 	u8 hdiv;
532 	u8 vdiv;
533 	u8 block_w[4];
534 	u8 block_h[4];
535 };
536 
537 static inline bool v4l2_is_format_rgb(const struct v4l2_format_info *f)
538 {
539 	return f && f->pixel_enc == V4L2_PIXEL_ENC_RGB;
540 }
541 
542 static inline bool v4l2_is_format_yuv(const struct v4l2_format_info *f)
543 {
544 	return f && f->pixel_enc == V4L2_PIXEL_ENC_YUV;
545 }
546 
547 static inline bool v4l2_is_format_bayer(const struct v4l2_format_info *f)
548 {
549 	return f && f->pixel_enc == V4L2_PIXEL_ENC_BAYER;
550 }
551 
552 const struct v4l2_format_info *v4l2_format_info(u32 format);
553 void v4l2_apply_frmsize_constraints(u32 *width, u32 *height,
554 				    const struct v4l2_frmsize_stepwise *frmsize);
555 int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat,
556 		     u32 width, u32 height);
557 int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
558 			u32 width, u32 height);
559 
560 /**
561  * v4l2_get_link_freq - Get link rate from transmitter
562  *
563  * @pad: The transmitter's media pad
564  * @mul: The multiplier between pixel rate and link frequency. Bits per pixel on
565  *	 D-PHY, samples per clock on parallel. 0 otherwise.
566  * @div: The divisor between pixel rate and link frequency. Number of data lanes
567  *	 times two on D-PHY, 1 on parallel. 0 otherwise.
568  *
569  * This function obtains and returns the link frequency from the transmitter
570  * sub-device's pad. The link frequency is retrieved using the get_mbus_config
571  * sub-device pad operation. If this fails, the function falls back to obtaining
572  * the frequency either directly from the V4L2_CID_LINK_FREQ control if
573  * implemented by the transmitter, or by calculating it from the pixel rate
574  * obtained from the V4L2_CID_PIXEL_RATE control.
575  *
576  * Return:
577  * * >0: Link frequency
578  * * %-ENOENT: Link frequency or pixel rate control not found
579  * * %-EINVAL: Invalid link frequency value
580  */
581 #ifdef CONFIG_MEDIA_CONTROLLER
582 s64 v4l2_get_link_freq(const struct media_pad *pad, unsigned int mul,
583 		       unsigned int div);
584 #endif
585 
586 void v4l2_simplify_fraction(u32 *numerator, u32 *denominator,
587 		unsigned int n_terms, unsigned int threshold);
588 u32 v4l2_fraction_to_interval(u32 numerator, u32 denominator);
589 
590 /**
591  * v4l2_link_freq_to_bitmap - Figure out platform-supported link frequencies
592  * @dev: The struct device
593  * @fw_link_freqs: Array of link frequencies from firmware
594  * @num_of_fw_link_freqs: Number of entries in @fw_link_freqs
595  * @driver_link_freqs: Array of link frequencies supported by the driver
596  * @num_of_driver_link_freqs: Number of entries in @driver_link_freqs
597  * @bitmap: Bitmap of driver-supported link frequencies found in @fw_link_freqs
598  *
599  * This function checks which driver-supported link frequencies are enabled in
600  * system firmware and sets the corresponding bits in @bitmap (after first
601  * zeroing it).
602  *
603  * Return:
604  * * %0: Success
605  * * %-ENOENT: No match found between driver-supported link frequencies and
606  *   those available in firmware.
607  * * %-ENODATA: No link frequencies were specified in firmware.
608  */
609 int v4l2_link_freq_to_bitmap(struct device *dev, const u64 *fw_link_freqs,
610 			     unsigned int num_of_fw_link_freqs,
611 			     const s64 *driver_link_freqs,
612 			     unsigned int num_of_driver_link_freqs,
613 			     unsigned long *bitmap);
614 
615 struct clk *__devm_v4l2_sensor_clk_get(struct device *dev, const char *id,
616 				       bool legacy, bool fixed_rate,
617 				       unsigned long clk_rate);
618 
619 /**
620  * devm_v4l2_sensor_clk_get - lookup and obtain a reference to a clock producer
621  *	for a camera sensor
622  *
623  * @dev: device for v4l2 sensor clock "consumer"
624  * @id: clock consumer ID
625  *
626  * This function behaves the same way as devm_clk_get() except where there
627  * is no clock producer like in ACPI-based platforms.
628  *
629  * For ACPI-based platforms, the function will read the "clock-frequency"
630  * ACPI _DSD property and register a fixed-clock with the frequency indicated
631  * in the property.
632  *
633  * This function also handles the special ACPI-based system case where:
634  *
635  * * The clock-frequency _DSD property is present.
636  * * A reference to the clock producer is present, where the clock is provided
637  *   by a camera sensor PMIC driver (e.g. int3472/tps68470.c)
638  *
639  * In this case try to set the clock-frequency value to the provided clock.
640  *
641  * As the name indicates, this function may only be used on camera sensor
642  * devices. This is because generally only camera sensors do need a clock to
643  * query the frequency from, due to the requirement to configure the PLL for a
644  * given CSI-2 interface frequency where the sensor's external clock frequency
645  * is a factor. Additionally, the clock frequency tends to be available on ACPI
646  * firmware based systems for camera sensors specifically (if e.g. DisCo for
647  * Imaging compliant).
648  *
649  * Returns a pointer to a struct clk on success or an error pointer on failure.
650  */
651 static inline struct clk *
652 devm_v4l2_sensor_clk_get(struct device *dev, const char *id)
653 {
654 	return __devm_v4l2_sensor_clk_get(dev, id, false, false, 0);
655 }
656 
657 /**
658  * devm_v4l2_sensor_clk_get_legacy - lookup and obtain a reference to a clock
659  *	producer for a camera sensor.
660  *
661  * @dev: device for v4l2 sensor clock "consumer"
662  * @id: clock consumer ID
663  * @fixed_rate: interpret the @clk_rate as a fixed rate or default rate
664  * @clk_rate: the clock rate
665  *
666  * This function behaves the same way as devm_v4l2_sensor_clk_get() except that
667  * it extends the behaviour on ACPI platforms to all platforms.
668  *
669  * The function also provides the ability to set the clock rate to a fixed
670  * frequency by setting @fixed_rate to true and specifying the fixed frequency
671  * in @clk_rate, or to use a default clock rate when the "clock-frequency"
672  * property is absent by setting @fixed_rate to false and specifying the default
673  * frequency in @clk_rate. Setting @fixed_rate to true and @clk_rate to 0 is an
674  * error.
675  *
676  * This function is meant to support legacy behaviour in existing drivers only.
677  * It must not be used in any new driver.
678  *
679  * Returns a pointer to a struct clk on success or an error pointer on failure.
680  */
681 static inline struct clk *
682 devm_v4l2_sensor_clk_get_legacy(struct device *dev, const char *id,
683 				bool fixed_rate, unsigned long clk_rate)
684 {
685 	return __devm_v4l2_sensor_clk_get(dev, id, true, fixed_rate, clk_rate);
686 }
687 
688 static inline u64 v4l2_buffer_get_timestamp(const struct v4l2_buffer *buf)
689 {
690 	/*
691 	 * When the timestamp comes from 32-bit user space, there may be
692 	 * uninitialized data in tv_usec, so cast it to u32.
693 	 * Otherwise allow invalid input for backwards compatibility.
694 	 */
695 	return buf->timestamp.tv_sec * NSEC_PER_SEC +
696 		(u32)buf->timestamp.tv_usec * NSEC_PER_USEC;
697 }
698 
699 static inline void v4l2_buffer_set_timestamp(struct v4l2_buffer *buf,
700 					     u64 timestamp)
701 {
702 	struct timespec64 ts = ns_to_timespec64(timestamp);
703 
704 	buf->timestamp.tv_sec  = ts.tv_sec;
705 	buf->timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
706 }
707 
708 static inline bool v4l2_is_colorspace_valid(__u32 colorspace)
709 {
710 	return colorspace > V4L2_COLORSPACE_DEFAULT &&
711 	       colorspace < V4L2_COLORSPACE_LAST;
712 }
713 
714 static inline bool v4l2_is_xfer_func_valid(__u32 xfer_func)
715 {
716 	return xfer_func > V4L2_XFER_FUNC_DEFAULT &&
717 	       xfer_func < V4L2_XFER_FUNC_LAST;
718 }
719 
720 static inline bool v4l2_is_ycbcr_enc_valid(__u8 ycbcr_enc)
721 {
722 	return ycbcr_enc > V4L2_YCBCR_ENC_DEFAULT &&
723 	       ycbcr_enc < V4L2_YCBCR_ENC_LAST;
724 }
725 
726 static inline bool v4l2_is_hsv_enc_valid(__u8 hsv_enc)
727 {
728 	return hsv_enc == V4L2_HSV_ENC_180 || hsv_enc == V4L2_HSV_ENC_256;
729 }
730 
731 static inline bool v4l2_is_quant_valid(__u8 quantization)
732 {
733 	return quantization == V4L2_QUANTIZATION_FULL_RANGE ||
734 	       quantization == V4L2_QUANTIZATION_LIM_RANGE;
735 }
736 
737 #endif /* V4L2_COMMON_H_ */
738