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>
19 MODULE_AUTHOR("Hans Verkuil");
20 MODULE_DESCRIPTION("V4L2 DV Timings Helper Functions");
21 MODULE_LICENSE("GPL");
23 const struct v4l2_dv_timings v4l2_dv_timings_presets
[] = {
24 V4L2_DV_BT_CEA_640X480P59_94
,
25 V4L2_DV_BT_CEA_720X480I59_94
,
26 V4L2_DV_BT_CEA_720X480P59_94
,
27 V4L2_DV_BT_CEA_720X576I50
,
28 V4L2_DV_BT_CEA_720X576P50
,
29 V4L2_DV_BT_CEA_1280X720P24
,
30 V4L2_DV_BT_CEA_1280X720P25
,
31 V4L2_DV_BT_CEA_1280X720P30
,
32 V4L2_DV_BT_CEA_1280X720P50
,
33 V4L2_DV_BT_CEA_1280X720P60
,
34 V4L2_DV_BT_CEA_1920X1080P24
,
35 V4L2_DV_BT_CEA_1920X1080P25
,
36 V4L2_DV_BT_CEA_1920X1080P30
,
37 V4L2_DV_BT_CEA_1920X1080I50
,
38 V4L2_DV_BT_CEA_1920X1080P50
,
39 V4L2_DV_BT_CEA_1920X1080I60
,
40 V4L2_DV_BT_CEA_1920X1080P60
,
41 V4L2_DV_BT_DMT_640X350P85
,
42 V4L2_DV_BT_DMT_640X400P85
,
43 V4L2_DV_BT_DMT_720X400P85
,
44 V4L2_DV_BT_DMT_640X480P72
,
45 V4L2_DV_BT_DMT_640X480P75
,
46 V4L2_DV_BT_DMT_640X480P85
,
47 V4L2_DV_BT_DMT_800X600P56
,
48 V4L2_DV_BT_DMT_800X600P60
,
49 V4L2_DV_BT_DMT_800X600P72
,
50 V4L2_DV_BT_DMT_800X600P75
,
51 V4L2_DV_BT_DMT_800X600P85
,
52 V4L2_DV_BT_DMT_800X600P120_RB
,
53 V4L2_DV_BT_DMT_848X480P60
,
54 V4L2_DV_BT_DMT_1024X768I43
,
55 V4L2_DV_BT_DMT_1024X768P60
,
56 V4L2_DV_BT_DMT_1024X768P70
,
57 V4L2_DV_BT_DMT_1024X768P75
,
58 V4L2_DV_BT_DMT_1024X768P85
,
59 V4L2_DV_BT_DMT_1024X768P120_RB
,
60 V4L2_DV_BT_DMT_1152X864P75
,
61 V4L2_DV_BT_DMT_1280X768P60_RB
,
62 V4L2_DV_BT_DMT_1280X768P60
,
63 V4L2_DV_BT_DMT_1280X768P75
,
64 V4L2_DV_BT_DMT_1280X768P85
,
65 V4L2_DV_BT_DMT_1280X768P120_RB
,
66 V4L2_DV_BT_DMT_1280X800P60_RB
,
67 V4L2_DV_BT_DMT_1280X800P60
,
68 V4L2_DV_BT_DMT_1280X800P75
,
69 V4L2_DV_BT_DMT_1280X800P85
,
70 V4L2_DV_BT_DMT_1280X800P120_RB
,
71 V4L2_DV_BT_DMT_1280X960P60
,
72 V4L2_DV_BT_DMT_1280X960P85
,
73 V4L2_DV_BT_DMT_1280X960P120_RB
,
74 V4L2_DV_BT_DMT_1280X1024P60
,
75 V4L2_DV_BT_DMT_1280X1024P75
,
76 V4L2_DV_BT_DMT_1280X1024P85
,
77 V4L2_DV_BT_DMT_1280X1024P120_RB
,
78 V4L2_DV_BT_DMT_1360X768P60
,
79 V4L2_DV_BT_DMT_1360X768P120_RB
,
80 V4L2_DV_BT_DMT_1366X768P60
,
81 V4L2_DV_BT_DMT_1366X768P60_RB
,
82 V4L2_DV_BT_DMT_1400X1050P60_RB
,
83 V4L2_DV_BT_DMT_1400X1050P60
,
84 V4L2_DV_BT_DMT_1400X1050P75
,
85 V4L2_DV_BT_DMT_1400X1050P85
,
86 V4L2_DV_BT_DMT_1400X1050P120_RB
,
87 V4L2_DV_BT_DMT_1440X900P60_RB
,
88 V4L2_DV_BT_DMT_1440X900P60
,
89 V4L2_DV_BT_DMT_1440X900P75
,
90 V4L2_DV_BT_DMT_1440X900P85
,
91 V4L2_DV_BT_DMT_1440X900P120_RB
,
92 V4L2_DV_BT_DMT_1600X900P60_RB
,
93 V4L2_DV_BT_DMT_1600X1200P60
,
94 V4L2_DV_BT_DMT_1600X1200P65
,
95 V4L2_DV_BT_DMT_1600X1200P70
,
96 V4L2_DV_BT_DMT_1600X1200P75
,
97 V4L2_DV_BT_DMT_1600X1200P85
,
98 V4L2_DV_BT_DMT_1600X1200P120_RB
,
99 V4L2_DV_BT_DMT_1680X1050P60_RB
,
100 V4L2_DV_BT_DMT_1680X1050P60
,
101 V4L2_DV_BT_DMT_1680X1050P75
,
102 V4L2_DV_BT_DMT_1680X1050P85
,
103 V4L2_DV_BT_DMT_1680X1050P120_RB
,
104 V4L2_DV_BT_DMT_1792X1344P60
,
105 V4L2_DV_BT_DMT_1792X1344P75
,
106 V4L2_DV_BT_DMT_1792X1344P120_RB
,
107 V4L2_DV_BT_DMT_1856X1392P60
,
108 V4L2_DV_BT_DMT_1856X1392P75
,
109 V4L2_DV_BT_DMT_1856X1392P120_RB
,
110 V4L2_DV_BT_DMT_1920X1200P60_RB
,
111 V4L2_DV_BT_DMT_1920X1200P60
,
112 V4L2_DV_BT_DMT_1920X1200P75
,
113 V4L2_DV_BT_DMT_1920X1200P85
,
114 V4L2_DV_BT_DMT_1920X1200P120_RB
,
115 V4L2_DV_BT_DMT_1920X1440P60
,
116 V4L2_DV_BT_DMT_1920X1440P75
,
117 V4L2_DV_BT_DMT_1920X1440P120_RB
,
118 V4L2_DV_BT_DMT_2048X1152P60_RB
,
119 V4L2_DV_BT_DMT_2560X1600P60_RB
,
120 V4L2_DV_BT_DMT_2560X1600P60
,
121 V4L2_DV_BT_DMT_2560X1600P75
,
122 V4L2_DV_BT_DMT_2560X1600P85
,
123 V4L2_DV_BT_DMT_2560X1600P120_RB
,
124 V4L2_DV_BT_CEA_3840X2160P24
,
125 V4L2_DV_BT_CEA_3840X2160P25
,
126 V4L2_DV_BT_CEA_3840X2160P30
,
127 V4L2_DV_BT_CEA_3840X2160P50
,
128 V4L2_DV_BT_CEA_3840X2160P60
,
129 V4L2_DV_BT_CEA_4096X2160P24
,
130 V4L2_DV_BT_CEA_4096X2160P25
,
131 V4L2_DV_BT_CEA_4096X2160P30
,
132 V4L2_DV_BT_CEA_4096X2160P50
,
133 V4L2_DV_BT_DMT_4096X2160P59_94_RB
,
134 V4L2_DV_BT_CEA_4096X2160P60
,
137 EXPORT_SYMBOL_GPL(v4l2_dv_timings_presets
);
139 bool v4l2_valid_dv_timings(const struct v4l2_dv_timings
*t
,
140 const struct v4l2_dv_timings_cap
*dvcap
,
141 v4l2_check_dv_timings_fnc fnc
,
144 const struct v4l2_bt_timings
*bt
= &t
->bt
;
145 const struct v4l2_bt_timings_cap
*cap
= &dvcap
->bt
;
146 u32 caps
= cap
->capabilities
;
148 if (t
->type
!= V4L2_DV_BT_656_1120
)
150 if (t
->type
!= dvcap
->type
||
151 bt
->height
< cap
->min_height
||
152 bt
->height
> cap
->max_height
||
153 bt
->width
< cap
->min_width
||
154 bt
->width
> cap
->max_width
||
155 bt
->pixelclock
< cap
->min_pixelclock
||
156 bt
->pixelclock
> cap
->max_pixelclock
||
157 (!(caps
& V4L2_DV_BT_CAP_CUSTOM
) &&
158 cap
->standards
&& bt
->standards
&&
159 !(bt
->standards
& cap
->standards
)) ||
160 (bt
->interlaced
&& !(caps
& V4L2_DV_BT_CAP_INTERLACED
)) ||
161 (!bt
->interlaced
&& !(caps
& V4L2_DV_BT_CAP_PROGRESSIVE
)))
163 return fnc
== NULL
|| fnc(t
, fnc_handle
);
165 EXPORT_SYMBOL_GPL(v4l2_valid_dv_timings
);
167 int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings
*t
,
168 const struct v4l2_dv_timings_cap
*cap
,
169 v4l2_check_dv_timings_fnc fnc
,
174 memset(t
->reserved
, 0, sizeof(t
->reserved
));
175 for (i
= idx
= 0; v4l2_dv_timings_presets
[i
].bt
.width
; i
++) {
176 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets
+ i
, cap
,
179 t
->timings
= v4l2_dv_timings_presets
[i
];
185 EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap
);
187 bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings
*t
,
188 const struct v4l2_dv_timings_cap
*cap
,
189 unsigned pclock_delta
,
190 v4l2_check_dv_timings_fnc fnc
,
195 if (!v4l2_valid_dv_timings(t
, cap
, fnc
, fnc_handle
))
198 for (i
= 0; i
< v4l2_dv_timings_presets
[i
].bt
.width
; i
++) {
199 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets
+ i
, cap
,
201 v4l2_match_dv_timings(t
, v4l2_dv_timings_presets
+ i
,
202 pclock_delta
, false)) {
203 u32 flags
= t
->bt
.flags
& V4L2_DV_FL_REDUCED_FPS
;
205 *t
= v4l2_dv_timings_presets
[i
];
206 if (can_reduce_fps(&t
->bt
))
207 t
->bt
.flags
|= flags
;
214 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap
);
216 bool v4l2_find_dv_timings_cea861_vic(struct v4l2_dv_timings
*t
, u8 vic
)
220 for (i
= 0; i
< v4l2_dv_timings_presets
[i
].bt
.width
; i
++) {
221 const struct v4l2_bt_timings
*bt
=
222 &v4l2_dv_timings_presets
[i
].bt
;
224 if ((bt
->flags
& V4L2_DV_FL_HAS_CEA861_VIC
) &&
225 bt
->cea861_vic
== vic
) {
226 *t
= v4l2_dv_timings_presets
[i
];
232 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cea861_vic
);
235 * v4l2_match_dv_timings - check if two timings match
236 * @t1: compare this v4l2_dv_timings struct...
237 * @t2: with this struct.
238 * @pclock_delta: the allowed pixelclock deviation.
239 * @match_reduced_fps: if true, then fail if V4L2_DV_FL_REDUCED_FPS does not
242 * Compare t1 with t2 with a given margin of error for the pixelclock.
244 bool v4l2_match_dv_timings(const struct v4l2_dv_timings
*t1
,
245 const struct v4l2_dv_timings
*t2
,
246 unsigned pclock_delta
, bool match_reduced_fps
)
248 if (t1
->type
!= t2
->type
|| t1
->type
!= V4L2_DV_BT_656_1120
)
250 if (t1
->bt
.width
== t2
->bt
.width
&&
251 t1
->bt
.height
== t2
->bt
.height
&&
252 t1
->bt
.interlaced
== t2
->bt
.interlaced
&&
253 t1
->bt
.polarities
== t2
->bt
.polarities
&&
254 t1
->bt
.pixelclock
>= t2
->bt
.pixelclock
- pclock_delta
&&
255 t1
->bt
.pixelclock
<= t2
->bt
.pixelclock
+ pclock_delta
&&
256 t1
->bt
.hfrontporch
== t2
->bt
.hfrontporch
&&
257 t1
->bt
.hsync
== t2
->bt
.hsync
&&
258 t1
->bt
.hbackporch
== t2
->bt
.hbackporch
&&
259 t1
->bt
.vfrontporch
== t2
->bt
.vfrontporch
&&
260 t1
->bt
.vsync
== t2
->bt
.vsync
&&
261 t1
->bt
.vbackporch
== t2
->bt
.vbackporch
&&
262 (!match_reduced_fps
||
263 (t1
->bt
.flags
& V4L2_DV_FL_REDUCED_FPS
) ==
264 (t2
->bt
.flags
& V4L2_DV_FL_REDUCED_FPS
)) &&
265 (!t1
->bt
.interlaced
||
266 (t1
->bt
.il_vfrontporch
== t2
->bt
.il_vfrontporch
&&
267 t1
->bt
.il_vsync
== t2
->bt
.il_vsync
&&
268 t1
->bt
.il_vbackporch
== t2
->bt
.il_vbackporch
)))
272 EXPORT_SYMBOL_GPL(v4l2_match_dv_timings
);
274 void v4l2_print_dv_timings(const char *dev_prefix
, const char *prefix
,
275 const struct v4l2_dv_timings
*t
, bool detailed
)
277 const struct v4l2_bt_timings
*bt
= &t
->bt
;
281 if (t
->type
!= V4L2_DV_BT_656_1120
)
284 htot
= V4L2_DV_BT_FRAME_WIDTH(bt
);
285 vtot
= V4L2_DV_BT_FRAME_HEIGHT(bt
);
289 fps
= (htot
* vtot
) > 0 ? div_u64((100 * (u64
)bt
->pixelclock
),
295 pr_info("%s: %s%ux%u%s%u.%u (%ux%u)\n", dev_prefix
, prefix
,
296 bt
->width
, bt
->height
, bt
->interlaced
? "i" : "p",
297 fps
/ 100, fps
% 100, htot
, vtot
);
302 pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n",
303 dev_prefix
, bt
->hfrontporch
,
304 (bt
->polarities
& V4L2_DV_HSYNC_POS_POL
) ? "+" : "-",
305 bt
->hsync
, bt
->hbackporch
);
306 pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n",
307 dev_prefix
, bt
->vfrontporch
,
308 (bt
->polarities
& V4L2_DV_VSYNC_POS_POL
) ? "+" : "-",
309 bt
->vsync
, bt
->vbackporch
);
311 pr_info("%s: vertical bottom field: fp = %u, %ssync = %u, bp = %u\n",
312 dev_prefix
, bt
->il_vfrontporch
,
313 (bt
->polarities
& V4L2_DV_VSYNC_POS_POL
) ? "+" : "-",
314 bt
->il_vsync
, bt
->il_vbackporch
);
315 pr_info("%s: pixelclock: %llu\n", dev_prefix
, bt
->pixelclock
);
316 pr_info("%s: flags (0x%x):%s%s%s%s%s%s%s%s%s%s\n",
317 dev_prefix
, bt
->flags
,
318 (bt
->flags
& V4L2_DV_FL_REDUCED_BLANKING
) ?
319 " REDUCED_BLANKING" : "",
320 ((bt
->flags
& V4L2_DV_FL_REDUCED_BLANKING
) &&
321 bt
->vsync
== 8) ? " (V2)" : "",
322 (bt
->flags
& V4L2_DV_FL_CAN_REDUCE_FPS
) ?
323 " CAN_REDUCE_FPS" : "",
324 (bt
->flags
& V4L2_DV_FL_REDUCED_FPS
) ?
326 (bt
->flags
& V4L2_DV_FL_HALF_LINE
) ?
328 (bt
->flags
& V4L2_DV_FL_IS_CE_VIDEO
) ?
330 (bt
->flags
& V4L2_DV_FL_FIRST_FIELD_EXTRA_LINE
) ?
331 " FIRST_FIELD_EXTRA_LINE" : "",
332 (bt
->flags
& V4L2_DV_FL_HAS_PICTURE_ASPECT
) ?
333 " HAS_PICTURE_ASPECT" : "",
334 (bt
->flags
& V4L2_DV_FL_HAS_CEA861_VIC
) ?
335 " HAS_CEA861_VIC" : "",
336 (bt
->flags
& V4L2_DV_FL_HAS_HDMI_VIC
) ?
337 " HAS_HDMI_VIC" : "");
338 pr_info("%s: standards (0x%x):%s%s%s%s%s\n", dev_prefix
, bt
->standards
,
339 (bt
->standards
& V4L2_DV_BT_STD_CEA861
) ? " CEA" : "",
340 (bt
->standards
& V4L2_DV_BT_STD_DMT
) ? " DMT" : "",
341 (bt
->standards
& V4L2_DV_BT_STD_CVT
) ? " CVT" : "",
342 (bt
->standards
& V4L2_DV_BT_STD_GTF
) ? " GTF" : "",
343 (bt
->standards
& V4L2_DV_BT_STD_SDI
) ? " SDI" : "");
344 if (bt
->flags
& V4L2_DV_FL_HAS_PICTURE_ASPECT
)
345 pr_info("%s: picture aspect (hor:vert): %u:%u\n", dev_prefix
,
346 bt
->picture_aspect
.numerator
,
347 bt
->picture_aspect
.denominator
);
348 if (bt
->flags
& V4L2_DV_FL_HAS_CEA861_VIC
)
349 pr_info("%s: CEA-861 VIC: %u\n", dev_prefix
, bt
->cea861_vic
);
350 if (bt
->flags
& V4L2_DV_FL_HAS_HDMI_VIC
)
351 pr_info("%s: HDMI VIC: %u\n", dev_prefix
, bt
->hdmi_vic
);
353 EXPORT_SYMBOL_GPL(v4l2_print_dv_timings
);
355 struct v4l2_fract
v4l2_dv_timings_aspect_ratio(const struct v4l2_dv_timings
*t
)
357 struct v4l2_fract ratio
= { 1, 1 };
360 if (t
->type
!= V4L2_DV_BT_656_1120
)
362 if (!(t
->bt
.flags
& V4L2_DV_FL_HAS_PICTURE_ASPECT
))
365 ratio
.numerator
= t
->bt
.width
* t
->bt
.picture_aspect
.denominator
;
366 ratio
.denominator
= t
->bt
.height
* t
->bt
.picture_aspect
.numerator
;
368 rational_best_approximation(ratio
.numerator
, ratio
.denominator
,
369 ratio
.numerator
, ratio
.denominator
, &n
, &d
);
371 ratio
.denominator
= d
;
374 EXPORT_SYMBOL_GPL(v4l2_dv_timings_aspect_ratio
);
378 * Based on Coordinated Video Timings Standard
379 * version 1.1 September 10, 2003
382 #define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */
383 #define CVT_PXL_CLK_GRAN_RB_V2 1000 /* granularity for reduced blanking v2*/
385 /* Normal blanking */
386 #define CVT_MIN_V_BPORCH 7 /* lines */
387 #define CVT_MIN_V_PORCH_RND 3 /* lines */
388 #define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
389 #define CVT_HSYNC_PERCENT 8 /* nominal hsync as percentage of line */
391 /* Normal blanking for CVT uses GTF to calculate horizontal blanking */
392 #define CVT_CELL_GRAN 8 /* character cell granularity */
393 #define CVT_M 600 /* blanking formula gradient */
394 #define CVT_C 40 /* blanking formula offset */
395 #define CVT_K 128 /* blanking formula scaling factor */
396 #define CVT_J 20 /* blanking formula scaling factor */
397 #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
398 #define CVT_M_PRIME (CVT_K * CVT_M / 256)
400 /* Reduced Blanking */
401 #define CVT_RB_MIN_V_BPORCH 7 /* lines */
402 #define CVT_RB_V_FPORCH 3 /* lines */
403 #define CVT_RB_MIN_V_BLANK 460 /* us */
404 #define CVT_RB_H_SYNC 32 /* pixels */
405 #define CVT_RB_H_BLANK 160 /* pixels */
406 /* Reduce blanking Version 2 */
407 #define CVT_RB_V2_H_BLANK 80 /* pixels */
408 #define CVT_RB_MIN_V_FPORCH 3 /* lines */
409 #define CVT_RB_V2_MIN_V_FPORCH 1 /* lines */
410 #define CVT_RB_V_BPORCH 6 /* lines */
412 /** v4l2_detect_cvt - detect if the given timings follow the CVT standard
413 * @frame_height - the total height of the frame (including blanking) in lines.
414 * @hfreq - the horizontal frequency in Hz.
415 * @vsync - the height of the vertical sync in lines.
416 * @active_width - active width of image (does not include blanking). This
417 * information is needed only in case of version 2 of reduced blanking.
418 * In other cases, this parameter does not have any effect on timings.
419 * @polarities - the horizontal and vertical polarities (same as struct
420 * v4l2_bt_timings polarities).
421 * @interlaced - if this flag is true, it indicates interlaced format
422 * @fmt - the resulting timings.
424 * This function will attempt to detect if the given values correspond to a
425 * valid CVT format. If so, then it will return true, and fmt will be filled
426 * in with the found CVT timings.
428 bool v4l2_detect_cvt(unsigned frame_height
,
431 unsigned active_width
,
434 struct v4l2_dv_timings
*fmt
)
436 int v_fp
, v_bp
, h_fp
, h_bp
, hsync
;
437 int frame_width
, image_height
, image_width
;
438 bool reduced_blanking
;
442 if (vsync
< 4 || vsync
> 8)
445 if (polarities
== V4L2_DV_VSYNC_POS_POL
)
446 reduced_blanking
= false;
447 else if (polarities
== V4L2_DV_HSYNC_POS_POL
)
448 reduced_blanking
= true;
452 if (reduced_blanking
&& vsync
== 8)
455 if (rb_v2
&& active_width
== 0)
458 if (!rb_v2
&& vsync
> 7)
465 if (reduced_blanking
) {
467 v_bp
= CVT_RB_V_BPORCH
;
468 v_fp
= (CVT_RB_MIN_V_BLANK
* hfreq
) / 1000000 + 1;
469 v_fp
-= vsync
+ v_bp
;
471 if (v_fp
< CVT_RB_V2_MIN_V_FPORCH
)
472 v_fp
= CVT_RB_V2_MIN_V_FPORCH
;
474 v_fp
= CVT_RB_V_FPORCH
;
475 v_bp
= (CVT_RB_MIN_V_BLANK
* hfreq
) / 1000000 + 1;
476 v_bp
-= vsync
+ v_fp
;
478 if (v_bp
< CVT_RB_MIN_V_BPORCH
)
479 v_bp
= CVT_RB_MIN_V_BPORCH
;
482 v_fp
= CVT_MIN_V_PORCH_RND
;
483 v_bp
= (CVT_MIN_VSYNC_BP
* hfreq
) / 1000000 + 1 - vsync
;
485 if (v_bp
< CVT_MIN_V_BPORCH
)
486 v_bp
= CVT_MIN_V_BPORCH
;
490 image_height
= (frame_height
- 2 * v_fp
- 2 * vsync
- 2 * v_bp
) & ~0x1;
492 image_height
= (frame_height
- v_fp
- vsync
- v_bp
+ 1) & ~0x1;
494 if (image_height
< 0)
497 /* Aspect ratio based on vsync */
500 image_width
= (image_height
* 4) / 3;
503 image_width
= (image_height
* 16) / 9;
506 image_width
= (image_height
* 16) / 10;
510 if (image_height
== 1024)
511 image_width
= (image_height
* 5) / 4;
512 else if (image_height
== 768)
513 image_width
= (image_height
* 15) / 9;
518 image_width
= active_width
;
525 image_width
= image_width
& ~7;
528 if (reduced_blanking
) {
532 h_blank
= rb_v2
? CVT_RB_V2_H_BLANK
: CVT_RB_H_BLANK
;
533 clk_gran
= rb_v2
? CVT_PXL_CLK_GRAN_RB_V2
: CVT_PXL_CLK_GRAN
;
535 pix_clk
= (image_width
+ h_blank
) * hfreq
;
536 pix_clk
= (pix_clk
/ clk_gran
) * clk_gran
;
539 hsync
= CVT_RB_H_SYNC
;
540 h_fp
= h_blank
- h_bp
- hsync
;
542 frame_width
= image_width
+ h_blank
;
544 unsigned ideal_duty_cycle_per_myriad
=
545 100 * CVT_C_PRIME
- (CVT_M_PRIME
* 100000) / hfreq
;
548 if (ideal_duty_cycle_per_myriad
< 2000)
549 ideal_duty_cycle_per_myriad
= 2000;
551 h_blank
= image_width
* ideal_duty_cycle_per_myriad
/
552 (10000 - ideal_duty_cycle_per_myriad
);
553 h_blank
= (h_blank
/ (2 * CVT_CELL_GRAN
)) * 2 * CVT_CELL_GRAN
;
555 pix_clk
= (image_width
+ h_blank
) * hfreq
;
556 pix_clk
= (pix_clk
/ CVT_PXL_CLK_GRAN
) * CVT_PXL_CLK_GRAN
;
559 frame_width
= image_width
+ h_blank
;
561 hsync
= frame_width
* CVT_HSYNC_PERCENT
/ 100;
562 hsync
= (hsync
/ CVT_CELL_GRAN
) * CVT_CELL_GRAN
;
563 h_fp
= h_blank
- hsync
- h_bp
;
566 fmt
->type
= V4L2_DV_BT_656_1120
;
567 fmt
->bt
.polarities
= polarities
;
568 fmt
->bt
.width
= image_width
;
569 fmt
->bt
.height
= image_height
;
570 fmt
->bt
.hfrontporch
= h_fp
;
571 fmt
->bt
.vfrontporch
= v_fp
;
572 fmt
->bt
.hsync
= hsync
;
573 fmt
->bt
.vsync
= vsync
;
574 fmt
->bt
.hbackporch
= frame_width
- image_width
- h_fp
- hsync
;
577 fmt
->bt
.vbackporch
= frame_height
- image_height
- v_fp
- vsync
;
578 fmt
->bt
.interlaced
= V4L2_DV_PROGRESSIVE
;
580 fmt
->bt
.vbackporch
= (frame_height
- image_height
- 2 * v_fp
-
582 fmt
->bt
.il_vbackporch
= frame_height
- image_height
- 2 * v_fp
-
583 2 * vsync
- fmt
->bt
.vbackporch
;
584 fmt
->bt
.il_vfrontporch
= v_fp
;
585 fmt
->bt
.il_vsync
= vsync
;
586 fmt
->bt
.flags
|= V4L2_DV_FL_HALF_LINE
;
587 fmt
->bt
.interlaced
= V4L2_DV_INTERLACED
;
590 fmt
->bt
.pixelclock
= pix_clk
;
591 fmt
->bt
.standards
= V4L2_DV_BT_STD_CVT
;
593 if (reduced_blanking
)
594 fmt
->bt
.flags
|= V4L2_DV_FL_REDUCED_BLANKING
;
598 EXPORT_SYMBOL_GPL(v4l2_detect_cvt
);
602 * Based on Generalized Timing Formula Standard
603 * Version 1.1 September 2, 1999
606 #define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */
608 #define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
609 #define GTF_V_FP 1 /* vertical front porch (lines) */
610 #define GTF_CELL_GRAN 8 /* character cell granularity */
613 #define GTF_D_M 600 /* blanking formula gradient */
614 #define GTF_D_C 40 /* blanking formula offset */
615 #define GTF_D_K 128 /* blanking formula scaling factor */
616 #define GTF_D_J 20 /* blanking formula scaling factor */
617 #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
618 #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
621 #define GTF_S_M 3600 /* blanking formula gradient */
622 #define GTF_S_C 40 /* blanking formula offset */
623 #define GTF_S_K 128 /* blanking formula scaling factor */
624 #define GTF_S_J 35 /* blanking formula scaling factor */
625 #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
626 #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
628 /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
629 * @frame_height - the total height of the frame (including blanking) in lines.
630 * @hfreq - the horizontal frequency in Hz.
631 * @vsync - the height of the vertical sync in lines.
632 * @polarities - the horizontal and vertical polarities (same as struct
633 * v4l2_bt_timings polarities).
634 * @interlaced - if this flag is true, it indicates interlaced format
635 * @aspect - preferred aspect ratio. GTF has no method of determining the
636 * aspect ratio in order to derive the image width from the
637 * image height, so it has to be passed explicitly. Usually
638 * the native screen aspect ratio is used for this. If it
639 * is not filled in correctly, then 16:9 will be assumed.
640 * @fmt - the resulting timings.
642 * This function will attempt to detect if the given values correspond to a
643 * valid GTF format. If so, then it will return true, and fmt will be filled
644 * in with the found GTF timings.
646 bool v4l2_detect_gtf(unsigned frame_height
,
651 struct v4l2_fract aspect
,
652 struct v4l2_dv_timings
*fmt
)
655 int v_fp
, v_bp
, h_fp
, hsync
;
656 int frame_width
, image_height
, image_width
;
663 if (polarities
== V4L2_DV_VSYNC_POS_POL
)
665 else if (polarities
== V4L2_DV_HSYNC_POS_POL
)
675 v_bp
= (GTF_MIN_VSYNC_BP
* hfreq
+ 500000) / 1000000 - vsync
;
677 image_height
= (frame_height
- 2 * v_fp
- 2 * vsync
- 2 * v_bp
) & ~0x1;
679 image_height
= (frame_height
- v_fp
- vsync
- v_bp
+ 1) & ~0x1;
681 if (image_height
< 0)
684 if (aspect
.numerator
== 0 || aspect
.denominator
== 0) {
685 aspect
.numerator
= 16;
686 aspect
.denominator
= 9;
688 image_width
= ((image_height
* aspect
.numerator
) / aspect
.denominator
);
689 image_width
= (image_width
+ GTF_CELL_GRAN
/2) & ~(GTF_CELL_GRAN
- 1);
696 num
= ((image_width
* GTF_D_C_PRIME
* (u64
)hfreq
) -
697 ((u64
)image_width
* GTF_D_M_PRIME
* 1000));
698 den
= (hfreq
* (100 - GTF_D_C_PRIME
) + GTF_D_M_PRIME
* 1000) *
700 h_blank
= div_u64((num
+ (den
>> 1)), den
);
701 h_blank
*= (2 * GTF_CELL_GRAN
);
706 num
= ((image_width
* GTF_S_C_PRIME
* (u64
)hfreq
) -
707 ((u64
)image_width
* GTF_S_M_PRIME
* 1000));
708 den
= (hfreq
* (100 - GTF_S_C_PRIME
) + GTF_S_M_PRIME
* 1000) *
710 h_blank
= div_u64((num
+ (den
>> 1)), den
);
711 h_blank
*= (2 * GTF_CELL_GRAN
);
714 frame_width
= image_width
+ h_blank
;
716 pix_clk
= (image_width
+ h_blank
) * hfreq
;
717 pix_clk
= pix_clk
/ GTF_PXL_CLK_GRAN
* GTF_PXL_CLK_GRAN
;
719 hsync
= (frame_width
* 8 + 50) / 100;
720 hsync
= ((hsync
+ GTF_CELL_GRAN
/ 2) / GTF_CELL_GRAN
) * GTF_CELL_GRAN
;
722 h_fp
= h_blank
/ 2 - hsync
;
724 fmt
->type
= V4L2_DV_BT_656_1120
;
725 fmt
->bt
.polarities
= polarities
;
726 fmt
->bt
.width
= image_width
;
727 fmt
->bt
.height
= image_height
;
728 fmt
->bt
.hfrontporch
= h_fp
;
729 fmt
->bt
.vfrontporch
= v_fp
;
730 fmt
->bt
.hsync
= hsync
;
731 fmt
->bt
.vsync
= vsync
;
732 fmt
->bt
.hbackporch
= frame_width
- image_width
- h_fp
- hsync
;
735 fmt
->bt
.vbackporch
= frame_height
- image_height
- v_fp
- vsync
;
736 fmt
->bt
.interlaced
= V4L2_DV_PROGRESSIVE
;
738 fmt
->bt
.vbackporch
= (frame_height
- image_height
- 2 * v_fp
-
740 fmt
->bt
.il_vbackporch
= frame_height
- image_height
- 2 * v_fp
-
741 2 * vsync
- fmt
->bt
.vbackporch
;
742 fmt
->bt
.il_vfrontporch
= v_fp
;
743 fmt
->bt
.il_vsync
= vsync
;
744 fmt
->bt
.flags
|= V4L2_DV_FL_HALF_LINE
;
745 fmt
->bt
.interlaced
= V4L2_DV_INTERLACED
;
748 fmt
->bt
.pixelclock
= pix_clk
;
749 fmt
->bt
.standards
= V4L2_DV_BT_STD_GTF
;
752 fmt
->bt
.flags
|= V4L2_DV_FL_REDUCED_BLANKING
;
756 EXPORT_SYMBOL_GPL(v4l2_detect_gtf
);
758 /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
759 * 0x15 and 0x16 from the EDID.
760 * @hor_landscape - byte 0x15 from the EDID.
761 * @vert_portrait - byte 0x16 from the EDID.
763 * Determines the aspect ratio from the EDID.
764 * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
765 * "Horizontal and Vertical Screen Size or Aspect Ratio"
767 struct v4l2_fract
v4l2_calc_aspect_ratio(u8 hor_landscape
, u8 vert_portrait
)
769 struct v4l2_fract aspect
= { 16, 9 };
772 /* Nothing filled in, fallback to 16:9 */
773 if (!hor_landscape
&& !vert_portrait
)
775 /* Both filled in, so they are interpreted as the screen size in cm */
776 if (hor_landscape
&& vert_portrait
) {
777 aspect
.numerator
= hor_landscape
;
778 aspect
.denominator
= vert_portrait
;
781 /* Only one is filled in, so interpret them as a ratio:
783 ratio
= hor_landscape
| vert_portrait
;
784 /* Change some rounded values into the exact aspect ratio */
786 aspect
.numerator
= 16;
787 aspect
.denominator
= 9;
788 } else if (ratio
== 34) {
789 aspect
.numerator
= 4;
790 aspect
.denominator
= 3;
791 } else if (ratio
== 68) {
792 aspect
.numerator
= 15;
793 aspect
.denominator
= 9;
795 aspect
.numerator
= hor_landscape
+ 99;
796 aspect
.denominator
= 100;
800 /* The aspect ratio is for portrait, so swap numerator and denominator */
801 swap(aspect
.denominator
, aspect
.numerator
);
804 EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio
);
806 /** v4l2_hdmi_rx_colorimetry - determine HDMI colorimetry information
807 * based on various InfoFrames.
808 * @avi: the AVI InfoFrame
809 * @hdmi: the HDMI Vendor InfoFrame, may be NULL
810 * @height: the frame height
812 * Determines the HDMI colorimetry information, i.e. how the HDMI
813 * pixel color data should be interpreted.
815 * Note that some of the newer features (DCI-P3, HDR) are not yet
816 * implemented: the hdmi.h header needs to be updated to the HDMI 2.0
817 * and CTA-861-G standards.
819 struct v4l2_hdmi_colorimetry
820 v4l2_hdmi_rx_colorimetry(const struct hdmi_avi_infoframe
*avi
,
821 const struct hdmi_vendor_infoframe
*hdmi
,
824 struct v4l2_hdmi_colorimetry c
= {
825 V4L2_COLORSPACE_SRGB
,
826 V4L2_YCBCR_ENC_DEFAULT
,
827 V4L2_QUANTIZATION_FULL_RANGE
,
830 bool is_ce
= avi
->video_code
|| (hdmi
&& hdmi
->vic
);
831 bool is_sdtv
= height
<= 576;
832 bool default_is_lim_range_rgb
= avi
->video_code
> 1;
834 switch (avi
->colorspace
) {
835 case HDMI_COLORSPACE_RGB
:
836 /* RGB pixel encoding */
837 switch (avi
->colorimetry
) {
838 case HDMI_COLORIMETRY_EXTENDED
:
839 switch (avi
->extended_colorimetry
) {
840 case HDMI_EXTENDED_COLORIMETRY_ADOBE_RGB
:
841 c
.colorspace
= V4L2_COLORSPACE_ADOBERGB
;
842 c
.xfer_func
= V4L2_XFER_FUNC_ADOBERGB
;
844 case HDMI_EXTENDED_COLORIMETRY_BT2020
:
845 c
.colorspace
= V4L2_COLORSPACE_BT2020
;
846 c
.xfer_func
= V4L2_XFER_FUNC_709
;
855 switch (avi
->quantization_range
) {
856 case HDMI_QUANTIZATION_RANGE_LIMITED
:
857 c
.quantization
= V4L2_QUANTIZATION_LIM_RANGE
;
859 case HDMI_QUANTIZATION_RANGE_FULL
:
862 if (default_is_lim_range_rgb
)
863 c
.quantization
= V4L2_QUANTIZATION_LIM_RANGE
;
869 /* YCbCr pixel encoding */
870 c
.quantization
= V4L2_QUANTIZATION_LIM_RANGE
;
871 switch (avi
->colorimetry
) {
872 case HDMI_COLORIMETRY_NONE
:
876 c
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
877 c
.ycbcr_enc
= V4L2_YCBCR_ENC_601
;
879 c
.colorspace
= V4L2_COLORSPACE_REC709
;
880 c
.ycbcr_enc
= V4L2_YCBCR_ENC_709
;
882 c
.xfer_func
= V4L2_XFER_FUNC_709
;
884 case HDMI_COLORIMETRY_ITU_601
:
885 c
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
886 c
.ycbcr_enc
= V4L2_YCBCR_ENC_601
;
887 c
.xfer_func
= V4L2_XFER_FUNC_709
;
889 case HDMI_COLORIMETRY_ITU_709
:
890 c
.colorspace
= V4L2_COLORSPACE_REC709
;
891 c
.ycbcr_enc
= V4L2_YCBCR_ENC_709
;
892 c
.xfer_func
= V4L2_XFER_FUNC_709
;
894 case HDMI_COLORIMETRY_EXTENDED
:
895 switch (avi
->extended_colorimetry
) {
896 case HDMI_EXTENDED_COLORIMETRY_XV_YCC_601
:
897 c
.colorspace
= V4L2_COLORSPACE_REC709
;
898 c
.ycbcr_enc
= V4L2_YCBCR_ENC_XV709
;
899 c
.xfer_func
= V4L2_XFER_FUNC_709
;
901 case HDMI_EXTENDED_COLORIMETRY_XV_YCC_709
:
902 c
.colorspace
= V4L2_COLORSPACE_REC709
;
903 c
.ycbcr_enc
= V4L2_YCBCR_ENC_XV601
;
904 c
.xfer_func
= V4L2_XFER_FUNC_709
;
906 case HDMI_EXTENDED_COLORIMETRY_S_YCC_601
:
907 c
.colorspace
= V4L2_COLORSPACE_SRGB
;
908 c
.ycbcr_enc
= V4L2_YCBCR_ENC_601
;
909 c
.xfer_func
= V4L2_XFER_FUNC_SRGB
;
911 case HDMI_EXTENDED_COLORIMETRY_ADOBE_YCC_601
:
912 c
.colorspace
= V4L2_COLORSPACE_ADOBERGB
;
913 c
.ycbcr_enc
= V4L2_YCBCR_ENC_601
;
914 c
.xfer_func
= V4L2_XFER_FUNC_ADOBERGB
;
916 case HDMI_EXTENDED_COLORIMETRY_BT2020
:
917 c
.colorspace
= V4L2_COLORSPACE_BT2020
;
918 c
.ycbcr_enc
= V4L2_YCBCR_ENC_BT2020
;
919 c
.xfer_func
= V4L2_XFER_FUNC_709
;
921 case HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM
:
922 c
.colorspace
= V4L2_COLORSPACE_BT2020
;
923 c
.ycbcr_enc
= V4L2_YCBCR_ENC_BT2020_CONST_LUM
;
924 c
.xfer_func
= V4L2_XFER_FUNC_709
;
926 default: /* fall back to ITU_709 */
927 c
.colorspace
= V4L2_COLORSPACE_REC709
;
928 c
.ycbcr_enc
= V4L2_YCBCR_ENC_709
;
929 c
.xfer_func
= V4L2_XFER_FUNC_709
;
937 * YCC Quantization Range signaling is more-or-less broken,
938 * let's just ignore this.
944 EXPORT_SYMBOL_GPL(v4l2_hdmi_rx_colorimetry
);