goldfishfb.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007 Google, Inc.
  4. * Copyright (C) 2012 Intel, Inc.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/errno.h>
  10. #include <linux/string.h>
  11. #include <linux/slab.h>
  12. #include <linux/delay.h>
  13. #include <linux/mm.h>
  14. #include <linux/fb.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ioport.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/acpi.h>
  20. enum {
  21. FB_GET_WIDTH = 0x00,
  22. FB_GET_HEIGHT = 0x04,
  23. FB_INT_STATUS = 0x08,
  24. FB_INT_ENABLE = 0x0c,
  25. FB_SET_BASE = 0x10,
  26. FB_SET_ROTATION = 0x14,
  27. FB_SET_BLANK = 0x18,
  28. FB_GET_PHYS_WIDTH = 0x1c,
  29. FB_GET_PHYS_HEIGHT = 0x20,
  30. FB_INT_VSYNC = 1U << 0,
  31. FB_INT_BASE_UPDATE_DONE = 1U << 1
  32. };
  33. struct goldfish_fb {
  34. void __iomem *reg_base;
  35. int irq;
  36. spinlock_t lock;
  37. wait_queue_head_t wait;
  38. int base_update_count;
  39. int rotation;
  40. struct fb_info fb;
  41. u32 cmap[16];
  42. };
  43. static irqreturn_t goldfish_fb_interrupt(int irq, void *dev_id)
  44. {
  45. unsigned long irq_flags;
  46. struct goldfish_fb *fb = dev_id;
  47. u32 status;
  48. spin_lock_irqsave(&fb->lock, irq_flags);
  49. status = readl(fb->reg_base + FB_INT_STATUS);
  50. if (status & FB_INT_BASE_UPDATE_DONE) {
  51. fb->base_update_count++;
  52. wake_up(&fb->wait);
  53. }
  54. spin_unlock_irqrestore(&fb->lock, irq_flags);
  55. return status ? IRQ_HANDLED : IRQ_NONE;
  56. }
  57. static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
  58. {
  59. unsigned int mask = (1 << bf->length) - 1;
  60. return (val >> (16 - bf->length) & mask) << bf->offset;
  61. }
  62. static int
  63. goldfish_fb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
  64. unsigned int blue, unsigned int transp, struct fb_info *info)
  65. {
  66. struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
  67. if (regno < 16) {
  68. fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
  69. convert_bitfield(blue, &fb->fb.var.blue) |
  70. convert_bitfield(green, &fb->fb.var.green) |
  71. convert_bitfield(red, &fb->fb.var.red);
  72. return 0;
  73. } else {
  74. return 1;
  75. }
  76. }
  77. static int goldfish_fb_check_var(struct fb_var_screeninfo *var,
  78. struct fb_info *info)
  79. {
  80. if ((var->rotate & 1) != (info->var.rotate & 1)) {
  81. if ((var->xres != info->var.yres) ||
  82. (var->yres != info->var.xres) ||
  83. (var->xres_virtual != info->var.yres) ||
  84. (var->yres_virtual > info->var.xres * 2) ||
  85. (var->yres_virtual < info->var.xres)) {
  86. return -EINVAL;
  87. }
  88. } else {
  89. if ((var->xres != info->var.xres) ||
  90. (var->yres != info->var.yres) ||
  91. (var->xres_virtual != info->var.xres) ||
  92. (var->yres_virtual > info->var.yres * 2) ||
  93. (var->yres_virtual < info->var.yres)) {
  94. return -EINVAL;
  95. }
  96. }
  97. if ((var->xoffset != info->var.xoffset) ||
  98. (var->bits_per_pixel != info->var.bits_per_pixel) ||
  99. (var->grayscale != info->var.grayscale)) {
  100. return -EINVAL;
  101. }
  102. return 0;
  103. }
  104. static int goldfish_fb_set_par(struct fb_info *info)
  105. {
  106. struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
  107. if (fb->rotation != fb->fb.var.rotate) {
  108. info->fix.line_length = info->var.xres * 2;
  109. fb->rotation = fb->fb.var.rotate;
  110. writel(fb->rotation, fb->reg_base + FB_SET_ROTATION);
  111. }
  112. return 0;
  113. }
  114. static int goldfish_fb_pan_display(struct fb_var_screeninfo *var,
  115. struct fb_info *info)
  116. {
  117. unsigned long irq_flags;
  118. int base_update_count;
  119. struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
  120. spin_lock_irqsave(&fb->lock, irq_flags);
  121. base_update_count = fb->base_update_count;
  122. writel(fb->fb.fix.smem_start + fb->fb.var.xres * 2 * var->yoffset,
  123. fb->reg_base + FB_SET_BASE);
  124. spin_unlock_irqrestore(&fb->lock, irq_flags);
  125. wait_event_timeout(fb->wait,
  126. fb->base_update_count != base_update_count, HZ / 15);
  127. if (fb->base_update_count == base_update_count)
  128. pr_err("%s: timeout waiting for base update\n", __func__);
  129. return 0;
  130. }
  131. static int goldfish_fb_blank(int blank, struct fb_info *info)
  132. {
  133. struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
  134. switch (blank) {
  135. case FB_BLANK_NORMAL:
  136. writel(1, fb->reg_base + FB_SET_BLANK);
  137. break;
  138. case FB_BLANK_UNBLANK:
  139. writel(0, fb->reg_base + FB_SET_BLANK);
  140. break;
  141. }
  142. return 0;
  143. }
  144. static const struct fb_ops goldfish_fb_ops = {
  145. .owner = THIS_MODULE,
  146. .fb_check_var = goldfish_fb_check_var,
  147. .fb_set_par = goldfish_fb_set_par,
  148. .fb_setcolreg = goldfish_fb_setcolreg,
  149. .fb_pan_display = goldfish_fb_pan_display,
  150. .fb_blank = goldfish_fb_blank,
  151. .fb_fillrect = cfb_fillrect,
  152. .fb_copyarea = cfb_copyarea,
  153. .fb_imageblit = cfb_imageblit,
  154. };
  155. static int goldfish_fb_probe(struct platform_device *pdev)
  156. {
  157. int ret;
  158. struct resource *r;
  159. struct goldfish_fb *fb;
  160. size_t framesize;
  161. u32 width, height;
  162. dma_addr_t fbpaddr;
  163. fb = kzalloc(sizeof(*fb), GFP_KERNEL);
  164. if (fb == NULL) {
  165. ret = -ENOMEM;
  166. goto err_fb_alloc_failed;
  167. }
  168. spin_lock_init(&fb->lock);
  169. init_waitqueue_head(&fb->wait);
  170. platform_set_drvdata(pdev, fb);
  171. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  172. if (r == NULL) {
  173. ret = -ENODEV;
  174. goto err_no_io_base;
  175. }
  176. fb->reg_base = ioremap(r->start, PAGE_SIZE);
  177. if (fb->reg_base == NULL) {
  178. ret = -ENOMEM;
  179. goto err_no_io_base;
  180. }
  181. fb->irq = platform_get_irq(pdev, 0);
  182. if (fb->irq <= 0) {
  183. ret = -ENODEV;
  184. goto err_no_irq;
  185. }
  186. width = readl(fb->reg_base + FB_GET_WIDTH);
  187. height = readl(fb->reg_base + FB_GET_HEIGHT);
  188. fb->fb.fbops = &goldfish_fb_ops;
  189. fb->fb.flags = FBINFO_FLAG_DEFAULT;
  190. fb->fb.pseudo_palette = fb->cmap;
  191. fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  192. fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  193. fb->fb.fix.line_length = width * 2;
  194. fb->fb.fix.accel = FB_ACCEL_NONE;
  195. fb->fb.fix.ypanstep = 1;
  196. fb->fb.var.xres = width;
  197. fb->fb.var.yres = height;
  198. fb->fb.var.xres_virtual = width;
  199. fb->fb.var.yres_virtual = height * 2;
  200. fb->fb.var.bits_per_pixel = 16;
  201. fb->fb.var.activate = FB_ACTIVATE_NOW;
  202. fb->fb.var.height = readl(fb->reg_base + FB_GET_PHYS_HEIGHT);
  203. fb->fb.var.width = readl(fb->reg_base + FB_GET_PHYS_WIDTH);
  204. fb->fb.var.pixclock = 0;
  205. fb->fb.var.red.offset = 11;
  206. fb->fb.var.red.length = 5;
  207. fb->fb.var.green.offset = 5;
  208. fb->fb.var.green.length = 6;
  209. fb->fb.var.blue.offset = 0;
  210. fb->fb.var.blue.length = 5;
  211. framesize = width * height * 2 * 2;
  212. fb->fb.screen_base = (char __force __iomem *)dma_alloc_coherent(
  213. &pdev->dev, framesize,
  214. &fbpaddr, GFP_KERNEL);
  215. pr_debug("allocating frame buffer %d * %d, got %p\n",
  216. width, height, fb->fb.screen_base);
  217. if (fb->fb.screen_base == NULL) {
  218. ret = -ENOMEM;
  219. goto err_alloc_screen_base_failed;
  220. }
  221. fb->fb.fix.smem_start = fbpaddr;
  222. fb->fb.fix.smem_len = framesize;
  223. ret = fb_set_var(&fb->fb, &fb->fb.var);
  224. if (ret)
  225. goto err_fb_set_var_failed;
  226. ret = request_irq(fb->irq, goldfish_fb_interrupt, IRQF_SHARED,
  227. pdev->name, fb);
  228. if (ret)
  229. goto err_request_irq_failed;
  230. writel(FB_INT_BASE_UPDATE_DONE, fb->reg_base + FB_INT_ENABLE);
  231. goldfish_fb_pan_display(&fb->fb.var, &fb->fb); /* updates base */
  232. ret = register_framebuffer(&fb->fb);
  233. if (ret)
  234. goto err_register_framebuffer_failed;
  235. return 0;
  236. err_register_framebuffer_failed:
  237. free_irq(fb->irq, fb);
  238. err_request_irq_failed:
  239. err_fb_set_var_failed:
  240. dma_free_coherent(&pdev->dev, framesize,
  241. (void *)fb->fb.screen_base,
  242. fb->fb.fix.smem_start);
  243. err_alloc_screen_base_failed:
  244. err_no_irq:
  245. iounmap(fb->reg_base);
  246. err_no_io_base:
  247. kfree(fb);
  248. err_fb_alloc_failed:
  249. return ret;
  250. }
  251. static int goldfish_fb_remove(struct platform_device *pdev)
  252. {
  253. size_t framesize;
  254. struct goldfish_fb *fb = platform_get_drvdata(pdev);
  255. framesize = fb->fb.var.xres_virtual * fb->fb.var.yres_virtual * 2;
  256. unregister_framebuffer(&fb->fb);
  257. free_irq(fb->irq, fb);
  258. dma_free_coherent(&pdev->dev, framesize, (void *)fb->fb.screen_base,
  259. fb->fb.fix.smem_start);
  260. iounmap(fb->reg_base);
  261. kfree(fb);
  262. return 0;
  263. }
  264. static const struct of_device_id goldfish_fb_of_match[] = {
  265. { .compatible = "google,goldfish-fb", },
  266. {},
  267. };
  268. MODULE_DEVICE_TABLE(of, goldfish_fb_of_match);
  269. static const struct acpi_device_id goldfish_fb_acpi_match[] = {
  270. { "GFSH0004", 0 },
  271. { },
  272. };
  273. MODULE_DEVICE_TABLE(acpi, goldfish_fb_acpi_match);
  274. static struct platform_driver goldfish_fb_driver = {
  275. .probe = goldfish_fb_probe,
  276. .remove = goldfish_fb_remove,
  277. .driver = {
  278. .name = "goldfish_fb",
  279. .of_match_table = goldfish_fb_of_match,
  280. .acpi_match_table = ACPI_PTR(goldfish_fb_acpi_match),
  281. }
  282. };
  283. module_platform_driver(goldfish_fb_driver);
  284. MODULE_LICENSE("GPL v2");