summaryrefslogtreecommitdiff
path: root/cvt.c
blob: d7cd570a38a014ed3d1246797a7db3c5b0619fab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright 2005-2006 Luc Verhaegen.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "minitru-int.h"

/*
 * Generate a CVT standard mode from HDisplay, VDisplay and VRefresh.
 *
 * These calculations are stolen from the CVT calculation spreadsheet written
 * by Graham Loveridge. He seems to be claiming no copyright and there seems to
 * be no license attached to this. He apparently just wants to see his name
 * mentioned.
 *
 * This file can be found at http://www.vesa.org/Public/CVT/CVTd6r1.xls
 *
 * Comments and structure corresponds to the comments and structure of the xls.
 * This should ease importing of future changes to the standard (not very
 * likely though).
 *
 * About margins; i'm sure that they are to be the bit between HDisplay and
 * HBlankStart, HBlankEnd and HTotal, VDisplay and VBlankStart, VBlankEnd and 
 * VTotal, where the overscan colour is shown. FB seems to call _all_ blanking
 * outside sync "margin" for some reason. Since we prefer seeing proper
 * blanking instead of the overscan colour, and since the Crtc* values will
 * probably get altered after us, we will disable margins altogether. With
 * these calculations, Margins will plainly expand H/VDisplay, and we don't
 * want that. -- libv
 *
 */
struct mt_mode *
mt_cvt_mode(uint32_t hdisplay, uint32_t vdisplay, float refresh, uint32_t flags)
{
    struct mt_mode *mode = mt_mode_alloc();
    int interlaced = !!(flags & MT_FLAG_INTERLACED);
    int reduced = !!(flags & MT_FLAG_REDUCED);

    /* 1) top/bottom margin size (% of height) - default: 1.8 */
#define CVT_MARGIN_PERCENTAGE 1.8    

    /* 2) character cell horizontal granularity (pixels) - default 8 */
#define CVT_H_GRANULARITY 8

    /* 4) Minimum vertical porch (lines) - default 3 */
#define CVT_MIN_V_PORCH 3

    /* 4) Minimum number of vertical back porch lines - default 6 */
#define CVT_MIN_V_BPORCH 6

    /* Pixel Clock step (kHz) */
#define CVT_CLOCK_STEP 250

    uint32_t margins = 0;
    float  vfieldrate, hperiod;
    int  hdisplayrnd, hmargin;
    int  vdisplayrnd, vmargin, vsync;
    float  interlace; /* please rename this */

    /* CVT default is 60.0Hz */
    if (!refresh)
        refresh = 60.0;

    /* 1. Required field rate */
    if (interlaced)
        vfieldrate = refresh * 2;
    else
        vfieldrate = refresh;

    /* 2. Horizontal pixels */
    hdisplayrnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);

    /* 3. Determine left and right borders */
    if (margins) {
        /* right margin is actually exactly the same as left */
        hmargin = (((float) hdisplayrnd) * CVT_MARGIN_PERCENTAGE / 100.0);
        hmargin -= hmargin % CVT_H_GRANULARITY;
    } else
        hmargin = 0;

    /* 4. Find total active pixels */
    mode->hdisplay = hdisplayrnd + 2*hmargin;

    /* 5. Find number of lines per field */
    if (interlaced)
        vdisplayrnd = vdisplay / 2;
    else
        vdisplayrnd = vdisplay;

    /* 6. Find top and bottom margins */
    /* nope. */
    if (margins)
        /* top and bottom margins are equal again. */
        vmargin = (((float) vdisplayrnd) * CVT_MARGIN_PERCENTAGE / 100.0);
    else
        vmargin = 0;

    mode->vdisplay = vdisplay + 2*vmargin;

    /* 7. Interlace */
    if (interlaced)
        interlace = 0.5;
    else
        interlace = 0.0;

    /* Determine VSync Width from aspect ratio */
    if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
        vsync = 4;
    else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
        vsync = 5;
    else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
        vsync = 6;
    else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
        vsync = 7;
    else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
        vsync = 7;
    else /* Custom */
        vsync = 10;

    if (!reduced) { /* simplified GTF calculation */

        /* 4) Minimum time of vertical sync + back porch interval (µs) 
         * default 550.0 */
#define CVT_MIN_VSYNC_BP 550.0

        /* 3) Nominal HSync width (% of line period) - default 8 */
#define CVT_HSYNC_PERCENTAGE 8

        float  hblankpercentage;
        int  vsyncandbackporch, vbackporch;
        int  hblank;

        /* 8. Estimated Horizontal period */
        hperiod = ((float) (1000000.0 / vfieldrate - CVT_MIN_VSYNC_BP)) / 
            (vdisplayrnd + 2 * vmargin + CVT_MIN_V_PORCH + interlace);

        /* 9. Find number of lines in sync + backporch */
        if (((int)(CVT_MIN_VSYNC_BP / hperiod) + 1) < (vsync + CVT_MIN_V_PORCH))
            vsyncandbackporch = vsync + CVT_MIN_V_PORCH;
        else
            vsyncandbackporch = (int)(CVT_MIN_VSYNC_BP / hperiod) + 1;

        /* 10. Find number of lines in back porch */
        vbackporch = vsyncandbackporch - vsync;

        /* 11. Find total number of lines in vertical field */
        mode->vtotal = vdisplayrnd + 2 * vmargin + vsyncandbackporch + interlace
            + CVT_MIN_V_PORCH;

        /* 5) Definition of Horizontal blanking time limitation */
        /* Gradient (%/kHz) - default 600 */
#define CVT_M_FACTOR 600

        /* Offset (%) - default 40 */
#define CVT_C_FACTOR 40

        /* Blanking time scaling factor - default 128 */
#define CVT_K_FACTOR 128

        /* Scaling factor weighting - default 20 */
#define CVT_J_FACTOR 20

#define CVT_M_PRIME CVT_M_FACTOR * CVT_K_FACTOR / 256
#define CVT_C_PRIME (CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
        CVT_J_FACTOR

        /* 12. Find ideal blanking duty cycle from formula */
        hblankpercentage = CVT_C_PRIME - CVT_M_PRIME * hperiod/1000.0;

        /* 13. Blanking time */
        if (hblankpercentage < 20)
            hblankpercentage = 20;

        hblank = mode->hdisplay * hblankpercentage/(100.0 - hblankpercentage);
        hblank -= hblank % (2*CVT_H_GRANULARITY);
        
        /* 14. Find total number of pixels in a line. */
        mode->htotal = mode->hdisplay + hblank;

        /* Fill in HSync values */
        mode->hsyncend = mode->hdisplay + hblank / 2;

        mode->hsyncstart = mode->hsyncend - 
            (mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
        mode->hsyncstart += CVT_H_GRANULARITY - 
            mode->hsyncstart % CVT_H_GRANULARITY;

        /* Fill in VSync values */
        mode->vsyncstart = mode->vdisplay + CVT_MIN_V_PORCH;
        mode->vsyncend = mode->vsyncstart + vsync;

    } else { /* Reduced blanking */
        /* Minimum vertical blanking interval time (µs) - default 460 */
#define CVT_RB_MIN_VBLANK 460.0

        /* Fixed number of clocks for horizontal sync */
#define CVT_RB_H_SYNC 32.0

        /* Fixed number of clocks for horizontal blanking */
#define CVT_RB_H_BLANK 160.0

        /* Fixed number of lines for vertical front porch - default 3 */
#define CVT_RB_VFPORCH 3

        int  vbilines;

        /* 8. Estimate Horizontal period. */
        hperiod = ((float) (1000000.0 / vfieldrate - CVT_RB_MIN_VBLANK)) / 
            (vdisplayrnd + 2*vmargin);

        /* 9. Find number of lines in vertical blanking */
        vbilines = ((float) CVT_RB_MIN_VBLANK) / hperiod + 1;

        /* 10. Check if vertical blanking is sufficient */
        if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
            vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
        
        /* 11. Find total number of lines in vertical field */
        mode->vtotal = vdisplayrnd + 2 * vmargin + interlace + vbilines;

        /* 12. Find total number of pixels in a line */
        mode->htotal = mode->hdisplay + CVT_RB_H_BLANK;

        /* Fill in HSync values */
        mode->hsyncend = mode->hdisplay + CVT_RB_H_BLANK / 2;
        mode->hsyncstart = mode->hsyncend - CVT_RB_H_SYNC;

        /* Fill in VSync values */
        mode->vsyncstart = mode->vdisplay + CVT_RB_VFPORCH;
        mode->vsyncend = mode->vsyncstart + vsync;
    }

    /* 15/13. Find pixel clock frequency (kHz for xf86) */
    mode->clock = mode->htotal * 1000.0 / hperiod;
    mode->clock -= mode->clock % CVT_CLOCK_STEP;

    /* 18/16. Find actual vertical frame frequency */
    /* ignore - just set the mode flag for interlaced */
    if (interlaced)
        mode->vtotal *= 2;

    if (reduced)
        mode->flags |= MT_FLAG_PHSYNC | MT_FLAG_NVSYNC;
    else
        mode->flags |= MT_FLAG_NHSYNC | MT_FLAG_PVSYNC;

    if (interlaced)
        mode->flags |= MT_FLAG_INTERLACED;

    return mode;
}