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 #ifdef HAVE_XORG_CONFIG_H
30 #include <xorg-config.h>
38 #include "xf86Modes.h"
43 * Generate a CVT standard mode from HDisplay, VDisplay and VRefresh.
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
50 * This file can be found at http://www.vesa.org/Public/CVT/CVTd6r1.xls
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
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
67 xf86CVTMode(int HDisplay
, int VDisplay
, float VRefresh
, Bool Reduced
,
70 DisplayModeRec
*Mode
= xnfcalloc(1, sizeof(DisplayModeRec
));
72 /* 1) top/bottom margin size (% of height) - default: 1.8 */
73 #define CVT_MARGIN_PERCENTAGE 1.8
75 /* 2) character cell horizontal granularity (pixels) - default 8 */
76 #define CVT_H_GRANULARITY 8
78 /* 4) Minimum vertical porch (lines) - default 3 */
79 #define CVT_MIN_V_PORCH 3
81 /* 4) Minimum number of vertical back porch lines - default 6 */
82 #define CVT_MIN_V_BPORCH 6
84 /* Pixel Clock step (kHz) */
85 #define CVT_CLOCK_STEP 250
88 float VFieldRate
, HPeriod
;
89 int HDisplayRnd
, HMargin
;
90 int VDisplayRnd
, VMargin
, VSync
;
91 float Interlace
; /* Please rename this */
93 /* CVT default is 60.0Hz */
97 /* 1. Required field rate */
99 VFieldRate
= VRefresh
* 2;
101 VFieldRate
= VRefresh
;
103 /* 2. Horizontal pixels */
104 HDisplayRnd
= HDisplay
- (HDisplay
% CVT_H_GRANULARITY
);
106 /* 3. Determine left and right borders */
108 /* right margin is actually exactly the same as left */
109 HMargin
= (((float) HDisplayRnd
) * CVT_MARGIN_PERCENTAGE
/ 100.0);
110 HMargin
-= HMargin
% CVT_H_GRANULARITY
;
115 /* 4. Find total active pixels */
116 Mode
->HDisplay
= HDisplayRnd
+ 2 * HMargin
;
118 /* 5. Find number of lines per field */
120 VDisplayRnd
= VDisplay
/ 2;
122 VDisplayRnd
= VDisplay
;
124 /* 6. Find top and bottom margins */
127 /* top and bottom margins are equal again. */
128 VMargin
= (((float) VDisplayRnd
) * CVT_MARGIN_PERCENTAGE
/ 100.0);
132 Mode
->VDisplay
= VDisplay
+ 2 * VMargin
;
140 /* Determine VSync Width from aspect ratio */
141 if (!(VDisplay
% 3) && ((VDisplay
* 4 / 3) == HDisplay
))
143 else if (!(VDisplay
% 9) && ((VDisplay
* 16 / 9) == HDisplay
))
145 else if (!(VDisplay
% 10) && ((VDisplay
* 16 / 10) == HDisplay
))
147 else if (!(VDisplay
% 4) && ((VDisplay
* 5 / 4) == HDisplay
))
149 else if (!(VDisplay
% 9) && ((VDisplay
* 15 / 9) == HDisplay
))
154 if (!Reduced
) { /* simplified GTF calculation */
156 /* 4) Minimum time of vertical sync + back porch interval (µs)
158 #define CVT_MIN_VSYNC_BP 550.0
160 /* 3) Nominal HSync width (% of line period) - default 8 */
161 #define CVT_HSYNC_PERCENTAGE 8
163 float HBlankPercentage
;
164 int VSyncAndBackPorch
, VBackPorch
;
167 /* 8. Estimated Horizontal period */
168 HPeriod
= ((float) (1000000.0 / VFieldRate
- CVT_MIN_VSYNC_BP
)) /
169 (VDisplayRnd
+ 2 * VMargin
+ CVT_MIN_V_PORCH
+ Interlace
);
171 /* 9. Find number of lines in sync + backporch */
172 if (((int) (CVT_MIN_VSYNC_BP
/ HPeriod
) + 1) <
173 (VSync
+ CVT_MIN_V_PORCH
))
174 VSyncAndBackPorch
= VSync
+ CVT_MIN_V_PORCH
;
176 VSyncAndBackPorch
= (int) (CVT_MIN_VSYNC_BP
/ HPeriod
) + 1;
178 /* 10. Find number of lines in back porch */
179 VBackPorch
= VSyncAndBackPorch
- VSync
;
181 /* 11. Find total number of lines in vertical field */
182 Mode
->VTotal
= VDisplayRnd
+ 2 * VMargin
+ VSyncAndBackPorch
+ Interlace
185 /* 5) Definition of Horizontal blanking time limitation */
186 /* Gradient (%/kHz) - default 600 */
187 #define CVT_M_FACTOR 600
189 /* Offset (%) - default 40 */
190 #define CVT_C_FACTOR 40
192 /* Blanking time scaling factor - default 128 */
193 #define CVT_K_FACTOR 128
195 /* Scaling factor weighting - default 20 */
196 #define CVT_J_FACTOR 20
198 #define CVT_M_PRIME CVT_M_FACTOR * CVT_K_FACTOR / 256
199 #define CVT_C_PRIME (CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
202 /* 12. Find ideal blanking duty cycle from formula */
203 HBlankPercentage
= CVT_C_PRIME
- CVT_M_PRIME
* HPeriod
/ 1000.0;
205 /* 13. Blanking time */
206 if (HBlankPercentage
< 20)
207 HBlankPercentage
= 20;
209 HBlank
= Mode
->HDisplay
* HBlankPercentage
/ (100.0 - HBlankPercentage
);
210 HBlank
-= HBlank
% (2 * CVT_H_GRANULARITY
);
212 /* 14. Find total number of pixels in a line. */
213 Mode
->HTotal
= Mode
->HDisplay
+ HBlank
;
215 /* Fill in HSync values */
216 Mode
->HSyncEnd
= Mode
->HDisplay
+ HBlank
/ 2;
218 Mode
->HSyncStart
= Mode
->HSyncEnd
-
219 (Mode
->HTotal
* CVT_HSYNC_PERCENTAGE
) / 100;
220 Mode
->HSyncStart
+= CVT_H_GRANULARITY
-
221 Mode
->HSyncStart
% CVT_H_GRANULARITY
;
223 /* Fill in VSync values */
224 Mode
->VSyncStart
= Mode
->VDisplay
+ CVT_MIN_V_PORCH
;
225 Mode
->VSyncEnd
= Mode
->VSyncStart
+ VSync
;
228 else { /* Reduced blanking */
229 /* Minimum vertical blanking interval time (µs) - default 460 */
230 #define CVT_RB_MIN_VBLANK 460.0
232 /* Fixed number of clocks for horizontal sync */
233 #define CVT_RB_H_SYNC 32.0
235 /* Fixed number of clocks for horizontal blanking */
236 #define CVT_RB_H_BLANK 160.0
238 /* Fixed number of lines for vertical front porch - default 3 */
239 #define CVT_RB_VFPORCH 3
243 /* 8. Estimate Horizontal period. */
244 HPeriod
= ((float) (1000000.0 / VFieldRate
- CVT_RB_MIN_VBLANK
)) /
245 (VDisplayRnd
+ 2 * VMargin
);
247 /* 9. Find number of lines in vertical blanking */
248 VBILines
= ((float) CVT_RB_MIN_VBLANK
) / HPeriod
+ 1;
250 /* 10. Check if vertical blanking is sufficient */
251 if (VBILines
< (CVT_RB_VFPORCH
+ VSync
+ CVT_MIN_V_BPORCH
))
252 VBILines
= CVT_RB_VFPORCH
+ VSync
+ CVT_MIN_V_BPORCH
;
254 /* 11. Find total number of lines in vertical field */
255 Mode
->VTotal
= VDisplayRnd
+ 2 * VMargin
+ Interlace
+ VBILines
;
257 /* 12. Find total number of pixels in a line */
258 Mode
->HTotal
= Mode
->HDisplay
+ CVT_RB_H_BLANK
;
260 /* Fill in HSync values */
261 Mode
->HSyncEnd
= Mode
->HDisplay
+ CVT_RB_H_BLANK
/ 2;
262 Mode
->HSyncStart
= Mode
->HSyncEnd
- CVT_RB_H_SYNC
;
264 /* Fill in VSync values */
265 Mode
->VSyncStart
= Mode
->VDisplay
+ CVT_RB_VFPORCH
;
266 Mode
->VSyncEnd
= Mode
->VSyncStart
+ VSync
;
269 /* 15/13. Find pixel clock frequency (kHz for xf86) */
270 Mode
->Clock
= Mode
->HTotal
* 1000.0 / HPeriod
;
271 Mode
->Clock
-= Mode
->Clock
% CVT_CLOCK_STEP
;
273 /* 16/14. Find actual Horizontal Frequency (kHz) */
274 Mode
->HSync
= ((float) Mode
->Clock
) / ((float) Mode
->HTotal
);
276 /* 17/15. Find actual Field rate */
277 Mode
->VRefresh
= (1000.0 * ((float) Mode
->Clock
)) /
278 ((float) (Mode
->HTotal
* Mode
->VTotal
));
280 /* 18/16. Find actual vertical frame frequency */
281 /* ignore - just set the mode flag for interlaced */
285 XNFasprintf(&Mode
->name
, "%dx%d", HDisplay
, VDisplay
);
288 Mode
->Flags
|= V_PHSYNC
| V_NVSYNC
;
290 Mode
->Flags
|= V_NHSYNC
| V_PVSYNC
;
293 Mode
->Flags
|= V_INTERLACE
;