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