18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Panasonic MN88473 DVB-T/T2/C demodulator driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "mn88473_priv.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic int mn88473_get_tune_settings(struct dvb_frontend *fe,
118c2ecf20Sopenharmony_ci				     struct dvb_frontend_tune_settings *s)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	s->min_delay_ms = 1000;
148c2ecf20Sopenharmony_ci	return 0;
158c2ecf20Sopenharmony_ci}
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic int mn88473_set_frontend(struct dvb_frontend *fe)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	struct i2c_client *client = fe->demodulator_priv;
208c2ecf20Sopenharmony_ci	struct mn88473_dev *dev = i2c_get_clientdata(client);
218c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
228c2ecf20Sopenharmony_ci	int ret, i;
238c2ecf20Sopenharmony_ci	unsigned int uitmp;
248c2ecf20Sopenharmony_ci	u32 if_frequency;
258c2ecf20Sopenharmony_ci	u8 delivery_system_val, if_val[3], *conf_val_ptr;
268c2ecf20Sopenharmony_ci	u8 reg_bank2_2d_val, reg_bank0_d2_val;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	dev_dbg(&client->dev,
298c2ecf20Sopenharmony_ci		"delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%d stream_id=%d\n",
308c2ecf20Sopenharmony_ci		c->delivery_system, c->modulation, c->frequency,
318c2ecf20Sopenharmony_ci		c->bandwidth_hz, c->symbol_rate, c->inversion, c->stream_id);
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	if (!dev->active) {
348c2ecf20Sopenharmony_ci		ret = -EAGAIN;
358c2ecf20Sopenharmony_ci		goto err;
368c2ecf20Sopenharmony_ci	}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	switch (c->delivery_system) {
398c2ecf20Sopenharmony_ci	case SYS_DVBT:
408c2ecf20Sopenharmony_ci		delivery_system_val = 0x02;
418c2ecf20Sopenharmony_ci		reg_bank2_2d_val = 0x23;
428c2ecf20Sopenharmony_ci		reg_bank0_d2_val = 0x2a;
438c2ecf20Sopenharmony_ci		break;
448c2ecf20Sopenharmony_ci	case SYS_DVBT2:
458c2ecf20Sopenharmony_ci		delivery_system_val = 0x03;
468c2ecf20Sopenharmony_ci		reg_bank2_2d_val = 0x3b;
478c2ecf20Sopenharmony_ci		reg_bank0_d2_val = 0x29;
488c2ecf20Sopenharmony_ci		break;
498c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_A:
508c2ecf20Sopenharmony_ci		delivery_system_val = 0x04;
518c2ecf20Sopenharmony_ci		reg_bank2_2d_val = 0x3b;
528c2ecf20Sopenharmony_ci		reg_bank0_d2_val = 0x29;
538c2ecf20Sopenharmony_ci		break;
548c2ecf20Sopenharmony_ci	default:
558c2ecf20Sopenharmony_ci		ret = -EINVAL;
568c2ecf20Sopenharmony_ci		goto err;
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	switch (c->delivery_system) {
608c2ecf20Sopenharmony_ci	case SYS_DVBT:
618c2ecf20Sopenharmony_ci	case SYS_DVBT2:
628c2ecf20Sopenharmony_ci		switch (c->bandwidth_hz) {
638c2ecf20Sopenharmony_ci		case 6000000:
648c2ecf20Sopenharmony_ci			conf_val_ptr = "\xe9\x55\x55\x1c\x29\x1c\x29";
658c2ecf20Sopenharmony_ci			break;
668c2ecf20Sopenharmony_ci		case 7000000:
678c2ecf20Sopenharmony_ci			conf_val_ptr = "\xc8\x00\x00\x17\x0a\x17\x0a";
688c2ecf20Sopenharmony_ci			break;
698c2ecf20Sopenharmony_ci		case 8000000:
708c2ecf20Sopenharmony_ci			conf_val_ptr = "\xaf\x00\x00\x11\xec\x11\xec";
718c2ecf20Sopenharmony_ci			break;
728c2ecf20Sopenharmony_ci		default:
738c2ecf20Sopenharmony_ci			ret = -EINVAL;
748c2ecf20Sopenharmony_ci			goto err;
758c2ecf20Sopenharmony_ci		}
768c2ecf20Sopenharmony_ci		break;
778c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_A:
788c2ecf20Sopenharmony_ci		conf_val_ptr = "\x10\xab\x0d\xae\x1d\x9d";
798c2ecf20Sopenharmony_ci		break;
808c2ecf20Sopenharmony_ci	default:
818c2ecf20Sopenharmony_ci		break;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/* Program tuner */
858c2ecf20Sopenharmony_ci	if (fe->ops.tuner_ops.set_params) {
868c2ecf20Sopenharmony_ci		ret = fe->ops.tuner_ops.set_params(fe);
878c2ecf20Sopenharmony_ci		if (ret)
888c2ecf20Sopenharmony_ci			goto err;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (fe->ops.tuner_ops.get_if_frequency) {
928c2ecf20Sopenharmony_ci		ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
938c2ecf20Sopenharmony_ci		if (ret)
948c2ecf20Sopenharmony_ci			goto err;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "get_if_frequency=%u\n", if_frequency);
978c2ecf20Sopenharmony_ci	} else {
988c2ecf20Sopenharmony_ci		ret = -EINVAL;
998c2ecf20Sopenharmony_ci		goto err;
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	/* Calculate IF registers */
1038c2ecf20Sopenharmony_ci	uitmp = DIV_ROUND_CLOSEST_ULL((u64) if_frequency * 0x1000000, dev->clk);
1048c2ecf20Sopenharmony_ci	if_val[0] = (uitmp >> 16) & 0xff;
1058c2ecf20Sopenharmony_ci	if_val[1] = (uitmp >>  8) & 0xff;
1068c2ecf20Sopenharmony_ci	if_val[2] = (uitmp >>  0) & 0xff;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x05, 0x00);
1098c2ecf20Sopenharmony_ci	if (ret)
1108c2ecf20Sopenharmony_ci		goto err;
1118c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0xfb, 0x13);
1128c2ecf20Sopenharmony_ci	if (ret)
1138c2ecf20Sopenharmony_ci		goto err;
1148c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0xef, 0x13);
1158c2ecf20Sopenharmony_ci	if (ret)
1168c2ecf20Sopenharmony_ci		goto err;
1178c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0xf9, 0x13);
1188c2ecf20Sopenharmony_ci	if (ret)
1198c2ecf20Sopenharmony_ci		goto err;
1208c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x00, 0x18);
1218c2ecf20Sopenharmony_ci	if (ret)
1228c2ecf20Sopenharmony_ci		goto err;
1238c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x01, 0x01);
1248c2ecf20Sopenharmony_ci	if (ret)
1258c2ecf20Sopenharmony_ci		goto err;
1268c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x02, 0x21);
1278c2ecf20Sopenharmony_ci	if (ret)
1288c2ecf20Sopenharmony_ci		goto err;
1298c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x03, delivery_system_val);
1308c2ecf20Sopenharmony_ci	if (ret)
1318c2ecf20Sopenharmony_ci		goto err;
1328c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x0b, 0x00);
1338c2ecf20Sopenharmony_ci	if (ret)
1348c2ecf20Sopenharmony_ci		goto err;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	for (i = 0; i < sizeof(if_val); i++) {
1378c2ecf20Sopenharmony_ci		ret = regmap_write(dev->regmap[2], 0x10 + i, if_val[i]);
1388c2ecf20Sopenharmony_ci		if (ret)
1398c2ecf20Sopenharmony_ci			goto err;
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	switch (c->delivery_system) {
1438c2ecf20Sopenharmony_ci	case SYS_DVBT:
1448c2ecf20Sopenharmony_ci	case SYS_DVBT2:
1458c2ecf20Sopenharmony_ci		for (i = 0; i < 7; i++) {
1468c2ecf20Sopenharmony_ci			ret = regmap_write(dev->regmap[2], 0x13 + i,
1478c2ecf20Sopenharmony_ci					   conf_val_ptr[i]);
1488c2ecf20Sopenharmony_ci			if (ret)
1498c2ecf20Sopenharmony_ci				goto err;
1508c2ecf20Sopenharmony_ci		}
1518c2ecf20Sopenharmony_ci		break;
1528c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_A:
1538c2ecf20Sopenharmony_ci		ret = regmap_bulk_write(dev->regmap[1], 0x10, conf_val_ptr, 6);
1548c2ecf20Sopenharmony_ci		if (ret)
1558c2ecf20Sopenharmony_ci			goto err;
1568c2ecf20Sopenharmony_ci		break;
1578c2ecf20Sopenharmony_ci	default:
1588c2ecf20Sopenharmony_ci		break;
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x2d, reg_bank2_2d_val);
1628c2ecf20Sopenharmony_ci	if (ret)
1638c2ecf20Sopenharmony_ci		goto err;
1648c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x2e, 0x00);
1658c2ecf20Sopenharmony_ci	if (ret)
1668c2ecf20Sopenharmony_ci		goto err;
1678c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x56, 0x0d);
1688c2ecf20Sopenharmony_ci	if (ret)
1698c2ecf20Sopenharmony_ci		goto err;
1708c2ecf20Sopenharmony_ci	ret = regmap_bulk_write(dev->regmap[0], 0x01,
1718c2ecf20Sopenharmony_ci				"\xba\x13\x80\xba\x91\xdd\xe7\x28", 8);
1728c2ecf20Sopenharmony_ci	if (ret)
1738c2ecf20Sopenharmony_ci		goto err;
1748c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0x0a, 0x1a);
1758c2ecf20Sopenharmony_ci	if (ret)
1768c2ecf20Sopenharmony_ci		goto err;
1778c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0x13, 0x1f);
1788c2ecf20Sopenharmony_ci	if (ret)
1798c2ecf20Sopenharmony_ci		goto err;
1808c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0x19, 0x03);
1818c2ecf20Sopenharmony_ci	if (ret)
1828c2ecf20Sopenharmony_ci		goto err;
1838c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0x1d, 0xb0);
1848c2ecf20Sopenharmony_ci	if (ret)
1858c2ecf20Sopenharmony_ci		goto err;
1868c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0x2a, 0x72);
1878c2ecf20Sopenharmony_ci	if (ret)
1888c2ecf20Sopenharmony_ci		goto err;
1898c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0x2d, 0x00);
1908c2ecf20Sopenharmony_ci	if (ret)
1918c2ecf20Sopenharmony_ci		goto err;
1928c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0x3c, 0x00);
1938c2ecf20Sopenharmony_ci	if (ret)
1948c2ecf20Sopenharmony_ci		goto err;
1958c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0x3f, 0xf8);
1968c2ecf20Sopenharmony_ci	if (ret)
1978c2ecf20Sopenharmony_ci		goto err;
1988c2ecf20Sopenharmony_ci	ret = regmap_bulk_write(dev->regmap[0], 0x40, "\xf4\x08", 2);
1998c2ecf20Sopenharmony_ci	if (ret)
2008c2ecf20Sopenharmony_ci		goto err;
2018c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0xd2, reg_bank0_d2_val);
2028c2ecf20Sopenharmony_ci	if (ret)
2038c2ecf20Sopenharmony_ci		goto err;
2048c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0xd4, 0x55);
2058c2ecf20Sopenharmony_ci	if (ret)
2068c2ecf20Sopenharmony_ci		goto err;
2078c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[1], 0xbe, 0x08);
2088c2ecf20Sopenharmony_ci	if (ret)
2098c2ecf20Sopenharmony_ci		goto err;
2108c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0xb2, 0x37);
2118c2ecf20Sopenharmony_ci	if (ret)
2128c2ecf20Sopenharmony_ci		goto err;
2138c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0xd7, 0x04);
2148c2ecf20Sopenharmony_ci	if (ret)
2158c2ecf20Sopenharmony_ci		goto err;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* PLP */
2188c2ecf20Sopenharmony_ci	if (c->delivery_system == SYS_DVBT2) {
2198c2ecf20Sopenharmony_ci		ret = regmap_write(dev->regmap[2], 0x36,
2208c2ecf20Sopenharmony_ci				(c->stream_id == NO_STREAM_ID_FILTER) ? 0 :
2218c2ecf20Sopenharmony_ci				c->stream_id );
2228c2ecf20Sopenharmony_ci		if (ret)
2238c2ecf20Sopenharmony_ci			goto err;
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* Reset FSM */
2278c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0xf8, 0x9f);
2288c2ecf20Sopenharmony_ci	if (ret)
2298c2ecf20Sopenharmony_ci		goto err;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	return 0;
2328c2ecf20Sopenharmony_cierr:
2338c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "failed=%d\n", ret);
2348c2ecf20Sopenharmony_ci	return ret;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic int mn88473_read_status(struct dvb_frontend *fe, enum fe_status *status)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct i2c_client *client = fe->demodulator_priv;
2408c2ecf20Sopenharmony_ci	struct mn88473_dev *dev = i2c_get_clientdata(client);
2418c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2428c2ecf20Sopenharmony_ci	int ret, i, stmp;
2438c2ecf20Sopenharmony_ci	unsigned int utmp, utmp1, utmp2;
2448c2ecf20Sopenharmony_ci	u8 buf[5];
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	if (!dev->active) {
2478c2ecf20Sopenharmony_ci		ret = -EAGAIN;
2488c2ecf20Sopenharmony_ci		goto err;
2498c2ecf20Sopenharmony_ci	}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	/* Lock detection */
2528c2ecf20Sopenharmony_ci	switch (c->delivery_system) {
2538c2ecf20Sopenharmony_ci	case SYS_DVBT:
2548c2ecf20Sopenharmony_ci		ret = regmap_read(dev->regmap[0], 0x62, &utmp);
2558c2ecf20Sopenharmony_ci		if (ret)
2568c2ecf20Sopenharmony_ci			goto err;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci		if (!(utmp & 0xa0)) {
2598c2ecf20Sopenharmony_ci			if ((utmp & 0x0f) >= 0x09)
2608c2ecf20Sopenharmony_ci				*status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
2618c2ecf20Sopenharmony_ci					  FE_HAS_VITERBI | FE_HAS_SYNC |
2628c2ecf20Sopenharmony_ci					  FE_HAS_LOCK;
2638c2ecf20Sopenharmony_ci			else if ((utmp & 0x0f) >= 0x03)
2648c2ecf20Sopenharmony_ci				*status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
2658c2ecf20Sopenharmony_ci		} else {
2668c2ecf20Sopenharmony_ci			*status = 0;
2678c2ecf20Sopenharmony_ci		}
2688c2ecf20Sopenharmony_ci		break;
2698c2ecf20Sopenharmony_ci	case SYS_DVBT2:
2708c2ecf20Sopenharmony_ci		ret = regmap_read(dev->regmap[2], 0x8b, &utmp);
2718c2ecf20Sopenharmony_ci		if (ret)
2728c2ecf20Sopenharmony_ci			goto err;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci		if (!(utmp & 0x40)) {
2758c2ecf20Sopenharmony_ci			if ((utmp & 0x0f) >= 0x0d)
2768c2ecf20Sopenharmony_ci				*status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
2778c2ecf20Sopenharmony_ci					  FE_HAS_VITERBI | FE_HAS_SYNC |
2788c2ecf20Sopenharmony_ci					  FE_HAS_LOCK;
2798c2ecf20Sopenharmony_ci			else if ((utmp & 0x0f) >= 0x0a)
2808c2ecf20Sopenharmony_ci				*status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
2818c2ecf20Sopenharmony_ci					  FE_HAS_VITERBI;
2828c2ecf20Sopenharmony_ci			else if ((utmp & 0x0f) >= 0x07)
2838c2ecf20Sopenharmony_ci				*status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
2848c2ecf20Sopenharmony_ci		} else {
2858c2ecf20Sopenharmony_ci			*status = 0;
2868c2ecf20Sopenharmony_ci		}
2878c2ecf20Sopenharmony_ci		break;
2888c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_A:
2898c2ecf20Sopenharmony_ci		ret = regmap_read(dev->regmap[1], 0x85, &utmp);
2908c2ecf20Sopenharmony_ci		if (ret)
2918c2ecf20Sopenharmony_ci			goto err;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci		if (!(utmp & 0x40)) {
2948c2ecf20Sopenharmony_ci			ret = regmap_read(dev->regmap[1], 0x89, &utmp);
2958c2ecf20Sopenharmony_ci			if (ret)
2968c2ecf20Sopenharmony_ci				goto err;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci			if (utmp & 0x01)
2998c2ecf20Sopenharmony_ci				*status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
3008c2ecf20Sopenharmony_ci						FE_HAS_VITERBI | FE_HAS_SYNC |
3018c2ecf20Sopenharmony_ci						FE_HAS_LOCK;
3028c2ecf20Sopenharmony_ci		} else {
3038c2ecf20Sopenharmony_ci			*status = 0;
3048c2ecf20Sopenharmony_ci		}
3058c2ecf20Sopenharmony_ci		break;
3068c2ecf20Sopenharmony_ci	default:
3078c2ecf20Sopenharmony_ci		ret = -EINVAL;
3088c2ecf20Sopenharmony_ci		goto err;
3098c2ecf20Sopenharmony_ci	}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	/* Signal strength */
3128c2ecf20Sopenharmony_ci	if (*status & FE_HAS_SIGNAL) {
3138c2ecf20Sopenharmony_ci		for (i = 0; i < 2; i++) {
3148c2ecf20Sopenharmony_ci			ret = regmap_bulk_read(dev->regmap[2], 0x86 + i,
3158c2ecf20Sopenharmony_ci					       &buf[i], 1);
3168c2ecf20Sopenharmony_ci			if (ret)
3178c2ecf20Sopenharmony_ci				goto err;
3188c2ecf20Sopenharmony_ci		}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci		/* AGCRD[15:6] gives us a 10bit value ([5:0] are always 0) */
3218c2ecf20Sopenharmony_ci		utmp1 = buf[0] << 8 | buf[1] << 0 | buf[0] >> 2;
3228c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "strength=%u\n", utmp1);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci		c->strength.stat[0].scale = FE_SCALE_RELATIVE;
3258c2ecf20Sopenharmony_ci		c->strength.stat[0].uvalue = utmp1;
3268c2ecf20Sopenharmony_ci	} else {
3278c2ecf20Sopenharmony_ci		c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3288c2ecf20Sopenharmony_ci	}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	/* CNR */
3318c2ecf20Sopenharmony_ci	if (*status & FE_HAS_VITERBI && c->delivery_system == SYS_DVBT) {
3328c2ecf20Sopenharmony_ci		/* DVB-T CNR */
3338c2ecf20Sopenharmony_ci		ret = regmap_bulk_read(dev->regmap[0], 0x8f, buf, 2);
3348c2ecf20Sopenharmony_ci		if (ret)
3358c2ecf20Sopenharmony_ci			goto err;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci		utmp = buf[0] << 8 | buf[1] << 0;
3388c2ecf20Sopenharmony_ci		if (utmp) {
3398c2ecf20Sopenharmony_ci			/* CNR[dB]: 10 * (log10(65536 / value) + 0.2) */
3408c2ecf20Sopenharmony_ci			/* log10(65536) = 80807124, 0.2 = 3355443 */
3418c2ecf20Sopenharmony_ci			stmp = div_u64(((u64)80807124 - intlog10(utmp)
3428c2ecf20Sopenharmony_ci					+ 3355443) * 10000, 1 << 24);
3438c2ecf20Sopenharmony_ci			dev_dbg(&client->dev, "cnr=%d value=%u\n", stmp, utmp);
3448c2ecf20Sopenharmony_ci		} else {
3458c2ecf20Sopenharmony_ci			stmp = 0;
3468c2ecf20Sopenharmony_ci		}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci		c->cnr.stat[0].svalue = stmp;
3498c2ecf20Sopenharmony_ci		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
3508c2ecf20Sopenharmony_ci	} else if (*status & FE_HAS_VITERBI &&
3518c2ecf20Sopenharmony_ci		   c->delivery_system == SYS_DVBT2) {
3528c2ecf20Sopenharmony_ci		/* DVB-T2 CNR */
3538c2ecf20Sopenharmony_ci		for (i = 0; i < 3; i++) {
3548c2ecf20Sopenharmony_ci			ret = regmap_bulk_read(dev->regmap[2], 0xb7 + i,
3558c2ecf20Sopenharmony_ci					       &buf[i], 1);
3568c2ecf20Sopenharmony_ci			if (ret)
3578c2ecf20Sopenharmony_ci				goto err;
3588c2ecf20Sopenharmony_ci		}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci		utmp = buf[1] << 8 | buf[2] << 0;
3618c2ecf20Sopenharmony_ci		utmp1 = (buf[0] >> 2) & 0x01; /* 0=SISO, 1=MISO */
3628c2ecf20Sopenharmony_ci		if (utmp) {
3638c2ecf20Sopenharmony_ci			if (utmp1) {
3648c2ecf20Sopenharmony_ci				/* CNR[dB]: 10 * (log10(16384 / value) - 0.6) */
3658c2ecf20Sopenharmony_ci				/* log10(16384) = 70706234, 0.6 = 10066330 */
3668c2ecf20Sopenharmony_ci				stmp = div_u64(((u64)70706234 - intlog10(utmp)
3678c2ecf20Sopenharmony_ci						- 10066330) * 10000, 1 << 24);
3688c2ecf20Sopenharmony_ci				dev_dbg(&client->dev, "cnr=%d value=%u MISO\n",
3698c2ecf20Sopenharmony_ci					stmp, utmp);
3708c2ecf20Sopenharmony_ci			} else {
3718c2ecf20Sopenharmony_ci				/* CNR[dB]: 10 * (log10(65536 / value) + 0.2) */
3728c2ecf20Sopenharmony_ci				/* log10(65536) = 80807124, 0.2 = 3355443 */
3738c2ecf20Sopenharmony_ci				stmp = div_u64(((u64)80807124 - intlog10(utmp)
3748c2ecf20Sopenharmony_ci						+ 3355443) * 10000, 1 << 24);
3758c2ecf20Sopenharmony_ci				dev_dbg(&client->dev, "cnr=%d value=%u SISO\n",
3768c2ecf20Sopenharmony_ci					stmp, utmp);
3778c2ecf20Sopenharmony_ci			}
3788c2ecf20Sopenharmony_ci		} else {
3798c2ecf20Sopenharmony_ci			stmp = 0;
3808c2ecf20Sopenharmony_ci		}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci		c->cnr.stat[0].svalue = stmp;
3838c2ecf20Sopenharmony_ci		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
3848c2ecf20Sopenharmony_ci	} else if (*status & FE_HAS_VITERBI &&
3858c2ecf20Sopenharmony_ci		   c->delivery_system == SYS_DVBC_ANNEX_A) {
3868c2ecf20Sopenharmony_ci		/* DVB-C CNR */
3878c2ecf20Sopenharmony_ci		ret = regmap_bulk_read(dev->regmap[1], 0xa1, buf, 4);
3888c2ecf20Sopenharmony_ci		if (ret)
3898c2ecf20Sopenharmony_ci			goto err;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci		utmp1 = buf[0] << 8 | buf[1] << 0; /* signal */
3928c2ecf20Sopenharmony_ci		utmp2 = buf[2] << 8 | buf[3] << 0; /* noise */
3938c2ecf20Sopenharmony_ci		if (utmp1 && utmp2) {
3948c2ecf20Sopenharmony_ci			/* CNR[dB]: 10 * log10(8 * (signal / noise)) */
3958c2ecf20Sopenharmony_ci			/* log10(8) = 15151336 */
3968c2ecf20Sopenharmony_ci			stmp = div_u64(((u64)15151336 + intlog10(utmp1)
3978c2ecf20Sopenharmony_ci					- intlog10(utmp2)) * 10000, 1 << 24);
3988c2ecf20Sopenharmony_ci			dev_dbg(&client->dev, "cnr=%d signal=%u noise=%u\n",
3998c2ecf20Sopenharmony_ci				stmp, utmp1, utmp2);
4008c2ecf20Sopenharmony_ci		} else {
4018c2ecf20Sopenharmony_ci			stmp = 0;
4028c2ecf20Sopenharmony_ci		}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci		c->cnr.stat[0].svalue = stmp;
4058c2ecf20Sopenharmony_ci		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
4068c2ecf20Sopenharmony_ci	} else {
4078c2ecf20Sopenharmony_ci		c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	/* BER */
4118c2ecf20Sopenharmony_ci	if (*status & FE_HAS_LOCK && (c->delivery_system == SYS_DVBT ||
4128c2ecf20Sopenharmony_ci				      c->delivery_system == SYS_DVBC_ANNEX_A)) {
4138c2ecf20Sopenharmony_ci		/* DVB-T & DVB-C BER */
4148c2ecf20Sopenharmony_ci		ret = regmap_bulk_read(dev->regmap[0], 0x92, buf, 5);
4158c2ecf20Sopenharmony_ci		if (ret)
4168c2ecf20Sopenharmony_ci			goto err;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci		utmp1 = buf[0] << 16 | buf[1] << 8 | buf[2] << 0;
4198c2ecf20Sopenharmony_ci		utmp2 = buf[3] << 8 | buf[4] << 0;
4208c2ecf20Sopenharmony_ci		utmp2 = utmp2 * 8 * 204;
4218c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "post_bit_error=%u post_bit_count=%u\n",
4228c2ecf20Sopenharmony_ci			utmp1, utmp2);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci		c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
4258c2ecf20Sopenharmony_ci		c->post_bit_error.stat[0].uvalue += utmp1;
4268c2ecf20Sopenharmony_ci		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
4278c2ecf20Sopenharmony_ci		c->post_bit_count.stat[0].uvalue += utmp2;
4288c2ecf20Sopenharmony_ci	} else {
4298c2ecf20Sopenharmony_ci		c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
4308c2ecf20Sopenharmony_ci		c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	/* PER */
4348c2ecf20Sopenharmony_ci	if (*status & FE_HAS_LOCK) {
4358c2ecf20Sopenharmony_ci		ret = regmap_bulk_read(dev->regmap[0], 0xdd, buf, 4);
4368c2ecf20Sopenharmony_ci		if (ret)
4378c2ecf20Sopenharmony_ci			goto err;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci		utmp1 = buf[0] << 8 | buf[1] << 0;
4408c2ecf20Sopenharmony_ci		utmp2 = buf[2] << 8 | buf[3] << 0;
4418c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "block_error=%u block_count=%u\n",
4428c2ecf20Sopenharmony_ci			utmp1, utmp2);
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci		c->block_error.stat[0].scale = FE_SCALE_COUNTER;
4458c2ecf20Sopenharmony_ci		c->block_error.stat[0].uvalue += utmp1;
4468c2ecf20Sopenharmony_ci		c->block_count.stat[0].scale = FE_SCALE_COUNTER;
4478c2ecf20Sopenharmony_ci		c->block_count.stat[0].uvalue += utmp2;
4488c2ecf20Sopenharmony_ci	} else {
4498c2ecf20Sopenharmony_ci		c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
4508c2ecf20Sopenharmony_ci		c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
4518c2ecf20Sopenharmony_ci	}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	return 0;
4548c2ecf20Sopenharmony_cierr:
4558c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "failed=%d\n", ret);
4568c2ecf20Sopenharmony_ci	return ret;
4578c2ecf20Sopenharmony_ci}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_cistatic int mn88473_init(struct dvb_frontend *fe)
4608c2ecf20Sopenharmony_ci{
4618c2ecf20Sopenharmony_ci	struct i2c_client *client = fe->demodulator_priv;
4628c2ecf20Sopenharmony_ci	struct mn88473_dev *dev = i2c_get_clientdata(client);
4638c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
4648c2ecf20Sopenharmony_ci	int ret, len, remain;
4658c2ecf20Sopenharmony_ci	unsigned int uitmp;
4668c2ecf20Sopenharmony_ci	const struct firmware *fw;
4678c2ecf20Sopenharmony_ci	const char *name = MN88473_FIRMWARE;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "\n");
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	/* Check if firmware is already running */
4728c2ecf20Sopenharmony_ci	ret = regmap_read(dev->regmap[0], 0xf5, &uitmp);
4738c2ecf20Sopenharmony_ci	if (ret)
4748c2ecf20Sopenharmony_ci		goto err;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	if (!(uitmp & 0x01))
4778c2ecf20Sopenharmony_ci		goto warm;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	/* Request the firmware, this will block and timeout */
4808c2ecf20Sopenharmony_ci	ret = request_firmware(&fw, name, &client->dev);
4818c2ecf20Sopenharmony_ci	if (ret) {
4828c2ecf20Sopenharmony_ci		dev_err(&client->dev, "firmware file '%s' not found\n", name);
4838c2ecf20Sopenharmony_ci		goto err;
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	dev_info(&client->dev, "downloading firmware from file '%s'\n", name);
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0xf5, 0x03);
4898c2ecf20Sopenharmony_ci	if (ret)
4908c2ecf20Sopenharmony_ci		goto err_release_firmware;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	for (remain = fw->size; remain > 0; remain -= (dev->i2c_wr_max - 1)) {
4938c2ecf20Sopenharmony_ci		len = min(dev->i2c_wr_max - 1, remain);
4948c2ecf20Sopenharmony_ci		ret = regmap_bulk_write(dev->regmap[0], 0xf6,
4958c2ecf20Sopenharmony_ci					&fw->data[fw->size - remain], len);
4968c2ecf20Sopenharmony_ci		if (ret) {
4978c2ecf20Sopenharmony_ci			dev_err(&client->dev, "firmware download failed %d\n",
4988c2ecf20Sopenharmony_ci				ret);
4998c2ecf20Sopenharmony_ci			goto err_release_firmware;
5008c2ecf20Sopenharmony_ci		}
5018c2ecf20Sopenharmony_ci	}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	release_firmware(fw);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	/* Parity check of firmware */
5068c2ecf20Sopenharmony_ci	ret = regmap_read(dev->regmap[0], 0xf8, &uitmp);
5078c2ecf20Sopenharmony_ci	if (ret)
5088c2ecf20Sopenharmony_ci		goto err;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	if (uitmp & 0x10) {
5118c2ecf20Sopenharmony_ci		dev_err(&client->dev, "firmware parity check failed\n");
5128c2ecf20Sopenharmony_ci		ret = -EINVAL;
5138c2ecf20Sopenharmony_ci		goto err;
5148c2ecf20Sopenharmony_ci	}
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[0], 0xf5, 0x00);
5178c2ecf20Sopenharmony_ci	if (ret)
5188c2ecf20Sopenharmony_ci		goto err;
5198c2ecf20Sopenharmony_ciwarm:
5208c2ecf20Sopenharmony_ci	/* TS config */
5218c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x09, 0x08);
5228c2ecf20Sopenharmony_ci	if (ret)
5238c2ecf20Sopenharmony_ci		goto err;
5248c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x08, 0x1d);
5258c2ecf20Sopenharmony_ci	if (ret)
5268c2ecf20Sopenharmony_ci		goto err;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	dev->active = true;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	/* init stats here to indicate which stats are supported */
5318c2ecf20Sopenharmony_ci	c->strength.len = 1;
5328c2ecf20Sopenharmony_ci	c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
5338c2ecf20Sopenharmony_ci	c->cnr.len = 1;
5348c2ecf20Sopenharmony_ci	c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
5358c2ecf20Sopenharmony_ci	c->post_bit_error.len = 1;
5368c2ecf20Sopenharmony_ci	c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
5378c2ecf20Sopenharmony_ci	c->post_bit_count.len = 1;
5388c2ecf20Sopenharmony_ci	c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
5398c2ecf20Sopenharmony_ci	c->block_error.len = 1;
5408c2ecf20Sopenharmony_ci	c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
5418c2ecf20Sopenharmony_ci	c->block_count.len = 1;
5428c2ecf20Sopenharmony_ci	c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	return 0;
5458c2ecf20Sopenharmony_cierr_release_firmware:
5468c2ecf20Sopenharmony_ci	release_firmware(fw);
5478c2ecf20Sopenharmony_cierr:
5488c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "failed=%d\n", ret);
5498c2ecf20Sopenharmony_ci	return ret;
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_cistatic int mn88473_sleep(struct dvb_frontend *fe)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	struct i2c_client *client = fe->demodulator_priv;
5558c2ecf20Sopenharmony_ci	struct mn88473_dev *dev = i2c_get_clientdata(client);
5568c2ecf20Sopenharmony_ci	int ret;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "\n");
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	dev->active = false;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x05, 0x3e);
5638c2ecf20Sopenharmony_ci	if (ret)
5648c2ecf20Sopenharmony_ci		goto err;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	return 0;
5678c2ecf20Sopenharmony_cierr:
5688c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "failed=%d\n", ret);
5698c2ecf20Sopenharmony_ci	return ret;
5708c2ecf20Sopenharmony_ci}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistatic const struct dvb_frontend_ops mn88473_ops = {
5738c2ecf20Sopenharmony_ci	.delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
5748c2ecf20Sopenharmony_ci	.info = {
5758c2ecf20Sopenharmony_ci		.name = "Panasonic MN88473",
5768c2ecf20Sopenharmony_ci		.symbol_rate_min = 1000000,
5778c2ecf20Sopenharmony_ci		.symbol_rate_max = 7200000,
5788c2ecf20Sopenharmony_ci		.caps =	FE_CAN_FEC_1_2                 |
5798c2ecf20Sopenharmony_ci			FE_CAN_FEC_2_3                 |
5808c2ecf20Sopenharmony_ci			FE_CAN_FEC_3_4                 |
5818c2ecf20Sopenharmony_ci			FE_CAN_FEC_5_6                 |
5828c2ecf20Sopenharmony_ci			FE_CAN_FEC_7_8                 |
5838c2ecf20Sopenharmony_ci			FE_CAN_FEC_AUTO                |
5848c2ecf20Sopenharmony_ci			FE_CAN_QPSK                    |
5858c2ecf20Sopenharmony_ci			FE_CAN_QAM_16                  |
5868c2ecf20Sopenharmony_ci			FE_CAN_QAM_32                  |
5878c2ecf20Sopenharmony_ci			FE_CAN_QAM_64                  |
5888c2ecf20Sopenharmony_ci			FE_CAN_QAM_128                 |
5898c2ecf20Sopenharmony_ci			FE_CAN_QAM_256                 |
5908c2ecf20Sopenharmony_ci			FE_CAN_QAM_AUTO                |
5918c2ecf20Sopenharmony_ci			FE_CAN_TRANSMISSION_MODE_AUTO  |
5928c2ecf20Sopenharmony_ci			FE_CAN_GUARD_INTERVAL_AUTO     |
5938c2ecf20Sopenharmony_ci			FE_CAN_HIERARCHY_AUTO          |
5948c2ecf20Sopenharmony_ci			FE_CAN_MUTE_TS                 |
5958c2ecf20Sopenharmony_ci			FE_CAN_2G_MODULATION           |
5968c2ecf20Sopenharmony_ci			FE_CAN_MULTISTREAM
5978c2ecf20Sopenharmony_ci	},
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	.get_tune_settings = mn88473_get_tune_settings,
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	.init = mn88473_init,
6028c2ecf20Sopenharmony_ci	.sleep = mn88473_sleep,
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	.set_frontend = mn88473_set_frontend,
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	.read_status = mn88473_read_status,
6078c2ecf20Sopenharmony_ci};
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_cistatic int mn88473_probe(struct i2c_client *client,
6108c2ecf20Sopenharmony_ci			 const struct i2c_device_id *id)
6118c2ecf20Sopenharmony_ci{
6128c2ecf20Sopenharmony_ci	struct mn88473_config *config = client->dev.platform_data;
6138c2ecf20Sopenharmony_ci	struct mn88473_dev *dev;
6148c2ecf20Sopenharmony_ci	int ret;
6158c2ecf20Sopenharmony_ci	unsigned int uitmp;
6168c2ecf20Sopenharmony_ci	static const struct regmap_config regmap_config = {
6178c2ecf20Sopenharmony_ci		.reg_bits = 8,
6188c2ecf20Sopenharmony_ci		.val_bits = 8,
6198c2ecf20Sopenharmony_ci	};
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "\n");
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	/* Caller really need to provide pointer for frontend we create */
6248c2ecf20Sopenharmony_ci	if (config->fe == NULL) {
6258c2ecf20Sopenharmony_ci		dev_err(&client->dev, "frontend pointer not defined\n");
6268c2ecf20Sopenharmony_ci		ret = -EINVAL;
6278c2ecf20Sopenharmony_ci		goto err;
6288c2ecf20Sopenharmony_ci	}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
6318c2ecf20Sopenharmony_ci	if (dev == NULL) {
6328c2ecf20Sopenharmony_ci		ret = -ENOMEM;
6338c2ecf20Sopenharmony_ci		goto err;
6348c2ecf20Sopenharmony_ci	}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	if (config->i2c_wr_max)
6378c2ecf20Sopenharmony_ci		dev->i2c_wr_max = config->i2c_wr_max;
6388c2ecf20Sopenharmony_ci	else
6398c2ecf20Sopenharmony_ci		dev->i2c_wr_max = ~0;
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	if (config->xtal)
6428c2ecf20Sopenharmony_ci		dev->clk = config->xtal;
6438c2ecf20Sopenharmony_ci	else
6448c2ecf20Sopenharmony_ci		dev->clk = 25000000;
6458c2ecf20Sopenharmony_ci	dev->client[0] = client;
6468c2ecf20Sopenharmony_ci	dev->regmap[0] = regmap_init_i2c(dev->client[0], &regmap_config);
6478c2ecf20Sopenharmony_ci	if (IS_ERR(dev->regmap[0])) {
6488c2ecf20Sopenharmony_ci		ret = PTR_ERR(dev->regmap[0]);
6498c2ecf20Sopenharmony_ci		goto err_kfree;
6508c2ecf20Sopenharmony_ci	}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	/*
6538c2ecf20Sopenharmony_ci	 * Chip has three I2C addresses for different register banks. Used
6548c2ecf20Sopenharmony_ci	 * addresses are 0x18, 0x1a and 0x1c. We register two dummy clients,
6558c2ecf20Sopenharmony_ci	 * 0x1a and 0x1c, in order to get own I2C client for each register bank.
6568c2ecf20Sopenharmony_ci	 *
6578c2ecf20Sopenharmony_ci	 * Also, register bank 2 do not support sequential I/O. Only single
6588c2ecf20Sopenharmony_ci	 * register write or read is allowed to that bank.
6598c2ecf20Sopenharmony_ci	 */
6608c2ecf20Sopenharmony_ci	dev->client[1] = i2c_new_dummy_device(client->adapter, 0x1a);
6618c2ecf20Sopenharmony_ci	if (IS_ERR(dev->client[1])) {
6628c2ecf20Sopenharmony_ci		ret = PTR_ERR(dev->client[1]);
6638c2ecf20Sopenharmony_ci		dev_err(&client->dev, "I2C registration failed\n");
6648c2ecf20Sopenharmony_ci		goto err_regmap_0_regmap_exit;
6658c2ecf20Sopenharmony_ci	}
6668c2ecf20Sopenharmony_ci	dev->regmap[1] = regmap_init_i2c(dev->client[1], &regmap_config);
6678c2ecf20Sopenharmony_ci	if (IS_ERR(dev->regmap[1])) {
6688c2ecf20Sopenharmony_ci		ret = PTR_ERR(dev->regmap[1]);
6698c2ecf20Sopenharmony_ci		goto err_client_1_i2c_unregister_device;
6708c2ecf20Sopenharmony_ci	}
6718c2ecf20Sopenharmony_ci	i2c_set_clientdata(dev->client[1], dev);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	dev->client[2] = i2c_new_dummy_device(client->adapter, 0x1c);
6748c2ecf20Sopenharmony_ci	if (IS_ERR(dev->client[2])) {
6758c2ecf20Sopenharmony_ci		ret = PTR_ERR(dev->client[2]);
6768c2ecf20Sopenharmony_ci		dev_err(&client->dev, "2nd I2C registration failed\n");
6778c2ecf20Sopenharmony_ci		goto err_regmap_1_regmap_exit;
6788c2ecf20Sopenharmony_ci	}
6798c2ecf20Sopenharmony_ci	dev->regmap[2] = regmap_init_i2c(dev->client[2], &regmap_config);
6808c2ecf20Sopenharmony_ci	if (IS_ERR(dev->regmap[2])) {
6818c2ecf20Sopenharmony_ci		ret = PTR_ERR(dev->regmap[2]);
6828c2ecf20Sopenharmony_ci		goto err_client_2_i2c_unregister_device;
6838c2ecf20Sopenharmony_ci	}
6848c2ecf20Sopenharmony_ci	i2c_set_clientdata(dev->client[2], dev);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	/* Check demod answers with correct chip id */
6878c2ecf20Sopenharmony_ci	ret = regmap_read(dev->regmap[2], 0xff, &uitmp);
6888c2ecf20Sopenharmony_ci	if (ret)
6898c2ecf20Sopenharmony_ci		goto err_regmap_2_regmap_exit;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "chip id=%02x\n", uitmp);
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	if (uitmp != 0x03) {
6948c2ecf20Sopenharmony_ci		ret = -ENODEV;
6958c2ecf20Sopenharmony_ci		goto err_regmap_2_regmap_exit;
6968c2ecf20Sopenharmony_ci	}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	/* Sleep because chip is active by default */
6998c2ecf20Sopenharmony_ci	ret = regmap_write(dev->regmap[2], 0x05, 0x3e);
7008c2ecf20Sopenharmony_ci	if (ret)
7018c2ecf20Sopenharmony_ci		goto err_regmap_2_regmap_exit;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	/* Create dvb frontend */
7048c2ecf20Sopenharmony_ci	memcpy(&dev->frontend.ops, &mn88473_ops, sizeof(dev->frontend.ops));
7058c2ecf20Sopenharmony_ci	dev->frontend.demodulator_priv = client;
7068c2ecf20Sopenharmony_ci	*config->fe = &dev->frontend;
7078c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, dev);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	dev_info(&client->dev, "Panasonic MN88473 successfully identified\n");
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	return 0;
7128c2ecf20Sopenharmony_cierr_regmap_2_regmap_exit:
7138c2ecf20Sopenharmony_ci	regmap_exit(dev->regmap[2]);
7148c2ecf20Sopenharmony_cierr_client_2_i2c_unregister_device:
7158c2ecf20Sopenharmony_ci	i2c_unregister_device(dev->client[2]);
7168c2ecf20Sopenharmony_cierr_regmap_1_regmap_exit:
7178c2ecf20Sopenharmony_ci	regmap_exit(dev->regmap[1]);
7188c2ecf20Sopenharmony_cierr_client_1_i2c_unregister_device:
7198c2ecf20Sopenharmony_ci	i2c_unregister_device(dev->client[1]);
7208c2ecf20Sopenharmony_cierr_regmap_0_regmap_exit:
7218c2ecf20Sopenharmony_ci	regmap_exit(dev->regmap[0]);
7228c2ecf20Sopenharmony_cierr_kfree:
7238c2ecf20Sopenharmony_ci	kfree(dev);
7248c2ecf20Sopenharmony_cierr:
7258c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "failed=%d\n", ret);
7268c2ecf20Sopenharmony_ci	return ret;
7278c2ecf20Sopenharmony_ci}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_cistatic int mn88473_remove(struct i2c_client *client)
7308c2ecf20Sopenharmony_ci{
7318c2ecf20Sopenharmony_ci	struct mn88473_dev *dev = i2c_get_clientdata(client);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "\n");
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	regmap_exit(dev->regmap[2]);
7368c2ecf20Sopenharmony_ci	i2c_unregister_device(dev->client[2]);
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	regmap_exit(dev->regmap[1]);
7398c2ecf20Sopenharmony_ci	i2c_unregister_device(dev->client[1]);
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	regmap_exit(dev->regmap[0]);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	kfree(dev);
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	return 0;
7468c2ecf20Sopenharmony_ci}
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_cistatic const struct i2c_device_id mn88473_id_table[] = {
7498c2ecf20Sopenharmony_ci	{"mn88473", 0},
7508c2ecf20Sopenharmony_ci	{}
7518c2ecf20Sopenharmony_ci};
7528c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, mn88473_id_table);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_cistatic struct i2c_driver mn88473_driver = {
7558c2ecf20Sopenharmony_ci	.driver = {
7568c2ecf20Sopenharmony_ci		.name		     = "mn88473",
7578c2ecf20Sopenharmony_ci		.suppress_bind_attrs = true,
7588c2ecf20Sopenharmony_ci	},
7598c2ecf20Sopenharmony_ci	.probe		= mn88473_probe,
7608c2ecf20Sopenharmony_ci	.remove		= mn88473_remove,
7618c2ecf20Sopenharmony_ci	.id_table	= mn88473_id_table,
7628c2ecf20Sopenharmony_ci};
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_cimodule_i2c_driver(mn88473_driver);
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ciMODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
7678c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Panasonic MN88473 DVB-T/T2/C demodulator driver");
7688c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
7698c2ecf20Sopenharmony_ciMODULE_FIRMWARE(MN88473_FIRMWARE);
770