13e54a169SMatthias Schwarzott /*
218349f40SMatthias Schwarzott  *  Driver for Silicon Labs Si2161 DVB-T and Si2165 DVB-C/-T Demodulator
318349f40SMatthias Schwarzott  *
477f887cdSMatthias Schwarzott  *  Copyright (C) 2013-2017 Matthias Schwarzott <zzam@gentoo.org>
518349f40SMatthias Schwarzott  *
618349f40SMatthias Schwarzott  *  This program is free software; you can redistribute it and/or modify
718349f40SMatthias Schwarzott  *  it under the terms of the GNU General Public License as published by
818349f40SMatthias Schwarzott  *  the Free Software Foundation; either version 2 of the License, or
918349f40SMatthias Schwarzott  *  (at your option) any later version.
1018349f40SMatthias Schwarzott  *
1118349f40SMatthias Schwarzott  *  This program is distributed in the hope that it will be useful,
1218349f40SMatthias Schwarzott  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1318349f40SMatthias Schwarzott  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1418349f40SMatthias Schwarzott  *  GNU General Public License for more details.
1518349f40SMatthias Schwarzott  *
1618349f40SMatthias Schwarzott  *  References:
1718349f40SMatthias Schwarzott  *  http://www.silabs.com/Support%20Documents/TechnicalDocs/Si2165-short.pdf
183e54a169SMatthias Schwarzott  */
193e54a169SMatthias Schwarzott 
203e54a169SMatthias Schwarzott #include <linux/delay.h>
213e54a169SMatthias Schwarzott #include <linux/errno.h>
223e54a169SMatthias Schwarzott #include <linux/init.h>
233e54a169SMatthias Schwarzott #include <linux/kernel.h>
243e54a169SMatthias Schwarzott #include <linux/module.h>
253e54a169SMatthias Schwarzott #include <linux/string.h>
263e54a169SMatthias Schwarzott #include <linux/slab.h>
273e54a169SMatthias Schwarzott #include <linux/firmware.h>
28e3ea5e94SMatthias Schwarzott #include <linux/regmap.h>
293e54a169SMatthias Schwarzott 
303e54a169SMatthias Schwarzott #include "dvb_frontend.h"
313e54a169SMatthias Schwarzott #include "dvb_math.h"
323e54a169SMatthias Schwarzott #include "si2165_priv.h"
333e54a169SMatthias Schwarzott #include "si2165.h"
343e54a169SMatthias Schwarzott 
3518349f40SMatthias Schwarzott /*
3618349f40SMatthias Schwarzott  * Hauppauge WinTV-HVR-930C-HD B130 / PCTV QuatroStick 521e 1113xx
3718349f40SMatthias Schwarzott  * uses 16 MHz xtal
3818349f40SMatthias Schwarzott  *
3918349f40SMatthias Schwarzott  * Hauppauge WinTV-HVR-930C-HD B131 / PCTV QuatroStick 522e 1114xx
4018349f40SMatthias Schwarzott  * uses 24 MHz clock provided by tuner
4118349f40SMatthias Schwarzott  */
423e54a169SMatthias Schwarzott 
433e54a169SMatthias Schwarzott struct si2165_state {
447cd785adSMatthias Schwarzott 	struct i2c_client *client;
457cd785adSMatthias Schwarzott 
46e3ea5e94SMatthias Schwarzott 	struct regmap *regmap;
473e54a169SMatthias Schwarzott 
48d9a201dfSMatthias Schwarzott 	struct dvb_frontend fe;
493e54a169SMatthias Schwarzott 
503e54a169SMatthias Schwarzott 	struct si2165_config config;
513e54a169SMatthias Schwarzott 
5255bea400SMatthias Schwarzott 	u8 chip_revcode;
533e54a169SMatthias Schwarzott 	u8 chip_type;
543e54a169SMatthias Schwarzott 
553e54a169SMatthias Schwarzott 	/* calculated by xtal and div settings */
563e54a169SMatthias Schwarzott 	u32 fvco_hz;
573e54a169SMatthias Schwarzott 	u32 sys_clk;
583e54a169SMatthias Schwarzott 	u32 adc_clk;
593e54a169SMatthias Schwarzott 
603e54a169SMatthias Schwarzott 	bool has_dvbc;
613e54a169SMatthias Schwarzott 	bool has_dvbt;
623e54a169SMatthias Schwarzott 	bool firmware_loaded;
633e54a169SMatthias Schwarzott };
643e54a169SMatthias Schwarzott 
653e54a169SMatthias Schwarzott static int si2165_write(struct si2165_state *state, const u16 reg,
663e54a169SMatthias Schwarzott 			const u8 *src, const int count)
673e54a169SMatthias Schwarzott {
683e54a169SMatthias Schwarzott 	int ret;
693e54a169SMatthias Schwarzott 
707dbbb4bfSMatthias Schwarzott 	dev_dbg(&state->client->dev, "i2c write: reg: 0x%04x, data: %*ph\n",
717dbbb4bfSMatthias Schwarzott 		reg, count, src);
723e54a169SMatthias Schwarzott 
73e3ea5e94SMatthias Schwarzott 	ret = regmap_bulk_write(state->regmap, reg, src, count);
743e54a169SMatthias Schwarzott 
75e3ea5e94SMatthias Schwarzott 	if (ret)
76aa155449SMatthias Schwarzott 		dev_err(&state->client->dev, "%s: ret == %d\n", __func__, ret);
773e54a169SMatthias Schwarzott 
78e3ea5e94SMatthias Schwarzott 	return ret;
793e54a169SMatthias Schwarzott }
803e54a169SMatthias Schwarzott 
813e54a169SMatthias Schwarzott static int si2165_read(struct si2165_state *state,
823e54a169SMatthias Schwarzott 		       const u16 reg, u8 *val, const int count)
833e54a169SMatthias Schwarzott {
84e3ea5e94SMatthias Schwarzott 	int ret = regmap_bulk_read(state->regmap, reg, val, count);
853e54a169SMatthias Schwarzott 
86e3ea5e94SMatthias Schwarzott 	if (ret) {
87aa155449SMatthias Schwarzott 		dev_err(&state->client->dev, "%s: error (addr %02x reg %04x error (ret == %i)\n",
883e54a169SMatthias Schwarzott 			__func__, state->config.i2c_addr, reg, ret);
893e54a169SMatthias Schwarzott 		return ret;
903e54a169SMatthias Schwarzott 	}
913e54a169SMatthias Schwarzott 
927dbbb4bfSMatthias Schwarzott 	dev_dbg(&state->client->dev, "i2c read: reg: 0x%04x, data: %*ph\n",
937dbbb4bfSMatthias Schwarzott 		reg, count, val);
943e54a169SMatthias Schwarzott 
953e54a169SMatthias Schwarzott 	return 0;
963e54a169SMatthias Schwarzott }
973e54a169SMatthias Schwarzott 
983e54a169SMatthias Schwarzott static int si2165_readreg8(struct si2165_state *state,
993e54a169SMatthias Schwarzott 			   const u16 reg, u8 *val)
1003e54a169SMatthias Schwarzott {
101e3ea5e94SMatthias Schwarzott 	unsigned int val_tmp;
102e3ea5e94SMatthias Schwarzott 	int ret = regmap_read(state->regmap, reg, &val_tmp);
103e3ea5e94SMatthias Schwarzott 	*val = (u8)val_tmp;
1041b54da77SMatthias Schwarzott 	dev_dbg(&state->client->dev, "reg read: R(0x%04x)=0x%02x\n", reg, *val);
1053e54a169SMatthias Schwarzott 	return ret;
1063e54a169SMatthias Schwarzott }
1073e54a169SMatthias Schwarzott 
1083e54a169SMatthias Schwarzott static int si2165_readreg16(struct si2165_state *state,
1093e54a169SMatthias Schwarzott 			    const u16 reg, u16 *val)
1103e54a169SMatthias Schwarzott {
1113e54a169SMatthias Schwarzott 	u8 buf[2];
1123e54a169SMatthias Schwarzott 
1133e54a169SMatthias Schwarzott 	int ret = si2165_read(state, reg, buf, 2);
1143e54a169SMatthias Schwarzott 	*val = buf[0] | buf[1] << 8;
1151b54da77SMatthias Schwarzott 	dev_dbg(&state->client->dev, "reg read: R(0x%04x)=0x%04x\n", reg, *val);
1163e54a169SMatthias Schwarzott 	return ret;
1173e54a169SMatthias Schwarzott }
1183e54a169SMatthias Schwarzott 
1193e54a169SMatthias Schwarzott static int si2165_writereg8(struct si2165_state *state, const u16 reg, u8 val)
1203e54a169SMatthias Schwarzott {
121e3ea5e94SMatthias Schwarzott 	return regmap_write(state->regmap, reg, val);
1223e54a169SMatthias Schwarzott }
1233e54a169SMatthias Schwarzott 
1243e54a169SMatthias Schwarzott static int si2165_writereg16(struct si2165_state *state, const u16 reg, u16 val)
1253e54a169SMatthias Schwarzott {
1263e54a169SMatthias Schwarzott 	u8 buf[2] = { val & 0xff, (val >> 8) & 0xff };
1273e54a169SMatthias Schwarzott 
1283e54a169SMatthias Schwarzott 	return si2165_write(state, reg, buf, 2);
1293e54a169SMatthias Schwarzott }
1303e54a169SMatthias Schwarzott 
1313e54a169SMatthias Schwarzott static int si2165_writereg24(struct si2165_state *state, const u16 reg, u32 val)
1323e54a169SMatthias Schwarzott {
1333e54a169SMatthias Schwarzott 	u8 buf[3] = { val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff };
1343e54a169SMatthias Schwarzott 
1353e54a169SMatthias Schwarzott 	return si2165_write(state, reg, buf, 3);
1363e54a169SMatthias Schwarzott }
1373e54a169SMatthias Schwarzott 
1383e54a169SMatthias Schwarzott static int si2165_writereg32(struct si2165_state *state, const u16 reg, u32 val)
1393e54a169SMatthias Schwarzott {
1403e54a169SMatthias Schwarzott 	u8 buf[4] = {
1413e54a169SMatthias Schwarzott 		val & 0xff,
1423e54a169SMatthias Schwarzott 		(val >> 8) & 0xff,
1433e54a169SMatthias Schwarzott 		(val >> 16) & 0xff,
1443e54a169SMatthias Schwarzott 		(val >> 24) & 0xff
1453e54a169SMatthias Schwarzott 	};
1463e54a169SMatthias Schwarzott 	return si2165_write(state, reg, buf, 4);
1473e54a169SMatthias Schwarzott }
1483e54a169SMatthias Schwarzott 
1493e54a169SMatthias Schwarzott static int si2165_writereg_mask8(struct si2165_state *state, const u16 reg,
1503e54a169SMatthias Schwarzott 				 u8 val, u8 mask)
1513e54a169SMatthias Schwarzott {
1523e54a169SMatthias Schwarzott 	if (mask != 0xff) {
153c2e5c951SMarkus Elfring 		u8 tmp;
154c2e5c951SMarkus Elfring 		int ret = si2165_readreg8(state, reg, &tmp);
155c2e5c951SMarkus Elfring 
1563e54a169SMatthias Schwarzott 		if (ret < 0)
157c2e5c951SMarkus Elfring 			return ret;
1583e54a169SMatthias Schwarzott 
1593e54a169SMatthias Schwarzott 		val &= mask;
1603e54a169SMatthias Schwarzott 		tmp &= ~mask;
1613e54a169SMatthias Schwarzott 		val |= tmp;
1623e54a169SMatthias Schwarzott 	}
163c2e5c951SMarkus Elfring 	return si2165_writereg8(state, reg, val);
1643e54a169SMatthias Schwarzott }
1653e54a169SMatthias Schwarzott 
1667dbbb4bfSMatthias Schwarzott #define REG16(reg, val) \
1677dbbb4bfSMatthias Schwarzott 	{ (reg), (val) & 0xff }, \
1687dbbb4bfSMatthias Schwarzott 	{ (reg) + 1, (val) >> 8 & 0xff }
169a5293dbdSMatthias Schwarzott struct si2165_reg_value_pair {
170a5293dbdSMatthias Schwarzott 	u16 reg;
171a5293dbdSMatthias Schwarzott 	u8 val;
172a5293dbdSMatthias Schwarzott };
173a5293dbdSMatthias Schwarzott 
174a5293dbdSMatthias Schwarzott static int si2165_write_reg_list(struct si2165_state *state,
175a5293dbdSMatthias Schwarzott 				 const struct si2165_reg_value_pair *regs,
176a5293dbdSMatthias Schwarzott 				 int count)
177a5293dbdSMatthias Schwarzott {
178a5293dbdSMatthias Schwarzott 	int i;
179a5293dbdSMatthias Schwarzott 	int ret;
180a5293dbdSMatthias Schwarzott 
181a5293dbdSMatthias Schwarzott 	for (i = 0; i < count; i++) {
182a5293dbdSMatthias Schwarzott 		ret = si2165_writereg8(state, regs[i].reg, regs[i].val);
183a5293dbdSMatthias Schwarzott 		if (ret < 0)
184a5293dbdSMatthias Schwarzott 			return ret;
185a5293dbdSMatthias Schwarzott 	}
186a5293dbdSMatthias Schwarzott 	return 0;
187a5293dbdSMatthias Schwarzott }
188a5293dbdSMatthias Schwarzott 
1893e54a169SMatthias Schwarzott static int si2165_get_tune_settings(struct dvb_frontend *fe,
1903e54a169SMatthias Schwarzott 				    struct dvb_frontend_tune_settings *s)
1913e54a169SMatthias Schwarzott {
1923e54a169SMatthias Schwarzott 	s->min_delay_ms = 1000;
1933e54a169SMatthias Schwarzott 	return 0;
1943e54a169SMatthias Schwarzott }
1953e54a169SMatthias Schwarzott 
1963e54a169SMatthias Schwarzott static int si2165_init_pll(struct si2165_state *state)
1973e54a169SMatthias Schwarzott {
1987dbbb4bfSMatthias Schwarzott 	u32 ref_freq_hz = state->config.ref_freq_hz;
1993e54a169SMatthias Schwarzott 	u8 divr = 1; /* 1..7 */
2003e54a169SMatthias Schwarzott 	u8 divp = 1; /* only 1 or 4 */
2013e54a169SMatthias Schwarzott 	u8 divn = 56; /* 1..63 */
2023e54a169SMatthias Schwarzott 	u8 divm = 8;
2033e54a169SMatthias Schwarzott 	u8 divl = 12;
2043e54a169SMatthias Schwarzott 	u8 buf[4];
2053e54a169SMatthias Schwarzott 
20618349f40SMatthias Schwarzott 	/*
20718349f40SMatthias Schwarzott 	 * hardcoded values can be deleted if calculation is verified
20818349f40SMatthias Schwarzott 	 * or it yields the same values as the windows driver
20918349f40SMatthias Schwarzott 	 */
2107dbbb4bfSMatthias Schwarzott 	switch (ref_freq_hz) {
2113e54a169SMatthias Schwarzott 	case 16000000u:
2123e54a169SMatthias Schwarzott 		divn = 56;
2133e54a169SMatthias Schwarzott 		break;
2143e54a169SMatthias Schwarzott 	case 24000000u:
2153e54a169SMatthias Schwarzott 		divr = 2;
2163e54a169SMatthias Schwarzott 		divp = 4;
2173e54a169SMatthias Schwarzott 		divn = 19;
2183e54a169SMatthias Schwarzott 		break;
2193e54a169SMatthias Schwarzott 	default:
2203e54a169SMatthias Schwarzott 		/* ref_freq / divr must be between 4 and 16 MHz */
2217dbbb4bfSMatthias Schwarzott 		if (ref_freq_hz > 16000000u)
2223e54a169SMatthias Schwarzott 			divr = 2;
2233e54a169SMatthias Schwarzott 
22418349f40SMatthias Schwarzott 		/*
22518349f40SMatthias Schwarzott 		 * now select divn and divp such that
22618349f40SMatthias Schwarzott 		 * fvco is in 1624..1824 MHz
22718349f40SMatthias Schwarzott 		 */
2287dbbb4bfSMatthias Schwarzott 		if (1624000000u * divr > ref_freq_hz * 2u * 63u)
2293e54a169SMatthias Schwarzott 			divp = 4;
2303e54a169SMatthias Schwarzott 
2313e54a169SMatthias Schwarzott 		/* is this already correct regarding rounding? */
2327dbbb4bfSMatthias Schwarzott 		divn = 1624000000u * divr / (ref_freq_hz * 2u * divp);
2333e54a169SMatthias Schwarzott 		break;
2343e54a169SMatthias Schwarzott 	}
2353e54a169SMatthias Schwarzott 
2363e54a169SMatthias Schwarzott 	/* adc_clk and sys_clk depend on xtal and pll settings */
2377dbbb4bfSMatthias Schwarzott 	state->fvco_hz = ref_freq_hz / divr
2383e54a169SMatthias Schwarzott 			* 2u * divn * divp;
2393e54a169SMatthias Schwarzott 	state->adc_clk = state->fvco_hz / (divm * 4u);
2403e54a169SMatthias Schwarzott 	state->sys_clk = state->fvco_hz / (divl * 2u);
2413e54a169SMatthias Schwarzott 
242814f86c9SMatthias Schwarzott 	/* write all 4 pll registers 0x00a0..0x00a3 at once */
2433e54a169SMatthias Schwarzott 	buf[0] = divl;
2443e54a169SMatthias Schwarzott 	buf[1] = divm;
2453e54a169SMatthias Schwarzott 	buf[2] = (divn & 0x3f) | ((divp == 1) ? 0x40 : 0x00) | 0x80;
2463e54a169SMatthias Schwarzott 	buf[3] = divr;
247814f86c9SMatthias Schwarzott 	return si2165_write(state, REG_PLL_DIVL, buf, 4);
2483e54a169SMatthias Schwarzott }
2493e54a169SMatthias Schwarzott 
2503e54a169SMatthias Schwarzott static int si2165_adjust_pll_divl(struct si2165_state *state, u8 divl)
2513e54a169SMatthias Schwarzott {
2523e54a169SMatthias Schwarzott 	state->sys_clk = state->fvco_hz / (divl * 2u);
253814f86c9SMatthias Schwarzott 	return si2165_writereg8(state, REG_PLL_DIVL, divl);
2543e54a169SMatthias Schwarzott }
2553e54a169SMatthias Schwarzott 
2563e54a169SMatthias Schwarzott static u32 si2165_get_fe_clk(struct si2165_state *state)
2573e54a169SMatthias Schwarzott {
2583e54a169SMatthias Schwarzott 	/* assume Oversampling mode Ovr4 is used */
2593e54a169SMatthias Schwarzott 	return state->adc_clk;
2603e54a169SMatthias Schwarzott }
2613e54a169SMatthias Schwarzott 
262e73c7bfeSHans Verkuil static int si2165_wait_init_done(struct si2165_state *state)
2633e54a169SMatthias Schwarzott {
2643e54a169SMatthias Schwarzott 	int ret = -EINVAL;
2653e54a169SMatthias Schwarzott 	u8 val = 0;
2663e54a169SMatthias Schwarzott 	int i;
2673e54a169SMatthias Schwarzott 
2683e54a169SMatthias Schwarzott 	for (i = 0; i < 3; ++i) {
269814f86c9SMatthias Schwarzott 		si2165_readreg8(state, REG_INIT_DONE, &val);
2703e54a169SMatthias Schwarzott 		if (val == 0x01)
2713e54a169SMatthias Schwarzott 			return 0;
2723e54a169SMatthias Schwarzott 		usleep_range(1000, 50000);
2733e54a169SMatthias Schwarzott 	}
27477f887cdSMatthias Schwarzott 	dev_err(&state->client->dev, "init_done was not set\n");
2753e54a169SMatthias Schwarzott 	return ret;
2763e54a169SMatthias Schwarzott }
2773e54a169SMatthias Schwarzott 
2783e54a169SMatthias Schwarzott static int si2165_upload_firmware_block(struct si2165_state *state,
2797dbbb4bfSMatthias Schwarzott 					const u8 *data, u32 len, u32 *poffset,
2807dbbb4bfSMatthias Schwarzott 					u32 block_count)
2813e54a169SMatthias Schwarzott {
2823e54a169SMatthias Schwarzott 	int ret;
2833e54a169SMatthias Schwarzott 	u8 buf_ctrl[4] = { 0x00, 0x00, 0x00, 0xc0 };
2843e54a169SMatthias Schwarzott 	u8 wordcount;
2853e54a169SMatthias Schwarzott 	u32 cur_block = 0;
2863e54a169SMatthias Schwarzott 	u32 offset = poffset ? *poffset : 0;
2873e54a169SMatthias Schwarzott 
2883e54a169SMatthias Schwarzott 	if (len < 4)
2893e54a169SMatthias Schwarzott 		return -EINVAL;
2903e54a169SMatthias Schwarzott 	if (len % 4 != 0)
2913e54a169SMatthias Schwarzott 		return -EINVAL;
2923e54a169SMatthias Schwarzott 
2931b54da77SMatthias Schwarzott 	dev_dbg(&state->client->dev,
2947dbbb4bfSMatthias Schwarzott 		"fw load: %s: called with len=0x%x offset=0x%x blockcount=0x%x\n",
2957dbbb4bfSMatthias Schwarzott 		__func__, len, offset, block_count);
2963e54a169SMatthias Schwarzott 	while (offset + 12 <= len && cur_block < block_count) {
2971b54da77SMatthias Schwarzott 		dev_dbg(&state->client->dev,
2987dbbb4bfSMatthias Schwarzott 			"fw load: %s: in while len=0x%x offset=0x%x cur_block=0x%x blockcount=0x%x\n",
2997dbbb4bfSMatthias Schwarzott 			__func__, len, offset, cur_block, block_count);
3003e54a169SMatthias Schwarzott 		wordcount = data[offset];
3013e54a169SMatthias Schwarzott 		if (wordcount < 1 || data[offset + 1] ||
3023e54a169SMatthias Schwarzott 		    data[offset + 2] || data[offset + 3]) {
303aa155449SMatthias Schwarzott 			dev_warn(&state->client->dev,
30477f887cdSMatthias Schwarzott 				 "bad fw data[0..3] = %*ph\n",
30577f887cdSMatthias Schwarzott 				 4, data);
3063e54a169SMatthias Schwarzott 			return -EINVAL;
3073e54a169SMatthias Schwarzott 		}
3083e54a169SMatthias Schwarzott 
3093e54a169SMatthias Schwarzott 		if (offset + 8 + wordcount * 4 > len) {
310aa155449SMatthias Schwarzott 			dev_warn(&state->client->dev,
31177f887cdSMatthias Schwarzott 				 "len is too small for block len=%d, wordcount=%d\n",
31277f887cdSMatthias Schwarzott 				len, wordcount);
3133e54a169SMatthias Schwarzott 			return -EINVAL;
3143e54a169SMatthias Schwarzott 		}
3153e54a169SMatthias Schwarzott 
3163e54a169SMatthias Schwarzott 		buf_ctrl[0] = wordcount - 1;
3173e54a169SMatthias Schwarzott 
318814f86c9SMatthias Schwarzott 		ret = si2165_write(state, REG_DCOM_CONTROL_BYTE, buf_ctrl, 4);
3193e54a169SMatthias Schwarzott 		if (ret < 0)
3203e54a169SMatthias Schwarzott 			goto error;
321814f86c9SMatthias Schwarzott 		ret = si2165_write(state, REG_DCOM_ADDR, data + offset + 4, 4);
3223e54a169SMatthias Schwarzott 		if (ret < 0)
3233e54a169SMatthias Schwarzott 			goto error;
3243e54a169SMatthias Schwarzott 
3253e54a169SMatthias Schwarzott 		offset += 8;
3263e54a169SMatthias Schwarzott 
3273e54a169SMatthias Schwarzott 		while (wordcount > 0) {
328814f86c9SMatthias Schwarzott 			ret = si2165_write(state, REG_DCOM_DATA,
329814f86c9SMatthias Schwarzott 					   data + offset, 4);
3303e54a169SMatthias Schwarzott 			if (ret < 0)
3313e54a169SMatthias Schwarzott 				goto error;
3323e54a169SMatthias Schwarzott 			wordcount--;
3333e54a169SMatthias Schwarzott 			offset += 4;
3343e54a169SMatthias Schwarzott 		}
3353e54a169SMatthias Schwarzott 		cur_block++;
3363e54a169SMatthias Schwarzott 	}
3373e54a169SMatthias Schwarzott 
3381b54da77SMatthias Schwarzott 	dev_dbg(&state->client->dev,
3397dbbb4bfSMatthias Schwarzott 		"fw load: %s: after while len=0x%x offset=0x%x cur_block=0x%x blockcount=0x%x\n",
3407dbbb4bfSMatthias Schwarzott 		__func__, len, offset, cur_block, block_count);
3413e54a169SMatthias Schwarzott 
3423e54a169SMatthias Schwarzott 	if (poffset)
3433e54a169SMatthias Schwarzott 		*poffset = offset;
3443e54a169SMatthias Schwarzott 
3451b54da77SMatthias Schwarzott 	dev_dbg(&state->client->dev,
3467dbbb4bfSMatthias Schwarzott 		"fw load: %s: returned offset=0x%x\n",
3477dbbb4bfSMatthias Schwarzott 		__func__, offset);
3483e54a169SMatthias Schwarzott 
3493e54a169SMatthias Schwarzott 	return 0;
3503e54a169SMatthias Schwarzott error:
3513e54a169SMatthias Schwarzott 	return ret;
3523e54a169SMatthias Schwarzott }
3533e54a169SMatthias Schwarzott 
3543e54a169SMatthias Schwarzott static int si2165_upload_firmware(struct si2165_state *state)
3553e54a169SMatthias Schwarzott {
3563e54a169SMatthias Schwarzott 	/* int ret; */
3573e54a169SMatthias Schwarzott 	u8 val[3];
3583e54a169SMatthias Schwarzott 	u16 val16;
3593e54a169SMatthias Schwarzott 	int ret;
3603e54a169SMatthias Schwarzott 
3613e54a169SMatthias Schwarzott 	const struct firmware *fw = NULL;
36255bea400SMatthias Schwarzott 	u8 *fw_file;
3633e54a169SMatthias Schwarzott 	const u8 *data;
3643e54a169SMatthias Schwarzott 	u32 len;
3653e54a169SMatthias Schwarzott 	u32 offset;
3663e54a169SMatthias Schwarzott 	u8 patch_version;
3673e54a169SMatthias Schwarzott 	u8 block_count;
3683e54a169SMatthias Schwarzott 	u16 crc_expected;
3693e54a169SMatthias Schwarzott 
37055bea400SMatthias Schwarzott 	switch (state->chip_revcode) {
37155bea400SMatthias Schwarzott 	case 0x03: /* revision D */
37255bea400SMatthias Schwarzott 		fw_file = SI2165_FIRMWARE_REV_D;
37355bea400SMatthias Schwarzott 		break;
37455bea400SMatthias Schwarzott 	default:
37577f887cdSMatthias Schwarzott 		dev_info(&state->client->dev, "no firmware file for revision=%d\n",
37677f887cdSMatthias Schwarzott 			 state->chip_revcode);
37755bea400SMatthias Schwarzott 		return 0;
37855bea400SMatthias Schwarzott 	}
37955bea400SMatthias Schwarzott 
3803e54a169SMatthias Schwarzott 	/* request the firmware, this will block and timeout */
381aa155449SMatthias Schwarzott 	ret = request_firmware(&fw, fw_file, &state->client->dev);
3823e54a169SMatthias Schwarzott 	if (ret) {
38377f887cdSMatthias Schwarzott 		dev_warn(&state->client->dev, "firmware file '%s' not found\n",
38477f887cdSMatthias Schwarzott 			 fw_file);
3853e54a169SMatthias Schwarzott 		goto error;
3863e54a169SMatthias Schwarzott 	}
3873e54a169SMatthias Schwarzott 
3883e54a169SMatthias Schwarzott 	data = fw->data;
3893e54a169SMatthias Schwarzott 	len = fw->size;
3903e54a169SMatthias Schwarzott 
39177f887cdSMatthias Schwarzott 	dev_info(&state->client->dev, "downloading firmware from file '%s' size=%d\n",
39277f887cdSMatthias Schwarzott 		 fw_file, len);
3933e54a169SMatthias Schwarzott 
3943e54a169SMatthias Schwarzott 	if (len % 4 != 0) {
39577f887cdSMatthias Schwarzott 		dev_warn(&state->client->dev, "firmware size is not multiple of 4\n");
3963e54a169SMatthias Schwarzott 		ret = -EINVAL;
3973e54a169SMatthias Schwarzott 		goto error;
3983e54a169SMatthias Schwarzott 	}
3993e54a169SMatthias Schwarzott 
4003e54a169SMatthias Schwarzott 	/* check header (8 bytes) */
4013e54a169SMatthias Schwarzott 	if (len < 8) {
40277f887cdSMatthias Schwarzott 		dev_warn(&state->client->dev, "firmware header is missing\n");
4033e54a169SMatthias Schwarzott 		ret = -EINVAL;
4043e54a169SMatthias Schwarzott 		goto error;
4053e54a169SMatthias Schwarzott 	}
4063e54a169SMatthias Schwarzott 
4073e54a169SMatthias Schwarzott 	if (data[0] != 1 || data[1] != 0) {
40877f887cdSMatthias Schwarzott 		dev_warn(&state->client->dev, "firmware file version is wrong\n");
4093e54a169SMatthias Schwarzott 		ret = -EINVAL;
4103e54a169SMatthias Schwarzott 		goto error;
4113e54a169SMatthias Schwarzott 	}
4123e54a169SMatthias Schwarzott 
4133e54a169SMatthias Schwarzott 	patch_version = data[2];
4143e54a169SMatthias Schwarzott 	block_count = data[4];
4153e54a169SMatthias Schwarzott 	crc_expected = data[7] << 8 | data[6];
4163e54a169SMatthias Schwarzott 
4173e54a169SMatthias Schwarzott 	/* start uploading fw */
4183e54a169SMatthias Schwarzott 	/* boot/wdog status */
419814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_WDOG_AND_BOOT, 0x00);
4203e54a169SMatthias Schwarzott 	if (ret < 0)
4213e54a169SMatthias Schwarzott 		goto error;
4223e54a169SMatthias Schwarzott 	/* reset */
423814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_RST_ALL, 0x00);
4243e54a169SMatthias Schwarzott 	if (ret < 0)
4253e54a169SMatthias Schwarzott 		goto error;
4263e54a169SMatthias Schwarzott 	/* boot/wdog status */
427814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REG_WDOG_AND_BOOT, val);
4283e54a169SMatthias Schwarzott 	if (ret < 0)
4293e54a169SMatthias Schwarzott 		goto error;
4303e54a169SMatthias Schwarzott 
4313e54a169SMatthias Schwarzott 	/* enable reset on error */
432814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REG_EN_RST_ERROR, val);
4333e54a169SMatthias Schwarzott 	if (ret < 0)
4343e54a169SMatthias Schwarzott 		goto error;
435814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REG_EN_RST_ERROR, val);
4363e54a169SMatthias Schwarzott 	if (ret < 0)
4373e54a169SMatthias Schwarzott 		goto error;
438814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_EN_RST_ERROR, 0x02);
4393e54a169SMatthias Schwarzott 	if (ret < 0)
4403e54a169SMatthias Schwarzott 		goto error;
4413e54a169SMatthias Schwarzott 
4423e54a169SMatthias Schwarzott 	/* start right after the header */
4433e54a169SMatthias Schwarzott 	offset = 8;
4443e54a169SMatthias Schwarzott 
4457dbbb4bfSMatthias Schwarzott 	dev_info(&state->client->dev, "%s: extracted patch_version=0x%02x, block_count=0x%02x, crc_expected=0x%04x\n",
4467dbbb4bfSMatthias Schwarzott 		 __func__, patch_version, block_count, crc_expected);
4473e54a169SMatthias Schwarzott 
4483e54a169SMatthias Schwarzott 	ret = si2165_upload_firmware_block(state, data, len, &offset, 1);
4493e54a169SMatthias Schwarzott 	if (ret < 0)
4503e54a169SMatthias Schwarzott 		goto error;
4513e54a169SMatthias Schwarzott 
452814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_PATCH_VERSION, patch_version);
4533e54a169SMatthias Schwarzott 	if (ret < 0)
4543e54a169SMatthias Schwarzott 		goto error;
4553e54a169SMatthias Schwarzott 
4563e54a169SMatthias Schwarzott 	/* reset crc */
457814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_RST_CRC, 0x01);
4583e54a169SMatthias Schwarzott 	if (ret)
459ec73b9fdSChristian Engelmayer 		goto error;
4603e54a169SMatthias Schwarzott 
4613e54a169SMatthias Schwarzott 	ret = si2165_upload_firmware_block(state, data, len,
4623e54a169SMatthias Schwarzott 					   &offset, block_count);
4633e54a169SMatthias Schwarzott 	if (ret < 0) {
464aa155449SMatthias Schwarzott 		dev_err(&state->client->dev,
46577f887cdSMatthias Schwarzott 			"firmware could not be uploaded\n");
4663e54a169SMatthias Schwarzott 		goto error;
4673e54a169SMatthias Schwarzott 	}
4683e54a169SMatthias Schwarzott 
4693e54a169SMatthias Schwarzott 	/* read crc */
470814f86c9SMatthias Schwarzott 	ret = si2165_readreg16(state, REG_CRC, &val16);
4713e54a169SMatthias Schwarzott 	if (ret)
4723e54a169SMatthias Schwarzott 		goto error;
4733e54a169SMatthias Schwarzott 
4743e54a169SMatthias Schwarzott 	if (val16 != crc_expected) {
475aa155449SMatthias Schwarzott 		dev_err(&state->client->dev,
47677f887cdSMatthias Schwarzott 			"firmware crc mismatch %04x != %04x\n",
47777f887cdSMatthias Schwarzott 			val16, crc_expected);
4783e54a169SMatthias Schwarzott 		ret = -EINVAL;
4793e54a169SMatthias Schwarzott 		goto error;
4803e54a169SMatthias Schwarzott 	}
4813e54a169SMatthias Schwarzott 
4823e54a169SMatthias Schwarzott 	ret = si2165_upload_firmware_block(state, data, len, &offset, 5);
4833e54a169SMatthias Schwarzott 	if (ret)
4843e54a169SMatthias Schwarzott 		goto error;
4853e54a169SMatthias Schwarzott 
4863e54a169SMatthias Schwarzott 	if (len != offset) {
487aa155449SMatthias Schwarzott 		dev_err(&state->client->dev,
48877f887cdSMatthias Schwarzott 			"firmware len mismatch %04x != %04x\n",
48977f887cdSMatthias Schwarzott 			len, offset);
4903e54a169SMatthias Schwarzott 		ret = -EINVAL;
4913e54a169SMatthias Schwarzott 		goto error;
4923e54a169SMatthias Schwarzott 	}
4933e54a169SMatthias Schwarzott 
4943e54a169SMatthias Schwarzott 	/* reset watchdog error register */
495814f86c9SMatthias Schwarzott 	ret = si2165_writereg_mask8(state, REG_WDOG_AND_BOOT, 0x02, 0x02);
4963e54a169SMatthias Schwarzott 	if (ret < 0)
4973e54a169SMatthias Schwarzott 		goto error;
4983e54a169SMatthias Schwarzott 
4993e54a169SMatthias Schwarzott 	/* enable reset on error */
500814f86c9SMatthias Schwarzott 	ret = si2165_writereg_mask8(state, REG_EN_RST_ERROR, 0x01, 0x01);
5013e54a169SMatthias Schwarzott 	if (ret < 0)
5023e54a169SMatthias Schwarzott 		goto error;
5033e54a169SMatthias Schwarzott 
50477f887cdSMatthias Schwarzott 	dev_info(&state->client->dev, "fw load finished\n");
5053e54a169SMatthias Schwarzott 
5063e54a169SMatthias Schwarzott 	ret = 0;
5073e54a169SMatthias Schwarzott 	state->firmware_loaded = true;
5083e54a169SMatthias Schwarzott error:
5093e54a169SMatthias Schwarzott 	if (fw) {
5103e54a169SMatthias Schwarzott 		release_firmware(fw);
5113e54a169SMatthias Schwarzott 		fw = NULL;
5123e54a169SMatthias Schwarzott 	}
5133e54a169SMatthias Schwarzott 
5143e54a169SMatthias Schwarzott 	return ret;
5153e54a169SMatthias Schwarzott }
5163e54a169SMatthias Schwarzott 
5173e54a169SMatthias Schwarzott static int si2165_init(struct dvb_frontend *fe)
5183e54a169SMatthias Schwarzott {
5193e54a169SMatthias Schwarzott 	int ret = 0;
5203e54a169SMatthias Schwarzott 	struct si2165_state *state = fe->demodulator_priv;
5213e54a169SMatthias Schwarzott 	u8 val;
5223e54a169SMatthias Schwarzott 	u8 patch_version = 0x00;
5233e54a169SMatthias Schwarzott 
5241b54da77SMatthias Schwarzott 	dev_dbg(&state->client->dev, "%s: called\n", __func__);
5253e54a169SMatthias Schwarzott 
5263e54a169SMatthias Schwarzott 	/* powerup */
527814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_CHIP_MODE, state->config.chip_mode);
5283e54a169SMatthias Schwarzott 	if (ret < 0)
5293e54a169SMatthias Schwarzott 		goto error;
5303e54a169SMatthias Schwarzott 	/* dsp_clock_enable */
531814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_DSP_CLOCK, 0x01);
5323e54a169SMatthias Schwarzott 	if (ret < 0)
5333e54a169SMatthias Schwarzott 		goto error;
534814f86c9SMatthias Schwarzott 	/* verify chip_mode */
535814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REG_CHIP_MODE, &val);
5363e54a169SMatthias Schwarzott 	if (ret < 0)
5373e54a169SMatthias Schwarzott 		goto error;
5383e54a169SMatthias Schwarzott 	if (val != state->config.chip_mode) {
53977f887cdSMatthias Schwarzott 		dev_err(&state->client->dev, "could not set chip_mode\n");
5403e54a169SMatthias Schwarzott 		return -EINVAL;
5413e54a169SMatthias Schwarzott 	}
5423e54a169SMatthias Schwarzott 
5433e54a169SMatthias Schwarzott 	/* agc */
544814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_AGC_IF_TRI, 0x00);
5453e54a169SMatthias Schwarzott 	if (ret < 0)
5463e54a169SMatthias Schwarzott 		goto error;
547814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_AGC_IF_SLR, 0x01);
5483e54a169SMatthias Schwarzott 	if (ret < 0)
5493e54a169SMatthias Schwarzott 		goto error;
550814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_AGC2_OUTPUT, 0x00);
5513e54a169SMatthias Schwarzott 	if (ret < 0)
5523e54a169SMatthias Schwarzott 		goto error;
553814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_AGC2_CLKDIV, 0x07);
5543e54a169SMatthias Schwarzott 	if (ret < 0)
5553e54a169SMatthias Schwarzott 		goto error;
5563e54a169SMatthias Schwarzott 	/* rssi pad */
557814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_RSSI_PAD_CTRL, 0x00);
5583e54a169SMatthias Schwarzott 	if (ret < 0)
5593e54a169SMatthias Schwarzott 		goto error;
560814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_RSSI_ENABLE, 0x00);
5613e54a169SMatthias Schwarzott 	if (ret < 0)
5623e54a169SMatthias Schwarzott 		goto error;
5633e54a169SMatthias Schwarzott 
5643e54a169SMatthias Schwarzott 	ret = si2165_init_pll(state);
5653e54a169SMatthias Schwarzott 	if (ret < 0)
5663e54a169SMatthias Schwarzott 		goto error;
5673e54a169SMatthias Schwarzott 
5683e54a169SMatthias Schwarzott 	/* enable chip_init */
569814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_CHIP_INIT, 0x01);
5703e54a169SMatthias Schwarzott 	if (ret < 0)
5713e54a169SMatthias Schwarzott 		goto error;
5723e54a169SMatthias Schwarzott 	/* set start_init */
573814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_START_INIT, 0x01);
5743e54a169SMatthias Schwarzott 	if (ret < 0)
5753e54a169SMatthias Schwarzott 		goto error;
5763e54a169SMatthias Schwarzott 	ret = si2165_wait_init_done(state);
5773e54a169SMatthias Schwarzott 	if (ret < 0)
5783e54a169SMatthias Schwarzott 		goto error;
5793e54a169SMatthias Schwarzott 
5803e54a169SMatthias Schwarzott 	/* disable chip_init */
581814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_CHIP_INIT, 0x00);
5823e54a169SMatthias Schwarzott 	if (ret < 0)
5833e54a169SMatthias Schwarzott 		goto error;
5843e54a169SMatthias Schwarzott 
5853e54a169SMatthias Schwarzott 	/* ber_pkt */
586814f86c9SMatthias Schwarzott 	ret = si2165_writereg16(state, REG_BER_PKT, 0x7530);
5873e54a169SMatthias Schwarzott 	if (ret < 0)
5883e54a169SMatthias Schwarzott 		goto error;
5893e54a169SMatthias Schwarzott 
590814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REG_PATCH_VERSION, &patch_version);
5913e54a169SMatthias Schwarzott 	if (ret < 0)
5923e54a169SMatthias Schwarzott 		goto error;
5933e54a169SMatthias Schwarzott 
594814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_AUTO_RESET, 0x00);
5953e54a169SMatthias Schwarzott 	if (ret < 0)
5963e54a169SMatthias Schwarzott 		goto error;
5973e54a169SMatthias Schwarzott 
5983e54a169SMatthias Schwarzott 	/* dsp_addr_jump */
599814f86c9SMatthias Schwarzott 	ret = si2165_writereg32(state, REG_ADDR_JUMP, 0xf4000000);
6003e54a169SMatthias Schwarzott 	if (ret < 0)
6013e54a169SMatthias Schwarzott 		goto error;
6023e54a169SMatthias Schwarzott 	/* boot/wdog status */
603814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REG_WDOG_AND_BOOT, &val);
6043e54a169SMatthias Schwarzott 	if (ret < 0)
6053e54a169SMatthias Schwarzott 		goto error;
6063e54a169SMatthias Schwarzott 
6073e54a169SMatthias Schwarzott 	if (patch_version == 0x00) {
6083e54a169SMatthias Schwarzott 		ret = si2165_upload_firmware(state);
6093e54a169SMatthias Schwarzott 		if (ret < 0)
6103e54a169SMatthias Schwarzott 			goto error;
6113e54a169SMatthias Schwarzott 	}
6123e54a169SMatthias Schwarzott 
61375d62fc0SMatthias Schwarzott 	/* ts output config */
614814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_TS_DATA_MODE, 0x20);
61575d62fc0SMatthias Schwarzott 	if (ret < 0)
61675d62fc0SMatthias Schwarzott 		return ret;
617814f86c9SMatthias Schwarzott 	ret = si2165_writereg16(state, REG_TS_TRI, 0x00fe);
61875d62fc0SMatthias Schwarzott 	if (ret < 0)
61975d62fc0SMatthias Schwarzott 		return ret;
620814f86c9SMatthias Schwarzott 	ret = si2165_writereg24(state, REG_TS_SLR, 0x555555);
62175d62fc0SMatthias Schwarzott 	if (ret < 0)
62275d62fc0SMatthias Schwarzott 		return ret;
623814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_TS_CLK_MODE, 0x01);
62475d62fc0SMatthias Schwarzott 	if (ret < 0)
62575d62fc0SMatthias Schwarzott 		return ret;
626ec278f87SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_TS_PARALLEL_MODE, 0x00);
627ec278f87SMatthias Schwarzott 	if (ret < 0)
628ec278f87SMatthias Schwarzott 		return ret;
62975d62fc0SMatthias Schwarzott 
6303e54a169SMatthias Schwarzott 	return 0;
6313e54a169SMatthias Schwarzott error:
6323e54a169SMatthias Schwarzott 	return ret;
6333e54a169SMatthias Schwarzott }
6343e54a169SMatthias Schwarzott 
6353e54a169SMatthias Schwarzott static int si2165_sleep(struct dvb_frontend *fe)
6363e54a169SMatthias Schwarzott {
6373e54a169SMatthias Schwarzott 	int ret;
6383e54a169SMatthias Schwarzott 	struct si2165_state *state = fe->demodulator_priv;
6393e54a169SMatthias Schwarzott 
6403e54a169SMatthias Schwarzott 	/* dsp clock disable */
641814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_DSP_CLOCK, 0x00);
6423e54a169SMatthias Schwarzott 	if (ret < 0)
6433e54a169SMatthias Schwarzott 		return ret;
6443e54a169SMatthias Schwarzott 	/* chip mode */
645814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_CHIP_MODE, SI2165_MODE_OFF);
6463e54a169SMatthias Schwarzott 	if (ret < 0)
6473e54a169SMatthias Schwarzott 		return ret;
6483e54a169SMatthias Schwarzott 	return 0;
6493e54a169SMatthias Schwarzott }
6503e54a169SMatthias Schwarzott 
6510df289a2SMauro Carvalho Chehab static int si2165_read_status(struct dvb_frontend *fe, enum fe_status *status)
6523e54a169SMatthias Schwarzott {
6533e54a169SMatthias Schwarzott 	int ret;
6543e54a169SMatthias Schwarzott 	u8 fec_lock = 0;
6553e54a169SMatthias Schwarzott 	struct si2165_state *state = fe->demodulator_priv;
6563e54a169SMatthias Schwarzott 
6573e54a169SMatthias Schwarzott 	if (!state->has_dvbt)
6583e54a169SMatthias Schwarzott 		return -EINVAL;
6593e54a169SMatthias Schwarzott 
6603e54a169SMatthias Schwarzott 	/* check fec_lock */
661814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REG_FEC_LOCK, &fec_lock);
6623e54a169SMatthias Schwarzott 	if (ret < 0)
6633e54a169SMatthias Schwarzott 		return ret;
6643e54a169SMatthias Schwarzott 	*status = 0;
6653e54a169SMatthias Schwarzott 	if (fec_lock & 0x01) {
6663e54a169SMatthias Schwarzott 		*status |= FE_HAS_SIGNAL;
6673e54a169SMatthias Schwarzott 		*status |= FE_HAS_CARRIER;
6683e54a169SMatthias Schwarzott 		*status |= FE_HAS_VITERBI;
6693e54a169SMatthias Schwarzott 		*status |= FE_HAS_SYNC;
6703e54a169SMatthias Schwarzott 		*status |= FE_HAS_LOCK;
6713e54a169SMatthias Schwarzott 	}
6723e54a169SMatthias Schwarzott 
6733e54a169SMatthias Schwarzott 	return 0;
6743e54a169SMatthias Schwarzott }
6753e54a169SMatthias Schwarzott 
6763e54a169SMatthias Schwarzott static int si2165_set_oversamp(struct si2165_state *state, u32 dvb_rate)
6773e54a169SMatthias Schwarzott {
6783e54a169SMatthias Schwarzott 	u64 oversamp;
6793e54a169SMatthias Schwarzott 	u32 reg_value;
6803e54a169SMatthias Schwarzott 
6812df9dda0SMatthias Schwarzott 	if (!dvb_rate)
6822df9dda0SMatthias Schwarzott 		return -EINVAL;
6832df9dda0SMatthias Schwarzott 
6843e54a169SMatthias Schwarzott 	oversamp = si2165_get_fe_clk(state);
6853e54a169SMatthias Schwarzott 	oversamp <<= 23;
6863e54a169SMatthias Schwarzott 	do_div(oversamp, dvb_rate);
6873e54a169SMatthias Schwarzott 	reg_value = oversamp & 0x3fffffff;
6883e54a169SMatthias Schwarzott 
6891b54da77SMatthias Schwarzott 	dev_dbg(&state->client->dev, "Write oversamp=%#x\n", reg_value);
690814f86c9SMatthias Schwarzott 	return si2165_writereg32(state, REG_OVERSAMP, reg_value);
6913e54a169SMatthias Schwarzott }
6923e54a169SMatthias Schwarzott 
693542fb3c5SMatthias Schwarzott static int si2165_set_if_freq_shift(struct si2165_state *state)
6943e54a169SMatthias Schwarzott {
695542fb3c5SMatthias Schwarzott 	struct dvb_frontend *fe = &state->fe;
6963e54a169SMatthias Schwarzott 	u64 if_freq_shift;
6973e54a169SMatthias Schwarzott 	s32 reg_value = 0;
6983e54a169SMatthias Schwarzott 	u32 fe_clk = si2165_get_fe_clk(state);
699542fb3c5SMatthias Schwarzott 	u32 IF = 0;
7003e54a169SMatthias Schwarzott 
701542fb3c5SMatthias Schwarzott 	if (!fe->ops.tuner_ops.get_if_frequency) {
702aa155449SMatthias Schwarzott 		dev_err(&state->client->dev,
70377f887cdSMatthias Schwarzott 			"Error: get_if_frequency() not defined at tuner. Can't work without it!\n");
704542fb3c5SMatthias Schwarzott 		return -EINVAL;
705542fb3c5SMatthias Schwarzott 	}
706542fb3c5SMatthias Schwarzott 
7072df9dda0SMatthias Schwarzott 	if (!fe_clk)
7082df9dda0SMatthias Schwarzott 		return -EINVAL;
7092df9dda0SMatthias Schwarzott 
710542fb3c5SMatthias Schwarzott 	fe->ops.tuner_ops.get_if_frequency(fe, &IF);
7113e54a169SMatthias Schwarzott 	if_freq_shift = IF;
7123e54a169SMatthias Schwarzott 	if_freq_shift <<= 29;
7133e54a169SMatthias Schwarzott 
7143e54a169SMatthias Schwarzott 	do_div(if_freq_shift, fe_clk);
7153e54a169SMatthias Schwarzott 	reg_value = (s32)if_freq_shift;
7163e54a169SMatthias Schwarzott 
7173e54a169SMatthias Schwarzott 	if (state->config.inversion)
7183e54a169SMatthias Schwarzott 		reg_value = -reg_value;
7193e54a169SMatthias Schwarzott 
7203e54a169SMatthias Schwarzott 	reg_value = reg_value & 0x1fffffff;
7213e54a169SMatthias Schwarzott 
7223e54a169SMatthias Schwarzott 	/* if_freq_shift, usbdump contained 0x023ee08f; */
723814f86c9SMatthias Schwarzott 	return si2165_writereg32(state, REG_IF_FREQ_SHIFT, reg_value);
7243e54a169SMatthias Schwarzott }
7253e54a169SMatthias Schwarzott 
72625e73753SMatthias Schwarzott static const struct si2165_reg_value_pair dvbt_regs[] = {
72725e73753SMatthias Schwarzott 	/* standard = DVB-T */
728814f86c9SMatthias Schwarzott 	{ REG_DVB_STANDARD, 0x01 },
72925e73753SMatthias Schwarzott 	/* impulsive_noise_remover */
730814f86c9SMatthias Schwarzott 	{ REG_IMPULSIVE_NOISE_REM, 0x01 },
731814f86c9SMatthias Schwarzott 	{ REG_AUTO_RESET, 0x00 },
73225e73753SMatthias Schwarzott 	/* agc2 */
733814f86c9SMatthias Schwarzott 	{ REG_AGC2_MIN, 0x41 },
734814f86c9SMatthias Schwarzott 	{ REG_AGC2_KACQ, 0x0e },
735814f86c9SMatthias Schwarzott 	{ REG_AGC2_KLOC, 0x10 },
73625e73753SMatthias Schwarzott 	/* agc */
737814f86c9SMatthias Schwarzott 	{ REG_AGC_UNFREEZE_THR, 0x03 },
738814f86c9SMatthias Schwarzott 	{ REG_AGC_CRESTF_DBX8, 0x78 },
73925e73753SMatthias Schwarzott 	/* agc */
740814f86c9SMatthias Schwarzott 	{ REG_AAF_CRESTF_DBX8, 0x78 },
741814f86c9SMatthias Schwarzott 	{ REG_ACI_CRESTF_DBX8, 0x68 },
74225e73753SMatthias Schwarzott 	/* freq_sync_range */
743814f86c9SMatthias Schwarzott 	REG16(REG_FREQ_SYNC_RANGE, 0x0064),
74425e73753SMatthias Schwarzott 	/* gp_reg0 */
745814f86c9SMatthias Schwarzott 	{ REG_GP_REG0_MSB, 0x00 }
74625e73753SMatthias Schwarzott };
74725e73753SMatthias Schwarzott 
7483b0c9807SMatthias Schwarzott static int si2165_set_frontend_dvbt(struct dvb_frontend *fe)
7493e54a169SMatthias Schwarzott {
7503e54a169SMatthias Schwarzott 	int ret;
7513e54a169SMatthias Schwarzott 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
7523e54a169SMatthias Schwarzott 	struct si2165_state *state = fe->demodulator_priv;
7533e54a169SMatthias Schwarzott 	u32 dvb_rate = 0;
7543e54a169SMatthias Schwarzott 	u16 bw10k;
7557655a3aeSMatthias Schwarzott 	u32 bw_hz = p->bandwidth_hz;
7563e54a169SMatthias Schwarzott 
7571b54da77SMatthias Schwarzott 	dev_dbg(&state->client->dev, "%s: called\n", __func__);
7583e54a169SMatthias Schwarzott 
7593e54a169SMatthias Schwarzott 	if (!state->has_dvbt)
7603e54a169SMatthias Schwarzott 		return -EINVAL;
7613e54a169SMatthias Schwarzott 
7627655a3aeSMatthias Schwarzott 	/* no bandwidth auto-detection */
7637655a3aeSMatthias Schwarzott 	if (bw_hz == 0)
7647655a3aeSMatthias Schwarzott 		return -EINVAL;
7657655a3aeSMatthias Schwarzott 
7667655a3aeSMatthias Schwarzott 	dvb_rate = bw_hz * 8 / 7;
7677655a3aeSMatthias Schwarzott 	bw10k = bw_hz / 10000;
7683e54a169SMatthias Schwarzott 
7693e54a169SMatthias Schwarzott 	ret = si2165_adjust_pll_divl(state, 12);
7703e54a169SMatthias Schwarzott 	if (ret < 0)
7713e54a169SMatthias Schwarzott 		return ret;
7723e54a169SMatthias Schwarzott 
7733e54a169SMatthias Schwarzott 	/* bandwidth in 10KHz steps */
774814f86c9SMatthias Schwarzott 	ret = si2165_writereg16(state, REG_T_BANDWIDTH, bw10k);
7753e54a169SMatthias Schwarzott 	if (ret < 0)
7763e54a169SMatthias Schwarzott 		return ret;
7773e54a169SMatthias Schwarzott 	ret = si2165_set_oversamp(state, dvb_rate);
7783e54a169SMatthias Schwarzott 	if (ret < 0)
7793e54a169SMatthias Schwarzott 		return ret;
78025e73753SMatthias Schwarzott 
78125e73753SMatthias Schwarzott 	ret = si2165_write_reg_list(state, dvbt_regs, ARRAY_SIZE(dvbt_regs));
7823e54a169SMatthias Schwarzott 	if (ret < 0)
7833e54a169SMatthias Schwarzott 		return ret;
78425e73753SMatthias Schwarzott 
7853b0c9807SMatthias Schwarzott 	return 0;
7863b0c9807SMatthias Schwarzott }
7873b0c9807SMatthias Schwarzott 
78894c17334SMatthias Schwarzott static const struct si2165_reg_value_pair dvbc_regs[] = {
78994c17334SMatthias Schwarzott 	/* standard = DVB-C */
790814f86c9SMatthias Schwarzott 	{ REG_DVB_STANDARD, 0x05 },
79194c17334SMatthias Schwarzott 
79294c17334SMatthias Schwarzott 	/* agc2 */
793814f86c9SMatthias Schwarzott 	{ REG_AGC2_MIN, 0x50 },
794814f86c9SMatthias Schwarzott 	{ REG_AGC2_KACQ, 0x0e },
795814f86c9SMatthias Schwarzott 	{ REG_AGC2_KLOC, 0x10 },
79694c17334SMatthias Schwarzott 	/* agc */
797814f86c9SMatthias Schwarzott 	{ REG_AGC_UNFREEZE_THR, 0x03 },
798814f86c9SMatthias Schwarzott 	{ REG_AGC_CRESTF_DBX8, 0x68 },
79994c17334SMatthias Schwarzott 	/* agc */
800814f86c9SMatthias Schwarzott 	{ REG_AAF_CRESTF_DBX8, 0x68 },
801814f86c9SMatthias Schwarzott 	{ REG_ACI_CRESTF_DBX8, 0x50 },
80294c17334SMatthias Schwarzott 
803814f86c9SMatthias Schwarzott 	{ REG_EQ_AUTO_CONTROL, 0x0d },
80494c17334SMatthias Schwarzott 
805814f86c9SMatthias Schwarzott 	{ REG_KP_LOCK, 0x05 },
806814f86c9SMatthias Schwarzott 	{ REG_CENTRAL_TAP, 0x09 },
807814f86c9SMatthias Schwarzott 	REG16(REG_UNKNOWN_350, 0x3e80),
80894c17334SMatthias Schwarzott 
809814f86c9SMatthias Schwarzott 	{ REG_AUTO_RESET, 0x01 },
810814f86c9SMatthias Schwarzott 	REG16(REG_UNKNOWN_24C, 0x0000),
811814f86c9SMatthias Schwarzott 	REG16(REG_UNKNOWN_27C, 0x0000),
812814f86c9SMatthias Schwarzott 	{ REG_SWEEP_STEP, 0x03 },
813814f86c9SMatthias Schwarzott 	{ REG_AGC_IF_TRI, 0x00 },
81494c17334SMatthias Schwarzott };
81594c17334SMatthias Schwarzott 
81694c17334SMatthias Schwarzott static int si2165_set_frontend_dvbc(struct dvb_frontend *fe)
81794c17334SMatthias Schwarzott {
81894c17334SMatthias Schwarzott 	struct si2165_state *state = fe->demodulator_priv;
81994c17334SMatthias Schwarzott 	int ret;
82094c17334SMatthias Schwarzott 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
82194c17334SMatthias Schwarzott 	const u32 dvb_rate = p->symbol_rate;
822548b1f94SMatthias Schwarzott 	u8 u8tmp;
82394c17334SMatthias Schwarzott 
82494c17334SMatthias Schwarzott 	if (!state->has_dvbc)
82594c17334SMatthias Schwarzott 		return -EINVAL;
82694c17334SMatthias Schwarzott 
82794c17334SMatthias Schwarzott 	if (dvb_rate == 0)
82894c17334SMatthias Schwarzott 		return -EINVAL;
82994c17334SMatthias Schwarzott 
83094c17334SMatthias Schwarzott 	ret = si2165_adjust_pll_divl(state, 14);
83194c17334SMatthias Schwarzott 	if (ret < 0)
83294c17334SMatthias Schwarzott 		return ret;
83394c17334SMatthias Schwarzott 
83494c17334SMatthias Schwarzott 	/* Oversampling */
83594c17334SMatthias Schwarzott 	ret = si2165_set_oversamp(state, dvb_rate);
83694c17334SMatthias Schwarzott 	if (ret < 0)
83794c17334SMatthias Schwarzott 		return ret;
83894c17334SMatthias Schwarzott 
839548b1f94SMatthias Schwarzott 	switch (p->modulation) {
840548b1f94SMatthias Schwarzott 	case QPSK:
841548b1f94SMatthias Schwarzott 		u8tmp = 0x3;
842548b1f94SMatthias Schwarzott 		break;
843548b1f94SMatthias Schwarzott 	case QAM_16:
844548b1f94SMatthias Schwarzott 		u8tmp = 0x7;
845548b1f94SMatthias Schwarzott 		break;
846548b1f94SMatthias Schwarzott 	case QAM_32:
847548b1f94SMatthias Schwarzott 		u8tmp = 0x8;
848548b1f94SMatthias Schwarzott 		break;
849548b1f94SMatthias Schwarzott 	case QAM_64:
850548b1f94SMatthias Schwarzott 		u8tmp = 0x9;
851548b1f94SMatthias Schwarzott 		break;
852548b1f94SMatthias Schwarzott 	case QAM_128:
853548b1f94SMatthias Schwarzott 		u8tmp = 0xa;
854548b1f94SMatthias Schwarzott 		break;
855548b1f94SMatthias Schwarzott 	case QAM_256:
856548b1f94SMatthias Schwarzott 	default:
857548b1f94SMatthias Schwarzott 		u8tmp = 0xb;
858548b1f94SMatthias Schwarzott 		break;
859548b1f94SMatthias Schwarzott 	}
860548b1f94SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_REQ_CONSTELLATION, u8tmp);
861548b1f94SMatthias Schwarzott 	if (ret < 0)
862548b1f94SMatthias Schwarzott 		return ret;
863548b1f94SMatthias Schwarzott 
864f4d90518SMatthias Schwarzott 	ret = si2165_writereg32(state, REG_LOCK_TIMEOUT, 0x007a1200);
86594c17334SMatthias Schwarzott 	if (ret < 0)
86694c17334SMatthias Schwarzott 		return ret;
86794c17334SMatthias Schwarzott 
86894c17334SMatthias Schwarzott 	ret = si2165_write_reg_list(state, dvbc_regs, ARRAY_SIZE(dvbc_regs));
86994c17334SMatthias Schwarzott 	if (ret < 0)
87094c17334SMatthias Schwarzott 		return ret;
87194c17334SMatthias Schwarzott 
87294c17334SMatthias Schwarzott 	return 0;
87394c17334SMatthias Schwarzott }
87494c17334SMatthias Schwarzott 
875814f86c9SMatthias Schwarzott static const struct si2165_reg_value_pair adc_rewrite[] = {
876814f86c9SMatthias Schwarzott 	{ REG_ADC_RI1, 0x46 },
877814f86c9SMatthias Schwarzott 	{ REG_ADC_RI3, 0x00 },
878814f86c9SMatthias Schwarzott 	{ REG_ADC_RI5, 0x0a },
879814f86c9SMatthias Schwarzott 	{ REG_ADC_RI6, 0xff },
880814f86c9SMatthias Schwarzott 	{ REG_ADC_RI8, 0x70 }
8813b0c9807SMatthias Schwarzott };
8823b0c9807SMatthias Schwarzott 
8833b0c9807SMatthias Schwarzott static int si2165_set_frontend(struct dvb_frontend *fe)
8843b0c9807SMatthias Schwarzott {
8853b0c9807SMatthias Schwarzott 	struct si2165_state *state = fe->demodulator_priv;
8863b0c9807SMatthias Schwarzott 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
8873b0c9807SMatthias Schwarzott 	u32 delsys = p->delivery_system;
8883b0c9807SMatthias Schwarzott 	int ret;
8893b0c9807SMatthias Schwarzott 	u8 val[3];
8903b0c9807SMatthias Schwarzott 
8913b0c9807SMatthias Schwarzott 	/* initial setting of if freq shift */
8923b0c9807SMatthias Schwarzott 	ret = si2165_set_if_freq_shift(state);
8933b0c9807SMatthias Schwarzott 	if (ret < 0)
8943b0c9807SMatthias Schwarzott 		return ret;
8953b0c9807SMatthias Schwarzott 
8963b0c9807SMatthias Schwarzott 	switch (delsys) {
8973b0c9807SMatthias Schwarzott 	case SYS_DVBT:
8983b0c9807SMatthias Schwarzott 		ret = si2165_set_frontend_dvbt(fe);
8993b0c9807SMatthias Schwarzott 		if (ret < 0)
9003b0c9807SMatthias Schwarzott 			return ret;
9013b0c9807SMatthias Schwarzott 		break;
90294c17334SMatthias Schwarzott 	case SYS_DVBC_ANNEX_A:
90394c17334SMatthias Schwarzott 		ret = si2165_set_frontend_dvbc(fe);
90494c17334SMatthias Schwarzott 		if (ret < 0)
90594c17334SMatthias Schwarzott 			return ret;
90694c17334SMatthias Schwarzott 		break;
9073b0c9807SMatthias Schwarzott 	default:
9083b0c9807SMatthias Schwarzott 		return -EINVAL;
9093b0c9807SMatthias Schwarzott 	}
9103b0c9807SMatthias Schwarzott 
9113e54a169SMatthias Schwarzott 	/* dsp_addr_jump */
912814f86c9SMatthias Schwarzott 	ret = si2165_writereg32(state, REG_ADDR_JUMP, 0xf4000000);
9133e54a169SMatthias Schwarzott 	if (ret < 0)
9143e54a169SMatthias Schwarzott 		return ret;
9153e54a169SMatthias Schwarzott 
9163e54a169SMatthias Schwarzott 	if (fe->ops.tuner_ops.set_params)
9173e54a169SMatthias Schwarzott 		fe->ops.tuner_ops.set_params(fe);
9183e54a169SMatthias Schwarzott 
9193e54a169SMatthias Schwarzott 	/* recalc if_freq_shift if IF might has changed */
920542fb3c5SMatthias Schwarzott 	ret = si2165_set_if_freq_shift(state);
9213e54a169SMatthias Schwarzott 	if (ret < 0)
9223e54a169SMatthias Schwarzott 		return ret;
9233e54a169SMatthias Schwarzott 
9243e54a169SMatthias Schwarzott 	/* boot/wdog status */
925814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REG_WDOG_AND_BOOT, val);
9263e54a169SMatthias Schwarzott 	if (ret < 0)
9273e54a169SMatthias Schwarzott 		return ret;
928814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_WDOG_AND_BOOT, 0x00);
9293e54a169SMatthias Schwarzott 	if (ret < 0)
9303e54a169SMatthias Schwarzott 		return ret;
9313b0c9807SMatthias Schwarzott 
9323e54a169SMatthias Schwarzott 	/* reset all */
933814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_RST_ALL, 0x00);
9343e54a169SMatthias Schwarzott 	if (ret < 0)
9353e54a169SMatthias Schwarzott 		return ret;
9363e54a169SMatthias Schwarzott 	/* gp_reg0 */
937814f86c9SMatthias Schwarzott 	ret = si2165_writereg32(state, REG_GP_REG0_LSB, 0x00000000);
9383e54a169SMatthias Schwarzott 	if (ret < 0)
9393e54a169SMatthias Schwarzott 		return ret;
940eae56684SMatthias Schwarzott 
941eae56684SMatthias Schwarzott 	/* write adc values after each reset*/
942814f86c9SMatthias Schwarzott 	ret = si2165_write_reg_list(state, adc_rewrite,
943814f86c9SMatthias Schwarzott 				    ARRAY_SIZE(adc_rewrite));
944eae56684SMatthias Schwarzott 	if (ret < 0)
945eae56684SMatthias Schwarzott 		return ret;
946eae56684SMatthias Schwarzott 
9473e54a169SMatthias Schwarzott 	/* start_synchro */
948814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_START_SYNCHRO, 0x01);
9493e54a169SMatthias Schwarzott 	if (ret < 0)
9503e54a169SMatthias Schwarzott 		return ret;
9513e54a169SMatthias Schwarzott 	/* boot/wdog status */
952814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REG_WDOG_AND_BOOT, val);
9533e54a169SMatthias Schwarzott 	if (ret < 0)
9543e54a169SMatthias Schwarzott 		return ret;
9553e54a169SMatthias Schwarzott 
9563e54a169SMatthias Schwarzott 	return 0;
9573e54a169SMatthias Schwarzott }
9583e54a169SMatthias Schwarzott 
959bd336e63SMax Kellermann static const struct dvb_frontend_ops si2165_ops = {
9603e54a169SMatthias Schwarzott 	.info = {
961119bd82eSMatthias Schwarzott 		.name = "Silicon Labs ",
96294c17334SMatthias Schwarzott 		 /* For DVB-C */
96394c17334SMatthias Schwarzott 		.symbol_rate_min = 1000000,
96494c17334SMatthias Schwarzott 		.symbol_rate_max = 7200000,
96594c17334SMatthias Schwarzott 		/* For DVB-T */
96694c17334SMatthias Schwarzott 		.frequency_stepsize = 166667,
9673e54a169SMatthias Schwarzott 		.caps = FE_CAN_FEC_1_2 |
9683e54a169SMatthias Schwarzott 			FE_CAN_FEC_2_3 |
9693e54a169SMatthias Schwarzott 			FE_CAN_FEC_3_4 |
9703e54a169SMatthias Schwarzott 			FE_CAN_FEC_5_6 |
9713e54a169SMatthias Schwarzott 			FE_CAN_FEC_7_8 |
9723e54a169SMatthias Schwarzott 			FE_CAN_FEC_AUTO |
9733e54a169SMatthias Schwarzott 			FE_CAN_QPSK |
9743e54a169SMatthias Schwarzott 			FE_CAN_QAM_16 |
9753e54a169SMatthias Schwarzott 			FE_CAN_QAM_32 |
9763e54a169SMatthias Schwarzott 			FE_CAN_QAM_64 |
9773e54a169SMatthias Schwarzott 			FE_CAN_QAM_128 |
9783e54a169SMatthias Schwarzott 			FE_CAN_QAM_256 |
9793e54a169SMatthias Schwarzott 			FE_CAN_GUARD_INTERVAL_AUTO |
9803e54a169SMatthias Schwarzott 			FE_CAN_HIERARCHY_AUTO |
9813e54a169SMatthias Schwarzott 			FE_CAN_MUTE_TS |
9823e54a169SMatthias Schwarzott 			FE_CAN_TRANSMISSION_MODE_AUTO |
9833e54a169SMatthias Schwarzott 			FE_CAN_RECOVER
9843e54a169SMatthias Schwarzott 	},
9853e54a169SMatthias Schwarzott 
9863e54a169SMatthias Schwarzott 	.get_tune_settings = si2165_get_tune_settings,
9873e54a169SMatthias Schwarzott 
9883e54a169SMatthias Schwarzott 	.init = si2165_init,
9893e54a169SMatthias Schwarzott 	.sleep = si2165_sleep,
9903e54a169SMatthias Schwarzott 
991c1c49674SMatthias Schwarzott 	.set_frontend      = si2165_set_frontend,
9923e54a169SMatthias Schwarzott 	.read_status       = si2165_read_status,
9933e54a169SMatthias Schwarzott };
9943e54a169SMatthias Schwarzott 
9957cd785adSMatthias Schwarzott static int si2165_probe(struct i2c_client *client,
9967cd785adSMatthias Schwarzott 			const struct i2c_device_id *id)
9977cd785adSMatthias Schwarzott {
9987cd785adSMatthias Schwarzott 	struct si2165_state *state = NULL;
9997cd785adSMatthias Schwarzott 	struct si2165_platform_data *pdata = client->dev.platform_data;
10007cd785adSMatthias Schwarzott 	int n;
10017cd785adSMatthias Schwarzott 	int ret = 0;
10027cd785adSMatthias Schwarzott 	u8 val;
10037cd785adSMatthias Schwarzott 	char rev_char;
10047cd785adSMatthias Schwarzott 	const char *chip_name;
1005e3ea5e94SMatthias Schwarzott 	static const struct regmap_config regmap_config = {
1006e3ea5e94SMatthias Schwarzott 		.reg_bits = 16,
1007e3ea5e94SMatthias Schwarzott 		.val_bits = 8,
1008e3ea5e94SMatthias Schwarzott 		.max_register = 0x08ff,
1009e3ea5e94SMatthias Schwarzott 	};
10107cd785adSMatthias Schwarzott 
10117cd785adSMatthias Schwarzott 	/* allocate memory for the internal state */
10127dbbb4bfSMatthias Schwarzott 	state = kzalloc(sizeof(*state), GFP_KERNEL);
10137dbbb4bfSMatthias Schwarzott 	if (!state) {
10147cd785adSMatthias Schwarzott 		ret = -ENOMEM;
10157cd785adSMatthias Schwarzott 		goto error;
10167cd785adSMatthias Schwarzott 	}
10177cd785adSMatthias Schwarzott 
1018e3ea5e94SMatthias Schwarzott 	/* create regmap */
1019e3ea5e94SMatthias Schwarzott 	state->regmap = devm_regmap_init_i2c(client, &regmap_config);
1020e3ea5e94SMatthias Schwarzott 	if (IS_ERR(state->regmap)) {
1021e3ea5e94SMatthias Schwarzott 		ret = PTR_ERR(state->regmap);
1022e3ea5e94SMatthias Schwarzott 		goto error;
1023e3ea5e94SMatthias Schwarzott 	}
1024e3ea5e94SMatthias Schwarzott 
10257cd785adSMatthias Schwarzott 	/* setup the state */
10267cd785adSMatthias Schwarzott 	state->client = client;
10277cd785adSMatthias Schwarzott 	state->config.i2c_addr = client->addr;
10287cd785adSMatthias Schwarzott 	state->config.chip_mode = pdata->chip_mode;
10297dbbb4bfSMatthias Schwarzott 	state->config.ref_freq_hz = pdata->ref_freq_hz;
10307cd785adSMatthias Schwarzott 	state->config.inversion = pdata->inversion;
10317cd785adSMatthias Schwarzott 
10327dbbb4bfSMatthias Schwarzott 	if (state->config.ref_freq_hz < 4000000 ||
10337dbbb4bfSMatthias Schwarzott 	    state->config.ref_freq_hz > 27000000) {
103477f887cdSMatthias Schwarzott 		dev_err(&state->client->dev, "ref_freq of %d Hz not supported by this driver\n",
10357dbbb4bfSMatthias Schwarzott 			state->config.ref_freq_hz);
10367cd785adSMatthias Schwarzott 		ret = -EINVAL;
10377cd785adSMatthias Schwarzott 		goto error;
10387cd785adSMatthias Schwarzott 	}
10397cd785adSMatthias Schwarzott 
10407cd785adSMatthias Schwarzott 	/* create dvb_frontend */
10417cd785adSMatthias Schwarzott 	memcpy(&state->fe.ops, &si2165_ops,
10427cd785adSMatthias Schwarzott 	       sizeof(struct dvb_frontend_ops));
10437cd785adSMatthias Schwarzott 	state->fe.ops.release = NULL;
10447cd785adSMatthias Schwarzott 	state->fe.demodulator_priv = state;
10457cd785adSMatthias Schwarzott 	i2c_set_clientdata(client, state);
10467cd785adSMatthias Schwarzott 
10477cd785adSMatthias Schwarzott 	/* powerup */
1048814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_CHIP_MODE, state->config.chip_mode);
10497cd785adSMatthias Schwarzott 	if (ret < 0)
10507cd785adSMatthias Schwarzott 		goto nodev_error;
10517cd785adSMatthias Schwarzott 
1052814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REG_CHIP_MODE, &val);
10537cd785adSMatthias Schwarzott 	if (ret < 0)
10547cd785adSMatthias Schwarzott 		goto nodev_error;
10557cd785adSMatthias Schwarzott 	if (val != state->config.chip_mode)
10567cd785adSMatthias Schwarzott 		goto nodev_error;
10577cd785adSMatthias Schwarzott 
1058814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REG_CHIP_REVCODE, &state->chip_revcode);
10597cd785adSMatthias Schwarzott 	if (ret < 0)
10607cd785adSMatthias Schwarzott 		goto nodev_error;
10617cd785adSMatthias Schwarzott 
1062814f86c9SMatthias Schwarzott 	ret = si2165_readreg8(state, REV_CHIP_TYPE, &state->chip_type);
10637cd785adSMatthias Schwarzott 	if (ret < 0)
10647cd785adSMatthias Schwarzott 		goto nodev_error;
10657cd785adSMatthias Schwarzott 
10667cd785adSMatthias Schwarzott 	/* powerdown */
1067814f86c9SMatthias Schwarzott 	ret = si2165_writereg8(state, REG_CHIP_MODE, SI2165_MODE_OFF);
10687cd785adSMatthias Schwarzott 	if (ret < 0)
10697cd785adSMatthias Schwarzott 		goto nodev_error;
10707cd785adSMatthias Schwarzott 
10717cd785adSMatthias Schwarzott 	if (state->chip_revcode < 26)
10727cd785adSMatthias Schwarzott 		rev_char = 'A' + state->chip_revcode;
10737cd785adSMatthias Schwarzott 	else
10747cd785adSMatthias Schwarzott 		rev_char = '?';
10757cd785adSMatthias Schwarzott 
10767cd785adSMatthias Schwarzott 	switch (state->chip_type) {
10777cd785adSMatthias Schwarzott 	case 0x06:
10787cd785adSMatthias Schwarzott 		chip_name = "Si2161";
10797cd785adSMatthias Schwarzott 		state->has_dvbt = true;
10807cd785adSMatthias Schwarzott 		break;
10817cd785adSMatthias Schwarzott 	case 0x07:
10827cd785adSMatthias Schwarzott 		chip_name = "Si2165";
10837cd785adSMatthias Schwarzott 		state->has_dvbt = true;
10847cd785adSMatthias Schwarzott 		state->has_dvbc = true;
10857cd785adSMatthias Schwarzott 		break;
10867cd785adSMatthias Schwarzott 	default:
108777f887cdSMatthias Schwarzott 		dev_err(&state->client->dev, "Unsupported Silicon Labs chip (type %d, rev %d)\n",
108877f887cdSMatthias Schwarzott 			state->chip_type, state->chip_revcode);
10897cd785adSMatthias Schwarzott 		goto nodev_error;
10907cd785adSMatthias Schwarzott 	}
10917cd785adSMatthias Schwarzott 
1092aa155449SMatthias Schwarzott 	dev_info(&state->client->dev,
109377f887cdSMatthias Schwarzott 		 "Detected Silicon Labs %s-%c (type %d, rev %d)\n",
109477f887cdSMatthias Schwarzott 		chip_name, rev_char, state->chip_type,
10957cd785adSMatthias Schwarzott 		state->chip_revcode);
10967cd785adSMatthias Schwarzott 
10977cd785adSMatthias Schwarzott 	strlcat(state->fe.ops.info.name, chip_name,
10987cd785adSMatthias Schwarzott 		sizeof(state->fe.ops.info.name));
10997cd785adSMatthias Schwarzott 
11007cd785adSMatthias Schwarzott 	n = 0;
11017cd785adSMatthias Schwarzott 	if (state->has_dvbt) {
11027cd785adSMatthias Schwarzott 		state->fe.ops.delsys[n++] = SYS_DVBT;
11037cd785adSMatthias Schwarzott 		strlcat(state->fe.ops.info.name, " DVB-T",
11047cd785adSMatthias Schwarzott 			sizeof(state->fe.ops.info.name));
11057cd785adSMatthias Schwarzott 	}
11067cd785adSMatthias Schwarzott 	if (state->has_dvbc) {
11077cd785adSMatthias Schwarzott 		state->fe.ops.delsys[n++] = SYS_DVBC_ANNEX_A;
11087cd785adSMatthias Schwarzott 		strlcat(state->fe.ops.info.name, " DVB-C",
11097cd785adSMatthias Schwarzott 			sizeof(state->fe.ops.info.name));
11107cd785adSMatthias Schwarzott 	}
11117cd785adSMatthias Schwarzott 
11127cd785adSMatthias Schwarzott 	/* return fe pointer */
11137cd785adSMatthias Schwarzott 	*pdata->fe = &state->fe;
11147cd785adSMatthias Schwarzott 
11157cd785adSMatthias Schwarzott 	return 0;
11167cd785adSMatthias Schwarzott 
11177cd785adSMatthias Schwarzott nodev_error:
11187cd785adSMatthias Schwarzott 	ret = -ENODEV;
11197cd785adSMatthias Schwarzott error:
11207cd785adSMatthias Schwarzott 	kfree(state);
11217cd785adSMatthias Schwarzott 	dev_dbg(&client->dev, "failed=%d\n", ret);
11227cd785adSMatthias Schwarzott 	return ret;
11237cd785adSMatthias Schwarzott }
11247cd785adSMatthias Schwarzott 
11257cd785adSMatthias Schwarzott static int si2165_remove(struct i2c_client *client)
11267cd785adSMatthias Schwarzott {
11277cd785adSMatthias Schwarzott 	struct si2165_state *state = i2c_get_clientdata(client);
11287cd785adSMatthias Schwarzott 
11297cd785adSMatthias Schwarzott 	dev_dbg(&client->dev, "\n");
11307cd785adSMatthias Schwarzott 
11317cd785adSMatthias Schwarzott 	kfree(state);
11327cd785adSMatthias Schwarzott 	return 0;
11337cd785adSMatthias Schwarzott }
11347cd785adSMatthias Schwarzott 
11357cd785adSMatthias Schwarzott static const struct i2c_device_id si2165_id_table[] = {
11367cd785adSMatthias Schwarzott 	{"si2165", 0},
11377cd785adSMatthias Schwarzott 	{}
11387cd785adSMatthias Schwarzott };
11397cd785adSMatthias Schwarzott MODULE_DEVICE_TABLE(i2c, si2165_id_table);
11407cd785adSMatthias Schwarzott 
11417cd785adSMatthias Schwarzott static struct i2c_driver si2165_driver = {
11427cd785adSMatthias Schwarzott 	.driver = {
11437cd785adSMatthias Schwarzott 		.owner	= THIS_MODULE,
11447cd785adSMatthias Schwarzott 		.name	= "si2165",
11457cd785adSMatthias Schwarzott 	},
11467cd785adSMatthias Schwarzott 	.probe		= si2165_probe,
11477cd785adSMatthias Schwarzott 	.remove		= si2165_remove,
11487cd785adSMatthias Schwarzott 	.id_table	= si2165_id_table,
11497cd785adSMatthias Schwarzott };
11507cd785adSMatthias Schwarzott 
11517cd785adSMatthias Schwarzott module_i2c_driver(si2165_driver);
11527cd785adSMatthias Schwarzott 
11533e54a169SMatthias Schwarzott MODULE_DESCRIPTION("Silicon Labs Si2165 DVB-C/-T Demodulator driver");
11543e54a169SMatthias Schwarzott MODULE_AUTHOR("Matthias Schwarzott <zzam@gentoo.org>");
11553e54a169SMatthias Schwarzott MODULE_LICENSE("GPL");
115655bea400SMatthias Schwarzott MODULE_FIRMWARE(SI2165_FIRMWARE_REV_D);
1157