2 * Copyright (C) 2007 Google, Inc.
3 * Copyright (C) 2012 Intel, Inc.
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.
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.
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>
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>
37 FB_SET_ROTATION = 0x14,
39 FB_GET_PHYS_WIDTH = 0x1c,
40 FB_GET_PHYS_HEIGHT = 0x20,
43 FB_INT_VSYNC = 1U << 0,
44 FB_INT_BASE_UPDATE_DONE = 1U << 1
47 /* These values *must* match the platform definitions found under
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,
58 struct framebuffer_config {
74 static const struct framebuffer_config *get_fb_config_from_format(int format)
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 */
86 format < sizeof(fb_configs) / sizeof(struct framebuffer_config)) {
87 return &fb_configs[format];
90 return &fb_configs[HAL_PIXEL_FORMAT_RGB_565]; /* legacy default */
94 void __iomem *reg_base;
97 wait_queue_head_t wait;
98 int base_update_count;
104 static irqreturn_t goldfish_fb_interrupt(int irq, void *dev_id)
106 unsigned long irq_flags;
107 struct goldfish_fb *fb = dev_id;
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++;
116 spin_unlock_irqrestore(&fb->lock, irq_flags);
117 return status ? IRQ_HANDLED : IRQ_NONE;
120 static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
122 unsigned int mask = (1 << bf->length) - 1;
124 return (val >> (16 - bf->length) & mask) << bf->offset;
128 goldfish_fb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
129 unsigned int blue, unsigned int transp, struct fb_info *info)
131 struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
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);
144 static int goldfish_fb_check_var(struct fb_var_screeninfo *var,
145 struct fb_info *info)
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)) {
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)) {
164 if ((var->xoffset != info->var.xoffset) ||
165 (var->bits_per_pixel != info->var.bits_per_pixel) ||
166 (var->grayscale != info->var.grayscale)) {
172 static int goldfish_fb_set_par(struct fb_info *info)
174 struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
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);
186 static int goldfish_fb_pan_display(struct fb_var_screeninfo *var,
187 struct fb_info *info)
189 unsigned long irq_flags;
190 int base_update_count;
191 struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
193 spin_lock_irqsave(&fb->lock, irq_flags);
194 base_update_count = fb->base_update_count;
195 writel(fb->fb.fix.smem_start +
197 (fb->fb.var.bits_per_pixel / CHAR_BIT) *
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 "
209 static int goldfish_fb_blank(int blank, struct fb_info *info)
211 struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
214 case FB_BLANK_NORMAL:
215 writel(1, fb->reg_base + FB_SET_BLANK);
217 case FB_BLANK_UNBLANK:
218 writel(0, fb->reg_base + FB_SET_BLANK);
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,
237 static int goldfish_fb_probe(struct platform_device *pdev)
241 struct goldfish_fb *fb;
243 u32 width, height, format;
246 const struct framebuffer_config *fb_config;
248 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
251 goto err_fb_alloc_failed;
253 spin_lock_init(&fb->lock);
254 init_waitqueue_head(&fb->wait);
255 platform_set_drvdata(pdev, fb);
257 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
262 fb->reg_base = ioremap(r->start, PAGE_SIZE);
263 if (fb->reg_base == NULL) {
268 fb->irq = platform_get_irq(pdev, 0);
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);
282 bytes_per_pixel = fb_config->bytes_per_pixel;
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;
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;
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;
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) {
320 goto err_alloc_screen_base_failed;
322 fb->fb.fix.smem_start = fbpaddr;
323 fb->fb.fix.smem_len = framesize;
325 ret = fb_set_var(&fb->fb, &fb->fb.var);
327 goto err_fb_set_var_failed;
329 ret = request_irq(fb->irq, goldfish_fb_interrupt, IRQF_SHARED,
332 goto err_request_irq_failed;
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 */
337 ret = register_framebuffer(&fb->fb);
339 goto err_register_framebuffer_failed;
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:
351 iounmap(fb->reg_base);
358 static int goldfish_fb_remove(struct platform_device *pdev)
361 struct goldfish_fb *fb = platform_get_drvdata(pdev);
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);
368 dma_free_coherent(&pdev->dev, framesize, (void *)fb->fb.screen_base,
369 fb->fb.fix.smem_start);
370 iounmap(fb->reg_base);
375 static const struct of_device_id goldfish_fb_of_match[] = {
376 { .compatible = "google,goldfish-fb", },
379 MODULE_DEVICE_TABLE(of, goldfish_fb_of_match);
381 static const struct acpi_device_id goldfish_fb_acpi_match[] = {
385 MODULE_DEVICE_TABLE(acpi, goldfish_fb_acpi_match);
387 static struct platform_driver goldfish_fb_driver = {
388 .probe = goldfish_fb_probe,
389 .remove = goldfish_fb_remove,
391 .name = "goldfish_fb",
392 .of_match_table = goldfish_fb_of_match,
393 .acpi_match_table = ACPI_PTR(goldfish_fb_acpi_match),
397 module_platform_driver(goldfish_fb_driver);
399 MODULE_LICENSE("GPL v2");