backlight: Pass device through notify callback in the pwm driver
[safe/jmp/linux-2.6] / drivers / video / metronomefb.c
index e9a89fd..661bfd2 100644 (file)
  * Corporation. http://support.eink.com/community
  *
  * This driver is written to be used with the Metronome display controller.
- * It was tested with an E-Ink 800x600 Vizplex EPD on a Gumstix Connex board
- * using the Lyre interface board.
+ * It is intended to be architecture independent. A board specific driver
+ * must be used to perform all the physical IO interactions. An example
+ * is provided as am200epd.c
  *
- * General notes:
- * - User must set metronomefb_enable=1 to enable it.
- * - See Documentation/fb/metronomefb.txt for how metronome works.
  */
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/uaccess.h>
 #include <linux/irq.h>
 
-#include <asm/arch/pxa-regs.h>
-#include <asm/unaligned.h>
-
-#define DEBUG 1
-#ifdef DEBUG
-#define DPRINTK(f, a...) printk(KERN_DEBUG "%s: " f, __func__ , ## a)
-#else
-#define DPRINTK(f, a...)
-#endif
+#include <video/metronomefb.h>
 
+#include <asm/unaligned.h>
 
 /* Display specific information */
 #define DPY_W 832
 #define DPY_H 622
 
-struct metromem_desc {
-       u32 mFDADR0;
-       u32 mFSADR0;
-       u32 mFIDR0;
-       u32 mLDCMD0;
-};
-
-struct metromem_cmd {
-       u16 opcode;
-       u16 args[((64-2)/2)];
-       u16 csum;
-};
-
-struct metronomefb_par {
-       unsigned char *metromem;
-       struct metromem_desc *metromem_desc;
-       struct metromem_cmd *metromem_cmd;
-       unsigned char *metromem_wfm;
-       unsigned char *metromem_img;
-       u16 *metromem_img_csum;
-       u16 *csum_table;
-       int metromemsize;
-       dma_addr_t metromem_dma;
-       dma_addr_t metromem_desc_dma;
-       struct fb_info *info;
-       wait_queue_head_t waitq;
-       u8 frame_count;
-};
+static int user_wfm_size;
 
 /* frame differs from image. frame includes non-visible pixels */
 struct epd_frame {
        int fw; /* frame width */
        int fh; /* frame height */
+       u16 config[4];
+       int wfm_size;
 };
 
 static struct epd_frame epd_frame_table[] = {
        {
-       .fw = 832,
-       .fh = 622
+               .fw = 832,
+               .fh = 622,
+               .config = {
+                       15 /* sdlew */
+                       | 2 << 8 /* sdosz */
+                       | 0 << 11 /* sdor */
+                       | 0 << 12 /* sdces */
+                       | 0 << 15, /* sdcer */
+                       42 /* gdspl */
+                       | 1 << 8 /* gdr1 */
+                       | 1 << 9 /* sdshr */
+                       | 0 << 15, /* gdspp */
+                       18 /* gdspw */
+                       | 0 << 15, /* dispc */
+                       599 /* vdlc */
+                       | 0 << 11 /* dsi */
+                       | 0 << 12, /* dsic */
+               },
+               .wfm_size = 47001,
+       },
+       {
+               .fw = 1088,
+               .fh = 791,
+               .config = {
+                       0x0104,
+                       0x031f,
+                       0x0088,
+                       0x02ff,
+               },
+               .wfm_size = 46770,
+       },
+       {
+               .fw = 1200,
+               .fh = 842,
+               .config = {
+                       0x0101,
+                       0x030e,
+                       0x0012,
+                       0x0280,
+               },
+               .wfm_size = 46770,
        },
 };
 
@@ -120,8 +125,7 @@ static struct fb_var_screeninfo metronomefb_var __devinitdata = {
        .transp =       { 0, 0, 0 },
 };
 
-static unsigned int metronomefb_enable;
-
+/* the waveform structure that is coming from userspace firmware */
 struct waveform_hdr {
        u8 stuff[32];
 
@@ -164,9 +168,8 @@ static u16 calc_img_cksum(u16 *start, int length)
 }
 
 /* here we decode the incoming waveform file and populate metromem */
-#define EXP_WFORM_SIZE 47001
-static int load_waveform(u8 *mem, size_t size, u8 *metromem, int m, int t,
-                               u8 *frame_count)
+static int __devinit load_waveform(u8 *mem, size_t size, int m, int t,
+                               struct metronomefb_par *par)
 {
        int tta;
        int wmta;
@@ -178,26 +181,31 @@ static int load_waveform(u8 *mem, size_t size, u8 *metromem, int m, int t,
        int wfm_idx, owfm_idx;
        int mem_idx = 0;
        struct waveform_hdr *wfm_hdr;
+       u8 *metromem = par->metromem_wfm;
+       struct device *dev = par->info->dev;
 
-       if (size != EXP_WFORM_SIZE) {
-               printk(KERN_ERR "Error: unexpected size %d != %d\n", size,
-                                       EXP_WFORM_SIZE);
+       if (user_wfm_size)
+               epd_frame_table[par->dt].wfm_size = user_wfm_size;
+
+       if (size != epd_frame_table[par->dt].wfm_size) {
+               dev_err(dev, "Error: unexpected size %Zd != %d\n", size,
+                                       epd_frame_table[par->dt].wfm_size);
                return -EINVAL;
        }
 
        wfm_hdr = (struct waveform_hdr *) mem;
 
        if (wfm_hdr->fvsn != 1) {
-               printk(KERN_ERR "Error: bad fvsn %x\n", wfm_hdr->fvsn);
+               dev_err(dev, "Error: bad fvsn %x\n", wfm_hdr->fvsn);
                return -EINVAL;
        }
        if (wfm_hdr->luts != 0) {
-               printk(KERN_ERR "Error: bad luts %x\n", wfm_hdr->luts);
+               dev_err(dev, "Error: bad luts %x\n", wfm_hdr->luts);
                return -EINVAL;
        }
        cksum = calc_cksum(32, 47, mem);
        if (cksum != wfm_hdr->wfm_cs) {
-               printk(KERN_ERR "Error: bad cksum %x != %x\n", cksum,
+               dev_err(dev, "Error: bad cksum %x != %x\n", cksum,
                                        wfm_hdr->wfm_cs);
                return -EINVAL;
        }
@@ -205,7 +213,7 @@ static int load_waveform(u8 *mem, size_t size, u8 *metromem, int m, int t,
        wfm_hdr->trc += 1;
        for (i = 0; i < 5; i++) {
                if (*(wfm_hdr->stuff2a + i) != 0) {
-                       printk(KERN_ERR "Error: unexpected value in padding\n");
+                       dev_err(dev, "Error: unexpected value in padding\n");
                        return -EINVAL;
                }
        }
@@ -230,41 +238,38 @@ static int load_waveform(u8 *mem, size_t size, u8 *metromem, int m, int t,
                return -EINVAL;
        cksum = calc_cksum(sizeof(*wfm_hdr), cksum_idx, mem);
        if (cksum != mem[cksum_idx]) {
-               printk(KERN_ERR "Error: bad temperature range table cksum"
+               dev_err(dev, "Error: bad temperature range table cksum"
                                " %x != %x\n", cksum, mem[cksum_idx]);
                return -EINVAL;
        }
 
        /* check waveform mode table address checksum */
-       wmta = le32_to_cpu(get_unaligned((__le32 *) wfm_hdr->wmta));
-       wmta &= 0x00FFFFFF;
+       wmta = get_unaligned_le32(wfm_hdr->wmta) & 0x00FFFFFF;
        cksum_idx = wmta + m*4 + 3;
        if (cksum_idx > size)
                return -EINVAL;
        cksum = calc_cksum(cksum_idx - 3, cksum_idx, mem);
        if (cksum != mem[cksum_idx]) {
-               printk(KERN_ERR "Error: bad mode table address cksum"
+               dev_err(dev, "Error: bad mode table address cksum"
                                " %x != %x\n", cksum, mem[cksum_idx]);
                return -EINVAL;
        }
 
        /* check waveform temperature table address checksum */
-       tta = le32_to_cpu(get_unaligned((int *) (mem + wmta + m*4)));
-       tta &= 0x00FFFFFF;
+       tta = get_unaligned_le32(mem + wmta + m * 4) & 0x00FFFFFF;
        cksum_idx = tta + trn*4 + 3;
        if (cksum_idx > size)
                return -EINVAL;
        cksum = calc_cksum(cksum_idx - 3, cksum_idx, mem);
        if (cksum != mem[cksum_idx]) {
-               printk(KERN_ERR "Error: bad temperature table address cksum"
+               dev_err(dev, "Error: bad temperature table address cksum"
                        " %x != %x\n", cksum, mem[cksum_idx]);
                return -EINVAL;
        }
 
        /* here we do the real work of putting the waveform into the
        metromem buffer. this does runlength decoding of the waveform */
-       wfm_idx = le32_to_cpu(get_unaligned((__le32 *) (mem + tta + trn*4)));
-       wfm_idx &= 0x00FFFFFF;
+       wfm_idx = get_unaligned_le32(mem + tta + trn * 4) & 0x00FFFFFF;
        owfm_idx = wfm_idx;
        if (wfm_idx > size)
                return -EINVAL;
@@ -292,189 +297,27 @@ static int load_waveform(u8 *mem, size_t size, u8 *metromem, int m, int t,
                return -EINVAL;
        cksum = calc_cksum(owfm_idx, cksum_idx, mem);
        if (cksum != mem[cksum_idx]) {
-               printk(KERN_ERR "Error: bad waveform data cksum"
+               dev_err(dev, "Error: bad waveform data cksum"
                                " %x != %x\n", cksum, mem[cksum_idx]);
                return -EINVAL;
        }
-       *frame_count = (mem_idx/64);
+       par->frame_count = (mem_idx/64);
 
        return 0;
 }
 
-/* register offsets for gpio control */
-#define LED_GPIO_PIN 51
-#define STDBY_GPIO_PIN 48
-#define RST_GPIO_PIN 49
-#define RDY_GPIO_PIN 32
-#define ERR_GPIO_PIN 17
-#define PCBPWR_GPIO_PIN 16
-
-#define AF_SEL_GPIO_N 0x3
-#define GAFR0_U_OFFSET(pin) ((pin - 16) * 2)
-#define GAFR1_L_OFFSET(pin) ((pin - 32) * 2)
-#define GAFR1_U_OFFSET(pin) ((pin - 48) * 2)
-#define GPDR1_OFFSET(pin) (pin - 32)
-#define GPCR1_OFFSET(pin) (pin - 32)
-#define GPSR1_OFFSET(pin) (pin - 32)
-#define GPCR0_OFFSET(pin) (pin)
-#define GPSR0_OFFSET(pin) (pin)
-
-static void metronome_set_gpio_output(int pin, int val)
-{
-       u8 index;
-
-       index = pin >> 4;
-
-       switch (index) {
-       case 1:
-               if (val)
-                       GPSR0 |= (1 << GPSR0_OFFSET(pin));
-               else
-                       GPCR0 |= (1 << GPCR0_OFFSET(pin));
-               break;
-       case 2:
-               break;
-       case 3:
-               if (val)
-                       GPSR1 |= (1 << GPSR1_OFFSET(pin));
-               else
-                       GPCR1 |= (1 << GPCR1_OFFSET(pin));
-               break;
-       default:
-               printk(KERN_ERR "unimplemented\n");
-       }
-}
-
-static void __devinit metronome_init_gpio_pin(int pin, int dir)
-{
-       u8 index;
-       /* dir 0 is output, 1 is input
-       - do 2 things here:
-       - set gpio alternate function to standard gpio
-       - set gpio direction to input or output  */
-
-       index = pin >> 4;
-       switch (index) {
-       case 1:
-               GAFR0_U &= ~(AF_SEL_GPIO_N << GAFR0_U_OFFSET(pin));
-
-               if (dir)
-                       GPDR0 &= ~(1 << pin);
-               else
-                       GPDR0 |= (1 << pin);
-               break;
-       case 2:
-               GAFR1_L &= ~(AF_SEL_GPIO_N << GAFR1_L_OFFSET(pin));
-
-               if (dir)
-                       GPDR1 &= ~(1 << GPDR1_OFFSET(pin));
-               else
-                       GPDR1 |= (1 << GPDR1_OFFSET(pin));
-               break;
-       case 3:
-               GAFR1_U &= ~(AF_SEL_GPIO_N << GAFR1_U_OFFSET(pin));
-
-               if (dir)
-                       GPDR1 &= ~(1 << GPDR1_OFFSET(pin));
-               else
-                       GPDR1 |= (1 << GPDR1_OFFSET(pin));
-               break;
-       default:
-               printk(KERN_ERR "unimplemented\n");
-       }
-}
-
-static void __devinit metronome_init_gpio_regs(void)
-{
-       metronome_init_gpio_pin(LED_GPIO_PIN, 0);
-       metronome_set_gpio_output(LED_GPIO_PIN, 0);
-
-       metronome_init_gpio_pin(STDBY_GPIO_PIN, 0);
-       metronome_set_gpio_output(STDBY_GPIO_PIN, 0);
-
-       metronome_init_gpio_pin(RST_GPIO_PIN, 0);
-       metronome_set_gpio_output(RST_GPIO_PIN, 0);
-
-       metronome_init_gpio_pin(RDY_GPIO_PIN, 1);
-
-       metronome_init_gpio_pin(ERR_GPIO_PIN, 1);
-
-       metronome_init_gpio_pin(PCBPWR_GPIO_PIN, 0);
-       metronome_set_gpio_output(PCBPWR_GPIO_PIN, 0);
-}
-
-static void metronome_disable_lcd_controller(struct metronomefb_par *par)
-{
-       LCSR = 0xffffffff;      /* Clear LCD Status Register */
-       LCCR0 |= LCCR0_DIS;     /* Disable LCD Controller */
-
-       /* we reset and just wait for things to settle */
-       msleep(200);
-}
-
-static void metronome_enable_lcd_controller(struct metronomefb_par *par)
-{
-       LCSR = 0xffffffff;
-       FDADR0 = par->metromem_desc_dma;
-       LCCR0 |= LCCR0_ENB;
-}
-
-static void __devinit metronome_init_lcdc_regs(struct metronomefb_par *par)
-{
-       /* here we do:
-       - disable the lcd controller
-       - setup lcd control registers
-       - setup dma descriptor
-       - reenable lcd controller
-       */
-
-       /* disable the lcd controller */
-       metronome_disable_lcd_controller(par);
-
-       /* setup lcd control registers */
-       LCCR0 = LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM | LCCR0_PAS
-               | LCCR0_QDM | LCCR0_BM | LCCR0_OUM;
-
-       LCCR1 = (epd_frame_table[0].fw/2 - 1) /* pixels per line */
-               | (27 << 10) /* hsync pulse width - 1 */
-               | (33 << 16) /* eol pixel count */
-               | (33 << 24); /* bol pixel count */
-
-       LCCR2 = (epd_frame_table[0].fh - 1) /* lines per panel */
-               | (24 << 10) /* vsync pulse width - 1 */
-               | (2 << 16) /* eof pixel count */
-               | (0 << 24); /* bof pixel count */
-
-       LCCR3 = 2 /* pixel clock divisor */
-               | (24 << 8) /* AC Bias pin freq */
-               | LCCR3_16BPP /* BPP */
-               | LCCR3_PCP;  /* PCP falling edge */
-
-       /* setup dma descriptor */
-       par->metromem_desc->mFDADR0 = par->metromem_desc_dma;
-       par->metromem_desc->mFSADR0 = par->metromem_dma;
-       par->metromem_desc->mFIDR0 = 0;
-       par->metromem_desc->mLDCMD0 = epd_frame_table[0].fw
-                                       * epd_frame_table[0].fh;
-       /* reenable lcd controller */
-       metronome_enable_lcd_controller(par);
-}
-
 static int metronome_display_cmd(struct metronomefb_par *par)
 {
        int i;
        u16 cs;
        u16 opcode;
        static u8 borderval;
-       u8 *ptr;
 
        /* setup display command
        we can't immediately set the opcode since the controller
        will try parse the command before we've set it all up
        so we just set cs here and set the opcode at the end */
 
-       ptr = par->metromem;
-
        if (par->metromem_cmd->opcode == 0xCC40)
                opcode = cs = 0xCC41;
        else
@@ -493,8 +336,7 @@ static int metronome_display_cmd(struct metronomefb_par *par)
        par->metromem_cmd->csum = cs;
        par->metromem_cmd->opcode = opcode; /* display cmd */
 
-       i = wait_event_interruptible_timeout(par->waitq, (GPLR1 & 0x01), HZ);
-       return i;
+       return par->board->met_wait_event_intr(par);
 }
 
 static int __devinit metronome_powerup_cmd(struct metronomefb_par *par)
@@ -518,59 +360,30 @@ static int __devinit metronome_powerup_cmd(struct metronomefb_par *par)
        par->metromem_cmd->csum = cs;
 
        msleep(1);
-       metronome_set_gpio_output(RST_GPIO_PIN, 1);
+       par->board->set_rst(par, 1);
 
        msleep(1);
-       metronome_set_gpio_output(STDBY_GPIO_PIN, 1);
+       par->board->set_stdby(par, 1);
 
-       i = wait_event_timeout(par->waitq, (GPLR1 & 0x01), HZ);
-       return i;
+       return par->board->met_wait_event(par);
 }
 
 static int __devinit metronome_config_cmd(struct metronomefb_par *par)
 {
-       int i;
-       u16 cs;
-
        /* setup config command
        we can't immediately set the opcode since the controller
-       will try parse the command before we've set it all up
-       so we just set cs here and set the opcode at the end */
-
-       cs = 0xCC10;
-
-       /* set the 12 args ( 8 bytes ) for config. see spec for meanings */
-       i = 0;
-       par->metromem_cmd->args[i] =    15 /* sdlew */
-                                       | 2 << 8 /* sdosz */
-                                       | 0 << 11 /* sdor */
-                                       | 0 << 12 /* sdces */
-                                       | 0 << 15; /* sdcer */
-       cs += par->metromem_cmd->args[i++];
-
-       par->metromem_cmd->args[i] =    42 /* gdspl */
-                                       | 1 << 8 /* gdr1 */
-                                       | 1 << 9 /* sdshr */
-                                       | 0 << 15; /* gdspp */
-       cs += par->metromem_cmd->args[i++];
-
-       par->metromem_cmd->args[i] =    18 /* gdspw */
-                                       | 0 << 15; /* dispc */
-       cs += par->metromem_cmd->args[i++];
-
-       par->metromem_cmd->args[i] =    599 /* vdlc */
-                                       | 0 << 11 /* dsi */
-                                       | 0 << 12; /* dsic */
-       cs += par->metromem_cmd->args[i++];
+       will try parse the command before we've set it all up */
 
+       memcpy(par->metromem_cmd->args, epd_frame_table[par->dt].config,
+               sizeof(epd_frame_table[par->dt].config));
        /* the rest are 0 */
-       memset((u8 *) (par->metromem_cmd->args + i), 0, (32-i)*2);
+       memset((u8 *) (par->metromem_cmd->args + 4), 0, (32-4)*2);
 
-       par->metromem_cmd->csum = cs;
+       par->metromem_cmd->csum = 0xCC10;
+       par->metromem_cmd->csum += calc_img_cksum(par->metromem_cmd->args, 4);
        par->metromem_cmd->opcode = 0xCC10; /* config cmd */
 
-       i = wait_event_timeout(par->waitq, (GPLR1 & 0x01), HZ);
-       return i;
+       return par->board->met_wait_event(par);
 }
 
 static int __devinit metronome_init_cmd(struct metronomefb_par *par)
@@ -596,16 +409,16 @@ static int __devinit metronome_init_cmd(struct metronomefb_par *par)
        par->metromem_cmd->csum = cs;
        par->metromem_cmd->opcode = 0xCC20; /* init cmd */
 
-       i = wait_event_timeout(par->waitq, (GPLR1 & 0x01), HZ);
-       return i;
+       return par->board->met_wait_event(par);
 }
 
 static int __devinit metronome_init_regs(struct metronomefb_par *par)
 {
        int res;
 
-       metronome_init_gpio_regs();
-       metronome_init_lcdc_regs(par);
+       res = par->board->setup_io(par);
+       if (res)
+               return res;
 
        res = metronome_powerup_cmd(par);
        if (res)
@@ -616,24 +429,22 @@ static int __devinit metronome_init_regs(struct metronomefb_par *par)
                return res;
 
        res = metronome_init_cmd(par);
-       if (res)
-               return res;
 
        return res;
 }
 
 static void metronomefb_dpy_update(struct metronomefb_par *par)
 {
+       int fbsize;
        u16 cksum;
        unsigned char *buf = (unsigned char __force *)par->info->screen_base;
 
+       fbsize = par->info->fix.smem_len;
        /* copy from vm to metromem */
-       memcpy(par->metromem_img, buf, DPY_W*DPY_H);
+       memcpy(par->metromem_img, buf, fbsize);
 
-       cksum = calc_img_cksum((u16 *) par->metromem_img,
-                               (epd_frame_table[0].fw * DPY_H)/2);
-       *((u16 *) (par->metromem_img) +
-                       (epd_frame_table[0].fw * DPY_H)/2) = cksum;
+       cksum = calc_img_cksum((u16 *) par->metromem_img, fbsize/2);
+       *((u16 *)(par->metromem_img) + fbsize/2) = cksum;
        metronome_display_cmd(par);
 }
 
@@ -641,8 +452,8 @@ static u16 metronomefb_dpy_update_page(struct metronomefb_par *par, int index)
 {
        int i;
        u16 csum = 0;
-       u16 *buf = (u16 __force *) (par->info->screen_base + index);
-       u16 *img = (u16 *) (par->metromem_img + index);
+       u16 *buf = (u16 __force *)(par->info->screen_base + index);
+       u16 *img = (u16 *)(par->metromem_img + index);
 
        /* swizzle from vm to metromem and recalc cksum at the same time*/
        for (i = 0; i < PAGE_SIZE/2; i++) {
@@ -678,7 +489,7 @@ static void metronomefb_fillrect(struct fb_info *info,
 {
        struct metronomefb_par *par = info->par;
 
-       cfb_fillrect(info, rect);
+       sys_fillrect(info, rect);
        metronomefb_dpy_update(par);
 }
 
@@ -687,7 +498,7 @@ static void metronomefb_copyarea(struct fb_info *info,
 {
        struct metronomefb_par *par = info->par;
 
-       cfb_copyarea(info, area);
+       sys_copyarea(info, area);
        metronomefb_dpy_update(par);
 }
 
@@ -696,7 +507,7 @@ static void metronomefb_imageblit(struct fb_info *info,
 {
        struct metronomefb_par *par = info->par;
 
-       cfb_imageblit(info, image);
+       sys_imageblit(info, image);
        metronomefb_dpy_update(par);
 }
 
@@ -733,7 +544,7 @@ static ssize_t metronomefb_write(struct fb_info *info, const char __user *buf,
                count = total_size - p;
        }
 
-       dst = (void __force *) (info->screen_base + p);
+       dst = (void __force *)(info->screen_base + p);
 
        if (copy_from_user(dst, buf, count))
                err = -EFAULT;
@@ -759,135 +570,145 @@ static struct fb_deferred_io metronomefb_defio = {
        .deferred_io    = metronomefb_dpy_deferred_io,
 };
 
-static irqreturn_t metronome_handle_irq(int irq, void *dev_id)
-{
-       struct fb_info *info = dev_id;
-       struct metronomefb_par *par = info->par;
-
-       wake_up_interruptible(&par->waitq);
-       return IRQ_HANDLED;
-}
-
 static int __devinit metronomefb_probe(struct platform_device *dev)
 {
        struct fb_info *info;
+       struct metronome_board *board;
        int retval = -ENOMEM;
        int videomemorysize;
        unsigned char *videomemory;
        struct metronomefb_par *par;
        const struct firmware *fw_entry;
-       int cmd_size, wfm_size, img_size, padding_size, totalsize;
        int i;
+       int panel_type;
+       int fw, fh;
+       int epd_dt_index;
+
+       /* pick up board specific routines */
+       board = dev->dev.platform_data;
+       if (!board)
+               return -EINVAL;
+
+       /* try to count device specific driver, if can't, platform recalls */
+       if (!try_module_get(board->owner))
+               return -ENODEV;
+
+       info = framebuffer_alloc(sizeof(struct metronomefb_par), &dev->dev);
+       if (!info)
+               goto err;
 
        /* we have two blocks of memory.
        info->screen_base which is vm, and is the fb used by apps.
        par->metromem which is physically contiguous memory and
        contains the display controller commands, waveform,
        processed image data and padding. this is the data pulled
-       by the pxa255's LCD controller and pushed to Metronome */
+       by the device's LCD controller and pushed to Metronome.
+       the metromem memory is allocated by the board driver and
+       is provided to us */
+
+       panel_type = board->get_panel_type();
+       switch (panel_type) {
+       case 6:
+               epd_dt_index = 0;
+               break;
+       case 8:
+               epd_dt_index = 1;
+               break;
+       case 97:
+               epd_dt_index = 2;
+               break;
+       default:
+               dev_err(&dev->dev, "Unexpected panel type. Defaulting to 6\n");
+               epd_dt_index = 0;
+               break;
+       }
 
-       videomemorysize = (DPY_W*DPY_H);
+       fw = epd_frame_table[epd_dt_index].fw;
+       fh = epd_frame_table[epd_dt_index].fh;
+
+       /* we need to add a spare page because our csum caching scheme walks
+        * to the end of the page */
+       videomemorysize = PAGE_SIZE + (fw * fh);
        videomemory = vmalloc(videomemorysize);
        if (!videomemory)
-               return retval;
+               goto err_fb_rel;
 
        memset(videomemory, 0, videomemorysize);
 
-       info = framebuffer_alloc(sizeof(struct metronomefb_par), &dev->dev);
-       if (!info)
-               goto err_vfree;
-
-       info->screen_base = (char __iomem *) videomemory;
+       info->screen_base = (char __force __iomem *)videomemory;
        info->fbops = &metronomefb_ops;
 
+       metronomefb_fix.line_length = fw;
+       metronomefb_var.xres = fw;
+       metronomefb_var.yres = fh;
+       metronomefb_var.xres_virtual = fw;
+       metronomefb_var.yres_virtual = fh;
        info->var = metronomefb_var;
        info->fix = metronomefb_fix;
        info->fix.smem_len = videomemorysize;
        par = info->par;
        par->info = info;
+       par->board = board;
+       par->dt = epd_dt_index;
        init_waitqueue_head(&par->waitq);
 
        /* this table caches per page csum values. */
        par->csum_table = vmalloc(videomemorysize/PAGE_SIZE);
        if (!par->csum_table)
+               goto err_vfree;
+
+       /* the physical framebuffer that we use is setup by
+        * the platform device driver. It will provide us
+        * with cmd, wfm and image memory in a contiguous area. */
+       retval = board->setup_fb(par);
+       if (retval) {
+               dev_err(&dev->dev, "Failed to setup fb\n");
                goto err_csum_table;
+       }
 
-       /* the metromem buffer is divided as follows:
-       command | CRC | padding
-       16kb waveform data | CRC | padding
-       image data | CRC
-       and an extra 256 bytes for dma descriptors
-       eg: IW=832 IH=622 WS=128
-       */
-
-       cmd_size = 1 * epd_frame_table[0].fw;
-       wfm_size = ((16*1024 + 2 + epd_frame_table[0].fw - 1)
-                       / epd_frame_table[0].fw) * epd_frame_table[0].fw;
-       img_size = epd_frame_table[0].fh * epd_frame_table[0].fw;
-       padding_size = 4 * epd_frame_table[0].fw;
-       totalsize = cmd_size + wfm_size + img_size + padding_size;
-       par->metromemsize = PAGE_ALIGN(totalsize + 256);
-       DPRINTK("desired memory size = %d\n", par->metromemsize);
-       dev->dev.coherent_dma_mask = 0xffffffffull;
-       par->metromem = dma_alloc_writecombine(&dev->dev, par->metromemsize,
-                                               &par->metromem_dma, GFP_KERNEL);
-       if (!par->metromem) {
-               printk(KERN_ERR
-                       "metronomefb: unable to allocate dma buffer\n");
-               goto err_vfree;
+       /* after this point we should have a framebuffer */
+       if ((!par->metromem_wfm) ||  (!par->metromem_img) ||
+               (!par->metromem_dma)) {
+               dev_err(&dev->dev, "fb access failure\n");
+               retval = -EINVAL;
+               goto err_csum_table;
        }
 
        info->fix.smem_start = par->metromem_dma;
-       par->metromem_cmd = (struct metromem_cmd *) par->metromem;
-       par->metromem_wfm = par->metromem + cmd_size;
-       par->metromem_img = par->metromem + cmd_size + wfm_size;
-       par->metromem_img_csum = (u16 *) (par->metromem_img +
-                                       (epd_frame_table[0].fw * DPY_H));
-       DPRINTK("img offset=0x%x\n", cmd_size + wfm_size);
-       par->metromem_desc = (struct metromem_desc *) (par->metromem + cmd_size
-                                       + wfm_size + img_size + padding_size);
-       par->metromem_desc_dma = par->metromem_dma + cmd_size + wfm_size
-                                + img_size + padding_size;
-
-       /* load the waveform in. assume mode 3, temp 31 for now */
-       /*      a) request the waveform file from userspace
-               b) process waveform and decode into metromem */
 
-       retval = request_firmware(&fw_entry, "waveform.wbf", &dev->dev);
+       /* load the waveform in. assume mode 3, temp 31 for now
+               a) request the waveform file from userspace
+               b) process waveform and decode into metromem */
+       retval = request_firmware(&fw_entry, "metronome.wbf", &dev->dev);
        if (retval < 0) {
-               printk(KERN_ERR "metronomefb: couldn't get waveform\n");
-               goto err_dma_free;
+               dev_err(&dev->dev, "Failed to get waveform\n");
+               goto err_csum_table;
        }
 
-       retval = load_waveform((u8 *) fw_entry->data, fw_entry->size,
-                               par->metromem_wfm, 3, 31, &par->frame_count);
+       retval = load_waveform((u8 *) fw_entry->data, fw_entry->size, 3, 31,
+                               par);
+       release_firmware(fw_entry);
        if (retval < 0) {
-               printk(KERN_ERR "metronomefb: couldn't process waveform\n");
-               goto err_ld_wfm;
+               dev_err(&dev->dev, "Failed processing waveform\n");
+               goto err_csum_table;
        }
-       release_firmware(fw_entry);
 
-       retval = request_irq(IRQ_GPIO(RDY_GPIO_PIN), metronome_handle_irq,
-                               IRQF_DISABLED, "Metronome", info);
-       if (retval) {
-               dev_err(&dev->dev, "request_irq failed: %d\n", retval);
-               goto err_ld_wfm;
-       }
-       set_irq_type(IRQ_GPIO(RDY_GPIO_PIN), IRQT_FALLING);
+       if (board->setup_irq(info))
+               goto err_csum_table;
 
        retval = metronome_init_regs(par);
        if (retval < 0)
                goto err_free_irq;
 
-       info->flags = FBINFO_FLAG_DEFAULT;
+       info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
 
        info->fbdefio = &metronomefb_defio;
        fb_deferred_io_init(info);
 
        retval = fb_alloc_cmap(&info->cmap, 8, 0);
        if (retval < 0) {
-               printk(KERN_ERR "Failed to allocate colormap\n");
-               goto err_fb_rel;
+               dev_err(&dev->dev, "Failed to allocate colormap\n");
+               goto err_free_irq;
        }
 
        /* set cmap */
@@ -902,7 +723,7 @@ static int __devinit metronomefb_probe(struct platform_device *dev)
 
        platform_set_drvdata(dev, info);
 
-       printk(KERN_INFO
+       dev_dbg(&dev->dev,
                "fb%d: Metronome frame buffer device, using %dK of video"
                " memory\n", info->node, videomemorysize >> 10);
 
@@ -910,19 +731,16 @@ static int __devinit metronomefb_probe(struct platform_device *dev)
 
 err_cmap:
        fb_dealloc_cmap(&info->cmap);
-err_fb_rel:
-       framebuffer_release(info);
 err_free_irq:
-       free_irq(IRQ_GPIO(RDY_GPIO_PIN), info);
-err_ld_wfm:
-       release_firmware(fw_entry);
-err_dma_free:
-       dma_free_writecombine(&dev->dev, par->metromemsize, par->metromem,
-                               par->metromem_dma);
+       board->cleanup(par);
 err_csum_table:
        vfree(par->csum_table);
 err_vfree:
        vfree(videomemory);
+err_fb_rel:
+       framebuffer_release(info);
+err:
+       module_put(board->owner);
        return retval;
 }
 
@@ -932,14 +750,15 @@ static int __devexit metronomefb_remove(struct platform_device *dev)
 
        if (info) {
                struct metronomefb_par *par = info->par;
+
+               unregister_framebuffer(info);
                fb_deferred_io_cleanup(info);
-               dma_free_writecombine(&dev->dev, par->metromemsize,
-                                       par->metromem, par->metromem_dma);
                fb_dealloc_cmap(&info->cmap);
+               par->board->cleanup(par);
                vfree(par->csum_table);
-               unregister_framebuffer(info);
                vfree((void __force *)info->screen_base);
-               free_irq(IRQ_GPIO(RDY_GPIO_PIN), info);
+               module_put(par->board->owner);
+               dev_dbg(&dev->dev, "calling release\n");
                framebuffer_release(info);
        }
        return 0;
@@ -949,47 +768,23 @@ static struct platform_driver metronomefb_driver = {
        .probe  = metronomefb_probe,
        .remove = metronomefb_remove,
        .driver = {
+               .owner  = THIS_MODULE,
                .name   = "metronomefb",
        },
 };
 
-static struct platform_device *metronomefb_device;
-
 static int __init metronomefb_init(void)
 {
-       int ret;
-
-       if (!metronomefb_enable) {
-               printk(KERN_ERR
-                       "Use metronomefb_enable to enable the device\n");
-               return -ENXIO;
-       }
-
-       ret = platform_driver_register(&metronomefb_driver);
-       if (!ret) {
-               metronomefb_device = platform_device_alloc("metronomefb", 0);
-               if (metronomefb_device)
-                       ret = platform_device_add(metronomefb_device);
-               else
-                       ret = -ENOMEM;
-
-               if (ret) {
-                       platform_device_put(metronomefb_device);
-                       platform_driver_unregister(&metronomefb_driver);
-               }
-       }
-       return ret;
-
+       return platform_driver_register(&metronomefb_driver);
 }
 
 static void __exit metronomefb_exit(void)
 {
-       platform_device_unregister(metronomefb_device);
        platform_driver_unregister(&metronomefb_driver);
 }
 
-module_param(metronomefb_enable, uint, 0);
-MODULE_PARM_DESC(metronomefb_enable, "Enable communication with Metronome");
+module_param(user_wfm_size, uint, 0);
+MODULE_PARM_DESC(user_wfm_size, "Set custom waveform size");
 
 module_init(metronomefb_init);
 module_exit(metronomefb_exit);