1 /*
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>
27 #include <media/v4l2-dv-timings.h>
28 #include <linux/math64.h>
29 
30 MODULE_AUTHOR("Hans Verkuil");
31 MODULE_DESCRIPTION("V4L2 DV Timings Helper Functions");
32 MODULE_LICENSE("GPL");
33 
34 const struct v4l2_dv_timings v4l2_dv_timings_presets[] = {
35 	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,
135 	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,
146 	{ }
147 };
148 EXPORT_SYMBOL_GPL(v4l2_dv_timings_presets);
149 
150 bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t,
151 			   const struct v4l2_dv_timings_cap *dvcap,
152 			   v4l2_check_dv_timings_fnc fnc,
153 			   void *fnc_handle)
154 {
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 ||
168 	    (cap->standards && bt->standards &&
169 	     !(bt->standards & cap->standards)) ||
170 	    (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) ||
171 	    (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE)))
172 		return false;
173 	return fnc == NULL || fnc(t, fnc_handle);
174 }
175 EXPORT_SYMBOL_GPL(v4l2_valid_dv_timings);
176 
177 int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
178 			     const struct v4l2_dv_timings_cap *cap,
179 			     v4l2_check_dv_timings_fnc fnc,
180 			     void *fnc_handle)
181 {
182 	u32 i, idx;
183 
184 	memset(t->reserved, 0, sizeof(t->reserved));
185 	for (i = idx = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
186 		if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
187 					  fnc, fnc_handle) &&
188 		    idx++ == t->index) {
189 			t->timings = v4l2_dv_timings_presets[i];
190 			return 0;
191 		}
192 	}
193 	return -EINVAL;
194 }
195 EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap);
196 
197 bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
198 			      const struct v4l2_dv_timings_cap *cap,
199 			      unsigned pclock_delta,
200 			      v4l2_check_dv_timings_fnc fnc,
201 			      void *fnc_handle)
202 {
203 	int i;
204 
205 	if (!v4l2_valid_dv_timings(t, cap, fnc, fnc_handle))
206 		return false;
207 
208 	for (i = 0; i < v4l2_dv_timings_presets[i].bt.width; i++) {
209 		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)) {
213 			*t = v4l2_dv_timings_presets[i];
214 			return true;
215 		}
216 	}
217 	return false;
218 }
219 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap);
220 
221 /**
222  * v4l2_match_dv_timings - check if two timings match
223  * @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  */
229 bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1,
230 			   const struct v4l2_dv_timings *t2,
231 			   unsigned pclock_delta)
232 {
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 }
252 EXPORT_SYMBOL_GPL(v4l2_match_dv_timings);
253 
254 void 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 	u32 fps;
260 
261 	if (t->type != V4L2_DV_BT_656_1120)
262 		return;
263 
264 	htot = V4L2_DV_BT_FRAME_WIDTH(bt);
265 	vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
266 	if (bt->interlaced)
267 		vtot /= 2;
268 
269 	fps = (htot * vtot) > 0 ? div_u64((100 * (u64)bt->pixelclock),
270 				  (htot * vtot)) : 0;
271 
272 	if (prefix == NULL)
273 		prefix = "";
274 
275 	pr_info("%s: %s%ux%u%s%u.%u (%ux%u)\n", dev_prefix, prefix,
276 		bt->width, bt->height, bt->interlaced ? "i" : "p",
277 		fps / 100, fps % 100, htot, vtot);
278 
279 	if (!detailed)
280 		return;
281 
282 	pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n",
283 			dev_prefix, bt->hfrontporch,
284 			(bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
285 			bt->hsync, bt->hbackporch);
286 	pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n",
287 			dev_prefix, bt->vfrontporch,
288 			(bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
289 			bt->vsync, bt->vbackporch);
290 	if (bt->interlaced)
291 		pr_info("%s: vertical bottom field: fp = %u, %ssync = %u, bp = %u\n",
292 			dev_prefix, bt->il_vfrontporch,
293 			(bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
294 			bt->il_vsync, bt->il_vbackporch);
295 	pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock);
296 	pr_info("%s: flags (0x%x):%s%s%s%s%s%s\n", dev_prefix, bt->flags,
297 			(bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ?
298 			" REDUCED_BLANKING" : "",
299 			((bt->flags & V4L2_DV_FL_REDUCED_BLANKING) &&
300 			 bt->vsync == 8) ? " (V2)" : "",
301 			(bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ?
302 			" CAN_REDUCE_FPS" : "",
303 			(bt->flags & V4L2_DV_FL_REDUCED_FPS) ?
304 			" REDUCED_FPS" : "",
305 			(bt->flags & V4L2_DV_FL_HALF_LINE) ?
306 			" HALF_LINE" : "",
307 			(bt->flags & V4L2_DV_FL_IS_CE_VIDEO) ?
308 			" CE_VIDEO" : "");
309 	pr_info("%s: standards (0x%x):%s%s%s%s\n", dev_prefix, bt->standards,
310 			(bt->standards & V4L2_DV_BT_STD_CEA861) ?  " CEA" : "",
311 			(bt->standards & V4L2_DV_BT_STD_DMT) ?  " DMT" : "",
312 			(bt->standards & V4L2_DV_BT_STD_CVT) ?  " CVT" : "",
313 			(bt->standards & V4L2_DV_BT_STD_GTF) ?  " GTF" : "");
314 }
315 EXPORT_SYMBOL_GPL(v4l2_print_dv_timings);
316 
317 /*
318  * CVT defines
319  * Based on Coordinated Video Timings Standard
320  * version 1.1 September 10, 2003
321  */
322 
323 #define CVT_PXL_CLK_GRAN	250000	/* pixel clock granularity */
324 #define CVT_PXL_CLK_GRAN_RB_V2 1000	/* granularity for reduced blanking v2*/
325 
326 /* Normal blanking */
327 #define CVT_MIN_V_BPORCH	7	/* lines */
328 #define CVT_MIN_V_PORCH_RND	3	/* lines */
329 #define CVT_MIN_VSYNC_BP	550	/* min time of vsync + back porch (us) */
330 #define CVT_HSYNC_PERCENT       8       /* nominal hsync as percentage of line */
331 
332 /* Normal blanking for CVT uses GTF to calculate horizontal blanking */
333 #define CVT_CELL_GRAN		8	/* character cell granularity */
334 #define CVT_M			600	/* blanking formula gradient */
335 #define CVT_C			40	/* blanking formula offset */
336 #define CVT_K			128	/* blanking formula scaling factor */
337 #define CVT_J			20	/* blanking formula scaling factor */
338 #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
339 #define CVT_M_PRIME (CVT_K * CVT_M / 256)
340 
341 /* Reduced Blanking */
342 #define CVT_RB_MIN_V_BPORCH    7       /* lines  */
343 #define CVT_RB_V_FPORCH        3       /* lines  */
344 #define CVT_RB_MIN_V_BLANK   460       /* us     */
345 #define CVT_RB_H_SYNC         32       /* pixels */
346 #define CVT_RB_H_BLANK       160       /* pixels */
347 /* Reduce blanking Version 2 */
348 #define CVT_RB_V2_H_BLANK     80       /* pixels */
349 #define CVT_RB_MIN_V_FPORCH    3       /* lines  */
350 #define CVT_RB_V2_MIN_V_FPORCH 1       /* lines  */
351 #define CVT_RB_V_BPORCH        6       /* lines  */
352 
353 /** v4l2_detect_cvt - detect if the given timings follow the CVT standard
354  * @frame_height - the total height of the frame (including blanking) in lines.
355  * @hfreq - the horizontal frequency in Hz.
356  * @vsync - the height of the vertical sync in lines.
357  * @active_width - active width of image (does not include blanking). This
358  * information is needed only in case of version 2 of reduced blanking.
359  * In other cases, this parameter does not have any effect on timings.
360  * @polarities - the horizontal and vertical polarities (same as struct
361  *		v4l2_bt_timings polarities).
362  * @interlaced - if this flag is true, it indicates interlaced format
363  * @fmt - the resulting timings.
364  *
365  * This function will attempt to detect if the given values correspond to a
366  * valid CVT format. If so, then it will return true, and fmt will be filled
367  * in with the found CVT timings.
368  */
369 bool v4l2_detect_cvt(unsigned frame_height,
370 		     unsigned hfreq,
371 		     unsigned vsync,
372 		     unsigned active_width,
373 		     u32 polarities,
374 		     bool interlaced,
375 		     struct v4l2_dv_timings *fmt)
376 {
377 	int  v_fp, v_bp, h_fp, h_bp, hsync;
378 	int  frame_width, image_height, image_width;
379 	bool reduced_blanking;
380 	bool rb_v2 = false;
381 	unsigned pix_clk;
382 
383 	if (vsync < 4 || vsync > 8)
384 		return false;
385 
386 	if (polarities == V4L2_DV_VSYNC_POS_POL)
387 		reduced_blanking = false;
388 	else if (polarities == V4L2_DV_HSYNC_POS_POL)
389 		reduced_blanking = true;
390 	else
391 		return false;
392 
393 	if (reduced_blanking && vsync == 8)
394 		rb_v2 = true;
395 
396 	if (rb_v2 && active_width == 0)
397 		return false;
398 
399 	if (!rb_v2 && vsync > 7)
400 		return false;
401 
402 	if (hfreq == 0)
403 		return false;
404 
405 	/* Vertical */
406 	if (reduced_blanking) {
407 		if (rb_v2) {
408 			v_bp = CVT_RB_V_BPORCH;
409 			v_fp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
410 			v_fp -= vsync + v_bp;
411 
412 			if (v_fp < CVT_RB_V2_MIN_V_FPORCH)
413 				v_fp = CVT_RB_V2_MIN_V_FPORCH;
414 		} else {
415 			v_fp = CVT_RB_V_FPORCH;
416 			v_bp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
417 			v_bp -= vsync + v_fp;
418 
419 			if (v_bp < CVT_RB_MIN_V_BPORCH)
420 				v_bp = CVT_RB_MIN_V_BPORCH;
421 		}
422 	} else {
423 		v_fp = CVT_MIN_V_PORCH_RND;
424 		v_bp = (CVT_MIN_VSYNC_BP * hfreq) / 1000000 + 1 - vsync;
425 
426 		if (v_bp < CVT_MIN_V_BPORCH)
427 			v_bp = CVT_MIN_V_BPORCH;
428 	}
429 
430 	if (interlaced)
431 		image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
432 	else
433 		image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
434 
435 	if (image_height < 0)
436 		return false;
437 
438 	/* Aspect ratio based on vsync */
439 	switch (vsync) {
440 	case 4:
441 		image_width = (image_height * 4) / 3;
442 		break;
443 	case 5:
444 		image_width = (image_height * 16) / 9;
445 		break;
446 	case 6:
447 		image_width = (image_height * 16) / 10;
448 		break;
449 	case 7:
450 		/* special case */
451 		if (image_height == 1024)
452 			image_width = (image_height * 5) / 4;
453 		else if (image_height == 768)
454 			image_width = (image_height * 15) / 9;
455 		else
456 			return false;
457 		break;
458 	case 8:
459 		image_width = active_width;
460 		break;
461 	default:
462 		return false;
463 	}
464 
465 	if (!rb_v2)
466 		image_width = image_width & ~7;
467 
468 	/* Horizontal */
469 	if (reduced_blanking) {
470 		int h_blank;
471 		int clk_gran;
472 
473 		h_blank = rb_v2 ? CVT_RB_V2_H_BLANK : CVT_RB_H_BLANK;
474 		clk_gran = rb_v2 ? CVT_PXL_CLK_GRAN_RB_V2 : CVT_PXL_CLK_GRAN;
475 
476 		pix_clk = (image_width + h_blank) * hfreq;
477 		pix_clk = (pix_clk / clk_gran) * clk_gran;
478 
479 		h_bp  = h_blank / 2;
480 		hsync = CVT_RB_H_SYNC;
481 		h_fp  = h_blank - h_bp - hsync;
482 
483 		frame_width = image_width + h_blank;
484 	} else {
485 		unsigned ideal_duty_cycle_per_myriad =
486 			100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq;
487 		int h_blank;
488 
489 		if (ideal_duty_cycle_per_myriad < 2000)
490 			ideal_duty_cycle_per_myriad = 2000;
491 
492 		h_blank = image_width * ideal_duty_cycle_per_myriad /
493 					(10000 - ideal_duty_cycle_per_myriad);
494 		h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN;
495 
496 		pix_clk = (image_width + h_blank) * hfreq;
497 		pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
498 
499 		h_bp = h_blank / 2;
500 		frame_width = image_width + h_blank;
501 
502 		hsync = frame_width * CVT_HSYNC_PERCENT / 100;
503 		hsync = (hsync / CVT_CELL_GRAN) * CVT_CELL_GRAN;
504 		h_fp = h_blank - hsync - h_bp;
505 	}
506 
507 	fmt->type = V4L2_DV_BT_656_1120;
508 	fmt->bt.polarities = polarities;
509 	fmt->bt.width = image_width;
510 	fmt->bt.height = image_height;
511 	fmt->bt.hfrontporch = h_fp;
512 	fmt->bt.vfrontporch = v_fp;
513 	fmt->bt.hsync = hsync;
514 	fmt->bt.vsync = vsync;
515 	fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
516 
517 	if (!interlaced) {
518 		fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
519 		fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
520 	} else {
521 		fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
522 				      2 * vsync) / 2;
523 		fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
524 					2 * vsync - fmt->bt.vbackporch;
525 		fmt->bt.il_vfrontporch = v_fp;
526 		fmt->bt.il_vsync = vsync;
527 		fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
528 		fmt->bt.interlaced = V4L2_DV_INTERLACED;
529 	}
530 
531 	fmt->bt.pixelclock = pix_clk;
532 	fmt->bt.standards = V4L2_DV_BT_STD_CVT;
533 
534 	if (reduced_blanking)
535 		fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
536 
537 	return true;
538 }
539 EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
540 
541 /*
542  * GTF defines
543  * Based on Generalized Timing Formula Standard
544  * Version 1.1 September 2, 1999
545  */
546 
547 #define GTF_PXL_CLK_GRAN	250000	/* pixel clock granularity */
548 
549 #define GTF_MIN_VSYNC_BP	550	/* min time of vsync + back porch (us) */
550 #define GTF_V_FP		1	/* vertical front porch (lines) */
551 #define GTF_CELL_GRAN		8	/* character cell granularity */
552 
553 /* Default */
554 #define GTF_D_M			600	/* blanking formula gradient */
555 #define GTF_D_C			40	/* blanking formula offset */
556 #define GTF_D_K			128	/* blanking formula scaling factor */
557 #define GTF_D_J			20	/* blanking formula scaling factor */
558 #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
559 #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
560 
561 /* Secondary */
562 #define GTF_S_M			3600	/* blanking formula gradient */
563 #define GTF_S_C			40	/* blanking formula offset */
564 #define GTF_S_K			128	/* blanking formula scaling factor */
565 #define GTF_S_J			35	/* blanking formula scaling factor */
566 #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
567 #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
568 
569 /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
570  * @frame_height - the total height of the frame (including blanking) in lines.
571  * @hfreq - the horizontal frequency in Hz.
572  * @vsync - the height of the vertical sync in lines.
573  * @polarities - the horizontal and vertical polarities (same as struct
574  *		v4l2_bt_timings polarities).
575  * @interlaced - if this flag is true, it indicates interlaced format
576  * @aspect - preferred aspect ratio. GTF has no method of determining the
577  *		aspect ratio in order to derive the image width from the
578  *		image height, so it has to be passed explicitly. Usually
579  *		the native screen aspect ratio is used for this. If it
580  *		is not filled in correctly, then 16:9 will be assumed.
581  * @fmt - the resulting timings.
582  *
583  * This function will attempt to detect if the given values correspond to a
584  * valid GTF format. If so, then it will return true, and fmt will be filled
585  * in with the found GTF timings.
586  */
587 bool v4l2_detect_gtf(unsigned frame_height,
588 		unsigned hfreq,
589 		unsigned vsync,
590 		u32 polarities,
591 		bool interlaced,
592 		struct v4l2_fract aspect,
593 		struct v4l2_dv_timings *fmt)
594 {
595 	int pix_clk;
596 	int  v_fp, v_bp, h_fp, hsync;
597 	int frame_width, image_height, image_width;
598 	bool default_gtf;
599 	int h_blank;
600 
601 	if (vsync != 3)
602 		return false;
603 
604 	if (polarities == V4L2_DV_VSYNC_POS_POL)
605 		default_gtf = true;
606 	else if (polarities == V4L2_DV_HSYNC_POS_POL)
607 		default_gtf = false;
608 	else
609 		return false;
610 
611 	if (hfreq == 0)
612 		return false;
613 
614 	/* Vertical */
615 	v_fp = GTF_V_FP;
616 	v_bp = (GTF_MIN_VSYNC_BP * hfreq + 500000) / 1000000 - vsync;
617 	if (interlaced)
618 		image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
619 	else
620 		image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
621 
622 	if (image_height < 0)
623 		return false;
624 
625 	if (aspect.numerator == 0 || aspect.denominator == 0) {
626 		aspect.numerator = 16;
627 		aspect.denominator = 9;
628 	}
629 	image_width = ((image_height * aspect.numerator) / aspect.denominator);
630 	image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1);
631 
632 	/* Horizontal */
633 	if (default_gtf) {
634 		u64 num;
635 		u32 den;
636 
637 		num = ((image_width * GTF_D_C_PRIME * (u64)hfreq) -
638 		      ((u64)image_width * GTF_D_M_PRIME * 1000));
639 		den = (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) *
640 		      (2 * GTF_CELL_GRAN);
641 		h_blank = div_u64((num + (den >> 1)), den);
642 		h_blank *= (2 * GTF_CELL_GRAN);
643 	} else {
644 		u64 num;
645 		u32 den;
646 
647 		num = ((image_width * GTF_S_C_PRIME * (u64)hfreq) -
648 		      ((u64)image_width * GTF_S_M_PRIME * 1000));
649 		den = (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) *
650 		      (2 * GTF_CELL_GRAN);
651 		h_blank = div_u64((num + (den >> 1)), den);
652 		h_blank *= (2 * GTF_CELL_GRAN);
653 	}
654 
655 	frame_width = image_width + h_blank;
656 
657 	pix_clk = (image_width + h_blank) * hfreq;
658 	pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
659 
660 	hsync = (frame_width * 8 + 50) / 100;
661 	hsync = ((hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN) * GTF_CELL_GRAN;
662 
663 	h_fp = h_blank / 2 - hsync;
664 
665 	fmt->type = V4L2_DV_BT_656_1120;
666 	fmt->bt.polarities = polarities;
667 	fmt->bt.width = image_width;
668 	fmt->bt.height = image_height;
669 	fmt->bt.hfrontporch = h_fp;
670 	fmt->bt.vfrontporch = v_fp;
671 	fmt->bt.hsync = hsync;
672 	fmt->bt.vsync = vsync;
673 	fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
674 
675 	if (!interlaced) {
676 		fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
677 		fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
678 	} else {
679 		fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
680 				      2 * vsync) / 2;
681 		fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
682 					2 * vsync - fmt->bt.vbackporch;
683 		fmt->bt.il_vfrontporch = v_fp;
684 		fmt->bt.il_vsync = vsync;
685 		fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
686 		fmt->bt.interlaced = V4L2_DV_INTERLACED;
687 	}
688 
689 	fmt->bt.pixelclock = pix_clk;
690 	fmt->bt.standards = V4L2_DV_BT_STD_GTF;
691 
692 	if (!default_gtf)
693 		fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
694 
695 	return true;
696 }
697 EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
698 
699 /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
700  *	0x15 and 0x16 from the EDID.
701  * @hor_landscape - byte 0x15 from the EDID.
702  * @vert_portrait - byte 0x16 from the EDID.
703  *
704  * Determines the aspect ratio from the EDID.
705  * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
706  * "Horizontal and Vertical Screen Size or Aspect Ratio"
707  */
708 struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
709 {
710 	struct v4l2_fract aspect = { 16, 9 };
711 	u8 ratio;
712 
713 	/* Nothing filled in, fallback to 16:9 */
714 	if (!hor_landscape && !vert_portrait)
715 		return aspect;
716 	/* Both filled in, so they are interpreted as the screen size in cm */
717 	if (hor_landscape && vert_portrait) {
718 		aspect.numerator = hor_landscape;
719 		aspect.denominator = vert_portrait;
720 		return aspect;
721 	}
722 	/* Only one is filled in, so interpret them as a ratio:
723 	   (val + 99) / 100 */
724 	ratio = hor_landscape | vert_portrait;
725 	/* Change some rounded values into the exact aspect ratio */
726 	if (ratio == 79) {
727 		aspect.numerator = 16;
728 		aspect.denominator = 9;
729 	} else if (ratio == 34) {
730 		aspect.numerator = 4;
731 		aspect.denominator = 3;
732 	} else if (ratio == 68) {
733 		aspect.numerator = 15;
734 		aspect.denominator = 9;
735 	} else {
736 		aspect.numerator = hor_landscape + 99;
737 		aspect.denominator = 100;
738 	}
739 	if (hor_landscape)
740 		return aspect;
741 	/* The aspect ratio is for portrait, so swap numerator and denominator */
742 	swap(aspect.denominator, aspect.numerator);
743 	return aspect;
744 }
745 EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);
746