wm8505fb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 <mach/vt8500fb.h>
  31. #include "wm8505fb_regs.h"
  32. #include "wmt_ge_rops.h"
  33. #define DRIVER_NAME "wm8505-fb"
  34. #define to_wm8505fb_info(__info) container_of(__info, \
  35. struct wm8505fb_info, fb)
  36. struct wm8505fb_info {
  37. struct fb_info fb;
  38. void __iomem *regbase;
  39. unsigned int contrast;
  40. };
  41. static int wm8505fb_init_hw(struct fb_info *info)
  42. {
  43. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  44. int i;
  45. /* I know the purpose only of few registers, so clear unknown */
  46. for (i = 0; i < 0x200; i += 4)
  47. writel(0, fbi->regbase + i);
  48. /* Set frame buffer address */
  49. writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR);
  50. writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR1);
  51. /* Set in-memory picture format to RGB 32bpp */
  52. writel(0x1c, fbi->regbase + WMT_GOVR_COLORSPACE);
  53. writel(1, fbi->regbase + WMT_GOVR_COLORSPACE1);
  54. /* Virtual buffer size */
  55. writel(info->var.xres, fbi->regbase + WMT_GOVR_XRES);
  56. writel(info->var.xres_virtual, fbi->regbase + WMT_GOVR_XRES_VIRTUAL);
  57. /* black magic ;) */
  58. writel(0xf, fbi->regbase + WMT_GOVR_FHI);
  59. writel(4, fbi->regbase + WMT_GOVR_DVO_SET);
  60. writel(1, fbi->regbase + WMT_GOVR_MIF_ENABLE);
  61. writel(1, fbi->regbase + WMT_GOVR_REG_UPDATE);
  62. return 0;
  63. }
  64. static int wm8505fb_set_timing(struct fb_info *info)
  65. {
  66. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  67. int h_start = info->var.left_margin;
  68. int h_end = h_start + info->var.xres;
  69. int h_all = h_end + info->var.right_margin;
  70. int h_sync = info->var.hsync_len;
  71. int v_start = info->var.upper_margin;
  72. int v_end = v_start + info->var.yres;
  73. int v_all = v_end + info->var.lower_margin;
  74. int v_sync = info->var.vsync_len;
  75. writel(0, fbi->regbase + WMT_GOVR_TG);
  76. writel(h_start, fbi->regbase + WMT_GOVR_TIMING_H_START);
  77. writel(h_end, fbi->regbase + WMT_GOVR_TIMING_H_END);
  78. writel(h_all, fbi->regbase + WMT_GOVR_TIMING_H_ALL);
  79. writel(h_sync, fbi->regbase + WMT_GOVR_TIMING_H_SYNC);
  80. writel(v_start, fbi->regbase + WMT_GOVR_TIMING_V_START);
  81. writel(v_end, fbi->regbase + WMT_GOVR_TIMING_V_END);
  82. writel(v_all, fbi->regbase + WMT_GOVR_TIMING_V_ALL);
  83. writel(v_sync, fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
  84. writel(1, fbi->regbase + WMT_GOVR_TG);
  85. return 0;
  86. }
  87. static int wm8505fb_set_par(struct fb_info *info)
  88. {
  89. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  90. if (!fbi)
  91. return -EINVAL;
  92. if (info->var.bits_per_pixel == 32) {
  93. info->var.red.offset = 16;
  94. info->var.red.length = 8;
  95. info->var.red.msb_right = 0;
  96. info->var.green.offset = 8;
  97. info->var.green.length = 8;
  98. info->var.green.msb_right = 0;
  99. info->var.blue.offset = 0;
  100. info->var.blue.length = 8;
  101. info->var.blue.msb_right = 0;
  102. info->fix.visual = FB_VISUAL_TRUECOLOR;
  103. info->fix.line_length = info->var.xres_virtual << 2;
  104. }
  105. wm8505fb_set_timing(info);
  106. writel(fbi->contrast<<16 | fbi->contrast<<8 | fbi->contrast,
  107. fbi->regbase + WMT_GOVR_CONTRAST);
  108. return 0;
  109. }
  110. static ssize_t contrast_show(struct device *dev,
  111. struct device_attribute *attr, char *buf)
  112. {
  113. struct fb_info *info = dev_get_drvdata(dev);
  114. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  115. return sprintf(buf, "%d\n", fbi->contrast);
  116. }
  117. static ssize_t contrast_store(struct device *dev,
  118. struct device_attribute *attr,
  119. const char *buf, size_t count)
  120. {
  121. struct fb_info *info = dev_get_drvdata(dev);
  122. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  123. unsigned long tmp;
  124. if (strict_strtoul(buf, 10, &tmp) || (tmp > 0xff))
  125. return -EINVAL;
  126. fbi->contrast = tmp;
  127. wm8505fb_set_par(info);
  128. return count;
  129. }
  130. static DEVICE_ATTR(contrast, 0644, contrast_show, contrast_store);
  131. static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
  132. {
  133. chan &= 0xffff;
  134. chan >>= 16 - bf->length;
  135. return chan << bf->offset;
  136. }
  137. static int wm8505fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  138. unsigned blue, unsigned transp,
  139. struct fb_info *info) {
  140. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  141. int ret = 1;
  142. unsigned int val;
  143. if (regno >= 256)
  144. return -EINVAL;
  145. if (info->var.grayscale)
  146. red = green = blue =
  147. (19595 * red + 38470 * green + 7471 * blue) >> 16;
  148. switch (fbi->fb.fix.visual) {
  149. case FB_VISUAL_TRUECOLOR:
  150. if (regno < 16) {
  151. u32 *pal = info->pseudo_palette;
  152. val = chan_to_field(red, &fbi->fb.var.red);
  153. val |= chan_to_field(green, &fbi->fb.var.green);
  154. val |= chan_to_field(blue, &fbi->fb.var.blue);
  155. pal[regno] = val;
  156. ret = 0;
  157. }
  158. break;
  159. }
  160. return ret;
  161. }
  162. static int wm8505fb_pan_display(struct fb_var_screeninfo *var,
  163. struct fb_info *info)
  164. {
  165. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  166. writel(var->xoffset, fbi->regbase + WMT_GOVR_XPAN);
  167. writel(var->yoffset, fbi->regbase + WMT_GOVR_YPAN);
  168. return 0;
  169. }
  170. static int wm8505fb_blank(int blank, struct fb_info *info)
  171. {
  172. struct wm8505fb_info *fbi = to_wm8505fb_info(info);
  173. switch (blank) {
  174. case FB_BLANK_UNBLANK:
  175. wm8505fb_set_timing(info);
  176. break;
  177. default:
  178. writel(0, fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
  179. break;
  180. }
  181. return 0;
  182. }
  183. static struct fb_ops wm8505fb_ops = {
  184. .owner = THIS_MODULE,
  185. .fb_set_par = wm8505fb_set_par,
  186. .fb_setcolreg = wm8505fb_setcolreg,
  187. .fb_fillrect = wmt_ge_fillrect,
  188. .fb_copyarea = wmt_ge_copyarea,
  189. .fb_imageblit = sys_imageblit,
  190. .fb_sync = wmt_ge_sync,
  191. .fb_pan_display = wm8505fb_pan_display,
  192. .fb_blank = wm8505fb_blank,
  193. };
  194. static int __devinit wm8505fb_probe(struct platform_device *pdev)
  195. {
  196. struct wm8505fb_info *fbi;
  197. struct resource *res;
  198. void *addr;
  199. struct vt8500fb_platform_data *pdata;
  200. int ret;
  201. pdata = pdev->dev.platform_data;
  202. ret = -ENOMEM;
  203. fbi = NULL;
  204. fbi = kzalloc(sizeof(struct wm8505fb_info) + sizeof(u32) * 16,
  205. GFP_KERNEL);
  206. if (!fbi) {
  207. dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
  208. ret = -ENOMEM;
  209. goto failed;
  210. }
  211. strcpy(fbi->fb.fix.id, DRIVER_NAME);
  212. fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  213. fbi->fb.fix.xpanstep = 1;
  214. fbi->fb.fix.ypanstep = 1;
  215. fbi->fb.fix.ywrapstep = 0;
  216. fbi->fb.fix.accel = FB_ACCEL_NONE;
  217. fbi->fb.fbops = &wm8505fb_ops;
  218. fbi->fb.flags = FBINFO_DEFAULT
  219. | FBINFO_HWACCEL_COPYAREA
  220. | FBINFO_HWACCEL_FILLRECT
  221. | FBINFO_HWACCEL_XPAN
  222. | FBINFO_HWACCEL_YPAN
  223. | FBINFO_VIRTFB
  224. | FBINFO_PARTIAL_PAN_OK;
  225. fbi->fb.node = -1;
  226. addr = fbi;
  227. addr = addr + sizeof(struct wm8505fb_info);
  228. fbi->fb.pseudo_palette = addr;
  229. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  230. if (res == NULL) {
  231. dev_err(&pdev->dev, "no I/O memory resource defined\n");
  232. ret = -ENODEV;
  233. goto failed_fbi;
  234. }
  235. res = request_mem_region(res->start, resource_size(res), DRIVER_NAME);
  236. if (res == NULL) {
  237. dev_err(&pdev->dev, "failed to request I/O memory\n");
  238. ret = -EBUSY;
  239. goto failed_fbi;
  240. }
  241. fbi->regbase = ioremap(res->start, resource_size(res));
  242. if (fbi->regbase == NULL) {
  243. dev_err(&pdev->dev, "failed to map I/O memory\n");
  244. ret = -EBUSY;
  245. goto failed_free_res;
  246. }
  247. fb_videomode_to_var(&fbi->fb.var, &pdata->mode);
  248. fbi->fb.var.nonstd = 0;
  249. fbi->fb.var.activate = FB_ACTIVATE_NOW;
  250. fbi->fb.var.height = -1;
  251. fbi->fb.var.width = -1;
  252. fbi->fb.var.xres_virtual = pdata->xres_virtual;
  253. fbi->fb.var.yres_virtual = pdata->yres_virtual;
  254. fbi->fb.var.bits_per_pixel = pdata->bpp;
  255. fbi->fb.fix.smem_start = pdata->video_mem_phys;
  256. fbi->fb.fix.smem_len = pdata->video_mem_len;
  257. fbi->fb.screen_base = pdata->video_mem_virt;
  258. fbi->fb.screen_size = pdata->video_mem_len;
  259. if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
  260. dev_err(&pdev->dev, "Failed to allocate color map\n");
  261. ret = -ENOMEM;
  262. goto failed_free_io;
  263. }
  264. wm8505fb_init_hw(&fbi->fb);
  265. fbi->contrast = 0x80;
  266. ret = wm8505fb_set_par(&fbi->fb);
  267. if (ret) {
  268. dev_err(&pdev->dev, "Failed to set parameters\n");
  269. goto failed_free_cmap;
  270. }
  271. platform_set_drvdata(pdev, fbi);
  272. ret = register_framebuffer(&fbi->fb);
  273. if (ret < 0) {
  274. dev_err(&pdev->dev,
  275. "Failed to register framebuffer device: %d\n", ret);
  276. goto failed_free_cmap;
  277. }
  278. ret = device_create_file(&pdev->dev, &dev_attr_contrast);
  279. if (ret < 0) {
  280. printk(KERN_WARNING "fb%d: failed to register attributes (%d)\n",
  281. fbi->fb.node, ret);
  282. }
  283. printk(KERN_INFO "fb%d: %s frame buffer at 0x%lx-0x%lx\n",
  284. fbi->fb.node, fbi->fb.fix.id, fbi->fb.fix.smem_start,
  285. fbi->fb.fix.smem_start + fbi->fb.fix.smem_len - 1);
  286. return 0;
  287. failed_free_cmap:
  288. if (fbi->fb.cmap.len)
  289. fb_dealloc_cmap(&fbi->fb.cmap);
  290. failed_free_io:
  291. iounmap(fbi->regbase);
  292. failed_free_res:
  293. release_mem_region(res->start, resource_size(res));
  294. failed_fbi:
  295. platform_set_drvdata(pdev, NULL);
  296. kfree(fbi);
  297. failed:
  298. return ret;
  299. }
  300. static int __devexit wm8505fb_remove(struct platform_device *pdev)
  301. {
  302. struct wm8505fb_info *fbi = platform_get_drvdata(pdev);
  303. struct resource *res;
  304. device_remove_file(&pdev->dev, &dev_attr_contrast);
  305. unregister_framebuffer(&fbi->fb);
  306. writel(0, fbi->regbase);
  307. if (fbi->fb.cmap.len)
  308. fb_dealloc_cmap(&fbi->fb.cmap);
  309. iounmap(fbi->regbase);
  310. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  311. release_mem_region(res->start, resource_size(res));
  312. kfree(fbi);
  313. return 0;
  314. }
  315. static struct platform_driver wm8505fb_driver = {
  316. .probe = wm8505fb_probe,
  317. .remove = __devexit_p(wm8505fb_remove),
  318. .driver = {
  319. .owner = THIS_MODULE,
  320. .name = DRIVER_NAME,
  321. },
  322. };
  323. static int __init wm8505fb_init(void)
  324. {
  325. return platform_driver_register(&wm8505fb_driver);
  326. }
  327. static void __exit wm8505fb_exit(void)
  328. {
  329. platform_driver_unregister(&wm8505fb_driver);
  330. }
  331. module_init(wm8505fb_init);
  332. module_exit(wm8505fb_exit);
  333. MODULE_AUTHOR("Ed Spiridonov <edo.rus@gmail.com>");
  334. MODULE_DESCRIPTION("Framebuffer driver for WMT WM8505");
  335. MODULE_LICENSE("GPL");