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