vesagtf.c 22 KB

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