OSDN Git Service

Merge 4.4.187 into android-4.4
[sagit-ice-cold/kernel_xiaomi_msm8998.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         FB_GET_FORMAT       = 0x24,
42
43         FB_INT_VSYNC             = 1U << 0,
44         FB_INT_BASE_UPDATE_DONE  = 1U << 1
45 };
46
47 /* These values *must* match the platform definitions found under
48  * <system/graphics.h>
49  */
50 enum {
51         HAL_PIXEL_FORMAT_RGBA_8888          = 1,
52         HAL_PIXEL_FORMAT_RGBX_8888          = 2,
53         HAL_PIXEL_FORMAT_RGB_888            = 3,
54         HAL_PIXEL_FORMAT_RGB_565            = 4,
55         HAL_PIXEL_FORMAT_BGRA_8888          = 5,
56 };
57
58 struct framebuffer_config {
59         u8 bytes_per_pixel;
60         u8 red_offset;
61         u8 red_length;
62         u8 green_offset;
63         u8 green_length;
64         u8 blue_offset;
65         u8 blue_length;
66         u8 transp_offset;
67         u8 transp_length;
68 };
69
70 enum {
71         CHAR_BIT = 8
72 };
73
74 static const struct framebuffer_config *get_fb_config_from_format(int format)
75 {
76         static const struct framebuffer_config fb_configs[] = {
77                 { 0, 0,  0, 0, 0, 0,  0, 0,  0 }, /* Invalid, assume RGB_565 */
78                 { 4, 0,  8, 8, 8, 16, 8, 24, 8 }, /* HAL_PIXEL_FORMAT_RGBA_8888 */
79                 { 4, 0,  8, 8, 8, 16, 8, 0,  0 }, /* HAL_PIXEL_FORMAT_RGBX_8888 */
80                 { 3, 0,  8, 8, 8, 16, 8, 0,  0 }, /* HAL_PIXEL_FORMAT_RGB_888 */
81                 { 2, 11, 5, 5, 6, 0,  5, 0,  0 }, /* HAL_PIXEL_FORMAT_RGB_565 */
82                 { 4, 16, 8, 8, 8, 0,  8, 24, 8 }, /* HAL_PIXEL_FORMAT_BGRA_8888 */
83         };
84
85         if (format > 0 &&
86                 format < sizeof(fb_configs) / sizeof(struct framebuffer_config)) {
87                 return &fb_configs[format];
88         }
89
90         return &fb_configs[HAL_PIXEL_FORMAT_RGB_565]; /* legacy default */
91 }
92
93 struct goldfish_fb {
94         void __iomem *reg_base;
95         int irq;
96         spinlock_t lock;
97         wait_queue_head_t wait;
98         int base_update_count;
99         int rotation;
100         struct fb_info fb;
101         u32 cmap[16];
102 };
103
104 static irqreturn_t goldfish_fb_interrupt(int irq, void *dev_id)
105 {
106         unsigned long irq_flags;
107         struct goldfish_fb *fb = dev_id;
108         u32 status;
109
110         spin_lock_irqsave(&fb->lock, irq_flags);
111         status = readl(fb->reg_base + FB_INT_STATUS);
112         if (status & FB_INT_BASE_UPDATE_DONE) {
113                 fb->base_update_count++;
114                 wake_up(&fb->wait);
115         }
116         spin_unlock_irqrestore(&fb->lock, irq_flags);
117         return status ? IRQ_HANDLED : IRQ_NONE;
118 }
119
120 static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
121 {
122         unsigned int mask = (1 << bf->length) - 1;
123
124         return (val >> (16 - bf->length) & mask) << bf->offset;
125 }
126
127 static int
128 goldfish_fb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
129                  unsigned int blue, unsigned int transp, struct fb_info *info)
130 {
131         struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
132
133         if (regno < 16) {
134                 fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
135                                   convert_bitfield(blue, &fb->fb.var.blue) |
136                                   convert_bitfield(green, &fb->fb.var.green) |
137                                   convert_bitfield(red, &fb->fb.var.red);
138                 return 0;
139         } else {
140                 return 1;
141         }
142 }
143
144 static int goldfish_fb_check_var(struct fb_var_screeninfo *var,
145                                                         struct fb_info *info)
146 {
147         if ((var->rotate & 1) != (info->var.rotate & 1)) {
148                 if ((var->xres != info->var.yres) ||
149                                 (var->yres != info->var.xres) ||
150                                 (var->xres_virtual != info->var.yres) ||
151                                 (var->yres_virtual > info->var.xres * 2) ||
152                                 (var->yres_virtual < info->var.xres)) {
153                         return -EINVAL;
154                 }
155         } else {
156                 if ((var->xres != info->var.xres) ||
157                    (var->yres != info->var.yres) ||
158                    (var->xres_virtual != info->var.xres) ||
159                    (var->yres_virtual > info->var.yres * 2) ||
160                    (var->yres_virtual < info->var.yres)) {
161                         return -EINVAL;
162                 }
163         }
164         if ((var->xoffset != info->var.xoffset) ||
165                         (var->bits_per_pixel != info->var.bits_per_pixel) ||
166                         (var->grayscale != info->var.grayscale)) {
167                 return -EINVAL;
168         }
169         return 0;
170 }
171
172 static int goldfish_fb_set_par(struct fb_info *info)
173 {
174         struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
175
176         if (fb->rotation != fb->fb.var.rotate) {
177                 info->fix.line_length = info->var.xres *
178                         (fb->fb.var.bits_per_pixel / CHAR_BIT);
179                 fb->rotation = fb->fb.var.rotate;
180                 writel(fb->rotation, fb->reg_base + FB_SET_ROTATION);
181         }
182         return 0;
183 }
184
185
186 static int goldfish_fb_pan_display(struct fb_var_screeninfo *var,
187                                                         struct fb_info *info)
188 {
189         unsigned long irq_flags;
190         int base_update_count;
191         struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
192
193         spin_lock_irqsave(&fb->lock, irq_flags);
194         base_update_count = fb->base_update_count;
195         writel(fb->fb.fix.smem_start +
196                         fb->fb.var.xres *
197                         (fb->fb.var.bits_per_pixel / CHAR_BIT) *
198                         var->yoffset,
199                         fb->reg_base + FB_SET_BASE);
200         spin_unlock_irqrestore(&fb->lock, irq_flags);
201         wait_event_timeout(fb->wait,
202                         fb->base_update_count != base_update_count, HZ / 15);
203         if (fb->base_update_count == base_update_count)
204                 pr_err("goldfish_fb_pan_display: timeout waiting for "
205                         "base update\n");
206         return 0;
207 }
208
209 static int goldfish_fb_blank(int blank, struct fb_info *info)
210 {
211         struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
212
213         switch (blank) {
214         case FB_BLANK_NORMAL:
215                 writel(1, fb->reg_base + FB_SET_BLANK);
216                 break;
217         case FB_BLANK_UNBLANK:
218                 writel(0, fb->reg_base + FB_SET_BLANK);
219                 break;
220         }
221         return 0;
222 }
223
224 static struct fb_ops goldfish_fb_ops = {
225         .owner          = THIS_MODULE,
226         .fb_check_var   = goldfish_fb_check_var,
227         .fb_set_par     = goldfish_fb_set_par,
228         .fb_setcolreg   = goldfish_fb_setcolreg,
229         .fb_pan_display = goldfish_fb_pan_display,
230         .fb_blank       = goldfish_fb_blank,
231         .fb_fillrect    = cfb_fillrect,
232         .fb_copyarea    = cfb_copyarea,
233         .fb_imageblit   = cfb_imageblit,
234 };
235
236
237 static int goldfish_fb_probe(struct platform_device *pdev)
238 {
239         int ret;
240         struct resource *r;
241         struct goldfish_fb *fb;
242         size_t framesize;
243         u32 width, height, format;
244         int bytes_per_pixel;
245         dma_addr_t fbpaddr;
246         const struct framebuffer_config *fb_config;
247
248         fb = kzalloc(sizeof(*fb), GFP_KERNEL);
249         if (fb == NULL) {
250                 ret = -ENOMEM;
251                 goto err_fb_alloc_failed;
252         }
253         spin_lock_init(&fb->lock);
254         init_waitqueue_head(&fb->wait);
255         platform_set_drvdata(pdev, fb);
256
257         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258         if (r == NULL) {
259                 ret = -ENODEV;
260                 goto err_no_io_base;
261         }
262         fb->reg_base = ioremap(r->start, PAGE_SIZE);
263         if (fb->reg_base == NULL) {
264                 ret = -ENOMEM;
265                 goto err_no_io_base;
266         }
267
268         fb->irq = platform_get_irq(pdev, 0);
269         if (fb->irq <= 0) {
270                 ret = -ENODEV;
271                 goto err_no_irq;
272         }
273
274         width = readl(fb->reg_base + FB_GET_WIDTH);
275         height = readl(fb->reg_base + FB_GET_HEIGHT);
276         format = readl(fb->reg_base + FB_GET_FORMAT);
277         fb_config = get_fb_config_from_format(format);
278         if (!fb_config) {
279                 ret = -EINVAL;
280                 goto err_no_irq;
281         }
282         bytes_per_pixel = fb_config->bytes_per_pixel;
283
284         fb->fb.fbops            = &goldfish_fb_ops;
285         fb->fb.flags            = FBINFO_FLAG_DEFAULT;
286         fb->fb.pseudo_palette   = fb->cmap;
287         fb->fb.fix.type         = FB_TYPE_PACKED_PIXELS;
288         fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
289         fb->fb.fix.line_length = width * bytes_per_pixel;
290         fb->fb.fix.accel        = FB_ACCEL_NONE;
291         fb->fb.fix.ypanstep = 1;
292
293         fb->fb.var.xres         = width;
294         fb->fb.var.yres         = height;
295         fb->fb.var.xres_virtual = width;
296         fb->fb.var.yres_virtual = height * 2;
297         fb->fb.var.bits_per_pixel = bytes_per_pixel * CHAR_BIT;
298         fb->fb.var.activate     = FB_ACTIVATE_NOW;
299         fb->fb.var.height       = readl(fb->reg_base + FB_GET_PHYS_HEIGHT);
300         fb->fb.var.width        = readl(fb->reg_base + FB_GET_PHYS_WIDTH);
301         fb->fb.var.pixclock     = 0;
302
303         fb->fb.var.red.offset = fb_config->red_offset;
304         fb->fb.var.red.length = fb_config->red_length;
305         fb->fb.var.green.offset = fb_config->green_offset;
306         fb->fb.var.green.length = fb_config->green_length;
307         fb->fb.var.blue.offset = fb_config->blue_offset;
308         fb->fb.var.blue.length = fb_config->blue_length;
309         fb->fb.var.transp.offset = fb_config->transp_offset;
310         fb->fb.var.transp.length = fb_config->transp_length;
311
312         framesize = width * height * 2 * bytes_per_pixel;
313         fb->fb.screen_base = (char __force __iomem *)dma_alloc_coherent(
314                                                 &pdev->dev, framesize,
315                                                 &fbpaddr, GFP_KERNEL);
316         pr_debug("allocating frame buffer %d * %d, got %p\n",
317                                         width, height, fb->fb.screen_base);
318         if (fb->fb.screen_base == NULL) {
319                 ret = -ENOMEM;
320                 goto err_alloc_screen_base_failed;
321         }
322         fb->fb.fix.smem_start = fbpaddr;
323         fb->fb.fix.smem_len = framesize;
324
325         ret = fb_set_var(&fb->fb, &fb->fb.var);
326         if (ret)
327                 goto err_fb_set_var_failed;
328
329         ret = request_irq(fb->irq, goldfish_fb_interrupt, IRQF_SHARED,
330                                                         pdev->name, fb);
331         if (ret)
332                 goto err_request_irq_failed;
333
334         writel(FB_INT_BASE_UPDATE_DONE, fb->reg_base + FB_INT_ENABLE);
335         goldfish_fb_pan_display(&fb->fb.var, &fb->fb); /* updates base */
336
337         ret = register_framebuffer(&fb->fb);
338         if (ret)
339                 goto err_register_framebuffer_failed;
340         return 0;
341
342 err_register_framebuffer_failed:
343         free_irq(fb->irq, fb);
344 err_request_irq_failed:
345 err_fb_set_var_failed:
346         dma_free_coherent(&pdev->dev, framesize,
347                                 (void *)fb->fb.screen_base,
348                                 fb->fb.fix.smem_start);
349 err_alloc_screen_base_failed:
350 err_no_irq:
351         iounmap(fb->reg_base);
352 err_no_io_base:
353         kfree(fb);
354 err_fb_alloc_failed:
355         return ret;
356 }
357
358 static int goldfish_fb_remove(struct platform_device *pdev)
359 {
360         size_t framesize;
361         struct goldfish_fb *fb = platform_get_drvdata(pdev);
362
363         framesize = fb->fb.var.xres_virtual * fb->fb.var.yres_virtual *
364                 (fb->fb.var.bits_per_pixel / CHAR_BIT);
365         unregister_framebuffer(&fb->fb);
366         free_irq(fb->irq, fb);
367
368         dma_free_coherent(&pdev->dev, framesize, (void *)fb->fb.screen_base,
369                                                 fb->fb.fix.smem_start);
370         iounmap(fb->reg_base);
371         kfree(fb);
372         return 0;
373 }
374
375 static const struct of_device_id goldfish_fb_of_match[] = {
376         { .compatible = "google,goldfish-fb", },
377         {},
378 };
379 MODULE_DEVICE_TABLE(of, goldfish_fb_of_match);
380
381 static const struct acpi_device_id goldfish_fb_acpi_match[] = {
382         { "GFSH0004", 0 },
383         { },
384 };
385 MODULE_DEVICE_TABLE(acpi, goldfish_fb_acpi_match);
386
387 static struct platform_driver goldfish_fb_driver = {
388         .probe          = goldfish_fb_probe,
389         .remove         = goldfish_fb_remove,
390         .driver = {
391                 .name = "goldfish_fb",
392                 .of_match_table = goldfish_fb_of_match,
393                 .acpi_match_table = ACPI_PTR(goldfish_fb_acpi_match),
394         }
395 };
396
397 module_platform_driver(goldfish_fb_driver);
398
399 MODULE_LICENSE("GPL v2");