fbdev: move FBIO_WAITFORVSYNC to linux/fb.h
[safe/jmp/linux-2.6] / drivers / video / da8xx-fb.c
index e0bbc66..cad7d45 100644 (file)
 #include <linux/device.h>
 #include <linux/platform_device.h>
 #include <linux/uaccess.h>
-#include <linux/device.h>
 #include <linux/interrupt.h>
 #include <linux/clk.h>
+#include <linux/cpufreq.h>
+#include <linux/console.h>
+#include <linux/slab.h>
 #include <video/da8xx-fb.h>
 
 #define DRIVER_NAME "da8xx_lcdc"
 
 /* LCD Status Register */
+#define LCD_END_OF_FRAME1              BIT(9)
 #define LCD_END_OF_FRAME0              BIT(8)
+#define LCD_PL_LOAD_DONE               BIT(6)
 #define LCD_FIFO_UNDERFLOW             BIT(5)
 #define LCD_SYNC_LOST                  BIT(2)
 
 #define LCD_PALETTE_LOAD_MODE(x)       ((x) << 20)
 #define PALETTE_AND_DATA               0x00
 #define PALETTE_ONLY                   0x01
+#define DATA_ONLY                      0x02
 
 #define LCD_MONO_8BIT_MODE             BIT(9)
 #define LCD_RASTER_ORDER               BIT(8)
 #define LCD_TFT_MODE                   BIT(7)
 #define LCD_UNDERFLOW_INT_ENA          BIT(6)
+#define LCD_PL_ENABLE                  BIT(4)
 #define LCD_MONOCHROME_MODE            BIT(1)
 #define LCD_RASTER_ENABLE              BIT(0)
 #define LCD_TFT_ALT_ENABLE             BIT(23)
 #define  LCD_DMA_CTRL_REG                      0x40
 #define  LCD_DMA_FRM_BUF_BASE_ADDR_0_REG       0x44
 #define  LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG    0x48
+#define  LCD_DMA_FRM_BUF_BASE_ADDR_1_REG       0x4C
+#define  LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG    0x50
+
+#define LCD_NUM_BUFFERS        2
 
 #define WSI_TIMEOUT    50
 #define PALETTE_SIZE   256
@@ -107,14 +117,26 @@ static inline void lcdc_write(unsigned int val, unsigned int addr)
 }
 
 struct da8xx_fb_par {
-       wait_queue_head_t da8xx_wq;
        resource_size_t p_palette_base;
        unsigned char *v_palette_base;
+       dma_addr_t              vram_phys;
+       unsigned long           vram_size;
+       void                    *vram_virt;
+       unsigned int            dma_start;
+       unsigned int            dma_end;
        struct clk *lcdc_clk;
        int irq;
        unsigned short pseudo_palette[16];
-       unsigned int databuf_sz;
        unsigned int palette_sz;
+       unsigned int pxl_clk;
+       int blank;
+       wait_queue_head_t       vsync_wait;
+       int                     vsync_flag;
+       int                     vsync_timeout;
+#ifdef CONFIG_CPU_FREQ
+       struct notifier_block   freq_transition;
+#endif
+       void (*panel_power_ctrl)(int);
 };
 
 /* Variable Screen Information */
@@ -141,9 +163,9 @@ static struct fb_fix_screeninfo da8xx_fb_fix __devinitdata = {
        .type = FB_TYPE_PACKED_PIXELS,
        .type_aux = 0,
        .visual = FB_VISUAL_PSEUDOCOLOR,
-       .xpanstep = 1,
+       .xpanstep = 0,
        .ypanstep = 1,
-       .ywrapstep = 1,
+       .ywrapstep = 0,
        .accel = FB_ACCEL_NONE
 };
 
@@ -157,7 +179,8 @@ struct da8xx_panel {
        int             vfp;            /* Vertical front porch */
        int             vbp;            /* Vertical back porch */
        int             vsw;            /* Vertical Sync Pulse Width */
-       int             pxl_clk;        /* Pixel clock */
+       unsigned int    pxl_clk;        /* Pixel clock */
+       unsigned char   invert_pxl_clk; /* Invert Pixel clock */
 };
 
 static struct da8xx_panel known_lcd_panels[] = {
@@ -172,7 +195,8 @@ static struct da8xx_panel known_lcd_panels[] = {
                .vfp = 2,
                .vbp = 2,
                .vsw = 0,
-               .pxl_clk = 0x10,
+               .pxl_clk = 4608000,
+               .invert_pxl_clk = 1,
        },
        /* Sharp LK043T1DG01 */
        [1] = {
@@ -185,50 +209,75 @@ static struct da8xx_panel known_lcd_panels[] = {
                .vfp = 2,
                .vbp = 2,
                .vsw = 10,
-               .pxl_clk = 0x12,
+               .pxl_clk = 7833600,
+               .invert_pxl_clk = 0,
        },
 };
 
-/* Disable the Raster Engine of the LCD Controller */
-static int lcd_disable_raster(struct da8xx_fb_par *par)
+/* Enable the Raster Engine of the LCD Controller */
+static inline void lcd_enable_raster(void)
 {
-       int ret = 0;
        u32 reg;
 
        reg = lcdc_read(LCD_RASTER_CTRL_REG);
-       if (reg & LCD_RASTER_ENABLE) {
-               lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
-               ret = wait_event_interruptible_timeout(par->da8xx_wq,
-                                               !lcdc_read(LCD_STAT_REG) &
-                                               LCD_END_OF_FRAME0, WSI_TIMEOUT);
-       }
+       if (!(reg & LCD_RASTER_ENABLE))
+               lcdc_write(reg | LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
+}
 
-       if (ret < 0)
-               return ret;
-       if (ret == 0)
-               return -ETIMEDOUT;
+/* Disable the Raster Engine of the LCD Controller */
+static inline void lcd_disable_raster(void)
+{
+       u32 reg;
 
-       return 0;
+       reg = lcdc_read(LCD_RASTER_CTRL_REG);
+       if (reg & LCD_RASTER_ENABLE)
+               lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
 }
 
 static void lcd_blit(int load_mode, struct da8xx_fb_par *par)
 {
-       u32 tmp = par->p_palette_base + par->databuf_sz - 4;
-       u32 reg;
+       u32 start;
+       u32 end;
+       u32 reg_ras;
+       u32 reg_dma;
+
+       /* init reg to clear PLM (loading mode) fields */
+       reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
+       reg_ras &= ~(3 << 20);
+
+       reg_dma  = lcdc_read(LCD_DMA_CTRL_REG);
+
+       if (load_mode == LOAD_DATA) {
+               start    = par->dma_start;
+               end      = par->dma_end;
+
+               reg_ras |= LCD_PALETTE_LOAD_MODE(DATA_ONLY);
+               reg_dma |= LCD_END_OF_FRAME_INT_ENA;
+               reg_dma |= LCD_DUAL_FRAME_BUFFER_ENABLE;
+
+               lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
+               lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
+               lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
+               lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
+       } else if (load_mode == LOAD_PALETTE) {
+               start    = par->p_palette_base;
+               end      = start + par->palette_sz - 1;
+
+               reg_ras |= LCD_PALETTE_LOAD_MODE(PALETTE_ONLY);
+               reg_ras |= LCD_PL_ENABLE;
+
+               lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
+               lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
+       }
 
-       /* Update the databuf in the hw. */
-       lcdc_write(par->p_palette_base, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
-       lcdc_write(tmp, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
+       lcdc_write(reg_dma, LCD_DMA_CTRL_REG);
+       lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
 
-       /* Start the DMA. */
-       reg = lcdc_read(LCD_RASTER_CTRL_REG);
-       reg &= ~(3 << 20);
-       if (load_mode == LOAD_DATA)
-               reg |= LCD_PALETTE_LOAD_MODE(PALETTE_AND_DATA);
-       else if (load_mode == LOAD_PALETTE)
-               reg |= LCD_PALETTE_LOAD_MODE(PALETTE_ONLY);
-
-       lcdc_write(reg, LCD_RASTER_CTRL_REG);
+       /*
+        * The Raster enable bit must be set after all other control fields are
+        * set.
+        */
+       lcd_enable_raster();
 }
 
 /* Configure the Burst Size of DMA */
@@ -256,7 +305,7 @@ static int lcd_cfg_dma(int burst_size)
        default:
                return -EINVAL;
        }
-       lcdc_write(reg | LCD_END_OF_FRAME_INT_ENA, LCD_DMA_CTRL_REG);
+       lcdc_write(reg, LCD_DMA_CTRL_REG);
 
        return 0;
 }
@@ -342,11 +391,6 @@ static int lcd_cfg_display(const struct lcd_ctrl_config *cfg)
        else
                reg &= ~LCD_SYNC_EDGE;
 
-       if (cfg->invert_pxl_clock)
-               reg |= LCD_INVERT_PIXEL_CLOCK;
-       else
-               reg &= ~LCD_INVERT_PIXEL_CLOCK;
-
        if (cfg->invert_line_clock)
                reg |= LCD_INVERT_LINE_CLOCK;
        else
@@ -365,12 +409,8 @@ static int lcd_cfg_display(const struct lcd_ctrl_config *cfg)
 static int lcd_cfg_frame_buffer(struct da8xx_fb_par *par, u32 width, u32 height,
                u32 bpp, u32 raster_order)
 {
-       u32 bpl, reg;
+       u32 reg;
 
-       /* Disable Dual Frame Buffer. */
-       reg = lcdc_read(LCD_DMA_CTRL_REG);
-       lcdc_write(reg & ~LCD_DUAL_FRAME_BUFFER_ENABLE,
-                                               LCD_DMA_CTRL_REG);
        /* Set the Panel Width */
        /* Pixels per line = (PPL + 1)*16 */
        /*0x3F in bits 4..9 gives max horisontal resolution = 1024 pixels*/
@@ -407,9 +447,6 @@ static int lcd_cfg_frame_buffer(struct da8xx_fb_par *par, u32 width, u32 height,
                return -EINVAL;
        }
 
-       bpl = width * bpp / 8;
-       par->databuf_sz = height * bpl + par->palette_sz;
-
        return 0;
 }
 
@@ -418,8 +455,9 @@ static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
                              struct fb_info *info)
 {
        struct da8xx_fb_par *par = info->par;
-       unsigned short *palette = (unsigned short *)par->v_palette_base;
+       unsigned short *palette = (unsigned short *) par->v_palette_base;
        u_short pal;
+       int update_hw = 0;
 
        if (regno > 255)
                return 1;
@@ -436,8 +474,10 @@ static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
                pal |= (green & 0x00f0);
                pal |= (blue & 0x000f);
 
-               palette[regno] = pal;
-
+               if (palette[regno] != pal) {
+                       update_hw = 1;
+                       palette[regno] = pal;
+               }
        } else if ((info->var.bits_per_pixel == 16) && regno < 16) {
                red >>= (16 - info->var.red.length);
                red <<= info->var.red.offset;
@@ -450,25 +490,39 @@ static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
 
                par->pseudo_palette[regno] = red | green | blue;
 
-               palette[0] = 0x4000;
+               if (palette[0] != 0x4000) {
+                       update_hw = 1;
+                       palette[0] = 0x4000;
+               }
        }
 
+       /* Update the palette in the h/w as needed. */
+       if (update_hw)
+               lcd_blit(LOAD_PALETTE, par);
+
        return 0;
 }
 
-static int lcd_reset(struct da8xx_fb_par *par)
+static void lcd_reset(struct da8xx_fb_par *par)
 {
-       int ret = 0;
-
        /* Disable the Raster if previously Enabled */
-       if (lcdc_read(LCD_RASTER_CTRL_REG) & LCD_RASTER_ENABLE)
-               ret = lcd_disable_raster(par);
+       lcd_disable_raster();
 
        /* DMA has to be disabled */
        lcdc_write(0, LCD_DMA_CTRL_REG);
        lcdc_write(0, LCD_RASTER_CTRL_REG);
+}
 
-       return ret;
+static void lcd_calc_clk_divider(struct da8xx_fb_par *par)
+{
+       unsigned int lcd_clk, div;
+
+       lcd_clk = clk_get_rate(par->lcdc_clk);
+       div = lcd_clk / par->pxl_clk;
+
+       /* Configure the LCD clock divisor. */
+       lcdc_write(LCD_CLK_DIVISOR(div) |
+                       (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG);
 }
 
 static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
@@ -477,13 +531,17 @@ static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
        u32 bpp;
        int ret = 0;
 
-       ret = lcd_reset(par);
-       if (ret != 0)
-               return ret;
+       lcd_reset(par);
 
-       /* Configure the LCD clock divisor. */
-       lcdc_write(LCD_CLK_DIVISOR(panel->pxl_clk) |
-                       (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG);
+       /* Calculate the divider */
+       lcd_calc_clk_divider(par);
+
+       if (panel->invert_pxl_clk)
+               lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) |
+                       LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
+       else
+               lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) &
+                       ~LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
 
        /* Configure the DMA burst size. */
        ret = lcd_cfg_dma(cfg->dma_burst_sz);
@@ -527,19 +585,54 @@ static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
 
 static irqreturn_t lcdc_irq_handler(int irq, void *arg)
 {
-       u32 stat = lcdc_read(LCD_STAT_REG);
        struct da8xx_fb_par *par = arg;
-       u32 reg;
+       u32 stat = lcdc_read(LCD_STAT_REG);
+       u32 reg_ras;
 
        if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
-               reg = lcdc_read(LCD_RASTER_CTRL_REG);
-               lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
+               lcd_disable_raster();
                lcdc_write(stat, LCD_STAT_REG);
-               lcdc_write(reg | LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
-       } else
+               lcd_enable_raster();
+       } else if (stat & LCD_PL_LOAD_DONE) {
+               /*
+                * Must disable raster before changing state of any control bit.
+                * And also must be disabled before clearing the PL loading
+                * interrupt via the following write to the status register. If
+                * this is done after then one gets multiple PL done interrupts.
+                */
+               lcd_disable_raster();
+
                lcdc_write(stat, LCD_STAT_REG);
 
-       wake_up_interruptible(&par->da8xx_wq);
+               /* Disable PL completion inerrupt */
+               reg_ras  = lcdc_read(LCD_RASTER_CTRL_REG);
+               reg_ras &= ~LCD_PL_ENABLE;
+               lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
+
+               /* Setup and start data loading mode */
+               lcd_blit(LOAD_DATA, par);
+       } else {
+               lcdc_write(stat, LCD_STAT_REG);
+
+               if (stat & LCD_END_OF_FRAME0) {
+                       lcdc_write(par->dma_start,
+                                  LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
+                       lcdc_write(par->dma_end,
+                                  LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
+                       par->vsync_flag = 1;
+                       wake_up_interruptible(&par->vsync_wait);
+               }
+
+               if (stat & LCD_END_OF_FRAME1) {
+                       lcdc_write(par->dma_start,
+                                  LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
+                       lcdc_write(par->dma_end,
+                                  LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
+                       par->vsync_flag = 1;
+                       wake_up_interruptible(&par->vsync_wait);
+               }
+       }
+
        return IRQ_HANDLED;
 }
 
@@ -571,11 +664,11 @@ static int fb_check_var(struct fb_var_screeninfo *var,
                var->transp.length = 0;
                break;
        case 16:                /* RGB 565 */
-               var->red.offset = 0;
+               var->red.offset = 11;
                var->red.length = 5;
                var->green.offset = 5;
                var->green.length = 6;
-               var->blue.offset = 11;
+               var->blue.offset = 0;
                var->blue.length = 5;
                var->transp.offset = 0;
                var->transp.length = 0;
@@ -591,16 +684,52 @@ static int fb_check_var(struct fb_var_screeninfo *var,
        return err;
 }
 
+#ifdef CONFIG_CPU_FREQ
+static int lcd_da8xx_cpufreq_transition(struct notifier_block *nb,
+                                    unsigned long val, void *data)
+{
+       struct da8xx_fb_par *par;
+
+       par = container_of(nb, struct da8xx_fb_par, freq_transition);
+       if (val == CPUFREQ_PRECHANGE) {
+               lcd_disable_raster();
+       } else if (val == CPUFREQ_POSTCHANGE) {
+               lcd_calc_clk_divider(par);
+               lcd_enable_raster();
+       }
+
+       return 0;
+}
+
+static inline int lcd_da8xx_cpufreq_register(struct da8xx_fb_par *par)
+{
+       par->freq_transition.notifier_call = lcd_da8xx_cpufreq_transition;
+
+       return cpufreq_register_notifier(&par->freq_transition,
+                                        CPUFREQ_TRANSITION_NOTIFIER);
+}
+
+static inline void lcd_da8xx_cpufreq_deregister(struct da8xx_fb_par *par)
+{
+       cpufreq_unregister_notifier(&par->freq_transition,
+                                   CPUFREQ_TRANSITION_NOTIFIER);
+}
+#endif
+
 static int __devexit fb_remove(struct platform_device *dev)
 {
        struct fb_info *info = dev_get_drvdata(&dev->dev);
-       int ret = 0;
 
        if (info) {
                struct da8xx_fb_par *par = info->par;
 
-               if (lcdc_read(LCD_RASTER_CTRL_REG) & LCD_RASTER_ENABLE)
-                       ret = lcd_disable_raster(par);
+#ifdef CONFIG_CPU_FREQ
+               lcd_da8xx_cpufreq_deregister(par);
+#endif
+               if (par->panel_power_ctrl)
+                       par->panel_power_ctrl(0);
+
+               lcd_disable_raster();
                lcdc_write(0, LCD_RASTER_CTRL_REG);
 
                /* disable DMA  */
@@ -608,9 +737,10 @@ static int __devexit fb_remove(struct platform_device *dev)
 
                unregister_framebuffer(info);
                fb_dealloc_cmap(&info->cmap);
-               dma_free_coherent(NULL, par->databuf_sz + PAGE_SIZE,
-                                       info->screen_base,
-                                       info->fix.smem_start);
+               dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
+                                 par->p_palette_base);
+               dma_free_coherent(NULL, par->vram_size, par->vram_virt,
+                                 par->vram_phys);
                free_irq(par->irq, par);
                clk_disable(par->lcdc_clk);
                clk_put(par->lcdc_clk);
@@ -619,7 +749,40 @@ static int __devexit fb_remove(struct platform_device *dev)
                release_mem_region(lcdc_regs->start, resource_size(lcdc_regs));
 
        }
-       return ret;
+       return 0;
+}
+
+/*
+ * Function to wait for vertical sync which for this LCD peripheral
+ * translates into waiting for the current raster frame to complete.
+ */
+static int fb_wait_for_vsync(struct fb_info *info)
+{
+       struct da8xx_fb_par *par = info->par;
+       int ret;
+
+       /*
+        * Set flag to 0 and wait for isr to set to 1. It would seem there is a
+        * race condition here where the ISR could have occured just before or
+        * just after this set. But since we are just coarsely waiting for
+        * a frame to complete then that's OK. i.e. if the frame completed
+        * just before this code executed then we have to wait another full
+        * frame time but there is no way to avoid such a situation. On the
+        * other hand if the frame completed just after then we don't need
+        * to wait long at all. Either way we are guaranteed to return to the
+        * user immediately after a frame completion which is all that is
+        * required.
+        */
+       par->vsync_flag = 0;
+       ret = wait_event_interruptible_timeout(par->vsync_wait,
+                                              par->vsync_flag != 0,
+                                              par->vsync_timeout);
+       if (ret < 0)
+               return ret;
+       if (ret == 0)
+               return -ETIMEDOUT;
+
+       return 0;
 }
 
 static int fb_ioctl(struct fb_info *info, unsigned int cmd,
@@ -634,11 +797,11 @@ static int fb_ioctl(struct fb_info *info, unsigned int cmd,
        case FBIPUT_BRIGHTNESS:
        case FBIGET_COLOR:
        case FBIPUT_COLOR:
-               return -EINVAL;
+               return -ENOTTY;
        case FBIPUT_HSYNC:
                if (copy_from_user(&sync_arg, (char *)arg,
                                sizeof(struct lcd_sync_arg)))
-                       return -EINVAL;
+                       return -EFAULT;
                lcd_cfg_horizontal_sync(sync_arg.back_porch,
                                        sync_arg.pulse_width,
                                        sync_arg.front_porch);
@@ -646,25 +809,94 @@ static int fb_ioctl(struct fb_info *info, unsigned int cmd,
        case FBIPUT_VSYNC:
                if (copy_from_user(&sync_arg, (char *)arg,
                                sizeof(struct lcd_sync_arg)))
-                       return -EINVAL;
+                       return -EFAULT;
                lcd_cfg_vertical_sync(sync_arg.back_porch,
                                        sync_arg.pulse_width,
                                        sync_arg.front_porch);
                break;
+       case FBIO_WAITFORVSYNC:
+               return fb_wait_for_vsync(info);
        default:
                return -EINVAL;
        }
        return 0;
 }
 
+static int cfb_blank(int blank, struct fb_info *info)
+{
+       struct da8xx_fb_par *par = info->par;
+       int ret = 0;
+
+       if (par->blank == blank)
+               return 0;
+
+       par->blank = blank;
+       switch (blank) {
+       case FB_BLANK_UNBLANK:
+               if (par->panel_power_ctrl)
+                       par->panel_power_ctrl(1);
+
+               lcd_enable_raster();
+               break;
+       case FB_BLANK_POWERDOWN:
+               if (par->panel_power_ctrl)
+                       par->panel_power_ctrl(0);
+
+               lcd_disable_raster();
+               break;
+       default:
+               ret = -EINVAL;
+       }
+
+       return ret;
+}
+
+/*
+ * Set new x,y offsets in the virtual display for the visible area and switch
+ * to the new mode.
+ */
+static int da8xx_pan_display(struct fb_var_screeninfo *var,
+                            struct fb_info *fbi)
+{
+       int ret = 0;
+       struct fb_var_screeninfo new_var;
+       struct da8xx_fb_par         *par = fbi->par;
+       struct fb_fix_screeninfo    *fix = &fbi->fix;
+       unsigned int end;
+       unsigned int start;
+
+       if (var->xoffset != fbi->var.xoffset ||
+                       var->yoffset != fbi->var.yoffset) {
+               memcpy(&new_var, &fbi->var, sizeof(new_var));
+               new_var.xoffset = var->xoffset;
+               new_var.yoffset = var->yoffset;
+               if (fb_check_var(&new_var, fbi))
+                       ret = -EINVAL;
+               else {
+                       memcpy(&fbi->var, &new_var, sizeof(new_var));
+
+                       start   = fix->smem_start +
+                               new_var.yoffset * fix->line_length +
+                               new_var.xoffset * var->bits_per_pixel / 8;
+                       end     = start + var->yres * fix->line_length - 1;
+                       par->dma_start  = start;
+                       par->dma_end    = end;
+               }
+       }
+
+       return ret;
+}
+
 static struct fb_ops da8xx_fb_ops = {
        .owner = THIS_MODULE,
        .fb_check_var = fb_check_var,
        .fb_setcolreg = fb_setcolreg,
+       .fb_pan_display = da8xx_pan_display,
        .fb_ioctl = fb_ioctl,
        .fb_fillrect = cfb_fillrect,
        .fb_copyarea = cfb_copyarea,
        .fb_imageblit = cfb_imageblit,
+       .fb_blank = cfb_blank,
 };
 
 static int __init fb_probe(struct platform_device *device)
@@ -722,7 +954,7 @@ static int __init fb_probe(struct platform_device *device)
 
        if (i == ARRAY_SIZE(known_lcd_panels)) {
                dev_err(&device->dev, "GLCD: No valid panel found\n");
-               ret = ENODEV;
+               ret = -ENODEV;
                goto err_clk_disable;
        } else
                dev_info(&device->dev, "GLCD: Found %s panel\n",
@@ -739,6 +971,12 @@ static int __init fb_probe(struct platform_device *device)
        }
 
        par = da8xx_fb_info->par;
+       par->lcdc_clk = fb_clk;
+       par->pxl_clk = lcdc_info->pxl_clk;
+       if (fb_pdata->panel_power_ctrl) {
+               par->panel_power_ctrl = fb_pdata->panel_power_ctrl;
+               par->panel_power_ctrl(1);
+       }
 
        if (lcd_init(par, lcd_cfg, lcdc_info) < 0) {
                dev_err(&device->dev, "lcd_init failed\n");
@@ -747,43 +985,53 @@ static int __init fb_probe(struct platform_device *device)
        }
 
        /* allocate frame buffer */
-       da8xx_fb_info->screen_base = dma_alloc_coherent(NULL,
-                                       par->databuf_sz + PAGE_SIZE,
-                                       (resource_size_t *)
-                                       &da8xx_fb_info->fix.smem_start,
-                                       GFP_KERNEL | GFP_DMA);
-
-       if (!da8xx_fb_info->screen_base) {
+       par->vram_size = lcdc_info->width * lcdc_info->height * lcd_cfg->bpp;
+       par->vram_size = PAGE_ALIGN(par->vram_size/8);
+       par->vram_size = par->vram_size * LCD_NUM_BUFFERS;
+
+       par->vram_virt = dma_alloc_coherent(NULL,
+                                           par->vram_size,
+                                           (resource_size_t *) &par->vram_phys,
+                                           GFP_KERNEL | GFP_DMA);
+       if (!par->vram_virt) {
                dev_err(&device->dev,
                        "GLCD: kmalloc for frame buffer failed\n");
                ret = -EINVAL;
                goto err_release_fb;
        }
 
-       /* move palette base pointer by (PAGE_SIZE - palette_sz) bytes */
-       par->v_palette_base = da8xx_fb_info->screen_base +
-                               (PAGE_SIZE - par->palette_sz);
-       par->p_palette_base = da8xx_fb_info->fix.smem_start +
-                               (PAGE_SIZE - par->palette_sz);
-
-       /* the rest of the frame buffer is pixel data */
-       da8xx_fb_fix.smem_start = par->p_palette_base + par->palette_sz;
-       da8xx_fb_fix.smem_len = par->databuf_sz - par->palette_sz;
-       da8xx_fb_fix.line_length = (lcdc_info->width * lcd_cfg->bpp) / 8;
-
-       par->lcdc_clk = fb_clk;
-
-       init_waitqueue_head(&par->da8xx_wq);
+       da8xx_fb_info->screen_base = (char __iomem *) par->vram_virt;
+       da8xx_fb_fix.smem_start    = par->vram_phys;
+       da8xx_fb_fix.smem_len      = par->vram_size;
+       da8xx_fb_fix.line_length   = (lcdc_info->width * lcd_cfg->bpp) / 8;
+
+       par->dma_start = par->vram_phys;
+       par->dma_end   = par->dma_start + lcdc_info->height *
+               da8xx_fb_fix.line_length - 1;
+
+       /* allocate palette buffer */
+       par->v_palette_base = dma_alloc_coherent(NULL,
+                                              PALETTE_SIZE,
+                                              (resource_size_t *)
+                                              &par->p_palette_base,
+                                              GFP_KERNEL | GFP_DMA);
+       if (!par->v_palette_base) {
+               dev_err(&device->dev,
+                       "GLCD: kmalloc for palette buffer failed\n");
+               ret = -EINVAL;
+               goto err_release_fb_mem;
+       }
+       memset(par->v_palette_base, 0, PALETTE_SIZE);
 
        par->irq = platform_get_irq(device, 0);
        if (par->irq < 0) {
                ret = -ENOENT;
-               goto err_release_fb_mem;
+               goto err_release_pl_mem;
        }
 
        ret = request_irq(par->irq, lcdc_irq_handler, 0, DRIVER_NAME, par);
        if (ret)
-               goto err_release_fb_mem;
+               goto err_release_pl_mem;
 
        /* Initialize par */
        da8xx_fb_info->var.bits_per_pixel = lcd_cfg->bpp;
@@ -791,8 +1039,8 @@ static int __init fb_probe(struct platform_device *device)
        da8xx_fb_var.xres = lcdc_info->width;
        da8xx_fb_var.xres_virtual = lcdc_info->width;
 
-       da8xx_fb_var.yres = lcdc_info->height;
-       da8xx_fb_var.yres_virtual = lcdc_info->height;
+       da8xx_fb_var.yres         = lcdc_info->height;
+       da8xx_fb_var.yres_virtual = lcdc_info->height * LCD_NUM_BUFFERS;
 
        da8xx_fb_var.grayscale =
            lcd_cfg->p_disp_panel->panel_shade == MONOCHROME ? 1 : 0;
@@ -807,22 +1055,24 @@ static int __init fb_probe(struct platform_device *device)
        da8xx_fb_info->var = da8xx_fb_var;
        da8xx_fb_info->fbops = &da8xx_fb_ops;
        da8xx_fb_info->pseudo_palette = par->pseudo_palette;
+       da8xx_fb_info->fix.visual = (da8xx_fb_info->var.bits_per_pixel <= 8) ?
+                               FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
 
        ret = fb_alloc_cmap(&da8xx_fb_info->cmap, PALETTE_SIZE, 0);
        if (ret)
                goto err_free_irq;
-
-       /* First palette_sz byte of the frame buffer is the palette */
        da8xx_fb_info->cmap.len = par->palette_sz;
 
-       /* Flush the buffer to the screen. */
-       lcd_blit(LOAD_DATA, par);
-
        /* initialize var_screeninfo */
        da8xx_fb_var.activate = FB_ACTIVATE_FORCE;
        fb_set_var(da8xx_fb_info, &da8xx_fb_var);
 
        dev_set_drvdata(&device->dev, da8xx_fb_info);
+
+       /* initialize the vsync wait queue */
+       init_waitqueue_head(&par->vsync_wait);
+       par->vsync_timeout = HZ / 5;
+
        /* Register the Frame Buffer  */
        if (register_framebuffer(da8xx_fb_info) < 0) {
                dev_err(&device->dev,
@@ -831,22 +1081,32 @@ static int __init fb_probe(struct platform_device *device)
                goto err_dealloc_cmap;
        }
 
-       /* enable raster engine */
-       lcdc_write(lcdc_read(LCD_RASTER_CTRL_REG) |
-                       LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
-
+#ifdef CONFIG_CPU_FREQ
+       ret = lcd_da8xx_cpufreq_register(par);
+       if (ret) {
+               dev_err(&device->dev, "failed to register cpufreq\n");
+               goto err_cpu_freq;
+       }
+#endif
        return 0;
 
+#ifdef CONFIG_CPU_FREQ
+err_cpu_freq:
+       unregister_framebuffer(da8xx_fb_info);
+#endif
+
 err_dealloc_cmap:
        fb_dealloc_cmap(&da8xx_fb_info->cmap);
 
 err_free_irq:
        free_irq(par->irq, par);
 
+err_release_pl_mem:
+       dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
+                         par->p_palette_base);
+
 err_release_fb_mem:
-       dma_free_coherent(NULL, par->databuf_sz + PAGE_SIZE,
-                               da8xx_fb_info->screen_base,
-                               da8xx_fb_info->fix.smem_start);
+       dma_free_coherent(NULL, par->vram_size, par->vram_virt, par->vram_phys);
 
 err_release_fb:
        framebuffer_release(da8xx_fb_info);
@@ -869,11 +1129,35 @@ err_request_mem:
 #ifdef CONFIG_PM
 static int fb_suspend(struct platform_device *dev, pm_message_t state)
 {
-        return -EBUSY;
+       struct fb_info *info = platform_get_drvdata(dev);
+       struct da8xx_fb_par *par = info->par;
+
+       acquire_console_sem();
+       if (par->panel_power_ctrl)
+               par->panel_power_ctrl(0);
+
+       fb_set_suspend(info, 1);
+       lcd_disable_raster();
+       clk_disable(par->lcdc_clk);
+       release_console_sem();
+
+       return 0;
 }
 static int fb_resume(struct platform_device *dev)
 {
-        return -EBUSY;
+       struct fb_info *info = platform_get_drvdata(dev);
+       struct da8xx_fb_par *par = info->par;
+
+       acquire_console_sem();
+       if (par->panel_power_ctrl)
+               par->panel_power_ctrl(1);
+
+       clk_enable(par->lcdc_clk);
+       lcd_enable_raster();
+       fb_set_suspend(info, 0);
+       release_console_sem();
+
+       return 0;
 }
 #else
 #define fb_suspend NULL