This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / media / dvb / frontends / sp887x.c
index ac2c86d..0aeb812 100644 (file)
 /*
-   Driver for the Spase sp887x demodulator
+   Driver for the Microtune 7202D Frontend
 */
 
 /*
- * This driver needs external firmware. Please use the command
- * "<kerneldir>/Documentation/dvb/get_dvb_firmware sp887x" to
- * download/extract it, and then copy it to /usr/lib/hotplug/firmware.
+   This driver needs a copy of the Avermedia firmware. The version tested
+   is part of the Avermedia DVB-T 1.3.26.3 Application. If the software is
+   installed in Windows the file will be in the /Program Files/AVerTV DVB-T/
+   directory and is called sc_main.mc. Alternatively it can "extracted" from
+   the install cab files. Copy this file to '/usr/lib/hotplug/firmware/sc_main.mc'.
+   With this version of the file the first 10 bytes are discarded and the
+   next 0x4000 loaded. This may change in future versions.
  */
-#define SP887X_DEFAULT_FIRMWARE "dvb-fe-sp887x.fw"
 
-#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/vmalloc.h>
 #include <linux/module.h>
-#include <linux/moduleparam.h>
-#include <linux/device.h>
-#include <linux/firmware.h>
+#include <linux/init.h>
+#include <linux/string.h>
+#include <linux/slab.h>
+#include <linux/fs.h>
+#include <linux/unistd.h>
+#include <linux/fcntl.h>
+#include <linux/errno.h>
+#include <linux/i2c.h>
+#include <linux/syscalls.h>
 
 #include "dvb_frontend.h"
-#include "sp887x.h"
-
-
-struct sp887x_state {
-
-       struct i2c_adapter* i2c;
-
-       struct dvb_frontend_ops ops;
-
-       const struct sp887x_config* config;
-
-       struct dvb_frontend frontend;
-
-       /* demodulator private data */
-       u8 initialised:1;
-};
-
-static int debug;
-#define dprintk(args...) \
-       do { \
-               if (debug) printk(KERN_DEBUG "sp887x: " args); \
+#include "dvb_functions.h"
+
+#ifndef DVB_SP887X_FIRMWARE_FILE
+#define DVB_SP887X_FIRMWARE_FILE "/usr/lib/hotplug/firmware/sc_main.mc"
+#endif
+
+static char *sp887x_firmware = DVB_SP887X_FIRMWARE_FILE;
+
+#if 0
+#define dprintk(x...) printk(x)
+#else
+#define dprintk(x...)
+#endif
+
+#if 0
+#define LOG(dir,addr,buf,len)                                  \
+       do {                                                    \
+               int i;                                          \
+               printk("%s (%02x):", dir, addr & 0xff);         \
+               for (i=0; i<len; i++)                           \
+                       printk(" 0x%02x,", buf[i] & 0xff);      \
+               printk("\n");                                   \
        } while (0)
+#else
+#define LOG(dir,addr,buf,len)
+#endif
+
+
+static
+struct dvb_frontend_info sp887x_info = {
+       .name = "Microtune MT7202DTF",
+       .type = FE_OFDM,
+       .frequency_min =  50500000,
+       .frequency_max = 858000000,
+       .frequency_stepsize = 166666,
+       .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
+               FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
+               FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
+                FE_CAN_RECOVER
+};
 
-static int i2c_writebytes (struct sp887x_state* state, u8 *buf, u8 len)
+static
+int i2c_writebytes (struct dvb_frontend *fe, u8 addr, u8 *buf, u8 len)
 {
-       struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = len };
+       struct dvb_i2c_bus *i2c = fe->i2c;
+       struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = len };
        int err;
 
-       if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
+       LOG("i2c_writebytes", msg.addr, msg.buf, msg.len);
+
+       if ((err = i2c->xfer (i2c, &msg, 1)) != 1) {
                printk ("%s: i2c write error (addr %02x, err == %i)\n",
-                       __FUNCTION__, state->config->demod_address, err);
+                       __FUNCTION__, addr, err);
                return -EREMOTEIO;
        }
 
        return 0;
 }
 
-static int sp887x_writereg (struct sp887x_state* state, u16 reg, u16 data)
+
+
+static
+int sp887x_writereg (struct dvb_frontend *fe, u16 reg, u16 data)
 {
+       struct dvb_i2c_bus *i2c = fe->i2c;
        u8 b0 [] = { reg >> 8 , reg & 0xff, data >> 8, data & 0xff };
-       struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 4 };
+       struct i2c_msg msg = { .addr = 0x70, .flags = 0, .buf = b0, .len = 4 };
        int ret;
 
-       if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1) {
+       LOG("sp887x_writereg", msg.addr, msg.buf, msg.len);
+
+       if ((ret = i2c->xfer(i2c, &msg, 1)) != 1) {
                /**
                 *  in case of soft reset we ignore ACK errors...
                 */
-               if (!(reg == 0xf1a && data == 0x000 &&
+               if (!(reg == 0xf1a && data == 0x000 && 
                        (ret == -EREMOTEIO || ret == -EFAULT)))
                {
                        printk("%s: writereg error "
@@ -76,103 +114,153 @@ static int sp887x_writereg (struct sp887x_state* state, u16 reg, u16 data)
        return 0;
 }
 
-static int sp887x_readreg (struct sp887x_state* state, u16 reg)
+
+static
+u16 sp887x_readreg (struct dvb_frontend *fe, u16 reg)
 {
+       struct dvb_i2c_bus *i2c = fe->i2c;
        u8 b0 [] = { reg >> 8 , reg & 0xff };
        u8 b1 [2];
        int ret;
-       struct i2c_msg msg[] = {{ .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 2 },
-                        { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 2 }};
+       struct i2c_msg msg[] = {{ .addr = 0x70, .flags = 0, .buf = b0, .len = 2 },
+                        { .addr = 0x70, .flags = I2C_M_RD, .buf = b1, .len = 2 }};
 
-       if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) {
+       LOG("sp887x_readreg (w)", msg[0].addr, msg[0].buf, msg[0].len);
+       LOG("sp887x_readreg (r)", msg[1].addr, msg[1].buf, msg[1].len);
+
+       if ((ret = i2c->xfer(i2c, msg, 2)) != 2)
                printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
-               return -1;
-       }
 
        return (((b1[0] << 8) | b1[1]) & 0xfff);
 }
 
-static void sp887x_microcontroller_stop (struct sp887x_state* state)
+
+static
+void sp887x_microcontroller_stop (struct dvb_frontend *fe)
 {
        dprintk("%s\n", __FUNCTION__);
-       sp887x_writereg(state, 0xf08, 0x000);
-       sp887x_writereg(state, 0xf09, 0x000);
+       sp887x_writereg(fe, 0xf08, 0x000);
+       sp887x_writereg(fe, 0xf09, 0x000);              
 
        /* microcontroller STOP */
-       sp887x_writereg(state, 0xf00, 0x000);
+       sp887x_writereg(fe, 0xf00, 0x000);
 }
 
-static void sp887x_microcontroller_start (struct sp887x_state* state)
+
+static
+void sp887x_microcontroller_start (struct dvb_frontend *fe)
 {
        dprintk("%s\n", __FUNCTION__);
-       sp887x_writereg(state, 0xf08, 0x000);
-       sp887x_writereg(state, 0xf09, 0x000);
+       sp887x_writereg(fe, 0xf08, 0x000);
+       sp887x_writereg(fe, 0xf09, 0x000);              
 
        /* microcontroller START */
-       sp887x_writereg(state, 0xf00, 0x001);
+       sp887x_writereg(fe, 0xf00, 0x001);
 }
 
-static void sp887x_setup_agc (struct sp887x_state* state)
+
+static
+void sp887x_setup_agc (struct dvb_frontend *fe)
 {
        /* setup AGC parameters */
        dprintk("%s\n", __FUNCTION__);
-       sp887x_writereg(state, 0x33c, 0x054);
-       sp887x_writereg(state, 0x33b, 0x04c);
-       sp887x_writereg(state, 0x328, 0x000);
-       sp887x_writereg(state, 0x327, 0x005);
-       sp887x_writereg(state, 0x326, 0x001);
-       sp887x_writereg(state, 0x325, 0x001);
-       sp887x_writereg(state, 0x324, 0x001);
-       sp887x_writereg(state, 0x318, 0x050);
-       sp887x_writereg(state, 0x317, 0x3fe);
-       sp887x_writereg(state, 0x316, 0x001);
-       sp887x_writereg(state, 0x313, 0x005);
-       sp887x_writereg(state, 0x312, 0x002);
-       sp887x_writereg(state, 0x306, 0x000);
-       sp887x_writereg(state, 0x303, 0x000);
+       sp887x_writereg(fe, 0x33c, 0x054);
+       sp887x_writereg(fe, 0x33b, 0x04c);
+       sp887x_writereg(fe, 0x328, 0x000);
+       sp887x_writereg(fe, 0x327, 0x005);
+       sp887x_writereg(fe, 0x326, 0x001);
+       sp887x_writereg(fe, 0x325, 0x001);
+       sp887x_writereg(fe, 0x324, 0x001);
+       sp887x_writereg(fe, 0x318, 0x050);
+       sp887x_writereg(fe, 0x317, 0x3fe);
+       sp887x_writereg(fe, 0x316, 0x001);
+       sp887x_writereg(fe, 0x313, 0x005);
+       sp887x_writereg(fe, 0x312, 0x002);
+       sp887x_writereg(fe, 0x306, 0x000);
+       sp887x_writereg(fe, 0x303, 0x000);
 }
 
+
 #define BLOCKSIZE 30
-#define FW_SIZE 0x4000
+
 /**
  *  load firmware and setup MPEG interface...
  */
-static int sp887x_initial_setup (struct dvb_frontend* fe, const struct firmware *fw)
+static
+int sp887x_initial_setup (struct dvb_frontend *fe)
 {
-       struct sp887x_state* state = (struct sp887x_state*) fe->demodulator_priv;
        u8 buf [BLOCKSIZE+2];
+       unsigned char *firmware = NULL;
        int i;
-       int fw_size = fw->size;
-       unsigned char *mem = fw->data;
+       int fd;
+       int filesize;
+       int fw_size;
+       mm_segment_t fs;
 
        dprintk("%s\n", __FUNCTION__);
 
-       /* ignore the first 10 bytes, then we expect 0x4000 bytes of firmware */
-       if (fw_size < FW_SIZE+10)
-               return -ENODEV;
+       /* soft reset */
+       sp887x_writereg(fe, 0xf1a, 0x000);
 
-       mem = fw->data + 10;
+       sp887x_microcontroller_stop (fe);
 
-       /* soft reset */
-       sp887x_writereg(state, 0xf1a, 0x000);
+       fs = get_fs();
 
-       sp887x_microcontroller_stop (state);
+       // Load the firmware
+       set_fs(get_ds());
+       fd = sys_open(sp887x_firmware, 0, 0);
+       if (fd < 0) {
+               printk(KERN_WARNING "%s: Unable to open firmware %s\n", __FUNCTION__,
+                      sp887x_firmware);
+               return -EIO;
+       }
+       filesize = sys_lseek(fd, 0L, 2);
+       if (filesize <= 0) {
+               printk(KERN_WARNING "%s: Firmware %s is empty\n", __FUNCTION__,
+                      sp887x_firmware);
+               sys_close(fd);
+               return -EIO;
+       }
+
+       fw_size = 0x4000;
+
+       // allocate buffer for it
+       firmware = vmalloc(fw_size);
+       if (firmware == NULL) {
+               printk(KERN_WARNING "%s: Out of memory loading firmware\n",
+                      __FUNCTION__);
+               sys_close(fd);
+               return -EIO;
+       }
+
+       // read it!
+       // read the first 16384 bytes from the file
+       // ignore the first 10 bytes
+       sys_lseek(fd, 10, 0);
+       if (sys_read(fd, firmware, fw_size) != fw_size) {
+               printk(KERN_WARNING "%s: Failed to read firmware\n", __FUNCTION__);
+               vfree(firmware);
+               sys_close(fd);
+               return -EIO;
+       }
+       sys_close(fd);
+       set_fs(fs);
 
        printk ("%s: firmware upload... ", __FUNCTION__);
 
        /* setup write pointer to -1 (end of memory) */
        /* bit 0x8000 in address is set to enable 13bit mode */
-       sp887x_writereg(state, 0x8f08, 0x1fff);
+       sp887x_writereg(fe, 0x8f08, 0x1fff);
 
        /* dummy write (wrap around to start of memory) */
-       sp887x_writereg(state, 0x8f0a, 0x0000);
+       sp887x_writereg(fe, 0x8f0a, 0x0000);
 
-       for (i = 0; i < FW_SIZE; i += BLOCKSIZE) {
+       for (i=0; i<fw_size; i+=BLOCKSIZE) {
                int c = BLOCKSIZE;
                int err;
 
-               if (i+c > FW_SIZE)
-                       c = FW_SIZE - i;
+               if (i+c > fw_size)
+                       c = fw_size - i;
 
                /* bit 0x8000 in address is set to enable 13bit mode */
                /* bit 0x4000 enables multibyte read/write transfers */
@@ -180,51 +268,83 @@ static int sp887x_initial_setup (struct dvb_frontend* fe, const struct firmware
                buf[0] = 0xcf;
                buf[1] = 0x0a;
 
-               memcpy(&buf[2], mem + i, c);
+               memcpy(&buf[2], firmware + i, c);
 
-               if ((err = i2c_writebytes (state, buf, c+2)) < 0) {
+               if ((err = i2c_writebytes (fe, 0x70, buf, c+2)) < 0) {
                        printk ("failed.\n");
                        printk ("%s: i2c error (err == %i)\n", __FUNCTION__, err);
+                       vfree(firmware);
                        return err;
                }
        }
 
+       vfree(firmware);
+
        /* don't write RS bytes between packets */
-       sp887x_writereg(state, 0xc13, 0x001);
+       sp887x_writereg(fe, 0xc13, 0x001);
 
        /* suppress clock if (!data_valid) */
-       sp887x_writereg(state, 0xc14, 0x000);
+       sp887x_writereg(fe, 0xc14, 0x000);
 
        /* setup MPEG interface... */
-       sp887x_writereg(state, 0xc1a, 0x872);
-       sp887x_writereg(state, 0xc1b, 0x001);
-       sp887x_writereg(state, 0xc1c, 0x000); /* parallel mode (serial mode == 1) */
-       sp887x_writereg(state, 0xc1a, 0x871);
+       sp887x_writereg(fe, 0xc1a, 0x872);
+       sp887x_writereg(fe, 0xc1b, 0x001);
+       sp887x_writereg(fe, 0xc1c, 0x000); /* parallel mode (serial mode == 1) */
+       sp887x_writereg(fe, 0xc1a, 0x871);
 
        /* ADC mode, 2 for MT8872, 3 for SP8870/SP8871 */
-       sp887x_writereg(state, 0x301, 0x002);
+       sp887x_writereg(fe, 0x301, 0x002);
 
-       sp887x_setup_agc(state);
+       sp887x_setup_agc(fe);
 
        /* bit 0x010: enable data valid signal */
-       sp887x_writereg(state, 0xd00, 0x010);
-       sp887x_writereg(state, 0x0d1, 0x000);
-
-       /* setup the PLL */
-       if (state->config->pll_init) {
-               sp887x_writereg(state, 0x206, 0x001);
-               state->config->pll_init(fe);
-               sp887x_writereg(state, 0x206, 0x000);
-       }
+       sp887x_writereg(fe, 0xd00, 0x010);
+       sp887x_writereg(fe, 0x0d1, 0x000);
 
        printk ("done.\n");
        return 0;
 };
 
-static int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05)
+
+/**
+ *  returns the actual tuned center frequency which can be used
+ *  to initialise the AFC registers
+ */
+static
+int tsa5060_setup_pll (struct dvb_frontend *fe, int freq)
 {
-       int known_parameters = 1;
+       u8 cfg, cpump, band_select;
+       u8 buf [4];
+       u32 div;
+
+       div = (36000000 + freq + 83333) / 166666;
+       cfg = 0x88;
+
+       cpump = freq < 175000000 ? 2 : freq < 390000000 ? 1 :
+               freq < 470000000 ? 2 : freq < 750000000 ? 2 : 3;
+
+       band_select = freq < 175000000 ? 0x0e : freq < 470000000 ? 0x05 : 0x03;
 
+       buf [0] = (div >> 8) & 0x7f;
+       buf [1] = div & 0xff;
+       buf [2] = ((div >> 10) & 0x60) | cfg;
+       buf [3] = cpump | band_select;
+
+       /* open i2c gate for PLL message transmission... */
+       sp887x_writereg(fe, 0x206, 0x001);
+       i2c_writebytes(fe, 0x60, buf, 4);
+       sp887x_writereg(fe, 0x206, 0x000);
+
+       return (div * 166666 - 36000000);
+}
+
+
+
+static
+int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05)
+{
+       int known_parameters = 1;
+       
        *reg0xc05 = 0x000;
 
        switch (p->u.ofdm.constellation) {
@@ -292,11 +412,13 @@ static int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05)
        return 0;
 }
 
+
 /**
  *  estimates division of two 24bit numbers,
  *  derived from the ves1820/stv0299 driver code
  */
-static void divide (int n, int d, int *quotient_i, int *quotient_f)
+static
+void divide (int n, int d, int *quotient_i, int *quotient_f)
 {
        unsigned int q, r;
 
@@ -314,7 +436,9 @@ static void divide (int n, int d, int *quotient_i, int *quotient_f)
        }
 }
 
-static void sp887x_correct_offsets (struct sp887x_state* state,
+
+static
+void sp887x_correct_offsets (struct dvb_frontend *fe,
                             struct dvb_frontend_parameters *p,
                             int actual_freq)
 {
@@ -337,31 +461,19 @@ static void sp887x_correct_offsets (struct sp887x_state* state,
                frequency_shift = -frequency_shift;
 
        /* sample rate correction */
-       sp887x_writereg(state, 0x319, srate_correction[bw_index] >> 12);
-       sp887x_writereg(state, 0x31a, srate_correction[bw_index] & 0xfff);
+       sp887x_writereg(fe, 0x319, srate_correction[bw_index] >> 12);
+       sp887x_writereg(fe, 0x31a, srate_correction[bw_index] & 0xfff);
 
        /* carrier offset correction */
-       sp887x_writereg(state, 0x309, frequency_shift >> 12);
-       sp887x_writereg(state, 0x30a, frequency_shift & 0xfff);
+       sp887x_writereg(fe, 0x309, frequency_shift >> 12);
+       sp887x_writereg(fe, 0x30a, frequency_shift & 0xfff);
 }
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-static int sp887x_setup_frontend_parameters (struct dvb_frontend* fe,
+static
+int sp887x_setup_frontend_parameters (struct dvb_frontend *fe,
                                      struct dvb_frontend_parameters *p)
 {
-       struct sp887x_state* state = (struct sp887x_state*) fe->demodulator_priv;
        int actual_freq, err;
        u16 val, reg0xc05;
 
@@ -369,21 +481,18 @@ static int sp887x_setup_frontend_parameters (struct dvb_frontend* fe,
            p->u.ofdm.bandwidth != BANDWIDTH_7_MHZ &&
            p->u.ofdm.bandwidth != BANDWIDTH_6_MHZ)
                return -EINVAL;
-
+       
        if ((err = configure_reg0xc05(p, &reg0xc05)))
                return err;
 
-       sp887x_microcontroller_stop(state);
+       sp887x_microcontroller_stop(fe);
 
-       /* setup the PLL */
-       sp887x_writereg(state, 0x206, 0x001);
-       actual_freq = state->config->pll_set(fe, p);
-       sp887x_writereg(state, 0x206, 0x000);
+       actual_freq = tsa5060_setup_pll(fe, p->frequency);
 
-       /* read status reg in order to clear <pending irqs */
-       sp887x_readreg(state, 0x200);
+       /* read status reg in order to clear pending irqs */
+       sp887x_readreg(fe, 0x200);
 
-       sp887x_correct_offsets(state, p, actual_freq);
+       sp887x_correct_offsets(fe, p, actual_freq);
 
        /* filter for 6/7/8 Mhz channel */
        if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
@@ -393,15 +502,15 @@ static int sp887x_setup_frontend_parameters (struct dvb_frontend* fe,
        else
                val = 0;
 
-       sp887x_writereg(state, 0x311, val);
+       sp887x_writereg(fe, 0x311, val);
 
        /* scan order: 2k first = 0, 8k first = 1 */
        if (p->u.ofdm.transmission_mode == TRANSMISSION_MODE_2K)
-               sp887x_writereg(state, 0x338, 0x000);
+               sp887x_writereg(fe, 0x338, 0x000);
        else
-               sp887x_writereg(state, 0x338, 0x001);
+               sp887x_writereg(fe, 0x338, 0x001);
 
-       sp887x_writereg(state, 0xc05, reg0xc05);
+       sp887x_writereg(fe, 0xc05, reg0xc05);
 
        if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
                val = 2 << 3;
@@ -413,19 +522,28 @@ static int sp887x_setup_frontend_parameters (struct dvb_frontend* fe,
        /* enable OFDM and SAW bits as lock indicators in sync register 0xf17,
         * optimize algorithm for given bandwidth...
         */
-       sp887x_writereg(state, 0xf14, 0x160 | val);
-       sp887x_writereg(state, 0xf15, 0x000);
+       sp887x_writereg(fe, 0xf14, 0x160 | val);
+       sp887x_writereg(fe, 0xf15, 0x000);
 
-       sp887x_microcontroller_start(state);
+       sp887x_microcontroller_start(fe);
        return 0;
 }
 
-static int sp887x_read_status(struct dvb_frontend* fe, fe_status_t* status)
+
+static
+int sp887x_ioctl (struct dvb_frontend *fe, unsigned int cmd, void *arg)
+{
+        switch (cmd) {
+        case FE_GET_INFO:
+               memcpy (arg, &sp887x_info, sizeof(struct dvb_frontend_info));
+               break;
+
+        case FE_READ_STATUS:
        {
-       struct sp887x_state* state = (struct sp887x_state*) fe->demodulator_priv;
-       u16 snr12 = sp887x_readreg(state, 0xf16);
-       u16 sync0x200 = sp887x_readreg(state, 0x200);
-       u16 sync0xf17 = sp887x_readreg(state, 0xf17);
+               u16 snr12 = sp887x_readreg(fe, 0xf16);
+               u16 sync0x200 = sp887x_readreg(fe, 0x200);
+               u16 sync0xf17 = sp887x_readreg(fe, 0xf17);
+               fe_status_t *status = arg;
 
                *status = 0;
 
@@ -451,172 +569,128 @@ static int sp887x_read_status(struct dvb_frontend* fe, fe_status_t* status)
                               steps);
                }
 
-       return 0;
+               break;
+
        }
 
-static int sp887x_read_ber(struct dvb_frontend* fe, u32* ber)
+        case FE_READ_BER:
        {
-       struct sp887x_state* state = (struct sp887x_state*) fe->demodulator_priv;
-
-       *ber = (sp887x_readreg(state, 0xc08) & 0x3f) |
-              (sp887x_readreg(state, 0xc07) << 6);
-       sp887x_writereg(state, 0xc08, 0x000);
-       sp887x_writereg(state, 0xc07, 0x000);
+               u32* ber = arg;
+               *ber = (sp887x_readreg(fe, 0xc08) & 0x3f) |
+                      (sp887x_readreg(fe, 0xc07) << 6);
+               sp887x_writereg(fe, 0xc08, 0x000);
+               sp887x_writereg(fe, 0xc07, 0x000);
                if (*ber >= 0x3fff0)
                        *ber = ~0;
+               break;
 
-       return 0;
        }
 
-static int sp887x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
+        case FE_READ_SIGNAL_STRENGTH:          // FIXME: correct registers ?
        {
-       struct sp887x_state* state = (struct sp887x_state*) fe->demodulator_priv;
-
-       u16 snr12 = sp887x_readreg(state, 0xf16);
+               u16 snr12 = sp887x_readreg(fe, 0xf16);
                u32 signal = 3 * (snr12 << 4);
-       *strength = (signal < 0xffff) ? signal : 0xffff;
+               *((u16*) arg) = (signal < 0xffff) ? signal : 0xffff;
+               break;
+       }
 
-       return 0;
+        case FE_READ_SNR:
+       {
+               u16 snr12 = sp887x_readreg(fe, 0xf16);
+               *(u16*) arg = (snr12 << 4) | (snr12 >> 8);
+               break;
        }
 
-static int sp887x_read_snr(struct dvb_frontend* fe, u16* snr)
+       case FE_READ_UNCORRECTED_BLOCKS:
        {
-       struct sp887x_state* state = (struct sp887x_state*) fe->demodulator_priv;
+               u32 *ublocks = (u32 *) arg;
+               *ublocks = sp887x_readreg(fe, 0xc0c);
+               if (*ublocks == 0xfff)
+                       *ublocks = ~0;
+               break;
+       }
 
-       u16 snr12 = sp887x_readreg(state, 0xf16);
-       *snr = (snr12 << 4) | (snr12 >> 8);
+        case FE_SET_FRONTEND:
+               return sp887x_setup_frontend_parameters(fe, arg);
 
-               return 0;
-       }
+       case FE_GET_FRONTEND:  // FIXME: read known values back from Hardware...
+               break;
 
-static int sp887x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
-{
-       struct sp887x_state* state = (struct sp887x_state*) fe->demodulator_priv;
+        case FE_SLEEP:
+               /* tristate TS output and disable interface pins */
+               sp887x_writereg(fe, 0xc18, 0x000);
+               break;
 
-       *ucblocks = sp887x_readreg(state, 0xc0c);
-       if (*ucblocks == 0xfff)
-               *ucblocks = ~0;
+        case FE_INIT:
+               if (fe->data == NULL) {   /* first time initialisation... */
+                       fe->data = (void*) ~0;
+                       sp887x_initial_setup (fe);
+               }
+               /* enable TS output and interface pins */
+               sp887x_writereg(fe, 0xc18, 0x00d);
+               break;
+
+       case FE_GET_TUNE_SETTINGS:
+       {
+               struct dvb_frontend_tune_settings* fesettings = (struct dvb_frontend_tune_settings*) arg;
+               fesettings->min_delay_ms = 50;
+               fesettings->step_size = 0;
+               fesettings->max_drift = 0;
+               return 0;
+       }           
+
+       default:
+               return -EOPNOTSUPP;
+        };
 
         return 0;
 }
 
-static int sp887x_sleep(struct dvb_frontend* fe)
-{
-       struct sp887x_state* state = (struct sp887x_state*) fe->demodulator_priv;
 
-       /* tristate TS output and disable interface pins */
-       sp887x_writereg(state, 0xc18, 0x000);
 
-       return 0;
-       }
-
-static int sp887x_init(struct dvb_frontend* fe)
+static
+int sp887x_attach (struct dvb_i2c_bus *i2c, void **data)
 {
-       struct sp887x_state* state = (struct sp887x_state*) fe->demodulator_priv;
-        const struct firmware *fw = NULL;
-       int ret;
+       struct i2c_msg msg = {.addr = 0x70, .flags = 0, .buf = NULL, .len = 0 };
 
-       if (!state->initialised) {
-       /* request the firmware, this will block until someone uploads it */
-       printk("sp887x: waiting for firmware upload...\n");
-               ret = state->config->request_firmware(fe, &fw, SP887X_DEFAULT_FIRMWARE);
-       if (ret) {
-               printk("sp887x: no firmware upload (timeout or file not found?)\n");
-                       return ret;
-       }
+       dprintk ("%s\n", __FUNCTION__);
 
-               ret = sp887x_initial_setup(fe, fw);
-       if (ret) {
-               printk("sp887x: writing firmware to device failed\n");
-                       release_firmware(fw);
-                       return ret;
-       }
-               state->initialised = 1;
-       }
-
-       /* enable TS output and interface pins */
-       sp887x_writereg(state, 0xc18, 0x00d);
+       if (i2c->xfer (i2c, &msg, 1) != 1)
+                return -ENODEV;
 
-       return 0;
+       return dvb_register_frontend (sp887x_ioctl, i2c, NULL, &sp887x_info);
 }
 
-static int sp887x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
-{
-        fesettings->min_delay_ms = 350;
-        fesettings->step_size = 166666*2;
-        fesettings->max_drift = (166666*2)+1;
-        return 0;
-}
 
-static void sp887x_release(struct dvb_frontend* fe)
+static
+void sp887x_detach (struct dvb_i2c_bus *i2c, void *data)
 {
-       struct sp887x_state* state = (struct sp887x_state*) fe->demodulator_priv;
-       kfree(state);
+       dprintk ("%s\n", __FUNCTION__);
+       dvb_unregister_frontend (sp887x_ioctl, i2c);
 }
 
-static struct dvb_frontend_ops sp887x_ops;
 
-struct dvb_frontend* sp887x_attach(const struct sp887x_config* config,
-                                  struct i2c_adapter* i2c)
+static
+int __init init_sp887x (void)
 {
-       struct sp887x_state* state = NULL;
-
-       /* allocate memory for the internal state */
-       state = (struct sp887x_state*) kmalloc(sizeof(struct sp887x_state), GFP_KERNEL);
-       if (state == NULL) goto error;
-
-       /* setup the state */
-       state->config = config;
-       state->i2c = i2c;
-       memcpy(&state->ops, &sp887x_ops, sizeof(struct dvb_frontend_ops));
-       state->initialised = 0;
-
-       /* check if the demod is there */
-       if (sp887x_readreg(state, 0x0200) < 0) goto error;
+       dprintk ("%s\n", __FUNCTION__);
+       return dvb_register_i2c_device (NULL, sp887x_attach, sp887x_detach);
+}
 
-       /* create dvb_frontend */
-       state->frontend.ops = &state->ops;
-       state->frontend.demodulator_priv = state;
-       return &state->frontend;
 
-error:
-       if (state) kfree(state);
-       return NULL;
+static
+void __exit exit_sp887x (void)
+{
+       dprintk ("%s\n", __FUNCTION__);
+       dvb_unregister_i2c_device (sp887x_attach);
 }
 
-static struct dvb_frontend_ops sp887x_ops = {
-
-       .info = {
-               .name = "Spase SP887x DVB-T",
-               .type = FE_OFDM,
-               .frequency_min =  50500000,
-               .frequency_max = 858000000,
-               .frequency_stepsize = 166666,
-               .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
-                       FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
-                       FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
-                       FE_CAN_RECOVER
-       },
-
-       .release = sp887x_release,
-
-       .init = sp887x_init,
-       .sleep = sp887x_sleep,
-
-       .set_frontend = sp887x_setup_frontend_parameters,
-       .get_tune_settings = sp887x_get_tune_settings,
-
-       .read_status = sp887x_read_status,
-       .read_ber = sp887x_read_ber,
-       .read_signal_strength = sp887x_read_signal_strength,
-       .read_snr = sp887x_read_snr,
-       .read_ucblocks = sp887x_read_ucblocks,
-};
 
-module_param(debug, int, 0644);
-MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
+module_init(init_sp887x);
+module_exit(exit_sp887x);
+
 
-MODULE_DESCRIPTION("Spase sp887x DVB-T demodulator driver");
+MODULE_DESCRIPTION("sp887x DVB-T demodulator driver");
 MODULE_LICENSE("GPL");
 
-EXPORT_SYMBOL(sp887x_attach);
+