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