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) && | |
ef1ed8f5 | 184 | v4l2_match_dv_timings(t, timings + i, pclock_delta)) { |
b18787ed HV |
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 | /** | |
ef1ed8f5 | 194 | * v4l2_match_dv_timings - check if two timings match |
25764158 HV |
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 | */ | |
ef1ed8f5 HV |
201 | bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1, |
202 | const struct v4l2_dv_timings *t2, | |
203 | unsigned pclock_delta) | |
25764158 HV |
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 | } | |
ef1ed8f5 | 224 | EXPORT_SYMBOL_GPL(v4l2_match_dv_timings); |
25764158 | 225 | |
0216dc2f HV |
226 | void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix, |
227 | const struct v4l2_dv_timings *t, bool detailed) | |
228 | { | |
229 | const struct v4l2_bt_timings *bt = &t->bt; | |
230 | u32 htot, vtot; | |
231 | ||
232 | if (t->type != V4L2_DV_BT_656_1120) | |
233 | return; | |
234 | ||
235 | htot = V4L2_DV_BT_FRAME_WIDTH(bt); | |
236 | vtot = V4L2_DV_BT_FRAME_HEIGHT(bt); | |
237 | ||
238 | if (prefix == NULL) | |
239 | prefix = ""; | |
240 | ||
241 | pr_info("%s: %s%ux%u%s%u (%ux%u)\n", dev_prefix, prefix, | |
242 | bt->width, bt->height, bt->interlaced ? "i" : "p", | |
243 | (htot * vtot) > 0 ? ((u32)bt->pixelclock / (htot * vtot)) : 0, | |
244 | htot, vtot); | |
245 | ||
246 | if (!detailed) | |
247 | return; | |
248 | ||
249 | pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n", | |
250 | dev_prefix, bt->hfrontporch, | |
251 | (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-", | |
252 | bt->hsync, bt->hbackporch); | |
253 | pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n", | |
254 | dev_prefix, bt->vfrontporch, | |
255 | (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-", | |
256 | bt->vsync, bt->vbackporch); | |
257 | pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock); | |
258 | pr_info("%s: flags (0x%x):%s%s%s%s\n", dev_prefix, bt->flags, | |
259 | (bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ? | |
260 | " REDUCED_BLANKING" : "", | |
261 | (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ? | |
262 | " CAN_REDUCE_FPS" : "", | |
263 | (bt->flags & V4L2_DV_FL_REDUCED_FPS) ? | |
264 | " REDUCED_FPS" : "", | |
265 | (bt->flags & V4L2_DV_FL_HALF_LINE) ? | |
266 | " HALF_LINE" : ""); | |
267 | pr_info("%s: standards (0x%x):%s%s%s%s\n", dev_prefix, bt->standards, | |
268 | (bt->standards & V4L2_DV_BT_STD_CEA861) ? " CEA" : "", | |
269 | (bt->standards & V4L2_DV_BT_STD_DMT) ? " DMT" : "", | |
270 | (bt->standards & V4L2_DV_BT_STD_CVT) ? " CVT" : "", | |
271 | (bt->standards & V4L2_DV_BT_STD_GTF) ? " GTF" : ""); | |
272 | } | |
273 | EXPORT_SYMBOL_GPL(v4l2_print_dv_timings); | |
274 | ||
25764158 HV |
275 | /* |
276 | * CVT defines | |
277 | * Based on Coordinated Video Timings Standard | |
278 | * version 1.1 September 10, 2003 | |
279 | */ | |
280 | ||
281 | #define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */ | |
282 | ||
283 | /* Normal blanking */ | |
284 | #define CVT_MIN_V_BPORCH 7 /* lines */ | |
285 | #define CVT_MIN_V_PORCH_RND 3 /* lines */ | |
286 | #define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */ | |
287 | ||
288 | /* Normal blanking for CVT uses GTF to calculate horizontal blanking */ | |
289 | #define CVT_CELL_GRAN 8 /* character cell granularity */ | |
290 | #define CVT_M 600 /* blanking formula gradient */ | |
291 | #define CVT_C 40 /* blanking formula offset */ | |
292 | #define CVT_K 128 /* blanking formula scaling factor */ | |
293 | #define CVT_J 20 /* blanking formula scaling factor */ | |
294 | #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J) | |
295 | #define CVT_M_PRIME (CVT_K * CVT_M / 256) | |
296 | ||
297 | /* Reduced Blanking */ | |
298 | #define CVT_RB_MIN_V_BPORCH 7 /* lines */ | |
299 | #define CVT_RB_V_FPORCH 3 /* lines */ | |
300 | #define CVT_RB_MIN_V_BLANK 460 /* us */ | |
301 | #define CVT_RB_H_SYNC 32 /* pixels */ | |
302 | #define CVT_RB_H_BPORCH 80 /* pixels */ | |
303 | #define CVT_RB_H_BLANK 160 /* pixels */ | |
304 | ||
305 | /** v4l2_detect_cvt - detect if the given timings follow the CVT standard | |
306 | * @frame_height - the total height of the frame (including blanking) in lines. | |
307 | * @hfreq - the horizontal frequency in Hz. | |
308 | * @vsync - the height of the vertical sync in lines. | |
309 | * @polarities - the horizontal and vertical polarities (same as struct | |
310 | * v4l2_bt_timings polarities). | |
311 | * @fmt - the resulting timings. | |
312 | * | |
313 | * This function will attempt to detect if the given values correspond to a | |
314 | * valid CVT format. If so, then it will return true, and fmt will be filled | |
315 | * in with the found CVT timings. | |
316 | */ | |
317 | bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync, | |
318 | u32 polarities, struct v4l2_dv_timings *fmt) | |
319 | { | |
320 | int v_fp, v_bp, h_fp, h_bp, hsync; | |
321 | int frame_width, image_height, image_width; | |
322 | bool reduced_blanking; | |
323 | unsigned pix_clk; | |
324 | ||
325 | if (vsync < 4 || vsync > 7) | |
326 | return false; | |
327 | ||
328 | if (polarities == V4L2_DV_VSYNC_POS_POL) | |
329 | reduced_blanking = false; | |
330 | else if (polarities == V4L2_DV_HSYNC_POS_POL) | |
331 | reduced_blanking = true; | |
332 | else | |
333 | return false; | |
334 | ||
335 | /* Vertical */ | |
336 | if (reduced_blanking) { | |
337 | v_fp = CVT_RB_V_FPORCH; | |
c3e75c7d | 338 | v_bp = (CVT_RB_MIN_V_BLANK * hfreq + 1999999) / 1000000; |
25764158 HV |
339 | v_bp -= vsync + v_fp; |
340 | ||
341 | if (v_bp < CVT_RB_MIN_V_BPORCH) | |
342 | v_bp = CVT_RB_MIN_V_BPORCH; | |
343 | } else { | |
344 | v_fp = CVT_MIN_V_PORCH_RND; | |
c3e75c7d | 345 | v_bp = (CVT_MIN_VSYNC_BP * hfreq + 1999999) / 1000000 - vsync; |
25764158 HV |
346 | |
347 | if (v_bp < CVT_MIN_V_BPORCH) | |
348 | v_bp = CVT_MIN_V_BPORCH; | |
349 | } | |
350 | image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1; | |
351 | ||
352 | /* Aspect ratio based on vsync */ | |
353 | switch (vsync) { | |
354 | case 4: | |
355 | image_width = (image_height * 4) / 3; | |
356 | break; | |
357 | case 5: | |
358 | image_width = (image_height * 16) / 9; | |
359 | break; | |
360 | case 6: | |
361 | image_width = (image_height * 16) / 10; | |
362 | break; | |
363 | case 7: | |
364 | /* special case */ | |
365 | if (image_height == 1024) | |
366 | image_width = (image_height * 5) / 4; | |
367 | else if (image_height == 768) | |
368 | image_width = (image_height * 15) / 9; | |
369 | else | |
370 | return false; | |
371 | break; | |
372 | default: | |
373 | return false; | |
374 | } | |
375 | ||
376 | image_width = image_width & ~7; | |
377 | ||
378 | /* Horizontal */ | |
379 | if (reduced_blanking) { | |
380 | pix_clk = (image_width + CVT_RB_H_BLANK) * hfreq; | |
381 | pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN; | |
382 | ||
383 | h_bp = CVT_RB_H_BPORCH; | |
384 | hsync = CVT_RB_H_SYNC; | |
385 | h_fp = CVT_RB_H_BLANK - h_bp - hsync; | |
386 | ||
387 | frame_width = image_width + CVT_RB_H_BLANK; | |
388 | } else { | |
c3e75c7d MB |
389 | unsigned ideal_duty_cycle_per_myriad = |
390 | 100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq; | |
25764158 | 391 | int h_blank; |
25764158 | 392 | |
c3e75c7d MB |
393 | if (ideal_duty_cycle_per_myriad < 2000) |
394 | ideal_duty_cycle_per_myriad = 2000; | |
25764158 | 395 | |
c3e75c7d MB |
396 | h_blank = image_width * ideal_duty_cycle_per_myriad / |
397 | (10000 - ideal_duty_cycle_per_myriad); | |
398 | h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN; | |
25764158 HV |
399 | |
400 | pix_clk = (image_width + h_blank) * hfreq; | |
401 | pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN; | |
402 | ||
403 | h_bp = h_blank / 2; | |
404 | frame_width = image_width + h_blank; | |
405 | ||
406 | hsync = (frame_width * 8 + 50) / 100; | |
407 | hsync = hsync - hsync % CVT_CELL_GRAN; | |
408 | h_fp = h_blank - hsync - h_bp; | |
409 | } | |
410 | ||
411 | fmt->bt.polarities = polarities; | |
412 | fmt->bt.width = image_width; | |
413 | fmt->bt.height = image_height; | |
414 | fmt->bt.hfrontporch = h_fp; | |
415 | fmt->bt.vfrontporch = v_fp; | |
416 | fmt->bt.hsync = hsync; | |
417 | fmt->bt.vsync = vsync; | |
418 | fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync; | |
419 | fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync; | |
420 | fmt->bt.pixelclock = pix_clk; | |
421 | fmt->bt.standards = V4L2_DV_BT_STD_CVT; | |
422 | if (reduced_blanking) | |
423 | fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING; | |
424 | return true; | |
425 | } | |
426 | EXPORT_SYMBOL_GPL(v4l2_detect_cvt); | |
427 | ||
428 | /* | |
429 | * GTF defines | |
430 | * Based on Generalized Timing Formula Standard | |
431 | * Version 1.1 September 2, 1999 | |
432 | */ | |
433 | ||
434 | #define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */ | |
435 | ||
436 | #define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */ | |
437 | #define GTF_V_FP 1 /* vertical front porch (lines) */ | |
438 | #define GTF_CELL_GRAN 8 /* character cell granularity */ | |
439 | ||
440 | /* Default */ | |
441 | #define GTF_D_M 600 /* blanking formula gradient */ | |
442 | #define GTF_D_C 40 /* blanking formula offset */ | |
443 | #define GTF_D_K 128 /* blanking formula scaling factor */ | |
444 | #define GTF_D_J 20 /* blanking formula scaling factor */ | |
445 | #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J) | |
446 | #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256) | |
447 | ||
448 | /* Secondary */ | |
449 | #define GTF_S_M 3600 /* blanking formula gradient */ | |
450 | #define GTF_S_C 40 /* blanking formula offset */ | |
451 | #define GTF_S_K 128 /* blanking formula scaling factor */ | |
452 | #define GTF_S_J 35 /* blanking formula scaling factor */ | |
453 | #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J) | |
454 | #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256) | |
455 | ||
456 | /** v4l2_detect_gtf - detect if the given timings follow the GTF standard | |
457 | * @frame_height - the total height of the frame (including blanking) in lines. | |
458 | * @hfreq - the horizontal frequency in Hz. | |
459 | * @vsync - the height of the vertical sync in lines. | |
460 | * @polarities - the horizontal and vertical polarities (same as struct | |
461 | * v4l2_bt_timings polarities). | |
462 | * @aspect - preferred aspect ratio. GTF has no method of determining the | |
463 | * aspect ratio in order to derive the image width from the | |
464 | * image height, so it has to be passed explicitly. Usually | |
465 | * the native screen aspect ratio is used for this. If it | |
466 | * is not filled in correctly, then 16:9 will be assumed. | |
467 | * @fmt - the resulting timings. | |
468 | * | |
469 | * This function will attempt to detect if the given values correspond to a | |
470 | * valid GTF format. If so, then it will return true, and fmt will be filled | |
471 | * in with the found GTF timings. | |
472 | */ | |
473 | bool v4l2_detect_gtf(unsigned frame_height, | |
474 | unsigned hfreq, | |
475 | unsigned vsync, | |
476 | u32 polarities, | |
477 | struct v4l2_fract aspect, | |
478 | struct v4l2_dv_timings *fmt) | |
479 | { | |
480 | int pix_clk; | |
481 | int v_fp, v_bp, h_fp, hsync; | |
482 | int frame_width, image_height, image_width; | |
483 | bool default_gtf; | |
484 | int h_blank; | |
485 | ||
486 | if (vsync != 3) | |
487 | return false; | |
488 | ||
489 | if (polarities == V4L2_DV_VSYNC_POS_POL) | |
490 | default_gtf = true; | |
491 | else if (polarities == V4L2_DV_HSYNC_POS_POL) | |
492 | default_gtf = false; | |
493 | else | |
494 | return false; | |
495 | ||
496 | /* Vertical */ | |
497 | v_fp = GTF_V_FP; | |
498 | v_bp = (GTF_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync; | |
499 | image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1; | |
500 | ||
501 | if (aspect.numerator == 0 || aspect.denominator == 0) { | |
502 | aspect.numerator = 16; | |
503 | aspect.denominator = 9; | |
504 | } | |
505 | image_width = ((image_height * aspect.numerator) / aspect.denominator); | |
506 | ||
507 | /* Horizontal */ | |
508 | if (default_gtf) | |
509 | h_blank = ((image_width * GTF_D_C_PRIME * hfreq) - | |
510 | (image_width * GTF_D_M_PRIME * 1000) + | |
511 | (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) / 2) / | |
512 | (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000); | |
513 | else | |
514 | h_blank = ((image_width * GTF_S_C_PRIME * hfreq) - | |
515 | (image_width * GTF_S_M_PRIME * 1000) + | |
516 | (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) / | |
517 | (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000); | |
518 | ||
519 | h_blank = h_blank - h_blank % (2 * GTF_CELL_GRAN); | |
520 | frame_width = image_width + h_blank; | |
521 | ||
522 | pix_clk = (image_width + h_blank) * hfreq; | |
523 | pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN; | |
524 | ||
525 | hsync = (frame_width * 8 + 50) / 100; | |
526 | hsync = hsync - hsync % GTF_CELL_GRAN; | |
527 | ||
528 | h_fp = h_blank / 2 - hsync; | |
529 | ||
530 | fmt->bt.polarities = polarities; | |
531 | fmt->bt.width = image_width; | |
532 | fmt->bt.height = image_height; | |
533 | fmt->bt.hfrontporch = h_fp; | |
534 | fmt->bt.vfrontporch = v_fp; | |
535 | fmt->bt.hsync = hsync; | |
536 | fmt->bt.vsync = vsync; | |
537 | fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync; | |
538 | fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync; | |
539 | fmt->bt.pixelclock = pix_clk; | |
540 | fmt->bt.standards = V4L2_DV_BT_STD_GTF; | |
541 | if (!default_gtf) | |
542 | fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING; | |
543 | return true; | |
544 | } | |
545 | EXPORT_SYMBOL_GPL(v4l2_detect_gtf); | |
546 | ||
547 | /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes | |
548 | * 0x15 and 0x16 from the EDID. | |
549 | * @hor_landscape - byte 0x15 from the EDID. | |
550 | * @vert_portrait - byte 0x16 from the EDID. | |
551 | * | |
552 | * Determines the aspect ratio from the EDID. | |
553 | * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2: | |
554 | * "Horizontal and Vertical Screen Size or Aspect Ratio" | |
555 | */ | |
556 | struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait) | |
557 | { | |
558 | struct v4l2_fract aspect = { 16, 9 }; | |
559 | u32 tmp; | |
560 | u8 ratio; | |
561 | ||
562 | /* Nothing filled in, fallback to 16:9 */ | |
563 | if (!hor_landscape && !vert_portrait) | |
564 | return aspect; | |
565 | /* Both filled in, so they are interpreted as the screen size in cm */ | |
566 | if (hor_landscape && vert_portrait) { | |
567 | aspect.numerator = hor_landscape; | |
568 | aspect.denominator = vert_portrait; | |
569 | return aspect; | |
570 | } | |
571 | /* Only one is filled in, so interpret them as a ratio: | |
572 | (val + 99) / 100 */ | |
573 | ratio = hor_landscape | vert_portrait; | |
574 | /* Change some rounded values into the exact aspect ratio */ | |
575 | if (ratio == 79) { | |
576 | aspect.numerator = 16; | |
577 | aspect.denominator = 9; | |
578 | } else if (ratio == 34) { | |
579 | aspect.numerator = 4; | |
580 | aspect.numerator = 3; | |
581 | } else if (ratio == 68) { | |
582 | aspect.numerator = 15; | |
583 | aspect.numerator = 9; | |
584 | } else { | |
585 | aspect.numerator = hor_landscape + 99; | |
586 | aspect.denominator = 100; | |
587 | } | |
588 | if (hor_landscape) | |
589 | return aspect; | |
590 | /* The aspect ratio is for portrait, so swap numerator and denominator */ | |
591 | tmp = aspect.denominator; | |
592 | aspect.denominator = aspect.numerator; | |
593 | aspect.numerator = tmp; | |
594 | return aspect; | |
595 | } | |
596 | EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio); |