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;
39e53c4744SAntti Palosaari 	u64 post_bit_error_prev; /* for old read_ber we return (curr - prev) */
406bb096c9SAntti Palosaari 	u64 post_bit_error;
416bb096c9SAntti Palosaari 	u64 post_bit_count;
42204f4319SAntti Palosaari 	u64 error_block_count;
43204f4319SAntti Palosaari 	u64 total_block_count;
4483f11619SAntti Palosaari 	struct delayed_work stat_work;
459a0bf528SMauro Carvalho Chehab };
469a0bf528SMauro Carvalho Chehab 
479a0bf528SMauro Carvalho Chehab /* write multiple registers */
4809611caaSAntti Palosaari static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val,
499a0bf528SMauro Carvalho Chehab 		int len)
509a0bf528SMauro Carvalho Chehab {
519a0bf528SMauro Carvalho Chehab 	int ret;
5237ebaf68SMauro Carvalho Chehab 	u8 buf[MAX_XFER_SIZE];
539a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[1] = {
549a0bf528SMauro Carvalho Chehab 		{
55f5b00a76SAntti Palosaari 			.addr = dev->client->addr,
569a0bf528SMauro Carvalho Chehab 			.flags = 0,
5737ebaf68SMauro Carvalho Chehab 			.len = 3 + len,
589a0bf528SMauro Carvalho Chehab 			.buf = buf,
599a0bf528SMauro Carvalho Chehab 		}
609a0bf528SMauro Carvalho Chehab 	};
619a0bf528SMauro Carvalho Chehab 
6237ebaf68SMauro Carvalho Chehab 	if (3 + len > sizeof(buf)) {
63f5b00a76SAntti Palosaari 		dev_warn(&dev->client->dev,
646a087f1fSAntti Palosaari 				"i2c wr reg=%04x: len=%d is too big!\n",
656a087f1fSAntti Palosaari 				reg, len);
6637ebaf68SMauro Carvalho Chehab 		return -EINVAL;
6737ebaf68SMauro Carvalho Chehab 	}
6837ebaf68SMauro Carvalho Chehab 
699a0bf528SMauro Carvalho Chehab 	buf[0] = (reg >> 16) & 0xff;
709a0bf528SMauro Carvalho Chehab 	buf[1] = (reg >>  8) & 0xff;
719a0bf528SMauro Carvalho Chehab 	buf[2] = (reg >>  0) & 0xff;
729a0bf528SMauro Carvalho Chehab 	memcpy(&buf[3], val, len);
739a0bf528SMauro Carvalho Chehab 
74f5b00a76SAntti Palosaari 	ret = i2c_transfer(dev->client->adapter, msg, 1);
759a0bf528SMauro Carvalho Chehab 	if (ret == 1) {
769a0bf528SMauro Carvalho Chehab 		ret = 0;
779a0bf528SMauro Carvalho Chehab 	} else {
786a087f1fSAntti Palosaari 		dev_warn(&dev->client->dev, "i2c wr failed=%d reg=%06x len=%d\n",
796a087f1fSAntti Palosaari 				ret, reg, len);
809a0bf528SMauro Carvalho Chehab 		ret = -EREMOTEIO;
819a0bf528SMauro Carvalho Chehab 	}
829a0bf528SMauro Carvalho Chehab 
839a0bf528SMauro Carvalho Chehab 	return ret;
849a0bf528SMauro Carvalho Chehab }
859a0bf528SMauro Carvalho Chehab 
869a0bf528SMauro Carvalho Chehab /* read multiple registers */
8709611caaSAntti Palosaari static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len)
889a0bf528SMauro Carvalho Chehab {
899a0bf528SMauro Carvalho Chehab 	int ret;
909a0bf528SMauro Carvalho Chehab 	u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
919a0bf528SMauro Carvalho Chehab 			(reg >> 0) & 0xff };
929a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[2] = {
939a0bf528SMauro Carvalho Chehab 		{
94f5b00a76SAntti Palosaari 			.addr = dev->client->addr,
959a0bf528SMauro Carvalho Chehab 			.flags = 0,
969a0bf528SMauro Carvalho Chehab 			.len = sizeof(buf),
979a0bf528SMauro Carvalho Chehab 			.buf = buf
989a0bf528SMauro Carvalho Chehab 		}, {
99f5b00a76SAntti Palosaari 			.addr = dev->client->addr,
1009a0bf528SMauro Carvalho Chehab 			.flags = I2C_M_RD,
1019a0bf528SMauro Carvalho Chehab 			.len = len,
1029a0bf528SMauro Carvalho Chehab 			.buf = val
1039a0bf528SMauro Carvalho Chehab 		}
1049a0bf528SMauro Carvalho Chehab 	};
1059a0bf528SMauro Carvalho Chehab 
106f5b00a76SAntti Palosaari 	ret = i2c_transfer(dev->client->adapter, msg, 2);
1079a0bf528SMauro Carvalho Chehab 	if (ret == 2) {
1089a0bf528SMauro Carvalho Chehab 		ret = 0;
1099a0bf528SMauro Carvalho Chehab 	} else {
1106a087f1fSAntti Palosaari 		dev_warn(&dev->client->dev, "i2c rd failed=%d reg=%06x len=%d\n",
1116a087f1fSAntti Palosaari 				ret, reg, len);
1129a0bf528SMauro Carvalho Chehab 		ret = -EREMOTEIO;
1139a0bf528SMauro Carvalho Chehab 	}
1149a0bf528SMauro Carvalho Chehab 
1159a0bf528SMauro Carvalho Chehab 	return ret;
1169a0bf528SMauro Carvalho Chehab }
1179a0bf528SMauro Carvalho Chehab 
1189a0bf528SMauro Carvalho Chehab 
1199a0bf528SMauro Carvalho Chehab /* write single register */
12009611caaSAntti Palosaari static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val)
1219a0bf528SMauro Carvalho Chehab {
12209611caaSAntti Palosaari 	return af9033_wr_regs(dev, reg, &val, 1);
1239a0bf528SMauro Carvalho Chehab }
1249a0bf528SMauro Carvalho Chehab 
1259a0bf528SMauro Carvalho Chehab /* read single register */
12609611caaSAntti Palosaari static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val)
1279a0bf528SMauro Carvalho Chehab {
12809611caaSAntti Palosaari 	return af9033_rd_regs(dev, reg, val, 1);
1299a0bf528SMauro Carvalho Chehab }
1309a0bf528SMauro Carvalho Chehab 
1319a0bf528SMauro Carvalho Chehab /* write single register with mask */
13209611caaSAntti Palosaari static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val,
1339a0bf528SMauro Carvalho Chehab 		u8 mask)
1349a0bf528SMauro Carvalho Chehab {
1359a0bf528SMauro Carvalho Chehab 	int ret;
1369a0bf528SMauro Carvalho Chehab 	u8 tmp;
1379a0bf528SMauro Carvalho Chehab 
1389a0bf528SMauro Carvalho Chehab 	/* no need for read if whole reg is written */
1399a0bf528SMauro Carvalho Chehab 	if (mask != 0xff) {
14009611caaSAntti Palosaari 		ret = af9033_rd_regs(dev, reg, &tmp, 1);
1419a0bf528SMauro Carvalho Chehab 		if (ret)
1429a0bf528SMauro Carvalho Chehab 			return ret;
1439a0bf528SMauro Carvalho Chehab 
1449a0bf528SMauro Carvalho Chehab 		val &= mask;
1459a0bf528SMauro Carvalho Chehab 		tmp &= ~mask;
1469a0bf528SMauro Carvalho Chehab 		val |= tmp;
1479a0bf528SMauro Carvalho Chehab 	}
1489a0bf528SMauro Carvalho Chehab 
14909611caaSAntti Palosaari 	return af9033_wr_regs(dev, reg, &val, 1);
1509a0bf528SMauro Carvalho Chehab }
1519a0bf528SMauro Carvalho Chehab 
1529a0bf528SMauro Carvalho Chehab /* read single register with mask */
15309611caaSAntti Palosaari static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val,
1549a0bf528SMauro Carvalho Chehab 		u8 mask)
1559a0bf528SMauro Carvalho Chehab {
1569a0bf528SMauro Carvalho Chehab 	int ret, i;
1579a0bf528SMauro Carvalho Chehab 	u8 tmp;
1589a0bf528SMauro Carvalho Chehab 
15909611caaSAntti Palosaari 	ret = af9033_rd_regs(dev, reg, &tmp, 1);
1609a0bf528SMauro Carvalho Chehab 	if (ret)
1619a0bf528SMauro Carvalho Chehab 		return ret;
1629a0bf528SMauro Carvalho Chehab 
1639a0bf528SMauro Carvalho Chehab 	tmp &= mask;
1649a0bf528SMauro Carvalho Chehab 
1659a0bf528SMauro Carvalho Chehab 	/* find position of the first bit */
1669a0bf528SMauro Carvalho Chehab 	for (i = 0; i < 8; i++) {
1679a0bf528SMauro Carvalho Chehab 		if ((mask >> i) & 0x01)
1689a0bf528SMauro Carvalho Chehab 			break;
1699a0bf528SMauro Carvalho Chehab 	}
1709a0bf528SMauro Carvalho Chehab 	*val = tmp >> i;
1719a0bf528SMauro Carvalho Chehab 
1729a0bf528SMauro Carvalho Chehab 	return 0;
1739a0bf528SMauro Carvalho Chehab }
1749a0bf528SMauro Carvalho Chehab 
1753bf5e552SAntti Palosaari /* write reg val table using reg addr auto increment */
17609611caaSAntti Palosaari static int af9033_wr_reg_val_tab(struct af9033_dev *dev,
1773bf5e552SAntti Palosaari 		const struct reg_val *tab, int tab_len)
1783bf5e552SAntti Palosaari {
179d18a88b1SAntti Palosaari #define MAX_TAB_LEN 212
1803bf5e552SAntti Palosaari 	int ret, i, j;
181d18a88b1SAntti Palosaari 	u8 buf[1 + MAX_TAB_LEN];
182d18a88b1SAntti Palosaari 
1836a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "tab_len=%d\n", tab_len);
18437ebaf68SMauro Carvalho Chehab 
18537ebaf68SMauro Carvalho Chehab 	if (tab_len > sizeof(buf)) {
1866a087f1fSAntti Palosaari 		dev_warn(&dev->client->dev, "tab len %d is too big\n", tab_len);
18737ebaf68SMauro Carvalho Chehab 		return -EINVAL;
18837ebaf68SMauro Carvalho Chehab 	}
1893bf5e552SAntti Palosaari 
1903bf5e552SAntti Palosaari 	for (i = 0, j = 0; i < tab_len; i++) {
1913bf5e552SAntti Palosaari 		buf[j] = tab[i].val;
1923bf5e552SAntti Palosaari 
1933bf5e552SAntti Palosaari 		if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
19409611caaSAntti Palosaari 			ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1);
1953bf5e552SAntti Palosaari 			if (ret < 0)
1963bf5e552SAntti Palosaari 				goto err;
1973bf5e552SAntti Palosaari 
1983bf5e552SAntti Palosaari 			j = 0;
1993bf5e552SAntti Palosaari 		} else {
2003bf5e552SAntti Palosaari 			j++;
2013bf5e552SAntti Palosaari 		}
2023bf5e552SAntti Palosaari 	}
2033bf5e552SAntti Palosaari 
2043bf5e552SAntti Palosaari 	return 0;
2053bf5e552SAntti Palosaari 
2063bf5e552SAntti Palosaari err:
2076a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
2083bf5e552SAntti Palosaari 
2093bf5e552SAntti Palosaari 	return ret;
2103bf5e552SAntti Palosaari }
2113bf5e552SAntti Palosaari 
21209611caaSAntti Palosaari static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x)
2139a0bf528SMauro Carvalho Chehab {
2149a0bf528SMauro Carvalho Chehab 	u32 r = 0, c = 0, i;
2159a0bf528SMauro Carvalho Chehab 
2166a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "a=%d b=%d x=%d\n", a, b, x);
2179a0bf528SMauro Carvalho Chehab 
2189a0bf528SMauro Carvalho Chehab 	if (a > b) {
2199a0bf528SMauro Carvalho Chehab 		c = a / b;
2209a0bf528SMauro Carvalho Chehab 		a = a - c * b;
2219a0bf528SMauro Carvalho Chehab 	}
2229a0bf528SMauro Carvalho Chehab 
2239a0bf528SMauro Carvalho Chehab 	for (i = 0; i < x; i++) {
2249a0bf528SMauro Carvalho Chehab 		if (a >= b) {
2259a0bf528SMauro Carvalho Chehab 			r += 1;
2269a0bf528SMauro Carvalho Chehab 			a -= b;
2279a0bf528SMauro Carvalho Chehab 		}
2289a0bf528SMauro Carvalho Chehab 		a <<= 1;
2299a0bf528SMauro Carvalho Chehab 		r <<= 1;
2309a0bf528SMauro Carvalho Chehab 	}
2319a0bf528SMauro Carvalho Chehab 	r = (c << (u32)x) + r;
2329a0bf528SMauro Carvalho Chehab 
2336a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "a=%d b=%d x=%d r=%d r=%x\n", a, b, x, r, r);
2349a0bf528SMauro Carvalho Chehab 
2359a0bf528SMauro Carvalho Chehab 	return r;
2369a0bf528SMauro Carvalho Chehab }
2379a0bf528SMauro Carvalho Chehab 
2389a0bf528SMauro Carvalho Chehab static int af9033_init(struct dvb_frontend *fe)
2399a0bf528SMauro Carvalho Chehab {
24009611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
2412db4d179SAntti Palosaari 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2429a0bf528SMauro Carvalho Chehab 	int ret, i, len;
2439a0bf528SMauro Carvalho Chehab 	const struct reg_val *init;
2449a0bf528SMauro Carvalho Chehab 	u8 buf[4];
2459a0bf528SMauro Carvalho Chehab 	u32 adc_cw, clock_cw;
2469a0bf528SMauro Carvalho Chehab 	struct reg_val_mask tab[] = {
2479a0bf528SMauro Carvalho Chehab 		{ 0x80fb24, 0x00, 0x08 },
2489a0bf528SMauro Carvalho Chehab 		{ 0x80004c, 0x00, 0xff },
24909611caaSAntti Palosaari 		{ 0x00f641, dev->cfg.tuner, 0xff },
2509a0bf528SMauro Carvalho Chehab 		{ 0x80f5ca, 0x01, 0x01 },
2519a0bf528SMauro Carvalho Chehab 		{ 0x80f715, 0x01, 0x01 },
2529a0bf528SMauro Carvalho Chehab 		{ 0x00f41f, 0x04, 0x04 },
2539a0bf528SMauro Carvalho Chehab 		{ 0x00f41a, 0x01, 0x01 },
2549a0bf528SMauro Carvalho Chehab 		{ 0x80f731, 0x00, 0x01 },
2559a0bf528SMauro Carvalho Chehab 		{ 0x00d91e, 0x00, 0x01 },
2569a0bf528SMauro Carvalho Chehab 		{ 0x00d919, 0x00, 0x01 },
2579a0bf528SMauro Carvalho Chehab 		{ 0x80f732, 0x00, 0x01 },
2589a0bf528SMauro Carvalho Chehab 		{ 0x00d91f, 0x00, 0x01 },
2599a0bf528SMauro Carvalho Chehab 		{ 0x00d91a, 0x00, 0x01 },
2609a0bf528SMauro Carvalho Chehab 		{ 0x80f730, 0x00, 0x01 },
2619a0bf528SMauro Carvalho Chehab 		{ 0x80f778, 0x00, 0xff },
2629a0bf528SMauro Carvalho Chehab 		{ 0x80f73c, 0x01, 0x01 },
2639a0bf528SMauro Carvalho Chehab 		{ 0x80f776, 0x00, 0x01 },
2649a0bf528SMauro Carvalho Chehab 		{ 0x00d8fd, 0x01, 0xff },
2659a0bf528SMauro Carvalho Chehab 		{ 0x00d830, 0x01, 0xff },
2669a0bf528SMauro Carvalho Chehab 		{ 0x00d831, 0x00, 0xff },
2679a0bf528SMauro Carvalho Chehab 		{ 0x00d832, 0x00, 0xff },
26809611caaSAntti Palosaari 		{ 0x80f985, dev->ts_mode_serial, 0x01 },
26909611caaSAntti Palosaari 		{ 0x80f986, dev->ts_mode_parallel, 0x01 },
2709a0bf528SMauro Carvalho Chehab 		{ 0x00d827, 0x00, 0xff },
2719a0bf528SMauro Carvalho Chehab 		{ 0x00d829, 0x00, 0xff },
27209611caaSAntti Palosaari 		{ 0x800045, dev->cfg.adc_multiplier, 0xff },
2739a0bf528SMauro Carvalho Chehab 	};
2749a0bf528SMauro Carvalho Chehab 
2759a0bf528SMauro Carvalho Chehab 	/* program clock control */
27609611caaSAntti Palosaari 	clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul);
2779a0bf528SMauro Carvalho Chehab 	buf[0] = (clock_cw >>  0) & 0xff;
2789a0bf528SMauro Carvalho Chehab 	buf[1] = (clock_cw >>  8) & 0xff;
2799a0bf528SMauro Carvalho Chehab 	buf[2] = (clock_cw >> 16) & 0xff;
2809a0bf528SMauro Carvalho Chehab 	buf[3] = (clock_cw >> 24) & 0xff;
2819a0bf528SMauro Carvalho Chehab 
2826a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "clock=%d clock_cw=%08x\n",
2836a087f1fSAntti Palosaari 			dev->cfg.clock, clock_cw);
2849a0bf528SMauro Carvalho Chehab 
28509611caaSAntti Palosaari 	ret = af9033_wr_regs(dev, 0x800025, buf, 4);
2869a0bf528SMauro Carvalho Chehab 	if (ret < 0)
2879a0bf528SMauro Carvalho Chehab 		goto err;
2889a0bf528SMauro Carvalho Chehab 
2899a0bf528SMauro Carvalho Chehab 	/* program ADC control */
2909a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
29109611caaSAntti Palosaari 		if (clock_adc_lut[i].clock == dev->cfg.clock)
2929a0bf528SMauro Carvalho Chehab 			break;
2939a0bf528SMauro Carvalho Chehab 	}
2949a0bf528SMauro Carvalho Chehab 
29509611caaSAntti Palosaari 	adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul);
2969a0bf528SMauro Carvalho Chehab 	buf[0] = (adc_cw >>  0) & 0xff;
2979a0bf528SMauro Carvalho Chehab 	buf[1] = (adc_cw >>  8) & 0xff;
2989a0bf528SMauro Carvalho Chehab 	buf[2] = (adc_cw >> 16) & 0xff;
2999a0bf528SMauro Carvalho Chehab 
3006a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "adc=%d adc_cw=%06x\n",
3016a087f1fSAntti Palosaari 			clock_adc_lut[i].adc, adc_cw);
3029a0bf528SMauro Carvalho Chehab 
30309611caaSAntti Palosaari 	ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3);
3049a0bf528SMauro Carvalho Chehab 	if (ret < 0)
3059a0bf528SMauro Carvalho Chehab 		goto err;
3069a0bf528SMauro Carvalho Chehab 
3079a0bf528SMauro Carvalho Chehab 	/* program register table */
3089a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
30909611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val,
3109a0bf528SMauro Carvalho Chehab 				tab[i].mask);
3119a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3129a0bf528SMauro Carvalho Chehab 			goto err;
3139a0bf528SMauro Carvalho Chehab 	}
3149a0bf528SMauro Carvalho Chehab 
315ca681fe0SAntti Palosaari 	/* clock output */
31609611caaSAntti Palosaari 	if (dev->cfg.dyn0_clk) {
31709611caaSAntti Palosaari 		ret = af9033_wr_reg(dev, 0x80fba8, 0x00);
3189dc0f3feSAntti Palosaari 		if (ret < 0)
3199dc0f3feSAntti Palosaari 			goto err;
3209dc0f3feSAntti Palosaari 	}
3219dc0f3feSAntti Palosaari 
3229a0bf528SMauro Carvalho Chehab 	/* settings for TS interface */
32309611caaSAntti Palosaari 	if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) {
32409611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01);
3259a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3269a0bf528SMauro Carvalho Chehab 			goto err;
3279a0bf528SMauro Carvalho Chehab 
32809611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01);
3299a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3309a0bf528SMauro Carvalho Chehab 			goto err;
3319a0bf528SMauro Carvalho Chehab 	} else {
33209611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01);
3339a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3349a0bf528SMauro Carvalho Chehab 			goto err;
3359a0bf528SMauro Carvalho Chehab 
33609611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01);
3379a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3389a0bf528SMauro Carvalho Chehab 			goto err;
3399a0bf528SMauro Carvalho Chehab 	}
3409a0bf528SMauro Carvalho Chehab 
3419a0bf528SMauro Carvalho Chehab 	/* load OFSM settings */
3426a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "load ofsm settings\n");
34309611caaSAntti Palosaari 	switch (dev->cfg.tuner) {
344fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_38:
345fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_51:
346fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_52:
347463c399cSAntti Palosaari 		len = ARRAY_SIZE(ofsm_init_it9135_v1);
348463c399cSAntti Palosaari 		init = ofsm_init_it9135_v1;
349463c399cSAntti Palosaari 		break;
350fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_60:
351fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_61:
352fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_62:
353463c399cSAntti Palosaari 		len = ARRAY_SIZE(ofsm_init_it9135_v2);
354463c399cSAntti Palosaari 		init = ofsm_init_it9135_v2;
355fe8eece1SAntti Palosaari 		break;
356fe8eece1SAntti Palosaari 	default:
3579a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(ofsm_init);
3589a0bf528SMauro Carvalho Chehab 		init = ofsm_init;
359fe8eece1SAntti Palosaari 		break;
360fe8eece1SAntti Palosaari 	}
361fe8eece1SAntti Palosaari 
36209611caaSAntti Palosaari 	ret = af9033_wr_reg_val_tab(dev, init, len);
3639a0bf528SMauro Carvalho Chehab 	if (ret < 0)
3649a0bf528SMauro Carvalho Chehab 		goto err;
3659a0bf528SMauro Carvalho Chehab 
3669a0bf528SMauro Carvalho Chehab 	/* load tuner specific settings */
3676a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "load tuner specific settings\n");
36809611caaSAntti Palosaari 	switch (dev->cfg.tuner) {
3699a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_TUA9001:
3709a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_tua9001);
3719a0bf528SMauro Carvalho Chehab 		init = tuner_init_tua9001;
3729a0bf528SMauro Carvalho Chehab 		break;
3739a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_FC0011:
3749a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_fc0011);
3759a0bf528SMauro Carvalho Chehab 		init = tuner_init_fc0011;
3769a0bf528SMauro Carvalho Chehab 		break;
3779a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_MXL5007T:
3789a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_mxl5007t);
3799a0bf528SMauro Carvalho Chehab 		init = tuner_init_mxl5007t;
3809a0bf528SMauro Carvalho Chehab 		break;
3819a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_TDA18218:
3829a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_tda18218);
3839a0bf528SMauro Carvalho Chehab 		init = tuner_init_tda18218;
3849a0bf528SMauro Carvalho Chehab 		break;
385d67ceb33SOliver Schinagl 	case AF9033_TUNER_FC2580:
386d67ceb33SOliver Schinagl 		len = ARRAY_SIZE(tuner_init_fc2580);
387d67ceb33SOliver Schinagl 		init = tuner_init_fc2580;
388d67ceb33SOliver Schinagl 		break;
389e713ad15SAntti Palosaari 	case AF9033_TUNER_FC0012:
390e713ad15SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_fc0012);
391e713ad15SAntti Palosaari 		init = tuner_init_fc0012;
392e713ad15SAntti Palosaari 		break;
3934902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_38:
394a72cbb77SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_38);
395a72cbb77SAntti Palosaari 		init = tuner_init_it9135_38;
396a72cbb77SAntti Palosaari 		break;
3974902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_51:
398bb2e12a6SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_51);
399bb2e12a6SAntti Palosaari 		init = tuner_init_it9135_51;
400bb2e12a6SAntti Palosaari 		break;
4014902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_52:
40222d729f3SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_52);
40322d729f3SAntti Palosaari 		init = tuner_init_it9135_52;
40422d729f3SAntti Palosaari 		break;
4054902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_60:
406a49f53a0SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_60);
407a49f53a0SAntti Palosaari 		init = tuner_init_it9135_60;
408a49f53a0SAntti Palosaari 		break;
4094902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_61:
41085211323SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_61);
41185211323SAntti Palosaari 		init = tuner_init_it9135_61;
41285211323SAntti Palosaari 		break;
4134902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_62:
414dc4a2c40SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_62);
415dc4a2c40SAntti Palosaari 		init = tuner_init_it9135_62;
4164902bb39SAntti Palosaari 		break;
4179a0bf528SMauro Carvalho Chehab 	default:
4186a087f1fSAntti Palosaari 		dev_dbg(&dev->client->dev, "unsupported tuner ID=%d\n",
4196a087f1fSAntti Palosaari 				dev->cfg.tuner);
4209a0bf528SMauro Carvalho Chehab 		ret = -ENODEV;
4219a0bf528SMauro Carvalho Chehab 		goto err;
4229a0bf528SMauro Carvalho Chehab 	}
4239a0bf528SMauro Carvalho Chehab 
42409611caaSAntti Palosaari 	ret = af9033_wr_reg_val_tab(dev, init, len);
4259a0bf528SMauro Carvalho Chehab 	if (ret < 0)
4269a0bf528SMauro Carvalho Chehab 		goto err;
4279a0bf528SMauro Carvalho Chehab 
42809611caaSAntti Palosaari 	if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
42909611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01);
4309805992fSJose Alberto Reguero 		if (ret < 0)
4319805992fSJose Alberto Reguero 			goto err;
432bf97b637SAntti Palosaari 
43309611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
4349805992fSJose Alberto Reguero 		if (ret < 0)
4359805992fSJose Alberto Reguero 			goto err;
436bf97b637SAntti Palosaari 
43709611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01);
4389805992fSJose Alberto Reguero 		if (ret < 0)
4399805992fSJose Alberto Reguero 			goto err;
4409805992fSJose Alberto Reguero 	}
4419805992fSJose Alberto Reguero 
44209611caaSAntti Palosaari 	switch (dev->cfg.tuner) {
443086991ddSAntti Palosaari 	case AF9033_TUNER_IT9135_60:
444086991ddSAntti Palosaari 	case AF9033_TUNER_IT9135_61:
445086991ddSAntti Palosaari 	case AF9033_TUNER_IT9135_62:
44609611caaSAntti Palosaari 		ret = af9033_wr_reg(dev, 0x800000, 0x01);
447086991ddSAntti Palosaari 		if (ret < 0)
448086991ddSAntti Palosaari 			goto err;
449086991ddSAntti Palosaari 	}
450086991ddSAntti Palosaari 
45109611caaSAntti Palosaari 	dev->bandwidth_hz = 0; /* force to program all parameters */
4522db4d179SAntti Palosaari 	/* init stats here in order signal app which stats are supported */
4532db4d179SAntti Palosaari 	c->strength.len = 1;
4542db4d179SAntti Palosaari 	c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
4552db4d179SAntti Palosaari 	c->cnr.len = 1;
4562db4d179SAntti Palosaari 	c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
4572db4d179SAntti Palosaari 	c->block_count.len = 1;
4582db4d179SAntti Palosaari 	c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
4592db4d179SAntti Palosaari 	c->block_error.len = 1;
4602db4d179SAntti Palosaari 	c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
4612db4d179SAntti Palosaari 	c->post_bit_count.len = 1;
4622db4d179SAntti Palosaari 	c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
4632db4d179SAntti Palosaari 	c->post_bit_error.len = 1;
4642db4d179SAntti Palosaari 	c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
46583f11619SAntti Palosaari 	/* start statistics polling */
46683f11619SAntti Palosaari 	schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
4679a0bf528SMauro Carvalho Chehab 
4689a0bf528SMauro Carvalho Chehab 	return 0;
4699a0bf528SMauro Carvalho Chehab 
4709a0bf528SMauro Carvalho Chehab err:
4716a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
4729a0bf528SMauro Carvalho Chehab 
4739a0bf528SMauro Carvalho Chehab 	return ret;
4749a0bf528SMauro Carvalho Chehab }
4759a0bf528SMauro Carvalho Chehab 
4769a0bf528SMauro Carvalho Chehab static int af9033_sleep(struct dvb_frontend *fe)
4779a0bf528SMauro Carvalho Chehab {
47809611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
4799a0bf528SMauro Carvalho Chehab 	int ret, i;
4809a0bf528SMauro Carvalho Chehab 	u8 tmp;
4819a0bf528SMauro Carvalho Chehab 
48283f11619SAntti Palosaari 	/* stop statistics polling */
48383f11619SAntti Palosaari 	cancel_delayed_work_sync(&dev->stat_work);
48483f11619SAntti Palosaari 
48509611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x80004c, 1);
4869a0bf528SMauro Carvalho Chehab 	if (ret < 0)
4879a0bf528SMauro Carvalho Chehab 		goto err;
4889a0bf528SMauro Carvalho Chehab 
48909611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x800000, 0);
4909a0bf528SMauro Carvalho Chehab 	if (ret < 0)
4919a0bf528SMauro Carvalho Chehab 		goto err;
4929a0bf528SMauro Carvalho Chehab 
4939a0bf528SMauro Carvalho Chehab 	for (i = 100, tmp = 1; i && tmp; i--) {
49409611caaSAntti Palosaari 		ret = af9033_rd_reg(dev, 0x80004c, &tmp);
4959a0bf528SMauro Carvalho Chehab 		if (ret < 0)
4969a0bf528SMauro Carvalho Chehab 			goto err;
4979a0bf528SMauro Carvalho Chehab 
4989a0bf528SMauro Carvalho Chehab 		usleep_range(200, 10000);
4999a0bf528SMauro Carvalho Chehab 	}
5009a0bf528SMauro Carvalho Chehab 
5016a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "loop=%d\n", i);
5029a0bf528SMauro Carvalho Chehab 
5039a0bf528SMauro Carvalho Chehab 	if (i == 0) {
5049a0bf528SMauro Carvalho Chehab 		ret = -ETIMEDOUT;
5059a0bf528SMauro Carvalho Chehab 		goto err;
5069a0bf528SMauro Carvalho Chehab 	}
5079a0bf528SMauro Carvalho Chehab 
50809611caaSAntti Palosaari 	ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08);
5099a0bf528SMauro Carvalho Chehab 	if (ret < 0)
5109a0bf528SMauro Carvalho Chehab 		goto err;
5119a0bf528SMauro Carvalho Chehab 
5129a0bf528SMauro Carvalho Chehab 	/* prevent current leak (?) */
51309611caaSAntti Palosaari 	if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
5149a0bf528SMauro Carvalho Chehab 		/* enable parallel TS */
51509611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
5169a0bf528SMauro Carvalho Chehab 		if (ret < 0)
5179a0bf528SMauro Carvalho Chehab 			goto err;
5189a0bf528SMauro Carvalho Chehab 
51909611caaSAntti Palosaari 		ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01);
5209a0bf528SMauro Carvalho Chehab 		if (ret < 0)
5219a0bf528SMauro Carvalho Chehab 			goto err;
5229a0bf528SMauro Carvalho Chehab 	}
5239a0bf528SMauro Carvalho Chehab 
5249a0bf528SMauro Carvalho Chehab 	return 0;
5259a0bf528SMauro Carvalho Chehab 
5269a0bf528SMauro Carvalho Chehab err:
5276a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
5289a0bf528SMauro Carvalho Chehab 
5299a0bf528SMauro Carvalho Chehab 	return ret;
5309a0bf528SMauro Carvalho Chehab }
5319a0bf528SMauro Carvalho Chehab 
5329a0bf528SMauro Carvalho Chehab static int af9033_get_tune_settings(struct dvb_frontend *fe,
5339a0bf528SMauro Carvalho Chehab 		struct dvb_frontend_tune_settings *fesettings)
5349a0bf528SMauro Carvalho Chehab {
535fe8eece1SAntti Palosaari 	/* 800 => 2000 because IT9135 v2 is slow to gain lock */
536fe8eece1SAntti Palosaari 	fesettings->min_delay_ms = 2000;
5379a0bf528SMauro Carvalho Chehab 	fesettings->step_size = 0;
5389a0bf528SMauro Carvalho Chehab 	fesettings->max_drift = 0;
5399a0bf528SMauro Carvalho Chehab 
5409a0bf528SMauro Carvalho Chehab 	return 0;
5419a0bf528SMauro Carvalho Chehab }
5429a0bf528SMauro Carvalho Chehab 
5439a0bf528SMauro Carvalho Chehab static int af9033_set_frontend(struct dvb_frontend *fe)
5449a0bf528SMauro Carvalho Chehab {
54509611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
5469a0bf528SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
547182b967eSHans-Frieder Vogt 	int ret, i, spec_inv, sampling_freq;
5489a0bf528SMauro Carvalho Chehab 	u8 tmp, buf[3], bandwidth_reg_val;
5499a0bf528SMauro Carvalho Chehab 	u32 if_frequency, freq_cw, adc_freq;
5509a0bf528SMauro Carvalho Chehab 
5516a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "frequency=%d bandwidth_hz=%d\n",
5526a087f1fSAntti Palosaari 			c->frequency, c->bandwidth_hz);
5539a0bf528SMauro Carvalho Chehab 
5549a0bf528SMauro Carvalho Chehab 	/* check bandwidth */
5559a0bf528SMauro Carvalho Chehab 	switch (c->bandwidth_hz) {
5569a0bf528SMauro Carvalho Chehab 	case 6000000:
5579a0bf528SMauro Carvalho Chehab 		bandwidth_reg_val = 0x00;
5589a0bf528SMauro Carvalho Chehab 		break;
5599a0bf528SMauro Carvalho Chehab 	case 7000000:
5609a0bf528SMauro Carvalho Chehab 		bandwidth_reg_val = 0x01;
5619a0bf528SMauro Carvalho Chehab 		break;
5629a0bf528SMauro Carvalho Chehab 	case 8000000:
5639a0bf528SMauro Carvalho Chehab 		bandwidth_reg_val = 0x02;
5649a0bf528SMauro Carvalho Chehab 		break;
5659a0bf528SMauro Carvalho Chehab 	default:
5666a087f1fSAntti Palosaari 		dev_dbg(&dev->client->dev, "invalid bandwidth_hz\n");
5679a0bf528SMauro Carvalho Chehab 		ret = -EINVAL;
5689a0bf528SMauro Carvalho Chehab 		goto err;
5699a0bf528SMauro Carvalho Chehab 	}
5709a0bf528SMauro Carvalho Chehab 
5719a0bf528SMauro Carvalho Chehab 	/* program tuner */
5729a0bf528SMauro Carvalho Chehab 	if (fe->ops.tuner_ops.set_params)
5739a0bf528SMauro Carvalho Chehab 		fe->ops.tuner_ops.set_params(fe);
5749a0bf528SMauro Carvalho Chehab 
5759a0bf528SMauro Carvalho Chehab 	/* program CFOE coefficients */
57609611caaSAntti Palosaari 	if (c->bandwidth_hz != dev->bandwidth_hz) {
5779a0bf528SMauro Carvalho Chehab 		for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
57809611caaSAntti Palosaari 			if (coeff_lut[i].clock == dev->cfg.clock &&
5799a0bf528SMauro Carvalho Chehab 				coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
5809a0bf528SMauro Carvalho Chehab 				break;
5819a0bf528SMauro Carvalho Chehab 			}
5829a0bf528SMauro Carvalho Chehab 		}
58309611caaSAntti Palosaari 		ret =  af9033_wr_regs(dev, 0x800001,
5849a0bf528SMauro Carvalho Chehab 				coeff_lut[i].val, sizeof(coeff_lut[i].val));
5859a0bf528SMauro Carvalho Chehab 	}
5869a0bf528SMauro Carvalho Chehab 
5879a0bf528SMauro Carvalho Chehab 	/* program frequency control */
58809611caaSAntti Palosaari 	if (c->bandwidth_hz != dev->bandwidth_hz) {
58909611caaSAntti Palosaari 		spec_inv = dev->cfg.spec_inv ? -1 : 1;
5909a0bf528SMauro Carvalho Chehab 
5919a0bf528SMauro Carvalho Chehab 		for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
59209611caaSAntti Palosaari 			if (clock_adc_lut[i].clock == dev->cfg.clock)
5939a0bf528SMauro Carvalho Chehab 				break;
5949a0bf528SMauro Carvalho Chehab 		}
5959a0bf528SMauro Carvalho Chehab 		adc_freq = clock_adc_lut[i].adc;
5969a0bf528SMauro Carvalho Chehab 
5979a0bf528SMauro Carvalho Chehab 		/* get used IF frequency */
5989a0bf528SMauro Carvalho Chehab 		if (fe->ops.tuner_ops.get_if_frequency)
5999a0bf528SMauro Carvalho Chehab 			fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
6009a0bf528SMauro Carvalho Chehab 		else
6019a0bf528SMauro Carvalho Chehab 			if_frequency = 0;
6029a0bf528SMauro Carvalho Chehab 
603182b967eSHans-Frieder Vogt 		sampling_freq = if_frequency;
6049a0bf528SMauro Carvalho Chehab 
605182b967eSHans-Frieder Vogt 		while (sampling_freq > (adc_freq / 2))
606182b967eSHans-Frieder Vogt 			sampling_freq -= adc_freq;
607182b967eSHans-Frieder Vogt 
608182b967eSHans-Frieder Vogt 		if (sampling_freq >= 0)
6099a0bf528SMauro Carvalho Chehab 			spec_inv *= -1;
6109a0bf528SMauro Carvalho Chehab 		else
611182b967eSHans-Frieder Vogt 			sampling_freq *= -1;
6129a0bf528SMauro Carvalho Chehab 
61309611caaSAntti Palosaari 		freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul);
6149a0bf528SMauro Carvalho Chehab 
6159a0bf528SMauro Carvalho Chehab 		if (spec_inv == -1)
616182b967eSHans-Frieder Vogt 			freq_cw = 0x800000 - freq_cw;
6179a0bf528SMauro Carvalho Chehab 
61809611caaSAntti Palosaari 		if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
6199a0bf528SMauro Carvalho Chehab 			freq_cw /= 2;
6209a0bf528SMauro Carvalho Chehab 
6219a0bf528SMauro Carvalho Chehab 		buf[0] = (freq_cw >>  0) & 0xff;
6229a0bf528SMauro Carvalho Chehab 		buf[1] = (freq_cw >>  8) & 0xff;
6239a0bf528SMauro Carvalho Chehab 		buf[2] = (freq_cw >> 16) & 0x7f;
624fe8eece1SAntti Palosaari 
625fe8eece1SAntti Palosaari 		/* FIXME: there seems to be calculation error here... */
626fe8eece1SAntti Palosaari 		if (if_frequency == 0)
627fe8eece1SAntti Palosaari 			buf[2] = 0;
628fe8eece1SAntti Palosaari 
62909611caaSAntti Palosaari 		ret = af9033_wr_regs(dev, 0x800029, buf, 3);
6309a0bf528SMauro Carvalho Chehab 		if (ret < 0)
6319a0bf528SMauro Carvalho Chehab 			goto err;
6329a0bf528SMauro Carvalho Chehab 
63309611caaSAntti Palosaari 		dev->bandwidth_hz = c->bandwidth_hz;
6349a0bf528SMauro Carvalho Chehab 	}
6359a0bf528SMauro Carvalho Chehab 
63609611caaSAntti Palosaari 	ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03);
6379a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6389a0bf528SMauro Carvalho Chehab 		goto err;
6399a0bf528SMauro Carvalho Chehab 
64009611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x800040, 0x00);
6419a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6429a0bf528SMauro Carvalho Chehab 		goto err;
6439a0bf528SMauro Carvalho Chehab 
64409611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x800047, 0x00);
6459a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6469a0bf528SMauro Carvalho Chehab 		goto err;
6479a0bf528SMauro Carvalho Chehab 
64809611caaSAntti Palosaari 	ret = af9033_wr_reg_mask(dev, 0x80f999, 0x00, 0x01);
6499a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6509a0bf528SMauro Carvalho Chehab 		goto err;
6519a0bf528SMauro Carvalho Chehab 
6529a0bf528SMauro Carvalho Chehab 	if (c->frequency <= 230000000)
6539a0bf528SMauro Carvalho Chehab 		tmp = 0x00; /* VHF */
6549a0bf528SMauro Carvalho Chehab 	else
6559a0bf528SMauro Carvalho Chehab 		tmp = 0x01; /* UHF */
6569a0bf528SMauro Carvalho Chehab 
65709611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x80004b, tmp);
6589a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6599a0bf528SMauro Carvalho Chehab 		goto err;
6609a0bf528SMauro Carvalho Chehab 
66109611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x800000, 0x00);
6629a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6639a0bf528SMauro Carvalho Chehab 		goto err;
6649a0bf528SMauro Carvalho Chehab 
6659a0bf528SMauro Carvalho Chehab 	return 0;
6669a0bf528SMauro Carvalho Chehab 
6679a0bf528SMauro Carvalho Chehab err:
6686a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
6699a0bf528SMauro Carvalho Chehab 
6709a0bf528SMauro Carvalho Chehab 	return ret;
6719a0bf528SMauro Carvalho Chehab }
6729a0bf528SMauro Carvalho Chehab 
6739a0bf528SMauro Carvalho Chehab static int af9033_get_frontend(struct dvb_frontend *fe)
6749a0bf528SMauro Carvalho Chehab {
67509611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
6769a0bf528SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
6779a0bf528SMauro Carvalho Chehab 	int ret;
6789a0bf528SMauro Carvalho Chehab 	u8 buf[8];
6799a0bf528SMauro Carvalho Chehab 
6806a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "\n");
6819a0bf528SMauro Carvalho Chehab 
6829a0bf528SMauro Carvalho Chehab 	/* read all needed registers */
68309611caaSAntti Palosaari 	ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf));
6849a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6859a0bf528SMauro Carvalho Chehab 		goto err;
6869a0bf528SMauro Carvalho Chehab 
6879a0bf528SMauro Carvalho Chehab 	switch ((buf[0] >> 0) & 3) {
6889a0bf528SMauro Carvalho Chehab 	case 0:
6899a0bf528SMauro Carvalho Chehab 		c->transmission_mode = TRANSMISSION_MODE_2K;
6909a0bf528SMauro Carvalho Chehab 		break;
6919a0bf528SMauro Carvalho Chehab 	case 1:
6929a0bf528SMauro Carvalho Chehab 		c->transmission_mode = TRANSMISSION_MODE_8K;
6939a0bf528SMauro Carvalho Chehab 		break;
6949a0bf528SMauro Carvalho Chehab 	}
6959a0bf528SMauro Carvalho Chehab 
6969a0bf528SMauro Carvalho Chehab 	switch ((buf[1] >> 0) & 3) {
6979a0bf528SMauro Carvalho Chehab 	case 0:
6989a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_32;
6999a0bf528SMauro Carvalho Chehab 		break;
7009a0bf528SMauro Carvalho Chehab 	case 1:
7019a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_16;
7029a0bf528SMauro Carvalho Chehab 		break;
7039a0bf528SMauro Carvalho Chehab 	case 2:
7049a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_8;
7059a0bf528SMauro Carvalho Chehab 		break;
7069a0bf528SMauro Carvalho Chehab 	case 3:
7079a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_4;
7089a0bf528SMauro Carvalho Chehab 		break;
7099a0bf528SMauro Carvalho Chehab 	}
7109a0bf528SMauro Carvalho Chehab 
7119a0bf528SMauro Carvalho Chehab 	switch ((buf[2] >> 0) & 7) {
7129a0bf528SMauro Carvalho Chehab 	case 0:
7139a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_NONE;
7149a0bf528SMauro Carvalho Chehab 		break;
7159a0bf528SMauro Carvalho Chehab 	case 1:
7169a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_1;
7179a0bf528SMauro Carvalho Chehab 		break;
7189a0bf528SMauro Carvalho Chehab 	case 2:
7199a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_2;
7209a0bf528SMauro Carvalho Chehab 		break;
7219a0bf528SMauro Carvalho Chehab 	case 3:
7229a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_4;
7239a0bf528SMauro Carvalho Chehab 		break;
7249a0bf528SMauro Carvalho Chehab 	}
7259a0bf528SMauro Carvalho Chehab 
7269a0bf528SMauro Carvalho Chehab 	switch ((buf[3] >> 0) & 3) {
7279a0bf528SMauro Carvalho Chehab 	case 0:
7289a0bf528SMauro Carvalho Chehab 		c->modulation = QPSK;
7299a0bf528SMauro Carvalho Chehab 		break;
7309a0bf528SMauro Carvalho Chehab 	case 1:
7319a0bf528SMauro Carvalho Chehab 		c->modulation = QAM_16;
7329a0bf528SMauro Carvalho Chehab 		break;
7339a0bf528SMauro Carvalho Chehab 	case 2:
7349a0bf528SMauro Carvalho Chehab 		c->modulation = QAM_64;
7359a0bf528SMauro Carvalho Chehab 		break;
7369a0bf528SMauro Carvalho Chehab 	}
7379a0bf528SMauro Carvalho Chehab 
7389a0bf528SMauro Carvalho Chehab 	switch ((buf[4] >> 0) & 3) {
7399a0bf528SMauro Carvalho Chehab 	case 0:
7409a0bf528SMauro Carvalho Chehab 		c->bandwidth_hz = 6000000;
7419a0bf528SMauro Carvalho Chehab 		break;
7429a0bf528SMauro Carvalho Chehab 	case 1:
7439a0bf528SMauro Carvalho Chehab 		c->bandwidth_hz = 7000000;
7449a0bf528SMauro Carvalho Chehab 		break;
7459a0bf528SMauro Carvalho Chehab 	case 2:
7469a0bf528SMauro Carvalho Chehab 		c->bandwidth_hz = 8000000;
7479a0bf528SMauro Carvalho Chehab 		break;
7489a0bf528SMauro Carvalho Chehab 	}
7499a0bf528SMauro Carvalho Chehab 
7509a0bf528SMauro Carvalho Chehab 	switch ((buf[6] >> 0) & 7) {
7519a0bf528SMauro Carvalho Chehab 	case 0:
7529a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_1_2;
7539a0bf528SMauro Carvalho Chehab 		break;
7549a0bf528SMauro Carvalho Chehab 	case 1:
7559a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_2_3;
7569a0bf528SMauro Carvalho Chehab 		break;
7579a0bf528SMauro Carvalho Chehab 	case 2:
7589a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_3_4;
7599a0bf528SMauro Carvalho Chehab 		break;
7609a0bf528SMauro Carvalho Chehab 	case 3:
7619a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_5_6;
7629a0bf528SMauro Carvalho Chehab 		break;
7639a0bf528SMauro Carvalho Chehab 	case 4:
7649a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_7_8;
7659a0bf528SMauro Carvalho Chehab 		break;
7669a0bf528SMauro Carvalho Chehab 	case 5:
7679a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_NONE;
7689a0bf528SMauro Carvalho Chehab 		break;
7699a0bf528SMauro Carvalho Chehab 	}
7709a0bf528SMauro Carvalho Chehab 
7719a0bf528SMauro Carvalho Chehab 	switch ((buf[7] >> 0) & 7) {
7729a0bf528SMauro Carvalho Chehab 	case 0:
7739a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_1_2;
7749a0bf528SMauro Carvalho Chehab 		break;
7759a0bf528SMauro Carvalho Chehab 	case 1:
7769a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_2_3;
7779a0bf528SMauro Carvalho Chehab 		break;
7789a0bf528SMauro Carvalho Chehab 	case 2:
7799a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_3_4;
7809a0bf528SMauro Carvalho Chehab 		break;
7819a0bf528SMauro Carvalho Chehab 	case 3:
7829a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_5_6;
7839a0bf528SMauro Carvalho Chehab 		break;
7849a0bf528SMauro Carvalho Chehab 	case 4:
7859a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_7_8;
7869a0bf528SMauro Carvalho Chehab 		break;
7879a0bf528SMauro Carvalho Chehab 	case 5:
7889a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_NONE;
7899a0bf528SMauro Carvalho Chehab 		break;
7909a0bf528SMauro Carvalho Chehab 	}
7919a0bf528SMauro Carvalho Chehab 
7929a0bf528SMauro Carvalho Chehab 	return 0;
7939a0bf528SMauro Carvalho Chehab 
7949a0bf528SMauro Carvalho Chehab err:
7956a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
7969a0bf528SMauro Carvalho Chehab 
7979a0bf528SMauro Carvalho Chehab 	return ret;
7989a0bf528SMauro Carvalho Chehab }
7999a0bf528SMauro Carvalho Chehab 
8009a0bf528SMauro Carvalho Chehab static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status)
8019a0bf528SMauro Carvalho Chehab {
80209611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
8039a0bf528SMauro Carvalho Chehab 	int ret;
8049a0bf528SMauro Carvalho Chehab 	u8 tmp;
8059a0bf528SMauro Carvalho Chehab 
8069a0bf528SMauro Carvalho Chehab 	*status = 0;
8079a0bf528SMauro Carvalho Chehab 
8089a0bf528SMauro Carvalho Chehab 	/* radio channel status, 0=no result, 1=has signal, 2=no signal */
80909611caaSAntti Palosaari 	ret = af9033_rd_reg(dev, 0x800047, &tmp);
8109a0bf528SMauro Carvalho Chehab 	if (ret < 0)
8119a0bf528SMauro Carvalho Chehab 		goto err;
8129a0bf528SMauro Carvalho Chehab 
8139a0bf528SMauro Carvalho Chehab 	/* has signal */
8149a0bf528SMauro Carvalho Chehab 	if (tmp == 0x01)
8159a0bf528SMauro Carvalho Chehab 		*status |= FE_HAS_SIGNAL;
8169a0bf528SMauro Carvalho Chehab 
8179a0bf528SMauro Carvalho Chehab 	if (tmp != 0x02) {
8189a0bf528SMauro Carvalho Chehab 		/* TPS lock */
81909611caaSAntti Palosaari 		ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01);
8209a0bf528SMauro Carvalho Chehab 		if (ret < 0)
8219a0bf528SMauro Carvalho Chehab 			goto err;
8229a0bf528SMauro Carvalho Chehab 
8239a0bf528SMauro Carvalho Chehab 		if (tmp)
8249a0bf528SMauro Carvalho Chehab 			*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
8259a0bf528SMauro Carvalho Chehab 					FE_HAS_VITERBI;
8269a0bf528SMauro Carvalho Chehab 
8279a0bf528SMauro Carvalho Chehab 		/* full lock */
82809611caaSAntti Palosaari 		ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01);
8299a0bf528SMauro Carvalho Chehab 		if (ret < 0)
8309a0bf528SMauro Carvalho Chehab 			goto err;
8319a0bf528SMauro Carvalho Chehab 
8329a0bf528SMauro Carvalho Chehab 		if (tmp)
8339a0bf528SMauro Carvalho Chehab 			*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
8349a0bf528SMauro Carvalho Chehab 					FE_HAS_VITERBI | FE_HAS_SYNC |
8359a0bf528SMauro Carvalho Chehab 					FE_HAS_LOCK;
8369a0bf528SMauro Carvalho Chehab 	}
8379a0bf528SMauro Carvalho Chehab 
83883f11619SAntti Palosaari 	dev->fe_status = *status;
83983f11619SAntti Palosaari 
8409a0bf528SMauro Carvalho Chehab 	return 0;
8419a0bf528SMauro Carvalho Chehab 
8429a0bf528SMauro Carvalho Chehab err:
8436a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
8449a0bf528SMauro Carvalho Chehab 
8459a0bf528SMauro Carvalho Chehab 	return ret;
8469a0bf528SMauro Carvalho Chehab }
8479a0bf528SMauro Carvalho Chehab 
8489a0bf528SMauro Carvalho Chehab static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
8499a0bf528SMauro Carvalho Chehab {
85009611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
8516b457786SAntti Palosaari 	struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
8526d03f6a8SBimow Chen 	int ret;
8536d03f6a8SBimow Chen 	u8 u8tmp;
8549a0bf528SMauro Carvalho Chehab 
8556b457786SAntti Palosaari 	/* use DVBv5 CNR */
8566d03f6a8SBimow Chen 	if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) {
8576d03f6a8SBimow Chen 		*snr = div_s64(c->cnr.stat[0].svalue, 1000);
8586d03f6a8SBimow Chen 
8596d03f6a8SBimow Chen 		/* read current modulation */
8606d03f6a8SBimow Chen 		ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
8616d03f6a8SBimow Chen 		if (ret)
8626d03f6a8SBimow Chen 			goto err;
8636d03f6a8SBimow Chen 
8646d03f6a8SBimow Chen 		/* scale value to 0x0000-0xffff */
8656d03f6a8SBimow Chen 		switch ((u8tmp >> 0) & 3) {
8666d03f6a8SBimow Chen 		case 0:
8676d03f6a8SBimow Chen 			*snr = *snr * 0xFFFF / 23;
8686d03f6a8SBimow Chen 			break;
8696d03f6a8SBimow Chen 		case 1:
8706d03f6a8SBimow Chen 			*snr = *snr * 0xFFFF / 26;
8716d03f6a8SBimow Chen 			break;
8726d03f6a8SBimow Chen 		case 2:
8736d03f6a8SBimow Chen 			*snr = *snr * 0xFFFF / 32;
8746d03f6a8SBimow Chen 			break;
8756d03f6a8SBimow Chen 		default:
8766d03f6a8SBimow Chen 			goto err;
8776d03f6a8SBimow Chen 		}
8786d03f6a8SBimow Chen 	} else {
8796b457786SAntti Palosaari 		*snr = 0;
8806d03f6a8SBimow Chen 	}
8819a0bf528SMauro Carvalho Chehab 
8829a0bf528SMauro Carvalho Chehab 	return 0;
8836d03f6a8SBimow Chen 
8846d03f6a8SBimow Chen err:
8856d03f6a8SBimow Chen 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
8866d03f6a8SBimow Chen 
8876d03f6a8SBimow Chen 	return ret;
8889a0bf528SMauro Carvalho Chehab }
8899a0bf528SMauro Carvalho Chehab 
8909a0bf528SMauro Carvalho Chehab static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
8919a0bf528SMauro Carvalho Chehab {
89209611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
8933adec272SBimow Chen 	struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
8943adec272SBimow Chen 	int ret, tmp, power_real;
8953adec272SBimow Chen 	u8 u8tmp, gain_offset, buf[7];
8969a0bf528SMauro Carvalho Chehab 
8973adec272SBimow Chen 	if (dev->is_af9035) {
8980b0d9628SAntti Palosaari 		/* read signal strength of 0-100 scale */
8990b0d9628SAntti Palosaari 		ret = af9033_rd_reg(dev, 0x800048, &u8tmp);
9000b0d9628SAntti Palosaari 		if (ret < 0)
9010b0d9628SAntti Palosaari 			goto err;
9020b0d9628SAntti Palosaari 
9033adec272SBimow Chen 		/* scale value to 0x0000-0xffff */
9043adec272SBimow Chen 		*strength = u8tmp * 0xffff / 100;
9053adec272SBimow Chen 	} else {
9063adec272SBimow Chen 		ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
9071620d221SAntti Palosaari 		if (ret < 0)
9081620d221SAntti Palosaari 			goto err;
9091620d221SAntti Palosaari 
9101620d221SAntti Palosaari 		ret = af9033_rd_regs(dev, 0x80f900, buf, 7);
9111620d221SAntti Palosaari 		if (ret < 0)
9121620d221SAntti Palosaari 			goto err;
9133adec272SBimow Chen 
9143adec272SBimow Chen 		if (c->frequency <= 300000000)
9153adec272SBimow Chen 			gain_offset = 7; /* VHF */
9163adec272SBimow Chen 		else
9173adec272SBimow Chen 			gain_offset = 4; /* UHF */
9183adec272SBimow Chen 
9193adec272SBimow Chen 		power_real = (u8tmp - 100 - gain_offset) -
9203adec272SBimow Chen 			power_reference[((buf[3] >> 0) & 3)][((buf[6] >> 0) & 7)];
9213adec272SBimow Chen 
9223adec272SBimow Chen 		if (power_real < -15)
9233adec272SBimow Chen 			tmp = 0;
9243adec272SBimow Chen 		else if ((power_real >= -15) && (power_real < 0))
9253adec272SBimow Chen 			tmp = (2 * (power_real + 15)) / 3;
9263adec272SBimow Chen 		else if ((power_real >= 0) && (power_real < 20))
9273adec272SBimow Chen 			tmp = 4 * power_real + 10;
9283adec272SBimow Chen 		else if ((power_real >= 20) && (power_real < 35))
9293adec272SBimow Chen 			tmp = (2 * (power_real - 20)) / 3 + 90;
9303adec272SBimow Chen 		else
9313adec272SBimow Chen 			tmp = 100;
9329a0bf528SMauro Carvalho Chehab 
9339a0bf528SMauro Carvalho Chehab 		/* scale value to 0x0000-0xffff */
9343adec272SBimow Chen 		*strength = tmp * 0xffff / 100;
9353adec272SBimow Chen 	}
9363adec272SBimow Chen 
9379a0bf528SMauro Carvalho Chehab 	return 0;
9389a0bf528SMauro Carvalho Chehab 
9399a0bf528SMauro Carvalho Chehab err:
9406a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
9419a0bf528SMauro Carvalho Chehab 
9429a0bf528SMauro Carvalho Chehab 	return ret;
9439a0bf528SMauro Carvalho Chehab }
9449a0bf528SMauro Carvalho Chehab 
9459a0bf528SMauro Carvalho Chehab static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
9469a0bf528SMauro Carvalho Chehab {
94709611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
9489a0bf528SMauro Carvalho Chehab 
949e53c4744SAntti Palosaari 	*ber = (dev->post_bit_error - dev->post_bit_error_prev);
950e53c4744SAntti Palosaari 	dev->post_bit_error_prev = dev->post_bit_error;
9519a0bf528SMauro Carvalho Chehab 
9529a0bf528SMauro Carvalho Chehab 	return 0;
9539a0bf528SMauro Carvalho Chehab }
9549a0bf528SMauro Carvalho Chehab 
9559a0bf528SMauro Carvalho Chehab static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
9569a0bf528SMauro Carvalho Chehab {
95709611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
9589a0bf528SMauro Carvalho Chehab 
9591d0ceae4SAntti Palosaari 	*ucblocks = dev->error_block_count;
9609a0bf528SMauro Carvalho Chehab 	return 0;
9619a0bf528SMauro Carvalho Chehab }
9629a0bf528SMauro Carvalho Chehab 
9639a0bf528SMauro Carvalho Chehab static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
9649a0bf528SMauro Carvalho Chehab {
96509611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
9669a0bf528SMauro Carvalho Chehab 	int ret;
9679a0bf528SMauro Carvalho Chehab 
9686a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "enable=%d\n", enable);
9699a0bf528SMauro Carvalho Chehab 
97009611caaSAntti Palosaari 	ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01);
9719a0bf528SMauro Carvalho Chehab 	if (ret < 0)
9729a0bf528SMauro Carvalho Chehab 		goto err;
9739a0bf528SMauro Carvalho Chehab 
9749a0bf528SMauro Carvalho Chehab 	return 0;
9759a0bf528SMauro Carvalho Chehab 
9769a0bf528SMauro Carvalho Chehab err:
9776a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
9789a0bf528SMauro Carvalho Chehab 
9799a0bf528SMauro Carvalho Chehab 	return ret;
9809a0bf528SMauro Carvalho Chehab }
9819a0bf528SMauro Carvalho Chehab 
982ed97a6feSMauro Carvalho Chehab static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
983040cf86cSAntti Palosaari {
98409611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
985040cf86cSAntti Palosaari 	int ret;
986040cf86cSAntti Palosaari 
9876a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "onoff=%d\n", onoff);
988040cf86cSAntti Palosaari 
98909611caaSAntti Palosaari 	ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01);
990040cf86cSAntti Palosaari 	if (ret < 0)
991040cf86cSAntti Palosaari 		goto err;
992040cf86cSAntti Palosaari 
993040cf86cSAntti Palosaari 	return 0;
994040cf86cSAntti Palosaari 
995040cf86cSAntti Palosaari err:
9966a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
997040cf86cSAntti Palosaari 
998040cf86cSAntti Palosaari 	return ret;
999040cf86cSAntti Palosaari }
1000040cf86cSAntti Palosaari 
100124e419a0SAntti Palosaari static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
100224e419a0SAntti Palosaari 		int onoff)
1003040cf86cSAntti Palosaari {
100409611caaSAntti Palosaari 	struct af9033_dev *dev = fe->demodulator_priv;
1005040cf86cSAntti Palosaari 	int ret;
1006040cf86cSAntti Palosaari 	u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1007040cf86cSAntti Palosaari 
10086a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "index=%d pid=%04x onoff=%d\n",
10096a087f1fSAntti Palosaari 			index, pid, onoff);
1010040cf86cSAntti Palosaari 
1011040cf86cSAntti Palosaari 	if (pid > 0x1fff)
1012040cf86cSAntti Palosaari 		return 0;
1013040cf86cSAntti Palosaari 
101409611caaSAntti Palosaari 	ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2);
1015040cf86cSAntti Palosaari 	if (ret < 0)
1016040cf86cSAntti Palosaari 		goto err;
1017040cf86cSAntti Palosaari 
101809611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x80f994, onoff);
1019040cf86cSAntti Palosaari 	if (ret < 0)
1020040cf86cSAntti Palosaari 		goto err;
1021040cf86cSAntti Palosaari 
102209611caaSAntti Palosaari 	ret = af9033_wr_reg(dev, 0x80f995, index);
1023040cf86cSAntti Palosaari 	if (ret < 0)
1024040cf86cSAntti Palosaari 		goto err;
1025040cf86cSAntti Palosaari 
1026040cf86cSAntti Palosaari 	return 0;
1027040cf86cSAntti Palosaari 
1028040cf86cSAntti Palosaari err:
10296a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
1030040cf86cSAntti Palosaari 
1031040cf86cSAntti Palosaari 	return ret;
1032040cf86cSAntti Palosaari }
1033040cf86cSAntti Palosaari 
103483f11619SAntti Palosaari static void af9033_stat_work(struct work_struct *work)
103583f11619SAntti Palosaari {
103683f11619SAntti Palosaari 	struct af9033_dev *dev = container_of(work, struct af9033_dev, stat_work.work);
103783f11619SAntti Palosaari 	struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
10383e41313aSAntti Palosaari 	int ret, tmp, i, len;
1039204f4319SAntti Palosaari 	u8 u8tmp, buf[7];
104083f11619SAntti Palosaari 
104183f11619SAntti Palosaari 	dev_dbg(&dev->client->dev, "\n");
104283f11619SAntti Palosaari 
10433e41313aSAntti Palosaari 	/* signal strength */
104483f11619SAntti Palosaari 	if (dev->fe_status & FE_HAS_SIGNAL) {
104583f11619SAntti Palosaari 		if (dev->is_af9035) {
104683f11619SAntti Palosaari 			ret = af9033_rd_reg(dev, 0x80004a, &u8tmp);
104783f11619SAntti Palosaari 			tmp = -u8tmp * 1000;
104883f11619SAntti Palosaari 		} else {
104983f11619SAntti Palosaari 			ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
105083f11619SAntti Palosaari 			tmp = (u8tmp - 100) * 1000;
105183f11619SAntti Palosaari 		}
105283f11619SAntti Palosaari 		if (ret)
105383f11619SAntti Palosaari 			goto err;
105483f11619SAntti Palosaari 
105583f11619SAntti Palosaari 		c->strength.len = 1;
105683f11619SAntti Palosaari 		c->strength.stat[0].scale = FE_SCALE_DECIBEL;
105783f11619SAntti Palosaari 		c->strength.stat[0].svalue = tmp;
105883f11619SAntti Palosaari 	} else {
105983f11619SAntti Palosaari 		c->strength.len = 1;
106083f11619SAntti Palosaari 		c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
106183f11619SAntti Palosaari 	}
106283f11619SAntti Palosaari 
10633e41313aSAntti Palosaari 	/* CNR */
10643e41313aSAntti Palosaari 	if (dev->fe_status & FE_HAS_VITERBI) {
10653e41313aSAntti Palosaari 		u32 snr_val;
10663e41313aSAntti Palosaari 		const struct val_snr *snr_lut;
10673e41313aSAntti Palosaari 
10683e41313aSAntti Palosaari 		/* read value */
10693e41313aSAntti Palosaari 		ret = af9033_rd_regs(dev, 0x80002c, buf, 3);
10703e41313aSAntti Palosaari 		if (ret)
10713e41313aSAntti Palosaari 			goto err;
10723e41313aSAntti Palosaari 
10733e41313aSAntti Palosaari 		snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0);
10743e41313aSAntti Palosaari 
10756d03f6a8SBimow Chen 		/* read superframe number */
10766d03f6a8SBimow Chen 		ret = af9033_rd_reg(dev, 0x80f78b, &u8tmp);
10776d03f6a8SBimow Chen 		if (ret)
10786d03f6a8SBimow Chen 			goto err;
10796d03f6a8SBimow Chen 
10806d03f6a8SBimow Chen 		if (u8tmp)
10816d03f6a8SBimow Chen 			snr_val /= u8tmp;
10826d03f6a8SBimow Chen 
10836d03f6a8SBimow Chen 		/* read current transmission mode */
10846d03f6a8SBimow Chen 		ret = af9033_rd_reg(dev, 0x80f900, &u8tmp);
10856d03f6a8SBimow Chen 		if (ret)
10866d03f6a8SBimow Chen 			goto err;
10876d03f6a8SBimow Chen 
10886d03f6a8SBimow Chen 		switch ((u8tmp >> 0) & 3) {
10896d03f6a8SBimow Chen 		case 0:
10906d03f6a8SBimow Chen 			snr_val *= 4;
10916d03f6a8SBimow Chen 			break;
10926d03f6a8SBimow Chen 		case 1:
10936d03f6a8SBimow Chen 			snr_val *= 1;
10946d03f6a8SBimow Chen 			break;
10956d03f6a8SBimow Chen 		case 2:
10966d03f6a8SBimow Chen 			snr_val *= 2;
10976d03f6a8SBimow Chen 			break;
10986d03f6a8SBimow Chen 		default:
10996d03f6a8SBimow Chen 			goto err;
11006d03f6a8SBimow Chen 		}
11016d03f6a8SBimow Chen 
11023e41313aSAntti Palosaari 		/* read current modulation */
11033e41313aSAntti Palosaari 		ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
11043e41313aSAntti Palosaari 		if (ret)
11053e41313aSAntti Palosaari 			goto err;
11063e41313aSAntti Palosaari 
11073e41313aSAntti Palosaari 		switch ((u8tmp >> 0) & 3) {
11083e41313aSAntti Palosaari 		case 0:
11093e41313aSAntti Palosaari 			len = ARRAY_SIZE(qpsk_snr_lut);
11103e41313aSAntti Palosaari 			snr_lut = qpsk_snr_lut;
11113e41313aSAntti Palosaari 			break;
11123e41313aSAntti Palosaari 		case 1:
11133e41313aSAntti Palosaari 			len = ARRAY_SIZE(qam16_snr_lut);
11143e41313aSAntti Palosaari 			snr_lut = qam16_snr_lut;
11153e41313aSAntti Palosaari 			break;
11163e41313aSAntti Palosaari 		case 2:
11173e41313aSAntti Palosaari 			len = ARRAY_SIZE(qam64_snr_lut);
11183e41313aSAntti Palosaari 			snr_lut = qam64_snr_lut;
11193e41313aSAntti Palosaari 			break;
11203e41313aSAntti Palosaari 		default:
11213e41313aSAntti Palosaari 			goto err_schedule_delayed_work;
11223e41313aSAntti Palosaari 		}
11233e41313aSAntti Palosaari 
11243e41313aSAntti Palosaari 		for (i = 0; i < len; i++) {
11253e41313aSAntti Palosaari 			tmp = snr_lut[i].snr * 1000;
11263e41313aSAntti Palosaari 			if (snr_val < snr_lut[i].val)
11273e41313aSAntti Palosaari 				break;
11283e41313aSAntti Palosaari 		}
11293e41313aSAntti Palosaari 
11303e41313aSAntti Palosaari 		c->cnr.len = 1;
11313e41313aSAntti Palosaari 		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
11323e41313aSAntti Palosaari 		c->cnr.stat[0].svalue = tmp;
11333e41313aSAntti Palosaari 	} else {
11343e41313aSAntti Palosaari 		c->cnr.len = 1;
11353e41313aSAntti Palosaari 		c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
11363e41313aSAntti Palosaari 	}
11373e41313aSAntti Palosaari 
1138204f4319SAntti Palosaari 	/* UCB/PER/BER */
1139204f4319SAntti Palosaari 	if (dev->fe_status & FE_HAS_LOCK) {
1140204f4319SAntti Palosaari 		/* outer FEC, 204 byte packets */
1141204f4319SAntti Palosaari 		u16 abort_packet_count, rsd_packet_count;
11426bb096c9SAntti Palosaari 		/* inner FEC, bits */
11436bb096c9SAntti Palosaari 		u32 rsd_bit_err_count;
1144204f4319SAntti Palosaari 
1145204f4319SAntti Palosaari 		/*
1146204f4319SAntti Palosaari 		 * Packet count used for measurement is 10000
1147204f4319SAntti Palosaari 		 * (rsd_packet_count). Maybe it should be increased?
1148204f4319SAntti Palosaari 		 */
1149204f4319SAntti Palosaari 
1150204f4319SAntti Palosaari 		ret = af9033_rd_regs(dev, 0x800032, buf, 7);
1151204f4319SAntti Palosaari 		if (ret)
1152204f4319SAntti Palosaari 			goto err;
1153204f4319SAntti Palosaari 
1154204f4319SAntti Palosaari 		abort_packet_count = (buf[1] << 8) | (buf[0] << 0);
11556bb096c9SAntti Palosaari 		rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2];
1156204f4319SAntti Palosaari 		rsd_packet_count = (buf[6] << 8) | (buf[5] << 0);
1157204f4319SAntti Palosaari 
1158204f4319SAntti Palosaari 		dev->error_block_count += abort_packet_count;
1159204f4319SAntti Palosaari 		dev->total_block_count += rsd_packet_count;
11606bb096c9SAntti Palosaari 		dev->post_bit_error += rsd_bit_err_count;
11616bb096c9SAntti Palosaari 		dev->post_bit_count += rsd_packet_count * 204 * 8;
1162204f4319SAntti Palosaari 
1163204f4319SAntti Palosaari 		c->block_count.len = 1;
1164204f4319SAntti Palosaari 		c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1165204f4319SAntti Palosaari 		c->block_count.stat[0].uvalue = dev->total_block_count;
1166204f4319SAntti Palosaari 
1167204f4319SAntti Palosaari 		c->block_error.len = 1;
1168204f4319SAntti Palosaari 		c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1169204f4319SAntti Palosaari 		c->block_error.stat[0].uvalue = dev->error_block_count;
11706bb096c9SAntti Palosaari 
11716bb096c9SAntti Palosaari 		c->post_bit_count.len = 1;
11726bb096c9SAntti Palosaari 		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
11736bb096c9SAntti Palosaari 		c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
11746bb096c9SAntti Palosaari 
11756bb096c9SAntti Palosaari 		c->post_bit_error.len = 1;
11766bb096c9SAntti Palosaari 		c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
11776bb096c9SAntti Palosaari 		c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
1178204f4319SAntti Palosaari 	}
1179204f4319SAntti Palosaari 
11803e41313aSAntti Palosaari err_schedule_delayed_work:
118183f11619SAntti Palosaari 	schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
118283f11619SAntti Palosaari 	return;
118383f11619SAntti Palosaari err:
118483f11619SAntti Palosaari 	dev_dbg(&dev->client->dev, "failed=%d\n", ret);
118583f11619SAntti Palosaari }
118683f11619SAntti Palosaari 
11879a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops af9033_ops = {
11889a0bf528SMauro Carvalho Chehab 	.delsys = { SYS_DVBT },
11899a0bf528SMauro Carvalho Chehab 	.info = {
11909a0bf528SMauro Carvalho Chehab 		.name = "Afatech AF9033 (DVB-T)",
11919a0bf528SMauro Carvalho Chehab 		.frequency_min = 174000000,
11929a0bf528SMauro Carvalho Chehab 		.frequency_max = 862000000,
11939a0bf528SMauro Carvalho Chehab 		.frequency_stepsize = 250000,
11949a0bf528SMauro Carvalho Chehab 		.frequency_tolerance = 0,
11959a0bf528SMauro Carvalho Chehab 		.caps =	FE_CAN_FEC_1_2 |
11969a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_2_3 |
11979a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_3_4 |
11989a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_5_6 |
11999a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_7_8 |
12009a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_AUTO |
12019a0bf528SMauro Carvalho Chehab 			FE_CAN_QPSK |
12029a0bf528SMauro Carvalho Chehab 			FE_CAN_QAM_16 |
12039a0bf528SMauro Carvalho Chehab 			FE_CAN_QAM_64 |
12049a0bf528SMauro Carvalho Chehab 			FE_CAN_QAM_AUTO |
12059a0bf528SMauro Carvalho Chehab 			FE_CAN_TRANSMISSION_MODE_AUTO |
12069a0bf528SMauro Carvalho Chehab 			FE_CAN_GUARD_INTERVAL_AUTO |
12079a0bf528SMauro Carvalho Chehab 			FE_CAN_HIERARCHY_AUTO |
12089a0bf528SMauro Carvalho Chehab 			FE_CAN_RECOVER |
12099a0bf528SMauro Carvalho Chehab 			FE_CAN_MUTE_TS
12109a0bf528SMauro Carvalho Chehab 	},
12119a0bf528SMauro Carvalho Chehab 
12129a0bf528SMauro Carvalho Chehab 	.init = af9033_init,
12139a0bf528SMauro Carvalho Chehab 	.sleep = af9033_sleep,
12149a0bf528SMauro Carvalho Chehab 
12159a0bf528SMauro Carvalho Chehab 	.get_tune_settings = af9033_get_tune_settings,
12169a0bf528SMauro Carvalho Chehab 	.set_frontend = af9033_set_frontend,
12179a0bf528SMauro Carvalho Chehab 	.get_frontend = af9033_get_frontend,
12189a0bf528SMauro Carvalho Chehab 
12199a0bf528SMauro Carvalho Chehab 	.read_status = af9033_read_status,
12209a0bf528SMauro Carvalho Chehab 	.read_snr = af9033_read_snr,
12219a0bf528SMauro Carvalho Chehab 	.read_signal_strength = af9033_read_signal_strength,
12229a0bf528SMauro Carvalho Chehab 	.read_ber = af9033_read_ber,
12239a0bf528SMauro Carvalho Chehab 	.read_ucblocks = af9033_read_ucblocks,
12249a0bf528SMauro Carvalho Chehab 
12259a0bf528SMauro Carvalho Chehab 	.i2c_gate_ctrl = af9033_i2c_gate_ctrl,
12269a0bf528SMauro Carvalho Chehab };
12279a0bf528SMauro Carvalho Chehab 
1228f5b00a76SAntti Palosaari static int af9033_probe(struct i2c_client *client,
1229f5b00a76SAntti Palosaari 		const struct i2c_device_id *id)
1230f5b00a76SAntti Palosaari {
1231f5b00a76SAntti Palosaari 	struct af9033_config *cfg = client->dev.platform_data;
1232f5b00a76SAntti Palosaari 	struct af9033_dev *dev;
1233f5b00a76SAntti Palosaari 	int ret;
1234f5b00a76SAntti Palosaari 	u8 buf[8];
1235f5b00a76SAntti Palosaari 	u32 reg;
1236f5b00a76SAntti Palosaari 
1237f5b00a76SAntti Palosaari 	/* allocate memory for the internal state */
1238f5b00a76SAntti Palosaari 	dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL);
1239f5b00a76SAntti Palosaari 	if (dev == NULL) {
1240f5b00a76SAntti Palosaari 		ret = -ENOMEM;
1241f5b00a76SAntti Palosaari 		dev_err(&client->dev, "Could not allocate memory for state\n");
1242f5b00a76SAntti Palosaari 		goto err;
1243f5b00a76SAntti Palosaari 	}
1244f5b00a76SAntti Palosaari 
1245f5b00a76SAntti Palosaari 	/* setup the state */
1246f5b00a76SAntti Palosaari 	dev->client = client;
124783f11619SAntti Palosaari 	INIT_DELAYED_WORK(&dev->stat_work, af9033_stat_work);
1248f5b00a76SAntti Palosaari 	memcpy(&dev->cfg, cfg, sizeof(struct af9033_config));
1249f5b00a76SAntti Palosaari 
1250f5b00a76SAntti Palosaari 	if (dev->cfg.clock != 12000000) {
1251f5b00a76SAntti Palosaari 		ret = -ENODEV;
1252f5b00a76SAntti Palosaari 		dev_err(&dev->client->dev,
12536a087f1fSAntti Palosaari 				"unsupported clock %d Hz, only 12000000 Hz is supported currently\n",
12546a087f1fSAntti Palosaari 				dev->cfg.clock);
1255f5b00a76SAntti Palosaari 		goto err_kfree;
1256f5b00a76SAntti Palosaari 	}
1257f5b00a76SAntti Palosaari 
1258f5b00a76SAntti Palosaari 	/* firmware version */
1259f5b00a76SAntti Palosaari 	switch (dev->cfg.tuner) {
1260f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_38:
1261f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_51:
1262f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_52:
1263f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_60:
1264f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_61:
1265f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_62:
126683f11619SAntti Palosaari 		dev->is_it9135 = true;
1267f5b00a76SAntti Palosaari 		reg = 0x004bfc;
1268f5b00a76SAntti Palosaari 		break;
1269f5b00a76SAntti Palosaari 	default:
127083f11619SAntti Palosaari 		dev->is_af9035 = true;
1271f5b00a76SAntti Palosaari 		reg = 0x0083e9;
1272f5b00a76SAntti Palosaari 		break;
1273f5b00a76SAntti Palosaari 	}
1274f5b00a76SAntti Palosaari 
1275f5b00a76SAntti Palosaari 	ret = af9033_rd_regs(dev, reg, &buf[0], 4);
1276f5b00a76SAntti Palosaari 	if (ret < 0)
1277f5b00a76SAntti Palosaari 		goto err_kfree;
1278f5b00a76SAntti Palosaari 
1279f5b00a76SAntti Palosaari 	ret = af9033_rd_regs(dev, 0x804191, &buf[4], 4);
1280f5b00a76SAntti Palosaari 	if (ret < 0)
1281f5b00a76SAntti Palosaari 		goto err_kfree;
1282f5b00a76SAntti Palosaari 
1283f5b00a76SAntti Palosaari 	dev_info(&dev->client->dev,
12846a087f1fSAntti Palosaari 			"firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n",
12856a087f1fSAntti Palosaari 			buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
12866a087f1fSAntti Palosaari 			buf[7]);
1287f5b00a76SAntti Palosaari 
1288f5b00a76SAntti Palosaari 	/* sleep */
1289f5b00a76SAntti Palosaari 	switch (dev->cfg.tuner) {
1290f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_38:
1291f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_51:
1292f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_52:
1293f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_60:
1294f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_61:
1295f5b00a76SAntti Palosaari 	case AF9033_TUNER_IT9135_62:
1296f5b00a76SAntti Palosaari 		/* IT9135 did not like to sleep at that early */
1297f5b00a76SAntti Palosaari 		break;
1298f5b00a76SAntti Palosaari 	default:
1299f5b00a76SAntti Palosaari 		ret = af9033_wr_reg(dev, 0x80004c, 1);
1300f5b00a76SAntti Palosaari 		if (ret < 0)
1301f5b00a76SAntti Palosaari 			goto err_kfree;
1302f5b00a76SAntti Palosaari 
1303f5b00a76SAntti Palosaari 		ret = af9033_wr_reg(dev, 0x800000, 0);
1304f5b00a76SAntti Palosaari 		if (ret < 0)
1305f5b00a76SAntti Palosaari 			goto err_kfree;
1306f5b00a76SAntti Palosaari 	}
1307f5b00a76SAntti Palosaari 
1308f5b00a76SAntti Palosaari 	/* configure internal TS mode */
1309f5b00a76SAntti Palosaari 	switch (dev->cfg.ts_mode) {
1310f5b00a76SAntti Palosaari 	case AF9033_TS_MODE_PARALLEL:
1311f5b00a76SAntti Palosaari 		dev->ts_mode_parallel = true;
1312f5b00a76SAntti Palosaari 		break;
1313f5b00a76SAntti Palosaari 	case AF9033_TS_MODE_SERIAL:
1314f5b00a76SAntti Palosaari 		dev->ts_mode_serial = true;
1315f5b00a76SAntti Palosaari 		break;
1316f5b00a76SAntti Palosaari 	case AF9033_TS_MODE_USB:
1317f5b00a76SAntti Palosaari 		/* usb mode for AF9035 */
1318f5b00a76SAntti Palosaari 	default:
1319f5b00a76SAntti Palosaari 		break;
1320f5b00a76SAntti Palosaari 	}
1321f5b00a76SAntti Palosaari 
1322f5b00a76SAntti Palosaari 	/* create dvb_frontend */
1323f5b00a76SAntti Palosaari 	memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
1324f5b00a76SAntti Palosaari 	dev->fe.demodulator_priv = dev;
1325f5b00a76SAntti Palosaari 	*cfg->fe = &dev->fe;
1326f5b00a76SAntti Palosaari 	if (cfg->ops) {
1327f5b00a76SAntti Palosaari 		cfg->ops->pid_filter = af9033_pid_filter;
1328f5b00a76SAntti Palosaari 		cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl;
1329f5b00a76SAntti Palosaari 	}
1330f5b00a76SAntti Palosaari 	i2c_set_clientdata(client, dev);
1331f5b00a76SAntti Palosaari 
1332f5b00a76SAntti Palosaari 	dev_info(&dev->client->dev, "Afatech AF9033 successfully attached\n");
1333f5b00a76SAntti Palosaari 	return 0;
1334f5b00a76SAntti Palosaari err_kfree:
1335f5b00a76SAntti Palosaari 	kfree(dev);
1336f5b00a76SAntti Palosaari err:
13376a087f1fSAntti Palosaari 	dev_dbg(&client->dev, "failed=%d\n", ret);
1338f5b00a76SAntti Palosaari 	return ret;
1339f5b00a76SAntti Palosaari }
1340f5b00a76SAntti Palosaari 
1341f5b00a76SAntti Palosaari static int af9033_remove(struct i2c_client *client)
1342f5b00a76SAntti Palosaari {
1343f5b00a76SAntti Palosaari 	struct af9033_dev *dev = i2c_get_clientdata(client);
1344f5b00a76SAntti Palosaari 
13456a087f1fSAntti Palosaari 	dev_dbg(&dev->client->dev, "\n");
1346f5b00a76SAntti Palosaari 
1347f5b00a76SAntti Palosaari 	dev->fe.ops.release = NULL;
1348f5b00a76SAntti Palosaari 	dev->fe.demodulator_priv = NULL;
1349f5b00a76SAntti Palosaari 	kfree(dev);
1350f5b00a76SAntti Palosaari 
1351f5b00a76SAntti Palosaari 	return 0;
1352f5b00a76SAntti Palosaari }
1353f5b00a76SAntti Palosaari 
1354f5b00a76SAntti Palosaari static const struct i2c_device_id af9033_id_table[] = {
1355f5b00a76SAntti Palosaari 	{"af9033", 0},
1356f5b00a76SAntti Palosaari 	{}
1357f5b00a76SAntti Palosaari };
1358f5b00a76SAntti Palosaari MODULE_DEVICE_TABLE(i2c, af9033_id_table);
1359f5b00a76SAntti Palosaari 
1360f5b00a76SAntti Palosaari static struct i2c_driver af9033_driver = {
1361f5b00a76SAntti Palosaari 	.driver = {
1362f5b00a76SAntti Palosaari 		.owner	= THIS_MODULE,
1363f5b00a76SAntti Palosaari 		.name	= "af9033",
1364f5b00a76SAntti Palosaari 	},
1365f5b00a76SAntti Palosaari 	.probe		= af9033_probe,
1366f5b00a76SAntti Palosaari 	.remove		= af9033_remove,
1367f5b00a76SAntti Palosaari 	.id_table	= af9033_id_table,
1368f5b00a76SAntti Palosaari };
1369f5b00a76SAntti Palosaari 
1370f5b00a76SAntti Palosaari module_i2c_driver(af9033_driver);
1371f5b00a76SAntti Palosaari 
13729a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
13739a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
13749a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
1375