goldfishfb.c 8.3 KB

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