0001
0002
0003
0004
0005
0006
0007 #include <linux/module.h>
0008 #include <linux/kernel.h>
0009 #include <linux/dma-mapping.h>
0010 #include <linux/errno.h>
0011 #include <linux/string.h>
0012 #include <linux/slab.h>
0013 #include <linux/delay.h>
0014 #include <linux/mm.h>
0015 #include <linux/fb.h>
0016 #include <linux/init.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/ioport.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/acpi.h>
0021
0022 enum {
0023 FB_GET_WIDTH = 0x00,
0024 FB_GET_HEIGHT = 0x04,
0025 FB_INT_STATUS = 0x08,
0026 FB_INT_ENABLE = 0x0c,
0027 FB_SET_BASE = 0x10,
0028 FB_SET_ROTATION = 0x14,
0029 FB_SET_BLANK = 0x18,
0030 FB_GET_PHYS_WIDTH = 0x1c,
0031 FB_GET_PHYS_HEIGHT = 0x20,
0032
0033 FB_INT_VSYNC = 1U << 0,
0034 FB_INT_BASE_UPDATE_DONE = 1U << 1
0035 };
0036
0037 struct goldfish_fb {
0038 void __iomem *reg_base;
0039 int irq;
0040 spinlock_t lock;
0041 wait_queue_head_t wait;
0042 int base_update_count;
0043 int rotation;
0044 struct fb_info fb;
0045 u32 cmap[16];
0046 };
0047
0048 static irqreturn_t goldfish_fb_interrupt(int irq, void *dev_id)
0049 {
0050 unsigned long irq_flags;
0051 struct goldfish_fb *fb = dev_id;
0052 u32 status;
0053
0054 spin_lock_irqsave(&fb->lock, irq_flags);
0055 status = readl(fb->reg_base + FB_INT_STATUS);
0056 if (status & FB_INT_BASE_UPDATE_DONE) {
0057 fb->base_update_count++;
0058 wake_up(&fb->wait);
0059 }
0060 spin_unlock_irqrestore(&fb->lock, irq_flags);
0061 return status ? IRQ_HANDLED : IRQ_NONE;
0062 }
0063
0064 static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
0065 {
0066 unsigned int mask = (1 << bf->length) - 1;
0067
0068 return (val >> (16 - bf->length) & mask) << bf->offset;
0069 }
0070
0071 static int
0072 goldfish_fb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
0073 unsigned int blue, unsigned int transp, struct fb_info *info)
0074 {
0075 struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
0076
0077 if (regno < 16) {
0078 fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
0079 convert_bitfield(blue, &fb->fb.var.blue) |
0080 convert_bitfield(green, &fb->fb.var.green) |
0081 convert_bitfield(red, &fb->fb.var.red);
0082 return 0;
0083 } else {
0084 return 1;
0085 }
0086 }
0087
0088 static int goldfish_fb_check_var(struct fb_var_screeninfo *var,
0089 struct fb_info *info)
0090 {
0091 if ((var->rotate & 1) != (info->var.rotate & 1)) {
0092 if ((var->xres != info->var.yres) ||
0093 (var->yres != info->var.xres) ||
0094 (var->xres_virtual != info->var.yres) ||
0095 (var->yres_virtual > info->var.xres * 2) ||
0096 (var->yres_virtual < info->var.xres)) {
0097 return -EINVAL;
0098 }
0099 } else {
0100 if ((var->xres != info->var.xres) ||
0101 (var->yres != info->var.yres) ||
0102 (var->xres_virtual != info->var.xres) ||
0103 (var->yres_virtual > info->var.yres * 2) ||
0104 (var->yres_virtual < info->var.yres)) {
0105 return -EINVAL;
0106 }
0107 }
0108 if ((var->xoffset != info->var.xoffset) ||
0109 (var->bits_per_pixel != info->var.bits_per_pixel) ||
0110 (var->grayscale != info->var.grayscale)) {
0111 return -EINVAL;
0112 }
0113 return 0;
0114 }
0115
0116 static int goldfish_fb_set_par(struct fb_info *info)
0117 {
0118 struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
0119
0120 if (fb->rotation != fb->fb.var.rotate) {
0121 info->fix.line_length = info->var.xres * 2;
0122 fb->rotation = fb->fb.var.rotate;
0123 writel(fb->rotation, fb->reg_base + FB_SET_ROTATION);
0124 }
0125 return 0;
0126 }
0127
0128
0129 static int goldfish_fb_pan_display(struct fb_var_screeninfo *var,
0130 struct fb_info *info)
0131 {
0132 unsigned long irq_flags;
0133 int base_update_count;
0134 struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
0135
0136 spin_lock_irqsave(&fb->lock, irq_flags);
0137 base_update_count = fb->base_update_count;
0138 writel(fb->fb.fix.smem_start + fb->fb.var.xres * 2 * var->yoffset,
0139 fb->reg_base + FB_SET_BASE);
0140 spin_unlock_irqrestore(&fb->lock, irq_flags);
0141 wait_event_timeout(fb->wait,
0142 fb->base_update_count != base_update_count, HZ / 15);
0143 if (fb->base_update_count == base_update_count)
0144 pr_err("%s: timeout waiting for base update\n", __func__);
0145 return 0;
0146 }
0147
0148 static int goldfish_fb_blank(int blank, struct fb_info *info)
0149 {
0150 struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
0151
0152 switch (blank) {
0153 case FB_BLANK_NORMAL:
0154 writel(1, fb->reg_base + FB_SET_BLANK);
0155 break;
0156 case FB_BLANK_UNBLANK:
0157 writel(0, fb->reg_base + FB_SET_BLANK);
0158 break;
0159 }
0160 return 0;
0161 }
0162
0163 static const struct fb_ops goldfish_fb_ops = {
0164 .owner = THIS_MODULE,
0165 .fb_check_var = goldfish_fb_check_var,
0166 .fb_set_par = goldfish_fb_set_par,
0167 .fb_setcolreg = goldfish_fb_setcolreg,
0168 .fb_pan_display = goldfish_fb_pan_display,
0169 .fb_blank = goldfish_fb_blank,
0170 .fb_fillrect = cfb_fillrect,
0171 .fb_copyarea = cfb_copyarea,
0172 .fb_imageblit = cfb_imageblit,
0173 };
0174
0175
0176 static int goldfish_fb_probe(struct platform_device *pdev)
0177 {
0178 int ret;
0179 struct resource *r;
0180 struct goldfish_fb *fb;
0181 size_t framesize;
0182 u32 width, height;
0183 dma_addr_t fbpaddr;
0184
0185 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
0186 if (fb == NULL) {
0187 ret = -ENOMEM;
0188 goto err_fb_alloc_failed;
0189 }
0190 spin_lock_init(&fb->lock);
0191 init_waitqueue_head(&fb->wait);
0192 platform_set_drvdata(pdev, fb);
0193
0194 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0195 if (r == NULL) {
0196 ret = -ENODEV;
0197 goto err_no_io_base;
0198 }
0199 fb->reg_base = ioremap(r->start, PAGE_SIZE);
0200 if (fb->reg_base == NULL) {
0201 ret = -ENOMEM;
0202 goto err_no_io_base;
0203 }
0204
0205 fb->irq = platform_get_irq(pdev, 0);
0206 if (fb->irq <= 0) {
0207 ret = -ENODEV;
0208 goto err_no_irq;
0209 }
0210
0211 width = readl(fb->reg_base + FB_GET_WIDTH);
0212 height = readl(fb->reg_base + FB_GET_HEIGHT);
0213
0214 fb->fb.fbops = &goldfish_fb_ops;
0215 fb->fb.flags = FBINFO_FLAG_DEFAULT;
0216 fb->fb.pseudo_palette = fb->cmap;
0217 fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
0218 fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
0219 fb->fb.fix.line_length = width * 2;
0220 fb->fb.fix.accel = FB_ACCEL_NONE;
0221 fb->fb.fix.ypanstep = 1;
0222
0223 fb->fb.var.xres = width;
0224 fb->fb.var.yres = height;
0225 fb->fb.var.xres_virtual = width;
0226 fb->fb.var.yres_virtual = height * 2;
0227 fb->fb.var.bits_per_pixel = 16;
0228 fb->fb.var.activate = FB_ACTIVATE_NOW;
0229 fb->fb.var.height = readl(fb->reg_base + FB_GET_PHYS_HEIGHT);
0230 fb->fb.var.width = readl(fb->reg_base + FB_GET_PHYS_WIDTH);
0231 fb->fb.var.pixclock = 0;
0232
0233 fb->fb.var.red.offset = 11;
0234 fb->fb.var.red.length = 5;
0235 fb->fb.var.green.offset = 5;
0236 fb->fb.var.green.length = 6;
0237 fb->fb.var.blue.offset = 0;
0238 fb->fb.var.blue.length = 5;
0239
0240 framesize = width * height * 2 * 2;
0241 fb->fb.screen_base = (char __force __iomem *)dma_alloc_coherent(
0242 &pdev->dev, framesize,
0243 &fbpaddr, GFP_KERNEL);
0244 pr_debug("allocating frame buffer %d * %d, got %p\n",
0245 width, height, fb->fb.screen_base);
0246 if (fb->fb.screen_base == NULL) {
0247 ret = -ENOMEM;
0248 goto err_alloc_screen_base_failed;
0249 }
0250 fb->fb.fix.smem_start = fbpaddr;
0251 fb->fb.fix.smem_len = framesize;
0252
0253 ret = fb_set_var(&fb->fb, &fb->fb.var);
0254 if (ret)
0255 goto err_fb_set_var_failed;
0256
0257 ret = request_irq(fb->irq, goldfish_fb_interrupt, IRQF_SHARED,
0258 pdev->name, fb);
0259 if (ret)
0260 goto err_request_irq_failed;
0261
0262 writel(FB_INT_BASE_UPDATE_DONE, fb->reg_base + FB_INT_ENABLE);
0263 goldfish_fb_pan_display(&fb->fb.var, &fb->fb);
0264
0265 ret = register_framebuffer(&fb->fb);
0266 if (ret)
0267 goto err_register_framebuffer_failed;
0268 return 0;
0269
0270 err_register_framebuffer_failed:
0271 free_irq(fb->irq, fb);
0272 err_request_irq_failed:
0273 err_fb_set_var_failed:
0274 dma_free_coherent(&pdev->dev, framesize,
0275 (void *)fb->fb.screen_base,
0276 fb->fb.fix.smem_start);
0277 err_alloc_screen_base_failed:
0278 err_no_irq:
0279 iounmap(fb->reg_base);
0280 err_no_io_base:
0281 kfree(fb);
0282 err_fb_alloc_failed:
0283 return ret;
0284 }
0285
0286 static int goldfish_fb_remove(struct platform_device *pdev)
0287 {
0288 size_t framesize;
0289 struct goldfish_fb *fb = platform_get_drvdata(pdev);
0290
0291 framesize = fb->fb.var.xres_virtual * fb->fb.var.yres_virtual * 2;
0292 unregister_framebuffer(&fb->fb);
0293 free_irq(fb->irq, fb);
0294
0295 dma_free_coherent(&pdev->dev, framesize, (void *)fb->fb.screen_base,
0296 fb->fb.fix.smem_start);
0297 iounmap(fb->reg_base);
0298 kfree(fb);
0299 return 0;
0300 }
0301
0302 static const struct of_device_id goldfish_fb_of_match[] = {
0303 { .compatible = "google,goldfish-fb", },
0304 {},
0305 };
0306 MODULE_DEVICE_TABLE(of, goldfish_fb_of_match);
0307
0308 #ifdef CONFIG_ACPI
0309 static const struct acpi_device_id goldfish_fb_acpi_match[] = {
0310 { "GFSH0004", 0 },
0311 { },
0312 };
0313 MODULE_DEVICE_TABLE(acpi, goldfish_fb_acpi_match);
0314 #endif
0315
0316 static struct platform_driver goldfish_fb_driver = {
0317 .probe = goldfish_fb_probe,
0318 .remove = goldfish_fb_remove,
0319 .driver = {
0320 .name = "goldfish_fb",
0321 .of_match_table = goldfish_fb_of_match,
0322 .acpi_match_table = ACPI_PTR(goldfish_fb_acpi_match),
0323 }
0324 };
0325
0326 module_platform_driver(goldfish_fb_driver);
0327
0328 MODULE_LICENSE("GPL v2");