Merge tag 'acpica-4.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[firefly-linux-kernel-4.4.55.git] / drivers / media / dvb-frontends / cx24120.c
index eecfe2284c91c8c418face49de0696c7d7f8fd6b..3b0ef52bb8342a049ea7cbd6a719b71ae548776a 100644 (file)
@@ -87,7 +87,7 @@ enum command_message_id {
 
        CMD_FWVERSION           = 0x35,
 
-       CMD_TUNER_INIT          = 0x3c,         /* cmd.len = 0x03; */
+       CMD_BER_CTRL            = 0x3c,         /* cmd.len = 0x03; */
 };
 
 #define CX24120_MAX_CMD_LEN    30
@@ -107,6 +107,10 @@ enum command_message_id {
 #define CX24120_STATUS_MASK    0x0f
 #define CX24120_SIGNAL_MASK    0xc0
 
+/* ber window */
+#define CX24120_BER_WINDOW     16
+#define CX24120_BER_WSIZE      ((1 << CX24120_BER_WINDOW) * 208 * 8)
+
 #define info(args...) pr_info("cx24120: " args)
 #define err(args...)  pr_err("cx24120: ### ERROR: " args)
 
@@ -114,12 +118,12 @@ enum command_message_id {
 struct cx24120_tuning {
        u32 frequency;
        u32 symbol_rate;
-       fe_spectral_inversion_t inversion;
-       fe_code_rate_t fec;
+       enum fe_spectral_inversion inversion;
+       enum fe_code_rate fec;
 
-       fe_delivery_system_t delsys;
-       fe_modulation_t modulation;
-       fe_pilot_t pilot;
+       enum fe_delivery_system delsys;
+       enum fe_modulation modulation;
+       enum fe_pilot pilot;
 
        /* Demod values */
        u8 fec_val;
@@ -144,7 +148,15 @@ struct cx24120_state {
        struct cx24120_tuning dcur;
        struct cx24120_tuning dnxt;
 
-       fe_status_t fe_status;
+       enum fe_status fe_status;
+
+       /* dvbv5 stats calculations */
+       u32 bitrate;
+       u32 berw_usecs;
+       u32 ber_prev;
+       u32 ucb_offset;
+       unsigned long ber_jiffies_stats;
+       unsigned long per_jiffies_stats;
 };
 
 /* Command message to firmware */
@@ -160,7 +172,8 @@ static int cx24120_readreg(struct cx24120_state *state, u8 reg)
        int ret;
        u8 buf = 0;
        struct i2c_msg msg[] = {
-               {       .addr = state->config->i2c_addr,
+               {
+                       .addr = state->config->i2c_addr,
                        .flags = 0,
                        .len = 1,
                        .buf = &reg
@@ -178,8 +191,7 @@ static int cx24120_readreg(struct cx24120_state *state, u8 reg)
                return ret;
        }
 
-       dev_dbg(&state->i2c->dev, "%s: reg=0x%02x; data=0x%02x\n",
-               __func__, reg, buf);
+       dev_dbg(&state->i2c->dev, "reg=0x%02x; data=0x%02x\n", reg, buf);
 
        return buf;
 }
@@ -203,8 +215,7 @@ static int cx24120_writereg(struct cx24120_state *state, u8 reg, u8 data)
                return ret;
        }
 
-       dev_dbg(&state->i2c->dev, "%s: reg=0x%02x; data=0x%02x\n",
-               __func__, reg, data);
+       dev_dbg(&state->i2c->dev, "reg=0x%02x; data=0x%02x\n", reg, data);
 
        return 0;
 }
@@ -245,9 +256,8 @@ static int cx24120_writeregs(struct cx24120_state *state,
                        goto out;
                }
 
-               dev_dbg(&state->i2c->dev,
-                       "%s: reg=0x%02x; data=%*ph\n",
-                       __func__, reg, msg.len - 1, msg.buf + 1);
+               dev_dbg(&state->i2c->dev, "reg=0x%02x; data=%*ph\n",
+                       reg, msg.len - 1, msg.buf + 1);
        }
 
        ret = 0;
@@ -319,11 +329,12 @@ static int cx24120_test_rom(struct cx24120_state *state)
 
 static int cx24120_read_snr(struct dvb_frontend *fe, u16 *snr)
 {
-       struct cx24120_state *state = fe->demodulator_priv;
+       struct dtv_frontend_properties *c = &fe->dtv_property_cache;
 
-       *snr =  (cx24120_readreg(state, CX24120_REG_QUALITY_H) << 8) |
-               (cx24120_readreg(state, CX24120_REG_QUALITY_L));
-       dev_dbg(&state->i2c->dev, "%s: read SNR index = %d\n", __func__, *snr);
+       if (c->cnr.stat[0].scale != FE_SCALE_DECIBEL)
+               *snr = 0;
+       else
+               *snr = div_s64(c->cnr.stat[0].svalue, 100);
 
        return 0;
 }
@@ -331,12 +342,15 @@ static int cx24120_read_snr(struct dvb_frontend *fe, u16 *snr)
 static int cx24120_read_ber(struct dvb_frontend *fe, u32 *ber)
 {
        struct cx24120_state *state = fe->demodulator_priv;
+       struct dtv_frontend_properties *c = &fe->dtv_property_cache;
+
+       if (c->post_bit_error.stat[0].scale != FE_SCALE_COUNTER) {
+               *ber = 0;
+               return 0;
+       }
 
-       *ber =  (cx24120_readreg(state, CX24120_REG_BER_HH) << 24)      |
-               (cx24120_readreg(state, CX24120_REG_BER_HL) << 16)      |
-               (cx24120_readreg(state, CX24120_REG_BER_LH) <<  8)      |
-                cx24120_readreg(state, CX24120_REG_BER_LL);
-       dev_dbg(&state->i2c->dev, "%s: read BER index = %d\n", __func__, *ber);
+       *ber = c->post_bit_error.stat[0].uvalue - state->ber_prev;
+       state->ber_prev = c->post_bit_error.stat[0].uvalue;
 
        return 0;
 }
@@ -366,17 +380,17 @@ static void cx24120_check_cmd(struct cx24120_state *state, u8 id)
 static int cx24120_message_send(struct cx24120_state *state,
                                struct cx24120_cmd *cmd)
 {
-       int ret, ficus;
+       int ficus;
 
        if (state->mpeg_enabled) {
                /* Disable mpeg out on certain commands */
                cx24120_check_cmd(state, cmd->id);
        }
 
-       ret = cx24120_writereg(state, CX24120_REG_CMD_START, cmd->id);
-       ret = cx24120_writeregs(state, CX24120_REG_CMD_ARGS, &cmd->arg[0],
-                               cmd->len, 1);
-       ret = cx24120_writereg(state, CX24120_REG_CMD_END, 0x01);
+       cx24120_writereg(state, CX24120_REG_CMD_START, cmd->id);
+       cx24120_writeregs(state, CX24120_REG_CMD_ARGS, &cmd->arg[0],
+                         cmd->len, 1);
+       cx24120_writereg(state, CX24120_REG_CMD_END, 0x01);
 
        ficus = 1000;
        while (cx24120_readreg(state, CX24120_REG_CMD_END)) {
@@ -387,8 +401,7 @@ static int cx24120_message_send(struct cx24120_state *state,
                        return -EREMOTEIO;
                }
        }
-       dev_dbg(&state->i2c->dev, "%s: Successfully send message 0x%02x\n",
-               __func__, cmd->id);
+       dev_dbg(&state->i2c->dev, "sent message 0x%02x\n", cmd->id);
 
        return 0;
 }
@@ -421,7 +434,6 @@ static int cx24120_message_sendrcv(struct cx24120_state *state,
 static int cx24120_read_signal_strength(struct dvb_frontend *fe,
                                        u16 *signal_strength)
 {
-       struct cx24120_state *state = fe->demodulator_priv;
        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
 
        if (c->strength.stat[0].scale != FE_SCALE_RELATIVE)
@@ -429,9 +441,6 @@ static int cx24120_read_signal_strength(struct dvb_frontend *fe,
        else
                *signal_strength = c->strength.stat[0].uvalue;
 
-       dev_dbg(&state->i2c->dev, "%s: Signal strength from cache: 0x%x\n",
-               __func__, *signal_strength);
-
        return 0;
 }
 
@@ -450,15 +459,14 @@ static int cx24120_msg_mpeg_output_global_config(struct cx24120_state *state,
 
        ret = cx24120_message_send(state, &cmd);
        if (ret != 0) {
-               dev_dbg(&state->i2c->dev,
-                       "%s: Failed to set MPEG output to %s\n",
-                       __func__, enable ? "enabled" : "disabled");
+               dev_dbg(&state->i2c->dev, "failed to %s MPEG output\n",
+                       enable ? "enable" : "disable");
                return ret;
        }
 
        state->mpeg_enabled = enable;
-       dev_dbg(&state->i2c->dev, "%s: MPEG output %s\n",
-               __func__, enable ? "enabled" : "disabled");
+       dev_dbg(&state->i2c->dev, "MPEG output %s\n",
+               enable ? "enabled" : "disabled");
 
        return 0;
 }
@@ -483,12 +491,15 @@ static int cx24120_msg_mpeg_output_config(struct cx24120_state *state, u8 seq)
 }
 
 static int cx24120_diseqc_send_burst(struct dvb_frontend *fe,
-                                    fe_sec_mini_cmd_t burst)
+                                    enum fe_sec_mini_cmd burst)
 {
        struct cx24120_state *state = fe->demodulator_priv;
        struct cx24120_cmd cmd;
 
-       /* Yes, cmd.len is set to zero. The old driver
+       dev_dbg(&state->i2c->dev, "\n");
+
+       /*
+        * Yes, cmd.len is set to zero. The old driver
         * didn't specify any len, but also had a
         * memset 0 before every use of the cmd struct
         * which would have set it to zero.
@@ -497,20 +508,17 @@ static int cx24120_diseqc_send_burst(struct dvb_frontend *fe,
        cmd.id = CMD_DISEQC_BURST;
        cmd.len = 0;
        cmd.arg[0] = 0x00;
-       if (burst)
-               cmd.arg[1] = 0x01;
-
-       dev_dbg(&state->i2c->dev, "%s: burst sent.\n", __func__);
+       cmd.arg[1] = (burst == SEC_MINI_B) ? 0x01 : 0x00;
 
        return cx24120_message_send(state, &cmd);
 }
 
-static int cx24120_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
+static int cx24120_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
 {
        struct cx24120_state *state = fe->demodulator_priv;
        struct cx24120_cmd cmd;
 
-       dev_dbg(&state->i2c->dev, "%s(%d)\n", __func__, tone);
+       dev_dbg(&state->i2c->dev, "(%d)\n", tone);
 
        if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) {
                err("Invalid tone=%d\n", tone);
@@ -528,12 +536,12 @@ static int cx24120_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
 }
 
 static int cx24120_set_voltage(struct dvb_frontend *fe,
-                              fe_sec_voltage_t voltage)
+                              enum fe_sec_voltage voltage)
 {
        struct cx24120_state *state = fe->demodulator_priv;
        struct cx24120_cmd cmd;
 
-       dev_dbg(&state->i2c->dev, "%s(%d)\n", __func__, voltage);
+       dev_dbg(&state->i2c->dev, "(%d)\n", voltage);
 
        cmd.id = CMD_SETVOLTAGE;
        cmd.len = 2;
@@ -550,7 +558,7 @@ static int cx24120_send_diseqc_msg(struct dvb_frontend *fe,
        struct cx24120_cmd cmd;
        int back_count;
 
-       dev_dbg(&state->i2c->dev, "%s()\n", __func__);
+       dev_dbg(&state->i2c->dev, "\n");
 
        cmd.id = CMD_DISEQC_MSG1;
        cmd.len = 11;
@@ -590,9 +598,7 @@ static int cx24120_send_diseqc_msg(struct dvb_frontend *fe,
        back_count = 500;
        do {
                if (!(cx24120_readreg(state, 0x93) & 0x01)) {
-                       dev_dbg(&state->i2c->dev,
-                               "%s: diseqc sequence sent success\n",
-                               __func__);
+                       dev_dbg(&state->i2c->dev, "diseqc sequence sent\n");
                        return 0;
                }
                msleep(20);
@@ -608,10 +614,11 @@ static void cx24120_get_stats(struct cx24120_state *state)
        struct dvb_frontend *fe = &state->frontend;
        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
        struct cx24120_cmd cmd;
-       int ret, sigstr_h, sigstr_l;
-       u16 u16tmp;
+       int ret, cnr, msecs;
+       u16 sig, ucb;
+       u32 ber;
 
-       dev_dbg(&state->i2c->dev, "%s()\n", __func__);
+       dev_dbg(&state->i2c->dev, "\n");
 
        /* signal strength */
        if (state->fe_status & FE_HAS_SIGNAL) {
@@ -626,39 +633,94 @@ static void cx24120_get_stats(struct cx24120_state *state)
                }
 
                /* raw */
-               sigstr_h = (cx24120_readreg(state, CX24120_REG_SIGSTR_H) >> 6) << 8;
-               sigstr_l = cx24120_readreg(state, CX24120_REG_SIGSTR_L);
-               dev_dbg(&state->i2c->dev, "%s: Signal strength from firmware= 0x%x\n",
-                       __func__, (sigstr_h | sigstr_l));
+               sig = cx24120_readreg(state, CX24120_REG_SIGSTR_H) >> 6;
+               sig = sig << 8;
+               sig |= cx24120_readreg(state, CX24120_REG_SIGSTR_L);
+               dev_dbg(&state->i2c->dev,
+                       "signal strength from firmware = 0x%x\n", sig);
 
                /* cooked */
-               u16tmp = ((sigstr_h | sigstr_l)  << 5) & 0x0000ffff;
-               dev_dbg(&state->i2c->dev, "%s: Signal strength= 0x%x\n",
-                       __func__, u16tmp);
+               sig = -100 * sig + 94324;
 
                c->strength.stat[0].scale = FE_SCALE_RELATIVE;
-               c->strength.stat[0].uvalue = u16tmp;
+               c->strength.stat[0].uvalue = sig;
        } else {
                c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
        }
 
-       /* FIXME: add CNR */
+       /* CNR */
+       if (state->fe_status & FE_HAS_VITERBI) {
+               cnr = cx24120_readreg(state, CX24120_REG_QUALITY_H) << 8;
+               cnr |= cx24120_readreg(state, CX24120_REG_QUALITY_L);
+               dev_dbg(&state->i2c->dev, "read SNR index = %d\n", cnr);
+
+               /* guessed - seems about right */
+               cnr = cnr * 100;
+
+               c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
+               c->cnr.stat[0].svalue = cnr;
+       } else {
+               c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+       }
+
+       /* BER & UCB require lock */
+       if (!(state->fe_status & FE_HAS_LOCK)) {
+               c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+               c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+               c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+               c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+               return;
+       }
+
+       /* BER */
+       if (time_after(jiffies, state->ber_jiffies_stats)) {
+               msecs = (state->berw_usecs + 500) / 1000;
+               state->ber_jiffies_stats = jiffies + msecs_to_jiffies(msecs);
+
+               ber = cx24120_readreg(state, CX24120_REG_BER_HH) << 24;
+               ber |= cx24120_readreg(state, CX24120_REG_BER_HL) << 16;
+               ber |= cx24120_readreg(state, CX24120_REG_BER_LH) << 8;
+               ber |= cx24120_readreg(state, CX24120_REG_BER_LL);
+               dev_dbg(&state->i2c->dev, "read BER index = %d\n", ber);
+
+               c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
+               c->post_bit_error.stat[0].uvalue += ber;
+
+               c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
+               c->post_bit_count.stat[0].uvalue += CX24120_BER_WSIZE;
+       }
+
+       /* UCB */
+       if (time_after(jiffies, state->per_jiffies_stats)) {
+               state->per_jiffies_stats = jiffies + msecs_to_jiffies(1000);
+
+               ucb = cx24120_readreg(state, CX24120_REG_UCB_H) << 8;
+               ucb |= cx24120_readreg(state, CX24120_REG_UCB_L);
+               dev_dbg(&state->i2c->dev, "ucblocks = %d\n", ucb);
+
+               /* handle reset */
+               if (ucb < state->ucb_offset)
+                       state->ucb_offset = c->block_error.stat[0].uvalue;
 
-       /* FIXME: add UCB/BER */
+               c->block_error.stat[0].scale = FE_SCALE_COUNTER;
+               c->block_error.stat[0].uvalue = ucb + state->ucb_offset;
+
+               c->block_count.stat[0].scale = FE_SCALE_COUNTER;
+               c->block_count.stat[0].uvalue += state->bitrate / 8 / 208;
+       }
 }
 
 static void cx24120_set_clock_ratios(struct dvb_frontend *fe);
 
 /* Read current tuning status */
-static int cx24120_read_status(struct dvb_frontend *fe, fe_status_t *status)
+static int cx24120_read_status(struct dvb_frontend *fe, enum fe_status *status)
 {
        struct cx24120_state *state = fe->demodulator_priv;
        int lock;
 
        lock = cx24120_readreg(state, CX24120_REG_STATUS);
 
-       dev_dbg(&state->i2c->dev, "%s() status = 0x%02x\n",
-               __func__, lock);
+       dev_dbg(&state->i2c->dev, "status = 0x%02x\n", lock);
 
        *status = 0;
 
@@ -671,9 +733,11 @@ static int cx24120_read_status(struct dvb_frontend *fe, fe_status_t *status)
        if (lock & CX24120_HAS_LOCK)
                *status |= FE_HAS_LOCK;
 
-       /* TODO: is FE_HAS_SYNC in the right place?
+       /*
+        * TODO: is FE_HAS_SYNC in the right place?
         * Other cx241xx drivers have this slightly
-        * different */
+        * different
+        */
 
        state->fe_status = *status;
        cx24120_get_stats(state);
@@ -695,16 +759,19 @@ static int cx24120_read_status(struct dvb_frontend *fe, fe_status_t *status)
        return 0;
 }
 
-/* FEC & modulation lookup table
+/*
+ * FEC & modulation lookup table
  * Used for decoding the REG_FECMODE register
  * once tuned in.
  */
-static struct cx24120_modfec {
-       fe_delivery_system_t delsys;
-       fe_modulation_t mod;
-       fe_code_rate_t fec;
+struct cx24120_modfec {
+       enum fe_delivery_system delsys;
+       enum fe_modulation mod;
+       enum fe_code_rate fec;
        u8 val;
-} modfec_lookup_table[] = {
+};
+
+static const struct cx24120_modfec modfec_lookup_table[] = {
        /*delsys     mod    fec       val */
        { SYS_DVBS,  QPSK,  FEC_1_2,  0x01 },
        { SYS_DVBS,  QPSK,  FEC_2_3,  0x02 },
@@ -740,12 +807,10 @@ static int cx24120_get_fec(struct dvb_frontend *fe)
        int ret;
        int fec;
 
-       dev_dbg(&state->i2c->dev, "%s()\n", __func__);
-
        ret = cx24120_readreg(state, CX24120_REG_FECMODE);
        fec = ret & 0x3f; /* Lower 6 bits */
 
-       dev_dbg(&state->i2c->dev, "%s: Get FEC: %d\n", __func__, fec);
+       dev_dbg(&state->i2c->dev, "raw fec = %d\n", fec);
 
        for (idx = 0; idx < ARRAY_SIZE(modfec_lookup_table); idx++) {
                if (modfec_lookup_table[idx].delsys != state->dcur.delsys)
@@ -757,8 +822,7 @@ static int cx24120_get_fec(struct dvb_frontend *fe)
        }
 
        if (idx >= ARRAY_SIZE(modfec_lookup_table)) {
-               dev_dbg(&state->i2c->dev, "%s: Couldn't find fec!\n",
-                       __func__);
+               dev_dbg(&state->i2c->dev, "couldn't find fec!\n");
                return -EINVAL;
        }
 
@@ -767,31 +831,56 @@ static int cx24120_get_fec(struct dvb_frontend *fe)
        c->fec_inner = modfec_lookup_table[idx].fec;
        c->pilot = (ret & 0x80) ? PILOT_ON : PILOT_OFF;
 
-       dev_dbg(&state->i2c->dev,
-               "%s: mod(%d), fec(%d), pilot(%d)\n",
-               __func__,
+       dev_dbg(&state->i2c->dev, "mod(%d), fec(%d), pilot(%d)\n",
                c->modulation, c->fec_inner, c->pilot);
 
        return 0;
 }
 
-/* Clock ratios lookup table
+/* Calculate ber window time */
+static void cx24120_calculate_ber_window(struct cx24120_state *state, u32 rate)
+{
+       struct dvb_frontend *fe = &state->frontend;
+       struct dtv_frontend_properties *c = &fe->dtv_property_cache;
+       u64 tmp;
+
+       /*
+        * Calculate bitrate from rate in the clock ratios table.
+        * This isn't *exactly* right but close enough.
+        */
+       tmp = (u64)c->symbol_rate * rate;
+       do_div(tmp, 256);
+       state->bitrate = tmp;
+
+       /* usecs per ber window */
+       tmp = 1000000ULL * CX24120_BER_WSIZE;
+       do_div(tmp, state->bitrate);
+       state->berw_usecs = tmp;
+
+       dev_dbg(&state->i2c->dev, "bitrate: %u, berw_usecs: %u\n",
+               state->bitrate, state->berw_usecs);
+}
+
+/*
+ * Clock ratios lookup table
  *
  * Values obtained from much larger table in old driver
  * which had numerous entries which would never match.
  *
  * There's probably some way of calculating these but I
  * can't determine the pattern
-*/
-static struct cx24120_clock_ratios_table {
-       fe_delivery_system_t delsys;
-       fe_pilot_t pilot;
-       fe_modulation_t mod;
-       fe_code_rate_t fec;
+ */
+struct cx24120_clock_ratios_table {
+       enum fe_delivery_system delsys;
+       enum fe_pilot pilot;
+       enum fe_modulation mod;
+       enum fe_code_rate fec;
        u32 m_rat;
        u32 n_rat;
        u32 rate;
-} clock_ratios_table[] = {
+};
+
+static const struct cx24120_clock_ratios_table clock_ratios_table[] = {
        /*delsys     pilot      mod    fec       m_rat    n_rat   rate */
        { SYS_DVBS2, PILOT_OFF, QPSK,  FEC_1_2,  273088,  254505, 274 },
        { SYS_DVBS2, PILOT_OFF, QPSK,  FEC_3_5,  17272,   13395,  330 },
@@ -869,9 +958,7 @@ static void cx24120_set_clock_ratios(struct dvb_frontend *fe)
                return;
        /* in cmd[0]-[5] - result */
 
-       dev_dbg(&state->i2c->dev,
-               "%s: m=%d, n=%d; idx: %d m=%d, n=%d, rate=%d\n",
-               __func__,
+       dev_dbg(&state->i2c->dev, "m=%d, n=%d; idx: %d m=%d, n=%d, rate=%d\n",
                cmd.arg[2] | (cmd.arg[1] << 8) | (cmd.arg[0] << 16),
                cmd.arg[5] | (cmd.arg[4] << 8) | (cmd.arg[3] << 16),
                idx,
@@ -894,13 +981,16 @@ static void cx24120_set_clock_ratios(struct dvb_frontend *fe)
        cmd.arg[9] = (clock_ratios_table[idx].rate >> 0) & 0xff;
 
        cx24120_message_send(state, &cmd);
+
+       /* Calculate ber window rates for stat work */
+       cx24120_calculate_ber_window(state, clock_ratios_table[idx].rate);
 }
 
 /* Set inversion value */
 static int cx24120_set_inversion(struct cx24120_state *state,
-                                fe_spectral_inversion_t inversion)
+                                enum fe_spectral_inversion inversion)
 {
-       dev_dbg(&state->i2c->dev, "%s(%d)\n", __func__, inversion);
+       dev_dbg(&state->i2c->dev, "(%d)\n", inversion);
 
        switch (inversion) {
        case INVERSION_OFF:
@@ -921,41 +1011,47 @@ static int cx24120_set_inversion(struct cx24120_state *state,
        return 0;
 }
 
-/*
- * FEC lookup table for tuning Some DVB-S2 val's have been found by
- * trial and error. Sofar it seems to match up with the contents of
- * the REG_FECMODE after tuning The rest will probably be the same but
- * would need testing.  Anything not in the table will run with
- * FEC_AUTO and take a while longer to tune in ( c.500ms instead of
- * 30ms )
- */
-static struct cx24120_modfec_table {
-       fe_delivery_system_t delsys;
-       fe_modulation_t mod;
-       fe_code_rate_t fec;
+/* FEC lookup table for tuning */
+struct cx24120_modfec_table {
+       enum fe_delivery_system delsys;
+       enum fe_modulation mod;
+       enum fe_code_rate fec;
        u8 val;
-} modfec_table[] = {
-/*delsys       mod     fec      val */
-       { SYS_DVBS,  QPSK,  FEC_1_2, 0x2e },
-       { SYS_DVBS,  QPSK,  FEC_2_3, 0x2f },
-       { SYS_DVBS,  QPSK,  FEC_3_4, 0x30 },
-       { SYS_DVBS,  QPSK,  FEC_5_6, 0x31 },
-       { SYS_DVBS,  QPSK,  FEC_6_7, 0x32 },
-       { SYS_DVBS,  QPSK,  FEC_7_8, 0x33 },
-
-       { SYS_DVBS2, QPSK,  FEC_3_4, 0x07 },
-
-       { SYS_DVBS2, PSK_8, FEC_2_3, 0x0d },
-       { SYS_DVBS2, PSK_8, FEC_3_4, 0x0e },
+};
+
+static const struct cx24120_modfec_table modfec_table[] = {
+       /*delsys     mod    fec       val */
+       { SYS_DVBS,  QPSK,  FEC_1_2,  0x2e },
+       { SYS_DVBS,  QPSK,  FEC_2_3,  0x2f },
+       { SYS_DVBS,  QPSK,  FEC_3_4,  0x30 },
+       { SYS_DVBS,  QPSK,  FEC_5_6,  0x31 },
+       { SYS_DVBS,  QPSK,  FEC_6_7,  0x32 },
+       { SYS_DVBS,  QPSK,  FEC_7_8,  0x33 },
+
+       { SYS_DVBS2, QPSK,  FEC_1_2,  0x04 },
+       { SYS_DVBS2, QPSK,  FEC_3_5,  0x05 },
+       { SYS_DVBS2, QPSK,  FEC_2_3,  0x06 },
+       { SYS_DVBS2, QPSK,  FEC_3_4,  0x07 },
+       { SYS_DVBS2, QPSK,  FEC_4_5,  0x08 },
+       { SYS_DVBS2, QPSK,  FEC_5_6,  0x09 },
+       { SYS_DVBS2, QPSK,  FEC_8_9,  0x0a },
+       { SYS_DVBS2, QPSK,  FEC_9_10, 0x0b },
+
+       { SYS_DVBS2, PSK_8, FEC_3_5,  0x0c },
+       { SYS_DVBS2, PSK_8, FEC_2_3,  0x0d },
+       { SYS_DVBS2, PSK_8, FEC_3_4,  0x0e },
+       { SYS_DVBS2, PSK_8, FEC_5_6,  0x0f },
+       { SYS_DVBS2, PSK_8, FEC_8_9,  0x10 },
+       { SYS_DVBS2, PSK_8, FEC_9_10, 0x11 },
 };
 
 /* Set fec_val & fec_mask values from delsys, modulation & fec */
-static int cx24120_set_fec(struct cx24120_state *state, fe_modulation_t mod,
-                          fe_code_rate_t fec)
+static int cx24120_set_fec(struct cx24120_state *state, enum fe_modulation mod,
+                          enum fe_code_rate fec)
 {
        int idx;
 
-       dev_dbg(&state->i2c->dev, "%s(0x%02x,0x%02x)\n", __func__, mod, fec);
+       dev_dbg(&state->i2c->dev, "(0x%02x,0x%02x)\n", mod, fec);
 
        state->dnxt.fec = fec;
 
@@ -988,9 +1084,9 @@ static int cx24120_set_fec(struct cx24120_state *state, fe_modulation_t mod,
 }
 
 /* Set pilot */
-static int cx24120_set_pilot(struct cx24120_state *state, fe_pilot_t pilot)
+static int cx24120_set_pilot(struct cx24120_state *state, enum fe_pilot pilot)
 {
-       dev_dbg(&state->i2c->dev, "%s(%d)\n", __func__, pilot);
+       dev_dbg(&state->i2c->dev, "(%d)\n", pilot);
 
        /* Pilot only valid in DVBS2 */
        if (state->dnxt.delsys != SYS_DVBS2) {
@@ -1016,8 +1112,7 @@ static int cx24120_set_pilot(struct cx24120_state *state, fe_pilot_t pilot)
 /* Set symbol rate */
 static int cx24120_set_symbolrate(struct cx24120_state *state, u32 rate)
 {
-       dev_dbg(&state->i2c->dev, "%s(%d)\n",
-               __func__, rate);
+       dev_dbg(&state->i2c->dev, "(%d)\n", rate);
 
        state->dnxt.symbol_rate = rate;
 
@@ -1050,17 +1145,15 @@ static int cx24120_set_frontend(struct dvb_frontend *fe)
 
        switch (c->delivery_system) {
        case SYS_DVBS2:
-               dev_dbg(&state->i2c->dev, "%s() DVB-S2\n",
-                       __func__);
+               dev_dbg(&state->i2c->dev, "DVB-S2\n");
                break;
        case SYS_DVBS:
-               dev_dbg(&state->i2c->dev, "%s() DVB-S\n",
-                       __func__);
+               dev_dbg(&state->i2c->dev, "DVB-S\n");
                break;
        default:
                dev_dbg(&state->i2c->dev,
-                       "%s() Delivery system(%d) not supported\n",
-                       __func__, c->delivery_system);
+                       "delivery system(%d) not supported\n",
+                       c->delivery_system);
                ret = -EINVAL;
                break;
        }
@@ -1090,23 +1183,23 @@ static int cx24120_set_frontend(struct dvb_frontend *fe)
        cx24120_clone_params(fe);
 
        dev_dbg(&state->i2c->dev,
-               "%s: delsys      = %d\n", __func__, state->dcur.delsys);
+               "delsys      = %d\n", state->dcur.delsys);
        dev_dbg(&state->i2c->dev,
-               "%s: modulation  = %d\n", __func__, state->dcur.modulation);
+               "modulation  = %d\n", state->dcur.modulation);
        dev_dbg(&state->i2c->dev,
-               "%s: frequency   = %d\n", __func__, state->dcur.frequency);
+               "frequency   = %d\n", state->dcur.frequency);
        dev_dbg(&state->i2c->dev,
-               "%s: pilot       = %d (val = 0x%02x)\n", __func__,
+               "pilot       = %d (val = 0x%02x)\n",
                state->dcur.pilot, state->dcur.pilot_val);
        dev_dbg(&state->i2c->dev,
-               "%s: symbol_rate = %d (clkdiv/ratediv = 0x%02x/0x%02x)\n",
-                __func__, state->dcur.symbol_rate,
+               "symbol_rate = %d (clkdiv/ratediv = 0x%02x/0x%02x)\n",
+                state->dcur.symbol_rate,
                 state->dcur.clkdiv, state->dcur.ratediv);
        dev_dbg(&state->i2c->dev,
-               "%s: FEC         = %d (mask/val = 0x%02x/0x%02x)\n", __func__,
+               "FEC         = %d (mask/val = 0x%02x/0x%02x)\n",
                state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val);
        dev_dbg(&state->i2c->dev,
-               "%s: Inversion   = %d (val = 0x%02x)\n", __func__,
+               "Inversion   = %d (val = 0x%02x)\n",
                state->dcur.inversion, state->dcur.inversion_val);
 
        /* Flag that clock needs to be set after tune */
@@ -1158,9 +1251,8 @@ static int cx24120_set_vco(struct cx24120_state *state)
        vco = nxtal_khz * 10;
        inv_vco = DIV_ROUND_CLOSEST_ULL(0x400000000ULL, vco);
 
-       dev_dbg(&state->i2c->dev,
-               "%s: xtal=%d, vco=%d, inv_vco=%lld\n",
-               __func__, xtal_khz, vco, inv_vco);
+       dev_dbg(&state->i2c->dev, "xtal=%d, vco=%d, inv_vco=%lld\n",
+               xtal_khz, vco, inv_vco);
 
        cmd.id = CMD_VCO_SET;
        cmd.len = 12;
@@ -1180,59 +1272,56 @@ static int cx24120_set_vco(struct cx24120_state *state)
        return cx24120_message_send(state, &cmd);
 }
 
-int cx24120_init(struct dvb_frontend *fe)
+static int cx24120_init(struct dvb_frontend *fe)
 {
        const struct firmware *fw;
        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
        struct cx24120_state *state = fe->demodulator_priv;
        struct cx24120_cmd cmd;
-       u8 ret, ret_EA, reg1;
-       int reset_result;
-
-       int i;
+       u8 reg;
+       int ret, i;
        unsigned char vers[4];
 
        if (state->cold_init)
                return 0;
 
        /* ???? */
-       ret = cx24120_writereg(state, 0xea, 0x00);
-       ret = cx24120_test_rom(state);
-       ret = cx24120_readreg(state, 0xfb) & 0xfe;
-       ret = cx24120_writereg(state, 0xfb, ret);
-       ret = cx24120_readreg(state, 0xfc) & 0xfe;
-       ret = cx24120_writereg(state, 0xfc, ret);
-       ret = cx24120_writereg(state, 0xc3, 0x04);
-       ret = cx24120_writereg(state, 0xc4, 0x04);
-       ret = cx24120_writereg(state, 0xce, 0x00);
-       ret = cx24120_writereg(state, 0xcf, 0x00);
-       ret_EA = cx24120_readreg(state, 0xea) & 0xfe;
-       ret = cx24120_writereg(state, 0xea, ret_EA);
-       ret = cx24120_writereg(state, 0xeb, 0x0c);
-       ret = cx24120_writereg(state, 0xec, 0x06);
-       ret = cx24120_writereg(state, 0xed, 0x05);
-       ret = cx24120_writereg(state, 0xee, 0x03);
-       ret = cx24120_writereg(state, 0xef, 0x05);
-       ret = cx24120_writereg(state, 0xf3, 0x03);
-       ret = cx24120_writereg(state, 0xf4, 0x44);
-
-       for (reg1 = 0xf0; reg1 < 0xf3; reg1++) {
-               cx24120_writereg(state, reg1, 0x04);
-               cx24120_writereg(state, reg1 - 10, 0x02);
+       cx24120_writereg(state, 0xea, 0x00);
+       cx24120_test_rom(state);
+       reg = cx24120_readreg(state, 0xfb) & 0xfe;
+       cx24120_writereg(state, 0xfb, reg);
+       reg = cx24120_readreg(state, 0xfc) & 0xfe;
+       cx24120_writereg(state, 0xfc, reg);
+       cx24120_writereg(state, 0xc3, 0x04);
+       cx24120_writereg(state, 0xc4, 0x04);
+       cx24120_writereg(state, 0xce, 0x00);
+       cx24120_writereg(state, 0xcf, 0x00);
+       reg = cx24120_readreg(state, 0xea) & 0xfe;
+       cx24120_writereg(state, 0xea, reg);
+       cx24120_writereg(state, 0xeb, 0x0c);
+       cx24120_writereg(state, 0xec, 0x06);
+       cx24120_writereg(state, 0xed, 0x05);
+       cx24120_writereg(state, 0xee, 0x03);
+       cx24120_writereg(state, 0xef, 0x05);
+       cx24120_writereg(state, 0xf3, 0x03);
+       cx24120_writereg(state, 0xf4, 0x44);
+
+       for (i = 0; i < 3; i++) {
+               cx24120_writereg(state, 0xf0 + i, 0x04);
+               cx24120_writereg(state, 0xe6 + i, 0x02);
        }
 
-       ret = cx24120_writereg(state, 0xea, (ret_EA | 0x01));
-       for (reg1 = 0xc5; reg1 < 0xcb; reg1 += 2) {
-               ret = cx24120_writereg(state, reg1, 0x00);
-               ret = cx24120_writereg(state, reg1 + 1, 0x00);
+       cx24120_writereg(state, 0xea, (reg | 0x01));
+       for (i = 0; i < 6; i += 2) {
+               cx24120_writereg(state, 0xc5 + i, 0x00);
+               cx24120_writereg(state, 0xc6 + i, 0x00);
        }
 
-       ret = cx24120_writereg(state, 0xe4, 0x03);
-       ret = cx24120_writereg(state, 0xeb, 0x0a);
+       cx24120_writereg(state, 0xe4, 0x03);
+       cx24120_writereg(state, 0xeb, 0x0a);
 
-       dev_dbg(&state->i2c->dev,
-               "%s: Requesting firmware (%s) to download...\n",
-               __func__, CX24120_FIRMWARE);
+       dev_dbg(&state->i2c->dev, "requesting firmware (%s) to download...\n",
+               CX24120_FIRMWARE);
 
        ret = state->config->request_firmware(fe, &fw, CX24120_FIRMWARE);
        if (ret) {
@@ -1242,48 +1331,45 @@ int cx24120_init(struct dvb_frontend *fe)
        }
 
        dev_dbg(&state->i2c->dev,
-               "%s: Firmware found, size %d bytes (%02x %02x .. %02x %02x)\n",
-               __func__,
+               "Firmware found, size %d bytes (%02x %02x .. %02x %02x)\n",
                (int)fw->size,                  /* firmware_size in bytes */
                fw->data[0],                    /* fw 1st byte */
                fw->data[1],                    /* fw 2d byte */
                fw->data[fw->size - 2],         /* fw before last byte */
                fw->data[fw->size - 1]);        /* fw last byte */
 
-       ret = cx24120_test_rom(state);
-       ret = cx24120_readreg(state, 0xfb) & 0xfe;
-       ret = cx24120_writereg(state, 0xfb, ret);
-       ret = cx24120_writereg(state, 0xe0, 0x76);
-       ret = cx24120_writereg(state, 0xf7, 0x81);
-       ret = cx24120_writereg(state, 0xf8, 0x00);
-       ret = cx24120_writereg(state, 0xf9, 0x00);
-       ret = cx24120_writeregs(state, 0xfa, fw->data, (fw->size - 1), 0x00);
-       ret = cx24120_writereg(state, 0xf7, 0xc0);
-       ret = cx24120_writereg(state, 0xe0, 0x00);
-       ret = (fw->size - 2) & 0x00ff;
-       ret = cx24120_writereg(state, 0xf8, ret);
-       ret = ((fw->size - 2) >> 8) & 0x00ff;
-       ret = cx24120_writereg(state, 0xf9, ret);
-       ret = cx24120_writereg(state, 0xf7, 0x00);
-       ret = cx24120_writereg(state, 0xdc, 0x00);
-       ret = cx24120_writereg(state, 0xdc, 0x07);
+       cx24120_test_rom(state);
+       reg = cx24120_readreg(state, 0xfb) & 0xfe;
+       cx24120_writereg(state, 0xfb, reg);
+       cx24120_writereg(state, 0xe0, 0x76);
+       cx24120_writereg(state, 0xf7, 0x81);
+       cx24120_writereg(state, 0xf8, 0x00);
+       cx24120_writereg(state, 0xf9, 0x00);
+       cx24120_writeregs(state, 0xfa, fw->data, (fw->size - 1), 0x00);
+       cx24120_writereg(state, 0xf7, 0xc0);
+       cx24120_writereg(state, 0xe0, 0x00);
+       reg = (fw->size - 2) & 0x00ff;
+       cx24120_writereg(state, 0xf8, reg);
+       reg = ((fw->size - 2) >> 8) & 0x00ff;
+       cx24120_writereg(state, 0xf9, reg);
+       cx24120_writereg(state, 0xf7, 0x00);
+       cx24120_writereg(state, 0xdc, 0x00);
+       cx24120_writereg(state, 0xdc, 0x07);
        msleep(500);
 
        /* Check final byte matches final byte of firmware */
-       ret = cx24120_readreg(state, 0xe1);
-       if (ret == fw->data[fw->size - 1]) {
-               dev_dbg(&state->i2c->dev,
-                       "%s: Firmware uploaded successfully\n",
-                       __func__);
-               reset_result = 0;
+       reg = cx24120_readreg(state, 0xe1);
+       if (reg == fw->data[fw->size - 1]) {
+               dev_dbg(&state->i2c->dev, "Firmware uploaded successfully\n");
+               ret = 0;
        } else {
                err("Firmware upload failed. Last byte returned=0x%x\n", ret);
-               reset_result = -EREMOTEIO;
+               ret = -EREMOTEIO;
        }
-       ret = cx24120_writereg(state, 0xdc, 0x00);
+       cx24120_writereg(state, 0xdc, 0x00);
        release_firmware(fw);
-       if (reset_result != 0)
-               return reset_result;
+       if (ret != 0)
+               return ret;
 
        /* Start tuner */
        cmd.id = CMD_START_TUNER;
@@ -1325,19 +1411,17 @@ int cx24120_init(struct dvb_frontend *fe)
                return -EREMOTEIO;
        }
 
-       ret = cx24120_readreg(state, 0xba);
-       if (ret > 3) {
-               dev_dbg(&state->i2c->dev, "%s: Reset-readreg 0xba: %x\n",
-                       __func__, ret);
+       reg = cx24120_readreg(state, 0xba);
+       if (reg > 3) {
+               dev_dbg(&state->i2c->dev, "Reset-readreg 0xba: %x\n", ret);
                err("Error initialising tuner!\n");
                return -EREMOTEIO;
        }
 
-       dev_dbg(&state->i2c->dev, "%s: Tuner initialised correctly.\n",
-               __func__);
+       dev_dbg(&state->i2c->dev, "Tuner initialised correctly.\n");
 
        /* Initialise mpeg outputs */
-       ret = cx24120_writereg(state, 0xeb, 0x0a);
+       cx24120_writereg(state, 0xeb, 0x0a);
        if (cx24120_msg_mpeg_output_global_config(state, 0) ||
            cx24120_msg_mpeg_output_config(state, 0) ||
            cx24120_msg_mpeg_output_config(state, 1) ||
@@ -1346,14 +1430,14 @@ int cx24120_init(struct dvb_frontend *fe)
                return -EREMOTEIO;
        }
 
-       /* ???? */
-       cmd.id = CMD_TUNER_INIT;
+       /* Set size of BER window */
+       cmd.id = CMD_BER_CTRL;
        cmd.len = 3;
        cmd.arg[0] = 0x00;
-       cmd.arg[1] = 0x10;
-       cmd.arg[2] = 0x10;
+       cmd.arg[1] = CX24120_BER_WINDOW;
+       cmd.arg[2] = CX24120_BER_WINDOW;
        if (cx24120_message_send(state, &cmd)) {
-               err("Error sending final init message. :(\n");
+               err("Error setting ber window\n");
                return -EREMOTEIO;
        }
 
@@ -1372,19 +1456,30 @@ int cx24120_init(struct dvb_frontend *fe)
        /* init stats here in order signal app which stats are supported */
        c->strength.len = 1;
        c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+       c->cnr.len = 1;
+       c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+       c->post_bit_error.len = 1;
+       c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+       c->post_bit_count.len = 1;
+       c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+       c->block_error.len = 1;
+       c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+       c->block_count.len = 1;
+       c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 
        state->cold_init = 1;
+
        return 0;
 }
 
 static int cx24120_tune(struct dvb_frontend *fe, bool re_tune,
                        unsigned int mode_flags, unsigned int *delay,
-                       fe_status_t *status)
+                       enum fe_status *status)
 {
        struct cx24120_state *state = fe->demodulator_priv;
        int ret;
 
-       dev_dbg(&state->i2c->dev, "%s(%d)\n", __func__, re_tune);
+       dev_dbg(&state->i2c->dev, "(%d)\n", re_tune);
 
        /* TODO: Do we need to set delay? */
 
@@ -1413,7 +1508,7 @@ static int cx24120_get_frontend(struct dvb_frontend *fe)
        struct cx24120_state *state = fe->demodulator_priv;
        u8 freq1, freq2, freq3;
 
-       dev_dbg(&state->i2c->dev, "%s()", __func__);
+       dev_dbg(&state->i2c->dev, "\n");
 
        /* don't return empty data if we're not tuned in */
        if ((state->fe_status & FE_HAS_LOCK) == 0)
@@ -1424,8 +1519,7 @@ static int cx24120_get_frontend(struct dvb_frontend *fe)
        freq2 = cx24120_readreg(state, CX24120_REG_FREQ2);
        freq3 = cx24120_readreg(state, CX24120_REG_FREQ3);
        c->frequency = (freq3 << 16) | (freq2 << 8) | freq1;
-       dev_dbg(&state->i2c->dev, "%s frequency = %d\n", __func__,
-               c->frequency);
+       dev_dbg(&state->i2c->dev, "frequency = %d\n", c->frequency);
 
        /* Get modulation, fec, pilot */
        cx24120_get_fec(fe);
@@ -1437,18 +1531,22 @@ static void cx24120_release(struct dvb_frontend *fe)
 {
        struct cx24120_state *state = fe->demodulator_priv;
 
-       dev_dbg(&state->i2c->dev, "%s: Clear state structure\n", __func__);
+       dev_dbg(&state->i2c->dev, "Clear state structure\n");
        kfree(state);
 }
 
 static int cx24120_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
 {
        struct cx24120_state *state = fe->demodulator_priv;
+       struct dtv_frontend_properties *c = &fe->dtv_property_cache;
+
+       if (c->block_error.stat[0].scale != FE_SCALE_COUNTER) {
+               *ucblocks = 0;
+               return 0;
+       }
 
-       *ucblocks = (cx24120_readreg(state, CX24120_REG_UCB_H) << 8) |
-                    cx24120_readreg(state, CX24120_REG_UCB_L);
+       *ucblocks = c->block_error.stat[0].uvalue - state->ucb_offset;
 
-       dev_dbg(&state->i2c->dev, "%s: Blocks = %d\n", __func__, *ucblocks);
        return 0;
 }