4 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
7 * This file replaces the videodev.c file that comes with the
8 * regular kernel distribution.
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.
15 * Author: Bill Dirks <bill@thedirks.org>
16 * based on code by Alan Cox, <alan@cymru.net>
21 * Video capture interface for Linux
23 * A generic video device interface for the LINUX operating system
24 * using a set of device structures/vectors for low level operations.
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * as published by the Free Software Foundation; either version
29 * 2 of the License, or (at your option) any later version.
31 * Author: Alan Cox, <alan@lxorguk.ukuu.org.uk>
37 * Video4linux 1/2 integration by Justin Schoeman
38 * <justin@suntiger.ee.up.ac.za>
39 * 2.4 PROCFS support ported from 2.4 kernels by
40 * Iñaki García Etxebarria <garetxe@euskalnet.net>
41 * Makefile fix by "W. Michael Petullo" <mike@flyn.org>
42 * 2.4 devfs support ported from 2.4 kernels by
43 * Dan Merillat <dan@merillat.org>
44 * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman)
47 #include <linux/module.h>
48 #include <linux/types.h>
49 #include <linux/kernel.h>
51 #include <linux/string.h>
52 #include <linux/errno.h>
53 #include <linux/i2c.h>
54 #if defined(CONFIG_SPI)
55 #include <linux/spi/spi.h>
57 #include <asm/uaccess.h>
58 #include <asm/pgtable.h>
60 #include <asm/div64.h>
61 #include <media/v4l2-common.h>
62 #include <media/v4l2-device.h>
63 #include <media/v4l2-ctrls.h>
64 #include <media/v4l2-chip-ident.h>
66 #include <linux/videodev2.h>
68 MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
69 MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
70 MODULE_LICENSE("GPL");
74 * V 4 L 2 D R I V E R H E L P E R A P I
79 * Video Standard Operations (contributed by Michael Schimek)
82 /* Helper functions for control handling */
84 /* Check for correctness of the ctrl's value based on the data from
85 struct v4l2_queryctrl and the available menu items. Note that
86 menu_items may be NULL, in that case it is ignored. */
87 int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
88 const char * const *menu_items)
90 if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
92 if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
94 if (qctrl->type == V4L2_CTRL_TYPE_STRING)
96 if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
97 qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
98 qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
100 if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
102 if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
103 if (menu_items[ctrl->value] == NULL ||
104 menu_items[ctrl->value][0] == '\0')
107 if (qctrl->type == V4L2_CTRL_TYPE_BITMASK &&
108 (ctrl->value & ~qctrl->maximum))
112 EXPORT_SYMBOL(v4l2_ctrl_check);
114 /* Fill in a struct v4l2_queryctrl */
115 int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
119 v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
120 &min, &max, &step, &def, &qctrl->flags);
125 qctrl->minimum = min;
126 qctrl->maximum = max;
128 qctrl->default_value = def;
129 qctrl->reserved[0] = qctrl->reserved[1] = 0;
130 strlcpy(qctrl->name, name, sizeof(qctrl->name));
133 EXPORT_SYMBOL(v4l2_ctrl_query_fill);
135 /* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
136 the menu. The qctrl pointer may be NULL, in which case it is ignored.
137 If menu_items is NULL, then the menu items are retrieved using
138 v4l2_ctrl_get_menu. */
139 int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
140 const char * const *menu_items)
145 if (menu_items == NULL)
146 menu_items = v4l2_ctrl_get_menu(qmenu->id);
147 if (menu_items == NULL ||
148 (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
150 for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
151 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
153 strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name));
156 EXPORT_SYMBOL(v4l2_ctrl_query_menu);
158 /* Fill in a struct v4l2_querymenu based on the specified array of valid
159 menu items (terminated by V4L2_CTRL_MENU_IDS_END).
160 Use this if there are 'holes' in the list of valid menu items. */
161 int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
163 const char * const *menu_items = v4l2_ctrl_get_menu(qmenu->id);
166 if (menu_items == NULL || ids == NULL)
168 while (*ids != V4L2_CTRL_MENU_IDS_END) {
169 if (*ids++ == qmenu->index) {
170 strlcpy(qmenu->name, menu_items[qmenu->index],
171 sizeof(qmenu->name));
177 EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
179 /* ctrl_classes points to an array of u32 pointers, the last element is
180 a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
181 Each array must be sorted low to high and belong to the same control
182 class. The array of u32 pointers must also be sorted, from low class IDs
185 This function returns the first ID that follows after the given ID.
186 When no more controls are available 0 is returned. */
187 u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
189 u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
192 if (ctrl_classes == NULL)
195 /* if no query is desired, then check if the ID is part of ctrl_classes */
196 if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
198 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
200 if (*ctrl_classes == NULL)
202 pctrl = *ctrl_classes;
203 /* find control ID */
204 while (*pctrl && *pctrl != id) pctrl++;
205 return *pctrl ? id : 0;
207 id &= V4L2_CTRL_ID_MASK;
208 id++; /* select next control */
209 /* find first class that matches (or is greater than) the class of
211 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
213 /* no more classes */
214 if (*ctrl_classes == NULL)
216 pctrl = *ctrl_classes;
217 /* find first ctrl within the class that is >= ID */
218 while (*pctrl && *pctrl < id) pctrl++;
221 /* we are at the end of the controls of the current class. */
222 /* continue with next class if available */
224 if (*ctrl_classes == NULL)
226 return **ctrl_classes;
228 EXPORT_SYMBOL(v4l2_ctrl_next);
230 int v4l2_chip_match_host(const struct v4l2_dbg_match *match)
232 switch (match->type) {
233 case V4L2_CHIP_MATCH_HOST:
234 return match->addr == 0;
239 EXPORT_SYMBOL(v4l2_chip_match_host);
241 #if IS_ENABLED(CONFIG_I2C)
242 int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match)
246 if (c == NULL || match == NULL)
249 switch (match->type) {
250 case V4L2_CHIP_MATCH_I2C_DRIVER:
251 if (c->driver == NULL || c->driver->driver.name == NULL)
253 len = strlen(c->driver->driver.name);
254 /* legacy drivers have a ' suffix, don't try to match that */
255 if (len && c->driver->driver.name[len - 1] == '\'')
257 return len && !strncmp(c->driver->driver.name, match->name, len);
258 case V4L2_CHIP_MATCH_I2C_ADDR:
259 return c->addr == match->addr;
264 EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
266 int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip,
267 u32 ident, u32 revision)
269 if (!v4l2_chip_match_i2c_client(c, &chip->match))
271 if (chip->ident == V4L2_IDENT_NONE) {
273 chip->revision = revision;
276 chip->ident = V4L2_IDENT_AMBIGUOUS;
281 EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
283 /* ----------------------------------------------------------------- */
285 /* I2C Helper functions */
288 void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
289 const struct v4l2_subdev_ops *ops)
291 v4l2_subdev_init(sd, ops);
292 sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
293 /* the owner is the same as the i2c_client's driver owner */
294 sd->owner = client->driver->driver.owner;
295 /* i2c_client and v4l2_subdev point to one another */
296 v4l2_set_subdevdata(sd, client);
297 i2c_set_clientdata(client, sd);
298 /* initialize name */
299 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
300 client->driver->driver.name, i2c_adapter_id(client->adapter),
303 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
307 /* Load an i2c sub-device. */
308 struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
309 struct i2c_adapter *adapter, struct i2c_board_info *info,
310 const unsigned short *probe_addrs)
312 struct v4l2_subdev *sd = NULL;
313 struct i2c_client *client;
317 request_module(I2C_MODULE_PREFIX "%s", info->type);
319 /* Create the i2c client */
320 if (info->addr == 0 && probe_addrs)
321 client = i2c_new_probed_device(adapter, info, probe_addrs,
324 client = i2c_new_device(adapter, info);
326 /* Note: by loading the module first we are certain that c->driver
327 will be set if the driver was found. If the module was not loaded
328 first, then the i2c core tries to delay-load the module for us,
329 and then c->driver is still NULL until the module is finally
330 loaded. This delay-load mechanism doesn't work if other drivers
331 want to use the i2c device, so explicitly loading the module
332 is the best alternative. */
333 if (client == NULL || client->driver == NULL)
336 /* Lock the module so we can safely get the v4l2_subdev pointer */
337 if (!try_module_get(client->driver->driver.owner))
339 sd = i2c_get_clientdata(client);
341 /* Register with the v4l2_device which increases the module's
342 use count as well. */
343 if (v4l2_device_register_subdev(v4l2_dev, sd))
345 /* Decrease the module use count to match the first try_module_get. */
346 module_put(client->driver->driver.owner);
349 /* If we have a client but no subdev, then something went wrong and
350 we must unregister the client. */
351 if (client && sd == NULL)
352 i2c_unregister_device(client);
355 EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
357 struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
358 struct i2c_adapter *adapter, const char *client_type,
359 u8 addr, const unsigned short *probe_addrs)
361 struct i2c_board_info info;
363 /* Setup the i2c board info with the device type and
364 the device address. */
365 memset(&info, 0, sizeof(info));
366 strlcpy(info.type, client_type, sizeof(info.type));
369 return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs);
371 EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
373 /* Return i2c client address of v4l2_subdev. */
374 unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
376 struct i2c_client *client = v4l2_get_subdevdata(sd);
378 return client ? client->addr : I2C_CLIENT_END;
380 EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
382 /* Return a list of I2C tuner addresses to probe. Use only if the tuner
383 addresses are unknown. */
384 const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
386 static const unsigned short radio_addrs[] = {
387 #if IS_ENABLED(CONFIG_MEDIA_TUNER_TEA5761)
393 static const unsigned short demod_addrs[] = {
394 0x42, 0x43, 0x4a, 0x4b,
397 static const unsigned short tv_addrs[] = {
398 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
399 0x60, 0x61, 0x62, 0x63, 0x64,
410 case ADDRS_TV_WITH_DEMOD:
415 EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
417 #endif /* defined(CONFIG_I2C) */
419 #if defined(CONFIG_SPI)
421 /* Load an spi sub-device. */
423 void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
424 const struct v4l2_subdev_ops *ops)
426 v4l2_subdev_init(sd, ops);
427 sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
428 /* the owner is the same as the spi_device's driver owner */
429 sd->owner = spi->dev.driver->owner;
430 /* spi_device and v4l2_subdev point to one another */
431 v4l2_set_subdevdata(sd, spi);
432 spi_set_drvdata(spi, sd);
433 /* initialize name */
434 strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
436 EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
438 struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
439 struct spi_master *master, struct spi_board_info *info)
441 struct v4l2_subdev *sd = NULL;
442 struct spi_device *spi = NULL;
446 if (info->modalias[0])
447 request_module(info->modalias);
449 spi = spi_new_device(master, info);
451 if (spi == NULL || spi->dev.driver == NULL)
454 if (!try_module_get(spi->dev.driver->owner))
457 sd = spi_get_drvdata(spi);
459 /* Register with the v4l2_device which increases the module's
460 use count as well. */
461 if (v4l2_device_register_subdev(v4l2_dev, sd))
464 /* Decrease the module use count to match the first try_module_get. */
465 module_put(spi->dev.driver->owner);
468 /* If we have a client but no subdev, then something went wrong and
469 we must unregister the client. */
470 if (spi && sd == NULL)
471 spi_unregister_device(spi);
475 EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
477 #endif /* defined(CONFIG_SPI) */
479 /* Clamp x to be between min and max, aligned to a multiple of 2^align. min
480 * and max don't have to be aligned, but there must be at least one valid
481 * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
482 * of 16 between 17 and 31. */
483 static unsigned int clamp_align(unsigned int x, unsigned int min,
484 unsigned int max, unsigned int align)
486 /* Bits that must be zero to be aligned */
487 unsigned int mask = ~((1 << align) - 1);
489 /* Round to nearest aligned value */
491 x = (x + (1 << (align - 1))) & mask;
493 /* Clamp to aligned value of min and max */
495 x = (min + ~mask) & mask;
502 /* Bound an image to have a width between wmin and wmax, and height between
503 * hmin and hmax, inclusive. Additionally, the width will be a multiple of
504 * 2^walign, the height will be a multiple of 2^halign, and the overall size
505 * (width*height) will be a multiple of 2^salign. The image may be shrunk
506 * or enlarged to fit the alignment constraints.
508 * The width or height maximum must not be smaller than the corresponding
509 * minimum. The alignments must not be so high there are no possible image
510 * sizes within the allowed bounds. wmin and hmin must be at least 1
511 * (don't use 0). If you don't care about a certain alignment, specify 0,
512 * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If
513 * you only want to adjust downward, specify a maximum that's the same as
516 void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
518 u32 *h, unsigned int hmin, unsigned int hmax,
519 unsigned int halign, unsigned int salign)
521 *w = clamp_align(*w, wmin, wmax, walign);
522 *h = clamp_align(*h, hmin, hmax, halign);
524 /* Usually we don't need to align the size and are done now. */
528 /* How much alignment do we have? */
531 /* Enough to satisfy the image alignment? */
532 if (walign + halign < salign) {
533 /* Max walign where there is still a valid width */
534 unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
535 /* Max halign where there is still a valid height */
536 unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
538 /* up the smaller alignment until we have enough */
540 if (halign >= hmaxa ||
541 (walign <= halign && walign < wmaxa)) {
542 *w = clamp_align(*w, wmin, wmax, walign + 1);
545 *h = clamp_align(*h, hmin, hmax, halign + 1);
548 } while (halign + walign < salign);
551 EXPORT_SYMBOL_GPL(v4l_bound_align_image);
554 * v4l_match_dv_timings - check if two timings match
555 * @t1 - compare this v4l2_dv_timings struct...
556 * @t2 - with this struct.
557 * @pclock_delta - the allowed pixelclock deviation.
559 * Compare t1 with t2 with a given margin of error for the pixelclock.
561 bool v4l_match_dv_timings(const struct v4l2_dv_timings *t1,
562 const struct v4l2_dv_timings *t2,
563 unsigned pclock_delta)
565 if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
567 if (t1->bt.width == t2->bt.width &&
568 t1->bt.height == t2->bt.height &&
569 t1->bt.interlaced == t2->bt.interlaced &&
570 t1->bt.polarities == t2->bt.polarities &&
571 t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
572 t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
573 t1->bt.hfrontporch == t2->bt.hfrontporch &&
574 t1->bt.vfrontporch == t2->bt.vfrontporch &&
575 t1->bt.vsync == t2->bt.vsync &&
576 t1->bt.vbackporch == t2->bt.vbackporch &&
577 (!t1->bt.interlaced ||
578 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
579 t1->bt.il_vsync == t2->bt.il_vsync &&
580 t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
584 EXPORT_SYMBOL_GPL(v4l_match_dv_timings);
588 * Based on Coordinated Video Timings Standard
589 * version 1.1 September 10, 2003
592 #define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */
594 /* Normal blanking */
595 #define CVT_MIN_V_BPORCH 7 /* lines */
596 #define CVT_MIN_V_PORCH_RND 3 /* lines */
597 #define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
599 /* Normal blanking for CVT uses GTF to calculate horizontal blanking */
600 #define CVT_CELL_GRAN 8 /* character cell granularity */
601 #define CVT_M 600 /* blanking formula gradient */
602 #define CVT_C 40 /* blanking formula offset */
603 #define CVT_K 128 /* blanking formula scaling factor */
604 #define CVT_J 20 /* blanking formula scaling factor */
605 #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
606 #define CVT_M_PRIME (CVT_K * CVT_M / 256)
608 /* Reduced Blanking */
609 #define CVT_RB_MIN_V_BPORCH 7 /* lines */
610 #define CVT_RB_V_FPORCH 3 /* lines */
611 #define CVT_RB_MIN_V_BLANK 460 /* us */
612 #define CVT_RB_H_SYNC 32 /* pixels */
613 #define CVT_RB_H_BPORCH 80 /* pixels */
614 #define CVT_RB_H_BLANK 160 /* pixels */
616 /** v4l2_detect_cvt - detect if the given timings follow the CVT standard
617 * @frame_height - the total height of the frame (including blanking) in lines.
618 * @hfreq - the horizontal frequency in Hz.
619 * @vsync - the height of the vertical sync in lines.
620 * @polarities - the horizontal and vertical polarities (same as struct
621 * v4l2_bt_timings polarities).
622 * @fmt - the resulting timings.
624 * This function will attempt to detect if the given values correspond to a
625 * valid CVT format. If so, then it will return true, and fmt will be filled
626 * in with the found CVT timings.
628 bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,
629 u32 polarities, struct v4l2_dv_timings *fmt)
631 int v_fp, v_bp, h_fp, h_bp, hsync;
632 int frame_width, image_height, image_width;
633 bool reduced_blanking;
636 if (vsync < 4 || vsync > 7)
639 if (polarities == V4L2_DV_VSYNC_POS_POL)
640 reduced_blanking = false;
641 else if (polarities == V4L2_DV_HSYNC_POS_POL)
642 reduced_blanking = true;
647 if (reduced_blanking) {
648 v_fp = CVT_RB_V_FPORCH;
649 v_bp = (CVT_RB_MIN_V_BLANK * hfreq + 999999) / 1000000;
650 v_bp -= vsync + v_fp;
652 if (v_bp < CVT_RB_MIN_V_BPORCH)
653 v_bp = CVT_RB_MIN_V_BPORCH;
655 v_fp = CVT_MIN_V_PORCH_RND;
656 v_bp = (CVT_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
658 if (v_bp < CVT_MIN_V_BPORCH)
659 v_bp = CVT_MIN_V_BPORCH;
661 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
663 /* Aspect ratio based on vsync */
666 image_width = (image_height * 4) / 3;
669 image_width = (image_height * 16) / 9;
672 image_width = (image_height * 16) / 10;
676 if (image_height == 1024)
677 image_width = (image_height * 5) / 4;
678 else if (image_height == 768)
679 image_width = (image_height * 15) / 9;
687 image_width = image_width & ~7;
690 if (reduced_blanking) {
691 pix_clk = (image_width + CVT_RB_H_BLANK) * hfreq;
692 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
694 h_bp = CVT_RB_H_BPORCH;
695 hsync = CVT_RB_H_SYNC;
696 h_fp = CVT_RB_H_BLANK - h_bp - hsync;
698 frame_width = image_width + CVT_RB_H_BLANK;
701 unsigned ideal_duty_cycle = CVT_C_PRIME - (CVT_M_PRIME * 1000) / hfreq;
703 h_blank = (image_width * ideal_duty_cycle + (100 - ideal_duty_cycle) / 2) /
704 (100 - ideal_duty_cycle);
705 h_blank = h_blank - h_blank % (2 * CVT_CELL_GRAN);
707 if (h_blank * 100 / image_width < 20) {
708 h_blank = image_width / 5;
709 h_blank = (h_blank + 0x7) & ~0x7;
712 pix_clk = (image_width + h_blank) * hfreq;
713 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
716 frame_width = image_width + h_blank;
718 hsync = (frame_width * 8 + 50) / 100;
719 hsync = hsync - hsync % CVT_CELL_GRAN;
720 h_fp = h_blank - hsync - h_bp;
723 fmt->bt.polarities = polarities;
724 fmt->bt.width = image_width;
725 fmt->bt.height = image_height;
726 fmt->bt.hfrontporch = h_fp;
727 fmt->bt.vfrontporch = v_fp;
728 fmt->bt.hsync = hsync;
729 fmt->bt.vsync = vsync;
730 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
731 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
732 fmt->bt.pixelclock = pix_clk;
733 fmt->bt.standards = V4L2_DV_BT_STD_CVT;
734 if (reduced_blanking)
735 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
738 EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
742 * Based on Generalized Timing Formula Standard
743 * Version 1.1 September 2, 1999
746 #define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */
748 #define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
749 #define GTF_V_FP 1 /* vertical front porch (lines) */
750 #define GTF_CELL_GRAN 8 /* character cell granularity */
753 #define GTF_D_M 600 /* blanking formula gradient */
754 #define GTF_D_C 40 /* blanking formula offset */
755 #define GTF_D_K 128 /* blanking formula scaling factor */
756 #define GTF_D_J 20 /* blanking formula scaling factor */
757 #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
758 #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
761 #define GTF_S_M 3600 /* blanking formula gradient */
762 #define GTF_S_C 40 /* blanking formula offset */
763 #define GTF_S_K 128 /* blanking formula scaling factor */
764 #define GTF_S_J 35 /* blanking formula scaling factor */
765 #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
766 #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
768 /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
769 * @frame_height - the total height of the frame (including blanking) in lines.
770 * @hfreq - the horizontal frequency in Hz.
771 * @vsync - the height of the vertical sync in lines.
772 * @polarities - the horizontal and vertical polarities (same as struct
773 * v4l2_bt_timings polarities).
774 * @aspect - preferred aspect ratio. GTF has no method of determining the
775 * aspect ratio in order to derive the image width from the
776 * image height, so it has to be passed explicitly. Usually
777 * the native screen aspect ratio is used for this. If it
778 * is not filled in correctly, then 16:9 will be assumed.
779 * @fmt - the resulting timings.
781 * This function will attempt to detect if the given values correspond to a
782 * valid GTF format. If so, then it will return true, and fmt will be filled
783 * in with the found GTF timings.
785 bool v4l2_detect_gtf(unsigned frame_height,
789 struct v4l2_fract aspect,
790 struct v4l2_dv_timings *fmt)
793 int v_fp, v_bp, h_fp, hsync;
794 int frame_width, image_height, image_width;
801 if (polarities == V4L2_DV_VSYNC_POS_POL)
803 else if (polarities == V4L2_DV_HSYNC_POS_POL)
810 v_bp = (GTF_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
811 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
813 if (aspect.numerator == 0 || aspect.denominator == 0) {
814 aspect.numerator = 16;
815 aspect.denominator = 9;
817 image_width = ((image_height * aspect.numerator) / aspect.denominator);
821 h_blank = ((image_width * GTF_D_C_PRIME * hfreq) -
822 (image_width * GTF_D_M_PRIME * 1000) +
823 (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) / 2) /
824 (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000);
826 h_blank = ((image_width * GTF_S_C_PRIME * hfreq) -
827 (image_width * GTF_S_M_PRIME * 1000) +
828 (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) /
829 (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000);
831 h_blank = h_blank - h_blank % (2 * GTF_CELL_GRAN);
832 frame_width = image_width + h_blank;
834 pix_clk = (image_width + h_blank) * hfreq;
835 pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
837 hsync = (frame_width * 8 + 50) / 100;
838 hsync = hsync - hsync % GTF_CELL_GRAN;
840 h_fp = h_blank / 2 - hsync;
842 fmt->bt.polarities = polarities;
843 fmt->bt.width = image_width;
844 fmt->bt.height = image_height;
845 fmt->bt.hfrontporch = h_fp;
846 fmt->bt.vfrontporch = v_fp;
847 fmt->bt.hsync = hsync;
848 fmt->bt.vsync = vsync;
849 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
850 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
851 fmt->bt.pixelclock = pix_clk;
852 fmt->bt.standards = V4L2_DV_BT_STD_GTF;
854 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
857 EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
859 /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
860 * 0x15 and 0x16 from the EDID.
861 * @hor_landscape - byte 0x15 from the EDID.
862 * @vert_portrait - byte 0x16 from the EDID.
864 * Determines the aspect ratio from the EDID.
865 * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
866 * "Horizontal and Vertical Screen Size or Aspect Ratio"
868 struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
870 struct v4l2_fract aspect = { 16, 9 };
874 /* Nothing filled in, fallback to 16:9 */
875 if (!hor_landscape && !vert_portrait)
877 /* Both filled in, so they are interpreted as the screen size in cm */
878 if (hor_landscape && vert_portrait) {
879 aspect.numerator = hor_landscape;
880 aspect.denominator = vert_portrait;
883 /* Only one is filled in, so interpret them as a ratio:
885 ratio = hor_landscape | vert_portrait;
886 /* Change some rounded values into the exact aspect ratio */
888 aspect.numerator = 16;
889 aspect.denominator = 9;
890 } else if (ratio == 34) {
891 aspect.numerator = 4;
892 aspect.numerator = 3;
893 } else if (ratio == 68) {
894 aspect.numerator = 15;
895 aspect.numerator = 9;
897 aspect.numerator = hor_landscape + 99;
898 aspect.denominator = 100;
902 /* The aspect ratio is for portrait, so swap numerator and denominator */
903 tmp = aspect.denominator;
904 aspect.denominator = aspect.numerator;
905 aspect.numerator = tmp;
908 EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);
910 const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
911 const struct v4l2_discrete_probe *probe,
912 s32 width, s32 height)
915 u32 error, min_error = UINT_MAX;
916 const struct v4l2_frmsize_discrete *size, *best = NULL;
921 for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) {
922 error = abs(size->width - width) + abs(size->height - height);
923 if (error < min_error) {
933 EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
935 void v4l2_get_timestamp(struct timeval *tv)
940 tv->tv_sec = ts.tv_sec;
941 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
943 EXPORT_SYMBOL_GPL(v4l2_get_timestamp);