GNU Linux-libre 4.14.332-gnu1
[releases.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/rational.h>
26 #include <linux/videodev2.h>
27 #include <linux/v4l2-dv-timings.h>
28 #include <media/v4l2-dv-timings.h>
29 #include <linux/math64.h>
30
31 MODULE_AUTHOR("Hans Verkuil");
32 MODULE_DESCRIPTION("V4L2 DV Timings Helper Functions");
33 MODULE_LICENSE("GPL");
34
35 const struct v4l2_dv_timings v4l2_dv_timings_presets[] = {
36         V4L2_DV_BT_CEA_640X480P59_94,
37         V4L2_DV_BT_CEA_720X480I59_94,
38         V4L2_DV_BT_CEA_720X480P59_94,
39         V4L2_DV_BT_CEA_720X576I50,
40         V4L2_DV_BT_CEA_720X576P50,
41         V4L2_DV_BT_CEA_1280X720P24,
42         V4L2_DV_BT_CEA_1280X720P25,
43         V4L2_DV_BT_CEA_1280X720P30,
44         V4L2_DV_BT_CEA_1280X720P50,
45         V4L2_DV_BT_CEA_1280X720P60,
46         V4L2_DV_BT_CEA_1920X1080P24,
47         V4L2_DV_BT_CEA_1920X1080P25,
48         V4L2_DV_BT_CEA_1920X1080P30,
49         V4L2_DV_BT_CEA_1920X1080I50,
50         V4L2_DV_BT_CEA_1920X1080P50,
51         V4L2_DV_BT_CEA_1920X1080I60,
52         V4L2_DV_BT_CEA_1920X1080P60,
53         V4L2_DV_BT_DMT_640X350P85,
54         V4L2_DV_BT_DMT_640X400P85,
55         V4L2_DV_BT_DMT_720X400P85,
56         V4L2_DV_BT_DMT_640X480P72,
57         V4L2_DV_BT_DMT_640X480P75,
58         V4L2_DV_BT_DMT_640X480P85,
59         V4L2_DV_BT_DMT_800X600P56,
60         V4L2_DV_BT_DMT_800X600P60,
61         V4L2_DV_BT_DMT_800X600P72,
62         V4L2_DV_BT_DMT_800X600P75,
63         V4L2_DV_BT_DMT_800X600P85,
64         V4L2_DV_BT_DMT_800X600P120_RB,
65         V4L2_DV_BT_DMT_848X480P60,
66         V4L2_DV_BT_DMT_1024X768I43,
67         V4L2_DV_BT_DMT_1024X768P60,
68         V4L2_DV_BT_DMT_1024X768P70,
69         V4L2_DV_BT_DMT_1024X768P75,
70         V4L2_DV_BT_DMT_1024X768P85,
71         V4L2_DV_BT_DMT_1024X768P120_RB,
72         V4L2_DV_BT_DMT_1152X864P75,
73         V4L2_DV_BT_DMT_1280X768P60_RB,
74         V4L2_DV_BT_DMT_1280X768P60,
75         V4L2_DV_BT_DMT_1280X768P75,
76         V4L2_DV_BT_DMT_1280X768P85,
77         V4L2_DV_BT_DMT_1280X768P120_RB,
78         V4L2_DV_BT_DMT_1280X800P60_RB,
79         V4L2_DV_BT_DMT_1280X800P60,
80         V4L2_DV_BT_DMT_1280X800P75,
81         V4L2_DV_BT_DMT_1280X800P85,
82         V4L2_DV_BT_DMT_1280X800P120_RB,
83         V4L2_DV_BT_DMT_1280X960P60,
84         V4L2_DV_BT_DMT_1280X960P85,
85         V4L2_DV_BT_DMT_1280X960P120_RB,
86         V4L2_DV_BT_DMT_1280X1024P60,
87         V4L2_DV_BT_DMT_1280X1024P75,
88         V4L2_DV_BT_DMT_1280X1024P85,
89         V4L2_DV_BT_DMT_1280X1024P120_RB,
90         V4L2_DV_BT_DMT_1360X768P60,
91         V4L2_DV_BT_DMT_1360X768P120_RB,
92         V4L2_DV_BT_DMT_1366X768P60,
93         V4L2_DV_BT_DMT_1366X768P60_RB,
94         V4L2_DV_BT_DMT_1400X1050P60_RB,
95         V4L2_DV_BT_DMT_1400X1050P60,
96         V4L2_DV_BT_DMT_1400X1050P75,
97         V4L2_DV_BT_DMT_1400X1050P85,
98         V4L2_DV_BT_DMT_1400X1050P120_RB,
99         V4L2_DV_BT_DMT_1440X900P60_RB,
100         V4L2_DV_BT_DMT_1440X900P60,
101         V4L2_DV_BT_DMT_1440X900P75,
102         V4L2_DV_BT_DMT_1440X900P85,
103         V4L2_DV_BT_DMT_1440X900P120_RB,
104         V4L2_DV_BT_DMT_1600X900P60_RB,
105         V4L2_DV_BT_DMT_1600X1200P60,
106         V4L2_DV_BT_DMT_1600X1200P65,
107         V4L2_DV_BT_DMT_1600X1200P70,
108         V4L2_DV_BT_DMT_1600X1200P75,
109         V4L2_DV_BT_DMT_1600X1200P85,
110         V4L2_DV_BT_DMT_1600X1200P120_RB,
111         V4L2_DV_BT_DMT_1680X1050P60_RB,
112         V4L2_DV_BT_DMT_1680X1050P60,
113         V4L2_DV_BT_DMT_1680X1050P75,
114         V4L2_DV_BT_DMT_1680X1050P85,
115         V4L2_DV_BT_DMT_1680X1050P120_RB,
116         V4L2_DV_BT_DMT_1792X1344P60,
117         V4L2_DV_BT_DMT_1792X1344P75,
118         V4L2_DV_BT_DMT_1792X1344P120_RB,
119         V4L2_DV_BT_DMT_1856X1392P60,
120         V4L2_DV_BT_DMT_1856X1392P75,
121         V4L2_DV_BT_DMT_1856X1392P120_RB,
122         V4L2_DV_BT_DMT_1920X1200P60_RB,
123         V4L2_DV_BT_DMT_1920X1200P60,
124         V4L2_DV_BT_DMT_1920X1200P75,
125         V4L2_DV_BT_DMT_1920X1200P85,
126         V4L2_DV_BT_DMT_1920X1200P120_RB,
127         V4L2_DV_BT_DMT_1920X1440P60,
128         V4L2_DV_BT_DMT_1920X1440P75,
129         V4L2_DV_BT_DMT_1920X1440P120_RB,
130         V4L2_DV_BT_DMT_2048X1152P60_RB,
131         V4L2_DV_BT_DMT_2560X1600P60_RB,
132         V4L2_DV_BT_DMT_2560X1600P60,
133         V4L2_DV_BT_DMT_2560X1600P75,
134         V4L2_DV_BT_DMT_2560X1600P85,
135         V4L2_DV_BT_DMT_2560X1600P120_RB,
136         V4L2_DV_BT_CEA_3840X2160P24,
137         V4L2_DV_BT_CEA_3840X2160P25,
138         V4L2_DV_BT_CEA_3840X2160P30,
139         V4L2_DV_BT_CEA_3840X2160P50,
140         V4L2_DV_BT_CEA_3840X2160P60,
141         V4L2_DV_BT_CEA_4096X2160P24,
142         V4L2_DV_BT_CEA_4096X2160P25,
143         V4L2_DV_BT_CEA_4096X2160P30,
144         V4L2_DV_BT_CEA_4096X2160P50,
145         V4L2_DV_BT_DMT_4096X2160P59_94_RB,
146         V4L2_DV_BT_CEA_4096X2160P60,
147         { }
148 };
149 EXPORT_SYMBOL_GPL(v4l2_dv_timings_presets);
150
151 bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t,
152                            const struct v4l2_dv_timings_cap *dvcap,
153                            v4l2_check_dv_timings_fnc fnc,
154                            void *fnc_handle)
155 {
156         const struct v4l2_bt_timings *bt = &t->bt;
157         const struct v4l2_bt_timings_cap *cap = &dvcap->bt;
158         u32 caps = cap->capabilities;
159         const u32 max_vert = 10240;
160         u32 max_hor = 3 * bt->width;
161
162         if (t->type != V4L2_DV_BT_656_1120)
163                 return false;
164         if (t->type != dvcap->type ||
165             bt->height < cap->min_height ||
166             bt->height > cap->max_height ||
167             bt->width < cap->min_width ||
168             bt->width > cap->max_width ||
169             bt->pixelclock < cap->min_pixelclock ||
170             bt->pixelclock > cap->max_pixelclock ||
171             (!(caps & V4L2_DV_BT_CAP_CUSTOM) &&
172              cap->standards && bt->standards &&
173              !(bt->standards & cap->standards)) ||
174             (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) ||
175             (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE)))
176                 return false;
177
178         /* sanity checks for the blanking timings */
179         if (!bt->interlaced &&
180             (bt->il_vbackporch || bt->il_vsync || bt->il_vfrontporch))
181                 return false;
182         /*
183          * Some video receivers cannot properly separate the frontporch,
184          * backporch and sync values, and instead they only have the total
185          * blanking. That can be assigned to any of these three fields.
186          * So just check that none of these are way out of range.
187          */
188         if (bt->hfrontporch > max_hor ||
189             bt->hsync > max_hor || bt->hbackporch > max_hor)
190                 return false;
191         if (bt->vfrontporch > max_vert ||
192             bt->vsync > max_vert || bt->vbackporch > max_vert)
193                 return false;
194         if (bt->interlaced && (bt->il_vfrontporch > max_vert ||
195             bt->il_vsync > max_vert || bt->il_vbackporch > max_vert))
196                 return false;
197         return fnc == NULL || fnc(t, fnc_handle);
198 }
199 EXPORT_SYMBOL_GPL(v4l2_valid_dv_timings);
200
201 int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
202                              const struct v4l2_dv_timings_cap *cap,
203                              v4l2_check_dv_timings_fnc fnc,
204                              void *fnc_handle)
205 {
206         u32 i, idx;
207
208         memset(t->reserved, 0, sizeof(t->reserved));
209         for (i = idx = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
210                 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
211                                           fnc, fnc_handle) &&
212                     idx++ == t->index) {
213                         t->timings = v4l2_dv_timings_presets[i];
214                         return 0;
215                 }
216         }
217         return -EINVAL;
218 }
219 EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap);
220
221 bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
222                               const struct v4l2_dv_timings_cap *cap,
223                               unsigned pclock_delta,
224                               v4l2_check_dv_timings_fnc fnc,
225                               void *fnc_handle)
226 {
227         int i;
228
229         if (!v4l2_valid_dv_timings(t, cap, fnc, fnc_handle))
230                 return false;
231
232         for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
233                 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
234                                           fnc, fnc_handle) &&
235                     v4l2_match_dv_timings(t, v4l2_dv_timings_presets + i,
236                                           pclock_delta, false)) {
237                         u32 flags = t->bt.flags & V4L2_DV_FL_REDUCED_FPS;
238
239                         *t = v4l2_dv_timings_presets[i];
240                         if (can_reduce_fps(&t->bt))
241                                 t->bt.flags |= flags;
242
243                         return true;
244                 }
245         }
246         return false;
247 }
248 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap);
249
250 bool v4l2_find_dv_timings_cea861_vic(struct v4l2_dv_timings *t, u8 vic)
251 {
252         unsigned int i;
253
254         for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
255                 const struct v4l2_bt_timings *bt =
256                         &v4l2_dv_timings_presets[i].bt;
257
258                 if ((bt->flags & V4L2_DV_FL_HAS_CEA861_VIC) &&
259                     bt->cea861_vic == vic) {
260                         *t = v4l2_dv_timings_presets[i];
261                         return true;
262                 }
263         }
264         return false;
265 }
266 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cea861_vic);
267
268 /**
269  * v4l2_match_dv_timings - check if two timings match
270  * @t1 - compare this v4l2_dv_timings struct...
271  * @t2 - with this struct.
272  * @pclock_delta - the allowed pixelclock deviation.
273  * @match_reduced_fps - if true, then fail if V4L2_DV_FL_REDUCED_FPS does not
274  * match.
275  *
276  * Compare t1 with t2 with a given margin of error for the pixelclock.
277  */
278 bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1,
279                            const struct v4l2_dv_timings *t2,
280                            unsigned pclock_delta, bool match_reduced_fps)
281 {
282         if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
283                 return false;
284         if (t1->bt.width == t2->bt.width &&
285             t1->bt.height == t2->bt.height &&
286             t1->bt.interlaced == t2->bt.interlaced &&
287             t1->bt.polarities == t2->bt.polarities &&
288             t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
289             t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
290             t1->bt.hfrontporch == t2->bt.hfrontporch &&
291             t1->bt.hsync == t2->bt.hsync &&
292             t1->bt.hbackporch == t2->bt.hbackporch &&
293             t1->bt.vfrontporch == t2->bt.vfrontporch &&
294             t1->bt.vsync == t2->bt.vsync &&
295             t1->bt.vbackporch == t2->bt.vbackporch &&
296             (!match_reduced_fps ||
297              (t1->bt.flags & V4L2_DV_FL_REDUCED_FPS) ==
298                 (t2->bt.flags & V4L2_DV_FL_REDUCED_FPS)) &&
299             (!t1->bt.interlaced ||
300                 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
301                  t1->bt.il_vsync == t2->bt.il_vsync &&
302                  t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
303                 return true;
304         return false;
305 }
306 EXPORT_SYMBOL_GPL(v4l2_match_dv_timings);
307
308 void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,
309                            const struct v4l2_dv_timings *t, bool detailed)
310 {
311         const struct v4l2_bt_timings *bt = &t->bt;
312         u32 htot, vtot;
313         u32 fps;
314
315         if (t->type != V4L2_DV_BT_656_1120)
316                 return;
317
318         htot = V4L2_DV_BT_FRAME_WIDTH(bt);
319         vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
320         if (bt->interlaced)
321                 vtot /= 2;
322
323         fps = (htot * vtot) > 0 ? div_u64((100 * (u64)bt->pixelclock),
324                                   (htot * vtot)) : 0;
325
326         if (prefix == NULL)
327                 prefix = "";
328
329         pr_info("%s: %s%ux%u%s%u.%u (%ux%u)\n", dev_prefix, prefix,
330                 bt->width, bt->height, bt->interlaced ? "i" : "p",
331                 fps / 100, fps % 100, htot, vtot);
332
333         if (!detailed)
334                 return;
335
336         pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n",
337                         dev_prefix, bt->hfrontporch,
338                         (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
339                         bt->hsync, bt->hbackporch);
340         pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n",
341                         dev_prefix, bt->vfrontporch,
342                         (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
343                         bt->vsync, bt->vbackporch);
344         if (bt->interlaced)
345                 pr_info("%s: vertical bottom field: fp = %u, %ssync = %u, bp = %u\n",
346                         dev_prefix, bt->il_vfrontporch,
347                         (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
348                         bt->il_vsync, bt->il_vbackporch);
349         pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock);
350         pr_info("%s: flags (0x%x):%s%s%s%s%s%s%s%s%s%s\n",
351                         dev_prefix, bt->flags,
352                         (bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ?
353                         " REDUCED_BLANKING" : "",
354                         ((bt->flags & V4L2_DV_FL_REDUCED_BLANKING) &&
355                          bt->vsync == 8) ? " (V2)" : "",
356                         (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ?
357                         " CAN_REDUCE_FPS" : "",
358                         (bt->flags & V4L2_DV_FL_REDUCED_FPS) ?
359                         " REDUCED_FPS" : "",
360                         (bt->flags & V4L2_DV_FL_HALF_LINE) ?
361                         " HALF_LINE" : "",
362                         (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) ?
363                         " CE_VIDEO" : "",
364                         (bt->flags & V4L2_DV_FL_FIRST_FIELD_EXTRA_LINE) ?
365                         " FIRST_FIELD_EXTRA_LINE" : "",
366                         (bt->flags & V4L2_DV_FL_HAS_PICTURE_ASPECT) ?
367                         " HAS_PICTURE_ASPECT" : "",
368                         (bt->flags & V4L2_DV_FL_HAS_CEA861_VIC) ?
369                         " HAS_CEA861_VIC" : "",
370                         (bt->flags & V4L2_DV_FL_HAS_HDMI_VIC) ?
371                         " HAS_HDMI_VIC" : "");
372         pr_info("%s: standards (0x%x):%s%s%s%s%s\n", dev_prefix, bt->standards,
373                         (bt->standards & V4L2_DV_BT_STD_CEA861) ?  " CEA" : "",
374                         (bt->standards & V4L2_DV_BT_STD_DMT) ?  " DMT" : "",
375                         (bt->standards & V4L2_DV_BT_STD_CVT) ?  " CVT" : "",
376                         (bt->standards & V4L2_DV_BT_STD_GTF) ?  " GTF" : "",
377                         (bt->standards & V4L2_DV_BT_STD_SDI) ?  " SDI" : "");
378         if (bt->flags & V4L2_DV_FL_HAS_PICTURE_ASPECT)
379                 pr_info("%s: picture aspect (hor:vert): %u:%u\n", dev_prefix,
380                         bt->picture_aspect.numerator,
381                         bt->picture_aspect.denominator);
382         if (bt->flags & V4L2_DV_FL_HAS_CEA861_VIC)
383                 pr_info("%s: CEA-861 VIC: %u\n", dev_prefix, bt->cea861_vic);
384         if (bt->flags & V4L2_DV_FL_HAS_HDMI_VIC)
385                 pr_info("%s: HDMI VIC: %u\n", dev_prefix, bt->hdmi_vic);
386 }
387 EXPORT_SYMBOL_GPL(v4l2_print_dv_timings);
388
389 struct v4l2_fract v4l2_dv_timings_aspect_ratio(const struct v4l2_dv_timings *t)
390 {
391         struct v4l2_fract ratio = { 1, 1 };
392         unsigned long n, d;
393
394         if (t->type != V4L2_DV_BT_656_1120)
395                 return ratio;
396         if (!(t->bt.flags & V4L2_DV_FL_HAS_PICTURE_ASPECT))
397                 return ratio;
398
399         ratio.numerator = t->bt.width * t->bt.picture_aspect.denominator;
400         ratio.denominator = t->bt.height * t->bt.picture_aspect.numerator;
401
402         rational_best_approximation(ratio.numerator, ratio.denominator,
403                                     ratio.numerator, ratio.denominator, &n, &d);
404         ratio.numerator = n;
405         ratio.denominator = d;
406         return ratio;
407 }
408 EXPORT_SYMBOL_GPL(v4l2_dv_timings_aspect_ratio);
409
410 /*
411  * CVT defines
412  * Based on Coordinated Video Timings Standard
413  * version 1.1 September 10, 2003
414  */
415
416 #define CVT_PXL_CLK_GRAN        250000  /* pixel clock granularity */
417 #define CVT_PXL_CLK_GRAN_RB_V2 1000     /* granularity for reduced blanking v2*/
418
419 /* Normal blanking */
420 #define CVT_MIN_V_BPORCH        7       /* lines */
421 #define CVT_MIN_V_PORCH_RND     3       /* lines */
422 #define CVT_MIN_VSYNC_BP        550     /* min time of vsync + back porch (us) */
423 #define CVT_HSYNC_PERCENT       8       /* nominal hsync as percentage of line */
424
425 /* Normal blanking for CVT uses GTF to calculate horizontal blanking */
426 #define CVT_CELL_GRAN           8       /* character cell granularity */
427 #define CVT_M                   600     /* blanking formula gradient */
428 #define CVT_C                   40      /* blanking formula offset */
429 #define CVT_K                   128     /* blanking formula scaling factor */
430 #define CVT_J                   20      /* blanking formula scaling factor */
431 #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
432 #define CVT_M_PRIME (CVT_K * CVT_M / 256)
433
434 /* Reduced Blanking */
435 #define CVT_RB_MIN_V_BPORCH    7       /* lines  */
436 #define CVT_RB_V_FPORCH        3       /* lines  */
437 #define CVT_RB_MIN_V_BLANK   460       /* us     */
438 #define CVT_RB_H_SYNC         32       /* pixels */
439 #define CVT_RB_H_BLANK       160       /* pixels */
440 /* Reduce blanking Version 2 */
441 #define CVT_RB_V2_H_BLANK     80       /* pixels */
442 #define CVT_RB_MIN_V_FPORCH    3       /* lines  */
443 #define CVT_RB_V2_MIN_V_FPORCH 1       /* lines  */
444 #define CVT_RB_V_BPORCH        6       /* lines  */
445
446 /** v4l2_detect_cvt - detect if the given timings follow the CVT standard
447  * @frame_height - the total height of the frame (including blanking) in lines.
448  * @hfreq - the horizontal frequency in Hz.
449  * @vsync - the height of the vertical sync in lines.
450  * @active_width - active width of image (does not include blanking). This
451  * information is needed only in case of version 2 of reduced blanking.
452  * In other cases, this parameter does not have any effect on timings.
453  * @polarities - the horizontal and vertical polarities (same as struct
454  *              v4l2_bt_timings polarities).
455  * @interlaced - if this flag is true, it indicates interlaced format
456  * @fmt - the resulting timings.
457  *
458  * This function will attempt to detect if the given values correspond to a
459  * valid CVT format. If so, then it will return true, and fmt will be filled
460  * in with the found CVT timings.
461  */
462 bool v4l2_detect_cvt(unsigned frame_height,
463                      unsigned hfreq,
464                      unsigned vsync,
465                      unsigned active_width,
466                      u32 polarities,
467                      bool interlaced,
468                      struct v4l2_dv_timings *fmt)
469 {
470         int  v_fp, v_bp, h_fp, h_bp, hsync;
471         int  frame_width, image_height, image_width;
472         bool reduced_blanking;
473         bool rb_v2 = false;
474         unsigned pix_clk;
475
476         if (vsync < 4 || vsync > 8)
477                 return false;
478
479         if (polarities == V4L2_DV_VSYNC_POS_POL)
480                 reduced_blanking = false;
481         else if (polarities == V4L2_DV_HSYNC_POS_POL)
482                 reduced_blanking = true;
483         else
484                 return false;
485
486         if (reduced_blanking && vsync == 8)
487                 rb_v2 = true;
488
489         if (rb_v2 && active_width == 0)
490                 return false;
491
492         if (!rb_v2 && vsync > 7)
493                 return false;
494
495         if (hfreq == 0)
496                 return false;
497
498         /* Vertical */
499         if (reduced_blanking) {
500                 if (rb_v2) {
501                         v_bp = CVT_RB_V_BPORCH;
502                         v_fp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
503                         v_fp -= vsync + v_bp;
504
505                         if (v_fp < CVT_RB_V2_MIN_V_FPORCH)
506                                 v_fp = CVT_RB_V2_MIN_V_FPORCH;
507                 } else {
508                         v_fp = CVT_RB_V_FPORCH;
509                         v_bp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
510                         v_bp -= vsync + v_fp;
511
512                         if (v_bp < CVT_RB_MIN_V_BPORCH)
513                                 v_bp = CVT_RB_MIN_V_BPORCH;
514                 }
515         } else {
516                 v_fp = CVT_MIN_V_PORCH_RND;
517                 v_bp = (CVT_MIN_VSYNC_BP * hfreq) / 1000000 + 1 - vsync;
518
519                 if (v_bp < CVT_MIN_V_BPORCH)
520                         v_bp = CVT_MIN_V_BPORCH;
521         }
522
523         if (interlaced)
524                 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
525         else
526                 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
527
528         if (image_height < 0)
529                 return false;
530
531         /* Aspect ratio based on vsync */
532         switch (vsync) {
533         case 4:
534                 image_width = (image_height * 4) / 3;
535                 break;
536         case 5:
537                 image_width = (image_height * 16) / 9;
538                 break;
539         case 6:
540                 image_width = (image_height * 16) / 10;
541                 break;
542         case 7:
543                 /* special case */
544                 if (image_height == 1024)
545                         image_width = (image_height * 5) / 4;
546                 else if (image_height == 768)
547                         image_width = (image_height * 15) / 9;
548                 else
549                         return false;
550                 break;
551         case 8:
552                 image_width = active_width;
553                 break;
554         default:
555                 return false;
556         }
557
558         if (!rb_v2)
559                 image_width = image_width & ~7;
560
561         /* Horizontal */
562         if (reduced_blanking) {
563                 int h_blank;
564                 int clk_gran;
565
566                 h_blank = rb_v2 ? CVT_RB_V2_H_BLANK : CVT_RB_H_BLANK;
567                 clk_gran = rb_v2 ? CVT_PXL_CLK_GRAN_RB_V2 : CVT_PXL_CLK_GRAN;
568
569                 pix_clk = (image_width + h_blank) * hfreq;
570                 pix_clk = (pix_clk / clk_gran) * clk_gran;
571
572                 h_bp  = h_blank / 2;
573                 hsync = CVT_RB_H_SYNC;
574                 h_fp  = h_blank - h_bp - hsync;
575
576                 frame_width = image_width + h_blank;
577         } else {
578                 unsigned ideal_duty_cycle_per_myriad =
579                         100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq;
580                 int h_blank;
581
582                 if (ideal_duty_cycle_per_myriad < 2000)
583                         ideal_duty_cycle_per_myriad = 2000;
584
585                 h_blank = image_width * ideal_duty_cycle_per_myriad /
586                                         (10000 - ideal_duty_cycle_per_myriad);
587                 h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN;
588
589                 pix_clk = (image_width + h_blank) * hfreq;
590                 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
591
592                 h_bp = h_blank / 2;
593                 frame_width = image_width + h_blank;
594
595                 hsync = frame_width * CVT_HSYNC_PERCENT / 100;
596                 hsync = (hsync / CVT_CELL_GRAN) * CVT_CELL_GRAN;
597                 h_fp = h_blank - hsync - h_bp;
598         }
599
600         fmt->type = V4L2_DV_BT_656_1120;
601         fmt->bt.polarities = polarities;
602         fmt->bt.width = image_width;
603         fmt->bt.height = image_height;
604         fmt->bt.hfrontporch = h_fp;
605         fmt->bt.vfrontporch = v_fp;
606         fmt->bt.hsync = hsync;
607         fmt->bt.vsync = vsync;
608         fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
609
610         if (!interlaced) {
611                 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
612                 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
613         } else {
614                 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
615                                       2 * vsync) / 2;
616                 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
617                                         2 * vsync - fmt->bt.vbackporch;
618                 fmt->bt.il_vfrontporch = v_fp;
619                 fmt->bt.il_vsync = vsync;
620                 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
621                 fmt->bt.interlaced = V4L2_DV_INTERLACED;
622         }
623
624         fmt->bt.pixelclock = pix_clk;
625         fmt->bt.standards = V4L2_DV_BT_STD_CVT;
626
627         if (reduced_blanking)
628                 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
629
630         return true;
631 }
632 EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
633
634 /*
635  * GTF defines
636  * Based on Generalized Timing Formula Standard
637  * Version 1.1 September 2, 1999
638  */
639
640 #define GTF_PXL_CLK_GRAN        250000  /* pixel clock granularity */
641
642 #define GTF_MIN_VSYNC_BP        550     /* min time of vsync + back porch (us) */
643 #define GTF_V_FP                1       /* vertical front porch (lines) */
644 #define GTF_CELL_GRAN           8       /* character cell granularity */
645
646 /* Default */
647 #define GTF_D_M                 600     /* blanking formula gradient */
648 #define GTF_D_C                 40      /* blanking formula offset */
649 #define GTF_D_K                 128     /* blanking formula scaling factor */
650 #define GTF_D_J                 20      /* blanking formula scaling factor */
651 #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
652 #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
653
654 /* Secondary */
655 #define GTF_S_M                 3600    /* blanking formula gradient */
656 #define GTF_S_C                 40      /* blanking formula offset */
657 #define GTF_S_K                 128     /* blanking formula scaling factor */
658 #define GTF_S_J                 35      /* blanking formula scaling factor */
659 #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
660 #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
661
662 /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
663  * @frame_height - the total height of the frame (including blanking) in lines.
664  * @hfreq - the horizontal frequency in Hz.
665  * @vsync - the height of the vertical sync in lines.
666  * @polarities - the horizontal and vertical polarities (same as struct
667  *              v4l2_bt_timings polarities).
668  * @interlaced - if this flag is true, it indicates interlaced format
669  * @aspect - preferred aspect ratio. GTF has no method of determining the
670  *              aspect ratio in order to derive the image width from the
671  *              image height, so it has to be passed explicitly. Usually
672  *              the native screen aspect ratio is used for this. If it
673  *              is not filled in correctly, then 16:9 will be assumed.
674  * @fmt - the resulting timings.
675  *
676  * This function will attempt to detect if the given values correspond to a
677  * valid GTF format. If so, then it will return true, and fmt will be filled
678  * in with the found GTF timings.
679  */
680 bool v4l2_detect_gtf(unsigned frame_height,
681                 unsigned hfreq,
682                 unsigned vsync,
683                 u32 polarities,
684                 bool interlaced,
685                 struct v4l2_fract aspect,
686                 struct v4l2_dv_timings *fmt)
687 {
688         int pix_clk;
689         int  v_fp, v_bp, h_fp, hsync;
690         int frame_width, image_height, image_width;
691         bool default_gtf;
692         int h_blank;
693
694         if (vsync != 3)
695                 return false;
696
697         if (polarities == V4L2_DV_VSYNC_POS_POL)
698                 default_gtf = true;
699         else if (polarities == V4L2_DV_HSYNC_POS_POL)
700                 default_gtf = false;
701         else
702                 return false;
703
704         if (hfreq == 0)
705                 return false;
706
707         /* Vertical */
708         v_fp = GTF_V_FP;
709         v_bp = (GTF_MIN_VSYNC_BP * hfreq + 500000) / 1000000 - vsync;
710         if (interlaced)
711                 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
712         else
713                 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
714
715         if (image_height < 0)
716                 return false;
717
718         if (aspect.numerator == 0 || aspect.denominator == 0) {
719                 aspect.numerator = 16;
720                 aspect.denominator = 9;
721         }
722         image_width = ((image_height * aspect.numerator) / aspect.denominator);
723         image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1);
724
725         /* Horizontal */
726         if (default_gtf) {
727                 u64 num;
728                 u32 den;
729
730                 num = ((image_width * GTF_D_C_PRIME * (u64)hfreq) -
731                       ((u64)image_width * GTF_D_M_PRIME * 1000));
732                 den = (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) *
733                       (2 * GTF_CELL_GRAN);
734                 h_blank = div_u64((num + (den >> 1)), den);
735                 h_blank *= (2 * GTF_CELL_GRAN);
736         } else {
737                 u64 num;
738                 u32 den;
739
740                 num = ((image_width * GTF_S_C_PRIME * (u64)hfreq) -
741                       ((u64)image_width * GTF_S_M_PRIME * 1000));
742                 den = (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) *
743                       (2 * GTF_CELL_GRAN);
744                 h_blank = div_u64((num + (den >> 1)), den);
745                 h_blank *= (2 * GTF_CELL_GRAN);
746         }
747
748         frame_width = image_width + h_blank;
749
750         pix_clk = (image_width + h_blank) * hfreq;
751         pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
752
753         hsync = (frame_width * 8 + 50) / 100;
754         hsync = ((hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN) * GTF_CELL_GRAN;
755
756         h_fp = h_blank / 2 - hsync;
757
758         fmt->type = V4L2_DV_BT_656_1120;
759         fmt->bt.polarities = polarities;
760         fmt->bt.width = image_width;
761         fmt->bt.height = image_height;
762         fmt->bt.hfrontporch = h_fp;
763         fmt->bt.vfrontporch = v_fp;
764         fmt->bt.hsync = hsync;
765         fmt->bt.vsync = vsync;
766         fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
767
768         if (!interlaced) {
769                 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
770                 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
771         } else {
772                 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
773                                       2 * vsync) / 2;
774                 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
775                                         2 * vsync - fmt->bt.vbackporch;
776                 fmt->bt.il_vfrontporch = v_fp;
777                 fmt->bt.il_vsync = vsync;
778                 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
779                 fmt->bt.interlaced = V4L2_DV_INTERLACED;
780         }
781
782         fmt->bt.pixelclock = pix_clk;
783         fmt->bt.standards = V4L2_DV_BT_STD_GTF;
784
785         if (!default_gtf)
786                 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
787
788         return true;
789 }
790 EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
791
792 /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
793  *      0x15 and 0x16 from the EDID.
794  * @hor_landscape - byte 0x15 from the EDID.
795  * @vert_portrait - byte 0x16 from the EDID.
796  *
797  * Determines the aspect ratio from the EDID.
798  * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
799  * "Horizontal and Vertical Screen Size or Aspect Ratio"
800  */
801 struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
802 {
803         struct v4l2_fract aspect = { 16, 9 };
804         u8 ratio;
805
806         /* Nothing filled in, fallback to 16:9 */
807         if (!hor_landscape && !vert_portrait)
808                 return aspect;
809         /* Both filled in, so they are interpreted as the screen size in cm */
810         if (hor_landscape && vert_portrait) {
811                 aspect.numerator = hor_landscape;
812                 aspect.denominator = vert_portrait;
813                 return aspect;
814         }
815         /* Only one is filled in, so interpret them as a ratio:
816            (val + 99) / 100 */
817         ratio = hor_landscape | vert_portrait;
818         /* Change some rounded values into the exact aspect ratio */
819         if (ratio == 79) {
820                 aspect.numerator = 16;
821                 aspect.denominator = 9;
822         } else if (ratio == 34) {
823                 aspect.numerator = 4;
824                 aspect.denominator = 3;
825         } else if (ratio == 68) {
826                 aspect.numerator = 15;
827                 aspect.denominator = 9;
828         } else {
829                 aspect.numerator = hor_landscape + 99;
830                 aspect.denominator = 100;
831         }
832         if (hor_landscape)
833                 return aspect;
834         /* The aspect ratio is for portrait, so swap numerator and denominator */
835         swap(aspect.denominator, aspect.numerator);
836         return aspect;
837 }
838 EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);