wm8505fb.c 10 KB

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