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 
279a0bf528SMauro Carvalho Chehab struct af9033_state {
289a0bf528SMauro Carvalho Chehab 	struct i2c_adapter *i2c;
299a0bf528SMauro Carvalho Chehab 	struct dvb_frontend fe;
309a0bf528SMauro Carvalho Chehab 	struct af9033_config cfg;
319a0bf528SMauro Carvalho Chehab 
329a0bf528SMauro Carvalho Chehab 	u32 bandwidth_hz;
339a0bf528SMauro Carvalho Chehab 	bool ts_mode_parallel;
349a0bf528SMauro Carvalho Chehab 	bool ts_mode_serial;
359a0bf528SMauro Carvalho Chehab 
369a0bf528SMauro Carvalho Chehab 	u32 ber;
379a0bf528SMauro Carvalho Chehab 	u32 ucb;
389a0bf528SMauro Carvalho Chehab 	unsigned long last_stat_check;
399a0bf528SMauro Carvalho Chehab };
409a0bf528SMauro Carvalho Chehab 
419a0bf528SMauro Carvalho Chehab /* write multiple registers */
429a0bf528SMauro Carvalho Chehab static int af9033_wr_regs(struct af9033_state *state, u32 reg, const u8 *val,
439a0bf528SMauro Carvalho Chehab 		int len)
449a0bf528SMauro Carvalho Chehab {
459a0bf528SMauro Carvalho Chehab 	int ret;
4637ebaf68SMauro Carvalho Chehab 	u8 buf[MAX_XFER_SIZE];
479a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[1] = {
489a0bf528SMauro Carvalho Chehab 		{
499a0bf528SMauro Carvalho Chehab 			.addr = state->cfg.i2c_addr,
509a0bf528SMauro Carvalho Chehab 			.flags = 0,
5137ebaf68SMauro Carvalho Chehab 			.len = 3 + len,
529a0bf528SMauro Carvalho Chehab 			.buf = buf,
539a0bf528SMauro Carvalho Chehab 		}
549a0bf528SMauro Carvalho Chehab 	};
559a0bf528SMauro Carvalho Chehab 
5637ebaf68SMauro Carvalho Chehab 	if (3 + len > sizeof(buf)) {
5737ebaf68SMauro Carvalho Chehab 		dev_warn(&state->i2c->dev,
5837ebaf68SMauro Carvalho Chehab 			 "%s: i2c wr reg=%04x: len=%d is too big!\n",
5937ebaf68SMauro Carvalho Chehab 			 KBUILD_MODNAME, reg, len);
6037ebaf68SMauro Carvalho Chehab 		return -EINVAL;
6137ebaf68SMauro Carvalho Chehab 	}
6237ebaf68SMauro Carvalho Chehab 
639a0bf528SMauro Carvalho Chehab 	buf[0] = (reg >> 16) & 0xff;
649a0bf528SMauro Carvalho Chehab 	buf[1] = (reg >>  8) & 0xff;
659a0bf528SMauro Carvalho Chehab 	buf[2] = (reg >>  0) & 0xff;
669a0bf528SMauro Carvalho Chehab 	memcpy(&buf[3], val, len);
679a0bf528SMauro Carvalho Chehab 
689a0bf528SMauro Carvalho Chehab 	ret = i2c_transfer(state->i2c, msg, 1);
699a0bf528SMauro Carvalho Chehab 	if (ret == 1) {
709a0bf528SMauro Carvalho Chehab 		ret = 0;
719a0bf528SMauro Carvalho Chehab 	} else {
720a73f2d6SAntti Palosaari 		dev_warn(&state->i2c->dev, "%s: i2c wr failed=%d reg=%06x " \
730a73f2d6SAntti Palosaari 				"len=%d\n", KBUILD_MODNAME, ret, reg, len);
749a0bf528SMauro Carvalho Chehab 		ret = -EREMOTEIO;
759a0bf528SMauro Carvalho Chehab 	}
769a0bf528SMauro Carvalho Chehab 
779a0bf528SMauro Carvalho Chehab 	return ret;
789a0bf528SMauro Carvalho Chehab }
799a0bf528SMauro Carvalho Chehab 
809a0bf528SMauro Carvalho Chehab /* read multiple registers */
819a0bf528SMauro Carvalho Chehab static int af9033_rd_regs(struct af9033_state *state, u32 reg, u8 *val, int len)
829a0bf528SMauro Carvalho Chehab {
839a0bf528SMauro Carvalho Chehab 	int ret;
849a0bf528SMauro Carvalho Chehab 	u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
859a0bf528SMauro Carvalho Chehab 			(reg >> 0) & 0xff };
869a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[2] = {
879a0bf528SMauro Carvalho Chehab 		{
889a0bf528SMauro Carvalho Chehab 			.addr = state->cfg.i2c_addr,
899a0bf528SMauro Carvalho Chehab 			.flags = 0,
909a0bf528SMauro Carvalho Chehab 			.len = sizeof(buf),
919a0bf528SMauro Carvalho Chehab 			.buf = buf
929a0bf528SMauro Carvalho Chehab 		}, {
939a0bf528SMauro Carvalho Chehab 			.addr = state->cfg.i2c_addr,
949a0bf528SMauro Carvalho Chehab 			.flags = I2C_M_RD,
959a0bf528SMauro Carvalho Chehab 			.len = len,
969a0bf528SMauro Carvalho Chehab 			.buf = val
979a0bf528SMauro Carvalho Chehab 		}
989a0bf528SMauro Carvalho Chehab 	};
999a0bf528SMauro Carvalho Chehab 
1009a0bf528SMauro Carvalho Chehab 	ret = i2c_transfer(state->i2c, msg, 2);
1019a0bf528SMauro Carvalho Chehab 	if (ret == 2) {
1029a0bf528SMauro Carvalho Chehab 		ret = 0;
1039a0bf528SMauro Carvalho Chehab 	} else {
1040a73f2d6SAntti Palosaari 		dev_warn(&state->i2c->dev, "%s: i2c rd failed=%d reg=%06x " \
1050a73f2d6SAntti Palosaari 				"len=%d\n", KBUILD_MODNAME, ret, reg, len);
1069a0bf528SMauro Carvalho Chehab 		ret = -EREMOTEIO;
1079a0bf528SMauro Carvalho Chehab 	}
1089a0bf528SMauro Carvalho Chehab 
1099a0bf528SMauro Carvalho Chehab 	return ret;
1109a0bf528SMauro Carvalho Chehab }
1119a0bf528SMauro Carvalho Chehab 
1129a0bf528SMauro Carvalho Chehab 
1139a0bf528SMauro Carvalho Chehab /* write single register */
1149a0bf528SMauro Carvalho Chehab static int af9033_wr_reg(struct af9033_state *state, u32 reg, u8 val)
1159a0bf528SMauro Carvalho Chehab {
1169a0bf528SMauro Carvalho Chehab 	return af9033_wr_regs(state, reg, &val, 1);
1179a0bf528SMauro Carvalho Chehab }
1189a0bf528SMauro Carvalho Chehab 
1199a0bf528SMauro Carvalho Chehab /* read single register */
1209a0bf528SMauro Carvalho Chehab static int af9033_rd_reg(struct af9033_state *state, u32 reg, u8 *val)
1219a0bf528SMauro Carvalho Chehab {
1229a0bf528SMauro Carvalho Chehab 	return af9033_rd_regs(state, reg, val, 1);
1239a0bf528SMauro Carvalho Chehab }
1249a0bf528SMauro Carvalho Chehab 
1259a0bf528SMauro Carvalho Chehab /* write single register with mask */
1269a0bf528SMauro Carvalho Chehab static int af9033_wr_reg_mask(struct af9033_state *state, u32 reg, u8 val,
1279a0bf528SMauro Carvalho Chehab 		u8 mask)
1289a0bf528SMauro Carvalho Chehab {
1299a0bf528SMauro Carvalho Chehab 	int ret;
1309a0bf528SMauro Carvalho Chehab 	u8 tmp;
1319a0bf528SMauro Carvalho Chehab 
1329a0bf528SMauro Carvalho Chehab 	/* no need for read if whole reg is written */
1339a0bf528SMauro Carvalho Chehab 	if (mask != 0xff) {
1349a0bf528SMauro Carvalho Chehab 		ret = af9033_rd_regs(state, reg, &tmp, 1);
1359a0bf528SMauro Carvalho Chehab 		if (ret)
1369a0bf528SMauro Carvalho Chehab 			return ret;
1379a0bf528SMauro Carvalho Chehab 
1389a0bf528SMauro Carvalho Chehab 		val &= mask;
1399a0bf528SMauro Carvalho Chehab 		tmp &= ~mask;
1409a0bf528SMauro Carvalho Chehab 		val |= tmp;
1419a0bf528SMauro Carvalho Chehab 	}
1429a0bf528SMauro Carvalho Chehab 
1439a0bf528SMauro Carvalho Chehab 	return af9033_wr_regs(state, reg, &val, 1);
1449a0bf528SMauro Carvalho Chehab }
1459a0bf528SMauro Carvalho Chehab 
1469a0bf528SMauro Carvalho Chehab /* read single register with mask */
1479a0bf528SMauro Carvalho Chehab static int af9033_rd_reg_mask(struct af9033_state *state, u32 reg, u8 *val,
1489a0bf528SMauro Carvalho Chehab 		u8 mask)
1499a0bf528SMauro Carvalho Chehab {
1509a0bf528SMauro Carvalho Chehab 	int ret, i;
1519a0bf528SMauro Carvalho Chehab 	u8 tmp;
1529a0bf528SMauro Carvalho Chehab 
1539a0bf528SMauro Carvalho Chehab 	ret = af9033_rd_regs(state, reg, &tmp, 1);
1549a0bf528SMauro Carvalho Chehab 	if (ret)
1559a0bf528SMauro Carvalho Chehab 		return ret;
1569a0bf528SMauro Carvalho Chehab 
1579a0bf528SMauro Carvalho Chehab 	tmp &= mask;
1589a0bf528SMauro Carvalho Chehab 
1599a0bf528SMauro Carvalho Chehab 	/* find position of the first bit */
1609a0bf528SMauro Carvalho Chehab 	for (i = 0; i < 8; i++) {
1619a0bf528SMauro Carvalho Chehab 		if ((mask >> i) & 0x01)
1629a0bf528SMauro Carvalho Chehab 			break;
1639a0bf528SMauro Carvalho Chehab 	}
1649a0bf528SMauro Carvalho Chehab 	*val = tmp >> i;
1659a0bf528SMauro Carvalho Chehab 
1669a0bf528SMauro Carvalho Chehab 	return 0;
1679a0bf528SMauro Carvalho Chehab }
1689a0bf528SMauro Carvalho Chehab 
1693bf5e552SAntti Palosaari /* write reg val table using reg addr auto increment */
1703bf5e552SAntti Palosaari static int af9033_wr_reg_val_tab(struct af9033_state *state,
1713bf5e552SAntti Palosaari 		const struct reg_val *tab, int tab_len)
1723bf5e552SAntti Palosaari {
173d18a88b1SAntti Palosaari #define MAX_TAB_LEN 212
1743bf5e552SAntti Palosaari 	int ret, i, j;
175d18a88b1SAntti Palosaari 	u8 buf[1 + MAX_TAB_LEN];
176d18a88b1SAntti Palosaari 
177d18a88b1SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: tab_len=%d\n", __func__, tab_len);
17837ebaf68SMauro Carvalho Chehab 
17937ebaf68SMauro Carvalho Chehab 	if (tab_len > sizeof(buf)) {
180d18a88b1SAntti Palosaari 		dev_warn(&state->i2c->dev, "%s: tab len %d is too big\n",
18137ebaf68SMauro Carvalho Chehab 				KBUILD_MODNAME, tab_len);
18237ebaf68SMauro Carvalho Chehab 		return -EINVAL;
18337ebaf68SMauro Carvalho Chehab 	}
1843bf5e552SAntti Palosaari 
1853bf5e552SAntti Palosaari 	for (i = 0, j = 0; i < tab_len; i++) {
1863bf5e552SAntti Palosaari 		buf[j] = tab[i].val;
1873bf5e552SAntti Palosaari 
1883bf5e552SAntti Palosaari 		if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
1893bf5e552SAntti Palosaari 			ret = af9033_wr_regs(state, tab[i].reg - j, buf, j + 1);
1903bf5e552SAntti Palosaari 			if (ret < 0)
1913bf5e552SAntti Palosaari 				goto err;
1923bf5e552SAntti Palosaari 
1933bf5e552SAntti Palosaari 			j = 0;
1943bf5e552SAntti Palosaari 		} else {
1953bf5e552SAntti Palosaari 			j++;
1963bf5e552SAntti Palosaari 		}
1973bf5e552SAntti Palosaari 	}
1983bf5e552SAntti Palosaari 
1993bf5e552SAntti Palosaari 	return 0;
2003bf5e552SAntti Palosaari 
2013bf5e552SAntti Palosaari err:
2023bf5e552SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
2033bf5e552SAntti Palosaari 
2043bf5e552SAntti Palosaari 	return ret;
2053bf5e552SAntti Palosaari }
2063bf5e552SAntti Palosaari 
2070a73f2d6SAntti Palosaari static u32 af9033_div(struct af9033_state *state, u32 a, u32 b, u32 x)
2089a0bf528SMauro Carvalho Chehab {
2099a0bf528SMauro Carvalho Chehab 	u32 r = 0, c = 0, i;
2109a0bf528SMauro Carvalho Chehab 
2110a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d\n", __func__, a, b, x);
2129a0bf528SMauro Carvalho Chehab 
2139a0bf528SMauro Carvalho Chehab 	if (a > b) {
2149a0bf528SMauro Carvalho Chehab 		c = a / b;
2159a0bf528SMauro Carvalho Chehab 		a = a - c * b;
2169a0bf528SMauro Carvalho Chehab 	}
2179a0bf528SMauro Carvalho Chehab 
2189a0bf528SMauro Carvalho Chehab 	for (i = 0; i < x; i++) {
2199a0bf528SMauro Carvalho Chehab 		if (a >= b) {
2209a0bf528SMauro Carvalho Chehab 			r += 1;
2219a0bf528SMauro Carvalho Chehab 			a -= b;
2229a0bf528SMauro Carvalho Chehab 		}
2239a0bf528SMauro Carvalho Chehab 		a <<= 1;
2249a0bf528SMauro Carvalho Chehab 		r <<= 1;
2259a0bf528SMauro Carvalho Chehab 	}
2269a0bf528SMauro Carvalho Chehab 	r = (c << (u32)x) + r;
2279a0bf528SMauro Carvalho Chehab 
2280a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d r=%d r=%x\n",
2290a73f2d6SAntti Palosaari 			__func__, a, b, x, r, r);
2309a0bf528SMauro Carvalho Chehab 
2319a0bf528SMauro Carvalho Chehab 	return r;
2329a0bf528SMauro Carvalho Chehab }
2339a0bf528SMauro Carvalho Chehab 
2349a0bf528SMauro Carvalho Chehab static void af9033_release(struct dvb_frontend *fe)
2359a0bf528SMauro Carvalho Chehab {
2369a0bf528SMauro Carvalho Chehab 	struct af9033_state *state = fe->demodulator_priv;
2379a0bf528SMauro Carvalho Chehab 
2389a0bf528SMauro Carvalho Chehab 	kfree(state);
2399a0bf528SMauro Carvalho Chehab }
2409a0bf528SMauro Carvalho Chehab 
2419a0bf528SMauro Carvalho Chehab static int af9033_init(struct dvb_frontend *fe)
2429a0bf528SMauro Carvalho Chehab {
2439a0bf528SMauro Carvalho Chehab 	struct af9033_state *state = fe->demodulator_priv;
2449a0bf528SMauro Carvalho Chehab 	int ret, i, len;
2459a0bf528SMauro Carvalho Chehab 	const struct reg_val *init;
2469a0bf528SMauro Carvalho Chehab 	u8 buf[4];
2479a0bf528SMauro Carvalho Chehab 	u32 adc_cw, clock_cw;
2489a0bf528SMauro Carvalho Chehab 	struct reg_val_mask tab[] = {
2499a0bf528SMauro Carvalho Chehab 		{ 0x80fb24, 0x00, 0x08 },
2509a0bf528SMauro Carvalho Chehab 		{ 0x80004c, 0x00, 0xff },
2519a0bf528SMauro Carvalho Chehab 		{ 0x00f641, state->cfg.tuner, 0xff },
2529a0bf528SMauro Carvalho Chehab 		{ 0x80f5ca, 0x01, 0x01 },
2539a0bf528SMauro Carvalho Chehab 		{ 0x80f715, 0x01, 0x01 },
2549a0bf528SMauro Carvalho Chehab 		{ 0x00f41f, 0x04, 0x04 },
2559a0bf528SMauro Carvalho Chehab 		{ 0x00f41a, 0x01, 0x01 },
2569a0bf528SMauro Carvalho Chehab 		{ 0x80f731, 0x00, 0x01 },
2579a0bf528SMauro Carvalho Chehab 		{ 0x00d91e, 0x00, 0x01 },
2589a0bf528SMauro Carvalho Chehab 		{ 0x00d919, 0x00, 0x01 },
2599a0bf528SMauro Carvalho Chehab 		{ 0x80f732, 0x00, 0x01 },
2609a0bf528SMauro Carvalho Chehab 		{ 0x00d91f, 0x00, 0x01 },
2619a0bf528SMauro Carvalho Chehab 		{ 0x00d91a, 0x00, 0x01 },
2629a0bf528SMauro Carvalho Chehab 		{ 0x80f730, 0x00, 0x01 },
2639a0bf528SMauro Carvalho Chehab 		{ 0x80f778, 0x00, 0xff },
2649a0bf528SMauro Carvalho Chehab 		{ 0x80f73c, 0x01, 0x01 },
2659a0bf528SMauro Carvalho Chehab 		{ 0x80f776, 0x00, 0x01 },
2669a0bf528SMauro Carvalho Chehab 		{ 0x00d8fd, 0x01, 0xff },
2679a0bf528SMauro Carvalho Chehab 		{ 0x00d830, 0x01, 0xff },
2689a0bf528SMauro Carvalho Chehab 		{ 0x00d831, 0x00, 0xff },
2699a0bf528SMauro Carvalho Chehab 		{ 0x00d832, 0x00, 0xff },
2709a0bf528SMauro Carvalho Chehab 		{ 0x80f985, state->ts_mode_serial, 0x01 },
2719a0bf528SMauro Carvalho Chehab 		{ 0x80f986, state->ts_mode_parallel, 0x01 },
2729a0bf528SMauro Carvalho Chehab 		{ 0x00d827, 0x00, 0xff },
2739a0bf528SMauro Carvalho Chehab 		{ 0x00d829, 0x00, 0xff },
2744902bb39SAntti Palosaari 		{ 0x800045, state->cfg.adc_multiplier, 0xff },
2759a0bf528SMauro Carvalho Chehab 	};
2769a0bf528SMauro Carvalho Chehab 
2779a0bf528SMauro Carvalho Chehab 	/* program clock control */
2780a73f2d6SAntti Palosaari 	clock_cw = af9033_div(state, state->cfg.clock, 1000000ul, 19ul);
2799a0bf528SMauro Carvalho Chehab 	buf[0] = (clock_cw >>  0) & 0xff;
2809a0bf528SMauro Carvalho Chehab 	buf[1] = (clock_cw >>  8) & 0xff;
2819a0bf528SMauro Carvalho Chehab 	buf[2] = (clock_cw >> 16) & 0xff;
2829a0bf528SMauro Carvalho Chehab 	buf[3] = (clock_cw >> 24) & 0xff;
2839a0bf528SMauro Carvalho Chehab 
2840a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: clock=%d clock_cw=%08x\n",
2850a73f2d6SAntti Palosaari 			__func__, state->cfg.clock, clock_cw);
2869a0bf528SMauro Carvalho Chehab 
2879a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_regs(state, 0x800025, buf, 4);
2889a0bf528SMauro Carvalho Chehab 	if (ret < 0)
2899a0bf528SMauro Carvalho Chehab 		goto err;
2909a0bf528SMauro Carvalho Chehab 
2919a0bf528SMauro Carvalho Chehab 	/* program ADC control */
2929a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
2939a0bf528SMauro Carvalho Chehab 		if (clock_adc_lut[i].clock == state->cfg.clock)
2949a0bf528SMauro Carvalho Chehab 			break;
2959a0bf528SMauro Carvalho Chehab 	}
2969a0bf528SMauro Carvalho Chehab 
2970a73f2d6SAntti Palosaari 	adc_cw = af9033_div(state, clock_adc_lut[i].adc, 1000000ul, 19ul);
2989a0bf528SMauro Carvalho Chehab 	buf[0] = (adc_cw >>  0) & 0xff;
2999a0bf528SMauro Carvalho Chehab 	buf[1] = (adc_cw >>  8) & 0xff;
3009a0bf528SMauro Carvalho Chehab 	buf[2] = (adc_cw >> 16) & 0xff;
3019a0bf528SMauro Carvalho Chehab 
3020a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: adc=%d adc_cw=%06x\n",
3030a73f2d6SAntti Palosaari 			__func__, clock_adc_lut[i].adc, adc_cw);
3049a0bf528SMauro Carvalho Chehab 
3059a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_regs(state, 0x80f1cd, buf, 3);
3069a0bf528SMauro Carvalho Chehab 	if (ret < 0)
3079a0bf528SMauro Carvalho Chehab 		goto err;
3089a0bf528SMauro Carvalho Chehab 
3099a0bf528SMauro Carvalho Chehab 	/* program register table */
3109a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
3119a0bf528SMauro Carvalho Chehab 		ret = af9033_wr_reg_mask(state, tab[i].reg, tab[i].val,
3129a0bf528SMauro Carvalho Chehab 				tab[i].mask);
3139a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3149a0bf528SMauro Carvalho Chehab 			goto err;
3159a0bf528SMauro Carvalho Chehab 	}
3169a0bf528SMauro Carvalho Chehab 
3179a0bf528SMauro Carvalho Chehab 	/* settings for TS interface */
3189a0bf528SMauro Carvalho Chehab 	if (state->cfg.ts_mode == AF9033_TS_MODE_USB) {
3199a0bf528SMauro Carvalho Chehab 		ret = af9033_wr_reg_mask(state, 0x80f9a5, 0x00, 0x01);
3209a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3219a0bf528SMauro Carvalho Chehab 			goto err;
3229a0bf528SMauro Carvalho Chehab 
3239a0bf528SMauro Carvalho Chehab 		ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x01, 0x01);
3249a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3259a0bf528SMauro Carvalho Chehab 			goto err;
3269a0bf528SMauro Carvalho Chehab 	} else {
3279a0bf528SMauro Carvalho Chehab 		ret = af9033_wr_reg_mask(state, 0x80f990, 0x00, 0x01);
3289a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3299a0bf528SMauro Carvalho Chehab 			goto err;
3309a0bf528SMauro Carvalho Chehab 
3319a0bf528SMauro Carvalho Chehab 		ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x00, 0x01);
3329a0bf528SMauro Carvalho Chehab 		if (ret < 0)
3339a0bf528SMauro Carvalho Chehab 			goto err;
3349a0bf528SMauro Carvalho Chehab 	}
3359a0bf528SMauro Carvalho Chehab 
3369a0bf528SMauro Carvalho Chehab 	/* load OFSM settings */
3370a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: load ofsm settings\n", __func__);
338fe8eece1SAntti Palosaari 	switch (state->cfg.tuner) {
339fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_38:
340fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_51:
341fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_52:
342463c399cSAntti Palosaari 		len = ARRAY_SIZE(ofsm_init_it9135_v1);
343463c399cSAntti Palosaari 		init = ofsm_init_it9135_v1;
344463c399cSAntti Palosaari 		break;
345fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_60:
346fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_61:
347fe8eece1SAntti Palosaari 	case AF9033_TUNER_IT9135_62:
348463c399cSAntti Palosaari 		len = ARRAY_SIZE(ofsm_init_it9135_v2);
349463c399cSAntti Palosaari 		init = ofsm_init_it9135_v2;
350fe8eece1SAntti Palosaari 		break;
351fe8eece1SAntti Palosaari 	default:
3529a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(ofsm_init);
3539a0bf528SMauro Carvalho Chehab 		init = ofsm_init;
354fe8eece1SAntti Palosaari 		break;
355fe8eece1SAntti Palosaari 	}
356fe8eece1SAntti Palosaari 
3573bf5e552SAntti Palosaari 	ret = af9033_wr_reg_val_tab(state, init, len);
3589a0bf528SMauro Carvalho Chehab 	if (ret < 0)
3599a0bf528SMauro Carvalho Chehab 		goto err;
3609a0bf528SMauro Carvalho Chehab 
3619a0bf528SMauro Carvalho Chehab 	/* load tuner specific settings */
3620a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: load tuner specific settings\n",
3639a0bf528SMauro Carvalho Chehab 			__func__);
3649a0bf528SMauro Carvalho Chehab 	switch (state->cfg.tuner) {
3659a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_TUA9001:
3669a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_tua9001);
3679a0bf528SMauro Carvalho Chehab 		init = tuner_init_tua9001;
3689a0bf528SMauro Carvalho Chehab 		break;
3699a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_FC0011:
3709a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_fc0011);
3719a0bf528SMauro Carvalho Chehab 		init = tuner_init_fc0011;
3729a0bf528SMauro Carvalho Chehab 		break;
3739a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_MXL5007T:
3749a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_mxl5007t);
3759a0bf528SMauro Carvalho Chehab 		init = tuner_init_mxl5007t;
3769a0bf528SMauro Carvalho Chehab 		break;
3779a0bf528SMauro Carvalho Chehab 	case AF9033_TUNER_TDA18218:
3789a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(tuner_init_tda18218);
3799a0bf528SMauro Carvalho Chehab 		init = tuner_init_tda18218;
3809a0bf528SMauro Carvalho Chehab 		break;
381d67ceb33SOliver Schinagl 	case AF9033_TUNER_FC2580:
382d67ceb33SOliver Schinagl 		len = ARRAY_SIZE(tuner_init_fc2580);
383d67ceb33SOliver Schinagl 		init = tuner_init_fc2580;
384d67ceb33SOliver Schinagl 		break;
385e713ad15SAntti Palosaari 	case AF9033_TUNER_FC0012:
386e713ad15SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_fc0012);
387e713ad15SAntti Palosaari 		init = tuner_init_fc0012;
388e713ad15SAntti Palosaari 		break;
3894902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_38:
390a72cbb77SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_38);
391a72cbb77SAntti Palosaari 		init = tuner_init_it9135_38;
392a72cbb77SAntti Palosaari 		break;
3934902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_51:
394bb2e12a6SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_51);
395bb2e12a6SAntti Palosaari 		init = tuner_init_it9135_51;
396bb2e12a6SAntti Palosaari 		break;
3974902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_52:
39822d729f3SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_52);
39922d729f3SAntti Palosaari 		init = tuner_init_it9135_52;
40022d729f3SAntti Palosaari 		break;
4014902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_60:
402a49f53a0SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_60);
403a49f53a0SAntti Palosaari 		init = tuner_init_it9135_60;
404a49f53a0SAntti Palosaari 		break;
4054902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_61:
40685211323SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_61);
40785211323SAntti Palosaari 		init = tuner_init_it9135_61;
40885211323SAntti Palosaari 		break;
4094902bb39SAntti Palosaari 	case AF9033_TUNER_IT9135_62:
410dc4a2c40SAntti Palosaari 		len = ARRAY_SIZE(tuner_init_it9135_62);
411dc4a2c40SAntti Palosaari 		init = tuner_init_it9135_62;
4124902bb39SAntti Palosaari 		break;
4139a0bf528SMauro Carvalho Chehab 	default:
4140a73f2d6SAntti Palosaari 		dev_dbg(&state->i2c->dev, "%s: unsupported tuner ID=%d\n",
4150a73f2d6SAntti Palosaari 				__func__, state->cfg.tuner);
4169a0bf528SMauro Carvalho Chehab 		ret = -ENODEV;
4179a0bf528SMauro Carvalho Chehab 		goto err;
4189a0bf528SMauro Carvalho Chehab 	}
4199a0bf528SMauro Carvalho Chehab 
4203bf5e552SAntti Palosaari 	ret = af9033_wr_reg_val_tab(state, init, len);
4219a0bf528SMauro Carvalho Chehab 	if (ret < 0)
4229a0bf528SMauro Carvalho Chehab 		goto err;
4239a0bf528SMauro Carvalho Chehab 
4249805992fSJose Alberto Reguero 	if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
4259805992fSJose Alberto Reguero 		ret = af9033_wr_reg_mask(state, 0x00d91c, 0x01, 0x01);
4269805992fSJose Alberto Reguero 		if (ret < 0)
4279805992fSJose Alberto Reguero 			goto err;
428bf97b637SAntti Palosaari 
4299805992fSJose Alberto Reguero 		ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01);
4309805992fSJose Alberto Reguero 		if (ret < 0)
4319805992fSJose Alberto Reguero 			goto err;
432bf97b637SAntti Palosaari 
4339805992fSJose Alberto Reguero 		ret = af9033_wr_reg_mask(state, 0x00d916, 0x00, 0x01);
4349805992fSJose Alberto Reguero 		if (ret < 0)
4359805992fSJose Alberto Reguero 			goto err;
4369805992fSJose Alberto Reguero 	}
4379805992fSJose Alberto Reguero 
438086991ddSAntti Palosaari 	switch (state->cfg.tuner) {
439086991ddSAntti Palosaari 	case AF9033_TUNER_IT9135_60:
440086991ddSAntti Palosaari 	case AF9033_TUNER_IT9135_61:
441086991ddSAntti Palosaari 	case AF9033_TUNER_IT9135_62:
442086991ddSAntti Palosaari 		ret = af9033_wr_reg(state, 0x800000, 0x01);
443086991ddSAntti Palosaari 		if (ret < 0)
444086991ddSAntti Palosaari 			goto err;
445086991ddSAntti Palosaari 	}
446086991ddSAntti Palosaari 
4479a0bf528SMauro Carvalho Chehab 	state->bandwidth_hz = 0; /* force to program all parameters */
4489a0bf528SMauro Carvalho Chehab 
4499a0bf528SMauro Carvalho Chehab 	return 0;
4509a0bf528SMauro Carvalho Chehab 
4519a0bf528SMauro Carvalho Chehab err:
4520a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
4539a0bf528SMauro Carvalho Chehab 
4549a0bf528SMauro Carvalho Chehab 	return ret;
4559a0bf528SMauro Carvalho Chehab }
4569a0bf528SMauro Carvalho Chehab 
4579a0bf528SMauro Carvalho Chehab static int af9033_sleep(struct dvb_frontend *fe)
4589a0bf528SMauro Carvalho Chehab {
4599a0bf528SMauro Carvalho Chehab 	struct af9033_state *state = fe->demodulator_priv;
4609a0bf528SMauro Carvalho Chehab 	int ret, i;
4619a0bf528SMauro Carvalho Chehab 	u8 tmp;
4629a0bf528SMauro Carvalho Chehab 
4639a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_reg(state, 0x80004c, 1);
4649a0bf528SMauro Carvalho Chehab 	if (ret < 0)
4659a0bf528SMauro Carvalho Chehab 		goto err;
4669a0bf528SMauro Carvalho Chehab 
4679a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_reg(state, 0x800000, 0);
4689a0bf528SMauro Carvalho Chehab 	if (ret < 0)
4699a0bf528SMauro Carvalho Chehab 		goto err;
4709a0bf528SMauro Carvalho Chehab 
4719a0bf528SMauro Carvalho Chehab 	for (i = 100, tmp = 1; i && tmp; i--) {
4729a0bf528SMauro Carvalho Chehab 		ret = af9033_rd_reg(state, 0x80004c, &tmp);
4739a0bf528SMauro Carvalho Chehab 		if (ret < 0)
4749a0bf528SMauro Carvalho Chehab 			goto err;
4759a0bf528SMauro Carvalho Chehab 
4769a0bf528SMauro Carvalho Chehab 		usleep_range(200, 10000);
4779a0bf528SMauro Carvalho Chehab 	}
4789a0bf528SMauro Carvalho Chehab 
4790a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: loop=%d\n", __func__, i);
4809a0bf528SMauro Carvalho Chehab 
4819a0bf528SMauro Carvalho Chehab 	if (i == 0) {
4829a0bf528SMauro Carvalho Chehab 		ret = -ETIMEDOUT;
4839a0bf528SMauro Carvalho Chehab 		goto err;
4849a0bf528SMauro Carvalho Chehab 	}
4859a0bf528SMauro Carvalho Chehab 
4869a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_reg_mask(state, 0x80fb24, 0x08, 0x08);
4879a0bf528SMauro Carvalho Chehab 	if (ret < 0)
4889a0bf528SMauro Carvalho Chehab 		goto err;
4899a0bf528SMauro Carvalho Chehab 
4909a0bf528SMauro Carvalho Chehab 	/* prevent current leak (?) */
4919a0bf528SMauro Carvalho Chehab 	if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
4929a0bf528SMauro Carvalho Chehab 		/* enable parallel TS */
4939a0bf528SMauro Carvalho Chehab 		ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01);
4949a0bf528SMauro Carvalho Chehab 		if (ret < 0)
4959a0bf528SMauro Carvalho Chehab 			goto err;
4969a0bf528SMauro Carvalho Chehab 
4979a0bf528SMauro Carvalho Chehab 		ret = af9033_wr_reg_mask(state, 0x00d916, 0x01, 0x01);
4989a0bf528SMauro Carvalho Chehab 		if (ret < 0)
4999a0bf528SMauro Carvalho Chehab 			goto err;
5009a0bf528SMauro Carvalho Chehab 	}
5019a0bf528SMauro Carvalho Chehab 
5029a0bf528SMauro Carvalho Chehab 	return 0;
5039a0bf528SMauro Carvalho Chehab 
5049a0bf528SMauro Carvalho Chehab err:
5050a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
5069a0bf528SMauro Carvalho Chehab 
5079a0bf528SMauro Carvalho Chehab 	return ret;
5089a0bf528SMauro Carvalho Chehab }
5099a0bf528SMauro Carvalho Chehab 
5109a0bf528SMauro Carvalho Chehab static int af9033_get_tune_settings(struct dvb_frontend *fe,
5119a0bf528SMauro Carvalho Chehab 		struct dvb_frontend_tune_settings *fesettings)
5129a0bf528SMauro Carvalho Chehab {
513fe8eece1SAntti Palosaari 	/* 800 => 2000 because IT9135 v2 is slow to gain lock */
514fe8eece1SAntti Palosaari 	fesettings->min_delay_ms = 2000;
5159a0bf528SMauro Carvalho Chehab 	fesettings->step_size = 0;
5169a0bf528SMauro Carvalho Chehab 	fesettings->max_drift = 0;
5179a0bf528SMauro Carvalho Chehab 
5189a0bf528SMauro Carvalho Chehab 	return 0;
5199a0bf528SMauro Carvalho Chehab }
5209a0bf528SMauro Carvalho Chehab 
5219a0bf528SMauro Carvalho Chehab static int af9033_set_frontend(struct dvb_frontend *fe)
5229a0bf528SMauro Carvalho Chehab {
5239a0bf528SMauro Carvalho Chehab 	struct af9033_state *state = fe->demodulator_priv;
5249a0bf528SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
525182b967eSHans-Frieder Vogt 	int ret, i, spec_inv, sampling_freq;
5269a0bf528SMauro Carvalho Chehab 	u8 tmp, buf[3], bandwidth_reg_val;
5279a0bf528SMauro Carvalho Chehab 	u32 if_frequency, freq_cw, adc_freq;
5289a0bf528SMauro Carvalho Chehab 
5290a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n",
5300a73f2d6SAntti Palosaari 			__func__, c->frequency, c->bandwidth_hz);
5319a0bf528SMauro Carvalho Chehab 
5329a0bf528SMauro Carvalho Chehab 	/* check bandwidth */
5339a0bf528SMauro Carvalho Chehab 	switch (c->bandwidth_hz) {
5349a0bf528SMauro Carvalho Chehab 	case 6000000:
5359a0bf528SMauro Carvalho Chehab 		bandwidth_reg_val = 0x00;
5369a0bf528SMauro Carvalho Chehab 		break;
5379a0bf528SMauro Carvalho Chehab 	case 7000000:
5389a0bf528SMauro Carvalho Chehab 		bandwidth_reg_val = 0x01;
5399a0bf528SMauro Carvalho Chehab 		break;
5409a0bf528SMauro Carvalho Chehab 	case 8000000:
5419a0bf528SMauro Carvalho Chehab 		bandwidth_reg_val = 0x02;
5429a0bf528SMauro Carvalho Chehab 		break;
5439a0bf528SMauro Carvalho Chehab 	default:
5440a73f2d6SAntti Palosaari 		dev_dbg(&state->i2c->dev, "%s: invalid bandwidth_hz\n",
5450a73f2d6SAntti Palosaari 				__func__);
5469a0bf528SMauro Carvalho Chehab 		ret = -EINVAL;
5479a0bf528SMauro Carvalho Chehab 		goto err;
5489a0bf528SMauro Carvalho Chehab 	}
5499a0bf528SMauro Carvalho Chehab 
5509a0bf528SMauro Carvalho Chehab 	/* program tuner */
5519a0bf528SMauro Carvalho Chehab 	if (fe->ops.tuner_ops.set_params)
5529a0bf528SMauro Carvalho Chehab 		fe->ops.tuner_ops.set_params(fe);
5539a0bf528SMauro Carvalho Chehab 
5549a0bf528SMauro Carvalho Chehab 	/* program CFOE coefficients */
5559a0bf528SMauro Carvalho Chehab 	if (c->bandwidth_hz != state->bandwidth_hz) {
5569a0bf528SMauro Carvalho Chehab 		for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
5579a0bf528SMauro Carvalho Chehab 			if (coeff_lut[i].clock == state->cfg.clock &&
5589a0bf528SMauro Carvalho Chehab 				coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
5599a0bf528SMauro Carvalho Chehab 				break;
5609a0bf528SMauro Carvalho Chehab 			}
5619a0bf528SMauro Carvalho Chehab 		}
5629a0bf528SMauro Carvalho Chehab 		ret =  af9033_wr_regs(state, 0x800001,
5639a0bf528SMauro Carvalho Chehab 				coeff_lut[i].val, sizeof(coeff_lut[i].val));
5649a0bf528SMauro Carvalho Chehab 	}
5659a0bf528SMauro Carvalho Chehab 
5669a0bf528SMauro Carvalho Chehab 	/* program frequency control */
5679a0bf528SMauro Carvalho Chehab 	if (c->bandwidth_hz != state->bandwidth_hz) {
5689a0bf528SMauro Carvalho Chehab 		spec_inv = state->cfg.spec_inv ? -1 : 1;
5699a0bf528SMauro Carvalho Chehab 
5709a0bf528SMauro Carvalho Chehab 		for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
5719a0bf528SMauro Carvalho Chehab 			if (clock_adc_lut[i].clock == state->cfg.clock)
5729a0bf528SMauro Carvalho Chehab 				break;
5739a0bf528SMauro Carvalho Chehab 		}
5749a0bf528SMauro Carvalho Chehab 		adc_freq = clock_adc_lut[i].adc;
5759a0bf528SMauro Carvalho Chehab 
5769a0bf528SMauro Carvalho Chehab 		/* get used IF frequency */
5779a0bf528SMauro Carvalho Chehab 		if (fe->ops.tuner_ops.get_if_frequency)
5789a0bf528SMauro Carvalho Chehab 			fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
5799a0bf528SMauro Carvalho Chehab 		else
5809a0bf528SMauro Carvalho Chehab 			if_frequency = 0;
5819a0bf528SMauro Carvalho Chehab 
582182b967eSHans-Frieder Vogt 		sampling_freq = if_frequency;
5839a0bf528SMauro Carvalho Chehab 
584182b967eSHans-Frieder Vogt 		while (sampling_freq > (adc_freq / 2))
585182b967eSHans-Frieder Vogt 			sampling_freq -= adc_freq;
586182b967eSHans-Frieder Vogt 
587182b967eSHans-Frieder Vogt 		if (sampling_freq >= 0)
5889a0bf528SMauro Carvalho Chehab 			spec_inv *= -1;
5899a0bf528SMauro Carvalho Chehab 		else
590182b967eSHans-Frieder Vogt 			sampling_freq *= -1;
5919a0bf528SMauro Carvalho Chehab 
592182b967eSHans-Frieder Vogt 		freq_cw = af9033_div(state, sampling_freq, adc_freq, 23ul);
5939a0bf528SMauro Carvalho Chehab 
5949a0bf528SMauro Carvalho Chehab 		if (spec_inv == -1)
595182b967eSHans-Frieder Vogt 			freq_cw = 0x800000 - freq_cw;
5969a0bf528SMauro Carvalho Chehab 
5974902bb39SAntti Palosaari 		if (state->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
5989a0bf528SMauro Carvalho Chehab 			freq_cw /= 2;
5999a0bf528SMauro Carvalho Chehab 
6009a0bf528SMauro Carvalho Chehab 		buf[0] = (freq_cw >>  0) & 0xff;
6019a0bf528SMauro Carvalho Chehab 		buf[1] = (freq_cw >>  8) & 0xff;
6029a0bf528SMauro Carvalho Chehab 		buf[2] = (freq_cw >> 16) & 0x7f;
603fe8eece1SAntti Palosaari 
604fe8eece1SAntti Palosaari 		/* FIXME: there seems to be calculation error here... */
605fe8eece1SAntti Palosaari 		if (if_frequency == 0)
606fe8eece1SAntti Palosaari 			buf[2] = 0;
607fe8eece1SAntti Palosaari 
6089a0bf528SMauro Carvalho Chehab 		ret = af9033_wr_regs(state, 0x800029, buf, 3);
6099a0bf528SMauro Carvalho Chehab 		if (ret < 0)
6109a0bf528SMauro Carvalho Chehab 			goto err;
6119a0bf528SMauro Carvalho Chehab 
6129a0bf528SMauro Carvalho Chehab 		state->bandwidth_hz = c->bandwidth_hz;
6139a0bf528SMauro Carvalho Chehab 	}
6149a0bf528SMauro Carvalho Chehab 
6159a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_reg_mask(state, 0x80f904, bandwidth_reg_val, 0x03);
6169a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6179a0bf528SMauro Carvalho Chehab 		goto err;
6189a0bf528SMauro Carvalho Chehab 
6199a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_reg(state, 0x800040, 0x00);
6209a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6219a0bf528SMauro Carvalho Chehab 		goto err;
6229a0bf528SMauro Carvalho Chehab 
6239a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_reg(state, 0x800047, 0x00);
6249a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6259a0bf528SMauro Carvalho Chehab 		goto err;
6269a0bf528SMauro Carvalho Chehab 
6279a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_reg_mask(state, 0x80f999, 0x00, 0x01);
6289a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6299a0bf528SMauro Carvalho Chehab 		goto err;
6309a0bf528SMauro Carvalho Chehab 
6319a0bf528SMauro Carvalho Chehab 	if (c->frequency <= 230000000)
6329a0bf528SMauro Carvalho Chehab 		tmp = 0x00; /* VHF */
6339a0bf528SMauro Carvalho Chehab 	else
6349a0bf528SMauro Carvalho Chehab 		tmp = 0x01; /* UHF */
6359a0bf528SMauro Carvalho Chehab 
6369a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_reg(state, 0x80004b, tmp);
6379a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6389a0bf528SMauro Carvalho Chehab 		goto err;
6399a0bf528SMauro Carvalho Chehab 
6409a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_reg(state, 0x800000, 0x00);
6419a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6429a0bf528SMauro Carvalho Chehab 		goto err;
6439a0bf528SMauro Carvalho Chehab 
6449a0bf528SMauro Carvalho Chehab 	return 0;
6459a0bf528SMauro Carvalho Chehab 
6469a0bf528SMauro Carvalho Chehab err:
6470a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
6489a0bf528SMauro Carvalho Chehab 
6499a0bf528SMauro Carvalho Chehab 	return ret;
6509a0bf528SMauro Carvalho Chehab }
6519a0bf528SMauro Carvalho Chehab 
6529a0bf528SMauro Carvalho Chehab static int af9033_get_frontend(struct dvb_frontend *fe)
6539a0bf528SMauro Carvalho Chehab {
6549a0bf528SMauro Carvalho Chehab 	struct af9033_state *state = fe->demodulator_priv;
6559a0bf528SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
6569a0bf528SMauro Carvalho Chehab 	int ret;
6579a0bf528SMauro Carvalho Chehab 	u8 buf[8];
6589a0bf528SMauro Carvalho Chehab 
6590a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s:\n", __func__);
6609a0bf528SMauro Carvalho Chehab 
6619a0bf528SMauro Carvalho Chehab 	/* read all needed registers */
6629a0bf528SMauro Carvalho Chehab 	ret = af9033_rd_regs(state, 0x80f900, buf, sizeof(buf));
6639a0bf528SMauro Carvalho Chehab 	if (ret < 0)
6649a0bf528SMauro Carvalho Chehab 		goto err;
6659a0bf528SMauro Carvalho Chehab 
6669a0bf528SMauro Carvalho Chehab 	switch ((buf[0] >> 0) & 3) {
6679a0bf528SMauro Carvalho Chehab 	case 0:
6689a0bf528SMauro Carvalho Chehab 		c->transmission_mode = TRANSMISSION_MODE_2K;
6699a0bf528SMauro Carvalho Chehab 		break;
6709a0bf528SMauro Carvalho Chehab 	case 1:
6719a0bf528SMauro Carvalho Chehab 		c->transmission_mode = TRANSMISSION_MODE_8K;
6729a0bf528SMauro Carvalho Chehab 		break;
6739a0bf528SMauro Carvalho Chehab 	}
6749a0bf528SMauro Carvalho Chehab 
6759a0bf528SMauro Carvalho Chehab 	switch ((buf[1] >> 0) & 3) {
6769a0bf528SMauro Carvalho Chehab 	case 0:
6779a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_32;
6789a0bf528SMauro Carvalho Chehab 		break;
6799a0bf528SMauro Carvalho Chehab 	case 1:
6809a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_16;
6819a0bf528SMauro Carvalho Chehab 		break;
6829a0bf528SMauro Carvalho Chehab 	case 2:
6839a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_8;
6849a0bf528SMauro Carvalho Chehab 		break;
6859a0bf528SMauro Carvalho Chehab 	case 3:
6869a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_4;
6879a0bf528SMauro Carvalho Chehab 		break;
6889a0bf528SMauro Carvalho Chehab 	}
6899a0bf528SMauro Carvalho Chehab 
6909a0bf528SMauro Carvalho Chehab 	switch ((buf[2] >> 0) & 7) {
6919a0bf528SMauro Carvalho Chehab 	case 0:
6929a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_NONE;
6939a0bf528SMauro Carvalho Chehab 		break;
6949a0bf528SMauro Carvalho Chehab 	case 1:
6959a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_1;
6969a0bf528SMauro Carvalho Chehab 		break;
6979a0bf528SMauro Carvalho Chehab 	case 2:
6989a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_2;
6999a0bf528SMauro Carvalho Chehab 		break;
7009a0bf528SMauro Carvalho Chehab 	case 3:
7019a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_4;
7029a0bf528SMauro Carvalho Chehab 		break;
7039a0bf528SMauro Carvalho Chehab 	}
7049a0bf528SMauro Carvalho Chehab 
7059a0bf528SMauro Carvalho Chehab 	switch ((buf[3] >> 0) & 3) {
7069a0bf528SMauro Carvalho Chehab 	case 0:
7079a0bf528SMauro Carvalho Chehab 		c->modulation = QPSK;
7089a0bf528SMauro Carvalho Chehab 		break;
7099a0bf528SMauro Carvalho Chehab 	case 1:
7109a0bf528SMauro Carvalho Chehab 		c->modulation = QAM_16;
7119a0bf528SMauro Carvalho Chehab 		break;
7129a0bf528SMauro Carvalho Chehab 	case 2:
7139a0bf528SMauro Carvalho Chehab 		c->modulation = QAM_64;
7149a0bf528SMauro Carvalho Chehab 		break;
7159a0bf528SMauro Carvalho Chehab 	}
7169a0bf528SMauro Carvalho Chehab 
7179a0bf528SMauro Carvalho Chehab 	switch ((buf[4] >> 0) & 3) {
7189a0bf528SMauro Carvalho Chehab 	case 0:
7199a0bf528SMauro Carvalho Chehab 		c->bandwidth_hz = 6000000;
7209a0bf528SMauro Carvalho Chehab 		break;
7219a0bf528SMauro Carvalho Chehab 	case 1:
7229a0bf528SMauro Carvalho Chehab 		c->bandwidth_hz = 7000000;
7239a0bf528SMauro Carvalho Chehab 		break;
7249a0bf528SMauro Carvalho Chehab 	case 2:
7259a0bf528SMauro Carvalho Chehab 		c->bandwidth_hz = 8000000;
7269a0bf528SMauro Carvalho Chehab 		break;
7279a0bf528SMauro Carvalho Chehab 	}
7289a0bf528SMauro Carvalho Chehab 
7299a0bf528SMauro Carvalho Chehab 	switch ((buf[6] >> 0) & 7) {
7309a0bf528SMauro Carvalho Chehab 	case 0:
7319a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_1_2;
7329a0bf528SMauro Carvalho Chehab 		break;
7339a0bf528SMauro Carvalho Chehab 	case 1:
7349a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_2_3;
7359a0bf528SMauro Carvalho Chehab 		break;
7369a0bf528SMauro Carvalho Chehab 	case 2:
7379a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_3_4;
7389a0bf528SMauro Carvalho Chehab 		break;
7399a0bf528SMauro Carvalho Chehab 	case 3:
7409a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_5_6;
7419a0bf528SMauro Carvalho Chehab 		break;
7429a0bf528SMauro Carvalho Chehab 	case 4:
7439a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_7_8;
7449a0bf528SMauro Carvalho Chehab 		break;
7459a0bf528SMauro Carvalho Chehab 	case 5:
7469a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_NONE;
7479a0bf528SMauro Carvalho Chehab 		break;
7489a0bf528SMauro Carvalho Chehab 	}
7499a0bf528SMauro Carvalho Chehab 
7509a0bf528SMauro Carvalho Chehab 	switch ((buf[7] >> 0) & 7) {
7519a0bf528SMauro Carvalho Chehab 	case 0:
7529a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_1_2;
7539a0bf528SMauro Carvalho Chehab 		break;
7549a0bf528SMauro Carvalho Chehab 	case 1:
7559a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_2_3;
7569a0bf528SMauro Carvalho Chehab 		break;
7579a0bf528SMauro Carvalho Chehab 	case 2:
7589a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_3_4;
7599a0bf528SMauro Carvalho Chehab 		break;
7609a0bf528SMauro Carvalho Chehab 	case 3:
7619a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_5_6;
7629a0bf528SMauro Carvalho Chehab 		break;
7639a0bf528SMauro Carvalho Chehab 	case 4:
7649a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_7_8;
7659a0bf528SMauro Carvalho Chehab 		break;
7669a0bf528SMauro Carvalho Chehab 	case 5:
7679a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_NONE;
7689a0bf528SMauro Carvalho Chehab 		break;
7699a0bf528SMauro Carvalho Chehab 	}
7709a0bf528SMauro Carvalho Chehab 
7719a0bf528SMauro Carvalho Chehab 	return 0;
7729a0bf528SMauro Carvalho Chehab 
7739a0bf528SMauro Carvalho Chehab err:
7740a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
7759a0bf528SMauro Carvalho Chehab 
7769a0bf528SMauro Carvalho Chehab 	return ret;
7779a0bf528SMauro Carvalho Chehab }
7789a0bf528SMauro Carvalho Chehab 
7799a0bf528SMauro Carvalho Chehab static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status)
7809a0bf528SMauro Carvalho Chehab {
7819a0bf528SMauro Carvalho Chehab 	struct af9033_state *state = fe->demodulator_priv;
7829a0bf528SMauro Carvalho Chehab 	int ret;
7839a0bf528SMauro Carvalho Chehab 	u8 tmp;
7849a0bf528SMauro Carvalho Chehab 
7859a0bf528SMauro Carvalho Chehab 	*status = 0;
7869a0bf528SMauro Carvalho Chehab 
7879a0bf528SMauro Carvalho Chehab 	/* radio channel status, 0=no result, 1=has signal, 2=no signal */
7889a0bf528SMauro Carvalho Chehab 	ret = af9033_rd_reg(state, 0x800047, &tmp);
7899a0bf528SMauro Carvalho Chehab 	if (ret < 0)
7909a0bf528SMauro Carvalho Chehab 		goto err;
7919a0bf528SMauro Carvalho Chehab 
7929a0bf528SMauro Carvalho Chehab 	/* has signal */
7939a0bf528SMauro Carvalho Chehab 	if (tmp == 0x01)
7949a0bf528SMauro Carvalho Chehab 		*status |= FE_HAS_SIGNAL;
7959a0bf528SMauro Carvalho Chehab 
7969a0bf528SMauro Carvalho Chehab 	if (tmp != 0x02) {
7979a0bf528SMauro Carvalho Chehab 		/* TPS lock */
7989a0bf528SMauro Carvalho Chehab 		ret = af9033_rd_reg_mask(state, 0x80f5a9, &tmp, 0x01);
7999a0bf528SMauro Carvalho Chehab 		if (ret < 0)
8009a0bf528SMauro Carvalho Chehab 			goto err;
8019a0bf528SMauro Carvalho Chehab 
8029a0bf528SMauro Carvalho Chehab 		if (tmp)
8039a0bf528SMauro Carvalho Chehab 			*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
8049a0bf528SMauro Carvalho Chehab 					FE_HAS_VITERBI;
8059a0bf528SMauro Carvalho Chehab 
8069a0bf528SMauro Carvalho Chehab 		/* full lock */
8079a0bf528SMauro Carvalho Chehab 		ret = af9033_rd_reg_mask(state, 0x80f999, &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 | FE_HAS_SYNC |
8149a0bf528SMauro Carvalho Chehab 					FE_HAS_LOCK;
8159a0bf528SMauro Carvalho Chehab 	}
8169a0bf528SMauro Carvalho Chehab 
8179a0bf528SMauro Carvalho Chehab 	return 0;
8189a0bf528SMauro Carvalho Chehab 
8199a0bf528SMauro Carvalho Chehab err:
8200a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
8219a0bf528SMauro Carvalho Chehab 
8229a0bf528SMauro Carvalho Chehab 	return ret;
8239a0bf528SMauro Carvalho Chehab }
8249a0bf528SMauro Carvalho Chehab 
8259a0bf528SMauro Carvalho Chehab static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
8269a0bf528SMauro Carvalho Chehab {
8279a0bf528SMauro Carvalho Chehab 	struct af9033_state *state = fe->demodulator_priv;
8289a0bf528SMauro Carvalho Chehab 	int ret, i, len;
8299a0bf528SMauro Carvalho Chehab 	u8 buf[3], tmp;
8309a0bf528SMauro Carvalho Chehab 	u32 snr_val;
8319a0bf528SMauro Carvalho Chehab 	const struct val_snr *uninitialized_var(snr_lut);
8329a0bf528SMauro Carvalho Chehab 
8339a0bf528SMauro Carvalho Chehab 	/* read value */
8349a0bf528SMauro Carvalho Chehab 	ret = af9033_rd_regs(state, 0x80002c, buf, 3);
8359a0bf528SMauro Carvalho Chehab 	if (ret < 0)
8369a0bf528SMauro Carvalho Chehab 		goto err;
8379a0bf528SMauro Carvalho Chehab 
8389a0bf528SMauro Carvalho Chehab 	snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
8399a0bf528SMauro Carvalho Chehab 
8409a0bf528SMauro Carvalho Chehab 	/* read current modulation */
8419a0bf528SMauro Carvalho Chehab 	ret = af9033_rd_reg(state, 0x80f903, &tmp);
8429a0bf528SMauro Carvalho Chehab 	if (ret < 0)
8439a0bf528SMauro Carvalho Chehab 		goto err;
8449a0bf528SMauro Carvalho Chehab 
8459a0bf528SMauro Carvalho Chehab 	switch ((tmp >> 0) & 3) {
8469a0bf528SMauro Carvalho Chehab 	case 0:
8479a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(qpsk_snr_lut);
8489a0bf528SMauro Carvalho Chehab 		snr_lut = qpsk_snr_lut;
8499a0bf528SMauro Carvalho Chehab 		break;
8509a0bf528SMauro Carvalho Chehab 	case 1:
8519a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(qam16_snr_lut);
8529a0bf528SMauro Carvalho Chehab 		snr_lut = qam16_snr_lut;
8539a0bf528SMauro Carvalho Chehab 		break;
8549a0bf528SMauro Carvalho Chehab 	case 2:
8559a0bf528SMauro Carvalho Chehab 		len = ARRAY_SIZE(qam64_snr_lut);
8569a0bf528SMauro Carvalho Chehab 		snr_lut = qam64_snr_lut;
8579a0bf528SMauro Carvalho Chehab 		break;
8589a0bf528SMauro Carvalho Chehab 	default:
8599a0bf528SMauro Carvalho Chehab 		goto err;
8609a0bf528SMauro Carvalho Chehab 	}
8619a0bf528SMauro Carvalho Chehab 
8629a0bf528SMauro Carvalho Chehab 	for (i = 0; i < len; i++) {
8639a0bf528SMauro Carvalho Chehab 		tmp = snr_lut[i].snr;
8649a0bf528SMauro Carvalho Chehab 
8659a0bf528SMauro Carvalho Chehab 		if (snr_val < snr_lut[i].val)
8669a0bf528SMauro Carvalho Chehab 			break;
8679a0bf528SMauro Carvalho Chehab 	}
8689a0bf528SMauro Carvalho Chehab 
8699a0bf528SMauro Carvalho Chehab 	*snr = tmp * 10; /* dB/10 */
8709a0bf528SMauro Carvalho Chehab 
8719a0bf528SMauro Carvalho Chehab 	return 0;
8729a0bf528SMauro Carvalho Chehab 
8739a0bf528SMauro Carvalho Chehab err:
8740a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
8759a0bf528SMauro Carvalho Chehab 
8769a0bf528SMauro Carvalho Chehab 	return ret;
8779a0bf528SMauro Carvalho Chehab }
8789a0bf528SMauro Carvalho Chehab 
8799a0bf528SMauro Carvalho Chehab static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
8809a0bf528SMauro Carvalho Chehab {
8819a0bf528SMauro Carvalho Chehab 	struct af9033_state *state = fe->demodulator_priv;
8829a0bf528SMauro Carvalho Chehab 	int ret;
8839a0bf528SMauro Carvalho Chehab 	u8 strength2;
8849a0bf528SMauro Carvalho Chehab 
8859a0bf528SMauro Carvalho Chehab 	/* read signal strength of 0-100 scale */
8869a0bf528SMauro Carvalho Chehab 	ret = af9033_rd_reg(state, 0x800048, &strength2);
8879a0bf528SMauro Carvalho Chehab 	if (ret < 0)
8889a0bf528SMauro Carvalho Chehab 		goto err;
8899a0bf528SMauro Carvalho Chehab 
8909a0bf528SMauro Carvalho Chehab 	/* scale value to 0x0000-0xffff */
8919a0bf528SMauro Carvalho Chehab 	*strength = strength2 * 0xffff / 100;
8929a0bf528SMauro Carvalho Chehab 
8939a0bf528SMauro Carvalho Chehab 	return 0;
8949a0bf528SMauro Carvalho Chehab 
8959a0bf528SMauro Carvalho Chehab err:
8960a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
8979a0bf528SMauro Carvalho Chehab 
8989a0bf528SMauro Carvalho Chehab 	return ret;
8999a0bf528SMauro Carvalho Chehab }
9009a0bf528SMauro Carvalho Chehab 
9019a0bf528SMauro Carvalho Chehab static int af9033_update_ch_stat(struct af9033_state *state)
9029a0bf528SMauro Carvalho Chehab {
9039a0bf528SMauro Carvalho Chehab 	int ret = 0;
9049a0bf528SMauro Carvalho Chehab 	u32 err_cnt, bit_cnt;
9059a0bf528SMauro Carvalho Chehab 	u16 abort_cnt;
9069a0bf528SMauro Carvalho Chehab 	u8 buf[7];
9079a0bf528SMauro Carvalho Chehab 
9089a0bf528SMauro Carvalho Chehab 	/* only update data every half second */
9099a0bf528SMauro Carvalho Chehab 	if (time_after(jiffies, state->last_stat_check + msecs_to_jiffies(500))) {
9109a0bf528SMauro Carvalho Chehab 		ret = af9033_rd_regs(state, 0x800032, buf, sizeof(buf));
9119a0bf528SMauro Carvalho Chehab 		if (ret < 0)
9129a0bf528SMauro Carvalho Chehab 			goto err;
9139a0bf528SMauro Carvalho Chehab 		/* in 8 byte packets? */
9149a0bf528SMauro Carvalho Chehab 		abort_cnt = (buf[1] << 8) + buf[0];
9159a0bf528SMauro Carvalho Chehab 		/* in bits */
9169a0bf528SMauro Carvalho Chehab 		err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2];
9179a0bf528SMauro Carvalho Chehab 		/* in 8 byte packets? always(?) 0x2710 = 10000 */
9189a0bf528SMauro Carvalho Chehab 		bit_cnt = (buf[6] << 8) + buf[5];
9199a0bf528SMauro Carvalho Chehab 
9209a0bf528SMauro Carvalho Chehab 		if (bit_cnt < abort_cnt) {
9219a0bf528SMauro Carvalho Chehab 			abort_cnt = 1000;
9229a0bf528SMauro Carvalho Chehab 			state->ber = 0xffffffff;
9239a0bf528SMauro Carvalho Chehab 		} else {
9249a0bf528SMauro Carvalho Chehab 			/* 8 byte packets, that have not been rejected already */
9259a0bf528SMauro Carvalho Chehab 			bit_cnt -= (u32)abort_cnt;
9269a0bf528SMauro Carvalho Chehab 			if (bit_cnt == 0) {
9279a0bf528SMauro Carvalho Chehab 				state->ber = 0xffffffff;
9289a0bf528SMauro Carvalho Chehab 			} else {
9299a0bf528SMauro Carvalho Chehab 				err_cnt -= (u32)abort_cnt * 8 * 8;
9309a0bf528SMauro Carvalho Chehab 				bit_cnt *= 8 * 8;
9319a0bf528SMauro Carvalho Chehab 				state->ber = err_cnt * (0xffffffff / bit_cnt);
9329a0bf528SMauro Carvalho Chehab 			}
9339a0bf528SMauro Carvalho Chehab 		}
9349a0bf528SMauro Carvalho Chehab 		state->ucb += abort_cnt;
9359a0bf528SMauro Carvalho Chehab 		state->last_stat_check = jiffies;
9369a0bf528SMauro Carvalho Chehab 	}
9379a0bf528SMauro Carvalho Chehab 
9389a0bf528SMauro Carvalho Chehab 	return 0;
9399a0bf528SMauro Carvalho Chehab err:
9400a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
9410a73f2d6SAntti Palosaari 
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 {
9479a0bf528SMauro Carvalho Chehab 	struct af9033_state *state = fe->demodulator_priv;
9489a0bf528SMauro Carvalho Chehab 	int ret;
9499a0bf528SMauro Carvalho Chehab 
9509a0bf528SMauro Carvalho Chehab 	ret = af9033_update_ch_stat(state);
9519a0bf528SMauro Carvalho Chehab 	if (ret < 0)
9529a0bf528SMauro Carvalho Chehab 		return ret;
9539a0bf528SMauro Carvalho Chehab 
9549a0bf528SMauro Carvalho Chehab 	*ber = state->ber;
9559a0bf528SMauro Carvalho Chehab 
9569a0bf528SMauro Carvalho Chehab 	return 0;
9579a0bf528SMauro Carvalho Chehab }
9589a0bf528SMauro Carvalho Chehab 
9599a0bf528SMauro Carvalho Chehab static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
9609a0bf528SMauro Carvalho Chehab {
9619a0bf528SMauro Carvalho Chehab 	struct af9033_state *state = fe->demodulator_priv;
9629a0bf528SMauro Carvalho Chehab 	int ret;
9639a0bf528SMauro Carvalho Chehab 
9649a0bf528SMauro Carvalho Chehab 	ret = af9033_update_ch_stat(state);
9659a0bf528SMauro Carvalho Chehab 	if (ret < 0)
9669a0bf528SMauro Carvalho Chehab 		return ret;
9679a0bf528SMauro Carvalho Chehab 
9689a0bf528SMauro Carvalho Chehab 	*ucblocks = state->ucb;
9699a0bf528SMauro Carvalho Chehab 
9709a0bf528SMauro Carvalho Chehab 	return 0;
9719a0bf528SMauro Carvalho Chehab }
9729a0bf528SMauro Carvalho Chehab 
9739a0bf528SMauro Carvalho Chehab static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
9749a0bf528SMauro Carvalho Chehab {
9759a0bf528SMauro Carvalho Chehab 	struct af9033_state *state = fe->demodulator_priv;
9769a0bf528SMauro Carvalho Chehab 	int ret;
9779a0bf528SMauro Carvalho Chehab 
9780a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: enable=%d\n", __func__, enable);
9799a0bf528SMauro Carvalho Chehab 
9809a0bf528SMauro Carvalho Chehab 	ret = af9033_wr_reg_mask(state, 0x00fa04, enable, 0x01);
9819a0bf528SMauro Carvalho Chehab 	if (ret < 0)
9829a0bf528SMauro Carvalho Chehab 		goto err;
9839a0bf528SMauro Carvalho Chehab 
9849a0bf528SMauro Carvalho Chehab 	return 0;
9859a0bf528SMauro Carvalho Chehab 
9869a0bf528SMauro Carvalho Chehab err:
9870a73f2d6SAntti Palosaari 	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
9889a0bf528SMauro Carvalho Chehab 
9899a0bf528SMauro Carvalho Chehab 	return ret;
9909a0bf528SMauro Carvalho Chehab }
9919a0bf528SMauro Carvalho Chehab 
9929a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops af9033_ops;
9939a0bf528SMauro Carvalho Chehab 
9949a0bf528SMauro Carvalho Chehab struct dvb_frontend *af9033_attach(const struct af9033_config *config,
9959a0bf528SMauro Carvalho Chehab 		struct i2c_adapter *i2c)
9969a0bf528SMauro Carvalho Chehab {
9979a0bf528SMauro Carvalho Chehab 	int ret;
9989a0bf528SMauro Carvalho Chehab 	struct af9033_state *state;
9999a0bf528SMauro Carvalho Chehab 	u8 buf[8];
10009a0bf528SMauro Carvalho Chehab 
10010a73f2d6SAntti Palosaari 	dev_dbg(&i2c->dev, "%s:\n", __func__);
10029a0bf528SMauro Carvalho Chehab 
10039a0bf528SMauro Carvalho Chehab 	/* allocate memory for the internal state */
10049a0bf528SMauro Carvalho Chehab 	state = kzalloc(sizeof(struct af9033_state), GFP_KERNEL);
10059a0bf528SMauro Carvalho Chehab 	if (state == NULL)
10069a0bf528SMauro Carvalho Chehab 		goto err;
10079a0bf528SMauro Carvalho Chehab 
10089a0bf528SMauro Carvalho Chehab 	/* setup the state */
10099a0bf528SMauro Carvalho Chehab 	state->i2c = i2c;
10109a0bf528SMauro Carvalho Chehab 	memcpy(&state->cfg, config, sizeof(struct af9033_config));
10119a0bf528SMauro Carvalho Chehab 
10129a0bf528SMauro Carvalho Chehab 	if (state->cfg.clock != 12000000) {
10130a73f2d6SAntti Palosaari 		dev_err(&state->i2c->dev, "%s: af9033: unsupported clock=%d, " \
10140a73f2d6SAntti Palosaari 				"only 12000000 Hz is supported currently\n",
10150a73f2d6SAntti Palosaari 				KBUILD_MODNAME, state->cfg.clock);
10169a0bf528SMauro Carvalho Chehab 		goto err;
10179a0bf528SMauro Carvalho Chehab 	}
10189a0bf528SMauro Carvalho Chehab 
10199a0bf528SMauro Carvalho Chehab 	/* firmware version */
10209a0bf528SMauro Carvalho Chehab 	ret = af9033_rd_regs(state, 0x0083e9, &buf[0], 4);
10219a0bf528SMauro Carvalho Chehab 	if (ret < 0)
10229a0bf528SMauro Carvalho Chehab 		goto err;
10239a0bf528SMauro Carvalho Chehab 
10249a0bf528SMauro Carvalho Chehab 	ret = af9033_rd_regs(state, 0x804191, &buf[4], 4);
10259a0bf528SMauro Carvalho Chehab 	if (ret < 0)
10269a0bf528SMauro Carvalho Chehab 		goto err;
10279a0bf528SMauro Carvalho Chehab 
10280a73f2d6SAntti Palosaari 	dev_info(&state->i2c->dev, "%s: firmware version: LINK=%d.%d.%d.%d " \
10290a73f2d6SAntti Palosaari 			"OFDM=%d.%d.%d.%d\n", KBUILD_MODNAME, buf[0], buf[1],
10300a73f2d6SAntti Palosaari 			buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
10319a0bf528SMauro Carvalho Chehab 
103212897dc3SAntti Palosaari 	/* sleep */
10330c13c54dSAntti Palosaari 	switch (state->cfg.tuner) {
10340c13c54dSAntti Palosaari 	case AF9033_TUNER_IT9135_38:
10350c13c54dSAntti Palosaari 	case AF9033_TUNER_IT9135_51:
10360c13c54dSAntti Palosaari 	case AF9033_TUNER_IT9135_52:
10370c13c54dSAntti Palosaari 	case AF9033_TUNER_IT9135_60:
10380c13c54dSAntti Palosaari 	case AF9033_TUNER_IT9135_61:
10390c13c54dSAntti Palosaari 	case AF9033_TUNER_IT9135_62:
10400c13c54dSAntti Palosaari 		/* IT9135 did not like to sleep at that early */
10410c13c54dSAntti Palosaari 		break;
10420c13c54dSAntti Palosaari 	default:
104312897dc3SAntti Palosaari 		ret = af9033_wr_reg(state, 0x80004c, 1);
104412897dc3SAntti Palosaari 		if (ret < 0)
104512897dc3SAntti Palosaari 			goto err;
104612897dc3SAntti Palosaari 
104712897dc3SAntti Palosaari 		ret = af9033_wr_reg(state, 0x800000, 0);
104812897dc3SAntti Palosaari 		if (ret < 0)
104912897dc3SAntti Palosaari 			goto err;
10504902bb39SAntti Palosaari 	}
105112897dc3SAntti Palosaari 
10529a0bf528SMauro Carvalho Chehab 	/* configure internal TS mode */
10539a0bf528SMauro Carvalho Chehab 	switch (state->cfg.ts_mode) {
10549a0bf528SMauro Carvalho Chehab 	case AF9033_TS_MODE_PARALLEL:
10559a0bf528SMauro Carvalho Chehab 		state->ts_mode_parallel = true;
10569a0bf528SMauro Carvalho Chehab 		break;
10579a0bf528SMauro Carvalho Chehab 	case AF9033_TS_MODE_SERIAL:
10589a0bf528SMauro Carvalho Chehab 		state->ts_mode_serial = true;
10599a0bf528SMauro Carvalho Chehab 		break;
10609a0bf528SMauro Carvalho Chehab 	case AF9033_TS_MODE_USB:
10619a0bf528SMauro Carvalho Chehab 		/* usb mode for AF9035 */
10629a0bf528SMauro Carvalho Chehab 	default:
10639a0bf528SMauro Carvalho Chehab 		break;
10649a0bf528SMauro Carvalho Chehab 	}
10659a0bf528SMauro Carvalho Chehab 
10669a0bf528SMauro Carvalho Chehab 	/* create dvb_frontend */
10679a0bf528SMauro Carvalho Chehab 	memcpy(&state->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
10689a0bf528SMauro Carvalho Chehab 	state->fe.demodulator_priv = state;
10699a0bf528SMauro Carvalho Chehab 
10709a0bf528SMauro Carvalho Chehab 	return &state->fe;
10719a0bf528SMauro Carvalho Chehab 
10729a0bf528SMauro Carvalho Chehab err:
10739a0bf528SMauro Carvalho Chehab 	kfree(state);
10749a0bf528SMauro Carvalho Chehab 	return NULL;
10759a0bf528SMauro Carvalho Chehab }
10769a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(af9033_attach);
10779a0bf528SMauro Carvalho Chehab 
10789a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops af9033_ops = {
10799a0bf528SMauro Carvalho Chehab 	.delsys = { SYS_DVBT },
10809a0bf528SMauro Carvalho Chehab 	.info = {
10819a0bf528SMauro Carvalho Chehab 		.name = "Afatech AF9033 (DVB-T)",
10829a0bf528SMauro Carvalho Chehab 		.frequency_min = 174000000,
10839a0bf528SMauro Carvalho Chehab 		.frequency_max = 862000000,
10849a0bf528SMauro Carvalho Chehab 		.frequency_stepsize = 250000,
10859a0bf528SMauro Carvalho Chehab 		.frequency_tolerance = 0,
10869a0bf528SMauro Carvalho Chehab 		.caps =	FE_CAN_FEC_1_2 |
10879a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_2_3 |
10889a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_3_4 |
10899a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_5_6 |
10909a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_7_8 |
10919a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_AUTO |
10929a0bf528SMauro Carvalho Chehab 			FE_CAN_QPSK |
10939a0bf528SMauro Carvalho Chehab 			FE_CAN_QAM_16 |
10949a0bf528SMauro Carvalho Chehab 			FE_CAN_QAM_64 |
10959a0bf528SMauro Carvalho Chehab 			FE_CAN_QAM_AUTO |
10969a0bf528SMauro Carvalho Chehab 			FE_CAN_TRANSMISSION_MODE_AUTO |
10979a0bf528SMauro Carvalho Chehab 			FE_CAN_GUARD_INTERVAL_AUTO |
10989a0bf528SMauro Carvalho Chehab 			FE_CAN_HIERARCHY_AUTO |
10999a0bf528SMauro Carvalho Chehab 			FE_CAN_RECOVER |
11009a0bf528SMauro Carvalho Chehab 			FE_CAN_MUTE_TS
11019a0bf528SMauro Carvalho Chehab 	},
11029a0bf528SMauro Carvalho Chehab 
11039a0bf528SMauro Carvalho Chehab 	.release = af9033_release,
11049a0bf528SMauro Carvalho Chehab 
11059a0bf528SMauro Carvalho Chehab 	.init = af9033_init,
11069a0bf528SMauro Carvalho Chehab 	.sleep = af9033_sleep,
11079a0bf528SMauro Carvalho Chehab 
11089a0bf528SMauro Carvalho Chehab 	.get_tune_settings = af9033_get_tune_settings,
11099a0bf528SMauro Carvalho Chehab 	.set_frontend = af9033_set_frontend,
11109a0bf528SMauro Carvalho Chehab 	.get_frontend = af9033_get_frontend,
11119a0bf528SMauro Carvalho Chehab 
11129a0bf528SMauro Carvalho Chehab 	.read_status = af9033_read_status,
11139a0bf528SMauro Carvalho Chehab 	.read_snr = af9033_read_snr,
11149a0bf528SMauro Carvalho Chehab 	.read_signal_strength = af9033_read_signal_strength,
11159a0bf528SMauro Carvalho Chehab 	.read_ber = af9033_read_ber,
11169a0bf528SMauro Carvalho Chehab 	.read_ucblocks = af9033_read_ucblocks,
11179a0bf528SMauro Carvalho Chehab 
11189a0bf528SMauro Carvalho Chehab 	.i2c_gate_ctrl = af9033_i2c_gate_ctrl,
11199a0bf528SMauro Carvalho Chehab };
11209a0bf528SMauro Carvalho Chehab 
11219a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
11229a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
11239a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
1124