xf86cvt.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright 2005-2006 Luc Verhaegen.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. /*
  23. * The reason for having this function in a file of its own is
  24. * so that ../utils/cvt/cvt can link to it, and that xf86CVTMode
  25. * code is shared directly.
  26. */
  27. #ifdef HAVE_XORG_CONFIG_H
  28. #include <xorg-config.h>
  29. #else
  30. #ifdef HAVE_CONFIG_H
  31. #include <config.h>
  32. #endif
  33. #endif
  34. #include "xf86.h"
  35. #include "xf86Modes.h"
  36. #include <string.h>
  37. /*
  38. * Generate a CVT standard mode from HDisplay, VDisplay and VRefresh.
  39. *
  40. * These calculations are stolen from the CVT calculation spreadsheet written
  41. * by Graham Loveridge. He seems to be claiming no copyright and there seems to
  42. * be no license attached to this. He apparently just wants to see his name
  43. * mentioned.
  44. *
  45. * This file can be found at http://www.vesa.org/Public/CVT/CVTd6r1.xls
  46. *
  47. * Comments and structure corresponds to the comments and structure of the xls.
  48. * This should ease importing of future changes to the standard (not very
  49. * likely though).
  50. *
  51. * About margins; i'm sure that they are to be the bit between HDisplay and
  52. * HBlankStart, HBlankEnd and HTotal, VDisplay and VBlankStart, VBlankEnd and
  53. * VTotal, where the overscan colour is shown. FB seems to call _all_ blanking
  54. * outside sync "margin" for some reason. Since we prefer seeing proper
  55. * blanking instead of the overscan colour, and since the Crtc* values will
  56. * probably get altered after us, we will disable margins altogether. With
  57. * these calculations, Margins will plainly expand H/VDisplay, and we don't
  58. * want that. -- libv
  59. *
  60. */
  61. DisplayModePtr
  62. xf86CVTMode(int HDisplay, int VDisplay, float VRefresh, Bool Reduced,
  63. Bool Interlaced)
  64. {
  65. DisplayModeRec *Mode = xnfcalloc(1, sizeof(DisplayModeRec));
  66. /* 1) top/bottom margin size (% of height) - default: 1.8 */
  67. #define CVT_MARGIN_PERCENTAGE 1.8
  68. /* 2) character cell horizontal granularity (pixels) - default 8 */
  69. #define CVT_H_GRANULARITY 8
  70. /* 4) Minimum vertical porch (lines) - default 3 */
  71. #define CVT_MIN_V_PORCH 3
  72. /* 4) Minimum number of vertical back porch lines - default 6 */
  73. #define CVT_MIN_V_BPORCH 6
  74. /* Pixel Clock step (kHz) */
  75. #define CVT_CLOCK_STEP 250
  76. Bool Margins = FALSE;
  77. float VFieldRate, HPeriod;
  78. int HDisplayRnd, HMargin;
  79. int VDisplayRnd, VMargin, VSync;
  80. float Interlace; /* Please rename this */
  81. char *tmp;
  82. /* CVT default is 60.0Hz */
  83. if (!VRefresh)
  84. VRefresh = 60.0;
  85. /* 1. Required field rate */
  86. if (Interlaced)
  87. VFieldRate = VRefresh * 2;
  88. else
  89. VFieldRate = VRefresh;
  90. /* 2. Horizontal pixels */
  91. HDisplayRnd = HDisplay - (HDisplay % CVT_H_GRANULARITY);
  92. /* 3. Determine left and right borders */
  93. if (Margins) {
  94. /* right margin is actually exactly the same as left */
  95. HMargin = (((float) HDisplayRnd) * CVT_MARGIN_PERCENTAGE / 100.0);
  96. HMargin -= HMargin % CVT_H_GRANULARITY;
  97. }
  98. else
  99. HMargin = 0;
  100. /* 4. Find total active pixels */
  101. Mode->HDisplay = HDisplayRnd + 2 * HMargin;
  102. /* 5. Find number of lines per field */
  103. if (Interlaced)
  104. VDisplayRnd = VDisplay / 2;
  105. else
  106. VDisplayRnd = VDisplay;
  107. /* 6. Find top and bottom margins */
  108. /* nope. */
  109. if (Margins)
  110. /* top and bottom margins are equal again. */
  111. VMargin = (((float) VDisplayRnd) * CVT_MARGIN_PERCENTAGE / 100.0);
  112. else
  113. VMargin = 0;
  114. Mode->VDisplay = VDisplay + 2 * VMargin;
  115. /* 7. Interlace */
  116. if (Interlaced)
  117. Interlace = 0.5;
  118. else
  119. Interlace = 0.0;
  120. /* Determine VSync Width from aspect ratio */
  121. if (!(VDisplay % 3) && ((VDisplay * 4 / 3) == HDisplay))
  122. VSync = 4;
  123. else if (!(VDisplay % 9) && ((VDisplay * 16 / 9) == HDisplay))
  124. VSync = 5;
  125. else if (!(VDisplay % 10) && ((VDisplay * 16 / 10) == HDisplay))
  126. VSync = 6;
  127. else if (!(VDisplay % 4) && ((VDisplay * 5 / 4) == HDisplay))
  128. VSync = 7;
  129. else if (!(VDisplay % 9) && ((VDisplay * 15 / 9) == HDisplay))
  130. VSync = 7;
  131. else /* Custom */
  132. VSync = 10;
  133. if (!Reduced) { /* simplified GTF calculation */
  134. /* 4) Minimum time of vertical sync + back porch interval (µs)
  135. * default 550.0 */
  136. #define CVT_MIN_VSYNC_BP 550.0
  137. /* 3) Nominal HSync width (% of line period) - default 8 */
  138. #define CVT_HSYNC_PERCENTAGE 8
  139. float HBlankPercentage;
  140. int VSyncAndBackPorch, VBackPorch;
  141. int HBlank;
  142. /* 8. Estimated Horizontal period */
  143. HPeriod = ((float) (1000000.0 / VFieldRate - CVT_MIN_VSYNC_BP)) /
  144. (VDisplayRnd + 2 * VMargin + CVT_MIN_V_PORCH + Interlace);
  145. /* 9. Find number of lines in sync + backporch */
  146. if (((int) (CVT_MIN_VSYNC_BP / HPeriod) + 1) <
  147. (VSync + CVT_MIN_V_PORCH))
  148. VSyncAndBackPorch = VSync + CVT_MIN_V_PORCH;
  149. else
  150. VSyncAndBackPorch = (int) (CVT_MIN_VSYNC_BP / HPeriod) + 1;
  151. /* 10. Find number of lines in back porch */
  152. VBackPorch = VSyncAndBackPorch - VSync;
  153. (void) VBackPorch;
  154. /* 11. Find total number of lines in vertical field */
  155. Mode->VTotal = VDisplayRnd + 2 * VMargin + VSyncAndBackPorch + Interlace
  156. + CVT_MIN_V_PORCH;
  157. /* 5) Definition of Horizontal blanking time limitation */
  158. /* Gradient (%/kHz) - default 600 */
  159. #define CVT_M_FACTOR 600
  160. /* Offset (%) - default 40 */
  161. #define CVT_C_FACTOR 40
  162. /* Blanking time scaling factor - default 128 */
  163. #define CVT_K_FACTOR 128
  164. /* Scaling factor weighting - default 20 */
  165. #define CVT_J_FACTOR 20
  166. #define CVT_M_PRIME CVT_M_FACTOR * CVT_K_FACTOR / 256
  167. #define CVT_C_PRIME (CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
  168. CVT_J_FACTOR
  169. /* 12. Find ideal blanking duty cycle from formula */
  170. HBlankPercentage = CVT_C_PRIME - CVT_M_PRIME * HPeriod / 1000.0;
  171. /* 13. Blanking time */
  172. if (HBlankPercentage < 20)
  173. HBlankPercentage = 20;
  174. HBlank = Mode->HDisplay * HBlankPercentage / (100.0 - HBlankPercentage);
  175. HBlank -= HBlank % (2 * CVT_H_GRANULARITY);
  176. /* 14. Find total number of pixels in a line. */
  177. Mode->HTotal = Mode->HDisplay + HBlank;
  178. /* Fill in HSync values */
  179. Mode->HSyncEnd = Mode->HDisplay + HBlank / 2;
  180. Mode->HSyncStart = Mode->HSyncEnd -
  181. (Mode->HTotal * CVT_HSYNC_PERCENTAGE) / 100;
  182. Mode->HSyncStart += CVT_H_GRANULARITY -
  183. Mode->HSyncStart % CVT_H_GRANULARITY;
  184. /* Fill in VSync values */
  185. Mode->VSyncStart = Mode->VDisplay + CVT_MIN_V_PORCH;
  186. Mode->VSyncEnd = Mode->VSyncStart + VSync;
  187. }
  188. else { /* Reduced blanking */
  189. /* Minimum vertical blanking interval time (µs) - default 460 */
  190. #define CVT_RB_MIN_VBLANK 460.0
  191. /* Fixed number of clocks for horizontal sync */
  192. #define CVT_RB_H_SYNC 32.0
  193. /* Fixed number of clocks for horizontal blanking */
  194. #define CVT_RB_H_BLANK 160.0
  195. /* Fixed number of lines for vertical front porch - default 3 */
  196. #define CVT_RB_VFPORCH 3
  197. int VBILines;
  198. /* 8. Estimate Horizontal period. */
  199. HPeriod = ((float) (1000000.0 / VFieldRate - CVT_RB_MIN_VBLANK)) /
  200. (VDisplayRnd + 2 * VMargin);
  201. /* 9. Find number of lines in vertical blanking */
  202. VBILines = ((float) CVT_RB_MIN_VBLANK) / HPeriod + 1;
  203. /* 10. Check if vertical blanking is sufficient */
  204. if (VBILines < (CVT_RB_VFPORCH + VSync + CVT_MIN_V_BPORCH))
  205. VBILines = CVT_RB_VFPORCH + VSync + CVT_MIN_V_BPORCH;
  206. /* 11. Find total number of lines in vertical field */
  207. Mode->VTotal = VDisplayRnd + 2 * VMargin + Interlace + VBILines;
  208. /* 12. Find total number of pixels in a line */
  209. Mode->HTotal = Mode->HDisplay + CVT_RB_H_BLANK;
  210. /* Fill in HSync values */
  211. Mode->HSyncEnd = Mode->HDisplay + CVT_RB_H_BLANK / 2;
  212. Mode->HSyncStart = Mode->HSyncEnd - CVT_RB_H_SYNC;
  213. /* Fill in VSync values */
  214. Mode->VSyncStart = Mode->VDisplay + CVT_RB_VFPORCH;
  215. Mode->VSyncEnd = Mode->VSyncStart + VSync;
  216. }
  217. /* 15/13. Find pixel clock frequency (kHz for xf86) */
  218. Mode->Clock = Mode->HTotal * 1000.0 / HPeriod;
  219. Mode->Clock -= Mode->Clock % CVT_CLOCK_STEP;
  220. /* 16/14. Find actual Horizontal Frequency (kHz) */
  221. Mode->HSync = ((float) Mode->Clock) / ((float) Mode->HTotal);
  222. /* 17/15. Find actual Field rate */
  223. Mode->VRefresh = (1000.0 * ((float) Mode->Clock)) /
  224. ((float) (Mode->HTotal * Mode->VTotal));
  225. /* 18/16. Find actual vertical frame frequency */
  226. /* ignore - just set the mode flag for interlaced */
  227. if (Interlaced)
  228. Mode->VTotal *= 2;
  229. XNFasprintf(&tmp, "%dx%d", HDisplay, VDisplay);
  230. Mode->name = tmp;
  231. if (Reduced)
  232. Mode->Flags |= V_PHSYNC | V_NVSYNC;
  233. else
  234. Mode->Flags |= V_NHSYNC | V_PVSYNC;
  235. if (Interlaced)
  236. Mode->Flags |= V_INTERLACE;
  237. return Mode;
  238. }