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 { 28f5b00a76SAntti Palosaari struct i2c_client *client; 299a0bf528SMauro Carvalho Chehab struct dvb_frontend fe; 309a0bf528SMauro Carvalho Chehab struct af9033_config cfg; 3183f11619SAntti Palosaari bool is_af9035; 3283f11619SAntti Palosaari bool is_it9135; 339a0bf528SMauro Carvalho Chehab 349a0bf528SMauro Carvalho Chehab u32 bandwidth_hz; 359a0bf528SMauro Carvalho Chehab bool ts_mode_parallel; 369a0bf528SMauro Carvalho Chehab bool ts_mode_serial; 379a0bf528SMauro Carvalho Chehab 3883f11619SAntti Palosaari fe_status_t fe_status; 399a0bf528SMauro Carvalho Chehab u32 ber; 409a0bf528SMauro Carvalho Chehab u32 ucb; 4183f11619SAntti Palosaari struct delayed_work stat_work; 429a0bf528SMauro Carvalho Chehab unsigned long last_stat_check; 439a0bf528SMauro Carvalho Chehab }; 449a0bf528SMauro Carvalho Chehab 459a0bf528SMauro Carvalho Chehab /* write multiple registers */ 4609611caaSAntti Palosaari static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val, 479a0bf528SMauro Carvalho Chehab int len) 489a0bf528SMauro Carvalho Chehab { 499a0bf528SMauro Carvalho Chehab int ret; 5037ebaf68SMauro Carvalho Chehab u8 buf[MAX_XFER_SIZE]; 519a0bf528SMauro Carvalho Chehab struct i2c_msg msg[1] = { 529a0bf528SMauro Carvalho Chehab { 53f5b00a76SAntti Palosaari .addr = dev->client->addr, 549a0bf528SMauro Carvalho Chehab .flags = 0, 5537ebaf68SMauro Carvalho Chehab .len = 3 + len, 569a0bf528SMauro Carvalho Chehab .buf = buf, 579a0bf528SMauro Carvalho Chehab } 589a0bf528SMauro Carvalho Chehab }; 599a0bf528SMauro Carvalho Chehab 6037ebaf68SMauro Carvalho Chehab if (3 + len > sizeof(buf)) { 61f5b00a76SAntti Palosaari dev_warn(&dev->client->dev, 626a087f1fSAntti Palosaari "i2c wr reg=%04x: len=%d is too big!\n", 636a087f1fSAntti Palosaari reg, len); 6437ebaf68SMauro Carvalho Chehab return -EINVAL; 6537ebaf68SMauro Carvalho Chehab } 6637ebaf68SMauro Carvalho Chehab 679a0bf528SMauro Carvalho Chehab buf[0] = (reg >> 16) & 0xff; 689a0bf528SMauro Carvalho Chehab buf[1] = (reg >> 8) & 0xff; 699a0bf528SMauro Carvalho Chehab buf[2] = (reg >> 0) & 0xff; 709a0bf528SMauro Carvalho Chehab memcpy(&buf[3], val, len); 719a0bf528SMauro Carvalho Chehab 72f5b00a76SAntti Palosaari ret = i2c_transfer(dev->client->adapter, msg, 1); 739a0bf528SMauro Carvalho Chehab if (ret == 1) { 749a0bf528SMauro Carvalho Chehab ret = 0; 759a0bf528SMauro Carvalho Chehab } else { 766a087f1fSAntti Palosaari dev_warn(&dev->client->dev, "i2c wr failed=%d reg=%06x len=%d\n", 776a087f1fSAntti Palosaari ret, reg, len); 789a0bf528SMauro Carvalho Chehab ret = -EREMOTEIO; 799a0bf528SMauro Carvalho Chehab } 809a0bf528SMauro Carvalho Chehab 819a0bf528SMauro Carvalho Chehab return ret; 829a0bf528SMauro Carvalho Chehab } 839a0bf528SMauro Carvalho Chehab 849a0bf528SMauro Carvalho Chehab /* read multiple registers */ 8509611caaSAntti Palosaari static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len) 869a0bf528SMauro Carvalho Chehab { 879a0bf528SMauro Carvalho Chehab int ret; 889a0bf528SMauro Carvalho Chehab u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff, 899a0bf528SMauro Carvalho Chehab (reg >> 0) & 0xff }; 909a0bf528SMauro Carvalho Chehab struct i2c_msg msg[2] = { 919a0bf528SMauro Carvalho Chehab { 92f5b00a76SAntti Palosaari .addr = dev->client->addr, 939a0bf528SMauro Carvalho Chehab .flags = 0, 949a0bf528SMauro Carvalho Chehab .len = sizeof(buf), 959a0bf528SMauro Carvalho Chehab .buf = buf 969a0bf528SMauro Carvalho Chehab }, { 97f5b00a76SAntti Palosaari .addr = dev->client->addr, 989a0bf528SMauro Carvalho Chehab .flags = I2C_M_RD, 999a0bf528SMauro Carvalho Chehab .len = len, 1009a0bf528SMauro Carvalho Chehab .buf = val 1019a0bf528SMauro Carvalho Chehab } 1029a0bf528SMauro Carvalho Chehab }; 1039a0bf528SMauro Carvalho Chehab 104f5b00a76SAntti Palosaari ret = i2c_transfer(dev->client->adapter, msg, 2); 1059a0bf528SMauro Carvalho Chehab if (ret == 2) { 1069a0bf528SMauro Carvalho Chehab ret = 0; 1079a0bf528SMauro Carvalho Chehab } else { 1086a087f1fSAntti Palosaari dev_warn(&dev->client->dev, "i2c rd failed=%d reg=%06x len=%d\n", 1096a087f1fSAntti Palosaari ret, reg, len); 1109a0bf528SMauro Carvalho Chehab ret = -EREMOTEIO; 1119a0bf528SMauro Carvalho Chehab } 1129a0bf528SMauro Carvalho Chehab 1139a0bf528SMauro Carvalho Chehab return ret; 1149a0bf528SMauro Carvalho Chehab } 1159a0bf528SMauro Carvalho Chehab 1169a0bf528SMauro Carvalho Chehab 1179a0bf528SMauro Carvalho Chehab /* write single register */ 11809611caaSAntti Palosaari static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val) 1199a0bf528SMauro Carvalho Chehab { 12009611caaSAntti Palosaari return af9033_wr_regs(dev, reg, &val, 1); 1219a0bf528SMauro Carvalho Chehab } 1229a0bf528SMauro Carvalho Chehab 1239a0bf528SMauro Carvalho Chehab /* read single register */ 12409611caaSAntti Palosaari static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val) 1259a0bf528SMauro Carvalho Chehab { 12609611caaSAntti Palosaari return af9033_rd_regs(dev, reg, val, 1); 1279a0bf528SMauro Carvalho Chehab } 1289a0bf528SMauro Carvalho Chehab 1299a0bf528SMauro Carvalho Chehab /* write single register with mask */ 13009611caaSAntti Palosaari static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val, 1319a0bf528SMauro Carvalho Chehab u8 mask) 1329a0bf528SMauro Carvalho Chehab { 1339a0bf528SMauro Carvalho Chehab int ret; 1349a0bf528SMauro Carvalho Chehab u8 tmp; 1359a0bf528SMauro Carvalho Chehab 1369a0bf528SMauro Carvalho Chehab /* no need for read if whole reg is written */ 1379a0bf528SMauro Carvalho Chehab if (mask != 0xff) { 13809611caaSAntti Palosaari ret = af9033_rd_regs(dev, reg, &tmp, 1); 1399a0bf528SMauro Carvalho Chehab if (ret) 1409a0bf528SMauro Carvalho Chehab return ret; 1419a0bf528SMauro Carvalho Chehab 1429a0bf528SMauro Carvalho Chehab val &= mask; 1439a0bf528SMauro Carvalho Chehab tmp &= ~mask; 1449a0bf528SMauro Carvalho Chehab val |= tmp; 1459a0bf528SMauro Carvalho Chehab } 1469a0bf528SMauro Carvalho Chehab 14709611caaSAntti Palosaari return af9033_wr_regs(dev, reg, &val, 1); 1489a0bf528SMauro Carvalho Chehab } 1499a0bf528SMauro Carvalho Chehab 1509a0bf528SMauro Carvalho Chehab /* read single register with mask */ 15109611caaSAntti Palosaari static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val, 1529a0bf528SMauro Carvalho Chehab u8 mask) 1539a0bf528SMauro Carvalho Chehab { 1549a0bf528SMauro Carvalho Chehab int ret, i; 1559a0bf528SMauro Carvalho Chehab u8 tmp; 1569a0bf528SMauro Carvalho Chehab 15709611caaSAntti Palosaari ret = af9033_rd_regs(dev, reg, &tmp, 1); 1589a0bf528SMauro Carvalho Chehab if (ret) 1599a0bf528SMauro Carvalho Chehab return ret; 1609a0bf528SMauro Carvalho Chehab 1619a0bf528SMauro Carvalho Chehab tmp &= mask; 1629a0bf528SMauro Carvalho Chehab 1639a0bf528SMauro Carvalho Chehab /* find position of the first bit */ 1649a0bf528SMauro Carvalho Chehab for (i = 0; i < 8; i++) { 1659a0bf528SMauro Carvalho Chehab if ((mask >> i) & 0x01) 1669a0bf528SMauro Carvalho Chehab break; 1679a0bf528SMauro Carvalho Chehab } 1689a0bf528SMauro Carvalho Chehab *val = tmp >> i; 1699a0bf528SMauro Carvalho Chehab 1709a0bf528SMauro Carvalho Chehab return 0; 1719a0bf528SMauro Carvalho Chehab } 1729a0bf528SMauro Carvalho Chehab 1733bf5e552SAntti Palosaari /* write reg val table using reg addr auto increment */ 17409611caaSAntti Palosaari static int af9033_wr_reg_val_tab(struct af9033_dev *dev, 1753bf5e552SAntti Palosaari const struct reg_val *tab, int tab_len) 1763bf5e552SAntti Palosaari { 177d18a88b1SAntti Palosaari #define MAX_TAB_LEN 212 1783bf5e552SAntti Palosaari int ret, i, j; 179d18a88b1SAntti Palosaari u8 buf[1 + MAX_TAB_LEN]; 180d18a88b1SAntti Palosaari 1816a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "tab_len=%d\n", tab_len); 18237ebaf68SMauro Carvalho Chehab 18337ebaf68SMauro Carvalho Chehab if (tab_len > sizeof(buf)) { 1846a087f1fSAntti Palosaari dev_warn(&dev->client->dev, "tab len %d is too big\n", tab_len); 18537ebaf68SMauro Carvalho Chehab return -EINVAL; 18637ebaf68SMauro Carvalho Chehab } 1873bf5e552SAntti Palosaari 1883bf5e552SAntti Palosaari for (i = 0, j = 0; i < tab_len; i++) { 1893bf5e552SAntti Palosaari buf[j] = tab[i].val; 1903bf5e552SAntti Palosaari 1913bf5e552SAntti Palosaari if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) { 19209611caaSAntti Palosaari ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1); 1933bf5e552SAntti Palosaari if (ret < 0) 1943bf5e552SAntti Palosaari goto err; 1953bf5e552SAntti Palosaari 1963bf5e552SAntti Palosaari j = 0; 1973bf5e552SAntti Palosaari } else { 1983bf5e552SAntti Palosaari j++; 1993bf5e552SAntti Palosaari } 2003bf5e552SAntti Palosaari } 2013bf5e552SAntti Palosaari 2023bf5e552SAntti Palosaari return 0; 2033bf5e552SAntti Palosaari 2043bf5e552SAntti Palosaari err: 2056a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 2063bf5e552SAntti Palosaari 2073bf5e552SAntti Palosaari return ret; 2083bf5e552SAntti Palosaari } 2093bf5e552SAntti Palosaari 21009611caaSAntti Palosaari static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x) 2119a0bf528SMauro Carvalho Chehab { 2129a0bf528SMauro Carvalho Chehab u32 r = 0, c = 0, i; 2139a0bf528SMauro Carvalho Chehab 2146a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "a=%d b=%d x=%d\n", a, b, x); 2159a0bf528SMauro Carvalho Chehab 2169a0bf528SMauro Carvalho Chehab if (a > b) { 2179a0bf528SMauro Carvalho Chehab c = a / b; 2189a0bf528SMauro Carvalho Chehab a = a - c * b; 2199a0bf528SMauro Carvalho Chehab } 2209a0bf528SMauro Carvalho Chehab 2219a0bf528SMauro Carvalho Chehab for (i = 0; i < x; i++) { 2229a0bf528SMauro Carvalho Chehab if (a >= b) { 2239a0bf528SMauro Carvalho Chehab r += 1; 2249a0bf528SMauro Carvalho Chehab a -= b; 2259a0bf528SMauro Carvalho Chehab } 2269a0bf528SMauro Carvalho Chehab a <<= 1; 2279a0bf528SMauro Carvalho Chehab r <<= 1; 2289a0bf528SMauro Carvalho Chehab } 2299a0bf528SMauro Carvalho Chehab r = (c << (u32)x) + r; 2309a0bf528SMauro Carvalho Chehab 2316a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "a=%d b=%d x=%d r=%d r=%x\n", a, b, x, r, r); 2329a0bf528SMauro Carvalho Chehab 2339a0bf528SMauro Carvalho Chehab return r; 2349a0bf528SMauro Carvalho Chehab } 2359a0bf528SMauro Carvalho Chehab 2369a0bf528SMauro Carvalho Chehab static int af9033_init(struct dvb_frontend *fe) 2379a0bf528SMauro Carvalho Chehab { 23809611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 2399a0bf528SMauro Carvalho Chehab int ret, i, len; 2409a0bf528SMauro Carvalho Chehab const struct reg_val *init; 2419a0bf528SMauro Carvalho Chehab u8 buf[4]; 2429a0bf528SMauro Carvalho Chehab u32 adc_cw, clock_cw; 2439a0bf528SMauro Carvalho Chehab struct reg_val_mask tab[] = { 2449a0bf528SMauro Carvalho Chehab { 0x80fb24, 0x00, 0x08 }, 2459a0bf528SMauro Carvalho Chehab { 0x80004c, 0x00, 0xff }, 24609611caaSAntti Palosaari { 0x00f641, dev->cfg.tuner, 0xff }, 2479a0bf528SMauro Carvalho Chehab { 0x80f5ca, 0x01, 0x01 }, 2489a0bf528SMauro Carvalho Chehab { 0x80f715, 0x01, 0x01 }, 2499a0bf528SMauro Carvalho Chehab { 0x00f41f, 0x04, 0x04 }, 2509a0bf528SMauro Carvalho Chehab { 0x00f41a, 0x01, 0x01 }, 2519a0bf528SMauro Carvalho Chehab { 0x80f731, 0x00, 0x01 }, 2529a0bf528SMauro Carvalho Chehab { 0x00d91e, 0x00, 0x01 }, 2539a0bf528SMauro Carvalho Chehab { 0x00d919, 0x00, 0x01 }, 2549a0bf528SMauro Carvalho Chehab { 0x80f732, 0x00, 0x01 }, 2559a0bf528SMauro Carvalho Chehab { 0x00d91f, 0x00, 0x01 }, 2569a0bf528SMauro Carvalho Chehab { 0x00d91a, 0x00, 0x01 }, 2579a0bf528SMauro Carvalho Chehab { 0x80f730, 0x00, 0x01 }, 2589a0bf528SMauro Carvalho Chehab { 0x80f778, 0x00, 0xff }, 2599a0bf528SMauro Carvalho Chehab { 0x80f73c, 0x01, 0x01 }, 2609a0bf528SMauro Carvalho Chehab { 0x80f776, 0x00, 0x01 }, 2619a0bf528SMauro Carvalho Chehab { 0x00d8fd, 0x01, 0xff }, 2629a0bf528SMauro Carvalho Chehab { 0x00d830, 0x01, 0xff }, 2639a0bf528SMauro Carvalho Chehab { 0x00d831, 0x00, 0xff }, 2649a0bf528SMauro Carvalho Chehab { 0x00d832, 0x00, 0xff }, 26509611caaSAntti Palosaari { 0x80f985, dev->ts_mode_serial, 0x01 }, 26609611caaSAntti Palosaari { 0x80f986, dev->ts_mode_parallel, 0x01 }, 2679a0bf528SMauro Carvalho Chehab { 0x00d827, 0x00, 0xff }, 2689a0bf528SMauro Carvalho Chehab { 0x00d829, 0x00, 0xff }, 26909611caaSAntti Palosaari { 0x800045, dev->cfg.adc_multiplier, 0xff }, 2709a0bf528SMauro Carvalho Chehab }; 2719a0bf528SMauro Carvalho Chehab 2729a0bf528SMauro Carvalho Chehab /* program clock control */ 27309611caaSAntti Palosaari clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul); 2749a0bf528SMauro Carvalho Chehab buf[0] = (clock_cw >> 0) & 0xff; 2759a0bf528SMauro Carvalho Chehab buf[1] = (clock_cw >> 8) & 0xff; 2769a0bf528SMauro Carvalho Chehab buf[2] = (clock_cw >> 16) & 0xff; 2779a0bf528SMauro Carvalho Chehab buf[3] = (clock_cw >> 24) & 0xff; 2789a0bf528SMauro Carvalho Chehab 2796a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "clock=%d clock_cw=%08x\n", 2806a087f1fSAntti Palosaari dev->cfg.clock, clock_cw); 2819a0bf528SMauro Carvalho Chehab 28209611caaSAntti Palosaari ret = af9033_wr_regs(dev, 0x800025, buf, 4); 2839a0bf528SMauro Carvalho Chehab if (ret < 0) 2849a0bf528SMauro Carvalho Chehab goto err; 2859a0bf528SMauro Carvalho Chehab 2869a0bf528SMauro Carvalho Chehab /* program ADC control */ 2879a0bf528SMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { 28809611caaSAntti Palosaari if (clock_adc_lut[i].clock == dev->cfg.clock) 2899a0bf528SMauro Carvalho Chehab break; 2909a0bf528SMauro Carvalho Chehab } 2919a0bf528SMauro Carvalho Chehab 29209611caaSAntti Palosaari adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul); 2939a0bf528SMauro Carvalho Chehab buf[0] = (adc_cw >> 0) & 0xff; 2949a0bf528SMauro Carvalho Chehab buf[1] = (adc_cw >> 8) & 0xff; 2959a0bf528SMauro Carvalho Chehab buf[2] = (adc_cw >> 16) & 0xff; 2969a0bf528SMauro Carvalho Chehab 2976a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "adc=%d adc_cw=%06x\n", 2986a087f1fSAntti Palosaari clock_adc_lut[i].adc, adc_cw); 2999a0bf528SMauro Carvalho Chehab 30009611caaSAntti Palosaari ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3); 3019a0bf528SMauro Carvalho Chehab if (ret < 0) 3029a0bf528SMauro Carvalho Chehab goto err; 3039a0bf528SMauro Carvalho Chehab 3049a0bf528SMauro Carvalho Chehab /* program register table */ 3059a0bf528SMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(tab); i++) { 30609611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val, 3079a0bf528SMauro Carvalho Chehab tab[i].mask); 3089a0bf528SMauro Carvalho Chehab if (ret < 0) 3099a0bf528SMauro Carvalho Chehab goto err; 3109a0bf528SMauro Carvalho Chehab } 3119a0bf528SMauro Carvalho Chehab 312ca681fe0SAntti Palosaari /* clock output */ 31309611caaSAntti Palosaari if (dev->cfg.dyn0_clk) { 31409611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x80fba8, 0x00); 3159dc0f3feSAntti Palosaari if (ret < 0) 3169dc0f3feSAntti Palosaari goto err; 3179dc0f3feSAntti Palosaari } 3189dc0f3feSAntti Palosaari 3199a0bf528SMauro Carvalho Chehab /* settings for TS interface */ 32009611caaSAntti Palosaari if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) { 32109611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01); 3229a0bf528SMauro Carvalho Chehab if (ret < 0) 3239a0bf528SMauro Carvalho Chehab goto err; 3249a0bf528SMauro Carvalho Chehab 32509611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01); 3269a0bf528SMauro Carvalho Chehab if (ret < 0) 3279a0bf528SMauro Carvalho Chehab goto err; 3289a0bf528SMauro Carvalho Chehab } else { 32909611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01); 3309a0bf528SMauro Carvalho Chehab if (ret < 0) 3319a0bf528SMauro Carvalho Chehab goto err; 3329a0bf528SMauro Carvalho Chehab 33309611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01); 3349a0bf528SMauro Carvalho Chehab if (ret < 0) 3359a0bf528SMauro Carvalho Chehab goto err; 3369a0bf528SMauro Carvalho Chehab } 3379a0bf528SMauro Carvalho Chehab 3389a0bf528SMauro Carvalho Chehab /* load OFSM settings */ 3396a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "load ofsm settings\n"); 34009611caaSAntti Palosaari switch (dev->cfg.tuner) { 341fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_38: 342fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_51: 343fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_52: 344463c399cSAntti Palosaari len = ARRAY_SIZE(ofsm_init_it9135_v1); 345463c399cSAntti Palosaari init = ofsm_init_it9135_v1; 346463c399cSAntti Palosaari break; 347fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_60: 348fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_61: 349fe8eece1SAntti Palosaari case AF9033_TUNER_IT9135_62: 350463c399cSAntti Palosaari len = ARRAY_SIZE(ofsm_init_it9135_v2); 351463c399cSAntti Palosaari init = ofsm_init_it9135_v2; 352fe8eece1SAntti Palosaari break; 353fe8eece1SAntti Palosaari default: 3549a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(ofsm_init); 3559a0bf528SMauro Carvalho Chehab init = ofsm_init; 356fe8eece1SAntti Palosaari break; 357fe8eece1SAntti Palosaari } 358fe8eece1SAntti Palosaari 35909611caaSAntti Palosaari ret = af9033_wr_reg_val_tab(dev, init, len); 3609a0bf528SMauro Carvalho Chehab if (ret < 0) 3619a0bf528SMauro Carvalho Chehab goto err; 3629a0bf528SMauro Carvalho Chehab 3639a0bf528SMauro Carvalho Chehab /* load tuner specific settings */ 3646a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "load tuner specific settings\n"); 36509611caaSAntti Palosaari switch (dev->cfg.tuner) { 3669a0bf528SMauro Carvalho Chehab case AF9033_TUNER_TUA9001: 3679a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(tuner_init_tua9001); 3689a0bf528SMauro Carvalho Chehab init = tuner_init_tua9001; 3699a0bf528SMauro Carvalho Chehab break; 3709a0bf528SMauro Carvalho Chehab case AF9033_TUNER_FC0011: 3719a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(tuner_init_fc0011); 3729a0bf528SMauro Carvalho Chehab init = tuner_init_fc0011; 3739a0bf528SMauro Carvalho Chehab break; 3749a0bf528SMauro Carvalho Chehab case AF9033_TUNER_MXL5007T: 3759a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(tuner_init_mxl5007t); 3769a0bf528SMauro Carvalho Chehab init = tuner_init_mxl5007t; 3779a0bf528SMauro Carvalho Chehab break; 3789a0bf528SMauro Carvalho Chehab case AF9033_TUNER_TDA18218: 3799a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(tuner_init_tda18218); 3809a0bf528SMauro Carvalho Chehab init = tuner_init_tda18218; 3819a0bf528SMauro Carvalho Chehab break; 382d67ceb33SOliver Schinagl case AF9033_TUNER_FC2580: 383d67ceb33SOliver Schinagl len = ARRAY_SIZE(tuner_init_fc2580); 384d67ceb33SOliver Schinagl init = tuner_init_fc2580; 385d67ceb33SOliver Schinagl break; 386e713ad15SAntti Palosaari case AF9033_TUNER_FC0012: 387e713ad15SAntti Palosaari len = ARRAY_SIZE(tuner_init_fc0012); 388e713ad15SAntti Palosaari init = tuner_init_fc0012; 389e713ad15SAntti Palosaari break; 3904902bb39SAntti Palosaari case AF9033_TUNER_IT9135_38: 391a72cbb77SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_38); 392a72cbb77SAntti Palosaari init = tuner_init_it9135_38; 393a72cbb77SAntti Palosaari break; 3944902bb39SAntti Palosaari case AF9033_TUNER_IT9135_51: 395bb2e12a6SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_51); 396bb2e12a6SAntti Palosaari init = tuner_init_it9135_51; 397bb2e12a6SAntti Palosaari break; 3984902bb39SAntti Palosaari case AF9033_TUNER_IT9135_52: 39922d729f3SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_52); 40022d729f3SAntti Palosaari init = tuner_init_it9135_52; 40122d729f3SAntti Palosaari break; 4024902bb39SAntti Palosaari case AF9033_TUNER_IT9135_60: 403a49f53a0SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_60); 404a49f53a0SAntti Palosaari init = tuner_init_it9135_60; 405a49f53a0SAntti Palosaari break; 4064902bb39SAntti Palosaari case AF9033_TUNER_IT9135_61: 40785211323SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_61); 40885211323SAntti Palosaari init = tuner_init_it9135_61; 40985211323SAntti Palosaari break; 4104902bb39SAntti Palosaari case AF9033_TUNER_IT9135_62: 411dc4a2c40SAntti Palosaari len = ARRAY_SIZE(tuner_init_it9135_62); 412dc4a2c40SAntti Palosaari init = tuner_init_it9135_62; 4134902bb39SAntti Palosaari break; 4149a0bf528SMauro Carvalho Chehab default: 4156a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "unsupported tuner ID=%d\n", 4166a087f1fSAntti Palosaari dev->cfg.tuner); 4179a0bf528SMauro Carvalho Chehab ret = -ENODEV; 4189a0bf528SMauro Carvalho Chehab goto err; 4199a0bf528SMauro Carvalho Chehab } 4209a0bf528SMauro Carvalho Chehab 42109611caaSAntti Palosaari ret = af9033_wr_reg_val_tab(dev, init, len); 4229a0bf528SMauro Carvalho Chehab if (ret < 0) 4239a0bf528SMauro Carvalho Chehab goto err; 4249a0bf528SMauro Carvalho Chehab 42509611caaSAntti Palosaari if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { 42609611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01); 4279805992fSJose Alberto Reguero if (ret < 0) 4289805992fSJose Alberto Reguero goto err; 429bf97b637SAntti Palosaari 43009611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01); 4319805992fSJose Alberto Reguero if (ret < 0) 4329805992fSJose Alberto Reguero goto err; 433bf97b637SAntti Palosaari 43409611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01); 4359805992fSJose Alberto Reguero if (ret < 0) 4369805992fSJose Alberto Reguero goto err; 4379805992fSJose Alberto Reguero } 4389805992fSJose Alberto Reguero 43909611caaSAntti Palosaari switch (dev->cfg.tuner) { 440086991ddSAntti Palosaari case AF9033_TUNER_IT9135_60: 441086991ddSAntti Palosaari case AF9033_TUNER_IT9135_61: 442086991ddSAntti Palosaari case AF9033_TUNER_IT9135_62: 44309611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x800000, 0x01); 444086991ddSAntti Palosaari if (ret < 0) 445086991ddSAntti Palosaari goto err; 446086991ddSAntti Palosaari } 447086991ddSAntti Palosaari 44809611caaSAntti Palosaari dev->bandwidth_hz = 0; /* force to program all parameters */ 44983f11619SAntti Palosaari /* start statistics polling */ 45083f11619SAntti Palosaari schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000)); 4519a0bf528SMauro Carvalho Chehab 4529a0bf528SMauro Carvalho Chehab return 0; 4539a0bf528SMauro Carvalho Chehab 4549a0bf528SMauro Carvalho Chehab err: 4556a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 4569a0bf528SMauro Carvalho Chehab 4579a0bf528SMauro Carvalho Chehab return ret; 4589a0bf528SMauro Carvalho Chehab } 4599a0bf528SMauro Carvalho Chehab 4609a0bf528SMauro Carvalho Chehab static int af9033_sleep(struct dvb_frontend *fe) 4619a0bf528SMauro Carvalho Chehab { 46209611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 4639a0bf528SMauro Carvalho Chehab int ret, i; 4649a0bf528SMauro Carvalho Chehab u8 tmp; 4659a0bf528SMauro Carvalho Chehab 46683f11619SAntti Palosaari /* stop statistics polling */ 46783f11619SAntti Palosaari cancel_delayed_work_sync(&dev->stat_work); 46883f11619SAntti Palosaari 46909611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x80004c, 1); 4709a0bf528SMauro Carvalho Chehab if (ret < 0) 4719a0bf528SMauro Carvalho Chehab goto err; 4729a0bf528SMauro Carvalho Chehab 47309611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x800000, 0); 4749a0bf528SMauro Carvalho Chehab if (ret < 0) 4759a0bf528SMauro Carvalho Chehab goto err; 4769a0bf528SMauro Carvalho Chehab 4779a0bf528SMauro Carvalho Chehab for (i = 100, tmp = 1; i && tmp; i--) { 47809611caaSAntti Palosaari ret = af9033_rd_reg(dev, 0x80004c, &tmp); 4799a0bf528SMauro Carvalho Chehab if (ret < 0) 4809a0bf528SMauro Carvalho Chehab goto err; 4819a0bf528SMauro Carvalho Chehab 4829a0bf528SMauro Carvalho Chehab usleep_range(200, 10000); 4839a0bf528SMauro Carvalho Chehab } 4849a0bf528SMauro Carvalho Chehab 4856a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "loop=%d\n", i); 4869a0bf528SMauro Carvalho Chehab 4879a0bf528SMauro Carvalho Chehab if (i == 0) { 4889a0bf528SMauro Carvalho Chehab ret = -ETIMEDOUT; 4899a0bf528SMauro Carvalho Chehab goto err; 4909a0bf528SMauro Carvalho Chehab } 4919a0bf528SMauro Carvalho Chehab 49209611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08); 4939a0bf528SMauro Carvalho Chehab if (ret < 0) 4949a0bf528SMauro Carvalho Chehab goto err; 4959a0bf528SMauro Carvalho Chehab 4969a0bf528SMauro Carvalho Chehab /* prevent current leak (?) */ 49709611caaSAntti Palosaari if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { 4989a0bf528SMauro Carvalho Chehab /* enable parallel TS */ 49909611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01); 5009a0bf528SMauro Carvalho Chehab if (ret < 0) 5019a0bf528SMauro Carvalho Chehab goto err; 5029a0bf528SMauro Carvalho Chehab 50309611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01); 5049a0bf528SMauro Carvalho Chehab if (ret < 0) 5059a0bf528SMauro Carvalho Chehab goto err; 5069a0bf528SMauro Carvalho Chehab } 5079a0bf528SMauro Carvalho Chehab 5089a0bf528SMauro Carvalho Chehab return 0; 5099a0bf528SMauro Carvalho Chehab 5109a0bf528SMauro Carvalho Chehab err: 5116a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 5129a0bf528SMauro Carvalho Chehab 5139a0bf528SMauro Carvalho Chehab return ret; 5149a0bf528SMauro Carvalho Chehab } 5159a0bf528SMauro Carvalho Chehab 5169a0bf528SMauro Carvalho Chehab static int af9033_get_tune_settings(struct dvb_frontend *fe, 5179a0bf528SMauro Carvalho Chehab struct dvb_frontend_tune_settings *fesettings) 5189a0bf528SMauro Carvalho Chehab { 519fe8eece1SAntti Palosaari /* 800 => 2000 because IT9135 v2 is slow to gain lock */ 520fe8eece1SAntti Palosaari fesettings->min_delay_ms = 2000; 5219a0bf528SMauro Carvalho Chehab fesettings->step_size = 0; 5229a0bf528SMauro Carvalho Chehab fesettings->max_drift = 0; 5239a0bf528SMauro Carvalho Chehab 5249a0bf528SMauro Carvalho Chehab return 0; 5259a0bf528SMauro Carvalho Chehab } 5269a0bf528SMauro Carvalho Chehab 5279a0bf528SMauro Carvalho Chehab static int af9033_set_frontend(struct dvb_frontend *fe) 5289a0bf528SMauro Carvalho Chehab { 52909611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 5309a0bf528SMauro Carvalho Chehab struct dtv_frontend_properties *c = &fe->dtv_property_cache; 531182b967eSHans-Frieder Vogt int ret, i, spec_inv, sampling_freq; 5329a0bf528SMauro Carvalho Chehab u8 tmp, buf[3], bandwidth_reg_val; 5339a0bf528SMauro Carvalho Chehab u32 if_frequency, freq_cw, adc_freq; 5349a0bf528SMauro Carvalho Chehab 5356a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "frequency=%d bandwidth_hz=%d\n", 5366a087f1fSAntti Palosaari c->frequency, c->bandwidth_hz); 5379a0bf528SMauro Carvalho Chehab 5389a0bf528SMauro Carvalho Chehab /* check bandwidth */ 5399a0bf528SMauro Carvalho Chehab switch (c->bandwidth_hz) { 5409a0bf528SMauro Carvalho Chehab case 6000000: 5419a0bf528SMauro Carvalho Chehab bandwidth_reg_val = 0x00; 5429a0bf528SMauro Carvalho Chehab break; 5439a0bf528SMauro Carvalho Chehab case 7000000: 5449a0bf528SMauro Carvalho Chehab bandwidth_reg_val = 0x01; 5459a0bf528SMauro Carvalho Chehab break; 5469a0bf528SMauro Carvalho Chehab case 8000000: 5479a0bf528SMauro Carvalho Chehab bandwidth_reg_val = 0x02; 5489a0bf528SMauro Carvalho Chehab break; 5499a0bf528SMauro Carvalho Chehab default: 5506a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "invalid bandwidth_hz\n"); 5519a0bf528SMauro Carvalho Chehab ret = -EINVAL; 5529a0bf528SMauro Carvalho Chehab goto err; 5539a0bf528SMauro Carvalho Chehab } 5549a0bf528SMauro Carvalho Chehab 5559a0bf528SMauro Carvalho Chehab /* program tuner */ 5569a0bf528SMauro Carvalho Chehab if (fe->ops.tuner_ops.set_params) 5579a0bf528SMauro Carvalho Chehab fe->ops.tuner_ops.set_params(fe); 5589a0bf528SMauro Carvalho Chehab 5599a0bf528SMauro Carvalho Chehab /* program CFOE coefficients */ 56009611caaSAntti Palosaari if (c->bandwidth_hz != dev->bandwidth_hz) { 5619a0bf528SMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) { 56209611caaSAntti Palosaari if (coeff_lut[i].clock == dev->cfg.clock && 5639a0bf528SMauro Carvalho Chehab coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { 5649a0bf528SMauro Carvalho Chehab break; 5659a0bf528SMauro Carvalho Chehab } 5669a0bf528SMauro Carvalho Chehab } 56709611caaSAntti Palosaari ret = af9033_wr_regs(dev, 0x800001, 5689a0bf528SMauro Carvalho Chehab coeff_lut[i].val, sizeof(coeff_lut[i].val)); 5699a0bf528SMauro Carvalho Chehab } 5709a0bf528SMauro Carvalho Chehab 5719a0bf528SMauro Carvalho Chehab /* program frequency control */ 57209611caaSAntti Palosaari if (c->bandwidth_hz != dev->bandwidth_hz) { 57309611caaSAntti Palosaari spec_inv = dev->cfg.spec_inv ? -1 : 1; 5749a0bf528SMauro Carvalho Chehab 5759a0bf528SMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { 57609611caaSAntti Palosaari if (clock_adc_lut[i].clock == dev->cfg.clock) 5779a0bf528SMauro Carvalho Chehab break; 5789a0bf528SMauro Carvalho Chehab } 5799a0bf528SMauro Carvalho Chehab adc_freq = clock_adc_lut[i].adc; 5809a0bf528SMauro Carvalho Chehab 5819a0bf528SMauro Carvalho Chehab /* get used IF frequency */ 5829a0bf528SMauro Carvalho Chehab if (fe->ops.tuner_ops.get_if_frequency) 5839a0bf528SMauro Carvalho Chehab fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); 5849a0bf528SMauro Carvalho Chehab else 5859a0bf528SMauro Carvalho Chehab if_frequency = 0; 5869a0bf528SMauro Carvalho Chehab 587182b967eSHans-Frieder Vogt sampling_freq = if_frequency; 5889a0bf528SMauro Carvalho Chehab 589182b967eSHans-Frieder Vogt while (sampling_freq > (adc_freq / 2)) 590182b967eSHans-Frieder Vogt sampling_freq -= adc_freq; 591182b967eSHans-Frieder Vogt 592182b967eSHans-Frieder Vogt if (sampling_freq >= 0) 5939a0bf528SMauro Carvalho Chehab spec_inv *= -1; 5949a0bf528SMauro Carvalho Chehab else 595182b967eSHans-Frieder Vogt sampling_freq *= -1; 5969a0bf528SMauro Carvalho Chehab 59709611caaSAntti Palosaari freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul); 5989a0bf528SMauro Carvalho Chehab 5999a0bf528SMauro Carvalho Chehab if (spec_inv == -1) 600182b967eSHans-Frieder Vogt freq_cw = 0x800000 - freq_cw; 6019a0bf528SMauro Carvalho Chehab 60209611caaSAntti Palosaari if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X) 6039a0bf528SMauro Carvalho Chehab freq_cw /= 2; 6049a0bf528SMauro Carvalho Chehab 6059a0bf528SMauro Carvalho Chehab buf[0] = (freq_cw >> 0) & 0xff; 6069a0bf528SMauro Carvalho Chehab buf[1] = (freq_cw >> 8) & 0xff; 6079a0bf528SMauro Carvalho Chehab buf[2] = (freq_cw >> 16) & 0x7f; 608fe8eece1SAntti Palosaari 609fe8eece1SAntti Palosaari /* FIXME: there seems to be calculation error here... */ 610fe8eece1SAntti Palosaari if (if_frequency == 0) 611fe8eece1SAntti Palosaari buf[2] = 0; 612fe8eece1SAntti Palosaari 61309611caaSAntti Palosaari ret = af9033_wr_regs(dev, 0x800029, buf, 3); 6149a0bf528SMauro Carvalho Chehab if (ret < 0) 6159a0bf528SMauro Carvalho Chehab goto err; 6169a0bf528SMauro Carvalho Chehab 61709611caaSAntti Palosaari dev->bandwidth_hz = c->bandwidth_hz; 6189a0bf528SMauro Carvalho Chehab } 6199a0bf528SMauro Carvalho Chehab 62009611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03); 6219a0bf528SMauro Carvalho Chehab if (ret < 0) 6229a0bf528SMauro Carvalho Chehab goto err; 6239a0bf528SMauro Carvalho Chehab 62409611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x800040, 0x00); 6259a0bf528SMauro Carvalho Chehab if (ret < 0) 6269a0bf528SMauro Carvalho Chehab goto err; 6279a0bf528SMauro Carvalho Chehab 62809611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x800047, 0x00); 6299a0bf528SMauro Carvalho Chehab if (ret < 0) 6309a0bf528SMauro Carvalho Chehab goto err; 6319a0bf528SMauro Carvalho Chehab 63209611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f999, 0x00, 0x01); 6339a0bf528SMauro Carvalho Chehab if (ret < 0) 6349a0bf528SMauro Carvalho Chehab goto err; 6359a0bf528SMauro Carvalho Chehab 6369a0bf528SMauro Carvalho Chehab if (c->frequency <= 230000000) 6379a0bf528SMauro Carvalho Chehab tmp = 0x00; /* VHF */ 6389a0bf528SMauro Carvalho Chehab else 6399a0bf528SMauro Carvalho Chehab tmp = 0x01; /* UHF */ 6409a0bf528SMauro Carvalho Chehab 64109611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x80004b, tmp); 6429a0bf528SMauro Carvalho Chehab if (ret < 0) 6439a0bf528SMauro Carvalho Chehab goto err; 6449a0bf528SMauro Carvalho Chehab 64509611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x800000, 0x00); 6469a0bf528SMauro Carvalho Chehab if (ret < 0) 6479a0bf528SMauro Carvalho Chehab goto err; 6489a0bf528SMauro Carvalho Chehab 6499a0bf528SMauro Carvalho Chehab return 0; 6509a0bf528SMauro Carvalho Chehab 6519a0bf528SMauro Carvalho Chehab err: 6526a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 6539a0bf528SMauro Carvalho Chehab 6549a0bf528SMauro Carvalho Chehab return ret; 6559a0bf528SMauro Carvalho Chehab } 6569a0bf528SMauro Carvalho Chehab 6579a0bf528SMauro Carvalho Chehab static int af9033_get_frontend(struct dvb_frontend *fe) 6589a0bf528SMauro Carvalho Chehab { 65909611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 6609a0bf528SMauro Carvalho Chehab struct dtv_frontend_properties *c = &fe->dtv_property_cache; 6619a0bf528SMauro Carvalho Chehab int ret; 6629a0bf528SMauro Carvalho Chehab u8 buf[8]; 6639a0bf528SMauro Carvalho Chehab 6646a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "\n"); 6659a0bf528SMauro Carvalho Chehab 6669a0bf528SMauro Carvalho Chehab /* read all needed registers */ 66709611caaSAntti Palosaari ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf)); 6689a0bf528SMauro Carvalho Chehab if (ret < 0) 6699a0bf528SMauro Carvalho Chehab goto err; 6709a0bf528SMauro Carvalho Chehab 6719a0bf528SMauro Carvalho Chehab switch ((buf[0] >> 0) & 3) { 6729a0bf528SMauro Carvalho Chehab case 0: 6739a0bf528SMauro Carvalho Chehab c->transmission_mode = TRANSMISSION_MODE_2K; 6749a0bf528SMauro Carvalho Chehab break; 6759a0bf528SMauro Carvalho Chehab case 1: 6769a0bf528SMauro Carvalho Chehab c->transmission_mode = TRANSMISSION_MODE_8K; 6779a0bf528SMauro Carvalho Chehab break; 6789a0bf528SMauro Carvalho Chehab } 6799a0bf528SMauro Carvalho Chehab 6809a0bf528SMauro Carvalho Chehab switch ((buf[1] >> 0) & 3) { 6819a0bf528SMauro Carvalho Chehab case 0: 6829a0bf528SMauro Carvalho Chehab c->guard_interval = GUARD_INTERVAL_1_32; 6839a0bf528SMauro Carvalho Chehab break; 6849a0bf528SMauro Carvalho Chehab case 1: 6859a0bf528SMauro Carvalho Chehab c->guard_interval = GUARD_INTERVAL_1_16; 6869a0bf528SMauro Carvalho Chehab break; 6879a0bf528SMauro Carvalho Chehab case 2: 6889a0bf528SMauro Carvalho Chehab c->guard_interval = GUARD_INTERVAL_1_8; 6899a0bf528SMauro Carvalho Chehab break; 6909a0bf528SMauro Carvalho Chehab case 3: 6919a0bf528SMauro Carvalho Chehab c->guard_interval = GUARD_INTERVAL_1_4; 6929a0bf528SMauro Carvalho Chehab break; 6939a0bf528SMauro Carvalho Chehab } 6949a0bf528SMauro Carvalho Chehab 6959a0bf528SMauro Carvalho Chehab switch ((buf[2] >> 0) & 7) { 6969a0bf528SMauro Carvalho Chehab case 0: 6979a0bf528SMauro Carvalho Chehab c->hierarchy = HIERARCHY_NONE; 6989a0bf528SMauro Carvalho Chehab break; 6999a0bf528SMauro Carvalho Chehab case 1: 7009a0bf528SMauro Carvalho Chehab c->hierarchy = HIERARCHY_1; 7019a0bf528SMauro Carvalho Chehab break; 7029a0bf528SMauro Carvalho Chehab case 2: 7039a0bf528SMauro Carvalho Chehab c->hierarchy = HIERARCHY_2; 7049a0bf528SMauro Carvalho Chehab break; 7059a0bf528SMauro Carvalho Chehab case 3: 7069a0bf528SMauro Carvalho Chehab c->hierarchy = HIERARCHY_4; 7079a0bf528SMauro Carvalho Chehab break; 7089a0bf528SMauro Carvalho Chehab } 7099a0bf528SMauro Carvalho Chehab 7109a0bf528SMauro Carvalho Chehab switch ((buf[3] >> 0) & 3) { 7119a0bf528SMauro Carvalho Chehab case 0: 7129a0bf528SMauro Carvalho Chehab c->modulation = QPSK; 7139a0bf528SMauro Carvalho Chehab break; 7149a0bf528SMauro Carvalho Chehab case 1: 7159a0bf528SMauro Carvalho Chehab c->modulation = QAM_16; 7169a0bf528SMauro Carvalho Chehab break; 7179a0bf528SMauro Carvalho Chehab case 2: 7189a0bf528SMauro Carvalho Chehab c->modulation = QAM_64; 7199a0bf528SMauro Carvalho Chehab break; 7209a0bf528SMauro Carvalho Chehab } 7219a0bf528SMauro Carvalho Chehab 7229a0bf528SMauro Carvalho Chehab switch ((buf[4] >> 0) & 3) { 7239a0bf528SMauro Carvalho Chehab case 0: 7249a0bf528SMauro Carvalho Chehab c->bandwidth_hz = 6000000; 7259a0bf528SMauro Carvalho Chehab break; 7269a0bf528SMauro Carvalho Chehab case 1: 7279a0bf528SMauro Carvalho Chehab c->bandwidth_hz = 7000000; 7289a0bf528SMauro Carvalho Chehab break; 7299a0bf528SMauro Carvalho Chehab case 2: 7309a0bf528SMauro Carvalho Chehab c->bandwidth_hz = 8000000; 7319a0bf528SMauro Carvalho Chehab break; 7329a0bf528SMauro Carvalho Chehab } 7339a0bf528SMauro Carvalho Chehab 7349a0bf528SMauro Carvalho Chehab switch ((buf[6] >> 0) & 7) { 7359a0bf528SMauro Carvalho Chehab case 0: 7369a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_1_2; 7379a0bf528SMauro Carvalho Chehab break; 7389a0bf528SMauro Carvalho Chehab case 1: 7399a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_2_3; 7409a0bf528SMauro Carvalho Chehab break; 7419a0bf528SMauro Carvalho Chehab case 2: 7429a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_3_4; 7439a0bf528SMauro Carvalho Chehab break; 7449a0bf528SMauro Carvalho Chehab case 3: 7459a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_5_6; 7469a0bf528SMauro Carvalho Chehab break; 7479a0bf528SMauro Carvalho Chehab case 4: 7489a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_7_8; 7499a0bf528SMauro Carvalho Chehab break; 7509a0bf528SMauro Carvalho Chehab case 5: 7519a0bf528SMauro Carvalho Chehab c->code_rate_HP = FEC_NONE; 7529a0bf528SMauro Carvalho Chehab break; 7539a0bf528SMauro Carvalho Chehab } 7549a0bf528SMauro Carvalho Chehab 7559a0bf528SMauro Carvalho Chehab switch ((buf[7] >> 0) & 7) { 7569a0bf528SMauro Carvalho Chehab case 0: 7579a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_1_2; 7589a0bf528SMauro Carvalho Chehab break; 7599a0bf528SMauro Carvalho Chehab case 1: 7609a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_2_3; 7619a0bf528SMauro Carvalho Chehab break; 7629a0bf528SMauro Carvalho Chehab case 2: 7639a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_3_4; 7649a0bf528SMauro Carvalho Chehab break; 7659a0bf528SMauro Carvalho Chehab case 3: 7669a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_5_6; 7679a0bf528SMauro Carvalho Chehab break; 7689a0bf528SMauro Carvalho Chehab case 4: 7699a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_7_8; 7709a0bf528SMauro Carvalho Chehab break; 7719a0bf528SMauro Carvalho Chehab case 5: 7729a0bf528SMauro Carvalho Chehab c->code_rate_LP = FEC_NONE; 7739a0bf528SMauro Carvalho Chehab break; 7749a0bf528SMauro Carvalho Chehab } 7759a0bf528SMauro Carvalho Chehab 7769a0bf528SMauro Carvalho Chehab return 0; 7779a0bf528SMauro Carvalho Chehab 7789a0bf528SMauro Carvalho Chehab err: 7796a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 7809a0bf528SMauro Carvalho Chehab 7819a0bf528SMauro Carvalho Chehab return ret; 7829a0bf528SMauro Carvalho Chehab } 7839a0bf528SMauro Carvalho Chehab 7849a0bf528SMauro Carvalho Chehab static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status) 7859a0bf528SMauro Carvalho Chehab { 78609611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 7879a0bf528SMauro Carvalho Chehab int ret; 7889a0bf528SMauro Carvalho Chehab u8 tmp; 7899a0bf528SMauro Carvalho Chehab 7909a0bf528SMauro Carvalho Chehab *status = 0; 7919a0bf528SMauro Carvalho Chehab 7929a0bf528SMauro Carvalho Chehab /* radio channel status, 0=no result, 1=has signal, 2=no signal */ 79309611caaSAntti Palosaari ret = af9033_rd_reg(dev, 0x800047, &tmp); 7949a0bf528SMauro Carvalho Chehab if (ret < 0) 7959a0bf528SMauro Carvalho Chehab goto err; 7969a0bf528SMauro Carvalho Chehab 7979a0bf528SMauro Carvalho Chehab /* has signal */ 7989a0bf528SMauro Carvalho Chehab if (tmp == 0x01) 7999a0bf528SMauro Carvalho Chehab *status |= FE_HAS_SIGNAL; 8009a0bf528SMauro Carvalho Chehab 8019a0bf528SMauro Carvalho Chehab if (tmp != 0x02) { 8029a0bf528SMauro Carvalho Chehab /* TPS lock */ 80309611caaSAntti Palosaari ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01); 8049a0bf528SMauro Carvalho Chehab if (ret < 0) 8059a0bf528SMauro Carvalho Chehab goto err; 8069a0bf528SMauro Carvalho Chehab 8079a0bf528SMauro Carvalho Chehab if (tmp) 8089a0bf528SMauro Carvalho Chehab *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | 8099a0bf528SMauro Carvalho Chehab FE_HAS_VITERBI; 8109a0bf528SMauro Carvalho Chehab 8119a0bf528SMauro Carvalho Chehab /* full lock */ 81209611caaSAntti Palosaari ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01); 8139a0bf528SMauro Carvalho Chehab if (ret < 0) 8149a0bf528SMauro Carvalho Chehab goto err; 8159a0bf528SMauro Carvalho Chehab 8169a0bf528SMauro Carvalho Chehab if (tmp) 8179a0bf528SMauro Carvalho Chehab *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | 8189a0bf528SMauro Carvalho Chehab FE_HAS_VITERBI | FE_HAS_SYNC | 8199a0bf528SMauro Carvalho Chehab FE_HAS_LOCK; 8209a0bf528SMauro Carvalho Chehab } 8219a0bf528SMauro Carvalho Chehab 82283f11619SAntti Palosaari dev->fe_status = *status; 82383f11619SAntti Palosaari 8249a0bf528SMauro Carvalho Chehab return 0; 8259a0bf528SMauro Carvalho Chehab 8269a0bf528SMauro Carvalho Chehab err: 8276a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 8289a0bf528SMauro Carvalho Chehab 8299a0bf528SMauro Carvalho Chehab return ret; 8309a0bf528SMauro Carvalho Chehab } 8319a0bf528SMauro Carvalho Chehab 8329a0bf528SMauro Carvalho Chehab static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr) 8339a0bf528SMauro Carvalho Chehab { 83409611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 8359a0bf528SMauro Carvalho Chehab int ret, i, len; 8369a0bf528SMauro Carvalho Chehab u8 buf[3], tmp; 8379a0bf528SMauro Carvalho Chehab u32 snr_val; 83824e419a0SAntti Palosaari const struct val_snr *snr_lut; 8399a0bf528SMauro Carvalho Chehab 8409a0bf528SMauro Carvalho Chehab /* read value */ 84109611caaSAntti Palosaari ret = af9033_rd_regs(dev, 0x80002c, buf, 3); 8429a0bf528SMauro Carvalho Chehab if (ret < 0) 8439a0bf528SMauro Carvalho Chehab goto err; 8449a0bf528SMauro Carvalho Chehab 8459a0bf528SMauro Carvalho Chehab snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0]; 8469a0bf528SMauro Carvalho Chehab 8479a0bf528SMauro Carvalho Chehab /* read current modulation */ 84809611caaSAntti Palosaari ret = af9033_rd_reg(dev, 0x80f903, &tmp); 8499a0bf528SMauro Carvalho Chehab if (ret < 0) 8509a0bf528SMauro Carvalho Chehab goto err; 8519a0bf528SMauro Carvalho Chehab 8529a0bf528SMauro Carvalho Chehab switch ((tmp >> 0) & 3) { 8539a0bf528SMauro Carvalho Chehab case 0: 8549a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(qpsk_snr_lut); 8559a0bf528SMauro Carvalho Chehab snr_lut = qpsk_snr_lut; 8569a0bf528SMauro Carvalho Chehab break; 8579a0bf528SMauro Carvalho Chehab case 1: 8589a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(qam16_snr_lut); 8599a0bf528SMauro Carvalho Chehab snr_lut = qam16_snr_lut; 8609a0bf528SMauro Carvalho Chehab break; 8619a0bf528SMauro Carvalho Chehab case 2: 8629a0bf528SMauro Carvalho Chehab len = ARRAY_SIZE(qam64_snr_lut); 8639a0bf528SMauro Carvalho Chehab snr_lut = qam64_snr_lut; 8649a0bf528SMauro Carvalho Chehab break; 8659a0bf528SMauro Carvalho Chehab default: 8669a0bf528SMauro Carvalho Chehab goto err; 8679a0bf528SMauro Carvalho Chehab } 8689a0bf528SMauro Carvalho Chehab 8699a0bf528SMauro Carvalho Chehab for (i = 0; i < len; i++) { 8709a0bf528SMauro Carvalho Chehab tmp = snr_lut[i].snr; 8719a0bf528SMauro Carvalho Chehab 8729a0bf528SMauro Carvalho Chehab if (snr_val < snr_lut[i].val) 8739a0bf528SMauro Carvalho Chehab break; 8749a0bf528SMauro Carvalho Chehab } 8759a0bf528SMauro Carvalho Chehab 8769a0bf528SMauro Carvalho Chehab *snr = tmp * 10; /* dB/10 */ 8779a0bf528SMauro Carvalho Chehab 8789a0bf528SMauro Carvalho Chehab return 0; 8799a0bf528SMauro Carvalho Chehab 8809a0bf528SMauro Carvalho Chehab err: 8816a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 8829a0bf528SMauro Carvalho Chehab 8839a0bf528SMauro Carvalho Chehab return ret; 8849a0bf528SMauro Carvalho Chehab } 8859a0bf528SMauro Carvalho Chehab 8869a0bf528SMauro Carvalho Chehab static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength) 8879a0bf528SMauro Carvalho Chehab { 88809611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 8899a0bf528SMauro Carvalho Chehab int ret; 8909a0bf528SMauro Carvalho Chehab u8 strength2; 8919a0bf528SMauro Carvalho Chehab 8929a0bf528SMauro Carvalho Chehab /* read signal strength of 0-100 scale */ 89309611caaSAntti Palosaari ret = af9033_rd_reg(dev, 0x800048, &strength2); 8949a0bf528SMauro Carvalho Chehab if (ret < 0) 8959a0bf528SMauro Carvalho Chehab goto err; 8969a0bf528SMauro Carvalho Chehab 8979a0bf528SMauro Carvalho Chehab /* scale value to 0x0000-0xffff */ 8989a0bf528SMauro Carvalho Chehab *strength = strength2 * 0xffff / 100; 8999a0bf528SMauro Carvalho Chehab 9009a0bf528SMauro Carvalho Chehab return 0; 9019a0bf528SMauro Carvalho Chehab 9029a0bf528SMauro Carvalho Chehab err: 9036a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 9049a0bf528SMauro Carvalho Chehab 9059a0bf528SMauro Carvalho Chehab return ret; 9069a0bf528SMauro Carvalho Chehab } 9079a0bf528SMauro Carvalho Chehab 90809611caaSAntti Palosaari static int af9033_update_ch_stat(struct af9033_dev *dev) 9099a0bf528SMauro Carvalho Chehab { 9109a0bf528SMauro Carvalho Chehab int ret = 0; 9119a0bf528SMauro Carvalho Chehab u32 err_cnt, bit_cnt; 9129a0bf528SMauro Carvalho Chehab u16 abort_cnt; 9139a0bf528SMauro Carvalho Chehab u8 buf[7]; 9149a0bf528SMauro Carvalho Chehab 9159a0bf528SMauro Carvalho Chehab /* only update data every half second */ 91609611caaSAntti Palosaari if (time_after(jiffies, dev->last_stat_check + msecs_to_jiffies(500))) { 91709611caaSAntti Palosaari ret = af9033_rd_regs(dev, 0x800032, buf, sizeof(buf)); 9189a0bf528SMauro Carvalho Chehab if (ret < 0) 9199a0bf528SMauro Carvalho Chehab goto err; 9209a0bf528SMauro Carvalho Chehab /* in 8 byte packets? */ 9219a0bf528SMauro Carvalho Chehab abort_cnt = (buf[1] << 8) + buf[0]; 9229a0bf528SMauro Carvalho Chehab /* in bits */ 9239a0bf528SMauro Carvalho Chehab err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2]; 9249a0bf528SMauro Carvalho Chehab /* in 8 byte packets? always(?) 0x2710 = 10000 */ 9259a0bf528SMauro Carvalho Chehab bit_cnt = (buf[6] << 8) + buf[5]; 9269a0bf528SMauro Carvalho Chehab 9279a0bf528SMauro Carvalho Chehab if (bit_cnt < abort_cnt) { 9289a0bf528SMauro Carvalho Chehab abort_cnt = 1000; 92909611caaSAntti Palosaari dev->ber = 0xffffffff; 9309a0bf528SMauro Carvalho Chehab } else { 93124e419a0SAntti Palosaari /* 93224e419a0SAntti Palosaari * 8 byte packets, that have not been rejected already 93324e419a0SAntti Palosaari */ 9349a0bf528SMauro Carvalho Chehab bit_cnt -= (u32)abort_cnt; 9359a0bf528SMauro Carvalho Chehab if (bit_cnt == 0) { 93609611caaSAntti Palosaari dev->ber = 0xffffffff; 9379a0bf528SMauro Carvalho Chehab } else { 9389a0bf528SMauro Carvalho Chehab err_cnt -= (u32)abort_cnt * 8 * 8; 9399a0bf528SMauro Carvalho Chehab bit_cnt *= 8 * 8; 94009611caaSAntti Palosaari dev->ber = err_cnt * (0xffffffff / bit_cnt); 9419a0bf528SMauro Carvalho Chehab } 9429a0bf528SMauro Carvalho Chehab } 94309611caaSAntti Palosaari dev->ucb += abort_cnt; 94409611caaSAntti Palosaari dev->last_stat_check = jiffies; 9459a0bf528SMauro Carvalho Chehab } 9469a0bf528SMauro Carvalho Chehab 9479a0bf528SMauro Carvalho Chehab return 0; 9489a0bf528SMauro Carvalho Chehab err: 9496a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 9500a73f2d6SAntti Palosaari 9519a0bf528SMauro Carvalho Chehab return ret; 9529a0bf528SMauro Carvalho Chehab } 9539a0bf528SMauro Carvalho Chehab 9549a0bf528SMauro Carvalho Chehab static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber) 9559a0bf528SMauro Carvalho Chehab { 95609611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 9579a0bf528SMauro Carvalho Chehab int ret; 9589a0bf528SMauro Carvalho Chehab 95909611caaSAntti Palosaari ret = af9033_update_ch_stat(dev); 9609a0bf528SMauro Carvalho Chehab if (ret < 0) 9619a0bf528SMauro Carvalho Chehab return ret; 9629a0bf528SMauro Carvalho Chehab 96309611caaSAntti Palosaari *ber = dev->ber; 9649a0bf528SMauro Carvalho Chehab 9659a0bf528SMauro Carvalho Chehab return 0; 9669a0bf528SMauro Carvalho Chehab } 9679a0bf528SMauro Carvalho Chehab 9689a0bf528SMauro Carvalho Chehab static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) 9699a0bf528SMauro Carvalho Chehab { 97009611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 9719a0bf528SMauro Carvalho Chehab int ret; 9729a0bf528SMauro Carvalho Chehab 97309611caaSAntti Palosaari ret = af9033_update_ch_stat(dev); 9749a0bf528SMauro Carvalho Chehab if (ret < 0) 9759a0bf528SMauro Carvalho Chehab return ret; 9769a0bf528SMauro Carvalho Chehab 97709611caaSAntti Palosaari *ucblocks = dev->ucb; 9789a0bf528SMauro Carvalho Chehab 9799a0bf528SMauro Carvalho Chehab return 0; 9809a0bf528SMauro Carvalho Chehab } 9819a0bf528SMauro Carvalho Chehab 9829a0bf528SMauro Carvalho Chehab static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) 9839a0bf528SMauro Carvalho Chehab { 98409611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 9859a0bf528SMauro Carvalho Chehab int ret; 9869a0bf528SMauro Carvalho Chehab 9876a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "enable=%d\n", enable); 9889a0bf528SMauro Carvalho Chehab 98909611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01); 9909a0bf528SMauro Carvalho Chehab if (ret < 0) 9919a0bf528SMauro Carvalho Chehab goto err; 9929a0bf528SMauro Carvalho Chehab 9939a0bf528SMauro Carvalho Chehab return 0; 9949a0bf528SMauro Carvalho Chehab 9959a0bf528SMauro Carvalho Chehab err: 9966a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 9979a0bf528SMauro Carvalho Chehab 9989a0bf528SMauro Carvalho Chehab return ret; 9999a0bf528SMauro Carvalho Chehab } 10009a0bf528SMauro Carvalho Chehab 1001ed97a6feSMauro Carvalho Chehab static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff) 1002040cf86cSAntti Palosaari { 100309611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 1004040cf86cSAntti Palosaari int ret; 1005040cf86cSAntti Palosaari 10066a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "onoff=%d\n", onoff); 1007040cf86cSAntti Palosaari 100809611caaSAntti Palosaari ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01); 1009040cf86cSAntti Palosaari if (ret < 0) 1010040cf86cSAntti Palosaari goto err; 1011040cf86cSAntti Palosaari 1012040cf86cSAntti Palosaari return 0; 1013040cf86cSAntti Palosaari 1014040cf86cSAntti Palosaari err: 10156a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 1016040cf86cSAntti Palosaari 1017040cf86cSAntti Palosaari return ret; 1018040cf86cSAntti Palosaari } 1019040cf86cSAntti Palosaari 102024e419a0SAntti Palosaari static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid, 102124e419a0SAntti Palosaari int onoff) 1022040cf86cSAntti Palosaari { 102309611caaSAntti Palosaari struct af9033_dev *dev = fe->demodulator_priv; 1024040cf86cSAntti Palosaari int ret; 1025040cf86cSAntti Palosaari u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff}; 1026040cf86cSAntti Palosaari 10276a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "index=%d pid=%04x onoff=%d\n", 10286a087f1fSAntti Palosaari index, pid, onoff); 1029040cf86cSAntti Palosaari 1030040cf86cSAntti Palosaari if (pid > 0x1fff) 1031040cf86cSAntti Palosaari return 0; 1032040cf86cSAntti Palosaari 103309611caaSAntti Palosaari ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2); 1034040cf86cSAntti Palosaari if (ret < 0) 1035040cf86cSAntti Palosaari goto err; 1036040cf86cSAntti Palosaari 103709611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x80f994, onoff); 1038040cf86cSAntti Palosaari if (ret < 0) 1039040cf86cSAntti Palosaari goto err; 1040040cf86cSAntti Palosaari 104109611caaSAntti Palosaari ret = af9033_wr_reg(dev, 0x80f995, index); 1042040cf86cSAntti Palosaari if (ret < 0) 1043040cf86cSAntti Palosaari goto err; 1044040cf86cSAntti Palosaari 1045040cf86cSAntti Palosaari return 0; 1046040cf86cSAntti Palosaari 1047040cf86cSAntti Palosaari err: 10486a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 1049040cf86cSAntti Palosaari 1050040cf86cSAntti Palosaari return ret; 1051040cf86cSAntti Palosaari } 1052040cf86cSAntti Palosaari 105383f11619SAntti Palosaari static void af9033_stat_work(struct work_struct *work) 105483f11619SAntti Palosaari { 105583f11619SAntti Palosaari struct af9033_dev *dev = container_of(work, struct af9033_dev, stat_work.work); 105683f11619SAntti Palosaari struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache; 10573e41313aSAntti Palosaari int ret, tmp, i, len; 10583e41313aSAntti Palosaari u8 u8tmp, buf[3]; 105983f11619SAntti Palosaari 106083f11619SAntti Palosaari dev_dbg(&dev->client->dev, "\n"); 106183f11619SAntti Palosaari 10623e41313aSAntti Palosaari /* signal strength */ 106383f11619SAntti Palosaari if (dev->fe_status & FE_HAS_SIGNAL) { 106483f11619SAntti Palosaari if (dev->is_af9035) { 106583f11619SAntti Palosaari ret = af9033_rd_reg(dev, 0x80004a, &u8tmp); 106683f11619SAntti Palosaari tmp = -u8tmp * 1000; 106783f11619SAntti Palosaari } else { 106883f11619SAntti Palosaari ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp); 106983f11619SAntti Palosaari tmp = (u8tmp - 100) * 1000; 107083f11619SAntti Palosaari } 107183f11619SAntti Palosaari if (ret) 107283f11619SAntti Palosaari goto err; 107383f11619SAntti Palosaari 107483f11619SAntti Palosaari c->strength.len = 1; 107583f11619SAntti Palosaari c->strength.stat[0].scale = FE_SCALE_DECIBEL; 107683f11619SAntti Palosaari c->strength.stat[0].svalue = tmp; 107783f11619SAntti Palosaari } else { 107883f11619SAntti Palosaari c->strength.len = 1; 107983f11619SAntti Palosaari c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 108083f11619SAntti Palosaari } 108183f11619SAntti Palosaari 10823e41313aSAntti Palosaari /* CNR */ 10833e41313aSAntti Palosaari if (dev->fe_status & FE_HAS_VITERBI) { 10843e41313aSAntti Palosaari u32 snr_val; 10853e41313aSAntti Palosaari const struct val_snr *snr_lut; 10863e41313aSAntti Palosaari 10873e41313aSAntti Palosaari /* read value */ 10883e41313aSAntti Palosaari ret = af9033_rd_regs(dev, 0x80002c, buf, 3); 10893e41313aSAntti Palosaari if (ret) 10903e41313aSAntti Palosaari goto err; 10913e41313aSAntti Palosaari 10923e41313aSAntti Palosaari snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0); 10933e41313aSAntti Palosaari 10943e41313aSAntti Palosaari /* read current modulation */ 10953e41313aSAntti Palosaari ret = af9033_rd_reg(dev, 0x80f903, &u8tmp); 10963e41313aSAntti Palosaari if (ret) 10973e41313aSAntti Palosaari goto err; 10983e41313aSAntti Palosaari 10993e41313aSAntti Palosaari switch ((u8tmp >> 0) & 3) { 11003e41313aSAntti Palosaari case 0: 11013e41313aSAntti Palosaari len = ARRAY_SIZE(qpsk_snr_lut); 11023e41313aSAntti Palosaari snr_lut = qpsk_snr_lut; 11033e41313aSAntti Palosaari break; 11043e41313aSAntti Palosaari case 1: 11053e41313aSAntti Palosaari len = ARRAY_SIZE(qam16_snr_lut); 11063e41313aSAntti Palosaari snr_lut = qam16_snr_lut; 11073e41313aSAntti Palosaari break; 11083e41313aSAntti Palosaari case 2: 11093e41313aSAntti Palosaari len = ARRAY_SIZE(qam64_snr_lut); 11103e41313aSAntti Palosaari snr_lut = qam64_snr_lut; 11113e41313aSAntti Palosaari break; 11123e41313aSAntti Palosaari default: 11133e41313aSAntti Palosaari goto err_schedule_delayed_work; 11143e41313aSAntti Palosaari } 11153e41313aSAntti Palosaari 11163e41313aSAntti Palosaari for (i = 0; i < len; i++) { 11173e41313aSAntti Palosaari tmp = snr_lut[i].snr * 1000; 11183e41313aSAntti Palosaari if (snr_val < snr_lut[i].val) 11193e41313aSAntti Palosaari break; 11203e41313aSAntti Palosaari } 11213e41313aSAntti Palosaari 11223e41313aSAntti Palosaari c->cnr.len = 1; 11233e41313aSAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_DECIBEL; 11243e41313aSAntti Palosaari c->cnr.stat[0].svalue = tmp; 11253e41313aSAntti Palosaari } else { 11263e41313aSAntti Palosaari c->cnr.len = 1; 11273e41313aSAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 11283e41313aSAntti Palosaari } 11293e41313aSAntti Palosaari 11303e41313aSAntti Palosaari err_schedule_delayed_work: 113183f11619SAntti Palosaari schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000)); 113283f11619SAntti Palosaari return; 113383f11619SAntti Palosaari err: 113483f11619SAntti Palosaari dev_dbg(&dev->client->dev, "failed=%d\n", ret); 113583f11619SAntti Palosaari } 113683f11619SAntti Palosaari 11379a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops af9033_ops = { 11389a0bf528SMauro Carvalho Chehab .delsys = { SYS_DVBT }, 11399a0bf528SMauro Carvalho Chehab .info = { 11409a0bf528SMauro Carvalho Chehab .name = "Afatech AF9033 (DVB-T)", 11419a0bf528SMauro Carvalho Chehab .frequency_min = 174000000, 11429a0bf528SMauro Carvalho Chehab .frequency_max = 862000000, 11439a0bf528SMauro Carvalho Chehab .frequency_stepsize = 250000, 11449a0bf528SMauro Carvalho Chehab .frequency_tolerance = 0, 11459a0bf528SMauro Carvalho Chehab .caps = FE_CAN_FEC_1_2 | 11469a0bf528SMauro Carvalho Chehab FE_CAN_FEC_2_3 | 11479a0bf528SMauro Carvalho Chehab FE_CAN_FEC_3_4 | 11489a0bf528SMauro Carvalho Chehab FE_CAN_FEC_5_6 | 11499a0bf528SMauro Carvalho Chehab FE_CAN_FEC_7_8 | 11509a0bf528SMauro Carvalho Chehab FE_CAN_FEC_AUTO | 11519a0bf528SMauro Carvalho Chehab FE_CAN_QPSK | 11529a0bf528SMauro Carvalho Chehab FE_CAN_QAM_16 | 11539a0bf528SMauro Carvalho Chehab FE_CAN_QAM_64 | 11549a0bf528SMauro Carvalho Chehab FE_CAN_QAM_AUTO | 11559a0bf528SMauro Carvalho Chehab FE_CAN_TRANSMISSION_MODE_AUTO | 11569a0bf528SMauro Carvalho Chehab FE_CAN_GUARD_INTERVAL_AUTO | 11579a0bf528SMauro Carvalho Chehab FE_CAN_HIERARCHY_AUTO | 11589a0bf528SMauro Carvalho Chehab FE_CAN_RECOVER | 11599a0bf528SMauro Carvalho Chehab FE_CAN_MUTE_TS 11609a0bf528SMauro Carvalho Chehab }, 11619a0bf528SMauro Carvalho Chehab 11629a0bf528SMauro Carvalho Chehab .init = af9033_init, 11639a0bf528SMauro Carvalho Chehab .sleep = af9033_sleep, 11649a0bf528SMauro Carvalho Chehab 11659a0bf528SMauro Carvalho Chehab .get_tune_settings = af9033_get_tune_settings, 11669a0bf528SMauro Carvalho Chehab .set_frontend = af9033_set_frontend, 11679a0bf528SMauro Carvalho Chehab .get_frontend = af9033_get_frontend, 11689a0bf528SMauro Carvalho Chehab 11699a0bf528SMauro Carvalho Chehab .read_status = af9033_read_status, 11709a0bf528SMauro Carvalho Chehab .read_snr = af9033_read_snr, 11719a0bf528SMauro Carvalho Chehab .read_signal_strength = af9033_read_signal_strength, 11729a0bf528SMauro Carvalho Chehab .read_ber = af9033_read_ber, 11739a0bf528SMauro Carvalho Chehab .read_ucblocks = af9033_read_ucblocks, 11749a0bf528SMauro Carvalho Chehab 11759a0bf528SMauro Carvalho Chehab .i2c_gate_ctrl = af9033_i2c_gate_ctrl, 11769a0bf528SMauro Carvalho Chehab }; 11779a0bf528SMauro Carvalho Chehab 1178f5b00a76SAntti Palosaari static int af9033_probe(struct i2c_client *client, 1179f5b00a76SAntti Palosaari const struct i2c_device_id *id) 1180f5b00a76SAntti Palosaari { 1181f5b00a76SAntti Palosaari struct af9033_config *cfg = client->dev.platform_data; 1182f5b00a76SAntti Palosaari struct af9033_dev *dev; 1183f5b00a76SAntti Palosaari int ret; 1184f5b00a76SAntti Palosaari u8 buf[8]; 1185f5b00a76SAntti Palosaari u32 reg; 1186f5b00a76SAntti Palosaari 1187f5b00a76SAntti Palosaari /* allocate memory for the internal state */ 1188f5b00a76SAntti Palosaari dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL); 1189f5b00a76SAntti Palosaari if (dev == NULL) { 1190f5b00a76SAntti Palosaari ret = -ENOMEM; 1191f5b00a76SAntti Palosaari dev_err(&client->dev, "Could not allocate memory for state\n"); 1192f5b00a76SAntti Palosaari goto err; 1193f5b00a76SAntti Palosaari } 1194f5b00a76SAntti Palosaari 1195f5b00a76SAntti Palosaari /* setup the state */ 1196f5b00a76SAntti Palosaari dev->client = client; 119783f11619SAntti Palosaari INIT_DELAYED_WORK(&dev->stat_work, af9033_stat_work); 1198f5b00a76SAntti Palosaari memcpy(&dev->cfg, cfg, sizeof(struct af9033_config)); 1199f5b00a76SAntti Palosaari 1200f5b00a76SAntti Palosaari if (dev->cfg.clock != 12000000) { 1201f5b00a76SAntti Palosaari ret = -ENODEV; 1202f5b00a76SAntti Palosaari dev_err(&dev->client->dev, 12036a087f1fSAntti Palosaari "unsupported clock %d Hz, only 12000000 Hz is supported currently\n", 12046a087f1fSAntti Palosaari dev->cfg.clock); 1205f5b00a76SAntti Palosaari goto err_kfree; 1206f5b00a76SAntti Palosaari } 1207f5b00a76SAntti Palosaari 1208f5b00a76SAntti Palosaari /* firmware version */ 1209f5b00a76SAntti Palosaari switch (dev->cfg.tuner) { 1210f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_38: 1211f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_51: 1212f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_52: 1213f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_60: 1214f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_61: 1215f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_62: 121683f11619SAntti Palosaari dev->is_it9135 = true; 1217f5b00a76SAntti Palosaari reg = 0x004bfc; 1218f5b00a76SAntti Palosaari break; 1219f5b00a76SAntti Palosaari default: 122083f11619SAntti Palosaari dev->is_af9035 = true; 1221f5b00a76SAntti Palosaari reg = 0x0083e9; 1222f5b00a76SAntti Palosaari break; 1223f5b00a76SAntti Palosaari } 1224f5b00a76SAntti Palosaari 1225f5b00a76SAntti Palosaari ret = af9033_rd_regs(dev, reg, &buf[0], 4); 1226f5b00a76SAntti Palosaari if (ret < 0) 1227f5b00a76SAntti Palosaari goto err_kfree; 1228f5b00a76SAntti Palosaari 1229f5b00a76SAntti Palosaari ret = af9033_rd_regs(dev, 0x804191, &buf[4], 4); 1230f5b00a76SAntti Palosaari if (ret < 0) 1231f5b00a76SAntti Palosaari goto err_kfree; 1232f5b00a76SAntti Palosaari 1233f5b00a76SAntti Palosaari dev_info(&dev->client->dev, 12346a087f1fSAntti Palosaari "firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n", 12356a087f1fSAntti Palosaari buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], 12366a087f1fSAntti Palosaari buf[7]); 1237f5b00a76SAntti Palosaari 1238f5b00a76SAntti Palosaari /* sleep */ 1239f5b00a76SAntti Palosaari switch (dev->cfg.tuner) { 1240f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_38: 1241f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_51: 1242f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_52: 1243f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_60: 1244f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_61: 1245f5b00a76SAntti Palosaari case AF9033_TUNER_IT9135_62: 1246f5b00a76SAntti Palosaari /* IT9135 did not like to sleep at that early */ 1247f5b00a76SAntti Palosaari break; 1248f5b00a76SAntti Palosaari default: 1249f5b00a76SAntti Palosaari ret = af9033_wr_reg(dev, 0x80004c, 1); 1250f5b00a76SAntti Palosaari if (ret < 0) 1251f5b00a76SAntti Palosaari goto err_kfree; 1252f5b00a76SAntti Palosaari 1253f5b00a76SAntti Palosaari ret = af9033_wr_reg(dev, 0x800000, 0); 1254f5b00a76SAntti Palosaari if (ret < 0) 1255f5b00a76SAntti Palosaari goto err_kfree; 1256f5b00a76SAntti Palosaari } 1257f5b00a76SAntti Palosaari 1258f5b00a76SAntti Palosaari /* configure internal TS mode */ 1259f5b00a76SAntti Palosaari switch (dev->cfg.ts_mode) { 1260f5b00a76SAntti Palosaari case AF9033_TS_MODE_PARALLEL: 1261f5b00a76SAntti Palosaari dev->ts_mode_parallel = true; 1262f5b00a76SAntti Palosaari break; 1263f5b00a76SAntti Palosaari case AF9033_TS_MODE_SERIAL: 1264f5b00a76SAntti Palosaari dev->ts_mode_serial = true; 1265f5b00a76SAntti Palosaari break; 1266f5b00a76SAntti Palosaari case AF9033_TS_MODE_USB: 1267f5b00a76SAntti Palosaari /* usb mode for AF9035 */ 1268f5b00a76SAntti Palosaari default: 1269f5b00a76SAntti Palosaari break; 1270f5b00a76SAntti Palosaari } 1271f5b00a76SAntti Palosaari 1272f5b00a76SAntti Palosaari /* create dvb_frontend */ 1273f5b00a76SAntti Palosaari memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops)); 1274f5b00a76SAntti Palosaari dev->fe.demodulator_priv = dev; 1275f5b00a76SAntti Palosaari *cfg->fe = &dev->fe; 1276f5b00a76SAntti Palosaari if (cfg->ops) { 1277f5b00a76SAntti Palosaari cfg->ops->pid_filter = af9033_pid_filter; 1278f5b00a76SAntti Palosaari cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl; 1279f5b00a76SAntti Palosaari } 1280f5b00a76SAntti Palosaari i2c_set_clientdata(client, dev); 1281f5b00a76SAntti Palosaari 1282f5b00a76SAntti Palosaari dev_info(&dev->client->dev, "Afatech AF9033 successfully attached\n"); 1283f5b00a76SAntti Palosaari return 0; 1284f5b00a76SAntti Palosaari err_kfree: 1285f5b00a76SAntti Palosaari kfree(dev); 1286f5b00a76SAntti Palosaari err: 12876a087f1fSAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1288f5b00a76SAntti Palosaari return ret; 1289f5b00a76SAntti Palosaari } 1290f5b00a76SAntti Palosaari 1291f5b00a76SAntti Palosaari static int af9033_remove(struct i2c_client *client) 1292f5b00a76SAntti Palosaari { 1293f5b00a76SAntti Palosaari struct af9033_dev *dev = i2c_get_clientdata(client); 1294f5b00a76SAntti Palosaari 12956a087f1fSAntti Palosaari dev_dbg(&dev->client->dev, "\n"); 1296f5b00a76SAntti Palosaari 1297f5b00a76SAntti Palosaari dev->fe.ops.release = NULL; 1298f5b00a76SAntti Palosaari dev->fe.demodulator_priv = NULL; 1299f5b00a76SAntti Palosaari kfree(dev); 1300f5b00a76SAntti Palosaari 1301f5b00a76SAntti Palosaari return 0; 1302f5b00a76SAntti Palosaari } 1303f5b00a76SAntti Palosaari 1304f5b00a76SAntti Palosaari static const struct i2c_device_id af9033_id_table[] = { 1305f5b00a76SAntti Palosaari {"af9033", 0}, 1306f5b00a76SAntti Palosaari {} 1307f5b00a76SAntti Palosaari }; 1308f5b00a76SAntti Palosaari MODULE_DEVICE_TABLE(i2c, af9033_id_table); 1309f5b00a76SAntti Palosaari 1310f5b00a76SAntti Palosaari static struct i2c_driver af9033_driver = { 1311f5b00a76SAntti Palosaari .driver = { 1312f5b00a76SAntti Palosaari .owner = THIS_MODULE, 1313f5b00a76SAntti Palosaari .name = "af9033", 1314f5b00a76SAntti Palosaari }, 1315f5b00a76SAntti Palosaari .probe = af9033_probe, 1316f5b00a76SAntti Palosaari .remove = af9033_remove, 1317f5b00a76SAntti Palosaari .id_table = af9033_id_table, 1318f5b00a76SAntti Palosaari }; 1319f5b00a76SAntti Palosaari 1320f5b00a76SAntti Palosaari module_i2c_driver(af9033_driver); 1321f5b00a76SAntti Palosaari 13229a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 13239a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver"); 13249a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL"); 1325