2 * v4l2-dv-timings - dv-timings helper functions
4 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/rational.h>
26 #include <linux/videodev2.h>
27 #include <linux/v4l2-dv-timings.h>
28 #include <media/v4l2-dv-timings.h>
29 #include <linux/math64.h>
31 MODULE_AUTHOR("Hans Verkuil");
32 MODULE_DESCRIPTION("V4L2 DV Timings Helper Functions");
33 MODULE_LICENSE("GPL");
35 const struct v4l2_dv_timings v4l2_dv_timings_presets[] = {
36 V4L2_DV_BT_CEA_640X480P59_94,
37 V4L2_DV_BT_CEA_720X480I59_94,
38 V4L2_DV_BT_CEA_720X480P59_94,
39 V4L2_DV_BT_CEA_720X576I50,
40 V4L2_DV_BT_CEA_720X576P50,
41 V4L2_DV_BT_CEA_1280X720P24,
42 V4L2_DV_BT_CEA_1280X720P25,
43 V4L2_DV_BT_CEA_1280X720P30,
44 V4L2_DV_BT_CEA_1280X720P50,
45 V4L2_DV_BT_CEA_1280X720P60,
46 V4L2_DV_BT_CEA_1920X1080P24,
47 V4L2_DV_BT_CEA_1920X1080P25,
48 V4L2_DV_BT_CEA_1920X1080P30,
49 V4L2_DV_BT_CEA_1920X1080I50,
50 V4L2_DV_BT_CEA_1920X1080P50,
51 V4L2_DV_BT_CEA_1920X1080I60,
52 V4L2_DV_BT_CEA_1920X1080P60,
53 V4L2_DV_BT_DMT_640X350P85,
54 V4L2_DV_BT_DMT_640X400P85,
55 V4L2_DV_BT_DMT_720X400P85,
56 V4L2_DV_BT_DMT_640X480P72,
57 V4L2_DV_BT_DMT_640X480P75,
58 V4L2_DV_BT_DMT_640X480P85,
59 V4L2_DV_BT_DMT_800X600P56,
60 V4L2_DV_BT_DMT_800X600P60,
61 V4L2_DV_BT_DMT_800X600P72,
62 V4L2_DV_BT_DMT_800X600P75,
63 V4L2_DV_BT_DMT_800X600P85,
64 V4L2_DV_BT_DMT_800X600P120_RB,
65 V4L2_DV_BT_DMT_848X480P60,
66 V4L2_DV_BT_DMT_1024X768I43,
67 V4L2_DV_BT_DMT_1024X768P60,
68 V4L2_DV_BT_DMT_1024X768P70,
69 V4L2_DV_BT_DMT_1024X768P75,
70 V4L2_DV_BT_DMT_1024X768P85,
71 V4L2_DV_BT_DMT_1024X768P120_RB,
72 V4L2_DV_BT_DMT_1152X864P75,
73 V4L2_DV_BT_DMT_1280X768P60_RB,
74 V4L2_DV_BT_DMT_1280X768P60,
75 V4L2_DV_BT_DMT_1280X768P75,
76 V4L2_DV_BT_DMT_1280X768P85,
77 V4L2_DV_BT_DMT_1280X768P120_RB,
78 V4L2_DV_BT_DMT_1280X800P60_RB,
79 V4L2_DV_BT_DMT_1280X800P60,
80 V4L2_DV_BT_DMT_1280X800P75,
81 V4L2_DV_BT_DMT_1280X800P85,
82 V4L2_DV_BT_DMT_1280X800P120_RB,
83 V4L2_DV_BT_DMT_1280X960P60,
84 V4L2_DV_BT_DMT_1280X960P85,
85 V4L2_DV_BT_DMT_1280X960P120_RB,
86 V4L2_DV_BT_DMT_1280X1024P60,
87 V4L2_DV_BT_DMT_1280X1024P75,
88 V4L2_DV_BT_DMT_1280X1024P85,
89 V4L2_DV_BT_DMT_1280X1024P120_RB,
90 V4L2_DV_BT_DMT_1360X768P60,
91 V4L2_DV_BT_DMT_1360X768P120_RB,
92 V4L2_DV_BT_DMT_1366X768P60,
93 V4L2_DV_BT_DMT_1366X768P60_RB,
94 V4L2_DV_BT_DMT_1400X1050P60_RB,
95 V4L2_DV_BT_DMT_1400X1050P60,
96 V4L2_DV_BT_DMT_1400X1050P75,
97 V4L2_DV_BT_DMT_1400X1050P85,
98 V4L2_DV_BT_DMT_1400X1050P120_RB,
99 V4L2_DV_BT_DMT_1440X900P60_RB,
100 V4L2_DV_BT_DMT_1440X900P60,
101 V4L2_DV_BT_DMT_1440X900P75,
102 V4L2_DV_BT_DMT_1440X900P85,
103 V4L2_DV_BT_DMT_1440X900P120_RB,
104 V4L2_DV_BT_DMT_1600X900P60_RB,
105 V4L2_DV_BT_DMT_1600X1200P60,
106 V4L2_DV_BT_DMT_1600X1200P65,
107 V4L2_DV_BT_DMT_1600X1200P70,
108 V4L2_DV_BT_DMT_1600X1200P75,
109 V4L2_DV_BT_DMT_1600X1200P85,
110 V4L2_DV_BT_DMT_1600X1200P120_RB,
111 V4L2_DV_BT_DMT_1680X1050P60_RB,
112 V4L2_DV_BT_DMT_1680X1050P60,
113 V4L2_DV_BT_DMT_1680X1050P75,
114 V4L2_DV_BT_DMT_1680X1050P85,
115 V4L2_DV_BT_DMT_1680X1050P120_RB,
116 V4L2_DV_BT_DMT_1792X1344P60,
117 V4L2_DV_BT_DMT_1792X1344P75,
118 V4L2_DV_BT_DMT_1792X1344P120_RB,
119 V4L2_DV_BT_DMT_1856X1392P60,
120 V4L2_DV_BT_DMT_1856X1392P75,
121 V4L2_DV_BT_DMT_1856X1392P120_RB,
122 V4L2_DV_BT_DMT_1920X1200P60_RB,
123 V4L2_DV_BT_DMT_1920X1200P60,
124 V4L2_DV_BT_DMT_1920X1200P75,
125 V4L2_DV_BT_DMT_1920X1200P85,
126 V4L2_DV_BT_DMT_1920X1200P120_RB,
127 V4L2_DV_BT_DMT_1920X1440P60,
128 V4L2_DV_BT_DMT_1920X1440P75,
129 V4L2_DV_BT_DMT_1920X1440P120_RB,
130 V4L2_DV_BT_DMT_2048X1152P60_RB,
131 V4L2_DV_BT_DMT_2560X1600P60_RB,
132 V4L2_DV_BT_DMT_2560X1600P60,
133 V4L2_DV_BT_DMT_2560X1600P75,
134 V4L2_DV_BT_DMT_2560X1600P85,
135 V4L2_DV_BT_DMT_2560X1600P120_RB,
136 V4L2_DV_BT_CEA_3840X2160P24,
137 V4L2_DV_BT_CEA_3840X2160P25,
138 V4L2_DV_BT_CEA_3840X2160P30,
139 V4L2_DV_BT_CEA_3840X2160P50,
140 V4L2_DV_BT_CEA_3840X2160P60,
141 V4L2_DV_BT_CEA_4096X2160P24,
142 V4L2_DV_BT_CEA_4096X2160P25,
143 V4L2_DV_BT_CEA_4096X2160P30,
144 V4L2_DV_BT_CEA_4096X2160P50,
145 V4L2_DV_BT_DMT_4096X2160P59_94_RB,
146 V4L2_DV_BT_CEA_4096X2160P60,
149 EXPORT_SYMBOL_GPL(v4l2_dv_timings_presets);
151 bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t,
152 const struct v4l2_dv_timings_cap *dvcap,
153 v4l2_check_dv_timings_fnc fnc,
156 const struct v4l2_bt_timings *bt = &t->bt;
157 const struct v4l2_bt_timings_cap *cap = &dvcap->bt;
158 u32 caps = cap->capabilities;
159 const u32 max_vert = 10240;
160 u32 max_hor = 3 * bt->width;
162 if (t->type != V4L2_DV_BT_656_1120)
164 if (t->type != dvcap->type ||
165 bt->height < cap->min_height ||
166 bt->height > cap->max_height ||
167 bt->width < cap->min_width ||
168 bt->width > cap->max_width ||
169 bt->pixelclock < cap->min_pixelclock ||
170 bt->pixelclock > cap->max_pixelclock ||
171 (!(caps & V4L2_DV_BT_CAP_CUSTOM) &&
172 cap->standards && bt->standards &&
173 !(bt->standards & cap->standards)) ||
174 (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) ||
175 (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE)))
178 /* sanity checks for the blanking timings */
179 if (!bt->interlaced &&
180 (bt->il_vbackporch || bt->il_vsync || bt->il_vfrontporch))
183 * Some video receivers cannot properly separate the frontporch,
184 * backporch and sync values, and instead they only have the total
185 * blanking. That can be assigned to any of these three fields.
186 * So just check that none of these are way out of range.
188 if (bt->hfrontporch > max_hor ||
189 bt->hsync > max_hor || bt->hbackporch > max_hor)
191 if (bt->vfrontporch > max_vert ||
192 bt->vsync > max_vert || bt->vbackporch > max_vert)
194 if (bt->interlaced && (bt->il_vfrontporch > max_vert ||
195 bt->il_vsync > max_vert || bt->il_vbackporch > max_vert))
197 return fnc == NULL || fnc(t, fnc_handle);
199 EXPORT_SYMBOL_GPL(v4l2_valid_dv_timings);
201 int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
202 const struct v4l2_dv_timings_cap *cap,
203 v4l2_check_dv_timings_fnc fnc,
208 memset(t->reserved, 0, sizeof(t->reserved));
209 for (i = idx = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
210 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
213 t->timings = v4l2_dv_timings_presets[i];
219 EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap);
221 bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
222 const struct v4l2_dv_timings_cap *cap,
223 unsigned pclock_delta,
224 v4l2_check_dv_timings_fnc fnc,
229 if (!v4l2_valid_dv_timings(t, cap, fnc, fnc_handle))
232 for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
233 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
235 v4l2_match_dv_timings(t, v4l2_dv_timings_presets + i,
236 pclock_delta, false)) {
237 u32 flags = t->bt.flags & V4L2_DV_FL_REDUCED_FPS;
239 *t = v4l2_dv_timings_presets[i];
240 if (can_reduce_fps(&t->bt))
241 t->bt.flags |= flags;
248 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap);
250 bool v4l2_find_dv_timings_cea861_vic(struct v4l2_dv_timings *t, u8 vic)
254 for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
255 const struct v4l2_bt_timings *bt =
256 &v4l2_dv_timings_presets[i].bt;
258 if ((bt->flags & V4L2_DV_FL_HAS_CEA861_VIC) &&
259 bt->cea861_vic == vic) {
260 *t = v4l2_dv_timings_presets[i];
266 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cea861_vic);
269 * v4l2_match_dv_timings - check if two timings match
270 * @t1 - compare this v4l2_dv_timings struct...
271 * @t2 - with this struct.
272 * @pclock_delta - the allowed pixelclock deviation.
273 * @match_reduced_fps - if true, then fail if V4L2_DV_FL_REDUCED_FPS does not
276 * Compare t1 with t2 with a given margin of error for the pixelclock.
278 bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1,
279 const struct v4l2_dv_timings *t2,
280 unsigned pclock_delta, bool match_reduced_fps)
282 if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
284 if (t1->bt.width == t2->bt.width &&
285 t1->bt.height == t2->bt.height &&
286 t1->bt.interlaced == t2->bt.interlaced &&
287 t1->bt.polarities == t2->bt.polarities &&
288 t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
289 t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
290 t1->bt.hfrontporch == t2->bt.hfrontporch &&
291 t1->bt.hsync == t2->bt.hsync &&
292 t1->bt.hbackporch == t2->bt.hbackporch &&
293 t1->bt.vfrontporch == t2->bt.vfrontporch &&
294 t1->bt.vsync == t2->bt.vsync &&
295 t1->bt.vbackporch == t2->bt.vbackporch &&
296 (!match_reduced_fps ||
297 (t1->bt.flags & V4L2_DV_FL_REDUCED_FPS) ==
298 (t2->bt.flags & V4L2_DV_FL_REDUCED_FPS)) &&
299 (!t1->bt.interlaced ||
300 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
301 t1->bt.il_vsync == t2->bt.il_vsync &&
302 t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
306 EXPORT_SYMBOL_GPL(v4l2_match_dv_timings);
308 void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,
309 const struct v4l2_dv_timings *t, bool detailed)
311 const struct v4l2_bt_timings *bt = &t->bt;
315 if (t->type != V4L2_DV_BT_656_1120)
318 htot = V4L2_DV_BT_FRAME_WIDTH(bt);
319 vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
323 fps = (htot * vtot) > 0 ? div_u64((100 * (u64)bt->pixelclock),
329 pr_info("%s: %s%ux%u%s%u.%u (%ux%u)\n", dev_prefix, prefix,
330 bt->width, bt->height, bt->interlaced ? "i" : "p",
331 fps / 100, fps % 100, htot, vtot);
336 pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n",
337 dev_prefix, bt->hfrontporch,
338 (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
339 bt->hsync, bt->hbackporch);
340 pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n",
341 dev_prefix, bt->vfrontporch,
342 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
343 bt->vsync, bt->vbackporch);
345 pr_info("%s: vertical bottom field: fp = %u, %ssync = %u, bp = %u\n",
346 dev_prefix, bt->il_vfrontporch,
347 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
348 bt->il_vsync, bt->il_vbackporch);
349 pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock);
350 pr_info("%s: flags (0x%x):%s%s%s%s%s%s%s%s%s%s\n",
351 dev_prefix, bt->flags,
352 (bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ?
353 " REDUCED_BLANKING" : "",
354 ((bt->flags & V4L2_DV_FL_REDUCED_BLANKING) &&
355 bt->vsync == 8) ? " (V2)" : "",
356 (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ?
357 " CAN_REDUCE_FPS" : "",
358 (bt->flags & V4L2_DV_FL_REDUCED_FPS) ?
360 (bt->flags & V4L2_DV_FL_HALF_LINE) ?
362 (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) ?
364 (bt->flags & V4L2_DV_FL_FIRST_FIELD_EXTRA_LINE) ?
365 " FIRST_FIELD_EXTRA_LINE" : "",
366 (bt->flags & V4L2_DV_FL_HAS_PICTURE_ASPECT) ?
367 " HAS_PICTURE_ASPECT" : "",
368 (bt->flags & V4L2_DV_FL_HAS_CEA861_VIC) ?
369 " HAS_CEA861_VIC" : "",
370 (bt->flags & V4L2_DV_FL_HAS_HDMI_VIC) ?
371 " HAS_HDMI_VIC" : "");
372 pr_info("%s: standards (0x%x):%s%s%s%s%s\n", dev_prefix, bt->standards,
373 (bt->standards & V4L2_DV_BT_STD_CEA861) ? " CEA" : "",
374 (bt->standards & V4L2_DV_BT_STD_DMT) ? " DMT" : "",
375 (bt->standards & V4L2_DV_BT_STD_CVT) ? " CVT" : "",
376 (bt->standards & V4L2_DV_BT_STD_GTF) ? " GTF" : "",
377 (bt->standards & V4L2_DV_BT_STD_SDI) ? " SDI" : "");
378 if (bt->flags & V4L2_DV_FL_HAS_PICTURE_ASPECT)
379 pr_info("%s: picture aspect (hor:vert): %u:%u\n", dev_prefix,
380 bt->picture_aspect.numerator,
381 bt->picture_aspect.denominator);
382 if (bt->flags & V4L2_DV_FL_HAS_CEA861_VIC)
383 pr_info("%s: CEA-861 VIC: %u\n", dev_prefix, bt->cea861_vic);
384 if (bt->flags & V4L2_DV_FL_HAS_HDMI_VIC)
385 pr_info("%s: HDMI VIC: %u\n", dev_prefix, bt->hdmi_vic);
387 EXPORT_SYMBOL_GPL(v4l2_print_dv_timings);
389 struct v4l2_fract v4l2_dv_timings_aspect_ratio(const struct v4l2_dv_timings *t)
391 struct v4l2_fract ratio = { 1, 1 };
394 if (t->type != V4L2_DV_BT_656_1120)
396 if (!(t->bt.flags & V4L2_DV_FL_HAS_PICTURE_ASPECT))
399 ratio.numerator = t->bt.width * t->bt.picture_aspect.denominator;
400 ratio.denominator = t->bt.height * t->bt.picture_aspect.numerator;
402 rational_best_approximation(ratio.numerator, ratio.denominator,
403 ratio.numerator, ratio.denominator, &n, &d);
405 ratio.denominator = d;
408 EXPORT_SYMBOL_GPL(v4l2_dv_timings_aspect_ratio);
412 * Based on Coordinated Video Timings Standard
413 * version 1.1 September 10, 2003
416 #define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */
417 #define CVT_PXL_CLK_GRAN_RB_V2 1000 /* granularity for reduced blanking v2*/
419 /* Normal blanking */
420 #define CVT_MIN_V_BPORCH 7 /* lines */
421 #define CVT_MIN_V_PORCH_RND 3 /* lines */
422 #define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
423 #define CVT_HSYNC_PERCENT 8 /* nominal hsync as percentage of line */
425 /* Normal blanking for CVT uses GTF to calculate horizontal blanking */
426 #define CVT_CELL_GRAN 8 /* character cell granularity */
427 #define CVT_M 600 /* blanking formula gradient */
428 #define CVT_C 40 /* blanking formula offset */
429 #define CVT_K 128 /* blanking formula scaling factor */
430 #define CVT_J 20 /* blanking formula scaling factor */
431 #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
432 #define CVT_M_PRIME (CVT_K * CVT_M / 256)
434 /* Reduced Blanking */
435 #define CVT_RB_MIN_V_BPORCH 7 /* lines */
436 #define CVT_RB_V_FPORCH 3 /* lines */
437 #define CVT_RB_MIN_V_BLANK 460 /* us */
438 #define CVT_RB_H_SYNC 32 /* pixels */
439 #define CVT_RB_H_BLANK 160 /* pixels */
440 /* Reduce blanking Version 2 */
441 #define CVT_RB_V2_H_BLANK 80 /* pixels */
442 #define CVT_RB_MIN_V_FPORCH 3 /* lines */
443 #define CVT_RB_V2_MIN_V_FPORCH 1 /* lines */
444 #define CVT_RB_V_BPORCH 6 /* lines */
446 /** v4l2_detect_cvt - detect if the given timings follow the CVT standard
447 * @frame_height - the total height of the frame (including blanking) in lines.
448 * @hfreq - the horizontal frequency in Hz.
449 * @vsync - the height of the vertical sync in lines.
450 * @active_width - active width of image (does not include blanking). This
451 * information is needed only in case of version 2 of reduced blanking.
452 * In other cases, this parameter does not have any effect on timings.
453 * @polarities - the horizontal and vertical polarities (same as struct
454 * v4l2_bt_timings polarities).
455 * @interlaced - if this flag is true, it indicates interlaced format
456 * @fmt - the resulting timings.
458 * This function will attempt to detect if the given values correspond to a
459 * valid CVT format. If so, then it will return true, and fmt will be filled
460 * in with the found CVT timings.
462 bool v4l2_detect_cvt(unsigned frame_height,
465 unsigned active_width,
468 struct v4l2_dv_timings *fmt)
470 int v_fp, v_bp, h_fp, h_bp, hsync;
471 int frame_width, image_height, image_width;
472 bool reduced_blanking;
476 if (vsync < 4 || vsync > 8)
479 if (polarities == V4L2_DV_VSYNC_POS_POL)
480 reduced_blanking = false;
481 else if (polarities == V4L2_DV_HSYNC_POS_POL)
482 reduced_blanking = true;
486 if (reduced_blanking && vsync == 8)
489 if (rb_v2 && active_width == 0)
492 if (!rb_v2 && vsync > 7)
499 if (reduced_blanking) {
501 v_bp = CVT_RB_V_BPORCH;
502 v_fp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
503 v_fp -= vsync + v_bp;
505 if (v_fp < CVT_RB_V2_MIN_V_FPORCH)
506 v_fp = CVT_RB_V2_MIN_V_FPORCH;
508 v_fp = CVT_RB_V_FPORCH;
509 v_bp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
510 v_bp -= vsync + v_fp;
512 if (v_bp < CVT_RB_MIN_V_BPORCH)
513 v_bp = CVT_RB_MIN_V_BPORCH;
516 v_fp = CVT_MIN_V_PORCH_RND;
517 v_bp = (CVT_MIN_VSYNC_BP * hfreq) / 1000000 + 1 - vsync;
519 if (v_bp < CVT_MIN_V_BPORCH)
520 v_bp = CVT_MIN_V_BPORCH;
524 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
526 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
528 if (image_height < 0)
531 /* Aspect ratio based on vsync */
534 image_width = (image_height * 4) / 3;
537 image_width = (image_height * 16) / 9;
540 image_width = (image_height * 16) / 10;
544 if (image_height == 1024)
545 image_width = (image_height * 5) / 4;
546 else if (image_height == 768)
547 image_width = (image_height * 15) / 9;
552 image_width = active_width;
559 image_width = image_width & ~7;
562 if (reduced_blanking) {
566 h_blank = rb_v2 ? CVT_RB_V2_H_BLANK : CVT_RB_H_BLANK;
567 clk_gran = rb_v2 ? CVT_PXL_CLK_GRAN_RB_V2 : CVT_PXL_CLK_GRAN;
569 pix_clk = (image_width + h_blank) * hfreq;
570 pix_clk = (pix_clk / clk_gran) * clk_gran;
573 hsync = CVT_RB_H_SYNC;
574 h_fp = h_blank - h_bp - hsync;
576 frame_width = image_width + h_blank;
578 unsigned ideal_duty_cycle_per_myriad =
579 100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq;
582 if (ideal_duty_cycle_per_myriad < 2000)
583 ideal_duty_cycle_per_myriad = 2000;
585 h_blank = image_width * ideal_duty_cycle_per_myriad /
586 (10000 - ideal_duty_cycle_per_myriad);
587 h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN;
589 pix_clk = (image_width + h_blank) * hfreq;
590 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
593 frame_width = image_width + h_blank;
595 hsync = frame_width * CVT_HSYNC_PERCENT / 100;
596 hsync = (hsync / CVT_CELL_GRAN) * CVT_CELL_GRAN;
597 h_fp = h_blank - hsync - h_bp;
600 fmt->type = V4L2_DV_BT_656_1120;
601 fmt->bt.polarities = polarities;
602 fmt->bt.width = image_width;
603 fmt->bt.height = image_height;
604 fmt->bt.hfrontporch = h_fp;
605 fmt->bt.vfrontporch = v_fp;
606 fmt->bt.hsync = hsync;
607 fmt->bt.vsync = vsync;
608 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
611 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
612 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
614 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
616 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
617 2 * vsync - fmt->bt.vbackporch;
618 fmt->bt.il_vfrontporch = v_fp;
619 fmt->bt.il_vsync = vsync;
620 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
621 fmt->bt.interlaced = V4L2_DV_INTERLACED;
624 fmt->bt.pixelclock = pix_clk;
625 fmt->bt.standards = V4L2_DV_BT_STD_CVT;
627 if (reduced_blanking)
628 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
632 EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
636 * Based on Generalized Timing Formula Standard
637 * Version 1.1 September 2, 1999
640 #define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */
642 #define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
643 #define GTF_V_FP 1 /* vertical front porch (lines) */
644 #define GTF_CELL_GRAN 8 /* character cell granularity */
647 #define GTF_D_M 600 /* blanking formula gradient */
648 #define GTF_D_C 40 /* blanking formula offset */
649 #define GTF_D_K 128 /* blanking formula scaling factor */
650 #define GTF_D_J 20 /* blanking formula scaling factor */
651 #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
652 #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
655 #define GTF_S_M 3600 /* blanking formula gradient */
656 #define GTF_S_C 40 /* blanking formula offset */
657 #define GTF_S_K 128 /* blanking formula scaling factor */
658 #define GTF_S_J 35 /* blanking formula scaling factor */
659 #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
660 #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
662 /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
663 * @frame_height - the total height of the frame (including blanking) in lines.
664 * @hfreq - the horizontal frequency in Hz.
665 * @vsync - the height of the vertical sync in lines.
666 * @polarities - the horizontal and vertical polarities (same as struct
667 * v4l2_bt_timings polarities).
668 * @interlaced - if this flag is true, it indicates interlaced format
669 * @aspect - preferred aspect ratio. GTF has no method of determining the
670 * aspect ratio in order to derive the image width from the
671 * image height, so it has to be passed explicitly. Usually
672 * the native screen aspect ratio is used for this. If it
673 * is not filled in correctly, then 16:9 will be assumed.
674 * @fmt - the resulting timings.
676 * This function will attempt to detect if the given values correspond to a
677 * valid GTF format. If so, then it will return true, and fmt will be filled
678 * in with the found GTF timings.
680 bool v4l2_detect_gtf(unsigned frame_height,
685 struct v4l2_fract aspect,
686 struct v4l2_dv_timings *fmt)
689 int v_fp, v_bp, h_fp, hsync;
690 int frame_width, image_height, image_width;
697 if (polarities == V4L2_DV_VSYNC_POS_POL)
699 else if (polarities == V4L2_DV_HSYNC_POS_POL)
709 v_bp = (GTF_MIN_VSYNC_BP * hfreq + 500000) / 1000000 - vsync;
711 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
713 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
715 if (image_height < 0)
718 if (aspect.numerator == 0 || aspect.denominator == 0) {
719 aspect.numerator = 16;
720 aspect.denominator = 9;
722 image_width = ((image_height * aspect.numerator) / aspect.denominator);
723 image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1);
730 num = ((image_width * GTF_D_C_PRIME * (u64)hfreq) -
731 ((u64)image_width * GTF_D_M_PRIME * 1000));
732 den = (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) *
734 h_blank = div_u64((num + (den >> 1)), den);
735 h_blank *= (2 * GTF_CELL_GRAN);
740 num = ((image_width * GTF_S_C_PRIME * (u64)hfreq) -
741 ((u64)image_width * GTF_S_M_PRIME * 1000));
742 den = (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) *
744 h_blank = div_u64((num + (den >> 1)), den);
745 h_blank *= (2 * GTF_CELL_GRAN);
748 frame_width = image_width + h_blank;
750 pix_clk = (image_width + h_blank) * hfreq;
751 pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
753 hsync = (frame_width * 8 + 50) / 100;
754 hsync = ((hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN) * GTF_CELL_GRAN;
756 h_fp = h_blank / 2 - hsync;
758 fmt->type = V4L2_DV_BT_656_1120;
759 fmt->bt.polarities = polarities;
760 fmt->bt.width = image_width;
761 fmt->bt.height = image_height;
762 fmt->bt.hfrontporch = h_fp;
763 fmt->bt.vfrontporch = v_fp;
764 fmt->bt.hsync = hsync;
765 fmt->bt.vsync = vsync;
766 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
769 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
770 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
772 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
774 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
775 2 * vsync - fmt->bt.vbackporch;
776 fmt->bt.il_vfrontporch = v_fp;
777 fmt->bt.il_vsync = vsync;
778 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
779 fmt->bt.interlaced = V4L2_DV_INTERLACED;
782 fmt->bt.pixelclock = pix_clk;
783 fmt->bt.standards = V4L2_DV_BT_STD_GTF;
786 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
790 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.
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"
801 struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
803 struct v4l2_fract aspect = { 16, 9 };
806 /* Nothing filled in, fallback to 16:9 */
807 if (!hor_landscape && !vert_portrait)
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;
815 /* Only one is filled in, so interpret them as a ratio:
817 ratio = hor_landscape | vert_portrait;
818 /* Change some rounded values into the exact aspect ratio */
820 aspect.numerator = 16;
821 aspect.denominator = 9;
822 } else if (ratio == 34) {
823 aspect.numerator = 4;
824 aspect.denominator = 3;
825 } else if (ratio == 68) {
826 aspect.numerator = 15;
827 aspect.denominator = 9;
829 aspect.numerator = hor_landscape + 99;
830 aspect.denominator = 100;
834 /* The aspect ratio is for portrait, so swap numerator and denominator */
835 swap(aspect.denominator, aspect.numerator);
838 EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);