xref: /linux/drivers/media/dvb-frontends/cxd2820r_t.c (revision c95baf12f5077419db01313ab61c2aac007d40cd)
116216333SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29a0bf528SMauro Carvalho Chehab /*
39a0bf528SMauro Carvalho Chehab  * Sony CXD2820R demodulator driver
49a0bf528SMauro Carvalho Chehab  *
59a0bf528SMauro Carvalho Chehab  * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
69a0bf528SMauro Carvalho Chehab  */
79a0bf528SMauro Carvalho Chehab 
89a0bf528SMauro Carvalho Chehab 
99a0bf528SMauro Carvalho Chehab #include "cxd2820r_priv.h"
109a0bf528SMauro Carvalho Chehab 
cxd2820r_set_frontend_t(struct dvb_frontend * fe)119a0bf528SMauro Carvalho Chehab int cxd2820r_set_frontend_t(struct dvb_frontend *fe)
129a0bf528SMauro Carvalho Chehab {
139a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
14c98975f9SAntti Palosaari 	struct i2c_client *client = priv->client[0];
159a0bf528SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1643e2ea63SAntti Palosaari 	int ret, bw_i;
17fcd09f65SAntti Palosaari 	unsigned int utmp;
18fcd09f65SAntti Palosaari 	u32 if_frequency;
199a0bf528SMauro Carvalho Chehab 	u8 buf[3], bw_param;
209a0bf528SMauro Carvalho Chehab 	u8 bw_params1[][5] = {
219a0bf528SMauro Carvalho Chehab 		{ 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
229a0bf528SMauro Carvalho Chehab 		{ 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
239a0bf528SMauro Carvalho Chehab 		{ 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
249a0bf528SMauro Carvalho Chehab 	};
259a0bf528SMauro Carvalho Chehab 	u8 bw_params2[][2] = {
269a0bf528SMauro Carvalho Chehab 		{ 0x1f, 0xdc }, /* 6 MHz */
279a0bf528SMauro Carvalho Chehab 		{ 0x12, 0xf8 }, /* 7 MHz */
289a0bf528SMauro Carvalho Chehab 		{ 0x01, 0xe0 }, /* 8 MHz */
299a0bf528SMauro Carvalho Chehab 	};
309a0bf528SMauro Carvalho Chehab 	struct reg_val_mask tab[] = {
319a0bf528SMauro Carvalho Chehab 		{ 0x00080, 0x00, 0xff },
329a0bf528SMauro Carvalho Chehab 		{ 0x00081, 0x03, 0xff },
339a0bf528SMauro Carvalho Chehab 		{ 0x00085, 0x07, 0xff },
349a0bf528SMauro Carvalho Chehab 		{ 0x00088, 0x01, 0xff },
359a0bf528SMauro Carvalho Chehab 
3607fdf7d9SAntti Palosaari 		{ 0x00070, priv->ts_mode, 0xff },
3707fdf7d9SAntti Palosaari 		{ 0x00071, !priv->ts_clk_inv << 4, 0x10 },
3807fdf7d9SAntti Palosaari 		{ 0x000cb, priv->if_agc_polarity << 6, 0x40 },
399a0bf528SMauro Carvalho Chehab 		{ 0x000a5, 0x00, 0x01 },
409a0bf528SMauro Carvalho Chehab 		{ 0x00082, 0x20, 0x60 },
419a0bf528SMauro Carvalho Chehab 		{ 0x000c2, 0xc3, 0xff },
429a0bf528SMauro Carvalho Chehab 		{ 0x0016a, 0x50, 0xff },
439a0bf528SMauro Carvalho Chehab 		{ 0x00427, 0x41, 0xff },
449a0bf528SMauro Carvalho Chehab 	};
459a0bf528SMauro Carvalho Chehab 
46c98975f9SAntti Palosaari 	dev_dbg(&client->dev,
47c98975f9SAntti Palosaari 		"delivery_system=%d modulation=%d frequency=%u bandwidth_hz=%u inversion=%d\n",
48c98975f9SAntti Palosaari 		c->delivery_system, c->modulation, c->frequency,
49c98975f9SAntti Palosaari 		c->bandwidth_hz, c->inversion);
509a0bf528SMauro Carvalho Chehab 
519a0bf528SMauro Carvalho Chehab 	switch (c->bandwidth_hz) {
529a0bf528SMauro Carvalho Chehab 	case 6000000:
539a0bf528SMauro Carvalho Chehab 		bw_i = 0;
549a0bf528SMauro Carvalho Chehab 		bw_param = 2;
559a0bf528SMauro Carvalho Chehab 		break;
569a0bf528SMauro Carvalho Chehab 	case 7000000:
579a0bf528SMauro Carvalho Chehab 		bw_i = 1;
589a0bf528SMauro Carvalho Chehab 		bw_param = 1;
599a0bf528SMauro Carvalho Chehab 		break;
609a0bf528SMauro Carvalho Chehab 	case 8000000:
619a0bf528SMauro Carvalho Chehab 		bw_i = 2;
629a0bf528SMauro Carvalho Chehab 		bw_param = 0;
639a0bf528SMauro Carvalho Chehab 		break;
649a0bf528SMauro Carvalho Chehab 	default:
659a0bf528SMauro Carvalho Chehab 		return -EINVAL;
669a0bf528SMauro Carvalho Chehab 	}
679a0bf528SMauro Carvalho Chehab 
689a0bf528SMauro Carvalho Chehab 	/* program tuner */
699a0bf528SMauro Carvalho Chehab 	if (fe->ops.tuner_ops.set_params)
709a0bf528SMauro Carvalho Chehab 		fe->ops.tuner_ops.set_params(fe);
719a0bf528SMauro Carvalho Chehab 
729a0bf528SMauro Carvalho Chehab 	if (priv->delivery_system != SYS_DVBT) {
7343e2ea63SAntti Palosaari 		ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
749a0bf528SMauro Carvalho Chehab 		if (ret)
759a0bf528SMauro Carvalho Chehab 			goto error;
769a0bf528SMauro Carvalho Chehab 	}
779a0bf528SMauro Carvalho Chehab 
789a0bf528SMauro Carvalho Chehab 	priv->delivery_system = SYS_DVBT;
79285c0b00SMauro Carvalho Chehab 	priv->ber_running = false; /* tune stops BER counter */
809a0bf528SMauro Carvalho Chehab 
819a0bf528SMauro Carvalho Chehab 	/* program IF frequency */
829a0bf528SMauro Carvalho Chehab 	if (fe->ops.tuner_ops.get_if_frequency) {
83fcd09f65SAntti Palosaari 		ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
849a0bf528SMauro Carvalho Chehab 		if (ret)
859a0bf528SMauro Carvalho Chehab 			goto error;
86c98975f9SAntti Palosaari 		dev_dbg(&client->dev, "if_frequency=%u\n", if_frequency);
87fcd09f65SAntti Palosaari 	} else {
88fcd09f65SAntti Palosaari 		ret = -EINVAL;
89fcd09f65SAntti Palosaari 		goto error;
90fcd09f65SAntti Palosaari 	}
919a0bf528SMauro Carvalho Chehab 
92fcd09f65SAntti Palosaari 	utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x1000000, CXD2820R_CLK);
93fcd09f65SAntti Palosaari 	buf[0] = (utmp >> 16) & 0xff;
94fcd09f65SAntti Palosaari 	buf[1] = (utmp >>  8) & 0xff;
95fcd09f65SAntti Palosaari 	buf[2] = (utmp >>  0) & 0xff;
9643e2ea63SAntti Palosaari 	ret = regmap_bulk_write(priv->regmap[0], 0x00b6, buf, 3);
979a0bf528SMauro Carvalho Chehab 	if (ret)
989a0bf528SMauro Carvalho Chehab 		goto error;
999a0bf528SMauro Carvalho Chehab 
10043e2ea63SAntti Palosaari 	ret = regmap_bulk_write(priv->regmap[0], 0x009f, bw_params1[bw_i], 5);
1019a0bf528SMauro Carvalho Chehab 	if (ret)
1029a0bf528SMauro Carvalho Chehab 		goto error;
1039a0bf528SMauro Carvalho Chehab 
10443e2ea63SAntti Palosaari 	ret = regmap_update_bits(priv->regmap[0], 0x00d7, 0xc0, bw_param << 6);
1059a0bf528SMauro Carvalho Chehab 	if (ret)
1069a0bf528SMauro Carvalho Chehab 		goto error;
1079a0bf528SMauro Carvalho Chehab 
10843e2ea63SAntti Palosaari 	ret = regmap_bulk_write(priv->regmap[0], 0x00d9, bw_params2[bw_i], 2);
1099a0bf528SMauro Carvalho Chehab 	if (ret)
1109a0bf528SMauro Carvalho Chehab 		goto error;
1119a0bf528SMauro Carvalho Chehab 
11243e2ea63SAntti Palosaari 	ret = regmap_write(priv->regmap[0], 0x00ff, 0x08);
1139a0bf528SMauro Carvalho Chehab 	if (ret)
1149a0bf528SMauro Carvalho Chehab 		goto error;
1159a0bf528SMauro Carvalho Chehab 
11643e2ea63SAntti Palosaari 	ret = regmap_write(priv->regmap[0], 0x00fe, 0x01);
1179a0bf528SMauro Carvalho Chehab 	if (ret)
1189a0bf528SMauro Carvalho Chehab 		goto error;
1199a0bf528SMauro Carvalho Chehab 
1209a0bf528SMauro Carvalho Chehab 	return ret;
1219a0bf528SMauro Carvalho Chehab error:
122c98975f9SAntti Palosaari 	dev_dbg(&client->dev, "failed=%d\n", ret);
1239a0bf528SMauro Carvalho Chehab 	return ret;
1249a0bf528SMauro Carvalho Chehab }
1259a0bf528SMauro Carvalho Chehab 
cxd2820r_get_frontend_t(struct dvb_frontend * fe,struct dtv_frontend_properties * c)1267e3e68bcSMauro Carvalho Chehab int cxd2820r_get_frontend_t(struct dvb_frontend *fe,
1277e3e68bcSMauro Carvalho Chehab 			    struct dtv_frontend_properties *c)
1289a0bf528SMauro Carvalho Chehab {
1299a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
130c98975f9SAntti Palosaari 	struct i2c_client *client = priv->client[0];
1319a0bf528SMauro Carvalho Chehab 	int ret;
13243e2ea63SAntti Palosaari 	unsigned int utmp;
1339a0bf528SMauro Carvalho Chehab 	u8 buf[2];
1349a0bf528SMauro Carvalho Chehab 
135c98975f9SAntti Palosaari 	dev_dbg(&client->dev, "\n");
136c98975f9SAntti Palosaari 
13743e2ea63SAntti Palosaari 	ret = regmap_bulk_read(priv->regmap[0], 0x002f, buf, sizeof(buf));
1389a0bf528SMauro Carvalho Chehab 	if (ret)
1399a0bf528SMauro Carvalho Chehab 		goto error;
1409a0bf528SMauro Carvalho Chehab 
1419a0bf528SMauro Carvalho Chehab 	switch ((buf[0] >> 6) & 0x03) {
1429a0bf528SMauro Carvalho Chehab 	case 0:
1439a0bf528SMauro Carvalho Chehab 		c->modulation = QPSK;
1449a0bf528SMauro Carvalho Chehab 		break;
1459a0bf528SMauro Carvalho Chehab 	case 1:
1469a0bf528SMauro Carvalho Chehab 		c->modulation = QAM_16;
1479a0bf528SMauro Carvalho Chehab 		break;
1489a0bf528SMauro Carvalho Chehab 	case 2:
1499a0bf528SMauro Carvalho Chehab 		c->modulation = QAM_64;
1509a0bf528SMauro Carvalho Chehab 		break;
1519a0bf528SMauro Carvalho Chehab 	}
1529a0bf528SMauro Carvalho Chehab 
1539a0bf528SMauro Carvalho Chehab 	switch ((buf[1] >> 1) & 0x03) {
1549a0bf528SMauro Carvalho Chehab 	case 0:
1559a0bf528SMauro Carvalho Chehab 		c->transmission_mode = TRANSMISSION_MODE_2K;
1569a0bf528SMauro Carvalho Chehab 		break;
1579a0bf528SMauro Carvalho Chehab 	case 1:
1589a0bf528SMauro Carvalho Chehab 		c->transmission_mode = TRANSMISSION_MODE_8K;
1599a0bf528SMauro Carvalho Chehab 		break;
1609a0bf528SMauro Carvalho Chehab 	}
1619a0bf528SMauro Carvalho Chehab 
1629a0bf528SMauro Carvalho Chehab 	switch ((buf[1] >> 3) & 0x03) {
1639a0bf528SMauro Carvalho Chehab 	case 0:
1649a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_32;
1659a0bf528SMauro Carvalho Chehab 		break;
1669a0bf528SMauro Carvalho Chehab 	case 1:
1679a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_16;
1689a0bf528SMauro Carvalho Chehab 		break;
1699a0bf528SMauro Carvalho Chehab 	case 2:
1709a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_8;
1719a0bf528SMauro Carvalho Chehab 		break;
1729a0bf528SMauro Carvalho Chehab 	case 3:
1739a0bf528SMauro Carvalho Chehab 		c->guard_interval = GUARD_INTERVAL_1_4;
1749a0bf528SMauro Carvalho Chehab 		break;
1759a0bf528SMauro Carvalho Chehab 	}
1769a0bf528SMauro Carvalho Chehab 
1779a0bf528SMauro Carvalho Chehab 	switch ((buf[0] >> 3) & 0x07) {
1789a0bf528SMauro Carvalho Chehab 	case 0:
1799a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_NONE;
1809a0bf528SMauro Carvalho Chehab 		break;
1819a0bf528SMauro Carvalho Chehab 	case 1:
1829a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_1;
1839a0bf528SMauro Carvalho Chehab 		break;
1849a0bf528SMauro Carvalho Chehab 	case 2:
1859a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_2;
1869a0bf528SMauro Carvalho Chehab 		break;
1879a0bf528SMauro Carvalho Chehab 	case 3:
1889a0bf528SMauro Carvalho Chehab 		c->hierarchy = HIERARCHY_4;
1899a0bf528SMauro Carvalho Chehab 		break;
1909a0bf528SMauro Carvalho Chehab 	}
1919a0bf528SMauro Carvalho Chehab 
1929a0bf528SMauro Carvalho Chehab 	switch ((buf[0] >> 0) & 0x07) {
1939a0bf528SMauro Carvalho Chehab 	case 0:
1949a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_1_2;
1959a0bf528SMauro Carvalho Chehab 		break;
1969a0bf528SMauro Carvalho Chehab 	case 1:
1979a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_2_3;
1989a0bf528SMauro Carvalho Chehab 		break;
1999a0bf528SMauro Carvalho Chehab 	case 2:
2009a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_3_4;
2019a0bf528SMauro Carvalho Chehab 		break;
2029a0bf528SMauro Carvalho Chehab 	case 3:
2039a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_5_6;
2049a0bf528SMauro Carvalho Chehab 		break;
2059a0bf528SMauro Carvalho Chehab 	case 4:
2069a0bf528SMauro Carvalho Chehab 		c->code_rate_HP = FEC_7_8;
2079a0bf528SMauro Carvalho Chehab 		break;
2089a0bf528SMauro Carvalho Chehab 	}
2099a0bf528SMauro Carvalho Chehab 
2109a0bf528SMauro Carvalho Chehab 	switch ((buf[1] >> 5) & 0x07) {
2119a0bf528SMauro Carvalho Chehab 	case 0:
2129a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_1_2;
2139a0bf528SMauro Carvalho Chehab 		break;
2149a0bf528SMauro Carvalho Chehab 	case 1:
2159a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_2_3;
2169a0bf528SMauro Carvalho Chehab 		break;
2179a0bf528SMauro Carvalho Chehab 	case 2:
2189a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_3_4;
2199a0bf528SMauro Carvalho Chehab 		break;
2209a0bf528SMauro Carvalho Chehab 	case 3:
2219a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_5_6;
2229a0bf528SMauro Carvalho Chehab 		break;
2239a0bf528SMauro Carvalho Chehab 	case 4:
2249a0bf528SMauro Carvalho Chehab 		c->code_rate_LP = FEC_7_8;
2259a0bf528SMauro Carvalho Chehab 		break;
2269a0bf528SMauro Carvalho Chehab 	}
2279a0bf528SMauro Carvalho Chehab 
22843e2ea63SAntti Palosaari 	ret = regmap_read(priv->regmap[0], 0x07c6, &utmp);
2299a0bf528SMauro Carvalho Chehab 	if (ret)
2309a0bf528SMauro Carvalho Chehab 		goto error;
2319a0bf528SMauro Carvalho Chehab 
23243e2ea63SAntti Palosaari 	switch ((utmp >> 0) & 0x01) {
2339a0bf528SMauro Carvalho Chehab 	case 0:
2349a0bf528SMauro Carvalho Chehab 		c->inversion = INVERSION_OFF;
2359a0bf528SMauro Carvalho Chehab 		break;
2369a0bf528SMauro Carvalho Chehab 	case 1:
2379a0bf528SMauro Carvalho Chehab 		c->inversion = INVERSION_ON;
2389a0bf528SMauro Carvalho Chehab 		break;
2399a0bf528SMauro Carvalho Chehab 	}
2409a0bf528SMauro Carvalho Chehab 
2419a0bf528SMauro Carvalho Chehab 	return ret;
2429a0bf528SMauro Carvalho Chehab error:
243c98975f9SAntti Palosaari 	dev_dbg(&client->dev, "failed=%d\n", ret);
2449a0bf528SMauro Carvalho Chehab 	return ret;
2459a0bf528SMauro Carvalho Chehab }
2469a0bf528SMauro Carvalho Chehab 
cxd2820r_read_status_t(struct dvb_frontend * fe,enum fe_status * status)2470df289a2SMauro Carvalho Chehab int cxd2820r_read_status_t(struct dvb_frontend *fe, enum fe_status *status)
2489a0bf528SMauro Carvalho Chehab {
2499a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
250c98975f9SAntti Palosaari 	struct i2c_client *client = priv->client[0];
2512832fd31SAntti Palosaari 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2529a0bf528SMauro Carvalho Chehab 	int ret;
253d51dc917SAntti Palosaari 	unsigned int utmp, utmp1, utmp2;
254d51dc917SAntti Palosaari 	u8 buf[3];
2559a0bf528SMauro Carvalho Chehab 
256d51dc917SAntti Palosaari 	/* Lock detection */
25743e2ea63SAntti Palosaari 	ret = regmap_bulk_read(priv->regmap[0], 0x0010, &buf[0], 1);
2589a0bf528SMauro Carvalho Chehab 	if (ret)
2599a0bf528SMauro Carvalho Chehab 		goto error;
26043e2ea63SAntti Palosaari 	ret = regmap_bulk_read(priv->regmap[0], 0x0073, &buf[1], 1);
2619a0bf528SMauro Carvalho Chehab 	if (ret)
2629a0bf528SMauro Carvalho Chehab 		goto error;
2639a0bf528SMauro Carvalho Chehab 
264d51dc917SAntti Palosaari 	utmp1 = (buf[0] >> 0) & 0x07;
265d51dc917SAntti Palosaari 	utmp2 = (buf[1] >> 3) & 0x01;
266d51dc917SAntti Palosaari 
267d51dc917SAntti Palosaari 	if (utmp1 == 6 && utmp2 == 1) {
268d51dc917SAntti Palosaari 		*status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
2699a0bf528SMauro Carvalho Chehab 			  FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
270d51dc917SAntti Palosaari 	} else if (utmp1 == 6 || utmp2 == 1) {
271d51dc917SAntti Palosaari 		*status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
2729a0bf528SMauro Carvalho Chehab 			  FE_HAS_VITERBI | FE_HAS_SYNC;
2739a0bf528SMauro Carvalho Chehab 	} else {
274d51dc917SAntti Palosaari 		*status = 0;
2759a0bf528SMauro Carvalho Chehab 	}
2769a0bf528SMauro Carvalho Chehab 
277d51dc917SAntti Palosaari 	dev_dbg(&client->dev, "status=%02x raw=%*ph sync=%u ts=%u\n",
278d51dc917SAntti Palosaari 		*status, 2, buf, utmp1, utmp2);
2799a0bf528SMauro Carvalho Chehab 
2802832fd31SAntti Palosaari 	/* Signal strength */
2812832fd31SAntti Palosaari 	if (*status & FE_HAS_SIGNAL) {
2822832fd31SAntti Palosaari 		unsigned int strength;
2832832fd31SAntti Palosaari 
28443e2ea63SAntti Palosaari 		ret = regmap_bulk_read(priv->regmap[0], 0x0026, buf, 2);
2852832fd31SAntti Palosaari 		if (ret)
2862832fd31SAntti Palosaari 			goto error;
2872832fd31SAntti Palosaari 
2882832fd31SAntti Palosaari 		utmp = buf[0] << 8 | buf[1] << 0;
2892832fd31SAntti Palosaari 		utmp = ~utmp & 0x0fff;
2902832fd31SAntti Palosaari 		/* Scale value to 0x0000-0xffff */
2912832fd31SAntti Palosaari 		strength = utmp << 4 | utmp >> 8;
2922832fd31SAntti Palosaari 
2932832fd31SAntti Palosaari 		c->strength.len = 1;
2942832fd31SAntti Palosaari 		c->strength.stat[0].scale = FE_SCALE_RELATIVE;
2952832fd31SAntti Palosaari 		c->strength.stat[0].uvalue = strength;
2962832fd31SAntti Palosaari 	} else {
2972832fd31SAntti Palosaari 		c->strength.len = 1;
2982832fd31SAntti Palosaari 		c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2992832fd31SAntti Palosaari 	}
3002832fd31SAntti Palosaari 
3012832fd31SAntti Palosaari 	/* CNR */
3022832fd31SAntti Palosaari 	if (*status & FE_HAS_VITERBI) {
3032832fd31SAntti Palosaari 		unsigned int cnr;
3042832fd31SAntti Palosaari 
30543e2ea63SAntti Palosaari 		ret = regmap_bulk_read(priv->regmap[0], 0x002c, buf, 2);
3062832fd31SAntti Palosaari 		if (ret)
3072832fd31SAntti Palosaari 			goto error;
3082832fd31SAntti Palosaari 
3092832fd31SAntti Palosaari 		utmp = buf[0] << 8 | buf[1] << 0;
3102832fd31SAntti Palosaari 		if (utmp)
3112832fd31SAntti Palosaari 			cnr = div_u64((u64)(intlog10(utmp)
3122832fd31SAntti Palosaari 				      - intlog10(32000 - utmp) + 55532585)
3132832fd31SAntti Palosaari 				      * 10000, (1 << 24));
3142832fd31SAntti Palosaari 		else
3152832fd31SAntti Palosaari 			cnr = 0;
3162832fd31SAntti Palosaari 
3172832fd31SAntti Palosaari 		c->cnr.len = 1;
3182832fd31SAntti Palosaari 		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
3192832fd31SAntti Palosaari 		c->cnr.stat[0].svalue = cnr;
3202832fd31SAntti Palosaari 	} else {
3212832fd31SAntti Palosaari 		c->cnr.len = 1;
3222832fd31SAntti Palosaari 		c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3232832fd31SAntti Palosaari 	}
3242832fd31SAntti Palosaari 
3252832fd31SAntti Palosaari 	/* BER */
3262832fd31SAntti Palosaari 	if (*status & FE_HAS_SYNC) {
3272832fd31SAntti Palosaari 		unsigned int post_bit_error;
3282832fd31SAntti Palosaari 		bool start_ber;
3292832fd31SAntti Palosaari 
3302832fd31SAntti Palosaari 		if (priv->ber_running) {
33143e2ea63SAntti Palosaari 			ret = regmap_bulk_read(priv->regmap[0], 0x0076, buf, 3);
3322832fd31SAntti Palosaari 			if (ret)
3332832fd31SAntti Palosaari 				goto error;
3342832fd31SAntti Palosaari 
3352832fd31SAntti Palosaari 			if ((buf[2] >> 7) & 0x01) {
3362832fd31SAntti Palosaari 				post_bit_error = buf[2] << 16 | buf[1] << 8 |
3372832fd31SAntti Palosaari 						 buf[0] << 0;
3382832fd31SAntti Palosaari 				post_bit_error &= 0x0fffff;
3392832fd31SAntti Palosaari 				start_ber = true;
3402832fd31SAntti Palosaari 			} else {
3412832fd31SAntti Palosaari 				post_bit_error = 0;
3422832fd31SAntti Palosaari 				start_ber = false;
3432832fd31SAntti Palosaari 			}
3442832fd31SAntti Palosaari 		} else {
3452832fd31SAntti Palosaari 			post_bit_error = 0;
3462832fd31SAntti Palosaari 			start_ber = true;
3472832fd31SAntti Palosaari 		}
3482832fd31SAntti Palosaari 
3492832fd31SAntti Palosaari 		if (start_ber) {
35043e2ea63SAntti Palosaari 			ret = regmap_write(priv->regmap[0], 0x0079, 0x01);
3512832fd31SAntti Palosaari 			if (ret)
3522832fd31SAntti Palosaari 				goto error;
3532832fd31SAntti Palosaari 			priv->ber_running = true;
3542832fd31SAntti Palosaari 		}
3552832fd31SAntti Palosaari 
3562832fd31SAntti Palosaari 		priv->post_bit_error += post_bit_error;
3572832fd31SAntti Palosaari 
3582832fd31SAntti Palosaari 		c->post_bit_error.len = 1;
3592832fd31SAntti Palosaari 		c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
3602832fd31SAntti Palosaari 		c->post_bit_error.stat[0].uvalue = priv->post_bit_error;
3612832fd31SAntti Palosaari 	} else {
3622832fd31SAntti Palosaari 		c->post_bit_error.len = 1;
3632832fd31SAntti Palosaari 		c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3642832fd31SAntti Palosaari 	}
3652832fd31SAntti Palosaari 
3669a0bf528SMauro Carvalho Chehab 	return ret;
3679a0bf528SMauro Carvalho Chehab error:
368c98975f9SAntti Palosaari 	dev_dbg(&client->dev, "failed=%d\n", ret);
3699a0bf528SMauro Carvalho Chehab 	return ret;
3709a0bf528SMauro Carvalho Chehab }
3719a0bf528SMauro Carvalho Chehab 
cxd2820r_init_t(struct dvb_frontend * fe)3729a0bf528SMauro Carvalho Chehab int cxd2820r_init_t(struct dvb_frontend *fe)
3739a0bf528SMauro Carvalho Chehab {
3749a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
375c98975f9SAntti Palosaari 	struct i2c_client *client = priv->client[0];
3769a0bf528SMauro Carvalho Chehab 	int ret;
3779a0bf528SMauro Carvalho Chehab 
378c98975f9SAntti Palosaari 	dev_dbg(&client->dev, "\n");
379c98975f9SAntti Palosaari 
38043e2ea63SAntti Palosaari 	ret = regmap_write(priv->regmap[0], 0x0085, 0x07);
3819a0bf528SMauro Carvalho Chehab 	if (ret)
3829a0bf528SMauro Carvalho Chehab 		goto error;
3839a0bf528SMauro Carvalho Chehab 
3849a0bf528SMauro Carvalho Chehab 	return ret;
3859a0bf528SMauro Carvalho Chehab error:
386c98975f9SAntti Palosaari 	dev_dbg(&client->dev, "failed=%d\n", ret);
3879a0bf528SMauro Carvalho Chehab 	return ret;
3889a0bf528SMauro Carvalho Chehab }
3899a0bf528SMauro Carvalho Chehab 
cxd2820r_sleep_t(struct dvb_frontend * fe)3909a0bf528SMauro Carvalho Chehab int cxd2820r_sleep_t(struct dvb_frontend *fe)
3919a0bf528SMauro Carvalho Chehab {
3929a0bf528SMauro Carvalho Chehab 	struct cxd2820r_priv *priv = fe->demodulator_priv;
393c98975f9SAntti Palosaari 	struct i2c_client *client = priv->client[0];
39443e2ea63SAntti Palosaari 	int ret;
395*ed4e1569SColin Ian King 	static struct reg_val_mask tab[] = {
3969a0bf528SMauro Carvalho Chehab 		{ 0x000ff, 0x1f, 0xff },
3979a0bf528SMauro Carvalho Chehab 		{ 0x00085, 0x00, 0xff },
3989a0bf528SMauro Carvalho Chehab 		{ 0x00088, 0x01, 0xff },
3999a0bf528SMauro Carvalho Chehab 		{ 0x00081, 0x00, 0xff },
4009a0bf528SMauro Carvalho Chehab 		{ 0x00080, 0x00, 0xff },
4019a0bf528SMauro Carvalho Chehab 	};
4029a0bf528SMauro Carvalho Chehab 
403c98975f9SAntti Palosaari 	dev_dbg(&client->dev, "\n");
4049a0bf528SMauro Carvalho Chehab 
4059a0bf528SMauro Carvalho Chehab 	priv->delivery_system = SYS_UNDEFINED;
4069a0bf528SMauro Carvalho Chehab 
40743e2ea63SAntti Palosaari 	ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
4089a0bf528SMauro Carvalho Chehab 	if (ret)
4099a0bf528SMauro Carvalho Chehab 		goto error;
4109a0bf528SMauro Carvalho Chehab 
4119a0bf528SMauro Carvalho Chehab 	return ret;
4129a0bf528SMauro Carvalho Chehab error:
413c98975f9SAntti Palosaari 	dev_dbg(&client->dev, "failed=%d\n", ret);
4149a0bf528SMauro Carvalho Chehab 	return ret;
4159a0bf528SMauro Carvalho Chehab }
4169a0bf528SMauro Carvalho Chehab 
cxd2820r_get_tune_settings_t(struct dvb_frontend * fe,struct dvb_frontend_tune_settings * s)4179a0bf528SMauro Carvalho Chehab int cxd2820r_get_tune_settings_t(struct dvb_frontend *fe,
4189a0bf528SMauro Carvalho Chehab 	struct dvb_frontend_tune_settings *s)
4199a0bf528SMauro Carvalho Chehab {
4209a0bf528SMauro Carvalho Chehab 	s->min_delay_ms = 500;
421f1b1eabfSMauro Carvalho Chehab 	s->step_size = fe->ops.info.frequency_stepsize_hz * 2;
422f1b1eabfSMauro Carvalho Chehab 	s->max_drift = (fe->ops.info.frequency_stepsize_hz * 2) + 1;
4239a0bf528SMauro Carvalho Chehab 
4249a0bf528SMauro Carvalho Chehab 	return 0;
4259a0bf528SMauro Carvalho Chehab }
426