wm8505fb.c 11 KB

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