blob: 730a7c392c1db22ce71a24ad44389fd7b54d5442 [file] [log] [blame]
Hans Verkuilb18787e2013-07-29 08:40:55 -03001/*
2 * v4l2-dv-timings - dv-timings helper functions
3 *
4 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
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.
9 *
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
17 * SOFTWARE.
18 *
19 */
20
21#include <linux/module.h>
22#include <linux/types.h>
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/videodev2.h>
26#include <linux/v4l2-dv-timings.h>
Hans Verkuilb18787e2013-07-29 08:40:55 -030027#include <media/v4l2-dv-timings.h>
Prashant Laddhac9bc9f52015-05-05 10:16:27 -030028#include <linux/math64.h>
Hans Verkuilb18787e2013-07-29 08:40:55 -030029
Hans Verkuilc4885ad2014-03-04 04:51:50 -030030MODULE_AUTHOR("Hans Verkuil");
31MODULE_DESCRIPTION("V4L2 DV Timings Helper Functions");
32MODULE_LICENSE("GPL");
33
Hans Verkuild1c65ad2013-08-19 10:19:54 -030034const struct v4l2_dv_timings v4l2_dv_timings_presets[] = {
Hans Verkuilb18787e2013-07-29 08:40:55 -030035 V4L2_DV_BT_CEA_640X480P59_94,
36 V4L2_DV_BT_CEA_720X480I59_94,
37 V4L2_DV_BT_CEA_720X480P59_94,
38 V4L2_DV_BT_CEA_720X576I50,
39 V4L2_DV_BT_CEA_720X576P50,
40 V4L2_DV_BT_CEA_1280X720P24,
41 V4L2_DV_BT_CEA_1280X720P25,
42 V4L2_DV_BT_CEA_1280X720P30,
43 V4L2_DV_BT_CEA_1280X720P50,
44 V4L2_DV_BT_CEA_1280X720P60,
45 V4L2_DV_BT_CEA_1920X1080P24,
46 V4L2_DV_BT_CEA_1920X1080P25,
47 V4L2_DV_BT_CEA_1920X1080P30,
48 V4L2_DV_BT_CEA_1920X1080I50,
49 V4L2_DV_BT_CEA_1920X1080P50,
50 V4L2_DV_BT_CEA_1920X1080I60,
51 V4L2_DV_BT_CEA_1920X1080P60,
52 V4L2_DV_BT_DMT_640X350P85,
53 V4L2_DV_BT_DMT_640X400P85,
54 V4L2_DV_BT_DMT_720X400P85,
55 V4L2_DV_BT_DMT_640X480P72,
56 V4L2_DV_BT_DMT_640X480P75,
57 V4L2_DV_BT_DMT_640X480P85,
58 V4L2_DV_BT_DMT_800X600P56,
59 V4L2_DV_BT_DMT_800X600P60,
60 V4L2_DV_BT_DMT_800X600P72,
61 V4L2_DV_BT_DMT_800X600P75,
62 V4L2_DV_BT_DMT_800X600P85,
63 V4L2_DV_BT_DMT_800X600P120_RB,
64 V4L2_DV_BT_DMT_848X480P60,
65 V4L2_DV_BT_DMT_1024X768I43,
66 V4L2_DV_BT_DMT_1024X768P60,
67 V4L2_DV_BT_DMT_1024X768P70,
68 V4L2_DV_BT_DMT_1024X768P75,
69 V4L2_DV_BT_DMT_1024X768P85,
70 V4L2_DV_BT_DMT_1024X768P120_RB,
71 V4L2_DV_BT_DMT_1152X864P75,
72 V4L2_DV_BT_DMT_1280X768P60_RB,
73 V4L2_DV_BT_DMT_1280X768P60,
74 V4L2_DV_BT_DMT_1280X768P75,
75 V4L2_DV_BT_DMT_1280X768P85,
76 V4L2_DV_BT_DMT_1280X768P120_RB,
77 V4L2_DV_BT_DMT_1280X800P60_RB,
78 V4L2_DV_BT_DMT_1280X800P60,
79 V4L2_DV_BT_DMT_1280X800P75,
80 V4L2_DV_BT_DMT_1280X800P85,
81 V4L2_DV_BT_DMT_1280X800P120_RB,
82 V4L2_DV_BT_DMT_1280X960P60,
83 V4L2_DV_BT_DMT_1280X960P85,
84 V4L2_DV_BT_DMT_1280X960P120_RB,
85 V4L2_DV_BT_DMT_1280X1024P60,
86 V4L2_DV_BT_DMT_1280X1024P75,
87 V4L2_DV_BT_DMT_1280X1024P85,
88 V4L2_DV_BT_DMT_1280X1024P120_RB,
89 V4L2_DV_BT_DMT_1360X768P60,
90 V4L2_DV_BT_DMT_1360X768P120_RB,
91 V4L2_DV_BT_DMT_1366X768P60,
92 V4L2_DV_BT_DMT_1366X768P60_RB,
93 V4L2_DV_BT_DMT_1400X1050P60_RB,
94 V4L2_DV_BT_DMT_1400X1050P60,
95 V4L2_DV_BT_DMT_1400X1050P75,
96 V4L2_DV_BT_DMT_1400X1050P85,
97 V4L2_DV_BT_DMT_1400X1050P120_RB,
98 V4L2_DV_BT_DMT_1440X900P60_RB,
99 V4L2_DV_BT_DMT_1440X900P60,
100 V4L2_DV_BT_DMT_1440X900P75,
101 V4L2_DV_BT_DMT_1440X900P85,
102 V4L2_DV_BT_DMT_1440X900P120_RB,
103 V4L2_DV_BT_DMT_1600X900P60_RB,
104 V4L2_DV_BT_DMT_1600X1200P60,
105 V4L2_DV_BT_DMT_1600X1200P65,
106 V4L2_DV_BT_DMT_1600X1200P70,
107 V4L2_DV_BT_DMT_1600X1200P75,
108 V4L2_DV_BT_DMT_1600X1200P85,
109 V4L2_DV_BT_DMT_1600X1200P120_RB,
110 V4L2_DV_BT_DMT_1680X1050P60_RB,
111 V4L2_DV_BT_DMT_1680X1050P60,
112 V4L2_DV_BT_DMT_1680X1050P75,
113 V4L2_DV_BT_DMT_1680X1050P85,
114 V4L2_DV_BT_DMT_1680X1050P120_RB,
115 V4L2_DV_BT_DMT_1792X1344P60,
116 V4L2_DV_BT_DMT_1792X1344P75,
117 V4L2_DV_BT_DMT_1792X1344P120_RB,
118 V4L2_DV_BT_DMT_1856X1392P60,
119 V4L2_DV_BT_DMT_1856X1392P75,
120 V4L2_DV_BT_DMT_1856X1392P120_RB,
121 V4L2_DV_BT_DMT_1920X1200P60_RB,
122 V4L2_DV_BT_DMT_1920X1200P60,
123 V4L2_DV_BT_DMT_1920X1200P75,
124 V4L2_DV_BT_DMT_1920X1200P85,
125 V4L2_DV_BT_DMT_1920X1200P120_RB,
126 V4L2_DV_BT_DMT_1920X1440P60,
127 V4L2_DV_BT_DMT_1920X1440P75,
128 V4L2_DV_BT_DMT_1920X1440P120_RB,
129 V4L2_DV_BT_DMT_2048X1152P60_RB,
130 V4L2_DV_BT_DMT_2560X1600P60_RB,
131 V4L2_DV_BT_DMT_2560X1600P60,
132 V4L2_DV_BT_DMT_2560X1600P75,
133 V4L2_DV_BT_DMT_2560X1600P85,
134 V4L2_DV_BT_DMT_2560X1600P120_RB,
Hans Verkuilbc96f302014-04-08 05:01:52 -0300135 V4L2_DV_BT_CEA_3840X2160P24,
136 V4L2_DV_BT_CEA_3840X2160P25,
137 V4L2_DV_BT_CEA_3840X2160P30,
138 V4L2_DV_BT_CEA_3840X2160P50,
139 V4L2_DV_BT_CEA_3840X2160P60,
140 V4L2_DV_BT_CEA_4096X2160P24,
141 V4L2_DV_BT_CEA_4096X2160P25,
142 V4L2_DV_BT_CEA_4096X2160P30,
143 V4L2_DV_BT_CEA_4096X2160P50,
144 V4L2_DV_BT_DMT_4096X2160P59_94_RB,
145 V4L2_DV_BT_CEA_4096X2160P60,
Hans Verkuild1c65ad2013-08-19 10:19:54 -0300146 { }
Hans Verkuilb18787e2013-07-29 08:40:55 -0300147};
Hans Verkuild1c65ad2013-08-19 10:19:54 -0300148EXPORT_SYMBOL_GPL(v4l2_dv_timings_presets);
Hans Verkuilb18787e2013-07-29 08:40:55 -0300149
Hans Verkuil70b65492013-08-19 10:23:33 -0300150bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t,
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300151 const struct v4l2_dv_timings_cap *dvcap,
152 v4l2_check_dv_timings_fnc fnc,
153 void *fnc_handle)
Hans Verkuilb18787e2013-07-29 08:40:55 -0300154{
155 const struct v4l2_bt_timings *bt = &t->bt;
156 const struct v4l2_bt_timings_cap *cap = &dvcap->bt;
157 u32 caps = cap->capabilities;
158
159 if (t->type != V4L2_DV_BT_656_1120)
160 return false;
161 if (t->type != dvcap->type ||
162 bt->height < cap->min_height ||
163 bt->height > cap->max_height ||
164 bt->width < cap->min_width ||
165 bt->width > cap->max_width ||
166 bt->pixelclock < cap->min_pixelclock ||
167 bt->pixelclock > cap->max_pixelclock ||
Hans Verkuil8e20b802016-01-13 11:44:41 -0200168 (!(caps & V4L2_DV_BT_CAP_CUSTOM) &&
169 cap->standards && bt->standards &&
Hans Verkuilc1668452014-09-20 07:36:38 -0300170 !(bt->standards & cap->standards)) ||
Hans Verkuilb18787e2013-07-29 08:40:55 -0300171 (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) ||
172 (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE)))
173 return false;
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300174 return fnc == NULL || fnc(t, fnc_handle);
Hans Verkuilb18787e2013-07-29 08:40:55 -0300175}
Hans Verkuil70b65492013-08-19 10:23:33 -0300176EXPORT_SYMBOL_GPL(v4l2_valid_dv_timings);
Hans Verkuilb18787e2013-07-29 08:40:55 -0300177
178int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300179 const struct v4l2_dv_timings_cap *cap,
180 v4l2_check_dv_timings_fnc fnc,
181 void *fnc_handle)
Hans Verkuilb18787e2013-07-29 08:40:55 -0300182{
183 u32 i, idx;
184
185 memset(t->reserved, 0, sizeof(t->reserved));
Hans Verkuild1c65ad2013-08-19 10:19:54 -0300186 for (i = idx = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300187 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
188 fnc, fnc_handle) &&
Hans Verkuilb18787e2013-07-29 08:40:55 -0300189 idx++ == t->index) {
Hans Verkuild1c65ad2013-08-19 10:19:54 -0300190 t->timings = v4l2_dv_timings_presets[i];
Hans Verkuilb18787e2013-07-29 08:40:55 -0300191 return 0;
192 }
193 }
194 return -EINVAL;
195}
196EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap);
197
198bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
199 const struct v4l2_dv_timings_cap *cap,
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300200 unsigned pclock_delta,
201 v4l2_check_dv_timings_fnc fnc,
202 void *fnc_handle)
Hans Verkuilb18787e2013-07-29 08:40:55 -0300203{
204 int i;
205
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300206 if (!v4l2_valid_dv_timings(t, cap, fnc, fnc_handle))
Hans Verkuilb18787e2013-07-29 08:40:55 -0300207 return false;
208
Hans Verkuild1c65ad2013-08-19 10:19:54 -0300209 for (i = 0; i < v4l2_dv_timings_presets[i].bt.width; i++) {
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300210 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
211 fnc, fnc_handle) &&
212 v4l2_match_dv_timings(t, v4l2_dv_timings_presets + i,
Hans Verkuil85f9e062015-11-13 09:46:26 -0200213 pclock_delta, false)) {
Prashant Laddha8d7322f2015-09-22 11:27:28 -0300214 u32 flags = t->bt.flags & V4L2_DV_FL_REDUCED_FPS;
215
Hans Verkuild1c65ad2013-08-19 10:19:54 -0300216 *t = v4l2_dv_timings_presets[i];
Prashant Laddha8d7322f2015-09-22 11:27:28 -0300217 if (can_reduce_fps(&t->bt))
218 t->bt.flags |= flags;
219
Hans Verkuilb18787e2013-07-29 08:40:55 -0300220 return true;
221 }
222 }
223 return false;
224}
225EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap);
Hans Verkuil25764152013-07-29 08:40:56 -0300226
227/**
Hans Verkuilef1ed8f2013-08-15 08:28:47 -0300228 * v4l2_match_dv_timings - check if two timings match
Hans Verkuil25764152013-07-29 08:40:56 -0300229 * @t1 - compare this v4l2_dv_timings struct...
230 * @t2 - with this struct.
231 * @pclock_delta - the allowed pixelclock deviation.
Hans Verkuil85f9e062015-11-13 09:46:26 -0200232 * @match_reduced_fps - if true, then fail if V4L2_DV_FL_REDUCED_FPS does not
233 * match.
Hans Verkuil25764152013-07-29 08:40:56 -0300234 *
235 * Compare t1 with t2 with a given margin of error for the pixelclock.
236 */
Hans Verkuilef1ed8f2013-08-15 08:28:47 -0300237bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1,
238 const struct v4l2_dv_timings *t2,
Hans Verkuil85f9e062015-11-13 09:46:26 -0200239 unsigned pclock_delta, bool match_reduced_fps)
Hans Verkuil25764152013-07-29 08:40:56 -0300240{
241 if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
242 return false;
243 if (t1->bt.width == t2->bt.width &&
244 t1->bt.height == t2->bt.height &&
245 t1->bt.interlaced == t2->bt.interlaced &&
246 t1->bt.polarities == t2->bt.polarities &&
247 t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
248 t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
249 t1->bt.hfrontporch == t2->bt.hfrontporch &&
Mats Randgaard74c9c0f2015-11-12 10:34:55 -0200250 t1->bt.hsync == t2->bt.hsync &&
251 t1->bt.hbackporch == t2->bt.hbackporch &&
Hans Verkuil25764152013-07-29 08:40:56 -0300252 t1->bt.vfrontporch == t2->bt.vfrontporch &&
253 t1->bt.vsync == t2->bt.vsync &&
254 t1->bt.vbackporch == t2->bt.vbackporch &&
Hans Verkuil85f9e062015-11-13 09:46:26 -0200255 (!match_reduced_fps ||
256 (t1->bt.flags & V4L2_DV_FL_REDUCED_FPS) ==
257 (t2->bt.flags & V4L2_DV_FL_REDUCED_FPS)) &&
Hans Verkuil25764152013-07-29 08:40:56 -0300258 (!t1->bt.interlaced ||
259 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
260 t1->bt.il_vsync == t2->bt.il_vsync &&
261 t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
262 return true;
263 return false;
264}
Hans Verkuilef1ed8f2013-08-15 08:28:47 -0300265EXPORT_SYMBOL_GPL(v4l2_match_dv_timings);
Hans Verkuil25764152013-07-29 08:40:56 -0300266
Hans Verkuil0216dc22013-08-15 08:02:40 -0300267void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,
268 const struct v4l2_dv_timings *t, bool detailed)
269{
270 const struct v4l2_bt_timings *bt = &t->bt;
271 u32 htot, vtot;
Prashant Laddha227da852015-06-12 08:48:10 -0300272 u32 fps;
Hans Verkuil0216dc22013-08-15 08:02:40 -0300273
274 if (t->type != V4L2_DV_BT_656_1120)
275 return;
276
277 htot = V4L2_DV_BT_FRAME_WIDTH(bt);
278 vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
Hans Verkuil8cf68742015-06-05 05:30:02 -0300279 if (bt->interlaced)
280 vtot /= 2;
Hans Verkuil0216dc22013-08-15 08:02:40 -0300281
Prashant Laddha227da852015-06-12 08:48:10 -0300282 fps = (htot * vtot) > 0 ? div_u64((100 * (u64)bt->pixelclock),
283 (htot * vtot)) : 0;
284
Hans Verkuil0216dc22013-08-15 08:02:40 -0300285 if (prefix == NULL)
286 prefix = "";
287
Prashant Laddha227da852015-06-12 08:48:10 -0300288 pr_info("%s: %s%ux%u%s%u.%u (%ux%u)\n", dev_prefix, prefix,
Hans Verkuil0216dc22013-08-15 08:02:40 -0300289 bt->width, bt->height, bt->interlaced ? "i" : "p",
Prashant Laddha227da852015-06-12 08:48:10 -0300290 fps / 100, fps % 100, htot, vtot);
Hans Verkuil0216dc22013-08-15 08:02:40 -0300291
292 if (!detailed)
293 return;
294
295 pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n",
296 dev_prefix, bt->hfrontporch,
297 (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
298 bt->hsync, bt->hbackporch);
299 pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n",
300 dev_prefix, bt->vfrontporch,
301 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
302 bt->vsync, bt->vbackporch);
Hans Verkuil8cf68742015-06-05 05:30:02 -0300303 if (bt->interlaced)
304 pr_info("%s: vertical bottom field: fp = %u, %ssync = %u, bp = %u\n",
305 dev_prefix, bt->il_vfrontporch,
306 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
307 bt->il_vsync, bt->il_vbackporch);
Hans Verkuil0216dc22013-08-15 08:02:40 -0300308 pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock);
Charles-Antoine Couret7389e6e2016-09-15 12:29:50 -0300309 pr_info("%s: flags (0x%x):%s%s%s%s%s%s%s\n", dev_prefix, bt->flags,
Hans Verkuil0216dc22013-08-15 08:02:40 -0300310 (bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ?
311 " REDUCED_BLANKING" : "",
Hans Verkuil1b3b3842015-06-12 03:52:54 -0300312 ((bt->flags & V4L2_DV_FL_REDUCED_BLANKING) &&
313 bt->vsync == 8) ? " (V2)" : "",
Hans Verkuil0216dc22013-08-15 08:02:40 -0300314 (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ?
315 " CAN_REDUCE_FPS" : "",
316 (bt->flags & V4L2_DV_FL_REDUCED_FPS) ?
317 " REDUCED_FPS" : "",
318 (bt->flags & V4L2_DV_FL_HALF_LINE) ?
Hans Verkuil74d802d2015-04-03 06:27:02 -0300319 " HALF_LINE" : "",
320 (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) ?
Charles-Antoine Couret7389e6e2016-09-15 12:29:50 -0300321 " CE_VIDEO" : "",
322 (bt->flags & V4L2_DV_FL_FIRST_FIELD_EXTRA_LINE) ?
323 " FIRST_FIELD_EXTRA_LINE" : "");
324 pr_info("%s: standards (0x%x):%s%s%s%s%s\n", dev_prefix, bt->standards,
Hans Verkuil0216dc22013-08-15 08:02:40 -0300325 (bt->standards & V4L2_DV_BT_STD_CEA861) ? " CEA" : "",
326 (bt->standards & V4L2_DV_BT_STD_DMT) ? " DMT" : "",
327 (bt->standards & V4L2_DV_BT_STD_CVT) ? " CVT" : "",
Charles-Antoine Couret7389e6e2016-09-15 12:29:50 -0300328 (bt->standards & V4L2_DV_BT_STD_GTF) ? " GTF" : "",
329 (bt->standards & V4L2_DV_BT_STD_SDI) ? " SDI" : "");
Hans Verkuil0216dc22013-08-15 08:02:40 -0300330}
331EXPORT_SYMBOL_GPL(v4l2_print_dv_timings);
332
Hans Verkuil25764152013-07-29 08:40:56 -0300333/*
334 * CVT defines
335 * Based on Coordinated Video Timings Standard
336 * version 1.1 September 10, 2003
337 */
338
339#define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300340#define CVT_PXL_CLK_GRAN_RB_V2 1000 /* granularity for reduced blanking v2*/
Hans Verkuil25764152013-07-29 08:40:56 -0300341
342/* Normal blanking */
343#define CVT_MIN_V_BPORCH 7 /* lines */
344#define CVT_MIN_V_PORCH_RND 3 /* lines */
345#define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
Prashant Laddha9c3f2052015-04-22 14:32:37 -0300346#define CVT_HSYNC_PERCENT 8 /* nominal hsync as percentage of line */
Hans Verkuil25764152013-07-29 08:40:56 -0300347
348/* Normal blanking for CVT uses GTF to calculate horizontal blanking */
349#define CVT_CELL_GRAN 8 /* character cell granularity */
350#define CVT_M 600 /* blanking formula gradient */
351#define CVT_C 40 /* blanking formula offset */
352#define CVT_K 128 /* blanking formula scaling factor */
353#define CVT_J 20 /* blanking formula scaling factor */
354#define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
355#define CVT_M_PRIME (CVT_K * CVT_M / 256)
356
357/* Reduced Blanking */
358#define CVT_RB_MIN_V_BPORCH 7 /* lines */
359#define CVT_RB_V_FPORCH 3 /* lines */
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300360#define CVT_RB_MIN_V_BLANK 460 /* us */
Hans Verkuil25764152013-07-29 08:40:56 -0300361#define CVT_RB_H_SYNC 32 /* pixels */
Hans Verkuil25764152013-07-29 08:40:56 -0300362#define CVT_RB_H_BLANK 160 /* pixels */
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300363/* Reduce blanking Version 2 */
364#define CVT_RB_V2_H_BLANK 80 /* pixels */
365#define CVT_RB_MIN_V_FPORCH 3 /* lines */
366#define CVT_RB_V2_MIN_V_FPORCH 1 /* lines */
367#define CVT_RB_V_BPORCH 6 /* lines */
Hans Verkuil25764152013-07-29 08:40:56 -0300368
369/** v4l2_detect_cvt - detect if the given timings follow the CVT standard
370 * @frame_height - the total height of the frame (including blanking) in lines.
371 * @hfreq - the horizontal frequency in Hz.
372 * @vsync - the height of the vertical sync in lines.
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300373 * @active_width - active width of image (does not include blanking). This
374 * information is needed only in case of version 2 of reduced blanking.
375 * In other cases, this parameter does not have any effect on timings.
Hans Verkuil25764152013-07-29 08:40:56 -0300376 * @polarities - the horizontal and vertical polarities (same as struct
377 * v4l2_bt_timings polarities).
Prashant Laddha061ddda2015-05-22 02:27:34 -0300378 * @interlaced - if this flag is true, it indicates interlaced format
Hans Verkuil25764152013-07-29 08:40:56 -0300379 * @fmt - the resulting timings.
380 *
381 * This function will attempt to detect if the given values correspond to a
382 * valid CVT format. If so, then it will return true, and fmt will be filled
383 * in with the found CVT timings.
384 */
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300385bool v4l2_detect_cvt(unsigned frame_height,
386 unsigned hfreq,
387 unsigned vsync,
388 unsigned active_width,
389 u32 polarities,
390 bool interlaced,
391 struct v4l2_dv_timings *fmt)
Hans Verkuil25764152013-07-29 08:40:56 -0300392{
393 int v_fp, v_bp, h_fp, h_bp, hsync;
394 int frame_width, image_height, image_width;
395 bool reduced_blanking;
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300396 bool rb_v2 = false;
Hans Verkuil25764152013-07-29 08:40:56 -0300397 unsigned pix_clk;
398
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300399 if (vsync < 4 || vsync > 8)
Hans Verkuil25764152013-07-29 08:40:56 -0300400 return false;
401
402 if (polarities == V4L2_DV_VSYNC_POS_POL)
403 reduced_blanking = false;
404 else if (polarities == V4L2_DV_HSYNC_POS_POL)
405 reduced_blanking = true;
406 else
407 return false;
408
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300409 if (reduced_blanking && vsync == 8)
410 rb_v2 = true;
411
412 if (rb_v2 && active_width == 0)
413 return false;
414
415 if (!rb_v2 && vsync > 7)
416 return false;
417
Prashant Laddha947ed992015-04-22 14:32:36 -0300418 if (hfreq == 0)
419 return false;
420
Hans Verkuil25764152013-07-29 08:40:56 -0300421 /* Vertical */
422 if (reduced_blanking) {
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300423 if (rb_v2) {
424 v_bp = CVT_RB_V_BPORCH;
425 v_fp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
426 v_fp -= vsync + v_bp;
Hans Verkuil25764152013-07-29 08:40:56 -0300427
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300428 if (v_fp < CVT_RB_V2_MIN_V_FPORCH)
429 v_fp = CVT_RB_V2_MIN_V_FPORCH;
430 } else {
431 v_fp = CVT_RB_V_FPORCH;
432 v_bp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
433 v_bp -= vsync + v_fp;
434
435 if (v_bp < CVT_RB_MIN_V_BPORCH)
436 v_bp = CVT_RB_MIN_V_BPORCH;
437 }
Hans Verkuil25764152013-07-29 08:40:56 -0300438 } else {
439 v_fp = CVT_MIN_V_PORCH_RND;
Prashant Laddhaf6747652015-04-22 14:32:34 -0300440 v_bp = (CVT_MIN_VSYNC_BP * hfreq) / 1000000 + 1 - vsync;
Hans Verkuil25764152013-07-29 08:40:56 -0300441
442 if (v_bp < CVT_MIN_V_BPORCH)
443 v_bp = CVT_MIN_V_BPORCH;
444 }
Prashant Laddha061ddda2015-05-22 02:27:34 -0300445
446 if (interlaced)
447 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
448 else
449 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
Hans Verkuil25764152013-07-29 08:40:56 -0300450
Prashant Laddha947ed992015-04-22 14:32:36 -0300451 if (image_height < 0)
452 return false;
453
Hans Verkuil25764152013-07-29 08:40:56 -0300454 /* Aspect ratio based on vsync */
455 switch (vsync) {
456 case 4:
457 image_width = (image_height * 4) / 3;
458 break;
459 case 5:
460 image_width = (image_height * 16) / 9;
461 break;
462 case 6:
463 image_width = (image_height * 16) / 10;
464 break;
465 case 7:
466 /* special case */
467 if (image_height == 1024)
468 image_width = (image_height * 5) / 4;
469 else if (image_height == 768)
470 image_width = (image_height * 15) / 9;
471 else
472 return false;
473 break;
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300474 case 8:
475 image_width = active_width;
476 break;
Hans Verkuil25764152013-07-29 08:40:56 -0300477 default:
478 return false;
479 }
480
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300481 if (!rb_v2)
482 image_width = image_width & ~7;
Hans Verkuil25764152013-07-29 08:40:56 -0300483
484 /* Horizontal */
485 if (reduced_blanking) {
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300486 int h_blank;
487 int clk_gran;
Hans Verkuil25764152013-07-29 08:40:56 -0300488
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300489 h_blank = rb_v2 ? CVT_RB_V2_H_BLANK : CVT_RB_H_BLANK;
490 clk_gran = rb_v2 ? CVT_PXL_CLK_GRAN_RB_V2 : CVT_PXL_CLK_GRAN;
491
492 pix_clk = (image_width + h_blank) * hfreq;
493 pix_clk = (pix_clk / clk_gran) * clk_gran;
494
495 h_bp = h_blank / 2;
Hans Verkuil25764152013-07-29 08:40:56 -0300496 hsync = CVT_RB_H_SYNC;
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300497 h_fp = h_blank - h_bp - hsync;
Hans Verkuil25764152013-07-29 08:40:56 -0300498
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300499 frame_width = image_width + h_blank;
Hans Verkuil25764152013-07-29 08:40:56 -0300500 } else {
Martin Buggec3e75c72013-08-14 04:58:38 -0300501 unsigned ideal_duty_cycle_per_myriad =
502 100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq;
Hans Verkuil25764152013-07-29 08:40:56 -0300503 int h_blank;
Hans Verkuil25764152013-07-29 08:40:56 -0300504
Martin Buggec3e75c72013-08-14 04:58:38 -0300505 if (ideal_duty_cycle_per_myriad < 2000)
506 ideal_duty_cycle_per_myriad = 2000;
Hans Verkuil25764152013-07-29 08:40:56 -0300507
Martin Buggec3e75c72013-08-14 04:58:38 -0300508 h_blank = image_width * ideal_duty_cycle_per_myriad /
509 (10000 - ideal_duty_cycle_per_myriad);
510 h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN;
Hans Verkuil25764152013-07-29 08:40:56 -0300511
512 pix_clk = (image_width + h_blank) * hfreq;
513 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
514
515 h_bp = h_blank / 2;
516 frame_width = image_width + h_blank;
517
Prashant Laddha9c3f2052015-04-22 14:32:37 -0300518 hsync = frame_width * CVT_HSYNC_PERCENT / 100;
Prashant Laddhad7ed5a32015-04-22 14:32:35 -0300519 hsync = (hsync / CVT_CELL_GRAN) * CVT_CELL_GRAN;
Hans Verkuil25764152013-07-29 08:40:56 -0300520 h_fp = h_blank - hsync - h_bp;
521 }
522
Hans Verkuil074ca432013-08-19 08:38:29 -0300523 fmt->type = V4L2_DV_BT_656_1120;
Hans Verkuil25764152013-07-29 08:40:56 -0300524 fmt->bt.polarities = polarities;
525 fmt->bt.width = image_width;
526 fmt->bt.height = image_height;
527 fmt->bt.hfrontporch = h_fp;
528 fmt->bt.vfrontporch = v_fp;
529 fmt->bt.hsync = hsync;
530 fmt->bt.vsync = vsync;
531 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300532
533 if (!interlaced) {
534 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
535 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
536 } else {
537 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
538 2 * vsync) / 2;
539 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
540 2 * vsync - fmt->bt.vbackporch;
541 fmt->bt.il_vfrontporch = v_fp;
542 fmt->bt.il_vsync = vsync;
543 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
544 fmt->bt.interlaced = V4L2_DV_INTERLACED;
545 }
546
Hans Verkuil25764152013-07-29 08:40:56 -0300547 fmt->bt.pixelclock = pix_clk;
548 fmt->bt.standards = V4L2_DV_BT_STD_CVT;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300549
Hans Verkuil25764152013-07-29 08:40:56 -0300550 if (reduced_blanking)
551 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300552
Hans Verkuil25764152013-07-29 08:40:56 -0300553 return true;
554}
555EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
556
557/*
558 * GTF defines
559 * Based on Generalized Timing Formula Standard
560 * Version 1.1 September 2, 1999
561 */
562
563#define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */
564
565#define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
566#define GTF_V_FP 1 /* vertical front porch (lines) */
567#define GTF_CELL_GRAN 8 /* character cell granularity */
568
569/* Default */
570#define GTF_D_M 600 /* blanking formula gradient */
571#define GTF_D_C 40 /* blanking formula offset */
572#define GTF_D_K 128 /* blanking formula scaling factor */
573#define GTF_D_J 20 /* blanking formula scaling factor */
574#define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
575#define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
576
577/* Secondary */
578#define GTF_S_M 3600 /* blanking formula gradient */
579#define GTF_S_C 40 /* blanking formula offset */
580#define GTF_S_K 128 /* blanking formula scaling factor */
581#define GTF_S_J 35 /* blanking formula scaling factor */
582#define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
583#define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
584
585/** v4l2_detect_gtf - detect if the given timings follow the GTF standard
586 * @frame_height - the total height of the frame (including blanking) in lines.
587 * @hfreq - the horizontal frequency in Hz.
588 * @vsync - the height of the vertical sync in lines.
589 * @polarities - the horizontal and vertical polarities (same as struct
590 * v4l2_bt_timings polarities).
Prashant Laddha061ddda2015-05-22 02:27:34 -0300591 * @interlaced - if this flag is true, it indicates interlaced format
Hans Verkuil25764152013-07-29 08:40:56 -0300592 * @aspect - preferred aspect ratio. GTF has no method of determining the
593 * aspect ratio in order to derive the image width from the
594 * image height, so it has to be passed explicitly. Usually
595 * the native screen aspect ratio is used for this. If it
596 * is not filled in correctly, then 16:9 will be assumed.
597 * @fmt - the resulting timings.
598 *
599 * This function will attempt to detect if the given values correspond to a
600 * valid GTF format. If so, then it will return true, and fmt will be filled
601 * in with the found GTF timings.
602 */
603bool v4l2_detect_gtf(unsigned frame_height,
604 unsigned hfreq,
605 unsigned vsync,
606 u32 polarities,
Prashant Laddha061ddda2015-05-22 02:27:34 -0300607 bool interlaced,
Hans Verkuil25764152013-07-29 08:40:56 -0300608 struct v4l2_fract aspect,
609 struct v4l2_dv_timings *fmt)
610{
611 int pix_clk;
612 int v_fp, v_bp, h_fp, hsync;
613 int frame_width, image_height, image_width;
614 bool default_gtf;
615 int h_blank;
616
617 if (vsync != 3)
618 return false;
619
620 if (polarities == V4L2_DV_VSYNC_POS_POL)
621 default_gtf = true;
622 else if (polarities == V4L2_DV_HSYNC_POS_POL)
623 default_gtf = false;
624 else
625 return false;
626
Prashant Laddha947ed992015-04-22 14:32:36 -0300627 if (hfreq == 0)
628 return false;
629
Hans Verkuil25764152013-07-29 08:40:56 -0300630 /* Vertical */
631 v_fp = GTF_V_FP;
Prashant Laddhaf6747652015-04-22 14:32:34 -0300632 v_bp = (GTF_MIN_VSYNC_BP * hfreq + 500000) / 1000000 - vsync;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300633 if (interlaced)
634 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
635 else
636 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
Hans Verkuil25764152013-07-29 08:40:56 -0300637
Prashant Laddha947ed992015-04-22 14:32:36 -0300638 if (image_height < 0)
639 return false;
640
Hans Verkuil25764152013-07-29 08:40:56 -0300641 if (aspect.numerator == 0 || aspect.denominator == 0) {
642 aspect.numerator = 16;
643 aspect.denominator = 9;
644 }
645 image_width = ((image_height * aspect.numerator) / aspect.denominator);
Martin Bugge257cc4b2014-01-23 06:40:00 -0300646 image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1);
Hans Verkuil25764152013-07-29 08:40:56 -0300647
648 /* Horizontal */
Prashant Laddhac9bc9f52015-05-05 10:16:27 -0300649 if (default_gtf) {
650 u64 num;
651 u32 den;
652
653 num = ((image_width * GTF_D_C_PRIME * (u64)hfreq) -
654 ((u64)image_width * GTF_D_M_PRIME * 1000));
Prashant Laddhadc0cf4c2015-06-05 05:13:31 -0300655 den = (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) *
656 (2 * GTF_CELL_GRAN);
Prashant Laddhac9bc9f52015-05-05 10:16:27 -0300657 h_blank = div_u64((num + (den >> 1)), den);
Prashant Laddhadc0cf4c2015-06-05 05:13:31 -0300658 h_blank *= (2 * GTF_CELL_GRAN);
Prashant Laddhac9bc9f52015-05-05 10:16:27 -0300659 } else {
660 u64 num;
661 u32 den;
662
663 num = ((image_width * GTF_S_C_PRIME * (u64)hfreq) -
664 ((u64)image_width * GTF_S_M_PRIME * 1000));
Prashant Laddhadc0cf4c2015-06-05 05:13:31 -0300665 den = (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) *
666 (2 * GTF_CELL_GRAN);
Prashant Laddhac9bc9f52015-05-05 10:16:27 -0300667 h_blank = div_u64((num + (den >> 1)), den);
Prashant Laddhadc0cf4c2015-06-05 05:13:31 -0300668 h_blank *= (2 * GTF_CELL_GRAN);
Prashant Laddhac9bc9f52015-05-05 10:16:27 -0300669 }
Hans Verkuil25764152013-07-29 08:40:56 -0300670
Hans Verkuil25764152013-07-29 08:40:56 -0300671 frame_width = image_width + h_blank;
672
673 pix_clk = (image_width + h_blank) * hfreq;
674 pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
675
676 hsync = (frame_width * 8 + 50) / 100;
Prashant Laddhad7ed5a32015-04-22 14:32:35 -0300677 hsync = ((hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN) * GTF_CELL_GRAN;
Hans Verkuil25764152013-07-29 08:40:56 -0300678
679 h_fp = h_blank / 2 - hsync;
680
Hans Verkuil074ca432013-08-19 08:38:29 -0300681 fmt->type = V4L2_DV_BT_656_1120;
Hans Verkuil25764152013-07-29 08:40:56 -0300682 fmt->bt.polarities = polarities;
683 fmt->bt.width = image_width;
684 fmt->bt.height = image_height;
685 fmt->bt.hfrontporch = h_fp;
686 fmt->bt.vfrontporch = v_fp;
687 fmt->bt.hsync = hsync;
688 fmt->bt.vsync = vsync;
689 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300690
691 if (!interlaced) {
692 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
693 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
694 } else {
695 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
696 2 * vsync) / 2;
697 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
698 2 * vsync - fmt->bt.vbackporch;
699 fmt->bt.il_vfrontporch = v_fp;
700 fmt->bt.il_vsync = vsync;
701 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
702 fmt->bt.interlaced = V4L2_DV_INTERLACED;
703 }
704
Hans Verkuil25764152013-07-29 08:40:56 -0300705 fmt->bt.pixelclock = pix_clk;
706 fmt->bt.standards = V4L2_DV_BT_STD_GTF;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300707
Hans Verkuil25764152013-07-29 08:40:56 -0300708 if (!default_gtf)
709 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300710
Hans Verkuil25764152013-07-29 08:40:56 -0300711 return true;
712}
713EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
714
715/** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
716 * 0x15 and 0x16 from the EDID.
717 * @hor_landscape - byte 0x15 from the EDID.
718 * @vert_portrait - byte 0x16 from the EDID.
719 *
720 * Determines the aspect ratio from the EDID.
721 * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
722 * "Horizontal and Vertical Screen Size or Aspect Ratio"
723 */
724struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
725{
726 struct v4l2_fract aspect = { 16, 9 };
Hans Verkuil25764152013-07-29 08:40:56 -0300727 u8 ratio;
728
729 /* Nothing filled in, fallback to 16:9 */
730 if (!hor_landscape && !vert_portrait)
731 return aspect;
732 /* Both filled in, so they are interpreted as the screen size in cm */
733 if (hor_landscape && vert_portrait) {
734 aspect.numerator = hor_landscape;
735 aspect.denominator = vert_portrait;
736 return aspect;
737 }
738 /* Only one is filled in, so interpret them as a ratio:
739 (val + 99) / 100 */
740 ratio = hor_landscape | vert_portrait;
741 /* Change some rounded values into the exact aspect ratio */
742 if (ratio == 79) {
743 aspect.numerator = 16;
744 aspect.denominator = 9;
745 } else if (ratio == 34) {
746 aspect.numerator = 4;
Rickard Strandqvistf71920e2014-06-14 08:37:09 -0300747 aspect.denominator = 3;
Hans Verkuil25764152013-07-29 08:40:56 -0300748 } else if (ratio == 68) {
749 aspect.numerator = 15;
Rickard Strandqvistf71920e2014-06-14 08:37:09 -0300750 aspect.denominator = 9;
Hans Verkuil25764152013-07-29 08:40:56 -0300751 } else {
752 aspect.numerator = hor_landscape + 99;
753 aspect.denominator = 100;
754 }
755 if (hor_landscape)
756 return aspect;
757 /* The aspect ratio is for portrait, so swap numerator and denominator */
Fabian Frederickfd752422015-06-10 13:32:33 -0300758 swap(aspect.denominator, aspect.numerator);
Hans Verkuil25764152013-07-29 08:40:56 -0300759 return aspect;
760}
761EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);