1 // SPDX-License-Identifier: GPL-2.0-only
3 * v4l2-dv-timings - dv-timings helper functions
5 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/rational.h>
13 #include <linux/videodev2.h>
14 #include <linux/v4l2-dv-timings.h>
15 #include <media/v4l2-dv-timings.h>
16 #include <linux/math64.h>
17 #include <linux/hdmi.h>
18 #include <media/cec.h>
20 MODULE_AUTHOR("Hans Verkuil");
21 MODULE_DESCRIPTION("V4L2 DV Timings Helper Functions");
22 MODULE_LICENSE("GPL");
24 const struct v4l2_dv_timings v4l2_dv_timings_presets[] = {
25 V4L2_DV_BT_CEA_640X480P59_94,
26 V4L2_DV_BT_CEA_720X480I59_94,
27 V4L2_DV_BT_CEA_720X480P59_94,
28 V4L2_DV_BT_CEA_720X576I50,
29 V4L2_DV_BT_CEA_720X576P50,
30 V4L2_DV_BT_CEA_1280X720P24,
31 V4L2_DV_BT_CEA_1280X720P25,
32 V4L2_DV_BT_CEA_1280X720P30,
33 V4L2_DV_BT_CEA_1280X720P50,
34 V4L2_DV_BT_CEA_1280X720P60,
35 V4L2_DV_BT_CEA_1920X1080P24,
36 V4L2_DV_BT_CEA_1920X1080P25,
37 V4L2_DV_BT_CEA_1920X1080P30,
38 V4L2_DV_BT_CEA_1920X1080I50,
39 V4L2_DV_BT_CEA_1920X1080P50,
40 V4L2_DV_BT_CEA_1920X1080I60,
41 V4L2_DV_BT_CEA_1920X1080P60,
42 V4L2_DV_BT_DMT_640X350P85,
43 V4L2_DV_BT_DMT_640X400P85,
44 V4L2_DV_BT_DMT_720X400P85,
45 V4L2_DV_BT_DMT_640X480P72,
46 V4L2_DV_BT_DMT_640X480P75,
47 V4L2_DV_BT_DMT_640X480P85,
48 V4L2_DV_BT_DMT_800X600P56,
49 V4L2_DV_BT_DMT_800X600P60,
50 V4L2_DV_BT_DMT_800X600P72,
51 V4L2_DV_BT_DMT_800X600P75,
52 V4L2_DV_BT_DMT_800X600P85,
53 V4L2_DV_BT_DMT_800X600P120_RB,
54 V4L2_DV_BT_DMT_848X480P60,
55 V4L2_DV_BT_DMT_1024X768I43,
56 V4L2_DV_BT_DMT_1024X768P60,
57 V4L2_DV_BT_DMT_1024X768P70,
58 V4L2_DV_BT_DMT_1024X768P75,
59 V4L2_DV_BT_DMT_1024X768P85,
60 V4L2_DV_BT_DMT_1024X768P120_RB,
61 V4L2_DV_BT_DMT_1152X864P75,
62 V4L2_DV_BT_DMT_1280X768P60_RB,
63 V4L2_DV_BT_DMT_1280X768P60,
64 V4L2_DV_BT_DMT_1280X768P75,
65 V4L2_DV_BT_DMT_1280X768P85,
66 V4L2_DV_BT_DMT_1280X768P120_RB,
67 V4L2_DV_BT_DMT_1280X800P60_RB,
68 V4L2_DV_BT_DMT_1280X800P60,
69 V4L2_DV_BT_DMT_1280X800P75,
70 V4L2_DV_BT_DMT_1280X800P85,
71 V4L2_DV_BT_DMT_1280X800P120_RB,
72 V4L2_DV_BT_DMT_1280X960P60,
73 V4L2_DV_BT_DMT_1280X960P85,
74 V4L2_DV_BT_DMT_1280X960P120_RB,
75 V4L2_DV_BT_DMT_1280X1024P60,
76 V4L2_DV_BT_DMT_1280X1024P75,
77 V4L2_DV_BT_DMT_1280X1024P85,
78 V4L2_DV_BT_DMT_1280X1024P120_RB,
79 V4L2_DV_BT_DMT_1360X768P60,
80 V4L2_DV_BT_DMT_1360X768P120_RB,
81 V4L2_DV_BT_DMT_1366X768P60,
82 V4L2_DV_BT_DMT_1366X768P60_RB,
83 V4L2_DV_BT_DMT_1400X1050P60_RB,
84 V4L2_DV_BT_DMT_1400X1050P60,
85 V4L2_DV_BT_DMT_1400X1050P75,
86 V4L2_DV_BT_DMT_1400X1050P85,
87 V4L2_DV_BT_DMT_1400X1050P120_RB,
88 V4L2_DV_BT_DMT_1440X900P60_RB,
89 V4L2_DV_BT_DMT_1440X900P60,
90 V4L2_DV_BT_DMT_1440X900P75,
91 V4L2_DV_BT_DMT_1440X900P85,
92 V4L2_DV_BT_DMT_1440X900P120_RB,
93 V4L2_DV_BT_DMT_1600X900P60_RB,
94 V4L2_DV_BT_DMT_1600X1200P60,
95 V4L2_DV_BT_DMT_1600X1200P65,
96 V4L2_DV_BT_DMT_1600X1200P70,
97 V4L2_DV_BT_DMT_1600X1200P75,
98 V4L2_DV_BT_DMT_1600X1200P85,
99 V4L2_DV_BT_DMT_1600X1200P120_RB,
100 V4L2_DV_BT_DMT_1680X1050P60_RB,
101 V4L2_DV_BT_DMT_1680X1050P60,
102 V4L2_DV_BT_DMT_1680X1050P75,
103 V4L2_DV_BT_DMT_1680X1050P85,
104 V4L2_DV_BT_DMT_1680X1050P120_RB,
105 V4L2_DV_BT_DMT_1792X1344P60,
106 V4L2_DV_BT_DMT_1792X1344P75,
107 V4L2_DV_BT_DMT_1792X1344P120_RB,
108 V4L2_DV_BT_DMT_1856X1392P60,
109 V4L2_DV_BT_DMT_1856X1392P75,
110 V4L2_DV_BT_DMT_1856X1392P120_RB,
111 V4L2_DV_BT_DMT_1920X1200P60_RB,
112 V4L2_DV_BT_DMT_1920X1200P60,
113 V4L2_DV_BT_DMT_1920X1200P75,
114 V4L2_DV_BT_DMT_1920X1200P85,
115 V4L2_DV_BT_DMT_1920X1200P120_RB,
116 V4L2_DV_BT_DMT_1920X1440P60,
117 V4L2_DV_BT_DMT_1920X1440P75,
118 V4L2_DV_BT_DMT_1920X1440P120_RB,
119 V4L2_DV_BT_DMT_2048X1152P60_RB,
120 V4L2_DV_BT_DMT_2560X1600P60_RB,
121 V4L2_DV_BT_DMT_2560X1600P60,
122 V4L2_DV_BT_DMT_2560X1600P75,
123 V4L2_DV_BT_DMT_2560X1600P85,
124 V4L2_DV_BT_DMT_2560X1600P120_RB,
125 V4L2_DV_BT_CEA_3840X2160P24,
126 V4L2_DV_BT_CEA_3840X2160P25,
127 V4L2_DV_BT_CEA_3840X2160P30,
128 V4L2_DV_BT_CEA_3840X2160P50,
129 V4L2_DV_BT_CEA_3840X2160P60,
130 V4L2_DV_BT_CEA_4096X2160P24,
131 V4L2_DV_BT_CEA_4096X2160P25,
132 V4L2_DV_BT_CEA_4096X2160P30,
133 V4L2_DV_BT_CEA_4096X2160P50,
134 V4L2_DV_BT_DMT_4096X2160P59_94_RB,
135 V4L2_DV_BT_CEA_4096X2160P60,
138 EXPORT_SYMBOL_GPL(v4l2_dv_timings_presets);
140 bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t,
141 const struct v4l2_dv_timings_cap *dvcap,
142 v4l2_check_dv_timings_fnc fnc,
145 const struct v4l2_bt_timings *bt = &t->bt;
146 const struct v4l2_bt_timings_cap *cap = &dvcap->bt;
147 u32 caps = cap->capabilities;
149 if (t->type != V4L2_DV_BT_656_1120)
151 if (t->type != dvcap->type ||
152 bt->height < cap->min_height ||
153 bt->height > cap->max_height ||
154 bt->width < cap->min_width ||
155 bt->width > cap->max_width ||
156 bt->pixelclock < cap->min_pixelclock ||
157 bt->pixelclock > cap->max_pixelclock ||
158 (!(caps & V4L2_DV_BT_CAP_CUSTOM) &&
159 cap->standards && bt->standards &&
160 !(bt->standards & cap->standards)) ||
161 (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) ||
162 (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE)))
164 return fnc == NULL || fnc(t, fnc_handle);
166 EXPORT_SYMBOL_GPL(v4l2_valid_dv_timings);
168 int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
169 const struct v4l2_dv_timings_cap *cap,
170 v4l2_check_dv_timings_fnc fnc,
175 memset(t->reserved, 0, sizeof(t->reserved));
176 for (i = idx = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
177 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
180 t->timings = v4l2_dv_timings_presets[i];
186 EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap);
188 bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
189 const struct v4l2_dv_timings_cap *cap,
190 unsigned pclock_delta,
191 v4l2_check_dv_timings_fnc fnc,
196 if (!v4l2_valid_dv_timings(t, cap, fnc, fnc_handle))
199 for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
200 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
202 v4l2_match_dv_timings(t, v4l2_dv_timings_presets + i,
203 pclock_delta, false)) {
204 u32 flags = t->bt.flags & V4L2_DV_FL_REDUCED_FPS;
206 *t = v4l2_dv_timings_presets[i];
207 if (can_reduce_fps(&t->bt))
208 t->bt.flags |= flags;
215 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap);
217 bool v4l2_find_dv_timings_cea861_vic(struct v4l2_dv_timings *t, u8 vic)
221 for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
222 const struct v4l2_bt_timings *bt =
223 &v4l2_dv_timings_presets[i].bt;
225 if ((bt->flags & V4L2_DV_FL_HAS_CEA861_VIC) &&
226 bt->cea861_vic == vic) {
227 *t = v4l2_dv_timings_presets[i];
233 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cea861_vic);
236 * v4l2_match_dv_timings - check if two timings match
237 * @t1: compare this v4l2_dv_timings struct...
238 * @t2: with this struct.
239 * @pclock_delta: the allowed pixelclock deviation.
240 * @match_reduced_fps: if true, then fail if V4L2_DV_FL_REDUCED_FPS does not
243 * Compare t1 with t2 with a given margin of error for the pixelclock.
245 bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1,
246 const struct v4l2_dv_timings *t2,
247 unsigned pclock_delta, bool match_reduced_fps)
249 if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
251 if (t1->bt.width == t2->bt.width &&
252 t1->bt.height == t2->bt.height &&
253 t1->bt.interlaced == t2->bt.interlaced &&
254 t1->bt.polarities == t2->bt.polarities &&
255 t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
256 t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
257 t1->bt.hfrontporch == t2->bt.hfrontporch &&
258 t1->bt.hsync == t2->bt.hsync &&
259 t1->bt.hbackporch == t2->bt.hbackporch &&
260 t1->bt.vfrontporch == t2->bt.vfrontporch &&
261 t1->bt.vsync == t2->bt.vsync &&
262 t1->bt.vbackporch == t2->bt.vbackporch &&
263 (!match_reduced_fps ||
264 (t1->bt.flags & V4L2_DV_FL_REDUCED_FPS) ==
265 (t2->bt.flags & V4L2_DV_FL_REDUCED_FPS)) &&
266 (!t1->bt.interlaced ||
267 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
268 t1->bt.il_vsync == t2->bt.il_vsync &&
269 t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
273 EXPORT_SYMBOL_GPL(v4l2_match_dv_timings);
275 void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,
276 const struct v4l2_dv_timings *t, bool detailed)
278 const struct v4l2_bt_timings *bt = &t->bt;
282 if (t->type != V4L2_DV_BT_656_1120)
285 htot = V4L2_DV_BT_FRAME_WIDTH(bt);
286 vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
290 fps = (htot * vtot) > 0 ? div_u64((100 * (u64)bt->pixelclock),
296 pr_info("%s: %s%ux%u%s%u.%u (%ux%u)\n", dev_prefix, prefix,
297 bt->width, bt->height, bt->interlaced ? "i" : "p",
298 fps / 100, fps % 100, htot, vtot);
303 pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n",
304 dev_prefix, bt->hfrontporch,
305 (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
306 bt->hsync, bt->hbackporch);
307 pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n",
308 dev_prefix, bt->vfrontporch,
309 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
310 bt->vsync, bt->vbackporch);
312 pr_info("%s: vertical bottom field: fp = %u, %ssync = %u, bp = %u\n",
313 dev_prefix, bt->il_vfrontporch,
314 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
315 bt->il_vsync, bt->il_vbackporch);
316 pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock);
317 pr_info("%s: flags (0x%x):%s%s%s%s%s%s%s%s%s%s\n",
318 dev_prefix, bt->flags,
319 (bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ?
320 " REDUCED_BLANKING" : "",
321 ((bt->flags & V4L2_DV_FL_REDUCED_BLANKING) &&
322 bt->vsync == 8) ? " (V2)" : "",
323 (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ?
324 " CAN_REDUCE_FPS" : "",
325 (bt->flags & V4L2_DV_FL_REDUCED_FPS) ?
327 (bt->flags & V4L2_DV_FL_HALF_LINE) ?
329 (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) ?
331 (bt->flags & V4L2_DV_FL_FIRST_FIELD_EXTRA_LINE) ?
332 " FIRST_FIELD_EXTRA_LINE" : "",
333 (bt->flags & V4L2_DV_FL_HAS_PICTURE_ASPECT) ?
334 " HAS_PICTURE_ASPECT" : "",
335 (bt->flags & V4L2_DV_FL_HAS_CEA861_VIC) ?
336 " HAS_CEA861_VIC" : "",
337 (bt->flags & V4L2_DV_FL_HAS_HDMI_VIC) ?
338 " HAS_HDMI_VIC" : "");
339 pr_info("%s: standards (0x%x):%s%s%s%s%s\n", dev_prefix, bt->standards,
340 (bt->standards & V4L2_DV_BT_STD_CEA861) ? " CEA" : "",
341 (bt->standards & V4L2_DV_BT_STD_DMT) ? " DMT" : "",
342 (bt->standards & V4L2_DV_BT_STD_CVT) ? " CVT" : "",
343 (bt->standards & V4L2_DV_BT_STD_GTF) ? " GTF" : "",
344 (bt->standards & V4L2_DV_BT_STD_SDI) ? " SDI" : "");
345 if (bt->flags & V4L2_DV_FL_HAS_PICTURE_ASPECT)
346 pr_info("%s: picture aspect (hor:vert): %u:%u\n", dev_prefix,
347 bt->picture_aspect.numerator,
348 bt->picture_aspect.denominator);
349 if (bt->flags & V4L2_DV_FL_HAS_CEA861_VIC)
350 pr_info("%s: CEA-861 VIC: %u\n", dev_prefix, bt->cea861_vic);
351 if (bt->flags & V4L2_DV_FL_HAS_HDMI_VIC)
352 pr_info("%s: HDMI VIC: %u\n", dev_prefix, bt->hdmi_vic);
354 EXPORT_SYMBOL_GPL(v4l2_print_dv_timings);
356 struct v4l2_fract v4l2_dv_timings_aspect_ratio(const struct v4l2_dv_timings *t)
358 struct v4l2_fract ratio = { 1, 1 };
361 if (t->type != V4L2_DV_BT_656_1120)
363 if (!(t->bt.flags & V4L2_DV_FL_HAS_PICTURE_ASPECT))
366 ratio.numerator = t->bt.width * t->bt.picture_aspect.denominator;
367 ratio.denominator = t->bt.height * t->bt.picture_aspect.numerator;
369 rational_best_approximation(ratio.numerator, ratio.denominator,
370 ratio.numerator, ratio.denominator, &n, &d);
372 ratio.denominator = d;
375 EXPORT_SYMBOL_GPL(v4l2_dv_timings_aspect_ratio);
379 * Based on Coordinated Video Timings Standard
380 * version 1.1 September 10, 2003
383 #define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */
384 #define CVT_PXL_CLK_GRAN_RB_V2 1000 /* granularity for reduced blanking v2*/
386 /* Normal blanking */
387 #define CVT_MIN_V_BPORCH 7 /* lines */
388 #define CVT_MIN_V_PORCH_RND 3 /* lines */
389 #define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
390 #define CVT_HSYNC_PERCENT 8 /* nominal hsync as percentage of line */
392 /* Normal blanking for CVT uses GTF to calculate horizontal blanking */
393 #define CVT_CELL_GRAN 8 /* character cell granularity */
394 #define CVT_M 600 /* blanking formula gradient */
395 #define CVT_C 40 /* blanking formula offset */
396 #define CVT_K 128 /* blanking formula scaling factor */
397 #define CVT_J 20 /* blanking formula scaling factor */
398 #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
399 #define CVT_M_PRIME (CVT_K * CVT_M / 256)
401 /* Reduced Blanking */
402 #define CVT_RB_MIN_V_BPORCH 7 /* lines */
403 #define CVT_RB_V_FPORCH 3 /* lines */
404 #define CVT_RB_MIN_V_BLANK 460 /* us */
405 #define CVT_RB_H_SYNC 32 /* pixels */
406 #define CVT_RB_H_BLANK 160 /* pixels */
407 /* Reduce blanking Version 2 */
408 #define CVT_RB_V2_H_BLANK 80 /* pixels */
409 #define CVT_RB_MIN_V_FPORCH 3 /* lines */
410 #define CVT_RB_V2_MIN_V_FPORCH 1 /* lines */
411 #define CVT_RB_V_BPORCH 6 /* lines */
413 /** v4l2_detect_cvt - detect if the given timings follow the CVT standard
414 * @frame_height - the total height of the frame (including blanking) in lines.
415 * @hfreq - the horizontal frequency in Hz.
416 * @vsync - the height of the vertical sync in lines.
417 * @active_width - active width of image (does not include blanking). This
418 * information is needed only in case of version 2 of reduced blanking.
419 * In other cases, this parameter does not have any effect on timings.
420 * @polarities - the horizontal and vertical polarities (same as struct
421 * v4l2_bt_timings polarities).
422 * @interlaced - if this flag is true, it indicates interlaced format
423 * @fmt - the resulting timings.
425 * This function will attempt to detect if the given values correspond to a
426 * valid CVT format. If so, then it will return true, and fmt will be filled
427 * in with the found CVT timings.
429 bool v4l2_detect_cvt(unsigned frame_height,
432 unsigned active_width,
435 struct v4l2_dv_timings *fmt)
437 int v_fp, v_bp, h_fp, h_bp, hsync;
438 int frame_width, image_height, image_width;
439 bool reduced_blanking;
443 if (vsync < 4 || vsync > 8)
446 if (polarities == V4L2_DV_VSYNC_POS_POL)
447 reduced_blanking = false;
448 else if (polarities == V4L2_DV_HSYNC_POS_POL)
449 reduced_blanking = true;
453 if (reduced_blanking && vsync == 8)
456 if (rb_v2 && active_width == 0)
459 if (!rb_v2 && vsync > 7)
466 if (reduced_blanking) {
468 v_bp = CVT_RB_V_BPORCH;
469 v_fp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
470 v_fp -= vsync + v_bp;
472 if (v_fp < CVT_RB_V2_MIN_V_FPORCH)
473 v_fp = CVT_RB_V2_MIN_V_FPORCH;
475 v_fp = CVT_RB_V_FPORCH;
476 v_bp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
477 v_bp -= vsync + v_fp;
479 if (v_bp < CVT_RB_MIN_V_BPORCH)
480 v_bp = CVT_RB_MIN_V_BPORCH;
483 v_fp = CVT_MIN_V_PORCH_RND;
484 v_bp = (CVT_MIN_VSYNC_BP * hfreq) / 1000000 + 1 - vsync;
486 if (v_bp < CVT_MIN_V_BPORCH)
487 v_bp = CVT_MIN_V_BPORCH;
491 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
493 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
495 if (image_height < 0)
498 /* Aspect ratio based on vsync */
501 image_width = (image_height * 4) / 3;
504 image_width = (image_height * 16) / 9;
507 image_width = (image_height * 16) / 10;
511 if (image_height == 1024)
512 image_width = (image_height * 5) / 4;
513 else if (image_height == 768)
514 image_width = (image_height * 15) / 9;
519 image_width = active_width;
526 image_width = image_width & ~7;
529 if (reduced_blanking) {
533 h_blank = rb_v2 ? CVT_RB_V2_H_BLANK : CVT_RB_H_BLANK;
534 clk_gran = rb_v2 ? CVT_PXL_CLK_GRAN_RB_V2 : CVT_PXL_CLK_GRAN;
536 pix_clk = (image_width + h_blank) * hfreq;
537 pix_clk = (pix_clk / clk_gran) * clk_gran;
540 hsync = CVT_RB_H_SYNC;
541 h_fp = h_blank - h_bp - hsync;
543 frame_width = image_width + h_blank;
545 unsigned ideal_duty_cycle_per_myriad =
546 100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq;
549 if (ideal_duty_cycle_per_myriad < 2000)
550 ideal_duty_cycle_per_myriad = 2000;
552 h_blank = image_width * ideal_duty_cycle_per_myriad /
553 (10000 - ideal_duty_cycle_per_myriad);
554 h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN;
556 pix_clk = (image_width + h_blank) * hfreq;
557 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
560 frame_width = image_width + h_blank;
562 hsync = frame_width * CVT_HSYNC_PERCENT / 100;
563 hsync = (hsync / CVT_CELL_GRAN) * CVT_CELL_GRAN;
564 h_fp = h_blank - hsync - h_bp;
567 fmt->type = V4L2_DV_BT_656_1120;
568 fmt->bt.polarities = polarities;
569 fmt->bt.width = image_width;
570 fmt->bt.height = image_height;
571 fmt->bt.hfrontporch = h_fp;
572 fmt->bt.vfrontporch = v_fp;
573 fmt->bt.hsync = hsync;
574 fmt->bt.vsync = vsync;
575 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
578 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
579 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
581 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
583 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
584 2 * vsync - fmt->bt.vbackporch;
585 fmt->bt.il_vfrontporch = v_fp;
586 fmt->bt.il_vsync = vsync;
587 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
588 fmt->bt.interlaced = V4L2_DV_INTERLACED;
591 fmt->bt.pixelclock = pix_clk;
592 fmt->bt.standards = V4L2_DV_BT_STD_CVT;
594 if (reduced_blanking)
595 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
599 EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
603 * Based on Generalized Timing Formula Standard
604 * Version 1.1 September 2, 1999
607 #define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */
609 #define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
610 #define GTF_V_FP 1 /* vertical front porch (lines) */
611 #define GTF_CELL_GRAN 8 /* character cell granularity */
614 #define GTF_D_M 600 /* blanking formula gradient */
615 #define GTF_D_C 40 /* blanking formula offset */
616 #define GTF_D_K 128 /* blanking formula scaling factor */
617 #define GTF_D_J 20 /* blanking formula scaling factor */
618 #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
619 #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
622 #define GTF_S_M 3600 /* blanking formula gradient */
623 #define GTF_S_C 40 /* blanking formula offset */
624 #define GTF_S_K 128 /* blanking formula scaling factor */
625 #define GTF_S_J 35 /* blanking formula scaling factor */
626 #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
627 #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
629 /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
630 * @frame_height - the total height of the frame (including blanking) in lines.
631 * @hfreq - the horizontal frequency in Hz.
632 * @vsync - the height of the vertical sync in lines.
633 * @polarities - the horizontal and vertical polarities (same as struct
634 * v4l2_bt_timings polarities).
635 * @interlaced - if this flag is true, it indicates interlaced format
636 * @aspect - preferred aspect ratio. GTF has no method of determining the
637 * aspect ratio in order to derive the image width from the
638 * image height, so it has to be passed explicitly. Usually
639 * the native screen aspect ratio is used for this. If it
640 * is not filled in correctly, then 16:9 will be assumed.
641 * @fmt - the resulting timings.
643 * This function will attempt to detect if the given values correspond to a
644 * valid GTF format. If so, then it will return true, and fmt will be filled
645 * in with the found GTF timings.
647 bool v4l2_detect_gtf(unsigned frame_height,
652 struct v4l2_fract aspect,
653 struct v4l2_dv_timings *fmt)
656 int v_fp, v_bp, h_fp, hsync;
657 int frame_width, image_height, image_width;
664 if (polarities == V4L2_DV_VSYNC_POS_POL)
666 else if (polarities == V4L2_DV_HSYNC_POS_POL)
676 v_bp = (GTF_MIN_VSYNC_BP * hfreq + 500000) / 1000000 - vsync;
678 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
680 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
682 if (image_height < 0)
685 if (aspect.numerator == 0 || aspect.denominator == 0) {
686 aspect.numerator = 16;
687 aspect.denominator = 9;
689 image_width = ((image_height * aspect.numerator) / aspect.denominator);
690 image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1);
697 num = ((image_width * GTF_D_C_PRIME * (u64)hfreq) -
698 ((u64)image_width * GTF_D_M_PRIME * 1000));
699 den = (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) *
701 h_blank = div_u64((num + (den >> 1)), den);
702 h_blank *= (2 * GTF_CELL_GRAN);
707 num = ((image_width * GTF_S_C_PRIME * (u64)hfreq) -
708 ((u64)image_width * GTF_S_M_PRIME * 1000));
709 den = (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) *
711 h_blank = div_u64((num + (den >> 1)), den);
712 h_blank *= (2 * GTF_CELL_GRAN);
715 frame_width = image_width + h_blank;
717 pix_clk = (image_width + h_blank) * hfreq;
718 pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
720 hsync = (frame_width * 8 + 50) / 100;
721 hsync = ((hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN) * GTF_CELL_GRAN;
723 h_fp = h_blank / 2 - hsync;
725 fmt->type = V4L2_DV_BT_656_1120;
726 fmt->bt.polarities = polarities;
727 fmt->bt.width = image_width;
728 fmt->bt.height = image_height;
729 fmt->bt.hfrontporch = h_fp;
730 fmt->bt.vfrontporch = v_fp;
731 fmt->bt.hsync = hsync;
732 fmt->bt.vsync = vsync;
733 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
736 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
737 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
739 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
741 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
742 2 * vsync - fmt->bt.vbackporch;
743 fmt->bt.il_vfrontporch = v_fp;
744 fmt->bt.il_vsync = vsync;
745 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
746 fmt->bt.interlaced = V4L2_DV_INTERLACED;
749 fmt->bt.pixelclock = pix_clk;
750 fmt->bt.standards = V4L2_DV_BT_STD_GTF;
753 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
757 EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
759 /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
760 * 0x15 and 0x16 from the EDID.
761 * @hor_landscape - byte 0x15 from the EDID.
762 * @vert_portrait - byte 0x16 from the EDID.
764 * Determines the aspect ratio from the EDID.
765 * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
766 * "Horizontal and Vertical Screen Size or Aspect Ratio"
768 struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
770 struct v4l2_fract aspect = { 16, 9 };
773 /* Nothing filled in, fallback to 16:9 */
774 if (!hor_landscape && !vert_portrait)
776 /* Both filled in, so they are interpreted as the screen size in cm */
777 if (hor_landscape && vert_portrait) {
778 aspect.numerator = hor_landscape;
779 aspect.denominator = vert_portrait;
782 /* Only one is filled in, so interpret them as a ratio:
784 ratio = hor_landscape | vert_portrait;
785 /* Change some rounded values into the exact aspect ratio */
787 aspect.numerator = 16;
788 aspect.denominator = 9;
789 } else if (ratio == 34) {
790 aspect.numerator = 4;
791 aspect.denominator = 3;
792 } else if (ratio == 68) {
793 aspect.numerator = 15;
794 aspect.denominator = 9;
796 aspect.numerator = hor_landscape + 99;
797 aspect.denominator = 100;
801 /* The aspect ratio is for portrait, so swap numerator and denominator */
802 swap(aspect.denominator, aspect.numerator);
805 EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);
807 /** v4l2_hdmi_rx_colorimetry - determine HDMI colorimetry information
808 * based on various InfoFrames.
809 * @avi: the AVI InfoFrame
810 * @hdmi: the HDMI Vendor InfoFrame, may be NULL
811 * @height: the frame height
813 * Determines the HDMI colorimetry information, i.e. how the HDMI
814 * pixel color data should be interpreted.
816 * Note that some of the newer features (DCI-P3, HDR) are not yet
817 * implemented: the hdmi.h header needs to be updated to the HDMI 2.0
818 * and CTA-861-G standards.
820 struct v4l2_hdmi_colorimetry
821 v4l2_hdmi_rx_colorimetry(const struct hdmi_avi_infoframe *avi,
822 const struct hdmi_vendor_infoframe *hdmi,
825 struct v4l2_hdmi_colorimetry c = {
826 V4L2_COLORSPACE_SRGB,
827 V4L2_YCBCR_ENC_DEFAULT,
828 V4L2_QUANTIZATION_FULL_RANGE,
831 bool is_ce = avi->video_code || (hdmi && hdmi->vic);
832 bool is_sdtv = height <= 576;
833 bool default_is_lim_range_rgb = avi->video_code > 1;
835 switch (avi->colorspace) {
836 case HDMI_COLORSPACE_RGB:
837 /* RGB pixel encoding */
838 switch (avi->colorimetry) {
839 case HDMI_COLORIMETRY_EXTENDED:
840 switch (avi->extended_colorimetry) {
841 case HDMI_EXTENDED_COLORIMETRY_OPRGB:
842 c.colorspace = V4L2_COLORSPACE_OPRGB;
843 c.xfer_func = V4L2_XFER_FUNC_OPRGB;
845 case HDMI_EXTENDED_COLORIMETRY_BT2020:
846 c.colorspace = V4L2_COLORSPACE_BT2020;
847 c.xfer_func = V4L2_XFER_FUNC_709;
856 switch (avi->quantization_range) {
857 case HDMI_QUANTIZATION_RANGE_LIMITED:
858 c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
860 case HDMI_QUANTIZATION_RANGE_FULL:
863 if (default_is_lim_range_rgb)
864 c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
870 /* YCbCr pixel encoding */
871 c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
872 switch (avi->colorimetry) {
873 case HDMI_COLORIMETRY_NONE:
877 c.colorspace = V4L2_COLORSPACE_SMPTE170M;
878 c.ycbcr_enc = V4L2_YCBCR_ENC_601;
880 c.colorspace = V4L2_COLORSPACE_REC709;
881 c.ycbcr_enc = V4L2_YCBCR_ENC_709;
883 c.xfer_func = V4L2_XFER_FUNC_709;
885 case HDMI_COLORIMETRY_ITU_601:
886 c.colorspace = V4L2_COLORSPACE_SMPTE170M;
887 c.ycbcr_enc = V4L2_YCBCR_ENC_601;
888 c.xfer_func = V4L2_XFER_FUNC_709;
890 case HDMI_COLORIMETRY_ITU_709:
891 c.colorspace = V4L2_COLORSPACE_REC709;
892 c.ycbcr_enc = V4L2_YCBCR_ENC_709;
893 c.xfer_func = V4L2_XFER_FUNC_709;
895 case HDMI_COLORIMETRY_EXTENDED:
896 switch (avi->extended_colorimetry) {
897 case HDMI_EXTENDED_COLORIMETRY_XV_YCC_601:
898 c.colorspace = V4L2_COLORSPACE_REC709;
899 c.ycbcr_enc = V4L2_YCBCR_ENC_XV709;
900 c.xfer_func = V4L2_XFER_FUNC_709;
902 case HDMI_EXTENDED_COLORIMETRY_XV_YCC_709:
903 c.colorspace = V4L2_COLORSPACE_REC709;
904 c.ycbcr_enc = V4L2_YCBCR_ENC_XV601;
905 c.xfer_func = V4L2_XFER_FUNC_709;
907 case HDMI_EXTENDED_COLORIMETRY_S_YCC_601:
908 c.colorspace = V4L2_COLORSPACE_SRGB;
909 c.ycbcr_enc = V4L2_YCBCR_ENC_601;
910 c.xfer_func = V4L2_XFER_FUNC_SRGB;
912 case HDMI_EXTENDED_COLORIMETRY_OPYCC_601:
913 c.colorspace = V4L2_COLORSPACE_OPRGB;
914 c.ycbcr_enc = V4L2_YCBCR_ENC_601;
915 c.xfer_func = V4L2_XFER_FUNC_OPRGB;
917 case HDMI_EXTENDED_COLORIMETRY_BT2020:
918 c.colorspace = V4L2_COLORSPACE_BT2020;
919 c.ycbcr_enc = V4L2_YCBCR_ENC_BT2020;
920 c.xfer_func = V4L2_XFER_FUNC_709;
922 case HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM:
923 c.colorspace = V4L2_COLORSPACE_BT2020;
924 c.ycbcr_enc = V4L2_YCBCR_ENC_BT2020_CONST_LUM;
925 c.xfer_func = V4L2_XFER_FUNC_709;
927 default: /* fall back to ITU_709 */
928 c.colorspace = V4L2_COLORSPACE_REC709;
929 c.ycbcr_enc = V4L2_YCBCR_ENC_709;
930 c.xfer_func = V4L2_XFER_FUNC_709;
938 * YCC Quantization Range signaling is more-or-less broken,
939 * let's just ignore this.
945 EXPORT_SYMBOL_GPL(v4l2_hdmi_rx_colorimetry);
948 * v4l2_get_edid_phys_addr() - find and return the physical address
950 * @edid: pointer to the EDID data
951 * @size: size in bytes of the EDID data
952 * @offset: If not %NULL then the location of the physical address
953 * bytes in the EDID will be returned here. This is set to 0
954 * if there is no physical address found.
956 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
958 u16 v4l2_get_edid_phys_addr(const u8 *edid, unsigned int size,
959 unsigned int *offset)
961 unsigned int loc = cec_get_edid_spa_location(edid, size);
966 return CEC_PHYS_ADDR_INVALID;
967 return (edid[loc] << 8) | edid[loc + 1];
969 EXPORT_SYMBOL_GPL(v4l2_get_edid_phys_addr);
972 * v4l2_set_edid_phys_addr() - find and set the physical address
974 * @edid: pointer to the EDID data
975 * @size: size in bytes of the EDID data
976 * @phys_addr: the new physical address
978 * This function finds the location of the physical address in the EDID
979 * and fills in the given physical address and updates the checksum
980 * at the end of the EDID block. It does nothing if the EDID doesn't
981 * contain a physical address.
983 void v4l2_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr)
985 unsigned int loc = cec_get_edid_spa_location(edid, size);
991 edid[loc] = phys_addr >> 8;
992 edid[loc + 1] = phys_addr & 0xff;
995 /* update the checksum */
996 for (i = loc; i < loc + 127; i++)
1000 EXPORT_SYMBOL_GPL(v4l2_set_edid_phys_addr);
1003 * v4l2_phys_addr_for_input() - calculate the PA for an input
1005 * @phys_addr: the physical address of the parent
1006 * @input: the number of the input port, must be between 1 and 15
1008 * This function calculates a new physical address based on the input
1009 * port number. For example:
1011 * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
1013 * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
1015 * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
1017 * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
1019 * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
1021 u16 v4l2_phys_addr_for_input(u16 phys_addr, u8 input)
1023 /* Check if input is sane */
1024 if (WARN_ON(input == 0 || input > 0xf))
1025 return CEC_PHYS_ADDR_INVALID;
1030 if ((phys_addr & 0x0fff) == 0)
1031 return phys_addr | (input << 8);
1033 if ((phys_addr & 0x00ff) == 0)
1034 return phys_addr | (input << 4);
1036 if ((phys_addr & 0x000f) == 0)
1037 return phys_addr | input;
1040 * All nibbles are used so no valid physical addresses can be assigned
1043 return CEC_PHYS_ADDR_INVALID;
1045 EXPORT_SYMBOL_GPL(v4l2_phys_addr_for_input);
1048 * v4l2_phys_addr_validate() - validate a physical address from an EDID
1050 * @phys_addr: the physical address to validate
1051 * @parent: if not %NULL, then this is filled with the parents PA.
1052 * @port: if not %NULL, then this is filled with the input port.
1054 * This validates a physical address as read from an EDID. If the
1055 * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
1056 * then it will return -EINVAL.
1058 * The parent PA is passed into %parent and the input port is passed into
1059 * %port. For example:
1061 * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
1063 * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
1065 * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
1067 * PA = f.f.f.f: has parent f.f.f.f and input port 0.
1069 * Return: 0 if the PA is valid, -EINVAL if not.
1071 int v4l2_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
1076 *parent = phys_addr;
1079 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1081 for (i = 0; i < 16; i += 4)
1082 if (phys_addr & (0xf << i))
1087 *parent = phys_addr & (0xfff0 << i);
1089 *port = (phys_addr >> i) & 0xf;
1090 for (i += 4; i < 16; i += 4)
1091 if ((phys_addr & (0xf << i)) == 0)
1095 EXPORT_SYMBOL_GPL(v4l2_phys_addr_validate);