Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / drivers / video / sh_mobile_lcdcfb.c
index b048417..facffc2 100644 (file)
 #include <linux/console.h>
 #include <linux/backlight.h>
 #include <linux/gpio.h>
+#include <linux/module.h>
 #include <video/sh_mobile_lcdc.h>
+#include <video/sh_mobile_meram.h>
 #include <linux/atomic.h>
 
 #include "sh_mobile_lcdcfb.h"
-#include "sh_mobile_meram.h"
 
 #define SIDE_B_OFFSET 0x1000
 #define MIRROR_OFFSET 0x2000
 
-/* shared registers */
-#define _LDDCKR 0x410
-#define _LDDCKSTPR 0x414
-#define _LDINTR 0x468
-#define _LDSR 0x46c
-#define _LDCNT1R 0x470
-#define _LDCNT2R 0x474
-#define _LDRCNTR 0x478
-#define _LDDDSR 0x47c
-#define _LDDWD0R 0x800
-#define _LDDRDR 0x840
-#define _LDDWAR 0x900
-#define _LDDRAR 0x904
-
-/* shared registers and their order for context save/restore */
-static int lcdc_shared_regs[] = {
-       _LDDCKR,
-       _LDDCKSTPR,
-       _LDINTR,
-       _LDDDSR,
-       _LDCNT1R,
-       _LDCNT2R,
-};
-#define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
-
 #define MAX_XRES 1920
 #define MAX_YRES 1080
 
@@ -98,22 +74,6 @@ static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
        [LDPMR] = 0x63c,
 };
 
-#define START_LCDC     0x00000001
-#define LCDC_RESET     0x00000100
-#define DISPLAY_BEU    0x00000008
-#define LCDC_ENABLE    0x00000001
-#define LDINTR_FE      0x00000400
-#define LDINTR_VSE     0x00000200
-#define LDINTR_VEE     0x00000100
-#define LDINTR_FS      0x00000004
-#define LDINTR_VSS     0x00000002
-#define LDINTR_VES     0x00000001
-#define LDRCNTR_SRS    0x00020000
-#define LDRCNTR_SRC    0x00010000
-#define LDRCNTR_MRS    0x00000002
-#define LDRCNTR_MRC    0x00000001
-#define LDSR_MRS       0x00000100
-
 static const struct fb_videomode default_720p = {
        .name = "HDMI 720p",
        .xres = 1280,
@@ -141,7 +101,6 @@ struct sh_mobile_lcdc_priv {
        unsigned long lddckr;
        struct sh_mobile_lcdc_chan ch[2];
        struct notifier_block notifier;
-       unsigned long saved_shared_regs[NR_SHARED_REGS];
        int started;
        int forced_bpp; /* 2 channel LCDC must share bpp setting */
        struct sh_mobile_meram_info *meram_dev;
@@ -218,33 +177,36 @@ static void lcdc_sys_write_index(void *handle, unsigned long data)
 {
        struct sh_mobile_lcdc_chan *ch = handle;
 
-       lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
-       lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
-       lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
-       lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
+       lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
+       lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
+       lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
+                  (lcdc_chan_is_sublcd(ch) ? 2 : 0));
+       lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
 }
 
 static void lcdc_sys_write_data(void *handle, unsigned long data)
 {
        struct sh_mobile_lcdc_chan *ch = handle;
 
-       lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
-       lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
-       lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
-       lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
+       lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
+       lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
+       lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
+                  (lcdc_chan_is_sublcd(ch) ? 2 : 0));
+       lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
 }
 
 static unsigned long lcdc_sys_read_data(void *handle)
 {
        struct sh_mobile_lcdc_chan *ch = handle;
 
-       lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
-       lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
-       lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
+       lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
+       lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
+       lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
+                  (lcdc_chan_is_sublcd(ch) ? 2 : 0));
        udelay(1);
-       lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
+       lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
 
-       return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
+       return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
 }
 
 struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
@@ -256,18 +218,22 @@ struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
 static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
 {
        if (atomic_inc_and_test(&priv->hw_usecnt)) {
-               pm_runtime_get_sync(priv->dev);
                if (priv->dot_clk)
                        clk_enable(priv->dot_clk);
+               pm_runtime_get_sync(priv->dev);
+               if (priv->meram_dev && priv->meram_dev->pdev)
+                       pm_runtime_get_sync(&priv->meram_dev->pdev->dev);
        }
 }
 
 static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
 {
        if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
+               if (priv->meram_dev && priv->meram_dev->pdev)
+                       pm_runtime_put_sync(&priv->meram_dev->pdev->dev);
+               pm_runtime_put(priv->dev);
                if (priv->dot_clk)
                        clk_disable(priv->dot_clk);
-               pm_runtime_put(priv->dev);
        }
 }
 
@@ -319,13 +285,13 @@ static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
                if (bcfg->start_transfer)
                        bcfg->start_transfer(bcfg->board_data, ch,
                                             &sh_mobile_lcdc_sys_bus_ops);
-               lcdc_write_chan(ch, LDSM2R, 1);
+               lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
                dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
        } else {
                if (bcfg->start_transfer)
                        bcfg->start_transfer(bcfg->board_data, ch,
                                             &sh_mobile_lcdc_sys_bus_ops);
-               lcdc_write_chan(ch, LDSM2R, 1);
+               lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
        }
 }
 
@@ -341,22 +307,16 @@ static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
 {
        struct sh_mobile_lcdc_priv *priv = data;
        struct sh_mobile_lcdc_chan *ch;
-       unsigned long tmp;
        unsigned long ldintr;
        int is_sub;
        int k;
 
-       /* acknowledge interrupt */
-       ldintr = tmp = lcdc_read(priv, _LDINTR);
-       /*
-        * disable further VSYNC End IRQs, preserve all other enabled IRQs,
-        * write 0 to bits 0-6 to ack all triggered IRQs.
-        */
-       tmp &= 0xffffff00 & ~LDINTR_VEE;
-       lcdc_write(priv, _LDINTR, tmp);
+       /* Acknowledge interrupts and disable further VSYNC End IRQs. */
+       ldintr = lcdc_read(priv, _LDINTR);
+       lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
 
        /* figure out if this interrupt is for main or sub lcd */
-       is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
+       is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
 
        /* wake up channel and disable clocks */
        for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
@@ -365,7 +325,7 @@ static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
                if (!ch->enabled)
                        continue;
 
-               /* Frame Start */
+               /* Frame End */
                if (ldintr & LDINTR_FS) {
                        if (is_sub == lcdc_chan_is_sublcd(ch)) {
                                ch->frame_end = 1;
@@ -391,16 +351,17 @@ static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
 
        /* start or stop the lcdc */
        if (start)
-               lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
+               lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
        else
-               lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
+               lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
 
        /* wait until power is applied/stopped on all channels */
        for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
                if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
                        while (1) {
-                               tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
-                               if (start && tmp == 3)
+                               tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
+                                   & LDPMR_LPS;
+                               if (start && tmp == LDPMR_LPS)
                                        break;
                                if (!start && tmp == 0)
                                        break;
@@ -418,13 +379,13 @@ static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
        u32 tmp;
 
        tmp = ch->ldmt1r_value;
-       tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
-       tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
-       tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
-       tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
-       tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
-       tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
-       tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
+       tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
+       tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
+       tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
+       tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
+       tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
+       tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
+       tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
        lcdc_write_chan(ch, LDMT1R, tmp);
 
        /* setup SYS bus */
@@ -463,242 +424,239 @@ static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
        lcdc_write_chan(ch, LDHAJR, tmp);
 }
 
-static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
+/*
+ * __sh_mobile_lcdc_start - Configure and tart the LCDC
+ * @priv: LCDC device
+ *
+ * Configure all enabled channels and start the LCDC device. All external
+ * devices (clocks, MERAM, panels, ...) are not touched by this function.
+ */
+static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
 {
        struct sh_mobile_lcdc_chan *ch;
-       struct sh_mobile_lcdc_board_cfg *board_cfg;
        unsigned long tmp;
        int bpp = 0;
-       unsigned long ldddsr;
-       int k, m, ret;
+       int k, m;
 
-       /* enable clocks before accessing the hardware */
-       for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
-               if (priv->ch[k].enabled) {
-                       sh_mobile_lcdc_clk_on(priv);
-                       if (!bpp)
-                               bpp = priv->ch[k].info->var.bits_per_pixel;
-               }
-       }
-
-       /* reset */
-       lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
-       lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
-
-       /* enable LCDC channels */
-       tmp = lcdc_read(priv, _LDCNT2R);
-       tmp |= priv->ch[0].enabled;
-       tmp |= priv->ch[1].enabled;
-       lcdc_write(priv, _LDCNT2R, tmp);
-
-       /* read data from external memory, avoid using the BEU for now */
-       lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
+       /* Enable LCDC channels. Read data from external memory, avoid using the
+        * BEU for now.
+        */
+       lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
 
-       /* stop the lcdc first */
+       /* Stop the LCDC first and disable all interrupts. */
        sh_mobile_lcdc_start_stop(priv, 0);
+       lcdc_write(priv, _LDINTR, 0);
 
-       /* configure clocks */
+       /* Configure power supply, dot clocks and start them. */
        tmp = priv->lddckr;
        for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
                ch = &priv->ch[k];
-
-               if (!priv->ch[k].enabled)
+               if (!ch->enabled)
                        continue;
 
+               if (!bpp)
+                       bpp = ch->info->var.bits_per_pixel;
+
+               /* Power supply */
+               lcdc_write_chan(ch, LDPMR, 0);
+
                m = ch->cfg.clock_divider;
                if (!m)
                        continue;
 
-               if (m == 1)
-                       m = 1 << 6;
-               tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
-
-               /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider denominator */
+               /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
+                * denominator.
+                */
                lcdc_write_chan(ch, LDDCKPAT1R, 0);
                lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
+
+               if (m == 1)
+                       m = LDDCKR_MOSEL;
+               tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
        }
 
        lcdc_write(priv, _LDDCKR, tmp);
-
-       /* start dotclock again */
        lcdc_write(priv, _LDDCKSTPR, 0);
        lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
 
-       /* interrupts are disabled to begin with */
-       lcdc_write(priv, _LDINTR, 0);
-
+       /* Setup geometry, format, frame buffer memory and operation mode. */
        for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
                ch = &priv->ch[k];
-
                if (!ch->enabled)
                        continue;
 
                sh_mobile_lcdc_geometry(ch);
 
-               /* power supply */
-               lcdc_write_chan(ch, LDPMR, 0);
-
-               board_cfg = &ch->cfg.board_cfg;
-               if (board_cfg->setup_sys) {
-                       ret = board_cfg->setup_sys(board_cfg->board_data,
-                                               ch, &sh_mobile_lcdc_sys_bus_ops);
-                       if (ret)
-                               return ret;
-               }
-       }
-
-       /* word and long word swap */
-       ldddsr = lcdc_read(priv, _LDDDSR);
-       if  (priv->ch[0].info->var.nonstd)
-               lcdc_write(priv, _LDDDSR, ldddsr | 7);
-       else {
-               switch (bpp) {
-               case 16:
-                       lcdc_write(priv, _LDDDSR, ldddsr | 6);
-                       break;
-               case 24:
-                       lcdc_write(priv, _LDDDSR, ldddsr | 7);
-                       break;
-               case 32:
-                       lcdc_write(priv, _LDDDSR, ldddsr | 4);
-                       break;
-               }
-       }
-
-       for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
-               unsigned long base_addr_y;
-               unsigned long base_addr_c = 0;
-               int pitch;
-               ch = &priv->ch[k];
-
-               if (!priv->ch[k].enabled)
-                       continue;
-
-               /* set bpp format in PKF[4:0] */
-               tmp = lcdc_read_chan(ch, LDDFR);
-               tmp &= ~0x0003031f;
                if (ch->info->var.nonstd) {
-                       tmp |= (ch->info->var.nonstd << 16);
+                       tmp = (ch->info->var.nonstd << 16);
                        switch (ch->info->var.bits_per_pixel) {
                        case 12:
+                               tmp |= LDDFR_YF_420;
                                break;
                        case 16:
-                               tmp |= (0x1 << 8);
+                               tmp |= LDDFR_YF_422;
                                break;
                        case 24:
-                               tmp |= (0x2 << 8);
+                       default:
+                               tmp |= LDDFR_YF_444;
                                break;
                        }
                } else {
                        switch (ch->info->var.bits_per_pixel) {
                        case 16:
-                               tmp |= 0x03;
+                               tmp = LDDFR_PKF_RGB16;
                                break;
                        case 24:
-                               tmp |= 0x0b;
+                               tmp = LDDFR_PKF_RGB24;
                                break;
                        case 32:
+                       default:
+                               tmp = LDDFR_PKF_ARGB32;
                                break;
                        }
                }
+
                lcdc_write_chan(ch, LDDFR, tmp);
+               lcdc_write_chan(ch, LDMLSR, ch->pitch);
+               lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
+               if (ch->info->var.nonstd)
+                       lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
 
-               base_addr_y = ch->info->fix.smem_start;
-               base_addr_c = base_addr_y +
-                               ch->info->var.xres *
-                               ch->info->var.yres_virtual;
-               pitch = ch->info->fix.line_length;
+               /* When using deferred I/O mode, configure the LCDC for one-shot
+                * operation and enable the frame end interrupt. Otherwise use
+                * continuous read mode.
+                */
+               if (ch->ldmt1r_value & LDMT1R_IFM &&
+                   ch->cfg.sys_bus_cfg.deferred_io_msec) {
+                       lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
+                       lcdc_write(priv, _LDINTR, LDINTR_FE);
+               } else {
+                       lcdc_write_chan(ch, LDSM1R, 0);
+               }
+       }
 
-               /* test if we can enable meram */
-               if (ch->cfg.meram_cfg && priv->meram_dev &&
-                               priv->meram_dev->ops) {
-                       struct sh_mobile_meram_cfg *cfg;
-                       struct sh_mobile_meram_info *mdev;
-                       unsigned long icb_addr_y, icb_addr_c;
-                       int icb_pitch;
-                       int pf;
+       /* Word and long word swap. */
+       if  (priv->ch[0].info->var.nonstd)
+               tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
+       else {
+               switch (bpp) {
+               case 16:
+                       tmp = LDDDSR_LS | LDDDSR_WS;
+                       break;
+               case 24:
+                       tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
+                       break;
+               case 32:
+               default:
+                       tmp = LDDDSR_LS;
+                       break;
+               }
+       }
+       lcdc_write(priv, _LDDDSR, tmp);
 
-                       cfg = ch->cfg.meram_cfg;
-                       mdev = priv->meram_dev;
-                       /* we need to de-init configured ICBs before we
-                        * we can re-initialize them.
-                        */
-                       if (ch->meram_enabled)
-                               mdev->ops->meram_unregister(mdev, cfg);
+       /* Enable the display output. */
+       lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
+       sh_mobile_lcdc_start_stop(priv, 1);
+       priv->started = 1;
+}
 
-                       ch->meram_enabled = 0;
+static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
+{
+       struct sh_mobile_meram_info *mdev = priv->meram_dev;
+       struct sh_mobile_lcdc_board_cfg *board_cfg;
+       struct sh_mobile_lcdc_chan *ch;
+       unsigned long tmp;
+       int ret;
+       int k;
 
-                       if (ch->info->var.nonstd) {
-                               if (ch->info->var.bits_per_pixel == 24)
-                                       pf = SH_MOBILE_MERAM_PF_NV24;
-                               else
-                                       pf = SH_MOBILE_MERAM_PF_NV;
-                       } else {
-                               pf = SH_MOBILE_MERAM_PF_RGB;
-                       }
+       /* enable clocks before accessing the hardware */
+       for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
+               if (priv->ch[k].enabled)
+                       sh_mobile_lcdc_clk_on(priv);
+       }
 
-                       ret = mdev->ops->meram_register(mdev, cfg, pitch,
-                                               ch->info->var.yres,
-                                               pf,
-                                               base_addr_y,
-                                               base_addr_c,
-                                               &icb_addr_y,
-                                               &icb_addr_c,
-                                               &icb_pitch);
-                       if (!ret)  {
-                               /* set LDSA1R value */
-                               base_addr_y = icb_addr_y;
-                               pitch = icb_pitch;
-
-                               /* set LDSA2R value if required */
-                               if (base_addr_c)
-                                       base_addr_c = icb_addr_c;
-
-                               ch->meram_enabled = 1;
-                       }
-               }
+       /* reset */
+       lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
+       lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
 
-               /* point out our frame buffer */
-               lcdc_write_chan(ch, LDSA1R, base_addr_y);
-               if (ch->info->var.nonstd)
-                       lcdc_write_chan(ch, LDSA2R, base_addr_c);
+       for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
+               ch = &priv->ch[k];
 
-               /* set line size */
-               lcdc_write_chan(ch, LDMLSR, pitch);
+               if (!ch->enabled)
+                       continue;
 
-               /* setup deferred io if SYS bus */
-               tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
-               if (ch->ldmt1r_value & (1 << 12) && tmp) {
-                       ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
-                       ch->defio.delay = msecs_to_jiffies(tmp);
-                       ch->info->fbdefio = &ch->defio;
-                       fb_deferred_io_init(ch->info);
+               board_cfg = &ch->cfg.board_cfg;
+               if (board_cfg->setup_sys) {
+                       ret = board_cfg->setup_sys(board_cfg->board_data, ch,
+                                                  &sh_mobile_lcdc_sys_bus_ops);
+                       if (ret)
+                               return ret;
+               }
+       }
 
-                       /* one-shot mode */
-                       lcdc_write_chan(ch, LDSM1R, 1);
+       /* Compute frame buffer base address and pitch for each channel. */
+       for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
+               struct sh_mobile_meram_cfg *cfg;
+               int pixelformat;
 
-                       /* enable "Frame End Interrupt Enable" bit */
-                       lcdc_write(priv, _LDINTR, LDINTR_FE);
+               ch = &priv->ch[k];
+               if (!ch->enabled)
+                       continue;
 
-               } else {
-                       /* continuous read mode */
-                       lcdc_write_chan(ch, LDSM1R, 0);
+               ch->base_addr_y = ch->info->fix.smem_start;
+               ch->base_addr_c = ch->base_addr_y
+                               + ch->info->var.xres
+                               * ch->info->var.yres_virtual;
+               ch->pitch = ch->info->fix.line_length;
+
+               /* Enable MERAM if possible. */
+               cfg = ch->cfg.meram_cfg;
+               if (mdev == NULL || mdev->ops == NULL || cfg == NULL)
+                       continue;
+
+               /* we need to de-init configured ICBs before we can
+                * re-initialize them.
+                */
+               if (ch->meram_enabled) {
+                       mdev->ops->meram_unregister(mdev, cfg);
+                       ch->meram_enabled = 0;
                }
+
+               if (!ch->info->var.nonstd)
+                       pixelformat = SH_MOBILE_MERAM_PF_RGB;
+               else if (ch->info->var.bits_per_pixel == 24)
+                       pixelformat = SH_MOBILE_MERAM_PF_NV24;
+               else
+                       pixelformat = SH_MOBILE_MERAM_PF_NV;
+
+               ret = mdev->ops->meram_register(mdev, cfg, ch->pitch,
+                                       ch->info->var.yres, pixelformat,
+                                       ch->base_addr_y, ch->base_addr_c,
+                                       &ch->base_addr_y, &ch->base_addr_c,
+                                       &ch->pitch);
+               if (!ret)
+                       ch->meram_enabled = 1;
        }
 
-       /* display output */
-       lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
+       /* Start the LCDC. */
+       __sh_mobile_lcdc_start(priv);
 
-       /* start the lcdc */
-       sh_mobile_lcdc_start_stop(priv, 1);
-       priv->started = 1;
-
-       /* tell the board code to enable the panel */
+       /* Setup deferred I/O, tell the board code to enable the panels, and
+        * turn backlight on.
+        */
        for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
                ch = &priv->ch[k];
                if (!ch->enabled)
                        continue;
 
+               tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
+               if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
+                       ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
+                       ch->defio.delay = msecs_to_jiffies(tmp);
+                       ch->info->fbdefio = &ch->defio;
+                       fb_deferred_io_init(ch->info);
+               }
+
                board_cfg = &ch->cfg.board_cfg;
                if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
                        board_cfg->display_on(board_cfg->board_data, ch->info);
@@ -776,42 +734,42 @@ static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
 
 static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
 {
-       int ifm, miftyp;
-
-       switch (ch->cfg.interface_type) {
-       case RGB8: ifm = 0; miftyp = 0; break;
-       case RGB9: ifm = 0; miftyp = 4; break;
-       case RGB12A: ifm = 0; miftyp = 5; break;
-       case RGB12B: ifm = 0; miftyp = 6; break;
-       case RGB16: ifm = 0; miftyp = 7; break;
-       case RGB18: ifm = 0; miftyp = 10; break;
-       case RGB24: ifm = 0; miftyp = 11; break;
-       case SYS8A: ifm = 1; miftyp = 0; break;
-       case SYS8B: ifm = 1; miftyp = 1; break;
-       case SYS8C: ifm = 1; miftyp = 2; break;
-       case SYS8D: ifm = 1; miftyp = 3; break;
-       case SYS9: ifm = 1; miftyp = 4; break;
-       case SYS12: ifm = 1; miftyp = 5; break;
-       case SYS16A: ifm = 1; miftyp = 7; break;
-       case SYS16B: ifm = 1; miftyp = 8; break;
-       case SYS16C: ifm = 1; miftyp = 9; break;
-       case SYS18: ifm = 1; miftyp = 10; break;
-       case SYS24: ifm = 1; miftyp = 11; break;
-       default: goto bad;
+       int interface_type = ch->cfg.interface_type;
+
+       switch (interface_type) {
+       case RGB8:
+       case RGB9:
+       case RGB12A:
+       case RGB12B:
+       case RGB16:
+       case RGB18:
+       case RGB24:
+       case SYS8A:
+       case SYS8B:
+       case SYS8C:
+       case SYS8D:
+       case SYS9:
+       case SYS12:
+       case SYS16A:
+       case SYS16B:
+       case SYS16C:
+       case SYS18:
+       case SYS24:
+               break;
+       default:
+               return -EINVAL;
        }
 
        /* SUBLCD only supports SYS interface */
        if (lcdc_chan_is_sublcd(ch)) {
-               if (ifm == 0)
-                       goto bad;
-               else
-                       ifm = 0;
+               if (!(interface_type & LDMT1R_IFM))
+                       return -EINVAL;
+
+               interface_type &= ~LDMT1R_IFM;
        }
 
-       ch->ldmt1r_value = (ifm << 12) | miftyp;
+       ch->ldmt1r_value = interface_type;
        return 0;
- bad:
-       return -EINVAL;
 }
 
 static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
@@ -819,18 +777,24 @@ static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
                                       struct sh_mobile_lcdc_priv *priv)
 {
        char *str;
-       int icksel;
 
        switch (clock_source) {
-       case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
-       case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
-       case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
+       case LCDC_CLK_BUS:
+               str = "bus_clk";
+               priv->lddckr = LDDCKR_ICKSEL_BUS;
+               break;
+       case LCDC_CLK_PERIPHERAL:
+               str = "peripheral_clk";
+               priv->lddckr = LDDCKR_ICKSEL_MIPI;
+               break;
+       case LCDC_CLK_EXTERNAL:
+               str = NULL;
+               priv->lddckr = LDDCKR_ICKSEL_HDMI;
+               break;
        default:
                return -EINVAL;
        }
 
-       priv->lddckr = icksel << 16;
-
        if (str) {
                priv->dot_clk = clk_get(&pdev->dev, str);
                if (IS_ERR(priv->dot_clk)) {
@@ -914,12 +878,12 @@ static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
        unsigned long base_addr_y, base_addr_c;
        unsigned long c_offset;
 
-       if (!var->nonstd)
-               new_pan_offset = (var->yoffset * info->fix.line_length) +
-                       (var->xoffset * (info->var.bits_per_pixel / 8));
+       if (!info->var.nonstd)
+               new_pan_offset = var->yoffset * info->fix.line_length
+                              + var->xoffset * (info->var.bits_per_pixel / 8);
        else
-               new_pan_offset = (var->yoffset * info->fix.line_length) +
-                       (var->xoffset);
+               new_pan_offset = var->yoffset * info->fix.line_length
+                              + var->xoffset;
 
        if (new_pan_offset == ch->pan_offset)
                return 0;       /* No change, do nothing */
@@ -928,44 +892,40 @@ static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
 
        /* Set the source address for the next refresh */
        base_addr_y = ch->dma_handle + new_pan_offset;
-       if (var->nonstd) {
+       if (info->var.nonstd) {
                /* Set y offset */
-               c_offset = (var->yoffset *
-                       info->fix.line_length *
-                       (info->var.bits_per_pixel - 8)) / 8;
-               base_addr_c = ch->dma_handle + var->xres * var->yres_virtual +
-                       c_offset;
+               c_offset = var->yoffset * info->fix.line_length
+                        * (info->var.bits_per_pixel - 8) / 8;
+               base_addr_c = ch->dma_handle
+                           + info->var.xres * info->var.yres_virtual
+                           + c_offset;
                /* Set x offset */
                if (info->var.bits_per_pixel == 24)
                        base_addr_c += 2 * var->xoffset;
                else
                        base_addr_c += var->xoffset;
-       } else
-               base_addr_c = 0;
+       }
 
-       if (!ch->meram_enabled) {
-               lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
-               if (base_addr_c)
-                       lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
-       } else {
+       if (ch->meram_enabled) {
                struct sh_mobile_meram_cfg *cfg;
                struct sh_mobile_meram_info *mdev;
-               unsigned long icb_addr_y, icb_addr_c;
                int ret;
 
                cfg = ch->cfg.meram_cfg;
                mdev = priv->meram_dev;
                ret = mdev->ops->meram_update(mdev, cfg,
                                        base_addr_y, base_addr_c,
-                                       &icb_addr_y, &icb_addr_c);
+                                       &base_addr_y, &base_addr_c);
                if (ret)
                        return ret;
+       }
 
-               lcdc_write_chan_mirror(ch, LDSA1R, icb_addr_y);
-               if (icb_addr_c)
-                       lcdc_write_chan_mirror(ch, LDSA2R, icb_addr_c);
+       ch->base_addr_y = base_addr_y;
+       ch->base_addr_c = base_addr_c;
 
-       }
+       lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
+       if (info->var.nonstd)
+               lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
 
        if (lcdc_chan_is_sublcd(ch))
                lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
@@ -985,9 +945,11 @@ static int sh_mobile_wait_for_vsync(struct fb_info *info)
        unsigned long ldintr;
        int ret;
 
-       /* Enable VSync End interrupt */
+       /* Enable VSync End interrupt and be careful not to acknowledge any
+        * pending interrupt.
+        */
        ldintr = lcdc_read(ch->lcdc, _LDINTR);
-       ldintr |= LDINTR_VEE;
+       ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
        lcdc_write(ch->lcdc, _LDINTR, ldintr);
 
        ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
@@ -1037,11 +999,6 @@ static void sh_mobile_fb_reconfig(struct fb_info *info)
                /* Couldn't reconfigure, hopefully, can continue as before */
                return;
 
-       if (info->var.nonstd)
-               info->fix.line_length = mode1.xres;
-       else
-               info->fix.line_length = mode1.xres * (ch->cfg.bpp / 8);
-
        /*
         * fb_set_var() calls the notifier change internally, only if
         * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
@@ -1094,30 +1051,126 @@ static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *in
 {
        struct sh_mobile_lcdc_chan *ch = info->par;
        struct sh_mobile_lcdc_priv *p = ch->lcdc;
+       unsigned int best_dist = (unsigned int)-1;
+       unsigned int best_xres = 0;
+       unsigned int best_yres = 0;
+       unsigned int i;
 
-       if (var->xres > MAX_XRES || var->yres > MAX_YRES ||
-           var->xres * var->yres * (ch->cfg.bpp / 8) * 2 > info->fix.smem_len) {
-               dev_warn(info->dev, "Invalid info: %u-%u-%u-%u x %u-%u-%u-%u @ %lukHz!\n",
-                        var->left_margin, var->xres, var->right_margin, var->hsync_len,
-                        var->upper_margin, var->yres, var->lower_margin, var->vsync_len,
-                        PICOS2KHZ(var->pixclock));
+       if (var->xres > MAX_XRES || var->yres > MAX_YRES)
                return -EINVAL;
+
+       /* If board code provides us with a list of available modes, make sure
+        * we use one of them. Find the mode closest to the requested one. The
+        * distance between two modes is defined as the size of the
+        * non-overlapping parts of the two rectangles.
+        */
+       for (i = 0; i < ch->cfg.num_cfg; ++i) {
+               const struct fb_videomode *mode = &ch->cfg.lcd_cfg[i];
+               unsigned int dist;
+
+               /* We can only round up. */
+               if (var->xres > mode->xres || var->yres > mode->yres)
+                       continue;
+
+               dist = var->xres * var->yres + mode->xres * mode->yres
+                    - 2 * min(var->xres, mode->xres)
+                        * min(var->yres, mode->yres);
+
+               if (dist < best_dist) {
+                       best_xres = mode->xres;
+                       best_yres = mode->yres;
+                       best_dist = dist;
+               }
+       }
+
+       /* If no available mode can be used, return an error. */
+       if (ch->cfg.num_cfg != 0) {
+               if (best_dist == (unsigned int)-1)
+                       return -EINVAL;
+
+               var->xres = best_xres;
+               var->yres = best_yres;
        }
 
+       /* Make sure the virtual resolution is at least as big as the visible
+        * resolution.
+        */
+       if (var->xres_virtual < var->xres)
+               var->xres_virtual = var->xres;
+       if (var->yres_virtual < var->yres)
+               var->yres_virtual = var->yres;
+
+       if (var->bits_per_pixel <= 16) {                /* RGB 565 */
+               var->bits_per_pixel = 16;
+               var->red.offset = 11;
+               var->red.length = 5;
+               var->green.offset = 5;
+               var->green.length = 6;
+               var->blue.offset = 0;
+               var->blue.length = 5;
+               var->transp.offset = 0;
+               var->transp.length = 0;
+       } else if (var->bits_per_pixel <= 24) {         /* RGB 888 */
+               var->bits_per_pixel = 24;
+               var->red.offset = 16;
+               var->red.length = 8;
+               var->green.offset = 8;
+               var->green.length = 8;
+               var->blue.offset = 0;
+               var->blue.length = 8;
+               var->transp.offset = 0;
+               var->transp.length = 0;
+       } else if (var->bits_per_pixel <= 32) {         /* RGBA 888 */
+               var->bits_per_pixel = 32;
+               var->red.offset = 16;
+               var->red.length = 8;
+               var->green.offset = 8;
+               var->green.length = 8;
+               var->blue.offset = 0;
+               var->blue.length = 8;
+               var->transp.offset = 24;
+               var->transp.length = 8;
+       } else
+               return -EINVAL;
+
+       var->red.msb_right = 0;
+       var->green.msb_right = 0;
+       var->blue.msb_right = 0;
+       var->transp.msb_right = 0;
+
+       /* Make sure we don't exceed our allocated memory. */
+       if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
+           info->fix.smem_len)
+               return -EINVAL;
+
        /* only accept the forced_bpp for dual channel configurations */
        if (p->forced_bpp && p->forced_bpp != var->bits_per_pixel)
                return -EINVAL;
 
-       switch (var->bits_per_pixel) {
-       case 16: /* PKF[4:0] = 00011 - RGB 565 */
-       case 24: /* PKF[4:0] = 01011 - RGB 888 */
-       case 32: /* PKF[4:0] = 00000 - RGBA 888 */
-               break;
-       default:
-               return -EINVAL;
+       return 0;
+}
+
+static int sh_mobile_set_par(struct fb_info *info)
+{
+       struct sh_mobile_lcdc_chan *ch = info->par;
+       u32 line_length = info->fix.line_length;
+       int ret;
+
+       sh_mobile_lcdc_stop(ch->lcdc);
+
+       if (info->var.nonstd)
+               info->fix.line_length = info->var.xres;
+       else
+               info->fix.line_length = info->var.xres
+                                     * info->var.bits_per_pixel / 8;
+
+       ret = sh_mobile_lcdc_start(ch->lcdc);
+       if (ret < 0) {
+               dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
+               info->fix.line_length = line_length;
        }
 
-       return 0;
+       return ret;
 }
 
 /*
@@ -1177,6 +1230,7 @@ static struct fb_ops sh_mobile_lcdc_ops = {
        .fb_open        = sh_mobile_open,
        .fb_release     = sh_mobile_release,
        .fb_check_var   = sh_mobile_check_var,
+       .fb_set_par     = sh_mobile_set_par,
 };
 
 static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
@@ -1238,66 +1292,6 @@ static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
        backlight_device_unregister(bdev);
 }
 
-static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp,
-                                  int nonstd)
-{
-       if (nonstd) {
-               switch (bpp) {
-               case 12:
-               case 16:
-               case 24:
-                       var->bits_per_pixel = bpp;
-                       var->nonstd = nonstd;
-                       return 0;
-               default:
-                       return -EINVAL;
-               }
-       }
-
-       switch (bpp) {
-       case 16: /* PKF[4:0] = 00011 - RGB 565 */
-               var->red.offset = 11;
-               var->red.length = 5;
-               var->green.offset = 5;
-               var->green.length = 6;
-               var->blue.offset = 0;
-               var->blue.length = 5;
-               var->transp.offset = 0;
-               var->transp.length = 0;
-               break;
-
-       case 24: /* PKF[4:0] = 01011 - RGB 888 */
-               var->red.offset = 16;
-               var->red.length = 8;
-               var->green.offset = 8;
-               var->green.length = 8;
-               var->blue.offset = 0;
-               var->blue.length = 8;
-               var->transp.offset = 0;
-               var->transp.length = 0;
-               break;
-
-       case 32: /* PKF[4:0] = 00000 - RGBA 888 */
-               var->red.offset = 16;
-               var->red.length = 8;
-               var->green.offset = 8;
-               var->green.length = 8;
-               var->blue.offset = 0;
-               var->blue.length = 8;
-               var->transp.offset = 24;
-               var->transp.length = 8;
-               break;
-       default:
-               return -EINVAL;
-       }
-       var->bits_per_pixel = bpp;
-       var->red.msb_right = 0;
-       var->green.msb_right = 0;
-       var->blue.msb_right = 0;
-       var->transp.msb_right = 0;
-       return 0;
-}
-
 static int sh_mobile_lcdc_suspend(struct device *dev)
 {
        struct platform_device *pdev = to_platform_device(dev);
@@ -1316,47 +1310,20 @@ static int sh_mobile_lcdc_resume(struct device *dev)
 static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
 {
        struct platform_device *pdev = to_platform_device(dev);
-       struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
-       struct sh_mobile_lcdc_chan *ch;
-       int k, n;
-
-       /* save per-channel registers */
-       for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
-               ch = &p->ch[k];
-               if (!ch->enabled)
-                       continue;
-               for (n = 0; n < NR_CH_REGS; n++)
-                       ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
-       }
-
-       /* save shared registers */
-       for (n = 0; n < NR_SHARED_REGS; n++)
-               p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
+       struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
 
        /* turn off LCDC hardware */
-       lcdc_write(p, _LDCNT1R, 0);
+       lcdc_write(priv, _LDCNT1R, 0);
+
        return 0;
 }
 
 static int sh_mobile_lcdc_runtime_resume(struct device *dev)
 {
        struct platform_device *pdev = to_platform_device(dev);
-       struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
-       struct sh_mobile_lcdc_chan *ch;
-       int k, n;
-
-       /* restore per-channel registers */
-       for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
-               ch = &p->ch[k];
-               if (!ch->enabled)
-                       continue;
-               for (n = 0; n < NR_CH_REGS; n++)
-                       lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
-       }
+       struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
 
-       /* restore shared registers */
-       for (n = 0; n < NR_SHARED_REGS; n++)
-               lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
+       __sh_mobile_lcdc_start(priv);
 
        return 0;
 }
@@ -1408,17 +1375,187 @@ static int sh_mobile_lcdc_notify(struct notifier_block *nb,
        return NOTIFY_OK;
 }
 
-static int sh_mobile_lcdc_remove(struct platform_device *pdev);
+static int sh_mobile_lcdc_remove(struct platform_device *pdev)
+{
+       struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
+       struct fb_info *info;
+       int i;
 
-static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
+       fb_unregister_client(&priv->notifier);
+
+       for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
+               if (priv->ch[i].info && priv->ch[i].info->dev)
+                       unregister_framebuffer(priv->ch[i].info);
+
+       sh_mobile_lcdc_stop(priv);
+
+       for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
+               info = priv->ch[i].info;
+
+               if (!info || !info->device)
+                       continue;
+
+               if (priv->ch[i].sglist)
+                       vfree(priv->ch[i].sglist);
+
+               if (info->screen_base)
+                       dma_free_coherent(&pdev->dev, info->fix.smem_len,
+                                         info->screen_base,
+                                         priv->ch[i].dma_handle);
+               fb_dealloc_cmap(&info->cmap);
+               framebuffer_release(info);
+       }
+
+       for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
+               if (priv->ch[i].bl)
+                       sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
+       }
+
+       if (priv->dot_clk)
+               clk_put(priv->dot_clk);
+
+       if (priv->dev)
+               pm_runtime_disable(priv->dev);
+
+       if (priv->base)
+               iounmap(priv->base);
+
+       if (priv->irq)
+               free_irq(priv->irq, priv);
+       kfree(priv);
+       return 0;
+}
+
+static int __devinit sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch,
+                                                struct device *dev)
 {
+       struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
+       const struct fb_videomode *max_mode;
+       const struct fb_videomode *mode;
+       struct fb_var_screeninfo *var;
        struct fb_info *info;
-       struct sh_mobile_lcdc_priv *priv;
+       unsigned int max_size;
+       int num_cfg;
+       void *buf;
+       int ret;
+       int i;
+
+       mutex_init(&ch->open_lock);
+
+       /* Allocate the frame buffer device. */
+       ch->info = framebuffer_alloc(0, dev);
+       if (!ch->info) {
+               dev_err(dev, "unable to allocate fb_info\n");
+               return -ENOMEM;
+       }
+
+       info = ch->info;
+       info->fbops = &sh_mobile_lcdc_ops;
+       info->par = ch;
+       info->pseudo_palette = &ch->pseudo_palette;
+       info->flags = FBINFO_FLAG_DEFAULT;
+
+       /* Iterate through the modes to validate them and find the highest
+        * resolution.
+        */
+       max_mode = NULL;
+       max_size = 0;
+
+       for (i = 0, mode = cfg->lcd_cfg; i < cfg->num_cfg; i++, mode++) {
+               unsigned int size = mode->yres * mode->xres;
+
+               /* NV12 buffers must have even number of lines */
+               if ((cfg->nonstd) && cfg->bpp == 12 &&
+                               (mode->yres & 0x1)) {
+                       dev_err(dev, "yres must be multiple of 2 for YCbCr420 "
+                               "mode.\n");
+                       return -EINVAL;
+               }
+
+               if (size > max_size) {
+                       max_mode = mode;
+                       max_size = size;
+               }
+       }
+
+       if (!max_size)
+               max_size = MAX_XRES * MAX_YRES;
+       else
+               dev_dbg(dev, "Found largest videomode %ux%u\n",
+                       max_mode->xres, max_mode->yres);
+
+       /* Initialize fixed screen information. Restrict pan to 2 lines steps
+        * for NV12.
+        */
+       info->fix = sh_mobile_lcdc_fix;
+       info->fix.smem_len = max_size * 2 * cfg->bpp / 8;
+       if (cfg->nonstd && cfg->bpp == 12)
+               info->fix.ypanstep = 2;
+
+       /* Create the mode list. */
+       if (cfg->lcd_cfg == NULL) {
+               mode = &default_720p;
+               num_cfg = 1;
+       } else {
+               mode = cfg->lcd_cfg;
+               num_cfg = cfg->num_cfg;
+       }
+
+       fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
+
+       /* Initialize variable screen information using the first mode as
+        * default. The default Y virtual resolution is twice the panel size to
+        * allow for double-buffering.
+        */
+       var = &info->var;
+       fb_videomode_to_var(var, mode);
+       var->bits_per_pixel = cfg->bpp;
+       var->width = cfg->lcd_size_cfg.width;
+       var->height = cfg->lcd_size_cfg.height;
+       var->yres_virtual = var->yres * 2;
+       var->activate = FB_ACTIVATE_NOW;
+
+       ret = sh_mobile_check_var(var, info);
+       if (ret)
+               return ret;
+
+       /* Allocate frame buffer memory and color map. */
+       buf = dma_alloc_coherent(dev, info->fix.smem_len, &ch->dma_handle,
+                                GFP_KERNEL);
+       if (!buf) {
+               dev_err(dev, "unable to allocate buffer\n");
+               return -ENOMEM;
+       }
+
+       ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
+       if (ret < 0) {
+               dev_err(dev, "unable to allocate cmap\n");
+               dma_free_coherent(dev, info->fix.smem_len,
+                                 buf, ch->dma_handle);
+               return ret;
+       }
+
+       info->fix.smem_start = ch->dma_handle;
+       if (var->nonstd)
+               info->fix.line_length = var->xres;
+       else
+               info->fix.line_length = var->xres * (cfg->bpp / 8);
+
+       info->screen_base = buf;
+       info->device = dev;
+       ch->display_var = *var;
+
+       return 0;
+}
+
+static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
+{
        struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
+       struct sh_mobile_lcdc_priv *priv;
        struct resource *res;
+       int num_channels;
        int error;
-       void *buf;
-       int i, j;
+       int i;
 
        if (!pdata) {
                dev_err(&pdev->dev, "no platform data defined\n");
@@ -1440,7 +1577,7 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
 
        platform_set_drvdata(pdev, priv);
 
-       error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
+       error = request_irq(i, sh_mobile_lcdc_irq, 0,
                            dev_name(&pdev->dev), priv);
        if (error) {
                dev_err(&pdev->dev, "unable to request irq\n");
@@ -1450,9 +1587,8 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
        priv->irq = i;
        atomic_set(&priv->hw_usecnt, -1);
 
-       j = 0;
-       for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
-               struct sh_mobile_lcdc_chan *ch = priv->ch + j;
+       for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
+               struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
 
                ch->lcdc = priv;
                memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
@@ -1472,26 +1608,26 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
 
                switch (pdata->ch[i].chan) {
                case LCDC_CHAN_MAINLCD:
-                       ch->enabled = 1 << 1;
+                       ch->enabled = LDCNT2R_ME;
                        ch->reg_offs = lcdc_offs_mainlcd;
-                       j++;
+                       num_channels++;
                        break;
                case LCDC_CHAN_SUBLCD:
-                       ch->enabled = 1 << 2;
+                       ch->enabled = LDCNT2R_SE;
                        ch->reg_offs = lcdc_offs_sublcd;
-                       j++;
+                       num_channels++;
                        break;
                }
        }
 
-       if (!j) {
+       if (!num_channels) {
                dev_err(&pdev->dev, "no channels defined\n");
                error = -EINVAL;
                goto err1;
        }
 
        /* for dual channel LCDC (MAIN + SUB) force shared bpp setting */
-       if (j == 2)
+       if (num_channels == 2)
                priv->forced_bpp = pdata->ch[0].bpp;
 
        priv->base = ioremap_nocache(res->start, resource_size(res));
@@ -1506,125 +1642,23 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
 
        priv->meram_dev = pdata->meram_dev;
 
-       for (i = 0; i < j; i++) {
-               struct fb_var_screeninfo *var;
-               const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
+       for (i = 0; i < num_channels; i++) {
                struct sh_mobile_lcdc_chan *ch = priv->ch + i;
-               struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
-               const struct fb_videomode *mode = cfg->lcd_cfg;
-               unsigned long max_size = 0;
-               int k;
-               int num_cfg;
-
-               ch->info = framebuffer_alloc(0, &pdev->dev);
-               if (!ch->info) {
-                       dev_err(&pdev->dev, "unable to allocate fb_info\n");
-                       error = -ENOMEM;
-                       break;
-               }
-
-               info = ch->info;
-               var = &info->var;
-               info->fbops = &sh_mobile_lcdc_ops;
-               info->par = ch;
-
-               mutex_init(&ch->open_lock);
-
-               for (k = 0, lcd_cfg = mode;
-                    k < cfg->num_cfg && lcd_cfg;
-                    k++, lcd_cfg++) {
-                       unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
-                       /* NV12 buffers must have even number of lines */
-                       if ((cfg->nonstd) && cfg->bpp == 12 &&
-                                       (lcd_cfg->yres & 0x1)) {
-                               dev_err(&pdev->dev, "yres must be multiple of 2"
-                                               " for YCbCr420 mode.\n");
-                               error = -EINVAL;
-                               goto err1;
-                       }
-
-                       if (size > max_size) {
-                               max_cfg = lcd_cfg;
-                               max_size = size;
-                       }
-               }
-
-               if (!mode)
-                       max_size = MAX_XRES * MAX_YRES;
-               else if (max_cfg)
-                       dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
-                               max_cfg->xres, max_cfg->yres);
 
-               info->fix = sh_mobile_lcdc_fix;
-               info->fix.smem_len = max_size * 2 * cfg->bpp / 8;
-
-                /* Only pan in 2 line steps for NV12 */
-               if (cfg->nonstd && cfg->bpp == 12)
-                       info->fix.ypanstep = 2;
-
-               if (!mode) {
-                       mode = &default_720p;
-                       num_cfg = 1;
-               } else {
-                       num_cfg = cfg->num_cfg;
-               }
-
-               fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
-
-               fb_videomode_to_var(var, mode);
-               var->width = cfg->lcd_size_cfg.width;
-               var->height = cfg->lcd_size_cfg.height;
-               /* Default Y virtual resolution is 2x panel size */
-               var->yres_virtual = var->yres * 2;
-               var->activate = FB_ACTIVATE_NOW;
-
-               error = sh_mobile_lcdc_set_bpp(var, cfg->bpp, cfg->nonstd);
+               error = sh_mobile_lcdc_channel_init(ch, &pdev->dev);
                if (error)
-                       break;
-
-               buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
-                                        &ch->dma_handle, GFP_KERNEL);
-               if (!buf) {
-                       dev_err(&pdev->dev, "unable to allocate buffer\n");
-                       error = -ENOMEM;
-                       break;
-               }
-
-               info->pseudo_palette = &ch->pseudo_palette;
-               info->flags = FBINFO_FLAG_DEFAULT;
-
-               error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
-               if (error < 0) {
-                       dev_err(&pdev->dev, "unable to allocate cmap\n");
-                       dma_free_coherent(&pdev->dev, info->fix.smem_len,
-                                         buf, ch->dma_handle);
-                       break;
-               }
-
-               info->fix.smem_start = ch->dma_handle;
-               if (var->nonstd)
-                       info->fix.line_length = var->xres;
-               else
-                       info->fix.line_length = var->xres * (cfg->bpp / 8);
-
-               info->screen_base = buf;
-               info->device = &pdev->dev;
-               ch->display_var = *var;
+                       goto err1;
        }
 
-       if (error)
-               goto err1;
-
        error = sh_mobile_lcdc_start(priv);
        if (error) {
                dev_err(&pdev->dev, "unable to start hardware\n");
                goto err1;
        }
 
-       for (i = 0; i < j; i++) {
+       for (i = 0; i < num_channels; i++) {
                struct sh_mobile_lcdc_chan *ch = priv->ch + i;
-
-               info = ch->info;
+               struct fb_info *info = ch->info;
 
                if (info->fbdefio) {
                        ch->sglist = vmalloc(sizeof(struct scatterlist) *
@@ -1665,57 +1699,6 @@ err1:
        return error;
 }
 
-static int sh_mobile_lcdc_remove(struct platform_device *pdev)
-{
-       struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
-       struct fb_info *info;
-       int i;
-
-       fb_unregister_client(&priv->notifier);
-
-       for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
-               if (priv->ch[i].info && priv->ch[i].info->dev)
-                       unregister_framebuffer(priv->ch[i].info);
-
-       sh_mobile_lcdc_stop(priv);
-
-       for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
-               info = priv->ch[i].info;
-
-               if (!info || !info->device)
-                       continue;
-
-               if (priv->ch[i].sglist)
-                       vfree(priv->ch[i].sglist);
-
-               if (info->screen_base)
-                       dma_free_coherent(&pdev->dev, info->fix.smem_len,
-                                         info->screen_base,
-                                         priv->ch[i].dma_handle);
-               fb_dealloc_cmap(&info->cmap);
-               framebuffer_release(info);
-       }
-
-       for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
-               if (priv->ch[i].bl)
-                       sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
-       }
-
-       if (priv->dot_clk)
-               clk_put(priv->dot_clk);
-
-       if (priv->dev)
-               pm_runtime_disable(priv->dev);
-
-       if (priv->base)
-               iounmap(priv->base);
-
-       if (priv->irq)
-               free_irq(priv->irq, priv);
-       kfree(priv);
-       return 0;
-}
-
 static struct platform_driver sh_mobile_lcdc_driver = {
        .driver         = {
                .name           = "sh_mobile_lcdc_fb",