19a0bf528SMauro Carvalho Chehab /* 29a0bf528SMauro Carvalho Chehab * Afatech AF9033 demodulator driver 39a0bf528SMauro Carvalho Chehab * 49a0bf528SMauro Carvalho Chehab * Copyright (C) 2009 Antti Palosaari <crope@iki.fi> 59a0bf528SMauro Carvalho Chehab * Copyright (C) 2012 Antti Palosaari <crope@iki.fi> 69a0bf528SMauro Carvalho Chehab * 79a0bf528SMauro Carvalho Chehab * This program is free software; you can redistribute it and/or modify 89a0bf528SMauro Carvalho Chehab * it under the terms of the GNU General Public License as published by 99a0bf528SMauro Carvalho Chehab * the Free Software Foundation; either version 2 of the License, or 109a0bf528SMauro Carvalho Chehab * (at your option) any later version. 119a0bf528SMauro Carvalho Chehab * 129a0bf528SMauro Carvalho Chehab * This program is distributed in the hope that it will be useful, 139a0bf528SMauro Carvalho Chehab * but WITHOUT ANY WARRANTY; without even the implied warranty of 149a0bf528SMauro Carvalho Chehab * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 159a0bf528SMauro Carvalho Chehab * GNU General Public License for more details. 169a0bf528SMauro Carvalho Chehab * 179a0bf528SMauro Carvalho Chehab * You should have received a copy of the GNU General Public License along 189a0bf528SMauro Carvalho Chehab * with this program; if not, write to the Free Software Foundation, Inc., 199a0bf528SMauro Carvalho Chehab * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 209a0bf528SMauro Carvalho Chehab */ 219a0bf528SMauro Carvalho Chehab 229a0bf528SMauro Carvalho Chehab #include "af9033_priv.h" 239a0bf528SMauro Carvalho Chehab 2437ebaf68SMauro Carvalho Chehab /* Max transfer size done by I2C transfer functions */ 2537ebaf68SMauro Carvalho Chehab #define MAX_XFER_SIZE 64 2637ebaf68SMauro Carvalho Chehab 2709611caaSAntti Palosaari struct af9033_dev { 289a0bf528SMauro Carvalho Chehab struct i2c_adapter *i2c; 299a0bf528SMauro Carvalho Chehab struct dvb_frontend fe; 309a0bf528SMauro Carvalho Chehab struct af9033_config cfg; 319a0bf528SMauro Carvalho Chehab 329a0bf528SMauro Carvalho Chehab u32 bandwidth_hz; 339a0bf528SMauro Carvalho Chehab bool ts_mode_parallel; 349a0bf528SMauro Carvalho Chehab bool ts_mode_serial; 359a0bf528SMauro Carvalho Chehab 369a0bf528SMauro Carvalho Chehab u32 ber; 379a0bf528SMauro Carvalho Chehab u32 ucb; 389a0bf528SMauro Carvalho Chehab unsigned long last_stat_check; 399a0bf528SMauro Carvalho Chehab }; 409a0bf528SMauro Carvalho Chehab 419a0bf528SMauro Carvalho Chehab /* write multiple registers */ 4209611caaSAntti Palosaari static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val, 439a0bf528SMauro Carvalho Chehab int len) 449a0bf528SMauro Carvalho Chehab { 459a0bf528SMauro Carvalho Chehab int ret; 4637ebaf68SMauro Carvalho Chehab u8 buf[MAX_XFER_SIZE]; 479a0bf528SMauro Carvalho Chehab struct i2c_msg msg[1] = { 489a0bf528SMauro Carvalho Chehab { 4909611caaSAntti Palosaari .addr = dev->cfg.i2c_addr, 509a0bf528SMauro Carvalho Chehab .flags = 0, 5137ebaf68SMauro Carvalho Chehab .len = 3 + len, 529a0bf528SMauro Carvalho Chehab .buf = buf, 539a0bf528SMauro Carvalho Chehab } 549a0bf528SMauro Carvalho Chehab }; 559a0bf528SMauro Carvalho Chehab 5637ebaf68SMauro Carvalho Chehab if (3 + len > sizeof(buf)) { 5709611caaSAntti Palosaari dev_warn(&dev->i2c->dev, 5837ebaf68SMauro Carvalho Chehab "%s: i2c wr reg=%04x: len=%d is too big!\n", 5937ebaf68SMauro Carvalho Chehab KBUILD_MODNAME, reg, len); 6037ebaf68SMauro Carvalho Chehab return -EINVAL; 6137ebaf68SMauro Carvalho Chehab } 6237ebaf68SMauro Carvalho Chehab 639a0bf528SMauro Carvalho Chehab buf[0] = (reg >> 16) & 0xff; 649a0bf528SMauro Carvalho Chehab buf[1] = (reg >> 8) & 0xff; 659a0bf528SMauro Carvalho Chehab buf[2] = (reg >> 0) & 0xff; 669a0bf528SMauro Carvalho Chehab memcpy(&buf[3], val, len); 679a0bf528SMauro Carvalho Chehab 6809611caaSAntti Palosaari ret = i2c_transfer(dev->i2c, msg, 1); 699a0bf528SMauro Carvalho Chehab if (ret == 1) { 709a0bf528SMauro Carvalho Chehab ret = 0; 719a0bf528SMauro Carvalho Chehab } else { 7209611caaSAntti Palosaari dev_warn(&dev->i2c->dev, 7324e419a0SAntti Palosaari "%s: i2c wr failed=%d reg=%06x len=%d\n", 7424e419a0SAntti Palosaari KBUILD_MODNAME, ret, reg, len); 759a0bf528SMauro Carvalho Chehab ret = -EREMOTEIO; 769a0bf528SMauro Carvalho Chehab } 779a0bf528SMauro Carvalho Chehab 789a0bf528SMauro Carvalho Chehab return ret; 799a0bf528SMauro Carvalho Chehab } 809a0bf528SMauro Carvalho Chehab 819a0bf528SMauro Carvalho Chehab /* read multiple registers */ 8209611caaSAntti Palosaari static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len) 839a0bf528SMauro Carvalho Chehab { 849a0bf528SMauro Carvalho Chehab int ret; 859a0bf528SMauro Carvalho Chehab u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff, 869a0bf528SMauro Carvalho Chehab (reg >> 0) & 0xff }; 879a0bf528SMauro Carvalho Chehab struct i2c_msg msg[2] = { 889a0bf528SMauro Carvalho Chehab { 8909611caaSAntti Palosaari .addr = dev->cfg.i2c_addr, 909a0bf528SMauro Carvalho Chehab .flags = 0, 919a0bf528SMauro Carvalho Chehab .len = sizeof(buf), 929a0bf528SMauro Carvalho Chehab .buf = buf 939a0bf528SMauro Carvalho Chehab }, { 9409611caaSAntti Palosaari .addr = dev->cfg.i2c_addr, 959a0bf528SMauro Carvalho Chehab .flags = I2C_M_RD, 969a0bf528SMauro Carvalho Chehab .len = len, 979a0bf528SMauro Carvalho Chehab .buf = val 989a0bf528SMauro Carvalho Chehab } 999a0bf528SMauro Carvalho Chehab }; 1009a0bf528SMauro Carvalho Chehab 10109611caaSAntti Palosaari ret = i2c_transfer(dev->i2c, msg, 2); 1029a0bf528SMauro Carvalho Chehab if (ret == 2) { 1039a0bf528SMauro Carvalho Chehab ret = 0; 1049a0bf528SMauro Carvalho Chehab } else { 10509611caaSAntti Palosaari dev_warn(&dev->i2c->dev, 10624e419a0SAntti Palosaari "%s: i2c rd failed=%d reg=%06x len=%d\n", 10724e419a0SAntti Palosaari KBUILD_MODNAME, ret, reg, len); 1089a0bf528SMauro Carvalho Chehab ret = -EREMOTEIO; 1099a0bf528SMauro Carvalho Chehab } 1109a0bf528SMauro Carvalho Chehab 1119a0bf528SMauro Carvalho Chehab return ret; 1129a0bf528SMauro Carvalho Chehab } 1139a0bf528SMauro Carvalho Chehab 1149a0bf528SMauro Carvalho Chehab 1159a0bf528SMauro Carvalho Chehab /* write single register */ 11609611caaSAntti Palosaari static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val) 1179a0bf528SMauro Carvalho Chehab { 11809611caaSAntti Palosaari return af9033_wr_regs(dev, reg, &val, 1); 1199a0bf528SMauro Carvalho Chehab } 1209a0bf528SMauro Carvalho Chehab 1219a0bf528SMauro Carvalho Chehab /* read single register */ 12209611caaSAntti Palosaari static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val) 1239a0bf528SMauro Carvalho Chehab { 12409611caaSAntti Palosaari return af9033_rd_regs(dev, reg, val, 1); 1259a0bf528SMauro Carvalho Chehab } 1269a0bf528SMauro Carvalho Chehab 1279a0bf528SMauro Carvalho Chehab /* write single register with mask */ 12809611caaSAntti Palosaari static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val, 1299a0bf528SMauro Carvalho Chehab u8 mask) 1309a0bf528SMauro Carvalho Chehab { 1319a0bf528SMauro Carvalho Chehab int ret; 1329a0bf528SMauro Carvalho Chehab u8 tmp; 1339a0bf528SMauro Carvalho Chehab 1349a0bf528SMauro Carvalho Chehab /* no need for read if whole reg is written */ 1359a0bf528SMauro Carvalho Chehab if (mask != 0xff) { 13609611caaSAntti Palosaari ret = af9033_rd_regs(dev, reg, &tmp, 1); 1379a0bf528SMauro Carvalho Chehab if (ret) 1389a0bf528SMauro Carvalho Chehab return ret; 1399a0bf528SMauro Carvalho Chehab 1409a0bf528SMauro Carvalho Chehab val &= mask; 1419a0bf528SMauro Carvalho Chehab tmp &= ~mask; 1429a0bf528SMauro Carvalho Chehab val |= tmp; 1439a0bf528SMauro Carvalho Chehab } 1449a0bf528SMauro Carvalho Chehab 14509611caaSAntti Palosaari return af9033_wr_regs(dev, reg, &val, 1); 1469a0bf528SMauro Carvalho Chehab } 1479a0bf528SMauro Carvalho Chehab 1489a0bf528SMauro Carvalho Chehab /* read single register with mask */ 14909611caaSAntti Palosaari static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val, 1509a0bf528SMauro Carvalho Chehab u8 mask) 1519a0bf528SMauro Carvalho Chehab { 1529a0bf528SMauro Carvalho Chehab int ret, i; 1539a0bf528SMauro Carvalho Chehab u8 tmp; 1549a0bf528SMauro Carvalho Chehab 15509611caaSAntti Palosaari ret = af9033_rd_regs(dev, reg, &tmp, 1); 1569a0bf528SMauro Carvalho Chehab if (ret) 1579a0bf528SMauro Carvalho Chehab return ret; 1589a0bf528SMauro Carvalho Chehab 1599a0bf528SMauro Carvalho Chehab tmp &= mask; 1609a0bf528SMauro Carvalho Chehab 1619a0bf528SMauro Carvalho Chehab /* find position of the first bit */ 1629a0bf528SMauro Carvalho Chehab for (i = 0; i < 8; i++) { 1639a0bf528SMauro Carvalho Chehab if ((mask >> i) & 0x01) 1649a0bf528SMauro Carvalho Chehab break; 1659a0bf528SMauro Carvalho Chehab } 1669a0bf528SMauro Carvalho Chehab *val = tmp >> i; 1679a0bf528SMauro Carvalho Chehab 1689a0bf528SMauro Carvalho Chehab return 0; 1699a0bf528SMauro Carvalho Chehab } 1709a0bf528SMauro Carvalho Chehab 1713bf5e552SAntti Palosaari /* write reg val table using reg addr auto increment */ 17209611caaSAntti Palosaari static int af9033_wr_reg_val_tab(struct af9033_dev *dev, 1733bf5e552SAntti Palosaari const struct reg_val *tab, int tab_len) 1743bf5e552SAntti Palosaari { 175d18a88b1SAntti Palosaari #define MAX_TAB_LEN 212 1763bf5e552SAntti Palosaari int ret, i, j; 177d18a88b1SAntti Palosaari u8 buf[1 + MAX_TAB_LEN]; 178d18a88b1SAntti Palosaari 17909611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: tab_len=%d\n", __func__, tab_len); 18037ebaf68SMauro Carvalho Chehab 18137ebaf68SMauro Carvalho Chehab if (tab_len > sizeof(buf)) { 18209611caaSAntti Palosaari dev_warn(&dev->i2c->dev, "%s: tab len %d is too big\n", 18337ebaf68SMauro Carvalho Chehab KBUILD_MODNAME, tab_len); 18437ebaf68SMauro Carvalho Chehab return -EINVAL; 18537ebaf68SMauro Carvalho Chehab } 1863bf5e552SAntti Palosaari 1873bf5e552SAntti Palosaari for (i = 0, j = 0; i < tab_len; i++) { 1883bf5e552SAntti Palosaari buf[j] = tab[i].val; 1893bf5e552SAntti Palosaari 1903bf5e552SAntti Palosaari if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) { 19109611caaSAntti Palosaari ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1); 1923bf5e552SAntti Palosaari if (ret < 0) 1933bf5e552SAntti Palosaari goto err; 1943bf5e552SAntti Palosaari 1953bf5e552SAntti Palosaari j = 0; 1963bf5e552SAntti Palosaari } else { 1973bf5e552SAntti Palosaari j++; 1983bf5e552SAntti Palosaari } 1993bf5e552SAntti Palosaari } 2003bf5e552SAntti Palosaari 2013bf5e552SAntti Palosaari return 0; 2023bf5e552SAntti Palosaari 2033bf5e552SAntti Palosaari err: 20409611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 2053bf5e552SAntti Palosaari 2063bf5e552SAntti Palosaari return ret; 2073bf5e552SAntti Palosaari } 2083bf5e552SAntti Palosaari 20909611caaSAntti Palosaari static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x) 2109a0bf528SMauro Carvalho Chehab { 2119a0bf528SMauro Carvalho Chehab u32 r = 0, c = 0, i; 2129a0bf528SMauro Carvalho Chehab 21309611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: a=%d b=%d x=%d\n", __func__, a, b, x); 2149a0bf528SMauro Carvalho Chehab 2159a0bf528SMauro Carvalho Chehab if (a > b) { 2169a0bf528SMauro Carvalho Chehab c = a / b; 2179a0bf528SMauro Carvalho Chehab a = a - c * b; 2189a0bf528SMauro Carvalho Chehab } 2199a0bf528SMauro Carvalho Chehab 2209a0bf528SMauro Carvalho Chehab for (i = 0; i < x; i++) { 2219a0bf528SMauro Carvalho Chehab if (a >= b) { 2229a0bf528SMauro Carvalho Chehab r += 1; 2239a0bf528SMauro Carvalho Chehab a -= b; 2249a0bf528SMauro Carvalho Chehab } 2259a0bf528SMauro Carvalho Chehab a <<= 1; 2269a0bf528SMauro Carvalho Chehab r <<= 1; 2279a0bf528SMauro Carvalho Chehab } 2289a0bf528SMauro Carvalho Chehab r = (c << (u32)x) + r; 2299a0bf528SMauro Carvalho Chehab 23009611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: a=%d b=%d x=%d r=%d r=%x\n", 2310a73f2d6SAntti Palosaari __func__, a, b, x, r, r); 2329a0bf528SMauro Carvalho Chehab 2339a0bf528SMauro Carvalho Chehab return r; 2349a0bf528SMauro Carvalho Chehab } 2359a0bf528SMauro Carvalho Chehab 2369a0bf528SMauro Carvalho Chehab static void af9033_release(struct dvb_frontend *fe) 2379a0bf528SMauro Carvalho Chehab { 23809611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 2399a0bf528SMauro Carvalho Chehab 24009611caaSAntti Palosaari kfree(dev); 2419a0bf528SMauro Carvalho Chehab } 2429a0bf528SMauro Carvalho Chehab 2439a0bf528SMauro Carvalho Chehab static int af9033_init(struct dvb_frontend *fe) 2449a0bf528SMauro Carvalho Chehab { 24509611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 2469a0bf528SMauro Carvalho Chehab int ret, i, len; 2479a0bf528SMauro Carvalho Chehab const struct reg_val *init; 2489a0bf528SMauro Carvalho Chehab u8 buf[4]; 2499a0bf528SMauro Carvalho Chehab u32 adc_cw, clock_cw; 2509a0bf528SMauro Carvalho Chehab struct reg_val_mask tab[] = { 2519a0bf528SMauro Carvalho Chehab { 0x80fb24, 0x00, 0x08 }, 2529a0bf528SMauro Carvalho Chehab { 0x80004c, 0x00, 0xff }, 25309611caaSAntti Palosaari { 0x00f641, dev->cfg.tuner, 0xff }, 2549a0bf528SMauro Carvalho Chehab { 0x80f5ca, 0x01, 0x01 }, 2559a0bf528SMauro Carvalho Chehab { 0x80f715, 0x01, 0x01 }, 2569a0bf528SMauro Carvalho Chehab { 0x00f41f, 0x04, 0x04 }, 2579a0bf528SMauro Carvalho Chehab { 0x00f41a, 0x01, 0x01 }, 2589a0bf528SMauro Carvalho Chehab { 0x80f731, 0x00, 0x01 }, 2599a0bf528SMauro Carvalho Chehab { 0x00d91e, 0x00, 0x01 }, 2609a0bf528SMauro Carvalho Chehab { 0x00d919, 0x00, 0x01 }, 2619a0bf528SMauro Carvalho Chehab { 0x80f732, 0x00, 0x01 }, 2629a0bf528SMauro Carvalho Chehab { 0x00d91f, 0x00, 0x01 }, 2639a0bf528SMauro Carvalho Chehab { 0x00d91a, 0x00, 0x01 }, 2649a0bf528SMauro Carvalho Chehab { 0x80f730, 0x00, 0x01 }, 2659a0bf528SMauro Carvalho Chehab { 0x80f778, 0x00, 0xff }, 2669a0bf528SMauro Carvalho Chehab { 0x80f73c, 0x01, 0x01 }, 2679a0bf528SMauro Carvalho Chehab { 0x80f776, 0x00, 0x01 }, 2689a0bf528SMauro Carvalho Chehab { 0x00d8fd, 0x01, 0xff }, 2699a0bf528SMauro Carvalho Chehab { 0x00d830, 0x01, 0xff }, 2709a0bf528SMauro Carvalho Chehab { 0x00d831, 0x00, 0xff }, 2719a0bf528SMauro Carvalho Chehab { 0x00d832, 0x00, 0xff }, 27209611caaSAntti Palosaari { 0x80f985, dev->ts_mode_serial, 0x01 }, 27309611caaSAntti Palosaari { 0x80f986, dev->ts_mode_parallel, 0x01 }, 2749a0bf528SMauro Carvalho Chehab { 0x00d827, 0x00, 0xff }, 2759a0bf528SMauro Carvalho Chehab { 0x00d829, 0x00, 0xff }, 27609611caaSAntti Palosaari { 0x800045, dev->cfg.adc_multiplier, 0xff }, 2779a0bf528SMauro Carvalho Chehab }; 2789a0bf528SMauro Carvalho Chehab 2799a0bf528SMauro Carvalho Chehab /* program clock control */ 28009611caaSAntti Palosaari clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul); 2819a0bf528SMauro Carvalho Chehab buf[0] = (clock_cw >> 0) & 0xff; 2829a0bf528SMauro Carvalho Chehab buf[1] = (clock_cw >> 8) & 0xff; 2839a0bf528SMauro Carvalho Chehab buf[2] = (clock_cw >> 16) & 0xff; 2849a0bf528SMauro Carvalho Chehab buf[3] = (clock_cw >> 24) & 0xff; 2859a0bf528SMauro Carvalho Chehab 28609611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: clock=%d clock_cw=%08x\n", 28709611caaSAntti Palosaari __func__, dev->cfg.clock, clock_cw); 2889a0bf528SMauro Carvalho Chehab 28909611caaSAntti Palosaari ret = af9033_wr_regs(dev, 0x800025, buf, 4); 2909a0bf528SMauro Carvalho Chehab if (ret < 0) 2919a0bf528SMauro Carvalho Chehab goto err; 2929a0bf528SMauro Carvalho Chehab 2939a0bf528SMauro Carvalho Chehab /* program ADC control */ 2949a0bf528SMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { 29509611caaSAntti Palosaari if (clock_adc_lut[i].clock == dev->cfg.clock) 2969a0bf528SMauro Carvalho Chehab break; 2979a0bf528SMauro Carvalho Chehab } 2989a0bf528SMauro Carvalho Chehab 29909611caaSAntti Palosaari adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul); 3009a0bf528SMauro Carvalho Chehab buf[0] = (adc_cw >> 0) & 0xff; 3019a0bf528SMauro Carvalho Chehab buf[1] = (adc_cw >> 8) & 0xff; 3029a0bf528SMauro Carvalho Chehab buf[2] = (adc_cw >> 16) & 0xff; 3039a0bf528SMauro Carvalho Chehab 30409611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: adc=%d adc_cw=%06x\n", 3050a73f2d6SAntti Palosaari __func__, clock_adc_lut[i].adc, adc_cw); 3069a0bf528SMauro Carvalho Chehab 30709611caaSAntti Palosaari ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3); 3089a0bf528SMauro Carvalho Chehab if (ret < 0) 3099a0bf528SMauro Carvalho Chehab goto err; 3109a0bf528SMauro Carvalho Chehab 3119a0bf528SMauro Carvalho Chehab /* program register table */ 3129a0bf528SMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(tab); i++) { 31309611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val, 3149a0bf528SMauro Carvalho Chehab tab[i].mask); 3159a0bf528SMauro Carvalho Chehab if (ret < 0) 3169a0bf528SMauro Carvalho Chehab goto err; 3179a0bf528SMauro Carvalho Chehab } 3189a0bf528SMauro Carvalho Chehab 319ca681fe0SAntti Palosaari /* clock output */ 32009611caaSAntti Palosaari if (dev->cfg.dyn0_clk) { 32109611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x80fba8, 0x00); 3229dc0f3feSAntti Palosaari if (ret < 0) 3239dc0f3feSAntti Palosaari goto err; 3249dc0f3feSAntti Palosaari } 3259dc0f3feSAntti Palosaari 3269a0bf528SMauro Carvalho Chehab /* settings for TS interface */ 32709611caaSAntti Palosaari if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) { 32809611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01); 3299a0bf528SMauro Carvalho Chehab if (ret < 0) 3309a0bf528SMauro Carvalho Chehab goto err; 3319a0bf528SMauro Carvalho Chehab 33209611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01); 3339a0bf528SMauro Carvalho Chehab if (ret < 0) 3349a0bf528SMauro Carvalho Chehab goto err; 3359a0bf528SMauro Carvalho Chehab } else { 33609611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01); 3379a0bf528SMauro Carvalho Chehab if (ret < 0) 3389a0bf528SMauro Carvalho Chehab goto err; 3399a0bf528SMauro Carvalho Chehab 34009611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01); 3419a0bf528SMauro Carvalho Chehab if (ret < 0) 3429a0bf528SMauro Carvalho Chehab goto err; 3439a0bf528SMauro Carvalho Chehab } 3449a0bf528SMauro Carvalho Chehab 3459a0bf528SMauro Carvalho Chehab /* load OFSM settings */ 34609611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: load ofsm settings\n", __func__); 34709611caaSAntti Palosaari switch (dev->cfg.tuner) { 348fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_38: 349fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_51: 350fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_52: 351463c399cSAntti Palosaari len = ARRAY_SIZE(ofsm_init_it9135_v1); 352463c399cSAntti Palosaari init = ofsm_init_it9135_v1; 353463c399cSAntti Palosaari break; 354fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_60: 355fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_61: 356fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_62: 357463c399cSAntti Palosaari len = ARRAY_SIZE(ofsm_init_it9135_v2); 358463c399cSAntti Palosaari init = ofsm_init_it9135_v2; 359fe8eece1SAntti Palosaari break; 360fe8eece1SAntti Palosaari default: 3619a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(ofsm_init); 3629a0bf528SMauro Carvalho Chehab init = ofsm_init; 363fe8eece1SAntti Palosaari break; 364fe8eece1SAntti Palosaari } 365fe8eece1SAntti Palosaari 36609611caaSAntti Palosaari ret = af9033_wr_reg_val_tab(dev, init, len); 3679a0bf528SMauro Carvalho Chehab if (ret < 0) 3689a0bf528SMauro Carvalho Chehab goto err; 3699a0bf528SMauro Carvalho Chehab 3709a0bf528SMauro Carvalho Chehab /* load tuner specific settings */ 37109611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: load tuner specific settings\n", 3729a0bf528SMauro Carvalho Chehab __func__); 37309611caaSAntti Palosaari switch (dev->cfg.tuner) { 3749a0bf528SMauro Carvalho Chehab case AF9033_TUNER_TUA9001: 3759a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(tuner_init_tua9001); 3769a0bf528SMauro Carvalho Chehab init = tuner_init_tua9001; 3779a0bf528SMauro Carvalho Chehab break; 3789a0bf528SMauro Carvalho Chehab case AF9033_TUNER_FC0011: 3799a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(tuner_init_fc0011); 3809a0bf528SMauro Carvalho Chehab init = tuner_init_fc0011; 3819a0bf528SMauro Carvalho Chehab break; 3829a0bf528SMauro Carvalho Chehab case AF9033_TUNER_MXL5007T: 3839a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(tuner_init_mxl5007t); 3849a0bf528SMauro Carvalho Chehab init = tuner_init_mxl5007t; 3859a0bf528SMauro Carvalho Chehab break; 3869a0bf528SMauro Carvalho Chehab case AF9033_TUNER_TDA18218: 3879a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(tuner_init_tda18218); 3889a0bf528SMauro Carvalho Chehab init = tuner_init_tda18218; 3899a0bf528SMauro Carvalho Chehab break; 390d67ceb33SOliver Schinagl case AF9033_TUNER_FC2580: 391d67ceb33SOliver Schinagl len = ARRAY_SIZE(tuner_init_fc2580); 392d67ceb33SOliver Schinagl init = tuner_init_fc2580; 393d67ceb33SOliver Schinagl break; 394e713ad15SAntti Palosaari case AF9033_TUNER_FC0012: 395e713ad15SAntti Palosaari len = ARRAY_SIZE(tuner_init_fc0012); 396e713ad15SAntti Palosaari init = tuner_init_fc0012; 397e713ad15SAntti Palosaari break; 3984902bb39SAntti Palosaari case AF9033_TUNER_IT9135_38: 399a72cbb77SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_38); 400a72cbb77SAntti Palosaari init = tuner_init_it9135_38; 401a72cbb77SAntti Palosaari break; 4024902bb39SAntti Palosaari case AF9033_TUNER_IT9135_51: 403bb2e12a6SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_51); 404bb2e12a6SAntti Palosaari init = tuner_init_it9135_51; 405bb2e12a6SAntti Palosaari break; 4064902bb39SAntti Palosaari case AF9033_TUNER_IT9135_52: 40722d729f3SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_52); 40822d729f3SAntti Palosaari init = tuner_init_it9135_52; 40922d729f3SAntti Palosaari break; 4104902bb39SAntti Palosaari case AF9033_TUNER_IT9135_60: 411a49f53a0SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_60); 412a49f53a0SAntti Palosaari init = tuner_init_it9135_60; 413a49f53a0SAntti Palosaari break; 4144902bb39SAntti Palosaari case AF9033_TUNER_IT9135_61: 41585211323SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_61); 41685211323SAntti Palosaari init = tuner_init_it9135_61; 41785211323SAntti Palosaari break; 4184902bb39SAntti Palosaari case AF9033_TUNER_IT9135_62: 419dc4a2c40SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_62); 420dc4a2c40SAntti Palosaari init = tuner_init_it9135_62; 4214902bb39SAntti Palosaari break; 4229a0bf528SMauro Carvalho Chehab default: 42309611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: unsupported tuner ID=%d\n", 42409611caaSAntti Palosaari __func__, dev->cfg.tuner); 4259a0bf528SMauro Carvalho Chehab ret = -ENODEV; 4269a0bf528SMauro Carvalho Chehab goto err; 4279a0bf528SMauro Carvalho Chehab } 4289a0bf528SMauro Carvalho Chehab 42909611caaSAntti Palosaari ret = af9033_wr_reg_val_tab(dev, init, len); 4309a0bf528SMauro Carvalho Chehab if (ret < 0) 4319a0bf528SMauro Carvalho Chehab goto err; 4329a0bf528SMauro Carvalho Chehab 43309611caaSAntti Palosaari if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { 43409611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01); 4359805992fSJose Alberto Reguero if (ret < 0) 4369805992fSJose Alberto Reguero goto err; 437bf97b637SAntti Palosaari 43809611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01); 4399805992fSJose Alberto Reguero if (ret < 0) 4409805992fSJose Alberto Reguero goto err; 441bf97b637SAntti Palosaari 44209611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01); 4439805992fSJose Alberto Reguero if (ret < 0) 4449805992fSJose Alberto Reguero goto err; 4459805992fSJose Alberto Reguero } 4469805992fSJose Alberto Reguero 44709611caaSAntti Palosaari switch (dev->cfg.tuner) { 448086991ddSAntti Palosaari case AF9033_TUNER_IT9135_60: 449086991ddSAntti Palosaari case AF9033_TUNER_IT9135_61: 450086991ddSAntti Palosaari case AF9033_TUNER_IT9135_62: 45109611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x800000, 0x01); 452086991ddSAntti Palosaari if (ret < 0) 453086991ddSAntti Palosaari goto err; 454086991ddSAntti Palosaari } 455086991ddSAntti Palosaari 45609611caaSAntti Palosaari dev->bandwidth_hz = 0; /* force to program all parameters */ 4579a0bf528SMauro Carvalho Chehab 4589a0bf528SMauro Carvalho Chehab return 0; 4599a0bf528SMauro Carvalho Chehab 4609a0bf528SMauro Carvalho Chehab err: 46109611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 4629a0bf528SMauro Carvalho Chehab 4639a0bf528SMauro Carvalho Chehab return ret; 4649a0bf528SMauro Carvalho Chehab } 4659a0bf528SMauro Carvalho Chehab 4669a0bf528SMauro Carvalho Chehab static int af9033_sleep(struct dvb_frontend *fe) 4679a0bf528SMauro Carvalho Chehab { 46809611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 4699a0bf528SMauro Carvalho Chehab int ret, i; 4709a0bf528SMauro Carvalho Chehab u8 tmp; 4719a0bf528SMauro Carvalho Chehab 47209611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x80004c, 1); 4739a0bf528SMauro Carvalho Chehab if (ret < 0) 4749a0bf528SMauro Carvalho Chehab goto err; 4759a0bf528SMauro Carvalho Chehab 47609611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x800000, 0); 4779a0bf528SMauro Carvalho Chehab if (ret < 0) 4789a0bf528SMauro Carvalho Chehab goto err; 4799a0bf528SMauro Carvalho Chehab 4809a0bf528SMauro Carvalho Chehab for (i = 100, tmp = 1; i && tmp; i--) { 48109611caaSAntti Palosaari ret = af9033_rd_reg(dev, 0x80004c, &tmp); 4829a0bf528SMauro Carvalho Chehab if (ret < 0) 4839a0bf528SMauro Carvalho Chehab goto err; 4849a0bf528SMauro Carvalho Chehab 4859a0bf528SMauro Carvalho Chehab usleep_range(200, 10000); 4869a0bf528SMauro Carvalho Chehab } 4879a0bf528SMauro Carvalho Chehab 48809611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: loop=%d\n", __func__, i); 4899a0bf528SMauro Carvalho Chehab 4909a0bf528SMauro Carvalho Chehab if (i == 0) { 4919a0bf528SMauro Carvalho Chehab ret = -ETIMEDOUT; 4929a0bf528SMauro Carvalho Chehab goto err; 4939a0bf528SMauro Carvalho Chehab } 4949a0bf528SMauro Carvalho Chehab 49509611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08); 4969a0bf528SMauro Carvalho Chehab if (ret < 0) 4979a0bf528SMauro Carvalho Chehab goto err; 4989a0bf528SMauro Carvalho Chehab 4999a0bf528SMauro Carvalho Chehab /* prevent current leak (?) */ 50009611caaSAntti Palosaari if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { 5019a0bf528SMauro Carvalho Chehab /* enable parallel TS */ 50209611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01); 5039a0bf528SMauro Carvalho Chehab if (ret < 0) 5049a0bf528SMauro Carvalho Chehab goto err; 5059a0bf528SMauro Carvalho Chehab 50609611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01); 5079a0bf528SMauro Carvalho Chehab if (ret < 0) 5089a0bf528SMauro Carvalho Chehab goto err; 5099a0bf528SMauro Carvalho Chehab } 5109a0bf528SMauro Carvalho Chehab 5119a0bf528SMauro Carvalho Chehab return 0; 5129a0bf528SMauro Carvalho Chehab 5139a0bf528SMauro Carvalho Chehab err: 51409611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 5159a0bf528SMauro Carvalho Chehab 5169a0bf528SMauro Carvalho Chehab return ret; 5179a0bf528SMauro Carvalho Chehab } 5189a0bf528SMauro Carvalho Chehab 5199a0bf528SMauro Carvalho Chehab static int af9033_get_tune_settings(struct dvb_frontend *fe, 5209a0bf528SMauro Carvalho Chehab struct dvb_frontend_tune_settings *fesettings) 5219a0bf528SMauro Carvalho Chehab { 522fe8eece1SAntti Palosaari /* 800 => 2000 because IT9135 v2 is slow to gain lock */ 523fe8eece1SAntti Palosaari fesettings->min_delay_ms = 2000; 5249a0bf528SMauro Carvalho Chehab fesettings->step_size = 0; 5259a0bf528SMauro Carvalho Chehab fesettings->max_drift = 0; 5269a0bf528SMauro Carvalho Chehab 5279a0bf528SMauro Carvalho Chehab return 0; 5289a0bf528SMauro Carvalho Chehab } 5299a0bf528SMauro Carvalho Chehab 5309a0bf528SMauro Carvalho Chehab static int af9033_set_frontend(struct dvb_frontend *fe) 5319a0bf528SMauro Carvalho Chehab { 53209611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 5339a0bf528SMauro Carvalho Chehab struct dtv_frontend_properties *c = &fe->dtv_property_cache; 534182b967eSHans-Frieder Vogt int ret, i, spec_inv, sampling_freq; 5359a0bf528SMauro Carvalho Chehab u8 tmp, buf[3], bandwidth_reg_val; 5369a0bf528SMauro Carvalho Chehab u32 if_frequency, freq_cw, adc_freq; 5379a0bf528SMauro Carvalho Chehab 53809611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n", 5390a73f2d6SAntti Palosaari __func__, c->frequency, c->bandwidth_hz); 5409a0bf528SMauro Carvalho Chehab 5419a0bf528SMauro Carvalho Chehab /* check bandwidth */ 5429a0bf528SMauro Carvalho Chehab switch (c->bandwidth_hz) { 5439a0bf528SMauro Carvalho Chehab case 6000000: 5449a0bf528SMauro Carvalho Chehab bandwidth_reg_val = 0x00; 5459a0bf528SMauro Carvalho Chehab break; 5469a0bf528SMauro Carvalho Chehab case 7000000: 5479a0bf528SMauro Carvalho Chehab bandwidth_reg_val = 0x01; 5489a0bf528SMauro Carvalho Chehab break; 5499a0bf528SMauro Carvalho Chehab case 8000000: 5509a0bf528SMauro Carvalho Chehab bandwidth_reg_val = 0x02; 5519a0bf528SMauro Carvalho Chehab break; 5529a0bf528SMauro Carvalho Chehab default: 55309611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: invalid bandwidth_hz\n", 5540a73f2d6SAntti Palosaari __func__); 5559a0bf528SMauro Carvalho Chehab ret = -EINVAL; 5569a0bf528SMauro Carvalho Chehab goto err; 5579a0bf528SMauro Carvalho Chehab } 5589a0bf528SMauro Carvalho Chehab 5599a0bf528SMauro Carvalho Chehab /* program tuner */ 5609a0bf528SMauro Carvalho Chehab if (fe->ops.tuner_ops.set_params) 5619a0bf528SMauro Carvalho Chehab fe->ops.tuner_ops.set_params(fe); 5629a0bf528SMauro Carvalho Chehab 5639a0bf528SMauro Carvalho Chehab /* program CFOE coefficients */ 56409611caaSAntti Palosaari if (c->bandwidth_hz != dev->bandwidth_hz) { 5659a0bf528SMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) { 56609611caaSAntti Palosaari if (coeff_lut[i].clock == dev->cfg.clock && 5679a0bf528SMauro Carvalho Chehab coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { 5689a0bf528SMauro Carvalho Chehab break; 5699a0bf528SMauro Carvalho Chehab } 5709a0bf528SMauro Carvalho Chehab } 57109611caaSAntti Palosaari ret = af9033_wr_regs(dev, 0x800001, 5729a0bf528SMauro Carvalho Chehab coeff_lut[i].val, sizeof(coeff_lut[i].val)); 5739a0bf528SMauro Carvalho Chehab } 5749a0bf528SMauro Carvalho Chehab 5759a0bf528SMauro Carvalho Chehab /* program frequency control */ 57609611caaSAntti Palosaari if (c->bandwidth_hz != dev->bandwidth_hz) { 57709611caaSAntti Palosaari spec_inv = dev->cfg.spec_inv ? -1 : 1; 5789a0bf528SMauro Carvalho Chehab 5799a0bf528SMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { 58009611caaSAntti Palosaari if (clock_adc_lut[i].clock == dev->cfg.clock) 5819a0bf528SMauro Carvalho Chehab break; 5829a0bf528SMauro Carvalho Chehab } 5839a0bf528SMauro Carvalho Chehab adc_freq = clock_adc_lut[i].adc; 5849a0bf528SMauro Carvalho Chehab 5859a0bf528SMauro Carvalho Chehab /* get used IF frequency */ 5869a0bf528SMauro Carvalho Chehab if (fe->ops.tuner_ops.get_if_frequency) 5879a0bf528SMauro Carvalho Chehab fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); 5889a0bf528SMauro Carvalho Chehab else 5899a0bf528SMauro Carvalho Chehab if_frequency = 0; 5909a0bf528SMauro Carvalho Chehab 591182b967eSHans-Frieder Vogt sampling_freq = if_frequency; 5929a0bf528SMauro Carvalho Chehab 593182b967eSHans-Frieder Vogt while (sampling_freq > (adc_freq / 2)) 594182b967eSHans-Frieder Vogt sampling_freq -= adc_freq; 595182b967eSHans-Frieder Vogt 596182b967eSHans-Frieder Vogt if (sampling_freq >= 0) 5979a0bf528SMauro Carvalho Chehab spec_inv *= -1; 5989a0bf528SMauro Carvalho Chehab else 599182b967eSHans-Frieder Vogt sampling_freq *= -1; 6009a0bf528SMauro Carvalho Chehab 60109611caaSAntti Palosaari freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul); 6029a0bf528SMauro Carvalho Chehab 6039a0bf528SMauro Carvalho Chehab if (spec_inv == -1) 604182b967eSHans-Frieder Vogt freq_cw = 0x800000 - freq_cw; 6059a0bf528SMauro Carvalho Chehab 60609611caaSAntti Palosaari if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X) 6079a0bf528SMauro Carvalho Chehab freq_cw /= 2; 6089a0bf528SMauro Carvalho Chehab 6099a0bf528SMauro Carvalho Chehab buf[0] = (freq_cw >> 0) & 0xff; 6109a0bf528SMauro Carvalho Chehab buf[1] = (freq_cw >> 8) & 0xff; 6119a0bf528SMauro Carvalho Chehab buf[2] = (freq_cw >> 16) & 0x7f; 612fe8eece1SAntti Palosaari 613fe8eece1SAntti Palosaari /* FIXME: there seems to be calculation error here... */ 614fe8eece1SAntti Palosaari if (if_frequency == 0) 615fe8eece1SAntti Palosaari buf[2] = 0; 616fe8eece1SAntti Palosaari 61709611caaSAntti Palosaari ret = af9033_wr_regs(dev, 0x800029, buf, 3); 6189a0bf528SMauro Carvalho Chehab if (ret < 0) 6199a0bf528SMauro Carvalho Chehab goto err; 6209a0bf528SMauro Carvalho Chehab 62109611caaSAntti Palosaari dev->bandwidth_hz = c->bandwidth_hz; 6229a0bf528SMauro Carvalho Chehab } 6239a0bf528SMauro Carvalho Chehab 62409611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03); 6259a0bf528SMauro Carvalho Chehab if (ret < 0) 6269a0bf528SMauro Carvalho Chehab goto err; 6279a0bf528SMauro Carvalho Chehab 62809611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x800040, 0x00); 6299a0bf528SMauro Carvalho Chehab if (ret < 0) 6309a0bf528SMauro Carvalho Chehab goto err; 6319a0bf528SMauro Carvalho Chehab 63209611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x800047, 0x00); 6339a0bf528SMauro Carvalho Chehab if (ret < 0) 6349a0bf528SMauro Carvalho Chehab goto err; 6359a0bf528SMauro Carvalho Chehab 63609611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f999, 0x00, 0x01); 6379a0bf528SMauro Carvalho Chehab if (ret < 0) 6389a0bf528SMauro Carvalho Chehab goto err; 6399a0bf528SMauro Carvalho Chehab 6409a0bf528SMauro Carvalho Chehab if (c->frequency <= 230000000) 6419a0bf528SMauro Carvalho Chehab tmp = 0x00; /* VHF */ 6429a0bf528SMauro Carvalho Chehab else 6439a0bf528SMauro Carvalho Chehab tmp = 0x01; /* UHF */ 6449a0bf528SMauro Carvalho Chehab 64509611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x80004b, tmp); 6469a0bf528SMauro Carvalho Chehab if (ret < 0) 6479a0bf528SMauro Carvalho Chehab goto err; 6489a0bf528SMauro Carvalho Chehab 64909611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x800000, 0x00); 6509a0bf528SMauro Carvalho Chehab if (ret < 0) 6519a0bf528SMauro Carvalho Chehab goto err; 6529a0bf528SMauro Carvalho Chehab 6539a0bf528SMauro Carvalho Chehab return 0; 6549a0bf528SMauro Carvalho Chehab 6559a0bf528SMauro Carvalho Chehab err: 65609611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 6579a0bf528SMauro Carvalho Chehab 6589a0bf528SMauro Carvalho Chehab return ret; 6599a0bf528SMauro Carvalho Chehab } 6609a0bf528SMauro Carvalho Chehab 6619a0bf528SMauro Carvalho Chehab static int af9033_get_frontend(struct dvb_frontend *fe) 6629a0bf528SMauro Carvalho Chehab { 66309611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 6649a0bf528SMauro Carvalho Chehab struct dtv_frontend_properties *c = &fe->dtv_property_cache; 6659a0bf528SMauro Carvalho Chehab int ret; 6669a0bf528SMauro Carvalho Chehab u8 buf[8]; 6679a0bf528SMauro Carvalho Chehab 66809611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s:\n", __func__); 6699a0bf528SMauro Carvalho Chehab 6709a0bf528SMauro Carvalho Chehab /* read all needed registers */ 67109611caaSAntti Palosaari ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf)); 6729a0bf528SMauro Carvalho Chehab if (ret < 0) 6739a0bf528SMauro Carvalho Chehab goto err; 6749a0bf528SMauro Carvalho Chehab 6759a0bf528SMauro Carvalho Chehab switch ((buf[0] >> 0) & 3) { 6769a0bf528SMauro Carvalho Chehab case 0: 6779a0bf528SMauro Carvalho Chehab c->transmission_mode = TRANSMISSION_MODE_2K; 6789a0bf528SMauro Carvalho Chehab break; 6799a0bf528SMauro Carvalho Chehab case 1: 6809a0bf528SMauro Carvalho Chehab c->transmission_mode = TRANSMISSION_MODE_8K; 6819a0bf528SMauro Carvalho Chehab break; 6829a0bf528SMauro Carvalho Chehab } 6839a0bf528SMauro Carvalho Chehab 6849a0bf528SMauro Carvalho Chehab switch ((buf[1] >> 0) & 3) { 6859a0bf528SMauro Carvalho Chehab case 0: 6869a0bf528SMauro Carvalho Chehab c->guard_interval = GUARD_INTERVAL_1_32; 6879a0bf528SMauro Carvalho Chehab break; 6889a0bf528SMauro Carvalho Chehab case 1: 6899a0bf528SMauro Carvalho Chehab c->guard_interval = GUARD_INTERVAL_1_16; 6909a0bf528SMauro Carvalho Chehab break; 6919a0bf528SMauro Carvalho Chehab case 2: 6929a0bf528SMauro Carvalho Chehab c->guard_interval = GUARD_INTERVAL_1_8; 6939a0bf528SMauro Carvalho Chehab break; 6949a0bf528SMauro Carvalho Chehab case 3: 6959a0bf528SMauro Carvalho Chehab c->guard_interval = GUARD_INTERVAL_1_4; 6969a0bf528SMauro Carvalho Chehab break; 6979a0bf528SMauro Carvalho Chehab } 6989a0bf528SMauro Carvalho Chehab 6999a0bf528SMauro Carvalho Chehab switch ((buf[2] >> 0) & 7) { 7009a0bf528SMauro Carvalho Chehab case 0: 7019a0bf528SMauro Carvalho Chehab c->hierarchy = HIERARCHY_NONE; 7029a0bf528SMauro Carvalho Chehab break; 7039a0bf528SMauro Carvalho Chehab case 1: 7049a0bf528SMauro Carvalho Chehab c->hierarchy = HIERARCHY_1; 7059a0bf528SMauro Carvalho Chehab break; 7069a0bf528SMauro Carvalho Chehab case 2: 7079a0bf528SMauro Carvalho Chehab c->hierarchy = HIERARCHY_2; 7089a0bf528SMauro Carvalho Chehab break; 7099a0bf528SMauro Carvalho Chehab case 3: 7109a0bf528SMauro Carvalho Chehab c->hierarchy = HIERARCHY_4; 7119a0bf528SMauro Carvalho Chehab break; 7129a0bf528SMauro Carvalho Chehab } 7139a0bf528SMauro Carvalho Chehab 7149a0bf528SMauro Carvalho Chehab switch ((buf[3] >> 0) & 3) { 7159a0bf528SMauro Carvalho Chehab case 0: 7169a0bf528SMauro Carvalho Chehab c->modulation = QPSK; 7179a0bf528SMauro Carvalho Chehab break; 7189a0bf528SMauro Carvalho Chehab case 1: 7199a0bf528SMauro Carvalho Chehab c->modulation = QAM_16; 7209a0bf528SMauro Carvalho Chehab break; 7219a0bf528SMauro Carvalho Chehab case 2: 7229a0bf528SMauro Carvalho Chehab c->modulation = QAM_64; 7239a0bf528SMauro Carvalho Chehab break; 7249a0bf528SMauro Carvalho Chehab } 7259a0bf528SMauro Carvalho Chehab 7269a0bf528SMauro Carvalho Chehab switch ((buf[4] >> 0) & 3) { 7279a0bf528SMauro Carvalho Chehab case 0: 7289a0bf528SMauro Carvalho Chehab c->bandwidth_hz = 6000000; 7299a0bf528SMauro Carvalho Chehab break; 7309a0bf528SMauro Carvalho Chehab case 1: 7319a0bf528SMauro Carvalho Chehab c->bandwidth_hz = 7000000; 7329a0bf528SMauro Carvalho Chehab break; 7339a0bf528SMauro Carvalho Chehab case 2: 7349a0bf528SMauro Carvalho Chehab c->bandwidth_hz = 8000000; 7359a0bf528SMauro Carvalho Chehab break; 7369a0bf528SMauro Carvalho Chehab } 7379a0bf528SMauro Carvalho Chehab 7389a0bf528SMauro Carvalho Chehab switch ((buf[6] >> 0) & 7) { 7399a0bf528SMauro Carvalho Chehab case 0: 7409a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_1_2; 7419a0bf528SMauro Carvalho Chehab break; 7429a0bf528SMauro Carvalho Chehab case 1: 7439a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_2_3; 7449a0bf528SMauro Carvalho Chehab break; 7459a0bf528SMauro Carvalho Chehab case 2: 7469a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_3_4; 7479a0bf528SMauro Carvalho Chehab break; 7489a0bf528SMauro Carvalho Chehab case 3: 7499a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_5_6; 7509a0bf528SMauro Carvalho Chehab break; 7519a0bf528SMauro Carvalho Chehab case 4: 7529a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_7_8; 7539a0bf528SMauro Carvalho Chehab break; 7549a0bf528SMauro Carvalho Chehab case 5: 7559a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_NONE; 7569a0bf528SMauro Carvalho Chehab break; 7579a0bf528SMauro Carvalho Chehab } 7589a0bf528SMauro Carvalho Chehab 7599a0bf528SMauro Carvalho Chehab switch ((buf[7] >> 0) & 7) { 7609a0bf528SMauro Carvalho Chehab case 0: 7619a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_1_2; 7629a0bf528SMauro Carvalho Chehab break; 7639a0bf528SMauro Carvalho Chehab case 1: 7649a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_2_3; 7659a0bf528SMauro Carvalho Chehab break; 7669a0bf528SMauro Carvalho Chehab case 2: 7679a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_3_4; 7689a0bf528SMauro Carvalho Chehab break; 7699a0bf528SMauro Carvalho Chehab case 3: 7709a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_5_6; 7719a0bf528SMauro Carvalho Chehab break; 7729a0bf528SMauro Carvalho Chehab case 4: 7739a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_7_8; 7749a0bf528SMauro Carvalho Chehab break; 7759a0bf528SMauro Carvalho Chehab case 5: 7769a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_NONE; 7779a0bf528SMauro Carvalho Chehab break; 7789a0bf528SMauro Carvalho Chehab } 7799a0bf528SMauro Carvalho Chehab 7809a0bf528SMauro Carvalho Chehab return 0; 7819a0bf528SMauro Carvalho Chehab 7829a0bf528SMauro Carvalho Chehab err: 78309611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 7849a0bf528SMauro Carvalho Chehab 7859a0bf528SMauro Carvalho Chehab return ret; 7869a0bf528SMauro Carvalho Chehab } 7879a0bf528SMauro Carvalho Chehab 7889a0bf528SMauro Carvalho Chehab static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status) 7899a0bf528SMauro Carvalho Chehab { 79009611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 7919a0bf528SMauro Carvalho Chehab int ret; 7929a0bf528SMauro Carvalho Chehab u8 tmp; 7939a0bf528SMauro Carvalho Chehab 7949a0bf528SMauro Carvalho Chehab *status = 0; 7959a0bf528SMauro Carvalho Chehab 7969a0bf528SMauro Carvalho Chehab /* radio channel status, 0=no result, 1=has signal, 2=no signal */ 79709611caaSAntti Palosaari ret = af9033_rd_reg(dev, 0x800047, &tmp); 7989a0bf528SMauro Carvalho Chehab if (ret < 0) 7999a0bf528SMauro Carvalho Chehab goto err; 8009a0bf528SMauro Carvalho Chehab 8019a0bf528SMauro Carvalho Chehab /* has signal */ 8029a0bf528SMauro Carvalho Chehab if (tmp == 0x01) 8039a0bf528SMauro Carvalho Chehab *status |= FE_HAS_SIGNAL; 8049a0bf528SMauro Carvalho Chehab 8059a0bf528SMauro Carvalho Chehab if (tmp != 0x02) { 8069a0bf528SMauro Carvalho Chehab /* TPS lock */ 80709611caaSAntti Palosaari ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01); 8089a0bf528SMauro Carvalho Chehab if (ret < 0) 8099a0bf528SMauro Carvalho Chehab goto err; 8109a0bf528SMauro Carvalho Chehab 8119a0bf528SMauro Carvalho Chehab if (tmp) 8129a0bf528SMauro Carvalho Chehab *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | 8139a0bf528SMauro Carvalho Chehab FE_HAS_VITERBI; 8149a0bf528SMauro Carvalho Chehab 8159a0bf528SMauro Carvalho Chehab /* full lock */ 81609611caaSAntti Palosaari ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01); 8179a0bf528SMauro Carvalho Chehab if (ret < 0) 8189a0bf528SMauro Carvalho Chehab goto err; 8199a0bf528SMauro Carvalho Chehab 8209a0bf528SMauro Carvalho Chehab if (tmp) 8219a0bf528SMauro Carvalho Chehab *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | 8229a0bf528SMauro Carvalho Chehab FE_HAS_VITERBI | FE_HAS_SYNC | 8239a0bf528SMauro Carvalho Chehab FE_HAS_LOCK; 8249a0bf528SMauro Carvalho Chehab } 8259a0bf528SMauro Carvalho Chehab 8269a0bf528SMauro Carvalho Chehab return 0; 8279a0bf528SMauro Carvalho Chehab 8289a0bf528SMauro Carvalho Chehab err: 82909611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 8309a0bf528SMauro Carvalho Chehab 8319a0bf528SMauro Carvalho Chehab return ret; 8329a0bf528SMauro Carvalho Chehab } 8339a0bf528SMauro Carvalho Chehab 8349a0bf528SMauro Carvalho Chehab static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr) 8359a0bf528SMauro Carvalho Chehab { 83609611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 8379a0bf528SMauro Carvalho Chehab int ret, i, len; 8389a0bf528SMauro Carvalho Chehab u8 buf[3], tmp; 8399a0bf528SMauro Carvalho Chehab u32 snr_val; 84024e419a0SAntti Palosaari const struct val_snr *snr_lut; 8419a0bf528SMauro Carvalho Chehab 8429a0bf528SMauro Carvalho Chehab /* read value */ 84309611caaSAntti Palosaari ret = af9033_rd_regs(dev, 0x80002c, buf, 3); 8449a0bf528SMauro Carvalho Chehab if (ret < 0) 8459a0bf528SMauro Carvalho Chehab goto err; 8469a0bf528SMauro Carvalho Chehab 8479a0bf528SMauro Carvalho Chehab snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0]; 8489a0bf528SMauro Carvalho Chehab 8499a0bf528SMauro Carvalho Chehab /* read current modulation */ 85009611caaSAntti Palosaari ret = af9033_rd_reg(dev, 0x80f903, &tmp); 8519a0bf528SMauro Carvalho Chehab if (ret < 0) 8529a0bf528SMauro Carvalho Chehab goto err; 8539a0bf528SMauro Carvalho Chehab 8549a0bf528SMauro Carvalho Chehab switch ((tmp >> 0) & 3) { 8559a0bf528SMauro Carvalho Chehab case 0: 8569a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(qpsk_snr_lut); 8579a0bf528SMauro Carvalho Chehab snr_lut = qpsk_snr_lut; 8589a0bf528SMauro Carvalho Chehab break; 8599a0bf528SMauro Carvalho Chehab case 1: 8609a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(qam16_snr_lut); 8619a0bf528SMauro Carvalho Chehab snr_lut = qam16_snr_lut; 8629a0bf528SMauro Carvalho Chehab break; 8639a0bf528SMauro Carvalho Chehab case 2: 8649a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(qam64_snr_lut); 8659a0bf528SMauro Carvalho Chehab snr_lut = qam64_snr_lut; 8669a0bf528SMauro Carvalho Chehab break; 8679a0bf528SMauro Carvalho Chehab default: 8689a0bf528SMauro Carvalho Chehab goto err; 8699a0bf528SMauro Carvalho Chehab } 8709a0bf528SMauro Carvalho Chehab 8719a0bf528SMauro Carvalho Chehab for (i = 0; i < len; i++) { 8729a0bf528SMauro Carvalho Chehab tmp = snr_lut[i].snr; 8739a0bf528SMauro Carvalho Chehab 8749a0bf528SMauro Carvalho Chehab if (snr_val < snr_lut[i].val) 8759a0bf528SMauro Carvalho Chehab break; 8769a0bf528SMauro Carvalho Chehab } 8779a0bf528SMauro Carvalho Chehab 8789a0bf528SMauro Carvalho Chehab *snr = tmp * 10; /* dB/10 */ 8799a0bf528SMauro Carvalho Chehab 8809a0bf528SMauro Carvalho Chehab return 0; 8819a0bf528SMauro Carvalho Chehab 8829a0bf528SMauro Carvalho Chehab err: 88309611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 8849a0bf528SMauro Carvalho Chehab 8859a0bf528SMauro Carvalho Chehab return ret; 8869a0bf528SMauro Carvalho Chehab } 8879a0bf528SMauro Carvalho Chehab 8889a0bf528SMauro Carvalho Chehab static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength) 8899a0bf528SMauro Carvalho Chehab { 89009611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 8919a0bf528SMauro Carvalho Chehab int ret; 8929a0bf528SMauro Carvalho Chehab u8 strength2; 8939a0bf528SMauro Carvalho Chehab 8949a0bf528SMauro Carvalho Chehab /* read signal strength of 0-100 scale */ 89509611caaSAntti Palosaari ret = af9033_rd_reg(dev, 0x800048, &strength2); 8969a0bf528SMauro Carvalho Chehab if (ret < 0) 8979a0bf528SMauro Carvalho Chehab goto err; 8989a0bf528SMauro Carvalho Chehab 8999a0bf528SMauro Carvalho Chehab /* scale value to 0x0000-0xffff */ 9009a0bf528SMauro Carvalho Chehab *strength = strength2 * 0xffff / 100; 9019a0bf528SMauro Carvalho Chehab 9029a0bf528SMauro Carvalho Chehab return 0; 9039a0bf528SMauro Carvalho Chehab 9049a0bf528SMauro Carvalho Chehab err: 90509611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 9069a0bf528SMauro Carvalho Chehab 9079a0bf528SMauro Carvalho Chehab return ret; 9089a0bf528SMauro Carvalho Chehab } 9099a0bf528SMauro Carvalho Chehab 91009611caaSAntti Palosaari static int af9033_update_ch_stat(struct af9033_dev *dev) 9119a0bf528SMauro Carvalho Chehab { 9129a0bf528SMauro Carvalho Chehab int ret = 0; 9139a0bf528SMauro Carvalho Chehab u32 err_cnt, bit_cnt; 9149a0bf528SMauro Carvalho Chehab u16 abort_cnt; 9159a0bf528SMauro Carvalho Chehab u8 buf[7]; 9169a0bf528SMauro Carvalho Chehab 9179a0bf528SMauro Carvalho Chehab /* only update data every half second */ 91809611caaSAntti Palosaari if (time_after(jiffies, dev->last_stat_check + msecs_to_jiffies(500))) { 91909611caaSAntti Palosaari ret = af9033_rd_regs(dev, 0x800032, buf, sizeof(buf)); 9209a0bf528SMauro Carvalho Chehab if (ret < 0) 9219a0bf528SMauro Carvalho Chehab goto err; 9229a0bf528SMauro Carvalho Chehab /* in 8 byte packets? */ 9239a0bf528SMauro Carvalho Chehab abort_cnt = (buf[1] << 8) + buf[0]; 9249a0bf528SMauro Carvalho Chehab /* in bits */ 9259a0bf528SMauro Carvalho Chehab err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2]; 9269a0bf528SMauro Carvalho Chehab /* in 8 byte packets? always(?) 0x2710 = 10000 */ 9279a0bf528SMauro Carvalho Chehab bit_cnt = (buf[6] << 8) + buf[5]; 9289a0bf528SMauro Carvalho Chehab 9299a0bf528SMauro Carvalho Chehab if (bit_cnt < abort_cnt) { 9309a0bf528SMauro Carvalho Chehab abort_cnt = 1000; 93109611caaSAntti Palosaari dev->ber = 0xffffffff; 9329a0bf528SMauro Carvalho Chehab } else { 93324e419a0SAntti Palosaari /* 93424e419a0SAntti Palosaari * 8 byte packets, that have not been rejected already 93524e419a0SAntti Palosaari */ 9369a0bf528SMauro Carvalho Chehab bit_cnt -= (u32)abort_cnt; 9379a0bf528SMauro Carvalho Chehab if (bit_cnt == 0) { 93809611caaSAntti Palosaari dev->ber = 0xffffffff; 9399a0bf528SMauro Carvalho Chehab } else { 9409a0bf528SMauro Carvalho Chehab err_cnt -= (u32)abort_cnt * 8 * 8; 9419a0bf528SMauro Carvalho Chehab bit_cnt *= 8 * 8; 94209611caaSAntti Palosaari dev->ber = err_cnt * (0xffffffff / bit_cnt); 9439a0bf528SMauro Carvalho Chehab } 9449a0bf528SMauro Carvalho Chehab } 94509611caaSAntti Palosaari dev->ucb += abort_cnt; 94609611caaSAntti Palosaari dev->last_stat_check = jiffies; 9479a0bf528SMauro Carvalho Chehab } 9489a0bf528SMauro Carvalho Chehab 9499a0bf528SMauro Carvalho Chehab return 0; 9509a0bf528SMauro Carvalho Chehab err: 95109611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 9520a73f2d6SAntti Palosaari 9539a0bf528SMauro Carvalho Chehab return ret; 9549a0bf528SMauro Carvalho Chehab } 9559a0bf528SMauro Carvalho Chehab 9569a0bf528SMauro Carvalho Chehab static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber) 9579a0bf528SMauro Carvalho Chehab { 95809611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 9599a0bf528SMauro Carvalho Chehab int ret; 9609a0bf528SMauro Carvalho Chehab 96109611caaSAntti Palosaari ret = af9033_update_ch_stat(dev); 9629a0bf528SMauro Carvalho Chehab if (ret < 0) 9639a0bf528SMauro Carvalho Chehab return ret; 9649a0bf528SMauro Carvalho Chehab 96509611caaSAntti Palosaari *ber = dev->ber; 9669a0bf528SMauro Carvalho Chehab 9679a0bf528SMauro Carvalho Chehab return 0; 9689a0bf528SMauro Carvalho Chehab } 9699a0bf528SMauro Carvalho Chehab 9709a0bf528SMauro Carvalho Chehab static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) 9719a0bf528SMauro Carvalho Chehab { 97209611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 9739a0bf528SMauro Carvalho Chehab int ret; 9749a0bf528SMauro Carvalho Chehab 97509611caaSAntti Palosaari ret = af9033_update_ch_stat(dev); 9769a0bf528SMauro Carvalho Chehab if (ret < 0) 9779a0bf528SMauro Carvalho Chehab return ret; 9789a0bf528SMauro Carvalho Chehab 97909611caaSAntti Palosaari *ucblocks = dev->ucb; 9809a0bf528SMauro Carvalho Chehab 9819a0bf528SMauro Carvalho Chehab return 0; 9829a0bf528SMauro Carvalho Chehab } 9839a0bf528SMauro Carvalho Chehab 9849a0bf528SMauro Carvalho Chehab static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) 9859a0bf528SMauro Carvalho Chehab { 98609611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 9879a0bf528SMauro Carvalho Chehab int ret; 9889a0bf528SMauro Carvalho Chehab 98909611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: enable=%d\n", __func__, enable); 9909a0bf528SMauro Carvalho Chehab 99109611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01); 9929a0bf528SMauro Carvalho Chehab if (ret < 0) 9939a0bf528SMauro Carvalho Chehab goto err; 9949a0bf528SMauro Carvalho Chehab 9959a0bf528SMauro Carvalho Chehab return 0; 9969a0bf528SMauro Carvalho Chehab 9979a0bf528SMauro Carvalho Chehab err: 99809611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 9999a0bf528SMauro Carvalho Chehab 10009a0bf528SMauro Carvalho Chehab return ret; 10019a0bf528SMauro Carvalho Chehab } 10029a0bf528SMauro Carvalho Chehab 1003ed97a6feSMauro Carvalho Chehab static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff) 1004040cf86cSAntti Palosaari { 100509611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 1006040cf86cSAntti Palosaari int ret; 1007040cf86cSAntti Palosaari 100809611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: onoff=%d\n", __func__, onoff); 1009040cf86cSAntti Palosaari 101009611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01); 1011040cf86cSAntti Palosaari if (ret < 0) 1012040cf86cSAntti Palosaari goto err; 1013040cf86cSAntti Palosaari 1014040cf86cSAntti Palosaari return 0; 1015040cf86cSAntti Palosaari 1016040cf86cSAntti Palosaari err: 101709611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 1018040cf86cSAntti Palosaari 1019040cf86cSAntti Palosaari return ret; 1020040cf86cSAntti Palosaari } 1021040cf86cSAntti Palosaari 102224e419a0SAntti Palosaari static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid, 102324e419a0SAntti Palosaari int onoff) 1024040cf86cSAntti Palosaari { 102509611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 1026040cf86cSAntti Palosaari int ret; 1027040cf86cSAntti Palosaari u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff}; 1028040cf86cSAntti Palosaari 102909611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: index=%d pid=%04x onoff=%d\n", 1030040cf86cSAntti Palosaari __func__, index, pid, onoff); 1031040cf86cSAntti Palosaari 1032040cf86cSAntti Palosaari if (pid > 0x1fff) 1033040cf86cSAntti Palosaari return 0; 1034040cf86cSAntti Palosaari 103509611caaSAntti Palosaari ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2); 1036040cf86cSAntti Palosaari if (ret < 0) 1037040cf86cSAntti Palosaari goto err; 1038040cf86cSAntti Palosaari 103909611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x80f994, onoff); 1040040cf86cSAntti Palosaari if (ret < 0) 1041040cf86cSAntti Palosaari goto err; 1042040cf86cSAntti Palosaari 104309611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x80f995, index); 1044040cf86cSAntti Palosaari if (ret < 0) 1045040cf86cSAntti Palosaari goto err; 1046040cf86cSAntti Palosaari 1047040cf86cSAntti Palosaari return 0; 1048040cf86cSAntti Palosaari 1049040cf86cSAntti Palosaari err: 105009611caaSAntti Palosaari dev_dbg(&dev->i2c->dev, "%s: failed=%d\n", __func__, ret); 1051040cf86cSAntti Palosaari 1052040cf86cSAntti Palosaari return ret; 1053040cf86cSAntti Palosaari } 1054040cf86cSAntti Palosaari 10559a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops af9033_ops; 10569a0bf528SMauro Carvalho Chehab 10579a0bf528SMauro Carvalho Chehab struct dvb_frontend *af9033_attach(const struct af9033_config *config, 1058ed97a6feSMauro Carvalho Chehab struct i2c_adapter *i2c, 1059ed97a6feSMauro Carvalho Chehab struct af9033_ops *ops) 10609a0bf528SMauro Carvalho Chehab { 10619a0bf528SMauro Carvalho Chehab int ret; 106209611caaSAntti Palosaari struct af9033_dev *dev; 10639a0bf528SMauro Carvalho Chehab u8 buf[8]; 1064ef5211fdSAntti Palosaari u32 reg; 10659a0bf528SMauro Carvalho Chehab 10660a73f2d6SAntti Palosaari dev_dbg(&i2c->dev, "%s:\n", __func__); 10679a0bf528SMauro Carvalho Chehab 10689a0bf528SMauro Carvalho Chehab /* allocate memory for the internal state */ 106909611caaSAntti Palosaari dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL); 107009611caaSAntti Palosaari if (dev == NULL) 10719a0bf528SMauro Carvalho Chehab goto err; 10729a0bf528SMauro Carvalho Chehab 10739a0bf528SMauro Carvalho Chehab /* setup the state */ 107409611caaSAntti Palosaari dev->i2c = i2c; 107509611caaSAntti Palosaari memcpy(&dev->cfg, config, sizeof(struct af9033_config)); 10769a0bf528SMauro Carvalho Chehab 107709611caaSAntti Palosaari if (dev->cfg.clock != 12000000) { 107809611caaSAntti Palosaari dev_err(&dev->i2c->dev, 107924e419a0SAntti Palosaari "%s: af9033: unsupported clock=%d, only 12000000 Hz is supported currently\n", 108009611caaSAntti Palosaari KBUILD_MODNAME, dev->cfg.clock); 10819a0bf528SMauro Carvalho Chehab goto err; 10829a0bf528SMauro Carvalho Chehab } 10839a0bf528SMauro Carvalho Chehab 10849a0bf528SMauro Carvalho Chehab /* firmware version */ 108509611caaSAntti Palosaari switch (dev->cfg.tuner) { 1086ef5211fdSAntti Palosaari case AF9033_TUNER_IT9135_38: 1087ef5211fdSAntti Palosaari case AF9033_TUNER_IT9135_51: 1088ef5211fdSAntti Palosaari case AF9033_TUNER_IT9135_52: 1089ef5211fdSAntti Palosaari case AF9033_TUNER_IT9135_60: 1090ef5211fdSAntti Palosaari case AF9033_TUNER_IT9135_61: 1091ef5211fdSAntti Palosaari case AF9033_TUNER_IT9135_62: 1092ef5211fdSAntti Palosaari reg = 0x004bfc; 1093ef5211fdSAntti Palosaari break; 1094ef5211fdSAntti Palosaari default: 1095ef5211fdSAntti Palosaari reg = 0x0083e9; 1096ef5211fdSAntti Palosaari break; 1097ef5211fdSAntti Palosaari } 1098ef5211fdSAntti Palosaari 109909611caaSAntti Palosaari ret = af9033_rd_regs(dev, reg, &buf[0], 4); 11009a0bf528SMauro Carvalho Chehab if (ret < 0) 11019a0bf528SMauro Carvalho Chehab goto err; 11029a0bf528SMauro Carvalho Chehab 110309611caaSAntti Palosaari ret = af9033_rd_regs(dev, 0x804191, &buf[4], 4); 11049a0bf528SMauro Carvalho Chehab if (ret < 0) 11059a0bf528SMauro Carvalho Chehab goto err; 11069a0bf528SMauro Carvalho Chehab 110709611caaSAntti Palosaari dev_info(&dev->i2c->dev, 110824e419a0SAntti Palosaari "%s: firmware version: LINK=%d.%d.%d.%d OFDM=%d.%d.%d.%d\n", 110924e419a0SAntti Palosaari KBUILD_MODNAME, buf[0], buf[1], buf[2], buf[3], buf[4], 111024e419a0SAntti Palosaari buf[5], buf[6], buf[7]); 11119a0bf528SMauro Carvalho Chehab 111212897dc3SAntti Palosaari /* sleep */ 111309611caaSAntti Palosaari switch (dev->cfg.tuner) { 11140c13c54dSAntti Palosaari case AF9033_TUNER_IT9135_38: 11150c13c54dSAntti Palosaari case AF9033_TUNER_IT9135_51: 11160c13c54dSAntti Palosaari case AF9033_TUNER_IT9135_52: 11170c13c54dSAntti Palosaari case AF9033_TUNER_IT9135_60: 11180c13c54dSAntti Palosaari case AF9033_TUNER_IT9135_61: 11190c13c54dSAntti Palosaari case AF9033_TUNER_IT9135_62: 11200c13c54dSAntti Palosaari /* IT9135 did not like to sleep at that early */ 11210c13c54dSAntti Palosaari break; 11220c13c54dSAntti Palosaari default: 112309611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x80004c, 1); 112412897dc3SAntti Palosaari if (ret < 0) 112512897dc3SAntti Palosaari goto err; 112612897dc3SAntti Palosaari 112709611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x800000, 0); 112812897dc3SAntti Palosaari if (ret < 0) 112912897dc3SAntti Palosaari goto err; 11304902bb39SAntti Palosaari } 113112897dc3SAntti Palosaari 11329a0bf528SMauro Carvalho Chehab /* configure internal TS mode */ 113309611caaSAntti Palosaari switch (dev->cfg.ts_mode) { 11349a0bf528SMauro Carvalho Chehab case AF9033_TS_MODE_PARALLEL: 113509611caaSAntti Palosaari dev->ts_mode_parallel = true; 11369a0bf528SMauro Carvalho Chehab break; 11379a0bf528SMauro Carvalho Chehab case AF9033_TS_MODE_SERIAL: 113809611caaSAntti Palosaari dev->ts_mode_serial = true; 11399a0bf528SMauro Carvalho Chehab break; 11409a0bf528SMauro Carvalho Chehab case AF9033_TS_MODE_USB: 11419a0bf528SMauro Carvalho Chehab /* usb mode for AF9035 */ 11429a0bf528SMauro Carvalho Chehab default: 11439a0bf528SMauro Carvalho Chehab break; 11449a0bf528SMauro Carvalho Chehab } 11459a0bf528SMauro Carvalho Chehab 11469a0bf528SMauro Carvalho Chehab /* create dvb_frontend */ 114709611caaSAntti Palosaari memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops)); 114809611caaSAntti Palosaari dev->fe.demodulator_priv = dev; 11499a0bf528SMauro Carvalho Chehab 1150ed97a6feSMauro Carvalho Chehab if (ops) { 1151ed97a6feSMauro Carvalho Chehab ops->pid_filter = af9033_pid_filter; 1152ed97a6feSMauro Carvalho Chehab ops->pid_filter_ctrl = af9033_pid_filter_ctrl; 1153ed97a6feSMauro Carvalho Chehab } 1154ed97a6feSMauro Carvalho Chehab 115509611caaSAntti Palosaari return &dev->fe; 11569a0bf528SMauro Carvalho Chehab 11579a0bf528SMauro Carvalho Chehab err: 115809611caaSAntti Palosaari kfree(dev); 11599a0bf528SMauro Carvalho Chehab return NULL; 11609a0bf528SMauro Carvalho Chehab } 11619a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(af9033_attach); 11629a0bf528SMauro Carvalho Chehab 11639a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops af9033_ops = { 11649a0bf528SMauro Carvalho Chehab .delsys = { SYS_DVBT }, 11659a0bf528SMauro Carvalho Chehab .info = { 11669a0bf528SMauro Carvalho Chehab .name = "Afatech AF9033 (DVB-T)", 11679a0bf528SMauro Carvalho Chehab .frequency_min = 174000000, 11689a0bf528SMauro Carvalho Chehab .frequency_max = 862000000, 11699a0bf528SMauro Carvalho Chehab .frequency_stepsize = 250000, 11709a0bf528SMauro Carvalho Chehab .frequency_tolerance = 0, 11719a0bf528SMauro Carvalho Chehab .caps = FE_CAN_FEC_1_2 | 11729a0bf528SMauro Carvalho Chehab FE_CAN_FEC_2_3 | 11739a0bf528SMauro Carvalho Chehab FE_CAN_FEC_3_4 | 11749a0bf528SMauro Carvalho Chehab FE_CAN_FEC_5_6 | 11759a0bf528SMauro Carvalho Chehab FE_CAN_FEC_7_8 | 11769a0bf528SMauro Carvalho Chehab FE_CAN_FEC_AUTO | 11779a0bf528SMauro Carvalho Chehab FE_CAN_QPSK | 11789a0bf528SMauro Carvalho Chehab FE_CAN_QAM_16 | 11799a0bf528SMauro Carvalho Chehab FE_CAN_QAM_64 | 11809a0bf528SMauro Carvalho Chehab FE_CAN_QAM_AUTO | 11819a0bf528SMauro Carvalho Chehab FE_CAN_TRANSMISSION_MODE_AUTO | 11829a0bf528SMauro Carvalho Chehab FE_CAN_GUARD_INTERVAL_AUTO | 11839a0bf528SMauro Carvalho Chehab FE_CAN_HIERARCHY_AUTO | 11849a0bf528SMauro Carvalho Chehab FE_CAN_RECOVER | 11859a0bf528SMauro Carvalho Chehab FE_CAN_MUTE_TS 11869a0bf528SMauro Carvalho Chehab }, 11879a0bf528SMauro Carvalho Chehab 11889a0bf528SMauro Carvalho Chehab .release = af9033_release, 11899a0bf528SMauro Carvalho Chehab 11909a0bf528SMauro Carvalho Chehab .init = af9033_init, 11919a0bf528SMauro Carvalho Chehab .sleep = af9033_sleep, 11929a0bf528SMauro Carvalho Chehab 11939a0bf528SMauro Carvalho Chehab .get_tune_settings = af9033_get_tune_settings, 11949a0bf528SMauro Carvalho Chehab .set_frontend = af9033_set_frontend, 11959a0bf528SMauro Carvalho Chehab .get_frontend = af9033_get_frontend, 11969a0bf528SMauro Carvalho Chehab 11979a0bf528SMauro Carvalho Chehab .read_status = af9033_read_status, 11989a0bf528SMauro Carvalho Chehab .read_snr = af9033_read_snr, 11999a0bf528SMauro Carvalho Chehab .read_signal_strength = af9033_read_signal_strength, 12009a0bf528SMauro Carvalho Chehab .read_ber = af9033_read_ber, 12019a0bf528SMauro Carvalho Chehab .read_ucblocks = af9033_read_ucblocks, 12029a0bf528SMauro Carvalho Chehab 12039a0bf528SMauro Carvalho Chehab .i2c_gate_ctrl = af9033_i2c_gate_ctrl, 12049a0bf528SMauro Carvalho Chehab }; 12059a0bf528SMauro Carvalho Chehab 12069a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 12079a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver"); 12089a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL"); 1209