vt8500lcdfb.c 13 KB

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