blob: 39662b2e35377c15beae013bfb748290fd1fff93 [file] [log] [blame]
/*
* Copyright (C) 2007 Google, Inc.
* Copyright (C) 2012 Intel, Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/dma-mapping.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/mm.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/platform_device.h>
#include <linux/acpi.h>
enum {
FB_GET_WIDTH = 0x00,
FB_GET_HEIGHT = 0x04,
FB_INT_STATUS = 0x08,
FB_INT_ENABLE = 0x0c,
FB_SET_BASE = 0x10,
FB_SET_ROTATION = 0x14,
FB_SET_BLANK = 0x18,
FB_GET_PHYS_WIDTH = 0x1c,
FB_GET_PHYS_HEIGHT = 0x20,
FB_GET_FORMAT = 0x24,
FB_INT_VSYNC = 1U << 0,
FB_INT_BASE_UPDATE_DONE = 1U << 1
};
/* These values *must* match the platform definitions found under
* <system/graphics.h>
*/
enum {
HAL_PIXEL_FORMAT_RGBA_8888 = 1,
HAL_PIXEL_FORMAT_RGBX_8888 = 2,
HAL_PIXEL_FORMAT_RGB_888 = 3,
HAL_PIXEL_FORMAT_RGB_565 = 4,
HAL_PIXEL_FORMAT_BGRA_8888 = 5,
};
struct framebuffer_config {
u8 bytes_per_pixel;
u8 red_offset;
u8 red_length;
u8 green_offset;
u8 green_length;
u8 blue_offset;
u8 blue_length;
u8 transp_offset;
u8 transp_length;
};
enum {
CHAR_BIT = 8
};
static const struct framebuffer_config *get_fb_config_from_format(int format)
{
static const struct framebuffer_config fb_configs[] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* Invalid, assume RGB_565 */
{ 4, 0, 8, 8, 8, 16, 8, 24, 8 }, /* HAL_PIXEL_FORMAT_RGBA_8888 */
{ 4, 0, 8, 8, 8, 16, 8, 0, 0 }, /* HAL_PIXEL_FORMAT_RGBX_8888 */
{ 3, 0, 8, 8, 8, 16, 8, 0, 0 }, /* HAL_PIXEL_FORMAT_RGB_888 */
{ 2, 11, 5, 5, 6, 0, 5, 0, 0 }, /* HAL_PIXEL_FORMAT_RGB_565 */
{ 4, 16, 8, 8, 8, 0, 8, 24, 8 }, /* HAL_PIXEL_FORMAT_BGRA_8888 */
};
if (format > 0 &&
format < sizeof(fb_configs) / sizeof(struct framebuffer_config)) {
return &fb_configs[format];
}
return &fb_configs[HAL_PIXEL_FORMAT_RGB_565]; /* legacy default */
}
struct goldfish_fb {
void __iomem *reg_base;
int irq;
spinlock_t lock;
wait_queue_head_t wait;
int base_update_count;
int rotation;
struct fb_info fb;
u32 cmap[16];
};
static irqreturn_t goldfish_fb_interrupt(int irq, void *dev_id)
{
unsigned long irq_flags;
struct goldfish_fb *fb = dev_id;
u32 status;
spin_lock_irqsave(&fb->lock, irq_flags);
status = readl(fb->reg_base + FB_INT_STATUS);
if (status & FB_INT_BASE_UPDATE_DONE) {
fb->base_update_count++;
wake_up(&fb->wait);
}
spin_unlock_irqrestore(&fb->lock, irq_flags);
return status ? IRQ_HANDLED : IRQ_NONE;
}
static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
{
unsigned int mask = (1 << bf->length) - 1;
return (val >> (16 - bf->length) & mask) << bf->offset;
}
static int
goldfish_fb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
unsigned int blue, unsigned int transp, struct fb_info *info)
{
struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
if (regno < 16) {
fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
convert_bitfield(blue, &fb->fb.var.blue) |
convert_bitfield(green, &fb->fb.var.green) |
convert_bitfield(red, &fb->fb.var.red);
return 0;
} else {
return 1;
}
}
static int goldfish_fb_check_var(struct fb_var_screeninfo *var,
struct fb_info *info)
{
if ((var->rotate & 1) != (info->var.rotate & 1)) {
if ((var->xres != info->var.yres) ||
(var->yres != info->var.xres) ||
(var->xres_virtual != info->var.yres) ||
(var->yres_virtual > info->var.xres * 2) ||
(var->yres_virtual < info->var.xres)) {
return -EINVAL;
}
} else {
if ((var->xres != info->var.xres) ||
(var->yres != info->var.yres) ||
(var->xres_virtual != info->var.xres) ||
(var->yres_virtual > info->var.yres * 2) ||
(var->yres_virtual < info->var.yres)) {
return -EINVAL;
}
}
if ((var->xoffset != info->var.xoffset) ||
(var->bits_per_pixel != info->var.bits_per_pixel) ||
(var->grayscale != info->var.grayscale)) {
return -EINVAL;
}
return 0;
}
static int goldfish_fb_set_par(struct fb_info *info)
{
struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
if (fb->rotation != fb->fb.var.rotate) {
info->fix.line_length = info->var.xres *
(fb->fb.var.bits_per_pixel / CHAR_BIT);
fb->rotation = fb->fb.var.rotate;
writel(fb->rotation, fb->reg_base + FB_SET_ROTATION);
}
return 0;
}
static int goldfish_fb_pan_display(struct fb_var_screeninfo *var,
struct fb_info *info)
{
unsigned long irq_flags;
int base_update_count;
struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
spin_lock_irqsave(&fb->lock, irq_flags);
base_update_count = fb->base_update_count;
writel(fb->fb.fix.smem_start +
fb->fb.var.xres *
(fb->fb.var.bits_per_pixel / CHAR_BIT) *
var->yoffset,
fb->reg_base + FB_SET_BASE);
spin_unlock_irqrestore(&fb->lock, irq_flags);
wait_event_timeout(fb->wait,
fb->base_update_count != base_update_count, HZ / 15);
if (fb->base_update_count == base_update_count)
pr_err("goldfish_fb_pan_display: timeout waiting for "
"base update\n");
return 0;
}
static int goldfish_fb_blank(int blank, struct fb_info *info)
{
struct goldfish_fb *fb = container_of(info, struct goldfish_fb, fb);
switch (blank) {
case FB_BLANK_NORMAL:
writel(1, fb->reg_base + FB_SET_BLANK);
break;
case FB_BLANK_UNBLANK:
writel(0, fb->reg_base + FB_SET_BLANK);
break;
}
return 0;
}
static struct fb_ops goldfish_fb_ops = {
.owner = THIS_MODULE,
.fb_check_var = goldfish_fb_check_var,
.fb_set_par = goldfish_fb_set_par,
.fb_setcolreg = goldfish_fb_setcolreg,
.fb_pan_display = goldfish_fb_pan_display,
.fb_blank = goldfish_fb_blank,
.fb_fillrect = cfb_fillrect,
.fb_copyarea = cfb_copyarea,
.fb_imageblit = cfb_imageblit,
};
static int goldfish_fb_probe(struct platform_device *pdev)
{
int ret;
struct resource *r;
struct goldfish_fb *fb;
size_t framesize;
u32 width, height, format;
int bytes_per_pixel;
dma_addr_t fbpaddr;
const struct framebuffer_config *fb_config;
fb = kzalloc(sizeof(*fb), GFP_KERNEL);
if (fb == NULL) {
ret = -ENOMEM;
goto err_fb_alloc_failed;
}
spin_lock_init(&fb->lock);
init_waitqueue_head(&fb->wait);
platform_set_drvdata(pdev, fb);
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (r == NULL) {
ret = -ENODEV;
goto err_no_io_base;
}
fb->reg_base = ioremap(r->start, PAGE_SIZE);
if (fb->reg_base == NULL) {
ret = -ENOMEM;
goto err_no_io_base;
}
fb->irq = platform_get_irq(pdev, 0);
if (fb->irq <= 0) {
ret = -ENODEV;
goto err_no_irq;
}
width = readl(fb->reg_base + FB_GET_WIDTH);
height = readl(fb->reg_base + FB_GET_HEIGHT);
format = readl(fb->reg_base + FB_GET_FORMAT);
fb_config = get_fb_config_from_format(format);
if (!fb_config) {
ret = -EINVAL;
goto err_no_irq;
}
bytes_per_pixel = fb_config->bytes_per_pixel;
fb->fb.fbops = &goldfish_fb_ops;
fb->fb.flags = FBINFO_FLAG_DEFAULT;
fb->fb.pseudo_palette = fb->cmap;
fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
fb->fb.fix.line_length = width * bytes_per_pixel;
fb->fb.fix.accel = FB_ACCEL_NONE;
fb->fb.fix.ypanstep = 1;
fb->fb.var.xres = width;
fb->fb.var.yres = height;
fb->fb.var.xres_virtual = width;
fb->fb.var.yres_virtual = height * 2;
fb->fb.var.bits_per_pixel = bytes_per_pixel * CHAR_BIT;
fb->fb.var.activate = FB_ACTIVATE_NOW;
fb->fb.var.height = readl(fb->reg_base + FB_GET_PHYS_HEIGHT);
fb->fb.var.width = readl(fb->reg_base + FB_GET_PHYS_WIDTH);
fb->fb.var.pixclock = 0;
fb->fb.var.red.offset = fb_config->red_offset;
fb->fb.var.red.length = fb_config->red_length;
fb->fb.var.green.offset = fb_config->green_offset;
fb->fb.var.green.length = fb_config->green_length;
fb->fb.var.blue.offset = fb_config->blue_offset;
fb->fb.var.blue.length = fb_config->blue_length;
fb->fb.var.transp.offset = fb_config->transp_offset;
fb->fb.var.transp.length = fb_config->transp_length;
framesize = width * height * 2 * bytes_per_pixel;
fb->fb.screen_base = (char __force __iomem *)dma_alloc_coherent(
&pdev->dev, framesize,
&fbpaddr, GFP_KERNEL);
pr_debug("allocating frame buffer %d * %d, got %p\n",
width, height, fb->fb.screen_base);
if (fb->fb.screen_base == NULL) {
ret = -ENOMEM;
goto err_alloc_screen_base_failed;
}
fb->fb.fix.smem_start = fbpaddr;
fb->fb.fix.smem_len = framesize;
ret = fb_set_var(&fb->fb, &fb->fb.var);
if (ret)
goto err_fb_set_var_failed;
ret = request_irq(fb->irq, goldfish_fb_interrupt, IRQF_SHARED,
pdev->name, fb);
if (ret)
goto err_request_irq_failed;
writel(FB_INT_BASE_UPDATE_DONE, fb->reg_base + FB_INT_ENABLE);
goldfish_fb_pan_display(&fb->fb.var, &fb->fb); /* updates base */
ret = register_framebuffer(&fb->fb);
if (ret)
goto err_register_framebuffer_failed;
return 0;
err_register_framebuffer_failed:
free_irq(fb->irq, fb);
err_request_irq_failed:
err_fb_set_var_failed:
dma_free_coherent(&pdev->dev, framesize,
(void *)fb->fb.screen_base,
fb->fb.fix.smem_start);
err_alloc_screen_base_failed:
err_no_irq:
iounmap(fb->reg_base);
err_no_io_base:
kfree(fb);
err_fb_alloc_failed:
return ret;
}
static int goldfish_fb_remove(struct platform_device *pdev)
{
size_t framesize;
struct goldfish_fb *fb = platform_get_drvdata(pdev);
framesize = fb->fb.var.xres_virtual * fb->fb.var.yres_virtual *
(fb->fb.var.bits_per_pixel / CHAR_BIT);
unregister_framebuffer(&fb->fb);
free_irq(fb->irq, fb);
dma_free_coherent(&pdev->dev, framesize, (void *)fb->fb.screen_base,
fb->fb.fix.smem_start);
iounmap(fb->reg_base);
kfree(fb);
return 0;
}
static const struct of_device_id goldfish_fb_of_match[] = {
{ .compatible = "google,goldfish-fb", },
{},
};
MODULE_DEVICE_TABLE(of, goldfish_fb_of_match);
static const struct acpi_device_id goldfish_fb_acpi_match[] = {
{ "GFSH0004", 0 },
{ },
};
MODULE_DEVICE_TABLE(acpi, goldfish_fb_acpi_match);
static struct platform_driver goldfish_fb_driver = {
.probe = goldfish_fb_probe,
.remove = goldfish_fb_remove,
.driver = {
.name = "goldfish_fb",
.of_match_table = goldfish_fb_of_match,
.acpi_match_table = ACPI_PTR(goldfish_fb_acpi_match),
}
};
module_platform_driver(goldfish_fb_driver);
MODULE_LICENSE("GPL v2");