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;
416bb096c9SAntti Palosaari 	u64 post_bit_error;
426bb096c9SAntti Palosaari 	u64 post_bit_count;
43204f4319SAntti Palosaari 	u64 error_block_count;
44204f4319SAntti Palosaari 	u64 total_block_count;
4583f11619SAntti Palosaari 	struct delayed_work stat_work;
469a0bf528SMauro Carvalho Chehab 	unsigned long last_stat_check;
479a0bf528SMauro Carvalho Chehab };
489a0bf528SMauro Carvalho Chehab 
499a0bf528SMauro Carvalho Chehab /* write multiple registers */
5009611caaSAntti Palosaari static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val,
519a0bf528SMauro Carvalho Chehab 		int len)
529a0bf528SMauro Carvalho Chehab {
539a0bf528SMauro Carvalho Chehab 	int ret;
5437ebaf68SMauro Carvalho Chehab 	u8 buf[MAX_XFER_SIZE];
559a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[1] = {
569a0bf528SMauro Carvalho Chehab 		{
57f5b00a76SAntti Palosaari 			.addr = dev->client->addr,
589a0bf528SMauro Carvalho Chehab 			.flags = 0,
5937ebaf68SMauro Carvalho Chehab 			.len = 3 + len,
609a0bf528SMauro Carvalho Chehab 			.buf = buf,
619a0bf528SMauro Carvalho Chehab 		}
629a0bf528SMauro Carvalho Chehab 	};
639a0bf528SMauro Carvalho Chehab 
6437ebaf68SMauro Carvalho Chehab 	if (3 + len > sizeof(buf)) {
65f5b00a76SAntti Palosaari 		dev_warn(&dev->client->dev,
666a087f1fSAntti Palosaari 				"i2c wr reg=%04x: len=%d is too big!\n",
676a087f1fSAntti Palosaari 				reg, len);
6837ebaf68SMauro Carvalho Chehab 		return -EINVAL;
6937ebaf68SMauro Carvalho Chehab 	}
7037ebaf68SMauro Carvalho Chehab 
719a0bf528SMauro Carvalho Chehab 	buf[0] = (reg >> 16) & 0xff;
729a0bf528SMauro Carvalho Chehab 	buf[1] = (reg >>  8) & 0xff;
739a0bf528SMauro Carvalho Chehab 	buf[2] = (reg >>  0) & 0xff;
749a0bf528SMauro Carvalho Chehab 	memcpy(&buf[3], val, len);
759a0bf528SMauro Carvalho Chehab 
76f5b00a76SAntti Palosaari 	ret = i2c_transfer(dev->client->adapter, msg, 1);
779a0bf528SMauro Carvalho Chehab 	if (ret == 1) {
789a0bf528SMauro Carvalho Chehab 		ret = 0;
799a0bf528SMauro Carvalho Chehab 	} else {
806a087f1fSAntti Palosaari 		dev_warn(&dev->client->dev, "i2c wr failed=%d reg=%06x len=%d\n",
816a087f1fSAntti Palosaari 				ret, reg, len);
829a0bf528SMauro Carvalho Chehab 		ret = -EREMOTEIO;
839a0bf528SMauro Carvalho Chehab 	}
849a0bf528SMauro Carvalho Chehab 
859a0bf528SMauro Carvalho Chehab 	return ret;
869a0bf528SMauro Carvalho Chehab }
879a0bf528SMauro Carvalho Chehab 
889a0bf528SMauro Carvalho Chehab /* read multiple registers */
8909611caaSAntti Palosaari static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len)
909a0bf528SMauro Carvalho Chehab {
919a0bf528SMauro Carvalho Chehab 	int ret;
929a0bf528SMauro Carvalho Chehab 	u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
939a0bf528SMauro Carvalho Chehab 			(reg >> 0) & 0xff };
949a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[2] = {
959a0bf528SMauro Carvalho Chehab 		{
96f5b00a76SAntti Palosaari 			.addr = dev->client->addr,
979a0bf528SMauro Carvalho Chehab 			.flags = 0,
989a0bf528SMauro Carvalho Chehab 			.len = sizeof(buf),
999a0bf528SMauro Carvalho Chehab 			.buf = buf
1009a0bf528SMauro Carvalho Chehab 		}, {
101f5b00a76SAntti Palosaari 			.addr = dev->client->addr,
1029a0bf528SMauro Carvalho Chehab 			.flags = I2C_M_RD,
1039a0bf528SMauro Carvalho Chehab 			.len = len,
1049a0bf528SMauro Carvalho Chehab 			.buf = val
1059a0bf528SMauro Carvalho Chehab 		}
1069a0bf528SMauro Carvalho Chehab 	};
1079a0bf528SMauro Carvalho Chehab 
108f5b00a76SAntti Palosaari 	ret = i2c_transfer(dev->client->adapter, msg, 2);
1099a0bf528SMauro Carvalho Chehab 	if (ret == 2) {
1109a0bf528SMauro Carvalho Chehab 		ret = 0;
1119a0bf528SMauro Carvalho Chehab 	} else {
1126a087f1fSAntti Palosaari 		dev_warn(&dev->client->dev, "i2c rd failed=%d reg=%06x len=%d\n",
1136a087f1fSAntti Palosaari 				ret, reg, len);
1149a0bf528SMauro Carvalho Chehab 		ret = -EREMOTEIO;
1159a0bf528SMauro Carvalho Chehab 	}
1169a0bf528SMauro Carvalho Chehab 
1179a0bf528SMauro Carvalho Chehab 	return ret;
1189a0bf528SMauro Carvalho Chehab }
1199a0bf528SMauro Carvalho Chehab 
1209a0bf528SMauro Carvalho Chehab 
1219a0bf528SMauro Carvalho Chehab /* write single register */
12209611caaSAntti Palosaari static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val)
1239a0bf528SMauro Carvalho Chehab {
12409611caaSAntti Palosaari 	return af9033_wr_regs(dev, reg, &val, 1);
1259a0bf528SMauro Carvalho Chehab }
1269a0bf528SMauro Carvalho Chehab 
1279a0bf528SMauro Carvalho Chehab /* read single register */
12809611caaSAntti Palosaari static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val)
1299a0bf528SMauro Carvalho Chehab {
13009611caaSAntti Palosaari 	return af9033_rd_regs(dev, reg, val, 1);
1319a0bf528SMauro Carvalho Chehab }
1329a0bf528SMauro Carvalho Chehab 
1339a0bf528SMauro Carvalho Chehab /* write single register with mask */
13409611caaSAntti Palosaari static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val,
1359a0bf528SMauro Carvalho Chehab 		u8 mask)
1369a0bf528SMauro Carvalho Chehab {
1379a0bf528SMauro Carvalho Chehab 	int ret;
1389a0bf528SMauro Carvalho Chehab 	u8 tmp;
1399a0bf528SMauro Carvalho Chehab 
1409a0bf528SMauro Carvalho Chehab 	/* no need for read if whole reg is written */
1419a0bf528SMauro Carvalho Chehab 	if (mask != 0xff) {
14209611caaSAntti Palosaari 		ret = af9033_rd_regs(dev, reg, &tmp, 1);
1439a0bf528SMauro Carvalho Chehab 		if (ret)
1449a0bf528SMauro Carvalho Chehab 			return ret;
1459a0bf528SMauro Carvalho Chehab 
1469a0bf528SMauro Carvalho Chehab 		val &= mask;
1479a0bf528SMauro Carvalho Chehab 		tmp &= ~mask;
1489a0bf528SMauro Carvalho Chehab 		val |= tmp;
1499a0bf528SMauro Carvalho Chehab 	}
1509a0bf528SMauro Carvalho Chehab 
15109611caaSAntti Palosaari 	return af9033_wr_regs(dev, reg, &val, 1);
1529a0bf528SMauro Carvalho Chehab }
1539a0bf528SMauro Carvalho Chehab 
1549a0bf528SMauro Carvalho Chehab /* read single register with mask */
15509611caaSAntti Palosaari static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val,
1569a0bf528SMauro Carvalho Chehab 		u8 mask)
1579a0bf528SMauro Carvalho Chehab {
1589a0bf528SMauro Carvalho Chehab 	int ret, i;
1599a0bf528SMauro Carvalho Chehab 	u8 tmp;
1609a0bf528SMauro Carvalho Chehab 
16109611caaSAntti Palosaari 	ret = af9033_rd_regs(dev, reg, &tmp, 1);
1629a0bf528SMauro Carvalho Chehab 	if (ret)
1639a0bf528SMauro Carvalho Chehab 		return ret;
1649a0bf528SMauro Carvalho Chehab 
1659a0bf528SMauro Carvalho Chehab 	tmp &= mask;
1669a0bf528SMauro Carvalho Chehab 
1679a0bf528SMauro Carvalho Chehab 	/* find position of the first bit */
1689a0bf528SMauro Carvalho Chehab 	for (i = 0; i < 8; i++) {
1699a0bf528SMauro Carvalho Chehab 		if ((mask >> i) & 0x01)
1709a0bf528SMauro Carvalho Chehab 			break;
1719a0bf528SMauro Carvalho Chehab 	}
1729a0bf528SMauro Carvalho Chehab 	*val = tmp >> i;
1739a0bf528SMauro Carvalho Chehab 
1749a0bf528SMauro Carvalho Chehab 	return 0;
1759a0bf528SMauro Carvalho Chehab }
1769a0bf528SMauro Carvalho Chehab 
1773bf5e552SAntti Palosaari /* write reg val table using reg addr auto increment */
17809611caaSAntti Palosaari static int af9033_wr_reg_val_tab(struct af9033_dev *dev,
1793bf5e552SAntti Palosaari 		const struct reg_val *tab, int tab_len)
1803bf5e552SAntti Palosaari {
181d18a88b1SAntti Palosaari #define MAX_TAB_LEN 212
1823bf5e552SAntti Palosaari 	int ret, i, j;
183d18a88b1SAntti Palosaari 	u8 buf[1 + MAX_TAB_LEN];
184d18a88b1SAntti Palosaari 
1856a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "tab_len=%d\n", tab_len);
18637ebaf68SMauro Carvalho Chehab 
18737ebaf68SMauro Carvalho Chehab 	if (tab_len > sizeof(buf)) {
1886a087f1fSAntti Palosaari 		dev_warn(&dev->client->dev, "tab len %d is too big\n", tab_len);
18937ebaf68SMauro Carvalho Chehab 		return -EINVAL;
19037ebaf68SMauro Carvalho Chehab 	}
1913bf5e552SAntti Palosaari 
1923bf5e552SAntti Palosaari 	for (i = 0, j = 0; i < tab_len; i++) {
1933bf5e552SAntti Palosaari 		buf[j] = tab[i].val;
1943bf5e552SAntti Palosaari 
1953bf5e552SAntti Palosaari 		if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
19609611caaSAntti Palosaari 			ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1);
1973bf5e552SAntti Palosaari 			if (ret < 0)
1983bf5e552SAntti Palosaari 				goto err;
1993bf5e552SAntti Palosaari 
2003bf5e552SAntti Palosaari 			j = 0;
2013bf5e552SAntti Palosaari 		} else {
2023bf5e552SAntti Palosaari 			j++;
2033bf5e552SAntti Palosaari 		}
2043bf5e552SAntti Palosaari 	}
2053bf5e552SAntti Palosaari 
2063bf5e552SAntti Palosaari 	return 0;
2073bf5e552SAntti Palosaari 
2083bf5e552SAntti Palosaari err:
2096a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
2103bf5e552SAntti Palosaari 
2113bf5e552SAntti Palosaari 	return ret;
2123bf5e552SAntti Palosaari }
2133bf5e552SAntti Palosaari 
21409611caaSAntti Palosaari static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x)
2159a0bf528SMauro Carvalho Chehab {
2169a0bf528SMauro Carvalho Chehab 	u32 r = 0, c = 0, i;
2179a0bf528SMauro Carvalho Chehab 
2186a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "a=%d b=%d x=%d\n", a, b, x);
2199a0bf528SMauro Carvalho Chehab 
2209a0bf528SMauro Carvalho Chehab 	if (a > b) {
2219a0bf528SMauro Carvalho Chehab 		c = a / b;
2229a0bf528SMauro Carvalho Chehab 		a = a - c * b;
2239a0bf528SMauro Carvalho Chehab 	}
2249a0bf528SMauro Carvalho Chehab 
2259a0bf528SMauro Carvalho Chehab 	for (i = 0; i < x; i++) {
2269a0bf528SMauro Carvalho Chehab 		if (a >= b) {
2279a0bf528SMauro Carvalho Chehab 			r += 1;
2289a0bf528SMauro Carvalho Chehab 			a -= b;
2299a0bf528SMauro Carvalho Chehab 		}
2309a0bf528SMauro Carvalho Chehab 		a <<= 1;
2319a0bf528SMauro Carvalho Chehab 		r <<= 1;
2329a0bf528SMauro Carvalho Chehab 	}
2339a0bf528SMauro Carvalho Chehab 	r = (c << (u32)x) + r;
2349a0bf528SMauro Carvalho Chehab 
2356a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "a=%d b=%d x=%d r=%d r=%x\n", a, b, x, r, r);
2369a0bf528SMauro Carvalho Chehab 
2379a0bf528SMauro Carvalho Chehab 	return r;
2389a0bf528SMauro Carvalho Chehab }
2399a0bf528SMauro Carvalho Chehab 
2409a0bf528SMauro Carvalho Chehab static int af9033_init(struct dvb_frontend *fe)
2419a0bf528SMauro Carvalho Chehab {
24209611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
2439a0bf528SMauro Carvalho Chehab 	int ret, i, len;
2449a0bf528SMauro Carvalho Chehab 	const struct reg_val *init;
2459a0bf528SMauro Carvalho Chehab 	u8 buf[4];
2469a0bf528SMauro Carvalho Chehab 	u32 adc_cw, clock_cw;
2479a0bf528SMauro Carvalho Chehab 	struct reg_val_mask tab[] = {
2489a0bf528SMauro Carvalho Chehab 		{ 0x80fb24, 0x00, 0x08 },
2499a0bf528SMauro Carvalho Chehab 		{ 0x80004c, 0x00, 0xff },
25009611caaSAntti Palosaari 		{ 0x00f641, dev->cfg.tuner, 0xff },
2519a0bf528SMauro Carvalho Chehab 		{ 0x80f5ca, 0x01, 0x01 },
2529a0bf528SMauro Carvalho Chehab 		{ 0x80f715, 0x01, 0x01 },
2539a0bf528SMauro Carvalho Chehab 		{ 0x00f41f, 0x04, 0x04 },
2549a0bf528SMauro Carvalho Chehab 		{ 0x00f41a, 0x01, 0x01 },
2559a0bf528SMauro Carvalho Chehab 		{ 0x80f731, 0x00, 0x01 },
2569a0bf528SMauro Carvalho Chehab 		{ 0x00d91e, 0x00, 0x01 },
2579a0bf528SMauro Carvalho Chehab 		{ 0x00d919, 0x00, 0x01 },
2589a0bf528SMauro Carvalho Chehab 		{ 0x80f732, 0x00, 0x01 },
2599a0bf528SMauro Carvalho Chehab 		{ 0x00d91f, 0x00, 0x01 },
2609a0bf528SMauro Carvalho Chehab 		{ 0x00d91a, 0x00, 0x01 },
2619a0bf528SMauro Carvalho Chehab 		{ 0x80f730, 0x00, 0x01 },
2629a0bf528SMauro Carvalho Chehab 		{ 0x80f778, 0x00, 0xff },
2639a0bf528SMauro Carvalho Chehab 		{ 0x80f73c, 0x01, 0x01 },
2649a0bf528SMauro Carvalho Chehab 		{ 0x80f776, 0x00, 0x01 },
2659a0bf528SMauro Carvalho Chehab 		{ 0x00d8fd, 0x01, 0xff },
2669a0bf528SMauro Carvalho Chehab 		{ 0x00d830, 0x01, 0xff },
2679a0bf528SMauro Carvalho Chehab 		{ 0x00d831, 0x00, 0xff },
2689a0bf528SMauro Carvalho Chehab 		{ 0x00d832, 0x00, 0xff },
26909611caaSAntti Palosaari 		{ 0x80f985, dev->ts_mode_serial, 0x01 },
27009611caaSAntti Palosaari 		{ 0x80f986, dev->ts_mode_parallel, 0x01 },
2719a0bf528SMauro Carvalho Chehab 		{ 0x00d827, 0x00, 0xff },
2729a0bf528SMauro Carvalho Chehab 		{ 0x00d829, 0x00, 0xff },
27309611caaSAntti Palosaari 		{ 0x800045, dev->cfg.adc_multiplier, 0xff },
2749a0bf528SMauro Carvalho Chehab 	};
2759a0bf528SMauro Carvalho Chehab 
2769a0bf528SMauro Carvalho Chehab 	/* program clock control */
27709611caaSAntti Palosaari 	clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul);
2789a0bf528SMauro Carvalho Chehab 	buf[0] = (clock_cw >>  0) & 0xff;
2799a0bf528SMauro Carvalho Chehab 	buf[1] = (clock_cw >>  8) & 0xff;
2809a0bf528SMauro Carvalho Chehab 	buf[2] = (clock_cw >> 16) & 0xff;
2819a0bf528SMauro Carvalho Chehab 	buf[3] = (clock_cw >> 24) & 0xff;
2829a0bf528SMauro Carvalho Chehab 
2836a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "clock=%d clock_cw=%08x\n",
2846a087f1fSAntti Palosaari 			dev->cfg.clock, clock_cw);
2859a0bf528SMauro Carvalho Chehab 
28609611caaSAntti Palosaari 	ret = af9033_wr_regs(dev, 0x800025, buf, 4);
2879a0bf528SMauro Carvalho Chehab 	if (ret < 0)
2889a0bf528SMauro Carvalho Chehab 		goto err;
2899a0bf528SMauro Carvalho Chehab 
2909a0bf528SMauro Carvalho Chehab 	/* program ADC control */
2919a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
29209611caaSAntti Palosaari 		if (clock_adc_lut[i].clock == dev->cfg.clock)
2939a0bf528SMauro Carvalho Chehab 			break;
2949a0bf528SMauro Carvalho Chehab 	}
2959a0bf528SMauro Carvalho Chehab 
29609611caaSAntti Palosaari 	adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul);
2979a0bf528SMauro Carvalho Chehab 	buf[0] = (adc_cw >>  0) & 0xff;
2989a0bf528SMauro Carvalho Chehab 	buf[1] = (adc_cw >>  8) & 0xff;
2999a0bf528SMauro Carvalho Chehab 	buf[2] = (adc_cw >> 16) & 0xff;
3009a0bf528SMauro Carvalho Chehab 
3016a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "adc=%d adc_cw=%06x\n",
3026a087f1fSAntti Palosaari 			clock_adc_lut[i].adc, adc_cw);
3039a0bf528SMauro Carvalho Chehab 
30409611caaSAntti Palosaari 	ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3);
3059a0bf528SMauro Carvalho Chehab 	if (ret < 0)
3069a0bf528SMauro Carvalho Chehab 		goto err;
3079a0bf528SMauro Carvalho Chehab 
3089a0bf528SMauro Carvalho Chehab 	/* program register table */
3099a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
31009611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val,
3119a0bf528SMauro Carvalho Chehab 				tab[i].mask);
3129a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3139a0bf528SMauro Carvalho Chehab 			goto err;
3149a0bf528SMauro Carvalho Chehab 	}
3159a0bf528SMauro Carvalho Chehab 
316ca681fe0SAntti Palosaari 	/* clock output */
31709611caaSAntti Palosaari 	if (dev->cfg.dyn0_clk) {
31809611caaSAntti Palosaari 		ret = af9033_wr_reg(dev, 0x80fba8, 0x00);
3199dc0f3feSAntti Palosaari 		if (ret < 0)
3209dc0f3feSAntti Palosaari 			goto err;
3219dc0f3feSAntti Palosaari 	}
3229dc0f3feSAntti Palosaari 
3239a0bf528SMauro Carvalho Chehab 	/* settings for TS interface */
32409611caaSAntti Palosaari 	if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) {
32509611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01);
3269a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3279a0bf528SMauro Carvalho Chehab 			goto err;
3289a0bf528SMauro Carvalho Chehab 
32909611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01);
3309a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3319a0bf528SMauro Carvalho Chehab 			goto err;
3329a0bf528SMauro Carvalho Chehab 	} else {
33309611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01);
3349a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3359a0bf528SMauro Carvalho Chehab 			goto err;
3369a0bf528SMauro Carvalho Chehab 
33709611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01);
3389a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3399a0bf528SMauro Carvalho Chehab 			goto err;
3409a0bf528SMauro Carvalho Chehab 	}
3419a0bf528SMauro Carvalho Chehab 
3429a0bf528SMauro Carvalho Chehab 	/* load OFSM settings */
3436a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "load ofsm settings\n");
34409611caaSAntti Palosaari 	switch (dev->cfg.tuner) {
345fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_38:
346fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_51:
347fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_52:
348463c399cSAntti Palosaari 		len = ARRAY_SIZE(ofsm_init_it9135_v1);
349463c399cSAntti Palosaari 		init = ofsm_init_it9135_v1;
350463c399cSAntti Palosaari 		break;
351fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_60:
352fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_61:
353fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_62:
354463c399cSAntti Palosaari 		len = ARRAY_SIZE(ofsm_init_it9135_v2);
355463c399cSAntti Palosaari 		init = ofsm_init_it9135_v2;
356fe8eece1SAntti Palosaari 		break;
357fe8eece1SAntti Palosaari 	default:
3589a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(ofsm_init);
3599a0bf528SMauro Carvalho Chehab 		init = ofsm_init;
360fe8eece1SAntti Palosaari 		break;
361fe8eece1SAntti Palosaari 	}
362fe8eece1SAntti Palosaari 
36309611caaSAntti Palosaari 	ret = af9033_wr_reg_val_tab(dev, init, len);
3649a0bf528SMauro Carvalho Chehab 	if (ret < 0)
3659a0bf528SMauro Carvalho Chehab 		goto err;
3669a0bf528SMauro Carvalho Chehab 
3679a0bf528SMauro Carvalho Chehab 	/* load tuner specific settings */
3686a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "load tuner specific settings\n");
36909611caaSAntti Palosaari 	switch (dev->cfg.tuner) {
3709a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_TUA9001:
3719a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_tua9001);
3729a0bf528SMauro Carvalho Chehab 		init = tuner_init_tua9001;
3739a0bf528SMauro Carvalho Chehab 		break;
3749a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_FC0011:
3759a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_fc0011);
3769a0bf528SMauro Carvalho Chehab 		init = tuner_init_fc0011;
3779a0bf528SMauro Carvalho Chehab 		break;
3789a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_MXL5007T:
3799a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_mxl5007t);
3809a0bf528SMauro Carvalho Chehab 		init = tuner_init_mxl5007t;
3819a0bf528SMauro Carvalho Chehab 		break;
3829a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_TDA18218:
3839a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_tda18218);
3849a0bf528SMauro Carvalho Chehab 		init = tuner_init_tda18218;
3859a0bf528SMauro Carvalho Chehab 		break;
386d67ceb33SOliver Schinagl 	case AF9033_TUNER_FC2580:
387d67ceb33SOliver Schinagl 		len = ARRAY_SIZE(tuner_init_fc2580);
388d67ceb33SOliver Schinagl 		init = tuner_init_fc2580;
389d67ceb33SOliver Schinagl 		break;
390e713ad15SAntti Palosaari 	case AF9033_TUNER_FC0012:
391e713ad15SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_fc0012);
392e713ad15SAntti Palosaari 		init = tuner_init_fc0012;
393e713ad15SAntti Palosaari 		break;
3944902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_38:
395a72cbb77SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_38);
396a72cbb77SAntti Palosaari 		init = tuner_init_it9135_38;
397a72cbb77SAntti Palosaari 		break;
3984902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_51:
399bb2e12a6SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_51);
400bb2e12a6SAntti Palosaari 		init = tuner_init_it9135_51;
401bb2e12a6SAntti Palosaari 		break;
4024902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_52:
40322d729f3SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_52);
40422d729f3SAntti Palosaari 		init = tuner_init_it9135_52;
40522d729f3SAntti Palosaari 		break;
4064902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_60:
407a49f53a0SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_60);
408a49f53a0SAntti Palosaari 		init = tuner_init_it9135_60;
409a49f53a0SAntti Palosaari 		break;
4104902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_61:
41185211323SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_61);
41285211323SAntti Palosaari 		init = tuner_init_it9135_61;
41385211323SAntti Palosaari 		break;
4144902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_62:
415dc4a2c40SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_62);
416dc4a2c40SAntti Palosaari 		init = tuner_init_it9135_62;
4174902bb39SAntti Palosaari 		break;
4189a0bf528SMauro Carvalho Chehab 	default:
4196a087f1fSAntti Palosaari 		dev_dbg(&dev->client->dev, "unsupported tuner ID=%d\n",
4206a087f1fSAntti Palosaari 				dev->cfg.tuner);
4219a0bf528SMauro Carvalho Chehab 		ret = -ENODEV;
4229a0bf528SMauro Carvalho Chehab 		goto err;
4239a0bf528SMauro Carvalho Chehab 	}
4249a0bf528SMauro Carvalho Chehab 
42509611caaSAntti Palosaari 	ret = af9033_wr_reg_val_tab(dev, init, len);
4269a0bf528SMauro Carvalho Chehab 	if (ret < 0)
4279a0bf528SMauro Carvalho Chehab 		goto err;
4289a0bf528SMauro Carvalho Chehab 
42909611caaSAntti Palosaari 	if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
43009611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01);
4319805992fSJose Alberto Reguero 		if (ret < 0)
4329805992fSJose Alberto Reguero 			goto err;
433bf97b637SAntti Palosaari 
43409611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
4359805992fSJose Alberto Reguero 		if (ret < 0)
4369805992fSJose Alberto Reguero 			goto err;
437bf97b637SAntti Palosaari 
43809611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01);
4399805992fSJose Alberto Reguero 		if (ret < 0)
4409805992fSJose Alberto Reguero 			goto err;
4419805992fSJose Alberto Reguero 	}
4429805992fSJose Alberto Reguero 
44309611caaSAntti Palosaari 	switch (dev->cfg.tuner) {
444086991ddSAntti Palosaari 	case AF9033_TUNER_IT9135_60:
445086991ddSAntti Palosaari 	case AF9033_TUNER_IT9135_61:
446086991ddSAntti Palosaari 	case AF9033_TUNER_IT9135_62:
44709611caaSAntti Palosaari 		ret = af9033_wr_reg(dev, 0x800000, 0x01);
448086991ddSAntti Palosaari 		if (ret < 0)
449086991ddSAntti Palosaari 			goto err;
450086991ddSAntti Palosaari 	}
451086991ddSAntti Palosaari 
45209611caaSAntti Palosaari 	dev->bandwidth_hz = 0; /* force to program all parameters */
45383f11619SAntti Palosaari 	/* start statistics polling */
45483f11619SAntti Palosaari 	schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
4559a0bf528SMauro Carvalho Chehab 
4569a0bf528SMauro Carvalho Chehab 	return 0;
4579a0bf528SMauro Carvalho Chehab 
4589a0bf528SMauro Carvalho Chehab err:
4596a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
4609a0bf528SMauro Carvalho Chehab 
4619a0bf528SMauro Carvalho Chehab 	return ret;
4629a0bf528SMauro Carvalho Chehab }
4639a0bf528SMauro Carvalho Chehab 
4649a0bf528SMauro Carvalho Chehab static int af9033_sleep(struct dvb_frontend *fe)
4659a0bf528SMauro Carvalho Chehab {
46609611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
4679a0bf528SMauro Carvalho Chehab 	int ret, i;
4689a0bf528SMauro Carvalho Chehab 	u8 tmp;
4699a0bf528SMauro Carvalho Chehab 
47083f11619SAntti Palosaari 	/* stop statistics polling */
47183f11619SAntti Palosaari 	cancel_delayed_work_sync(&dev->stat_work);
47283f11619SAntti Palosaari 
47309611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x80004c, 1);
4749a0bf528SMauro Carvalho Chehab 	if (ret < 0)
4759a0bf528SMauro Carvalho Chehab 		goto err;
4769a0bf528SMauro Carvalho Chehab 
47709611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x800000, 0);
4789a0bf528SMauro Carvalho Chehab 	if (ret < 0)
4799a0bf528SMauro Carvalho Chehab 		goto err;
4809a0bf528SMauro Carvalho Chehab 
4819a0bf528SMauro Carvalho Chehab 	for (i = 100, tmp = 1; i && tmp; i--) {
48209611caaSAntti Palosaari 		ret = af9033_rd_reg(dev, 0x80004c, &tmp);
4839a0bf528SMauro Carvalho Chehab 		if (ret < 0)
4849a0bf528SMauro Carvalho Chehab 			goto err;
4859a0bf528SMauro Carvalho Chehab 
4869a0bf528SMauro Carvalho Chehab 		usleep_range(200, 10000);
4879a0bf528SMauro Carvalho Chehab 	}
4889a0bf528SMauro Carvalho Chehab 
4896a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "loop=%d\n", i);
4909a0bf528SMauro Carvalho Chehab 
4919a0bf528SMauro Carvalho Chehab 	if (i == 0) {
4929a0bf528SMauro Carvalho Chehab 		ret = -ETIMEDOUT;
4939a0bf528SMauro Carvalho Chehab 		goto err;
4949a0bf528SMauro Carvalho Chehab 	}
4959a0bf528SMauro Carvalho Chehab 
49609611caaSAntti Palosaari 	ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08);
4979a0bf528SMauro Carvalho Chehab 	if (ret < 0)
4989a0bf528SMauro Carvalho Chehab 		goto err;
4999a0bf528SMauro Carvalho Chehab 
5009a0bf528SMauro Carvalho Chehab 	/* prevent current leak (?) */
50109611caaSAntti Palosaari 	if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
5029a0bf528SMauro Carvalho Chehab 		/* enable parallel TS */
50309611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
5049a0bf528SMauro Carvalho Chehab 		if (ret < 0)
5059a0bf528SMauro Carvalho Chehab 			goto err;
5069a0bf528SMauro Carvalho Chehab 
50709611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01);
5089a0bf528SMauro Carvalho Chehab 		if (ret < 0)
5099a0bf528SMauro Carvalho Chehab 			goto err;
5109a0bf528SMauro Carvalho Chehab 	}
5119a0bf528SMauro Carvalho Chehab 
5129a0bf528SMauro Carvalho Chehab 	return 0;
5139a0bf528SMauro Carvalho Chehab 
5149a0bf528SMauro Carvalho Chehab err:
5156a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
5169a0bf528SMauro Carvalho Chehab 
5179a0bf528SMauro Carvalho Chehab 	return ret;
5189a0bf528SMauro Carvalho Chehab }
5199a0bf528SMauro Carvalho Chehab 
5209a0bf528SMauro Carvalho Chehab static int af9033_get_tune_settings(struct dvb_frontend *fe,
5219a0bf528SMauro Carvalho Chehab 		struct dvb_frontend_tune_settings *fesettings)
5229a0bf528SMauro Carvalho Chehab {
523fe8eece1SAntti Palosaari 	/* 800 => 2000 because IT9135 v2 is slow to gain lock */
524fe8eece1SAntti Palosaari 	fesettings->min_delay_ms = 2000;
5259a0bf528SMauro Carvalho Chehab 	fesettings->step_size = 0;
5269a0bf528SMauro Carvalho Chehab 	fesettings->max_drift = 0;
5279a0bf528SMauro Carvalho Chehab 
5289a0bf528SMauro Carvalho Chehab 	return 0;
5299a0bf528SMauro Carvalho Chehab }
5309a0bf528SMauro Carvalho Chehab 
5319a0bf528SMauro Carvalho Chehab static int af9033_set_frontend(struct dvb_frontend *fe)
5329a0bf528SMauro Carvalho Chehab {
53309611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
5349a0bf528SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
535182b967eSHans-Frieder Vogt 	int ret, i, spec_inv, sampling_freq;
5369a0bf528SMauro Carvalho Chehab 	u8 tmp, buf[3], bandwidth_reg_val;
5379a0bf528SMauro Carvalho Chehab 	u32 if_frequency, freq_cw, adc_freq;
5389a0bf528SMauro Carvalho Chehab 
5396a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "frequency=%d bandwidth_hz=%d\n",
5406a087f1fSAntti Palosaari 			c->frequency, c->bandwidth_hz);
5419a0bf528SMauro Carvalho Chehab 
5429a0bf528SMauro Carvalho Chehab 	/* check bandwidth */
5439a0bf528SMauro Carvalho Chehab 	switch (c->bandwidth_hz) {
5449a0bf528SMauro Carvalho Chehab 	case 6000000:
5459a0bf528SMauro Carvalho Chehab 		bandwidth_reg_val = 0x00;
5469a0bf528SMauro Carvalho Chehab 		break;
5479a0bf528SMauro Carvalho Chehab 	case 7000000:
5489a0bf528SMauro Carvalho Chehab 		bandwidth_reg_val = 0x01;
5499a0bf528SMauro Carvalho Chehab 		break;
5509a0bf528SMauro Carvalho Chehab 	case 8000000:
5519a0bf528SMauro Carvalho Chehab 		bandwidth_reg_val = 0x02;
5529a0bf528SMauro Carvalho Chehab 		break;
5539a0bf528SMauro Carvalho Chehab 	default:
5546a087f1fSAntti Palosaari 		dev_dbg(&dev->client->dev, "invalid bandwidth_hz\n");
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:
6566a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", 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 
6686a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "\n");
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:
7836a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", 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 
82683f11619SAntti Palosaari 	dev->fe_status = *status;
82783f11619SAntti Palosaari 
8289a0bf528SMauro Carvalho Chehab 	return 0;
8299a0bf528SMauro Carvalho Chehab 
8309a0bf528SMauro Carvalho Chehab err:
8316a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
8329a0bf528SMauro Carvalho Chehab 
8339a0bf528SMauro Carvalho Chehab 	return ret;
8349a0bf528SMauro Carvalho Chehab }
8359a0bf528SMauro Carvalho Chehab 
8369a0bf528SMauro Carvalho Chehab static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
8379a0bf528SMauro Carvalho Chehab {
83809611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
8396b457786SAntti Palosaari 	struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
8409a0bf528SMauro Carvalho Chehab 
8416b457786SAntti Palosaari 	/* use DVBv5 CNR */
8426b457786SAntti Palosaari 	if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
8436b457786SAntti Palosaari 		*snr = div_s64(c->cnr.stat[0].svalue, 100); /* 1000x => 10x */
8446b457786SAntti Palosaari 	else
8456b457786SAntti Palosaari 		*snr = 0;
8469a0bf528SMauro Carvalho Chehab 
8479a0bf528SMauro Carvalho Chehab 	return 0;
8489a0bf528SMauro Carvalho Chehab }
8499a0bf528SMauro Carvalho Chehab 
8509a0bf528SMauro Carvalho Chehab static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
8519a0bf528SMauro Carvalho Chehab {
85209611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
8539a0bf528SMauro Carvalho Chehab 	int ret;
8549a0bf528SMauro Carvalho Chehab 	u8 strength2;
8559a0bf528SMauro Carvalho Chehab 
8569a0bf528SMauro Carvalho Chehab 	/* read signal strength of 0-100 scale */
85709611caaSAntti Palosaari 	ret = af9033_rd_reg(dev, 0x800048, &strength2);
8589a0bf528SMauro Carvalho Chehab 	if (ret < 0)
8599a0bf528SMauro Carvalho Chehab 		goto err;
8609a0bf528SMauro Carvalho Chehab 
8619a0bf528SMauro Carvalho Chehab 	/* scale value to 0x0000-0xffff */
8629a0bf528SMauro Carvalho Chehab 	*strength = strength2 * 0xffff / 100;
8639a0bf528SMauro Carvalho Chehab 
8649a0bf528SMauro Carvalho Chehab 	return 0;
8659a0bf528SMauro Carvalho Chehab 
8669a0bf528SMauro Carvalho Chehab err:
8676a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
8689a0bf528SMauro Carvalho Chehab 
8699a0bf528SMauro Carvalho Chehab 	return ret;
8709a0bf528SMauro Carvalho Chehab }
8719a0bf528SMauro Carvalho Chehab 
87209611caaSAntti Palosaari static int af9033_update_ch_stat(struct af9033_dev *dev)
8739a0bf528SMauro Carvalho Chehab {
8749a0bf528SMauro Carvalho Chehab 	int ret = 0;
8759a0bf528SMauro Carvalho Chehab 	u32 err_cnt, bit_cnt;
8769a0bf528SMauro Carvalho Chehab 	u16 abort_cnt;
8779a0bf528SMauro Carvalho Chehab 	u8 buf[7];
8789a0bf528SMauro Carvalho Chehab 
8799a0bf528SMauro Carvalho Chehab 	/* only update data every half second */
88009611caaSAntti Palosaari 	if (time_after(jiffies, dev->last_stat_check + msecs_to_jiffies(500))) {
88109611caaSAntti Palosaari 		ret = af9033_rd_regs(dev, 0x800032, buf, sizeof(buf));
8829a0bf528SMauro Carvalho Chehab 		if (ret < 0)
8839a0bf528SMauro Carvalho Chehab 			goto err;
8849a0bf528SMauro Carvalho Chehab 		/* in 8 byte packets? */
8859a0bf528SMauro Carvalho Chehab 		abort_cnt = (buf[1] << 8) + buf[0];
8869a0bf528SMauro Carvalho Chehab 		/* in bits */
8879a0bf528SMauro Carvalho Chehab 		err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2];
8889a0bf528SMauro Carvalho Chehab 		/* in 8 byte packets? always(?) 0x2710 = 10000 */
8899a0bf528SMauro Carvalho Chehab 		bit_cnt = (buf[6] << 8) + buf[5];
8909a0bf528SMauro Carvalho Chehab 
8919a0bf528SMauro Carvalho Chehab 		if (bit_cnt < abort_cnt) {
8929a0bf528SMauro Carvalho Chehab 			abort_cnt = 1000;
89309611caaSAntti Palosaari 			dev->ber = 0xffffffff;
8949a0bf528SMauro Carvalho Chehab 		} else {
89524e419a0SAntti Palosaari 			/*
89624e419a0SAntti Palosaari 			 * 8 byte packets, that have not been rejected already
89724e419a0SAntti Palosaari 			 */
8989a0bf528SMauro Carvalho Chehab 			bit_cnt -= (u32)abort_cnt;
8999a0bf528SMauro Carvalho Chehab 			if (bit_cnt == 0) {
90009611caaSAntti Palosaari 				dev->ber = 0xffffffff;
9019a0bf528SMauro Carvalho Chehab 			} else {
9029a0bf528SMauro Carvalho Chehab 				err_cnt -= (u32)abort_cnt * 8 * 8;
9039a0bf528SMauro Carvalho Chehab 				bit_cnt *= 8 * 8;
90409611caaSAntti Palosaari 				dev->ber = err_cnt * (0xffffffff / bit_cnt);
9059a0bf528SMauro Carvalho Chehab 			}
9069a0bf528SMauro Carvalho Chehab 		}
90709611caaSAntti Palosaari 		dev->ucb += abort_cnt;
90809611caaSAntti Palosaari 		dev->last_stat_check = jiffies;
9099a0bf528SMauro Carvalho Chehab 	}
9109a0bf528SMauro Carvalho Chehab 
9119a0bf528SMauro Carvalho Chehab 	return 0;
9129a0bf528SMauro Carvalho Chehab err:
9136a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
9140a73f2d6SAntti Palosaari 
9159a0bf528SMauro Carvalho Chehab 	return ret;
9169a0bf528SMauro Carvalho Chehab }
9179a0bf528SMauro Carvalho Chehab 
9189a0bf528SMauro Carvalho Chehab static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
9199a0bf528SMauro Carvalho Chehab {
92009611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
9219a0bf528SMauro Carvalho Chehab 	int ret;
9229a0bf528SMauro Carvalho Chehab 
92309611caaSAntti Palosaari 	ret = af9033_update_ch_stat(dev);
9249a0bf528SMauro Carvalho Chehab 	if (ret < 0)
9259a0bf528SMauro Carvalho Chehab 		return ret;
9269a0bf528SMauro Carvalho Chehab 
92709611caaSAntti Palosaari 	*ber = dev->ber;
9289a0bf528SMauro Carvalho Chehab 
9299a0bf528SMauro Carvalho Chehab 	return 0;
9309a0bf528SMauro Carvalho Chehab }
9319a0bf528SMauro Carvalho Chehab 
9329a0bf528SMauro Carvalho Chehab static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
9339a0bf528SMauro Carvalho Chehab {
93409611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
9359a0bf528SMauro Carvalho Chehab 
9361d0ceae4SAntti Palosaari 	*ucblocks = dev->error_block_count;
9379a0bf528SMauro Carvalho Chehab 	return 0;
9389a0bf528SMauro Carvalho Chehab }
9399a0bf528SMauro Carvalho Chehab 
9409a0bf528SMauro Carvalho Chehab static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
9419a0bf528SMauro Carvalho Chehab {
94209611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
9439a0bf528SMauro Carvalho Chehab 	int ret;
9449a0bf528SMauro Carvalho Chehab 
9456a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "enable=%d\n", enable);
9469a0bf528SMauro Carvalho Chehab 
94709611caaSAntti Palosaari 	ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01);
9489a0bf528SMauro Carvalho Chehab 	if (ret < 0)
9499a0bf528SMauro Carvalho Chehab 		goto err;
9509a0bf528SMauro Carvalho Chehab 
9519a0bf528SMauro Carvalho Chehab 	return 0;
9529a0bf528SMauro Carvalho Chehab 
9539a0bf528SMauro Carvalho Chehab err:
9546a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
9559a0bf528SMauro Carvalho Chehab 
9569a0bf528SMauro Carvalho Chehab 	return ret;
9579a0bf528SMauro Carvalho Chehab }
9589a0bf528SMauro Carvalho Chehab 
959ed97a6feSMauro Carvalho Chehab static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
960040cf86cSAntti Palosaari {
96109611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
962040cf86cSAntti Palosaari 	int ret;
963040cf86cSAntti Palosaari 
9646a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "onoff=%d\n", onoff);
965040cf86cSAntti Palosaari 
96609611caaSAntti Palosaari 	ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01);
967040cf86cSAntti Palosaari 	if (ret < 0)
968040cf86cSAntti Palosaari 		goto err;
969040cf86cSAntti Palosaari 
970040cf86cSAntti Palosaari 	return 0;
971040cf86cSAntti Palosaari 
972040cf86cSAntti Palosaari err:
9736a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
974040cf86cSAntti Palosaari 
975040cf86cSAntti Palosaari 	return ret;
976040cf86cSAntti Palosaari }
977040cf86cSAntti Palosaari 
97824e419a0SAntti Palosaari static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
97924e419a0SAntti Palosaari 		int onoff)
980040cf86cSAntti Palosaari {
98109611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
982040cf86cSAntti Palosaari 	int ret;
983040cf86cSAntti Palosaari 	u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
984040cf86cSAntti Palosaari 
9856a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "index=%d pid=%04x onoff=%d\n",
9866a087f1fSAntti Palosaari 			index, pid, onoff);
987040cf86cSAntti Palosaari 
988040cf86cSAntti Palosaari 	if (pid > 0x1fff)
989040cf86cSAntti Palosaari 		return 0;
990040cf86cSAntti Palosaari 
99109611caaSAntti Palosaari 	ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2);
992040cf86cSAntti Palosaari 	if (ret < 0)
993040cf86cSAntti Palosaari 		goto err;
994040cf86cSAntti Palosaari 
99509611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x80f994, onoff);
996040cf86cSAntti Palosaari 	if (ret < 0)
997040cf86cSAntti Palosaari 		goto err;
998040cf86cSAntti Palosaari 
99909611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x80f995, index);
1000040cf86cSAntti Palosaari 	if (ret < 0)
1001040cf86cSAntti Palosaari 		goto err;
1002040cf86cSAntti Palosaari 
1003040cf86cSAntti Palosaari 	return 0;
1004040cf86cSAntti Palosaari 
1005040cf86cSAntti Palosaari err:
10066a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
1007040cf86cSAntti Palosaari 
1008040cf86cSAntti Palosaari 	return ret;
1009040cf86cSAntti Palosaari }
1010040cf86cSAntti Palosaari 
101183f11619SAntti Palosaari static void af9033_stat_work(struct work_struct *work)
101283f11619SAntti Palosaari {
101383f11619SAntti Palosaari 	struct af9033_dev *dev = container_of(work, struct af9033_dev, stat_work.work);
101483f11619SAntti Palosaari 	struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
10153e41313aSAntti Palosaari 	int ret, tmp, i, len;
1016204f4319SAntti Palosaari 	u8 u8tmp, buf[7];
101783f11619SAntti Palosaari 
101883f11619SAntti Palosaari 	dev_dbg(&dev->client->dev, "\n");
101983f11619SAntti Palosaari 
10203e41313aSAntti Palosaari 	/* signal strength */
102183f11619SAntti Palosaari 	if (dev->fe_status & FE_HAS_SIGNAL) {
102283f11619SAntti Palosaari 		if (dev->is_af9035) {
102383f11619SAntti Palosaari 			ret = af9033_rd_reg(dev, 0x80004a, &u8tmp);
102483f11619SAntti Palosaari 			tmp = -u8tmp * 1000;
102583f11619SAntti Palosaari 		} else {
102683f11619SAntti Palosaari 			ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
102783f11619SAntti Palosaari 			tmp = (u8tmp - 100) * 1000;
102883f11619SAntti Palosaari 		}
102983f11619SAntti Palosaari 		if (ret)
103083f11619SAntti Palosaari 			goto err;
103183f11619SAntti Palosaari 
103283f11619SAntti Palosaari 		c->strength.len = 1;
103383f11619SAntti Palosaari 		c->strength.stat[0].scale = FE_SCALE_DECIBEL;
103483f11619SAntti Palosaari 		c->strength.stat[0].svalue = tmp;
103583f11619SAntti Palosaari 	} else {
103683f11619SAntti Palosaari 		c->strength.len = 1;
103783f11619SAntti Palosaari 		c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
103883f11619SAntti Palosaari 	}
103983f11619SAntti Palosaari 
10403e41313aSAntti Palosaari 	/* CNR */
10413e41313aSAntti Palosaari 	if (dev->fe_status & FE_HAS_VITERBI) {
10423e41313aSAntti Palosaari 		u32 snr_val;
10433e41313aSAntti Palosaari 		const struct val_snr *snr_lut;
10443e41313aSAntti Palosaari 
10453e41313aSAntti Palosaari 		/* read value */
10463e41313aSAntti Palosaari 		ret = af9033_rd_regs(dev, 0x80002c, buf, 3);
10473e41313aSAntti Palosaari 		if (ret)
10483e41313aSAntti Palosaari 			goto err;
10493e41313aSAntti Palosaari 
10503e41313aSAntti Palosaari 		snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0);
10513e41313aSAntti Palosaari 
10523e41313aSAntti Palosaari 		/* read current modulation */
10533e41313aSAntti Palosaari 		ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
10543e41313aSAntti Palosaari 		if (ret)
10553e41313aSAntti Palosaari 			goto err;
10563e41313aSAntti Palosaari 
10573e41313aSAntti Palosaari 		switch ((u8tmp >> 0) & 3) {
10583e41313aSAntti Palosaari 		case 0:
10593e41313aSAntti Palosaari 			len = ARRAY_SIZE(qpsk_snr_lut);
10603e41313aSAntti Palosaari 			snr_lut = qpsk_snr_lut;
10613e41313aSAntti Palosaari 			break;
10623e41313aSAntti Palosaari 		case 1:
10633e41313aSAntti Palosaari 			len = ARRAY_SIZE(qam16_snr_lut);
10643e41313aSAntti Palosaari 			snr_lut = qam16_snr_lut;
10653e41313aSAntti Palosaari 			break;
10663e41313aSAntti Palosaari 		case 2:
10673e41313aSAntti Palosaari 			len = ARRAY_SIZE(qam64_snr_lut);
10683e41313aSAntti Palosaari 			snr_lut = qam64_snr_lut;
10693e41313aSAntti Palosaari 			break;
10703e41313aSAntti Palosaari 		default:
10713e41313aSAntti Palosaari 			goto err_schedule_delayed_work;
10723e41313aSAntti Palosaari 		}
10733e41313aSAntti Palosaari 
10743e41313aSAntti Palosaari 		for (i = 0; i < len; i++) {
10753e41313aSAntti Palosaari 			tmp = snr_lut[i].snr * 1000;
10763e41313aSAntti Palosaari 			if (snr_val < snr_lut[i].val)
10773e41313aSAntti Palosaari 				break;
10783e41313aSAntti Palosaari 		}
10793e41313aSAntti Palosaari 
10803e41313aSAntti Palosaari 		c->cnr.len = 1;
10813e41313aSAntti Palosaari 		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
10823e41313aSAntti Palosaari 		c->cnr.stat[0].svalue = tmp;
10833e41313aSAntti Palosaari 	} else {
10843e41313aSAntti Palosaari 		c->cnr.len = 1;
10853e41313aSAntti Palosaari 		c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
10863e41313aSAntti Palosaari 	}
10873e41313aSAntti Palosaari 
1088204f4319SAntti Palosaari 	/* UCB/PER/BER */
1089204f4319SAntti Palosaari 	if (dev->fe_status & FE_HAS_LOCK) {
1090204f4319SAntti Palosaari 		/* outer FEC, 204 byte packets */
1091204f4319SAntti Palosaari 		u16 abort_packet_count, rsd_packet_count;
10926bb096c9SAntti Palosaari 		/* inner FEC, bits */
10936bb096c9SAntti Palosaari 		u32 rsd_bit_err_count;
1094204f4319SAntti Palosaari 
1095204f4319SAntti Palosaari 		/*
1096204f4319SAntti Palosaari 		 * Packet count used for measurement is 10000
1097204f4319SAntti Palosaari 		 * (rsd_packet_count). Maybe it should be increased?
1098204f4319SAntti Palosaari 		 */
1099204f4319SAntti Palosaari 
1100204f4319SAntti Palosaari 		ret = af9033_rd_regs(dev, 0x800032, buf, 7);
1101204f4319SAntti Palosaari 		if (ret)
1102204f4319SAntti Palosaari 			goto err;
1103204f4319SAntti Palosaari 
1104204f4319SAntti Palosaari 		abort_packet_count = (buf[1] << 8) | (buf[0] << 0);
11056bb096c9SAntti Palosaari 		rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2];
1106204f4319SAntti Palosaari 		rsd_packet_count = (buf[6] << 8) | (buf[5] << 0);
1107204f4319SAntti Palosaari 
1108204f4319SAntti Palosaari 		dev->error_block_count += abort_packet_count;
1109204f4319SAntti Palosaari 		dev->total_block_count += rsd_packet_count;
11106bb096c9SAntti Palosaari 		dev->post_bit_error += rsd_bit_err_count;
11116bb096c9SAntti Palosaari 		dev->post_bit_count += rsd_packet_count * 204 * 8;
1112204f4319SAntti Palosaari 
1113204f4319SAntti Palosaari 		c->block_count.len = 1;
1114204f4319SAntti Palosaari 		c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1115204f4319SAntti Palosaari 		c->block_count.stat[0].uvalue = dev->total_block_count;
1116204f4319SAntti Palosaari 
1117204f4319SAntti Palosaari 		c->block_error.len = 1;
1118204f4319SAntti Palosaari 		c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1119204f4319SAntti Palosaari 		c->block_error.stat[0].uvalue = dev->error_block_count;
11206bb096c9SAntti Palosaari 
11216bb096c9SAntti Palosaari 		c->post_bit_count.len = 1;
11226bb096c9SAntti Palosaari 		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
11236bb096c9SAntti Palosaari 		c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
11246bb096c9SAntti Palosaari 
11256bb096c9SAntti Palosaari 		c->post_bit_error.len = 1;
11266bb096c9SAntti Palosaari 		c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
11276bb096c9SAntti Palosaari 		c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
1128204f4319SAntti Palosaari 	}
1129204f4319SAntti 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