1 | /*
|
---|
2 | * Copyright 2005-2006 Luc Verhaegen.
|
---|
3 | *
|
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
5 | * copy of this software and associated documentation files (the "Software"),
|
---|
6 | * to deal in the Software without restriction, including without limitation
|
---|
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
---|
8 | * and/or sell copies of the Software, and to permit persons to whom the
|
---|
9 | * Software is furnished to do so, subject to the following conditions:
|
---|
10 | *
|
---|
11 | * The above copyright notice and this permission notice shall be included in
|
---|
12 | * all copies or substantial portions of the Software.
|
---|
13 | *
|
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
---|
18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
---|
19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
20 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * The reason for having this function in a file of its own is
|
---|
25 | * so that ../utils/cvt/cvt can link to it, and that xf86CVTMode
|
---|
26 | * code is shared directly.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifdef HAVE_XORG_CONFIG_H
|
---|
30 | #include <xorg-config.h>
|
---|
31 | #else
|
---|
32 | #ifdef HAVE_CONFIG_H
|
---|
33 | #include <config.h>
|
---|
34 | #endif
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #include "xf86.h"
|
---|
38 | #include "xf86Modes.h"
|
---|
39 |
|
---|
40 | #include <string.h>
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * Generate a CVT standard mode from HDisplay, VDisplay and VRefresh.
|
---|
44 | *
|
---|
45 | * These calculations are stolen from the CVT calculation spreadsheet written
|
---|
46 | * by Graham Loveridge. He seems to be claiming no copyright and there seems to
|
---|
47 | * be no license attached to this. He apparently just wants to see his name
|
---|
48 | * mentioned.
|
---|
49 | *
|
---|
50 | * This file can be found at http://www.vesa.org/Public/CVT/CVTd6r1.xls
|
---|
51 | *
|
---|
52 | * Comments and structure corresponds to the comments and structure of the xls.
|
---|
53 | * This should ease importing of future changes to the standard (not very
|
---|
54 | * likely though).
|
---|
55 | *
|
---|
56 | * About margins; i'm sure that they are to be the bit between HDisplay and
|
---|
57 | * HBlankStart, HBlankEnd and HTotal, VDisplay and VBlankStart, VBlankEnd and
|
---|
58 | * VTotal, where the overscan colour is shown. FB seems to call _all_ blanking
|
---|
59 | * outside sync "margin" for some reason. Since we prefer seeing proper
|
---|
60 | * blanking instead of the overscan colour, and since the Crtc* values will
|
---|
61 | * probably get altered after us, we will disable margins altogether. With
|
---|
62 | * these calculations, Margins will plainly expand H/VDisplay, and we don't
|
---|
63 | * want that. -- libv
|
---|
64 | *
|
---|
65 | */
|
---|
66 | DisplayModePtr
|
---|
67 | xf86CVTMode(int HDisplay, int VDisplay, float VRefresh, Bool Reduced,
|
---|
68 | Bool Interlaced)
|
---|
69 | {
|
---|
70 | DisplayModeRec *Mode = xnfcalloc(1, sizeof(DisplayModeRec));
|
---|
71 |
|
---|
72 | /* 1) top/bottom margin size (% of height) - default: 1.8 */
|
---|
73 | #define CVT_MARGIN_PERCENTAGE 1.8
|
---|
74 |
|
---|
75 | /* 2) character cell horizontal granularity (pixels) - default 8 */
|
---|
76 | #define CVT_H_GRANULARITY 8
|
---|
77 |
|
---|
78 | /* 4) Minimum vertical porch (lines) - default 3 */
|
---|
79 | #define CVT_MIN_V_PORCH 3
|
---|
80 |
|
---|
81 | /* 4) Minimum number of vertical back porch lines - default 6 */
|
---|
82 | #define CVT_MIN_V_BPORCH 6
|
---|
83 |
|
---|
84 | /* Pixel Clock step (kHz) */
|
---|
85 | #define CVT_CLOCK_STEP 250
|
---|
86 |
|
---|
87 | Bool Margins = FALSE;
|
---|
88 | float VFieldRate, HPeriod;
|
---|
89 | int HDisplayRnd, HMargin;
|
---|
90 | int VDisplayRnd, VMargin, VSync;
|
---|
91 | float Interlace; /* Please rename this */
|
---|
92 | char *tmp;
|
---|
93 |
|
---|
94 | /* CVT default is 60.0Hz */
|
---|
95 | if (!VRefresh)
|
---|
96 | VRefresh = 60.0;
|
---|
97 |
|
---|
98 | /* 1. Required field rate */
|
---|
99 | if (Interlaced)
|
---|
100 | VFieldRate = VRefresh * 2;
|
---|
101 | else
|
---|
102 | VFieldRate = VRefresh;
|
---|
103 |
|
---|
104 | /* 2. Horizontal pixels */
|
---|
105 | HDisplayRnd = HDisplay - (HDisplay % CVT_H_GRANULARITY);
|
---|
106 |
|
---|
107 | /* 3. Determine left and right borders */
|
---|
108 | if (Margins) {
|
---|
109 | /* right margin is actually exactly the same as left */
|
---|
110 | HMargin = (((float) HDisplayRnd) * CVT_MARGIN_PERCENTAGE / 100.0);
|
---|
111 | HMargin -= HMargin % CVT_H_GRANULARITY;
|
---|
112 | }
|
---|
113 | else
|
---|
114 | HMargin = 0;
|
---|
115 |
|
---|
116 | /* 4. Find total active pixels */
|
---|
117 | Mode->HDisplay = HDisplayRnd + 2 * HMargin;
|
---|
118 |
|
---|
119 | /* 5. Find number of lines per field */
|
---|
120 | if (Interlaced)
|
---|
121 | VDisplayRnd = VDisplay / 2;
|
---|
122 | else
|
---|
123 | VDisplayRnd = VDisplay;
|
---|
124 |
|
---|
125 | /* 6. Find top and bottom margins */
|
---|
126 | /* nope. */
|
---|
127 | if (Margins)
|
---|
128 | /* top and bottom margins are equal again. */
|
---|
129 | VMargin = (((float) VDisplayRnd) * CVT_MARGIN_PERCENTAGE / 100.0);
|
---|
130 | else
|
---|
131 | VMargin = 0;
|
---|
132 |
|
---|
133 | Mode->VDisplay = VDisplay + 2 * VMargin;
|
---|
134 |
|
---|
135 | /* 7. Interlace */
|
---|
136 | if (Interlaced)
|
---|
137 | Interlace = 0.5;
|
---|
138 | else
|
---|
139 | Interlace = 0.0;
|
---|
140 |
|
---|
141 | /* Determine VSync Width from aspect ratio */
|
---|
142 | if (!(VDisplay % 3) && ((VDisplay * 4 / 3) == HDisplay))
|
---|
143 | VSync = 4;
|
---|
144 | else if (!(VDisplay % 9) && ((VDisplay * 16 / 9) == HDisplay))
|
---|
145 | VSync = 5;
|
---|
146 | else if (!(VDisplay % 10) && ((VDisplay * 16 / 10) == HDisplay))
|
---|
147 | VSync = 6;
|
---|
148 | else if (!(VDisplay % 4) && ((VDisplay * 5 / 4) == HDisplay))
|
---|
149 | VSync = 7;
|
---|
150 | else if (!(VDisplay % 9) && ((VDisplay * 15 / 9) == HDisplay))
|
---|
151 | VSync = 7;
|
---|
152 | else /* Custom */
|
---|
153 | VSync = 10;
|
---|
154 |
|
---|
155 | if (!Reduced) { /* simplified GTF calculation */
|
---|
156 |
|
---|
157 | /* 4) Minimum time of vertical sync + back porch interval (µs)
|
---|
158 | * default 550.0 */
|
---|
159 | #define CVT_MIN_VSYNC_BP 550.0
|
---|
160 |
|
---|
161 | /* 3) Nominal HSync width (% of line period) - default 8 */
|
---|
162 | #define CVT_HSYNC_PERCENTAGE 8
|
---|
163 |
|
---|
164 | float HBlankPercentage;
|
---|
165 | int VSyncAndBackPorch, VBackPorch;
|
---|
166 | int HBlank;
|
---|
167 |
|
---|
168 | /* 8. Estimated Horizontal period */
|
---|
169 | HPeriod = ((float) (1000000.0 / VFieldRate - CVT_MIN_VSYNC_BP)) /
|
---|
170 | (VDisplayRnd + 2 * VMargin + CVT_MIN_V_PORCH + Interlace);
|
---|
171 |
|
---|
172 | /* 9. Find number of lines in sync + backporch */
|
---|
173 | if (((int) (CVT_MIN_VSYNC_BP / HPeriod) + 1) <
|
---|
174 | (VSync + CVT_MIN_V_PORCH))
|
---|
175 | VSyncAndBackPorch = VSync + CVT_MIN_V_PORCH;
|
---|
176 | else
|
---|
177 | VSyncAndBackPorch = (int) (CVT_MIN_VSYNC_BP / HPeriod) + 1;
|
---|
178 |
|
---|
179 | /* 10. Find number of lines in back porch */
|
---|
180 | VBackPorch = VSyncAndBackPorch - VSync;
|
---|
181 | (void) VBackPorch;
|
---|
182 |
|
---|
183 | /* 11. Find total number of lines in vertical field */
|
---|
184 | Mode->VTotal = VDisplayRnd + 2 * VMargin + VSyncAndBackPorch + Interlace
|
---|
185 | + CVT_MIN_V_PORCH;
|
---|
186 |
|
---|
187 | /* 5) Definition of Horizontal blanking time limitation */
|
---|
188 | /* Gradient (%/kHz) - default 600 */
|
---|
189 | #define CVT_M_FACTOR 600
|
---|
190 |
|
---|
191 | /* Offset (%) - default 40 */
|
---|
192 | #define CVT_C_FACTOR 40
|
---|
193 |
|
---|
194 | /* Blanking time scaling factor - default 128 */
|
---|
195 | #define CVT_K_FACTOR 128
|
---|
196 |
|
---|
197 | /* Scaling factor weighting - default 20 */
|
---|
198 | #define CVT_J_FACTOR 20
|
---|
199 |
|
---|
200 | #define CVT_M_PRIME CVT_M_FACTOR * CVT_K_FACTOR / 256
|
---|
201 | #define CVT_C_PRIME (CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
|
---|
202 | CVT_J_FACTOR
|
---|
203 |
|
---|
204 | /* 12. Find ideal blanking duty cycle from formula */
|
---|
205 | HBlankPercentage = CVT_C_PRIME - CVT_M_PRIME * HPeriod / 1000.0;
|
---|
206 |
|
---|
207 | /* 13. Blanking time */
|
---|
208 | if (HBlankPercentage < 20)
|
---|
209 | HBlankPercentage = 20;
|
---|
210 |
|
---|
211 | HBlank = Mode->HDisplay * HBlankPercentage / (100.0 - HBlankPercentage);
|
---|
212 | HBlank -= HBlank % (2 * CVT_H_GRANULARITY);
|
---|
213 |
|
---|
214 | /* 14. Find total number of pixels in a line. */
|
---|
215 | Mode->HTotal = Mode->HDisplay + HBlank;
|
---|
216 |
|
---|
217 | /* Fill in HSync values */
|
---|
218 | Mode->HSyncEnd = Mode->HDisplay + HBlank / 2;
|
---|
219 |
|
---|
220 | Mode->HSyncStart = Mode->HSyncEnd -
|
---|
221 | (Mode->HTotal * CVT_HSYNC_PERCENTAGE) / 100;
|
---|
222 | Mode->HSyncStart += CVT_H_GRANULARITY -
|
---|
223 | Mode->HSyncStart % CVT_H_GRANULARITY;
|
---|
224 |
|
---|
225 | /* Fill in VSync values */
|
---|
226 | Mode->VSyncStart = Mode->VDisplay + CVT_MIN_V_PORCH;
|
---|
227 | Mode->VSyncEnd = Mode->VSyncStart + VSync;
|
---|
228 |
|
---|
229 | }
|
---|
230 | else { /* Reduced blanking */
|
---|
231 | /* Minimum vertical blanking interval time (µs) - default 460 */
|
---|
232 | #define CVT_RB_MIN_VBLANK 460.0
|
---|
233 |
|
---|
234 | /* Fixed number of clocks for horizontal sync */
|
---|
235 | #define CVT_RB_H_SYNC 32.0
|
---|
236 |
|
---|
237 | /* Fixed number of clocks for horizontal blanking */
|
---|
238 | #define CVT_RB_H_BLANK 160.0
|
---|
239 |
|
---|
240 | /* Fixed number of lines for vertical front porch - default 3 */
|
---|
241 | #define CVT_RB_VFPORCH 3
|
---|
242 |
|
---|
243 | int VBILines;
|
---|
244 |
|
---|
245 | /* 8. Estimate Horizontal period. */
|
---|
246 | HPeriod = ((float) (1000000.0 / VFieldRate - CVT_RB_MIN_VBLANK)) /
|
---|
247 | (VDisplayRnd + 2 * VMargin);
|
---|
248 |
|
---|
249 | /* 9. Find number of lines in vertical blanking */
|
---|
250 | VBILines = ((float) CVT_RB_MIN_VBLANK) / HPeriod + 1;
|
---|
251 |
|
---|
252 | /* 10. Check if vertical blanking is sufficient */
|
---|
253 | if (VBILines < (CVT_RB_VFPORCH + VSync + CVT_MIN_V_BPORCH))
|
---|
254 | VBILines = CVT_RB_VFPORCH + VSync + CVT_MIN_V_BPORCH;
|
---|
255 |
|
---|
256 | /* 11. Find total number of lines in vertical field */
|
---|
257 | Mode->VTotal = VDisplayRnd + 2 * VMargin + Interlace + VBILines;
|
---|
258 |
|
---|
259 | /* 12. Find total number of pixels in a line */
|
---|
260 | Mode->HTotal = Mode->HDisplay + CVT_RB_H_BLANK;
|
---|
261 |
|
---|
262 | /* Fill in HSync values */
|
---|
263 | Mode->HSyncEnd = Mode->HDisplay + CVT_RB_H_BLANK / 2;
|
---|
264 | Mode->HSyncStart = Mode->HSyncEnd - CVT_RB_H_SYNC;
|
---|
265 |
|
---|
266 | /* Fill in VSync values */
|
---|
267 | Mode->VSyncStart = Mode->VDisplay + CVT_RB_VFPORCH;
|
---|
268 | Mode->VSyncEnd = Mode->VSyncStart + VSync;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* 15/13. Find pixel clock frequency (kHz for xf86) */
|
---|
272 | Mode->Clock = Mode->HTotal * 1000.0 / HPeriod;
|
---|
273 | Mode->Clock -= Mode->Clock % CVT_CLOCK_STEP;
|
---|
274 |
|
---|
275 | /* 16/14. Find actual Horizontal Frequency (kHz) */
|
---|
276 | Mode->HSync = ((float) Mode->Clock) / ((float) Mode->HTotal);
|
---|
277 |
|
---|
278 | /* 17/15. Find actual Field rate */
|
---|
279 | Mode->VRefresh = (1000.0 * ((float) Mode->Clock)) /
|
---|
280 | ((float) (Mode->HTotal * Mode->VTotal));
|
---|
281 |
|
---|
282 | /* 18/16. Find actual vertical frame frequency */
|
---|
283 | /* ignore - just set the mode flag for interlaced */
|
---|
284 | if (Interlaced)
|
---|
285 | Mode->VTotal *= 2;
|
---|
286 |
|
---|
287 | XNFasprintf(&tmp, "%dx%d", HDisplay, VDisplay);
|
---|
288 | Mode->name = tmp;
|
---|
289 |
|
---|
290 | if (Reduced)
|
---|
291 | Mode->Flags |= V_PHSYNC | V_NVSYNC;
|
---|
292 | else
|
---|
293 | Mode->Flags |= V_NHSYNC | V_PVSYNC;
|
---|
294 |
|
---|
295 | if (Interlaced)
|
---|
296 | Mode->Flags |= V_INTERLACE;
|
---|
297 |
|
---|
298 | return Mode;
|
---|
299 | }
|
---|