fbdev: move FBIO_WAITFORVSYNC to linux/fb.h
[safe/jmp/linux-2.6] / drivers / video / atmel_lcdfb.c
index 8ffdf35..8dce251 100644 (file)
 #include <linux/init.h>
 #include <linux/delay.h>
 #include <linux/backlight.h>
+#include <linux/gfp.h>
 
-#include <asm/arch/board.h>
-#include <asm/arch/cpu.h>
-#include <asm/arch/gpio.h>
+#include <mach/board.h>
+#include <mach/cpu.h>
+#include <mach/gpio.h>
 
 #include <video/atmel_lcdc.h>
 
 
 /* configurable parameters */
 #define ATMEL_LCDC_CVAL_DEFAULT                0xc8
-#define ATMEL_LCDC_DMA_BURST_LEN       8
-
-#if defined(CONFIG_ARCH_AT91SAM9263) || defined(CONFIG_ARCH_AT91CAP9) || \
-       defined(CONFIG_ARCH_AT91SAM9RL)
-#define ATMEL_LCDC_FIFO_SIZE           2048
-#else
-#define ATMEL_LCDC_FIFO_SIZE           512
-#endif
+#define ATMEL_LCDC_DMA_BURST_LEN       8       /* words */
+#define ATMEL_LCDC_FIFO_SIZE           512     /* words */
 
 #if defined(CONFIG_ARCH_AT91)
-#define        ATMEL_LCDFB_FBINFO_DEFAULT      FBINFO_DEFAULT
+#define        ATMEL_LCDFB_FBINFO_DEFAULT      (FBINFO_DEFAULT \
+                                        | FBINFO_PARTIAL_PAN_OK \
+                                        | FBINFO_HWACCEL_YPAN)
 
 static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
                                        struct fb_var_screeninfo *var)
@@ -121,6 +118,7 @@ static struct backlight_ops atmel_lcdc_bl_ops = {
 
 static void init_backlight(struct atmel_lcdfb_info *sinfo)
 {
+       struct backlight_properties props;
        struct backlight_device *bl;
 
        sinfo->bl_power = FB_BLANK_UNBLANK;
@@ -128,9 +126,11 @@ static void init_backlight(struct atmel_lcdfb_info *sinfo)
        if (sinfo->backlight)
                return;
 
-       bl = backlight_device_register("backlight", &sinfo->pdev->dev,
-                       sinfo, &atmel_lcdc_bl_ops);
-       if (IS_ERR(sinfo->backlight)) {
+       memset(&props, 0, sizeof(struct backlight_properties));
+       props.max_brightness = 0xff;
+       bl = backlight_device_register("backlight", &sinfo->pdev->dev, sinfo,
+                                      &atmel_lcdc_bl_ops, &props);
+       if (IS_ERR(bl)) {
                dev_err(&sinfo->pdev->dev, "error %ld on backlight register\n",
                                PTR_ERR(bl));
                return;
@@ -139,7 +139,6 @@ static void init_backlight(struct atmel_lcdfb_info *sinfo)
 
        bl->props.power = FB_BLANK_UNBLANK;
        bl->props.fb_blank = FB_BLANK_UNBLANK;
-       bl->props.max_brightness = 0xff;
        bl->props.brightness = atmel_bl_get_brightness(bl);
 }
 
@@ -177,7 +176,7 @@ static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata = {
        .type           = FB_TYPE_PACKED_PIXELS,
        .visual         = FB_VISUAL_TRUECOLOR,
        .xpanstep       = 0,
-       .ypanstep       = 0,
+       .ypanstep       = 1,
        .ywrapstep      = 0,
        .accel          = FB_ACCEL_NONE,
 };
@@ -186,7 +185,8 @@ static unsigned long compute_hozval(unsigned long xres, unsigned long lcdcon2)
 {
        unsigned long value;
 
-       if (!(cpu_is_at91sam9261() || cpu_is_at32ap7000()))
+       if (!(cpu_is_at91sam9261() || cpu_is_at91sam9g10()
+               || cpu_is_at32ap7000()))
                return xres;
 
        value = xres;
@@ -206,6 +206,36 @@ static unsigned long compute_hozval(unsigned long xres, unsigned long lcdcon2)
        return value;
 }
 
+static void atmel_lcdfb_stop_nowait(struct atmel_lcdfb_info *sinfo)
+{
+       /* Turn off the LCD controller and the DMA controller */
+       lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
+                       sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET);
+
+       /* Wait for the LCDC core to become idle */
+       while (lcdc_readl(sinfo, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
+               msleep(10);
+
+       lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0);
+}
+
+static void atmel_lcdfb_stop(struct atmel_lcdfb_info *sinfo)
+{
+       atmel_lcdfb_stop_nowait(sinfo);
+
+       /* Wait for DMA engine to become idle... */
+       while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
+               msleep(10);
+}
+
+static void atmel_lcdfb_start(struct atmel_lcdfb_info *sinfo)
+{
+       lcdc_writel(sinfo, ATMEL_LCDC_DMACON, sinfo->default_dmacon);
+       lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
+               (sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET)
+               | ATMEL_LCDC_PWR);
+}
+
 static void atmel_lcdfb_update_dma(struct fb_info *info,
                               struct fb_var_screeninfo *var)
 {
@@ -235,14 +265,19 @@ static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info *sinfo)
 /**
  *     atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory
  *     @sinfo: the frame buffer to allocate memory for
+ *     
+ *     This function is called only from the atmel_lcdfb_probe()
+ *     so no locking by fb_info->mm_lock around smem_len setting is needed.
  */
 static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
 {
        struct fb_info *info = sinfo->info;
        struct fb_var_screeninfo *var = &info->var;
+       unsigned int smem_len;
 
-       info->fix.smem_len = (var->xres_virtual * var->yres_virtual
-                           * ((var->bits_per_pixel + 7) / 8));
+       smem_len = (var->xres_virtual * var->yres_virtual
+                   * ((var->bits_per_pixel + 7) / 8));
+       info->fix.smem_len = max(smem_len, sinfo->smem_len);
 
        info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len,
                                        (dma_addr_t *)&info->fix.smem_start, GFP_KERNEL);
@@ -256,6 +291,20 @@ static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
        return 0;
 }
 
+static const struct fb_videomode *atmel_lcdfb_choose_mode(struct fb_var_screeninfo *var,
+                                                    struct fb_info *info)
+{
+       struct fb_videomode varfbmode;
+       const struct fb_videomode *fbmode = NULL;
+
+       fb_var_to_videomode(&varfbmode, var);
+       fbmode = fb_find_nearest_mode(&varfbmode, &info->modelist);
+       if (fbmode)
+               fb_videomode_to_var(var, fbmode);
+       return fbmode;
+}
+
+
 /**
  *      atmel_lcdfb_check_var - Validates a var passed in.
  *      @var: frame buffer variable screen structure
@@ -289,16 +338,32 @@ static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
        clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
 
        dev_dbg(dev, "%s:\n", __func__);
+
+       if (!(var->pixclock && var->bits_per_pixel)) {
+               /* choose a suitable mode if possible */
+               if (!atmel_lcdfb_choose_mode(var, info)) {
+                       dev_err(dev, "needed value not specified\n");
+                       return -EINVAL;
+               }
+       }
+
        dev_dbg(dev, "  resolution: %ux%u\n", var->xres, var->yres);
        dev_dbg(dev, "  pixclk:     %lu KHz\n", PICOS2KHZ(var->pixclock));
        dev_dbg(dev, "  bpp:        %u\n", var->bits_per_pixel);
        dev_dbg(dev, "  clk:        %lu KHz\n", clk_value_khz);
 
-       if ((PICOS2KHZ(var->pixclock) * var->bits_per_pixel / 8) > clk_value_khz) {
+       if (PICOS2KHZ(var->pixclock) > clk_value_khz) {
                dev_err(dev, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var->pixclock));
                return -EINVAL;
        }
 
+       /* Do not allow to have real resoulution larger than virtual */
+       if (var->xres > var->xres_virtual)
+               var->xres_virtual = var->xres;
+
+       if (var->yres > var->yres_virtual)
+               var->yres_virtual = var->yres;
+
        /* Force same alignment for each line */
        var->xres = (var->xres + 3) & ~3UL;
        var->xres_virtual = (var->xres_virtual + 3) & ~3UL;
@@ -308,6 +373,13 @@ static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
        var->transp.offset = var->transp.length = 0;
        var->xoffset = var->yoffset = 0;
 
+       if (info->fix.smem_len) {
+               unsigned int smem_len = (var->xres_virtual * var->yres_virtual
+                                        * ((var->bits_per_pixel + 7) / 8));
+               if (smem_len > info->fix.smem_len)
+                       return -EINVAL;
+       }
+
        /* Saturate vertical and horizontal timings at maximum values */
        var->vsync_len = min_t(u32, var->vsync_len,
                        (ATMEL_LCDC_VPW >> ATMEL_LCDC_VPW_OFFSET) + 1);
@@ -344,6 +416,10 @@ static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
                        var->red.offset = 11;
                        var->blue.offset = 0;
                        var->green.length = 6;
+               } else if (sinfo->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB555) {
+                       var->red.offset = 10;
+                       var->blue.offset = 0;
+                       var->green.length = 5;
                } else {
                        /* BGR:555 mode */
                        var->red.offset = 0;
@@ -379,6 +455,17 @@ static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
        return 0;
 }
 
+/*
+ * LCD reset sequence
+ */
+static void atmel_lcdfb_reset(struct atmel_lcdfb_info *sinfo)
+{
+       might_sleep();
+
+       atmel_lcdfb_stop(sinfo);
+       atmel_lcdfb_start(sinfo);
+}
+
 /**
  *      atmel_lcdfb_set_par - Alters the hardware state.
  *      @info: frame buffer structure that represents a single frame buffer
@@ -400,20 +487,16 @@ static int atmel_lcdfb_set_par(struct fb_info *info)
        unsigned long value;
        unsigned long clk_value_khz;
        unsigned long bits_per_line;
+       unsigned long pix_factor = 2;
+
+       might_sleep();
 
        dev_dbg(info->device, "%s:\n", __func__);
        dev_dbg(info->device, "  * resolution: %ux%u (%ux%u virtual)\n",
                 info->var.xres, info->var.yres,
                 info->var.xres_virtual, info->var.yres_virtual);
 
-       /* Turn off the LCD controller and the DMA controller */
-       lcdc_writel(sinfo, ATMEL_LCDC_PWRCON, sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET);
-
-       /* Wait for the LCDC core to become idle */
-       while (lcdc_readl(sinfo, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
-               msleep(10);
-
-       lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0);
+       atmel_lcdfb_stop_nowait(sinfo);
 
        if (info->var.bits_per_pixel == 1)
                info->fix.visual = FB_VISUAL_MONO01;
@@ -437,19 +520,24 @@ static int atmel_lcdfb_set_par(struct fb_info *info)
        /* Now, the LCDC core... */
 
        /* Set pixel clock */
+       if (cpu_is_at91sam9g45() && !cpu_is_at91sam9g45es())
+               pix_factor = 1;
+
        clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
 
        value = DIV_ROUND_UP(clk_value_khz, PICOS2KHZ(info->var.pixclock));
 
-       value = (value / 2) - 1;
-       dev_dbg(info->device, "  * programming CLKVAL = 0x%08lx\n", value);
-
-       if (value <= 0) {
+       if (value < pix_factor) {
                dev_notice(info->device, "Bypassing pixel clock divider\n");
                lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
        } else {
-               lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, value << ATMEL_LCDC_CLKVAL_OFFSET);
-               info->var.pixclock = KHZ2PICOS(clk_value_khz / (2 * (value + 1)));
+               value = (value / pix_factor) - 1;
+               dev_dbg(info->device, "  * programming CLKVAL = 0x%08lx\n",
+                               value);
+               lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1,
+                               value << ATMEL_LCDC_CLKVAL_OFFSET);
+               info->var.pixclock =
+                       KHZ2PICOS(clk_value_khz / (pix_factor * (value + 1)));
                dev_dbg(info->device, "  updated pixclk:     %lu KHz\n",
                                        PICOS2KHZ(info->var.pixclock));
        }
@@ -510,18 +598,14 @@ static int atmel_lcdfb_set_par(struct fb_info *info)
 
        /* Disable all interrupts */
        lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
+       /* Enable FIFO & DMA errors */
+       lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
 
        /* ...wait for DMA engine to become idle... */
        while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
                msleep(10);
 
-       dev_dbg(info->device, "  * re-enable DMA engine\n");
-       /* ...and enable it with updated configuration */
-       lcdc_writel(sinfo, ATMEL_LCDC_DMACON, sinfo->default_dmacon);
-
-       dev_dbg(info->device, "  * re-enable LCDC core\n");
-       lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
-               (sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET) | ATMEL_LCDC_PWR);
+       atmel_lcdfb_start(sinfo);
 
        dev_dbg(info->device, "  * DONE\n");
 
@@ -644,10 +728,26 @@ static irqreturn_t atmel_lcdfb_interrupt(int irq, void *dev_id)
        u32 status;
 
        status = lcdc_readl(sinfo, ATMEL_LCDC_ISR);
-       lcdc_writel(sinfo, ATMEL_LCDC_IDR, status);
+       if (status & ATMEL_LCDC_UFLWI) {
+               dev_warn(info->device, "FIFO underflow %#x\n", status);
+               /* reset DMA and FIFO to avoid screen shifting */
+               schedule_work(&sinfo->task);
+       }
+       lcdc_writel(sinfo, ATMEL_LCDC_ICR, status);
        return IRQ_HANDLED;
 }
 
+/*
+ * LCD controller task (to reset the LCD)
+ */
+static void atmel_lcdfb_task(struct work_struct *work)
+{
+       struct atmel_lcdfb_info *sinfo =
+               container_of(work, struct atmel_lcdfb_info, task);
+
+       atmel_lcdfb_reset(sinfo);
+}
+
 static int __init atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info *sinfo)
 {
        struct fb_info *info = sinfo->info;
@@ -690,6 +790,7 @@ static int __init atmel_lcdfb_probe(struct platform_device *pdev)
        struct fb_info *info;
        struct atmel_lcdfb_info *sinfo;
        struct atmel_lcdfb_info *pdata_sinfo;
+       struct fb_videomode fbmode;
        struct resource *regs = NULL;
        struct resource *map = NULL;
        int ret;
@@ -713,6 +814,7 @@ static int __init atmel_lcdfb_probe(struct platform_device *pdev)
                sinfo->default_monspecs = pdata_sinfo->default_monspecs;
                sinfo->atmel_lcdfb_power_control = pdata_sinfo->atmel_lcdfb_power_control;
                sinfo->guard_time = pdata_sinfo->guard_time;
+               sinfo->smem_len = pdata_sinfo->smem_len;
                sinfo->lcdcon_is_backlight = pdata_sinfo->lcdcon_is_backlight;
                sinfo->lcd_wiring_mode = pdata_sinfo->lcd_wiring_mode;
        } else {
@@ -731,7 +833,8 @@ static int __init atmel_lcdfb_probe(struct platform_device *pdev)
        info->fix = atmel_lcdfb_fix;
 
        /* Enable LCDC Clocks */
-       if (cpu_is_at91sam9261() || cpu_is_at32ap7000()) {
+       if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()
+        || cpu_is_at32ap7000()) {
                sinfo->bus_clk = clk_get(dev, "hck1");
                if (IS_ERR(sinfo->bus_clk)) {
                        ret = PTR_ERR(sinfo->bus_clk);
@@ -823,6 +926,10 @@ static int __init atmel_lcdfb_probe(struct platform_device *pdev)
                goto unmap_mmio;
        }
 
+       /* Some operations on the LCDC might sleep and
+        * require a preemptible task context */
+       INIT_WORK(&sinfo->task, atmel_lcdfb_task);
+
        ret = atmel_lcdfb_init_fbinfo(sinfo);
        if (ret < 0) {
                dev_err(dev, "init fbinfo failed: %d\n", ret);
@@ -849,22 +956,28 @@ static int __init atmel_lcdfb_probe(struct platform_device *pdev)
        ret = register_framebuffer(info);
        if (ret < 0) {
                dev_err(dev, "failed to register framebuffer device: %d\n", ret);
-               goto free_cmap;
+               goto reset_drvdata;
        }
 
+       /* add selected videomode to modelist */
+       fb_var_to_videomode(&fbmode, &info->var);
+       fb_add_videomode(&fbmode, &info->modelist);
+
        /* Power up the LCDC screen */
        if (sinfo->atmel_lcdfb_power_control)
                sinfo->atmel_lcdfb_power_control(1);
 
-       dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %lu\n",
+       dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %d\n",
                       info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base);
 
        return 0;
 
-
+reset_drvdata:
+       dev_set_drvdata(dev, NULL);
 free_cmap:
        fb_dealloc_cmap(&info->cmap);
 unregister_irqs:
+       cancel_work_sync(&sinfo->task);
        free_irq(sinfo->irq_base, info);
 unmap_mmio:
        exit_backlight(sinfo);
@@ -897,11 +1010,13 @@ static int __exit atmel_lcdfb_remove(struct platform_device *pdev)
 {
        struct device *dev = &pdev->dev;
        struct fb_info *info = dev_get_drvdata(dev);
-       struct atmel_lcdfb_info *sinfo = info->par;
+       struct atmel_lcdfb_info *sinfo;
 
-       if (!sinfo)
+       if (!info || !info->par)
                return 0;
+       sinfo = info->par;
 
+       cancel_work_sync(&sinfo->task);
        exit_backlight(sinfo);
        if (sinfo->atmel_lcdfb_power_control)
                sinfo->atmel_lcdfb_power_control(0);
@@ -934,11 +1049,20 @@ static int atmel_lcdfb_suspend(struct platform_device *pdev, pm_message_t mesg)
        struct fb_info *info = platform_get_drvdata(pdev);
        struct atmel_lcdfb_info *sinfo = info->par;
 
+       /*
+        * We don't want to handle interrupts while the clock is
+        * stopped. It may take forever.
+        */
+       lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
+
        sinfo->saved_lcdcon = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
        lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, 0);
        if (sinfo->atmel_lcdfb_power_control)
                sinfo->atmel_lcdfb_power_control(0);
+
+       atmel_lcdfb_stop(sinfo);
        atmel_lcdfb_stop_clock(sinfo);
+
        return 0;
 }
 
@@ -948,9 +1072,15 @@ static int atmel_lcdfb_resume(struct platform_device *pdev)
        struct atmel_lcdfb_info *sinfo = info->par;
 
        atmel_lcdfb_start_clock(sinfo);
+       atmel_lcdfb_start(sinfo);
        if (sinfo->atmel_lcdfb_power_control)
                sinfo->atmel_lcdfb_power_control(1);
        lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, sinfo->saved_lcdcon);
+
+       /* Enable FIFO & DMA errors */
+       lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI
+                       | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
+
        return 0;
 }