1
/* Copied from hw/xfree86/modes/xf86cvt.c into xwayland DDX and
2
* changed to generate an RRMode */
5
* Copyright 2005-2006 Luc Verhaegen.
7
* Permission is hereby granted, free of charge, to any person obtaining a
8
* copy of this software and associated documentation files (the "Software"),
9
* to deal in the Software without restriction, including without limitation
10
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
* and/or sell copies of the Software, and to permit persons to whom the
12
* Software is furnished to do so, subject to the following conditions:
14
* The above copyright notice and this permission notice shall be included in
15
* all copies or substantial portions of the Software.
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
* OTHER DEALINGS IN THE SOFTWARE.
27
* The reason for having this function in a file of its own is
28
* so that ../utils/cvt/cvt can link to it, and that xf86CVTMode
29
* code is shared directly.
32
#ifdef HAVE_DIX_CONFIG_H
33
#include <dix-config.h>
41
* Generate a CVT standard mode from HDisplay, VDisplay and VRefresh.
43
* These calculations are stolen from the CVT calculation spreadsheet written
44
* by Graham Loveridge. He seems to be claiming no copyright and there seems to
45
* be no license attached to this. He apparently just wants to see his name
48
* This file can be found at http://www.vesa.org/Public/CVT/CVTd6r1.xls
50
* Comments and structure corresponds to the comments and structure of the xls.
51
* This should ease importing of future changes to the standard (not very
54
* About margins; i'm sure that they are to be the bit between HDisplay and
55
* HBlankStart, HBlankEnd and HTotal, VDisplay and VBlankStart, VBlankEnd and
56
* VTotal, where the overscan colour is shown. FB seems to call _all_ blanking
57
* outside sync "margin" for some reason. Since we prefer seeing proper
58
* blanking instead of the overscan colour, and since the Crtc* values will
59
* probably get altered after us, we will disable margins altogether. With
60
* these calculations, Margins will plainly expand H/VDisplay, and we don't
65
xwayland_cvt(int HDisplay, int VDisplay, float VRefresh, Bool Reduced,
68
/* 1) top/bottom margin size (% of height) - default: 1.8 */
69
#define CVT_MARGIN_PERCENTAGE 1.8
71
/* 2) character cell horizontal granularity (pixels) - default 8 */
72
#define CVT_H_GRANULARITY 8
74
/* 4) Minimum vertical porch (lines) - default 3 */
75
#define CVT_MIN_V_PORCH 3
77
/* 4) Minimum number of vertical back porch lines - default 6 */
78
#define CVT_MIN_V_BPORCH 6
80
/* Pixel Clock step (kHz) */
81
#define CVT_CLOCK_STEP 250
84
float VFieldRate, HPeriod;
85
int HDisplayRnd, HMargin;
86
int VDisplayRnd, VMargin, VSync;
87
float Interlace; /* Please rename this */
91
memset(&modeinfo, 0, sizeof modeinfo);
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
modeinfo.width = 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
modeinfo.height = 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;
182
/* 11. Find total number of lines in vertical field */
184
VDisplayRnd + 2 * VMargin + VSyncAndBackPorch + Interlace +
187
/* 5) Definition of Horizontal blanking time limitation */
188
/* Gradient (%/kHz) - default 600 */
189
#define CVT_M_FACTOR 600
191
/* Offset (%) - default 40 */
192
#define CVT_C_FACTOR 40
194
/* Blanking time scaling factor - default 128 */
195
#define CVT_K_FACTOR 128
197
/* Scaling factor weighting - default 20 */
198
#define CVT_J_FACTOR 20
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 + \
204
/* 12. Find ideal blanking duty cycle from formula */
205
HBlankPercentage = CVT_C_PRIME - CVT_M_PRIME * HPeriod / 1000.0;
207
/* 13. Blanking time */
208
if (HBlankPercentage < 20)
209
HBlankPercentage = 20;
211
HBlank = modeinfo.width * HBlankPercentage / (100.0 - HBlankPercentage);
212
HBlank -= HBlank % (2 * CVT_H_GRANULARITY);
214
/* 14. Find total number of pixels in a line. */
215
modeinfo.hTotal = modeinfo.width + HBlank;
217
/* Fill in HSync values */
218
modeinfo.hSyncEnd = modeinfo.width + HBlank / 2;
220
modeinfo.hSyncStart = modeinfo.hSyncEnd -
221
(modeinfo.hTotal * CVT_HSYNC_PERCENTAGE) / 100;
222
modeinfo.hSyncStart += CVT_H_GRANULARITY -
223
modeinfo.hSyncStart % CVT_H_GRANULARITY;
225
/* Fill in VSync values */
226
modeinfo.vSyncStart = modeinfo.height + CVT_MIN_V_PORCH;
227
modeinfo.vSyncEnd = modeinfo.vSyncStart + VSync;
230
else { /* Reduced blanking */
231
/* Minimum vertical blanking interval time (µs) - default 460 */
232
#define CVT_RB_MIN_VBLANK 460.0
234
/* Fixed number of clocks for horizontal sync */
235
#define CVT_RB_H_SYNC 32.0
237
/* Fixed number of clocks for horizontal blanking */
238
#define CVT_RB_H_BLANK 160.0
240
/* Fixed number of lines for vertical front porch - default 3 */
241
#define CVT_RB_VFPORCH 3
245
/* 8. Estimate Horizontal period. */
246
HPeriod = ((float) (1000000.0 / VFieldRate - CVT_RB_MIN_VBLANK)) /
247
(VDisplayRnd + 2 * VMargin);
249
/* 9. Find number of lines in vertical blanking */
250
VBILines = ((float) CVT_RB_MIN_VBLANK) / HPeriod + 1;
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;
256
/* 11. Find total number of lines in vertical field */
257
modeinfo.vTotal = VDisplayRnd + 2 * VMargin + Interlace + VBILines;
259
/* 12. Find total number of pixels in a line */
260
modeinfo.hTotal = modeinfo.width + CVT_RB_H_BLANK;
262
/* Fill in HSync values */
263
modeinfo.hSyncEnd = modeinfo.width + CVT_RB_H_BLANK / 2;
264
modeinfo.hSyncStart = modeinfo.hSyncEnd - CVT_RB_H_SYNC;
266
/* Fill in VSync values */
267
modeinfo.vSyncStart = modeinfo.height + CVT_RB_VFPORCH;
268
modeinfo.vSyncEnd = modeinfo.vSyncStart + VSync;
271
/* 15/13. Find pixel clock frequency (kHz for xf86) */
272
modeinfo.dotClock = modeinfo.hTotal * 1000.0 / HPeriod;
273
modeinfo.dotClock -= modeinfo.dotClock % CVT_CLOCK_STEP;
274
modeinfo.dotClock *= 1000.0;
276
/* 16/14. Find actual Horizontal Frequency (kHz) */
277
modeinfo.hSync = ((float) modeinfo.dotClock) / ((float) modeinfo.hTotal);
281
/* 17/15. Find actual Field rate */
282
modeinfo.vRefresh = (1000.0 * ((float) modeinfo.dotClock)) /
283
((float) (modeinfo.hTotal * modeinfo.vTotal));
286
/* 18/16. Find actual vertical frame frequency */
287
/* ignore - just set the mode flag for interlaced */
289
modeinfo.vTotal *= 2;
292
modeinfo.modeFlags |= RR_HSyncPositive | RR_VSyncNegative;
294
modeinfo.modeFlags |= RR_HSyncNegative | RR_VSyncPositive;
297
modeinfo.modeFlags |= RR_Interlace;
299
snprintf(name, sizeof name, "%dx%d@%.1fHz",
300
modeinfo.width, modeinfo.height, VRefresh);
301
modeinfo.nameLength = strlen(name);
303
return RRModeGet(&modeinfo, name);