V4L/DVB (13985): [STV090x] reset mclk and dev_ver of internal structure after allocating
[safe/jmp/linux-2.6] / drivers / media / dvb / frontends / stv090x.c
index 4dc723c..3c5fba8 100644 (file)
 static unsigned int verbose;
 module_param(verbose, int, 0644);
 
-struct mutex demod_lock;
+/* internal params node */
+struct stv090x_dev {
+       /* pointer for internal params, one for each pair of demods */
+       struct stv090x_internal         *internal;
+       struct stv090x_dev              *next_dev;
+};
+
+/* first internal params */
+static struct stv090x_dev *stv090x_first_dev;
+
+/* find chip by i2c adapter and i2c address */
+static struct stv090x_dev *find_dev(struct i2c_adapter *i2c_adap,
+                                       u8 i2c_addr)
+{
+       struct stv090x_dev *temp_dev = stv090x_first_dev;
+
+       /*
+        Search of the last stv0900 chip or
+        find it by i2c adapter and i2c address */
+       while ((temp_dev != NULL) &&
+               ((temp_dev->internal->i2c_adap != i2c_adap) ||
+               (temp_dev->internal->i2c_addr != i2c_addr))) {
+
+               temp_dev = temp_dev->next_dev;
+       }
+
+       return temp_dev;
+}
+
+/* deallocating chip */
+static void remove_dev(struct stv090x_internal *internal)
+{
+       struct stv090x_dev *prev_dev = stv090x_first_dev;
+       struct stv090x_dev *del_dev = find_dev(internal->i2c_adap,
+                                               internal->i2c_addr);
+
+       if (del_dev != NULL) {
+               if (del_dev == stv090x_first_dev) {
+                       stv090x_first_dev = del_dev->next_dev;
+               } else {
+                       while (prev_dev->next_dev != del_dev)
+                               prev_dev = prev_dev->next_dev;
+
+                       prev_dev->next_dev = del_dev->next_dev;
+               }
+
+               kfree(del_dev);
+       }
+}
+
+/* allocating new chip */
+static struct stv090x_dev *append_internal(struct stv090x_internal *internal)
+{
+       struct stv090x_dev *new_dev;
+       struct stv090x_dev *temp_dev;
+
+       new_dev = kmalloc(sizeof(struct stv090x_dev), GFP_KERNEL);
+       if (new_dev != NULL) {
+               new_dev->internal = internal;
+               new_dev->next_dev = NULL;
+
+               /* append to list */
+               if (stv090x_first_dev == NULL) {
+                       stv090x_first_dev = new_dev;
+               } else {
+                       temp_dev = stv090x_first_dev;
+                       while (temp_dev->next_dev != NULL)
+                               temp_dev = temp_dev->next_dev;
+
+                       temp_dev->next_dev = new_dev;
+               }
+       }
+
+       return new_dev;
+}
+
 
 /* DVBS1 and DSS C/N Lookup table */
 static const struct stv090x_tab stv090x_s1cn_tab[] = {
@@ -503,25 +578,6 @@ static struct stv090x_reg stv0903_cut20_val[] = {
        { STV090x_GAINLLR_NF17,         0x21 }
 };
 
-/* Cut 1.x Long Frame Tracking CR loop */
-static struct stv090x_long_frame_crloop stv090x_s2_crl[] = {
-       /* MODCOD     2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
-       { STV090x_QPSK_12,  0x1c, 0x0d, 0x1b, 0x2c, 0x3a, 0x1c, 0x2a, 0x3b, 0x2a, 0x1b },
-       { STV090x_QPSK_35,  0x2c, 0x0d, 0x2b, 0x2c, 0x3a, 0x0c, 0x3a, 0x2b, 0x2a, 0x0b },
-       { STV090x_QPSK_23,  0x2c, 0x0d, 0x2b, 0x2c, 0x0b, 0x0c, 0x3a, 0x1b, 0x2a, 0x3a },
-       { STV090x_QPSK_34,  0x3c, 0x0d, 0x3b, 0x1c, 0x0b, 0x3b, 0x3a, 0x0b, 0x2a, 0x3a },
-       { STV090x_QPSK_45,  0x3c, 0x0d, 0x3b, 0x1c, 0x0b, 0x3b, 0x3a, 0x0b, 0x2a, 0x3a },
-       { STV090x_QPSK_56,  0x0d, 0x0d, 0x3b, 0x1c, 0x0b, 0x3b, 0x3a, 0x0b, 0x2a, 0x3a },
-       { STV090x_QPSK_89,  0x0d, 0x0d, 0x3b, 0x1c, 0x1b, 0x3b, 0x3a, 0x0b, 0x2a, 0x3a },
-       { STV090x_QPSK_910, 0x1d, 0x0d, 0x3b, 0x1c, 0x1b, 0x3b, 0x3a, 0x0b, 0x2a, 0x3a },
-       { STV090x_8PSK_35,  0x29, 0x3b, 0x09, 0x2b, 0x38, 0x0b, 0x18, 0x1a, 0x08, 0x0a },
-       { STV090x_8PSK_23,  0x0a, 0x3b, 0x29, 0x2b, 0x19, 0x0b, 0x38, 0x1a, 0x18, 0x0a },
-       { STV090x_8PSK_34,  0x3a, 0x3b, 0x2a, 0x2b, 0x39, 0x0b, 0x19, 0x1a, 0x38, 0x0a },
-       { STV090x_8PSK_56,  0x1b, 0x3b, 0x0b, 0x2b, 0x1a, 0x0b, 0x39, 0x1a, 0x19, 0x0a },
-       { STV090x_8PSK_89,  0x3b, 0x3b, 0x0b, 0x2b, 0x2a, 0x0b, 0x39, 0x1a, 0x29, 0x39 },
-       { STV090x_8PSK_910, 0x3b, 0x3b, 0x0b, 0x2b, 0x2a, 0x0b, 0x39, 0x1a, 0x29, 0x39 }
-};
-
 /* Cut 2.0 Long Frame Tracking CR loop */
 static struct stv090x_long_frame_crloop stv090x_s2_crl_cut20[] = {
        /* MODCOD  2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
@@ -541,6 +597,24 @@ static struct stv090x_long_frame_crloop stv090x_s2_crl_cut20[] = {
        { STV090x_8PSK_910, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x1d, 0x2d, 0x0d, 0x1d }
 };
 
+/* Cut 3.0 Long Frame Tracking CR loop */
+static struct stv090x_long_frame_crloop stv090x_s2_crl_cut30[] = {
+       /* MODCOD  2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
+       { STV090x_QPSK_12,  0x3c, 0x2c, 0x0c, 0x2c, 0x1b, 0x2c, 0x1b, 0x1c, 0x0b, 0x3b },
+       { STV090x_QPSK_35,  0x0d, 0x0d, 0x0c, 0x0d, 0x1b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
+       { STV090x_QPSK_23,  0x1d, 0x0d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
+       { STV090x_QPSK_34,  0x1d, 0x1d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
+       { STV090x_QPSK_45,  0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
+       { STV090x_QPSK_56,  0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
+       { STV090x_QPSK_89,  0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
+       { STV090x_QPSK_910, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
+       { STV090x_8PSK_35,  0x39, 0x29, 0x39, 0x19, 0x19, 0x19, 0x19, 0x19, 0x09, 0x19 },
+       { STV090x_8PSK_23,  0x2a, 0x39, 0x1a, 0x0a, 0x39, 0x0a, 0x29, 0x39, 0x29, 0x0a },
+       { STV090x_8PSK_34,  0x2b, 0x3a, 0x1b, 0x1b, 0x3a, 0x1b, 0x1a, 0x0b, 0x1a, 0x3a },
+       { STV090x_8PSK_56,  0x0c, 0x1b, 0x3b, 0x3b, 0x1b, 0x3b, 0x3a, 0x3b, 0x3a, 0x1b },
+       { STV090x_8PSK_89,  0x0d, 0x3c, 0x2c, 0x2c, 0x2b, 0x0c, 0x0b, 0x3b, 0x0b, 0x1b },
+       { STV090x_8PSK_910, 0x0d, 0x0d, 0x2c, 0x3c, 0x3b, 0x1c, 0x0b, 0x3b, 0x0b, 0x1b }
+};
 
 /* Cut 2.0 Long Frame Tracking CR Loop */
 static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut20[] = {
@@ -558,6 +632,21 @@ static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut20[] = {
        { STV090x_32APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c }
 };
 
+/* Cut 3.0 Long Frame Tracking CR Loop */
+static struct stv090x_long_frame_crloop        stv090x_s2_apsk_crl_cut30[] = {
+       /* MODCOD  2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
+       { STV090x_16APSK_23,  0x0a, 0x0a, 0x0a, 0x0a, 0x1a, 0x0a, 0x3a, 0x0a, 0x2a, 0x0a },
+       { STV090x_16APSK_34,  0x0a, 0x0a, 0x0a, 0x0a, 0x0b, 0x0a, 0x3b, 0x0a, 0x1b, 0x0a },
+       { STV090x_16APSK_45,  0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
+       { STV090x_16APSK_56,  0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
+       { STV090x_16APSK_89,  0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
+       { STV090x_16APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
+       { STV090x_32APSK_34,  0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
+       { STV090x_32APSK_45,  0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
+       { STV090x_32APSK_56,  0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
+       { STV090x_32APSK_89,  0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
+       { STV090x_32APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a }
+};
 
 static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut20[] = {
        /* MODCOD  2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
@@ -566,16 +655,30 @@ static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut20[] = {
        { STV090x_QPSK_25,  0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x2e }
 };
 
+static struct stv090x_long_frame_crloop        stv090x_s2_lowqpsk_crl_cut30[] = {
+       /* MODCOD  2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
+       { STV090x_QPSK_14,  0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x2a, 0x1c, 0x3a, 0x3b },
+       { STV090x_QPSK_13,  0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x3a, 0x0c, 0x3a, 0x2b },
+       { STV090x_QPSK_25,  0x1c, 0x3c, 0x1b, 0x3c, 0x3a, 0x1c, 0x3a, 0x3b, 0x3a, 0x2b }
+};
 
-/* Cut 1.2 & 2.0 Short Frame Tracking CR Loop */
-static struct stv090x_short_frame_crloop stv090x_s2_short_crl[] = {
-       /* MODCOD  2M_cut1.2 2M_cut2.0 5M_cut1.2 5M_cut2.0 10M_cut1.2 10M_cut2.0 20M_cut1.2 20M_cut2.0 30M_cut1.2 30M_cut2.0 */
-       { STV090x_QPSK,   0x3c, 0x2f, 0x2b, 0x2e, 0x0b, 0x0e, 0x3a, 0x0e, 0x2a, 0x3d },
-       { STV090x_8PSK,   0x0b, 0x3e, 0x2a, 0x0e, 0x0a, 0x2d, 0x19, 0x0d, 0x09, 0x3c },
-       { STV090x_16APSK, 0x1b, 0x1e, 0x1b, 0x1e, 0x1b, 0x1e, 0x3a, 0x3d, 0x2a, 0x2d },
-       { STV090x_32APSK, 0x1b, 0x1e, 0x1b, 0x1e, 0x1b, 0x1e, 0x3a, 0x3d, 0x2a, 0x2d }
+/* Cut 2.0 Short Frame Tracking CR Loop */
+static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut20[] = {
+       /* MODCOD         2M    5M    10M   20M   30M */
+       { STV090x_QPSK,   0x2f, 0x2e, 0x0e, 0x0e, 0x3d },
+       { STV090x_8PSK,   0x3e, 0x0e, 0x2d, 0x0d, 0x3c },
+       { STV090x_16APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d },
+       { STV090x_32APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d }
 };
 
+/* Cut 3.0 Short Frame Tracking CR Loop */
+static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut30[] = {
+       /* MODCOD         2M    5M    10M   20M   30M */
+       { STV090x_QPSK,   0x2C, 0x2B, 0x0B, 0x0B, 0x3A },
+       { STV090x_8PSK,   0x3B, 0x0B, 0x2A, 0x0A, 0x39 },
+       { STV090x_16APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A },
+       { STV090x_32APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A }
+};
 
 static inline s32 comp2(s32 __x, s32 __width)
 {
@@ -655,6 +758,9 @@ static int stv090x_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
        struct stv090x_state *state = fe->demodulator_priv;
        u32 reg;
 
+       if (enable)
+               mutex_lock(&state->internal->tuner_lock);
+
        reg = STV090x_READ_DEMOD(state, I2CRPT);
        if (enable) {
                dprintk(FE_DEBUG, 1, "Enable Gate");
@@ -668,9 +774,14 @@ static int stv090x_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
                if ((STV090x_WRITE_DEMOD(state, I2CRPT, reg)) < 0)
                        goto err;
        }
+
+       if (!enable)
+               mutex_unlock(&state->internal->tuner_lock);
+
        return 0;
 err:
        dprintk(FE_ERROR, 1, "I/O error");
+       mutex_unlock(&state->internal->tuner_lock);
        return -1;
 }
 
@@ -725,18 +836,22 @@ static int stv090x_set_srate(struct stv090x_state *state, u32 srate)
 {
        u32 sym;
 
-       if (srate > 6000000) {
-               sym  = (srate / 1000) * 65536;
-               sym /= (state->mclk / 1000);
+       if (srate > 60000000) {
+               sym  = (srate << 4); /* SR * 2^16 / master_clk */
+               sym /= (state->internal->mclk >> 12);
+       } else if (srate > 6000000) {
+               sym  = (srate << 6);
+               sym /= (state->internal->mclk >> 10);
        } else {
-               sym  = (srate / 100) * 65536;
-               sym /= (state->mclk / 100);
+               sym  = (srate << 9);
+               sym /= (state->internal->mclk >> 7);
        }
 
-       if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0) /* MSB */
+       if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0x7f) < 0) /* MSB */
                goto err;
        if (STV090x_WRITE_DEMOD(state, SFRINIT0, (sym & 0xff)) < 0) /* LSB */
                goto err;
+
        return 0;
 err:
        dprintk(FE_ERROR, 1, "I/O error");
@@ -748,17 +863,29 @@ static int stv090x_set_max_srate(struct stv090x_state *state, u32 clk, u32 srate
        u32 sym;
 
        srate = 105 * (srate / 100);
-       if (srate > 6000000) {
-               sym  = (srate / 1000) * 65536;
-               sym /= (clk / 1000);
+       if (srate > 60000000) {
+               sym  = (srate << 4); /* SR * 2^16 / master_clk */
+               sym /= (state->internal->mclk >> 12);
+       } else if (srate > 6000000) {
+               sym  = (srate << 6);
+               sym /= (state->internal->mclk >> 10);
        } else {
-               sym  = (srate / 100) * 65536;
-               sym /= (clk / 100);
+               sym  = (srate << 9);
+               sym /= (state->internal->mclk >> 7);
        }
-       if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) /* MSB */
-               goto err;
-       if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) /* LSB */
-               goto err;
+
+       if (sym < 0x7fff) {
+               if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) /* MSB */
+                       goto err;
+               if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) /* LSB */
+                       goto err;
+       } else {
+               if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x7f) < 0) /* MSB */
+                       goto err;
+               if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xff) < 0) /* LSB */
+                       goto err;
+       }
+
        return 0;
 err:
        dprintk(FE_ERROR, 1, "I/O error");
@@ -770,14 +897,18 @@ static int stv090x_set_min_srate(struct stv090x_state *state, u32 clk, u32 srate
        u32 sym;
 
        srate = 95 * (srate / 100);
-       if (srate > 6000000) {
-               sym  = (srate / 1000) * 65536;
-               sym /= (clk / 1000);
+       if (srate > 60000000) {
+               sym  = (srate << 4); /* SR * 2^16 / master_clk */
+               sym /= (state->internal->mclk >> 12);
+       } else if (srate > 6000000) {
+               sym  = (srate << 6);
+               sym /= (state->internal->mclk >> 10);
        } else {
-               sym  = (srate / 100) * 65536;
-               sym /= (clk / 100);
+               sym  = (srate << 9);
+               sym /= (state->internal->mclk >> 7);
        }
-       if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0xff)) < 0) /* MSB */
+
+       if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0x7f)) < 0) /* MSB */
                goto err;
        if (STV090x_WRITE_DEMOD(state, SFRLOW0, (sym & 0xff)) < 0) /* LSB */
                goto err;
@@ -787,9 +918,24 @@ err:
        return -1;
 }
 
-static u32 stv090x_car_width(u32 srate, u32 rolloff)
+static u32 stv090x_car_width(u32 srate, enum stv090x_rolloff rolloff)
 {
-       return srate + (srate * rolloff) / 100;
+       u32 ro;
+
+       switch (rolloff) {
+       case STV090x_RO_20:
+               ro = 20;
+               break;
+       case STV090x_RO_25:
+               ro = 25;
+               break;
+       case STV090x_RO_35:
+       default:
+               ro = 35;
+               break;
+       }
+
+       return srate + (srate * ro) / 100;
 }
 
 static int stv090x_set_vit_thacq(struct stv090x_state *state)
@@ -952,74 +1098,83 @@ err:
 
 static int stv090x_activate_modcod(struct stv090x_state *state)
 {
-       u32 matype, modcod, f_mod, index;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xfc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xcc) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
+               goto err;
 
-       if (state->dev_ver <= 0x11) {
-               msleep(5);
-               modcod = STV090x_READ_DEMOD(state, PLHMODCOD);
-               matype = modcod & 0x03;
-               modcod = (modcod & 0x7f) >> 2;
-               index = STV090x_ADDR_OFFST(state, MODCODLSTF) - (modcod / 2);
+       return 0;
+err:
+       dprintk(FE_ERROR, 1, "I/O error");
+       return -1;
+}
+
+static int stv090x_activate_modcod_single(struct stv090x_state *state)
+{
+
+       if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xf0) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0x00) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0x0f) < 0)
+               goto err;
 
-               switch (matype) {
-               default:
-               case 0:
-                       f_mod = 14;
-                       break;
-               case 1:
-                       f_mod = 13;
-                       break;
-               case 2:
-                       f_mod = 11;
-                       break;
-               case 3:
-                       f_mod = 7;
-                       break;
-               }
-               if (matype <= 1) {
-                       if (modcod % 2) {
-                               if (stv090x_write_reg(state, index, 0xf0 | f_mod) < 0)
-                                       goto err;
-                       } else {
-                               if (stv090x_write_reg(state, index, (f_mod << 4) | 0x0f) < 0)
-                                       goto err;
-                       }
-               }
-       } else if (state->dev_ver >= 0x12) {
-                       if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xfc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xcc) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
-                               goto err;
-       }
        return 0;
+
 err:
        dprintk(FE_ERROR, 1, "I/O error");
        return -1;
@@ -1031,21 +1186,21 @@ static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
 
        switch (state->demod) {
        case STV090x_DEMODULATOR_0:
-               mutex_lock(&demod_lock);
+               mutex_lock(&state->internal->demod_lock);
                reg = stv090x_read_reg(state, STV090x_STOPCLK2);
                STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, enable);
                if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
                        goto err;
-               mutex_unlock(&demod_lock);
+               mutex_unlock(&state->internal->demod_lock);
                break;
 
        case STV090x_DEMODULATOR_1:
-               mutex_lock(&demod_lock);
+               mutex_lock(&state->internal->demod_lock);
                reg = stv090x_read_reg(state, STV090x_STOPCLK2);
                STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, enable);
                if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
                        goto err;
-               mutex_unlock(&demod_lock);
+               mutex_unlock(&state->internal->demod_lock);
                break;
 
        default:
@@ -1054,7 +1209,41 @@ static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
        }
        return 0;
 err:
-       mutex_unlock(&demod_lock);
+       mutex_unlock(&state->internal->demod_lock);
+       dprintk(FE_ERROR, 1, "I/O error");
+       return -1;
+}
+
+static int stv090x_dvbs_track_crl(struct stv090x_state *state)
+{
+       if (state->internal->dev_ver >= 0x30) {
+               /* Set ACLC BCLC optimised value vs SR */
+               if (state->srate >= 15000000) {
+                       if (STV090x_WRITE_DEMOD(state, ACLC, 0x2b) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, BCLC, 0x1a) < 0)
+                               goto err;
+               } else if ((state->srate >= 7000000) && (15000000 > state->srate)) {
+                       if (STV090x_WRITE_DEMOD(state, ACLC, 0x0c) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, BCLC, 0x1b) < 0)
+                               goto err;
+               } else if (state->srate < 7000000) {
+                       if (STV090x_WRITE_DEMOD(state, ACLC, 0x2c) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, BCLC, 0x1c) < 0)
+                               goto err;
+               }
+
+       } else {
+               /* Cut 2.0 */
+               if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
+                       goto err;
+               if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
+                       goto err;
+       }
+       return 0;
+err:
        dprintk(FE_ERROR, 1, "I/O error");
        return -1;
 }
@@ -1072,19 +1261,22 @@ static int stv090x_delivery_search(struct stv090x_state *state)
                if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
                        goto err;
 
-               /* Activate Viterbi decoder in legacy search, do not use FRESVIT1, might impact VITERBI2 */
+               /* Activate Viterbi decoder in legacy search,
+                * do not use FRESVIT1, might impact VITERBI2
+                */
                if (stv090x_vitclk_ctl(state, 0) < 0)
                        goto err;
 
-               if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
-                       goto err;
-               if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
+               if (stv090x_dvbs_track_crl(state) < 0)
                        goto err;
+
                if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x22) < 0) /* disable DVB-S2 */
                        goto err;
 
-               stv090x_set_vit_thacq(state);
-               stv090x_set_viterbi(state);
+               if (stv090x_set_vit_thacq(state) < 0)
+                       goto err;
+               if (stv090x_set_viterbi(state) < 0)
+                       goto err;
                break;
 
        case STV090x_SEARCH_DVBS2:
@@ -1105,19 +1297,37 @@ static int stv090x_delivery_search(struct stv090x_state *state)
                        goto err;
                if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
                        goto err;
-               if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
-                       goto err;
+
+               if (state->internal->dev_ver <= 0x20) {
+                       /* enable S2 carrier loop */
+                       if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
+                               goto err;
+               } else {
+                       /* > Cut 3: Stop carrier 3 */
+                       if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
+                               goto err;
+               }
 
                if (state->demod_mode != STV090x_SINGLE) {
-                       if (state->dev_ver <= 0x11) /* 900 in dual TS mode */
-                               stv090x_stop_modcod(state);
-                       else
-                               stv090x_activate_modcod(state);
+                       /* Cut 2: enable link during search */
+                       if (stv090x_activate_modcod(state) < 0)
+                               goto err;
+               } else {
+                       /* Single demodulator
+                        * Authorize SHORT and LONG frames,
+                        * QPSK, 8PSK, 16APSK and 32APSK
+                        */
+                       if (stv090x_activate_modcod_single(state) < 0)
+                               goto err;
                }
+
+               if (stv090x_set_vit_thtracq(state) < 0)
+                       goto err;
                break;
 
        case STV090x_SEARCH_AUTO:
        default:
+               /* enable DVB-S2 and DVB-S2 in Auto MODE */
                reg = STV090x_READ_DEMOD(state, DMDCFGMD);
                STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
                STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
@@ -1128,24 +1338,40 @@ static int stv090x_delivery_search(struct stv090x_state *state)
                if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
                        goto err;
 
-               if (stv090x_vitclk_ctl(state, 1) < 0)
+               if (stv090x_vitclk_ctl(state, 0) < 0)
                        goto err;
 
-               if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
-                       goto err;
-               if (STV090x_WRITE_DEMOD(state, ACLC, 0x09) < 0)
-                       goto err;
-               if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
+               if (stv090x_dvbs_track_crl(state) < 0)
                        goto err;
 
+               if (state->internal->dev_ver <= 0x20) {
+                       /* enable S2 carrier loop */
+                       if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
+                               goto err;
+               } else {
+                       /* > Cut 3: Stop carrier 3 */
+                       if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
+                               goto err;
+               }
+
                if (state->demod_mode != STV090x_SINGLE) {
-                       if (state->dev_ver <= 0x11) /* 900 in dual TS mode */
-                               stv090x_stop_modcod(state);
-                       else
-                               stv090x_activate_modcod(state);
+                       /* Cut 2: enable link during search */
+                       if (stv090x_activate_modcod(state) < 0)
+                               goto err;
+               } else {
+                       /* Single demodulator
+                        * Authorize SHORT and LONG frames,
+                        * QPSK, 8PSK, 16APSK and 32APSK
+                        */
+                       if (stv090x_activate_modcod_single(state) < 0)
+                               goto err;
                }
-               stv090x_set_vit_thacq(state);
-               stv090x_set_viterbi(state);
+
+               if (stv090x_set_vit_thacq(state) < 0)
+                       goto err;
+
+               if (stv090x_set_viterbi(state) < 0)
+                       goto err;
                break;
        }
        return 0;
@@ -1156,51 +1382,93 @@ err:
 
 static int stv090x_start_search(struct stv090x_state *state)
 {
-       u32 reg;
+       u32 reg, freq_abs;
+       s16 freq;
 
+       /* Reset demodulator */
        reg = STV090x_READ_DEMOD(state, DMDISTATE);
        STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f);
        if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
                goto err;
 
-       if (state->dev_ver == 0x10) {
-               if (STV090x_WRITE_DEMOD(state, CORRELEXP, 0xaa) < 0)
-                       goto err;
-       }
-       if (state->dev_ver < 0x20) {
-               if (STV090x_WRITE_DEMOD(state, CARHDR, 0x55) < 0)
-                       goto err;
-       }
-       if (state->srate <= 5000000) {
-               if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0)
-                       goto err;
-               if (STV090x_WRITE_DEMOD(state, CFRUP1, 0x0f) < 0)
-                       goto err;
-               if (STV090x_WRITE_DEMOD(state, CFRUP1, 0xff) < 0)
-                       goto err;
-               if (STV090x_WRITE_DEMOD(state, CFRLOW1, 0xf0) < 0)
+       if (state->internal->dev_ver <= 0x20) {
+               if (state->srate <= 5000000) {
+                       if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, CFRUP1, 0x0f) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, CFRUP0, 0xff) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, CFRLOW1, 0xf0) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, CFRLOW0, 0x00) < 0)
+                               goto err;
+
+                       /*enlarge the timing bandwith for Low SR*/
+                       if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0)
+                               goto err;
+               } else {
+                       /* If the symbol rate is >5 Msps
+                       Set The carrier search up and low to auto mode */
+                       if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
+                               goto err;
+                       /*reduce the timing bandwith for high SR*/
+                       if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
+                               goto err;
+               }
+       } else {
+               /* >= Cut 3 */
+               if (state->srate <= 5000000) {
+                       /* enlarge the timing bandwith for Low SR */
+                       STV090x_WRITE_DEMOD(state, RTCS2, 0x68);
+               } else {
+                       /* reduce timing bandwith for high SR */
+                       STV090x_WRITE_DEMOD(state, RTCS2, 0x44);
+               }
+
+               /* Set CFR min and max to manual mode */
+               STV090x_WRITE_DEMOD(state, CARCFG, 0x46);
+
+               if (state->algo == STV090x_WARM_SEARCH) {
+                       /* WARM Start
+                        * CFR min = -1MHz,
+                        * CFR max = +1MHz
+                        */
+                       freq_abs  = 1000 << 16;
+                       freq_abs /= (state->internal->mclk / 1000);
+                       freq      = (s16) freq_abs;
+               } else {
+                       /* COLD Start
+                        * CFR min =- (SearchRange / 2 + 600KHz)
+                        * CFR max = +(SearchRange / 2 + 600KHz)
+                        * (600KHz for the tuner step size)
+                        */
+                       freq_abs  = (state->search_range / 2000) + 600;
+                       freq_abs  = freq_abs << 16;
+                       freq_abs /= (state->internal->mclk / 1000);
+                       freq      = (s16) freq_abs;
+               }
+
+               if (STV090x_WRITE_DEMOD(state, CFRUP1, MSB(freq)) < 0)
                        goto err;
-               if (STV090x_WRITE_DEMOD(state, CFRLOW0, 0x00) < 0)
+               if (STV090x_WRITE_DEMOD(state, CFRUP0, LSB(freq)) < 0)
                        goto err;
 
-               /*enlarge the timing bandwith for Low SR*/
-               if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0)
-                       goto err;
-       } else {
-               /* If the symbol rate is >5 Msps
-               Set The carrier search up and low to auto mode */
-               if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
+               freq *= -1;
+
+               if (STV090x_WRITE_DEMOD(state, CFRLOW1, MSB(freq)) < 0)
                        goto err;
-               /*reduce the timing bandwith for high SR*/
-               if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
+               if (STV090x_WRITE_DEMOD(state, CFRLOW0, LSB(freq)) < 0)
                        goto err;
+
        }
+
        if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0) < 0)
                goto err;
        if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0) < 0)
                goto err;
 
-       if (state->dev_ver >= 0x20) {
+       if (state->internal->dev_ver >= 0x20) {
                if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
                        goto err;
                if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
@@ -1234,13 +1502,33 @@ static int stv090x_start_search(struct stv090x_state *state)
        if (STV090x_WRITE_DEMOD(state, DMDCFG2, reg) < 0)
                goto err;
 
-       if (state->dev_ver >= 0x20) { /*Frequency offset detector setting*/
-               if (state->srate < 10000000) {
+       if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0)
+               goto err;
+
+       if (state->internal->dev_ver >= 0x20) {
+               /*Frequency offset detector setting*/
+               if (state->srate < 2000000) {
+                       if (state->internal->dev_ver <= 0x20) {
+                               /* Cut 2 */
+                               if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x39) < 0)
+                                       goto err;
+                       } else {
+                               /* Cut 3 */
+                               if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x89) < 0)
+                                       goto err;
+                       }
+                       if (STV090x_WRITE_DEMOD(state, CARHDR, 0x40) < 0)
+                               goto err;
+               } else if (state->srate < 10000000) {
                        if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4c) < 0)
                                goto err;
+                       if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
+                               goto err;
                } else {
                        if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4b) < 0)
                                goto err;
+                       if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
+                               goto err;
                }
        } else {
                if (state->srate < 10000000) {
@@ -1253,14 +1541,18 @@ static int stv090x_start_search(struct stv090x_state *state)
        }
 
        switch (state->algo) {
-       case STV090x_WARM_SEARCH:/*The symbol rate and the exact carrier Frequency are known */
+       case STV090x_WARM_SEARCH:
+               /* The symbol rate and the exact
+                * carrier Frequency are known
+                */
                if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
                        goto err;
                if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
                        goto err;
                break;
 
-       case STV090x_COLD_SEARCH:/*The symbol rate is known*/
+       case STV090x_COLD_SEARCH:
+               /* The symbol rate is known */
                if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
                        goto err;
                if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
@@ -1278,14 +1570,14 @@ err:
 
 static int stv090x_get_agc2_min_level(struct stv090x_state *state)
 {
-       u32 agc2_min = 0, agc2 = 0, freq_init, freq_step, reg;
+       u32 agc2_min = 0xffff, agc2 = 0, freq_init, freq_step, reg;
        s32 i, j, steps, dir;
 
        if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
                goto err;
        reg = STV090x_READ_DEMOD(state, DMDCFGMD);
-       STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 1);
-       STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 1);
+       STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
+       STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
        if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
                goto err;
 
@@ -1299,16 +1591,15 @@ static int stv090x_get_agc2_min_level(struct stv090x_state *state)
                goto err;
        if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0) /* stop acq @ coarse carrier state */
                goto err;
-       stv090x_set_srate(state, 1000000);
+       if (stv090x_set_srate(state, 1000000) < 0)
+               goto err;
 
-       steps  = -1 + state->search_range / 1000000;
-       steps /= 2;
-       steps  = (2 * steps) + 1;
-       if (steps < 0)
+       steps  = state->search_range / 1000000;
+       if (steps <= 0)
                steps = 1;
 
        dir = 1;
-       freq_step = (1000000 * 256) / (state->mclk / 256);
+       freq_step = (1000000 * 256) / (state->internal->mclk / 256);
        freq_init = 0;
 
        for (i = 0; i < steps; i++) {
@@ -1317,7 +1608,7 @@ static int stv090x_get_agc2_min_level(struct stv090x_state *state)
                else
                        freq_init = freq_init - (freq_step * i);
 
-               dir = -1;
+               dir *= -1;
 
                if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod RESET */
                        goto err;
@@ -1328,13 +1619,14 @@ static int stv090x_get_agc2_min_level(struct stv090x_state *state)
                if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x58) < 0) /* Demod RESET */
                        goto err;
                msleep(10);
+
+               agc2 = 0;
                for (j = 0; j < 10; j++) {
-                       agc2 += STV090x_READ_DEMOD(state, AGC2I1) << 8;
-                       agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
+                       agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
+                               STV090x_READ_DEMOD(state, AGC2I0);
                }
                agc2 /= 10;
-               agc2_min = 0xffff;
-               if (agc2 < 0xffff)
+               if (agc2 < agc2_min)
                        agc2_min = agc2;
        }
 
@@ -1348,7 +1640,6 @@ static u32 stv090x_get_srate(struct stv090x_state *state, u32 clk)
 {
        u8 r3, r2, r1, r0;
        s32 srate, int_1, int_2, tmp_1, tmp_2;
-       u32 pow2;
 
        r3 = STV090x_READ_DEMOD(state, SFR3);
        r2 = STV090x_READ_DEMOD(state, SFR2);
@@ -1357,16 +1648,15 @@ static u32 stv090x_get_srate(struct stv090x_state *state, u32 clk)
 
        srate = ((r3 << 24) | (r2 << 16) | (r1 <<  8) | r0);
 
-       pow2 = 1 << 16;
-       int_1 = clk / pow2;
-       int_2 = srate / pow2;
+       int_1 = clk >> 16;
+       int_2 = srate >> 16;
 
-       tmp_1 = clk % pow2;
-       tmp_2 = srate % pow2;
+       tmp_1 = clk % 0x10000;
+       tmp_2 = srate % 0x10000;
 
        srate = (int_1 * int_2) +
-               ((int_1 * tmp_2) / pow2) +
-               ((int_2 * tmp_1) / pow2);
+               ((int_1 * tmp_2) >> 16) +
+               ((int_2 * tmp_1) >> 16);
 
        return srate;
 }
@@ -1378,6 +1668,12 @@ static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
        int tmg_lock = 0, i;
        s32 tmg_cpt = 0, dir = 1, steps, cur_step = 0, freq;
        u32 srate_coarse = 0, agc2 = 0, car_step = 1200, reg;
+       u32 agc2th;
+
+       if (state->internal->dev_ver >= 0x30)
+               agc2th = 0x2e00;
+       else
+               agc2th = 0x1f00;
 
        reg = STV090x_READ_DEMOD(state, DMDISTATE);
        STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f); /* Demod RESET */
@@ -1385,13 +1681,15 @@ static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
                goto err;
        if (STV090x_WRITE_DEMOD(state, TMGCFG, 0x12) < 0)
                goto err;
+       if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0)
+               goto err;
        if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xf0) < 0)
                goto err;
        if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xe0) < 0)
                goto err;
        reg = STV090x_READ_DEMOD(state, DMDCFGMD);
        STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 1);
-       STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 1);
+       STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
        if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
                goto err;
 
@@ -1405,18 +1703,19 @@ static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
                goto err;
        if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0)
                goto err;
-       if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x60) < 0)
+       if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x50) < 0)
                goto err;
 
-       if (state->dev_ver >= 0x20) {
-               if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0)
+       if (state->internal->dev_ver >= 0x30) {
+               if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x99) < 0)
                        goto err;
-               if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
+               if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x98) < 0)
                        goto err;
-       } else {
-               if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
+
+       } else if (state->internal->dev_ver >= 0x20) {
+               if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0)
                        goto err;
-               if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x73) < 0)
+               if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
                        goto err;
        }
 
@@ -1445,23 +1744,31 @@ static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
        while ((!tmg_lock) && (cur_step < steps)) {
                if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5f) < 0) /* Demod RESET */
                        goto err;
-               reg = STV090x_READ_DEMOD(state, DMDISTATE);
-               STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x00); /* trigger acquisition */
-               if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
+               if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
+                       goto err;
+               if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
+                       goto err;
+               if (STV090x_WRITE_DEMOD(state, SFRINIT1, 0x00) < 0)
+                       goto err;
+               if (STV090x_WRITE_DEMOD(state, SFRINIT0, 0x00) < 0)
+                       goto err;
+               /* trigger acquisition */
+               if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x40) < 0)
                        goto err;
                msleep(50);
                for (i = 0; i < 10; i++) {
                        reg = STV090x_READ_DEMOD(state, DSTATUS);
                        if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
                                tmg_cpt++;
-                       agc2 += STV090x_READ_DEMOD(state, AGC2I1) << 8;
-                       agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
+                       agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
+                               STV090x_READ_DEMOD(state, AGC2I0);
                }
                agc2 /= 10;
-               srate_coarse = stv090x_get_srate(state, state->mclk);
+               srate_coarse = stv090x_get_srate(state, state->internal->mclk);
                cur_step++;
                dir *= -1;
-               if ((tmg_cpt >= 5) && (agc2 < 0x1f00) && (srate_coarse < 55000000) && (srate_coarse > 850000))
+               if ((tmg_cpt >= 5) && (agc2 < agc2th) &&
+                   (srate_coarse < 50000000) && (srate_coarse > 850000))
                        tmg_lock = 1;
                else if (cur_step < steps) {
                        if (dir > 0)
@@ -1470,34 +1777,46 @@ static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
                                freq -= cur_step * car_step;
 
                        /* Setup tuner */
-                       stv090x_i2c_gate_ctrl(fe, 1);
+                       if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
+                               goto err;
 
-                       if (state->config->tuner_set_frequency)
-                               state->config->tuner_set_frequency(fe, state->frequency);
+                       if (state->config->tuner_set_frequency) {
+                               if (state->config->tuner_set_frequency(fe, freq) < 0)
+                                       goto err;
+                       }
 
-                       if (state->config->tuner_set_bandwidth)
-                               state->config->tuner_set_bandwidth(fe, state->tuner_bw);
+                       if (state->config->tuner_set_bandwidth) {
+                               if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
+                                       goto err;
+                       }
+
+                       if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
+                               goto err;
 
-                       stv090x_i2c_gate_ctrl(fe, 0);
                        msleep(50);
-                       stv090x_i2c_gate_ctrl(fe, 1);
 
-                       if (state->config->tuner_get_status)
-                               state->config->tuner_get_status(fe, &reg);
+                       if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
+                               goto err;
+
+                       if (state->config->tuner_get_status) {
+                               if (state->config->tuner_get_status(fe, &reg) < 0)
+                                       goto err;
+                       }
 
                        if (reg)
                                dprintk(FE_DEBUG, 1, "Tuner phase locked");
                        else
                                dprintk(FE_DEBUG, 1, "Tuner unlocked");
 
-                       stv090x_i2c_gate_ctrl(fe, 0);
+                       if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
+                               goto err;
 
                }
        }
        if (!tmg_lock)
                srate_coarse = 0;
        else
-               srate_coarse = stv090x_get_srate(state, state->mclk);
+               srate_coarse = stv090x_get_srate(state, state->internal->mclk);
 
        return srate_coarse;
 err:
@@ -1509,7 +1828,7 @@ static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
 {
        u32 srate_coarse, freq_coarse, sym, reg;
 
-       srate_coarse = stv090x_get_srate(state, state->mclk);
+       srate_coarse = stv090x_get_srate(state, state->internal->mclk);
        freq_coarse  = STV090x_READ_DEMOD(state, CFR2) << 8;
        freq_coarse |= STV090x_READ_DEMOD(state, CFR1);
        sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
@@ -1519,7 +1838,7 @@ static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
        else {
                if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0) /* Demod RESET */
                        goto err;
-               if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0x01) < 0)
+               if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
                        goto err;
                if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
                        goto err;
@@ -1532,31 +1851,34 @@ static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
                if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
                        goto err;
 
-               if (state->dev_ver >= 0x20) {
-                       if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
+               if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
+                       goto err;
+
+               if (state->internal->dev_ver >= 0x30) {
+                       if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x79) < 0)
                                goto err;
-               } else {
-                       if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
+               } else if (state->internal->dev_ver >= 0x20) {
+                       if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
                                goto err;
                }
 
                if (srate_coarse > 3000000) {
                        sym  = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
                        sym  = (sym / 1000) * 65536;
-                       sym /= (state->mclk / 1000);
+                       sym /= (state->internal->mclk / 1000);
                        if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
                                goto err;
                        if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
                                goto err;
                        sym  = 10 * (srate_coarse / 13); /* SFRLOW = SFR - 30% */
                        sym  = (sym / 1000) * 65536;
-                       sym /= (state->mclk / 1000);
+                       sym /= (state->internal->mclk / 1000);
                        if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
                                goto err;
                        if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
                                goto err;
                        sym  = (srate_coarse / 1000) * 65536;
-                       sym /= (state->mclk / 1000);
+                       sym /= (state->internal->mclk / 1000);
                        if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
                                goto err;
                        if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
@@ -1564,20 +1886,20 @@ static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
                } else {
                        sym  = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
                        sym  = (sym / 100) * 65536;
-                       sym /= (state->mclk / 100);
+                       sym /= (state->internal->mclk / 100);
                        if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
                                goto err;
                        if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
                                goto err;
                        sym  = 10 * (srate_coarse / 14); /* SFRLOW = SFR - 30% */
                        sym  = (sym / 100) * 65536;
-                       sym /= (state->mclk / 100);
+                       sym /= (state->internal->mclk / 100);
                        if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
                                goto err;
                        if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
                                goto err;
                        sym  = (srate_coarse / 100) * 65536;
-                       sym /= (state->mclk / 100);
+                       sym /= (state->internal->mclk / 100);
                        if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
                                goto err;
                        if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
@@ -1637,37 +1959,32 @@ static int stv090x_get_dmdlock(struct stv090x_state *state, s32 timeout)
 static int stv090x_blind_search(struct stv090x_state *state)
 {
        u32 agc2, reg, srate_coarse;
-       s32 timeout_dmd = 500, cpt_fail, agc2_ovflw, i;
+       s32 cpt_fail, agc2_ovflw, i;
        u8 k_ref, k_max, k_min;
        int coarse_fail, lock;
 
-       if (state->dev_ver < 0x20) {
-               k_max = 233;
-               k_min = 143;
-       } else {
-               k_max = 120;
-               k_min = 30;
-       }
+       k_max = 110;
+       k_min = 10;
 
        agc2 = stv090x_get_agc2_min_level(state);
 
-       if (agc2 > STV090x_SEARCH_AGC2_TH) {
+       if (agc2 > STV090x_SEARCH_AGC2_TH(state->internal->dev_ver)) {
                lock = 0;
        } else {
-               if (state->dev_ver == 0x10) {
-                       if (STV090x_WRITE_DEMOD(state, CORRELEXP, 0xaa) < 0)
+
+               if (state->internal->dev_ver <= 0x20) {
+                       if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
                                goto err;
-               }
-               if (state->dev_ver < 0x20) {
-                       if (STV090x_WRITE_DEMOD(state, CARHDR, 0x55) < 0)
+               } else {
+                       /* > Cut 3 */
+                       if (STV090x_WRITE_DEMOD(state, CARCFG, 0x06) < 0)
                                goto err;
                }
 
-               if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
-                       goto err;
                if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
                        goto err;
-               if (state->dev_ver >= 0x20) {
+
+               if (state->internal->dev_ver >= 0x20) {
                        if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
                                goto err;
                        if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
@@ -1686,7 +2003,8 @@ static int stv090x_blind_search(struct stv090x_state *state)
                                srate_coarse = stv090x_srate_srch_fine(state);
                                if (srate_coarse != 0) {
                                        stv090x_get_lock_tmg(state);
-                                       lock = stv090x_get_dmdlock(state, timeout_dmd);
+                                       lock = stv090x_get_dmdlock(state,
+                                                       state->DemodTimeout);
                                } else {
                                        lock = 0;
                                }
@@ -1694,8 +2012,8 @@ static int stv090x_blind_search(struct stv090x_state *state)
                                cpt_fail = 0;
                                agc2_ovflw = 0;
                                for (i = 0; i < 10; i++) {
-                                       agc2  = STV090x_READ_DEMOD(state, AGC2I1) << 8;
-                                       agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
+                                       agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
+                                               STV090x_READ_DEMOD(state, AGC2I0);
                                        if (agc2 >= 0xff00)
                                                agc2_ovflw++;
                                        reg = STV090x_READ_DEMOD(state, DSTATUS2);
@@ -1709,7 +2027,7 @@ static int stv090x_blind_search(struct stv090x_state *state)
 
                                lock = 0;
                        }
-                       k_ref -= 30;
+                       k_ref -= 20;
                } while ((k_ref >= k_min) && (!lock) && (!coarse_fail));
        }
 
@@ -1723,7 +2041,7 @@ err:
 static int stv090x_chk_tmg(struct stv090x_state *state)
 {
        u32 reg;
-       s32 tmg_cpt, i;
+       s32 tmg_cpt = 0, i;
        u8 freq, tmg_thh, tmg_thl;
        int tmg_lock;
 
@@ -1844,42 +2162,41 @@ static int stv090x_get_coldlock(struct stv090x_state *state, s32 timeout_dmd)
                                                freq -= cur_step * car_step;
 
                                        /* Setup tuner */
-                                       stv090x_i2c_gate_ctrl(fe, 1);
+                                       if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
+                                               goto err;
 
-                                       if (state->config->tuner_set_frequency)
-                                               state->config->tuner_set_frequency(fe, state->frequency);
+                                       if (state->config->tuner_set_frequency) {
+                                               if (state->config->tuner_set_frequency(fe, freq) < 0)
+                                                       goto err;
+                                       }
 
-                                       if (state->config->tuner_set_bandwidth)
-                                               state->config->tuner_set_bandwidth(fe, state->tuner_bw);
+                                       if (state->config->tuner_set_bandwidth) {
+                                               if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
+                                                       goto err;
+                                       }
 
-                                       stv090x_i2c_gate_ctrl(fe, 0);
+                                       if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
+                                               goto err;
 
                                        msleep(50);
 
-                                       stv090x_i2c_gate_ctrl(fe, 1);
+                                       if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
+                                               goto err;
 
-                                       if (state->config->tuner_get_status)
-                                               state->config->tuner_get_status(fe, &reg);
+                                       if (state->config->tuner_get_status) {
+                                               if (state->config->tuner_get_status(fe, &reg) < 0)
+                                                       goto err;
+                                       }
 
                                        if (reg)
                                                dprintk(FE_DEBUG, 1, "Tuner phase locked");
                                        else
                                                dprintk(FE_DEBUG, 1, "Tuner unlocked");
 
-                                       stv090x_i2c_gate_ctrl(fe, 0);
+                                       if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
+                                               goto err;
 
                                        STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c);
-                                       if (state->delsys == STV090x_DVBS2) {
-                                               reg = STV090x_READ_DEMOD(state, DMDCFGMD);
-                                               STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
-                                               STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
-                                               if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
-                                                       goto err;
-                                               STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
-                                               STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
-                                               if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
-                                                       goto err;
-                                       }
                                        if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
                                                goto err;
                                        if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
@@ -1910,14 +2227,15 @@ static int stv090x_get_loop_params(struct stv090x_state *state, s32 *freq_inc, s
 
        srate = state->srate;
        car_max = state->search_range / 1000;
+       car_max += car_max / 10;
        car_max  = 65536 * (car_max / 2);
-       car_max /= (state->mclk / 1000);
+       car_max /= (state->internal->mclk / 1000);
 
        if (car_max > 0x4000)
                car_max = 0x4000 ; /* maxcarrier should be<= +-1/4 Mclk */
 
        inc  = srate;
-       inc /= state->mclk / 1000;
+       inc /= state->internal->mclk / 1000;
        inc *= 256;
        inc *= 256;
        inc /= 1000;
@@ -1970,6 +2288,7 @@ static int stv090x_chk_signal(struct stv090x_state *state)
 
        offst_car  = STV090x_READ_DEMOD(state, CFR2) << 8;
        offst_car |= STV090x_READ_DEMOD(state, CFR1);
+       offst_car = comp2(offst_car, 16);
 
        agc2  = STV090x_READ_DEMOD(state, AGC2I1) << 8;
        agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
@@ -1977,7 +2296,7 @@ static int stv090x_chk_signal(struct stv090x_state *state)
 
        car_max += (car_max / 10); /* 10% margin */
        car_max  = (65536 * car_max / 2);
-       car_max /= state->mclk / 1000;
+       car_max /= state->internal->mclk / 1000;
 
        if (car_max > 0x4000)
                car_max = 0x4000;
@@ -1996,13 +2315,13 @@ static int stv090x_chk_signal(struct stv090x_state *state)
 static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 timeout, int zigzag, s32 steps_max)
 {
        int no_signal, lock = 0;
-       s32 cpt_step, offst_freq, car_max;
+       s32 cpt_step = 0, offst_freq, car_max;
        u32 reg;
 
        car_max  = state->search_range / 1000;
        car_max += (car_max / 10);
        car_max  = (65536 * car_max / 2);
-       car_max /= (state->mclk / 1000);
+       car_max /= (state->internal->mclk / 1000);
        if (car_max > 0x4000)
                car_max = 0x4000;
 
@@ -2011,7 +2330,6 @@ static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 tim
        else
                offst_freq = -car_max + inc;
 
-       cpt_step = 0;
        do {
                if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0)
                        goto err;
@@ -2027,13 +2345,6 @@ static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 tim
                if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
                        goto err;
 
-               if (state->dev_ver == 0x12) {
-                       reg = STV090x_READ_DEMOD(state, TSCFGH);
-                       STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x1);
-                       if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
-                               goto err;
-               }
-
                if (zigzag) {
                        if (offst_freq >= 0)
                                offst_freq = -offst_freq - 2 * inc;
@@ -2043,6 +2354,8 @@ static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 tim
                        offst_freq += 2 * inc;
                }
 
+               cpt_step++;
+
                lock = stv090x_get_dmdlock(state, timeout);
                no_signal = stv090x_chk_signal(state);
 
@@ -2071,32 +2384,29 @@ static int stv090x_sw_algo(struct stv090x_state *state)
        s32 dvbs2_fly_wheel;
        s32 inc, timeout_step, trials, steps_max;
 
-       stv090x_get_loop_params(state, &inc, &timeout_step, &steps_max); /* get params */
+       /* get params */
+       stv090x_get_loop_params(state, &inc, &timeout_step, &steps_max);
 
        switch (state->search_mode) {
        case STV090x_SEARCH_DVBS1:
        case STV090x_SEARCH_DSS:
                /* accelerate the frequency detector */
-               if (state->dev_ver >= 0x20) {
+               if (state->internal->dev_ver >= 0x20) {
                        if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3B) < 0)
                                goto err;
-               } else {
-                       if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
-                               goto err;
                }
+
                if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x49) < 0)
                        goto err;
                zigzag = 0;
                break;
 
        case STV090x_SEARCH_DVBS2:
-               if (state->dev_ver >= 0x20) {
+               if (state->internal->dev_ver >= 0x20) {
                        if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
                                goto err;
-               } else {
-                       if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x68) < 0)
-                               goto err;
                }
+
                if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
                        goto err;
                zigzag = 1;
@@ -2105,18 +2415,14 @@ static int stv090x_sw_algo(struct stv090x_state *state)
        case STV090x_SEARCH_AUTO:
        default:
                /* accelerate the frequency detector */
-               if (state->dev_ver >= 0x20) {
+               if (state->internal->dev_ver >= 0x20) {
                        if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3b) < 0)
                                goto err;
                        if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
                                goto err;
-               } else {
-                       if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x68) < 0)
-                               goto err;
                }
-               if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x69) < 0)
+
+               if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0xc9) < 0)
                        goto err;
                zigzag = 0;
                break;
@@ -2131,16 +2437,11 @@ static int stv090x_sw_algo(struct stv090x_state *state)
                /*run the SW search 2 times maximum*/
                if (lock || no_signal || (trials == 2)) {
                        /*Check if the demod is not losing lock in DVBS2*/
-                       if (state->dev_ver >= 0x20) {
+                       if (state->internal->dev_ver >= 0x20) {
                                if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
                                        goto err;
                                if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
                                        goto err;
-                       } else {
-                               if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
-                                       goto err;
-                               if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x88) < 0)
-                                       goto err;
                        }
 
                        reg = STV090x_READ_DEMOD(state, DMDSTATE);
@@ -2158,13 +2459,11 @@ static int stv090x_sw_algo(struct stv090x_state *state)
                                        /*FALSE lock, The demod is loosing lock */
                                        lock = 0;
                                        if (trials < 2) {
-                                               if (state->dev_ver >= 0x20) {
+                                               if (state->internal->dev_ver >= 0x20) {
                                                        if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
                                                                goto err;
-                                               } else {
-                                                       if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x68) < 0)
-                                                               goto err;
                                                }
+
                                                if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
                                                        goto err;
                                        }
@@ -2204,23 +2503,22 @@ static enum stv090x_delsys stv090x_get_std(struct stv090x_state *state)
 static s32 stv090x_get_car_freq(struct stv090x_state *state, u32 mclk)
 {
        s32 derot, int_1, int_2, tmp_1, tmp_2;
-       u32 pow2;
 
        derot  = STV090x_READ_DEMOD(state, CFR2) << 16;
        derot |= STV090x_READ_DEMOD(state, CFR1) <<  8;
        derot |= STV090x_READ_DEMOD(state, CFR0);
 
        derot = comp2(derot, 24);
-       pow2 = 1 << 12;
-       int_1 = state->mclk / pow2;
-       int_2 = derot / pow2;
+       int_1 = mclk >> 12;
+       int_2 = derot >> 12;
 
-       tmp_1 = state->mclk % pow2;
-       tmp_2 = derot % pow2;
+       /* carrier_frequency = MasterClock * Reg / 2^24 */
+       tmp_1 = mclk % 0x1000;
+       tmp_2 = derot % 0x1000;
 
        derot = (int_1 * int_2) +
-               ((int_1 * tmp_2) / pow2) +
-               ((int_1 * tmp_1) / pow2);
+               ((int_1 * tmp_2) >> 12) +
+               ((int_2 * tmp_1) >> 12);
 
        return derot;
 }
@@ -2278,7 +2576,7 @@ static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *st
        if (state->algo == STV090x_BLIND_SEARCH) {
                tmg = STV090x_READ_DEMOD(state, TMGREG2);
                STV090x_WRITE_DEMOD(state, SFRSTEP, 0x5c);
-               while ((i <= 50) && (!tmg) && (tmg != 0xff)) {
+               while ((i <= 50) && (tmg != 0) && (tmg != 0xff)) {
                        tmg = STV090x_READ_DEMOD(state, TMGREG2);
                        msleep(5);
                        i += 5;
@@ -2286,16 +2584,23 @@ static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *st
        }
        state->delsys = stv090x_get_std(state);
 
-       stv090x_i2c_gate_ctrl(fe, 1);
+       if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
+               goto err;
 
-       if (state->config->tuner_get_frequency)
-               state->config->tuner_get_frequency(fe, &state->frequency);
+       if (state->config->tuner_get_frequency) {
+               if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
+                       goto err;
+       }
 
-       stv090x_i2c_gate_ctrl(fe, 0);
+       if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
+               goto err;
 
-       offst_freq = stv090x_get_car_freq(state, state->mclk) / 1000;
+       offst_freq = stv090x_get_car_freq(state, state->internal->mclk) / 1000;
        state->frequency += offst_freq;
-       stv090x_get_viterbi(state);
+
+       if (stv090x_get_viterbi(state) < 0)
+               goto err;
+
        reg = STV090x_READ_DEMOD(state, DMDMODCOD);
        state->modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
        state->pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
@@ -2307,15 +2612,19 @@ static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *st
 
        if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000)) {
 
-               stv090x_i2c_gate_ctrl(fe, 1);
+               if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
+                       goto err;
 
-               if (state->config->tuner_get_frequency)
-                       state->config->tuner_get_frequency(fe, &state->frequency);
+               if (state->config->tuner_get_frequency) {
+                       if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
+                               goto err;
+               }
 
-               stv090x_i2c_gate_ctrl(fe, 0);
+               if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
+                       goto err;
 
                if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
-                       return  STV090x_RANGEOK;
+                       return STV090x_RANGEOK;
                else if (abs(offst_freq) <= (stv090x_car_width(state->srate, state->rolloff) / 2000))
                        return STV090x_RANGEOK;
                else
@@ -2328,24 +2637,24 @@ static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *st
        }
 
        return STV090x_OUTOFRANGE;
+err:
+       dprintk(FE_ERROR, 1, "I/O error");
+       return -1;
 }
 
 static u32 stv090x_get_tmgoffst(struct stv090x_state *state, u32 srate)
 {
        s32 offst_tmg;
-       s32 pow2;
 
        offst_tmg  = STV090x_READ_DEMOD(state, TMGREG2) << 16;
        offst_tmg |= STV090x_READ_DEMOD(state, TMGREG1) <<  8;
        offst_tmg |= STV090x_READ_DEMOD(state, TMGREG0);
 
-       pow2 = 1 << 24;
-
        offst_tmg = comp2(offst_tmg, 24); /* 2's complement */
        if (!offst_tmg)
                offst_tmg = 1;
 
-       offst_tmg  = ((s32) srate * 10) / (pow2 / offst_tmg);
+       offst_tmg  = ((s32) srate * 10) / ((s32) 0x1000000 / offst_tmg);
        offst_tmg /= 320;
 
        return offst_tmg;
@@ -2355,19 +2664,22 @@ static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_mod
 {
        u8 aclc = 0x29;
        s32 i;
-       struct stv090x_long_frame_crloop *car_loop;
-
-       if (state->dev_ver <= 0x12)
-               car_loop = stv090x_s2_crl;
-       else if (state->dev_ver == 0x20)
-               car_loop = stv090x_s2_crl_cut20;
-       else
-               car_loop = stv090x_s2_crl;
+       struct stv090x_long_frame_crloop *car_loop, *car_loop_qpsk_low, *car_loop_apsk_low;
 
+       if (state->internal->dev_ver == 0x20) {
+               car_loop                = stv090x_s2_crl_cut20;
+               car_loop_qpsk_low       = stv090x_s2_lowqpsk_crl_cut20;
+               car_loop_apsk_low       = stv090x_s2_apsk_crl_cut20;
+       } else {
+               /* >= Cut 3 */
+               car_loop                = stv090x_s2_crl_cut30;
+               car_loop_qpsk_low       = stv090x_s2_lowqpsk_crl_cut30;
+               car_loop_apsk_low       = stv090x_s2_apsk_crl_cut30;
+       }
 
        if (modcod < STV090x_QPSK_12) {
                i = 0;
-               while ((i < 3) && (modcod != stv090x_s2_lowqpsk_crl_cut20[i].modcod))
+               while ((i < 3) && (modcod != car_loop_qpsk_low[i].modcod))
                        i++;
 
                if (i >= 3)
@@ -2380,7 +2692,7 @@ static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_mod
 
                if (i >= 14) {
                        i = 0;
-                       while ((i < 11) && (modcod != stv090x_s2_lowqpsk_crl_cut20[i].modcod))
+                       while ((i < 11) && (modcod != car_loop_apsk_low[i].modcod))
                                i++;
 
                        if (i >= 11)
@@ -2391,26 +2703,26 @@ static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_mod
        if (modcod <= STV090x_QPSK_25) {
                if (pilots) {
                        if (state->srate <= 3000000)
-                               aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_on_2;
+                               aclc = car_loop_qpsk_low[i].crl_pilots_on_2;
                        else if (state->srate <= 7000000)
-                               aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_on_5;
+                               aclc = car_loop_qpsk_low[i].crl_pilots_on_5;
                        else if (state->srate <= 15000000)
-                               aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_on_10;
+                               aclc = car_loop_qpsk_low[i].crl_pilots_on_10;
                        else if (state->srate <= 25000000)
-                               aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_on_20;
+                               aclc = car_loop_qpsk_low[i].crl_pilots_on_20;
                        else
-                               aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_on_30;
+                               aclc = car_loop_qpsk_low[i].crl_pilots_on_30;
                } else {
                        if (state->srate <= 3000000)
-                               aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_off_2;
+                               aclc = car_loop_qpsk_low[i].crl_pilots_off_2;
                        else if (state->srate <= 7000000)
-                               aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_off_5;
+                               aclc = car_loop_qpsk_low[i].crl_pilots_off_5;
                        else if (state->srate <= 15000000)
-                               aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_off_10;
+                               aclc = car_loop_qpsk_low[i].crl_pilots_off_10;
                        else if (state->srate <= 25000000)
-                               aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_off_20;
+                               aclc = car_loop_qpsk_low[i].crl_pilots_off_20;
                        else
-                               aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_off_30;
+                               aclc = car_loop_qpsk_low[i].crl_pilots_off_30;
                }
 
        } else if (modcod <= STV090x_8PSK_910) {
@@ -2439,15 +2751,15 @@ static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_mod
                }
        } else { /* 16APSK and 32APSK */
                if (state->srate <= 3000000)
-                       aclc = stv090x_s2_apsk_crl_cut20[i].crl_pilots_on_2;
+                       aclc = car_loop_apsk_low[i].crl_pilots_on_2;
                else if (state->srate <= 7000000)
-                       aclc = stv090x_s2_apsk_crl_cut20[i].crl_pilots_on_5;
+                       aclc = car_loop_apsk_low[i].crl_pilots_on_5;
                else if (state->srate <= 15000000)
-                       aclc = stv090x_s2_apsk_crl_cut20[i].crl_pilots_on_10;
+                       aclc = car_loop_apsk_low[i].crl_pilots_on_10;
                else if (state->srate <= 25000000)
-                       aclc = stv090x_s2_apsk_crl_cut20[i].crl_pilots_on_20;
+                       aclc = car_loop_apsk_low[i].crl_pilots_on_20;
                else
-                       aclc = stv090x_s2_apsk_crl_cut20[i].crl_pilots_on_30;
+                       aclc = car_loop_apsk_low[i].crl_pilots_on_30;
        }
 
        return aclc;
@@ -2455,6 +2767,7 @@ static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_mod
 
 static u8 stv090x_optimize_carloop_short(struct stv090x_state *state)
 {
+       struct stv090x_short_frame_crloop *short_crl = NULL;
        s32 index = 0;
        u8 aclc = 0x0b;
 
@@ -2474,35 +2787,25 @@ static u8 stv090x_optimize_carloop_short(struct stv090x_state *state)
                break;
        }
 
-       switch (state->dev_ver) {
-       case 0x20:
-               if (state->srate <= 3000000)
-                       aclc = stv090x_s2_short_crl[index].crl_cut20_2;
-               else if (state->srate <= 7000000)
-                       aclc = stv090x_s2_short_crl[index].crl_cut20_5;
-               else if (state->srate <= 15000000)
-                       aclc = stv090x_s2_short_crl[index].crl_cut20_10;
-               else if (state->srate <= 25000000)
-                       aclc = stv090x_s2_short_crl[index].crl_cut20_20;
-               else
-                       aclc = stv090x_s2_short_crl[index].crl_cut20_30;
-               break;
-
-       case 0x12:
-       default:
-               if (state->srate <= 3000000)
-                       aclc = stv090x_s2_short_crl[index].crl_cut12_2;
-               else if (state->srate <= 7000000)
-                       aclc = stv090x_s2_short_crl[index].crl_cut12_5;
-               else if (state->srate <= 15000000)
-                       aclc = stv090x_s2_short_crl[index].crl_cut12_10;
-               else if (state->srate <= 25000000)
-                       aclc = stv090x_s2_short_crl[index].crl_cut12_20;
-               else
-                       aclc = stv090x_s2_short_crl[index].crl_cut12_30;
-               break;
+       if (state->internal->dev_ver >= 0x30) {
+               /* Cut 3.0 and up */
+               short_crl = stv090x_s2_short_crl_cut30;
+       } else {
+               /* Cut 2.0 and up: we don't support cuts older than 2.0 */
+               short_crl = stv090x_s2_short_crl_cut20;
        }
 
+       if (state->srate <= 3000000)
+               aclc = short_crl[index].crl_2;
+       else if (state->srate <= 7000000)
+               aclc = short_crl[index].crl_5;
+       else if (state->srate <= 15000000)
+               aclc = short_crl[index].crl_10;
+       else if (state->srate <= 25000000)
+               aclc = short_crl[index].crl_20;
+       else
+               aclc = short_crl[index].crl_30;
+
        return aclc;
 }
 
@@ -2516,13 +2819,13 @@ static int stv090x_optimize_track(struct stv090x_state *state)
        s32 srate, pilots, aclc, f_1, f_0, i = 0, blind_tune = 0;
        u32 reg;
 
-       srate  = stv090x_get_srate(state, state->mclk);
+       srate  = stv090x_get_srate(state, state->internal->mclk);
        srate += stv090x_get_tmgoffst(state, srate);
 
        switch (state->delsys) {
        case STV090x_DVBS1:
        case STV090x_DSS:
-               if (state->algo == STV090x_SEARCH_AUTO) {
+               if (state->search_mode == STV090x_SEARCH_AUTO) {
                        reg = STV090x_READ_DEMOD(state, DMDCFGMD);
                        STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
                        STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
@@ -2531,17 +2834,35 @@ static int stv090x_optimize_track(struct stv090x_state *state)
                }
                reg = STV090x_READ_DEMOD(state, DEMOD);
                STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
-               STV090x_SETFIELD_Px(reg, MANUAL_ROLLOFF_FIELD, 0x01);
+               STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x01);
                if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
                        goto err;
+
+               if (state->internal->dev_ver >= 0x30) {
+                       if (stv090x_get_viterbi(state) < 0)
+                               goto err;
+
+                       if (state->fec == STV090x_PR12) {
+                               if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x98) < 0)
+                                       goto err;
+                               if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
+                                       goto err;
+                       } else {
+                               if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x18) < 0)
+                                       goto err;
+                               if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
+                                       goto err;
+                       }
+               }
+
                if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
                        goto err;
                break;
 
        case STV090x_DVBS2:
                reg = STV090x_READ_DEMOD(state, DMDCFGMD);
-               STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
-               STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
+               STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
+               STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
                if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
                        goto err;
                if (STV090x_WRITE_DEMOD(state, ACLC, 0) < 0)
@@ -2597,10 +2918,7 @@ static int stv090x_optimize_track(struct stv090x_state *state)
                                        goto err;
                        }
                }
-               if (state->dev_ver <= 0x11) {
-                       if (state->demod_mode != STV090x_SINGLE)
-                               stv090x_activate_modcod(state); /* link to LDPC after demod LOCK */
-               }
+
                STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67); /* PER */
                break;
 
@@ -2626,15 +2944,18 @@ static int stv090x_optimize_track(struct stv090x_state *state)
                STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
                if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
                        goto err;
-               if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0x01) < 0)
+               if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
+                       goto err;
+
+               if (stv090x_set_srate(state, srate) < 0)
                        goto err;
-               stv090x_set_srate(state, srate);
-               stv090x_set_max_srate(state, state->mclk, srate);
-               stv090x_set_min_srate(state, state->mclk, srate);
                blind_tune = 1;
+
+               if (stv090x_dvbs_track_crl(state) < 0)
+                       goto err;
        }
 
-       if (state->dev_ver >= 0x20) {
+       if (state->internal->dev_ver >= 0x20) {
                if ((state->search_mode == STV090x_SEARCH_DVBS1)        ||
                    (state->search_mode == STV090x_SEARCH_DSS)          ||
                    (state->search_mode == STV090x_SEARCH_AUTO)) {
@@ -2646,36 +2967,39 @@ static int stv090x_optimize_track(struct stv090x_state *state)
                }
        }
 
-       if (state->dev_ver < 0x20) {
-               if (STV090x_WRITE_DEMOD(state, CARHDR, 0x08) < 0)
-                       goto err;
-       }
-       if (state->dev_ver == 0x10) {
-               if (STV090x_WRITE_DEMOD(state, CORRELEXP, 0x0a) < 0)
-                       goto err;
-       }
-
        if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
                goto err;
 
-       if ((state->dev_ver >= 0x20) || (blind_tune == 1) || (state->srate < 10000000)) {
+       /* AUTO tracking MODE */
+       if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x80) < 0)
+               goto err;
+       /* AUTO tracking MODE */
+       if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x80) < 0)
+               goto err;
 
+       if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1) ||
+           (state->srate < 10000000)) {
+               /* update initial carrier freq with the found freq offset */
                if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
                        goto err;
                if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
                        goto err;
                state->tuner_bw = stv090x_car_width(srate, state->rolloff) + 10000000;
 
-               if ((state->dev_ver >= 0x20) || (blind_tune == 1)) {
+               if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1)) {
 
                        if (state->algo != STV090x_WARM_SEARCH) {
 
-                               stv090x_i2c_gate_ctrl(fe, 1);
+                               if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
+                                       goto err;
 
-                               if (state->config->tuner_set_bandwidth)
-                                       state->config->tuner_set_bandwidth(fe, state->tuner_bw);
+                               if (state->config->tuner_set_bandwidth) {
+                                       if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
+                                               goto err;
+                               }
 
-                               stv090x_i2c_gate_ctrl(fe, 0);
+                               if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
+                                       goto err;
 
                        }
                }
@@ -2714,10 +3038,11 @@ static int stv090x_optimize_track(struct stv090x_state *state)
 
        }
 
-       if (state->dev_ver >= 0x20) {
+       if (state->internal->dev_ver >= 0x20) {
                if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
                        goto err;
        }
+
        if ((state->delsys == STV090x_DVBS1) || (state->delsys == STV090x_DSS))
                stv090x_set_vit_thtracq(state);
 
@@ -2787,22 +3112,18 @@ static int stv090x_get_lock(struct stv090x_state *state, s32 timeout_dmd, s32 ti
 
 static int stv090x_set_s2rolloff(struct stv090x_state *state)
 {
-       s32 rolloff;
        u32 reg;
 
-       if (state->dev_ver == 0x10) {
+       if (state->internal->dev_ver <= 0x20) {
+               /* rolloff to auto mode if DVBS2 */
                reg = STV090x_READ_DEMOD(state, DEMOD);
-               STV090x_SETFIELD_Px(reg, MANUAL_ROLLOFF_FIELD, 0x01);
-               if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
-                       goto err;
-               rolloff = STV090x_READ_DEMOD(state, MATSTR1) & 0x03;
-               reg = STV090x_READ_DEMOD(state, DEMOD);
-               STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, reg);
+               STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x00);
                if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
                        goto err;
        } else {
+               /* DVB-S2 rolloff to auto mode if DVBS2 */
                reg = STV090x_READ_DEMOD(state, DEMOD);
-               STV090x_SETFIELD_Px(reg, MANUAL_ROLLOFF_FIELD, 0x00);
+               STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 0x00);
                if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
                        goto err;
        }
@@ -2812,85 +3133,14 @@ err:
        return -1;
 }
 
-static enum stv090x_signal_state stv090x_acq_fixs1(struct stv090x_state *state)
-{
-       s32 srate, f_1, f_2;
-       enum stv090x_signal_state signal_state = STV090x_NODATA;
-       u32 reg;
-       int lock;
-
-       reg = STV090x_READ_DEMOD(state, DMDSTATE);
-       if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 3) { /* DVB-S mode */
-               srate  = stv090x_get_srate(state, state->mclk);
-               srate += stv090x_get_tmgoffst(state, state->srate);
-
-               if (state->algo == STV090x_BLIND_SEARCH)
-                       stv090x_set_srate(state, state->srate);
-
-               stv090x_get_lock_tmg(state);
-
-               f_1 = STV090x_READ_DEMOD(state, CFR2);
-               f_2 = STV090x_READ_DEMOD(state, CFR1);
-
-               reg = STV090x_READ_DEMOD(state, DMDCFGMD);
-               STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
-               if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
-                       goto err;
-
-               reg = STV090x_READ_DEMOD(state, DEMOD);
-               STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, STV090x_IQ_SWAP);
-               if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
-                       goto err;
-               if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0) /* stop demod */
-                       goto err;
-               if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
-                       goto err;
-               if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_2) < 0)
-                       goto err;
-               if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* warm start trigger */
-                       goto err;
-
-               if (stv090x_get_lock(state, state->DemodTimeout, state->FecTimeout)) {
-                       lock = 1;
-                       stv090x_get_sig_params(state);
-                       stv090x_optimize_track(state);
-               } else {
-                       reg = STV090x_READ_DEMOD(state, DEMOD);
-                       STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, STV090x_IQ_NORMAL);
-                       if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_2) < 0)
-                               goto err;
-                       if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* warm start trigger */
-                               goto err;
-                       if (stv090x_get_lock(state, state->DemodTimeout, state->FecTimeout)) {
-                               lock = 1;
-                               signal_state = stv090x_get_sig_params(state);
-                               stv090x_optimize_track(state);
-                       }
-               }
-       } else {
-               lock = 0;
-       }
-
-       return signal_state;
-
-err:
-       dprintk(FE_ERROR, 1, "I/O error");
-       return -1;
-}
 
 static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
 {
        struct dvb_frontend *fe = &state->frontend;
        enum stv090x_signal_state signal_state = STV090x_NOCARRIER;
        u32 reg;
-       s32 timeout_dmd = 500, timeout_fec = 50;
-       int lock = 0, low_sr, no_signal = 0;
+       s32 agc1_power, power_iq = 0, i;
+       int lock = 0, low_sr = 0, no_signal = 0;
 
        reg = STV090x_READ_DEMOD(state, TSCFGH);
        STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* Stop path 1 stream merger */
@@ -2900,21 +3150,26 @@ static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
        if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod stop */
                goto err;
 
-       if (state->dev_ver >= 0x20) {
-               if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0) /* cut 2.0 */
-                       goto err;
-       } else {
-               if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x88) < 0) /* cut 1.x */
-                       goto err;
+       if (state->internal->dev_ver >= 0x20) {
+               if (state->srate > 5000000) {
+                       if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
+                               goto err;
+               } else {
+                       if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x82) < 0)
+                               goto err;
+               }
        }
 
        stv090x_get_lock_tmg(state);
 
        if (state->algo == STV090x_BLIND_SEARCH) {
                state->tuner_bw = 2 * 36000000; /* wide bw for unknown srate */
-               if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0x00) < 0) /* wider srate scan */
+               if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0) /* wider srate scan */
+                       goto err;
+               if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
+                       goto err;
+               if (stv090x_set_srate(state, 1000000) < 0) /* inital srate = 1Msps */
                        goto err;
-               stv090x_set_srate(state, 1000000); /* inital srate = 1Msps */
        } else {
                /* known srate */
                if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
@@ -2922,93 +3177,143 @@ static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
                if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
                        goto err;
 
-               if (state->srate >= 10000000) {
-                       if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0) /* High SR */
+               if (state->srate < 2000000) {
+                       /* SR < 2MSPS */
+                       if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x63) < 0)
                                goto err;
                } else {
-                       if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x60) < 0) /* Low SR */
+                       /* SR >= 2Msps */
+                       if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
                                goto err;
                }
 
-               if (state->dev_ver >= 0x20) {
+               if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
+                       goto err;
+
+               if (state->internal->dev_ver >= 0x20) {
                        if (STV090x_WRITE_DEMOD(state, KREFTMG, 0x5a) < 0)
                                goto err;
                        if (state->algo == STV090x_COLD_SEARCH)
-                               state->tuner_bw = (15 * (stv090x_car_width(state->srate, state->rolloff) + 1000000)) / 10;
+                               state->tuner_bw = (15 * (stv090x_car_width(state->srate, state->rolloff) + 10000000)) / 10;
                        else if (state->algo == STV090x_WARM_SEARCH)
                                state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + 10000000;
-               } else {
-                       if (STV090x_WRITE_DEMOD(state, KREFTMG, 0xc1) < 0)
-                               goto err;
-                       state->tuner_bw = (15 * (stv090x_car_width(state->srate, state->rolloff) + 10000000)) / 10;
                }
-               if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0x01) < 0) /* narrow srate scan */
+
+               /* if cold start or warm  (Symbolrate is known)
+                * use a Narrow symbol rate scan range
+                */
+               if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0) /* narrow srate scan */
+                       goto err;
+
+               if (stv090x_set_srate(state, state->srate) < 0)
+                       goto err;
+
+               if (stv090x_set_max_srate(state, state->internal->mclk,
+                                         state->srate) < 0)
+                       goto err;
+               if (stv090x_set_min_srate(state, state->internal->mclk,
+                                         state->srate) < 0)
                        goto err;
-               stv090x_set_srate(state, state->srate);
-               stv090x_set_max_srate(state, state->mclk, state->srate);
-               stv090x_set_min_srate(state, state->mclk, state->srate);
 
                if (state->srate >= 10000000)
+                       low_sr = 0;
+               else
                        low_sr = 1;
        }
 
        /* Setup tuner */
-       stv090x_i2c_gate_ctrl(fe, 1);
+       if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
+               goto err;
 
-       if (state->config->tuner_set_bbgain)
-               state->config->tuner_set_bbgain(fe, 10); /* 10dB */
+       if (state->config->tuner_set_bbgain) {
+               if (state->config->tuner_set_bbgain(fe, 10) < 0) /* 10dB */
+                       goto err;
+       }
 
-       if (state->config->tuner_set_frequency)
-               state->config->tuner_set_frequency(fe, state->frequency);
+       if (state->config->tuner_set_frequency) {
+               if (state->config->tuner_set_frequency(fe, state->frequency) < 0)
+                       goto err;
+       }
 
-       if (state->config->tuner_set_bandwidth)
-               state->config->tuner_set_bandwidth(fe, state->tuner_bw);
+       if (state->config->tuner_set_bandwidth) {
+               if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
+                       goto err;
+       }
 
-       stv090x_i2c_gate_ctrl(fe, 0);
+       if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
+               goto err;
 
        msleep(50);
 
-       stv090x_i2c_gate_ctrl(fe, 1);
+       if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
+               goto err;
 
-       if (state->config->tuner_get_status)
-               state->config->tuner_get_status(fe, &reg);
+       if (state->config->tuner_get_status) {
+               if (state->config->tuner_get_status(fe, &reg) < 0)
+                       goto err;
+       }
 
        if (reg)
                dprintk(FE_DEBUG, 1, "Tuner phase locked");
        else
                dprintk(FE_DEBUG, 1, "Tuner unlocked");
 
-       stv090x_i2c_gate_ctrl(fe, 0);
-
-       reg = STV090x_READ_DEMOD(state, DEMOD);
-       STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, state->inversion);
-       STV090x_SETFIELD_Px(reg, MANUAL_ROLLOFF_FIELD, 1);
-       if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
+       if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
                goto err;
-       stv090x_delivery_search(state);
-       if (state->algo == STV090x_BLIND_SEARCH)
-               stv090x_start_search(state);
 
-       if (state->dev_ver == 0x12) {
-               reg = STV090x_READ_DEMOD(state, TSCFGH);
-               STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
-               if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
-                       goto err;
-               msleep(3);
-               STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
-               if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
+       msleep(10);
+       agc1_power = MAKEWORD16(STV090x_READ_DEMOD(state, AGCIQIN1),
+                               STV090x_READ_DEMOD(state, AGCIQIN0));
+
+       if (agc1_power == 0) {
+               /* If AGC1 integrator value is 0
+                * then read POWERI, POWERQ
+                */
+               for (i = 0; i < 5; i++) {
+                       power_iq += (STV090x_READ_DEMOD(state, POWERI) +
+                                    STV090x_READ_DEMOD(state, POWERQ)) >> 1;
+               }
+               power_iq /= 5;
+       }
+
+       if ((agc1_power == 0) && (power_iq < STV090x_IQPOWER_THRESHOLD)) {
+               dprintk(FE_ERROR, 1, "No Signal: POWER_IQ=0x%02x", power_iq);
+               lock = 0;
+               signal_state = STV090x_NOAGC1;
+       } else {
+               reg = STV090x_READ_DEMOD(state, DEMOD);
+               STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, state->inversion);
+
+               if (state->internal->dev_ver <= 0x20) {
+                       /* rolloff to auto mode if DVBS2 */
+                       STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 1);
+               } else {
+                       /* DVB-S2 rolloff to auto mode if DVBS2 */
+                       STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 1);
+               }
+               if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
                        goto err;
-               STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
-               if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
+
+               if (stv090x_delivery_search(state) < 0)
                        goto err;
+
+               if (state->algo != STV090x_BLIND_SEARCH) {
+                       if (stv090x_start_search(state) < 0)
+                               goto err;
+               }
        }
 
+       if (signal_state == STV090x_NOAGC1)
+               return signal_state;
+
        if (state->algo == STV090x_BLIND_SEARCH)
                lock = stv090x_blind_search(state);
+
        else if (state->algo == STV090x_COLD_SEARCH)
-               lock = stv090x_get_coldlock(state, timeout_dmd);
+               lock = stv090x_get_coldlock(state, state->DemodTimeout);
+
        else if (state->algo == STV090x_WARM_SEARCH)
-               lock = stv090x_get_dmdlock(state, timeout_dmd);
+               lock = stv090x_get_dmdlock(state, state->DemodTimeout);
 
        if ((!lock) && (state->algo == STV090x_COLD_SEARCH)) {
                if (!low_sr) {
@@ -3022,32 +3327,18 @@ static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
 
        if ((lock) && (signal_state == STV090x_RANGEOK)) { /* signal within Range */
                stv090x_optimize_track(state);
-               if (state->dev_ver <= 0x11) { /*workaround for dual DVBS1 cut 1.1 and 1.0 only*/
-                       if (stv090x_get_std(state) == STV090x_DVBS1) {
-                               msleep(20);
-                               reg = STV090x_READ_DEMOD(state, TSCFGH);
-                               STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
-                               if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
-                                       goto err;
-                       } else {
-                               reg = STV090x_READ_DEMOD(state, TSCFGH);
-                               STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
-                               if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
-                                       goto err;
-                               msleep(3);
-                               STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
-                               if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
-                                       goto err;
-                               STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
-                               if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
-                                       goto err;
-                       }
-               } else if (state->dev_ver == 0x20) { /*cut 2.0 :release TS reset after demod lock and TrackingOptimization*/
+
+               if (state->internal->dev_ver >= 0x20) {
+                       /* >= Cut 2.0 :release TS reset after
+                        * demod lock and optimized Tracking
+                        */
                        reg = STV090x_READ_DEMOD(state, TSCFGH);
                        STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
                        if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
                                goto err;
+
                        msleep(3);
+
                        STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
                        if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
                                goto err;
@@ -3057,37 +3348,39 @@ static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
                                goto err;
                }
 
-               if (stv090x_get_lock(state, timeout_fec, timeout_fec)) {
-                       lock = 1;
+               lock = stv090x_get_lock(state, state->FecTimeout,
+                               state->FecTimeout);
+               if (lock) {
                        if (state->delsys == STV090x_DVBS2) {
                                stv090x_set_s2rolloff(state);
-                               if (STV090x_WRITE_DEMOD(state, PDELCTRL2, 0x40) < 0)
+
+                               reg = STV090x_READ_DEMOD(state, PDELCTRL2);
+                               STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 1);
+                               if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
                                        goto err;
-                               if (STV090x_WRITE_DEMOD(state, PDELCTRL2, 0x00) < 0) /* RESET counter */
+                               /* Reset DVBS2 packet delinator error counter */
+                               reg = STV090x_READ_DEMOD(state, PDELCTRL2);
+                               STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 0);
+                               if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
                                        goto err;
+
                                if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67) < 0) /* PER */
                                        goto err;
                        } else {
                                if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
                                        goto err;
                        }
+                       /* Reset the Total packet counter */
                        if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0x00) < 0)
                                goto err;
+                       /* Reset the packet Error counter2 */
                        if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
                                goto err;
                } else {
-                       lock = 0;
                        signal_state = STV090x_NODATA;
                        no_signal = stv090x_chk_signal(state);
                }
        }
-       if ((signal_state == STV090x_NODATA) && (!no_signal)) {
-               if (state->dev_ver <= 0x11) {
-                       reg = STV090x_READ_DEMOD(state, DMDSTATE);
-                       if (((STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD)) == STV090x_DVBS2) && (state->inversion == INVERSION_AUTO))
-                               signal_state = stv090x_acq_fixs1(state);
-               }
-       }
        return signal_state;
 
 err:
@@ -3100,11 +3393,24 @@ static enum dvbfe_search stv090x_search(struct dvb_frontend *fe, struct dvb_fron
        struct stv090x_state *state = fe->demodulator_priv;
        struct dtv_frontend_properties *props = &fe->dtv_property_cache;
 
+       if (p->frequency == 0)
+               return DVBFE_ALGO_SEARCH_INVALID;
+
        state->delsys = props->delivery_system;
        state->frequency = p->frequency;
        state->srate = p->u.qpsk.symbol_rate;
+       state->search_mode = STV090x_SEARCH_AUTO;
+       state->algo = STV090x_COLD_SEARCH;
+       state->fec = STV090x_PRERR;
+       if (state->srate > 10000000) {
+               dprintk(FE_DEBUG, 1, "Search range: 10 MHz");
+               state->search_range = 10000000;
+       } else {
+               dprintk(FE_DEBUG, 1, "Search range: 5 MHz");
+               state->search_range = 5000000;
+       }
 
-       if (!stv090x_algo(state)) {
+       if (stv090x_algo(state) == STV090x_RANGEOK) {
                dprintk(FE_DEBUG, 1, "Search success!");
                return DVBFE_ALGO_SEARCH_SUCCESS;
        } else {
@@ -3115,13 +3421,11 @@ static enum dvbfe_search stv090x_search(struct dvb_frontend *fe, struct dvb_fron
        return DVBFE_ALGO_SEARCH_ERROR;
 }
 
-/* FIXME! */
 static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
 {
        struct stv090x_state *state = fe->demodulator_priv;
        u32 reg;
        u8 search_state;
-       int locked = 0;
 
        reg = STV090x_READ_DEMOD(state, DMDSTATE);
        search_state = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
@@ -3131,17 +3435,23 @@ static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
        case 1: /* first PLH detected */
        default:
                dprintk(FE_DEBUG, 1, "Status: Unlocked (Searching ..)");
-               locked = 0;
+               *status = 0;
                break;
 
        case 2: /* DVB-S2 mode */
                dprintk(FE_DEBUG, 1, "Delivery system: DVB-S2");
                reg = STV090x_READ_DEMOD(state, DSTATUS);
                if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
-                       reg = STV090x_READ_DEMOD(state, TSSTATUS);
-                       if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
-                               locked = 1;
-                               *status = FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
+                       reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
+                       if (STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD)) {
+                               reg = STV090x_READ_DEMOD(state, TSSTATUS);
+                               if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
+                                       *status = FE_HAS_SIGNAL |
+                                                 FE_HAS_CARRIER |
+                                                 FE_HAS_VITERBI |
+                                                 FE_HAS_SYNC |
+                                                 FE_HAS_LOCK;
+                               }
                        }
                }
                break;
@@ -3154,15 +3464,18 @@ static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
                        if (STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD)) {
                                reg = STV090x_READ_DEMOD(state, TSSTATUS);
                                if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
-                                       locked = 1;
-                                       *status = FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
+                                       *status = FE_HAS_SIGNAL |
+                                                 FE_HAS_CARRIER |
+                                                 FE_HAS_VITERBI |
+                                                 FE_HAS_SYNC |
+                                                 FE_HAS_LOCK;
                                }
                        }
                }
                break;
        }
 
-       return locked;
+       return 0;
 }
 
 static int stv090x_read_per(struct dvb_frontend *fe, u32 *per)
@@ -3173,7 +3486,8 @@ static int stv090x_read_per(struct dvb_frontend *fe, u32 *per)
        u32 reg, h, m, l;
        enum fe_status status;
 
-       if (!stv090x_read_status(fe, &status)) {
+       stv090x_read_status(fe, &status);
+       if (!(status & FE_HAS_LOCK)) {
                *per = 1 << 23; /* Max PER */
        } else {
                /* Counter 2 */
@@ -3220,14 +3534,12 @@ static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
        int res = 0;
        int min = 0, med;
 
-       if (val < tab[min].read)
-               res = tab[min].real;
-       else if (val >= tab[max].read)
-               res = tab[max].real;
-       else {
+       if ((val >= tab[min].read && val < tab[max].read) ||
+           (val >= tab[max].read && val < tab[min].read)) {
                while ((max - min) > 1) {
                        med = (max + min) / 2;
-                       if (val >= tab[min].read && val < tab[med].read)
+                       if ((val >= tab[min].read && val < tab[med].read) ||
+                           (val >= tab[med].read && val < tab[min].read))
                                max = med;
                        else
                                min = med;
@@ -3236,6 +3548,18 @@ static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
                       (tab[max].real - tab[min].real) /
                       (tab[max].read - tab[min].read)) +
                        tab[min].real;
+       } else {
+               if (tab[min].read < tab[max].read) {
+                       if (val < tab[min].read)
+                               res = tab[min].real;
+                       else if (val >= tab[max].read)
+                               res = tab[max].real;
+               } else {
+                       if (val >= tab[min].read)
+                               res = tab[min].real;
+                       else if (val < tab[max].read)
+                               res = tab[max].real;
+               }
        }
 
        return res;
@@ -3245,16 +3569,22 @@ static int stv090x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
 {
        struct stv090x_state *state = fe->demodulator_priv;
        u32 reg;
-       s32 agc;
+       s32 agc_0, agc_1, agc;
+       s32 str;
 
        reg = STV090x_READ_DEMOD(state, AGCIQIN1);
-       agc = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
+       agc_1 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
+       reg = STV090x_READ_DEMOD(state, AGCIQIN0);
+       agc_0 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
+       agc = MAKEWORD16(agc_1, agc_0);
 
-       *strength = stv090x_table_lookup(stv090x_rf_tab, ARRAY_SIZE(stv090x_rf_tab) - 1, agc);
+       str = stv090x_table_lookup(stv090x_rf_tab,
+               ARRAY_SIZE(stv090x_rf_tab) - 1, agc);
        if (agc > stv090x_rf_tab[0].read)
-               *strength = 5;
+               str = 0;
        else if (agc < stv090x_rf_tab[ARRAY_SIZE(stv090x_rf_tab) - 1].read)
-               *strength = -100;
+               str = -100;
+       *strength = (str + 100) * 0xFFFF / 100;
 
        return 0;
 }
@@ -3265,6 +3595,8 @@ static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
        u32 reg_0, reg_1, reg, i;
        s32 val_0, val_1, val = 0;
        u8 lock_f;
+       s32 div;
+       u32 last;
 
        switch (state->delsys) {
        case STV090x_DVBS2:
@@ -3276,14 +3608,15 @@ static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
                                reg_1 = STV090x_READ_DEMOD(state, NNOSPLHT1);
                                val_1 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
                                reg_0 = STV090x_READ_DEMOD(state, NNOSPLHT0);
-                               val_0 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
+                               val_0 = STV090x_GETFIELD_Px(reg_0, NOSPLHT_NORMED_FIELD);
                                val  += MAKEWORD16(val_1, val_0);
                                msleep(1);
                        }
                        val /= 16;
-                       *cnr = stv090x_table_lookup(stv090x_s2cn_tab, ARRAY_SIZE(stv090x_s2cn_tab) - 1, val);
-                       if (val < stv090x_s2cn_tab[ARRAY_SIZE(stv090x_s2cn_tab) - 1].read)
-                               *cnr = 1000;
+                       last = ARRAY_SIZE(stv090x_s2cn_tab) - 1;
+                       div = stv090x_s2cn_tab[0].read -
+                             stv090x_s2cn_tab[last].read;
+                       *cnr = 0xFFFF - ((val * 0xFFFF) / div);
                }
                break;
 
@@ -3297,14 +3630,15 @@ static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
                                reg_1 = STV090x_READ_DEMOD(state, NOSDATAT1);
                                val_1 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
                                reg_0 = STV090x_READ_DEMOD(state, NOSDATAT0);
-                               val_0 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
+                               val_0 = STV090x_GETFIELD_Px(reg_0, NOSDATAT_UNNORMED_FIELD);
                                val  += MAKEWORD16(val_1, val_0);
                                msleep(1);
                        }
                        val /= 16;
-                       *cnr = stv090x_table_lookup(stv090x_s1cn_tab, ARRAY_SIZE(stv090x_s1cn_tab) - 1, val);
-                       if (val < stv090x_s2cn_tab[ARRAY_SIZE(stv090x_s1cn_tab) - 1].read)
-                               *cnr = 1000;
+                       last = ARRAY_SIZE(stv090x_s1cn_tab) - 1;
+                       div = stv090x_s1cn_tab[0].read -
+                             stv090x_s1cn_tab[last].read;
+                       *cnr = 0xFFFF - ((val * 0xFFFF) / div);
                }
                break;
        default:
@@ -3361,7 +3695,8 @@ static int stv090x_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_ma
 
        reg = STV090x_READ_DEMOD(state, DISTXCTL);
 
-       STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 2);
+       STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD,
+               (state->config->diseqc_envelope_mode) ? 4 : 2);
        STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
        if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
                goto err;
@@ -3413,10 +3748,10 @@ static int stv090x_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t
        reg = STV090x_READ_DEMOD(state, DISTXCTL);
 
        if (burst == SEC_MINI_A) {
-               mode = 3;
+               mode = (state->config->diseqc_envelope_mode) ? 5 : 3;
                value = 0x00;
        } else {
-               mode = 2;
+               mode = (state->config->diseqc_envelope_mode) ? 4 : 2;
                value = 0xFF;
        }
 
@@ -3533,22 +3868,34 @@ static void stv090x_release(struct dvb_frontend *fe)
 {
        struct stv090x_state *state = fe->demodulator_priv;
 
+       state->internal->num_used--;
+       if (state->internal->num_used <= 0) {
+
+               dprintk(FE_ERROR, 1, "Actually removing");
+
+               remove_dev(state->internal);
+               kfree(state->internal);
+       }
+
        kfree(state);
 }
 
 static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc_mode)
 {
-       u32 reg;
+       u32 reg = 0;
+
+       reg = stv090x_read_reg(state, STV090x_GENCFG);
 
        switch (ldpc_mode) {
        case STV090x_DUAL:
        default:
-               reg = stv090x_read_reg(state, STV090x_GENCFG);
                if ((state->demod_mode != STV090x_DUAL) || (STV090x_GETFIELD(reg, DDEMOD_FIELD) != 1)) {
-                       /* follow LDPC default state */
-                       if (stv090x_write_reg(state, STV090x_GENCFG, reg) < 0)
+                       /* set LDPC to dual mode */
+                       if (stv090x_write_reg(state, STV090x_GENCFG, 0x1d) < 0)
                                goto err;
+
                        state->demod_mode = STV090x_DUAL;
+
                        reg = stv090x_read_reg(state, STV090x_TSTRES0);
                        STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
                        if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
@@ -3556,10 +3903,50 @@ static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc
                        STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
                        if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
                                goto err;
+
+                       if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
+                               goto err;
+
+                       if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
+                               goto err;
+
+                       if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
+                               goto err;
+                       if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
+                               goto err;
                }
                break;
 
        case STV090x_SINGLE:
+               if (stv090x_stop_modcod(state) < 0)
+                       goto err;
+               if (stv090x_activate_modcod_single(state) < 0)
+                       goto err;
+
                if (state->demod == STV090x_DEMODULATOR_1) {
                        if (stv090x_write_reg(state, STV090x_GENCFG, 0x06) < 0) /* path 2 */
                                goto err;
@@ -3621,7 +4008,14 @@ static int stv090x_set_mclk(struct stv090x_state *state, u32 mclk, u32 clk)
        if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0)
                goto err;
 
-       state->mclk = stv090x_get_mclk(state);
+       state->internal->mclk = stv090x_get_mclk(state);
+
+       /*Set the DiseqC frequency to 22KHz */
+       div = state->internal->mclk / 704000;
+       if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0)
+               goto err;
+       if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
+               goto err;
 
        return 0;
 err:
@@ -3633,7 +4027,7 @@ static int stv090x_set_tspath(struct stv090x_state *state)
 {
        u32 reg;
 
-       if (state->dev_ver >= 0x20) {
+       if (state->internal->dev_ver >= 0x20) {
                switch (state->config->ts1_mode) {
                case STV090x_TSMODE_PARALLEL_PUNCTURED:
                case STV090x_TSMODE_DVBCI:
@@ -3771,40 +4165,105 @@ static int stv090x_set_tspath(struct stv090x_state *state)
 
        switch (state->config->ts2_mode) {
        case STV090x_TSMODE_PARALLEL_PUNCTURED:
-               reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
+               reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
                STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
                STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
-               if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
+               if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
                        goto err;
                break;
 
        case STV090x_TSMODE_DVBCI:
-               reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
+               reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
                STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
                STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
-               if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
+               if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
                        goto err;
                break;
 
        case STV090x_TSMODE_SERIAL_PUNCTURED:
-               reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
+               reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
                STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
                STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
-               if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
+               if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
                        goto err;
                break;
 
        case STV090x_TSMODE_SERIAL_CONTINUOUS:
-               reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
+               reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
                STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
                STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
-               if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
+               if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
                        goto err;
                break;
 
        default:
                break;
        }
+
+       if (state->config->ts1_clk > 0) {
+               u32 speed;
+
+               switch (state->config->ts1_mode) {
+               case STV090x_TSMODE_PARALLEL_PUNCTURED:
+               case STV090x_TSMODE_DVBCI:
+               default:
+                       speed = state->internal->mclk /
+                               (state->config->ts1_clk / 4);
+                       if (speed < 0x08)
+                               speed = 0x08;
+                       if (speed > 0xFF)
+                               speed = 0xFF;
+                       break;
+               case STV090x_TSMODE_SERIAL_PUNCTURED:
+               case STV090x_TSMODE_SERIAL_CONTINUOUS:
+                       speed = state->internal->mclk /
+                               (state->config->ts1_clk / 32);
+                       if (speed < 0x20)
+                               speed = 0x20;
+                       if (speed > 0xFF)
+                               speed = 0xFF;
+                       break;
+               }
+               reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
+               STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
+               if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
+                       goto err;
+               if (stv090x_write_reg(state, STV090x_P1_TSSPEED, speed) < 0)
+                       goto err;
+       }
+
+       if (state->config->ts2_clk > 0) {
+               u32 speed;
+
+               switch (state->config->ts2_mode) {
+               case STV090x_TSMODE_PARALLEL_PUNCTURED:
+               case STV090x_TSMODE_DVBCI:
+               default:
+                       speed = state->internal->mclk /
+                               (state->config->ts2_clk / 4);
+                       if (speed < 0x08)
+                               speed = 0x08;
+                       if (speed > 0xFF)
+                               speed = 0xFF;
+                       break;
+               case STV090x_TSMODE_SERIAL_PUNCTURED:
+               case STV090x_TSMODE_SERIAL_CONTINUOUS:
+                       speed = state->internal->mclk /
+                               (state->config->ts2_clk / 32);
+                       if (speed < 0x20)
+                               speed = 0x20;
+                       if (speed > 0xFF)
+                               speed = 0xFF;
+                       break;
+               }
+               reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
+               STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
+               if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
+                       goto err;
+               if (stv090x_write_reg(state, STV090x_P2_TSSPEED, speed) < 0)
+                       goto err;
+       }
+
        reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
        STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
        if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
@@ -3833,7 +4292,22 @@ static int stv090x_init(struct dvb_frontend *fe)
        const struct stv090x_config *config = state->config;
        u32 reg;
 
-       stv090x_ldpc_mode(state, state->demod_mode);
+       if (state->internal->mclk == 0) {
+               stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
+               msleep(5);
+               if (stv090x_write_reg(state, STV090x_SYNTCTRL,
+                                     0x20 | config->clk_mode) < 0)
+                       goto err;
+               stv090x_get_mclk(state);
+       }
+
+       if (stv090x_wakeup(fe) < 0) {
+               dprintk(FE_ERROR, 1, "Error waking device");
+               goto err;
+       }
+
+       if (stv090x_ldpc_mode(state, state->demod_mode) < 0)
+               goto err;
 
        reg = STV090x_READ_DEMOD(state, TNRCFG2);
        STV090x_SETFIELD_Px(reg, TUN_IQSWAP_FIELD, state->inversion);
@@ -3844,14 +4318,24 @@ static int stv090x_init(struct dvb_frontend *fe)
        if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
                goto err;
 
-       stv090x_i2c_gate_ctrl(fe, 1);
+       if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
+               goto err;
+
+       if (config->tuner_set_mode) {
+               if (config->tuner_set_mode(fe, TUNER_WAKE) < 0)
+                       goto err;
+       }
 
-       if (config->tuner_init)
-               config->tuner_init(fe);
+       if (config->tuner_init) {
+               if (config->tuner_init(fe) < 0)
+                       goto err;
+       }
 
-       stv090x_i2c_gate_ctrl(fe, 0);
+       if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
+               goto err;
 
-       stv090x_set_tspath(state);
+       if (stv090x_set_tspath(state) < 0)
+               goto err;
 
        return 0;
 err:
@@ -3885,16 +4369,26 @@ static int stv090x_setup(struct dvb_frontend *fe)
        }
 
        /* STV090x init */
-       if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Stop Demod */
+
+       /* Stop Demod */
+       if (stv090x_write_reg(state, STV090x_P1_DMDISTATE, 0x5c) < 0)
+               goto err;
+       if (stv090x_write_reg(state, STV090x_P2_DMDISTATE, 0x5c) < 0)
                goto err;
 
        msleep(5);
 
-       if (STV090x_WRITE_DEMOD(state, TNRCFG, 0x6c) < 0) /* check register ! (No Tuner Mode) */
+       /* Set No Tuner Mode */
+       if (stv090x_write_reg(state, STV090x_P1_TNRCFG, 0x6c) < 0)
+               goto err;
+       if (stv090x_write_reg(state, STV090x_P2_TNRCFG, 0x6c) < 0)
                goto err;
 
+       /* I2C repeater OFF */
        STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level);
-       if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0) /* repeater OFF */
+       if (stv090x_write_reg(state, STV090x_P1_I2CRPT, reg) < 0)
+               goto err;
+       if (stv090x_write_reg(state, STV090x_P2_I2CRPT, reg) < 0)
                goto err;
 
        if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
@@ -3907,13 +4401,14 @@ static int stv090x_setup(struct dvb_frontend *fe)
        msleep(5);
 
        /* write initval */
+       dprintk(FE_DEBUG, 1, "Setting up initial values");
        for (i = 0; i < t1_size; i++) {
-               dprintk(FE_DEBUG, 1, "Setting up initial values");
                if (stv090x_write_reg(state, stv090x_initval[i].addr, stv090x_initval[i].data) < 0)
                        goto err;
        }
 
-       if (state->dev_ver >= 0x20) {
+       state->internal->dev_ver = stv090x_read_reg(state, STV090x_MID);
+       if (state->internal->dev_ver >= 0x20) {
                if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
                        goto err;
 
@@ -3923,6 +4418,16 @@ static int stv090x_setup(struct dvb_frontend *fe)
                        if (stv090x_write_reg(state, stv090x_cut20_val[i].addr, stv090x_cut20_val[i].data) < 0)
                                goto err;
                }
+
+       } else if (state->internal->dev_ver < 0x20) {
+               dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!",
+                       state->internal->dev_ver);
+
+               goto err;
+       } else if (state->internal->dev_ver > 0x30) {
+               /* we shouldn't bail out from here */
+               dprintk(FE_ERROR, 1, "INFO: Cut: 0x%02x probably incomplete support!",
+                       state->internal->dev_ver);
        }
 
        if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
@@ -3930,12 +4435,6 @@ static int stv090x_setup(struct dvb_frontend *fe)
        if (stv090x_write_reg(state, STV090x_TSTRES0, 0x00) < 0)
                goto err;
 
-       stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
-       msleep(5);
-       if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0)
-               goto err;
-       stv090x_get_mclk(state);
-
        return 0;
 err:
        dprintk(FE_ERROR, 1, "I/O error");
@@ -3946,6 +4445,17 @@ static struct dvb_frontend_ops stv090x_ops = {
 
        .info = {
                .name                   = "STV090x Multistandard",
+               .type                   = FE_QPSK,
+               .frequency_min          = 950000,
+               .frequency_max          = 2150000,
+               .frequency_stepsize     = 0,
+               .frequency_tolerance    = 0,
+               .symbol_rate_min        = 1000000,
+               .symbol_rate_max        = 45000000,
+               .caps                   = FE_CAN_INVERSION_AUTO |
+                                         FE_CAN_FEC_AUTO       |
+                                         FE_CAN_QPSK           |
+                                         FE_CAN_2G_MODULATION
        },
 
        .release                        = stv090x_release,
@@ -3974,6 +4484,7 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
                                    enum stv090x_demodulator demod)
 {
        struct stv090x_state *state = NULL;
+       struct stv090x_dev *temp_int;
 
        state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL);
        if (state == NULL)
@@ -3987,10 +4498,34 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
        state->demod                            = demod;
        state->demod_mode                       = config->demod_mode; /* Single or Dual mode */
        state->device                           = config->device;
-       state->rolloff                          = 35; /* default */
+       state->rolloff                          = STV090x_RO_35; /* default */
+
+       temp_int = find_dev(state->i2c,
+                               state->config->address);
+
+       if ((temp_int != NULL) && (state->demod_mode == STV090x_DUAL)) {
+               state->internal = temp_int->internal;
+               state->internal->num_used++;
+               dprintk(FE_INFO, 1, "Found Internal Structure!");
+               dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
+                       state->device == STV0900 ? "STV0900" : "STV0903",
+                       demod,
+                       state->internal->dev_ver);
+               return &state->frontend;
+       } else {
+               state->internal = kmalloc(sizeof(struct stv090x_internal),
+                                         GFP_KERNEL);
+               temp_int = append_internal(state->internal);
+               state->internal->num_used = 1;
+               state->internal->mclk = 0;
+               state->internal->dev_ver = 0;
+               state->internal->i2c_adap = state->i2c;
+               state->internal->i2c_addr = state->config->address;
+               dprintk(FE_INFO, 1, "Create New Internal Structure!");
+       }
 
-       if (state->demod == STV090x_DEMODULATOR_0)
-               mutex_init(&demod_lock);
+       mutex_init(&state->internal->demod_lock);
+       mutex_init(&state->internal->tuner_lock);
 
        if (stv090x_sleep(&state->frontend) < 0) {
                dprintk(FE_ERROR, 1, "Error putting device to sleep");
@@ -4005,12 +4540,11 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
                dprintk(FE_ERROR, 1, "Error waking device");
                goto error;
        }
-       state->dev_ver = stv090x_read_reg(state, STV090x_MID);
 
-       dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x\n",
+       dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
               state->device == STV0900 ? "STV0900" : "STV0903",
               demod,
-              state->dev_ver);
+              state->internal->dev_ver);
 
        return &state->frontend;