v4l2-common.h 25 KB

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