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