blob: 2c7b9fdfa0af5f23f9236e71deba8d03e3b58ef1 [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 Verkuilc1668452014-09-20 07:36:38 -0300168 (cap->standards && bt->standards &&
169 !(bt->standards & cap->standards)) ||
Hans Verkuilb18787e2013-07-29 08:40:55 -0300170 (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) ||
171 (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE)))
172 return false;
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300173 return fnc == NULL || fnc(t, fnc_handle);
Hans Verkuilb18787e2013-07-29 08:40:55 -0300174}
Hans Verkuil70b65492013-08-19 10:23:33 -0300175EXPORT_SYMBOL_GPL(v4l2_valid_dv_timings);
Hans Verkuilb18787e2013-07-29 08:40:55 -0300176
177int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300178 const struct v4l2_dv_timings_cap *cap,
179 v4l2_check_dv_timings_fnc fnc,
180 void *fnc_handle)
Hans Verkuilb18787e2013-07-29 08:40:55 -0300181{
182 u32 i, idx;
183
184 memset(t->reserved, 0, sizeof(t->reserved));
Hans Verkuild1c65ad2013-08-19 10:19:54 -0300185 for (i = idx = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300186 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
187 fnc, fnc_handle) &&
Hans Verkuilb18787e2013-07-29 08:40:55 -0300188 idx++ == t->index) {
Hans Verkuild1c65ad2013-08-19 10:19:54 -0300189 t->timings = v4l2_dv_timings_presets[i];
Hans Verkuilb18787e2013-07-29 08:40:55 -0300190 return 0;
191 }
192 }
193 return -EINVAL;
194}
195EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap);
196
197bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
198 const struct v4l2_dv_timings_cap *cap,
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300199 unsigned pclock_delta,
200 v4l2_check_dv_timings_fnc fnc,
201 void *fnc_handle)
Hans Verkuilb18787e2013-07-29 08:40:55 -0300202{
203 int i;
204
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300205 if (!v4l2_valid_dv_timings(t, cap, fnc, fnc_handle))
Hans Verkuilb18787e2013-07-29 08:40:55 -0300206 return false;
207
Hans Verkuild1c65ad2013-08-19 10:19:54 -0300208 for (i = 0; i < v4l2_dv_timings_presets[i].bt.width; i++) {
Hans Verkuilb8f0fff2013-08-19 11:21:50 -0300209 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
210 fnc, fnc_handle) &&
211 v4l2_match_dv_timings(t, v4l2_dv_timings_presets + i,
212 pclock_delta)) {
Hans Verkuild1c65ad2013-08-19 10:19:54 -0300213 *t = v4l2_dv_timings_presets[i];
Hans Verkuilb18787e2013-07-29 08:40:55 -0300214 return true;
215 }
216 }
217 return false;
218}
219EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap);
Hans Verkuil25764152013-07-29 08:40:56 -0300220
221/**
Hans Verkuilef1ed8f2013-08-15 08:28:47 -0300222 * v4l2_match_dv_timings - check if two timings match
Hans Verkuil25764152013-07-29 08:40:56 -0300223 * @t1 - compare this v4l2_dv_timings struct...
224 * @t2 - with this struct.
225 * @pclock_delta - the allowed pixelclock deviation.
226 *
227 * Compare t1 with t2 with a given margin of error for the pixelclock.
228 */
Hans Verkuilef1ed8f2013-08-15 08:28:47 -0300229bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1,
230 const struct v4l2_dv_timings *t2,
231 unsigned pclock_delta)
Hans Verkuil25764152013-07-29 08:40:56 -0300232{
233 if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
234 return false;
235 if (t1->bt.width == t2->bt.width &&
236 t1->bt.height == t2->bt.height &&
237 t1->bt.interlaced == t2->bt.interlaced &&
238 t1->bt.polarities == t2->bt.polarities &&
239 t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
240 t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
241 t1->bt.hfrontporch == t2->bt.hfrontporch &&
242 t1->bt.vfrontporch == t2->bt.vfrontporch &&
243 t1->bt.vsync == t2->bt.vsync &&
244 t1->bt.vbackporch == t2->bt.vbackporch &&
245 (!t1->bt.interlaced ||
246 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
247 t1->bt.il_vsync == t2->bt.il_vsync &&
248 t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
249 return true;
250 return false;
251}
Hans Verkuilef1ed8f2013-08-15 08:28:47 -0300252EXPORT_SYMBOL_GPL(v4l2_match_dv_timings);
Hans Verkuil25764152013-07-29 08:40:56 -0300253
Hans Verkuil0216dc22013-08-15 08:02:40 -0300254void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,
255 const struct v4l2_dv_timings *t, bool detailed)
256{
257 const struct v4l2_bt_timings *bt = &t->bt;
258 u32 htot, vtot;
259
260 if (t->type != V4L2_DV_BT_656_1120)
261 return;
262
263 htot = V4L2_DV_BT_FRAME_WIDTH(bt);
264 vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
Hans Verkuil8cf68742015-06-05 05:30:02 -0300265 if (bt->interlaced)
266 vtot /= 2;
Hans Verkuil0216dc22013-08-15 08:02:40 -0300267
268 if (prefix == NULL)
269 prefix = "";
270
271 pr_info("%s: %s%ux%u%s%u (%ux%u)\n", dev_prefix, prefix,
272 bt->width, bt->height, bt->interlaced ? "i" : "p",
273 (htot * vtot) > 0 ? ((u32)bt->pixelclock / (htot * vtot)) : 0,
274 htot, vtot);
275
276 if (!detailed)
277 return;
278
279 pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n",
280 dev_prefix, bt->hfrontporch,
281 (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
282 bt->hsync, bt->hbackporch);
283 pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n",
284 dev_prefix, bt->vfrontporch,
285 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
286 bt->vsync, bt->vbackporch);
Hans Verkuil8cf68742015-06-05 05:30:02 -0300287 if (bt->interlaced)
288 pr_info("%s: vertical bottom field: fp = %u, %ssync = %u, bp = %u\n",
289 dev_prefix, bt->il_vfrontporch,
290 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
291 bt->il_vsync, bt->il_vbackporch);
Hans Verkuil0216dc22013-08-15 08:02:40 -0300292 pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock);
Hans Verkuil1b3b3842015-06-12 03:52:54 -0300293 pr_info("%s: flags (0x%x):%s%s%s%s%s%s\n", dev_prefix, bt->flags,
Hans Verkuil0216dc22013-08-15 08:02:40 -0300294 (bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ?
295 " REDUCED_BLANKING" : "",
Hans Verkuil1b3b3842015-06-12 03:52:54 -0300296 ((bt->flags & V4L2_DV_FL_REDUCED_BLANKING) &&
297 bt->vsync == 8) ? " (V2)" : "",
Hans Verkuil0216dc22013-08-15 08:02:40 -0300298 (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ?
299 " CAN_REDUCE_FPS" : "",
300 (bt->flags & V4L2_DV_FL_REDUCED_FPS) ?
301 " REDUCED_FPS" : "",
302 (bt->flags & V4L2_DV_FL_HALF_LINE) ?
Hans Verkuil74d802d2015-04-03 06:27:02 -0300303 " HALF_LINE" : "",
304 (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) ?
305 " CE_VIDEO" : "");
Hans Verkuil0216dc22013-08-15 08:02:40 -0300306 pr_info("%s: standards (0x%x):%s%s%s%s\n", dev_prefix, bt->standards,
307 (bt->standards & V4L2_DV_BT_STD_CEA861) ? " CEA" : "",
308 (bt->standards & V4L2_DV_BT_STD_DMT) ? " DMT" : "",
309 (bt->standards & V4L2_DV_BT_STD_CVT) ? " CVT" : "",
310 (bt->standards & V4L2_DV_BT_STD_GTF) ? " GTF" : "");
311}
312EXPORT_SYMBOL_GPL(v4l2_print_dv_timings);
313
Hans Verkuil25764152013-07-29 08:40:56 -0300314/*
315 * CVT defines
316 * Based on Coordinated Video Timings Standard
317 * version 1.1 September 10, 2003
318 */
319
320#define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300321#define CVT_PXL_CLK_GRAN_RB_V2 1000 /* granularity for reduced blanking v2*/
Hans Verkuil25764152013-07-29 08:40:56 -0300322
323/* Normal blanking */
324#define CVT_MIN_V_BPORCH 7 /* lines */
325#define CVT_MIN_V_PORCH_RND 3 /* lines */
326#define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
Prashant Laddha9c3f2052015-04-22 14:32:37 -0300327#define CVT_HSYNC_PERCENT 8 /* nominal hsync as percentage of line */
Hans Verkuil25764152013-07-29 08:40:56 -0300328
329/* Normal blanking for CVT uses GTF to calculate horizontal blanking */
330#define CVT_CELL_GRAN 8 /* character cell granularity */
331#define CVT_M 600 /* blanking formula gradient */
332#define CVT_C 40 /* blanking formula offset */
333#define CVT_K 128 /* blanking formula scaling factor */
334#define CVT_J 20 /* blanking formula scaling factor */
335#define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
336#define CVT_M_PRIME (CVT_K * CVT_M / 256)
337
338/* Reduced Blanking */
339#define CVT_RB_MIN_V_BPORCH 7 /* lines */
340#define CVT_RB_V_FPORCH 3 /* lines */
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300341#define CVT_RB_MIN_V_BLANK 460 /* us */
Hans Verkuil25764152013-07-29 08:40:56 -0300342#define CVT_RB_H_SYNC 32 /* pixels */
Hans Verkuil25764152013-07-29 08:40:56 -0300343#define CVT_RB_H_BLANK 160 /* pixels */
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300344/* Reduce blanking Version 2 */
345#define CVT_RB_V2_H_BLANK 80 /* pixels */
346#define CVT_RB_MIN_V_FPORCH 3 /* lines */
347#define CVT_RB_V2_MIN_V_FPORCH 1 /* lines */
348#define CVT_RB_V_BPORCH 6 /* lines */
Hans Verkuil25764152013-07-29 08:40:56 -0300349
350/** v4l2_detect_cvt - detect if the given timings follow the CVT standard
351 * @frame_height - the total height of the frame (including blanking) in lines.
352 * @hfreq - the horizontal frequency in Hz.
353 * @vsync - the height of the vertical sync in lines.
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300354 * @active_width - active width of image (does not include blanking). This
355 * information is needed only in case of version 2 of reduced blanking.
356 * In other cases, this parameter does not have any effect on timings.
Hans Verkuil25764152013-07-29 08:40:56 -0300357 * @polarities - the horizontal and vertical polarities (same as struct
358 * v4l2_bt_timings polarities).
Prashant Laddha061ddda2015-05-22 02:27:34 -0300359 * @interlaced - if this flag is true, it indicates interlaced format
Hans Verkuil25764152013-07-29 08:40:56 -0300360 * @fmt - the resulting timings.
361 *
362 * This function will attempt to detect if the given values correspond to a
363 * valid CVT format. If so, then it will return true, and fmt will be filled
364 * in with the found CVT timings.
365 */
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300366bool v4l2_detect_cvt(unsigned frame_height,
367 unsigned hfreq,
368 unsigned vsync,
369 unsigned active_width,
370 u32 polarities,
371 bool interlaced,
372 struct v4l2_dv_timings *fmt)
Hans Verkuil25764152013-07-29 08:40:56 -0300373{
374 int v_fp, v_bp, h_fp, h_bp, hsync;
375 int frame_width, image_height, image_width;
376 bool reduced_blanking;
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300377 bool rb_v2 = false;
Hans Verkuil25764152013-07-29 08:40:56 -0300378 unsigned pix_clk;
379
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300380 if (vsync < 4 || vsync > 8)
Hans Verkuil25764152013-07-29 08:40:56 -0300381 return false;
382
383 if (polarities == V4L2_DV_VSYNC_POS_POL)
384 reduced_blanking = false;
385 else if (polarities == V4L2_DV_HSYNC_POS_POL)
386 reduced_blanking = true;
387 else
388 return false;
389
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300390 if (reduced_blanking && vsync == 8)
391 rb_v2 = true;
392
393 if (rb_v2 && active_width == 0)
394 return false;
395
396 if (!rb_v2 && vsync > 7)
397 return false;
398
Prashant Laddha947ed992015-04-22 14:32:36 -0300399 if (hfreq == 0)
400 return false;
401
Hans Verkuil25764152013-07-29 08:40:56 -0300402 /* Vertical */
403 if (reduced_blanking) {
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300404 if (rb_v2) {
405 v_bp = CVT_RB_V_BPORCH;
406 v_fp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
407 v_fp -= vsync + v_bp;
Hans Verkuil25764152013-07-29 08:40:56 -0300408
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300409 if (v_fp < CVT_RB_V2_MIN_V_FPORCH)
410 v_fp = CVT_RB_V2_MIN_V_FPORCH;
411 } else {
412 v_fp = CVT_RB_V_FPORCH;
413 v_bp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
414 v_bp -= vsync + v_fp;
415
416 if (v_bp < CVT_RB_MIN_V_BPORCH)
417 v_bp = CVT_RB_MIN_V_BPORCH;
418 }
Hans Verkuil25764152013-07-29 08:40:56 -0300419 } else {
420 v_fp = CVT_MIN_V_PORCH_RND;
Prashant Laddhaf6747652015-04-22 14:32:34 -0300421 v_bp = (CVT_MIN_VSYNC_BP * hfreq) / 1000000 + 1 - vsync;
Hans Verkuil25764152013-07-29 08:40:56 -0300422
423 if (v_bp < CVT_MIN_V_BPORCH)
424 v_bp = CVT_MIN_V_BPORCH;
425 }
Prashant Laddha061ddda2015-05-22 02:27:34 -0300426
427 if (interlaced)
428 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
429 else
430 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
Hans Verkuil25764152013-07-29 08:40:56 -0300431
Prashant Laddha947ed992015-04-22 14:32:36 -0300432 if (image_height < 0)
433 return false;
434
Hans Verkuil25764152013-07-29 08:40:56 -0300435 /* Aspect ratio based on vsync */
436 switch (vsync) {
437 case 4:
438 image_width = (image_height * 4) / 3;
439 break;
440 case 5:
441 image_width = (image_height * 16) / 9;
442 break;
443 case 6:
444 image_width = (image_height * 16) / 10;
445 break;
446 case 7:
447 /* special case */
448 if (image_height == 1024)
449 image_width = (image_height * 5) / 4;
450 else if (image_height == 768)
451 image_width = (image_height * 15) / 9;
452 else
453 return false;
454 break;
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300455 case 8:
456 image_width = active_width;
457 break;
Hans Verkuil25764152013-07-29 08:40:56 -0300458 default:
459 return false;
460 }
461
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300462 if (!rb_v2)
463 image_width = image_width & ~7;
Hans Verkuil25764152013-07-29 08:40:56 -0300464
465 /* Horizontal */
466 if (reduced_blanking) {
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300467 int h_blank;
468 int clk_gran;
Hans Verkuil25764152013-07-29 08:40:56 -0300469
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300470 h_blank = rb_v2 ? CVT_RB_V2_H_BLANK : CVT_RB_H_BLANK;
471 clk_gran = rb_v2 ? CVT_PXL_CLK_GRAN_RB_V2 : CVT_PXL_CLK_GRAN;
472
473 pix_clk = (image_width + h_blank) * hfreq;
474 pix_clk = (pix_clk / clk_gran) * clk_gran;
475
476 h_bp = h_blank / 2;
Hans Verkuil25764152013-07-29 08:40:56 -0300477 hsync = CVT_RB_H_SYNC;
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300478 h_fp = h_blank - h_bp - hsync;
Hans Verkuil25764152013-07-29 08:40:56 -0300479
Prashant Laddha5fea1bb2015-06-10 13:51:42 -0300480 frame_width = image_width + h_blank;
Hans Verkuil25764152013-07-29 08:40:56 -0300481 } else {
Martin Buggec3e75c72013-08-14 04:58:38 -0300482 unsigned ideal_duty_cycle_per_myriad =
483 100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq;
Hans Verkuil25764152013-07-29 08:40:56 -0300484 int h_blank;
Hans Verkuil25764152013-07-29 08:40:56 -0300485
Martin Buggec3e75c72013-08-14 04:58:38 -0300486 if (ideal_duty_cycle_per_myriad < 2000)
487 ideal_duty_cycle_per_myriad = 2000;
Hans Verkuil25764152013-07-29 08:40:56 -0300488
Martin Buggec3e75c72013-08-14 04:58:38 -0300489 h_blank = image_width * ideal_duty_cycle_per_myriad /
490 (10000 - ideal_duty_cycle_per_myriad);
491 h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN;
Hans Verkuil25764152013-07-29 08:40:56 -0300492
493 pix_clk = (image_width + h_blank) * hfreq;
494 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
495
496 h_bp = h_blank / 2;
497 frame_width = image_width + h_blank;
498
Prashant Laddha9c3f2052015-04-22 14:32:37 -0300499 hsync = frame_width * CVT_HSYNC_PERCENT / 100;
Prashant Laddhad7ed5a32015-04-22 14:32:35 -0300500 hsync = (hsync / CVT_CELL_GRAN) * CVT_CELL_GRAN;
Hans Verkuil25764152013-07-29 08:40:56 -0300501 h_fp = h_blank - hsync - h_bp;
502 }
503
Hans Verkuil074ca432013-08-19 08:38:29 -0300504 fmt->type = V4L2_DV_BT_656_1120;
Hans Verkuil25764152013-07-29 08:40:56 -0300505 fmt->bt.polarities = polarities;
506 fmt->bt.width = image_width;
507 fmt->bt.height = image_height;
508 fmt->bt.hfrontporch = h_fp;
509 fmt->bt.vfrontporch = v_fp;
510 fmt->bt.hsync = hsync;
511 fmt->bt.vsync = vsync;
512 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300513
514 if (!interlaced) {
515 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
516 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
517 } else {
518 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
519 2 * vsync) / 2;
520 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
521 2 * vsync - fmt->bt.vbackporch;
522 fmt->bt.il_vfrontporch = v_fp;
523 fmt->bt.il_vsync = vsync;
524 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
525 fmt->bt.interlaced = V4L2_DV_INTERLACED;
526 }
527
Hans Verkuil25764152013-07-29 08:40:56 -0300528 fmt->bt.pixelclock = pix_clk;
529 fmt->bt.standards = V4L2_DV_BT_STD_CVT;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300530
Hans Verkuil25764152013-07-29 08:40:56 -0300531 if (reduced_blanking)
532 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300533
Hans Verkuil25764152013-07-29 08:40:56 -0300534 return true;
535}
536EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
537
538/*
539 * GTF defines
540 * Based on Generalized Timing Formula Standard
541 * Version 1.1 September 2, 1999
542 */
543
544#define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */
545
546#define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
547#define GTF_V_FP 1 /* vertical front porch (lines) */
548#define GTF_CELL_GRAN 8 /* character cell granularity */
549
550/* Default */
551#define GTF_D_M 600 /* blanking formula gradient */
552#define GTF_D_C 40 /* blanking formula offset */
553#define GTF_D_K 128 /* blanking formula scaling factor */
554#define GTF_D_J 20 /* blanking formula scaling factor */
555#define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
556#define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
557
558/* Secondary */
559#define GTF_S_M 3600 /* blanking formula gradient */
560#define GTF_S_C 40 /* blanking formula offset */
561#define GTF_S_K 128 /* blanking formula scaling factor */
562#define GTF_S_J 35 /* blanking formula scaling factor */
563#define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
564#define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
565
566/** v4l2_detect_gtf - detect if the given timings follow the GTF standard
567 * @frame_height - the total height of the frame (including blanking) in lines.
568 * @hfreq - the horizontal frequency in Hz.
569 * @vsync - the height of the vertical sync in lines.
570 * @polarities - the horizontal and vertical polarities (same as struct
571 * v4l2_bt_timings polarities).
Prashant Laddha061ddda2015-05-22 02:27:34 -0300572 * @interlaced - if this flag is true, it indicates interlaced format
Hans Verkuil25764152013-07-29 08:40:56 -0300573 * @aspect - preferred aspect ratio. GTF has no method of determining the
574 * aspect ratio in order to derive the image width from the
575 * image height, so it has to be passed explicitly. Usually
576 * the native screen aspect ratio is used for this. If it
577 * is not filled in correctly, then 16:9 will be assumed.
578 * @fmt - the resulting timings.
579 *
580 * This function will attempt to detect if the given values correspond to a
581 * valid GTF format. If so, then it will return true, and fmt will be filled
582 * in with the found GTF timings.
583 */
584bool v4l2_detect_gtf(unsigned frame_height,
585 unsigned hfreq,
586 unsigned vsync,
587 u32 polarities,
Prashant Laddha061ddda2015-05-22 02:27:34 -0300588 bool interlaced,
Hans Verkuil25764152013-07-29 08:40:56 -0300589 struct v4l2_fract aspect,
590 struct v4l2_dv_timings *fmt)
591{
592 int pix_clk;
593 int v_fp, v_bp, h_fp, hsync;
594 int frame_width, image_height, image_width;
595 bool default_gtf;
596 int h_blank;
597
598 if (vsync != 3)
599 return false;
600
601 if (polarities == V4L2_DV_VSYNC_POS_POL)
602 default_gtf = true;
603 else if (polarities == V4L2_DV_HSYNC_POS_POL)
604 default_gtf = false;
605 else
606 return false;
607
Prashant Laddha947ed992015-04-22 14:32:36 -0300608 if (hfreq == 0)
609 return false;
610
Hans Verkuil25764152013-07-29 08:40:56 -0300611 /* Vertical */
612 v_fp = GTF_V_FP;
Prashant Laddhaf6747652015-04-22 14:32:34 -0300613 v_bp = (GTF_MIN_VSYNC_BP * hfreq + 500000) / 1000000 - vsync;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300614 if (interlaced)
615 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
616 else
617 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
Hans Verkuil25764152013-07-29 08:40:56 -0300618
Prashant Laddha947ed992015-04-22 14:32:36 -0300619 if (image_height < 0)
620 return false;
621
Hans Verkuil25764152013-07-29 08:40:56 -0300622 if (aspect.numerator == 0 || aspect.denominator == 0) {
623 aspect.numerator = 16;
624 aspect.denominator = 9;
625 }
626 image_width = ((image_height * aspect.numerator) / aspect.denominator);
Martin Bugge257cc4b2014-01-23 06:40:00 -0300627 image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1);
Hans Verkuil25764152013-07-29 08:40:56 -0300628
629 /* Horizontal */
Prashant Laddhac9bc9f52015-05-05 10:16:27 -0300630 if (default_gtf) {
631 u64 num;
632 u32 den;
633
634 num = ((image_width * GTF_D_C_PRIME * (u64)hfreq) -
635 ((u64)image_width * GTF_D_M_PRIME * 1000));
Prashant Laddhadc0cf4c2015-06-05 05:13:31 -0300636 den = (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) *
637 (2 * GTF_CELL_GRAN);
Prashant Laddhac9bc9f52015-05-05 10:16:27 -0300638 h_blank = div_u64((num + (den >> 1)), den);
Prashant Laddhadc0cf4c2015-06-05 05:13:31 -0300639 h_blank *= (2 * GTF_CELL_GRAN);
Prashant Laddhac9bc9f52015-05-05 10:16:27 -0300640 } else {
641 u64 num;
642 u32 den;
643
644 num = ((image_width * GTF_S_C_PRIME * (u64)hfreq) -
645 ((u64)image_width * GTF_S_M_PRIME * 1000));
Prashant Laddhadc0cf4c2015-06-05 05:13:31 -0300646 den = (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) *
647 (2 * GTF_CELL_GRAN);
Prashant Laddhac9bc9f52015-05-05 10:16:27 -0300648 h_blank = div_u64((num + (den >> 1)), den);
Prashant Laddhadc0cf4c2015-06-05 05:13:31 -0300649 h_blank *= (2 * GTF_CELL_GRAN);
Prashant Laddhac9bc9f52015-05-05 10:16:27 -0300650 }
Hans Verkuil25764152013-07-29 08:40:56 -0300651
Hans Verkuil25764152013-07-29 08:40:56 -0300652 frame_width = image_width + h_blank;
653
654 pix_clk = (image_width + h_blank) * hfreq;
655 pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
656
657 hsync = (frame_width * 8 + 50) / 100;
Prashant Laddhad7ed5a32015-04-22 14:32:35 -0300658 hsync = ((hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN) * GTF_CELL_GRAN;
Hans Verkuil25764152013-07-29 08:40:56 -0300659
660 h_fp = h_blank / 2 - hsync;
661
Hans Verkuil074ca432013-08-19 08:38:29 -0300662 fmt->type = V4L2_DV_BT_656_1120;
Hans Verkuil25764152013-07-29 08:40:56 -0300663 fmt->bt.polarities = polarities;
664 fmt->bt.width = image_width;
665 fmt->bt.height = image_height;
666 fmt->bt.hfrontporch = h_fp;
667 fmt->bt.vfrontporch = v_fp;
668 fmt->bt.hsync = hsync;
669 fmt->bt.vsync = vsync;
670 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300671
672 if (!interlaced) {
673 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
674 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
675 } else {
676 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
677 2 * vsync) / 2;
678 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
679 2 * vsync - fmt->bt.vbackporch;
680 fmt->bt.il_vfrontporch = v_fp;
681 fmt->bt.il_vsync = vsync;
682 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
683 fmt->bt.interlaced = V4L2_DV_INTERLACED;
684 }
685
Hans Verkuil25764152013-07-29 08:40:56 -0300686 fmt->bt.pixelclock = pix_clk;
687 fmt->bt.standards = V4L2_DV_BT_STD_GTF;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300688
Hans Verkuil25764152013-07-29 08:40:56 -0300689 if (!default_gtf)
690 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
Prashant Laddha061ddda2015-05-22 02:27:34 -0300691
Hans Verkuil25764152013-07-29 08:40:56 -0300692 return true;
693}
694EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
695
696/** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
697 * 0x15 and 0x16 from the EDID.
698 * @hor_landscape - byte 0x15 from the EDID.
699 * @vert_portrait - byte 0x16 from the EDID.
700 *
701 * Determines the aspect ratio from the EDID.
702 * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
703 * "Horizontal and Vertical Screen Size or Aspect Ratio"
704 */
705struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
706{
707 struct v4l2_fract aspect = { 16, 9 };
Hans Verkuil25764152013-07-29 08:40:56 -0300708 u8 ratio;
709
710 /* Nothing filled in, fallback to 16:9 */
711 if (!hor_landscape && !vert_portrait)
712 return aspect;
713 /* Both filled in, so they are interpreted as the screen size in cm */
714 if (hor_landscape && vert_portrait) {
715 aspect.numerator = hor_landscape;
716 aspect.denominator = vert_portrait;
717 return aspect;
718 }
719 /* Only one is filled in, so interpret them as a ratio:
720 (val + 99) / 100 */
721 ratio = hor_landscape | vert_portrait;
722 /* Change some rounded values into the exact aspect ratio */
723 if (ratio == 79) {
724 aspect.numerator = 16;
725 aspect.denominator = 9;
726 } else if (ratio == 34) {
727 aspect.numerator = 4;
Rickard Strandqvistf71920e2014-06-14 08:37:09 -0300728 aspect.denominator = 3;
Hans Verkuil25764152013-07-29 08:40:56 -0300729 } else if (ratio == 68) {
730 aspect.numerator = 15;
Rickard Strandqvistf71920e2014-06-14 08:37:09 -0300731 aspect.denominator = 9;
Hans Verkuil25764152013-07-29 08:40:56 -0300732 } else {
733 aspect.numerator = hor_landscape + 99;
734 aspect.denominator = 100;
735 }
736 if (hor_landscape)
737 return aspect;
738 /* The aspect ratio is for portrait, so swap numerator and denominator */
Fabian Frederickfd752422015-06-10 13:32:33 -0300739 swap(aspect.denominator, aspect.numerator);
Hans Verkuil25764152013-07-29 08:40:56 -0300740 return aspect;
741}
742EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);