wm8505fb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * WonderMedia WM8505 Frame Buffer device driver
  3. *
  4. * Copyright (C) 2010 Ed Spiridonov <edo.rus@gmail.com>
  5. * Based on vt8500lcdfb.c
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/slab.h>
  22. #include <linux/delay.h>
  23. #include <linux/fb.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/wait.h>
  30. #include <linux/of.h>
  31. #include <linux/of_fdt.h>
  32. #include <linux/memblock.h>
  33. #include <linux/platform_data/video-vt8500lcdfb.h>
  34. #include "wm8505fb_regs.h"
  35. #include "wmt_ge_rops.h"
  36. #define DRIVER_NAME "wm8505-fb"
  37. #define to_wm8505fb_info(__info) container_of(__info, \
  38. struct wm8505fb_info, fb)
  39. struct wm8505fb_info {
  40. struct fb_info fb;
  41. void __iomem *regbase;
  42. unsigned int contrast;
  43. };
  44. static int wm8505fb_init_hw(struct fb_info *info)
  45. {
  46. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  47. int i;
  48. /* I know the purpose only of few registers, so clear unknown */
  49. for (i = 0; i < 0x200; i += 4)
  50. writel(0, fbi->regbase + i);
  51. /* Set frame buffer address */
  52. writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR);
  53. writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR1);
  54. /*
  55. * Set in-memory picture format to RGB
  56. * 0x31C sets the correct color mode (RGB565) for WM8650
  57. * Bit 8+9 (0x300) are ignored on WM8505 as reserved
  58. */
  59. writel(0x31c, fbi->regbase + WMT_GOVR_COLORSPACE);
  60. writel(1, fbi->regbase + WMT_GOVR_COLORSPACE1);
  61. /* Virtual buffer size */
  62. writel(info->var.xres, fbi->regbase + WMT_GOVR_XRES);
  63. writel(info->var.xres_virtual, fbi->regbase + WMT_GOVR_XRES_VIRTUAL);
  64. /* black magic ;) */
  65. writel(0xf, fbi->regbase + WMT_GOVR_FHI);
  66. writel(4, fbi->regbase + WMT_GOVR_DVO_SET);
  67. writel(1, fbi->regbase + WMT_GOVR_MIF_ENABLE);
  68. writel(1, fbi->regbase + WMT_GOVR_REG_UPDATE);
  69. return 0;
  70. }
  71. static int wm8505fb_set_timing(struct fb_info *info)
  72. {
  73. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  74. int h_start = info->var.left_margin;
  75. int h_end = h_start + info->var.xres;
  76. int h_all = h_end + info->var.right_margin;
  77. int h_sync = info->var.hsync_len;
  78. int v_start = info->var.upper_margin;
  79. int v_end = v_start + info->var.yres;
  80. int v_all = v_end + info->var.lower_margin;
  81. int v_sync = info->var.vsync_len;
  82. writel(0, fbi->regbase + WMT_GOVR_TG);
  83. writel(h_start, fbi->regbase + WMT_GOVR_TIMING_H_START);
  84. writel(h_end, fbi->regbase + WMT_GOVR_TIMING_H_END);
  85. writel(h_all, fbi->regbase + WMT_GOVR_TIMING_H_ALL);
  86. writel(h_sync, fbi->regbase + WMT_GOVR_TIMING_H_SYNC);
  87. writel(v_start, fbi->regbase + WMT_GOVR_TIMING_V_START);
  88. writel(v_end, fbi->regbase + WMT_GOVR_TIMING_V_END);
  89. writel(v_all, fbi->regbase + WMT_GOVR_TIMING_V_ALL);
  90. writel(v_sync, fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
  91. writel(1, fbi->regbase + WMT_GOVR_TG);
  92. return 0;
  93. }
  94. static int wm8505fb_set_par(struct fb_info *info)
  95. {
  96. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  97. if (!fbi)
  98. return -EINVAL;
  99. if (info->var.bits_per_pixel == 32) {
  100. info->var.red.offset = 16;
  101. info->var.red.length = 8;
  102. info->var.red.msb_right = 0;
  103. info->var.green.offset = 8;
  104. info->var.green.length = 8;
  105. info->var.green.msb_right = 0;
  106. info->var.blue.offset = 0;
  107. info->var.blue.length = 8;
  108. info->var.blue.msb_right = 0;
  109. info->fix.visual = FB_VISUAL_TRUECOLOR;
  110. info->fix.line_length = info->var.xres_virtual << 2;
  111. } else if (info->var.bits_per_pixel == 16) {
  112. info->var.red.offset = 11;
  113. info->var.red.length = 5;
  114. info->var.red.msb_right = 0;
  115. info->var.green.offset = 5;
  116. info->var.green.length = 6;
  117. info->var.green.msb_right = 0;
  118. info->var.blue.offset = 0;
  119. info->var.blue.length = 5;
  120. info->var.blue.msb_right = 0;
  121. info->fix.visual = FB_VISUAL_TRUECOLOR;
  122. info->fix.line_length = info->var.xres_virtual << 1;
  123. }
  124. wm8505fb_set_timing(info);
  125. writel(fbi->contrast<<16 | fbi->contrast<<8 | fbi->contrast,
  126. fbi->regbase + WMT_GOVR_CONTRAST);
  127. return 0;
  128. }
  129. static ssize_t contrast_show(struct device *dev,
  130. struct device_attribute *attr, char *buf)
  131. {
  132. struct fb_info *info = dev_get_drvdata(dev);
  133. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  134. return sprintf(buf, "%d\n", fbi->contrast);
  135. }
  136. static ssize_t contrast_store(struct device *dev,
  137. struct device_attribute *attr,
  138. const char *buf, size_t count)
  139. {
  140. struct fb_info *info = dev_get_drvdata(dev);
  141. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  142. unsigned long tmp;
  143. if (strict_strtoul(buf, 10, &tmp) || (tmp > 0xff))
  144. return -EINVAL;
  145. fbi->contrast = tmp;
  146. wm8505fb_set_par(info);
  147. return count;
  148. }
  149. static DEVICE_ATTR(contrast, 0644, contrast_show, contrast_store);
  150. static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
  151. {
  152. chan &= 0xffff;
  153. chan >>= 16 - bf->length;
  154. return chan << bf->offset;
  155. }
  156. static int wm8505fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  157. unsigned blue, unsigned transp,
  158. struct fb_info *info) {
  159. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  160. int ret = 1;
  161. unsigned int val;
  162. if (regno >= 256)
  163. return -EINVAL;
  164. if (info->var.grayscale)
  165. red = green = blue =
  166. (19595 * red + 38470 * green + 7471 * blue) >> 16;
  167. switch (fbi->fb.fix.visual) {
  168. case FB_VISUAL_TRUECOLOR:
  169. if (regno < 16) {
  170. u32 *pal = info->pseudo_palette;
  171. val = chan_to_field(red, &fbi->fb.var.red);
  172. val |= chan_to_field(green, &fbi->fb.var.green);
  173. val |= chan_to_field(blue, &fbi->fb.var.blue);
  174. pal[regno] = val;
  175. ret = 0;
  176. }
  177. break;
  178. }
  179. return ret;
  180. }
  181. static int wm8505fb_pan_display(struct fb_var_screeninfo *var,
  182. struct fb_info *info)
  183. {
  184. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  185. writel(var->xoffset, fbi->regbase + WMT_GOVR_XPAN);
  186. writel(var->yoffset, fbi->regbase + WMT_GOVR_YPAN);
  187. return 0;
  188. }
  189. static int wm8505fb_blank(int blank, struct fb_info *info)
  190. {
  191. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  192. switch (blank) {
  193. case FB_BLANK_UNBLANK:
  194. wm8505fb_set_timing(info);
  195. break;
  196. default:
  197. writel(0, fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
  198. break;
  199. }
  200. return 0;
  201. }
  202. static struct fb_ops wm8505fb_ops = {
  203. .owner = THIS_MODULE,
  204. .fb_set_par = wm8505fb_set_par,
  205. .fb_setcolreg = wm8505fb_setcolreg,
  206. .fb_fillrect = wmt_ge_fillrect,
  207. .fb_copyarea = wmt_ge_copyarea,
  208. .fb_imageblit = sys_imageblit,
  209. .fb_sync = wmt_ge_sync,
  210. .fb_pan_display = wm8505fb_pan_display,
  211. .fb_blank = wm8505fb_blank,
  212. };
  213. static int wm8505fb_probe(struct platform_device *pdev)
  214. {
  215. struct wm8505fb_info *fbi;
  216. struct resource *res;
  217. void *addr;
  218. int ret;
  219. struct fb_videomode of_mode;
  220. struct device_node *np;
  221. u32 bpp;
  222. dma_addr_t fb_mem_phys;
  223. unsigned long fb_mem_len;
  224. void *fb_mem_virt;
  225. ret = -ENOMEM;
  226. fbi = NULL;
  227. fbi = devm_kzalloc(&pdev->dev, sizeof(struct wm8505fb_info) +
  228. sizeof(u32) * 16, GFP_KERNEL);
  229. if (!fbi) {
  230. dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
  231. ret = -ENOMEM;
  232. goto failed;
  233. }
  234. strcpy(fbi->fb.fix.id, DRIVER_NAME);
  235. fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  236. fbi->fb.fix.xpanstep = 1;
  237. fbi->fb.fix.ypanstep = 1;
  238. fbi->fb.fix.ywrapstep = 0;
  239. fbi->fb.fix.accel = FB_ACCEL_NONE;
  240. fbi->fb.fbops = &wm8505fb_ops;
  241. fbi->fb.flags = FBINFO_DEFAULT
  242. | FBINFO_HWACCEL_COPYAREA
  243. | FBINFO_HWACCEL_FILLRECT
  244. | FBINFO_HWACCEL_XPAN
  245. | FBINFO_HWACCEL_YPAN
  246. | FBINFO_VIRTFB
  247. | FBINFO_PARTIAL_PAN_OK;
  248. fbi->fb.node = -1;
  249. addr = fbi;
  250. addr = addr + sizeof(struct wm8505fb_info);
  251. fbi->fb.pseudo_palette = addr;
  252. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  253. if (res == NULL) {
  254. dev_err(&pdev->dev, "no I/O memory resource defined\n");
  255. ret = -ENODEV;
  256. goto failed_fbi;
  257. }
  258. res = request_mem_region(res->start, resource_size(res), DRIVER_NAME);
  259. if (res == NULL) {
  260. dev_err(&pdev->dev, "failed to request I/O memory\n");
  261. ret = -EBUSY;
  262. goto failed_fbi;
  263. }
  264. fbi->regbase = ioremap(res->start, resource_size(res));
  265. if (fbi->regbase == NULL) {
  266. dev_err(&pdev->dev, "failed to map I/O memory\n");
  267. ret = -EBUSY;
  268. goto failed_free_res;
  269. }
  270. np = of_parse_phandle(pdev->dev.of_node, "default-mode", 0);
  271. if (!np) {
  272. pr_err("%s: No display description in Device Tree\n", __func__);
  273. ret = -EINVAL;
  274. goto failed_free_res;
  275. }
  276. /*
  277. * This code is copied from Sascha Hauer's of_videomode helper
  278. * and can be replaced with a call to the helper once mainlined
  279. */
  280. ret = 0;
  281. ret |= of_property_read_u32(np, "hactive", &of_mode.xres);
  282. ret |= of_property_read_u32(np, "vactive", &of_mode.yres);
  283. ret |= of_property_read_u32(np, "hback-porch", &of_mode.left_margin);
  284. ret |= of_property_read_u32(np, "hfront-porch", &of_mode.right_margin);
  285. ret |= of_property_read_u32(np, "hsync-len", &of_mode.hsync_len);
  286. ret |= of_property_read_u32(np, "vback-porch", &of_mode.upper_margin);
  287. ret |= of_property_read_u32(np, "vfront-porch", &of_mode.lower_margin);
  288. ret |= of_property_read_u32(np, "vsync-len", &of_mode.vsync_len);
  289. ret |= of_property_read_u32(np, "bpp", &bpp);
  290. if (ret) {
  291. pr_err("%s: Unable to read display properties\n", __func__);
  292. goto failed_free_res;
  293. }
  294. of_mode.vmode = FB_VMODE_NONINTERLACED;
  295. fb_videomode_to_var(&fbi->fb.var, &of_mode);
  296. fbi->fb.var.nonstd = 0;
  297. fbi->fb.var.activate = FB_ACTIVATE_NOW;
  298. fbi->fb.var.height = -1;
  299. fbi->fb.var.width = -1;
  300. /* try allocating the framebuffer */
  301. fb_mem_len = of_mode.xres * of_mode.yres * 2 * (bpp / 8);
  302. fb_mem_virt = dma_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys,
  303. GFP_KERNEL);
  304. if (!fb_mem_virt) {
  305. pr_err("%s: Failed to allocate framebuffer\n", __func__);
  306. return -ENOMEM;
  307. };
  308. fbi->fb.var.xres_virtual = of_mode.xres;
  309. fbi->fb.var.yres_virtual = of_mode.yres * 2;
  310. fbi->fb.var.bits_per_pixel = bpp;
  311. fbi->fb.fix.smem_start = fb_mem_phys;
  312. fbi->fb.fix.smem_len = fb_mem_len;
  313. fbi->fb.screen_base = fb_mem_virt;
  314. fbi->fb.screen_size = fb_mem_len;
  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_io;
  319. }
  320. wm8505fb_init_hw(&fbi->fb);
  321. fbi->contrast = 0x80;
  322. ret = wm8505fb_set_par(&fbi->fb);
  323. if (ret) {
  324. dev_err(&pdev->dev, "Failed to set parameters\n");
  325. goto failed_free_cmap;
  326. }
  327. platform_set_drvdata(pdev, fbi);
  328. ret = register_framebuffer(&fbi->fb);
  329. if (ret < 0) {
  330. dev_err(&pdev->dev,
  331. "Failed to register framebuffer device: %d\n", ret);
  332. goto failed_free_cmap;
  333. }
  334. ret = device_create_file(&pdev->dev, &dev_attr_contrast);
  335. if (ret < 0) {
  336. printk(KERN_WARNING "fb%d: failed to register attributes (%d)\n",
  337. fbi->fb.node, ret);
  338. }
  339. printk(KERN_INFO "fb%d: %s frame buffer at 0x%lx-0x%lx\n",
  340. fbi->fb.node, fbi->fb.fix.id, fbi->fb.fix.smem_start,
  341. fbi->fb.fix.smem_start + fbi->fb.fix.smem_len - 1);
  342. return 0;
  343. failed_free_cmap:
  344. if (fbi->fb.cmap.len)
  345. fb_dealloc_cmap(&fbi->fb.cmap);
  346. failed_free_io:
  347. iounmap(fbi->regbase);
  348. failed_free_res:
  349. release_mem_region(res->start, resource_size(res));
  350. failed_fbi:
  351. platform_set_drvdata(pdev, NULL);
  352. kfree(fbi);
  353. failed:
  354. return ret;
  355. }
  356. static int wm8505fb_remove(struct platform_device *pdev)
  357. {
  358. struct wm8505fb_info *fbi = platform_get_drvdata(pdev);
  359. struct resource *res;
  360. device_remove_file(&pdev->dev, &dev_attr_contrast);
  361. unregister_framebuffer(&fbi->fb);
  362. writel(0, fbi->regbase);
  363. if (fbi->fb.cmap.len)
  364. fb_dealloc_cmap(&fbi->fb.cmap);
  365. iounmap(fbi->regbase);
  366. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  367. release_mem_region(res->start, resource_size(res));
  368. kfree(fbi);
  369. return 0;
  370. }
  371. static const struct of_device_id wmt_dt_ids[] = {
  372. { .compatible = "wm,wm8505-fb", },
  373. {}
  374. };
  375. static struct platform_driver wm8505fb_driver = {
  376. .probe = wm8505fb_probe,
  377. .remove = wm8505fb_remove,
  378. .driver = {
  379. .owner = THIS_MODULE,
  380. .name = DRIVER_NAME,
  381. .of_match_table = of_match_ptr(wmt_dt_ids),
  382. },
  383. };
  384. module_platform_driver(wm8505fb_driver);
  385. MODULE_AUTHOR("Ed Spiridonov <edo.rus@gmail.com>");
  386. MODULE_DESCRIPTION("Framebuffer driver for WMT WM8505");
  387. MODULE_LICENSE("GPL v2");
  388. MODULE_DEVICE_TABLE(of, wmt_dt_ids);