fbcvt.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * linux/drivers/video/fbcvt.c - VESA(TM) Coordinated Video Timings
  3. *
  4. * Copyright (C) 2005 Antonino Daplas <adaplas@pol.net>
  5. *
  6. * Based from the VESA(TM) Coordinated Video Timing Generator by
  7. * Graham Loveridge April 9, 2003 available at
  8. * http://www.vesa.org/public/CVT/CVTd6r1.xls
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file COPYING in the main directory of this archive
  12. * for more details.
  13. *
  14. */
  15. #include <linux/fb.h>
  16. #define FB_CVT_CELLSIZE 8
  17. #define FB_CVT_GTF_C 40
  18. #define FB_CVT_GTF_J 20
  19. #define FB_CVT_GTF_K 128
  20. #define FB_CVT_GTF_M 600
  21. #define FB_CVT_MIN_VSYNC_BP 550
  22. #define FB_CVT_MIN_VPORCH 3
  23. #define FB_CVT_MIN_BPORCH 6
  24. #define FB_CVT_RB_MIN_VBLANK 460
  25. #define FB_CVT_RB_HBLANK 160
  26. #define FB_CVT_RB_V_FPORCH 3
  27. #define FB_CVT_FLAG_REDUCED_BLANK 1
  28. #define FB_CVT_FLAG_MARGINS 2
  29. #define FB_CVT_FLAG_INTERLACED 4
  30. struct fb_cvt_data {
  31. u32 xres;
  32. u32 yres;
  33. u32 refresh;
  34. u32 f_refresh;
  35. u32 pixclock;
  36. u32 hperiod;
  37. u32 hblank;
  38. u32 hfreq;
  39. u32 htotal;
  40. u32 vtotal;
  41. u32 vsync;
  42. u32 hsync;
  43. u32 h_front_porch;
  44. u32 h_back_porch;
  45. u32 v_front_porch;
  46. u32 v_back_porch;
  47. u32 h_margin;
  48. u32 v_margin;
  49. u32 interlace;
  50. u32 aspect_ratio;
  51. u32 active_pixels;
  52. u32 flags;
  53. u32 status;
  54. };
  55. static const unsigned char fb_cvt_vbi_tab[] = {
  56. 4, /* 4:3 */
  57. 5, /* 16:9 */
  58. 6, /* 16:10 */
  59. 7, /* 5:4 */
  60. 7, /* 15:9 */
  61. 8, /* reserved */
  62. 9, /* reserved */
  63. 10 /* custom */
  64. };
  65. /* returns hperiod * 1000 */
  66. static u32 fb_cvt_hperiod(struct fb_cvt_data *cvt)
  67. {
  68. u32 num = 1000000000/cvt->f_refresh;
  69. u32 den;
  70. if (cvt->flags & FB_CVT_FLAG_REDUCED_BLANK) {
  71. num -= FB_CVT_RB_MIN_VBLANK * 1000;
  72. den = 2 * (cvt->yres/cvt->interlace + 2 * cvt->v_margin);
  73. } else {
  74. num -= FB_CVT_MIN_VSYNC_BP * 1000;
  75. den = 2 * (cvt->yres/cvt->interlace + cvt->v_margin * 2
  76. + FB_CVT_MIN_VPORCH + cvt->interlace/2);
  77. }
  78. return 2 * (num/den);
  79. }
  80. /* returns ideal duty cycle * 1000 */
  81. static u32 fb_cvt_ideal_duty_cycle(struct fb_cvt_data *cvt)
  82. {
  83. u32 c_prime = (FB_CVT_GTF_C - FB_CVT_GTF_J) *
  84. (FB_CVT_GTF_K) + 256 * FB_CVT_GTF_J;
  85. u32 m_prime = (FB_CVT_GTF_K * FB_CVT_GTF_M);
  86. u32 h_period_est = cvt->hperiod;
  87. return (1000 * c_prime - ((m_prime * h_period_est)/1000))/256;
  88. }
  89. static u32 fb_cvt_hblank(struct fb_cvt_data *cvt)
  90. {
  91. u32 hblank = 0;
  92. if (cvt->flags & FB_CVT_FLAG_REDUCED_BLANK)
  93. hblank = FB_CVT_RB_HBLANK;
  94. else {
  95. u32 ideal_duty_cycle = fb_cvt_ideal_duty_cycle(cvt);
  96. u32 active_pixels = cvt->active_pixels;
  97. if (ideal_duty_cycle < 20000)
  98. hblank = (active_pixels * 20000)/
  99. (100000 - 20000);
  100. else {
  101. hblank = (active_pixels * ideal_duty_cycle)/
  102. (100000 - ideal_duty_cycle);
  103. }
  104. }
  105. hblank &= ~((2 * FB_CVT_CELLSIZE) - 1);
  106. return hblank;
  107. }
  108. static u32 fb_cvt_hsync(struct fb_cvt_data *cvt)
  109. {
  110. u32 hsync;
  111. if (cvt->flags & FB_CVT_FLAG_REDUCED_BLANK)
  112. hsync = 32;
  113. else
  114. hsync = (FB_CVT_CELLSIZE * cvt->htotal)/100;
  115. hsync &= ~(FB_CVT_CELLSIZE - 1);
  116. return hsync;
  117. }
  118. static u32 fb_cvt_vbi_lines(struct fb_cvt_data *cvt)
  119. {
  120. u32 vbi_lines, min_vbi_lines, act_vbi_lines;
  121. if (cvt->flags & FB_CVT_FLAG_REDUCED_BLANK) {
  122. vbi_lines = (1000 * FB_CVT_RB_MIN_VBLANK)/cvt->hperiod + 1;
  123. min_vbi_lines = FB_CVT_RB_V_FPORCH + cvt->vsync +
  124. FB_CVT_MIN_BPORCH;
  125. } else {
  126. vbi_lines = (FB_CVT_MIN_VSYNC_BP * 1000)/cvt->hperiod + 1 +
  127. FB_CVT_MIN_VPORCH;
  128. min_vbi_lines = cvt->vsync + FB_CVT_MIN_BPORCH +
  129. FB_CVT_MIN_VPORCH;
  130. }
  131. if (vbi_lines < min_vbi_lines)
  132. act_vbi_lines = min_vbi_lines;
  133. else
  134. act_vbi_lines = vbi_lines;
  135. return act_vbi_lines;
  136. }
  137. static u32 fb_cvt_vtotal(struct fb_cvt_data *cvt)
  138. {
  139. u32 vtotal = cvt->yres/cvt->interlace;
  140. vtotal += 2 * cvt->v_margin + cvt->interlace/2 + fb_cvt_vbi_lines(cvt);
  141. vtotal |= cvt->interlace/2;
  142. return vtotal;
  143. }
  144. static u32 fb_cvt_pixclock(struct fb_cvt_data *cvt)
  145. {
  146. u32 pixclock;
  147. if (cvt->flags & FB_CVT_FLAG_REDUCED_BLANK)
  148. pixclock = (cvt->f_refresh * cvt->vtotal * cvt->htotal)/1000;
  149. else
  150. pixclock = (cvt->htotal * 1000000)/cvt->hperiod;
  151. pixclock /= 250;
  152. pixclock *= 250;
  153. pixclock *= 1000;
  154. return pixclock;
  155. }
  156. static u32 fb_cvt_aspect_ratio(struct fb_cvt_data *cvt)
  157. {
  158. u32 xres = cvt->xres;
  159. u32 yres = cvt->yres;
  160. u32 aspect = -1;
  161. if (xres == (yres * 4)/3 && !((yres * 4) % 3))
  162. aspect = 0;
  163. else if (xres == (yres * 16)/9 && !((yres * 16) % 9))
  164. aspect = 1;
  165. else if (xres == (yres * 16)/10 && !((yres * 16) % 10))
  166. aspect = 2;
  167. else if (xres == (yres * 5)/4 && !((yres * 5) % 4))
  168. aspect = 3;
  169. else if (xres == (yres * 15)/9 && !((yres * 15) % 9))
  170. aspect = 4;
  171. else {
  172. printk(KERN_INFO "fbcvt: Aspect ratio not CVT "
  173. "standard\n");
  174. aspect = 7;
  175. cvt->status = 1;
  176. }
  177. return aspect;
  178. }
  179. static void fb_cvt_print_name(struct fb_cvt_data *cvt)
  180. {
  181. u32 pixcount, pixcount_mod;
  182. int cnt = 255, offset = 0, read = 0;
  183. u8 *buf = kzalloc(256, GFP_KERNEL);
  184. if (!buf)
  185. return;
  186. pixcount = (cvt->xres * (cvt->yres/cvt->interlace))/1000000;
  187. pixcount_mod = (cvt->xres * (cvt->yres/cvt->interlace)) % 1000000;
  188. pixcount_mod /= 1000;
  189. read = snprintf(buf+offset, cnt, "fbcvt: %dx%d@%d: CVT Name - ",
  190. cvt->xres, cvt->yres, cvt->refresh);
  191. offset += read;
  192. cnt -= read;
  193. if (cvt->status)
  194. snprintf(buf+offset, cnt, "Not a CVT standard - %d.%03d Mega "
  195. "Pixel Image\n", pixcount, pixcount_mod);
  196. else {
  197. if (pixcount) {
  198. read = snprintf(buf+offset, cnt, "%d", pixcount);
  199. cnt -= read;
  200. offset += read;
  201. }
  202. read = snprintf(buf+offset, cnt, ".%03dM", pixcount_mod);
  203. cnt -= read;
  204. offset += read;
  205. if (cvt->aspect_ratio == 0)
  206. read = snprintf(buf+offset, cnt, "3");
  207. else if (cvt->aspect_ratio == 3)
  208. read = snprintf(buf+offset, cnt, "4");
  209. else if (cvt->aspect_ratio == 1 || cvt->aspect_ratio == 4)
  210. read = snprintf(buf+offset, cnt, "9");
  211. else if (cvt->aspect_ratio == 2)
  212. read = snprintf(buf+offset, cnt, "A");
  213. else
  214. read = 0;
  215. cnt -= read;
  216. offset += read;
  217. if (cvt->flags & FB_CVT_FLAG_REDUCED_BLANK) {
  218. read = snprintf(buf+offset, cnt, "-R");
  219. cnt -= read;
  220. offset += read;
  221. }
  222. }
  223. printk(KERN_INFO "%s\n", buf);
  224. kfree(buf);
  225. }
  226. static void fb_cvt_convert_to_mode(struct fb_cvt_data *cvt,
  227. struct fb_videomode *mode)
  228. {
  229. mode->refresh = cvt->f_refresh;
  230. mode->pixclock = KHZ2PICOS(cvt->pixclock/1000);
  231. mode->left_margin = cvt->h_back_porch;
  232. mode->right_margin = cvt->h_front_porch;
  233. mode->hsync_len = cvt->hsync;
  234. mode->upper_margin = cvt->v_back_porch;
  235. mode->lower_margin = cvt->v_front_porch;
  236. mode->vsync_len = cvt->vsync;
  237. mode->sync &= ~(FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT);
  238. if (cvt->flags & FB_CVT_FLAG_REDUCED_BLANK)
  239. mode->sync |= FB_SYNC_HOR_HIGH_ACT;
  240. else
  241. mode->sync |= FB_SYNC_VERT_HIGH_ACT;
  242. }
  243. /*
  244. * fb_find_mode_cvt - calculate mode using VESA(TM) CVT
  245. * @mode: pointer to fb_videomode; xres, yres, refresh and vmode must be
  246. * pre-filled with the desired values
  247. * @margins: add margin to calculation (1.8% of xres and yres)
  248. * @rb: compute with reduced blanking (for flatpanels)
  249. *
  250. * RETURNS:
  251. * 0 for success
  252. * @mode is filled with computed values. If interlaced, the refresh field
  253. * will be filled with the field rate (2x the frame rate)
  254. *
  255. * DESCRIPTION:
  256. * Computes video timings using VESA(TM) Coordinated Video Timings
  257. */
  258. int fb_find_mode_cvt(struct fb_videomode *mode, int margins, int rb)
  259. {
  260. struct fb_cvt_data cvt;
  261. memset(&cvt, 0, sizeof(cvt));
  262. if (margins)
  263. cvt.flags |= FB_CVT_FLAG_MARGINS;
  264. if (rb)
  265. cvt.flags |= FB_CVT_FLAG_REDUCED_BLANK;
  266. if (mode->vmode & FB_VMODE_INTERLACED)
  267. cvt.flags |= FB_CVT_FLAG_INTERLACED;
  268. cvt.xres = mode->xres;
  269. cvt.yres = mode->yres;
  270. cvt.refresh = mode->refresh;
  271. cvt.f_refresh = cvt.refresh;
  272. cvt.interlace = 1;
  273. if (!cvt.xres || !cvt.yres || !cvt.refresh) {
  274. printk(KERN_INFO "fbcvt: Invalid input parameters\n");
  275. return 1;
  276. }
  277. if (!(cvt.refresh == 50 || cvt.refresh == 60 || cvt.refresh == 70 ||
  278. cvt.refresh == 85)) {
  279. printk(KERN_INFO "fbcvt: Refresh rate not CVT "
  280. "standard\n");
  281. cvt.status = 1;
  282. }
  283. cvt.xres &= ~(FB_CVT_CELLSIZE - 1);
  284. if (cvt.flags & FB_CVT_FLAG_INTERLACED) {
  285. cvt.interlace = 2;
  286. cvt.f_refresh *= 2;
  287. }
  288. if (cvt.flags & FB_CVT_FLAG_REDUCED_BLANK) {
  289. if (cvt.refresh != 60) {
  290. printk(KERN_INFO "fbcvt: 60Hz refresh rate "
  291. "advised for reduced blanking\n");
  292. cvt.status = 1;
  293. }
  294. }
  295. if (cvt.flags & FB_CVT_FLAG_MARGINS) {
  296. cvt.h_margin = (cvt.xres * 18)/1000;
  297. cvt.h_margin &= ~(FB_CVT_CELLSIZE - 1);
  298. cvt.v_margin = ((cvt.yres/cvt.interlace)* 18)/1000;
  299. }
  300. cvt.aspect_ratio = fb_cvt_aspect_ratio(&cvt);
  301. cvt.active_pixels = cvt.xres + 2 * cvt.h_margin;
  302. cvt.hperiod = fb_cvt_hperiod(&cvt);
  303. cvt.vsync = fb_cvt_vbi_tab[cvt.aspect_ratio];
  304. cvt.vtotal = fb_cvt_vtotal(&cvt);
  305. cvt.hblank = fb_cvt_hblank(&cvt);
  306. cvt.htotal = cvt.active_pixels + cvt.hblank;
  307. cvt.hsync = fb_cvt_hsync(&cvt);
  308. cvt.pixclock = fb_cvt_pixclock(&cvt);
  309. cvt.hfreq = cvt.pixclock/cvt.htotal;
  310. cvt.h_back_porch = cvt.hblank/2 + cvt.h_margin;
  311. cvt.h_front_porch = cvt.hblank - cvt.hsync - cvt.h_back_porch +
  312. 2 * cvt.h_margin;
  313. cvt.v_back_porch = 3 + cvt.v_margin;
  314. cvt.v_front_porch = cvt.vtotal - cvt.yres/cvt.interlace -
  315. cvt.v_back_porch - cvt.vsync;
  316. fb_cvt_print_name(&cvt);
  317. fb_cvt_convert_to_mode(&cvt, mode);
  318. return 0;
  319. }