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