vt8500lcdfb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 <linux/platform_data/video-vt8500lcdfb.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. continue;
  104. }
  105. }
  106. control0 = readl(fbi->regbase) & ~0xf;
  107. writel(0, fbi->regbase);
  108. while (readl(fbi->regbase + 0x38) & 0x10)
  109. /* wait */;
  110. writel((((info->var.hsync_len - 1) & 0x3f) << 26)
  111. | ((info->var.left_margin & 0xff) << 18)
  112. | (((info->var.xres - 1) & 0x3ff) << 8)
  113. | (info->var.right_margin & 0xff), fbi->regbase + 0x4);
  114. writel((((info->var.vsync_len - 1) & 0x3f) << 26)
  115. | ((info->var.upper_margin & 0xff) << 18)
  116. | (((info->var.yres - 1) & 0x3ff) << 8)
  117. | (info->var.lower_margin & 0xff), fbi->regbase + 0x8);
  118. writel((((info->var.yres - 1) & 0x400) << 2)
  119. | ((info->var.xres - 1) & 0x400), fbi->regbase + 0x10);
  120. writel(0x80000000, fbi->regbase + 0x20);
  121. writel(control0 | (reg_bpp << 1) | 0x100, fbi->regbase);
  122. return 0;
  123. }
  124. static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
  125. {
  126. chan &= 0xffff;
  127. chan >>= 16 - bf->length;
  128. return chan << bf->offset;
  129. }
  130. static int vt8500lcd_setcolreg(unsigned regno, unsigned red, unsigned green,
  131. unsigned blue, unsigned transp,
  132. struct fb_info *info) {
  133. struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
  134. int ret = 1;
  135. unsigned int val;
  136. if (regno >= 256)
  137. return -EINVAL;
  138. if (info->var.grayscale)
  139. red = green = blue =
  140. (19595 * red + 38470 * green + 7471 * blue) >> 16;
  141. switch (fbi->fb.fix.visual) {
  142. case FB_VISUAL_TRUECOLOR:
  143. if (regno < 16) {
  144. u32 *pal = fbi->fb.pseudo_palette;
  145. val = chan_to_field(red, &fbi->fb.var.red);
  146. val |= chan_to_field(green, &fbi->fb.var.green);
  147. val |= chan_to_field(blue, &fbi->fb.var.blue);
  148. pal[regno] = val;
  149. ret = 0;
  150. }
  151. break;
  152. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  153. case FB_VISUAL_PSEUDOCOLOR:
  154. writew((red & 0xf800)
  155. | ((green >> 5) & 0x7e0)
  156. | ((blue >> 11) & 0x1f),
  157. fbi->palette_cpu + sizeof(u16) * regno);
  158. break;
  159. }
  160. return ret;
  161. }
  162. static int vt8500lcd_ioctl(struct fb_info *info, unsigned int cmd,
  163. unsigned long arg)
  164. {
  165. int ret = 0;
  166. struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
  167. if (cmd == FBIO_WAITFORVSYNC) {
  168. /* Unmask End of Frame interrupt */
  169. writel(0xffffffff ^ (1 << 3), fbi->regbase + 0x3c);
  170. ret = wait_event_interruptible_timeout(fbi->wait,
  171. readl(fbi->regbase + 0x38) & (1 << 3), HZ / 10);
  172. /* Mask back to reduce unwanted interrupt traffic */
  173. writel(0xffffffff, fbi->regbase + 0x3c);
  174. if (ret < 0)
  175. return ret;
  176. if (ret == 0)
  177. return -ETIMEDOUT;
  178. }
  179. return ret;
  180. }
  181. static int vt8500lcd_pan_display(struct fb_var_screeninfo *var,
  182. struct fb_info *info)
  183. {
  184. unsigned pixlen = info->fix.line_length / info->var.xres_virtual;
  185. unsigned off = pixlen * var->xoffset
  186. + info->fix.line_length * var->yoffset;
  187. struct vt8500lcd_info *fbi = to_vt8500lcd_info(info);
  188. writel((1 << 31)
  189. | (((info->var.xres_virtual - info->var.xres) * pixlen / 4) << 20)
  190. | (off >> 2), fbi->regbase + 0x20);
  191. return 0;
  192. }
  193. /*
  194. * vt8500lcd_blank():
  195. * Blank the display by setting all palette values to zero. Note,
  196. * True Color modes do not really use the palette, so this will not
  197. * blank the display in all modes.
  198. */
  199. static int vt8500lcd_blank(int blank, struct fb_info *info)
  200. {
  201. int i;
  202. switch (blank) {
  203. case FB_BLANK_POWERDOWN:
  204. case FB_BLANK_VSYNC_SUSPEND:
  205. case FB_BLANK_HSYNC_SUSPEND:
  206. case FB_BLANK_NORMAL:
  207. if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR ||
  208. info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
  209. for (i = 0; i < 256; i++)
  210. vt8500lcd_setcolreg(i, 0, 0, 0, 0, info);
  211. case FB_BLANK_UNBLANK:
  212. if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR ||
  213. info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
  214. fb_set_cmap(&info->cmap, info);
  215. }
  216. return 0;
  217. }
  218. static struct fb_ops vt8500lcd_ops = {
  219. .owner = THIS_MODULE,
  220. .fb_set_par = vt8500lcd_set_par,
  221. .fb_setcolreg = vt8500lcd_setcolreg,
  222. .fb_fillrect = wmt_ge_fillrect,
  223. .fb_copyarea = wmt_ge_copyarea,
  224. .fb_imageblit = sys_imageblit,
  225. .fb_sync = wmt_ge_sync,
  226. .fb_ioctl = vt8500lcd_ioctl,
  227. .fb_pan_display = vt8500lcd_pan_display,
  228. .fb_blank = vt8500lcd_blank,
  229. };
  230. static irqreturn_t vt8500lcd_handle_irq(int irq, void *dev_id)
  231. {
  232. struct vt8500lcd_info *fbi = dev_id;
  233. if (readl(fbi->regbase + 0x38) & (1 << 3))
  234. wake_up_interruptible(&fbi->wait);
  235. writel(0xffffffff, fbi->regbase + 0x38);
  236. return IRQ_HANDLED;
  237. }
  238. static int vt8500lcd_probe(struct platform_device *pdev)
  239. {
  240. struct vt8500lcd_info *fbi;
  241. struct resource *res;
  242. void *addr;
  243. int irq, ret;
  244. struct fb_videomode of_mode;
  245. struct device_node *np;
  246. u32 bpp;
  247. dma_addr_t fb_mem_phys;
  248. unsigned long fb_mem_len;
  249. void *fb_mem_virt;
  250. ret = -ENOMEM;
  251. fbi = NULL;
  252. fbi = devm_kzalloc(&pdev->dev, sizeof(struct vt8500lcd_info)
  253. + sizeof(u32) * 16, GFP_KERNEL);
  254. if (!fbi) {
  255. dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
  256. ret = -ENOMEM;
  257. goto failed;
  258. }
  259. strcpy(fbi->fb.fix.id, "VT8500 LCD");
  260. fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  261. fbi->fb.fix.xpanstep = 0;
  262. fbi->fb.fix.ypanstep = 1;
  263. fbi->fb.fix.ywrapstep = 0;
  264. fbi->fb.fix.accel = FB_ACCEL_NONE;
  265. fbi->fb.var.nonstd = 0;
  266. fbi->fb.var.activate = FB_ACTIVATE_NOW;
  267. fbi->fb.var.height = -1;
  268. fbi->fb.var.width = -1;
  269. fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
  270. fbi->fb.fbops = &vt8500lcd_ops;
  271. fbi->fb.flags = FBINFO_DEFAULT
  272. | FBINFO_HWACCEL_COPYAREA
  273. | FBINFO_HWACCEL_FILLRECT
  274. | FBINFO_HWACCEL_YPAN
  275. | FBINFO_VIRTFB
  276. | FBINFO_PARTIAL_PAN_OK;
  277. fbi->fb.node = -1;
  278. addr = fbi;
  279. addr = addr + sizeof(struct vt8500lcd_info);
  280. fbi->fb.pseudo_palette = addr;
  281. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  282. if (res == NULL) {
  283. dev_err(&pdev->dev, "no I/O memory resource defined\n");
  284. ret = -ENODEV;
  285. goto failed_fbi;
  286. }
  287. res = request_mem_region(res->start, resource_size(res), "vt8500lcd");
  288. if (res == NULL) {
  289. dev_err(&pdev->dev, "failed to request I/O memory\n");
  290. ret = -EBUSY;
  291. goto failed_fbi;
  292. }
  293. fbi->regbase = ioremap(res->start, resource_size(res));
  294. if (fbi->regbase == NULL) {
  295. dev_err(&pdev->dev, "failed to map I/O memory\n");
  296. ret = -EBUSY;
  297. goto failed_free_res;
  298. }
  299. np = of_parse_phandle(pdev->dev.of_node, "default-mode", 0);
  300. if (!np) {
  301. pr_err("%s: No display description in Device Tree\n", __func__);
  302. ret = -EINVAL;
  303. goto failed_free_res;
  304. }
  305. /*
  306. * This code is copied from Sascha Hauer's of_videomode helper
  307. * and can be replaced with a call to the helper once mainlined
  308. */
  309. ret = 0;
  310. ret |= of_property_read_u32(np, "hactive", &of_mode.xres);
  311. ret |= of_property_read_u32(np, "vactive", &of_mode.yres);
  312. ret |= of_property_read_u32(np, "hback-porch", &of_mode.left_margin);
  313. ret |= of_property_read_u32(np, "hfront-porch", &of_mode.right_margin);
  314. ret |= of_property_read_u32(np, "hsync-len", &of_mode.hsync_len);
  315. ret |= of_property_read_u32(np, "vback-porch", &of_mode.upper_margin);
  316. ret |= of_property_read_u32(np, "vfront-porch", &of_mode.lower_margin);
  317. ret |= of_property_read_u32(np, "vsync-len", &of_mode.vsync_len);
  318. ret |= of_property_read_u32(np, "bpp", &bpp);
  319. if (ret) {
  320. pr_err("%s: Unable to read display properties\n", __func__);
  321. goto failed_free_res;
  322. }
  323. of_mode.vmode = FB_VMODE_NONINTERLACED;
  324. /* try allocating the framebuffer */
  325. fb_mem_len = of_mode.xres * of_mode.yres * 2 * (bpp / 8);
  326. fb_mem_virt = dma_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys,
  327. GFP_KERNEL);
  328. if (!fb_mem_virt) {
  329. pr_err("%s: Failed to allocate framebuffer\n", __func__);
  330. return -ENOMEM;
  331. };
  332. fbi->fb.fix.smem_start = fb_mem_phys;
  333. fbi->fb.fix.smem_len = fb_mem_len;
  334. fbi->fb.screen_base = fb_mem_virt;
  335. fbi->palette_size = PAGE_ALIGN(512);
  336. fbi->palette_cpu = dma_alloc_coherent(&pdev->dev,
  337. fbi->palette_size,
  338. &fbi->palette_phys,
  339. GFP_KERNEL);
  340. if (fbi->palette_cpu == NULL) {
  341. dev_err(&pdev->dev, "Failed to allocate palette buffer\n");
  342. ret = -ENOMEM;
  343. goto failed_free_io;
  344. }
  345. irq = platform_get_irq(pdev, 0);
  346. if (irq < 0) {
  347. dev_err(&pdev->dev, "no IRQ defined\n");
  348. ret = -ENODEV;
  349. goto failed_free_palette;
  350. }
  351. ret = request_irq(irq, vt8500lcd_handle_irq, 0, "LCD", fbi);
  352. if (ret) {
  353. dev_err(&pdev->dev, "request_irq failed: %d\n", ret);
  354. ret = -EBUSY;
  355. goto failed_free_palette;
  356. }
  357. init_waitqueue_head(&fbi->wait);
  358. if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
  359. dev_err(&pdev->dev, "Failed to allocate color map\n");
  360. ret = -ENOMEM;
  361. goto failed_free_irq;
  362. }
  363. fb_videomode_to_var(&fbi->fb.var, &of_mode);
  364. fbi->fb.var.xres_virtual = of_mode.xres;
  365. fbi->fb.var.yres_virtual = of_mode.yres * 2;
  366. fbi->fb.var.bits_per_pixel = bpp;
  367. ret = vt8500lcd_set_par(&fbi->fb);
  368. if (ret) {
  369. dev_err(&pdev->dev, "Failed to set parameters\n");
  370. goto failed_free_cmap;
  371. }
  372. writel(fbi->fb.fix.smem_start >> 22, fbi->regbase + 0x1c);
  373. writel((fbi->palette_phys & 0xfffffe00) | 1, fbi->regbase + 0x18);
  374. platform_set_drvdata(pdev, fbi);
  375. ret = register_framebuffer(&fbi->fb);
  376. if (ret < 0) {
  377. dev_err(&pdev->dev,
  378. "Failed to register framebuffer device: %d\n", ret);
  379. goto failed_free_cmap;
  380. }
  381. /*
  382. * Ok, now enable the LCD controller
  383. */
  384. writel(readl(fbi->regbase) | 1, fbi->regbase);
  385. return 0;
  386. failed_free_cmap:
  387. if (fbi->fb.cmap.len)
  388. fb_dealloc_cmap(&fbi->fb.cmap);
  389. failed_free_irq:
  390. free_irq(irq, fbi);
  391. failed_free_palette:
  392. dma_free_coherent(&pdev->dev, fbi->palette_size,
  393. fbi->palette_cpu, fbi->palette_phys);
  394. failed_free_io:
  395. iounmap(fbi->regbase);
  396. failed_free_res:
  397. release_mem_region(res->start, resource_size(res));
  398. failed_fbi:
  399. platform_set_drvdata(pdev, NULL);
  400. kfree(fbi);
  401. failed:
  402. return ret;
  403. }
  404. static int vt8500lcd_remove(struct platform_device *pdev)
  405. {
  406. struct vt8500lcd_info *fbi = platform_get_drvdata(pdev);
  407. struct resource *res;
  408. int irq;
  409. unregister_framebuffer(&fbi->fb);
  410. writel(0, fbi->regbase);
  411. if (fbi->fb.cmap.len)
  412. fb_dealloc_cmap(&fbi->fb.cmap);
  413. irq = platform_get_irq(pdev, 0);
  414. free_irq(irq, fbi);
  415. dma_free_coherent(&pdev->dev, fbi->palette_size,
  416. fbi->palette_cpu, fbi->palette_phys);
  417. iounmap(fbi->regbase);
  418. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  419. release_mem_region(res->start, resource_size(res));
  420. kfree(fbi);
  421. return 0;
  422. }
  423. static const struct of_device_id via_dt_ids[] = {
  424. { .compatible = "via,vt8500-fb", },
  425. {}
  426. };
  427. static struct platform_driver vt8500lcd_driver = {
  428. .probe = vt8500lcd_probe,
  429. .remove = vt8500lcd_remove,
  430. .driver = {
  431. .owner = THIS_MODULE,
  432. .name = "vt8500-lcd",
  433. .of_match_table = of_match_ptr(via_dt_ids),
  434. },
  435. };
  436. module_platform_driver(vt8500lcd_driver);
  437. MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
  438. MODULE_DESCRIPTION("LCD controller driver for VIA VT8500");
  439. MODULE_LICENSE("GPL v2");
  440. MODULE_DEVICE_TABLE(of, via_dt_ids);