xref: /linux/drivers/media/dvb-frontends/cxd2820r_t.c (revision 512eb720c9892261d68f86ba880a55cdbb74710d)
19a0bf528SMauro Carvalho Chehab /*
29a0bf528SMauro Carvalho Chehab  * Sony CXD2820R demodulator driver
39a0bf528SMauro Carvalho Chehab  *
49a0bf528SMauro Carvalho Chehab  * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
59a0bf528SMauro Carvalho Chehab  *
69a0bf528SMauro Carvalho Chehab  *    This program is free software; you can redistribute it and/or modify
79a0bf528SMauro Carvalho Chehab  *    it under the terms of the GNU General Public License as published by
89a0bf528SMauro Carvalho Chehab  *    the Free Software Foundation; either version 2 of the License, or
99a0bf528SMauro Carvalho Chehab  *    (at your option) any later version.
109a0bf528SMauro Carvalho Chehab  *
119a0bf528SMauro Carvalho Chehab  *    This program is distributed in the hope that it will be useful,
129a0bf528SMauro Carvalho Chehab  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
139a0bf528SMauro Carvalho Chehab  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
149a0bf528SMauro Carvalho Chehab  *    GNU General Public License for more details.
159a0bf528SMauro Carvalho Chehab  *
169a0bf528SMauro Carvalho Chehab  *    You should have received a copy of the GNU General Public License along
179a0bf528SMauro Carvalho Chehab  *    with this program; if not, write to the Free Software Foundation, Inc.,
189a0bf528SMauro Carvalho Chehab  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
199a0bf528SMauro Carvalho Chehab  */
209a0bf528SMauro Carvalho Chehab 
219a0bf528SMauro Carvalho Chehab 
229a0bf528SMauro Carvalho Chehab #include "cxd2820r_priv.h"
239a0bf528SMauro Carvalho Chehab 
249a0bf528SMauro Carvalho Chehab int cxd2820r_set_frontend_t(struct dvb_frontend *fe)
259a0bf528SMauro Carvalho Chehab {
269a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
279a0bf528SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
289a0bf528SMauro Carvalho Chehab 	int ret, i, bw_i;
299a0bf528SMauro Carvalho Chehab 	u32 if_freq, if_ctl;
309a0bf528SMauro Carvalho Chehab 	u64 num;
319a0bf528SMauro Carvalho Chehab 	u8 buf[3], bw_param;
329a0bf528SMauro Carvalho Chehab 	u8 bw_params1[][5] = {
339a0bf528SMauro Carvalho Chehab 		{ 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
349a0bf528SMauro Carvalho Chehab 		{ 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
359a0bf528SMauro Carvalho Chehab 		{ 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
369a0bf528SMauro Carvalho Chehab 	};
379a0bf528SMauro Carvalho Chehab 	u8 bw_params2[][2] = {
389a0bf528SMauro Carvalho Chehab 		{ 0x1f, 0xdc }, /* 6 MHz */
399a0bf528SMauro Carvalho Chehab 		{ 0x12, 0xf8 }, /* 7 MHz */
409a0bf528SMauro Carvalho Chehab 		{ 0x01, 0xe0 }, /* 8 MHz */
419a0bf528SMauro Carvalho Chehab 	};
429a0bf528SMauro Carvalho Chehab 	struct reg_val_mask tab[] = {
439a0bf528SMauro Carvalho Chehab 		{ 0x00080, 0x00, 0xff },
449a0bf528SMauro Carvalho Chehab 		{ 0x00081, 0x03, 0xff },
459a0bf528SMauro Carvalho Chehab 		{ 0x00085, 0x07, 0xff },
469a0bf528SMauro Carvalho Chehab 		{ 0x00088, 0x01, 0xff },
479a0bf528SMauro Carvalho Chehab 
489a0bf528SMauro Carvalho Chehab 		{ 0x00070, priv->cfg.ts_mode, 0xff },
494d1ab185SCrazyCat 		{ 0x00071, !priv->cfg.ts_clock_inv << 4, 0x10 },
509a0bf528SMauro Carvalho Chehab 		{ 0x000cb, priv->cfg.if_agc_polarity << 6, 0x40 },
519a0bf528SMauro Carvalho Chehab 		{ 0x000a5, 0x00, 0x01 },
529a0bf528SMauro Carvalho Chehab 		{ 0x00082, 0x20, 0x60 },
539a0bf528SMauro Carvalho Chehab 		{ 0x000c2, 0xc3, 0xff },
549a0bf528SMauro Carvalho Chehab 		{ 0x0016a, 0x50, 0xff },
559a0bf528SMauro Carvalho Chehab 		{ 0x00427, 0x41, 0xff },
569a0bf528SMauro Carvalho Chehab 	};
579a0bf528SMauro Carvalho Chehab 
5875aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n", __func__,
5975aeafc9SAntti Palosaari 			c->frequency, c->bandwidth_hz);
609a0bf528SMauro Carvalho Chehab 
619a0bf528SMauro Carvalho Chehab 	switch (c->bandwidth_hz) {
629a0bf528SMauro Carvalho Chehab 	case 6000000:
639a0bf528SMauro Carvalho Chehab 		bw_i = 0;
649a0bf528SMauro Carvalho Chehab 		bw_param = 2;
659a0bf528SMauro Carvalho Chehab 		break;
669a0bf528SMauro Carvalho Chehab 	case 7000000:
679a0bf528SMauro Carvalho Chehab 		bw_i = 1;
689a0bf528SMauro Carvalho Chehab 		bw_param = 1;
699a0bf528SMauro Carvalho Chehab 		break;
709a0bf528SMauro Carvalho Chehab 	case 8000000:
719a0bf528SMauro Carvalho Chehab 		bw_i = 2;
729a0bf528SMauro Carvalho Chehab 		bw_param = 0;
739a0bf528SMauro Carvalho Chehab 		break;
749a0bf528SMauro Carvalho Chehab 	default:
759a0bf528SMauro Carvalho Chehab 		return -EINVAL;
769a0bf528SMauro Carvalho Chehab 	}
779a0bf528SMauro Carvalho Chehab 
789a0bf528SMauro Carvalho Chehab 	/* program tuner */
799a0bf528SMauro Carvalho Chehab 	if (fe->ops.tuner_ops.set_params)
809a0bf528SMauro Carvalho Chehab 		fe->ops.tuner_ops.set_params(fe);
819a0bf528SMauro Carvalho Chehab 
829a0bf528SMauro Carvalho Chehab 	if (priv->delivery_system != SYS_DVBT) {
839a0bf528SMauro Carvalho Chehab 		for (i = 0; i < ARRAY_SIZE(tab); i++) {
849a0bf528SMauro Carvalho Chehab 			ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
859a0bf528SMauro Carvalho Chehab 				tab[i].val, tab[i].mask);
869a0bf528SMauro Carvalho Chehab 			if (ret)
879a0bf528SMauro Carvalho Chehab 				goto error;
889a0bf528SMauro Carvalho Chehab 		}
899a0bf528SMauro Carvalho Chehab 	}
909a0bf528SMauro Carvalho Chehab 
919a0bf528SMauro Carvalho Chehab 	priv->delivery_system = SYS_DVBT;
92285c0b00SMauro Carvalho Chehab 	priv->ber_running = false; /* tune stops BER counter */
939a0bf528SMauro Carvalho Chehab 
949a0bf528SMauro Carvalho Chehab 	/* program IF frequency */
959a0bf528SMauro Carvalho Chehab 	if (fe->ops.tuner_ops.get_if_frequency) {
969a0bf528SMauro Carvalho Chehab 		ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
979a0bf528SMauro Carvalho Chehab 		if (ret)
989a0bf528SMauro Carvalho Chehab 			goto error;
999a0bf528SMauro Carvalho Chehab 	} else
1009a0bf528SMauro Carvalho Chehab 		if_freq = 0;
1019a0bf528SMauro Carvalho Chehab 
10275aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: if_freq=%d\n", __func__, if_freq);
1039a0bf528SMauro Carvalho Chehab 
1049a0bf528SMauro Carvalho Chehab 	num = if_freq / 1000; /* Hz => kHz */
1059a0bf528SMauro Carvalho Chehab 	num *= 0x1000000;
106*512eb720SJavi Merino 	if_ctl = DIV_ROUND_CLOSEST_ULL(num, 41000);
1079a0bf528SMauro Carvalho Chehab 	buf[0] = ((if_ctl >> 16) & 0xff);
1089a0bf528SMauro Carvalho Chehab 	buf[1] = ((if_ctl >>  8) & 0xff);
1099a0bf528SMauro Carvalho Chehab 	buf[2] = ((if_ctl >>  0) & 0xff);
1109a0bf528SMauro Carvalho Chehab 
1119a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_wr_regs(priv, 0x000b6, buf, 3);
1129a0bf528SMauro Carvalho Chehab 	if (ret)
1139a0bf528SMauro Carvalho Chehab 		goto error;
1149a0bf528SMauro Carvalho Chehab 
1159a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_wr_regs(priv, 0x0009f, bw_params1[bw_i], 5);
1169a0bf528SMauro Carvalho Chehab 	if (ret)
1179a0bf528SMauro Carvalho Chehab 		goto error;
1189a0bf528SMauro Carvalho Chehab 
1199a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_wr_reg_mask(priv, 0x000d7, bw_param << 6, 0xc0);
1209a0bf528SMauro Carvalho Chehab 	if (ret)
1219a0bf528SMauro Carvalho Chehab 		goto error;
1229a0bf528SMauro Carvalho Chehab 
1239a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_wr_regs(priv, 0x000d9, bw_params2[bw_i], 2);
1249a0bf528SMauro Carvalho Chehab 	if (ret)
1259a0bf528SMauro Carvalho Chehab 		goto error;
1269a0bf528SMauro Carvalho Chehab 
1279a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
1289a0bf528SMauro Carvalho Chehab 	if (ret)
1299a0bf528SMauro Carvalho Chehab 		goto error;
1309a0bf528SMauro Carvalho Chehab 
1319a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
1329a0bf528SMauro Carvalho Chehab 	if (ret)
1339a0bf528SMauro Carvalho Chehab 		goto error;
1349a0bf528SMauro Carvalho Chehab 
1359a0bf528SMauro Carvalho Chehab 	return ret;
1369a0bf528SMauro Carvalho Chehab error:
13775aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
1389a0bf528SMauro Carvalho Chehab 	return ret;
1399a0bf528SMauro Carvalho Chehab }
1409a0bf528SMauro Carvalho Chehab 
1419a0bf528SMauro Carvalho Chehab int cxd2820r_get_frontend_t(struct dvb_frontend *fe)
1429a0bf528SMauro Carvalho Chehab {
1439a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
1449a0bf528SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1459a0bf528SMauro Carvalho Chehab 	int ret;
1469a0bf528SMauro Carvalho Chehab 	u8 buf[2];
1479a0bf528SMauro Carvalho Chehab 
1489a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_rd_regs(priv, 0x0002f, buf, sizeof(buf));
1499a0bf528SMauro Carvalho Chehab 	if (ret)
1509a0bf528SMauro Carvalho Chehab 		goto error;
1519a0bf528SMauro Carvalho Chehab 
1529a0bf528SMauro Carvalho Chehab 	switch ((buf[0] >> 6) & 0x03) {
1539a0bf528SMauro Carvalho Chehab 	case 0:
1549a0bf528SMauro Carvalho Chehab 		c->modulation = QPSK;
1559a0bf528SMauro Carvalho Chehab 		break;
1569a0bf528SMauro Carvalho Chehab 	case 1:
1579a0bf528SMauro Carvalho Chehab 		c->modulation = QAM_16;
1589a0bf528SMauro Carvalho Chehab 		break;
1599a0bf528SMauro Carvalho Chehab 	case 2:
1609a0bf528SMauro Carvalho Chehab 		c->modulation = QAM_64;
1619a0bf528SMauro Carvalho Chehab 		break;
1629a0bf528SMauro Carvalho Chehab 	}
1639a0bf528SMauro Carvalho Chehab 
1649a0bf528SMauro Carvalho Chehab 	switch ((buf[1] >> 1) & 0x03) {
1659a0bf528SMauro Carvalho Chehab 	case 0:
1669a0bf528SMauro Carvalho Chehab 		c->transmission_mode = TRANSMISSION_MODE_2K;
1679a0bf528SMauro Carvalho Chehab 		break;
1689a0bf528SMauro Carvalho Chehab 	case 1:
1699a0bf528SMauro Carvalho Chehab 		c->transmission_mode = TRANSMISSION_MODE_8K;
1709a0bf528SMauro Carvalho Chehab 		break;
1719a0bf528SMauro Carvalho Chehab 	}
1729a0bf528SMauro Carvalho Chehab 
1739a0bf528SMauro Carvalho Chehab 	switch ((buf[1] >> 3) & 0x03) {
1749a0bf528SMauro Carvalho Chehab 	case 0:
1759a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_32;
1769a0bf528SMauro Carvalho Chehab 		break;
1779a0bf528SMauro Carvalho Chehab 	case 1:
1789a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_16;
1799a0bf528SMauro Carvalho Chehab 		break;
1809a0bf528SMauro Carvalho Chehab 	case 2:
1819a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_8;
1829a0bf528SMauro Carvalho Chehab 		break;
1839a0bf528SMauro Carvalho Chehab 	case 3:
1849a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_4;
1859a0bf528SMauro Carvalho Chehab 		break;
1869a0bf528SMauro Carvalho Chehab 	}
1879a0bf528SMauro Carvalho Chehab 
1889a0bf528SMauro Carvalho Chehab 	switch ((buf[0] >> 3) & 0x07) {
1899a0bf528SMauro Carvalho Chehab 	case 0:
1909a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_NONE;
1919a0bf528SMauro Carvalho Chehab 		break;
1929a0bf528SMauro Carvalho Chehab 	case 1:
1939a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_1;
1949a0bf528SMauro Carvalho Chehab 		break;
1959a0bf528SMauro Carvalho Chehab 	case 2:
1969a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_2;
1979a0bf528SMauro Carvalho Chehab 		break;
1989a0bf528SMauro Carvalho Chehab 	case 3:
1999a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_4;
2009a0bf528SMauro Carvalho Chehab 		break;
2019a0bf528SMauro Carvalho Chehab 	}
2029a0bf528SMauro Carvalho Chehab 
2039a0bf528SMauro Carvalho Chehab 	switch ((buf[0] >> 0) & 0x07) {
2049a0bf528SMauro Carvalho Chehab 	case 0:
2059a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_1_2;
2069a0bf528SMauro Carvalho Chehab 		break;
2079a0bf528SMauro Carvalho Chehab 	case 1:
2089a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_2_3;
2099a0bf528SMauro Carvalho Chehab 		break;
2109a0bf528SMauro Carvalho Chehab 	case 2:
2119a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_3_4;
2129a0bf528SMauro Carvalho Chehab 		break;
2139a0bf528SMauro Carvalho Chehab 	case 3:
2149a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_5_6;
2159a0bf528SMauro Carvalho Chehab 		break;
2169a0bf528SMauro Carvalho Chehab 	case 4:
2179a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_7_8;
2189a0bf528SMauro Carvalho Chehab 		break;
2199a0bf528SMauro Carvalho Chehab 	}
2209a0bf528SMauro Carvalho Chehab 
2219a0bf528SMauro Carvalho Chehab 	switch ((buf[1] >> 5) & 0x07) {
2229a0bf528SMauro Carvalho Chehab 	case 0:
2239a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_1_2;
2249a0bf528SMauro Carvalho Chehab 		break;
2259a0bf528SMauro Carvalho Chehab 	case 1:
2269a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_2_3;
2279a0bf528SMauro Carvalho Chehab 		break;
2289a0bf528SMauro Carvalho Chehab 	case 2:
2299a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_3_4;
2309a0bf528SMauro Carvalho Chehab 		break;
2319a0bf528SMauro Carvalho Chehab 	case 3:
2329a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_5_6;
2339a0bf528SMauro Carvalho Chehab 		break;
2349a0bf528SMauro Carvalho Chehab 	case 4:
2359a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_7_8;
2369a0bf528SMauro Carvalho Chehab 		break;
2379a0bf528SMauro Carvalho Chehab 	}
2389a0bf528SMauro Carvalho Chehab 
2399a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_rd_reg(priv, 0x007c6, &buf[0]);
2409a0bf528SMauro Carvalho Chehab 	if (ret)
2419a0bf528SMauro Carvalho Chehab 		goto error;
2429a0bf528SMauro Carvalho Chehab 
2439a0bf528SMauro Carvalho Chehab 	switch ((buf[0] >> 0) & 0x01) {
2449a0bf528SMauro Carvalho Chehab 	case 0:
2459a0bf528SMauro Carvalho Chehab 		c->inversion = INVERSION_OFF;
2469a0bf528SMauro Carvalho Chehab 		break;
2479a0bf528SMauro Carvalho Chehab 	case 1:
2489a0bf528SMauro Carvalho Chehab 		c->inversion = INVERSION_ON;
2499a0bf528SMauro Carvalho Chehab 		break;
2509a0bf528SMauro Carvalho Chehab 	}
2519a0bf528SMauro Carvalho Chehab 
2529a0bf528SMauro Carvalho Chehab 	return ret;
2539a0bf528SMauro Carvalho Chehab error:
25475aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
2559a0bf528SMauro Carvalho Chehab 	return ret;
2569a0bf528SMauro Carvalho Chehab }
2579a0bf528SMauro Carvalho Chehab 
2589a0bf528SMauro Carvalho Chehab int cxd2820r_read_ber_t(struct dvb_frontend *fe, u32 *ber)
2599a0bf528SMauro Carvalho Chehab {
2609a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
2619a0bf528SMauro Carvalho Chehab 	int ret;
2629a0bf528SMauro Carvalho Chehab 	u8 buf[3], start_ber = 0;
2639a0bf528SMauro Carvalho Chehab 	*ber = 0;
2649a0bf528SMauro Carvalho Chehab 
2659a0bf528SMauro Carvalho Chehab 	if (priv->ber_running) {
2669a0bf528SMauro Carvalho Chehab 		ret = cxd2820r_rd_regs(priv, 0x00076, buf, sizeof(buf));
2679a0bf528SMauro Carvalho Chehab 		if (ret)
2689a0bf528SMauro Carvalho Chehab 			goto error;
2699a0bf528SMauro Carvalho Chehab 
2709a0bf528SMauro Carvalho Chehab 		if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) {
2719a0bf528SMauro Carvalho Chehab 			*ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0];
2729a0bf528SMauro Carvalho Chehab 			start_ber = 1;
2739a0bf528SMauro Carvalho Chehab 		}
2749a0bf528SMauro Carvalho Chehab 	} else {
275285c0b00SMauro Carvalho Chehab 		priv->ber_running = true;
2769a0bf528SMauro Carvalho Chehab 		start_ber = 1;
2779a0bf528SMauro Carvalho Chehab 	}
2789a0bf528SMauro Carvalho Chehab 
2799a0bf528SMauro Carvalho Chehab 	if (start_ber) {
2809a0bf528SMauro Carvalho Chehab 		/* (re)start BER */
2819a0bf528SMauro Carvalho Chehab 		ret = cxd2820r_wr_reg(priv, 0x00079, 0x01);
2829a0bf528SMauro Carvalho Chehab 		if (ret)
2839a0bf528SMauro Carvalho Chehab 			goto error;
2849a0bf528SMauro Carvalho Chehab 	}
2859a0bf528SMauro Carvalho Chehab 
2869a0bf528SMauro Carvalho Chehab 	return ret;
2879a0bf528SMauro Carvalho Chehab error:
28875aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
2899a0bf528SMauro Carvalho Chehab 	return ret;
2909a0bf528SMauro Carvalho Chehab }
2919a0bf528SMauro Carvalho Chehab 
2929a0bf528SMauro Carvalho Chehab int cxd2820r_read_signal_strength_t(struct dvb_frontend *fe,
2939a0bf528SMauro Carvalho Chehab 	u16 *strength)
2949a0bf528SMauro Carvalho Chehab {
2959a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
2969a0bf528SMauro Carvalho Chehab 	int ret;
2979a0bf528SMauro Carvalho Chehab 	u8 buf[2];
2989a0bf528SMauro Carvalho Chehab 	u16 tmp;
2999a0bf528SMauro Carvalho Chehab 
3009a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_rd_regs(priv, 0x00026, buf, sizeof(buf));
3019a0bf528SMauro Carvalho Chehab 	if (ret)
3029a0bf528SMauro Carvalho Chehab 		goto error;
3039a0bf528SMauro Carvalho Chehab 
3049a0bf528SMauro Carvalho Chehab 	tmp = (buf[0] & 0x0f) << 8 | buf[1];
3059a0bf528SMauro Carvalho Chehab 	tmp = ~tmp & 0x0fff;
3069a0bf528SMauro Carvalho Chehab 
3079a0bf528SMauro Carvalho Chehab 	/* scale value to 0x0000-0xffff from 0x0000-0x0fff */
3089a0bf528SMauro Carvalho Chehab 	*strength = tmp * 0xffff / 0x0fff;
3099a0bf528SMauro Carvalho Chehab 
3109a0bf528SMauro Carvalho Chehab 	return ret;
3119a0bf528SMauro Carvalho Chehab error:
31275aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
3139a0bf528SMauro Carvalho Chehab 	return ret;
3149a0bf528SMauro Carvalho Chehab }
3159a0bf528SMauro Carvalho Chehab 
3169a0bf528SMauro Carvalho Chehab int cxd2820r_read_snr_t(struct dvb_frontend *fe, u16 *snr)
3179a0bf528SMauro Carvalho Chehab {
3189a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
3199a0bf528SMauro Carvalho Chehab 	int ret;
3209a0bf528SMauro Carvalho Chehab 	u8 buf[2];
3219a0bf528SMauro Carvalho Chehab 	u16 tmp;
3229a0bf528SMauro Carvalho Chehab 	/* report SNR in dB * 10 */
3239a0bf528SMauro Carvalho Chehab 
3249a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_rd_regs(priv, 0x00028, buf, sizeof(buf));
3259a0bf528SMauro Carvalho Chehab 	if (ret)
3269a0bf528SMauro Carvalho Chehab 		goto error;
3279a0bf528SMauro Carvalho Chehab 
3289a0bf528SMauro Carvalho Chehab 	tmp = (buf[0] & 0x1f) << 8 | buf[1];
3299a0bf528SMauro Carvalho Chehab 	#define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */
3309a0bf528SMauro Carvalho Chehab 	if (tmp)
3319a0bf528SMauro Carvalho Chehab 		*snr = (intlog10(tmp) - CXD2820R_LOG10_8_24) / ((1 << 24)
3329a0bf528SMauro Carvalho Chehab 			/ 100);
3339a0bf528SMauro Carvalho Chehab 	else
3349a0bf528SMauro Carvalho Chehab 		*snr = 0;
3359a0bf528SMauro Carvalho Chehab 
33675aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: dBx10=%d val=%04x\n", __func__, *snr,
33775aeafc9SAntti Palosaari 			tmp);
3389a0bf528SMauro Carvalho Chehab 
3399a0bf528SMauro Carvalho Chehab 	return ret;
3409a0bf528SMauro Carvalho Chehab error:
34175aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
3429a0bf528SMauro Carvalho Chehab 	return ret;
3439a0bf528SMauro Carvalho Chehab }
3449a0bf528SMauro Carvalho Chehab 
3459a0bf528SMauro Carvalho Chehab int cxd2820r_read_ucblocks_t(struct dvb_frontend *fe, u32 *ucblocks)
3469a0bf528SMauro Carvalho Chehab {
3479a0bf528SMauro Carvalho Chehab 	*ucblocks = 0;
3489a0bf528SMauro Carvalho Chehab 	/* no way to read ? */
3499a0bf528SMauro Carvalho Chehab 	return 0;
3509a0bf528SMauro Carvalho Chehab }
3519a0bf528SMauro Carvalho Chehab 
3529a0bf528SMauro Carvalho Chehab int cxd2820r_read_status_t(struct dvb_frontend *fe, fe_status_t *status)
3539a0bf528SMauro Carvalho Chehab {
3549a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
3559a0bf528SMauro Carvalho Chehab 	int ret;
3569a0bf528SMauro Carvalho Chehab 	u8 buf[4];
3579a0bf528SMauro Carvalho Chehab 	*status = 0;
3589a0bf528SMauro Carvalho Chehab 
3599a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_rd_reg(priv, 0x00010, &buf[0]);
3609a0bf528SMauro Carvalho Chehab 	if (ret)
3619a0bf528SMauro Carvalho Chehab 		goto error;
3629a0bf528SMauro Carvalho Chehab 
3639a0bf528SMauro Carvalho Chehab 	if ((buf[0] & 0x07) == 6) {
3649a0bf528SMauro Carvalho Chehab 		ret = cxd2820r_rd_reg(priv, 0x00073, &buf[1]);
3659a0bf528SMauro Carvalho Chehab 		if (ret)
3669a0bf528SMauro Carvalho Chehab 			goto error;
3679a0bf528SMauro Carvalho Chehab 
3689a0bf528SMauro Carvalho Chehab 		if (((buf[1] >> 3) & 0x01) == 1) {
3699a0bf528SMauro Carvalho Chehab 			*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
3709a0bf528SMauro Carvalho Chehab 				FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
3719a0bf528SMauro Carvalho Chehab 		} else {
3729a0bf528SMauro Carvalho Chehab 			*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
3739a0bf528SMauro Carvalho Chehab 				FE_HAS_VITERBI | FE_HAS_SYNC;
3749a0bf528SMauro Carvalho Chehab 		}
3759a0bf528SMauro Carvalho Chehab 	} else {
3769a0bf528SMauro Carvalho Chehab 		ret = cxd2820r_rd_reg(priv, 0x00014, &buf[2]);
3779a0bf528SMauro Carvalho Chehab 		if (ret)
3789a0bf528SMauro Carvalho Chehab 			goto error;
3799a0bf528SMauro Carvalho Chehab 
3809a0bf528SMauro Carvalho Chehab 		if ((buf[2] & 0x0f) >= 4) {
3819a0bf528SMauro Carvalho Chehab 			ret = cxd2820r_rd_reg(priv, 0x00a14, &buf[3]);
3829a0bf528SMauro Carvalho Chehab 			if (ret)
3839a0bf528SMauro Carvalho Chehab 				goto error;
3849a0bf528SMauro Carvalho Chehab 
3859a0bf528SMauro Carvalho Chehab 			if (((buf[3] >> 4) & 0x01) == 1)
3869a0bf528SMauro Carvalho Chehab 				*status |= FE_HAS_SIGNAL;
3879a0bf528SMauro Carvalho Chehab 		}
3889a0bf528SMauro Carvalho Chehab 	}
3899a0bf528SMauro Carvalho Chehab 
39075aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: lock=%*ph\n", __func__, 4, buf);
3919a0bf528SMauro Carvalho Chehab 
3929a0bf528SMauro Carvalho Chehab 	return ret;
3939a0bf528SMauro Carvalho Chehab error:
39475aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
3959a0bf528SMauro Carvalho Chehab 	return ret;
3969a0bf528SMauro Carvalho Chehab }
3979a0bf528SMauro Carvalho Chehab 
3989a0bf528SMauro Carvalho Chehab int cxd2820r_init_t(struct dvb_frontend *fe)
3999a0bf528SMauro Carvalho Chehab {
4009a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
4019a0bf528SMauro Carvalho Chehab 	int ret;
4029a0bf528SMauro Carvalho Chehab 
4039a0bf528SMauro Carvalho Chehab 	ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
4049a0bf528SMauro Carvalho Chehab 	if (ret)
4059a0bf528SMauro Carvalho Chehab 		goto error;
4069a0bf528SMauro Carvalho Chehab 
4079a0bf528SMauro Carvalho Chehab 	return ret;
4089a0bf528SMauro Carvalho Chehab error:
40975aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
4109a0bf528SMauro Carvalho Chehab 	return ret;
4119a0bf528SMauro Carvalho Chehab }
4129a0bf528SMauro Carvalho Chehab 
4139a0bf528SMauro Carvalho Chehab int cxd2820r_sleep_t(struct dvb_frontend *fe)
4149a0bf528SMauro Carvalho Chehab {
4159a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
4169a0bf528SMauro Carvalho Chehab 	int ret, i;
4179a0bf528SMauro Carvalho Chehab 	struct reg_val_mask tab[] = {
4189a0bf528SMauro Carvalho Chehab 		{ 0x000ff, 0x1f, 0xff },
4199a0bf528SMauro Carvalho Chehab 		{ 0x00085, 0x00, 0xff },
4209a0bf528SMauro Carvalho Chehab 		{ 0x00088, 0x01, 0xff },
4219a0bf528SMauro Carvalho Chehab 		{ 0x00081, 0x00, 0xff },
4229a0bf528SMauro Carvalho Chehab 		{ 0x00080, 0x00, 0xff },
4239a0bf528SMauro Carvalho Chehab 	};
4249a0bf528SMauro Carvalho Chehab 
42575aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s\n", __func__);
4269a0bf528SMauro Carvalho Chehab 
4279a0bf528SMauro Carvalho Chehab 	priv->delivery_system = SYS_UNDEFINED;
4289a0bf528SMauro Carvalho Chehab 
4299a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
4309a0bf528SMauro Carvalho Chehab 		ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
4319a0bf528SMauro Carvalho Chehab 			tab[i].mask);
4329a0bf528SMauro Carvalho Chehab 		if (ret)
4339a0bf528SMauro Carvalho Chehab 			goto error;
4349a0bf528SMauro Carvalho Chehab 	}
4359a0bf528SMauro Carvalho Chehab 
4369a0bf528SMauro Carvalho Chehab 	return ret;
4379a0bf528SMauro Carvalho Chehab error:
43875aeafc9SAntti Palosaari 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
4399a0bf528SMauro Carvalho Chehab 	return ret;
4409a0bf528SMauro Carvalho Chehab }
4419a0bf528SMauro Carvalho Chehab 
4429a0bf528SMauro Carvalho Chehab int cxd2820r_get_tune_settings_t(struct dvb_frontend *fe,
4439a0bf528SMauro Carvalho Chehab 	struct dvb_frontend_tune_settings *s)
4449a0bf528SMauro Carvalho Chehab {
4459a0bf528SMauro Carvalho Chehab 	s->min_delay_ms = 500;
4469a0bf528SMauro Carvalho Chehab 	s->step_size = fe->ops.info.frequency_stepsize * 2;
4479a0bf528SMauro Carvalho Chehab 	s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
4489a0bf528SMauro Carvalho Chehab 
4499a0bf528SMauro Carvalho Chehab 	return 0;
4509a0bf528SMauro Carvalho Chehab }
451