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 * @file This is a copy of xf86cvt.c from the X Server, for compatibility with
25 * old servers (pre-1.2).
29 * The reason for having this function in a file of its own is
30 * so that ../utils/cvt/cvt can link to it, and that xf86CVTMode
31 * code is shared directly.
34 #ifdef HAVE_XORG_CONFIG_H
35 #include <xorg-config.h>
43 #include "xf86Modes.h"
48 * Generate a CVT standard mode from HDisplay, VDisplay and VRefresh.
50 * These calculations are stolen from the CVT calculation spreadsheet written
51 * by Graham Loveridge. He seems to be claiming no copyright and there seems to
52 * be no license attached to this. He apparently just wants to see his name
55 * This file can be found at http://www.vesa.org/Public/CVT/CVTd6r1.xls
57 * Comments and structure corresponds to the comments and structure of the xls.
58 * This should ease importing of future changes to the standard (not very
61 * About margins; i'm sure that they are to be the bit between HDisplay and
62 * HBlankStart, HBlankEnd and HTotal, VDisplay and VBlankStart, VBlankEnd and
63 * VTotal, where the overscan colour is shown. FB seems to call _all_ blanking
64 * outside sync "margin" for some reason. Since we prefer seeing proper
65 * blanking instead of the overscan colour, and since the Crtc* values will
66 * probably get altered after us, we will disable margins altogether. With
67 * these calculations, Margins will plainly expand H/VDisplay, and we don't
71 _X_EXPORT DisplayModePtr
72 xf86CVTMode(int HDisplay
, int VDisplay
, float VRefresh
, Bool Reduced
,
75 DisplayModeRec
*Mode
= xnfalloc(sizeof(DisplayModeRec
));
77 /* 1) top/bottom margin size (% of height) - default: 1.8 */
78 #define CVT_MARGIN_PERCENTAGE 1.8
80 /* 2) character cell horizontal granularity (pixels) - default 8 */
81 #define CVT_H_GRANULARITY 8
83 /* 4) Minimum vertical porch (lines) - default 3 */
84 #define CVT_MIN_V_PORCH 3
86 /* 4) Minimum number of vertical back porch lines - default 6 */
87 #define CVT_MIN_V_BPORCH 6
89 /* Pixel Clock step (kHz) */
90 #define CVT_CLOCK_STEP 250
93 float VFieldRate
, HPeriod
;
94 int HDisplayRnd
, HMargin
;
95 int VDisplayRnd
, VMargin
, VSync
;
96 float Interlace
; /* Please rename this */
98 memset(Mode
, 0, sizeof(DisplayModeRec
));
100 /* CVT default is 60.0Hz */
104 /* 1. Required field rate */
106 VFieldRate
= VRefresh
* 2;
108 VFieldRate
= VRefresh
;
110 /* 2. Horizontal pixels */
111 HDisplayRnd
= HDisplay
- (HDisplay
% CVT_H_GRANULARITY
);
113 /* 3. Determine left and right borders */
115 /* right margin is actually exactly the same as left */
116 HMargin
= (((float) HDisplayRnd
) * CVT_MARGIN_PERCENTAGE
/ 100.0);
117 HMargin
-= HMargin
% CVT_H_GRANULARITY
;
121 /* 4. Find total active pixels */
122 Mode
->HDisplay
= HDisplayRnd
+ 2*HMargin
;
124 /* 5. Find number of lines per field */
126 VDisplayRnd
= VDisplay
/ 2;
128 VDisplayRnd
= VDisplay
;
130 /* 6. Find top and bottom margins */
133 /* top and bottom margins are equal again. */
134 VMargin
= (((float) VDisplayRnd
) * CVT_MARGIN_PERCENTAGE
/ 100.0);
138 Mode
->VDisplay
= VDisplay
+ 2*VMargin
;
146 /* Determine VSync Width from aspect ratio */
147 if (!(VDisplay
% 3) && ((VDisplay
* 4 / 3) == HDisplay
))
149 else if (!(VDisplay
% 9) && ((VDisplay
* 16 / 9) == HDisplay
))
151 else if (!(VDisplay
% 10) && ((VDisplay
* 16 / 10) == HDisplay
))
153 else if (!(VDisplay
% 4) && ((VDisplay
* 5 / 4) == HDisplay
))
155 else if (!(VDisplay
% 9) && ((VDisplay
* 15 / 9) == HDisplay
))
160 if (!Reduced
) { /* simplified GTF calculation */
162 /* 4) Minimum time of vertical sync + back porch interval (µs)
164 #define CVT_MIN_VSYNC_BP 550.0
166 /* 3) Nominal HSync width (% of line period) - default 8 */
167 #define CVT_HSYNC_PERCENTAGE 8
169 float HBlankPercentage
;
170 int VSyncAndBackPorch
, VBackPorch
;
173 /* 8. Estimated Horizontal period */
174 HPeriod
= ((float) (1000000.0 / VFieldRate
- CVT_MIN_VSYNC_BP
)) /
175 (VDisplayRnd
+ 2 * VMargin
+ CVT_MIN_V_PORCH
+ Interlace
);
177 /* 9. Find number of lines in sync + backporch */
178 if (((int)(CVT_MIN_VSYNC_BP
/ HPeriod
) + 1) < (VSync
+ CVT_MIN_V_PORCH
))
179 VSyncAndBackPorch
= VSync
+ CVT_MIN_V_PORCH
;
181 VSyncAndBackPorch
= (int)(CVT_MIN_VSYNC_BP
/ HPeriod
) + 1;
183 /* 10. Find number of lines in back porch */
184 VBackPorch
= VSyncAndBackPorch
- VSync
;
186 /* 11. Find total number of lines in vertical field */
187 Mode
->VTotal
= VDisplayRnd
+ 2 * VMargin
+ VSyncAndBackPorch
+ Interlace
190 /* 5) Definition of Horizontal blanking time limitation */
191 /* Gradient (%/kHz) - default 600 */
192 #define CVT_M_FACTOR 600
194 /* Offset (%) - default 40 */
195 #define CVT_C_FACTOR 40
197 /* Blanking time scaling factor - default 128 */
198 #define CVT_K_FACTOR 128
200 /* Scaling factor weighting - default 20 */
201 #define CVT_J_FACTOR 20
203 #define CVT_M_PRIME CVT_M_FACTOR * CVT_K_FACTOR / 256
204 #define CVT_C_PRIME (CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
207 /* 12. Find ideal blanking duty cycle from formula */
208 HBlankPercentage
= CVT_C_PRIME
- CVT_M_PRIME
* HPeriod
/1000.0;
210 /* 13. Blanking time */
211 if (HBlankPercentage
< 20)
212 HBlankPercentage
= 20;
214 HBlank
= Mode
->HDisplay
* HBlankPercentage
/(100.0 - HBlankPercentage
);
215 HBlank
-= HBlank
% (2*CVT_H_GRANULARITY
);
217 /* 14. Find total number of pixels in a line. */
218 Mode
->HTotal
= Mode
->HDisplay
+ HBlank
;
220 /* Fill in HSync values */
221 Mode
->HSyncEnd
= Mode
->HDisplay
+ HBlank
/ 2;
223 Mode
->HSyncStart
= Mode
->HSyncEnd
-
224 (Mode
->HTotal
* CVT_HSYNC_PERCENTAGE
) / 100;
225 Mode
->HSyncStart
+= CVT_H_GRANULARITY
-
226 Mode
->HSyncStart
% CVT_H_GRANULARITY
;
228 /* Fill in VSync values */
229 Mode
->VSyncStart
= Mode
->VDisplay
+ CVT_MIN_V_PORCH
;
230 Mode
->VSyncEnd
= Mode
->VSyncStart
+ VSync
;
232 } else { /* Reduced blanking */
233 /* Minimum vertical blanking interval time (µs) - default 460 */
234 #define CVT_RB_MIN_VBLANK 460.0
236 /* Fixed number of clocks for horizontal sync */
237 #define CVT_RB_H_SYNC 32.0
239 /* Fixed number of clocks for horizontal blanking */
240 #define CVT_RB_H_BLANK 160.0
242 /* Fixed number of lines for vertical front porch - default 3 */
243 #define CVT_RB_VFPORCH 3
247 /* 8. Estimate Horizontal period. */
248 HPeriod
= ((float) (1000000.0 / VFieldRate
- CVT_RB_MIN_VBLANK
)) /
249 (VDisplayRnd
+ 2*VMargin
);
251 /* 9. Find number of lines in vertical blanking */
252 VBILines
= ((float) CVT_RB_MIN_VBLANK
) / HPeriod
+ 1;
254 /* 10. Check if vertical blanking is sufficient */
255 if (VBILines
< (CVT_RB_VFPORCH
+ VSync
+ CVT_MIN_V_BPORCH
))
256 VBILines
= CVT_RB_VFPORCH
+ VSync
+ CVT_MIN_V_BPORCH
;
258 /* 11. Find total number of lines in vertical field */
259 Mode
->VTotal
= VDisplayRnd
+ 2 * VMargin
+ Interlace
+ VBILines
;
261 /* 12. Find total number of pixels in a line */
262 Mode
->HTotal
= Mode
->HDisplay
+ CVT_RB_H_BLANK
;
264 /* Fill in HSync values */
265 Mode
->HSyncEnd
= Mode
->HDisplay
+ CVT_RB_H_BLANK
/ 2;
266 Mode
->HSyncStart
= Mode
->HSyncEnd
- CVT_RB_H_SYNC
;
268 /* Fill in VSync values */
269 Mode
->VSyncStart
= Mode
->VDisplay
+ CVT_RB_VFPORCH
;
270 Mode
->VSyncEnd
= Mode
->VSyncStart
+ VSync
;
273 /* 15/13. Find pixel clock frequency (kHz for xf86) */
274 Mode
->Clock
= Mode
->HTotal
* 1000.0 / HPeriod
;
275 Mode
->Clock
-= Mode
->Clock
% CVT_CLOCK_STEP
;
277 /* 16/14. Find actual Horizontal Frequency (kHz) */
278 Mode
->HSync
= ((float) Mode
->Clock
) / ((float) Mode
->HTotal
);
280 /* 17/15. Find actual Field rate */
281 Mode
->VRefresh
= (1000.0 * ((float) Mode
->Clock
)) /
282 ((float) (Mode
->HTotal
* Mode
->VTotal
));
284 /* 18/16. Find actual vertical frame frequency */
285 /* ignore - just set the mode flag for interlaced */
293 snprintf(Name
, 256, "%dx%d", HDisplay
, VDisplay
);
295 Mode
->name
= xnfalloc(strlen(Name
) + 1);
296 memcpy(Mode
->name
, Name
, strlen(Name
) + 1);
300 Mode
->Flags
|= V_PHSYNC
| V_NVSYNC
;
302 Mode
->Flags
|= V_NHSYNC
| V_PVSYNC
;
305 Mode
->Flags
|= V_INTERLACE
;