First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / common / xf86cvt.c
blobdfb6e71e41efa6888475a99b44852bcd63e3856f
1 /*
2 * Copyright 2005-2006 Luc Verhaegen.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
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.
29 #include "xf86.h"
32 * Generate a CVT standard mode from HDisplay, VDisplay and VRefresh.
34 * These calculations are stolen from the CVT calculation spreadsheet written
35 * by Graham Loveridge. He seems to be claiming no copyright and there seems to
36 * be no license attached to this. He apparently just wants to see his name
37 * mentioned.
39 * This file can be found at http://www.vesa.org/Public/CVT/CVTd6r1.xls
41 * Comments and structure corresponds to the comments and structure of the xls.
42 * This should ease importing of future changes to the standard (not very
43 * likely though).
45 * About margins; i'm sure that they are to be the bit between HDisplay and
46 * HBlankStart, HBlankEnd and HTotal, VDisplay and VBlankStart, VBlankEnd and
47 * VTotal, where the overscan colour is shown. FB seems to call _all_ blanking
48 * outside sync "margin" for some reason. Since we prefer seeing proper
49 * blanking instead of the overscan colour, and since the Crtc* values will
50 * probably get altered after us, we will disable margins altogether. With
51 * these calculations, Margins will plainly expand H/VDisplay, and we don't
52 * want that. -- libv
55 _X_EXPORT DisplayModePtr
56 xf86CVTMode(int HDisplay, int VDisplay, float VRefresh, Bool Reduced,
57 Bool Interlaced)
59 DisplayModeRec *Mode = xnfalloc(sizeof(DisplayModeRec));
61 /* 1) top/bottom margin size (% of height) - default: 1.8 */
62 #define CVT_MARGIN_PERCENTAGE 1.8
64 /* 2) character cell horizontal granularity (pixels) - default 8 */
65 #define CVT_H_GRANULARITY 8
67 /* 4) Minimum vertical porch (lines) - default 3 */
68 #define CVT_MIN_V_PORCH 3
70 /* 4) Minimum number of vertical back porch lines - default 6 */
71 #define CVT_MIN_V_BPORCH 6
73 /* Pixel Clock step (kHz) */
74 #define CVT_CLOCK_STEP 250
76 Bool Margins = FALSE;
77 float VFieldRate, HPeriod;
78 int HDisplayRnd, HMargin;
79 int VDisplayRnd, VMargin, VSync;
80 float Interlace; /* Please rename this */
82 memset(Mode, 0, sizeof(DisplayModeRec));
84 /* CVT default is 60.0Hz */
85 if (!VRefresh)
86 VRefresh = 60.0;
88 /* 1. Required field rate */
89 if (Interlaced)
90 VFieldRate = VRefresh * 2;
91 else
92 VFieldRate = VRefresh;
94 /* 2. Horizontal pixels */
95 HDisplayRnd = HDisplay - (HDisplay % CVT_H_GRANULARITY);
97 /* 3. Determine left and right borders */
98 if (Margins) {
99 /* right margin is actually exactly the same as left */
100 HMargin = (((float) HDisplayRnd) * CVT_MARGIN_PERCENTAGE / 100.0);
101 HMargin -= HMargin % CVT_H_GRANULARITY;
102 } else
103 HMargin = 0;
105 /* 4. Find total active pixels */
106 Mode->HDisplay = HDisplayRnd + 2*HMargin;
108 /* 5. Find number of lines per field */
109 if (Interlaced)
110 VDisplayRnd = VDisplay / 2;
111 else
112 VDisplayRnd = VDisplay;
114 /* 6. Find top and bottom margins */
115 /* nope. */
116 if (Margins)
117 /* top and bottom margins are equal again. */
118 VMargin = (((float) VDisplayRnd) * CVT_MARGIN_PERCENTAGE / 100.0);
119 else
120 VMargin = 0;
122 Mode->VDisplay = VDisplay + 2*VMargin;
124 /* 7. Interlace */
125 if (Interlaced)
126 Interlace = 0.5;
127 else
128 Interlace = 0.0;
130 /* Determine VSync Width from aspect ratio */
131 if (!(VDisplay % 3) && ((VDisplay * 4 / 3) == HDisplay))
132 VSync = 4;
133 else if (!(VDisplay % 9) && ((VDisplay * 16 / 9) == HDisplay))
134 VSync = 5;
135 else if (!(VDisplay % 10) && ((VDisplay * 16 / 10) == HDisplay))
136 VSync = 6;
137 else if (!(VDisplay % 4) && ((VDisplay * 5 / 4) == HDisplay))
138 VSync = 7;
139 else if (!(VDisplay % 9) && ((VDisplay * 15 / 9) == HDisplay))
140 VSync = 7;
141 else /* Custom */
142 VSync = 10;
144 if (!Reduced) { /* simplified GTF calculation */
146 /* 4) Minimum time of vertical sync + back porch interval (µs)
147 * default 550.0 */
148 #define CVT_MIN_VSYNC_BP 550.0
150 /* 3) Nominal HSync width (% of line period) - default 8 */
151 #define CVT_HSYNC_PERCENTAGE 8
153 float HBlankPercentage;
154 int VSyncAndBackPorch, VBackPorch;
155 int HBlank;
157 /* 8. Estimated Horizontal period */
158 HPeriod = ((float) (1000000.0 / VFieldRate - CVT_MIN_VSYNC_BP)) /
159 (VDisplayRnd + 2 * VMargin + CVT_MIN_V_PORCH + Interlace);
161 /* 9. Find number of lines in sync + backporch */
162 if (((int)(CVT_MIN_VSYNC_BP / HPeriod) + 1) < (VSync + CVT_MIN_V_PORCH))
163 VSyncAndBackPorch = VSync + CVT_MIN_V_PORCH;
164 else
165 VSyncAndBackPorch = (int)(CVT_MIN_VSYNC_BP / HPeriod) + 1;
167 /* 10. Find number of lines in back porch */
168 VBackPorch = VSyncAndBackPorch - VSync;
170 /* 11. Find total number of lines in vertical field */
171 Mode->VTotal = VDisplayRnd + 2 * VMargin + VSyncAndBackPorch + Interlace
172 + CVT_MIN_V_PORCH;
174 /* 5) Definition of Horizontal blanking time limitation */
175 /* Gradient (%/kHz) - default 600 */
176 #define CVT_M_FACTOR 600
178 /* Offset (%) - default 40 */
179 #define CVT_C_FACTOR 40
181 /* Blanking time scaling factor - default 128 */
182 #define CVT_K_FACTOR 128
184 /* Scaling factor weighting - default 20 */
185 #define CVT_J_FACTOR 20
187 #define CVT_M_PRIME CVT_M_FACTOR * CVT_K_FACTOR / 256
188 #define CVT_C_PRIME (CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
189 CVT_J_FACTOR
191 /* 12. Find ideal blanking duty cycle from formula */
192 HBlankPercentage = CVT_C_PRIME - CVT_M_PRIME * HPeriod/1000.0;
194 /* 13. Blanking time */
195 if (HBlankPercentage < 20)
196 HBlankPercentage = 20;
198 HBlank = Mode->HDisplay * HBlankPercentage/(100.0 - HBlankPercentage);
199 HBlank -= HBlank % (2*CVT_H_GRANULARITY);
201 /* 14. Find total number of pixels in a line. */
202 Mode->HTotal = Mode->HDisplay + HBlank;
204 /* Fill in HSync values */
205 Mode->HSyncEnd = Mode->HDisplay + HBlank / 2;
207 Mode->HSyncStart = Mode->HSyncEnd -
208 (Mode->HTotal * CVT_HSYNC_PERCENTAGE) / 100;
209 Mode->HSyncStart += CVT_H_GRANULARITY -
210 Mode->HSyncStart % CVT_H_GRANULARITY;
212 /* Fill in VSync values */
213 Mode->VSyncStart = Mode->VDisplay + CVT_MIN_V_PORCH;
214 Mode->VSyncEnd = Mode->VSyncStart + VSync;
216 } else { /* Reduced blanking */
217 /* Minimum vertical blanking interval time (µs) - default 460 */
218 #define CVT_RB_MIN_VBLANK 460.0
220 /* Fixed number of clocks for horizontal sync */
221 #define CVT_RB_H_SYNC 32.0
223 /* Fixed number of clocks for horizontal blanking */
224 #define CVT_RB_H_BLANK 160.0
226 /* Fixed number of lines for vertical front porch - default 3 */
227 #define CVT_RB_VFPORCH 3
229 int VBILines;
231 /* 8. Estimate Horizontal period. */
232 HPeriod = ((float) (1000000.0 / VFieldRate - CVT_RB_MIN_VBLANK)) /
233 (VDisplayRnd + 2*VMargin);
235 /* 9. Find number of lines in vertical blanking */
236 VBILines = ((float) CVT_RB_MIN_VBLANK) / HPeriod + 1;
238 /* 10. Check if vertical blanking is sufficient */
239 if (VBILines < (CVT_RB_VFPORCH + VSync + CVT_MIN_V_BPORCH))
240 VBILines = CVT_RB_VFPORCH + VSync + CVT_MIN_V_BPORCH;
242 /* 11. Find total number of lines in vertical field */
243 Mode->VTotal = VDisplayRnd + 2 * VMargin + Interlace + VBILines;
245 /* 12. Find total number of pixels in a line */
246 Mode->HTotal = Mode->HDisplay + CVT_RB_H_BLANK;
248 /* Fill in HSync values */
249 Mode->HSyncEnd = Mode->HDisplay + CVT_RB_H_BLANK / 2;
250 Mode->HSyncStart = Mode->HSyncEnd - CVT_RB_H_SYNC;
252 /* Fill in VSync values */
253 Mode->VSyncStart = Mode->VDisplay + CVT_RB_VFPORCH;
254 Mode->VSyncEnd = Mode->VSyncStart + VSync;
257 /* 15/13. Find pixel clock frequency (kHz for xf86) */
258 Mode->Clock = Mode->HTotal * 1000.0 / HPeriod;
259 Mode->Clock -= Mode->Clock % CVT_CLOCK_STEP;
261 /* 16/14. Find actual Horizontal Frequency (kHz) */
262 Mode->HSync = ((float) Mode->Clock) / ((float) Mode->HTotal);
264 /* 17/15. Find actual Field rate */
265 Mode->VRefresh = (1000.0 * ((float) Mode->Clock)) /
266 ((float) (Mode->HTotal * Mode->VTotal));
268 /* 18/16. Find actual vertical frame frequency */
269 /* ignore - just set the mode flag for interlaced */
270 if (Interlaced)
271 Mode->VTotal *= 2;
274 char Name[256];
275 Name[0] = 0;
277 snprintf(Name, 256, "%dx%d", HDisplay, VDisplay);
279 Mode->name = xnfalloc(strlen(Name) + 1);
280 memcpy(Mode->name, Name, strlen(Name) + 1);
283 if (Reduced)
284 Mode->Flags |= V_PHSYNC | V_NVSYNC;
285 else
286 Mode->Flags |= V_NHSYNC | V_PVSYNC;
288 if (Interlaced)
289 Mode->Flags |= V_INTERLACE;
291 return Mode;