V4L/DVB (7186): tda10086: make the 22kHz tone for DISEQC a config option
[linux-2.6.git] / drivers / media / dvb / frontends / tda10086.c
index 5d18eba..0d2b69a 100644 (file)
@@ -22,7 +22,6 @@
 
 #include <linux/init.h>
 #include <linux/module.h>
-#include <linux/moduleparam.h>
 #include <linux/device.h>
 #include <linux/jiffies.h>
 #include <linux/string.h>
@@ -41,9 +40,10 @@ struct tda10086_state {
        /* private demod data */
        u32 frequency;
        u32 symbol_rate;
+       bool has_lock;
 };
 
-static int debug = 1;
+static int debug = 0;
 #define dprintk(args...) \
        do { \
                if (debug) printk(KERN_DEBUG "tda10086: " args); \
@@ -106,9 +106,12 @@ static int tda10086_write_mask(struct tda10086_state *state, int reg, int mask,
 static int tda10086_init(struct dvb_frontend* fe)
 {
        struct tda10086_state* state = fe->demodulator_priv;
+       u8 t22k_off = 0x80;
 
        dprintk ("%s\n", __FUNCTION__);
 
+       if (state->config->diseqc_tone)
+               t22k_off = 0;
        // reset
        tda10086_write_byte(state, 0x00, 0x00);
        msleep(10);
@@ -116,7 +119,7 @@ static int tda10086_init(struct dvb_frontend* fe)
        // misc setup
        tda10086_write_byte(state, 0x01, 0x94);
        tda10086_write_byte(state, 0x02, 0x35); // NOTE: TT drivers appear to disable CSWP
-       tda10086_write_byte(state, 0x03, 0x64);
+       tda10086_write_byte(state, 0x03, 0xe4);
        tda10086_write_byte(state, 0x04, 0x43);
        tda10086_write_byte(state, 0x0c, 0x0c);
        tda10086_write_byte(state, 0x1b, 0xb0); // noise threshold
@@ -146,7 +149,7 @@ static int tda10086_init(struct dvb_frontend* fe)
        // setup AGC
        tda10086_write_byte(state, 0x05, 0x0B);
        tda10086_write_byte(state, 0x37, 0x63);
-       tda10086_write_byte(state, 0x3f, 0x03); // NOTE: flydvb uses 0x0a and varies it
+       tda10086_write_byte(state, 0x3f, 0x0a); // NOTE: flydvb varies it
        tda10086_write_byte(state, 0x40, 0x64);
        tda10086_write_byte(state, 0x41, 0x4f);
        tda10086_write_byte(state, 0x42, 0x43);
@@ -158,7 +161,7 @@ static int tda10086_init(struct dvb_frontend* fe)
        tda10086_write_byte(state, 0x3d, 0x80);
 
        // setup SEC
-       tda10086_write_byte(state, 0x36, 0x00); // all SEC off
+       tda10086_write_byte(state, 0x36, t22k_off); // all SEC off, 22k tone
        tda10086_write_byte(state, 0x34, (((1<<19) * (22000/1000)) / (SACLK/1000)));      // } tone frequency
        tda10086_write_byte(state, 0x35, (((1<<19) * (22000/1000)) / (SACLK/1000)) >> 8); // }
 
@@ -180,16 +183,20 @@ static void tda10086_diseqc_wait(struct tda10086_state *state)
 static int tda10086_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
 {
        struct tda10086_state* state = fe->demodulator_priv;
+       u8 t22k_off = 0x80;
 
        dprintk ("%s\n", __FUNCTION__);
 
-       switch(tone) {
+       if (state->config->diseqc_tone)
+               t22k_off = 0;
+
+       switch (tone) {
        case SEC_TONE_OFF:
-               tda10086_write_byte(state, 0x36, 0x00);
+               tda10086_write_byte(state, 0x36, t22k_off);
                break;
 
        case SEC_TONE_ON:
-               tda10086_write_byte(state, 0x36, 0x01);
+               tda10086_write_byte(state, 0x36, 0x01 + t22k_off);
                break;
        }
 
@@ -202,9 +209,13 @@ static int tda10086_send_master_cmd (struct dvb_frontend* fe,
        struct tda10086_state* state = fe->demodulator_priv;
        int i;
        u8 oldval;
+       u8 t22k_off = 0x80;
 
        dprintk ("%s\n", __FUNCTION__);
 
+       if (state->config->diseqc_tone)
+               t22k_off = 0;
+
        if (cmd->msg_len > 6)
                return -EINVAL;
        oldval = tda10086_read_byte(state, 0x36);
@@ -212,7 +223,8 @@ static int tda10086_send_master_cmd (struct dvb_frontend* fe,
        for(i=0; i< cmd->msg_len; i++) {
                tda10086_write_byte(state, 0x48+i, cmd->msg[i]);
        }
-       tda10086_write_byte(state, 0x36, 0x08 | ((cmd->msg_len + 1) << 4));
+       tda10086_write_byte(state, 0x36, (0x08 + t22k_off)
+                                       | ((cmd->msg_len - 1) << 4));
 
        tda10086_diseqc_wait(state);
 
@@ -225,16 +237,20 @@ static int tda10086_send_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t minic
 {
        struct tda10086_state* state = fe->demodulator_priv;
        u8 oldval = tda10086_read_byte(state, 0x36);
+       u8 t22k_off = 0x80;
 
        dprintk ("%s\n", __FUNCTION__);
 
+       if (state->config->diseqc_tone)
+               t22k_off = 0;
+
        switch(minicmd) {
        case SEC_MINI_A:
-               tda10086_write_byte(state, 0x36, 0x04);
+               tda10086_write_byte(state, 0x36, 0x04 + t22k_off);
                break;
 
        case SEC_MINI_B:
-               tda10086_write_byte(state, 0x36, 0x06);
+               tda10086_write_byte(state, 0x36, 0x06 + t22k_off);
                break;
        }
 
@@ -398,6 +414,10 @@ static int tda10086_set_frontend(struct dvb_frontend* fe,
 
        dprintk ("%s\n", __FUNCTION__);
 
+       // modify parameters for tuning
+       tda10086_write_byte(state, 0x02, 0x35);
+       state->has_lock = false;
+
        // set params
        if (fe->ops.tuner_ops.set_params) {
                fe->ops.tuner_ops.set_params(fe, fe_params);
@@ -441,6 +461,10 @@ static int tda10086_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_pa
 
        dprintk ("%s\n", __FUNCTION__);
 
+       // check for invalid symbol rate
+       if (fe_params->u.qpsk.symbol_rate < 500000)
+               return -EINVAL;
+
        // calculate the updated frequency (note: we convert from Hz->kHz)
        tmp64 = tda10086_read_byte(state, 0x52);
        tmp64 |= (tda10086_read_byte(state, 0x51) << 8);
@@ -538,8 +562,14 @@ static int tda10086_read_status(struct dvb_frontend* fe, fe_status_t *fe_status)
                *fe_status |= FE_HAS_VITERBI;
        if (val & 0x08)
                *fe_status |= FE_HAS_SYNC;
-       if (val & 0x10)
+       if (val & 0x10) {
                *fe_status |= FE_HAS_LOCK;
+               if (!state->has_lock) {
+                       state->has_lock = true;
+                       // modify parameters for stable reception
+                       tda10086_write_byte(state, 0x02, 0x00);
+               }
+       }
 
        return 0;
 }
@@ -551,7 +581,7 @@ static int tda10086_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
 
        dprintk ("%s\n", __FUNCTION__);
 
-       _str = tda10086_read_byte(state, 0x43);
+       _str = 0xff - tda10086_read_byte(state, 0x43);
        *signal = (_str << 8) | _str;
 
        return 0;
@@ -564,7 +594,7 @@ static int tda10086_read_snr(struct dvb_frontend* fe, u16 * snr)
 
        dprintk ("%s\n", __FUNCTION__);
 
-       _snr = tda10086_read_byte(state, 0x1c);
+       _snr = 0xff - tda10086_read_byte(state, 0x1c);
        *snr = (_snr << 8) | _snr;
 
        return 0;