xref: /OK3568_Linux_fs/kernel/drivers/media/dvb-frontends/nxt200x.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *    Support for NXT2002 and NXT2004 - VSB/QAM
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *    Copyright (C) 2005 Kirk Lapray <kirk.lapray@gmail.com>
6*4882a593Smuzhiyun  *    Copyright (C) 2006-2014 Michael Krufky <mkrufky@linuxtv.org>
7*4882a593Smuzhiyun  *    based on nxt2002 by Taylor Jacob <rtjacob@earthlink.net>
8*4882a593Smuzhiyun  *    and nxt2004 by Jean-Francois Thibert <jeanfrancois@sagetv.com>
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun  *                      NOTES ABOUT THIS DRIVER
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * This Linux driver supports:
15*4882a593Smuzhiyun  *   B2C2/BBTI Technisat Air2PC - ATSC (NXT2002)
16*4882a593Smuzhiyun  *   AverTVHD MCE A180 (NXT2004)
17*4882a593Smuzhiyun  *   ATI HDTV Wonder (NXT2004)
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * This driver needs external firmware. Please use the command
20*4882a593Smuzhiyun  * "<kerneldir>/scripts/get_dvb_firmware nxt2002" or
21*4882a593Smuzhiyun  * "<kerneldir>/scripts/get_dvb_firmware nxt2004" to
22*4882a593Smuzhiyun  * download/extract the appropriate firmware, and then copy it to
23*4882a593Smuzhiyun  * /usr/lib/hotplug/firmware/ or /lib/firmware/
24*4882a593Smuzhiyun  * (depending on configuration of firmware hotplug).
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* Max transfer size done by I2C transfer functions */
29*4882a593Smuzhiyun #define MAX_XFER_SIZE  256
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define NXT2002_DEFAULT_FIRMWARE "dvb-fe-nxt2002.fw"
32*4882a593Smuzhiyun #define NXT2004_DEFAULT_FIRMWARE "dvb-fe-nxt2004.fw"
33*4882a593Smuzhiyun #define CRC_CCIT_MASK 0x1021
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include <linux/kernel.h>
36*4882a593Smuzhiyun #include <linux/init.h>
37*4882a593Smuzhiyun #include <linux/module.h>
38*4882a593Smuzhiyun #include <linux/slab.h>
39*4882a593Smuzhiyun #include <linux/string.h>
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #include <media/dvb_frontend.h>
42*4882a593Smuzhiyun #include "nxt200x.h"
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun struct nxt200x_state {
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	struct i2c_adapter* i2c;
47*4882a593Smuzhiyun 	const struct nxt200x_config* config;
48*4882a593Smuzhiyun 	struct dvb_frontend frontend;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	/* demodulator private data */
51*4882a593Smuzhiyun 	nxt_chip_type demod_chip;
52*4882a593Smuzhiyun 	u8 initialised:1;
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun static int debug;
56*4882a593Smuzhiyun #define dprintk(args...)	do { if (debug) pr_debug(args); } while (0)
57*4882a593Smuzhiyun 
i2c_writebytes(struct nxt200x_state * state,u8 addr,u8 * buf,u8 len)58*4882a593Smuzhiyun static int i2c_writebytes (struct nxt200x_state* state, u8 addr, u8 *buf, u8 len)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	int err;
61*4882a593Smuzhiyun 	struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = len };
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
64*4882a593Smuzhiyun 		pr_warn("%s: i2c write error (addr 0x%02x, err == %i)\n",
65*4882a593Smuzhiyun 			__func__, addr, err);
66*4882a593Smuzhiyun 		return -EREMOTEIO;
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 	return 0;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
i2c_readbytes(struct nxt200x_state * state,u8 addr,u8 * buf,u8 len)71*4882a593Smuzhiyun static int i2c_readbytes(struct nxt200x_state *state, u8 addr, u8 *buf, u8 len)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	int err;
74*4882a593Smuzhiyun 	struct i2c_msg msg = { .addr = addr, .flags = I2C_M_RD, .buf = buf, .len = len };
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
77*4882a593Smuzhiyun 		pr_warn("%s: i2c read error (addr 0x%02x, err == %i)\n",
78*4882a593Smuzhiyun 			__func__, addr, err);
79*4882a593Smuzhiyun 		return -EREMOTEIO;
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 	return 0;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
nxt200x_writebytes(struct nxt200x_state * state,u8 reg,const u8 * buf,u8 len)84*4882a593Smuzhiyun static int nxt200x_writebytes (struct nxt200x_state* state, u8 reg,
85*4882a593Smuzhiyun 			       const u8 *buf, u8 len)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	u8 buf2[MAX_XFER_SIZE];
88*4882a593Smuzhiyun 	int err;
89*4882a593Smuzhiyun 	struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf2, .len = len + 1 };
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	if (1 + len > sizeof(buf2)) {
92*4882a593Smuzhiyun 		pr_warn("%s: i2c wr reg=%04x: len=%d is too big!\n",
93*4882a593Smuzhiyun 			 __func__, reg, len);
94*4882a593Smuzhiyun 		return -EINVAL;
95*4882a593Smuzhiyun 	}
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	buf2[0] = reg;
98*4882a593Smuzhiyun 	memcpy(&buf2[1], buf, len);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
101*4882a593Smuzhiyun 		pr_warn("%s: i2c write error (addr 0x%02x, err == %i)\n",
102*4882a593Smuzhiyun 			__func__, state->config->demod_address, err);
103*4882a593Smuzhiyun 		return -EREMOTEIO;
104*4882a593Smuzhiyun 	}
105*4882a593Smuzhiyun 	return 0;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
nxt200x_readbytes(struct nxt200x_state * state,u8 reg,u8 * buf,u8 len)108*4882a593Smuzhiyun static int nxt200x_readbytes(struct nxt200x_state *state, u8 reg, u8 *buf, u8 len)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	u8 reg2 [] = { reg };
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = reg2, .len = 1 },
113*4882a593Smuzhiyun 			{ .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = buf, .len = len } };
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	int err;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	if ((err = i2c_transfer (state->i2c, msg, 2)) != 2) {
118*4882a593Smuzhiyun 		pr_warn("%s: i2c read error (addr 0x%02x, err == %i)\n",
119*4882a593Smuzhiyun 			__func__, state->config->demod_address, err);
120*4882a593Smuzhiyun 		return -EREMOTEIO;
121*4882a593Smuzhiyun 	}
122*4882a593Smuzhiyun 	return 0;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
nxt200x_crc(u16 crc,u8 c)125*4882a593Smuzhiyun static u16 nxt200x_crc(u16 crc, u8 c)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	u8 i;
128*4882a593Smuzhiyun 	u16 input = (u16) c & 0xFF;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	input<<=8;
131*4882a593Smuzhiyun 	for(i=0; i<8; i++) {
132*4882a593Smuzhiyun 		if((crc^input) & 0x8000)
133*4882a593Smuzhiyun 			crc=(crc<<1)^CRC_CCIT_MASK;
134*4882a593Smuzhiyun 		else
135*4882a593Smuzhiyun 			crc<<=1;
136*4882a593Smuzhiyun 		input<<=1;
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun 	return crc;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
nxt200x_writereg_multibyte(struct nxt200x_state * state,u8 reg,u8 * data,u8 len)141*4882a593Smuzhiyun static int nxt200x_writereg_multibyte (struct nxt200x_state* state, u8 reg, u8* data, u8 len)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	u8 attr, len2, buf;
144*4882a593Smuzhiyun 	dprintk("%s\n", __func__);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	/* set multi register register */
147*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x35, &reg, 1);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* send the actual data */
150*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x36, data, len);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	switch (state->demod_chip) {
153*4882a593Smuzhiyun 		case NXT2002:
154*4882a593Smuzhiyun 			len2 = len;
155*4882a593Smuzhiyun 			buf = 0x02;
156*4882a593Smuzhiyun 			break;
157*4882a593Smuzhiyun 		case NXT2004:
158*4882a593Smuzhiyun 			/* probably not right, but gives correct values */
159*4882a593Smuzhiyun 			attr = 0x02;
160*4882a593Smuzhiyun 			if (reg & 0x80) {
161*4882a593Smuzhiyun 				attr = attr << 1;
162*4882a593Smuzhiyun 				if (reg & 0x04)
163*4882a593Smuzhiyun 					attr = attr >> 1;
164*4882a593Smuzhiyun 			}
165*4882a593Smuzhiyun 			/* set write bit */
166*4882a593Smuzhiyun 			len2 = ((attr << 4) | 0x10) | len;
167*4882a593Smuzhiyun 			buf = 0x80;
168*4882a593Smuzhiyun 			break;
169*4882a593Smuzhiyun 		default:
170*4882a593Smuzhiyun 			return -EINVAL;
171*4882a593Smuzhiyun 			break;
172*4882a593Smuzhiyun 	}
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	/* set multi register length */
175*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x34, &len2, 1);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	/* toggle the multireg write bit */
178*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x21, &buf, 1);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	nxt200x_readbytes(state, 0x21, &buf, 1);
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	switch (state->demod_chip) {
183*4882a593Smuzhiyun 		case NXT2002:
184*4882a593Smuzhiyun 			if ((buf & 0x02) == 0)
185*4882a593Smuzhiyun 				return 0;
186*4882a593Smuzhiyun 			break;
187*4882a593Smuzhiyun 		case NXT2004:
188*4882a593Smuzhiyun 			if (buf == 0)
189*4882a593Smuzhiyun 				return 0;
190*4882a593Smuzhiyun 			break;
191*4882a593Smuzhiyun 		default:
192*4882a593Smuzhiyun 			return -EINVAL;
193*4882a593Smuzhiyun 			break;
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	pr_warn("Error writing multireg register 0x%02X\n", reg);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	return 0;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
nxt200x_readreg_multibyte(struct nxt200x_state * state,u8 reg,u8 * data,u8 len)201*4882a593Smuzhiyun static int nxt200x_readreg_multibyte (struct nxt200x_state* state, u8 reg, u8* data, u8 len)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	int i;
204*4882a593Smuzhiyun 	u8 buf, len2, attr;
205*4882a593Smuzhiyun 	dprintk("%s\n", __func__);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	/* set multi register register */
208*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x35, &reg, 1);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	switch (state->demod_chip) {
211*4882a593Smuzhiyun 		case NXT2002:
212*4882a593Smuzhiyun 			/* set multi register length */
213*4882a593Smuzhiyun 			len2 = len & 0x80;
214*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x34, &len2, 1);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 			/* read the actual data */
217*4882a593Smuzhiyun 			nxt200x_readbytes(state, reg, data, len);
218*4882a593Smuzhiyun 			return 0;
219*4882a593Smuzhiyun 			break;
220*4882a593Smuzhiyun 		case NXT2004:
221*4882a593Smuzhiyun 			/* probably not right, but gives correct values */
222*4882a593Smuzhiyun 			attr = 0x02;
223*4882a593Smuzhiyun 			if (reg & 0x80) {
224*4882a593Smuzhiyun 				attr = attr << 1;
225*4882a593Smuzhiyun 				if (reg & 0x04)
226*4882a593Smuzhiyun 					attr = attr >> 1;
227*4882a593Smuzhiyun 			}
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 			/* set multi register length */
230*4882a593Smuzhiyun 			len2 = (attr << 4) | len;
231*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x34, &len2, 1);
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 			/* toggle the multireg bit*/
234*4882a593Smuzhiyun 			buf = 0x80;
235*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x21, &buf, 1);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 			/* read the actual data */
238*4882a593Smuzhiyun 			for(i = 0; i < len; i++) {
239*4882a593Smuzhiyun 				nxt200x_readbytes(state, 0x36 + i, &data[i], 1);
240*4882a593Smuzhiyun 			}
241*4882a593Smuzhiyun 			return 0;
242*4882a593Smuzhiyun 			break;
243*4882a593Smuzhiyun 		default:
244*4882a593Smuzhiyun 			return -EINVAL;
245*4882a593Smuzhiyun 			break;
246*4882a593Smuzhiyun 	}
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
nxt200x_microcontroller_stop(struct nxt200x_state * state)249*4882a593Smuzhiyun static void nxt200x_microcontroller_stop (struct nxt200x_state* state)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	u8 buf, stopval, counter = 0;
252*4882a593Smuzhiyun 	dprintk("%s\n", __func__);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	/* set correct stop value */
255*4882a593Smuzhiyun 	switch (state->demod_chip) {
256*4882a593Smuzhiyun 		case NXT2002:
257*4882a593Smuzhiyun 			stopval = 0x40;
258*4882a593Smuzhiyun 			break;
259*4882a593Smuzhiyun 		case NXT2004:
260*4882a593Smuzhiyun 			stopval = 0x10;
261*4882a593Smuzhiyun 			break;
262*4882a593Smuzhiyun 		default:
263*4882a593Smuzhiyun 			stopval = 0;
264*4882a593Smuzhiyun 			break;
265*4882a593Smuzhiyun 	}
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	buf = 0x80;
268*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x22, &buf, 1);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	while (counter < 20) {
271*4882a593Smuzhiyun 		nxt200x_readbytes(state, 0x31, &buf, 1);
272*4882a593Smuzhiyun 		if (buf & stopval)
273*4882a593Smuzhiyun 			return;
274*4882a593Smuzhiyun 		msleep(10);
275*4882a593Smuzhiyun 		counter++;
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	pr_warn("Timeout waiting for nxt200x to stop. This is ok after firmware upload.\n");
279*4882a593Smuzhiyun 	return;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun 
nxt200x_microcontroller_start(struct nxt200x_state * state)282*4882a593Smuzhiyun static void nxt200x_microcontroller_start (struct nxt200x_state* state)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun 	u8 buf;
285*4882a593Smuzhiyun 	dprintk("%s\n", __func__);
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	buf = 0x00;
288*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x22, &buf, 1);
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
nxt2004_microcontroller_init(struct nxt200x_state * state)291*4882a593Smuzhiyun static void nxt2004_microcontroller_init (struct nxt200x_state* state)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	u8 buf[9];
294*4882a593Smuzhiyun 	u8 counter = 0;
295*4882a593Smuzhiyun 	dprintk("%s\n", __func__);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	buf[0] = 0x00;
298*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x2b, buf, 1);
299*4882a593Smuzhiyun 	buf[0] = 0x70;
300*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x34, buf, 1);
301*4882a593Smuzhiyun 	buf[0] = 0x04;
302*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x35, buf, 1);
303*4882a593Smuzhiyun 	buf[0] = 0x01; buf[1] = 0x23; buf[2] = 0x45; buf[3] = 0x67; buf[4] = 0x89;
304*4882a593Smuzhiyun 	buf[5] = 0xAB; buf[6] = 0xCD; buf[7] = 0xEF; buf[8] = 0xC0;
305*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x36, buf, 9);
306*4882a593Smuzhiyun 	buf[0] = 0x80;
307*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x21, buf, 1);
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	while (counter < 20) {
310*4882a593Smuzhiyun 		nxt200x_readbytes(state, 0x21, buf, 1);
311*4882a593Smuzhiyun 		if (buf[0] == 0)
312*4882a593Smuzhiyun 			return;
313*4882a593Smuzhiyun 		msleep(10);
314*4882a593Smuzhiyun 		counter++;
315*4882a593Smuzhiyun 	}
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	pr_warn("Timeout waiting for nxt2004 to init.\n");
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	return;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun 
nxt200x_writetuner(struct nxt200x_state * state,u8 * data)322*4882a593Smuzhiyun static int nxt200x_writetuner (struct nxt200x_state* state, u8* data)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	u8 buf, count = 0;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	dprintk("%s\n", __func__);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	dprintk("Tuner Bytes: %*ph\n", 4, data + 1);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	/* if NXT2004, write directly to tuner. if NXT2002, write through NXT chip.
331*4882a593Smuzhiyun 	 * direct write is required for Philips TUV1236D and ALPS TDHU2 */
332*4882a593Smuzhiyun 	switch (state->demod_chip) {
333*4882a593Smuzhiyun 		case NXT2004:
334*4882a593Smuzhiyun 			if (i2c_writebytes(state, data[0], data+1, 4))
335*4882a593Smuzhiyun 				pr_warn("error writing to tuner\n");
336*4882a593Smuzhiyun 			/* wait until we have a lock */
337*4882a593Smuzhiyun 			while (count < 20) {
338*4882a593Smuzhiyun 				i2c_readbytes(state, data[0], &buf, 1);
339*4882a593Smuzhiyun 				if (buf & 0x40)
340*4882a593Smuzhiyun 					return 0;
341*4882a593Smuzhiyun 				msleep(100);
342*4882a593Smuzhiyun 				count++;
343*4882a593Smuzhiyun 			}
344*4882a593Smuzhiyun 			pr_warn("timeout waiting for tuner lock\n");
345*4882a593Smuzhiyun 			break;
346*4882a593Smuzhiyun 		case NXT2002:
347*4882a593Smuzhiyun 			/* set the i2c transfer speed to the tuner */
348*4882a593Smuzhiyun 			buf = 0x03;
349*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x20, &buf, 1);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 			/* setup to transfer 4 bytes via i2c */
352*4882a593Smuzhiyun 			buf = 0x04;
353*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x34, &buf, 1);
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 			/* write actual tuner bytes */
356*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x36, data+1, 4);
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 			/* set tuner i2c address */
359*4882a593Smuzhiyun 			buf = data[0] << 1;
360*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x35, &buf, 1);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 			/* write UC Opmode to begin transfer */
363*4882a593Smuzhiyun 			buf = 0x80;
364*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x21, &buf, 1);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 			while (count < 20) {
367*4882a593Smuzhiyun 				nxt200x_readbytes(state, 0x21, &buf, 1);
368*4882a593Smuzhiyun 				if ((buf & 0x80)== 0x00)
369*4882a593Smuzhiyun 					return 0;
370*4882a593Smuzhiyun 				msleep(100);
371*4882a593Smuzhiyun 				count++;
372*4882a593Smuzhiyun 			}
373*4882a593Smuzhiyun 			pr_warn("timeout error writing to tuner\n");
374*4882a593Smuzhiyun 			break;
375*4882a593Smuzhiyun 		default:
376*4882a593Smuzhiyun 			return -EINVAL;
377*4882a593Smuzhiyun 			break;
378*4882a593Smuzhiyun 	}
379*4882a593Smuzhiyun 	return 0;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun 
nxt200x_agc_reset(struct nxt200x_state * state)382*4882a593Smuzhiyun static void nxt200x_agc_reset(struct nxt200x_state* state)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun 	u8 buf;
385*4882a593Smuzhiyun 	dprintk("%s\n", __func__);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	switch (state->demod_chip) {
388*4882a593Smuzhiyun 		case NXT2002:
389*4882a593Smuzhiyun 			buf = 0x08;
390*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x08, &buf, 1);
391*4882a593Smuzhiyun 			buf = 0x00;
392*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x08, &buf, 1);
393*4882a593Smuzhiyun 			break;
394*4882a593Smuzhiyun 		case NXT2004:
395*4882a593Smuzhiyun 			nxt200x_readreg_multibyte(state, 0x08, &buf, 1);
396*4882a593Smuzhiyun 			buf = 0x08;
397*4882a593Smuzhiyun 			nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
398*4882a593Smuzhiyun 			buf = 0x00;
399*4882a593Smuzhiyun 			nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
400*4882a593Smuzhiyun 			break;
401*4882a593Smuzhiyun 		default:
402*4882a593Smuzhiyun 			break;
403*4882a593Smuzhiyun 	}
404*4882a593Smuzhiyun 	return;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
nxt2002_load_firmware(struct dvb_frontend * fe,const struct firmware * fw)407*4882a593Smuzhiyun static int nxt2002_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
411*4882a593Smuzhiyun 	u8 buf[3], written = 0, chunkpos = 0;
412*4882a593Smuzhiyun 	u16 rambase, position, crc = 0;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	dprintk("%s\n", __func__);
415*4882a593Smuzhiyun 	dprintk("Firmware is %zu bytes\n", fw->size);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	/* Get the RAM base for this nxt2002 */
418*4882a593Smuzhiyun 	nxt200x_readbytes(state, 0x10, buf, 1);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	if (buf[0] & 0x10)
421*4882a593Smuzhiyun 		rambase = 0x1000;
422*4882a593Smuzhiyun 	else
423*4882a593Smuzhiyun 		rambase = 0x0000;
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	dprintk("rambase on this nxt2002 is %04X\n", rambase);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	/* Hold the micro in reset while loading firmware */
428*4882a593Smuzhiyun 	buf[0] = 0x80;
429*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x2B, buf, 1);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	for (position = 0; position < fw->size; position++) {
432*4882a593Smuzhiyun 		if (written == 0) {
433*4882a593Smuzhiyun 			crc = 0;
434*4882a593Smuzhiyun 			chunkpos = 0x28;
435*4882a593Smuzhiyun 			buf[0] = ((rambase + position) >> 8);
436*4882a593Smuzhiyun 			buf[1] = (rambase + position) & 0xFF;
437*4882a593Smuzhiyun 			buf[2] = 0x81;
438*4882a593Smuzhiyun 			/* write starting address */
439*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x29, buf, 3);
440*4882a593Smuzhiyun 		}
441*4882a593Smuzhiyun 		written++;
442*4882a593Smuzhiyun 		chunkpos++;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 		if ((written % 4) == 0)
445*4882a593Smuzhiyun 			nxt200x_writebytes(state, chunkpos, &fw->data[position-3], 4);
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 		crc = nxt200x_crc(crc, fw->data[position]);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 		if ((written == 255) || (position+1 == fw->size)) {
450*4882a593Smuzhiyun 			/* write remaining bytes of firmware */
451*4882a593Smuzhiyun 			nxt200x_writebytes(state, chunkpos+4-(written %4),
452*4882a593Smuzhiyun 				&fw->data[position-(written %4) + 1],
453*4882a593Smuzhiyun 				written %4);
454*4882a593Smuzhiyun 			buf[0] = crc << 8;
455*4882a593Smuzhiyun 			buf[1] = crc & 0xFF;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 			/* write crc */
458*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x2C, buf, 2);
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 			/* do a read to stop things */
461*4882a593Smuzhiyun 			nxt200x_readbytes(state, 0x2A, buf, 1);
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 			/* set transfer mode to complete */
464*4882a593Smuzhiyun 			buf[0] = 0x80;
465*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x2B, buf, 1);
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 			written = 0;
468*4882a593Smuzhiyun 		}
469*4882a593Smuzhiyun 	}
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	return 0;
472*4882a593Smuzhiyun };
473*4882a593Smuzhiyun 
nxt2004_load_firmware(struct dvb_frontend * fe,const struct firmware * fw)474*4882a593Smuzhiyun static int nxt2004_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
478*4882a593Smuzhiyun 	u8 buf[3];
479*4882a593Smuzhiyun 	u16 rambase, position, crc=0;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	dprintk("%s\n", __func__);
482*4882a593Smuzhiyun 	dprintk("Firmware is %zu bytes\n", fw->size);
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	/* set rambase */
485*4882a593Smuzhiyun 	rambase = 0x1000;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	/* hold the micro in reset while loading firmware */
488*4882a593Smuzhiyun 	buf[0] = 0x80;
489*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x2B, buf,1);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	/* calculate firmware CRC */
492*4882a593Smuzhiyun 	for (position = 0; position < fw->size; position++) {
493*4882a593Smuzhiyun 		crc = nxt200x_crc(crc, fw->data[position]);
494*4882a593Smuzhiyun 	}
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	buf[0] = rambase >> 8;
497*4882a593Smuzhiyun 	buf[1] = rambase & 0xFF;
498*4882a593Smuzhiyun 	buf[2] = 0x81;
499*4882a593Smuzhiyun 	/* write starting address */
500*4882a593Smuzhiyun 	nxt200x_writebytes(state,0x29,buf,3);
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	for (position = 0; position < fw->size;) {
503*4882a593Smuzhiyun 		nxt200x_writebytes(state, 0x2C, &fw->data[position],
504*4882a593Smuzhiyun 			fw->size-position > 255 ? 255 : fw->size-position);
505*4882a593Smuzhiyun 		position += (fw->size-position > 255 ? 255 : fw->size-position);
506*4882a593Smuzhiyun 	}
507*4882a593Smuzhiyun 	buf[0] = crc >> 8;
508*4882a593Smuzhiyun 	buf[1] = crc & 0xFF;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	dprintk("firmware crc is 0x%02X 0x%02X\n", buf[0], buf[1]);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	/* write crc */
513*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x2C, buf,2);
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	/* do a read to stop things */
516*4882a593Smuzhiyun 	nxt200x_readbytes(state, 0x2C, buf, 1);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	/* set transfer mode to complete */
519*4882a593Smuzhiyun 	buf[0] = 0x80;
520*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x2B, buf,1);
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	return 0;
523*4882a593Smuzhiyun };
524*4882a593Smuzhiyun 
nxt200x_setup_frontend_parameters(struct dvb_frontend * fe)525*4882a593Smuzhiyun static int nxt200x_setup_frontend_parameters(struct dvb_frontend *fe)
526*4882a593Smuzhiyun {
527*4882a593Smuzhiyun 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
528*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
529*4882a593Smuzhiyun 	u8 buf[5];
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 	/* stop the micro first */
532*4882a593Smuzhiyun 	nxt200x_microcontroller_stop(state);
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	if (state->demod_chip == NXT2004) {
535*4882a593Smuzhiyun 		/* make sure demod is set to digital */
536*4882a593Smuzhiyun 		buf[0] = 0x04;
537*4882a593Smuzhiyun 		nxt200x_writebytes(state, 0x14, buf, 1);
538*4882a593Smuzhiyun 		buf[0] = 0x00;
539*4882a593Smuzhiyun 		nxt200x_writebytes(state, 0x17, buf, 1);
540*4882a593Smuzhiyun 	}
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	/* set additional params */
543*4882a593Smuzhiyun 	switch (p->modulation) {
544*4882a593Smuzhiyun 		case QAM_64:
545*4882a593Smuzhiyun 		case QAM_256:
546*4882a593Smuzhiyun 			/* Set punctured clock for QAM */
547*4882a593Smuzhiyun 			/* This is just a guess since I am unable to test it */
548*4882a593Smuzhiyun 			if (state->config->set_ts_params)
549*4882a593Smuzhiyun 				state->config->set_ts_params(fe, 1);
550*4882a593Smuzhiyun 			break;
551*4882a593Smuzhiyun 		case VSB_8:
552*4882a593Smuzhiyun 			/* Set non-punctured clock for VSB */
553*4882a593Smuzhiyun 			if (state->config->set_ts_params)
554*4882a593Smuzhiyun 				state->config->set_ts_params(fe, 0);
555*4882a593Smuzhiyun 			break;
556*4882a593Smuzhiyun 		default:
557*4882a593Smuzhiyun 			return -EINVAL;
558*4882a593Smuzhiyun 			break;
559*4882a593Smuzhiyun 	}
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	if (fe->ops.tuner_ops.calc_regs) {
562*4882a593Smuzhiyun 		/* get tuning information */
563*4882a593Smuzhiyun 		fe->ops.tuner_ops.calc_regs(fe, buf, 5);
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 		/* write frequency information */
566*4882a593Smuzhiyun 		nxt200x_writetuner(state, buf);
567*4882a593Smuzhiyun 	}
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	/* reset the agc now that tuning has been completed */
570*4882a593Smuzhiyun 	nxt200x_agc_reset(state);
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	/* set target power level */
573*4882a593Smuzhiyun 	switch (p->modulation) {
574*4882a593Smuzhiyun 		case QAM_64:
575*4882a593Smuzhiyun 		case QAM_256:
576*4882a593Smuzhiyun 			buf[0] = 0x74;
577*4882a593Smuzhiyun 			break;
578*4882a593Smuzhiyun 		case VSB_8:
579*4882a593Smuzhiyun 			buf[0] = 0x70;
580*4882a593Smuzhiyun 			break;
581*4882a593Smuzhiyun 		default:
582*4882a593Smuzhiyun 			return -EINVAL;
583*4882a593Smuzhiyun 			break;
584*4882a593Smuzhiyun 	}
585*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x42, buf, 1);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	/* configure sdm */
588*4882a593Smuzhiyun 	switch (state->demod_chip) {
589*4882a593Smuzhiyun 		case NXT2002:
590*4882a593Smuzhiyun 			buf[0] = 0x87;
591*4882a593Smuzhiyun 			break;
592*4882a593Smuzhiyun 		case NXT2004:
593*4882a593Smuzhiyun 			buf[0] = 0x07;
594*4882a593Smuzhiyun 			break;
595*4882a593Smuzhiyun 		default:
596*4882a593Smuzhiyun 			return -EINVAL;
597*4882a593Smuzhiyun 			break;
598*4882a593Smuzhiyun 	}
599*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x57, buf, 1);
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	/* write sdm1 input */
602*4882a593Smuzhiyun 	buf[0] = 0x10;
603*4882a593Smuzhiyun 	buf[1] = 0x00;
604*4882a593Smuzhiyun 	switch (state->demod_chip) {
605*4882a593Smuzhiyun 		case NXT2002:
606*4882a593Smuzhiyun 			nxt200x_writereg_multibyte(state, 0x58, buf, 2);
607*4882a593Smuzhiyun 			break;
608*4882a593Smuzhiyun 		case NXT2004:
609*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x58, buf, 2);
610*4882a593Smuzhiyun 			break;
611*4882a593Smuzhiyun 		default:
612*4882a593Smuzhiyun 			return -EINVAL;
613*4882a593Smuzhiyun 			break;
614*4882a593Smuzhiyun 	}
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	/* write sdmx input */
617*4882a593Smuzhiyun 	switch (p->modulation) {
618*4882a593Smuzhiyun 		case QAM_64:
619*4882a593Smuzhiyun 				buf[0] = 0x68;
620*4882a593Smuzhiyun 				break;
621*4882a593Smuzhiyun 		case QAM_256:
622*4882a593Smuzhiyun 				buf[0] = 0x64;
623*4882a593Smuzhiyun 				break;
624*4882a593Smuzhiyun 		case VSB_8:
625*4882a593Smuzhiyun 				buf[0] = 0x60;
626*4882a593Smuzhiyun 				break;
627*4882a593Smuzhiyun 		default:
628*4882a593Smuzhiyun 				return -EINVAL;
629*4882a593Smuzhiyun 				break;
630*4882a593Smuzhiyun 	}
631*4882a593Smuzhiyun 	buf[1] = 0x00;
632*4882a593Smuzhiyun 	switch (state->demod_chip) {
633*4882a593Smuzhiyun 		case NXT2002:
634*4882a593Smuzhiyun 			nxt200x_writereg_multibyte(state, 0x5C, buf, 2);
635*4882a593Smuzhiyun 			break;
636*4882a593Smuzhiyun 		case NXT2004:
637*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x5C, buf, 2);
638*4882a593Smuzhiyun 			break;
639*4882a593Smuzhiyun 		default:
640*4882a593Smuzhiyun 			return -EINVAL;
641*4882a593Smuzhiyun 			break;
642*4882a593Smuzhiyun 	}
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	/* write adc power lpf fc */
645*4882a593Smuzhiyun 	buf[0] = 0x05;
646*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x43, buf, 1);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	if (state->demod_chip == NXT2004) {
649*4882a593Smuzhiyun 		/* write ??? */
650*4882a593Smuzhiyun 		buf[0] = 0x00;
651*4882a593Smuzhiyun 		buf[1] = 0x00;
652*4882a593Smuzhiyun 		nxt200x_writebytes(state, 0x46, buf, 2);
653*4882a593Smuzhiyun 	}
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	/* write accumulator2 input */
656*4882a593Smuzhiyun 	buf[0] = 0x80;
657*4882a593Smuzhiyun 	buf[1] = 0x00;
658*4882a593Smuzhiyun 	switch (state->demod_chip) {
659*4882a593Smuzhiyun 		case NXT2002:
660*4882a593Smuzhiyun 			nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
661*4882a593Smuzhiyun 			break;
662*4882a593Smuzhiyun 		case NXT2004:
663*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x4B, buf, 2);
664*4882a593Smuzhiyun 			break;
665*4882a593Smuzhiyun 		default:
666*4882a593Smuzhiyun 			return -EINVAL;
667*4882a593Smuzhiyun 			break;
668*4882a593Smuzhiyun 	}
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	/* write kg1 */
671*4882a593Smuzhiyun 	buf[0] = 0x00;
672*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x4D, buf, 1);
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	/* write sdm12 lpf fc */
675*4882a593Smuzhiyun 	buf[0] = 0x44;
676*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x55, buf, 1);
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	/* write agc control reg */
679*4882a593Smuzhiyun 	buf[0] = 0x04;
680*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x41, buf, 1);
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	if (state->demod_chip == NXT2004) {
683*4882a593Smuzhiyun 		nxt200x_readreg_multibyte(state, 0x80, buf, 1);
684*4882a593Smuzhiyun 		buf[0] = 0x24;
685*4882a593Smuzhiyun 		nxt200x_writereg_multibyte(state, 0x80, buf, 1);
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 		/* soft reset? */
688*4882a593Smuzhiyun 		nxt200x_readreg_multibyte(state, 0x08, buf, 1);
689*4882a593Smuzhiyun 		buf[0] = 0x10;
690*4882a593Smuzhiyun 		nxt200x_writereg_multibyte(state, 0x08, buf, 1);
691*4882a593Smuzhiyun 		nxt200x_readreg_multibyte(state, 0x08, buf, 1);
692*4882a593Smuzhiyun 		buf[0] = 0x00;
693*4882a593Smuzhiyun 		nxt200x_writereg_multibyte(state, 0x08, buf, 1);
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 		nxt200x_readreg_multibyte(state, 0x80, buf, 1);
696*4882a593Smuzhiyun 		buf[0] = 0x04;
697*4882a593Smuzhiyun 		nxt200x_writereg_multibyte(state, 0x80, buf, 1);
698*4882a593Smuzhiyun 		buf[0] = 0x00;
699*4882a593Smuzhiyun 		nxt200x_writereg_multibyte(state, 0x81, buf, 1);
700*4882a593Smuzhiyun 		buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
701*4882a593Smuzhiyun 		nxt200x_writereg_multibyte(state, 0x82, buf, 3);
702*4882a593Smuzhiyun 		nxt200x_readreg_multibyte(state, 0x88, buf, 1);
703*4882a593Smuzhiyun 		buf[0] = 0x11;
704*4882a593Smuzhiyun 		nxt200x_writereg_multibyte(state, 0x88, buf, 1);
705*4882a593Smuzhiyun 		nxt200x_readreg_multibyte(state, 0x80, buf, 1);
706*4882a593Smuzhiyun 		buf[0] = 0x44;
707*4882a593Smuzhiyun 		nxt200x_writereg_multibyte(state, 0x80, buf, 1);
708*4882a593Smuzhiyun 	}
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	/* write agc ucgp0 */
711*4882a593Smuzhiyun 	switch (p->modulation) {
712*4882a593Smuzhiyun 		case QAM_64:
713*4882a593Smuzhiyun 				buf[0] = 0x02;
714*4882a593Smuzhiyun 				break;
715*4882a593Smuzhiyun 		case QAM_256:
716*4882a593Smuzhiyun 				buf[0] = 0x03;
717*4882a593Smuzhiyun 				break;
718*4882a593Smuzhiyun 		case VSB_8:
719*4882a593Smuzhiyun 				buf[0] = 0x00;
720*4882a593Smuzhiyun 				break;
721*4882a593Smuzhiyun 		default:
722*4882a593Smuzhiyun 				return -EINVAL;
723*4882a593Smuzhiyun 				break;
724*4882a593Smuzhiyun 	}
725*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x30, buf, 1);
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	/* write agc control reg */
728*4882a593Smuzhiyun 	buf[0] = 0x00;
729*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x41, buf, 1);
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	/* write accumulator2 input */
732*4882a593Smuzhiyun 	buf[0] = 0x80;
733*4882a593Smuzhiyun 	buf[1] = 0x00;
734*4882a593Smuzhiyun 	switch (state->demod_chip) {
735*4882a593Smuzhiyun 		case NXT2002:
736*4882a593Smuzhiyun 			nxt200x_writereg_multibyte(state, 0x49, buf, 2);
737*4882a593Smuzhiyun 			nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
738*4882a593Smuzhiyun 			break;
739*4882a593Smuzhiyun 		case NXT2004:
740*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x49, buf, 2);
741*4882a593Smuzhiyun 			nxt200x_writebytes(state, 0x4B, buf, 2);
742*4882a593Smuzhiyun 			break;
743*4882a593Smuzhiyun 		default:
744*4882a593Smuzhiyun 			return -EINVAL;
745*4882a593Smuzhiyun 			break;
746*4882a593Smuzhiyun 	}
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	/* write agc control reg */
749*4882a593Smuzhiyun 	buf[0] = 0x04;
750*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x41, buf, 1);
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	nxt200x_microcontroller_start(state);
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	if (state->demod_chip == NXT2004) {
755*4882a593Smuzhiyun 		nxt2004_microcontroller_init(state);
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 		/* ???? */
758*4882a593Smuzhiyun 		buf[0] = 0xF0;
759*4882a593Smuzhiyun 		buf[1] = 0x00;
760*4882a593Smuzhiyun 		nxt200x_writebytes(state, 0x5C, buf, 2);
761*4882a593Smuzhiyun 	}
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	/* adjacent channel detection should be done here, but I don't
764*4882a593Smuzhiyun 	have any stations with this need so I cannot test it */
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	return 0;
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun 
nxt200x_read_status(struct dvb_frontend * fe,enum fe_status * status)769*4882a593Smuzhiyun static int nxt200x_read_status(struct dvb_frontend *fe, enum fe_status *status)
770*4882a593Smuzhiyun {
771*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
772*4882a593Smuzhiyun 	u8 lock;
773*4882a593Smuzhiyun 	nxt200x_readbytes(state, 0x31, &lock, 1);
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	*status = 0;
776*4882a593Smuzhiyun 	if (lock & 0x20) {
777*4882a593Smuzhiyun 		*status |= FE_HAS_SIGNAL;
778*4882a593Smuzhiyun 		*status |= FE_HAS_CARRIER;
779*4882a593Smuzhiyun 		*status |= FE_HAS_VITERBI;
780*4882a593Smuzhiyun 		*status |= FE_HAS_SYNC;
781*4882a593Smuzhiyun 		*status |= FE_HAS_LOCK;
782*4882a593Smuzhiyun 	}
783*4882a593Smuzhiyun 	return 0;
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun 
nxt200x_read_ber(struct dvb_frontend * fe,u32 * ber)786*4882a593Smuzhiyun static int nxt200x_read_ber(struct dvb_frontend* fe, u32* ber)
787*4882a593Smuzhiyun {
788*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
789*4882a593Smuzhiyun 	u8 b[3];
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0xE6, b, 3);
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	*ber = ((b[0] << 8) + b[1]) * 8;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	return 0;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
nxt200x_read_signal_strength(struct dvb_frontend * fe,u16 * strength)798*4882a593Smuzhiyun static int nxt200x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
799*4882a593Smuzhiyun {
800*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
801*4882a593Smuzhiyun 	u8 b[2];
802*4882a593Smuzhiyun 	u16 temp = 0;
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	/* setup to read cluster variance */
805*4882a593Smuzhiyun 	b[0] = 0x00;
806*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0xA1, b, 1);
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	/* get multreg val */
809*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0xA6, b, 2);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	temp = (b[0] << 8) | b[1];
812*4882a593Smuzhiyun 	*strength = ((0x7FFF - temp) & 0x0FFF) * 16;
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	return 0;
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun 
nxt200x_read_snr(struct dvb_frontend * fe,u16 * snr)817*4882a593Smuzhiyun static int nxt200x_read_snr(struct dvb_frontend* fe, u16* snr)
818*4882a593Smuzhiyun {
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
821*4882a593Smuzhiyun 	u8 b[2];
822*4882a593Smuzhiyun 	u16 temp = 0, temp2;
823*4882a593Smuzhiyun 	u32 snrdb = 0;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	/* setup to read cluster variance */
826*4882a593Smuzhiyun 	b[0] = 0x00;
827*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0xA1, b, 1);
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	/* get multreg val from 0xA6 */
830*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0xA6, b, 2);
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	temp = (b[0] << 8) | b[1];
833*4882a593Smuzhiyun 	temp2 = 0x7FFF - temp;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	/* snr will be in db */
836*4882a593Smuzhiyun 	if (temp2 > 0x7F00)
837*4882a593Smuzhiyun 		snrdb = 1000*24 + ( 1000*(30-24) * ( temp2 - 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
838*4882a593Smuzhiyun 	else if (temp2 > 0x7EC0)
839*4882a593Smuzhiyun 		snrdb = 1000*18 + ( 1000*(24-18) * ( temp2 - 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
840*4882a593Smuzhiyun 	else if (temp2 > 0x7C00)
841*4882a593Smuzhiyun 		snrdb = 1000*12 + ( 1000*(18-12) * ( temp2 - 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
842*4882a593Smuzhiyun 	else
843*4882a593Smuzhiyun 		snrdb = 1000*0 + ( 1000*(12-0) * ( temp2 - 0 ) / ( 0x7C00 - 0 ) );
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	/* the value reported back from the frontend will be FFFF=32db 0000=0db */
846*4882a593Smuzhiyun 	*snr = snrdb * (0xFFFF/32000);
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	return 0;
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun 
nxt200x_read_ucblocks(struct dvb_frontend * fe,u32 * ucblocks)851*4882a593Smuzhiyun static int nxt200x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
852*4882a593Smuzhiyun {
853*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
854*4882a593Smuzhiyun 	u8 b[3];
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0xE6, b, 3);
857*4882a593Smuzhiyun 	*ucblocks = b[2];
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	return 0;
860*4882a593Smuzhiyun }
861*4882a593Smuzhiyun 
nxt200x_sleep(struct dvb_frontend * fe)862*4882a593Smuzhiyun static int nxt200x_sleep(struct dvb_frontend* fe)
863*4882a593Smuzhiyun {
864*4882a593Smuzhiyun 	return 0;
865*4882a593Smuzhiyun }
866*4882a593Smuzhiyun 
nxt2002_init(struct dvb_frontend * fe)867*4882a593Smuzhiyun static int nxt2002_init(struct dvb_frontend* fe)
868*4882a593Smuzhiyun {
869*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
870*4882a593Smuzhiyun 	const struct firmware *fw;
871*4882a593Smuzhiyun 	int ret;
872*4882a593Smuzhiyun 	u8 buf[2];
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	/* request the firmware, this will block until someone uploads it */
875*4882a593Smuzhiyun 	pr_debug("%s: Waiting for firmware upload (%s)...\n",
876*4882a593Smuzhiyun 		 __func__, NXT2002_DEFAULT_FIRMWARE);
877*4882a593Smuzhiyun 	ret = request_firmware(&fw, NXT2002_DEFAULT_FIRMWARE,
878*4882a593Smuzhiyun 			       state->i2c->dev.parent);
879*4882a593Smuzhiyun 	pr_debug("%s: Waiting for firmware upload(2)...\n", __func__);
880*4882a593Smuzhiyun 	if (ret) {
881*4882a593Smuzhiyun 		pr_err("%s: No firmware uploaded (timeout or file not found?)\n",
882*4882a593Smuzhiyun 		       __func__);
883*4882a593Smuzhiyun 		return ret;
884*4882a593Smuzhiyun 	}
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	ret = nxt2002_load_firmware(fe, fw);
887*4882a593Smuzhiyun 	release_firmware(fw);
888*4882a593Smuzhiyun 	if (ret) {
889*4882a593Smuzhiyun 		pr_err("%s: Writing firmware to device failed\n", __func__);
890*4882a593Smuzhiyun 		return ret;
891*4882a593Smuzhiyun 	}
892*4882a593Smuzhiyun 	pr_info("%s: Firmware upload complete\n", __func__);
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 	/* Put the micro into reset */
895*4882a593Smuzhiyun 	nxt200x_microcontroller_stop(state);
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	/* ensure transfer is complete */
898*4882a593Smuzhiyun 	buf[0]=0x00;
899*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x2B, buf, 1);
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	/* Put the micro into reset for real this time */
902*4882a593Smuzhiyun 	nxt200x_microcontroller_stop(state);
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 	/* soft reset everything (agc,frontend,eq,fec)*/
905*4882a593Smuzhiyun 	buf[0] = 0x0F;
906*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x08, buf, 1);
907*4882a593Smuzhiyun 	buf[0] = 0x00;
908*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x08, buf, 1);
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	/* write agc sdm configure */
911*4882a593Smuzhiyun 	buf[0] = 0xF1;
912*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x57, buf, 1);
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	/* write mod output format */
915*4882a593Smuzhiyun 	buf[0] = 0x20;
916*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x09, buf, 1);
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	/* write fec mpeg mode */
919*4882a593Smuzhiyun 	buf[0] = 0x7E;
920*4882a593Smuzhiyun 	buf[1] = 0x00;
921*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0xE9, buf, 2);
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	/* write mux selection */
924*4882a593Smuzhiyun 	buf[0] = 0x00;
925*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0xCC, buf, 1);
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	return 0;
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun 
nxt2004_init(struct dvb_frontend * fe)930*4882a593Smuzhiyun static int nxt2004_init(struct dvb_frontend* fe)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
933*4882a593Smuzhiyun 	const struct firmware *fw;
934*4882a593Smuzhiyun 	int ret;
935*4882a593Smuzhiyun 	u8 buf[3];
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	/* ??? */
938*4882a593Smuzhiyun 	buf[0]=0x00;
939*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x1E, buf, 1);
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	/* request the firmware, this will block until someone uploads it */
942*4882a593Smuzhiyun 	pr_debug("%s: Waiting for firmware upload (%s)...\n",
943*4882a593Smuzhiyun 		 __func__, NXT2004_DEFAULT_FIRMWARE);
944*4882a593Smuzhiyun 	ret = request_firmware(&fw, NXT2004_DEFAULT_FIRMWARE,
945*4882a593Smuzhiyun 			       state->i2c->dev.parent);
946*4882a593Smuzhiyun 	pr_debug("%s: Waiting for firmware upload(2)...\n", __func__);
947*4882a593Smuzhiyun 	if (ret) {
948*4882a593Smuzhiyun 		pr_err("%s: No firmware uploaded (timeout or file not found?)\n",
949*4882a593Smuzhiyun 		       __func__);
950*4882a593Smuzhiyun 		return ret;
951*4882a593Smuzhiyun 	}
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	ret = nxt2004_load_firmware(fe, fw);
954*4882a593Smuzhiyun 	release_firmware(fw);
955*4882a593Smuzhiyun 	if (ret) {
956*4882a593Smuzhiyun 		pr_err("%s: Writing firmware to device failed\n", __func__);
957*4882a593Smuzhiyun 		return ret;
958*4882a593Smuzhiyun 	}
959*4882a593Smuzhiyun 	pr_info("%s: Firmware upload complete\n", __func__);
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	/* ensure transfer is complete */
962*4882a593Smuzhiyun 	buf[0] = 0x01;
963*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x19, buf, 1);
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	nxt2004_microcontroller_init(state);
966*4882a593Smuzhiyun 	nxt200x_microcontroller_stop(state);
967*4882a593Smuzhiyun 	nxt200x_microcontroller_stop(state);
968*4882a593Smuzhiyun 	nxt2004_microcontroller_init(state);
969*4882a593Smuzhiyun 	nxt200x_microcontroller_stop(state);
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	/* soft reset everything (agc,frontend,eq,fec)*/
972*4882a593Smuzhiyun 	buf[0] = 0xFF;
973*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x08, buf, 1);
974*4882a593Smuzhiyun 	buf[0] = 0x00;
975*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x08, buf, 1);
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	/* write agc sdm configure */
978*4882a593Smuzhiyun 	buf[0] = 0xD7;
979*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x57, buf, 1);
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	/* ???*/
982*4882a593Smuzhiyun 	buf[0] = 0x07;
983*4882a593Smuzhiyun 	buf[1] = 0xfe;
984*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x35, buf, 2);
985*4882a593Smuzhiyun 	buf[0] = 0x12;
986*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x34, buf, 1);
987*4882a593Smuzhiyun 	buf[0] = 0x80;
988*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x21, buf, 1);
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun 	/* ???*/
991*4882a593Smuzhiyun 	buf[0] = 0x21;
992*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x0A, buf, 1);
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 	/* ???*/
995*4882a593Smuzhiyun 	buf[0] = 0x01;
996*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x80, buf, 1);
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun 	/* write fec mpeg mode */
999*4882a593Smuzhiyun 	buf[0] = 0x7E;
1000*4882a593Smuzhiyun 	buf[1] = 0x00;
1001*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0xE9, buf, 2);
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	/* write mux selection */
1004*4882a593Smuzhiyun 	buf[0] = 0x00;
1005*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0xCC, buf, 1);
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	/* ???*/
1008*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1009*4882a593Smuzhiyun 	buf[0] = 0x00;
1010*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun 	/* soft reset? */
1013*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1014*4882a593Smuzhiyun 	buf[0] = 0x10;
1015*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1016*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1017*4882a593Smuzhiyun 	buf[0] = 0x00;
1018*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	/* ???*/
1021*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1022*4882a593Smuzhiyun 	buf[0] = 0x01;
1023*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1024*4882a593Smuzhiyun 	buf[0] = 0x70;
1025*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1026*4882a593Smuzhiyun 	buf[0] = 0x31; buf[1] = 0x5E; buf[2] = 0x66;
1027*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1030*4882a593Smuzhiyun 	buf[0] = 0x11;
1031*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1032*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1033*4882a593Smuzhiyun 	buf[0] = 0x40;
1034*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 	nxt200x_readbytes(state, 0x10, buf, 1);
1037*4882a593Smuzhiyun 	buf[0] = 0x10;
1038*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x10, buf, 1);
1039*4882a593Smuzhiyun 	nxt200x_readbytes(state, 0x0A, buf, 1);
1040*4882a593Smuzhiyun 	buf[0] = 0x21;
1041*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x0A, buf, 1);
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	nxt2004_microcontroller_init(state);
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 	buf[0] = 0x21;
1046*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x0A, buf, 1);
1047*4882a593Smuzhiyun 	buf[0] = 0x7E;
1048*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0xE9, buf, 1);
1049*4882a593Smuzhiyun 	buf[0] = 0x00;
1050*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0xEA, buf, 1);
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1053*4882a593Smuzhiyun 	buf[0] = 0x00;
1054*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1055*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1056*4882a593Smuzhiyun 	buf[0] = 0x00;
1057*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun 	/* soft reset? */
1060*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1061*4882a593Smuzhiyun 	buf[0] = 0x10;
1062*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1063*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1064*4882a593Smuzhiyun 	buf[0] = 0x00;
1065*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1068*4882a593Smuzhiyun 	buf[0] = 0x04;
1069*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1070*4882a593Smuzhiyun 	buf[0] = 0x00;
1071*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1072*4882a593Smuzhiyun 	buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
1073*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1076*4882a593Smuzhiyun 	buf[0] = 0x11;
1077*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1080*4882a593Smuzhiyun 	buf[0] = 0x44;
1081*4882a593Smuzhiyun 	nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	/* initialize tuner */
1084*4882a593Smuzhiyun 	nxt200x_readbytes(state, 0x10, buf, 1);
1085*4882a593Smuzhiyun 	buf[0] = 0x12;
1086*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x10, buf, 1);
1087*4882a593Smuzhiyun 	buf[0] = 0x04;
1088*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x13, buf, 1);
1089*4882a593Smuzhiyun 	buf[0] = 0x00;
1090*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x16, buf, 1);
1091*4882a593Smuzhiyun 	buf[0] = 0x04;
1092*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x14, buf, 1);
1093*4882a593Smuzhiyun 	buf[0] = 0x00;
1094*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x14, buf, 1);
1095*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x17, buf, 1);
1096*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x14, buf, 1);
1097*4882a593Smuzhiyun 	nxt200x_writebytes(state, 0x17, buf, 1);
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	return 0;
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun 
nxt200x_init(struct dvb_frontend * fe)1102*4882a593Smuzhiyun static int nxt200x_init(struct dvb_frontend* fe)
1103*4882a593Smuzhiyun {
1104*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
1105*4882a593Smuzhiyun 	int ret = 0;
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	if (!state->initialised) {
1108*4882a593Smuzhiyun 		switch (state->demod_chip) {
1109*4882a593Smuzhiyun 			case NXT2002:
1110*4882a593Smuzhiyun 				ret = nxt2002_init(fe);
1111*4882a593Smuzhiyun 				break;
1112*4882a593Smuzhiyun 			case NXT2004:
1113*4882a593Smuzhiyun 				ret = nxt2004_init(fe);
1114*4882a593Smuzhiyun 				break;
1115*4882a593Smuzhiyun 			default:
1116*4882a593Smuzhiyun 				return -EINVAL;
1117*4882a593Smuzhiyun 				break;
1118*4882a593Smuzhiyun 		}
1119*4882a593Smuzhiyun 		state->initialised = 1;
1120*4882a593Smuzhiyun 	}
1121*4882a593Smuzhiyun 	return ret;
1122*4882a593Smuzhiyun }
1123*4882a593Smuzhiyun 
nxt200x_get_tune_settings(struct dvb_frontend * fe,struct dvb_frontend_tune_settings * fesettings)1124*4882a593Smuzhiyun static int nxt200x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
1125*4882a593Smuzhiyun {
1126*4882a593Smuzhiyun 	fesettings->min_delay_ms = 500;
1127*4882a593Smuzhiyun 	fesettings->step_size = 0;
1128*4882a593Smuzhiyun 	fesettings->max_drift = 0;
1129*4882a593Smuzhiyun 	return 0;
1130*4882a593Smuzhiyun }
1131*4882a593Smuzhiyun 
nxt200x_release(struct dvb_frontend * fe)1132*4882a593Smuzhiyun static void nxt200x_release(struct dvb_frontend* fe)
1133*4882a593Smuzhiyun {
1134*4882a593Smuzhiyun 	struct nxt200x_state* state = fe->demodulator_priv;
1135*4882a593Smuzhiyun 	kfree(state);
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun static const struct dvb_frontend_ops nxt200x_ops;
1139*4882a593Smuzhiyun 
nxt200x_attach(const struct nxt200x_config * config,struct i2c_adapter * i2c)1140*4882a593Smuzhiyun struct dvb_frontend* nxt200x_attach(const struct nxt200x_config* config,
1141*4882a593Smuzhiyun 				   struct i2c_adapter* i2c)
1142*4882a593Smuzhiyun {
1143*4882a593Smuzhiyun 	struct nxt200x_state* state = NULL;
1144*4882a593Smuzhiyun 	u8 buf [] = {0,0,0,0,0};
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	/* allocate memory for the internal state */
1147*4882a593Smuzhiyun 	state = kzalloc(sizeof(struct nxt200x_state), GFP_KERNEL);
1148*4882a593Smuzhiyun 	if (state == NULL)
1149*4882a593Smuzhiyun 		goto error;
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun 	/* setup the state */
1152*4882a593Smuzhiyun 	state->config = config;
1153*4882a593Smuzhiyun 	state->i2c = i2c;
1154*4882a593Smuzhiyun 	state->initialised = 0;
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	/* read card id */
1157*4882a593Smuzhiyun 	nxt200x_readbytes(state, 0x00, buf, 5);
1158*4882a593Smuzhiyun 	dprintk("NXT info: %*ph\n", 5, buf);
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	/* set demod chip */
1161*4882a593Smuzhiyun 	switch (buf[0]) {
1162*4882a593Smuzhiyun 		case 0x04:
1163*4882a593Smuzhiyun 			state->demod_chip = NXT2002;
1164*4882a593Smuzhiyun 			pr_info("NXT2002 Detected\n");
1165*4882a593Smuzhiyun 			break;
1166*4882a593Smuzhiyun 		case 0x05:
1167*4882a593Smuzhiyun 			state->demod_chip = NXT2004;
1168*4882a593Smuzhiyun 			pr_info("NXT2004 Detected\n");
1169*4882a593Smuzhiyun 			break;
1170*4882a593Smuzhiyun 		default:
1171*4882a593Smuzhiyun 			goto error;
1172*4882a593Smuzhiyun 	}
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	/* make sure demod chip is supported */
1175*4882a593Smuzhiyun 	switch (state->demod_chip) {
1176*4882a593Smuzhiyun 		case NXT2002:
1177*4882a593Smuzhiyun 			if (buf[0] != 0x04) goto error;		/* device id */
1178*4882a593Smuzhiyun 			if (buf[1] != 0x02) goto error;		/* fab id */
1179*4882a593Smuzhiyun 			if (buf[2] != 0x11) goto error;		/* month */
1180*4882a593Smuzhiyun 			if (buf[3] != 0x20) goto error;		/* year msb */
1181*4882a593Smuzhiyun 			if (buf[4] != 0x00) goto error;		/* year lsb */
1182*4882a593Smuzhiyun 			break;
1183*4882a593Smuzhiyun 		case NXT2004:
1184*4882a593Smuzhiyun 			if (buf[0] != 0x05) goto error;		/* device id */
1185*4882a593Smuzhiyun 			break;
1186*4882a593Smuzhiyun 		default:
1187*4882a593Smuzhiyun 			goto error;
1188*4882a593Smuzhiyun 	}
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun 	/* create dvb_frontend */
1191*4882a593Smuzhiyun 	memcpy(&state->frontend.ops, &nxt200x_ops, sizeof(struct dvb_frontend_ops));
1192*4882a593Smuzhiyun 	state->frontend.demodulator_priv = state;
1193*4882a593Smuzhiyun 	return &state->frontend;
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun error:
1196*4882a593Smuzhiyun 	kfree(state);
1197*4882a593Smuzhiyun 	pr_err("Unknown/Unsupported NXT chip: %*ph\n", 5, buf);
1198*4882a593Smuzhiyun 	return NULL;
1199*4882a593Smuzhiyun }
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun static const struct dvb_frontend_ops nxt200x_ops = {
1202*4882a593Smuzhiyun 	.delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
1203*4882a593Smuzhiyun 	.info = {
1204*4882a593Smuzhiyun 		.name = "Nextwave NXT200X VSB/QAM frontend",
1205*4882a593Smuzhiyun 		.frequency_min_hz =  54 * MHz,
1206*4882a593Smuzhiyun 		.frequency_max_hz = 860 * MHz,
1207*4882a593Smuzhiyun 		.frequency_stepsize_hz = 166666,	/* stepsize is just a guess */
1208*4882a593Smuzhiyun 		.caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1209*4882a593Smuzhiyun 			FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1210*4882a593Smuzhiyun 			FE_CAN_8VSB | FE_CAN_QAM_64 | FE_CAN_QAM_256
1211*4882a593Smuzhiyun 	},
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun 	.release = nxt200x_release,
1214*4882a593Smuzhiyun 
1215*4882a593Smuzhiyun 	.init = nxt200x_init,
1216*4882a593Smuzhiyun 	.sleep = nxt200x_sleep,
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	.set_frontend = nxt200x_setup_frontend_parameters,
1219*4882a593Smuzhiyun 	.get_tune_settings = nxt200x_get_tune_settings,
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun 	.read_status = nxt200x_read_status,
1222*4882a593Smuzhiyun 	.read_ber = nxt200x_read_ber,
1223*4882a593Smuzhiyun 	.read_signal_strength = nxt200x_read_signal_strength,
1224*4882a593Smuzhiyun 	.read_snr = nxt200x_read_snr,
1225*4882a593Smuzhiyun 	.read_ucblocks = nxt200x_read_ucblocks,
1226*4882a593Smuzhiyun };
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun module_param(debug, int, 0644);
1229*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun MODULE_DESCRIPTION("NXT200X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
1232*4882a593Smuzhiyun MODULE_AUTHOR("Kirk Lapray, Michael Krufky, Jean-Francois Thibert, and Taylor Jacob");
1233*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun EXPORT_SYMBOL(nxt200x_attach);
1236*4882a593Smuzhiyun 
1237