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