xref: /netbsd-src/sys/dev/videomode/vesagtf.c (revision 08b20cda6b2b0efeaebe2429dd7ea86410d163f1)
1*08b20cdaSmlelstv /* $NetBSD: vesagtf.c,v 1.4 2021/12/25 13:51:31 mlelstv Exp $ */
26594783dSgdamore 
36594783dSgdamore /*-
46594783dSgdamore  * Copyright (c) 2006 Itronix Inc.
56594783dSgdamore  * All rights reserved.
66594783dSgdamore  *
76594783dSgdamore  * Written by Garrett D'Amore for Itronix Inc.
86594783dSgdamore  *
96594783dSgdamore  * Redistribution and use in source and binary forms, with or without
106594783dSgdamore  * modification, are permitted provided that the following conditions
116594783dSgdamore  * are met:
126594783dSgdamore  * 1. Redistributions of source code must retain the above copyright
136594783dSgdamore  *    notice, this list of conditions and the following disclaimer.
146594783dSgdamore  * 2. Redistributions in binary form must reproduce the above copyright
156594783dSgdamore  *    notice, this list of conditions and the following disclaimer in the
166594783dSgdamore  *    documentation and/or other materials provided with the distribution.
176594783dSgdamore  * 3. The name of Itronix Inc. may not be used to endorse
186594783dSgdamore  *    or promote products derived from this software without specific
196594783dSgdamore  *    prior written permission.
206594783dSgdamore  *
216594783dSgdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
226594783dSgdamore  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
236594783dSgdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
246594783dSgdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
256594783dSgdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
266594783dSgdamore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
276594783dSgdamore  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
286594783dSgdamore  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
296594783dSgdamore  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
306594783dSgdamore  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
316594783dSgdamore  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
326594783dSgdamore  */
336594783dSgdamore 
346594783dSgdamore /*
356594783dSgdamore  * This was derived from a userland GTF program supplied by NVIDIA.
366594783dSgdamore  * NVIDIA's original boilerplate follows.
376594783dSgdamore  *
386594783dSgdamore  * Note that I have heavily modified the program for use in the EDID
396594783dSgdamore  * kernel code for NetBSD, including removing the use of floating
406594783dSgdamore  * point operations and making significant adjustments to minimize
419e949733Sdholland  * error propagation while operating with integer only math.
426594783dSgdamore  *
436594783dSgdamore  * This has required the use of 64-bit integers in a few places, but
446594783dSgdamore  * the upshot is that for a calculation of 1920x1200x85 (as an
456594783dSgdamore  * example), the error deviates by only ~.004% relative to the
466594783dSgdamore  * floating point version.  This error is *well* within VESA
476594783dSgdamore  * tolerances.
486594783dSgdamore  */
496594783dSgdamore 
506594783dSgdamore /*
516594783dSgdamore  * Copyright (c) 2001, Andy Ritger  aritger@nvidia.com
526594783dSgdamore  * All rights reserved.
536594783dSgdamore  *
546594783dSgdamore  * Redistribution and use in source and binary forms, with or without
556594783dSgdamore  * modification, are permitted provided that the following conditions
566594783dSgdamore  * are met:
576594783dSgdamore  *
586594783dSgdamore  * o Redistributions of source code must retain the above copyright
596594783dSgdamore  *   notice, this list of conditions and the following disclaimer.
606594783dSgdamore  * o Redistributions in binary form must reproduce the above copyright
616594783dSgdamore  *   notice, this list of conditions and the following disclaimer
626594783dSgdamore  *   in the documentation and/or other materials provided with the
636594783dSgdamore  *   distribution.
646594783dSgdamore  * o Neither the name of NVIDIA nor the names of its contributors
656594783dSgdamore  *   may be used to endorse or promote products derived from this
666594783dSgdamore  *   software without specific prior written permission.
676594783dSgdamore  *
686594783dSgdamore  *
696594783dSgdamore  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
706594783dSgdamore  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
716594783dSgdamore  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
726594783dSgdamore  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
736594783dSgdamore  * THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
746594783dSgdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
756594783dSgdamore  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
766594783dSgdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
776594783dSgdamore  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
786594783dSgdamore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
796594783dSgdamore  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
806594783dSgdamore  * POSSIBILITY OF SUCH DAMAGE.
816594783dSgdamore  *
826594783dSgdamore  *
836594783dSgdamore  *
846594783dSgdamore  * This program is based on the Generalized Timing Formula(GTF TM)
856594783dSgdamore  * Standard Version: 1.0, Revision: 1.0
866594783dSgdamore  *
876594783dSgdamore  * The GTF Document contains the following Copyright information:
886594783dSgdamore  *
896594783dSgdamore  * Copyright (c) 1994, 1995, 1996 - Video Electronics Standards
906594783dSgdamore  * Association. Duplication of this document within VESA member
916594783dSgdamore  * companies for review purposes is permitted. All other rights
926594783dSgdamore  * reserved.
936594783dSgdamore  *
946594783dSgdamore  * While every precaution has been taken in the preparation
956594783dSgdamore  * of this standard, the Video Electronics Standards Association and
966594783dSgdamore  * its contributors assume no responsibility for errors or omissions,
976594783dSgdamore  * and make no warranties, expressed or implied, of functionality
986594783dSgdamore  * of suitability for any purpose. The sample code contained within
996594783dSgdamore  * this standard may be used without restriction.
1006594783dSgdamore  *
1016594783dSgdamore  *
1026594783dSgdamore  *
1036594783dSgdamore  * The GTF EXCEL(TM) SPREADSHEET, a sample (and the definitive)
1046594783dSgdamore  * implementation of the GTF Timing Standard, is available at:
1056594783dSgdamore  *
1066594783dSgdamore  * ftp://ftp.vesa.org/pub/GTF/GTF_V1R1.xls
1076594783dSgdamore  *
1086594783dSgdamore  *
1096594783dSgdamore  *
1106594783dSgdamore  * This program takes a desired resolution and vertical refresh rate,
1116594783dSgdamore  * and computes mode timings according to the GTF Timing Standard.
1126594783dSgdamore  * These mode timings can then be formatted as an XFree86 modeline
1136594783dSgdamore  * or a mode description for use by fbset(8).
1146594783dSgdamore  *
1156594783dSgdamore  *
1166594783dSgdamore  *
1176594783dSgdamore  * NOTES:
1186594783dSgdamore  *
1196594783dSgdamore  * The GTF allows for computation of "margins" (the visible border
1206594783dSgdamore  * surrounding the addressable video); on most non-overscan type
1216594783dSgdamore  * systems, the margin period is zero.  I've implemented the margin
1226594783dSgdamore  * computations but not enabled it because 1) I don't really have
1236594783dSgdamore  * any experience with this, and 2) neither XFree86 modelines nor
1246594783dSgdamore  * fbset fb.modes provide an obvious way for margin timings to be
1256594783dSgdamore  * included in their mode descriptions (needs more investigation).
1266594783dSgdamore  *
1276594783dSgdamore  * The GTF provides for computation of interlaced mode timings;
1286594783dSgdamore  * I've implemented the computations but not enabled them, yet.
1296594783dSgdamore  * I should probably enable and test this at some point.
1306594783dSgdamore  *
1316594783dSgdamore  *
1326594783dSgdamore  *
1336594783dSgdamore  * TODO:
1346594783dSgdamore  *
1356594783dSgdamore  * o Add support for interlaced modes.
1366594783dSgdamore  *
1376594783dSgdamore  * o Implement the other portions of the GTF: compute mode timings
1386594783dSgdamore  *   given either the desired pixel clock or the desired horizontal
1396594783dSgdamore  *   frequency.
1406594783dSgdamore  *
1416594783dSgdamore  * o It would be nice if this were more general purpose to do things
1426594783dSgdamore  *   outside the scope of the GTF: like generate double scan mode
1436594783dSgdamore  *   timings, for example.
1446594783dSgdamore  *
1456594783dSgdamore  * o Printing digits to the right of the decimal point when the
1466594783dSgdamore  *   digits are 0 annoys me.
1476594783dSgdamore  *
1486594783dSgdamore  * o Error checking.
1496594783dSgdamore  *
1506594783dSgdamore  */
1516594783dSgdamore 
1526594783dSgdamore 
1536594783dSgdamore #ifdef	_KERNEL
1546594783dSgdamore #include <sys/cdefs.h>
1556594783dSgdamore 
156*08b20cdaSmlelstv __KERNEL_RCSID(0, "$NetBSD: vesagtf.c,v 1.4 2021/12/25 13:51:31 mlelstv Exp $");
1576594783dSgdamore #include <sys/types.h>
1586594783dSgdamore #include <sys/param.h>
1596594783dSgdamore #include <sys/systm.h>
1606594783dSgdamore #else
1616594783dSgdamore #include <stdio.h>
1626594783dSgdamore #include <stdlib.h>
163*08b20cdaSmlelstv #include <inttypes.h>
1646594783dSgdamore #endif
165*08b20cdaSmlelstv #include <dev/videomode/videomode.h>
166*08b20cdaSmlelstv #include <dev/videomode/vesagtf.h>
1676594783dSgdamore 
1686594783dSgdamore #define CELL_GRAN         8     /* assumed character cell granularity        */
1696594783dSgdamore 
1706594783dSgdamore /* C' and M' are part of the Blanking Duty Cycle computation */
1716594783dSgdamore /*
1726594783dSgdamore  * #define C_PRIME           (((C - J) * K/256.0) + J)
1736594783dSgdamore  * #define M_PRIME           (K/256.0 * M)
1746594783dSgdamore  */
1756594783dSgdamore 
1766594783dSgdamore /*
1776594783dSgdamore  * C' and M' multiplied by 256 to give integer math.  Make sure to
1786594783dSgdamore  * scale results using these back down, appropriately.
1796594783dSgdamore  */
1806594783dSgdamore #define	C_PRIME256(p)	  (((p->C - p->J) * p->K) + (p->J * 256))
1816594783dSgdamore #define	M_PRIME256(p)	  (p->K * p->M)
1826594783dSgdamore 
1836594783dSgdamore #define	DIVIDE(x,y)	(((x) + ((y) / 2)) / (y))
1846594783dSgdamore 
1856594783dSgdamore /*
1866594783dSgdamore  * print_value() - print the result of the named computation; this is
1876594783dSgdamore  * useful when comparing against the GTF EXCEL spreadsheet.
1886594783dSgdamore  */
1896594783dSgdamore 
1906594783dSgdamore #ifdef GTFDEBUG
1916594783dSgdamore 
1926594783dSgdamore static void
print_value(int n,const char * name,unsigned val)1936594783dSgdamore print_value(int n, const char *name, unsigned val)
1946594783dSgdamore {
1956594783dSgdamore         printf("%2d: %-27s: %u\n", n, name, val);
1966594783dSgdamore }
1976594783dSgdamore #else
1986594783dSgdamore #define	print_value(n, name, val)
1996594783dSgdamore #endif
2006594783dSgdamore 
2016594783dSgdamore 
2026594783dSgdamore /*
2036594783dSgdamore  * vert_refresh() - as defined by the GTF Timing Standard, compute the
2046594783dSgdamore  * Stage 1 Parameters using the vertical refresh frequency.  In other
2056594783dSgdamore  * words: input a desired resolution and desired refresh rate, and
2066594783dSgdamore  * output the GTF mode timings.
2076594783dSgdamore  *
2086594783dSgdamore  * XXX All the code is in place to compute interlaced modes, but I don't
2096594783dSgdamore  * feel like testing it right now.
2106594783dSgdamore  *
2116594783dSgdamore  * XXX margin computations are implemented but not tested (nor used by
2126594783dSgdamore  * XFree86 of fbset mode descriptions, from what I can tell).
2136594783dSgdamore  */
2146594783dSgdamore 
2156594783dSgdamore void
vesagtf_mode_params(unsigned h_pixels,unsigned v_lines,unsigned freq,struct vesagtf_params * params,int flags,struct videomode * vmp)2166594783dSgdamore vesagtf_mode_params(unsigned h_pixels, unsigned v_lines, unsigned freq,
2176594783dSgdamore     struct vesagtf_params *params, int flags, struct videomode *vmp)
2186594783dSgdamore {
2196594783dSgdamore     unsigned v_field_rqd;
2206594783dSgdamore     unsigned top_margin;
2216594783dSgdamore     unsigned bottom_margin;
2226594783dSgdamore     unsigned interlace;
2236594783dSgdamore     uint64_t h_period_est;
2246594783dSgdamore     unsigned vsync_plus_bp;
225e3d7ae4aSmartin     unsigned v_back_porch __unused;
2266594783dSgdamore     unsigned total_v_lines;
2276594783dSgdamore     uint64_t v_field_est;
2286594783dSgdamore     uint64_t h_period;
2296594783dSgdamore     unsigned v_field_rate;
230e3d7ae4aSmartin     unsigned v_frame_rate __unused;
2316594783dSgdamore     unsigned left_margin;
2326594783dSgdamore     unsigned right_margin;
2336594783dSgdamore     unsigned total_active_pixels;
2346594783dSgdamore     uint64_t ideal_duty_cycle;
2356594783dSgdamore     unsigned h_blank;
2366594783dSgdamore     unsigned total_pixels;
2376594783dSgdamore     unsigned pixel_freq;
2386594783dSgdamore 
2396594783dSgdamore     unsigned h_sync;
2406594783dSgdamore     unsigned h_front_porch;
2416594783dSgdamore     unsigned v_odd_front_porch_lines;
2426594783dSgdamore 
2436594783dSgdamore #ifdef	GTFDEBUG
2446594783dSgdamore     unsigned h_freq;
2456594783dSgdamore #endif
2466594783dSgdamore 
2476594783dSgdamore     /*  1. In order to give correct results, the number of horizontal
2486594783dSgdamore      *  pixels requested is first processed to ensure that it is divisible
2496594783dSgdamore      *  by the character size, by rounding it to the nearest character
2506594783dSgdamore      *  cell boundary:
2516594783dSgdamore      *
2526594783dSgdamore      *  [H PIXELS RND] = ((ROUND([H PIXELS]/[CELL GRAN RND],0))*[CELLGRAN RND])
2536594783dSgdamore      */
2546594783dSgdamore 
2556594783dSgdamore     h_pixels = DIVIDE(h_pixels, CELL_GRAN) * CELL_GRAN;
2566594783dSgdamore 
2576594783dSgdamore     print_value(1, "[H PIXELS RND]", h_pixels);
2586594783dSgdamore 
2596594783dSgdamore 
2606594783dSgdamore     /*  2. If interlace is requested, the number of vertical lines assumed
2616594783dSgdamore      *  by the calculation must be halved, as the computation calculates
2626594783dSgdamore      *  the number of vertical lines per field. In either case, the
2636594783dSgdamore      *  number of lines is rounded to the nearest integer.
2646594783dSgdamore      *
2656594783dSgdamore      *  [V LINES RND] = IF([INT RQD?]="y", ROUND([V LINES]/2,0),
2666594783dSgdamore      *                                     ROUND([V LINES],0))
2676594783dSgdamore      */
2686594783dSgdamore 
2696594783dSgdamore     v_lines = (flags & VESAGTF_FLAG_ILACE) ? DIVIDE(v_lines, 2) : v_lines;
2706594783dSgdamore 
2716594783dSgdamore     print_value(2, "[V LINES RND]", v_lines);
2726594783dSgdamore 
2736594783dSgdamore 
2746594783dSgdamore     /*  3. Find the frame rate required:
2756594783dSgdamore      *
2766594783dSgdamore      *  [V FIELD RATE RQD] = IF([INT RQD?]="y", [I/P FREQ RQD]*2,
2776594783dSgdamore      *                                          [I/P FREQ RQD])
2786594783dSgdamore      */
2796594783dSgdamore 
2806594783dSgdamore     v_field_rqd = (flags & VESAGTF_FLAG_ILACE) ? (freq * 2) : (freq);
2816594783dSgdamore 
2826594783dSgdamore     print_value(3, "[V FIELD RATE RQD]", v_field_rqd);
2836594783dSgdamore 
2846594783dSgdamore 
2856594783dSgdamore     /*  4. Find number of lines in Top margin:
2866594783dSgdamore      *  5. Find number of lines in Bottom margin:
2876594783dSgdamore      *
2886594783dSgdamore      *  [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
2896594783dSgdamore      *          ROUND(([MARGIN%]/100*[V LINES RND]),0),
2906594783dSgdamore      *          0)
2916594783dSgdamore      *
2926594783dSgdamore      *  Ditto for bottom margin.  Note that instead of %, we use PPT, which
2936594783dSgdamore      *  is parts per thousand.  This helps us with integer math.
2946594783dSgdamore      */
2956594783dSgdamore 
2966594783dSgdamore     top_margin = bottom_margin = (flags & VESAGTF_FLAG_MARGINS) ?
2976594783dSgdamore 	DIVIDE(v_lines * params->margin_ppt, 1000) : 0;
2986594783dSgdamore 
2996594783dSgdamore     print_value(4, "[TOP MARGIN (LINES)]", top_margin);
3006594783dSgdamore     print_value(5, "[BOT MARGIN (LINES)]", bottom_margin);
3016594783dSgdamore 
3026594783dSgdamore 
3036594783dSgdamore     /*  6. If interlace is required, then set variable [INTERLACE]=0.5:
3046594783dSgdamore      *
3056594783dSgdamore      *  [INTERLACE]=(IF([INT RQD?]="y",0.5,0))
3066594783dSgdamore      *
3076594783dSgdamore      *  To make this integer friendly, we use some special hacks in step
3086594783dSgdamore      *  7 below.  Please read those comments to understand why I am using
3096594783dSgdamore      *  a whole number of 1.0 instead of 0.5 here.
3106594783dSgdamore      */
3116594783dSgdamore     interlace = (flags & VESAGTF_FLAG_ILACE) ? 1 : 0;
3126594783dSgdamore 
3136594783dSgdamore     print_value(6, "[2*INTERLACE]", interlace);
3146594783dSgdamore 
3156594783dSgdamore 
3166594783dSgdamore     /*  7. Estimate the Horizontal period
3176594783dSgdamore      *
3186594783dSgdamore      *  [H PERIOD EST] = ((1/[V FIELD RATE RQD]) - [MIN VSYNC+BP]/1000000) /
3196594783dSgdamore      *                    ([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
3206594783dSgdamore      *                     [MIN PORCH RND]+[INTERLACE]) * 1000000
3216594783dSgdamore      *
3226594783dSgdamore      *  To make it integer friendly, we pre-multiply the 1000000 to get to
3236594783dSgdamore      *  usec.  This gives us:
3246594783dSgdamore      *
3256594783dSgdamore      *  [H PERIOD EST] = ((1000000/[V FIELD RATE RQD]) - [MIN VSYNC+BP]) /
3266594783dSgdamore      *			([V LINES RND] + (2 * [TOP MARGIN (LINES)]) +
3276594783dSgdamore      *			 [MIN PORCH RND]+[INTERLACE])
3286594783dSgdamore      *
3296594783dSgdamore      *  The other problem is that the interlace value is wrong.  To get
3306594783dSgdamore      *  the interlace to a whole number, we multiply both the numerator and
3316594783dSgdamore      *  divisor by 2, so we can use a value of either 1 or 0 for the interlace
3326594783dSgdamore      *  factor.
3336594783dSgdamore      *
3346594783dSgdamore      * This gives us:
3356594783dSgdamore      *
3366594783dSgdamore      * [H PERIOD EST] = ((2*((1000000/[V FIELD RATE RQD]) - [MIN VSYNC+BP])) /
3376594783dSgdamore      *			 (2*([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
3386594783dSgdamore      *			  [MIN PORCH RND]) + [2*INTERLACE]))
3396594783dSgdamore      *
3406594783dSgdamore      * Finally we multiply by another 1000, to get value in picosec.
3416594783dSgdamore      * Why picosec?  To minimize rounding errors.  Gotta love integer
3429e949733Sdholland      * math and error propagation.
3436594783dSgdamore      */
3446594783dSgdamore 
3456594783dSgdamore     h_period_est = DIVIDE(((DIVIDE(2000000000000ULL, v_field_rqd)) -
3466594783dSgdamore 			      (2000000 * params->min_vsbp)),
3476594783dSgdamore 	((2 * (v_lines + (2 * top_margin) + params->min_porch)) + interlace));
3486594783dSgdamore 
3496594783dSgdamore     print_value(7, "[H PERIOD EST (ps)]", h_period_est);
3506594783dSgdamore 
3516594783dSgdamore 
3526594783dSgdamore     /*  8. Find the number of lines in V sync + back porch:
3536594783dSgdamore      *
3546594783dSgdamore      *  [V SYNC+BP] = ROUND(([MIN VSYNC+BP]/[H PERIOD EST]),0)
3556594783dSgdamore      *
3566594783dSgdamore      *  But recall that h_period_est is in psec. So multiply by 1000000.
3576594783dSgdamore      */
3586594783dSgdamore 
3596594783dSgdamore     vsync_plus_bp = DIVIDE(params->min_vsbp * 1000000, h_period_est);
3606594783dSgdamore 
3616594783dSgdamore     print_value(8, "[V SYNC+BP]", vsync_plus_bp);
3626594783dSgdamore 
3636594783dSgdamore 
3646594783dSgdamore     /*  9. Find the number of lines in V back porch alone:
3656594783dSgdamore      *
3666594783dSgdamore      *  [V BACK PORCH] = [V SYNC+BP] - [V SYNC RND]
3676594783dSgdamore      *
3686594783dSgdamore      *  XXX is "[V SYNC RND]" a typo? should be [V SYNC RQD]?
3696594783dSgdamore      */
3706594783dSgdamore 
3716594783dSgdamore     v_back_porch = vsync_plus_bp - params->vsync_rqd;
3726594783dSgdamore 
3736594783dSgdamore     print_value(9, "[V BACK PORCH]", v_back_porch);
3746594783dSgdamore 
3756594783dSgdamore 
3766594783dSgdamore     /*  10. Find the total number of lines in Vertical field period:
3776594783dSgdamore      *
3786594783dSgdamore      *  [TOTAL V LINES] = [V LINES RND] + [TOP MARGIN (LINES)] +
3796594783dSgdamore      *                    [BOT MARGIN (LINES)] + [V SYNC+BP] + [INTERLACE] +
3806594783dSgdamore      *                    [MIN PORCH RND]
3816594783dSgdamore      */
3826594783dSgdamore 
3836594783dSgdamore     total_v_lines = v_lines + top_margin + bottom_margin + vsync_plus_bp +
3846594783dSgdamore         interlace + params->min_porch;
3856594783dSgdamore 
3866594783dSgdamore     print_value(10, "[TOTAL V LINES]", total_v_lines);
3876594783dSgdamore 
3886594783dSgdamore 
3896594783dSgdamore     /*  11. Estimate the Vertical field frequency:
3906594783dSgdamore      *
3916594783dSgdamore      *  [V FIELD RATE EST] = 1 / [H PERIOD EST] / [TOTAL V LINES] * 1000000
3926594783dSgdamore      *
3936594783dSgdamore      *  Again, we want to pre multiply by 10^9 to convert for nsec, thereby
3946594783dSgdamore      *  making it usable in integer math.
3956594783dSgdamore      *
3966594783dSgdamore      *  So we get:
3976594783dSgdamore      *
3986594783dSgdamore      *  [V FIELD RATE EST] = 1000000000 / [H PERIOD EST] / [TOTAL V LINES]
3996594783dSgdamore      *
4006594783dSgdamore      *  This is all scaled to get the result in uHz.  Again, we're trying to
4019e949733Sdholland      *  minimize error propagation.
4026594783dSgdamore      */
4036594783dSgdamore     v_field_est = DIVIDE(DIVIDE(1000000000000000ULL, h_period_est),
4046594783dSgdamore 	total_v_lines);
4056594783dSgdamore 
4066594783dSgdamore     print_value(11, "[V FIELD RATE EST(uHz)]", v_field_est);
4076594783dSgdamore 
4086594783dSgdamore 
4096594783dSgdamore     /*  12. Find the actual horizontal period:
4106594783dSgdamore      *
4116594783dSgdamore      *  [H PERIOD] = [H PERIOD EST] / ([V FIELD RATE RQD] / [V FIELD RATE EST])
4126594783dSgdamore      */
4136594783dSgdamore 
4146594783dSgdamore     h_period = DIVIDE(h_period_est * v_field_est, v_field_rqd * 1000);
4156594783dSgdamore 
4166594783dSgdamore     print_value(12, "[H PERIOD(ps)]", h_period);
4176594783dSgdamore 
4186594783dSgdamore 
4196594783dSgdamore     /*  13. Find the actual Vertical field frequency:
4206594783dSgdamore      *
4216594783dSgdamore      *  [V FIELD RATE] = 1 / [H PERIOD] / [TOTAL V LINES] * 1000000
4226594783dSgdamore      *
4236594783dSgdamore      *  And again, we convert to nsec ahead of time, giving us:
4246594783dSgdamore      *
4256594783dSgdamore      *  [V FIELD RATE] = 1000000 / [H PERIOD] / [TOTAL V LINES]
4266594783dSgdamore      *
4276594783dSgdamore      *  And another rescaling back to mHz.  Gotta love it.
4286594783dSgdamore      */
4296594783dSgdamore 
4306594783dSgdamore     v_field_rate = DIVIDE(1000000000000ULL, h_period * total_v_lines);
4316594783dSgdamore 
4326594783dSgdamore     print_value(13, "[V FIELD RATE]", v_field_rate);
4336594783dSgdamore 
4346594783dSgdamore 
4356594783dSgdamore     /*  14. Find the Vertical frame frequency:
4366594783dSgdamore      *
4376594783dSgdamore      *  [V FRAME RATE] = (IF([INT RQD?]="y", [V FIELD RATE]/2, [V FIELD RATE]))
4386594783dSgdamore      *
4396594783dSgdamore      *  N.B. that the result here is in mHz.
4406594783dSgdamore      */
4416594783dSgdamore 
4426594783dSgdamore     v_frame_rate = (flags & VESAGTF_FLAG_ILACE) ?
4436594783dSgdamore 	v_field_rate / 2 : v_field_rate;
4446594783dSgdamore 
4456594783dSgdamore     print_value(14, "[V FRAME RATE]", v_frame_rate);
4466594783dSgdamore 
4476594783dSgdamore 
4486594783dSgdamore     /*  15. Find number of pixels in left margin:
4496594783dSgdamore      *  16. Find number of pixels in right margin:
4506594783dSgdamore      *
4516594783dSgdamore      *  [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
4526594783dSgdamore      *          (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
4536594783dSgdamore      *                   [CELL GRAN RND]),0)) * [CELL GRAN RND],
4546594783dSgdamore      *          0))
4556594783dSgdamore      *
4566594783dSgdamore      *  Again, we deal with margin percentages as PPT (parts per thousand).
4576594783dSgdamore      *  And the calculations for left and right are the same.
4586594783dSgdamore      */
4596594783dSgdamore 
4606594783dSgdamore     left_margin = right_margin = (flags & VESAGTF_FLAG_MARGINS) ?
4616594783dSgdamore 	DIVIDE(DIVIDE(h_pixels * params->margin_ppt, 1000),
4626594783dSgdamore 	    CELL_GRAN) * CELL_GRAN : 0;
4636594783dSgdamore 
4646594783dSgdamore     print_value(15, "[LEFT MARGIN (PIXELS)]", left_margin);
4656594783dSgdamore     print_value(16, "[RIGHT MARGIN (PIXELS)]", right_margin);
4666594783dSgdamore 
4676594783dSgdamore 
4686594783dSgdamore     /*  17. Find total number of active pixels in image and left and right
4696594783dSgdamore      *  margins:
4706594783dSgdamore      *
4716594783dSgdamore      *  [TOTAL ACTIVE PIXELS] = [H PIXELS RND] + [LEFT MARGIN (PIXELS)] +
4726594783dSgdamore      *                          [RIGHT MARGIN (PIXELS)]
4736594783dSgdamore      */
4746594783dSgdamore 
4756594783dSgdamore     total_active_pixels = h_pixels + left_margin + right_margin;
4766594783dSgdamore 
4776594783dSgdamore     print_value(17, "[TOTAL ACTIVE PIXELS]", total_active_pixels);
4786594783dSgdamore 
4796594783dSgdamore 
4806594783dSgdamore     /*  18. Find the ideal blanking duty cycle from the blanking duty cycle
4816594783dSgdamore      *  equation:
4826594783dSgdamore      *
4836594783dSgdamore      *  [IDEAL DUTY CYCLE] = [C'] - ([M']*[H PERIOD]/1000)
4846594783dSgdamore      *
4856594783dSgdamore      *  However, we have modified values for [C'] as [256*C'] and
4866594783dSgdamore      *  [M'] as [256*M'].  Again the idea here is to get good scaling.
4876594783dSgdamore      *  We use 256 as the factor to make the math fast.
4886594783dSgdamore      *
4896594783dSgdamore      *  Note that this means that we have to scale it appropriately in
4906594783dSgdamore      *  later calculations.
4916594783dSgdamore      *
4926594783dSgdamore      *  The ending result is that our ideal_duty_cycle is 256000x larger
4936594783dSgdamore      *  than the duty cycle used by VESA.  But again, this reduces error
4949e949733Sdholland      *  propagation.
4956594783dSgdamore      */
4966594783dSgdamore 
4976594783dSgdamore     ideal_duty_cycle =
4986594783dSgdamore 	((C_PRIME256(params) * 1000) -
4996594783dSgdamore 	    (M_PRIME256(params) * h_period / 1000000));
5006594783dSgdamore 
5016594783dSgdamore     print_value(18, "[IDEAL DUTY CYCLE]", ideal_duty_cycle);
5026594783dSgdamore 
5036594783dSgdamore 
5046594783dSgdamore     /*  19. Find the number of pixels in the blanking time to the nearest
5056594783dSgdamore      *  double character cell:
5066594783dSgdamore      *
5076594783dSgdamore      *  [H BLANK (PIXELS)] = (ROUND(([TOTAL ACTIVE PIXELS] *
5086594783dSgdamore      *                               [IDEAL DUTY CYCLE] /
5096594783dSgdamore      *                               (100-[IDEAL DUTY CYCLE]) /
5106594783dSgdamore      *                               (2*[CELL GRAN RND])), 0))
5116594783dSgdamore      *                       * (2*[CELL GRAN RND])
5126594783dSgdamore      *
5136594783dSgdamore      *  Of course, we adjust to make this rounding work in integer math.
5146594783dSgdamore      */
5156594783dSgdamore 
5166594783dSgdamore     h_blank = DIVIDE(DIVIDE(total_active_pixels * ideal_duty_cycle,
5176594783dSgdamore 			 (256000 * 100ULL) - ideal_duty_cycle),
5186594783dSgdamore 	2 * CELL_GRAN) * (2 * CELL_GRAN);
5196594783dSgdamore 
5206594783dSgdamore     print_value(19, "[H BLANK (PIXELS)]", h_blank);
5216594783dSgdamore 
5226594783dSgdamore 
5236594783dSgdamore     /*  20. Find total number of pixels:
5246594783dSgdamore      *
5256594783dSgdamore      *  [TOTAL PIXELS] = [TOTAL ACTIVE PIXELS] + [H BLANK (PIXELS)]
5266594783dSgdamore      */
5276594783dSgdamore 
5286594783dSgdamore     total_pixels = total_active_pixels + h_blank;
5296594783dSgdamore 
5306594783dSgdamore     print_value(20, "[TOTAL PIXELS]", total_pixels);
5316594783dSgdamore 
5326594783dSgdamore 
5336594783dSgdamore     /*  21. Find pixel clock frequency:
5346594783dSgdamore      *
5356594783dSgdamore      *  [PIXEL FREQ] = [TOTAL PIXELS] / [H PERIOD]
5366594783dSgdamore      *
5376594783dSgdamore      *  We calculate this in Hz rather than MHz, to get a value that
5386594783dSgdamore      *  is usable with integer math.  Recall that the [H PERIOD] is in
5396594783dSgdamore      *  nsec.
5406594783dSgdamore      */
5416594783dSgdamore 
5426594783dSgdamore     pixel_freq = DIVIDE(total_pixels * 1000000, DIVIDE(h_period, 1000));
5436594783dSgdamore 
5446594783dSgdamore     print_value(21, "[PIXEL FREQ]", pixel_freq);
5456594783dSgdamore 
5466594783dSgdamore 
5476594783dSgdamore     /*  22. Find horizontal frequency:
5486594783dSgdamore      *
5496594783dSgdamore      *  [H FREQ] = 1000 / [H PERIOD]
5506594783dSgdamore      *
5516594783dSgdamore      *  I've ifdef'd this out, because we don't need it for any of
5526594783dSgdamore      *  our calculations.
5536594783dSgdamore      *  We calculate this in Hz rather than kHz, to avoid rounding
5546594783dSgdamore      *  errors.  Recall that the [H PERIOD] is in usec.
5556594783dSgdamore      */
5566594783dSgdamore 
5576594783dSgdamore #ifdef	GTFDEBUG
5586594783dSgdamore     h_freq = 1000000000 / h_period;
5596594783dSgdamore 
5606594783dSgdamore     print_value(22, "[H FREQ]", h_freq);
5616594783dSgdamore #endif
5626594783dSgdamore 
5636594783dSgdamore 
5646594783dSgdamore 
5656594783dSgdamore     /* Stage 1 computations are now complete; I should really pass
5666594783dSgdamore        the results to another function and do the Stage 2
5676594783dSgdamore        computations, but I only need a few more values so I'll just
5686594783dSgdamore        append the computations here for now */
5696594783dSgdamore 
5706594783dSgdamore 
5716594783dSgdamore 
5726594783dSgdamore     /*  17. Find the number of pixels in the horizontal sync period:
5736594783dSgdamore      *
5746594783dSgdamore      *  [H SYNC (PIXELS)] =(ROUND(([H SYNC%] / 100 * [TOTAL PIXELS] /
5756594783dSgdamore      *                             [CELL GRAN RND]),0))*[CELL GRAN RND]
5766594783dSgdamore      *
5776594783dSgdamore      *  Rewriting for integer math:
5786594783dSgdamore      *
5796594783dSgdamore      *  [H SYNC (PIXELS)]=(ROUND((H SYNC%] * [TOTAL PIXELS] / 100 /
5806594783dSgdamore      *				   [CELL GRAN RND),0))*[CELL GRAN RND]
5816594783dSgdamore      */
5826594783dSgdamore 
5836594783dSgdamore     h_sync = DIVIDE(((params->hsync_pct * total_pixels) / 100), CELL_GRAN) *
5846594783dSgdamore 	CELL_GRAN;
5856594783dSgdamore 
5866594783dSgdamore     print_value(17, "[H SYNC (PIXELS)]", h_sync);
5876594783dSgdamore 
5886594783dSgdamore 
5896594783dSgdamore     /*  18. Find the number of pixels in the horizontal front porch period:
5906594783dSgdamore      *
5916594783dSgdamore      *  [H FRONT PORCH (PIXELS)] = ([H BLANK (PIXELS)]/2)-[H SYNC (PIXELS)]
5926594783dSgdamore      *
5936594783dSgdamore      *  Note that h_blank is always an even number of characters (i.e.
5946594783dSgdamore      *  h_blank % (CELL_GRAN * 2) == 0)
5956594783dSgdamore      */
5966594783dSgdamore 
5976594783dSgdamore     h_front_porch = (h_blank / 2) - h_sync;
5986594783dSgdamore 
5996594783dSgdamore     print_value(18, "[H FRONT PORCH (PIXELS)]", h_front_porch);
6006594783dSgdamore 
6016594783dSgdamore 
6026594783dSgdamore     /*  36. Find the number of lines in the odd front porch period:
6036594783dSgdamore      *
6046594783dSgdamore      *  [V ODD FRONT PORCH(LINES)]=([MIN PORCH RND]+[INTERLACE])
6056594783dSgdamore      *
6066594783dSgdamore      *  Adjusting for the fact that the interlace is scaled:
6076594783dSgdamore      *
6086594783dSgdamore      *  [V ODD FRONT PORCH(LINES)]=(([MIN PORCH RND] * 2) + [2*INTERLACE]) / 2
6096594783dSgdamore      */
6106594783dSgdamore 
6116594783dSgdamore     v_odd_front_porch_lines = ((2 * params->min_porch) + interlace) / 2;
6126594783dSgdamore 
6136594783dSgdamore     print_value(36, "[V ODD FRONT PORCH(LINES)]", v_odd_front_porch_lines);
6146594783dSgdamore 
6156594783dSgdamore 
6166594783dSgdamore     /* finally, pack the results in the mode struct */
6176594783dSgdamore 
6186594783dSgdamore     vmp->hsync_start = h_pixels + h_front_porch;
6196594783dSgdamore     vmp->hsync_end = vmp->hsync_start + h_sync;
6206594783dSgdamore     vmp->htotal = total_pixels;
6216594783dSgdamore     vmp->hdisplay = h_pixels;
6226594783dSgdamore 
6236594783dSgdamore     vmp->vsync_start = v_lines + v_odd_front_porch_lines;
6246594783dSgdamore     vmp->vsync_end = vmp->vsync_start + params->vsync_rqd;
6256594783dSgdamore     vmp->vtotal = total_v_lines;
6266594783dSgdamore     vmp->vdisplay = v_lines;
6276594783dSgdamore 
6286594783dSgdamore     vmp->dot_clock = pixel_freq;
6296594783dSgdamore 
6306594783dSgdamore }
6316594783dSgdamore 
6326594783dSgdamore void
vesagtf_mode(unsigned x,unsigned y,unsigned refresh,struct videomode * vmp)6336594783dSgdamore vesagtf_mode(unsigned x, unsigned y, unsigned refresh, struct videomode *vmp)
6346594783dSgdamore {
6356594783dSgdamore 	struct vesagtf_params	params;
6366594783dSgdamore 
6376594783dSgdamore 	params.margin_ppt = VESAGTF_MARGIN_PPT;
6386594783dSgdamore 	params.min_porch = VESAGTF_MIN_PORCH;
6396594783dSgdamore 	params.vsync_rqd = VESAGTF_VSYNC_RQD;
6406594783dSgdamore 	params.hsync_pct = VESAGTF_HSYNC_PCT;
6416594783dSgdamore 	params.min_vsbp = VESAGTF_MIN_VSBP;
6426594783dSgdamore 	params.M = VESAGTF_M;
6436594783dSgdamore 	params.C = VESAGTF_C;
6446594783dSgdamore 	params.K = VESAGTF_K;
6456594783dSgdamore 	params.J = VESAGTF_J;
6466594783dSgdamore 
6476594783dSgdamore 	vesagtf_mode_params(x, y, refresh, &params, 0, vmp);
6486594783dSgdamore }
6496594783dSgdamore 
6506594783dSgdamore /*
6516594783dSgdamore  * The tidbit here is so that you can compile this file as a
6526594783dSgdamore  * standalone user program to generate X11 modelines using VESA GTF.
6536594783dSgdamore  * This also allows for testing of the code itself, without
6546594783dSgdamore  * necessitating a full kernel recompile.
6556594783dSgdamore  */
6566594783dSgdamore 
6576594783dSgdamore /* print_xf86_mode() - print the XFree86 modeline, given mode timings. */
6586594783dSgdamore 
659*08b20cdaSmlelstv #if 0
6606594783dSgdamore #ifndef _KERNEL
6616594783dSgdamore void
6626594783dSgdamore print_xf86_mode (struct videomode *vmp)
6636594783dSgdamore {
6646594783dSgdamore 	float	vf, hf;
6656594783dSgdamore 
6666594783dSgdamore 	hf = 1000.0 * vmp->dot_clock / vmp->htotal;
6676594783dSgdamore 	vf = 1.0 * hf / vmp->vtotal;
6686594783dSgdamore 
6696594783dSgdamore     printf("\n");
6706594783dSgdamore     printf("  # %dx%d @ %.2f Hz (GTF) hsync: %.2f kHz; pclk: %.2f MHz\n",
6716594783dSgdamore 	vmp->hdisplay, vmp->vdisplay, vf, hf, vmp->dot_clock / 1000.0);
6726594783dSgdamore 
6736594783dSgdamore     printf("  Modeline \"%dx%d_%.2f\"  %.2f"
6746594783dSgdamore 	"  %d %d %d %d"
6756594783dSgdamore 	"  %d %d %d %d"
6766594783dSgdamore 	"  -HSync +Vsync\n\n",
6776594783dSgdamore 	vmp->hdisplay, vmp->vdisplay, vf, (vmp->dot_clock / 1000.0),
6786594783dSgdamore 	vmp->hdisplay, vmp->hsync_start, vmp->hsync_end, vmp->htotal,
6796594783dSgdamore 	vmp->vdisplay, vmp->vsync_start, vmp->vsync_end, vmp->vtotal);
6806594783dSgdamore }
6816594783dSgdamore 
6826594783dSgdamore int
6836594783dSgdamore main (int argc, char *argv[])
6846594783dSgdamore {
6856594783dSgdamore 	struct videomode m;
6866594783dSgdamore 
6876594783dSgdamore 	if (argc != 4) {
6886594783dSgdamore 		printf("usage: %s x y refresh\n", argv[0]);
6896594783dSgdamore 		exit(1);
6906594783dSgdamore 	}
6916594783dSgdamore 
6926594783dSgdamore 	vesagtf_mode(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), &m);
6936594783dSgdamore 
6946594783dSgdamore         print_xf86_mode(&m);
6956594783dSgdamore 
6966594783dSgdamore 	return 0;
6976594783dSgdamore 
6986594783dSgdamore }
6996594783dSgdamore #endif
700*08b20cdaSmlelstv #endif
701