v4l2-common.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /*
  2. * Video for Linux Two
  3. *
  4. * A generic video device interface for the LINUX operating system
  5. * using a set of device structures/vectors for low level operations.
  6. *
  7. * This file replaces the videodev.c file that comes with the
  8. * regular kernel distribution.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. * Author: Bill Dirks <bill@thedirks.org>
  16. * based on code by Alan Cox, <alan@cymru.net>
  17. *
  18. */
  19. /*
  20. * Video capture interface for Linux
  21. *
  22. * A generic video device interface for the LINUX operating system
  23. * using a set of device structures/vectors for low level operations.
  24. *
  25. * This program is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU General Public License
  27. * as published by the Free Software Foundation; either version
  28. * 2 of the License, or (at your option) any later version.
  29. *
  30. * Author: Alan Cox, <alan@lxorguk.ukuu.org.uk>
  31. *
  32. * Fixes:
  33. */
  34. /*
  35. * Video4linux 1/2 integration by Justin Schoeman
  36. * <justin@suntiger.ee.up.ac.za>
  37. * 2.4 PROCFS support ported from 2.4 kernels by
  38. * Iñaki García Etxebarria <garetxe@euskalnet.net>
  39. * Makefile fix by "W. Michael Petullo" <mike@flyn.org>
  40. * 2.4 devfs support ported from 2.4 kernels by
  41. * Dan Merillat <dan@merillat.org>
  42. * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman)
  43. */
  44. #include <linux/module.h>
  45. #include <linux/types.h>
  46. #include <linux/kernel.h>
  47. #include <linux/mm.h>
  48. #include <linux/string.h>
  49. #include <linux/errno.h>
  50. #include <linux/i2c.h>
  51. #if defined(CONFIG_SPI)
  52. #include <linux/spi/spi.h>
  53. #endif
  54. #include <asm/uaccess.h>
  55. #include <asm/pgtable.h>
  56. #include <asm/io.h>
  57. #include <asm/div64.h>
  58. #include <media/v4l2-common.h>
  59. #include <media/v4l2-device.h>
  60. #include <media/v4l2-ctrls.h>
  61. #include <media/v4l2-chip-ident.h>
  62. #include <linux/videodev2.h>
  63. MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
  64. MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
  65. MODULE_LICENSE("GPL");
  66. /*
  67. *
  68. * V 4 L 2 D R I V E R H E L P E R A P I
  69. *
  70. */
  71. /*
  72. * Video Standard Operations (contributed by Michael Schimek)
  73. */
  74. /* Helper functions for control handling */
  75. /* Check for correctness of the ctrl's value based on the data from
  76. struct v4l2_queryctrl and the available menu items. Note that
  77. menu_items may be NULL, in that case it is ignored. */
  78. int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
  79. const char * const *menu_items)
  80. {
  81. if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
  82. return -EINVAL;
  83. if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
  84. return -EBUSY;
  85. if (qctrl->type == V4L2_CTRL_TYPE_STRING)
  86. return 0;
  87. if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
  88. qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
  89. qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
  90. return 0;
  91. if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
  92. return -ERANGE;
  93. if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
  94. if (menu_items[ctrl->value] == NULL ||
  95. menu_items[ctrl->value][0] == '\0')
  96. return -EINVAL;
  97. }
  98. if (qctrl->type == V4L2_CTRL_TYPE_BITMASK &&
  99. (ctrl->value & ~qctrl->maximum))
  100. return -ERANGE;
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(v4l2_ctrl_check);
  104. /* Fill in a struct v4l2_queryctrl */
  105. int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
  106. {
  107. const char *name;
  108. v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
  109. &min, &max, &step, &def, &qctrl->flags);
  110. if (name == NULL)
  111. return -EINVAL;
  112. qctrl->minimum = min;
  113. qctrl->maximum = max;
  114. qctrl->step = step;
  115. qctrl->default_value = def;
  116. qctrl->reserved[0] = qctrl->reserved[1] = 0;
  117. strlcpy(qctrl->name, name, sizeof(qctrl->name));
  118. return 0;
  119. }
  120. EXPORT_SYMBOL(v4l2_ctrl_query_fill);
  121. /* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
  122. the menu. The qctrl pointer may be NULL, in which case it is ignored.
  123. If menu_items is NULL, then the menu items are retrieved using
  124. v4l2_ctrl_get_menu. */
  125. int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
  126. const char * const *menu_items)
  127. {
  128. int i;
  129. qmenu->reserved = 0;
  130. if (menu_items == NULL)
  131. menu_items = v4l2_ctrl_get_menu(qmenu->id);
  132. if (menu_items == NULL ||
  133. (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
  134. return -EINVAL;
  135. for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
  136. if (menu_items[i] == NULL || menu_items[i][0] == '\0')
  137. return -EINVAL;
  138. strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name));
  139. return 0;
  140. }
  141. EXPORT_SYMBOL(v4l2_ctrl_query_menu);
  142. /* Fill in a struct v4l2_querymenu based on the specified array of valid
  143. menu items (terminated by V4L2_CTRL_MENU_IDS_END).
  144. Use this if there are 'holes' in the list of valid menu items. */
  145. int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
  146. {
  147. const char * const *menu_items = v4l2_ctrl_get_menu(qmenu->id);
  148. qmenu->reserved = 0;
  149. if (menu_items == NULL || ids == NULL)
  150. return -EINVAL;
  151. while (*ids != V4L2_CTRL_MENU_IDS_END) {
  152. if (*ids++ == qmenu->index) {
  153. strlcpy(qmenu->name, menu_items[qmenu->index],
  154. sizeof(qmenu->name));
  155. return 0;
  156. }
  157. }
  158. return -EINVAL;
  159. }
  160. EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
  161. /* ctrl_classes points to an array of u32 pointers, the last element is
  162. a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
  163. Each array must be sorted low to high and belong to the same control
  164. class. The array of u32 pointers must also be sorted, from low class IDs
  165. to high class IDs.
  166. This function returns the first ID that follows after the given ID.
  167. When no more controls are available 0 is returned. */
  168. u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
  169. {
  170. u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
  171. const u32 *pctrl;
  172. if (ctrl_classes == NULL)
  173. return 0;
  174. /* if no query is desired, then check if the ID is part of ctrl_classes */
  175. if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
  176. /* find class */
  177. while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
  178. ctrl_classes++;
  179. if (*ctrl_classes == NULL)
  180. return 0;
  181. pctrl = *ctrl_classes;
  182. /* find control ID */
  183. while (*pctrl && *pctrl != id) pctrl++;
  184. return *pctrl ? id : 0;
  185. }
  186. id &= V4L2_CTRL_ID_MASK;
  187. id++; /* select next control */
  188. /* find first class that matches (or is greater than) the class of
  189. the ID */
  190. while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
  191. ctrl_classes++;
  192. /* no more classes */
  193. if (*ctrl_classes == NULL)
  194. return 0;
  195. pctrl = *ctrl_classes;
  196. /* find first ctrl within the class that is >= ID */
  197. while (*pctrl && *pctrl < id) pctrl++;
  198. if (*pctrl)
  199. return *pctrl;
  200. /* we are at the end of the controls of the current class. */
  201. /* continue with next class if available */
  202. ctrl_classes++;
  203. if (*ctrl_classes == NULL)
  204. return 0;
  205. return **ctrl_classes;
  206. }
  207. EXPORT_SYMBOL(v4l2_ctrl_next);
  208. int v4l2_chip_match_host(const struct v4l2_dbg_match *match)
  209. {
  210. switch (match->type) {
  211. case V4L2_CHIP_MATCH_HOST:
  212. return match->addr == 0;
  213. default:
  214. return 0;
  215. }
  216. }
  217. EXPORT_SYMBOL(v4l2_chip_match_host);
  218. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  219. int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match)
  220. {
  221. int len;
  222. if (c == NULL || match == NULL)
  223. return 0;
  224. switch (match->type) {
  225. case V4L2_CHIP_MATCH_I2C_DRIVER:
  226. if (c->driver == NULL || c->driver->driver.name == NULL)
  227. return 0;
  228. len = strlen(c->driver->driver.name);
  229. /* legacy drivers have a ' suffix, don't try to match that */
  230. if (len && c->driver->driver.name[len - 1] == '\'')
  231. len--;
  232. return len && !strncmp(c->driver->driver.name, match->name, len);
  233. case V4L2_CHIP_MATCH_I2C_ADDR:
  234. return c->addr == match->addr;
  235. default:
  236. return 0;
  237. }
  238. }
  239. EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
  240. int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip,
  241. u32 ident, u32 revision)
  242. {
  243. if (!v4l2_chip_match_i2c_client(c, &chip->match))
  244. return 0;
  245. if (chip->ident == V4L2_IDENT_NONE) {
  246. chip->ident = ident;
  247. chip->revision = revision;
  248. }
  249. else {
  250. chip->ident = V4L2_IDENT_AMBIGUOUS;
  251. chip->revision = 0;
  252. }
  253. return 0;
  254. }
  255. EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
  256. /* ----------------------------------------------------------------- */
  257. /* I2C Helper functions */
  258. void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
  259. const struct v4l2_subdev_ops *ops)
  260. {
  261. v4l2_subdev_init(sd, ops);
  262. sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
  263. /* the owner is the same as the i2c_client's driver owner */
  264. sd->owner = client->driver->driver.owner;
  265. /* i2c_client and v4l2_subdev point to one another */
  266. v4l2_set_subdevdata(sd, client);
  267. i2c_set_clientdata(client, sd);
  268. /* initialize name */
  269. snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
  270. client->driver->driver.name, i2c_adapter_id(client->adapter),
  271. client->addr);
  272. }
  273. EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
  274. /* Load an i2c sub-device. */
  275. struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
  276. struct i2c_adapter *adapter, struct i2c_board_info *info,
  277. const unsigned short *probe_addrs)
  278. {
  279. struct v4l2_subdev *sd = NULL;
  280. struct i2c_client *client;
  281. BUG_ON(!v4l2_dev);
  282. request_module(I2C_MODULE_PREFIX "%s", info->type);
  283. /* Create the i2c client */
  284. if (info->addr == 0 && probe_addrs)
  285. client = i2c_new_probed_device(adapter, info, probe_addrs,
  286. NULL);
  287. else
  288. client = i2c_new_device(adapter, info);
  289. /* Note: by loading the module first we are certain that c->driver
  290. will be set if the driver was found. If the module was not loaded
  291. first, then the i2c core tries to delay-load the module for us,
  292. and then c->driver is still NULL until the module is finally
  293. loaded. This delay-load mechanism doesn't work if other drivers
  294. want to use the i2c device, so explicitly loading the module
  295. is the best alternative. */
  296. if (client == NULL || client->driver == NULL)
  297. goto error;
  298. /* Lock the module so we can safely get the v4l2_subdev pointer */
  299. if (!try_module_get(client->driver->driver.owner))
  300. goto error;
  301. sd = i2c_get_clientdata(client);
  302. /* Register with the v4l2_device which increases the module's
  303. use count as well. */
  304. if (v4l2_device_register_subdev(v4l2_dev, sd))
  305. sd = NULL;
  306. /* Decrease the module use count to match the first try_module_get. */
  307. module_put(client->driver->driver.owner);
  308. error:
  309. /* If we have a client but no subdev, then something went wrong and
  310. we must unregister the client. */
  311. if (client && sd == NULL)
  312. i2c_unregister_device(client);
  313. return sd;
  314. }
  315. EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
  316. struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
  317. struct i2c_adapter *adapter, const char *client_type,
  318. u8 addr, const unsigned short *probe_addrs)
  319. {
  320. struct i2c_board_info info;
  321. /* Setup the i2c board info with the device type and
  322. the device address. */
  323. memset(&info, 0, sizeof(info));
  324. strlcpy(info.type, client_type, sizeof(info.type));
  325. info.addr = addr;
  326. return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs);
  327. }
  328. EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
  329. /* Return i2c client address of v4l2_subdev. */
  330. unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
  331. {
  332. struct i2c_client *client = v4l2_get_subdevdata(sd);
  333. return client ? client->addr : I2C_CLIENT_END;
  334. }
  335. EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
  336. /* Return a list of I2C tuner addresses to probe. Use only if the tuner
  337. addresses are unknown. */
  338. const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
  339. {
  340. static const unsigned short radio_addrs[] = {
  341. #if defined(CONFIG_MEDIA_TUNER_TEA5761) || defined(CONFIG_MEDIA_TUNER_TEA5761_MODULE)
  342. 0x10,
  343. #endif
  344. 0x60,
  345. I2C_CLIENT_END
  346. };
  347. static const unsigned short demod_addrs[] = {
  348. 0x42, 0x43, 0x4a, 0x4b,
  349. I2C_CLIENT_END
  350. };
  351. static const unsigned short tv_addrs[] = {
  352. 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
  353. 0x60, 0x61, 0x62, 0x63, 0x64,
  354. I2C_CLIENT_END
  355. };
  356. switch (type) {
  357. case ADDRS_RADIO:
  358. return radio_addrs;
  359. case ADDRS_DEMOD:
  360. return demod_addrs;
  361. case ADDRS_TV:
  362. return tv_addrs;
  363. case ADDRS_TV_WITH_DEMOD:
  364. return tv_addrs + 4;
  365. }
  366. return NULL;
  367. }
  368. EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
  369. #endif /* defined(CONFIG_I2C) */
  370. #if defined(CONFIG_SPI)
  371. /* Load an spi sub-device. */
  372. void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
  373. const struct v4l2_subdev_ops *ops)
  374. {
  375. v4l2_subdev_init(sd, ops);
  376. sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
  377. /* the owner is the same as the spi_device's driver owner */
  378. sd->owner = spi->dev.driver->owner;
  379. /* spi_device and v4l2_subdev point to one another */
  380. v4l2_set_subdevdata(sd, spi);
  381. spi_set_drvdata(spi, sd);
  382. /* initialize name */
  383. strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
  384. }
  385. EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
  386. struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
  387. struct spi_master *master, struct spi_board_info *info)
  388. {
  389. struct v4l2_subdev *sd = NULL;
  390. struct spi_device *spi = NULL;
  391. BUG_ON(!v4l2_dev);
  392. if (info->modalias[0])
  393. request_module(info->modalias);
  394. spi = spi_new_device(master, info);
  395. if (spi == NULL || spi->dev.driver == NULL)
  396. goto error;
  397. if (!try_module_get(spi->dev.driver->owner))
  398. goto error;
  399. sd = spi_get_drvdata(spi);
  400. /* Register with the v4l2_device which increases the module's
  401. use count as well. */
  402. if (v4l2_device_register_subdev(v4l2_dev, sd))
  403. sd = NULL;
  404. /* Decrease the module use count to match the first try_module_get. */
  405. module_put(spi->dev.driver->owner);
  406. error:
  407. /* If we have a client but no subdev, then something went wrong and
  408. we must unregister the client. */
  409. if (spi && sd == NULL)
  410. spi_unregister_device(spi);
  411. return sd;
  412. }
  413. EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
  414. #endif /* defined(CONFIG_SPI) */
  415. /* Clamp x to be between min and max, aligned to a multiple of 2^align. min
  416. * and max don't have to be aligned, but there must be at least one valid
  417. * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
  418. * of 16 between 17 and 31. */
  419. static unsigned int clamp_align(unsigned int x, unsigned int min,
  420. unsigned int max, unsigned int align)
  421. {
  422. /* Bits that must be zero to be aligned */
  423. unsigned int mask = ~((1 << align) - 1);
  424. /* Round to nearest aligned value */
  425. if (align)
  426. x = (x + (1 << (align - 1))) & mask;
  427. /* Clamp to aligned value of min and max */
  428. if (x < min)
  429. x = (min + ~mask) & mask;
  430. else if (x > max)
  431. x = max & mask;
  432. return x;
  433. }
  434. /* Bound an image to have a width between wmin and wmax, and height between
  435. * hmin and hmax, inclusive. Additionally, the width will be a multiple of
  436. * 2^walign, the height will be a multiple of 2^halign, and the overall size
  437. * (width*height) will be a multiple of 2^salign. The image may be shrunk
  438. * or enlarged to fit the alignment constraints.
  439. *
  440. * The width or height maximum must not be smaller than the corresponding
  441. * minimum. The alignments must not be so high there are no possible image
  442. * sizes within the allowed bounds. wmin and hmin must be at least 1
  443. * (don't use 0). If you don't care about a certain alignment, specify 0,
  444. * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If
  445. * you only want to adjust downward, specify a maximum that's the same as
  446. * the initial value.
  447. */
  448. void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
  449. unsigned int walign,
  450. u32 *h, unsigned int hmin, unsigned int hmax,
  451. unsigned int halign, unsigned int salign)
  452. {
  453. *w = clamp_align(*w, wmin, wmax, walign);
  454. *h = clamp_align(*h, hmin, hmax, halign);
  455. /* Usually we don't need to align the size and are done now. */
  456. if (!salign)
  457. return;
  458. /* How much alignment do we have? */
  459. walign = __ffs(*w);
  460. halign = __ffs(*h);
  461. /* Enough to satisfy the image alignment? */
  462. if (walign + halign < salign) {
  463. /* Max walign where there is still a valid width */
  464. unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
  465. /* Max halign where there is still a valid height */
  466. unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
  467. /* up the smaller alignment until we have enough */
  468. do {
  469. if (halign >= hmaxa ||
  470. (walign <= halign && walign < wmaxa)) {
  471. *w = clamp_align(*w, wmin, wmax, walign + 1);
  472. walign = __ffs(*w);
  473. } else {
  474. *h = clamp_align(*h, hmin, hmax, halign + 1);
  475. halign = __ffs(*h);
  476. }
  477. } while (halign + walign < salign);
  478. }
  479. }
  480. EXPORT_SYMBOL_GPL(v4l_bound_align_image);
  481. /**
  482. * v4l_fill_dv_preset_info - fill description of a digital video preset
  483. * @preset - preset value
  484. * @info - pointer to struct v4l2_dv_enum_preset
  485. *
  486. * drivers can use this helper function to fill description of dv preset
  487. * in info.
  488. */
  489. int v4l_fill_dv_preset_info(u32 preset, struct v4l2_dv_enum_preset *info)
  490. {
  491. static const struct v4l2_dv_preset_info {
  492. u16 width;
  493. u16 height;
  494. const char *name;
  495. } dv_presets[] = {
  496. { 0, 0, "Invalid" }, /* V4L2_DV_INVALID */
  497. { 720, 480, "480p@59.94" }, /* V4L2_DV_480P59_94 */
  498. { 720, 576, "576p@50" }, /* V4L2_DV_576P50 */
  499. { 1280, 720, "720p@24" }, /* V4L2_DV_720P24 */
  500. { 1280, 720, "720p@25" }, /* V4L2_DV_720P25 */
  501. { 1280, 720, "720p@30" }, /* V4L2_DV_720P30 */
  502. { 1280, 720, "720p@50" }, /* V4L2_DV_720P50 */
  503. { 1280, 720, "720p@59.94" }, /* V4L2_DV_720P59_94 */
  504. { 1280, 720, "720p@60" }, /* V4L2_DV_720P60 */
  505. { 1920, 1080, "1080i@29.97" }, /* V4L2_DV_1080I29_97 */
  506. { 1920, 1080, "1080i@30" }, /* V4L2_DV_1080I30 */
  507. { 1920, 1080, "1080i@25" }, /* V4L2_DV_1080I25 */
  508. { 1920, 1080, "1080i@50" }, /* V4L2_DV_1080I50 */
  509. { 1920, 1080, "1080i@60" }, /* V4L2_DV_1080I60 */
  510. { 1920, 1080, "1080p@24" }, /* V4L2_DV_1080P24 */
  511. { 1920, 1080, "1080p@25" }, /* V4L2_DV_1080P25 */
  512. { 1920, 1080, "1080p@30" }, /* V4L2_DV_1080P30 */
  513. { 1920, 1080, "1080p@50" }, /* V4L2_DV_1080P50 */
  514. { 1920, 1080, "1080p@60" }, /* V4L2_DV_1080P60 */
  515. };
  516. if (info == NULL || preset >= ARRAY_SIZE(dv_presets))
  517. return -EINVAL;
  518. info->preset = preset;
  519. info->width = dv_presets[preset].width;
  520. info->height = dv_presets[preset].height;
  521. strlcpy(info->name, dv_presets[preset].name, sizeof(info->name));
  522. return 0;
  523. }
  524. EXPORT_SYMBOL_GPL(v4l_fill_dv_preset_info);
  525. /**
  526. * v4l_match_dv_timings - check if two timings match
  527. * @t1 - compare this v4l2_dv_timings struct...
  528. * @t2 - with this struct.
  529. * @pclock_delta - the allowed pixelclock deviation.
  530. *
  531. * Compare t1 with t2 with a given margin of error for the pixelclock.
  532. */
  533. bool v4l_match_dv_timings(const struct v4l2_dv_timings *t1,
  534. const struct v4l2_dv_timings *t2,
  535. unsigned pclock_delta)
  536. {
  537. if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
  538. return false;
  539. if (t1->bt.width == t2->bt.width &&
  540. t1->bt.height == t2->bt.height &&
  541. t1->bt.interlaced == t2->bt.interlaced &&
  542. t1->bt.polarities == t2->bt.polarities &&
  543. t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
  544. t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
  545. t1->bt.hfrontporch == t2->bt.hfrontporch &&
  546. t1->bt.vfrontporch == t2->bt.vfrontporch &&
  547. t1->bt.vsync == t2->bt.vsync &&
  548. t1->bt.vbackporch == t2->bt.vbackporch &&
  549. (!t1->bt.interlaced ||
  550. (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
  551. t1->bt.il_vsync == t2->bt.il_vsync &&
  552. t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
  553. return true;
  554. return false;
  555. }
  556. EXPORT_SYMBOL_GPL(v4l_match_dv_timings);
  557. /*
  558. * CVT defines
  559. * Based on Coordinated Video Timings Standard
  560. * version 1.1 September 10, 2003
  561. */
  562. #define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */
  563. /* Normal blanking */
  564. #define CVT_MIN_V_BPORCH 7 /* lines */
  565. #define CVT_MIN_V_PORCH_RND 3 /* lines */
  566. #define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
  567. /* Normal blanking for CVT uses GTF to calculate horizontal blanking */
  568. #define CVT_CELL_GRAN 8 /* character cell granularity */
  569. #define CVT_M 600 /* blanking formula gradient */
  570. #define CVT_C 40 /* blanking formula offset */
  571. #define CVT_K 128 /* blanking formula scaling factor */
  572. #define CVT_J 20 /* blanking formula scaling factor */
  573. #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
  574. #define CVT_M_PRIME (CVT_K * CVT_M / 256)
  575. /* Reduced Blanking */
  576. #define CVT_RB_MIN_V_BPORCH 7 /* lines */
  577. #define CVT_RB_V_FPORCH 3 /* lines */
  578. #define CVT_RB_MIN_V_BLANK 460 /* us */
  579. #define CVT_RB_H_SYNC 32 /* pixels */
  580. #define CVT_RB_H_BPORCH 80 /* pixels */
  581. #define CVT_RB_H_BLANK 160 /* pixels */
  582. /** v4l2_detect_cvt - detect if the given timings follow the CVT standard
  583. * @frame_height - the total height of the frame (including blanking) in lines.
  584. * @hfreq - the horizontal frequency in Hz.
  585. * @vsync - the height of the vertical sync in lines.
  586. * @polarities - the horizontal and vertical polarities (same as struct
  587. * v4l2_bt_timings polarities).
  588. * @fmt - the resulting timings.
  589. *
  590. * This function will attempt to detect if the given values correspond to a
  591. * valid CVT format. If so, then it will return true, and fmt will be filled
  592. * in with the found CVT timings.
  593. */
  594. bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,
  595. u32 polarities, struct v4l2_dv_timings *fmt)
  596. {
  597. int v_fp, v_bp, h_fp, h_bp, hsync;
  598. int frame_width, image_height, image_width;
  599. bool reduced_blanking;
  600. unsigned pix_clk;
  601. if (vsync < 4 || vsync > 7)
  602. return false;
  603. if (polarities == V4L2_DV_VSYNC_POS_POL)
  604. reduced_blanking = false;
  605. else if (polarities == V4L2_DV_HSYNC_POS_POL)
  606. reduced_blanking = true;
  607. else
  608. return false;
  609. /* Vertical */
  610. if (reduced_blanking) {
  611. v_fp = CVT_RB_V_FPORCH;
  612. v_bp = (CVT_RB_MIN_V_BLANK * hfreq + 999999) / 1000000;
  613. v_bp -= vsync + v_fp;
  614. if (v_bp < CVT_RB_MIN_V_BPORCH)
  615. v_bp = CVT_RB_MIN_V_BPORCH;
  616. } else {
  617. v_fp = CVT_MIN_V_PORCH_RND;
  618. v_bp = (CVT_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
  619. if (v_bp < CVT_MIN_V_BPORCH)
  620. v_bp = CVT_MIN_V_BPORCH;
  621. }
  622. image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
  623. /* Aspect ratio based on vsync */
  624. switch (vsync) {
  625. case 4:
  626. image_width = (image_height * 4) / 3;
  627. break;
  628. case 5:
  629. image_width = (image_height * 16) / 9;
  630. break;
  631. case 6:
  632. image_width = (image_height * 16) / 10;
  633. break;
  634. case 7:
  635. /* special case */
  636. if (image_height == 1024)
  637. image_width = (image_height * 5) / 4;
  638. else if (image_height == 768)
  639. image_width = (image_height * 15) / 9;
  640. else
  641. return false;
  642. break;
  643. default:
  644. return false;
  645. }
  646. image_width = image_width & ~7;
  647. /* Horizontal */
  648. if (reduced_blanking) {
  649. pix_clk = (image_width + CVT_RB_H_BLANK) * hfreq;
  650. pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
  651. h_bp = CVT_RB_H_BPORCH;
  652. hsync = CVT_RB_H_SYNC;
  653. h_fp = CVT_RB_H_BLANK - h_bp - hsync;
  654. frame_width = image_width + CVT_RB_H_BLANK;
  655. } else {
  656. int h_blank;
  657. unsigned ideal_duty_cycle = CVT_C_PRIME - (CVT_M_PRIME * 1000) / hfreq;
  658. h_blank = (image_width * ideal_duty_cycle + (100 - ideal_duty_cycle) / 2) /
  659. (100 - ideal_duty_cycle);
  660. h_blank = h_blank - h_blank % (2 * CVT_CELL_GRAN);
  661. if (h_blank * 100 / image_width < 20) {
  662. h_blank = image_width / 5;
  663. h_blank = (h_blank + 0x7) & ~0x7;
  664. }
  665. pix_clk = (image_width + h_blank) * hfreq;
  666. pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
  667. h_bp = h_blank / 2;
  668. frame_width = image_width + h_blank;
  669. hsync = (frame_width * 8 + 50) / 100;
  670. hsync = hsync - hsync % CVT_CELL_GRAN;
  671. h_fp = h_blank - hsync - h_bp;
  672. }
  673. fmt->bt.polarities = polarities;
  674. fmt->bt.width = image_width;
  675. fmt->bt.height = image_height;
  676. fmt->bt.hfrontporch = h_fp;
  677. fmt->bt.vfrontporch = v_fp;
  678. fmt->bt.hsync = hsync;
  679. fmt->bt.vsync = vsync;
  680. fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
  681. fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
  682. fmt->bt.pixelclock = pix_clk;
  683. fmt->bt.standards = V4L2_DV_BT_STD_CVT;
  684. if (reduced_blanking)
  685. fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
  686. return true;
  687. }
  688. EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
  689. /*
  690. * GTF defines
  691. * Based on Generalized Timing Formula Standard
  692. * Version 1.1 September 2, 1999
  693. */
  694. #define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */
  695. #define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
  696. #define GTF_V_FP 1 /* vertical front porch (lines) */
  697. #define GTF_CELL_GRAN 8 /* character cell granularity */
  698. /* Default */
  699. #define GTF_D_M 600 /* blanking formula gradient */
  700. #define GTF_D_C 40 /* blanking formula offset */
  701. #define GTF_D_K 128 /* blanking formula scaling factor */
  702. #define GTF_D_J 20 /* blanking formula scaling factor */
  703. #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
  704. #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
  705. /* Secondary */
  706. #define GTF_S_M 3600 /* blanking formula gradient */
  707. #define GTF_S_C 40 /* blanking formula offset */
  708. #define GTF_S_K 128 /* blanking formula scaling factor */
  709. #define GTF_S_J 35 /* blanking formula scaling factor */
  710. #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
  711. #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
  712. /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
  713. * @frame_height - the total height of the frame (including blanking) in lines.
  714. * @hfreq - the horizontal frequency in Hz.
  715. * @vsync - the height of the vertical sync in lines.
  716. * @polarities - the horizontal and vertical polarities (same as struct
  717. * v4l2_bt_timings polarities).
  718. * @aspect - preferred aspect ratio. GTF has no method of determining the
  719. * aspect ratio in order to derive the image width from the
  720. * image height, so it has to be passed explicitly. Usually
  721. * the native screen aspect ratio is used for this. If it
  722. * is not filled in correctly, then 16:9 will be assumed.
  723. * @fmt - the resulting timings.
  724. *
  725. * This function will attempt to detect if the given values correspond to a
  726. * valid GTF format. If so, then it will return true, and fmt will be filled
  727. * in with the found GTF timings.
  728. */
  729. bool v4l2_detect_gtf(unsigned frame_height,
  730. unsigned hfreq,
  731. unsigned vsync,
  732. u32 polarities,
  733. struct v4l2_fract aspect,
  734. struct v4l2_dv_timings *fmt)
  735. {
  736. int pix_clk;
  737. int v_fp, v_bp, h_fp, hsync;
  738. int frame_width, image_height, image_width;
  739. bool default_gtf;
  740. int h_blank;
  741. if (vsync != 3)
  742. return false;
  743. if (polarities == V4L2_DV_VSYNC_POS_POL)
  744. default_gtf = true;
  745. else if (polarities == V4L2_DV_HSYNC_POS_POL)
  746. default_gtf = false;
  747. else
  748. return false;
  749. /* Vertical */
  750. v_fp = GTF_V_FP;
  751. v_bp = (GTF_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
  752. image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
  753. if (aspect.numerator == 0 || aspect.denominator == 0) {
  754. aspect.numerator = 16;
  755. aspect.denominator = 9;
  756. }
  757. image_width = ((image_height * aspect.numerator) / aspect.denominator);
  758. /* Horizontal */
  759. if (default_gtf)
  760. h_blank = ((image_width * GTF_D_C_PRIME * hfreq) -
  761. (image_width * GTF_D_M_PRIME * 1000) +
  762. (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) / 2) /
  763. (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000);
  764. else
  765. h_blank = ((image_width * GTF_S_C_PRIME * hfreq) -
  766. (image_width * GTF_S_M_PRIME * 1000) +
  767. (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) /
  768. (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000);
  769. h_blank = h_blank - h_blank % (2 * GTF_CELL_GRAN);
  770. frame_width = image_width + h_blank;
  771. pix_clk = (image_width + h_blank) * hfreq;
  772. pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
  773. hsync = (frame_width * 8 + 50) / 100;
  774. hsync = hsync - hsync % GTF_CELL_GRAN;
  775. h_fp = h_blank / 2 - hsync;
  776. fmt->bt.polarities = polarities;
  777. fmt->bt.width = image_width;
  778. fmt->bt.height = image_height;
  779. fmt->bt.hfrontporch = h_fp;
  780. fmt->bt.vfrontporch = v_fp;
  781. fmt->bt.hsync = hsync;
  782. fmt->bt.vsync = vsync;
  783. fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
  784. fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
  785. fmt->bt.pixelclock = pix_clk;
  786. fmt->bt.standards = V4L2_DV_BT_STD_GTF;
  787. if (!default_gtf)
  788. fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
  789. return true;
  790. }
  791. EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
  792. /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
  793. * 0x15 and 0x16 from the EDID.
  794. * @hor_landscape - byte 0x15 from the EDID.
  795. * @vert_portrait - byte 0x16 from the EDID.
  796. *
  797. * Determines the aspect ratio from the EDID.
  798. * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
  799. * "Horizontal and Vertical Screen Size or Aspect Ratio"
  800. */
  801. struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
  802. {
  803. struct v4l2_fract aspect = { 16, 9 };
  804. u32 tmp;
  805. u8 ratio;
  806. /* Nothing filled in, fallback to 16:9 */
  807. if (!hor_landscape && !vert_portrait)
  808. return aspect;
  809. /* Both filled in, so they are interpreted as the screen size in cm */
  810. if (hor_landscape && vert_portrait) {
  811. aspect.numerator = hor_landscape;
  812. aspect.denominator = vert_portrait;
  813. return aspect;
  814. }
  815. /* Only one is filled in, so interpret them as a ratio:
  816. (val + 99) / 100 */
  817. ratio = hor_landscape | vert_portrait;
  818. /* Change some rounded values into the exact aspect ratio */
  819. if (ratio == 79) {
  820. aspect.numerator = 16;
  821. aspect.denominator = 9;
  822. } else if (ratio == 34) {
  823. aspect.numerator = 4;
  824. aspect.numerator = 3;
  825. } else if (ratio == 68) {
  826. aspect.numerator = 15;
  827. aspect.numerator = 9;
  828. } else {
  829. aspect.numerator = hor_landscape + 99;
  830. aspect.denominator = 100;
  831. }
  832. if (hor_landscape)
  833. return aspect;
  834. /* The aspect ratio is for portrait, so swap numerator and denominator */
  835. tmp = aspect.denominator;
  836. aspect.denominator = aspect.numerator;
  837. aspect.numerator = tmp;
  838. return aspect;
  839. }
  840. EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);
  841. const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
  842. const struct v4l2_discrete_probe *probe,
  843. s32 width, s32 height)
  844. {
  845. int i;
  846. u32 error, min_error = UINT_MAX;
  847. const struct v4l2_frmsize_discrete *size, *best = NULL;
  848. if (!probe)
  849. return best;
  850. for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) {
  851. error = abs(size->width - width) + abs(size->height - height);
  852. if (error < min_error) {
  853. min_error = error;
  854. best = size;
  855. }
  856. if (!error)
  857. break;
  858. }
  859. return best;
  860. }
  861. EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
  862. void v4l2_get_timestamp(struct timeval *tv)
  863. {
  864. struct timespec ts;
  865. ktime_get_ts(&ts);
  866. tv->tv_sec = ts.tv_sec;
  867. tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  868. }
  869. EXPORT_SYMBOL_GPL(v4l2_get_timestamp);