fbcvt.c 9.3 KB

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