imxfb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * linux/drivers/video/imxfb.c
  3. *
  4. * Freescale i.MX Frame Buffer device driver
  5. *
  6. * Copyright (C) 2004 Sascha Hauer, Pengutronix
  7. * Based on acornfb.c Copyright (C) Russell King.
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of this archive for
  11. * more details.
  12. *
  13. * Please direct your questions and comments on this driver to the following
  14. * email address:
  15. *
  16. * linux-arm-kernel@lists.arm.linux.org.uk
  17. */
  18. //#define DEBUG 1
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/string.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/slab.h>
  25. #include <linux/mm.h>
  26. #include <linux/fb.h>
  27. #include <linux/delay.h>
  28. #include <linux/init.h>
  29. #include <linux/ioport.h>
  30. #include <linux/cpufreq.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/dma-mapping.h>
  33. #include <mach/hardware.h>
  34. #include <asm/io.h>
  35. #include <mach/imxfb.h>
  36. /*
  37. * Complain if VAR is out of range.
  38. */
  39. #define DEBUG_VAR 1
  40. #include "imxfb.h"
  41. static struct imxfb_rgb def_rgb_16 = {
  42. .red = { .offset = 8, .length = 4, },
  43. .green = { .offset = 4, .length = 4, },
  44. .blue = { .offset = 0, .length = 4, },
  45. .transp = { .offset = 0, .length = 0, },
  46. };
  47. static struct imxfb_rgb def_rgb_8 = {
  48. .red = { .offset = 0, .length = 8, },
  49. .green = { .offset = 0, .length = 8, },
  50. .blue = { .offset = 0, .length = 8, },
  51. .transp = { .offset = 0, .length = 0, },
  52. };
  53. static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info);
  54. static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
  55. {
  56. chan &= 0xffff;
  57. chan >>= 16 - bf->length;
  58. return chan << bf->offset;
  59. }
  60. #define LCDC_PALETTE(x) __REG2(IMX_LCDC_BASE+0x800, (x)<<2)
  61. static int
  62. imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
  63. u_int trans, struct fb_info *info)
  64. {
  65. struct imxfb_info *fbi = info->par;
  66. u_int val, ret = 1;
  67. #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
  68. if (regno < fbi->palette_size) {
  69. val = (CNVT_TOHW(red, 4) << 8) |
  70. (CNVT_TOHW(green,4) << 4) |
  71. CNVT_TOHW(blue, 4);
  72. LCDC_PALETTE(regno) = val;
  73. ret = 0;
  74. }
  75. return ret;
  76. }
  77. static int
  78. imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  79. u_int trans, struct fb_info *info)
  80. {
  81. struct imxfb_info *fbi = info->par;
  82. unsigned int val;
  83. int ret = 1;
  84. /*
  85. * If inverse mode was selected, invert all the colours
  86. * rather than the register number. The register number
  87. * is what you poke into the framebuffer to produce the
  88. * colour you requested.
  89. */
  90. if (fbi->cmap_inverse) {
  91. red = 0xffff - red;
  92. green = 0xffff - green;
  93. blue = 0xffff - blue;
  94. }
  95. /*
  96. * If greyscale is true, then we convert the RGB value
  97. * to greyscale no mater what visual we are using.
  98. */
  99. if (info->var.grayscale)
  100. red = green = blue = (19595 * red + 38470 * green +
  101. 7471 * blue) >> 16;
  102. switch (info->fix.visual) {
  103. case FB_VISUAL_TRUECOLOR:
  104. /*
  105. * 12 or 16-bit True Colour. We encode the RGB value
  106. * according to the RGB bitfield information.
  107. */
  108. if (regno < 16) {
  109. u32 *pal = info->pseudo_palette;
  110. val = chan_to_field(red, &info->var.red);
  111. val |= chan_to_field(green, &info->var.green);
  112. val |= chan_to_field(blue, &info->var.blue);
  113. pal[regno] = val;
  114. ret = 0;
  115. }
  116. break;
  117. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  118. case FB_VISUAL_PSEUDOCOLOR:
  119. ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
  120. break;
  121. }
  122. return ret;
  123. }
  124. /*
  125. * imxfb_check_var():
  126. * Round up in the following order: bits_per_pixel, xres,
  127. * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
  128. * bitfields, horizontal timing, vertical timing.
  129. */
  130. static int
  131. imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  132. {
  133. struct imxfb_info *fbi = info->par;
  134. int rgbidx;
  135. if (var->xres < MIN_XRES)
  136. var->xres = MIN_XRES;
  137. if (var->yres < MIN_YRES)
  138. var->yres = MIN_YRES;
  139. if (var->xres > fbi->max_xres)
  140. var->xres = fbi->max_xres;
  141. if (var->yres > fbi->max_yres)
  142. var->yres = fbi->max_yres;
  143. var->xres_virtual = max(var->xres_virtual, var->xres);
  144. var->yres_virtual = max(var->yres_virtual, var->yres);
  145. pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
  146. switch (var->bits_per_pixel) {
  147. case 16:
  148. rgbidx = RGB_16;
  149. break;
  150. case 8:
  151. rgbidx = RGB_8;
  152. break;
  153. default:
  154. rgbidx = RGB_16;
  155. }
  156. /*
  157. * Copy the RGB parameters for this display
  158. * from the machine specific parameters.
  159. */
  160. var->red = fbi->rgb[rgbidx]->red;
  161. var->green = fbi->rgb[rgbidx]->green;
  162. var->blue = fbi->rgb[rgbidx]->blue;
  163. var->transp = fbi->rgb[rgbidx]->transp;
  164. pr_debug("RGBT length = %d:%d:%d:%d\n",
  165. var->red.length, var->green.length, var->blue.length,
  166. var->transp.length);
  167. pr_debug("RGBT offset = %d:%d:%d:%d\n",
  168. var->red.offset, var->green.offset, var->blue.offset,
  169. var->transp.offset);
  170. return 0;
  171. }
  172. /*
  173. * imxfb_set_par():
  174. * Set the user defined part of the display for the specified console
  175. */
  176. static int imxfb_set_par(struct fb_info *info)
  177. {
  178. struct imxfb_info *fbi = info->par;
  179. struct fb_var_screeninfo *var = &info->var;
  180. pr_debug("set_par\n");
  181. if (var->bits_per_pixel == 16)
  182. info->fix.visual = FB_VISUAL_TRUECOLOR;
  183. else if (!fbi->cmap_static)
  184. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  185. else {
  186. /*
  187. * Some people have weird ideas about wanting static
  188. * pseudocolor maps. I suspect their user space
  189. * applications are broken.
  190. */
  191. info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
  192. }
  193. info->fix.line_length = var->xres_virtual *
  194. var->bits_per_pixel / 8;
  195. fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
  196. imxfb_activate_var(var, info);
  197. return 0;
  198. }
  199. static void imxfb_enable_controller(struct imxfb_info *fbi)
  200. {
  201. pr_debug("Enabling LCD controller\n");
  202. /* initialize LCDC */
  203. LCDC_RMCR &= ~RMCR_LCDC_EN; /* just to be safe... */
  204. LCDC_SSA = fbi->screen_dma;
  205. /* physical screen start address */
  206. LCDC_VPW = VPW_VPW(fbi->max_xres * fbi->max_bpp / 8 / 4);
  207. LCDC_POS = 0x00000000; /* panning offset 0 (0 pixel offset) */
  208. /* disable hardware cursor */
  209. LCDC_CPOS &= ~(CPOS_CC0 | CPOS_CC1);
  210. LCDC_RMCR = RMCR_LCDC_EN;
  211. if(fbi->backlight_power)
  212. fbi->backlight_power(1);
  213. if(fbi->lcd_power)
  214. fbi->lcd_power(1);
  215. }
  216. static void imxfb_disable_controller(struct imxfb_info *fbi)
  217. {
  218. pr_debug("Disabling LCD controller\n");
  219. if(fbi->backlight_power)
  220. fbi->backlight_power(0);
  221. if(fbi->lcd_power)
  222. fbi->lcd_power(0);
  223. LCDC_RMCR = 0;
  224. }
  225. static int imxfb_blank(int blank, struct fb_info *info)
  226. {
  227. struct imxfb_info *fbi = info->par;
  228. pr_debug("imxfb_blank: blank=%d\n", blank);
  229. switch (blank) {
  230. case FB_BLANK_POWERDOWN:
  231. case FB_BLANK_VSYNC_SUSPEND:
  232. case FB_BLANK_HSYNC_SUSPEND:
  233. case FB_BLANK_NORMAL:
  234. imxfb_disable_controller(fbi);
  235. break;
  236. case FB_BLANK_UNBLANK:
  237. imxfb_enable_controller(fbi);
  238. break;
  239. }
  240. return 0;
  241. }
  242. static struct fb_ops imxfb_ops = {
  243. .owner = THIS_MODULE,
  244. .fb_check_var = imxfb_check_var,
  245. .fb_set_par = imxfb_set_par,
  246. .fb_setcolreg = imxfb_setcolreg,
  247. .fb_fillrect = cfb_fillrect,
  248. .fb_copyarea = cfb_copyarea,
  249. .fb_imageblit = cfb_imageblit,
  250. .fb_blank = imxfb_blank,
  251. };
  252. /*
  253. * imxfb_activate_var():
  254. * Configures LCD Controller based on entries in var parameter. Settings are
  255. * only written to the controller if changes were made.
  256. */
  257. static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
  258. {
  259. struct imxfb_info *fbi = info->par;
  260. pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
  261. var->xres, var->hsync_len,
  262. var->left_margin, var->right_margin);
  263. pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
  264. var->yres, var->vsync_len,
  265. var->upper_margin, var->lower_margin);
  266. #if DEBUG_VAR
  267. if (var->xres < 16 || var->xres > 1024)
  268. printk(KERN_ERR "%s: invalid xres %d\n",
  269. info->fix.id, var->xres);
  270. if (var->hsync_len < 1 || var->hsync_len > 64)
  271. printk(KERN_ERR "%s: invalid hsync_len %d\n",
  272. info->fix.id, var->hsync_len);
  273. if (var->left_margin > 255)
  274. printk(KERN_ERR "%s: invalid left_margin %d\n",
  275. info->fix.id, var->left_margin);
  276. if (var->right_margin > 255)
  277. printk(KERN_ERR "%s: invalid right_margin %d\n",
  278. info->fix.id, var->right_margin);
  279. if (var->yres < 1 || var->yres > 511)
  280. printk(KERN_ERR "%s: invalid yres %d\n",
  281. info->fix.id, var->yres);
  282. if (var->vsync_len > 100)
  283. printk(KERN_ERR "%s: invalid vsync_len %d\n",
  284. info->fix.id, var->vsync_len);
  285. if (var->upper_margin > 63)
  286. printk(KERN_ERR "%s: invalid upper_margin %d\n",
  287. info->fix.id, var->upper_margin);
  288. if (var->lower_margin > 255)
  289. printk(KERN_ERR "%s: invalid lower_margin %d\n",
  290. info->fix.id, var->lower_margin);
  291. #endif
  292. LCDC_HCR = HCR_H_WIDTH(var->hsync_len) |
  293. HCR_H_WAIT_1(var->left_margin) |
  294. HCR_H_WAIT_2(var->right_margin);
  295. LCDC_VCR = VCR_V_WIDTH(var->vsync_len) |
  296. VCR_V_WAIT_1(var->upper_margin) |
  297. VCR_V_WAIT_2(var->lower_margin);
  298. LCDC_SIZE = SIZE_XMAX(var->xres) | SIZE_YMAX(var->yres);
  299. LCDC_PCR = fbi->pcr;
  300. LCDC_PWMR = fbi->pwmr;
  301. LCDC_LSCR1 = fbi->lscr1;
  302. LCDC_DMACR = fbi->dmacr;
  303. return 0;
  304. }
  305. #ifdef CONFIG_PM
  306. /*
  307. * Power management hooks. Note that we won't be called from IRQ context,
  308. * unlike the blank functions above, so we may sleep.
  309. */
  310. static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
  311. {
  312. struct imxfb_info *fbi = platform_get_drvdata(dev);
  313. pr_debug("%s\n",__func__);
  314. imxfb_disable_controller(fbi);
  315. return 0;
  316. }
  317. static int imxfb_resume(struct platform_device *dev)
  318. {
  319. struct imxfb_info *fbi = platform_get_drvdata(dev);
  320. pr_debug("%s\n",__func__);
  321. imxfb_enable_controller(fbi);
  322. return 0;
  323. }
  324. #else
  325. #define imxfb_suspend NULL
  326. #define imxfb_resume NULL
  327. #endif
  328. static int __init imxfb_init_fbinfo(struct device *dev)
  329. {
  330. struct imxfb_mach_info *inf = dev->platform_data;
  331. struct fb_info *info = dev_get_drvdata(dev);
  332. struct imxfb_info *fbi = info->par;
  333. pr_debug("%s\n",__func__);
  334. info->pseudo_palette = kmalloc( sizeof(u32) * 16, GFP_KERNEL);
  335. if (!info->pseudo_palette)
  336. return -ENOMEM;
  337. memset(fbi, 0, sizeof(struct imxfb_info));
  338. fbi->dev = dev;
  339. strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
  340. info->fix.type = FB_TYPE_PACKED_PIXELS;
  341. info->fix.type_aux = 0;
  342. info->fix.xpanstep = 0;
  343. info->fix.ypanstep = 0;
  344. info->fix.ywrapstep = 0;
  345. info->fix.accel = FB_ACCEL_NONE;
  346. info->var.nonstd = 0;
  347. info->var.activate = FB_ACTIVATE_NOW;
  348. info->var.height = -1;
  349. info->var.width = -1;
  350. info->var.accel_flags = 0;
  351. info->var.vmode = FB_VMODE_NONINTERLACED;
  352. info->fbops = &imxfb_ops;
  353. info->flags = FBINFO_FLAG_DEFAULT | FBINFO_READS_FAST;
  354. fbi->rgb[RGB_16] = &def_rgb_16;
  355. fbi->rgb[RGB_8] = &def_rgb_8;
  356. fbi->max_xres = inf->xres;
  357. info->var.xres = inf->xres;
  358. info->var.xres_virtual = inf->xres;
  359. fbi->max_yres = inf->yres;
  360. info->var.yres = inf->yres;
  361. info->var.yres_virtual = inf->yres;
  362. fbi->max_bpp = inf->bpp;
  363. info->var.bits_per_pixel = inf->bpp;
  364. info->var.nonstd = inf->nonstd;
  365. info->var.pixclock = inf->pixclock;
  366. info->var.hsync_len = inf->hsync_len;
  367. info->var.left_margin = inf->left_margin;
  368. info->var.right_margin = inf->right_margin;
  369. info->var.vsync_len = inf->vsync_len;
  370. info->var.upper_margin = inf->upper_margin;
  371. info->var.lower_margin = inf->lower_margin;
  372. info->var.sync = inf->sync;
  373. info->var.grayscale = inf->cmap_greyscale;
  374. fbi->cmap_inverse = inf->cmap_inverse;
  375. fbi->cmap_static = inf->cmap_static;
  376. fbi->pcr = inf->pcr;
  377. fbi->lscr1 = inf->lscr1;
  378. fbi->dmacr = inf->dmacr;
  379. fbi->pwmr = inf->pwmr;
  380. fbi->lcd_power = inf->lcd_power;
  381. fbi->backlight_power = inf->backlight_power;
  382. info->fix.smem_len = fbi->max_xres * fbi->max_yres *
  383. fbi->max_bpp / 8;
  384. return 0;
  385. }
  386. /*
  387. * Allocates the DRAM memory for the frame buffer. This buffer is
  388. * remapped into a non-cached, non-buffered, memory region to
  389. * allow pixel writes to occur without flushing the cache.
  390. * Once this area is remapped, all virtual memory access to the
  391. * video memory should occur at the new region.
  392. */
  393. static int __init imxfb_map_video_memory(struct fb_info *info)
  394. {
  395. struct imxfb_info *fbi = info->par;
  396. fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
  397. fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
  398. &fbi->map_dma,GFP_KERNEL);
  399. if (fbi->map_cpu) {
  400. info->screen_base = fbi->map_cpu;
  401. fbi->screen_cpu = fbi->map_cpu;
  402. fbi->screen_dma = fbi->map_dma;
  403. info->fix.smem_start = fbi->screen_dma;
  404. }
  405. return fbi->map_cpu ? 0 : -ENOMEM;
  406. }
  407. static int __init imxfb_probe(struct platform_device *pdev)
  408. {
  409. struct imxfb_info *fbi;
  410. struct fb_info *info;
  411. struct imxfb_mach_info *inf;
  412. struct resource *res;
  413. int ret;
  414. printk("i.MX Framebuffer driver\n");
  415. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  416. if(!res)
  417. return -ENODEV;
  418. inf = pdev->dev.platform_data;
  419. if(!inf) {
  420. dev_err(&pdev->dev,"No platform_data available\n");
  421. return -ENOMEM;
  422. }
  423. info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
  424. if(!info)
  425. return -ENOMEM;
  426. fbi = info->par;
  427. platform_set_drvdata(pdev, info);
  428. ret = imxfb_init_fbinfo(&pdev->dev);
  429. if( ret < 0 )
  430. goto failed_init;
  431. res = request_mem_region(res->start, res->end - res->start + 1, "IMXFB");
  432. if (!res) {
  433. ret = -EBUSY;
  434. goto failed_regs;
  435. }
  436. if (!inf->fixed_screen_cpu) {
  437. ret = imxfb_map_video_memory(info);
  438. if (ret) {
  439. dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
  440. ret = -ENOMEM;
  441. goto failed_map;
  442. }
  443. } else {
  444. /* Fixed framebuffer mapping enables location of the screen in eSRAM */
  445. fbi->map_cpu = inf->fixed_screen_cpu;
  446. fbi->map_dma = inf->fixed_screen_dma;
  447. info->screen_base = fbi->map_cpu;
  448. fbi->screen_cpu = fbi->map_cpu;
  449. fbi->screen_dma = fbi->map_dma;
  450. info->fix.smem_start = fbi->screen_dma;
  451. }
  452. /*
  453. * This makes sure that our colour bitfield
  454. * descriptors are correctly initialised.
  455. */
  456. imxfb_check_var(&info->var, info);
  457. ret = fb_alloc_cmap(&info->cmap, 1<<info->var.bits_per_pixel, 0);
  458. if (ret < 0)
  459. goto failed_cmap;
  460. imxfb_set_par(info);
  461. ret = register_framebuffer(info);
  462. if (ret < 0) {
  463. dev_err(&pdev->dev, "failed to register framebuffer\n");
  464. goto failed_register;
  465. }
  466. imxfb_enable_controller(fbi);
  467. return 0;
  468. failed_register:
  469. fb_dealloc_cmap(&info->cmap);
  470. failed_cmap:
  471. if (!inf->fixed_screen_cpu)
  472. dma_free_writecombine(&pdev->dev,fbi->map_size,fbi->map_cpu,
  473. fbi->map_dma);
  474. failed_map:
  475. kfree(info->pseudo_palette);
  476. failed_regs:
  477. release_mem_region(res->start, res->end - res->start);
  478. failed_init:
  479. platform_set_drvdata(pdev, NULL);
  480. framebuffer_release(info);
  481. return ret;
  482. }
  483. static int imxfb_remove(struct platform_device *pdev)
  484. {
  485. struct fb_info *info = platform_get_drvdata(pdev);
  486. struct imxfb_info *fbi = info->par;
  487. struct resource *res;
  488. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  489. imxfb_disable_controller(fbi);
  490. unregister_framebuffer(info);
  491. fb_dealloc_cmap(&info->cmap);
  492. kfree(info->pseudo_palette);
  493. framebuffer_release(info);
  494. release_mem_region(res->start, res->end - res->start + 1);
  495. platform_set_drvdata(pdev, NULL);
  496. return 0;
  497. }
  498. void imxfb_shutdown(struct platform_device * dev)
  499. {
  500. struct fb_info *info = platform_get_drvdata(dev);
  501. struct imxfb_info *fbi = info->par;
  502. imxfb_disable_controller(fbi);
  503. }
  504. static struct platform_driver imxfb_driver = {
  505. .probe = imxfb_probe,
  506. .suspend = imxfb_suspend,
  507. .resume = imxfb_resume,
  508. .remove = imxfb_remove,
  509. .shutdown = imxfb_shutdown,
  510. .driver = {
  511. .name = "imx-fb",
  512. },
  513. };
  514. int __init imxfb_init(void)
  515. {
  516. return platform_driver_register(&imxfb_driver);
  517. }
  518. static void __exit imxfb_cleanup(void)
  519. {
  520. platform_driver_unregister(&imxfb_driver);
  521. }
  522. module_init(imxfb_init);
  523. module_exit(imxfb_cleanup);
  524. MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
  525. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  526. MODULE_LICENSE("GPL");