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