vesagtf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /****************************************************************************
  2. * video/videomode/vesagtf.c
  3. *
  4. * Copyright (C) 2019 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Derives from logic in FreeBSD which has an equivalent 3-clause BSD
  8. * license:
  9. *
  10. * Copyright (c) 2006 Itronix Inc. All rights reserved.
  11. * Written by Garrett D'Amore for Itronix Inc.
  12. *
  13. * That version, in turn, derived from a userland GTF program supplied by
  14. * Nvidia which was also released under a compatible 3-clause BSD license:
  15. *
  16. * Copyright (c) 2001, Andy Ritger <aritger@nvidia.com>
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. *
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. * 3. The name of Itronix Inc. may not be used to endorse
  29. * or promote products derived from this software without specific
  30. * prior written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
  33. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  34. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
  36. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  37. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  38. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  40. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  41. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  42. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. *
  44. ****************************************************************************/
  45. /* The logic in this file program was based on the Generalized Timing
  46. * Formula(GTF TM) Standard Version: 1.0, Revision: 1.0
  47. *
  48. * NOTES:
  49. *
  50. * The GTF allows for computation of "margins" (the visible border
  51. * surrounding the addressable video); on most non-overscan type
  52. * systems, the margin period is zero. I've implemented the margin
  53. * computations but not enabled it because 1) I don't really have
  54. * any experience with this, and 2) neither XFree86 modelines nor
  55. * fbset fb.modes provide an obvious way for margin timings to be
  56. * included in their mode descriptions (needs more investigation).
  57. *
  58. * The GTF provides for computation of interlaced mode timings;
  59. * I've implemented the computations but not enabled them, yet.
  60. * I should probably enable and test this at some point.
  61. *
  62. * TODO:
  63. *
  64. * o Add support for interlaced modes.
  65. *
  66. * o Implement the other portions of the GTF: compute mode timings
  67. * given either the desired pixel clock or the desired horizontal
  68. * frequency.
  69. *
  70. * o It would be nice if this were more general purpose to do things
  71. * outside the scope of the GTF: like generate double scan mode
  72. * timings, for example.
  73. *
  74. * o Printing digits to the right of the decimal point when the
  75. * digits are 0 annoys me.
  76. *
  77. * o Error checking.
  78. */
  79. /****************************************************************************
  80. * Included Files
  81. ****************************************************************************/
  82. #include <sys/types.h>
  83. #include <nuttx/video/videomode.h>
  84. #include <nuttx/video/vesagtf.h>
  85. /****************************************************************************
  86. * Pre-processor Definitions
  87. ****************************************************************************/
  88. #define CELL_GRAN 8 /* Assumed character cell granularity */
  89. /* c' and m' are part of the Blanking Duty Cycle computation
  90. *
  91. * #define C_PRIME (((c - j) * k/256.0) + j)
  92. * #define M_PRIME (k/256.0 * m)
  93. */
  94. /* c' and m' multiplied by 256 to give integer math. Make sure to
  95. * scale results using these back down, appropriately.
  96. */
  97. #define C_PRIME256(p) (((p->c - p->j) * p->k) + (p->j * 256))
  98. #define M_PRIME256(p) (p->k * p->m)
  99. #define DIVIDE(x,y) (((x) + ((y) / 2)) / (y))
  100. /****************************************************************************
  101. * Public Functions
  102. ****************************************************************************/
  103. /****************************************************************************
  104. * Name: vesagtf_mode_params
  105. *
  106. * Description:
  107. * vesagtf_mode_params() - as defined by the GTF Timing Standard, compute
  108. * the Stage 1 Parameters using the vertical refresh frequency. In other
  109. * words: input a desired resolution and desired refresh rate, and
  110. * output the GTF mode timings.
  111. *
  112. ****************************************************************************/
  113. void vesagtf_mode_params(unsigned int x, unsigned int y,
  114. unsigned int refresh,
  115. FAR struct vesagtf_params *params,
  116. unsigned int flags,
  117. FAR struct videomode_s *videomode)
  118. {
  119. uint64_t h_period_est;
  120. uint64_t v_field_est;
  121. uint64_t h_period;
  122. uint64_t ideal_duty_cycle;
  123. unsigned int v_field_rqd;
  124. unsigned int top_margin;
  125. unsigned int bottom_margin;
  126. unsigned int interlace;
  127. unsigned int vsync_plus_bp;
  128. unsigned total_v_lines;
  129. unsigned int left_margin;
  130. unsigned int right_margin;
  131. unsigned int total_active_pixels;
  132. unsigned int h_blank;
  133. unsigned int h_pixels;
  134. unsigned int v_lines;
  135. unsigned int total_pixels;
  136. unsigned int pixel_freq;
  137. unsigned int h_sync;
  138. unsigned int h_front_porch;
  139. unsigned int v_odd_front_porch_lines;
  140. #if 0 /* Unused, not needed */
  141. unsigned int v_field_rate;
  142. unsigned int v_back_porch;
  143. unsigned int v_frame_rate;
  144. unsigned int h_freq;
  145. #endif
  146. /* 1. In order to give correct results, the number of horizontal
  147. * pixels requested is first processed to ensure that it is divisible
  148. * by the character size, by rounding it to the nearest character
  149. * cell boundary:
  150. *
  151. * [H PIXELS RND] = ((ROUND([H PIXELS]/[CELL GRAN RND],0))*[CELLGRAN RND])
  152. */
  153. h_pixels = DIVIDE(x, CELL_GRAN) * CELL_GRAN;
  154. /* 2. If interlace is requested, the number of vertical lines assumed
  155. * by the calculation must be halved, as the computation calculates
  156. * the number of vertical lines per field. In either case, the
  157. * number of lines is rounded to the nearest integer.
  158. *
  159. * [V LINES RND] = IF([INT RQD?]="y", ROUND([V LINES]/2,0),
  160. * ROUND([V LINES],0))
  161. */
  162. v_lines = (flags & VESAGTF_FLAG_ILACE) ? DIVIDE(y, 2) : y;
  163. /* 3. Find the frame rate required:
  164. *
  165. * [V FIELD RATE RQD] = IF([INT RQD?]="y", [I/P FREQ RQD]*2,
  166. * [I/P FREQ RQD])
  167. */
  168. v_field_rqd = (flags & VESAGTF_FLAG_ILACE) ? (refresh * 2) : (refresh);
  169. /* 4. Find number of lines in Top margin:
  170. * 5. Find number of lines in Bottom margin:
  171. *
  172. * [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
  173. * ROUND(([MARGIN%]/100*[V LINES RND]),0),
  174. * 0)
  175. *
  176. * Ditto for bottom margin. Note that instead of %, we use PPT, which
  177. * is parts per thousand. This helps us with integer math.
  178. */
  179. top_margin = (flags & VESAGTF_FLAG_MARGINS) ?
  180. DIVIDE(v_lines * params->margin_ppt, 1000) : 0;
  181. bottom_margin = top_margin;
  182. /* 6. If interlace is required, then set variable [INTERLACE]=0.5:
  183. *
  184. * [INTERLACE]=(IF([INT RQD?]="y",0.5,0))
  185. *
  186. * To make this integer friendly, we use some special hacks in step
  187. * 7 below. Please read those comments to understand why I am using
  188. * a whole number of 1.0 instead of 0.5 here.
  189. */
  190. interlace = (flags & VESAGTF_FLAG_ILACE) ? 1 : 0;
  191. /* 7. Estimate the Horizontal period
  192. *
  193. * [H PERIOD EST] = ((1/[V FIELD RATE RQD]) - [MIN VSYNC+BP]/1000000) /
  194. * ([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
  195. * [MIN PORCH RND]+[INTERLACE]) * 1000000
  196. *
  197. * To make it integer friendly, we pre-multiply the 1000000 to get to
  198. * usec. This gives us:
  199. *
  200. * [H PERIOD EST] = ((1000000/[V FIELD RATE RQD]) - [MIN VSYNC+BP]) /
  201. * ([V LINES RND] + (2 * [TOP MARGIN (LINES)]) +
  202. * [MIN PORCH RND]+[INTERLACE])
  203. *
  204. * The other problem is that the interlace value is wrong. To get
  205. * the interlace to a whole number, we multiply both the numerator and
  206. * divisor by 2, so we can use a value of either 1 or 0 for the interlace
  207. * factor.
  208. *
  209. * This gives us:
  210. *
  211. * [H PERIOD EST] = ((2*((1000000/[V FIELD RATE RQD]) - [MIN VSYNC+BP])) /
  212. * (2*([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
  213. * [MIN PORCH RND]) + [2*INTERLACE]))
  214. *
  215. * Finally we multiply by another 1000, to get value in picosec.
  216. * Why picosec? To minimize rounding errors. Gotta love integer
  217. * math and error propagation.
  218. */
  219. h_period_est = DIVIDE(((DIVIDE(2000000000000ULL, v_field_rqd)) -
  220. (2000000 * params->min_vsbp)),
  221. ((2 * (v_lines +
  222. (2 * top_margin) + params->min_porch)) +
  223. interlace));
  224. /* 8. Find the number of lines in V sync + back porch:
  225. *
  226. * [V SYNC+BP] = ROUND(([MIN VSYNC+BP]/[H PERIOD EST]),0)
  227. *
  228. * But recall that h_period_est is in psec. So multiply by 1000000.
  229. */
  230. vsync_plus_bp = DIVIDE(params->min_vsbp * 1000000, h_period_est);
  231. #if 0 /* Not needed */
  232. /* 9. Find the number of lines in V back porch alone:
  233. *
  234. * [V BACK PORCH] = [V SYNC+BP] - [V SYNC RND]
  235. *
  236. * XXX is "[V SYNC RND]" a typo? should be [V SYNC RQD]?
  237. */
  238. v_back_porch = vsync_plus_bp - params->vsync_rqd;
  239. #endif
  240. /* 10. Find the total number of lines in Vertical field period:
  241. *
  242. * [TOTAL V LINES] = [V LINES RND] + [TOP MARGIN (LINES)] +
  243. * [BOT MARGIN (LINES)] + [V SYNC+BP] + [INTERLACE] +
  244. * [MIN PORCH RND]
  245. */
  246. total_v_lines = v_lines + top_margin + bottom_margin + vsync_plus_bp +
  247. interlace + params->min_porch;
  248. /* 11. Estimate the Vertical field frequency:
  249. *
  250. * [V FIELD RATE EST] = 1 / [H PERIOD EST] / [TOTAL V LINES] * 1000000
  251. *
  252. * Again, we want to pre multiply by 10^9 to convert for nsec, thereby
  253. * making it usable in integer math.
  254. *
  255. * So we get:
  256. *
  257. * [V FIELD RATE EST] = 1000000000 / [H PERIOD EST] / [TOTAL V LINES]
  258. *
  259. * This is all scaled to get the result in uHz. Again, we're trying to
  260. * minimize error propagation.
  261. */
  262. v_field_est = DIVIDE(DIVIDE(1000000000000000ULL, h_period_est),
  263. total_v_lines);
  264. /* 12. Find the actual horizontal period:
  265. *
  266. * [H PERIOD] = [H PERIOD EST] / ([V FIELD RATE RQD] / [V FIELD RATE EST])
  267. */
  268. h_period = DIVIDE(h_period_est * v_field_est, v_field_rqd * 1000);
  269. #if 0 /* Not needed */
  270. /* 13. Find the actual Vertical field frequency:
  271. *
  272. * [V FIELD RATE] = 1 / [H PERIOD] / [TOTAL V LINES] * 1000000
  273. *
  274. * And again, we convert to nsec ahead of time, giving us:
  275. *
  276. * [V FIELD RATE] = 1000000 / [H PERIOD] / [TOTAL V LINES]
  277. *
  278. * And another rescaling back to mHz. Gotta love it.
  279. */
  280. v_field_rate = DIVIDE(1000000000000ULL, h_period * total_v_lines);
  281. /* 14. Find the Vertical frame frequency:
  282. *
  283. * [V FRAME RATE] = (IF([INT RQD?]="y", [V FIELD RATE]/2, [V FIELD RATE]))
  284. *
  285. * N.B. that the result here is in mHz.
  286. */
  287. v_frame_rate = (flags & VESAGTF_FLAG_ILACE) ?
  288. v_field_rate / 2 : v_field_rate;
  289. #endif
  290. /* 15. Find number of pixels in left margin:
  291. * 16. Find number of pixels in right margin:
  292. *
  293. * [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
  294. * (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
  295. * [CELL GRAN RND]),0)) * [CELL GRAN RND],
  296. * 0))
  297. *
  298. * Again, we deal with margin percentages as PPT (parts per thousand).
  299. * And the calculations for left and right are the same.
  300. */
  301. left_margin = right_margin = (flags & VESAGTF_FLAG_MARGINS) ?
  302. DIVIDE(DIVIDE(h_pixels * params->margin_ppt, 1000),
  303. CELL_GRAN) * CELL_GRAN : 0;
  304. /* 17. Find total number of active pixels in image and left and right
  305. * margins:
  306. *
  307. * [TOTAL ACTIVE PIXELS] = [H PIXELS RND] + [LEFT MARGIN (PIXELS)] +
  308. * [RIGHT MARGIN (PIXELS)]
  309. */
  310. total_active_pixels = h_pixels + left_margin + right_margin;
  311. /* 18. Find the ideal blanking duty cycle from the blanking duty cycle
  312. * equation:
  313. *
  314. * [IDEAL DUTY CYCLE] = [c'] - ([m']*[H PERIOD]/1000)
  315. *
  316. * However, we have modified values for [c'] as [256*c'] and
  317. * [m'] as [256*m']. Again the idea here is to get good scaling.
  318. * We use 256 as the factor to make the math fast.
  319. *
  320. * Note that this means that we have to scale it appropriately in
  321. * later calculations.
  322. *
  323. * The ending result is that our ideal_duty_cycle is 256000x larger
  324. * than the duty cycle used by VESA. But again, this reduces error
  325. * propagation.
  326. */
  327. ideal_duty_cycle =
  328. ((C_PRIME256(params) * 1000) -
  329. (M_PRIME256(params) * h_period / 1000000));
  330. /* 19. Find the number of pixels in the blanking time to the nearest
  331. * double character cell:
  332. *
  333. * [H BLANK (PIXELS)] = (ROUND(([TOTAL ACTIVE PIXELS] *
  334. * [IDEAL DUTY CYCLE] /
  335. * (100-[IDEAL DUTY CYCLE]) /
  336. * (2*[CELL GRAN RND])), 0))
  337. * * (2*[CELL GRAN RND])
  338. *
  339. * Of course, we adjust to make this rounding work in integer math.
  340. */
  341. h_blank = DIVIDE(DIVIDE(total_active_pixels * ideal_duty_cycle,
  342. (256000 * 100ULL) - ideal_duty_cycle),
  343. 2 * CELL_GRAN) * (2 * CELL_GRAN);
  344. /* 20. Find total number of pixels:
  345. *
  346. * [TOTAL PIXELS] = [TOTAL ACTIVE PIXELS] + [H BLANK (PIXELS)]
  347. */
  348. total_pixels = total_active_pixels + h_blank;
  349. /* 21. Find pixel clock frequency:
  350. *
  351. * [PIXEL FREQ] = [TOTAL PIXELS] / [H PERIOD]
  352. *
  353. * We calculate this in Hz rather than MHz, to get a value that
  354. * is usable with integer math. Recall that the [H PERIOD] is in
  355. * nsec.
  356. */
  357. pixel_freq = DIVIDE(total_pixels * 1000000, DIVIDE(h_period, 1000));
  358. #if 0 /* Not needed */
  359. /* 22. Find horizontal frequency:
  360. *
  361. * [H FREQ] = 1000 / [H PERIOD]
  362. *
  363. * We calculate this in Hz rather than kHz, to avoid rounding
  364. * errors. Recall that the [H PERIOD] is in usec.
  365. */
  366. h_freq = 1000000000 / h_period;
  367. #endif
  368. /* Stage 1 computations are now complete; I should really pass
  369. * the results to another function and do the Stage 2
  370. * computations, but I only need a few more values so I'll just
  371. * append the computations here for now.
  372. */
  373. /* 17. Find the number of pixels in the horizontal sync period:
  374. *
  375. * [H SYNC (PIXELS)] =(ROUND(([H SYNC%] / 100 * [TOTAL PIXELS] /
  376. * [CELL GRAN RND]),0))*[CELL GRAN RND]
  377. *
  378. * Rewriting for integer math:
  379. *
  380. * [H SYNC (PIXELS)]=(ROUND((H SYNC%] * [TOTAL PIXELS] / 100 /
  381. * [CELL GRAN RND),0))*[CELL GRAN RND]
  382. */
  383. h_sync = DIVIDE(((params->hsync_pct * total_pixels) / 100), CELL_GRAN) *
  384. CELL_GRAN;
  385. /* 18. Find the number of pixels in the horizontal front porch period:
  386. *
  387. * [H FRONT PORCH (PIXELS)] = ([H BLANK (PIXELS)]/2)-[H SYNC (PIXELS)]
  388. *
  389. * Note that h_blank is always an even number of characters (i.e.
  390. * h_blank % (CELL_GRAN * 2) == 0)
  391. */
  392. h_front_porch = (h_blank / 2) - h_sync;
  393. /* 36. Find the number of lines in the odd front porch period:
  394. *
  395. * [V ODD FRONT PORCH(LINES)]=([MIN PORCH RND]+[INTERLACE])
  396. *
  397. * Adjusting for the fact that the interlace is scaled:
  398. *
  399. * [V ODD FRONT PORCH(LINES)]=(([MIN PORCH RND] * 2) + [2*INTERLACE]) / 2
  400. */
  401. v_odd_front_porch_lines = ((2 * params->min_porch) + interlace) / 2;
  402. /* finally, pack the results in the mode struct */
  403. videomode->hsync_start = h_pixels + h_front_porch;
  404. videomode->hsync_end = videomode->hsync_start + h_sync;
  405. videomode->htotal = total_pixels;
  406. videomode->hdisplay = h_pixels;
  407. videomode->vsync_start = v_lines + v_odd_front_porch_lines;
  408. videomode->vsync_end = videomode->vsync_start + params->vsync_rqd;
  409. videomode->vtotal = total_v_lines;
  410. videomode->vdisplay = v_lines;
  411. videomode->dotclock = pixel_freq;
  412. }
  413. /****************************************************************************
  414. * Name: vesagtf_mode
  415. *
  416. * Description:
  417. * Use VESA GTF formula to generate monitor timings. Assumes default
  418. * GTF parameters, non-interlaced, and no margins.
  419. *
  420. ****************************************************************************/
  421. void vesagtf_mode(unsigned int x, unsigned int y, unsigned int refresh,
  422. FAR struct videomode_s *videomode)
  423. {
  424. struct vesagtf_params params;
  425. params.margin_ppt = VESAGTF_MARGIN_PPT;
  426. params.min_porch = VESAGTF_MIN_PORCH;
  427. params.vsync_rqd = VESAGTF_VSYNC_RQD;
  428. params.hsync_pct = VESAGTF_HSYNC_PCT;
  429. params.min_vsbp = VESAGTF_MIN_VSBP;
  430. params.m = VESAGTF_M;
  431. params.c = VESAGTF_C;
  432. params.k = VESAGTF_K;
  433. params.j = VESAGTF_J;
  434. vesagtf_mode_params(x, y, refresh, &params, 0, videomode);
  435. }