vt8500lcdfb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * linux/drivers/video/vt8500lcdfb.c
  3. *
  4. * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
  5. *
  6. * Based on skeletonfb.c and pxafb.c
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/string.h>
  21. #include <linux/mm.h>
  22. #include <linux/slab.h>
  23. #include <linux/delay.h>
  24. #include <linux/fb.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/wait.h>
  31. #include <mach/vt8500fb.h>
  32. #include "vt8500lcdfb.h"
  33. #include "wmt_ge_rops.h"
  34. #define to_vt8500lcd_info(__info) container_of(__info, \
  35. struct vt8500lcd_info, fb)
  36. static int vt8500lcd_set_par(struct fb_info *info)
  37. {
  38. struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
  39. int reg_bpp = 5; /* 16bpp */
  40. int i;
  41. unsigned long control0;
  42. if (!fbi)
  43. return -EINVAL;
  44. if (info->var.bits_per_pixel <= 8) {
  45. /* palettized */
  46. info->var.red.offset = 0;
  47. info->var.red.length = info->var.bits_per_pixel;
  48. info->var.red.msb_right = 0;
  49. info->var.green.offset = 0;
  50. info->var.green.length = info->var.bits_per_pixel;
  51. info->var.green.msb_right = 0;
  52. info->var.blue.offset = 0;
  53. info->var.blue.length = info->var.bits_per_pixel;
  54. info->var.blue.msb_right = 0;
  55. info->var.transp.offset = 0;
  56. info->var.transp.length = 0;
  57. info->var.transp.msb_right = 0;
  58. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  59. info->fix.line_length = info->var.xres_virtual /
  60. (8/info->var.bits_per_pixel);
  61. } else {
  62. /* non-palettized */
  63. info->var.transp.offset = 0;
  64. info->var.transp.length = 0;
  65. info->var.transp.msb_right = 0;
  66. if (info->var.bits_per_pixel == 16) {
  67. /* RGB565 */
  68. info->var.red.offset = 11;
  69. info->var.red.length = 5;
  70. info->var.red.msb_right = 0;
  71. info->var.green.offset = 5;
  72. info->var.green.length = 6;
  73. info->var.green.msb_right = 0;
  74. info->var.blue.offset = 0;
  75. info->var.blue.length = 5;
  76. info->var.blue.msb_right = 0;
  77. } else {
  78. /* Equal depths per channel */
  79. info->var.red.offset = info->var.bits_per_pixel
  80. * 2 / 3;
  81. info->var.red.length = info->var.bits_per_pixel / 3;
  82. info->var.red.msb_right = 0;
  83. info->var.green.offset = info->var.bits_per_pixel / 3;
  84. info->var.green.length = info->var.bits_per_pixel / 3;
  85. info->var.green.msb_right = 0;
  86. info->var.blue.offset = 0;
  87. info->var.blue.length = info->var.bits_per_pixel / 3;
  88. info->var.blue.msb_right = 0;
  89. }
  90. info->fix.visual = FB_VISUAL_TRUECOLOR;
  91. info->fix.line_length = info->var.bits_per_pixel > 16 ?
  92. info->var.xres_virtual << 2 :
  93. info->var.xres_virtual << 1;
  94. }
  95. for (i = 0; i < 8; i++) {
  96. if (bpp_values[i] == info->var.bits_per_pixel) {
  97. reg_bpp = i;
  98. continue;
  99. }
  100. }
  101. control0 = readl(fbi->regbase) & ~0xf;
  102. writel(0, fbi->regbase);
  103. while (readl(fbi->regbase + 0x38) & 0x10)
  104. /* wait */;
  105. writel((((info->var.hsync_len - 1) & 0x3f) << 26)
  106. | ((info->var.left_margin & 0xff) << 18)
  107. | (((info->var.xres - 1) & 0x3ff) << 8)
  108. | (info->var.right_margin & 0xff), fbi->regbase + 0x4);
  109. writel((((info->var.vsync_len - 1) & 0x3f) << 26)
  110. | ((info->var.upper_margin & 0xff) << 18)
  111. | (((info->var.yres - 1) & 0x3ff) << 8)
  112. | (info->var.lower_margin & 0xff), fbi->regbase + 0x8);
  113. writel((((info->var.yres - 1) & 0x400) << 2)
  114. | ((info->var.xres - 1) & 0x400), fbi->regbase + 0x10);
  115. writel(0x80000000, fbi->regbase + 0x20);
  116. writel(control0 | (reg_bpp << 1) | 0x100, fbi->regbase);
  117. return 0;
  118. }
  119. static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
  120. {
  121. chan &= 0xffff;
  122. chan >>= 16 - bf->length;
  123. return chan << bf->offset;
  124. }
  125. static int vt8500lcd_setcolreg(unsigned regno, unsigned red, unsigned green,
  126. unsigned blue, unsigned transp,
  127. struct fb_info *info) {
  128. struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
  129. int ret = 1;
  130. unsigned int val;
  131. if (regno >= 256)
  132. return -EINVAL;
  133. if (info->var.grayscale)
  134. red = green = blue =
  135. (19595 * red + 38470 * green + 7471 * blue) >> 16;
  136. switch (fbi->fb.fix.visual) {
  137. case FB_VISUAL_TRUECOLOR:
  138. if (regno < 16) {
  139. u32 *pal = fbi->fb.pseudo_palette;
  140. val = chan_to_field(red, &fbi->fb.var.red);
  141. val |= chan_to_field(green, &fbi->fb.var.green);
  142. val |= chan_to_field(blue, &fbi->fb.var.blue);
  143. pal[regno] = val;
  144. ret = 0;
  145. }
  146. break;
  147. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  148. case FB_VISUAL_PSEUDOCOLOR:
  149. writew((red & 0xf800)
  150. | ((green >> 5) & 0x7e0)
  151. | ((blue >> 11) & 0x1f),
  152. fbi->palette_cpu + sizeof(u16) * regno);
  153. break;
  154. }
  155. return ret;
  156. }
  157. static int vt8500lcd_ioctl(struct fb_info *info, unsigned int cmd,
  158. unsigned long arg)
  159. {
  160. int ret = 0;
  161. struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
  162. if (cmd == FBIO_WAITFORVSYNC) {
  163. /* Unmask End of Frame interrupt */
  164. writel(0xffffffff ^ (1 << 3), fbi->regbase + 0x3c);
  165. ret = wait_event_interruptible_timeout(fbi->wait,
  166. readl(fbi->regbase + 0x38) & (1 << 3), HZ / 10);
  167. /* Mask back to reduce unwanted interrupt traffic */
  168. writel(0xffffffff, fbi->regbase + 0x3c);
  169. if (ret < 0)
  170. return ret;
  171. if (ret == 0)
  172. return -ETIMEDOUT;
  173. }
  174. return ret;
  175. }
  176. static int vt8500lcd_pan_display(struct fb_var_screeninfo *var,
  177. struct fb_info *info)
  178. {
  179. unsigned pixlen = info->fix.line_length / info->var.xres_virtual;
  180. unsigned off = pixlen * var->xoffset
  181. + info->fix.line_length * var->yoffset;
  182. struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
  183. writel((1 << 31)
  184. | (((var->xres_virtual - var->xres) * pixlen / 4) << 20)
  185. | (off >> 2), fbi->regbase + 0x20);
  186. return 0;
  187. }
  188. /*
  189. * vt8500lcd_blank():
  190. * Blank the display by setting all palette values to zero. Note,
  191. * True Color modes do not really use the palette, so this will not
  192. * blank the display in all modes.
  193. */
  194. static int vt8500lcd_blank(int blank, struct fb_info *info)
  195. {
  196. int i;
  197. switch (blank) {
  198. case FB_BLANK_POWERDOWN:
  199. case FB_BLANK_VSYNC_SUSPEND:
  200. case FB_BLANK_HSYNC_SUSPEND:
  201. case FB_BLANK_NORMAL:
  202. if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR ||
  203. info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
  204. for (i = 0; i < 256; i++)
  205. vt8500lcd_setcolreg(i, 0, 0, 0, 0, info);
  206. case FB_BLANK_UNBLANK:
  207. if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR ||
  208. info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
  209. fb_set_cmap(&info->cmap, info);
  210. }
  211. return 0;
  212. }
  213. static struct fb_ops vt8500lcd_ops = {
  214. .owner = THIS_MODULE,
  215. .fb_set_par = vt8500lcd_set_par,
  216. .fb_setcolreg = vt8500lcd_setcolreg,
  217. .fb_fillrect = wmt_ge_fillrect,
  218. .fb_copyarea = wmt_ge_copyarea,
  219. .fb_imageblit = sys_imageblit,
  220. .fb_sync = wmt_ge_sync,
  221. .fb_ioctl = vt8500lcd_ioctl,
  222. .fb_pan_display = vt8500lcd_pan_display,
  223. .fb_blank = vt8500lcd_blank,
  224. };
  225. static irqreturn_t vt8500lcd_handle_irq(int irq, void *dev_id)
  226. {
  227. struct vt8500lcd_info *fbi = dev_id;
  228. if (readl(fbi->regbase + 0x38) & (1 << 3))
  229. wake_up_interruptible(&fbi->wait);
  230. writel(0xffffffff, fbi->regbase + 0x38);
  231. return IRQ_HANDLED;
  232. }
  233. static int __devinit vt8500lcd_probe(struct platform_device *pdev)
  234. {
  235. struct vt8500lcd_info *fbi;
  236. struct resource *res;
  237. struct vt8500fb_platform_data *pdata = pdev->dev.platform_data;
  238. void *addr;
  239. int irq, ret;
  240. ret = -ENOMEM;
  241. fbi = NULL;
  242. fbi = kzalloc(sizeof(struct vt8500lcd_info) + sizeof(u32) * 16,
  243. GFP_KERNEL);
  244. if (!fbi) {
  245. dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
  246. ret = -ENOMEM;
  247. goto failed;
  248. }
  249. strcpy(fbi->fb.fix.id, "VT8500 LCD");
  250. fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  251. fbi->fb.fix.xpanstep = 0;
  252. fbi->fb.fix.ypanstep = 1;
  253. fbi->fb.fix.ywrapstep = 0;
  254. fbi->fb.fix.accel = FB_ACCEL_NONE;
  255. fbi->fb.var.nonstd = 0;
  256. fbi->fb.var.activate = FB_ACTIVATE_NOW;
  257. fbi->fb.var.height = -1;
  258. fbi->fb.var.width = -1;
  259. fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
  260. fbi->fb.fbops = &vt8500lcd_ops;
  261. fbi->fb.flags = FBINFO_DEFAULT
  262. | FBINFO_HWACCEL_COPYAREA
  263. | FBINFO_HWACCEL_FILLRECT
  264. | FBINFO_HWACCEL_YPAN
  265. | FBINFO_VIRTFB
  266. | FBINFO_PARTIAL_PAN_OK;
  267. fbi->fb.node = -1;
  268. addr = fbi;
  269. addr = addr + sizeof(struct vt8500lcd_info);
  270. fbi->fb.pseudo_palette = addr;
  271. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  272. if (res == NULL) {
  273. dev_err(&pdev->dev, "no I/O memory resource defined\n");
  274. ret = -ENODEV;
  275. goto failed_fbi;
  276. }
  277. res = request_mem_region(res->start, resource_size(res), "vt8500lcd");
  278. if (res == NULL) {
  279. dev_err(&pdev->dev, "failed to request I/O memory\n");
  280. ret = -EBUSY;
  281. goto failed_fbi;
  282. }
  283. fbi->regbase = ioremap(res->start, resource_size(res));
  284. if (fbi->regbase == NULL) {
  285. dev_err(&pdev->dev, "failed to map I/O memory\n");
  286. ret = -EBUSY;
  287. goto failed_free_res;
  288. }
  289. fbi->fb.fix.smem_start = pdata->video_mem_phys;
  290. fbi->fb.fix.smem_len = pdata->video_mem_len;
  291. fbi->fb.screen_base = pdata->video_mem_virt;
  292. fbi->palette_size = PAGE_ALIGN(512);
  293. fbi->palette_cpu = dma_alloc_coherent(&pdev->dev,
  294. fbi->palette_size,
  295. &fbi->palette_phys,
  296. GFP_KERNEL);
  297. if (fbi->palette_cpu == NULL) {
  298. dev_err(&pdev->dev, "Failed to allocate palette buffer\n");
  299. ret = -ENOMEM;
  300. goto failed_free_io;
  301. }
  302. irq = platform_get_irq(pdev, 0);
  303. if (irq < 0) {
  304. dev_err(&pdev->dev, "no IRQ defined\n");
  305. ret = -ENODEV;
  306. goto failed_free_palette;
  307. }
  308. ret = request_irq(irq, vt8500lcd_handle_irq, IRQF_DISABLED, "LCD", fbi);
  309. if (ret) {
  310. dev_err(&pdev->dev, "request_irq failed: %d\n", ret);
  311. ret = -EBUSY;
  312. goto failed_free_palette;
  313. }
  314. init_waitqueue_head(&fbi->wait);
  315. if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
  316. dev_err(&pdev->dev, "Failed to allocate color map\n");
  317. ret = -ENOMEM;
  318. goto failed_free_irq;
  319. }
  320. fb_videomode_to_var(&fbi->fb.var, &pdata->mode);
  321. fbi->fb.var.bits_per_pixel = pdata->bpp;
  322. fbi->fb.var.xres_virtual = pdata->xres_virtual;
  323. fbi->fb.var.yres_virtual = pdata->yres_virtual;
  324. ret = vt8500lcd_set_par(&fbi->fb);
  325. if (ret) {
  326. dev_err(&pdev->dev, "Failed to set parameters\n");
  327. goto failed_free_cmap;
  328. }
  329. writel(fbi->fb.fix.smem_start >> 22, fbi->regbase + 0x1c);
  330. writel((fbi->palette_phys & 0xfffffe00) | 1, fbi->regbase + 0x18);
  331. platform_set_drvdata(pdev, fbi);
  332. ret = register_framebuffer(&fbi->fb);
  333. if (ret < 0) {
  334. dev_err(&pdev->dev,
  335. "Failed to register framebuffer device: %d\n", ret);
  336. goto failed_free_cmap;
  337. }
  338. /*
  339. * Ok, now enable the LCD controller
  340. */
  341. writel(readl(fbi->regbase) | 1, fbi->regbase);
  342. return 0;
  343. failed_free_cmap:
  344. if (fbi->fb.cmap.len)
  345. fb_dealloc_cmap(&fbi->fb.cmap);
  346. failed_free_irq:
  347. free_irq(irq, fbi);
  348. failed_free_palette:
  349. dma_free_coherent(&pdev->dev, fbi->palette_size,
  350. fbi->palette_cpu, fbi->palette_phys);
  351. failed_free_io:
  352. iounmap(fbi->regbase);
  353. failed_free_res:
  354. release_mem_region(res->start, resource_size(res));
  355. failed_fbi:
  356. platform_set_drvdata(pdev, NULL);
  357. kfree(fbi);
  358. failed:
  359. return ret;
  360. }
  361. static int __devexit vt8500lcd_remove(struct platform_device *pdev)
  362. {
  363. struct vt8500lcd_info *fbi = platform_get_drvdata(pdev);
  364. struct resource *res;
  365. int irq;
  366. unregister_framebuffer(&fbi->fb);
  367. writel(0, fbi->regbase);
  368. if (fbi->fb.cmap.len)
  369. fb_dealloc_cmap(&fbi->fb.cmap);
  370. irq = platform_get_irq(pdev, 0);
  371. free_irq(irq, fbi);
  372. dma_free_coherent(&pdev->dev, fbi->palette_size,
  373. fbi->palette_cpu, fbi->palette_phys);
  374. iounmap(fbi->regbase);
  375. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  376. release_mem_region(res->start, resource_size(res));
  377. kfree(fbi);
  378. return 0;
  379. }
  380. static struct platform_driver vt8500lcd_driver = {
  381. .probe = vt8500lcd_probe,
  382. .remove = __devexit_p(vt8500lcd_remove),
  383. .driver = {
  384. .owner = THIS_MODULE,
  385. .name = "vt8500-lcd",
  386. },
  387. };
  388. static int __init vt8500lcd_init(void)
  389. {
  390. return platform_driver_register(&vt8500lcd_driver);
  391. }
  392. static void __exit vt8500lcd_exit(void)
  393. {
  394. platform_driver_unregister(&vt8500lcd_driver);
  395. }
  396. module_init(vt8500lcd_init);
  397. module_exit(vt8500lcd_exit);
  398. MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
  399. MODULE_DESCRIPTION("LCD controller driver for VIA VT8500");
  400. MODULE_LICENSE("GPL");