vt8500lcdfb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. static struct fb_ops vt8500lcd_ops = {
  189. .owner = THIS_MODULE,
  190. .fb_set_par = vt8500lcd_set_par,
  191. .fb_setcolreg = vt8500lcd_setcolreg,
  192. .fb_fillrect = wmt_ge_fillrect,
  193. .fb_copyarea = wmt_ge_copyarea,
  194. .fb_imageblit = sys_imageblit,
  195. .fb_sync = wmt_ge_sync,
  196. .fb_ioctl = vt8500lcd_ioctl,
  197. .fb_pan_display = vt8500lcd_pan_display,
  198. };
  199. static irqreturn_t vt8500lcd_handle_irq(int irq, void *dev_id)
  200. {
  201. struct vt8500lcd_info *fbi = dev_id;
  202. if (readl(fbi->regbase + 0x38) & (1 << 3))
  203. wake_up_interruptible(&fbi->wait);
  204. writel(0xffffffff, fbi->regbase + 0x38);
  205. return IRQ_HANDLED;
  206. }
  207. static int __devinit vt8500lcd_probe(struct platform_device *pdev)
  208. {
  209. struct vt8500lcd_info *fbi;
  210. struct resource *res;
  211. struct vt8500fb_platform_data *pdata = pdev->dev.platform_data;
  212. void *addr;
  213. int irq, ret;
  214. ret = -ENOMEM;
  215. fbi = NULL;
  216. fbi = kzalloc(sizeof(struct vt8500lcd_info) + sizeof(u32) * 16,
  217. GFP_KERNEL);
  218. if (!fbi) {
  219. dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
  220. ret = -ENOMEM;
  221. goto failed;
  222. }
  223. strcpy(fbi->fb.fix.id, "VT8500 LCD");
  224. fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  225. fbi->fb.fix.xpanstep = 0;
  226. fbi->fb.fix.ypanstep = 1;
  227. fbi->fb.fix.ywrapstep = 0;
  228. fbi->fb.fix.accel = FB_ACCEL_NONE;
  229. fbi->fb.var.nonstd = 0;
  230. fbi->fb.var.activate = FB_ACTIVATE_NOW;
  231. fbi->fb.var.height = -1;
  232. fbi->fb.var.width = -1;
  233. fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
  234. fbi->fb.fbops = &vt8500lcd_ops;
  235. fbi->fb.flags = FBINFO_DEFAULT
  236. | FBINFO_HWACCEL_COPYAREA
  237. | FBINFO_HWACCEL_FILLRECT
  238. | FBINFO_HWACCEL_YPAN
  239. | FBINFO_VIRTFB
  240. | FBINFO_PARTIAL_PAN_OK;
  241. fbi->fb.node = -1;
  242. addr = fbi;
  243. addr = addr + sizeof(struct vt8500lcd_info);
  244. fbi->fb.pseudo_palette = addr;
  245. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  246. if (res == NULL) {
  247. dev_err(&pdev->dev, "no I/O memory resource defined\n");
  248. ret = -ENODEV;
  249. goto failed_fbi;
  250. }
  251. res = request_mem_region(res->start, resource_size(res), "vt8500lcd");
  252. if (res == NULL) {
  253. dev_err(&pdev->dev, "failed to request I/O memory\n");
  254. ret = -EBUSY;
  255. goto failed_fbi;
  256. }
  257. fbi->regbase = ioremap(res->start, resource_size(res));
  258. if (fbi->regbase == NULL) {
  259. dev_err(&pdev->dev, "failed to map I/O memory\n");
  260. ret = -EBUSY;
  261. goto failed_free_res;
  262. }
  263. fbi->fb.fix.smem_start = pdata->video_mem_phys;
  264. fbi->fb.fix.smem_len = pdata->video_mem_len;
  265. fbi->fb.screen_base = pdata->video_mem_virt;
  266. fbi->palette_size = PAGE_ALIGN(512);
  267. fbi->palette_cpu = dma_alloc_coherent(&pdev->dev,
  268. fbi->palette_size,
  269. &fbi->palette_phys,
  270. GFP_KERNEL);
  271. if (fbi->palette_cpu == NULL) {
  272. dev_err(&pdev->dev, "Failed to allocate palette buffer\n");
  273. ret = -ENOMEM;
  274. goto failed_free_io;
  275. }
  276. irq = platform_get_irq(pdev, 0);
  277. if (irq < 0) {
  278. dev_err(&pdev->dev, "no IRQ defined\n");
  279. ret = -ENODEV;
  280. goto failed_free_palette;
  281. }
  282. ret = request_irq(irq, vt8500lcd_handle_irq, IRQF_DISABLED, "LCD", fbi);
  283. if (ret) {
  284. dev_err(&pdev->dev, "request_irq failed: %d\n", ret);
  285. ret = -EBUSY;
  286. goto failed_free_palette;
  287. }
  288. init_waitqueue_head(&fbi->wait);
  289. if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
  290. dev_err(&pdev->dev, "Failed to allocate color map\n");
  291. ret = -ENOMEM;
  292. goto failed_free_irq;
  293. }
  294. fb_videomode_to_var(&fbi->fb.var, &pdata->mode);
  295. fbi->fb.var.bits_per_pixel = pdata->bpp;
  296. fbi->fb.var.xres_virtual = pdata->xres_virtual;
  297. fbi->fb.var.yres_virtual = pdata->yres_virtual;
  298. ret = vt8500lcd_set_par(&fbi->fb);
  299. if (ret) {
  300. dev_err(&pdev->dev, "Failed to set parameters\n");
  301. goto failed_free_cmap;
  302. }
  303. writel(fbi->fb.fix.smem_start >> 22, fbi->regbase + 0x1c);
  304. writel((fbi->palette_phys & 0xfffffe00) | 1, fbi->regbase + 0x18);
  305. platform_set_drvdata(pdev, fbi);
  306. ret = register_framebuffer(&fbi->fb);
  307. if (ret < 0) {
  308. dev_err(&pdev->dev,
  309. "Failed to register framebuffer device: %d\n", ret);
  310. goto failed_free_cmap;
  311. }
  312. /*
  313. * Ok, now enable the LCD controller
  314. */
  315. writel(readl(fbi->regbase) | 1, fbi->regbase);
  316. return 0;
  317. failed_free_cmap:
  318. if (fbi->fb.cmap.len)
  319. fb_dealloc_cmap(&fbi->fb.cmap);
  320. failed_free_irq:
  321. free_irq(irq, fbi);
  322. failed_free_palette:
  323. dma_free_coherent(&pdev->dev, fbi->palette_size,
  324. fbi->palette_cpu, fbi->palette_phys);
  325. failed_free_io:
  326. iounmap(fbi->regbase);
  327. failed_free_res:
  328. release_mem_region(res->start, resource_size(res));
  329. failed_fbi:
  330. platform_set_drvdata(pdev, NULL);
  331. kfree(fbi);
  332. failed:
  333. return ret;
  334. }
  335. static int __devexit vt8500lcd_remove(struct platform_device *pdev)
  336. {
  337. struct vt8500lcd_info *fbi = platform_get_drvdata(pdev);
  338. struct resource *res;
  339. int irq;
  340. unregister_framebuffer(&fbi->fb);
  341. writel(0, fbi->regbase);
  342. if (fbi->fb.cmap.len)
  343. fb_dealloc_cmap(&fbi->fb.cmap);
  344. irq = platform_get_irq(pdev, 0);
  345. free_irq(irq, fbi);
  346. dma_free_coherent(&pdev->dev, fbi->palette_size,
  347. fbi->palette_cpu, fbi->palette_phys);
  348. iounmap(fbi->regbase);
  349. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  350. release_mem_region(res->start, resource_size(res));
  351. kfree(fbi);
  352. return 0;
  353. }
  354. static struct platform_driver vt8500lcd_driver = {
  355. .probe = vt8500lcd_probe,
  356. .remove = __devexit_p(vt8500lcd_remove),
  357. .driver = {
  358. .owner = THIS_MODULE,
  359. .name = "vt8500-lcd",
  360. },
  361. };
  362. static int __init vt8500lcd_init(void)
  363. {
  364. return platform_driver_register(&vt8500lcd_driver);
  365. }
  366. static void __exit vt8500lcd_exit(void)
  367. {
  368. platform_driver_unregister(&vt8500lcd_driver);
  369. }
  370. module_init(vt8500lcd_init);
  371. module_exit(vt8500lcd_exit);
  372. MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
  373. MODULE_DESCRIPTION("LCD controller driver for VIA VT8500");
  374. MODULE_LICENSE("GPL");