vesagtf.c revision 1.1.8.2 1 1.1.8.2 tron /* $NetBSD: vesagtf.c,v 1.1.8.2 2006/05/24 15:50:32 tron Exp $ */
2 1.1.8.2 tron
3 1.1.8.2 tron /*-
4 1.1.8.2 tron * Copyright (c) 2006 Itronix Inc.
5 1.1.8.2 tron * All rights reserved.
6 1.1.8.2 tron *
7 1.1.8.2 tron * Written by Garrett D'Amore for Itronix Inc.
8 1.1.8.2 tron *
9 1.1.8.2 tron * Redistribution and use in source and binary forms, with or without
10 1.1.8.2 tron * modification, are permitted provided that the following conditions
11 1.1.8.2 tron * are met:
12 1.1.8.2 tron * 1. Redistributions of source code must retain the above copyright
13 1.1.8.2 tron * notice, this list of conditions and the following disclaimer.
14 1.1.8.2 tron * 2. Redistributions in binary form must reproduce the above copyright
15 1.1.8.2 tron * notice, this list of conditions and the following disclaimer in the
16 1.1.8.2 tron * documentation and/or other materials provided with the distribution.
17 1.1.8.2 tron * 3. The name of Itronix Inc. may not be used to endorse
18 1.1.8.2 tron * or promote products derived from this software without specific
19 1.1.8.2 tron * prior written permission.
20 1.1.8.2 tron *
21 1.1.8.2 tron * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
22 1.1.8.2 tron * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1.8.2 tron * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1.8.2 tron * ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 1.1.8.2 tron * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1.8.2 tron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.1.8.2 tron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1.8.2 tron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1.8.2 tron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.1.8.2 tron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 1.1.8.2 tron * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1.8.2 tron */
33 1.1.8.2 tron
34 1.1.8.2 tron /*
35 1.1.8.2 tron * This was derived from a userland GTF program supplied by NVIDIA.
36 1.1.8.2 tron * NVIDIA's original boilerplate follows.
37 1.1.8.2 tron *
38 1.1.8.2 tron * Note that I have heavily modified the program for use in the EDID
39 1.1.8.2 tron * kernel code for NetBSD, including removing the use of floating
40 1.1.8.2 tron * point operations and making significant adjustments to minimize
41 1.1.8.2 tron * error propogation while operating with integer only math.
42 1.1.8.2 tron *
43 1.1.8.2 tron * This has required the use of 64-bit integers in a few places, but
44 1.1.8.2 tron * the upshot is that for a calculation of 1920x1200x85 (as an
45 1.1.8.2 tron * example), the error deviates by only ~.004% relative to the
46 1.1.8.2 tron * floating point version. This error is *well* within VESA
47 1.1.8.2 tron * tolerances.
48 1.1.8.2 tron */
49 1.1.8.2 tron
50 1.1.8.2 tron /*
51 1.1.8.2 tron * Copyright (c) 2001, Andy Ritger aritger (at) nvidia.com
52 1.1.8.2 tron * All rights reserved.
53 1.1.8.2 tron *
54 1.1.8.2 tron * Redistribution and use in source and binary forms, with or without
55 1.1.8.2 tron * modification, are permitted provided that the following conditions
56 1.1.8.2 tron * are met:
57 1.1.8.2 tron *
58 1.1.8.2 tron * o Redistributions of source code must retain the above copyright
59 1.1.8.2 tron * notice, this list of conditions and the following disclaimer.
60 1.1.8.2 tron * o Redistributions in binary form must reproduce the above copyright
61 1.1.8.2 tron * notice, this list of conditions and the following disclaimer
62 1.1.8.2 tron * in the documentation and/or other materials provided with the
63 1.1.8.2 tron * distribution.
64 1.1.8.2 tron * o Neither the name of NVIDIA nor the names of its contributors
65 1.1.8.2 tron * may be used to endorse or promote products derived from this
66 1.1.8.2 tron * software without specific prior written permission.
67 1.1.8.2 tron *
68 1.1.8.2 tron *
69 1.1.8.2 tron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
70 1.1.8.2 tron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
71 1.1.8.2 tron * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
72 1.1.8.2 tron * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
73 1.1.8.2 tron * THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
74 1.1.8.2 tron * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 1.1.8.2 tron * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
76 1.1.8.2 tron * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
77 1.1.8.2 tron * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
78 1.1.8.2 tron * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
79 1.1.8.2 tron * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
80 1.1.8.2 tron * POSSIBILITY OF SUCH DAMAGE.
81 1.1.8.2 tron *
82 1.1.8.2 tron *
83 1.1.8.2 tron *
84 1.1.8.2 tron * This program is based on the Generalized Timing Formula(GTF TM)
85 1.1.8.2 tron * Standard Version: 1.0, Revision: 1.0
86 1.1.8.2 tron *
87 1.1.8.2 tron * The GTF Document contains the following Copyright information:
88 1.1.8.2 tron *
89 1.1.8.2 tron * Copyright (c) 1994, 1995, 1996 - Video Electronics Standards
90 1.1.8.2 tron * Association. Duplication of this document within VESA member
91 1.1.8.2 tron * companies for review purposes is permitted. All other rights
92 1.1.8.2 tron * reserved.
93 1.1.8.2 tron *
94 1.1.8.2 tron * While every precaution has been taken in the preparation
95 1.1.8.2 tron * of this standard, the Video Electronics Standards Association and
96 1.1.8.2 tron * its contributors assume no responsibility for errors or omissions,
97 1.1.8.2 tron * and make no warranties, expressed or implied, of functionality
98 1.1.8.2 tron * of suitability for any purpose. The sample code contained within
99 1.1.8.2 tron * this standard may be used without restriction.
100 1.1.8.2 tron *
101 1.1.8.2 tron *
102 1.1.8.2 tron *
103 1.1.8.2 tron * The GTF EXCEL(TM) SPREADSHEET, a sample (and the definitive)
104 1.1.8.2 tron * implementation of the GTF Timing Standard, is available at:
105 1.1.8.2 tron *
106 1.1.8.2 tron * ftp://ftp.vesa.org/pub/GTF/GTF_V1R1.xls
107 1.1.8.2 tron *
108 1.1.8.2 tron *
109 1.1.8.2 tron *
110 1.1.8.2 tron * This program takes a desired resolution and vertical refresh rate,
111 1.1.8.2 tron * and computes mode timings according to the GTF Timing Standard.
112 1.1.8.2 tron * These mode timings can then be formatted as an XFree86 modeline
113 1.1.8.2 tron * or a mode description for use by fbset(8).
114 1.1.8.2 tron *
115 1.1.8.2 tron *
116 1.1.8.2 tron *
117 1.1.8.2 tron * NOTES:
118 1.1.8.2 tron *
119 1.1.8.2 tron * The GTF allows for computation of "margins" (the visible border
120 1.1.8.2 tron * surrounding the addressable video); on most non-overscan type
121 1.1.8.2 tron * systems, the margin period is zero. I've implemented the margin
122 1.1.8.2 tron * computations but not enabled it because 1) I don't really have
123 1.1.8.2 tron * any experience with this, and 2) neither XFree86 modelines nor
124 1.1.8.2 tron * fbset fb.modes provide an obvious way for margin timings to be
125 1.1.8.2 tron * included in their mode descriptions (needs more investigation).
126 1.1.8.2 tron *
127 1.1.8.2 tron * The GTF provides for computation of interlaced mode timings;
128 1.1.8.2 tron * I've implemented the computations but not enabled them, yet.
129 1.1.8.2 tron * I should probably enable and test this at some point.
130 1.1.8.2 tron *
131 1.1.8.2 tron *
132 1.1.8.2 tron *
133 1.1.8.2 tron * TODO:
134 1.1.8.2 tron *
135 1.1.8.2 tron * o Add support for interlaced modes.
136 1.1.8.2 tron *
137 1.1.8.2 tron * o Implement the other portions of the GTF: compute mode timings
138 1.1.8.2 tron * given either the desired pixel clock or the desired horizontal
139 1.1.8.2 tron * frequency.
140 1.1.8.2 tron *
141 1.1.8.2 tron * o It would be nice if this were more general purpose to do things
142 1.1.8.2 tron * outside the scope of the GTF: like generate double scan mode
143 1.1.8.2 tron * timings, for example.
144 1.1.8.2 tron *
145 1.1.8.2 tron * o Printing digits to the right of the decimal point when the
146 1.1.8.2 tron * digits are 0 annoys me.
147 1.1.8.2 tron *
148 1.1.8.2 tron * o Error checking.
149 1.1.8.2 tron *
150 1.1.8.2 tron */
151 1.1.8.2 tron
152 1.1.8.2 tron
153 1.1.8.2 tron #ifdef _KERNEL
154 1.1.8.2 tron #include <sys/cdefs.h>
155 1.1.8.2 tron
156 1.1.8.2 tron __KERNEL_RCSID(0, "$NetBSD: vesagtf.c,v 1.1.8.2 2006/05/24 15:50:32 tron Exp $");
157 1.1.8.2 tron #include <sys/types.h>
158 1.1.8.2 tron #include <sys/param.h>
159 1.1.8.2 tron #include <sys/systm.h>
160 1.1.8.2 tron #include <dev/videomode/videomode.h>
161 1.1.8.2 tron #include <dev/videomode/vesagtf.h>
162 1.1.8.2 tron #else
163 1.1.8.2 tron #include <stdio.h>
164 1.1.8.2 tron #include <stdlib.h>
165 1.1.8.2 tron #include <sys/types.h>
166 1.1.8.2 tron #include "videomode.h"
167 1.1.8.2 tron #include "vesagtf.h"
168 1.1.8.2 tron
169 1.1.8.2 tron void print_xf86_mode(struct videomode *m);
170 1.1.8.2 tron #endif
171 1.1.8.2 tron
172 1.1.8.2 tron #define CELL_GRAN 8 /* assumed character cell granularity */
173 1.1.8.2 tron
174 1.1.8.2 tron /* C' and M' are part of the Blanking Duty Cycle computation */
175 1.1.8.2 tron /*
176 1.1.8.2 tron * #define C_PRIME (((C - J) * K/256.0) + J)
177 1.1.8.2 tron * #define M_PRIME (K/256.0 * M)
178 1.1.8.2 tron */
179 1.1.8.2 tron
180 1.1.8.2 tron /*
181 1.1.8.2 tron * C' and M' multiplied by 256 to give integer math. Make sure to
182 1.1.8.2 tron * scale results using these back down, appropriately.
183 1.1.8.2 tron */
184 1.1.8.2 tron #define C_PRIME256(p) (((p->C - p->J) * p->K) + (p->J * 256))
185 1.1.8.2 tron #define M_PRIME256(p) (p->K * p->M)
186 1.1.8.2 tron
187 1.1.8.2 tron #define DIVIDE(x,y) (((x) + ((y) / 2)) / (y))
188 1.1.8.2 tron
189 1.1.8.2 tron /*
190 1.1.8.2 tron * print_value() - print the result of the named computation; this is
191 1.1.8.2 tron * useful when comparing against the GTF EXCEL spreadsheet.
192 1.1.8.2 tron */
193 1.1.8.2 tron
194 1.1.8.2 tron #ifdef GTFDEBUG
195 1.1.8.2 tron
196 1.1.8.2 tron static void
197 1.1.8.2 tron print_value(int n, const char *name, unsigned val)
198 1.1.8.2 tron {
199 1.1.8.2 tron printf("%2d: %-27s: %u\n", n, name, val);
200 1.1.8.2 tron }
201 1.1.8.2 tron #else
202 1.1.8.2 tron #define print_value(n, name, val)
203 1.1.8.2 tron #endif
204 1.1.8.2 tron
205 1.1.8.2 tron
206 1.1.8.2 tron /*
207 1.1.8.2 tron * vert_refresh() - as defined by the GTF Timing Standard, compute the
208 1.1.8.2 tron * Stage 1 Parameters using the vertical refresh frequency. In other
209 1.1.8.2 tron * words: input a desired resolution and desired refresh rate, and
210 1.1.8.2 tron * output the GTF mode timings.
211 1.1.8.2 tron *
212 1.1.8.2 tron * XXX All the code is in place to compute interlaced modes, but I don't
213 1.1.8.2 tron * feel like testing it right now.
214 1.1.8.2 tron *
215 1.1.8.2 tron * XXX margin computations are implemented but not tested (nor used by
216 1.1.8.2 tron * XFree86 of fbset mode descriptions, from what I can tell).
217 1.1.8.2 tron */
218 1.1.8.2 tron
219 1.1.8.2 tron void
220 1.1.8.2 tron vesagtf_mode_params(unsigned h_pixels, unsigned v_lines, unsigned freq,
221 1.1.8.2 tron struct vesagtf_params *params, int flags, struct videomode *vmp)
222 1.1.8.2 tron {
223 1.1.8.2 tron unsigned v_field_rqd;
224 1.1.8.2 tron unsigned top_margin;
225 1.1.8.2 tron unsigned bottom_margin;
226 1.1.8.2 tron unsigned interlace;
227 1.1.8.2 tron uint64_t h_period_est;
228 1.1.8.2 tron unsigned vsync_plus_bp;
229 1.1.8.2 tron unsigned v_back_porch;
230 1.1.8.2 tron unsigned total_v_lines;
231 1.1.8.2 tron uint64_t v_field_est;
232 1.1.8.2 tron uint64_t h_period;
233 1.1.8.2 tron unsigned v_field_rate;
234 1.1.8.2 tron unsigned v_frame_rate;
235 1.1.8.2 tron unsigned left_margin;
236 1.1.8.2 tron unsigned right_margin;
237 1.1.8.2 tron unsigned total_active_pixels;
238 1.1.8.2 tron uint64_t ideal_duty_cycle;
239 1.1.8.2 tron unsigned h_blank;
240 1.1.8.2 tron unsigned total_pixels;
241 1.1.8.2 tron unsigned pixel_freq;
242 1.1.8.2 tron
243 1.1.8.2 tron unsigned h_sync;
244 1.1.8.2 tron unsigned h_front_porch;
245 1.1.8.2 tron unsigned v_odd_front_porch_lines;
246 1.1.8.2 tron
247 1.1.8.2 tron #ifdef GTFDEBUG
248 1.1.8.2 tron unsigned h_freq;
249 1.1.8.2 tron #endif
250 1.1.8.2 tron
251 1.1.8.2 tron /* 1. In order to give correct results, the number of horizontal
252 1.1.8.2 tron * pixels requested is first processed to ensure that it is divisible
253 1.1.8.2 tron * by the character size, by rounding it to the nearest character
254 1.1.8.2 tron * cell boundary:
255 1.1.8.2 tron *
256 1.1.8.2 tron * [H PIXELS RND] = ((ROUND([H PIXELS]/[CELL GRAN RND],0))*[CELLGRAN RND])
257 1.1.8.2 tron */
258 1.1.8.2 tron
259 1.1.8.2 tron h_pixels = DIVIDE(h_pixels, CELL_GRAN) * CELL_GRAN;
260 1.1.8.2 tron
261 1.1.8.2 tron print_value(1, "[H PIXELS RND]", h_pixels);
262 1.1.8.2 tron
263 1.1.8.2 tron
264 1.1.8.2 tron /* 2. If interlace is requested, the number of vertical lines assumed
265 1.1.8.2 tron * by the calculation must be halved, as the computation calculates
266 1.1.8.2 tron * the number of vertical lines per field. In either case, the
267 1.1.8.2 tron * number of lines is rounded to the nearest integer.
268 1.1.8.2 tron *
269 1.1.8.2 tron * [V LINES RND] = IF([INT RQD?]="y", ROUND([V LINES]/2,0),
270 1.1.8.2 tron * ROUND([V LINES],0))
271 1.1.8.2 tron */
272 1.1.8.2 tron
273 1.1.8.2 tron v_lines = (flags & VESAGTF_FLAG_ILACE) ? DIVIDE(v_lines, 2) : v_lines;
274 1.1.8.2 tron
275 1.1.8.2 tron print_value(2, "[V LINES RND]", v_lines);
276 1.1.8.2 tron
277 1.1.8.2 tron
278 1.1.8.2 tron /* 3. Find the frame rate required:
279 1.1.8.2 tron *
280 1.1.8.2 tron * [V FIELD RATE RQD] = IF([INT RQD?]="y", [I/P FREQ RQD]*2,
281 1.1.8.2 tron * [I/P FREQ RQD])
282 1.1.8.2 tron */
283 1.1.8.2 tron
284 1.1.8.2 tron v_field_rqd = (flags & VESAGTF_FLAG_ILACE) ? (freq * 2) : (freq);
285 1.1.8.2 tron
286 1.1.8.2 tron print_value(3, "[V FIELD RATE RQD]", v_field_rqd);
287 1.1.8.2 tron
288 1.1.8.2 tron
289 1.1.8.2 tron /* 4. Find number of lines in Top margin:
290 1.1.8.2 tron * 5. Find number of lines in Bottom margin:
291 1.1.8.2 tron *
292 1.1.8.2 tron * [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
293 1.1.8.2 tron * ROUND(([MARGIN%]/100*[V LINES RND]),0),
294 1.1.8.2 tron * 0)
295 1.1.8.2 tron *
296 1.1.8.2 tron * Ditto for bottom margin. Note that instead of %, we use PPT, which
297 1.1.8.2 tron * is parts per thousand. This helps us with integer math.
298 1.1.8.2 tron */
299 1.1.8.2 tron
300 1.1.8.2 tron top_margin = bottom_margin = (flags & VESAGTF_FLAG_MARGINS) ?
301 1.1.8.2 tron DIVIDE(v_lines * params->margin_ppt, 1000) : 0;
302 1.1.8.2 tron
303 1.1.8.2 tron print_value(4, "[TOP MARGIN (LINES)]", top_margin);
304 1.1.8.2 tron print_value(5, "[BOT MARGIN (LINES)]", bottom_margin);
305 1.1.8.2 tron
306 1.1.8.2 tron
307 1.1.8.2 tron /* 6. If interlace is required, then set variable [INTERLACE]=0.5:
308 1.1.8.2 tron *
309 1.1.8.2 tron * [INTERLACE]=(IF([INT RQD?]="y",0.5,0))
310 1.1.8.2 tron *
311 1.1.8.2 tron * To make this integer friendly, we use some special hacks in step
312 1.1.8.2 tron * 7 below. Please read those comments to understand why I am using
313 1.1.8.2 tron * a whole number of 1.0 instead of 0.5 here.
314 1.1.8.2 tron */
315 1.1.8.2 tron interlace = (flags & VESAGTF_FLAG_ILACE) ? 1 : 0;
316 1.1.8.2 tron
317 1.1.8.2 tron print_value(6, "[2*INTERLACE]", interlace);
318 1.1.8.2 tron
319 1.1.8.2 tron
320 1.1.8.2 tron /* 7. Estimate the Horizontal period
321 1.1.8.2 tron *
322 1.1.8.2 tron * [H PERIOD EST] = ((1/[V FIELD RATE RQD]) - [MIN VSYNC+BP]/1000000) /
323 1.1.8.2 tron * ([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
324 1.1.8.2 tron * [MIN PORCH RND]+[INTERLACE]) * 1000000
325 1.1.8.2 tron *
326 1.1.8.2 tron * To make it integer friendly, we pre-multiply the 1000000 to get to
327 1.1.8.2 tron * usec. This gives us:
328 1.1.8.2 tron *
329 1.1.8.2 tron * [H PERIOD EST] = ((1000000/[V FIELD RATE RQD]) - [MIN VSYNC+BP]) /
330 1.1.8.2 tron * ([V LINES RND] + (2 * [TOP MARGIN (LINES)]) +
331 1.1.8.2 tron * [MIN PORCH RND]+[INTERLACE])
332 1.1.8.2 tron *
333 1.1.8.2 tron * The other problem is that the interlace value is wrong. To get
334 1.1.8.2 tron * the interlace to a whole number, we multiply both the numerator and
335 1.1.8.2 tron * divisor by 2, so we can use a value of either 1 or 0 for the interlace
336 1.1.8.2 tron * factor.
337 1.1.8.2 tron *
338 1.1.8.2 tron * This gives us:
339 1.1.8.2 tron *
340 1.1.8.2 tron * [H PERIOD EST] = ((2*((1000000/[V FIELD RATE RQD]) - [MIN VSYNC+BP])) /
341 1.1.8.2 tron * (2*([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
342 1.1.8.2 tron * [MIN PORCH RND]) + [2*INTERLACE]))
343 1.1.8.2 tron *
344 1.1.8.2 tron * Finally we multiply by another 1000, to get value in picosec.
345 1.1.8.2 tron * Why picosec? To minimize rounding errors. Gotta love integer
346 1.1.8.2 tron * math and error propogation.
347 1.1.8.2 tron */
348 1.1.8.2 tron
349 1.1.8.2 tron h_period_est = DIVIDE(((DIVIDE(2000000000000ULL, v_field_rqd)) -
350 1.1.8.2 tron (2000000 * params->min_vsbp)),
351 1.1.8.2 tron ((2 * (v_lines + (2 * top_margin) + params->min_porch)) + interlace));
352 1.1.8.2 tron
353 1.1.8.2 tron print_value(7, "[H PERIOD EST (ps)]", h_period_est);
354 1.1.8.2 tron
355 1.1.8.2 tron
356 1.1.8.2 tron /* 8. Find the number of lines in V sync + back porch:
357 1.1.8.2 tron *
358 1.1.8.2 tron * [V SYNC+BP] = ROUND(([MIN VSYNC+BP]/[H PERIOD EST]),0)
359 1.1.8.2 tron *
360 1.1.8.2 tron * But recall that h_period_est is in psec. So multiply by 1000000.
361 1.1.8.2 tron */
362 1.1.8.2 tron
363 1.1.8.2 tron vsync_plus_bp = DIVIDE(params->min_vsbp * 1000000, h_period_est);
364 1.1.8.2 tron
365 1.1.8.2 tron print_value(8, "[V SYNC+BP]", vsync_plus_bp);
366 1.1.8.2 tron
367 1.1.8.2 tron
368 1.1.8.2 tron /* 9. Find the number of lines in V back porch alone:
369 1.1.8.2 tron *
370 1.1.8.2 tron * [V BACK PORCH] = [V SYNC+BP] - [V SYNC RND]
371 1.1.8.2 tron *
372 1.1.8.2 tron * XXX is "[V SYNC RND]" a typo? should be [V SYNC RQD]?
373 1.1.8.2 tron */
374 1.1.8.2 tron
375 1.1.8.2 tron v_back_porch = vsync_plus_bp - params->vsync_rqd;
376 1.1.8.2 tron
377 1.1.8.2 tron print_value(9, "[V BACK PORCH]", v_back_porch);
378 1.1.8.2 tron
379 1.1.8.2 tron
380 1.1.8.2 tron /* 10. Find the total number of lines in Vertical field period:
381 1.1.8.2 tron *
382 1.1.8.2 tron * [TOTAL V LINES] = [V LINES RND] + [TOP MARGIN (LINES)] +
383 1.1.8.2 tron * [BOT MARGIN (LINES)] + [V SYNC+BP] + [INTERLACE] +
384 1.1.8.2 tron * [MIN PORCH RND]
385 1.1.8.2 tron */
386 1.1.8.2 tron
387 1.1.8.2 tron total_v_lines = v_lines + top_margin + bottom_margin + vsync_plus_bp +
388 1.1.8.2 tron interlace + params->min_porch;
389 1.1.8.2 tron
390 1.1.8.2 tron print_value(10, "[TOTAL V LINES]", total_v_lines);
391 1.1.8.2 tron
392 1.1.8.2 tron
393 1.1.8.2 tron /* 11. Estimate the Vertical field frequency:
394 1.1.8.2 tron *
395 1.1.8.2 tron * [V FIELD RATE EST] = 1 / [H PERIOD EST] / [TOTAL V LINES] * 1000000
396 1.1.8.2 tron *
397 1.1.8.2 tron * Again, we want to pre multiply by 10^9 to convert for nsec, thereby
398 1.1.8.2 tron * making it usable in integer math.
399 1.1.8.2 tron *
400 1.1.8.2 tron * So we get:
401 1.1.8.2 tron *
402 1.1.8.2 tron * [V FIELD RATE EST] = 1000000000 / [H PERIOD EST] / [TOTAL V LINES]
403 1.1.8.2 tron *
404 1.1.8.2 tron * This is all scaled to get the result in uHz. Again, we're trying to
405 1.1.8.2 tron * minimize error propogation.
406 1.1.8.2 tron */
407 1.1.8.2 tron v_field_est = DIVIDE(DIVIDE(1000000000000000ULL, h_period_est),
408 1.1.8.2 tron total_v_lines);
409 1.1.8.2 tron
410 1.1.8.2 tron print_value(11, "[V FIELD RATE EST(uHz)]", v_field_est);
411 1.1.8.2 tron
412 1.1.8.2 tron
413 1.1.8.2 tron /* 12. Find the actual horizontal period:
414 1.1.8.2 tron *
415 1.1.8.2 tron * [H PERIOD] = [H PERIOD EST] / ([V FIELD RATE RQD] / [V FIELD RATE EST])
416 1.1.8.2 tron */
417 1.1.8.2 tron
418 1.1.8.2 tron h_period = DIVIDE(h_period_est * v_field_est, v_field_rqd * 1000);
419 1.1.8.2 tron
420 1.1.8.2 tron print_value(12, "[H PERIOD(ps)]", h_period);
421 1.1.8.2 tron
422 1.1.8.2 tron
423 1.1.8.2 tron /* 13. Find the actual Vertical field frequency:
424 1.1.8.2 tron *
425 1.1.8.2 tron * [V FIELD RATE] = 1 / [H PERIOD] / [TOTAL V LINES] * 1000000
426 1.1.8.2 tron *
427 1.1.8.2 tron * And again, we convert to nsec ahead of time, giving us:
428 1.1.8.2 tron *
429 1.1.8.2 tron * [V FIELD RATE] = 1000000 / [H PERIOD] / [TOTAL V LINES]
430 1.1.8.2 tron *
431 1.1.8.2 tron * And another rescaling back to mHz. Gotta love it.
432 1.1.8.2 tron */
433 1.1.8.2 tron
434 1.1.8.2 tron v_field_rate = DIVIDE(1000000000000ULL, h_period * total_v_lines);
435 1.1.8.2 tron
436 1.1.8.2 tron print_value(13, "[V FIELD RATE]", v_field_rate);
437 1.1.8.2 tron
438 1.1.8.2 tron
439 1.1.8.2 tron /* 14. Find the Vertical frame frequency:
440 1.1.8.2 tron *
441 1.1.8.2 tron * [V FRAME RATE] = (IF([INT RQD?]="y", [V FIELD RATE]/2, [V FIELD RATE]))
442 1.1.8.2 tron *
443 1.1.8.2 tron * N.B. that the result here is in mHz.
444 1.1.8.2 tron */
445 1.1.8.2 tron
446 1.1.8.2 tron v_frame_rate = (flags & VESAGTF_FLAG_ILACE) ?
447 1.1.8.2 tron v_field_rate / 2 : v_field_rate;
448 1.1.8.2 tron
449 1.1.8.2 tron print_value(14, "[V FRAME RATE]", v_frame_rate);
450 1.1.8.2 tron
451 1.1.8.2 tron
452 1.1.8.2 tron /* 15. Find number of pixels in left margin:
453 1.1.8.2 tron * 16. Find number of pixels in right margin:
454 1.1.8.2 tron *
455 1.1.8.2 tron * [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
456 1.1.8.2 tron * (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
457 1.1.8.2 tron * [CELL GRAN RND]),0)) * [CELL GRAN RND],
458 1.1.8.2 tron * 0))
459 1.1.8.2 tron *
460 1.1.8.2 tron * Again, we deal with margin percentages as PPT (parts per thousand).
461 1.1.8.2 tron * And the calculations for left and right are the same.
462 1.1.8.2 tron */
463 1.1.8.2 tron
464 1.1.8.2 tron left_margin = right_margin = (flags & VESAGTF_FLAG_MARGINS) ?
465 1.1.8.2 tron DIVIDE(DIVIDE(h_pixels * params->margin_ppt, 1000),
466 1.1.8.2 tron CELL_GRAN) * CELL_GRAN : 0;
467 1.1.8.2 tron
468 1.1.8.2 tron print_value(15, "[LEFT MARGIN (PIXELS)]", left_margin);
469 1.1.8.2 tron print_value(16, "[RIGHT MARGIN (PIXELS)]", right_margin);
470 1.1.8.2 tron
471 1.1.8.2 tron
472 1.1.8.2 tron /* 17. Find total number of active pixels in image and left and right
473 1.1.8.2 tron * margins:
474 1.1.8.2 tron *
475 1.1.8.2 tron * [TOTAL ACTIVE PIXELS] = [H PIXELS RND] + [LEFT MARGIN (PIXELS)] +
476 1.1.8.2 tron * [RIGHT MARGIN (PIXELS)]
477 1.1.8.2 tron */
478 1.1.8.2 tron
479 1.1.8.2 tron total_active_pixels = h_pixels + left_margin + right_margin;
480 1.1.8.2 tron
481 1.1.8.2 tron print_value(17, "[TOTAL ACTIVE PIXELS]", total_active_pixels);
482 1.1.8.2 tron
483 1.1.8.2 tron
484 1.1.8.2 tron /* 18. Find the ideal blanking duty cycle from the blanking duty cycle
485 1.1.8.2 tron * equation:
486 1.1.8.2 tron *
487 1.1.8.2 tron * [IDEAL DUTY CYCLE] = [C'] - ([M']*[H PERIOD]/1000)
488 1.1.8.2 tron *
489 1.1.8.2 tron * However, we have modified values for [C'] as [256*C'] and
490 1.1.8.2 tron * [M'] as [256*M']. Again the idea here is to get good scaling.
491 1.1.8.2 tron * We use 256 as the factor to make the math fast.
492 1.1.8.2 tron *
493 1.1.8.2 tron * Note that this means that we have to scale it appropriately in
494 1.1.8.2 tron * later calculations.
495 1.1.8.2 tron *
496 1.1.8.2 tron * The ending result is that our ideal_duty_cycle is 256000x larger
497 1.1.8.2 tron * than the duty cycle used by VESA. But again, this reduces error
498 1.1.8.2 tron * propogation.
499 1.1.8.2 tron */
500 1.1.8.2 tron
501 1.1.8.2 tron ideal_duty_cycle =
502 1.1.8.2 tron ((C_PRIME256(params) * 1000) -
503 1.1.8.2 tron (M_PRIME256(params) * h_period / 1000000));
504 1.1.8.2 tron
505 1.1.8.2 tron print_value(18, "[IDEAL DUTY CYCLE]", ideal_duty_cycle);
506 1.1.8.2 tron
507 1.1.8.2 tron
508 1.1.8.2 tron /* 19. Find the number of pixels in the blanking time to the nearest
509 1.1.8.2 tron * double character cell:
510 1.1.8.2 tron *
511 1.1.8.2 tron * [H BLANK (PIXELS)] = (ROUND(([TOTAL ACTIVE PIXELS] *
512 1.1.8.2 tron * [IDEAL DUTY CYCLE] /
513 1.1.8.2 tron * (100-[IDEAL DUTY CYCLE]) /
514 1.1.8.2 tron * (2*[CELL GRAN RND])), 0))
515 1.1.8.2 tron * * (2*[CELL GRAN RND])
516 1.1.8.2 tron *
517 1.1.8.2 tron * Of course, we adjust to make this rounding work in integer math.
518 1.1.8.2 tron */
519 1.1.8.2 tron
520 1.1.8.2 tron h_blank = DIVIDE(DIVIDE(total_active_pixels * ideal_duty_cycle,
521 1.1.8.2 tron (256000 * 100ULL) - ideal_duty_cycle),
522 1.1.8.2 tron 2 * CELL_GRAN) * (2 * CELL_GRAN);
523 1.1.8.2 tron
524 1.1.8.2 tron print_value(19, "[H BLANK (PIXELS)]", h_blank);
525 1.1.8.2 tron
526 1.1.8.2 tron
527 1.1.8.2 tron /* 20. Find total number of pixels:
528 1.1.8.2 tron *
529 1.1.8.2 tron * [TOTAL PIXELS] = [TOTAL ACTIVE PIXELS] + [H BLANK (PIXELS)]
530 1.1.8.2 tron */
531 1.1.8.2 tron
532 1.1.8.2 tron total_pixels = total_active_pixels + h_blank;
533 1.1.8.2 tron
534 1.1.8.2 tron print_value(20, "[TOTAL PIXELS]", total_pixels);
535 1.1.8.2 tron
536 1.1.8.2 tron
537 1.1.8.2 tron /* 21. Find pixel clock frequency:
538 1.1.8.2 tron *
539 1.1.8.2 tron * [PIXEL FREQ] = [TOTAL PIXELS] / [H PERIOD]
540 1.1.8.2 tron *
541 1.1.8.2 tron * We calculate this in Hz rather than MHz, to get a value that
542 1.1.8.2 tron * is usable with integer math. Recall that the [H PERIOD] is in
543 1.1.8.2 tron * nsec.
544 1.1.8.2 tron */
545 1.1.8.2 tron
546 1.1.8.2 tron pixel_freq = DIVIDE(total_pixels * 1000000, DIVIDE(h_period, 1000));
547 1.1.8.2 tron
548 1.1.8.2 tron print_value(21, "[PIXEL FREQ]", pixel_freq);
549 1.1.8.2 tron
550 1.1.8.2 tron
551 1.1.8.2 tron /* 22. Find horizontal frequency:
552 1.1.8.2 tron *
553 1.1.8.2 tron * [H FREQ] = 1000 / [H PERIOD]
554 1.1.8.2 tron *
555 1.1.8.2 tron * I've ifdef'd this out, because we don't need it for any of
556 1.1.8.2 tron * our calculations.
557 1.1.8.2 tron * We calculate this in Hz rather than kHz, to avoid rounding
558 1.1.8.2 tron * errors. Recall that the [H PERIOD] is in usec.
559 1.1.8.2 tron */
560 1.1.8.2 tron
561 1.1.8.2 tron #ifdef GTFDEBUG
562 1.1.8.2 tron h_freq = 1000000000 / h_period;
563 1.1.8.2 tron
564 1.1.8.2 tron print_value(22, "[H FREQ]", h_freq);
565 1.1.8.2 tron #endif
566 1.1.8.2 tron
567 1.1.8.2 tron
568 1.1.8.2 tron
569 1.1.8.2 tron /* Stage 1 computations are now complete; I should really pass
570 1.1.8.2 tron the results to another function and do the Stage 2
571 1.1.8.2 tron computations, but I only need a few more values so I'll just
572 1.1.8.2 tron append the computations here for now */
573 1.1.8.2 tron
574 1.1.8.2 tron
575 1.1.8.2 tron
576 1.1.8.2 tron /* 17. Find the number of pixels in the horizontal sync period:
577 1.1.8.2 tron *
578 1.1.8.2 tron * [H SYNC (PIXELS)] =(ROUND(([H SYNC%] / 100 * [TOTAL PIXELS] /
579 1.1.8.2 tron * [CELL GRAN RND]),0))*[CELL GRAN RND]
580 1.1.8.2 tron *
581 1.1.8.2 tron * Rewriting for integer math:
582 1.1.8.2 tron *
583 1.1.8.2 tron * [H SYNC (PIXELS)]=(ROUND((H SYNC%] * [TOTAL PIXELS] / 100 /
584 1.1.8.2 tron * [CELL GRAN RND),0))*[CELL GRAN RND]
585 1.1.8.2 tron */
586 1.1.8.2 tron
587 1.1.8.2 tron h_sync = DIVIDE(((params->hsync_pct * total_pixels) / 100), CELL_GRAN) *
588 1.1.8.2 tron CELL_GRAN;
589 1.1.8.2 tron
590 1.1.8.2 tron print_value(17, "[H SYNC (PIXELS)]", h_sync);
591 1.1.8.2 tron
592 1.1.8.2 tron
593 1.1.8.2 tron /* 18. Find the number of pixels in the horizontal front porch period:
594 1.1.8.2 tron *
595 1.1.8.2 tron * [H FRONT PORCH (PIXELS)] = ([H BLANK (PIXELS)]/2)-[H SYNC (PIXELS)]
596 1.1.8.2 tron *
597 1.1.8.2 tron * Note that h_blank is always an even number of characters (i.e.
598 1.1.8.2 tron * h_blank % (CELL_GRAN * 2) == 0)
599 1.1.8.2 tron */
600 1.1.8.2 tron
601 1.1.8.2 tron h_front_porch = (h_blank / 2) - h_sync;
602 1.1.8.2 tron
603 1.1.8.2 tron print_value(18, "[H FRONT PORCH (PIXELS)]", h_front_porch);
604 1.1.8.2 tron
605 1.1.8.2 tron
606 1.1.8.2 tron /* 36. Find the number of lines in the odd front porch period:
607 1.1.8.2 tron *
608 1.1.8.2 tron * [V ODD FRONT PORCH(LINES)]=([MIN PORCH RND]+[INTERLACE])
609 1.1.8.2 tron *
610 1.1.8.2 tron * Adjusting for the fact that the interlace is scaled:
611 1.1.8.2 tron *
612 1.1.8.2 tron * [V ODD FRONT PORCH(LINES)]=(([MIN PORCH RND] * 2) + [2*INTERLACE]) / 2
613 1.1.8.2 tron */
614 1.1.8.2 tron
615 1.1.8.2 tron v_odd_front_porch_lines = ((2 * params->min_porch) + interlace) / 2;
616 1.1.8.2 tron
617 1.1.8.2 tron print_value(36, "[V ODD FRONT PORCH(LINES)]", v_odd_front_porch_lines);
618 1.1.8.2 tron
619 1.1.8.2 tron
620 1.1.8.2 tron /* finally, pack the results in the mode struct */
621 1.1.8.2 tron
622 1.1.8.2 tron vmp->hsync_start = h_pixels + h_front_porch;
623 1.1.8.2 tron vmp->hsync_end = vmp->hsync_start + h_sync;
624 1.1.8.2 tron vmp->htotal = total_pixels;
625 1.1.8.2 tron vmp->hdisplay = h_pixels;
626 1.1.8.2 tron
627 1.1.8.2 tron vmp->vsync_start = v_lines + v_odd_front_porch_lines;
628 1.1.8.2 tron vmp->vsync_end = vmp->vsync_start + params->vsync_rqd;
629 1.1.8.2 tron vmp->vtotal = total_v_lines;
630 1.1.8.2 tron vmp->vdisplay = v_lines;
631 1.1.8.2 tron
632 1.1.8.2 tron vmp->dot_clock = pixel_freq;
633 1.1.8.2 tron
634 1.1.8.2 tron }
635 1.1.8.2 tron
636 1.1.8.2 tron void
637 1.1.8.2 tron vesagtf_mode(unsigned x, unsigned y, unsigned refresh, struct videomode *vmp)
638 1.1.8.2 tron {
639 1.1.8.2 tron struct vesagtf_params params;
640 1.1.8.2 tron
641 1.1.8.2 tron params.margin_ppt = VESAGTF_MARGIN_PPT;
642 1.1.8.2 tron params.min_porch = VESAGTF_MIN_PORCH;
643 1.1.8.2 tron params.vsync_rqd = VESAGTF_VSYNC_RQD;
644 1.1.8.2 tron params.hsync_pct = VESAGTF_HSYNC_PCT;
645 1.1.8.2 tron params.min_vsbp = VESAGTF_MIN_VSBP;
646 1.1.8.2 tron params.M = VESAGTF_M;
647 1.1.8.2 tron params.C = VESAGTF_C;
648 1.1.8.2 tron params.K = VESAGTF_K;
649 1.1.8.2 tron params.J = VESAGTF_J;
650 1.1.8.2 tron
651 1.1.8.2 tron vesagtf_mode_params(x, y, refresh, ¶ms, 0, vmp);
652 1.1.8.2 tron }
653 1.1.8.2 tron
654 1.1.8.2 tron /*
655 1.1.8.2 tron * The tidbit here is so that you can compile this file as a
656 1.1.8.2 tron * standalone user program to generate X11 modelines using VESA GTF.
657 1.1.8.2 tron * This also allows for testing of the code itself, without
658 1.1.8.2 tron * necessitating a full kernel recompile.
659 1.1.8.2 tron */
660 1.1.8.2 tron
661 1.1.8.2 tron /* print_xf86_mode() - print the XFree86 modeline, given mode timings. */
662 1.1.8.2 tron
663 1.1.8.2 tron #ifndef _KERNEL
664 1.1.8.2 tron void
665 1.1.8.2 tron print_xf86_mode (struct videomode *vmp)
666 1.1.8.2 tron {
667 1.1.8.2 tron float vf, hf;
668 1.1.8.2 tron
669 1.1.8.2 tron hf = 1000.0 * vmp->dot_clock / vmp->htotal;
670 1.1.8.2 tron vf = 1.0 * hf / vmp->vtotal;
671 1.1.8.2 tron
672 1.1.8.2 tron printf("\n");
673 1.1.8.2 tron printf(" # %dx%d @ %.2f Hz (GTF) hsync: %.2f kHz; pclk: %.2f MHz\n",
674 1.1.8.2 tron vmp->hdisplay, vmp->vdisplay, vf, hf, vmp->dot_clock / 1000.0);
675 1.1.8.2 tron
676 1.1.8.2 tron printf(" Modeline \"%dx%d_%.2f\" %.2f"
677 1.1.8.2 tron " %d %d %d %d"
678 1.1.8.2 tron " %d %d %d %d"
679 1.1.8.2 tron " -HSync +Vsync\n\n",
680 1.1.8.2 tron vmp->hdisplay, vmp->vdisplay, vf, (vmp->dot_clock / 1000.0),
681 1.1.8.2 tron vmp->hdisplay, vmp->hsync_start, vmp->hsync_end, vmp->htotal,
682 1.1.8.2 tron vmp->vdisplay, vmp->vsync_start, vmp->vsync_end, vmp->vtotal);
683 1.1.8.2 tron }
684 1.1.8.2 tron
685 1.1.8.2 tron int
686 1.1.8.2 tron main (int argc, char *argv[])
687 1.1.8.2 tron {
688 1.1.8.2 tron struct videomode m;
689 1.1.8.2 tron
690 1.1.8.2 tron if (argc != 4) {
691 1.1.8.2 tron printf("usage: %s x y refresh\n", argv[0]);
692 1.1.8.2 tron exit(1);
693 1.1.8.2 tron }
694 1.1.8.2 tron
695 1.1.8.2 tron vesagtf_mode(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), &m);
696 1.1.8.2 tron
697 1.1.8.2 tron print_xf86_mode(&m);
698 1.1.8.2 tron
699 1.1.8.2 tron return 0;
700 1.1.8.2 tron
701 1.1.8.2 tron }
702 1.1.8.2 tron #endif
703