--- zzzz-none-000/linux-3.10.107/drivers/media/dvb-frontends/af9033.c 2017-06-27 09:49:32.000000000 +0000 +++ scorpion-7490-727/linux-3.10.107/drivers/media/dvb-frontends/af9033.c 2021-02-04 17:41:59.000000000 +0000 @@ -24,29 +24,35 @@ /* Max transfer size done by I2C transfer functions */ #define MAX_XFER_SIZE 64 -struct af9033_state { - struct i2c_adapter *i2c; +struct af9033_dev { + struct i2c_client *client; struct dvb_frontend fe; struct af9033_config cfg; + bool is_af9035; + bool is_it9135; u32 bandwidth_hz; bool ts_mode_parallel; bool ts_mode_serial; - u32 ber; - u32 ucb; - unsigned long last_stat_check; + enum fe_status fe_status; + u64 post_bit_error_prev; /* for old read_ber we return (curr - prev) */ + u64 post_bit_error; + u64 post_bit_count; + u64 error_block_count; + u64 total_block_count; + struct delayed_work stat_work; }; /* write multiple registers */ -static int af9033_wr_regs(struct af9033_state *state, u32 reg, const u8 *val, +static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val, int len) { int ret; u8 buf[MAX_XFER_SIZE]; struct i2c_msg msg[1] = { { - .addr = state->cfg.i2c_addr, + .addr = dev->client->addr, .flags = 0, .len = 3 + len, .buf = buf, @@ -54,9 +60,9 @@ }; if (3 + len > sizeof(buf)) { - dev_warn(&state->i2c->dev, - "%s: i2c wr reg=%04x: len=%d is too big!\n", - KBUILD_MODNAME, reg, len); + dev_warn(&dev->client->dev, + "i2c wr reg=%04x: len=%d is too big!\n", + reg, len); return -EINVAL; } @@ -65,12 +71,12 @@ buf[2] = (reg >> 0) & 0xff; memcpy(&buf[3], val, len); - ret = i2c_transfer(state->i2c, msg, 1); + ret = i2c_transfer(dev->client->adapter, msg, 1); if (ret == 1) { ret = 0; } else { - dev_warn(&state->i2c->dev, "%s: i2c wr failed=%d reg=%06x " \ - "len=%d\n", KBUILD_MODNAME, ret, reg, len); + dev_warn(&dev->client->dev, "i2c wr failed=%d reg=%06x len=%d\n", + ret, reg, len); ret = -EREMOTEIO; } @@ -78,31 +84,31 @@ } /* read multiple registers */ -static int af9033_rd_regs(struct af9033_state *state, u32 reg, u8 *val, int len) +static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len) { int ret; u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff, (reg >> 0) & 0xff }; struct i2c_msg msg[2] = { { - .addr = state->cfg.i2c_addr, + .addr = dev->client->addr, .flags = 0, .len = sizeof(buf), .buf = buf }, { - .addr = state->cfg.i2c_addr, + .addr = dev->client->addr, .flags = I2C_M_RD, .len = len, .buf = val } }; - ret = i2c_transfer(state->i2c, msg, 2); + ret = i2c_transfer(dev->client->adapter, msg, 2); if (ret == 2) { ret = 0; } else { - dev_warn(&state->i2c->dev, "%s: i2c rd failed=%d reg=%06x " \ - "len=%d\n", KBUILD_MODNAME, ret, reg, len); + dev_warn(&dev->client->dev, "i2c rd failed=%d reg=%06x len=%d\n", + ret, reg, len); ret = -EREMOTEIO; } @@ -111,19 +117,19 @@ /* write single register */ -static int af9033_wr_reg(struct af9033_state *state, u32 reg, u8 val) +static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val) { - return af9033_wr_regs(state, reg, &val, 1); + return af9033_wr_regs(dev, reg, &val, 1); } /* read single register */ -static int af9033_rd_reg(struct af9033_state *state, u32 reg, u8 *val) +static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val) { - return af9033_rd_regs(state, reg, val, 1); + return af9033_rd_regs(dev, reg, val, 1); } /* write single register with mask */ -static int af9033_wr_reg_mask(struct af9033_state *state, u32 reg, u8 val, +static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val, u8 mask) { int ret; @@ -131,7 +137,7 @@ /* no need for read if whole reg is written */ if (mask != 0xff) { - ret = af9033_rd_regs(state, reg, &tmp, 1); + ret = af9033_rd_regs(dev, reg, &tmp, 1); if (ret) return ret; @@ -140,17 +146,17 @@ val |= tmp; } - return af9033_wr_regs(state, reg, &val, 1); + return af9033_wr_regs(dev, reg, &val, 1); } /* read single register with mask */ -static int af9033_rd_reg_mask(struct af9033_state *state, u32 reg, u8 *val, +static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val, u8 mask) { int ret, i; u8 tmp; - ret = af9033_rd_regs(state, reg, &tmp, 1); + ret = af9033_rd_regs(dev, reg, &tmp, 1); if (ret) return ret; @@ -167,18 +173,17 @@ } /* write reg val table using reg addr auto increment */ -static int af9033_wr_reg_val_tab(struct af9033_state *state, +static int af9033_wr_reg_val_tab(struct af9033_dev *dev, const struct reg_val *tab, int tab_len) { #define MAX_TAB_LEN 212 int ret, i, j; u8 buf[1 + MAX_TAB_LEN]; - dev_dbg(&state->i2c->dev, "%s: tab_len=%d\n", __func__, tab_len); + dev_dbg(&dev->client->dev, "tab_len=%d\n", tab_len); if (tab_len > sizeof(buf)) { - dev_warn(&state->i2c->dev, "%s: tab len %d is too big\n", - KBUILD_MODNAME, tab_len); + dev_warn(&dev->client->dev, "tab len %d is too big\n", tab_len); return -EINVAL; } @@ -186,7 +191,7 @@ buf[j] = tab[i].val; if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) { - ret = af9033_wr_regs(state, tab[i].reg - j, buf, j + 1); + ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1); if (ret < 0) goto err; @@ -199,16 +204,16 @@ return 0; err: - dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); + dev_dbg(&dev->client->dev, "failed=%d\n", ret); return ret; } -static u32 af9033_div(struct af9033_state *state, u32 a, u32 b, u32 x) +static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x) { u32 r = 0, c = 0, i; - dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d\n", __func__, a, b, x); + dev_dbg(&dev->client->dev, "a=%d b=%d x=%d\n", a, b, x); if (a > b) { c = a / b; @@ -225,22 +230,15 @@ } r = (c << (u32)x) + r; - dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d r=%d r=%x\n", - __func__, a, b, x, r, r); + dev_dbg(&dev->client->dev, "a=%d b=%d x=%d r=%d r=%x\n", a, b, x, r, r); return r; } -static void af9033_release(struct dvb_frontend *fe) -{ - struct af9033_state *state = fe->demodulator_priv; - - kfree(state); -} - static int af9033_init(struct dvb_frontend *fe) { - struct af9033_state *state = fe->demodulator_priv; + struct af9033_dev *dev = fe->demodulator_priv; + struct dtv_frontend_properties *c = &fe->dtv_property_cache; int ret, i, len; const struct reg_val *init; u8 buf[4]; @@ -248,7 +246,7 @@ struct reg_val_mask tab[] = { { 0x80fb24, 0x00, 0x08 }, { 0x80004c, 0x00, 0xff }, - { 0x00f641, state->cfg.tuner, 0xff }, + { 0x00f641, dev->cfg.tuner, 0xff }, { 0x80f5ca, 0x01, 0x01 }, { 0x80f715, 0x01, 0x01 }, { 0x00f41f, 0x04, 0x04 }, @@ -267,75 +265,88 @@ { 0x00d830, 0x01, 0xff }, { 0x00d831, 0x00, 0xff }, { 0x00d832, 0x00, 0xff }, - { 0x80f985, state->ts_mode_serial, 0x01 }, - { 0x80f986, state->ts_mode_parallel, 0x01 }, + { 0x80f985, dev->ts_mode_serial, 0x01 }, + { 0x80f986, dev->ts_mode_parallel, 0x01 }, { 0x00d827, 0x00, 0xff }, { 0x00d829, 0x00, 0xff }, - { 0x800045, state->cfg.adc_multiplier, 0xff }, + { 0x800045, dev->cfg.adc_multiplier, 0xff }, }; /* program clock control */ - clock_cw = af9033_div(state, state->cfg.clock, 1000000ul, 19ul); + clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul); buf[0] = (clock_cw >> 0) & 0xff; buf[1] = (clock_cw >> 8) & 0xff; buf[2] = (clock_cw >> 16) & 0xff; buf[3] = (clock_cw >> 24) & 0xff; - dev_dbg(&state->i2c->dev, "%s: clock=%d clock_cw=%08x\n", - __func__, state->cfg.clock, clock_cw); + dev_dbg(&dev->client->dev, "clock=%d clock_cw=%08x\n", + dev->cfg.clock, clock_cw); - ret = af9033_wr_regs(state, 0x800025, buf, 4); + ret = af9033_wr_regs(dev, 0x800025, buf, 4); if (ret < 0) goto err; /* program ADC control */ for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { - if (clock_adc_lut[i].clock == state->cfg.clock) + if (clock_adc_lut[i].clock == dev->cfg.clock) break; } + if (i == ARRAY_SIZE(clock_adc_lut)) { + dev_err(&dev->client->dev, + "Couldn't find ADC config for clock=%d\n", + dev->cfg.clock); + goto err; + } - adc_cw = af9033_div(state, clock_adc_lut[i].adc, 1000000ul, 19ul); + adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul); buf[0] = (adc_cw >> 0) & 0xff; buf[1] = (adc_cw >> 8) & 0xff; buf[2] = (adc_cw >> 16) & 0xff; - dev_dbg(&state->i2c->dev, "%s: adc=%d adc_cw=%06x\n", - __func__, clock_adc_lut[i].adc, adc_cw); + dev_dbg(&dev->client->dev, "adc=%d adc_cw=%06x\n", + clock_adc_lut[i].adc, adc_cw); - ret = af9033_wr_regs(state, 0x80f1cd, buf, 3); + ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3); if (ret < 0) goto err; /* program register table */ for (i = 0; i < ARRAY_SIZE(tab); i++) { - ret = af9033_wr_reg_mask(state, tab[i].reg, tab[i].val, + ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val, tab[i].mask); if (ret < 0) goto err; } + /* clock output */ + if (dev->cfg.dyn0_clk) { + ret = af9033_wr_reg(dev, 0x80fba8, 0x00); + if (ret < 0) + goto err; + } + /* settings for TS interface */ - if (state->cfg.ts_mode == AF9033_TS_MODE_USB) { - ret = af9033_wr_reg_mask(state, 0x80f9a5, 0x00, 0x01); + if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) { + ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01); if (ret < 0) goto err; - ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x01, 0x01); + ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01); if (ret < 0) goto err; } else { - ret = af9033_wr_reg_mask(state, 0x80f990, 0x00, 0x01); + ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01); if (ret < 0) goto err; - ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x00, 0x01); + ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01); if (ret < 0) goto err; } /* load OFSM settings */ - dev_dbg(&state->i2c->dev, "%s: load ofsm settings\n", __func__); - switch (state->cfg.tuner) { + dev_dbg(&dev->client->dev, "load ofsm settings\n"); + switch (dev->cfg.tuner) { case AF9033_TUNER_IT9135_38: case AF9033_TUNER_IT9135_51: case AF9033_TUNER_IT9135_52: @@ -354,14 +365,13 @@ break; } - ret = af9033_wr_reg_val_tab(state, init, len); + ret = af9033_wr_reg_val_tab(dev, init, len); if (ret < 0) goto err; /* load tuner specific settings */ - dev_dbg(&state->i2c->dev, "%s: load tuner specific settings\n", - __func__); - switch (state->cfg.tuner) { + dev_dbg(&dev->client->dev, "load tuner specific settings\n"); + switch (dev->cfg.tuner) { case AF9033_TUNER_TUA9001: len = ARRAY_SIZE(tuner_init_tua9001); init = tuner_init_tua9001; @@ -411,90 +421,108 @@ init = tuner_init_it9135_62; break; default: - dev_dbg(&state->i2c->dev, "%s: unsupported tuner ID=%d\n", - __func__, state->cfg.tuner); + dev_dbg(&dev->client->dev, "unsupported tuner ID=%d\n", + dev->cfg.tuner); ret = -ENODEV; goto err; } - ret = af9033_wr_reg_val_tab(state, init, len); + ret = af9033_wr_reg_val_tab(dev, init, len); if (ret < 0) goto err; - if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { - ret = af9033_wr_reg_mask(state, 0x00d91c, 0x01, 0x01); + if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { + ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01); if (ret < 0) goto err; - ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01); + ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01); if (ret < 0) goto err; - ret = af9033_wr_reg_mask(state, 0x00d916, 0x00, 0x01); + ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01); if (ret < 0) goto err; } - switch (state->cfg.tuner) { + switch (dev->cfg.tuner) { case AF9033_TUNER_IT9135_60: case AF9033_TUNER_IT9135_61: case AF9033_TUNER_IT9135_62: - ret = af9033_wr_reg(state, 0x800000, 0x01); + ret = af9033_wr_reg(dev, 0x800000, 0x01); if (ret < 0) goto err; } - state->bandwidth_hz = 0; /* force to program all parameters */ + dev->bandwidth_hz = 0; /* force to program all parameters */ + /* 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->block_count.len = 1; + c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + c->block_error.len = 1; + c->block_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->post_bit_error.len = 1; + c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + /* start statistics polling */ + schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000)); return 0; err: - dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); + dev_dbg(&dev->client->dev, "failed=%d\n", ret); return ret; } static int af9033_sleep(struct dvb_frontend *fe) { - struct af9033_state *state = fe->demodulator_priv; + struct af9033_dev *dev = fe->demodulator_priv; int ret, i; u8 tmp; - ret = af9033_wr_reg(state, 0x80004c, 1); + /* stop statistics polling */ + cancel_delayed_work_sync(&dev->stat_work); + + ret = af9033_wr_reg(dev, 0x80004c, 1); if (ret < 0) goto err; - ret = af9033_wr_reg(state, 0x800000, 0); + ret = af9033_wr_reg(dev, 0x800000, 0); if (ret < 0) goto err; for (i = 100, tmp = 1; i && tmp; i--) { - ret = af9033_rd_reg(state, 0x80004c, &tmp); + ret = af9033_rd_reg(dev, 0x80004c, &tmp); if (ret < 0) goto err; usleep_range(200, 10000); } - dev_dbg(&state->i2c->dev, "%s: loop=%d\n", __func__, i); + dev_dbg(&dev->client->dev, "loop=%d\n", i); if (i == 0) { ret = -ETIMEDOUT; goto err; } - ret = af9033_wr_reg_mask(state, 0x80fb24, 0x08, 0x08); + ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08); if (ret < 0) goto err; /* prevent current leak (?) */ - if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { + if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { /* enable parallel TS */ - ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01); + ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01); if (ret < 0) goto err; - ret = af9033_wr_reg_mask(state, 0x00d916, 0x01, 0x01); + ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01); if (ret < 0) goto err; } @@ -502,7 +530,7 @@ return 0; err: - dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); + dev_dbg(&dev->client->dev, "failed=%d\n", ret); return ret; } @@ -520,14 +548,14 @@ static int af9033_set_frontend(struct dvb_frontend *fe) { - struct af9033_state *state = fe->demodulator_priv; + struct af9033_dev *dev = fe->demodulator_priv; struct dtv_frontend_properties *c = &fe->dtv_property_cache; int ret, i, spec_inv, sampling_freq; u8 tmp, buf[3], bandwidth_reg_val; u32 if_frequency, freq_cw, adc_freq; - dev_dbg(&state->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n", - __func__, c->frequency, c->bandwidth_hz); + dev_dbg(&dev->client->dev, "frequency=%d bandwidth_hz=%d\n", + c->frequency, c->bandwidth_hz); /* check bandwidth */ switch (c->bandwidth_hz) { @@ -541,8 +569,7 @@ bandwidth_reg_val = 0x02; break; default: - dev_dbg(&state->i2c->dev, "%s: invalid bandwidth_hz\n", - __func__); + dev_dbg(&dev->client->dev, "invalid bandwidth_hz\n"); ret = -EINVAL; goto err; } @@ -552,25 +579,40 @@ fe->ops.tuner_ops.set_params(fe); /* program CFOE coefficients */ - if (c->bandwidth_hz != state->bandwidth_hz) { + if (c->bandwidth_hz != dev->bandwidth_hz) { for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) { - if (coeff_lut[i].clock == state->cfg.clock && + if (coeff_lut[i].clock == dev->cfg.clock && coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { break; } } - ret = af9033_wr_regs(state, 0x800001, + if (i == ARRAY_SIZE(coeff_lut)) { + dev_err(&dev->client->dev, + "Couldn't find LUT config for clock=%d\n", + dev->cfg.clock); + ret = -EINVAL; + goto err; + } + + ret = af9033_wr_regs(dev, 0x800001, coeff_lut[i].val, sizeof(coeff_lut[i].val)); } /* program frequency control */ - if (c->bandwidth_hz != state->bandwidth_hz) { - spec_inv = state->cfg.spec_inv ? -1 : 1; + if (c->bandwidth_hz != dev->bandwidth_hz) { + spec_inv = dev->cfg.spec_inv ? -1 : 1; for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { - if (clock_adc_lut[i].clock == state->cfg.clock) + if (clock_adc_lut[i].clock == dev->cfg.clock) break; } + if (i == ARRAY_SIZE(clock_adc_lut)) { + dev_err(&dev->client->dev, + "Couldn't find ADC clock for clock=%d\n", + dev->cfg.clock); + ret = -EINVAL; + goto err; + } adc_freq = clock_adc_lut[i].adc; /* get used IF frequency */ @@ -589,12 +631,12 @@ else sampling_freq *= -1; - freq_cw = af9033_div(state, sampling_freq, adc_freq, 23ul); + freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul); if (spec_inv == -1) freq_cw = 0x800000 - freq_cw; - if (state->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X) + if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X) freq_cw /= 2; buf[0] = (freq_cw >> 0) & 0xff; @@ -605,26 +647,26 @@ if (if_frequency == 0) buf[2] = 0; - ret = af9033_wr_regs(state, 0x800029, buf, 3); + ret = af9033_wr_regs(dev, 0x800029, buf, 3); if (ret < 0) goto err; - state->bandwidth_hz = c->bandwidth_hz; + dev->bandwidth_hz = c->bandwidth_hz; } - ret = af9033_wr_reg_mask(state, 0x80f904, bandwidth_reg_val, 0x03); + ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03); if (ret < 0) goto err; - ret = af9033_wr_reg(state, 0x800040, 0x00); + ret = af9033_wr_reg(dev, 0x800040, 0x00); if (ret < 0) goto err; - ret = af9033_wr_reg(state, 0x800047, 0x00); + ret = af9033_wr_reg(dev, 0x800047, 0x00); if (ret < 0) goto err; - ret = af9033_wr_reg_mask(state, 0x80f999, 0x00, 0x01); + ret = af9033_wr_reg_mask(dev, 0x80f999, 0x00, 0x01); if (ret < 0) goto err; @@ -633,33 +675,33 @@ else tmp = 0x01; /* UHF */ - ret = af9033_wr_reg(state, 0x80004b, tmp); + ret = af9033_wr_reg(dev, 0x80004b, tmp); if (ret < 0) goto err; - ret = af9033_wr_reg(state, 0x800000, 0x00); + ret = af9033_wr_reg(dev, 0x800000, 0x00); if (ret < 0) goto err; return 0; err: - dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); + dev_dbg(&dev->client->dev, "failed=%d\n", ret); return ret; } static int af9033_get_frontend(struct dvb_frontend *fe) { - struct af9033_state *state = fe->demodulator_priv; + struct af9033_dev *dev = fe->demodulator_priv; struct dtv_frontend_properties *c = &fe->dtv_property_cache; int ret; u8 buf[8]; - dev_dbg(&state->i2c->dev, "%s:\n", __func__); + dev_dbg(&dev->client->dev, "\n"); /* read all needed registers */ - ret = af9033_rd_regs(state, 0x80f900, buf, sizeof(buf)); + ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf)); if (ret < 0) goto err; @@ -771,21 +813,21 @@ return 0; err: - dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); + dev_dbg(&dev->client->dev, "failed=%d\n", ret); return ret; } -static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status) +static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status) { - struct af9033_state *state = fe->demodulator_priv; + struct af9033_dev *dev = fe->demodulator_priv; int ret; u8 tmp; *status = 0; /* radio channel status, 0=no result, 1=has signal, 2=no signal */ - ret = af9033_rd_reg(state, 0x800047, &tmp); + ret = af9033_rd_reg(dev, 0x800047, &tmp); if (ret < 0) goto err; @@ -795,7 +837,7 @@ if (tmp != 0x02) { /* TPS lock */ - ret = af9033_rd_reg_mask(state, 0x80f5a9, &tmp, 0x01); + ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01); if (ret < 0) goto err; @@ -804,7 +846,7 @@ FE_HAS_VITERBI; /* full lock */ - ret = af9033_rd_reg_mask(state, 0x80f999, &tmp, 0x01); + ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01); if (ret < 0) goto err; @@ -814,266 +856,361 @@ FE_HAS_LOCK; } + dev->fe_status = *status; + return 0; err: - dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); + dev_dbg(&dev->client->dev, "failed=%d\n", ret); return ret; } static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr) { - struct af9033_state *state = fe->demodulator_priv; - int ret, i, len; - u8 buf[3], tmp; - u32 snr_val; - const struct val_snr *uninitialized_var(snr_lut); - - /* read value */ - ret = af9033_rd_regs(state, 0x80002c, buf, 3); - if (ret < 0) - goto err; - - snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0]; - - /* read current modulation */ - ret = af9033_rd_reg(state, 0x80f903, &tmp); - if (ret < 0) - goto err; + struct af9033_dev *dev = fe->demodulator_priv; + struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache; + int ret; + u8 u8tmp; - switch ((tmp >> 0) & 3) { - case 0: - len = ARRAY_SIZE(qpsk_snr_lut); - snr_lut = qpsk_snr_lut; - break; - case 1: - len = ARRAY_SIZE(qam16_snr_lut); - snr_lut = qam16_snr_lut; - break; - case 2: - len = ARRAY_SIZE(qam64_snr_lut); - snr_lut = qam64_snr_lut; - break; - default: - goto err; - } + /* use DVBv5 CNR */ + if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) { + /* Return 0.1 dB for AF9030 and 0-0xffff for IT9130. */ + if (dev->is_af9035) { + /* 1000x => 10x (0.1 dB) */ + *snr = div_s64(c->cnr.stat[0].svalue, 100); + } else { + /* 1000x => 1x (1 dB) */ + *snr = div_s64(c->cnr.stat[0].svalue, 1000); - for (i = 0; i < len; i++) { - tmp = snr_lut[i].snr; + /* read current modulation */ + ret = af9033_rd_reg(dev, 0x80f903, &u8tmp); + if (ret) + goto err; - if (snr_val < snr_lut[i].val) - break; + /* scale value to 0x0000-0xffff */ + switch ((u8tmp >> 0) & 3) { + case 0: + *snr = *snr * 0xffff / 23; + break; + case 1: + *snr = *snr * 0xffff / 26; + break; + case 2: + *snr = *snr * 0xffff / 32; + break; + default: + goto err; + } + } + } else { + *snr = 0; } - *snr = tmp * 10; /* dB/10 */ - return 0; err: - dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); + dev_dbg(&dev->client->dev, "failed=%d\n", ret); return ret; } static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength) { - struct af9033_state *state = fe->demodulator_priv; - int ret; - u8 strength2; + struct af9033_dev *dev = fe->demodulator_priv; + struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache; + int ret, tmp, power_real; + u8 u8tmp, gain_offset, buf[7]; + + if (dev->is_af9035) { + /* read signal strength of 0-100 scale */ + ret = af9033_rd_reg(dev, 0x800048, &u8tmp); + if (ret < 0) + goto err; - /* read signal strength of 0-100 scale */ - ret = af9033_rd_reg(state, 0x800048, &strength2); - if (ret < 0) - goto err; + /* scale value to 0x0000-0xffff */ + *strength = u8tmp * 0xffff / 100; + } else { + ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp); + if (ret < 0) + goto err; - /* scale value to 0x0000-0xffff */ - *strength = strength2 * 0xffff / 100; + ret = af9033_rd_regs(dev, 0x80f900, buf, 7); + if (ret < 0) + goto err; - return 0; + if (c->frequency <= 300000000) + gain_offset = 7; /* VHF */ + else + gain_offset = 4; /* UHF */ -err: - dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); + power_real = (u8tmp - 100 - gain_offset) - + power_reference[((buf[3] >> 0) & 3)][((buf[6] >> 0) & 7)]; - return ret; -} + if (power_real < -15) + tmp = 0; + else if ((power_real >= -15) && (power_real < 0)) + tmp = (2 * (power_real + 15)) / 3; + else if ((power_real >= 0) && (power_real < 20)) + tmp = 4 * power_real + 10; + else if ((power_real >= 20) && (power_real < 35)) + tmp = (2 * (power_real - 20)) / 3 + 90; + else + tmp = 100; -static int af9033_update_ch_stat(struct af9033_state *state) -{ - int ret = 0; - u32 err_cnt, bit_cnt; - u16 abort_cnt; - u8 buf[7]; - - /* only update data every half second */ - if (time_after(jiffies, state->last_stat_check + msecs_to_jiffies(500))) { - ret = af9033_rd_regs(state, 0x800032, buf, sizeof(buf)); - if (ret < 0) - goto err; - /* in 8 byte packets? */ - abort_cnt = (buf[1] << 8) + buf[0]; - /* in bits */ - err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2]; - /* in 8 byte packets? always(?) 0x2710 = 10000 */ - bit_cnt = (buf[6] << 8) + buf[5]; - - if (bit_cnt < abort_cnt) { - abort_cnt = 1000; - state->ber = 0xffffffff; - } else { - /* 8 byte packets, that have not been rejected already */ - bit_cnt -= (u32)abort_cnt; - if (bit_cnt == 0) { - state->ber = 0xffffffff; - } else { - err_cnt -= (u32)abort_cnt * 8 * 8; - bit_cnt *= 8 * 8; - state->ber = err_cnt * (0xffffffff / bit_cnt); - } - } - state->ucb += abort_cnt; - state->last_stat_check = jiffies; + /* scale value to 0x0000-0xffff */ + *strength = tmp * 0xffff / 100; } return 0; + err: - dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); + dev_dbg(&dev->client->dev, "failed=%d\n", ret); return ret; } static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber) { - struct af9033_state *state = fe->demodulator_priv; - int ret; - - ret = af9033_update_ch_stat(state); - if (ret < 0) - return ret; + struct af9033_dev *dev = fe->demodulator_priv; - *ber = state->ber; + *ber = (dev->post_bit_error - dev->post_bit_error_prev); + dev->post_bit_error_prev = dev->post_bit_error; return 0; } static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) { - struct af9033_state *state = fe->demodulator_priv; - int ret; - - ret = af9033_update_ch_stat(state); - if (ret < 0) - return ret; - - *ucblocks = state->ucb; + struct af9033_dev *dev = fe->demodulator_priv; + *ucblocks = dev->error_block_count; return 0; } static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) { - struct af9033_state *state = fe->demodulator_priv; + struct af9033_dev *dev = fe->demodulator_priv; int ret; - dev_dbg(&state->i2c->dev, "%s: enable=%d\n", __func__, enable); + dev_dbg(&dev->client->dev, "enable=%d\n", enable); - ret = af9033_wr_reg_mask(state, 0x00fa04, enable, 0x01); + ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01); if (ret < 0) goto err; return 0; err: - dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); + dev_dbg(&dev->client->dev, "failed=%d\n", ret); return ret; } -static struct dvb_frontend_ops af9033_ops; - -struct dvb_frontend *af9033_attach(const struct af9033_config *config, - struct i2c_adapter *i2c) +static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff) { + struct af9033_dev *dev = fe->demodulator_priv; int ret; - struct af9033_state *state; - u8 buf[8]; - dev_dbg(&i2c->dev, "%s:\n", __func__); + dev_dbg(&dev->client->dev, "onoff=%d\n", onoff); - /* allocate memory for the internal state */ - state = kzalloc(sizeof(struct af9033_state), GFP_KERNEL); - if (state == NULL) + ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01); + if (ret < 0) goto err; - /* setup the state */ - state->i2c = i2c; - memcpy(&state->cfg, config, sizeof(struct af9033_config)); + return 0; + +err: + dev_dbg(&dev->client->dev, "failed=%d\n", ret); + + return ret; +} + +static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid, + int onoff) +{ + struct af9033_dev *dev = fe->demodulator_priv; + int ret; + u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff}; + + dev_dbg(&dev->client->dev, "index=%d pid=%04x onoff=%d\n", + index, pid, onoff); - if (state->cfg.clock != 12000000) { - dev_err(&state->i2c->dev, "%s: af9033: unsupported clock=%d, " \ - "only 12000000 Hz is supported currently\n", - KBUILD_MODNAME, state->cfg.clock); + if (pid > 0x1fff) + return 0; + + ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2); + if (ret < 0) goto err; - } - /* firmware version */ - ret = af9033_rd_regs(state, 0x0083e9, &buf[0], 4); + ret = af9033_wr_reg(dev, 0x80f994, onoff); if (ret < 0) goto err; - ret = af9033_rd_regs(state, 0x804191, &buf[4], 4); + ret = af9033_wr_reg(dev, 0x80f995, index); if (ret < 0) goto err; - dev_info(&state->i2c->dev, "%s: firmware version: LINK=%d.%d.%d.%d " \ - "OFDM=%d.%d.%d.%d\n", KBUILD_MODNAME, buf[0], buf[1], - buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]); + return 0; - /* sleep */ - switch (state->cfg.tuner) { - case AF9033_TUNER_IT9135_38: - case AF9033_TUNER_IT9135_51: - case AF9033_TUNER_IT9135_52: - case AF9033_TUNER_IT9135_60: - case AF9033_TUNER_IT9135_61: - case AF9033_TUNER_IT9135_62: - /* IT9135 did not like to sleep at that early */ - break; - default: - ret = af9033_wr_reg(state, 0x80004c, 1); - if (ret < 0) - goto err; +err: + dev_dbg(&dev->client->dev, "failed=%d\n", ret); - ret = af9033_wr_reg(state, 0x800000, 0); - if (ret < 0) + return ret; +} + +static void af9033_stat_work(struct work_struct *work) +{ + struct af9033_dev *dev = container_of(work, struct af9033_dev, stat_work.work); + struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache; + int ret, tmp, i, len; + u8 u8tmp, buf[7]; + + dev_dbg(&dev->client->dev, "\n"); + + /* signal strength */ + if (dev->fe_status & FE_HAS_SIGNAL) { + if (dev->is_af9035) { + ret = af9033_rd_reg(dev, 0x80004a, &u8tmp); + tmp = -u8tmp * 1000; + } else { + ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp); + tmp = (u8tmp - 100) * 1000; + } + if (ret) goto err; + + c->strength.len = 1; + c->strength.stat[0].scale = FE_SCALE_DECIBEL; + c->strength.stat[0].svalue = tmp; + } else { + c->strength.len = 1; + c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; } - /* configure internal TS mode */ - switch (state->cfg.ts_mode) { - case AF9033_TS_MODE_PARALLEL: - state->ts_mode_parallel = true; - break; - case AF9033_TS_MODE_SERIAL: - state->ts_mode_serial = true; - break; - case AF9033_TS_MODE_USB: - /* usb mode for AF9035 */ - default: - break; + /* CNR */ + if (dev->fe_status & FE_HAS_VITERBI) { + u32 snr_val; + const struct val_snr *snr_lut; + + /* read value */ + ret = af9033_rd_regs(dev, 0x80002c, buf, 3); + if (ret) + goto err; + + snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0); + + /* read superframe number */ + ret = af9033_rd_reg(dev, 0x80f78b, &u8tmp); + if (ret) + goto err; + + if (u8tmp) + snr_val /= u8tmp; + + /* read current transmission mode */ + ret = af9033_rd_reg(dev, 0x80f900, &u8tmp); + if (ret) + goto err; + + switch ((u8tmp >> 0) & 3) { + case 0: + snr_val *= 4; + break; + case 1: + snr_val *= 1; + break; + case 2: + snr_val *= 2; + break; + default: + goto err_schedule_delayed_work; + } + + /* read current modulation */ + ret = af9033_rd_reg(dev, 0x80f903, &u8tmp); + if (ret) + goto err; + + switch ((u8tmp >> 0) & 3) { + case 0: + len = ARRAY_SIZE(qpsk_snr_lut); + snr_lut = qpsk_snr_lut; + break; + case 1: + len = ARRAY_SIZE(qam16_snr_lut); + snr_lut = qam16_snr_lut; + break; + case 2: + len = ARRAY_SIZE(qam64_snr_lut); + snr_lut = qam64_snr_lut; + break; + default: + goto err_schedule_delayed_work; + } + + for (i = 0; i < len; i++) { + tmp = snr_lut[i].snr * 1000; + if (snr_val < snr_lut[i].val) + break; + } + + c->cnr.len = 1; + c->cnr.stat[0].scale = FE_SCALE_DECIBEL; + c->cnr.stat[0].svalue = tmp; + } else { + c->cnr.len = 1; + c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; } - /* create dvb_frontend */ - memcpy(&state->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops)); - state->fe.demodulator_priv = state; + /* UCB/PER/BER */ + if (dev->fe_status & FE_HAS_LOCK) { + /* outer FEC, 204 byte packets */ + u16 abort_packet_count, rsd_packet_count; + /* inner FEC, bits */ + u32 rsd_bit_err_count; + + /* + * Packet count used for measurement is 10000 + * (rsd_packet_count). Maybe it should be increased? + */ - return &state->fe; + ret = af9033_rd_regs(dev, 0x800032, buf, 7); + if (ret) + goto err; + abort_packet_count = (buf[1] << 8) | (buf[0] << 0); + rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2]; + rsd_packet_count = (buf[6] << 8) | (buf[5] << 0); + + dev->error_block_count += abort_packet_count; + dev->total_block_count += rsd_packet_count; + dev->post_bit_error += rsd_bit_err_count; + dev->post_bit_count += rsd_packet_count * 204 * 8; + + c->block_count.len = 1; + c->block_count.stat[0].scale = FE_SCALE_COUNTER; + c->block_count.stat[0].uvalue = dev->total_block_count; + + c->block_error.len = 1; + c->block_error.stat[0].scale = FE_SCALE_COUNTER; + c->block_error.stat[0].uvalue = dev->error_block_count; + + c->post_bit_count.len = 1; + c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; + c->post_bit_count.stat[0].uvalue = dev->post_bit_count; + + c->post_bit_error.len = 1; + c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; + c->post_bit_error.stat[0].uvalue = dev->post_bit_error; + } + +err_schedule_delayed_work: + schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000)); + return; err: - kfree(state); - return NULL; + dev_dbg(&dev->client->dev, "failed=%d\n", ret); } -EXPORT_SYMBOL(af9033_attach); static struct dvb_frontend_ops af9033_ops = { .delsys = { SYS_DVBT }, @@ -1100,8 +1237,6 @@ FE_CAN_MUTE_TS }, - .release = af9033_release, - .init = af9033_init, .sleep = af9033_sleep, @@ -1118,6 +1253,149 @@ .i2c_gate_ctrl = af9033_i2c_gate_ctrl, }; +static int af9033_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct af9033_config *cfg = client->dev.platform_data; + struct af9033_dev *dev; + int ret; + u8 buf[8]; + u32 reg; + + /* allocate memory for the internal state */ + dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL); + if (dev == NULL) { + ret = -ENOMEM; + dev_err(&client->dev, "Could not allocate memory for state\n"); + goto err; + } + + /* setup the state */ + dev->client = client; + INIT_DELAYED_WORK(&dev->stat_work, af9033_stat_work); + memcpy(&dev->cfg, cfg, sizeof(struct af9033_config)); + + if (dev->cfg.clock != 12000000) { + ret = -ENODEV; + dev_err(&dev->client->dev, + "unsupported clock %d Hz, only 12000000 Hz is supported currently\n", + dev->cfg.clock); + goto err_kfree; + } + + /* firmware version */ + switch (dev->cfg.tuner) { + case AF9033_TUNER_IT9135_38: + case AF9033_TUNER_IT9135_51: + case AF9033_TUNER_IT9135_52: + case AF9033_TUNER_IT9135_60: + case AF9033_TUNER_IT9135_61: + case AF9033_TUNER_IT9135_62: + dev->is_it9135 = true; + reg = 0x004bfc; + break; + default: + dev->is_af9035 = true; + reg = 0x0083e9; + break; + } + + ret = af9033_rd_regs(dev, reg, &buf[0], 4); + if (ret < 0) + goto err_kfree; + + ret = af9033_rd_regs(dev, 0x804191, &buf[4], 4); + if (ret < 0) + goto err_kfree; + + dev_info(&dev->client->dev, + "firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n", + buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], + buf[7]); + + /* sleep */ + switch (dev->cfg.tuner) { + case AF9033_TUNER_IT9135_38: + case AF9033_TUNER_IT9135_51: + case AF9033_TUNER_IT9135_52: + case AF9033_TUNER_IT9135_60: + case AF9033_TUNER_IT9135_61: + case AF9033_TUNER_IT9135_62: + /* IT9135 did not like to sleep at that early */ + break; + default: + ret = af9033_wr_reg(dev, 0x80004c, 1); + if (ret < 0) + goto err_kfree; + + ret = af9033_wr_reg(dev, 0x800000, 0); + if (ret < 0) + goto err_kfree; + } + + /* configure internal TS mode */ + switch (dev->cfg.ts_mode) { + case AF9033_TS_MODE_PARALLEL: + dev->ts_mode_parallel = true; + break; + case AF9033_TS_MODE_SERIAL: + dev->ts_mode_serial = true; + break; + case AF9033_TS_MODE_USB: + /* usb mode for AF9035 */ + default: + break; + } + + /* create dvb_frontend */ + memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops)); + dev->fe.demodulator_priv = dev; + *cfg->fe = &dev->fe; + if (cfg->ops) { + cfg->ops->pid_filter = af9033_pid_filter; + cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl; + } + i2c_set_clientdata(client, dev); + + dev_info(&dev->client->dev, "Afatech AF9033 successfully attached\n"); + return 0; +err_kfree: + kfree(dev); +err: + dev_dbg(&client->dev, "failed=%d\n", ret); + return ret; +} + +static int af9033_remove(struct i2c_client *client) +{ + struct af9033_dev *dev = i2c_get_clientdata(client); + + dev_dbg(&dev->client->dev, "\n"); + + dev->fe.ops.release = NULL; + dev->fe.demodulator_priv = NULL; + kfree(dev); + + return 0; +} + +static const struct i2c_device_id af9033_id_table[] = { + {"af9033", 0}, + {} +}; +MODULE_DEVICE_TABLE(i2c, af9033_id_table); + +static struct i2c_driver af9033_driver = { + .driver = { + .name = "af9033", + }, + .probe = af9033_probe, + .remove = af9033_remove, + .id_table = af9033_id_table, +}; + +module_i2c_driver(af9033_driver); + MODULE_AUTHOR("Antti Palosaari "); MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver"); MODULE_LICENSE("GPL");