V4L/DVB (3875): Convert ttpci/budget to refactored tuner code
[safe/jmp/linux-2.6] / drivers / media / dvb / frontends / mt352.c
index 50326c7..a7a347a 100644 (file)
@@ -35,6 +35,8 @@
 #include <linux/moduleparam.h>
 #include <linux/init.h>
 #include <linux/delay.h>
+#include <linux/string.h>
+#include <linux/slab.h>
 
 #include "dvb_frontend.h"
 #include "mt352_priv.h"
@@ -46,7 +48,7 @@ struct mt352_state {
        struct dvb_frontend_ops ops;
 
        /* configuration settings */
-       const struct mt352_config* config;
+       struct mt352_config config;
 };
 
 static int debug;
@@ -59,7 +61,7 @@ static int mt352_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
 {
        struct mt352_state* state = fe->demodulator_priv;
        u8 buf[2] = { reg, val };
-       struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0,
+       struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
                               .buf = buf, .len = 2 };
        int err = i2c_transfer(state->i2c, &msg, 1);
        if (err != 1) {
@@ -84,10 +86,10 @@ static int mt352_read_register(struct mt352_state* state, u8 reg)
        int ret;
        u8 b0 [] = { reg };
        u8 b1 [] = { 0 };
-       struct i2c_msg msg [] = { { .addr = state->config->demod_address,
+       struct i2c_msg msg [] = { { .addr = state->config.demod_address,
                                    .flags = 0,
                                    .buf = b0, .len = 1 },
-                                 { .addr = state->config->demod_address,
+                                 { .addr = state->config.demod_address,
                                    .flags = I2C_M_RD,
                                    .buf = b1, .len = 1 } };
 
@@ -102,11 +104,6 @@ static int mt352_read_register(struct mt352_state* state, u8 reg)
        return b1[0];
 }
 
-int mt352_read(struct dvb_frontend *fe, u8 reg)
-{
-       return mt352_read_register(fe->demodulator_priv,reg);
-}
-
 static int mt352_sleep(struct dvb_frontend* fe)
 {
        static u8 mt352_softdown[] = { CLOCK_CTL, 0x20, 0x08 };
@@ -134,8 +131,8 @@ static void mt352_calc_nominal_rate(struct mt352_state* state,
                bw = 8;
                break;
        }
-       if (state->config->adc_clock)
-               adc_clock = state->config->adc_clock;
+       if (state->config.adc_clock)
+               adc_clock = state->config.adc_clock;
 
        value = 64 * bw * (1<<16) / (7 * 8);
        value = value * 1000 / adc_clock;
@@ -152,10 +149,10 @@ static void mt352_calc_input_freq(struct mt352_state* state,
        int if2       = 36167; /* 36.166667 MHz */
        int ife,value;
 
-       if (state->config->adc_clock)
-               adc_clock = state->config->adc_clock;
-       if (state->config->if2)
-               if2 = state->config->if2;
+       if (state->config.adc_clock)
+               adc_clock = state->config.adc_clock;
+       if (state->config.if2)
+               if2 = state->config.if2;
 
        ife = (2*adc_clock - if2);
        value = -16374 * ife / adc_clock;
@@ -289,10 +286,13 @@ static int mt352_set_parameters(struct dvb_frontend* fe,
 
        mt352_calc_nominal_rate(state, op->bandwidth, buf+4);
        mt352_calc_input_freq(state, buf+6);
-       state->config->pll_set(fe, param, buf+8);
 
-       mt352_write(fe, buf, sizeof(buf));
-       if (state->config->no_tuner) {
+       if (fe->ops->tuner_ops.pllbuf) {
+               fe->ops->tuner_ops.pllbuf(fe, param, buf+8, 5);
+               buf[8] <<= 1;
+               mt352_write(fe, buf, sizeof(buf));
+       }
+       if (state->config.no_tuner) {
                /* start decoding */
                mt352_write(fe, fsm_go, 2);
        } else {
@@ -467,9 +467,11 @@ static int mt352_read_signal_strength(struct dvb_frontend* fe, u16* strength)
 {
        struct mt352_state* state = fe->demodulator_priv;
 
-       u16 signal = ((mt352_read_register(state, AGC_GAIN_1) << 8) & 0x0f) |
-                     (mt352_read_register(state, AGC_GAIN_0));
+       /* align the 12 bit AGC gain with the most significant bits */
+       u16 signal = ((mt352_read_register(state, AGC_GAIN_1) & 0x0f) << 12) |
+               (mt352_read_register(state, AGC_GAIN_0) << 4);
 
+       /* inverse of gain is signal strength */
        *strength = ~signal;
        return 0;
 }
@@ -516,7 +518,7 @@ static int mt352_init(struct dvb_frontend* fe)
 
                /* Do a "hard" reset */
                mt352_write(fe, mt352_reset_attach, sizeof(mt352_reset_attach));
-               return state->config->demod_init(fe);
+               return state->config.demod_init(fe);
        }
 
        return 0;
@@ -536,13 +538,12 @@ struct dvb_frontend* mt352_attach(const struct mt352_config* config,
        struct mt352_state* state = NULL;
 
        /* allocate memory for the internal state */
-       state = kmalloc(sizeof(struct mt352_state), GFP_KERNEL);
+       state = kzalloc(sizeof(struct mt352_state), GFP_KERNEL);
        if (state == NULL) goto error;
-       memset(state,0,sizeof(*state));
 
        /* setup the state */
-       state->config = config;
        state->i2c = i2c;
+       memcpy(&state->config,config,sizeof(struct mt352_config));
        memcpy(&state->ops, &mt352_ops, sizeof(struct dvb_frontend_ops));
 
        /* check if the demod is there */
@@ -601,10 +602,3 @@ MODULE_LICENSE("GPL");
 
 EXPORT_SYMBOL(mt352_attach);
 EXPORT_SYMBOL(mt352_write);
-EXPORT_SYMBOL(mt352_read);
-/*
- * Local variables:
- * c-basic-offset: 8
- * compile-command: "make DVB=1"
- * End:
- */