diff options
Diffstat (limited to 'drivers/media/dvb-frontends/af9033.c')
-rw-r--r-- | drivers/media/dvb-frontends/af9033.c | 837 |
1 files changed, 332 insertions, 505 deletions
diff --git a/drivers/media/dvb-frontends/af9033.c b/drivers/media/dvb-frontends/af9033.c index f8818028752e..aaed7cfe5f66 100644 --- a/drivers/media/dvb-frontends/af9033.c +++ b/drivers/media/dvb-frontends/af9033.c @@ -13,19 +13,13 @@ * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "af9033_priv.h" -/* Max transfer size done by I2C transfer functions */ -#define MAX_XFER_SIZE 64 - struct af9033_dev { struct i2c_client *client; + struct regmap *regmap; struct dvb_frontend fe; struct af9033_config cfg; bool is_af9035; @@ -43,146 +37,19 @@ struct af9033_dev { u64 total_block_count; }; -/* write multiple registers */ -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 = dev->client->addr, - .flags = 0, - .len = 3 + len, - .buf = buf, - } - }; - - if (3 + len > sizeof(buf)) { - dev_warn(&dev->client->dev, - "i2c wr reg=%04x: len=%d is too big!\n", - reg, len); - return -EINVAL; - } - - buf[0] = (reg >> 16) & 0xff; - buf[1] = (reg >> 8) & 0xff; - buf[2] = (reg >> 0) & 0xff; - memcpy(&buf[3], val, len); - - ret = i2c_transfer(dev->client->adapter, msg, 1); - if (ret == 1) { - ret = 0; - } else { - dev_warn(&dev->client->dev, "i2c wr failed=%d reg=%06x len=%d\n", - ret, reg, len); - ret = -EREMOTEIO; - } - - return ret; -} - -/* read multiple registers */ -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 = dev->client->addr, - .flags = 0, - .len = sizeof(buf), - .buf = buf - }, { - .addr = dev->client->addr, - .flags = I2C_M_RD, - .len = len, - .buf = val - } - }; - - ret = i2c_transfer(dev->client->adapter, msg, 2); - if (ret == 2) { - ret = 0; - } else { - dev_warn(&dev->client->dev, "i2c rd failed=%d reg=%06x len=%d\n", - ret, reg, len); - ret = -EREMOTEIO; - } - - return ret; -} - - -/* write single register */ -static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val) -{ - return af9033_wr_regs(dev, reg, &val, 1); -} - -/* read single register */ -static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val) -{ - return af9033_rd_regs(dev, reg, val, 1); -} - -/* write single register with mask */ -static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val, - u8 mask) -{ - int ret; - u8 tmp; - - /* no need for read if whole reg is written */ - if (mask != 0xff) { - ret = af9033_rd_regs(dev, reg, &tmp, 1); - if (ret) - return ret; - - val &= mask; - tmp &= ~mask; - val |= tmp; - } - - return af9033_wr_regs(dev, reg, &val, 1); -} - -/* read single register with mask */ -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(dev, reg, &tmp, 1); - if (ret) - return ret; - - tmp &= mask; - - /* find position of the first bit */ - for (i = 0; i < 8; i++) { - if ((mask >> i) & 0x01) - break; - } - *val = tmp >> i; - - return 0; -} - -/* write reg val table using reg addr auto increment */ +/* Write reg val table using reg addr auto increment */ static int af9033_wr_reg_val_tab(struct af9033_dev *dev, - const struct reg_val *tab, int tab_len) + const struct reg_val *tab, int tab_len) { + struct i2c_client *client = dev->client; #define MAX_TAB_LEN 212 int ret, i, j; u8 buf[1 + MAX_TAB_LEN]; - dev_dbg(&dev->client->dev, "tab_len=%d\n", tab_len); + dev_dbg(&client->dev, "tab_len=%d\n", tab_len); if (tab_len > sizeof(buf)) { - dev_warn(&dev->client->dev, "tab len %d is too big\n", tab_len); + dev_warn(&client->dev, "tab len %d is too big\n", tab_len); return -EINVAL; } @@ -190,8 +57,9 @@ static int af9033_wr_reg_val_tab(struct af9033_dev *dev, buf[j] = tab[i].val; if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) { - ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1); - if (ret < 0) + ret = regmap_bulk_write(dev->regmap, tab[i].reg - j, + buf, j + 1); + if (ret) goto err; j = 0; @@ -201,47 +69,20 @@ static int af9033_wr_reg_val_tab(struct af9033_dev *dev, } return 0; - err: - dev_dbg(&dev->client->dev, "failed=%d\n", ret); - + dev_dbg(&client->dev, "failed=%d\n", ret); return ret; } -static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x) -{ - u32 r = 0, c = 0, i; - - dev_dbg(&dev->client->dev, "a=%d b=%d x=%d\n", a, b, x); - - if (a > b) { - c = a / b; - a = a - c * b; - } - - for (i = 0; i < x; i++) { - if (a >= b) { - r += 1; - a -= b; - } - a <<= 1; - r <<= 1; - } - r = (c << (u32)x) + 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 int af9033_init(struct dvb_frontend *fe) { struct af9033_dev *dev = fe->demodulator_priv; + struct i2c_client *client = dev->client; struct dtv_frontend_properties *c = &fe->dtv_property_cache; int ret, i, len; + unsigned int utmp; const struct reg_val *init; u8 buf[4]; - u32 adc_cw, clock_cw; struct reg_val_mask tab[] = { { 0x80fb24, 0x00, 0x08 }, { 0x80004c, 0x00, 0xff }, @@ -271,80 +112,76 @@ static int af9033_init(struct dvb_frontend *fe) { 0x800045, dev->cfg.adc_multiplier, 0xff }, }; - /* program clock control */ - 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(&dev->client->dev, "clock=%d clock_cw=%08x\n", - dev->cfg.clock, clock_cw); + dev_dbg(&client->dev, "\n"); - ret = af9033_wr_regs(dev, 0x800025, buf, 4); - if (ret < 0) + /* Main clk control */ + utmp = div_u64((u64)dev->cfg.clock * 0x80000, 1000000); + buf[0] = (utmp >> 0) & 0xff; + buf[1] = (utmp >> 8) & 0xff; + buf[2] = (utmp >> 16) & 0xff; + buf[3] = (utmp >> 24) & 0xff; + ret = regmap_bulk_write(dev->regmap, 0x800025, buf, 4); + if (ret) goto err; - /* program ADC control */ + dev_dbg(&client->dev, "clk=%u clk_cw=%08x\n", dev->cfg.clock, utmp); + + /* ADC clk control */ for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { 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_err(&client->dev, "Couldn't find ADC config for clock %d\n", dev->cfg.clock); goto err; } - 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(&dev->client->dev, "adc=%d adc_cw=%06x\n", - clock_adc_lut[i].adc, adc_cw); - - ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3); - if (ret < 0) + utmp = div_u64((u64)clock_adc_lut[i].adc * 0x80000, 1000000); + buf[0] = (utmp >> 0) & 0xff; + buf[1] = (utmp >> 8) & 0xff; + buf[2] = (utmp >> 16) & 0xff; + ret = regmap_bulk_write(dev->regmap, 0x80f1cd, buf, 3); + if (ret) goto err; - /* program register table */ + dev_dbg(&client->dev, "adc=%u adc_cw=%06x\n", + clock_adc_lut[i].adc, utmp); + + /* Config register table */ for (i = 0; i < ARRAY_SIZE(tab); i++) { - ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val, - tab[i].mask); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, tab[i].reg, tab[i].mask, + tab[i].val); + if (ret) goto err; } - /* clock output */ + /* Demod clk output */ if (dev->cfg.dyn0_clk) { - ret = af9033_wr_reg(dev, 0x80fba8, 0x00); - if (ret < 0) + ret = regmap_write(dev->regmap, 0x80fba8, 0x00); + if (ret) goto err; } - /* settings for TS interface */ + /* TS interface */ if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) { - ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x80f9a5, 0x01, 0x00); + if (ret) goto err; - - ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x80f9b5, 0x01, 0x01); + if (ret) goto err; } else { - ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x80f990, 0x01, 0x00); + if (ret) goto err; - - ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x80f9b5, 0x01, 0x00); + if (ret) goto err; } - /* load OFSM settings */ - dev_dbg(&dev->client->dev, "load ofsm settings\n"); + /* Demod core settings */ + dev_dbg(&client->dev, "load ofsm settings\n"); switch (dev->cfg.tuner) { case AF9033_TUNER_IT9135_38: case AF9033_TUNER_IT9135_51: @@ -365,11 +202,11 @@ static int af9033_init(struct dvb_frontend *fe) } ret = af9033_wr_reg_val_tab(dev, init, len); - if (ret < 0) + if (ret) goto err; - /* load tuner specific settings */ - dev_dbg(&dev->client->dev, "load tuner specific settings\n"); + /* Demod tuner specific settings */ + dev_dbg(&client->dev, "load tuner specific settings\n"); switch (dev->cfg.tuner) { case AF9033_TUNER_TUA9001: len = ARRAY_SIZE(tuner_init_tua9001); @@ -420,27 +257,25 @@ static int af9033_init(struct dvb_frontend *fe) init = tuner_init_it9135_62; break; default: - dev_dbg(&dev->client->dev, "unsupported tuner ID=%d\n", - dev->cfg.tuner); + dev_dbg(&client->dev, "unsupported tuner ID=%d\n", + dev->cfg.tuner); ret = -ENODEV; goto err; } ret = af9033_wr_reg_val_tab(dev, init, len); - if (ret < 0) + if (ret) goto err; if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { - ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x00d91c, 0x01, 0x01); + if (ret) goto err; - - ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x00d917, 0x01, 0x00); + if (ret) goto err; - - ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x00d916, 0x01, 0x00); + if (ret) goto err; } @@ -448,13 +283,13 @@ static int af9033_init(struct dvb_frontend *fe) case AF9033_TUNER_IT9135_60: case AF9033_TUNER_IT9135_61: case AF9033_TUNER_IT9135_62: - ret = af9033_wr_reg(dev, 0x800000, 0x01); - if (ret < 0) + ret = regmap_write(dev->regmap, 0x800000, 0x01); + if (ret) goto err; } - dev->bandwidth_hz = 0; /* force to program all parameters */ - /* init stats here in order signal app which stats are supported */ + 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; @@ -469,68 +304,53 @@ static int af9033_init(struct dvb_frontend *fe) c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; return 0; - err: - dev_dbg(&dev->client->dev, "failed=%d\n", ret); - + dev_dbg(&client->dev, "failed=%d\n", ret); return ret; } static int af9033_sleep(struct dvb_frontend *fe) { struct af9033_dev *dev = fe->demodulator_priv; - int ret, i; - u8 tmp; + struct i2c_client *client = dev->client; + int ret; + unsigned int utmp; - ret = af9033_wr_reg(dev, 0x80004c, 1); - if (ret < 0) - goto err; + dev_dbg(&client->dev, "\n"); - ret = af9033_wr_reg(dev, 0x800000, 0); - if (ret < 0) + ret = regmap_write(dev->regmap, 0x80004c, 0x01); + if (ret) goto err; - - for (i = 100, tmp = 1; i && tmp; i--) { - ret = af9033_rd_reg(dev, 0x80004c, &tmp); - if (ret < 0) - goto err; - - usleep_range(200, 10000); - } - - dev_dbg(&dev->client->dev, "loop=%d\n", i); - - if (i == 0) { - ret = -ETIMEDOUT; + ret = regmap_write(dev->regmap, 0x800000, 0x00); + if (ret) goto err; - } - - ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08); - if (ret < 0) + ret = regmap_read_poll_timeout(dev->regmap, 0x80004c, utmp, utmp == 0, + 5000, 1000000); + if (ret) + goto err; + ret = regmap_update_bits(dev->regmap, 0x80fb24, 0x08, 0x08); + if (ret) goto err; - /* prevent current leak (?) */ + /* Prevent current leak by setting TS interface to parallel mode */ if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { - /* enable parallel TS */ - ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01); - if (ret < 0) + /* Enable parallel TS */ + ret = regmap_update_bits(dev->regmap, 0x00d917, 0x01, 0x00); + if (ret) goto err; - - ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x00d916, 0x01, 0x01); + if (ret) goto err; } return 0; - err: - dev_dbg(&dev->client->dev, "failed=%d\n", ret); - + dev_dbg(&client->dev, "failed=%d\n", ret); return ret; } static int af9033_get_tune_settings(struct dvb_frontend *fe, - struct dvb_frontend_tune_settings *fesettings) + struct dvb_frontend_tune_settings *fesettings) { /* 800 => 2000 because IT9135 v2 is slow to gain lock */ fesettings->min_delay_ms = 2000; @@ -543,15 +363,17 @@ static int af9033_get_tune_settings(struct dvb_frontend *fe, static int af9033_set_frontend(struct dvb_frontend *fe) { struct af9033_dev *dev = fe->demodulator_priv; + struct i2c_client *client = dev->client; struct dtv_frontend_properties *c = &fe->dtv_property_cache; - int ret, i, spec_inv, sampling_freq; + int ret, i; + unsigned int utmp, adc_freq; u8 tmp, buf[3], bandwidth_reg_val; - u32 if_frequency, freq_cw, adc_freq; + u32 if_frequency; - dev_dbg(&dev->client->dev, "frequency=%d bandwidth_hz=%d\n", - c->frequency, c->bandwidth_hz); + dev_dbg(&client->dev, "frequency=%u bandwidth_hz=%u\n", + c->frequency, c->bandwidth_hz); - /* check bandwidth */ + /* Check bandwidth */ switch (c->bandwidth_hz) { case 6000000: bandwidth_reg_val = 0x00; @@ -563,105 +385,91 @@ static int af9033_set_frontend(struct dvb_frontend *fe) bandwidth_reg_val = 0x02; break; default: - dev_dbg(&dev->client->dev, "invalid bandwidth_hz\n"); + dev_dbg(&client->dev, "invalid bandwidth_hz\n"); ret = -EINVAL; goto err; } - /* program tuner */ + /* Program tuner */ if (fe->ops.tuner_ops.set_params) fe->ops.tuner_ops.set_params(fe); - /* program CFOE coefficients */ + /* Coefficients */ if (c->bandwidth_hz != dev->bandwidth_hz) { for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) { if (coeff_lut[i].clock == dev->cfg.clock && - coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { + coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { break; } } if (i == ARRAY_SIZE(coeff_lut)) { - dev_err(&dev->client->dev, - "Couldn't find LUT config for clock=%d\n", + dev_err(&client->dev, + "Couldn't find config for clock %u\n", dev->cfg.clock); ret = -EINVAL; goto err; } - ret = af9033_wr_regs(dev, 0x800001, - coeff_lut[i].val, sizeof(coeff_lut[i].val)); + ret = regmap_bulk_write(dev->regmap, 0x800001, coeff_lut[i].val, + sizeof(coeff_lut[i].val)); + if (ret) + goto err; } - /* program frequency control */ + /* IF frequency control */ 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 == 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_err(&client->dev, + "Couldn't find ADC clock for clock %u\n", dev->cfg.clock); ret = -EINVAL; goto err; } adc_freq = clock_adc_lut[i].adc; - /* get used IF frequency */ + if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X) + adc_freq = 2 * adc_freq; + + /* Get used IF frequency */ if (fe->ops.tuner_ops.get_if_frequency) fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); else if_frequency = 0; - sampling_freq = if_frequency; - - while (sampling_freq > (adc_freq / 2)) - sampling_freq -= adc_freq; - - if (sampling_freq >= 0) - spec_inv *= -1; - else - sampling_freq *= -1; - - freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul); + utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x800000, + adc_freq); - if (spec_inv == -1) - freq_cw = 0x800000 - freq_cw; + if (!dev->cfg.spec_inv && if_frequency) + utmp = 0x800000 - utmp; - if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X) - freq_cw /= 2; - - buf[0] = (freq_cw >> 0) & 0xff; - buf[1] = (freq_cw >> 8) & 0xff; - buf[2] = (freq_cw >> 16) & 0x7f; - - /* FIXME: there seems to be calculation error here... */ - if (if_frequency == 0) - buf[2] = 0; - - ret = af9033_wr_regs(dev, 0x800029, buf, 3); - if (ret < 0) + buf[0] = (utmp >> 0) & 0xff; + buf[1] = (utmp >> 8) & 0xff; + buf[2] = (utmp >> 16) & 0xff; + ret = regmap_bulk_write(dev->regmap, 0x800029, buf, 3); + if (ret) goto err; + dev_dbg(&client->dev, "if_frequency_cw=%06x\n", utmp); + dev->bandwidth_hz = c->bandwidth_hz; } - ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x80f904, 0x03, + bandwidth_reg_val); + if (ret) goto err; - - ret = af9033_wr_reg(dev, 0x800040, 0x00); - if (ret < 0) + ret = regmap_write(dev->regmap, 0x800040, 0x00); + if (ret) goto err; - - ret = af9033_wr_reg(dev, 0x800047, 0x00); - if (ret < 0) + ret = regmap_write(dev->regmap, 0x800047, 0x00); + if (ret) goto err; - - ret = af9033_wr_reg_mask(dev, 0x80f999, 0x00, 0x01); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x80f999, 0x01, 0x00); + if (ret) goto err; if (c->frequency <= 230000000) @@ -669,19 +477,17 @@ static int af9033_set_frontend(struct dvb_frontend *fe) else tmp = 0x01; /* UHF */ - ret = af9033_wr_reg(dev, 0x80004b, tmp); - if (ret < 0) + ret = regmap_write(dev->regmap, 0x80004b, tmp); + if (ret) goto err; - - ret = af9033_wr_reg(dev, 0x800000, 0x00); - if (ret < 0) + /* Reset FSM */ + ret = regmap_write(dev->regmap, 0x800000, 0x00); + if (ret) goto err; return 0; - err: - dev_dbg(&dev->client->dev, "failed=%d\n", ret); - + dev_dbg(&client->dev, "failed=%d\n", ret); return ret; } @@ -689,14 +495,15 @@ static int af9033_get_frontend(struct dvb_frontend *fe, struct dtv_frontend_properties *c) { struct af9033_dev *dev = fe->demodulator_priv; + struct i2c_client *client = dev->client; int ret; u8 buf[8]; - dev_dbg(&dev->client->dev, "\n"); + dev_dbg(&client->dev, "\n"); - /* read all needed registers */ - ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf)); - if (ret < 0) + /* Read all needed TPS registers */ + ret = regmap_bulk_read(dev->regmap, 0x80f900, buf, 8); + if (ret) goto err; switch ((buf[0] >> 0) & 3) { @@ -805,49 +612,49 @@ static int af9033_get_frontend(struct dvb_frontend *fe, } return 0; - err: - dev_dbg(&dev->client->dev, "failed=%d\n", ret); - + dev_dbg(&client->dev, "failed=%d\n", ret); return ret; } static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status) { struct af9033_dev *dev = fe->demodulator_priv; + struct i2c_client *client = dev->client; struct dtv_frontend_properties *c = &fe->dtv_property_cache; - int ret, i, tmp = 0; - u8 u8tmp, buf[7]; + int ret, tmp = 0; + u8 buf[7]; + unsigned int utmp, utmp1; - dev_dbg(&dev->client->dev, "\n"); + dev_dbg(&client->dev, "\n"); *status = 0; - /* radio channel status, 0=no result, 1=has signal, 2=no signal */ - ret = af9033_rd_reg(dev, 0x800047, &u8tmp); - if (ret < 0) + /* Radio channel status: 0=no result, 1=has signal, 2=no signal */ + ret = regmap_read(dev->regmap, 0x800047, &utmp); + if (ret) goto err; - /* has signal */ - if (u8tmp == 0x01) + /* Has signal */ + if (utmp == 0x01) *status |= FE_HAS_SIGNAL; - if (u8tmp != 0x02) { + if (utmp != 0x02) { /* TPS lock */ - ret = af9033_rd_reg_mask(dev, 0x80f5a9, &u8tmp, 0x01); - if (ret < 0) + ret = regmap_read(dev->regmap, 0x80f5a9, &utmp); + if (ret) goto err; - if (u8tmp) + if ((utmp >> 0) & 0x01) *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI; - /* full lock */ - ret = af9033_rd_reg_mask(dev, 0x80f999, &u8tmp, 0x01); - if (ret < 0) + /* Full lock */ + ret = regmap_read(dev->regmap, 0x80f999, &utmp); + if (ret) goto err; - if (u8tmp) + if ((utmp >> 0) & 0x01) *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK; @@ -855,18 +662,18 @@ static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status) dev->fe_status = *status; - /* signal strength */ + /* Signal strength */ if (dev->fe_status & FE_HAS_SIGNAL) { if (dev->is_af9035) { - ret = af9033_rd_reg(dev, 0x80004a, &u8tmp); + ret = regmap_read(dev->regmap, 0x80004a, &utmp); if (ret) goto err; - tmp = -u8tmp * 1000; + tmp = -utmp * 1000; } else { - ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp); + ret = regmap_read(dev->regmap, 0x8000f7, &utmp); if (ret) goto err; - tmp = (u8tmp - 100) * 1000; + tmp = (utmp - 100) * 1000; } c->strength.len = 1; @@ -879,87 +686,101 @@ static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status) /* CNR */ if (dev->fe_status & FE_HAS_VITERBI) { - u32 snr_val, snr_lut_size; - const struct val_snr *snr_lut = NULL; - - /* read value */ - ret = af9033_rd_regs(dev, 0x80002c, buf, 3); + /* Read raw SNR value */ + ret = regmap_bulk_read(dev->regmap, 0x80002c, buf, 3); if (ret) goto err; - snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0); + utmp1 = buf[2] << 16 | buf[1] << 8 | buf[0] << 0; - /* read superframe number */ - ret = af9033_rd_reg(dev, 0x80f78b, &u8tmp); + /* Read superframe number */ + ret = regmap_read(dev->regmap, 0x80f78b, &utmp); if (ret) goto err; - if (u8tmp) - snr_val /= u8tmp; + if (utmp) + utmp1 /= utmp; - /* read current transmission mode */ - ret = af9033_rd_reg(dev, 0x80f900, &u8tmp); + /* Read current transmission mode */ + ret = regmap_read(dev->regmap, 0x80f900, &utmp); if (ret) goto err; - switch ((u8tmp >> 0) & 3) { + switch ((utmp >> 0) & 3) { case 0: - snr_val *= 4; + /* 2k */ + utmp1 *= 4; break; case 1: - snr_val *= 1; + /* 8k */ + utmp1 *= 1; break; case 2: - snr_val *= 2; + /* 4k */ + utmp1 *= 2; break; default: - snr_val *= 0; + utmp1 *= 0; break; } - /* read current modulation */ - ret = af9033_rd_reg(dev, 0x80f903, &u8tmp); + /* Read current modulation */ + ret = regmap_read(dev->regmap, 0x80f903, &utmp); if (ret) goto err; - switch ((u8tmp >> 0) & 3) { + switch ((utmp >> 0) & 3) { case 0: - snr_lut_size = ARRAY_SIZE(qpsk_snr_lut); - snr_lut = qpsk_snr_lut; + /* + * QPSK + * CNR[dB] 13 * -log10((1690000 - value) / value) + 2.6 + * value [653799, 1689999], 2.6 / 13 = 3355443 + */ + utmp1 = clamp(utmp1, 653799U, 1689999U); + utmp1 = ((u64)(intlog10(utmp1) + - intlog10(1690000 - utmp1) + + 3355443) * 13 * 1000) >> 24; break; case 1: - snr_lut_size = ARRAY_SIZE(qam16_snr_lut); - snr_lut = qam16_snr_lut; + /* + * QAM-16 + * CNR[dB] 6 * log10((value - 370000) / (828000 - value)) + 15.7 + * value [371105, 827999], 15.7 / 6 = 43900382 + */ + utmp1 = clamp(utmp1, 371105U, 827999U); + utmp1 = ((u64)(intlog10(utmp1 - 370000) + - intlog10(828000 - utmp1) + + 43900382) * 6 * 1000) >> 24; break; case 2: - snr_lut_size = ARRAY_SIZE(qam64_snr_lut); - snr_lut = qam64_snr_lut; + /* + * QAM-64 + * CNR[dB] 8 * log10((value - 193000) / (425000 - value)) + 23.8 + * value [193246, 424999], 23.8 / 8 = 49912218 + */ + utmp1 = clamp(utmp1, 193246U, 424999U); + utmp1 = ((u64)(intlog10(utmp1 - 193000) + - intlog10(425000 - utmp1) + + 49912218) * 8 * 1000) >> 24; break; default: - snr_lut_size = 0; - tmp = 0; + utmp1 = 0; break; } - for (i = 0; i < snr_lut_size; i++) { - tmp = snr_lut[i].snr * 1000; - if (snr_val < snr_lut[i].val) - break; - } + dev_dbg(&client->dev, "cnr=%u\n", utmp1); - c->cnr.len = 1; c->cnr.stat[0].scale = FE_SCALE_DECIBEL; - c->cnr.stat[0].svalue = tmp; + c->cnr.stat[0].svalue = utmp1; } else { - c->cnr.len = 1; c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; } /* UCB/PER/BER */ if (dev->fe_status & FE_HAS_LOCK) { - /* outer FEC, 204 byte packets */ + /* Outer FEC, 204 byte packets */ u16 abort_packet_count, rsd_packet_count; - /* inner FEC, bits */ + /* Inner FEC, bits */ u32 rsd_bit_err_count; /* @@ -967,7 +788,7 @@ static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status) * (rsd_packet_count). Maybe it should be increased? */ - ret = af9033_rd_regs(dev, 0x800032, buf, 7); + ret = regmap_bulk_read(dev->regmap, 0x800032, buf, 7); if (ret) goto err; @@ -998,21 +819,22 @@ static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status) } return 0; - err: - dev_dbg(&dev->client->dev, "failed=%d\n", ret); - + dev_dbg(&client->dev, "failed=%d\n", ret); return ret; } static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr) { struct af9033_dev *dev = fe->demodulator_priv; + struct i2c_client *client = dev->client; struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache; int ret; - u8 u8tmp; + unsigned int utmp; + + dev_dbg(&client->dev, "\n"); - /* use DVBv5 CNR */ + /* 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) { @@ -1022,13 +844,13 @@ static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr) /* 1000x => 1x (1 dB) */ *snr = div_s64(c->cnr.stat[0].svalue, 1000); - /* read current modulation */ - ret = af9033_rd_reg(dev, 0x80f903, &u8tmp); + /* Read current modulation */ + ret = regmap_read(dev->regmap, 0x80f903, &utmp); if (ret) goto err; /* scale value to 0x0000-0xffff */ - switch ((u8tmp >> 0) & 3) { + switch ((utmp >> 0) & 3) { case 0: *snr = *snr * 0xffff / 23; break; @@ -1047,35 +869,37 @@ static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr) } return 0; - err: - dev_dbg(&dev->client->dev, "failed=%d\n", ret); - + dev_dbg(&client->dev, "failed=%d\n", ret); return ret; } static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength) { struct af9033_dev *dev = fe->demodulator_priv; + struct i2c_client *client = dev->client; struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache; int ret, tmp, power_real; - u8 u8tmp, gain_offset, buf[7]; + unsigned int utmp; + u8 gain_offset, buf[7]; + + dev_dbg(&client->dev, "\n"); if (dev->is_af9035) { - /* read signal strength of 0-100 scale */ - ret = af9033_rd_reg(dev, 0x800048, &u8tmp); - if (ret < 0) + /* Read signal strength of 0-100 scale */ + ret = regmap_read(dev->regmap, 0x800048, &utmp); + if (ret) goto err; - /* scale value to 0x0000-0xffff */ - *strength = u8tmp * 0xffff / 100; + /* Scale value to 0x0000-0xffff */ + *strength = utmp * 0xffff / 100; } else { - ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp); - if (ret < 0) + ret = regmap_read(dev->regmap, 0x8000f7, &utmp); + if (ret) goto err; - ret = af9033_rd_regs(dev, 0x80f900, buf, 7); - if (ret < 0) + ret = regmap_bulk_read(dev->regmap, 0x80f900, buf, 7); + if (ret) goto err; if (c->frequency <= 300000000) @@ -1083,7 +907,7 @@ static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength) else gain_offset = 4; /* UHF */ - power_real = (u8tmp - 100 - gain_offset) - + power_real = (utmp - 100 - gain_offset) - power_reference[((buf[3] >> 0) & 3)][((buf[6] >> 0) & 7)]; if (power_real < -15) @@ -1097,15 +921,13 @@ static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength) else tmp = 100; - /* scale value to 0x0000-0xffff */ + /* Scale value to 0x0000-0xffff */ *strength = tmp * 0xffff / 100; } return 0; - err: - dev_dbg(&dev->client->dev, "failed=%d\n", ret); - + dev_dbg(&client->dev, "failed=%d\n", ret); return ret; } @@ -1124,82 +946,78 @@ static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) 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_dev *dev = fe->demodulator_priv; + struct i2c_client *client = dev->client; int ret; - dev_dbg(&dev->client->dev, "enable=%d\n", enable); + dev_dbg(&client->dev, "enable=%d\n", enable); - ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x00fa04, 0x01, enable); + if (ret) goto err; return 0; - err: - dev_dbg(&dev->client->dev, "failed=%d\n", ret); - + dev_dbg(&client->dev, "failed=%d\n", ret); return ret; } static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff) { struct af9033_dev *dev = fe->demodulator_priv; + struct i2c_client *client = dev->client; int ret; - dev_dbg(&dev->client->dev, "onoff=%d\n", onoff); + dev_dbg(&client->dev, "onoff=%d\n", onoff); - ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01); - if (ret < 0) + ret = regmap_update_bits(dev->regmap, 0x80f993, 0x01, onoff); + if (ret) goto err; return 0; - err: - dev_dbg(&dev->client->dev, "failed=%d\n", ret); - + dev_dbg(&client->dev, "failed=%d\n", ret); return ret; } static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid, - int onoff) + int onoff) { struct af9033_dev *dev = fe->demodulator_priv; + struct i2c_client *client = dev->client; 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); + dev_dbg(&client->dev, "index=%d pid=%04x onoff=%d\n", + index, pid, onoff); if (pid > 0x1fff) return 0; - ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2); - if (ret < 0) + ret = regmap_bulk_write(dev->regmap, 0x80f996, wbuf, 2); + if (ret) goto err; - - ret = af9033_wr_reg(dev, 0x80f994, onoff); - if (ret < 0) + ret = regmap_write(dev->regmap, 0x80f994, onoff); + if (ret) goto err; - - ret = af9033_wr_reg(dev, 0x80f995, index); - if (ret < 0) + ret = regmap_write(dev->regmap, 0x80f995, index); + if (ret) goto err; return 0; - err: - dev_dbg(&dev->client->dev, "failed=%d\n", ret); - + dev_dbg(&client->dev, "failed=%d\n", ret); return ret; } static const struct dvb_frontend_ops af9033_ops = { - .delsys = { SYS_DVBT }, + .delsys = {SYS_DVBT}, .info = { .name = "Afatech AF9033 (DVB-T)", .frequency_min = 174000000, @@ -1240,35 +1058,57 @@ static const struct dvb_frontend_ops af9033_ops = { }; static int af9033_probe(struct i2c_client *client, - const struct i2c_device_id *id) + 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; + static const struct regmap_config regmap_config = { + .reg_bits = 24, + .val_bits = 8, + }; - /* allocate memory for the internal state */ - dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL); - if (dev == NULL) { + /* Allocate memory for the internal state */ + dev = kzalloc(sizeof(*dev), GFP_KERNEL); + if (!dev) { ret = -ENOMEM; - dev_err(&client->dev, "Could not allocate memory for state\n"); goto err; } - /* setup the state */ + /* Setup the state */ dev->client = client; - memcpy(&dev->cfg, cfg, sizeof(struct af9033_config)); + memcpy(&dev->cfg, cfg, sizeof(dev->cfg)); + 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; + } 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); + dev_err(&client->dev, + "Unsupported clock %u Hz. Only 12000000 Hz is supported currently\n", + dev->cfg.clock); + goto err_kfree; + } + + /* Create regmap */ + dev->regmap = regmap_init_i2c(client, ®map_config); + if (IS_ERR(dev->regmap)) { + ret = PTR_ERR(dev->regmap); goto err_kfree; } - /* firmware version */ + /* Firmware version */ switch (dev->cfg.tuner) { case AF9033_TUNER_IT9135_38: case AF9033_TUNER_IT9135_51: @@ -1285,20 +1125,19 @@ static int af9033_probe(struct i2c_client *client, 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; + ret = regmap_bulk_read(dev->regmap, reg, &buf[0], 4); + if (ret) + goto err_regmap_exit; + ret = regmap_bulk_read(dev->regmap, 0x804191, &buf[4], 4); + if (ret) + goto err_regmap_exit; - 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]); + dev_info(&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 */ + /* Sleep as chip seems to be partly active by default */ switch (dev->cfg.tuner) { case AF9033_TUNER_IT9135_38: case AF9033_TUNER_IT9135_51: @@ -1309,41 +1148,30 @@ static int af9033_probe(struct i2c_client *client, /* 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; + ret = regmap_write(dev->regmap, 0x80004c, 0x01); + if (ret) + goto err_regmap_exit; + ret = regmap_write(dev->regmap, 0x800000, 0x00); + if (ret) + goto err_regmap_exit; } - /* create dvb_frontend */ - memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops)); + /* Create dvb frontend */ + memcpy(&dev->fe.ops, &af9033_ops, sizeof(dev->fe.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; } + cfg->regmap = dev->regmap; i2c_set_clientdata(client, dev); - dev_info(&dev->client->dev, "Afatech AF9033 successfully attached\n"); + dev_info(&client->dev, "Afatech AF9033 successfully attached\n"); + return 0; +err_regmap_exit: + regmap_exit(dev->regmap); err_kfree: kfree(dev); err: @@ -1355,10 +1183,9 @@ static int af9033_remove(struct i2c_client *client) { struct af9033_dev *dev = i2c_get_clientdata(client); - dev_dbg(&dev->client->dev, "\n"); + dev_dbg(&client->dev, "\n"); - dev->fe.ops.release = NULL; - dev->fe.demodulator_priv = NULL; + regmap_exit(dev->regmap); kfree(dev); return 0; |