wm8505fb.c 11 KB

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