--- zzzz-none-000/linux-3.10.107/drivers/media/dvb-frontends/dib7000p.c 2017-06-27 09:49:32.000000000 +0000 +++ scorpion-7490-727/linux-3.10.107/drivers/media/dvb-frontends/dib7000p.c 2021-02-04 17:41:59.000000000 +0000 @@ -11,6 +11,7 @@ #include #include #include +#include #include "dvb_math.h" #include "dvb_frontend.h" @@ -72,6 +73,12 @@ struct mutex i2c_buffer_lock; u8 input_mode_mpeg; + + /* for DVBv5 stats */ + s64 old_ucb; + unsigned long per_jiffies_stats; + unsigned long ber_jiffies_stats; + unsigned long get_stats_time; }; enum dib7000p_power_mode { @@ -401,7 +408,7 @@ return 0; } -int dib7000p_set_wbd_ref(struct dvb_frontend *demod, u16 value) +static int dib7000p_set_wbd_ref(struct dvb_frontend *demod, u16 value) { struct dib7000p_state *state = demod->demodulator_priv; if (value > 4095) @@ -409,9 +416,8 @@ state->wbd_ref = value; return dib7000p_write_word(state, 105, (dib7000p_read_word(state, 105) & 0xf000) | value); } -EXPORT_SYMBOL(dib7000p_set_wbd_ref); -int dib7000p_get_agc_values(struct dvb_frontend *fe, +static int dib7000p_get_agc_values(struct dvb_frontend *fe, u16 *agc_global, u16 *agc1, u16 *agc2, u16 *wbd) { struct dib7000p_state *state = fe->demodulator_priv; @@ -427,14 +433,12 @@ return 0; } -EXPORT_SYMBOL(dib7000p_get_agc_values); -int dib7000p_set_agc1_min(struct dvb_frontend *fe, u16 v) +static int dib7000p_set_agc1_min(struct dvb_frontend *fe, u16 v) { struct dib7000p_state *state = fe->demodulator_priv; return dib7000p_write_word(state, 108, v); } -EXPORT_SYMBOL(dib7000p_set_agc1_min); static void dib7000p_reset_pll(struct dib7000p_state *state) { @@ -478,7 +482,7 @@ return internal; } -int dib7000p_update_pll(struct dvb_frontend *fe, struct dibx000_bandwidth_config *bw) +static int dib7000p_update_pll(struct dvb_frontend *fe, struct dibx000_bandwidth_config *bw) { struct dib7000p_state *state = fe->demodulator_priv; u16 reg_1857, reg_1856 = dib7000p_read_word(state, 1856); @@ -513,7 +517,6 @@ } return -EIO; } -EXPORT_SYMBOL(dib7000p_update_pll); static int dib7000p_reset_gpio(struct dib7000p_state *st) { @@ -546,12 +549,11 @@ return 0; } -int dib7000p_set_gpio(struct dvb_frontend *demod, u8 num, u8 dir, u8 val) +static int dib7000p_set_gpio(struct dvb_frontend *demod, u8 num, u8 dir, u8 val) { struct dib7000p_state *state = demod->demodulator_priv; return dib7000p_cfg_gpio(state, num, dir, val); } -EXPORT_SYMBOL(dib7000p_set_gpio); static u16 dib7000p_defaults[] = { // auto search configuration @@ -636,6 +638,8 @@ 0, }; +static void dib7000p_reset_stats(struct dvb_frontend *fe); + static int dib7000p_demod_reset(struct dib7000p_state *state) { dib7000p_set_power_mode(state, DIB7000P_POWER_ALL); @@ -934,7 +938,7 @@ } -u32 dib7000p_ctrl_timf(struct dvb_frontend *fe, u8 op, u32 timf) +static u32 dib7000p_ctrl_timf(struct dvb_frontend *fe, u8 op, u32 timf) { struct dib7000p_state *state = fe->demodulator_priv; switch (op) { @@ -950,7 +954,6 @@ dib7000p_set_bandwidth(state, state->current_bandwidth); return state->timf; } -EXPORT_SYMBOL(dib7000p_ctrl_timf); static void dib7000p_set_channel(struct dib7000p_state *state, struct dtv_frontend_properties *ch, u8 seq) @@ -1360,6 +1363,9 @@ dib7000p_spur_protect(state, ch->frequency / 1000, BANDWIDTH_TO_KHZ(ch->bandwidth_hz)); dib7000p_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->bandwidth_hz)); + + dib7000p_reset_stats(demod); + return 0; } @@ -1552,7 +1558,9 @@ return ret; } -static int dib7000p_read_status(struct dvb_frontend *fe, fe_status_t * stat) +static int dib7000p_get_stats(struct dvb_frontend *fe, enum fe_status stat); + +static int dib7000p_read_status(struct dvb_frontend *fe, enum fe_status *stat) { struct dib7000p_state *state = fe->demodulator_priv; u16 lock = dib7000p_read_word(state, 509); @@ -1570,6 +1578,8 @@ if ((lock & 0x0038) == 0x38) *stat |= FE_HAS_LOCK; + dib7000p_get_stats(fe, *stat); + return 0; } @@ -1595,7 +1605,7 @@ return 0; } -static int dib7000p_read_snr(struct dvb_frontend *fe, u16 * snr) +static u32 dib7000p_get_snr(struct dvb_frontend *fe) { struct dib7000p_state *state = fe->demodulator_priv; u16 val; @@ -1625,10 +1635,350 @@ else result -= intlog10(2) * 10 * noise_exp - 100; + return result; +} + +static int dib7000p_read_snr(struct dvb_frontend *fe, u16 *snr) +{ + u32 result; + + result = dib7000p_get_snr(fe); + *snr = result / ((1 << 24) / 10); return 0; } +static void dib7000p_reset_stats(struct dvb_frontend *demod) +{ + struct dib7000p_state *state = demod->demodulator_priv; + struct dtv_frontend_properties *c = &demod->dtv_property_cache; + u32 ucb; + + memset(&c->strength, 0, sizeof(c->strength)); + memset(&c->cnr, 0, sizeof(c->cnr)); + memset(&c->post_bit_error, 0, sizeof(c->post_bit_error)); + memset(&c->post_bit_count, 0, sizeof(c->post_bit_count)); + memset(&c->block_error, 0, sizeof(c->block_error)); + + c->strength.len = 1; + c->cnr.len = 1; + c->block_error.len = 1; + c->block_count.len = 1; + c->post_bit_error.len = 1; + c->post_bit_count.len = 1; + + c->strength.stat[0].scale = FE_SCALE_DECIBEL; + c->strength.stat[0].uvalue = 0; + + c->cnr.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; + c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + + dib7000p_read_unc_blocks(demod, &ucb); + + state->old_ucb = ucb; + state->ber_jiffies_stats = 0; + state->per_jiffies_stats = 0; +} + +struct linear_segments { + unsigned x; + signed y; +}; + +/* + * Table to estimate signal strength in dBm. + * This table should be empirically determinated by measuring the signal + * strength generated by a RF generator directly connected into + * a device. + * This table was determinated by measuring the signal strength generated + * by a DTA-2111 RF generator directly connected into a dib7000p device + * (a Hauppauge Nova-TD stick), using a good quality 3 meters length + * RC6 cable and good RC6 connectors, connected directly to antenna 1. + * As the minimum output power of DTA-2111 is -31dBm, a 16 dBm attenuator + * were used, for the lower power values. + * The real value can actually be on other devices, or even at the + * second antena input, depending on several factors, like if LNA + * is enabled or not, if diversity is enabled, type of connectors, etc. + * Yet, it is better to use this measure in dB than a random non-linear + * percentage value, especially for antenna adjustments. + * On my tests, the precision of the measure using this table is about + * 0.5 dB, with sounds reasonable enough to adjust antennas. + */ +#define DB_OFFSET 131000 + +static struct linear_segments strength_to_db_table[] = { + { 63630, DB_OFFSET - 20500}, + { 62273, DB_OFFSET - 21000}, + { 60162, DB_OFFSET - 22000}, + { 58730, DB_OFFSET - 23000}, + { 58294, DB_OFFSET - 24000}, + { 57778, DB_OFFSET - 25000}, + { 57320, DB_OFFSET - 26000}, + { 56779, DB_OFFSET - 27000}, + { 56293, DB_OFFSET - 28000}, + { 55724, DB_OFFSET - 29000}, + { 55145, DB_OFFSET - 30000}, + { 54680, DB_OFFSET - 31000}, + { 54293, DB_OFFSET - 32000}, + { 53813, DB_OFFSET - 33000}, + { 53427, DB_OFFSET - 34000}, + { 52981, DB_OFFSET - 35000}, + + { 52636, DB_OFFSET - 36000}, + { 52014, DB_OFFSET - 37000}, + { 51674, DB_OFFSET - 38000}, + { 50692, DB_OFFSET - 39000}, + { 49824, DB_OFFSET - 40000}, + { 49052, DB_OFFSET - 41000}, + { 48436, DB_OFFSET - 42000}, + { 47836, DB_OFFSET - 43000}, + { 47368, DB_OFFSET - 44000}, + { 46468, DB_OFFSET - 45000}, + { 45597, DB_OFFSET - 46000}, + { 44586, DB_OFFSET - 47000}, + { 43667, DB_OFFSET - 48000}, + { 42673, DB_OFFSET - 49000}, + { 41816, DB_OFFSET - 50000}, + { 40876, DB_OFFSET - 51000}, + { 0, 0}, +}; + +static u32 interpolate_value(u32 value, struct linear_segments *segments, + unsigned len) +{ + u64 tmp64; + u32 dx; + s32 dy; + int i, ret; + + if (value >= segments[0].x) + return segments[0].y; + if (value < segments[len-1].x) + return segments[len-1].y; + + for (i = 1; i < len - 1; i++) { + /* If value is identical, no need to interpolate */ + if (value == segments[i].x) + return segments[i].y; + if (value > segments[i].x) + break; + } + + /* Linear interpolation between the two (x,y) points */ + dy = segments[i - 1].y - segments[i].y; + dx = segments[i - 1].x - segments[i].x; + + tmp64 = value - segments[i].x; + tmp64 *= dy; + do_div(tmp64, dx); + ret = segments[i].y + tmp64; + + return ret; +} + +/* FIXME: may require changes - this one was borrowed from dib8000 */ +static u32 dib7000p_get_time_us(struct dvb_frontend *demod) +{ + struct dtv_frontend_properties *c = &demod->dtv_property_cache; + u64 time_us, tmp64; + u32 tmp, denom; + int guard, rate_num, rate_denum = 1, bits_per_symbol; + int interleaving = 0, fft_div; + + switch (c->guard_interval) { + case GUARD_INTERVAL_1_4: + guard = 4; + break; + case GUARD_INTERVAL_1_8: + guard = 8; + break; + case GUARD_INTERVAL_1_16: + guard = 16; + break; + default: + case GUARD_INTERVAL_1_32: + guard = 32; + break; + } + + switch (c->transmission_mode) { + case TRANSMISSION_MODE_2K: + fft_div = 4; + break; + case TRANSMISSION_MODE_4K: + fft_div = 2; + break; + default: + case TRANSMISSION_MODE_8K: + fft_div = 1; + break; + } + + switch (c->modulation) { + case DQPSK: + case QPSK: + bits_per_symbol = 2; + break; + case QAM_16: + bits_per_symbol = 4; + break; + default: + case QAM_64: + bits_per_symbol = 6; + break; + } + + switch ((c->hierarchy == 0 || 1 == 1) ? c->code_rate_HP : c->code_rate_LP) { + case FEC_1_2: + rate_num = 1; + rate_denum = 2; + break; + case FEC_2_3: + rate_num = 2; + rate_denum = 3; + break; + case FEC_3_4: + rate_num = 3; + rate_denum = 4; + break; + case FEC_5_6: + rate_num = 5; + rate_denum = 6; + break; + default: + case FEC_7_8: + rate_num = 7; + rate_denum = 8; + break; + } + + interleaving = interleaving; + + denom = bits_per_symbol * rate_num * fft_div * 384; + + /* If calculus gets wrong, wait for 1s for the next stats */ + if (!denom) + return 0; + + /* Estimate the period for the total bit rate */ + time_us = rate_denum * (1008 * 1562500L); + tmp64 = time_us; + do_div(tmp64, guard); + time_us = time_us + tmp64; + time_us += denom / 2; + do_div(time_us, denom); + + tmp = 1008 * 96 * interleaving; + time_us += tmp + tmp / guard; + + return time_us; +} + +static int dib7000p_get_stats(struct dvb_frontend *demod, enum fe_status stat) +{ + struct dib7000p_state *state = demod->demodulator_priv; + struct dtv_frontend_properties *c = &demod->dtv_property_cache; + int show_per_stats = 0; + u32 time_us = 0, val, snr; + u64 blocks, ucb; + s32 db; + u16 strength; + + /* Get Signal strength */ + dib7000p_read_signal_strength(demod, &strength); + val = strength; + db = interpolate_value(val, + strength_to_db_table, + ARRAY_SIZE(strength_to_db_table)) - DB_OFFSET; + c->strength.stat[0].svalue = db; + + /* UCB/BER/CNR measures require lock */ + if (!(stat & FE_HAS_LOCK)) { + c->cnr.len = 1; + c->block_count.len = 1; + c->block_error.len = 1; + c->post_bit_error.len = 1; + c->post_bit_count.len = 1; + c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + 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 0; + } + + /* Check if time for stats was elapsed */ + if (time_after(jiffies, state->per_jiffies_stats)) { + state->per_jiffies_stats = jiffies + msecs_to_jiffies(1000); + + /* Get SNR */ + snr = dib7000p_get_snr(demod); + if (snr) + snr = (1000L * snr) >> 24; + else + snr = 0; + c->cnr.stat[0].svalue = snr; + c->cnr.stat[0].scale = FE_SCALE_DECIBEL; + + /* Get UCB measures */ + dib7000p_read_unc_blocks(demod, &val); + ucb = val - state->old_ucb; + if (val < state->old_ucb) + ucb += 0x100000000LL; + + c->block_error.stat[0].scale = FE_SCALE_COUNTER; + c->block_error.stat[0].uvalue = ucb; + + /* Estimate the number of packets based on bitrate */ + if (!time_us) + time_us = dib7000p_get_time_us(demod); + + if (time_us) { + blocks = 1250000ULL * 1000000ULL; + do_div(blocks, time_us * 8 * 204); + c->block_count.stat[0].scale = FE_SCALE_COUNTER; + c->block_count.stat[0].uvalue += blocks; + } + + show_per_stats = 1; + } + + /* Get post-BER measures */ + if (time_after(jiffies, state->ber_jiffies_stats)) { + time_us = dib7000p_get_time_us(demod); + state->ber_jiffies_stats = jiffies + msecs_to_jiffies((time_us + 500) / 1000); + + dprintk("Next all layers stats available in %u us.", time_us); + + dib7000p_read_ber(demod, &val); + c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; + c->post_bit_error.stat[0].uvalue += val; + + c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; + c->post_bit_count.stat[0].uvalue += 100000000; + } + + /* Get PER measures */ + if (show_per_stats) { + dib7000p_read_unc_blocks(demod, &val); + + c->block_error.stat[0].scale = FE_SCALE_COUNTER; + c->block_error.stat[0].uvalue += val; + + time_us = dib7000p_get_time_us(demod); + if (time_us) { + blocks = 1250000ULL * 1000000ULL; + do_div(blocks, time_us * 8 * 204); + c->block_count.stat[0].scale = FE_SCALE_COUNTER; + c->block_count.stat[0].uvalue += blocks; + } + } + return 0; +} + static int dib7000p_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune) { tune->min_delay_ms = 1000; @@ -1643,7 +1993,7 @@ kfree(st); } -int dib7000pc_detection(struct i2c_adapter *i2c_adap) +static int dib7000pc_detection(struct i2c_adapter *i2c_adap) { u8 *tx, *rx; struct i2c_msg msg[2] = { @@ -1688,16 +2038,14 @@ kfree(tx); return ret; } -EXPORT_SYMBOL(dib7000pc_detection); -struct i2c_adapter *dib7000p_get_i2c_master(struct dvb_frontend *demod, enum dibx000_i2c_interface intf, int gating) +static struct i2c_adapter *dib7000p_get_i2c_master(struct dvb_frontend *demod, enum dibx000_i2c_interface intf, int gating) { struct dib7000p_state *st = demod->demodulator_priv; return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating); } -EXPORT_SYMBOL(dib7000p_get_i2c_master); -int dib7000p_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff) +static int dib7000p_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff) { struct dib7000p_state *state = fe->demodulator_priv; u16 val = dib7000p_read_word(state, 235) & 0xffef; @@ -1705,17 +2053,15 @@ dprintk("PID filter enabled %d", onoff); return dib7000p_write_word(state, 235, val); } -EXPORT_SYMBOL(dib7000p_pid_filter_ctrl); -int dib7000p_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff) +static int dib7000p_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff) { struct dib7000p_state *state = fe->demodulator_priv; dprintk("PID filter: index %x, PID %d, OnOff %d", id, pid, onoff); return dib7000p_write_word(state, 241 + id, onoff ? (1 << 13) | pid : 0); } -EXPORT_SYMBOL(dib7000p_pid_filter); -int dib7000p_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, struct dib7000p_config cfg[]) +static int dib7000p_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, struct dib7000p_config cfg[]) { struct dib7000p_state *dpst; int k = 0; @@ -1774,7 +2120,6 @@ kfree(dpst); return 0; } -EXPORT_SYMBOL(dib7000p_i2c_enumeration); static const s32 lut_1000ln_mant[] = { 6908, 6956, 7003, 7047, 7090, 7131, 7170, 7208, 7244, 7279, 7313, 7346, 7377, 7408, 7438, 7467, 7495, 7523, 7549, 7575, 7600 @@ -2032,12 +2377,11 @@ .functionality = dib7000p_i2c_func, }; -struct i2c_adapter *dib7090_get_i2c_tuner(struct dvb_frontend *fe) +static struct i2c_adapter *dib7090_get_i2c_tuner(struct dvb_frontend *fe) { struct dib7000p_state *st = fe->demodulator_priv; return &st->dib7090_tuner_adap; } -EXPORT_SYMBOL(dib7090_get_i2c_tuner); static int dib7090_host_bus_drive(struct dib7000p_state *state, u8 drive) { @@ -2214,7 +2558,7 @@ dib7000p_write_word(state, 1288, reg_1288); } -int dib7090_set_diversity_in(struct dvb_frontend *fe, int onoff) +static int dib7090_set_diversity_in(struct dvb_frontend *fe, int onoff) { struct dib7000p_state *state = fe->demodulator_priv; u16 reg_1287; @@ -2329,7 +2673,7 @@ return ret; } -int dib7090_tuner_sleep(struct dvb_frontend *fe, int onoff) +static int dib7090_tuner_sleep(struct dvb_frontend *fe, int onoff) { struct dib7000p_state *state = fe->demodulator_priv; u16 en_cur_state; @@ -2352,15 +2696,13 @@ return 0; } -EXPORT_SYMBOL(dib7090_tuner_sleep); -int dib7090_get_adc_power(struct dvb_frontend *fe) +static int dib7090_get_adc_power(struct dvb_frontend *fe) { return dib7000p_get_adc_power(fe); } -EXPORT_SYMBOL(dib7090_get_adc_power); -int dib7090_slave_reset(struct dvb_frontend *fe) +static int dib7090_slave_reset(struct dvb_frontend *fe) { struct dib7000p_state *state = fe->demodulator_priv; u16 reg; @@ -2371,10 +2713,9 @@ dib7000p_write_word(state, 1032, 0xffff); return 0; } -EXPORT_SYMBOL(dib7090_slave_reset); static struct dvb_frontend_ops dib7000p_ops; -struct dvb_frontend *dib7000p_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib7000p_config *cfg) +static struct dvb_frontend *dib7000p_init(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib7000p_config *cfg) { struct dvb_frontend *demod; struct dib7000p_state *st; @@ -2423,6 +2764,8 @@ dib7000p_demod_reset(st); + dib7000p_reset_stats(demod); + if (st->version == SOC7090) { dib7090_set_output_mode(demod, st->cfg.output_mode); dib7090_set_diversity_in(demod, 0); @@ -2434,6 +2777,31 @@ kfree(st); return NULL; } + +void *dib7000p_attach(struct dib7000p_ops *ops) +{ + if (!ops) + return NULL; + + ops->slave_reset = dib7090_slave_reset; + ops->get_adc_power = dib7090_get_adc_power; + ops->dib7000pc_detection = dib7000pc_detection; + ops->get_i2c_tuner = dib7090_get_i2c_tuner; + ops->tuner_sleep = dib7090_tuner_sleep; + ops->init = dib7000p_init; + ops->set_agc1_min = dib7000p_set_agc1_min; + ops->set_gpio = dib7000p_set_gpio; + ops->i2c_enumeration = dib7000p_i2c_enumeration; + ops->pid_filter = dib7000p_pid_filter; + ops->pid_filter_ctrl = dib7000p_pid_filter_ctrl; + ops->get_i2c_master = dib7000p_get_i2c_master; + ops->update_pll = dib7000p_update_pll; + ops->ctrl_timf = dib7000p_ctrl_timf; + ops->get_agc_values = dib7000p_get_agc_values; + ops->set_wbd_ref = dib7000p_set_wbd_ref; + + return ops; +} EXPORT_SYMBOL(dib7000p_attach); static struct dvb_frontend_ops dib7000p_ops = {