]> Git Repo - linux.git/blob - drivers/media/v4l2-core/v4l2-dv-timings.c
MIPS: Netlogic: XLP9XX on-chip SATA support
[linux.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  * TODO: VESA defined a new version 2 of their reduced blanking
333  * formula. Support for that is currently missing in this CVT
334  * detection function.
335  */
336 bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,
337                 u32 polarities, struct v4l2_dv_timings *fmt)
338 {
339         int  v_fp, v_bp, h_fp, h_bp, hsync;
340         int  frame_width, image_height, image_width;
341         bool reduced_blanking;
342         unsigned pix_clk;
343
344         if (vsync < 4 || vsync > 7)
345                 return false;
346
347         if (polarities == V4L2_DV_VSYNC_POS_POL)
348                 reduced_blanking = false;
349         else if (polarities == V4L2_DV_HSYNC_POS_POL)
350                 reduced_blanking = true;
351         else
352                 return false;
353
354         /* Vertical */
355         if (reduced_blanking) {
356                 v_fp = CVT_RB_V_FPORCH;
357                 v_bp = (CVT_RB_MIN_V_BLANK * hfreq + 1999999) / 1000000;
358                 v_bp -= vsync + v_fp;
359
360                 if (v_bp < CVT_RB_MIN_V_BPORCH)
361                         v_bp = CVT_RB_MIN_V_BPORCH;
362         } else {
363                 v_fp = CVT_MIN_V_PORCH_RND;
364                 v_bp = (CVT_MIN_VSYNC_BP * hfreq + 1999999) / 1000000 - vsync;
365
366                 if (v_bp < CVT_MIN_V_BPORCH)
367                         v_bp = CVT_MIN_V_BPORCH;
368         }
369         image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
370
371         /* Aspect ratio based on vsync */
372         switch (vsync) {
373         case 4:
374                 image_width = (image_height * 4) / 3;
375                 break;
376         case 5:
377                 image_width = (image_height * 16) / 9;
378                 break;
379         case 6:
380                 image_width = (image_height * 16) / 10;
381                 break;
382         case 7:
383                 /* special case */
384                 if (image_height == 1024)
385                         image_width = (image_height * 5) / 4;
386                 else if (image_height == 768)
387                         image_width = (image_height * 15) / 9;
388                 else
389                         return false;
390                 break;
391         default:
392                 return false;
393         }
394
395         image_width = image_width & ~7;
396
397         /* Horizontal */
398         if (reduced_blanking) {
399                 pix_clk = (image_width + CVT_RB_H_BLANK) * hfreq;
400                 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
401
402                 h_bp = CVT_RB_H_BPORCH;
403                 hsync = CVT_RB_H_SYNC;
404                 h_fp = CVT_RB_H_BLANK - h_bp - hsync;
405
406                 frame_width = image_width + CVT_RB_H_BLANK;
407         } else {
408                 unsigned ideal_duty_cycle_per_myriad =
409                         100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq;
410                 int h_blank;
411
412                 if (ideal_duty_cycle_per_myriad < 2000)
413                         ideal_duty_cycle_per_myriad = 2000;
414
415                 h_blank = image_width * ideal_duty_cycle_per_myriad /
416                                         (10000 - ideal_duty_cycle_per_myriad);
417                 h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN;
418
419                 pix_clk = (image_width + h_blank) * hfreq;
420                 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
421
422                 h_bp = h_blank / 2;
423                 frame_width = image_width + h_blank;
424
425                 hsync = (frame_width * 8 + 50) / 100;
426                 hsync = hsync - hsync % CVT_CELL_GRAN;
427                 h_fp = h_blank - hsync - h_bp;
428         }
429
430         fmt->type = V4L2_DV_BT_656_1120;
431         fmt->bt.polarities = polarities;
432         fmt->bt.width = image_width;
433         fmt->bt.height = image_height;
434         fmt->bt.hfrontporch = h_fp;
435         fmt->bt.vfrontporch = v_fp;
436         fmt->bt.hsync = hsync;
437         fmt->bt.vsync = vsync;
438         fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
439         fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
440         fmt->bt.pixelclock = pix_clk;
441         fmt->bt.standards = V4L2_DV_BT_STD_CVT;
442         if (reduced_blanking)
443                 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
444         return true;
445 }
446 EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
447
448 /*
449  * GTF defines
450  * Based on Generalized Timing Formula Standard
451  * Version 1.1 September 2, 1999
452  */
453
454 #define GTF_PXL_CLK_GRAN        250000  /* pixel clock granularity */
455
456 #define GTF_MIN_VSYNC_BP        550     /* min time of vsync + back porch (us) */
457 #define GTF_V_FP                1       /* vertical front porch (lines) */
458 #define GTF_CELL_GRAN           8       /* character cell granularity */
459
460 /* Default */
461 #define GTF_D_M                 600     /* blanking formula gradient */
462 #define GTF_D_C                 40      /* blanking formula offset */
463 #define GTF_D_K                 128     /* blanking formula scaling factor */
464 #define GTF_D_J                 20      /* blanking formula scaling factor */
465 #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
466 #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
467
468 /* Secondary */
469 #define GTF_S_M                 3600    /* blanking formula gradient */
470 #define GTF_S_C                 40      /* blanking formula offset */
471 #define GTF_S_K                 128     /* blanking formula scaling factor */
472 #define GTF_S_J                 35      /* blanking formula scaling factor */
473 #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
474 #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
475
476 /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
477  * @frame_height - the total height of the frame (including blanking) in lines.
478  * @hfreq - the horizontal frequency in Hz.
479  * @vsync - the height of the vertical sync in lines.
480  * @polarities - the horizontal and vertical polarities (same as struct
481  *              v4l2_bt_timings polarities).
482  * @aspect - preferred aspect ratio. GTF has no method of determining the
483  *              aspect ratio in order to derive the image width from the
484  *              image height, so it has to be passed explicitly. Usually
485  *              the native screen aspect ratio is used for this. If it
486  *              is not filled in correctly, then 16:9 will be assumed.
487  * @fmt - the resulting timings.
488  *
489  * This function will attempt to detect if the given values correspond to a
490  * valid GTF format. If so, then it will return true, and fmt will be filled
491  * in with the found GTF timings.
492  */
493 bool v4l2_detect_gtf(unsigned frame_height,
494                 unsigned hfreq,
495                 unsigned vsync,
496                 u32 polarities,
497                 struct v4l2_fract aspect,
498                 struct v4l2_dv_timings *fmt)
499 {
500         int pix_clk;
501         int  v_fp, v_bp, h_fp, hsync;
502         int frame_width, image_height, image_width;
503         bool default_gtf;
504         int h_blank;
505
506         if (vsync != 3)
507                 return false;
508
509         if (polarities == V4L2_DV_VSYNC_POS_POL)
510                 default_gtf = true;
511         else if (polarities == V4L2_DV_HSYNC_POS_POL)
512                 default_gtf = false;
513         else
514                 return false;
515
516         /* Vertical */
517         v_fp = GTF_V_FP;
518         v_bp = (GTF_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
519         image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
520
521         if (aspect.numerator == 0 || aspect.denominator == 0) {
522                 aspect.numerator = 16;
523                 aspect.denominator = 9;
524         }
525         image_width = ((image_height * aspect.numerator) / aspect.denominator);
526         image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1);
527
528         /* Horizontal */
529         if (default_gtf)
530                 h_blank = ((image_width * GTF_D_C_PRIME * hfreq) -
531                                         (image_width * GTF_D_M_PRIME * 1000) +
532                         (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) / 2) /
533                         (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000);
534         else
535                 h_blank = ((image_width * GTF_S_C_PRIME * hfreq) -
536                                         (image_width * GTF_S_M_PRIME * 1000) +
537                         (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) /
538                         (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000);
539
540         h_blank = h_blank - h_blank % (2 * GTF_CELL_GRAN);
541         frame_width = image_width + h_blank;
542
543         pix_clk = (image_width + h_blank) * hfreq;
544         pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
545
546         hsync = (frame_width * 8 + 50) / 100;
547         hsync = hsync - hsync % GTF_CELL_GRAN;
548
549         h_fp = h_blank / 2 - hsync;
550
551         fmt->type = V4L2_DV_BT_656_1120;
552         fmt->bt.polarities = polarities;
553         fmt->bt.width = image_width;
554         fmt->bt.height = image_height;
555         fmt->bt.hfrontporch = h_fp;
556         fmt->bt.vfrontporch = v_fp;
557         fmt->bt.hsync = hsync;
558         fmt->bt.vsync = vsync;
559         fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
560         fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
561         fmt->bt.pixelclock = pix_clk;
562         fmt->bt.standards = V4L2_DV_BT_STD_GTF;
563         if (!default_gtf)
564                 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
565         return true;
566 }
567 EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
568
569 /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
570  *      0x15 and 0x16 from the EDID.
571  * @hor_landscape - byte 0x15 from the EDID.
572  * @vert_portrait - byte 0x16 from the EDID.
573  *
574  * Determines the aspect ratio from the EDID.
575  * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
576  * "Horizontal and Vertical Screen Size or Aspect Ratio"
577  */
578 struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
579 {
580         struct v4l2_fract aspect = { 16, 9 };
581         u32 tmp;
582         u8 ratio;
583
584         /* Nothing filled in, fallback to 16:9 */
585         if (!hor_landscape && !vert_portrait)
586                 return aspect;
587         /* Both filled in, so they are interpreted as the screen size in cm */
588         if (hor_landscape && vert_portrait) {
589                 aspect.numerator = hor_landscape;
590                 aspect.denominator = vert_portrait;
591                 return aspect;
592         }
593         /* Only one is filled in, so interpret them as a ratio:
594            (val + 99) / 100 */
595         ratio = hor_landscape | vert_portrait;
596         /* Change some rounded values into the exact aspect ratio */
597         if (ratio == 79) {
598                 aspect.numerator = 16;
599                 aspect.denominator = 9;
600         } else if (ratio == 34) {
601                 aspect.numerator = 4;
602                 aspect.numerator = 3;
603         } else if (ratio == 68) {
604                 aspect.numerator = 15;
605                 aspect.numerator = 9;
606         } else {
607                 aspect.numerator = hor_landscape + 99;
608                 aspect.denominator = 100;
609         }
610         if (hor_landscape)
611                 return aspect;
612         /* The aspect ratio is for portrait, so swap numerator and denominator */
613         tmp = aspect.denominator;
614         aspect.denominator = aspect.numerator;
615         aspect.numerator = tmp;
616         return aspect;
617 }
618 EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);
This page took 0.069756 seconds and 4 git commands to generate.