c2f5af7acbedb89227cbe036daa1230b5067973c
[firefly-linux-kernel-4.4.55.git] / drivers / media / v4l2-core / v4l2-dv-timings.c
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
29 static const struct v4l2_dv_timings timings[] = {
30         V4L2_DV_BT_CEA_640X480P59_94,
31         V4L2_DV_BT_CEA_720X480I59_94,
32         V4L2_DV_BT_CEA_720X480P59_94,
33         V4L2_DV_BT_CEA_720X576I50,
34         V4L2_DV_BT_CEA_720X576P50,
35         V4L2_DV_BT_CEA_1280X720P24,
36         V4L2_DV_BT_CEA_1280X720P25,
37         V4L2_DV_BT_CEA_1280X720P30,
38         V4L2_DV_BT_CEA_1280X720P50,
39         V4L2_DV_BT_CEA_1280X720P60,
40         V4L2_DV_BT_CEA_1920X1080P24,
41         V4L2_DV_BT_CEA_1920X1080P25,
42         V4L2_DV_BT_CEA_1920X1080P30,
43         V4L2_DV_BT_CEA_1920X1080I50,
44         V4L2_DV_BT_CEA_1920X1080P50,
45         V4L2_DV_BT_CEA_1920X1080I60,
46         V4L2_DV_BT_CEA_1920X1080P60,
47         V4L2_DV_BT_DMT_640X350P85,
48         V4L2_DV_BT_DMT_640X400P85,
49         V4L2_DV_BT_DMT_720X400P85,
50         V4L2_DV_BT_DMT_640X480P72,
51         V4L2_DV_BT_DMT_640X480P75,
52         V4L2_DV_BT_DMT_640X480P85,
53         V4L2_DV_BT_DMT_800X600P56,
54         V4L2_DV_BT_DMT_800X600P60,
55         V4L2_DV_BT_DMT_800X600P72,
56         V4L2_DV_BT_DMT_800X600P75,
57         V4L2_DV_BT_DMT_800X600P85,
58         V4L2_DV_BT_DMT_800X600P120_RB,
59         V4L2_DV_BT_DMT_848X480P60,
60         V4L2_DV_BT_DMT_1024X768I43,
61         V4L2_DV_BT_DMT_1024X768P60,
62         V4L2_DV_BT_DMT_1024X768P70,
63         V4L2_DV_BT_DMT_1024X768P75,
64         V4L2_DV_BT_DMT_1024X768P85,
65         V4L2_DV_BT_DMT_1024X768P120_RB,
66         V4L2_DV_BT_DMT_1152X864P75,
67         V4L2_DV_BT_DMT_1280X768P60_RB,
68         V4L2_DV_BT_DMT_1280X768P60,
69         V4L2_DV_BT_DMT_1280X768P75,
70         V4L2_DV_BT_DMT_1280X768P85,
71         V4L2_DV_BT_DMT_1280X768P120_RB,
72         V4L2_DV_BT_DMT_1280X800P60_RB,
73         V4L2_DV_BT_DMT_1280X800P60,
74         V4L2_DV_BT_DMT_1280X800P75,
75         V4L2_DV_BT_DMT_1280X800P85,
76         V4L2_DV_BT_DMT_1280X800P120_RB,
77         V4L2_DV_BT_DMT_1280X960P60,
78         V4L2_DV_BT_DMT_1280X960P85,
79         V4L2_DV_BT_DMT_1280X960P120_RB,
80         V4L2_DV_BT_DMT_1280X1024P60,
81         V4L2_DV_BT_DMT_1280X1024P75,
82         V4L2_DV_BT_DMT_1280X1024P85,
83         V4L2_DV_BT_DMT_1280X1024P120_RB,
84         V4L2_DV_BT_DMT_1360X768P60,
85         V4L2_DV_BT_DMT_1360X768P120_RB,
86         V4L2_DV_BT_DMT_1366X768P60,
87         V4L2_DV_BT_DMT_1366X768P60_RB,
88         V4L2_DV_BT_DMT_1400X1050P60_RB,
89         V4L2_DV_BT_DMT_1400X1050P60,
90         V4L2_DV_BT_DMT_1400X1050P75,
91         V4L2_DV_BT_DMT_1400X1050P85,
92         V4L2_DV_BT_DMT_1400X1050P120_RB,
93         V4L2_DV_BT_DMT_1440X900P60_RB,
94         V4L2_DV_BT_DMT_1440X900P60,
95         V4L2_DV_BT_DMT_1440X900P75,
96         V4L2_DV_BT_DMT_1440X900P85,
97         V4L2_DV_BT_DMT_1440X900P120_RB,
98         V4L2_DV_BT_DMT_1600X900P60_RB,
99         V4L2_DV_BT_DMT_1600X1200P60,
100         V4L2_DV_BT_DMT_1600X1200P65,
101         V4L2_DV_BT_DMT_1600X1200P70,
102         V4L2_DV_BT_DMT_1600X1200P75,
103         V4L2_DV_BT_DMT_1600X1200P85,
104         V4L2_DV_BT_DMT_1600X1200P120_RB,
105         V4L2_DV_BT_DMT_1680X1050P60_RB,
106         V4L2_DV_BT_DMT_1680X1050P60,
107         V4L2_DV_BT_DMT_1680X1050P75,
108         V4L2_DV_BT_DMT_1680X1050P85,
109         V4L2_DV_BT_DMT_1680X1050P120_RB,
110         V4L2_DV_BT_DMT_1792X1344P60,
111         V4L2_DV_BT_DMT_1792X1344P75,
112         V4L2_DV_BT_DMT_1792X1344P120_RB,
113         V4L2_DV_BT_DMT_1856X1392P60,
114         V4L2_DV_BT_DMT_1856X1392P75,
115         V4L2_DV_BT_DMT_1856X1392P120_RB,
116         V4L2_DV_BT_DMT_1920X1200P60_RB,
117         V4L2_DV_BT_DMT_1920X1200P60,
118         V4L2_DV_BT_DMT_1920X1200P75,
119         V4L2_DV_BT_DMT_1920X1200P85,
120         V4L2_DV_BT_DMT_1920X1200P120_RB,
121         V4L2_DV_BT_DMT_1920X1440P60,
122         V4L2_DV_BT_DMT_1920X1440P75,
123         V4L2_DV_BT_DMT_1920X1440P120_RB,
124         V4L2_DV_BT_DMT_2048X1152P60_RB,
125         V4L2_DV_BT_DMT_2560X1600P60_RB,
126         V4L2_DV_BT_DMT_2560X1600P60,
127         V4L2_DV_BT_DMT_2560X1600P75,
128         V4L2_DV_BT_DMT_2560X1600P85,
129         V4L2_DV_BT_DMT_2560X1600P120_RB,
130 };
131
132 bool v4l2_dv_valid_timings(const struct v4l2_dv_timings *t,
133                            const struct v4l2_dv_timings_cap *dvcap)
134 {
135         const struct v4l2_bt_timings *bt = &t->bt;
136         const struct v4l2_bt_timings_cap *cap = &dvcap->bt;
137         u32 caps = cap->capabilities;
138
139         if (t->type != V4L2_DV_BT_656_1120)
140                 return false;
141         if (t->type != dvcap->type ||
142             bt->height < cap->min_height ||
143             bt->height > cap->max_height ||
144             bt->width < cap->min_width ||
145             bt->width > cap->max_width ||
146             bt->pixelclock < cap->min_pixelclock ||
147             bt->pixelclock > cap->max_pixelclock ||
148             (cap->standards && !(bt->standards & cap->standards)) ||
149             (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) ||
150             (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE)))
151                 return false;
152         return true;
153 }
154 EXPORT_SYMBOL_GPL(v4l2_dv_valid_timings);
155
156 int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
157                              const struct v4l2_dv_timings_cap *cap)
158 {
159         u32 i, idx;
160
161         memset(t->reserved, 0, sizeof(t->reserved));
162         for (i = idx = 0; i < ARRAY_SIZE(timings); i++) {
163                 if (v4l2_dv_valid_timings(timings + i, cap) &&
164                     idx++ == t->index) {
165                         t->timings = timings[i];
166                         return 0;
167                 }
168         }
169         return -EINVAL;
170 }
171 EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap);
172
173 bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
174                               const struct v4l2_dv_timings_cap *cap,
175                               unsigned pclock_delta)
176 {
177         int i;
178
179         if (!v4l2_dv_valid_timings(t, cap))
180                 return false;
181
182         for (i = 0; i < ARRAY_SIZE(timings); i++) {
183                 if (v4l2_dv_valid_timings(timings + i, cap) &&
184                     v4l2_match_dv_timings(t, timings + i, pclock_delta)) {
185                         *t = timings[i];
186                         return true;
187                 }
188         }
189         return false;
190 }
191 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap);
192
193 /**
194  * v4l2_match_dv_timings - check if two timings match
195  * @t1 - compare this v4l2_dv_timings struct...
196  * @t2 - with this struct.
197  * @pclock_delta - the allowed pixelclock deviation.
198  *
199  * Compare t1 with t2 with a given margin of error for the pixelclock.
200  */
201 bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1,
202                            const struct v4l2_dv_timings *t2,
203                            unsigned pclock_delta)
204 {
205         if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
206                 return false;
207         if (t1->bt.width == t2->bt.width &&
208             t1->bt.height == t2->bt.height &&
209             t1->bt.interlaced == t2->bt.interlaced &&
210             t1->bt.polarities == t2->bt.polarities &&
211             t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
212             t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
213             t1->bt.hfrontporch == t2->bt.hfrontporch &&
214             t1->bt.vfrontporch == t2->bt.vfrontporch &&
215             t1->bt.vsync == t2->bt.vsync &&
216             t1->bt.vbackporch == t2->bt.vbackporch &&
217             (!t1->bt.interlaced ||
218                 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
219                  t1->bt.il_vsync == t2->bt.il_vsync &&
220                  t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
221                 return true;
222         return false;
223 }
224 EXPORT_SYMBOL_GPL(v4l2_match_dv_timings);
225
226 void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,
227                            const struct v4l2_dv_timings *t, bool detailed)
228 {
229         const struct v4l2_bt_timings *bt = &t->bt;
230         u32 htot, vtot;
231
232         if (t->type != V4L2_DV_BT_656_1120)
233                 return;
234
235         htot = V4L2_DV_BT_FRAME_WIDTH(bt);
236         vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
237
238         if (prefix == NULL)
239                 prefix = "";
240
241         pr_info("%s: %s%ux%u%s%u (%ux%u)\n", dev_prefix, prefix,
242                 bt->width, bt->height, bt->interlaced ? "i" : "p",
243                 (htot * vtot) > 0 ? ((u32)bt->pixelclock / (htot * vtot)) : 0,
244                 htot, vtot);
245
246         if (!detailed)
247                 return;
248
249         pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n",
250                         dev_prefix, bt->hfrontporch,
251                         (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
252                         bt->hsync, bt->hbackporch);
253         pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n",
254                         dev_prefix, bt->vfrontporch,
255                         (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
256                         bt->vsync, bt->vbackporch);
257         pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock);
258         pr_info("%s: flags (0x%x):%s%s%s%s\n", dev_prefix, bt->flags,
259                         (bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ?
260                         " REDUCED_BLANKING" : "",
261                         (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ?
262                         " CAN_REDUCE_FPS" : "",
263                         (bt->flags & V4L2_DV_FL_REDUCED_FPS) ?
264                         " REDUCED_FPS" : "",
265                         (bt->flags & V4L2_DV_FL_HALF_LINE) ?
266                         " HALF_LINE" : "");
267         pr_info("%s: standards (0x%x):%s%s%s%s\n", dev_prefix, bt->standards,
268                         (bt->standards & V4L2_DV_BT_STD_CEA861) ?  " CEA" : "",
269                         (bt->standards & V4L2_DV_BT_STD_DMT) ?  " DMT" : "",
270                         (bt->standards & V4L2_DV_BT_STD_CVT) ?  " CVT" : "",
271                         (bt->standards & V4L2_DV_BT_STD_GTF) ?  " GTF" : "");
272 }
273 EXPORT_SYMBOL_GPL(v4l2_print_dv_timings);
274
275 /*
276  * CVT defines
277  * Based on Coordinated Video Timings Standard
278  * version 1.1 September 10, 2003
279  */
280
281 #define CVT_PXL_CLK_GRAN        250000  /* pixel clock granularity */
282
283 /* Normal blanking */
284 #define CVT_MIN_V_BPORCH        7       /* lines */
285 #define CVT_MIN_V_PORCH_RND     3       /* lines */
286 #define CVT_MIN_VSYNC_BP        550     /* min time of vsync + back porch (us) */
287
288 /* Normal blanking for CVT uses GTF to calculate horizontal blanking */
289 #define CVT_CELL_GRAN           8       /* character cell granularity */
290 #define CVT_M                   600     /* blanking formula gradient */
291 #define CVT_C                   40      /* blanking formula offset */
292 #define CVT_K                   128     /* blanking formula scaling factor */
293 #define CVT_J                   20      /* blanking formula scaling factor */
294 #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
295 #define CVT_M_PRIME (CVT_K * CVT_M / 256)
296
297 /* Reduced Blanking */
298 #define CVT_RB_MIN_V_BPORCH    7       /* lines  */
299 #define CVT_RB_V_FPORCH        3       /* lines  */
300 #define CVT_RB_MIN_V_BLANK   460     /* us     */
301 #define CVT_RB_H_SYNC         32       /* pixels */
302 #define CVT_RB_H_BPORCH       80       /* pixels */
303 #define CVT_RB_H_BLANK       160       /* pixels */
304
305 /** v4l2_detect_cvt - detect if the given timings follow the CVT standard
306  * @frame_height - the total height of the frame (including blanking) in lines.
307  * @hfreq - the horizontal frequency in Hz.
308  * @vsync - the height of the vertical sync in lines.
309  * @polarities - the horizontal and vertical polarities (same as struct
310  *              v4l2_bt_timings polarities).
311  * @fmt - the resulting timings.
312  *
313  * This function will attempt to detect if the given values correspond to a
314  * valid CVT format. If so, then it will return true, and fmt will be filled
315  * in with the found CVT timings.
316  */
317 bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,
318                 u32 polarities, struct v4l2_dv_timings *fmt)
319 {
320         int  v_fp, v_bp, h_fp, h_bp, hsync;
321         int  frame_width, image_height, image_width;
322         bool reduced_blanking;
323         unsigned pix_clk;
324
325         if (vsync < 4 || vsync > 7)
326                 return false;
327
328         if (polarities == V4L2_DV_VSYNC_POS_POL)
329                 reduced_blanking = false;
330         else if (polarities == V4L2_DV_HSYNC_POS_POL)
331                 reduced_blanking = true;
332         else
333                 return false;
334
335         /* Vertical */
336         if (reduced_blanking) {
337                 v_fp = CVT_RB_V_FPORCH;
338                 v_bp = (CVT_RB_MIN_V_BLANK * hfreq + 1999999) / 1000000;
339                 v_bp -= vsync + v_fp;
340
341                 if (v_bp < CVT_RB_MIN_V_BPORCH)
342                         v_bp = CVT_RB_MIN_V_BPORCH;
343         } else {
344                 v_fp = CVT_MIN_V_PORCH_RND;
345                 v_bp = (CVT_MIN_VSYNC_BP * hfreq + 1999999) / 1000000 - vsync;
346
347                 if (v_bp < CVT_MIN_V_BPORCH)
348                         v_bp = CVT_MIN_V_BPORCH;
349         }
350         image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
351
352         /* Aspect ratio based on vsync */
353         switch (vsync) {
354         case 4:
355                 image_width = (image_height * 4) / 3;
356                 break;
357         case 5:
358                 image_width = (image_height * 16) / 9;
359                 break;
360         case 6:
361                 image_width = (image_height * 16) / 10;
362                 break;
363         case 7:
364                 /* special case */
365                 if (image_height == 1024)
366                         image_width = (image_height * 5) / 4;
367                 else if (image_height == 768)
368                         image_width = (image_height * 15) / 9;
369                 else
370                         return false;
371                 break;
372         default:
373                 return false;
374         }
375
376         image_width = image_width & ~7;
377
378         /* Horizontal */
379         if (reduced_blanking) {
380                 pix_clk = (image_width + CVT_RB_H_BLANK) * hfreq;
381                 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
382
383                 h_bp = CVT_RB_H_BPORCH;
384                 hsync = CVT_RB_H_SYNC;
385                 h_fp = CVT_RB_H_BLANK - h_bp - hsync;
386
387                 frame_width = image_width + CVT_RB_H_BLANK;
388         } else {
389                 unsigned ideal_duty_cycle_per_myriad =
390                         100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq;
391                 int h_blank;
392
393                 if (ideal_duty_cycle_per_myriad < 2000)
394                         ideal_duty_cycle_per_myriad = 2000;
395
396                 h_blank = image_width * ideal_duty_cycle_per_myriad /
397                                         (10000 - ideal_duty_cycle_per_myriad);
398                 h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN;
399
400                 pix_clk = (image_width + h_blank) * hfreq;
401                 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
402
403                 h_bp = h_blank / 2;
404                 frame_width = image_width + h_blank;
405
406                 hsync = (frame_width * 8 + 50) / 100;
407                 hsync = hsync - hsync % CVT_CELL_GRAN;
408                 h_fp = h_blank - hsync - h_bp;
409         }
410
411         fmt->type = V4L2_DV_BT_656_1120;
412         fmt->bt.polarities = polarities;
413         fmt->bt.width = image_width;
414         fmt->bt.height = image_height;
415         fmt->bt.hfrontporch = h_fp;
416         fmt->bt.vfrontporch = v_fp;
417         fmt->bt.hsync = hsync;
418         fmt->bt.vsync = vsync;
419         fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
420         fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
421         fmt->bt.pixelclock = pix_clk;
422         fmt->bt.standards = V4L2_DV_BT_STD_CVT;
423         if (reduced_blanking)
424                 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
425         return true;
426 }
427 EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
428
429 /*
430  * GTF defines
431  * Based on Generalized Timing Formula Standard
432  * Version 1.1 September 2, 1999
433  */
434
435 #define GTF_PXL_CLK_GRAN        250000  /* pixel clock granularity */
436
437 #define GTF_MIN_VSYNC_BP        550     /* min time of vsync + back porch (us) */
438 #define GTF_V_FP                1       /* vertical front porch (lines) */
439 #define GTF_CELL_GRAN           8       /* character cell granularity */
440
441 /* Default */
442 #define GTF_D_M                 600     /* blanking formula gradient */
443 #define GTF_D_C                 40      /* blanking formula offset */
444 #define GTF_D_K                 128     /* blanking formula scaling factor */
445 #define GTF_D_J                 20      /* blanking formula scaling factor */
446 #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
447 #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
448
449 /* Secondary */
450 #define GTF_S_M                 3600    /* blanking formula gradient */
451 #define GTF_S_C                 40      /* blanking formula offset */
452 #define GTF_S_K                 128     /* blanking formula scaling factor */
453 #define GTF_S_J                 35      /* blanking formula scaling factor */
454 #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
455 #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
456
457 /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
458  * @frame_height - the total height of the frame (including blanking) in lines.
459  * @hfreq - the horizontal frequency in Hz.
460  * @vsync - the height of the vertical sync in lines.
461  * @polarities - the horizontal and vertical polarities (same as struct
462  *              v4l2_bt_timings polarities).
463  * @aspect - preferred aspect ratio. GTF has no method of determining the
464  *              aspect ratio in order to derive the image width from the
465  *              image height, so it has to be passed explicitly. Usually
466  *              the native screen aspect ratio is used for this. If it
467  *              is not filled in correctly, then 16:9 will be assumed.
468  * @fmt - the resulting timings.
469  *
470  * This function will attempt to detect if the given values correspond to a
471  * valid GTF format. If so, then it will return true, and fmt will be filled
472  * in with the found GTF timings.
473  */
474 bool v4l2_detect_gtf(unsigned frame_height,
475                 unsigned hfreq,
476                 unsigned vsync,
477                 u32 polarities,
478                 struct v4l2_fract aspect,
479                 struct v4l2_dv_timings *fmt)
480 {
481         int pix_clk;
482         int  v_fp, v_bp, h_fp, hsync;
483         int frame_width, image_height, image_width;
484         bool default_gtf;
485         int h_blank;
486
487         if (vsync != 3)
488                 return false;
489
490         if (polarities == V4L2_DV_VSYNC_POS_POL)
491                 default_gtf = true;
492         else if (polarities == V4L2_DV_HSYNC_POS_POL)
493                 default_gtf = false;
494         else
495                 return false;
496
497         /* Vertical */
498         v_fp = GTF_V_FP;
499         v_bp = (GTF_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
500         image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
501
502         if (aspect.numerator == 0 || aspect.denominator == 0) {
503                 aspect.numerator = 16;
504                 aspect.denominator = 9;
505         }
506         image_width = ((image_height * aspect.numerator) / aspect.denominator);
507
508         /* Horizontal */
509         if (default_gtf)
510                 h_blank = ((image_width * GTF_D_C_PRIME * hfreq) -
511                                         (image_width * GTF_D_M_PRIME * 1000) +
512                         (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) / 2) /
513                         (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000);
514         else
515                 h_blank = ((image_width * GTF_S_C_PRIME * hfreq) -
516                                         (image_width * GTF_S_M_PRIME * 1000) +
517                         (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) /
518                         (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000);
519
520         h_blank = h_blank - h_blank % (2 * GTF_CELL_GRAN);
521         frame_width = image_width + h_blank;
522
523         pix_clk = (image_width + h_blank) * hfreq;
524         pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
525
526         hsync = (frame_width * 8 + 50) / 100;
527         hsync = hsync - hsync % GTF_CELL_GRAN;
528
529         h_fp = h_blank / 2 - hsync;
530
531         fmt->type = V4L2_DV_BT_656_1120;
532         fmt->bt.polarities = polarities;
533         fmt->bt.width = image_width;
534         fmt->bt.height = image_height;
535         fmt->bt.hfrontporch = h_fp;
536         fmt->bt.vfrontporch = v_fp;
537         fmt->bt.hsync = hsync;
538         fmt->bt.vsync = vsync;
539         fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
540         fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
541         fmt->bt.pixelclock = pix_clk;
542         fmt->bt.standards = V4L2_DV_BT_STD_GTF;
543         if (!default_gtf)
544                 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
545         return true;
546 }
547 EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
548
549 /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
550  *      0x15 and 0x16 from the EDID.
551  * @hor_landscape - byte 0x15 from the EDID.
552  * @vert_portrait - byte 0x16 from the EDID.
553  *
554  * Determines the aspect ratio from the EDID.
555  * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
556  * "Horizontal and Vertical Screen Size or Aspect Ratio"
557  */
558 struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
559 {
560         struct v4l2_fract aspect = { 16, 9 };
561         u32 tmp;
562         u8 ratio;
563
564         /* Nothing filled in, fallback to 16:9 */
565         if (!hor_landscape && !vert_portrait)
566                 return aspect;
567         /* Both filled in, so they are interpreted as the screen size in cm */
568         if (hor_landscape && vert_portrait) {
569                 aspect.numerator = hor_landscape;
570                 aspect.denominator = vert_portrait;
571                 return aspect;
572         }
573         /* Only one is filled in, so interpret them as a ratio:
574            (val + 99) / 100 */
575         ratio = hor_landscape | vert_portrait;
576         /* Change some rounded values into the exact aspect ratio */
577         if (ratio == 79) {
578                 aspect.numerator = 16;
579                 aspect.denominator = 9;
580         } else if (ratio == 34) {
581                 aspect.numerator = 4;
582                 aspect.numerator = 3;
583         } else if (ratio == 68) {
584                 aspect.numerator = 15;
585                 aspect.numerator = 9;
586         } else {
587                 aspect.numerator = hor_landscape + 99;
588                 aspect.denominator = 100;
589         }
590         if (hor_landscape)
591                 return aspect;
592         /* The aspect ratio is for portrait, so swap numerator and denominator */
593         tmp = aspect.denominator;
594         aspect.denominator = aspect.numerator;
595         aspect.numerator = tmp;
596         return aspect;
597 }
598 EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);