2 * Support for NXT2002 and NXT2004 - VSB/QAM
4 * Copyright (C) 2005 Kirk Lapray <kirk.lapray@gmail.com>
5 * Copyright (C) 2006 Michael Krufky <mkrufky@m1k.net>
6 * based on nxt2002 by Taylor Jacob <rtjacob@earthlink.net>
7 * and nxt2004 by Jean-Francois Thibert <jeanfrancois@sagetv.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * NOTES ABOUT THIS DRIVER
28 * This Linux driver supports:
29 * B2C2/BBTI Technisat Air2PC - ATSC (NXT2002)
30 * AverTVHD MCE A180 (NXT2004)
31 * ATI HDTV Wonder (NXT2004)
33 * This driver needs external firmware. Please use the command
34 * "<kerneldir>/Documentation/dvb/get_dvb_firmware nxt2002" or
35 * "<kerneldir>/Documentation/dvb/get_dvb_firmware nxt2004" to
36 * download/extract the appropriate firmware, and then copy it to
37 * /usr/lib/hotplug/firmware/ or /lib/firmware/
38 * (depending on configuration of firmware hotplug).
40 #define NXT2002_DEFAULT_FIRMWARE "dvb-fe-nxt2002.fw"
41 #define NXT2004_DEFAULT_FIRMWARE "dvb-fe-nxt2004.fw"
42 #define CRC_CCIT_MASK 0x1021
44 #include <linux/kernel.h>
45 #include <linux/init.h>
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/slab.h>
49 #include <linux/string.h>
51 #include "dvb_frontend.h"
55 struct nxt200x_state
{
57 struct i2c_adapter
* i2c
;
58 const struct nxt200x_config
* config
;
59 struct dvb_frontend frontend
;
61 /* demodulator private data */
62 nxt_chip_type demod_chip
;
67 #define dprintk(args...) \
69 if (debug) printk(KERN_DEBUG "nxt200x: " args); \
72 static int i2c_writebytes (struct nxt200x_state
* state
, u8 addr
, u8
*buf
, u8 len
)
75 struct i2c_msg msg
= { .addr
= addr
, .flags
= 0, .buf
= buf
, .len
= len
};
77 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
78 printk (KERN_WARNING
"nxt200x: %s: i2c write error (addr 0x%02x, err == %i)\n",
79 __FUNCTION__
, addr
, err
);
85 static u8
i2c_readbytes (struct nxt200x_state
* state
, u8 addr
, u8
* buf
, u8 len
)
88 struct i2c_msg msg
= { .addr
= addr
, .flags
= I2C_M_RD
, .buf
= buf
, .len
= len
};
90 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
91 printk (KERN_WARNING
"nxt200x: %s: i2c read error (addr 0x%02x, err == %i)\n",
92 __FUNCTION__
, addr
, err
);
98 static int nxt200x_writebytes (struct nxt200x_state
* state
, u8 reg
, u8
*buf
, u8 len
)
102 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= buf2
, .len
= len
+ 1 };
105 memcpy(&buf2
[1], buf
, len
);
107 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
108 printk (KERN_WARNING
"nxt200x: %s: i2c write error (addr 0x%02x, err == %i)\n",
109 __FUNCTION__
, state
->config
->demod_address
, err
);
115 static u8
nxt200x_readbytes (struct nxt200x_state
* state
, u8 reg
, u8
* buf
, u8 len
)
117 u8 reg2
[] = { reg
};
119 struct i2c_msg msg
[] = { { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= reg2
, .len
= 1 },
120 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
, .buf
= buf
, .len
= len
} };
124 if ((err
= i2c_transfer (state
->i2c
, msg
, 2)) != 2) {
125 printk (KERN_WARNING
"nxt200x: %s: i2c read error (addr 0x%02x, err == %i)\n",
126 __FUNCTION__
, state
->config
->demod_address
, err
);
132 static u16
nxt200x_crc(u16 crc
, u8 c
)
135 u16 input
= (u16
) c
& 0xFF;
139 if((crc
^input
) & 0x8000)
140 crc
=(crc
<<1)^CRC_CCIT_MASK
;
148 static int nxt200x_writereg_multibyte (struct nxt200x_state
* state
, u8 reg
, u8
* data
, u8 len
)
151 dprintk("%s\n", __FUNCTION__
);
153 /* set mutli register register */
154 nxt200x_writebytes(state
, 0x35, ®
, 1);
156 /* send the actual data */
157 nxt200x_writebytes(state
, 0x36, data
, len
);
159 switch (state
->demod_chip
) {
165 /* probably not right, but gives correct values */
173 len2
= ((attr
<< 4) | 0x10) | len
;
181 /* set multi register length */
182 nxt200x_writebytes(state
, 0x34, &len2
, 1);
184 /* toggle the multireg write bit */
185 nxt200x_writebytes(state
, 0x21, &buf
, 1);
187 nxt200x_readbytes(state
, 0x21, &buf
, 1);
189 switch (state
->demod_chip
) {
191 if ((buf
& 0x02) == 0)
203 printk(KERN_WARNING
"nxt200x: Error writing multireg register 0x%02X\n",reg
);
208 static int nxt200x_readreg_multibyte (struct nxt200x_state
* state
, u8 reg
, u8
* data
, u8 len
)
212 dprintk("%s\n", __FUNCTION__
);
214 /* set mutli register register */
215 nxt200x_writebytes(state
, 0x35, ®
, 1);
217 switch (state
->demod_chip
) {
219 /* set multi register length */
221 nxt200x_writebytes(state
, 0x34, &len2
, 1);
223 /* read the actual data */
224 nxt200x_readbytes(state
, reg
, data
, len
);
228 /* probably not right, but gives correct values */
236 /* set multi register length */
237 len2
= (attr
<< 4) | len
;
238 nxt200x_writebytes(state
, 0x34, &len2
, 1);
240 /* toggle the multireg bit*/
242 nxt200x_writebytes(state
, 0x21, &buf
, 1);
244 /* read the actual data */
245 for(i
= 0; i
< len
; i
++) {
246 nxt200x_readbytes(state
, 0x36 + i
, &data
[i
], 1);
256 static void nxt200x_microcontroller_stop (struct nxt200x_state
* state
)
258 u8 buf
, stopval
, counter
= 0;
259 dprintk("%s\n", __FUNCTION__
);
261 /* set correct stop value */
262 switch (state
->demod_chip
) {
275 nxt200x_writebytes(state
, 0x22, &buf
, 1);
277 while (counter
< 20) {
278 nxt200x_readbytes(state
, 0x31, &buf
, 1);
285 printk(KERN_WARNING
"nxt200x: Timeout waiting for nxt200x to stop. This is ok after firmware upload.\n");
289 static void nxt200x_microcontroller_start (struct nxt200x_state
* state
)
292 dprintk("%s\n", __FUNCTION__
);
295 nxt200x_writebytes(state
, 0x22, &buf
, 1);
298 static void nxt2004_microcontroller_init (struct nxt200x_state
* state
)
302 dprintk("%s\n", __FUNCTION__
);
305 nxt200x_writebytes(state
, 0x2b, buf
, 1);
307 nxt200x_writebytes(state
, 0x34, buf
, 1);
309 nxt200x_writebytes(state
, 0x35, buf
, 1);
310 buf
[0] = 0x01; buf
[1] = 0x23; buf
[2] = 0x45; buf
[3] = 0x67; buf
[4] = 0x89;
311 buf
[5] = 0xAB; buf
[6] = 0xCD; buf
[7] = 0xEF; buf
[8] = 0xC0;
312 nxt200x_writebytes(state
, 0x36, buf
, 9);
314 nxt200x_writebytes(state
, 0x21, buf
, 1);
316 while (counter
< 20) {
317 nxt200x_readbytes(state
, 0x21, buf
, 1);
324 printk(KERN_WARNING
"nxt200x: Timeout waiting for nxt2004 to init.\n");
329 static int nxt200x_writetuner (struct nxt200x_state
* state
, u8
* data
)
333 dprintk("%s\n", __FUNCTION__
);
335 dprintk("Tuner Bytes: %02X %02X %02X %02X\n", data
[1], data
[2], data
[3], data
[4]);
337 /* if NXT2004, write directly to tuner. if NXT2002, write through NXT chip.
338 * direct write is required for Philips TUV1236D and ALPS TDHU2 */
339 switch (state
->demod_chip
) {
341 if (i2c_writebytes(state
, data
[0], data
+1, 4))
342 printk(KERN_WARNING
"nxt200x: error writing to tuner\n");
343 /* wait until we have a lock */
345 i2c_readbytes(state
, data
[0], &buf
, 1);
351 printk("nxt2004: timeout waiting for tuner lock\n");
354 /* set the i2c transfer speed to the tuner */
356 nxt200x_writebytes(state
, 0x20, &buf
, 1);
358 /* setup to transfer 4 bytes via i2c */
360 nxt200x_writebytes(state
, 0x34, &buf
, 1);
362 /* write actual tuner bytes */
363 nxt200x_writebytes(state
, 0x36, data
+1, 4);
365 /* set tuner i2c address */
367 nxt200x_writebytes(state
, 0x35, &buf
, 1);
369 /* write UC Opmode to begin transfer */
371 nxt200x_writebytes(state
, 0x21, &buf
, 1);
374 nxt200x_readbytes(state
, 0x21, &buf
, 1);
375 if ((buf
& 0x80)== 0x00)
380 printk("nxt2002: timeout error writing tuner\n");
389 static void nxt200x_agc_reset(struct nxt200x_state
* state
)
392 dprintk("%s\n", __FUNCTION__
);
394 switch (state
->demod_chip
) {
397 nxt200x_writebytes(state
, 0x08, &buf
, 1);
399 nxt200x_writebytes(state
, 0x08, &buf
, 1);
402 nxt200x_readreg_multibyte(state
, 0x08, &buf
, 1);
404 nxt200x_writereg_multibyte(state
, 0x08, &buf
, 1);
406 nxt200x_writereg_multibyte(state
, 0x08, &buf
, 1);
414 static int nxt2002_load_firmware (struct dvb_frontend
* fe
, const struct firmware
*fw
)
417 struct nxt200x_state
* state
= fe
->demodulator_priv
;
418 u8 buf
[3], written
= 0, chunkpos
= 0;
419 u16 rambase
, position
, crc
= 0;
421 dprintk("%s\n", __FUNCTION__
);
422 dprintk("Firmware is %zu bytes\n", fw
->size
);
424 /* Get the RAM base for this nxt2002 */
425 nxt200x_readbytes(state
, 0x10, buf
, 1);
432 dprintk("rambase on this nxt2002 is %04X\n", rambase
);
434 /* Hold the micro in reset while loading firmware */
436 nxt200x_writebytes(state
, 0x2B, buf
, 1);
438 for (position
= 0; position
< fw
->size
; position
++) {
442 buf
[0] = ((rambase
+ position
) >> 8);
443 buf
[1] = (rambase
+ position
) & 0xFF;
445 /* write starting address */
446 nxt200x_writebytes(state
, 0x29, buf
, 3);
451 if ((written
% 4) == 0)
452 nxt200x_writebytes(state
, chunkpos
, &fw
->data
[position
-3], 4);
454 crc
= nxt200x_crc(crc
, fw
->data
[position
]);
456 if ((written
== 255) || (position
+1 == fw
->size
)) {
457 /* write remaining bytes of firmware */
458 nxt200x_writebytes(state
, chunkpos
+4-(written
%4),
459 &fw
->data
[position
-(written
%4) + 1],
465 nxt200x_writebytes(state
, 0x2C, buf
, 2);
467 /* do a read to stop things */
468 nxt200x_readbytes(state
, 0x2A, buf
, 1);
470 /* set transfer mode to complete */
472 nxt200x_writebytes(state
, 0x2B, buf
, 1);
481 static int nxt2004_load_firmware (struct dvb_frontend
* fe
, const struct firmware
*fw
)
484 struct nxt200x_state
* state
= fe
->demodulator_priv
;
486 u16 rambase
, position
, crc
=0;
488 dprintk("%s\n", __FUNCTION__
);
489 dprintk("Firmware is %zu bytes\n", fw
->size
);
494 /* hold the micro in reset while loading firmware */
496 nxt200x_writebytes(state
, 0x2B, buf
,1);
498 /* calculate firmware CRC */
499 for (position
= 0; position
< fw
->size
; position
++) {
500 crc
= nxt200x_crc(crc
, fw
->data
[position
]);
503 buf
[0] = rambase
>> 8;
504 buf
[1] = rambase
& 0xFF;
506 /* write starting address */
507 nxt200x_writebytes(state
,0x29,buf
,3);
509 for (position
= 0; position
< fw
->size
;) {
510 nxt200x_writebytes(state
, 0x2C, &fw
->data
[position
],
511 fw
->size
-position
> 255 ? 255 : fw
->size
-position
);
512 position
+= (fw
->size
-position
> 255 ? 255 : fw
->size
-position
);
517 dprintk("firmware crc is 0x%02X 0x%02X\n", buf
[0], buf
[1]);
520 nxt200x_writebytes(state
, 0x2C, buf
,2);
522 /* do a read to stop things */
523 nxt200x_readbytes(state
, 0x2C, buf
, 1);
525 /* set transfer mode to complete */
527 nxt200x_writebytes(state
, 0x2B, buf
,1);
532 static int nxt200x_setup_frontend_parameters (struct dvb_frontend
* fe
,
533 struct dvb_frontend_parameters
*p
)
535 struct nxt200x_state
* state
= fe
->demodulator_priv
;
538 /* stop the micro first */
539 nxt200x_microcontroller_stop(state
);
541 if (state
->demod_chip
== NXT2004
) {
542 /* make sure demod is set to digital */
544 nxt200x_writebytes(state
, 0x14, buf
, 1);
546 nxt200x_writebytes(state
, 0x17, buf
, 1);
549 /* get tuning information */
550 if (fe
->ops
.tuner_ops
.calc_regs
) {
551 fe
->ops
.tuner_ops
.calc_regs(fe
, p
, buf
, 5);
554 /* set additional params */
555 switch (p
->u
.vsb
.modulation
) {
558 /* Set punctured clock for QAM */
559 /* This is just a guess since I am unable to test it */
560 if (state
->config
->set_ts_params
)
561 state
->config
->set_ts_params(fe
, 1);
564 if (state
->config
->set_pll_input
)
565 state
->config
->set_pll_input(buf
+1, 1);
568 /* Set non-punctured clock for VSB */
569 if (state
->config
->set_ts_params
)
570 state
->config
->set_ts_params(fe
, 0);
573 if (state
->config
->set_pll_input
)
574 state
->config
->set_pll_input(buf
+1, 0);
581 /* write frequency information */
582 nxt200x_writetuner(state
, buf
);
584 /* reset the agc now that tuning has been completed */
585 nxt200x_agc_reset(state
);
587 /* set target power level */
588 switch (p
->u
.vsb
.modulation
) {
600 nxt200x_writebytes(state
, 0x42, buf
, 1);
603 switch (state
->demod_chip
) {
614 nxt200x_writebytes(state
, 0x57, buf
, 1);
616 /* write sdm1 input */
619 switch (state
->demod_chip
) {
621 nxt200x_writereg_multibyte(state
, 0x58, buf
, 2);
624 nxt200x_writebytes(state
, 0x58, buf
, 2);
631 /* write sdmx input */
632 switch (p
->u
.vsb
.modulation
) {
647 switch (state
->demod_chip
) {
649 nxt200x_writereg_multibyte(state
, 0x5C, buf
, 2);
652 nxt200x_writebytes(state
, 0x5C, buf
, 2);
659 /* write adc power lpf fc */
661 nxt200x_writebytes(state
, 0x43, buf
, 1);
663 if (state
->demod_chip
== NXT2004
) {
667 nxt200x_writebytes(state
, 0x46, buf
, 2);
670 /* write accumulator2 input */
673 switch (state
->demod_chip
) {
675 nxt200x_writereg_multibyte(state
, 0x4B, buf
, 2);
678 nxt200x_writebytes(state
, 0x4B, buf
, 2);
687 nxt200x_writebytes(state
, 0x4D, buf
, 1);
689 /* write sdm12 lpf fc */
691 nxt200x_writebytes(state
, 0x55, buf
, 1);
693 /* write agc control reg */
695 nxt200x_writebytes(state
, 0x41, buf
, 1);
697 if (state
->demod_chip
== NXT2004
) {
698 nxt200x_readreg_multibyte(state
, 0x80, buf
, 1);
700 nxt200x_writereg_multibyte(state
, 0x80, buf
, 1);
703 nxt200x_readreg_multibyte(state
, 0x08, buf
, 1);
705 nxt200x_writereg_multibyte(state
, 0x08, buf
, 1);
706 nxt200x_readreg_multibyte(state
, 0x08, buf
, 1);
708 nxt200x_writereg_multibyte(state
, 0x08, buf
, 1);
710 nxt200x_readreg_multibyte(state
, 0x80, buf
, 1);
712 nxt200x_writereg_multibyte(state
, 0x80, buf
, 1);
714 nxt200x_writereg_multibyte(state
, 0x81, buf
, 1);
715 buf
[0] = 0x80; buf
[1] = 0x00; buf
[2] = 0x00;
716 nxt200x_writereg_multibyte(state
, 0x82, buf
, 3);
717 nxt200x_readreg_multibyte(state
, 0x88, buf
, 1);
719 nxt200x_writereg_multibyte(state
, 0x88, buf
, 1);
720 nxt200x_readreg_multibyte(state
, 0x80, buf
, 1);
722 nxt200x_writereg_multibyte(state
, 0x80, buf
, 1);
725 /* write agc ucgp0 */
726 switch (p
->u
.vsb
.modulation
) {
740 nxt200x_writebytes(state
, 0x30, buf
, 1);
742 /* write agc control reg */
744 nxt200x_writebytes(state
, 0x41, buf
, 1);
746 /* write accumulator2 input */
749 switch (state
->demod_chip
) {
751 nxt200x_writereg_multibyte(state
, 0x49, buf
, 2);
752 nxt200x_writereg_multibyte(state
, 0x4B, buf
, 2);
755 nxt200x_writebytes(state
, 0x49, buf
, 2);
756 nxt200x_writebytes(state
, 0x4B, buf
, 2);
763 /* write agc control reg */
765 nxt200x_writebytes(state
, 0x41, buf
, 1);
767 nxt200x_microcontroller_start(state
);
769 if (state
->demod_chip
== NXT2004
) {
770 nxt2004_microcontroller_init(state
);
775 nxt200x_writebytes(state
, 0x5C, buf
, 2);
778 /* adjacent channel detection should be done here, but I don't
779 have any stations with this need so I cannot test it */
784 static int nxt200x_read_status(struct dvb_frontend
* fe
, fe_status_t
* status
)
786 struct nxt200x_state
* state
= fe
->demodulator_priv
;
788 nxt200x_readbytes(state
, 0x31, &lock
, 1);
792 *status
|= FE_HAS_SIGNAL
;
793 *status
|= FE_HAS_CARRIER
;
794 *status
|= FE_HAS_VITERBI
;
795 *status
|= FE_HAS_SYNC
;
796 *status
|= FE_HAS_LOCK
;
801 static int nxt200x_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
803 struct nxt200x_state
* state
= fe
->demodulator_priv
;
806 nxt200x_readreg_multibyte(state
, 0xE6, b
, 3);
808 *ber
= ((b
[0] << 8) + b
[1]) * 8;
813 static int nxt200x_read_signal_strength(struct dvb_frontend
* fe
, u16
* strength
)
815 struct nxt200x_state
* state
= fe
->demodulator_priv
;
819 /* setup to read cluster variance */
821 nxt200x_writebytes(state
, 0xA1, b
, 1);
823 /* get multreg val */
824 nxt200x_readreg_multibyte(state
, 0xA6, b
, 2);
826 temp
= (b
[0] << 8) | b
[1];
827 *strength
= ((0x7FFF - temp
) & 0x0FFF) * 16;
832 static int nxt200x_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
835 struct nxt200x_state
* state
= fe
->demodulator_priv
;
840 /* setup to read cluster variance */
842 nxt200x_writebytes(state
, 0xA1, b
, 1);
844 /* get multreg val from 0xA6 */
845 nxt200x_readreg_multibyte(state
, 0xA6, b
, 2);
847 temp
= (b
[0] << 8) | b
[1];
848 temp2
= 0x7FFF - temp
;
850 /* snr will be in db */
852 snrdb
= 1000*24 + ( 1000*(30-24) * ( temp2
- 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
853 else if (temp2
> 0x7EC0)
854 snrdb
= 1000*18 + ( 1000*(24-18) * ( temp2
- 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
855 else if (temp2
> 0x7C00)
856 snrdb
= 1000*12 + ( 1000*(18-12) * ( temp2
- 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
858 snrdb
= 1000*0 + ( 1000*(12-0) * ( temp2
- 0 ) / ( 0x7C00 - 0 ) );
860 /* the value reported back from the frontend will be FFFF=32db 0000=0db */
861 *snr
= snrdb
* (0xFFFF/32000);
866 static int nxt200x_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
868 struct nxt200x_state
* state
= fe
->demodulator_priv
;
871 nxt200x_readreg_multibyte(state
, 0xE6, b
, 3);
877 static int nxt200x_sleep(struct dvb_frontend
* fe
)
882 static int nxt2002_init(struct dvb_frontend
* fe
)
884 struct nxt200x_state
* state
= fe
->demodulator_priv
;
885 const struct firmware
*fw
;
889 /* request the firmware, this will block until someone uploads it */
890 printk("nxt2002: Waiting for firmware upload (%s)...\n", NXT2002_DEFAULT_FIRMWARE
);
891 ret
= request_firmware(&fw
, NXT2002_DEFAULT_FIRMWARE
, &state
->i2c
->dev
);
892 printk("nxt2002: Waiting for firmware upload(2)...\n");
894 printk("nxt2002: No firmware uploaded (timeout or file not found?)\n");
898 ret
= nxt2002_load_firmware(fe
, fw
);
899 release_firmware(fw
);
901 printk("nxt2002: Writing firmware to device failed\n");
904 printk("nxt2002: Firmware upload complete\n");
906 /* Put the micro into reset */
907 nxt200x_microcontroller_stop(state
);
909 /* ensure transfer is complete */
911 nxt200x_writebytes(state
, 0x2B, buf
, 1);
913 /* Put the micro into reset for real this time */
914 nxt200x_microcontroller_stop(state
);
916 /* soft reset everything (agc,frontend,eq,fec)*/
918 nxt200x_writebytes(state
, 0x08, buf
, 1);
920 nxt200x_writebytes(state
, 0x08, buf
, 1);
922 /* write agc sdm configure */
924 nxt200x_writebytes(state
, 0x57, buf
, 1);
926 /* write mod output format */
928 nxt200x_writebytes(state
, 0x09, buf
, 1);
930 /* write fec mpeg mode */
933 nxt200x_writebytes(state
, 0xE9, buf
, 2);
935 /* write mux selection */
937 nxt200x_writebytes(state
, 0xCC, buf
, 1);
942 static int nxt2004_init(struct dvb_frontend
* fe
)
944 struct nxt200x_state
* state
= fe
->demodulator_priv
;
945 const struct firmware
*fw
;
951 nxt200x_writebytes(state
, 0x1E, buf
, 1);
953 /* request the firmware, this will block until someone uploads it */
954 printk("nxt2004: Waiting for firmware upload (%s)...\n", NXT2004_DEFAULT_FIRMWARE
);
955 ret
= request_firmware(&fw
, NXT2004_DEFAULT_FIRMWARE
, &state
->i2c
->dev
);
956 printk("nxt2004: Waiting for firmware upload(2)...\n");
958 printk("nxt2004: No firmware uploaded (timeout or file not found?)\n");
962 ret
= nxt2004_load_firmware(fe
, fw
);
963 release_firmware(fw
);
965 printk("nxt2004: Writing firmware to device failed\n");
968 printk("nxt2004: Firmware upload complete\n");
970 /* ensure transfer is complete */
972 nxt200x_writebytes(state
, 0x19, buf
, 1);
974 nxt2004_microcontroller_init(state
);
975 nxt200x_microcontroller_stop(state
);
976 nxt200x_microcontroller_stop(state
);
977 nxt2004_microcontroller_init(state
);
978 nxt200x_microcontroller_stop(state
);
980 /* soft reset everything (agc,frontend,eq,fec)*/
982 nxt200x_writereg_multibyte(state
, 0x08, buf
, 1);
984 nxt200x_writereg_multibyte(state
, 0x08, buf
, 1);
986 /* write agc sdm configure */
988 nxt200x_writebytes(state
, 0x57, buf
, 1);
993 nxt200x_writebytes(state
, 0x35, buf
, 2);
995 nxt200x_writebytes(state
, 0x34, buf
, 1);
997 nxt200x_writebytes(state
, 0x21, buf
, 1);
1001 nxt200x_writebytes(state
, 0x0A, buf
, 1);
1005 nxt200x_writereg_multibyte(state
, 0x80, buf
, 1);
1007 /* write fec mpeg mode */
1010 nxt200x_writebytes(state
, 0xE9, buf
, 2);
1012 /* write mux selection */
1014 nxt200x_writebytes(state
, 0xCC, buf
, 1);
1017 nxt200x_readreg_multibyte(state
, 0x80, buf
, 1);
1019 nxt200x_writereg_multibyte(state
, 0x80, buf
, 1);
1022 nxt200x_readreg_multibyte(state
, 0x08, buf
, 1);
1024 nxt200x_writereg_multibyte(state
, 0x08, buf
, 1);
1025 nxt200x_readreg_multibyte(state
, 0x08, buf
, 1);
1027 nxt200x_writereg_multibyte(state
, 0x08, buf
, 1);
1030 nxt200x_readreg_multibyte(state
, 0x80, buf
, 1);
1032 nxt200x_writereg_multibyte(state
, 0x80, buf
, 1);
1034 nxt200x_writereg_multibyte(state
, 0x81, buf
, 1);
1035 buf
[0] = 0x31; buf
[1] = 0x5E; buf
[2] = 0x66;
1036 nxt200x_writereg_multibyte(state
, 0x82, buf
, 3);
1038 nxt200x_readreg_multibyte(state
, 0x88, buf
, 1);
1040 nxt200x_writereg_multibyte(state
, 0x88, buf
, 1);
1041 nxt200x_readreg_multibyte(state
, 0x80, buf
, 1);
1043 nxt200x_writereg_multibyte(state
, 0x80, buf
, 1);
1045 nxt200x_readbytes(state
, 0x10, buf
, 1);
1047 nxt200x_writebytes(state
, 0x10, buf
, 1);
1048 nxt200x_readbytes(state
, 0x0A, buf
, 1);
1050 nxt200x_writebytes(state
, 0x0A, buf
, 1);
1052 nxt2004_microcontroller_init(state
);
1055 nxt200x_writebytes(state
, 0x0A, buf
, 1);
1057 nxt200x_writebytes(state
, 0xE9, buf
, 1);
1059 nxt200x_writebytes(state
, 0xEA, buf
, 1);
1061 nxt200x_readreg_multibyte(state
, 0x80, buf
, 1);
1063 nxt200x_writereg_multibyte(state
, 0x80, buf
, 1);
1064 nxt200x_readreg_multibyte(state
, 0x80, buf
, 1);
1066 nxt200x_writereg_multibyte(state
, 0x80, buf
, 1);
1069 nxt200x_readreg_multibyte(state
, 0x08, buf
, 1);
1071 nxt200x_writereg_multibyte(state
, 0x08, buf
, 1);
1072 nxt200x_readreg_multibyte(state
, 0x08, buf
, 1);
1074 nxt200x_writereg_multibyte(state
, 0x08, buf
, 1);
1076 nxt200x_readreg_multibyte(state
, 0x80, buf
, 1);
1078 nxt200x_writereg_multibyte(state
, 0x80, buf
, 1);
1080 nxt200x_writereg_multibyte(state
, 0x81, buf
, 1);
1081 buf
[0] = 0x80; buf
[1] = 0x00; buf
[2] = 0x00;
1082 nxt200x_writereg_multibyte(state
, 0x82, buf
, 3);
1084 nxt200x_readreg_multibyte(state
, 0x88, buf
, 1);
1086 nxt200x_writereg_multibyte(state
, 0x88, buf
, 1);
1088 nxt200x_readreg_multibyte(state
, 0x80, buf
, 1);
1090 nxt200x_writereg_multibyte(state
, 0x80, buf
, 1);
1092 /* initialize tuner */
1093 nxt200x_readbytes(state
, 0x10, buf
, 1);
1095 nxt200x_writebytes(state
, 0x10, buf
, 1);
1097 nxt200x_writebytes(state
, 0x13, buf
, 1);
1099 nxt200x_writebytes(state
, 0x16, buf
, 1);
1101 nxt200x_writebytes(state
, 0x14, buf
, 1);
1103 nxt200x_writebytes(state
, 0x14, buf
, 1);
1104 nxt200x_writebytes(state
, 0x17, buf
, 1);
1105 nxt200x_writebytes(state
, 0x14, buf
, 1);
1106 nxt200x_writebytes(state
, 0x17, buf
, 1);
1111 static int nxt200x_init(struct dvb_frontend
* fe
)
1113 struct nxt200x_state
* state
= fe
->demodulator_priv
;
1116 if (!state
->initialised
) {
1117 switch (state
->demod_chip
) {
1119 ret
= nxt2002_init(fe
);
1122 ret
= nxt2004_init(fe
);
1128 state
->initialised
= 1;
1133 static int nxt200x_get_tune_settings(struct dvb_frontend
* fe
, struct dvb_frontend_tune_settings
* fesettings
)
1135 fesettings
->min_delay_ms
= 500;
1136 fesettings
->step_size
= 0;
1137 fesettings
->max_drift
= 0;
1141 static void nxt200x_release(struct dvb_frontend
* fe
)
1143 struct nxt200x_state
* state
= fe
->demodulator_priv
;
1147 static struct dvb_frontend_ops nxt200x_ops
;
1149 struct dvb_frontend
* nxt200x_attach(const struct nxt200x_config
* config
,
1150 struct i2c_adapter
* i2c
)
1152 struct nxt200x_state
* state
= NULL
;
1153 u8 buf
[] = {0,0,0,0,0};
1155 /* allocate memory for the internal state */
1156 state
= kzalloc(sizeof(struct nxt200x_state
), GFP_KERNEL
);
1160 /* setup the state */
1161 state
->config
= config
;
1163 state
->initialised
= 0;
1166 nxt200x_readbytes(state
, 0x00, buf
, 5);
1167 dprintk("NXT info: %02X %02X %02X %02X %02X\n",
1168 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4]);
1170 /* set demod chip */
1173 state
->demod_chip
= NXT2002
;
1174 printk("nxt200x: NXT2002 Detected\n");
1177 state
->demod_chip
= NXT2004
;
1178 printk("nxt200x: NXT2004 Detected\n");
1184 /* make sure demod chip is supported */
1185 switch (state
->demod_chip
) {
1187 if (buf
[0] != 0x04) goto error
; /* device id */
1188 if (buf
[1] != 0x02) goto error
; /* fab id */
1189 if (buf
[2] != 0x11) goto error
; /* month */
1190 if (buf
[3] != 0x20) goto error
; /* year msb */
1191 if (buf
[4] != 0x00) goto error
; /* year lsb */
1194 if (buf
[0] != 0x05) goto error
; /* device id */
1200 /* create dvb_frontend */
1201 memcpy(&state
->frontend
.ops
, &nxt200x_ops
, sizeof(struct dvb_frontend_ops
));
1202 state
->frontend
.demodulator_priv
= state
;
1203 return &state
->frontend
;
1207 printk("Unknown/Unsupported NXT chip: %02X %02X %02X %02X %02X\n",
1208 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4]);
1212 static struct dvb_frontend_ops nxt200x_ops
= {
1215 .name
= "Nextwave NXT200X VSB/QAM frontend",
1217 .frequency_min
= 54000000,
1218 .frequency_max
= 860000000,
1219 .frequency_stepsize
= 166666, /* stepsize is just a guess */
1220 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
1221 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
1222 FE_CAN_8VSB
| FE_CAN_QAM_64
| FE_CAN_QAM_256
1225 .release
= nxt200x_release
,
1227 .init
= nxt200x_init
,
1228 .sleep
= nxt200x_sleep
,
1230 .set_frontend
= nxt200x_setup_frontend_parameters
,
1231 .get_tune_settings
= nxt200x_get_tune_settings
,
1233 .read_status
= nxt200x_read_status
,
1234 .read_ber
= nxt200x_read_ber
,
1235 .read_signal_strength
= nxt200x_read_signal_strength
,
1236 .read_snr
= nxt200x_read_snr
,
1237 .read_ucblocks
= nxt200x_read_ucblocks
,
1240 module_param(debug
, int, 0644);
1241 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
1243 MODULE_DESCRIPTION("NXT200X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
1244 MODULE_AUTHOR("Kirk Lapray, Michael Krufky, Jean-Francois Thibert, and Taylor Jacob");
1245 MODULE_LICENSE("GPL");
1247 EXPORT_SYMBOL(nxt200x_attach
);