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 #define CRC_CCIT_MASK 0x1021
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
34 #include "dvb_frontend.h"
37 struct nxt200x_state {
39 struct i2c_adapter* i2c;
40 const struct nxt200x_config* config;
41 struct dvb_frontend frontend;
43 /* demodulator private data */
44 nxt_chip_type demod_chip;
49 #define dprintk(args...) \
51 if (debug) printk(KERN_DEBUG "nxt200x: " args); \
54 static int i2c_writebytes (struct nxt200x_state* state, u8 addr, u8 *buf, u8 len)
57 struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = len };
59 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
60 printk (KERN_WARNING "nxt200x: %s: i2c write error (addr 0x%02x, err == %i)\n",
67 static int i2c_readbytes(struct nxt200x_state *state, u8 addr, u8 *buf, u8 len)
70 struct i2c_msg msg = { .addr = addr, .flags = I2C_M_RD, .buf = buf, .len = len };
72 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
73 printk (KERN_WARNING "nxt200x: %s: i2c read error (addr 0x%02x, err == %i)\n",
80 static int nxt200x_writebytes (struct nxt200x_state* state, u8 reg,
81 const u8 *buf, u8 len)
85 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf2, .len = len + 1 };
88 memcpy(&buf2[1], buf, len);
90 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
91 printk (KERN_WARNING "nxt200x: %s: i2c write error (addr 0x%02x, err == %i)\n",
92 __func__, state->config->demod_address, err);
98 static int nxt200x_readbytes(struct nxt200x_state *state, u8 reg, u8 *buf, u8 len)
100 u8 reg2 [] = { reg };
102 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = reg2, .len = 1 },
103 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = buf, .len = len } };
107 if ((err = i2c_transfer (state->i2c, msg, 2)) != 2) {
108 printk (KERN_WARNING "nxt200x: %s: i2c read error (addr 0x%02x, err == %i)\n",
109 __func__, state->config->demod_address, err);
115 static u16 nxt200x_crc(u16 crc, u8 c)
118 u16 input = (u16) c & 0xFF;
122 if((crc^input) & 0x8000)
123 crc=(crc<<1)^CRC_CCIT_MASK;
131 static int nxt200x_writereg_multibyte (struct nxt200x_state* state, u8 reg, u8* data, u8 len)
134 dprintk("%s\n", __func__);
136 /* set mutli register register */
137 nxt200x_writebytes(state, 0x35, ®, 1);
139 /* send the actual data */
140 nxt200x_writebytes(state, 0x36, data, len);
142 switch (state->demod_chip) {
148 /* probably not right, but gives correct values */
156 len2 = ((attr << 4) | 0x10) | len;
164 /* set multi register length */
165 nxt200x_writebytes(state, 0x34, &len2, 1);
167 /* toggle the multireg write bit */
168 nxt200x_writebytes(state, 0x21, &buf, 1);
170 nxt200x_readbytes(state, 0x21, &buf, 1);
172 switch (state->demod_chip) {
174 if ((buf & 0x02) == 0)
186 printk(KERN_WARNING "nxt200x: Error writing multireg register 0x%02X\n",reg);
191 static int nxt200x_readreg_multibyte (struct nxt200x_state* state, u8 reg, u8* data, u8 len)
195 dprintk("%s\n", __func__);
197 /* set mutli register register */
198 nxt200x_writebytes(state, 0x35, ®, 1);
200 switch (state->demod_chip) {
202 /* set multi register length */
204 nxt200x_writebytes(state, 0x34, &len2, 1);
206 /* read the actual data */
207 nxt200x_readbytes(state, reg, data, len);
211 /* probably not right, but gives correct values */
219 /* set multi register length */
220 len2 = (attr << 4) | len;
221 nxt200x_writebytes(state, 0x34, &len2, 1);
223 /* toggle the multireg bit*/
225 nxt200x_writebytes(state, 0x21, &buf, 1);
227 /* read the actual data */
228 for(i = 0; i < len; i++) {
229 nxt200x_readbytes(state, 0x36 + i, &data[i], 1);
239 static void nxt200x_microcontroller_stop (struct nxt200x_state* state)
241 u8 buf, stopval, counter = 0;
242 dprintk("%s\n", __func__);
244 /* set correct stop value */
245 switch (state->demod_chip) {
258 nxt200x_writebytes(state, 0x22, &buf, 1);
260 while (counter < 20) {
261 nxt200x_readbytes(state, 0x31, &buf, 1);
268 printk(KERN_WARNING "nxt200x: Timeout waiting for nxt200x to stop. This is ok after firmware upload.\n");
272 static void nxt200x_microcontroller_start (struct nxt200x_state* state)
275 dprintk("%s\n", __func__);
278 nxt200x_writebytes(state, 0x22, &buf, 1);
281 static void nxt2004_microcontroller_init (struct nxt200x_state* state)
285 dprintk("%s\n", __func__);
288 nxt200x_writebytes(state, 0x2b, buf, 1);
290 nxt200x_writebytes(state, 0x34, buf, 1);
292 nxt200x_writebytes(state, 0x35, buf, 1);
293 buf[0] = 0x01; buf[1] = 0x23; buf[2] = 0x45; buf[3] = 0x67; buf[4] = 0x89;
294 buf[5] = 0xAB; buf[6] = 0xCD; buf[7] = 0xEF; buf[8] = 0xC0;
295 nxt200x_writebytes(state, 0x36, buf, 9);
297 nxt200x_writebytes(state, 0x21, buf, 1);
299 while (counter < 20) {
300 nxt200x_readbytes(state, 0x21, buf, 1);
307 printk(KERN_WARNING "nxt200x: Timeout waiting for nxt2004 to init.\n");
312 static int nxt200x_writetuner (struct nxt200x_state* state, u8* data)
316 dprintk("%s\n", __func__);
318 dprintk("Tuner Bytes: %02X %02X %02X %02X\n", data[1], data[2], data[3], data[4]);
320 /* if NXT2004, write directly to tuner. if NXT2002, write through NXT chip.
321 * direct write is required for Philips TUV1236D and ALPS TDHU2 */
322 switch (state->demod_chip) {
324 if (i2c_writebytes(state, data[0], data+1, 4))
325 printk(KERN_WARNING "nxt200x: error writing to tuner\n");
326 /* wait until we have a lock */
328 i2c_readbytes(state, data[0], &buf, 1);
334 printk("nxt2004: timeout waiting for tuner lock\n");
337 /* set the i2c transfer speed to the tuner */
339 nxt200x_writebytes(state, 0x20, &buf, 1);
341 /* setup to transfer 4 bytes via i2c */
343 nxt200x_writebytes(state, 0x34, &buf, 1);
345 /* write actual tuner bytes */
346 nxt200x_writebytes(state, 0x36, data+1, 4);
348 /* set tuner i2c address */
350 nxt200x_writebytes(state, 0x35, &buf, 1);
352 /* write UC Opmode to begin transfer */
354 nxt200x_writebytes(state, 0x21, &buf, 1);
357 nxt200x_readbytes(state, 0x21, &buf, 1);
358 if ((buf & 0x80)== 0x00)
363 printk("nxt2002: timeout error writing tuner\n");
372 static void nxt200x_agc_reset(struct nxt200x_state* state)
375 dprintk("%s\n", __func__);
377 switch (state->demod_chip) {
380 nxt200x_writebytes(state, 0x08, &buf, 1);
382 nxt200x_writebytes(state, 0x08, &buf, 1);
385 nxt200x_readreg_multibyte(state, 0x08, &buf, 1);
387 nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
389 nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
397 static int nxt2002_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
400 struct nxt200x_state* state = fe->demodulator_priv;
401 u8 buf[3], written = 0, chunkpos = 0;
402 u16 rambase, position, crc = 0;
404 dprintk("%s\n", __func__);
405 dprintk("Firmware is %zu bytes\n", fw->size);
407 /* Get the RAM base for this nxt2002 */
408 nxt200x_readbytes(state, 0x10, buf, 1);
415 dprintk("rambase on this nxt2002 is %04X\n", rambase);
417 /* Hold the micro in reset while loading firmware */
419 nxt200x_writebytes(state, 0x2B, buf, 1);
421 for (position = 0; position < fw->size; position++) {
425 buf[0] = ((rambase + position) >> 8);
426 buf[1] = (rambase + position) & 0xFF;
428 /* write starting address */
429 nxt200x_writebytes(state, 0x29, buf, 3);
434 if ((written % 4) == 0)
435 nxt200x_writebytes(state, chunkpos, &fw->data[position-3], 4);
437 crc = nxt200x_crc(crc, fw->data[position]);
439 if ((written == 255) || (position+1 == fw->size)) {
440 /* write remaining bytes of firmware */
441 nxt200x_writebytes(state, chunkpos+4-(written %4),
442 &fw->data[position-(written %4) + 1],
448 nxt200x_writebytes(state, 0x2C, buf, 2);
450 /* do a read to stop things */
451 nxt200x_readbytes(state, 0x2A, buf, 1);
453 /* set transfer mode to complete */
455 nxt200x_writebytes(state, 0x2B, buf, 1);
464 static int nxt2004_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
467 struct nxt200x_state* state = fe->demodulator_priv;
469 u16 rambase, position, crc=0;
471 dprintk("%s\n", __func__);
472 dprintk("Firmware is %zu bytes\n", fw->size);
477 /* hold the micro in reset while loading firmware */
479 nxt200x_writebytes(state, 0x2B, buf,1);
481 /* calculate firmware CRC */
482 for (position = 0; position < fw->size; position++) {
483 crc = nxt200x_crc(crc, fw->data[position]);
486 buf[0] = rambase >> 8;
487 buf[1] = rambase & 0xFF;
489 /* write starting address */
490 nxt200x_writebytes(state,0x29,buf,3);
492 for (position = 0; position < fw->size;) {
493 nxt200x_writebytes(state, 0x2C, &fw->data[position],
494 fw->size-position > 255 ? 255 : fw->size-position);
495 position += (fw->size-position > 255 ? 255 : fw->size-position);
500 dprintk("firmware crc is 0x%02X 0x%02X\n", buf[0], buf[1]);
503 nxt200x_writebytes(state, 0x2C, buf,2);
505 /* do a read to stop things */
506 nxt200x_readbytes(state, 0x2C, buf, 1);
508 /* set transfer mode to complete */
510 nxt200x_writebytes(state, 0x2B, buf,1);
515 static int nxt200x_setup_frontend_parameters(struct dvb_frontend *fe)
517 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
518 struct nxt200x_state* state = fe->demodulator_priv;
521 /* stop the micro first */
522 nxt200x_microcontroller_stop(state);
524 if (state->demod_chip == NXT2004) {
525 /* make sure demod is set to digital */
527 nxt200x_writebytes(state, 0x14, buf, 1);
529 nxt200x_writebytes(state, 0x17, buf, 1);
532 /* set additional params */
533 switch (p->modulation) {
536 /* Set punctured clock for QAM */
537 /* This is just a guess since I am unable to test it */
538 if (state->config->set_ts_params)
539 state->config->set_ts_params(fe, 1);
542 /* Set non-punctured clock for VSB */
543 if (state->config->set_ts_params)
544 state->config->set_ts_params(fe, 0);
551 if (fe->ops.tuner_ops.calc_regs) {
552 /* get tuning information */
553 fe->ops.tuner_ops.calc_regs(fe, buf, 5);
555 /* write frequency information */
556 nxt200x_writetuner(state, buf);
559 /* reset the agc now that tuning has been completed */
560 nxt200x_agc_reset(state);
562 /* set target power level */
563 switch (p->modulation) {
575 nxt200x_writebytes(state, 0x42, buf, 1);
578 switch (state->demod_chip) {
589 nxt200x_writebytes(state, 0x57, buf, 1);
591 /* write sdm1 input */
594 switch (state->demod_chip) {
596 nxt200x_writereg_multibyte(state, 0x58, buf, 2);
599 nxt200x_writebytes(state, 0x58, buf, 2);
606 /* write sdmx input */
607 switch (p->modulation) {
622 switch (state->demod_chip) {
624 nxt200x_writereg_multibyte(state, 0x5C, buf, 2);
627 nxt200x_writebytes(state, 0x5C, buf, 2);
634 /* write adc power lpf fc */
636 nxt200x_writebytes(state, 0x43, buf, 1);
638 if (state->demod_chip == NXT2004) {
642 nxt200x_writebytes(state, 0x46, buf, 2);
645 /* write accumulator2 input */
648 switch (state->demod_chip) {
650 nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
653 nxt200x_writebytes(state, 0x4B, buf, 2);
662 nxt200x_writebytes(state, 0x4D, buf, 1);
664 /* write sdm12 lpf fc */
666 nxt200x_writebytes(state, 0x55, buf, 1);
668 /* write agc control reg */
670 nxt200x_writebytes(state, 0x41, buf, 1);
672 if (state->demod_chip == NXT2004) {
673 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
675 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
678 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
680 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
681 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
683 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
685 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
687 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
689 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
690 buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
691 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
692 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
694 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
695 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
697 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
700 /* write agc ucgp0 */
701 switch (p->modulation) {
715 nxt200x_writebytes(state, 0x30, buf, 1);
717 /* write agc control reg */
719 nxt200x_writebytes(state, 0x41, buf, 1);
721 /* write accumulator2 input */
724 switch (state->demod_chip) {
726 nxt200x_writereg_multibyte(state, 0x49, buf, 2);
727 nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
730 nxt200x_writebytes(state, 0x49, buf, 2);
731 nxt200x_writebytes(state, 0x4B, buf, 2);
738 /* write agc control reg */
740 nxt200x_writebytes(state, 0x41, buf, 1);
742 nxt200x_microcontroller_start(state);
744 if (state->demod_chip == NXT2004) {
745 nxt2004_microcontroller_init(state);
750 nxt200x_writebytes(state, 0x5C, buf, 2);
753 /* adjacent channel detection should be done here, but I don't
754 have any stations with this need so I cannot test it */
759 static int nxt200x_read_status(struct dvb_frontend* fe, fe_status_t* status)
761 struct nxt200x_state* state = fe->demodulator_priv;
763 nxt200x_readbytes(state, 0x31, &lock, 1);
767 *status |= FE_HAS_SIGNAL;
768 *status |= FE_HAS_CARRIER;
769 *status |= FE_HAS_VITERBI;
770 *status |= FE_HAS_SYNC;
771 *status |= FE_HAS_LOCK;
776 static int nxt200x_read_ber(struct dvb_frontend* fe, u32* ber)
778 struct nxt200x_state* state = fe->demodulator_priv;
781 nxt200x_readreg_multibyte(state, 0xE6, b, 3);
783 *ber = ((b[0] << 8) + b[1]) * 8;
788 static int nxt200x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
790 struct nxt200x_state* state = fe->demodulator_priv;
794 /* setup to read cluster variance */
796 nxt200x_writebytes(state, 0xA1, b, 1);
798 /* get multreg val */
799 nxt200x_readreg_multibyte(state, 0xA6, b, 2);
801 temp = (b[0] << 8) | b[1];
802 *strength = ((0x7FFF - temp) & 0x0FFF) * 16;
807 static int nxt200x_read_snr(struct dvb_frontend* fe, u16* snr)
810 struct nxt200x_state* state = fe->demodulator_priv;
815 /* setup to read cluster variance */
817 nxt200x_writebytes(state, 0xA1, b, 1);
819 /* get multreg val from 0xA6 */
820 nxt200x_readreg_multibyte(state, 0xA6, b, 2);
822 temp = (b[0] << 8) | b[1];
823 temp2 = 0x7FFF - temp;
825 /* snr will be in db */
827 snrdb = 1000*24 + ( 1000*(30-24) * ( temp2 - 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
828 else if (temp2 > 0x7EC0)
829 snrdb = 1000*18 + ( 1000*(24-18) * ( temp2 - 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
830 else if (temp2 > 0x7C00)
831 snrdb = 1000*12 + ( 1000*(18-12) * ( temp2 - 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
833 snrdb = 1000*0 + ( 1000*(12-0) * ( temp2 - 0 ) / ( 0x7C00 - 0 ) );
835 /* the value reported back from the frontend will be FFFF=32db 0000=0db */
836 *snr = snrdb * (0xFFFF/32000);
841 static int nxt200x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
843 struct nxt200x_state* state = fe->demodulator_priv;
846 nxt200x_readreg_multibyte(state, 0xE6, b, 3);
852 static int nxt200x_sleep(struct dvb_frontend* fe)
857 static int nxt2002_init(struct dvb_frontend* fe)
859 struct nxt200x_state* state = fe->demodulator_priv;
860 const struct firmware *fw;
864 /* request the firmware, this will block until someone uploads it */
865 printk("nxt2002: Waiting for firmware upload (%s)...\n", "/*(DEBLOBBED)*/");
866 ret = reject_firmware(&fw, "/*(DEBLOBBED)*/",
867 state->i2c->dev.parent);
868 printk("nxt2002: Waiting for firmware upload(2)...\n");
870 printk("nxt2002: No firmware uploaded (timeout or file not found?)\n");
874 ret = nxt2002_load_firmware(fe, fw);
875 release_firmware(fw);
877 printk("nxt2002: Writing firmware to device failed\n");
880 printk("nxt2002: Firmware upload complete\n");
882 /* Put the micro into reset */
883 nxt200x_microcontroller_stop(state);
885 /* ensure transfer is complete */
887 nxt200x_writebytes(state, 0x2B, buf, 1);
889 /* Put the micro into reset for real this time */
890 nxt200x_microcontroller_stop(state);
892 /* soft reset everything (agc,frontend,eq,fec)*/
894 nxt200x_writebytes(state, 0x08, buf, 1);
896 nxt200x_writebytes(state, 0x08, buf, 1);
898 /* write agc sdm configure */
900 nxt200x_writebytes(state, 0x57, buf, 1);
902 /* write mod output format */
904 nxt200x_writebytes(state, 0x09, buf, 1);
906 /* write fec mpeg mode */
909 nxt200x_writebytes(state, 0xE9, buf, 2);
911 /* write mux selection */
913 nxt200x_writebytes(state, 0xCC, buf, 1);
918 static int nxt2004_init(struct dvb_frontend* fe)
920 struct nxt200x_state* state = fe->demodulator_priv;
921 const struct firmware *fw;
927 nxt200x_writebytes(state, 0x1E, buf, 1);
929 /* request the firmware, this will block until someone uploads it */
930 printk("nxt2004: Waiting for firmware upload (%s)...\n", "/*(DEBLOBBED)*/");
931 ret = reject_firmware(&fw, "/*(DEBLOBBED)*/",
932 state->i2c->dev.parent);
933 printk("nxt2004: Waiting for firmware upload(2)...\n");
935 printk("nxt2004: No firmware uploaded (timeout or file not found?)\n");
939 ret = nxt2004_load_firmware(fe, fw);
940 release_firmware(fw);
942 printk("nxt2004: Writing firmware to device failed\n");
945 printk("nxt2004: Firmware upload complete\n");
947 /* ensure transfer is complete */
949 nxt200x_writebytes(state, 0x19, buf, 1);
951 nxt2004_microcontroller_init(state);
952 nxt200x_microcontroller_stop(state);
953 nxt200x_microcontroller_stop(state);
954 nxt2004_microcontroller_init(state);
955 nxt200x_microcontroller_stop(state);
957 /* soft reset everything (agc,frontend,eq,fec)*/
959 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
961 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
963 /* write agc sdm configure */
965 nxt200x_writebytes(state, 0x57, buf, 1);
970 nxt200x_writebytes(state, 0x35, buf, 2);
972 nxt200x_writebytes(state, 0x34, buf, 1);
974 nxt200x_writebytes(state, 0x21, buf, 1);
978 nxt200x_writebytes(state, 0x0A, buf, 1);
982 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
984 /* write fec mpeg mode */
987 nxt200x_writebytes(state, 0xE9, buf, 2);
989 /* write mux selection */
991 nxt200x_writebytes(state, 0xCC, buf, 1);
994 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
996 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
999 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1001 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1002 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1004 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1007 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1009 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1011 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1012 buf[0] = 0x31; buf[1] = 0x5E; buf[2] = 0x66;
1013 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1015 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1017 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1018 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1020 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1022 nxt200x_readbytes(state, 0x10, buf, 1);
1024 nxt200x_writebytes(state, 0x10, buf, 1);
1025 nxt200x_readbytes(state, 0x0A, buf, 1);
1027 nxt200x_writebytes(state, 0x0A, buf, 1);
1029 nxt2004_microcontroller_init(state);
1032 nxt200x_writebytes(state, 0x0A, buf, 1);
1034 nxt200x_writebytes(state, 0xE9, buf, 1);
1036 nxt200x_writebytes(state, 0xEA, buf, 1);
1038 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1040 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1041 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1043 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1046 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1048 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1049 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1051 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1053 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1055 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1057 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1058 buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
1059 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1061 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1063 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1065 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1067 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1069 /* initialize tuner */
1070 nxt200x_readbytes(state, 0x10, buf, 1);
1072 nxt200x_writebytes(state, 0x10, buf, 1);
1074 nxt200x_writebytes(state, 0x13, buf, 1);
1076 nxt200x_writebytes(state, 0x16, buf, 1);
1078 nxt200x_writebytes(state, 0x14, buf, 1);
1080 nxt200x_writebytes(state, 0x14, buf, 1);
1081 nxt200x_writebytes(state, 0x17, buf, 1);
1082 nxt200x_writebytes(state, 0x14, buf, 1);
1083 nxt200x_writebytes(state, 0x17, buf, 1);
1088 static int nxt200x_init(struct dvb_frontend* fe)
1090 struct nxt200x_state* state = fe->demodulator_priv;
1093 if (!state->initialised) {
1094 switch (state->demod_chip) {
1096 ret = nxt2002_init(fe);
1099 ret = nxt2004_init(fe);
1105 state->initialised = 1;
1110 static int nxt200x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
1112 fesettings->min_delay_ms = 500;
1113 fesettings->step_size = 0;
1114 fesettings->max_drift = 0;
1118 static void nxt200x_release(struct dvb_frontend* fe)
1120 struct nxt200x_state* state = fe->demodulator_priv;
1124 static struct dvb_frontend_ops nxt200x_ops;
1126 struct dvb_frontend* nxt200x_attach(const struct nxt200x_config* config,
1127 struct i2c_adapter* i2c)
1129 struct nxt200x_state* state = NULL;
1130 u8 buf [] = {0,0,0,0,0};
1132 /* allocate memory for the internal state */
1133 state = kzalloc(sizeof(struct nxt200x_state), GFP_KERNEL);
1137 /* setup the state */
1138 state->config = config;
1140 state->initialised = 0;
1143 nxt200x_readbytes(state, 0x00, buf, 5);
1144 dprintk("NXT info: %02X %02X %02X %02X %02X\n",
1145 buf[0], buf[1], buf[2], buf[3], buf[4]);
1147 /* set demod chip */
1150 state->demod_chip = NXT2002;
1151 printk("nxt200x: NXT2002 Detected\n");
1154 state->demod_chip = NXT2004;
1155 printk("nxt200x: NXT2004 Detected\n");
1161 /* make sure demod chip is supported */
1162 switch (state->demod_chip) {
1164 if (buf[0] != 0x04) goto error; /* device id */
1165 if (buf[1] != 0x02) goto error; /* fab id */
1166 if (buf[2] != 0x11) goto error; /* month */
1167 if (buf[3] != 0x20) goto error; /* year msb */
1168 if (buf[4] != 0x00) goto error; /* year lsb */
1171 if (buf[0] != 0x05) goto error; /* device id */
1177 /* create dvb_frontend */
1178 memcpy(&state->frontend.ops, &nxt200x_ops, sizeof(struct dvb_frontend_ops));
1179 state->frontend.demodulator_priv = state;
1180 return &state->frontend;
1184 printk("Unknown/Unsupported NXT chip: %02X %02X %02X %02X %02X\n",
1185 buf[0], buf[1], buf[2], buf[3], buf[4]);
1189 static struct dvb_frontend_ops nxt200x_ops = {
1190 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
1192 .name = "Nextwave NXT200X VSB/QAM frontend",
1193 .frequency_min = 54000000,
1194 .frequency_max = 860000000,
1195 .frequency_stepsize = 166666, /* stepsize is just a guess */
1196 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1197 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1198 FE_CAN_8VSB | FE_CAN_QAM_64 | FE_CAN_QAM_256
1201 .release = nxt200x_release,
1203 .init = nxt200x_init,
1204 .sleep = nxt200x_sleep,
1206 .set_frontend = nxt200x_setup_frontend_parameters,
1207 .get_tune_settings = nxt200x_get_tune_settings,
1209 .read_status = nxt200x_read_status,
1210 .read_ber = nxt200x_read_ber,
1211 .read_signal_strength = nxt200x_read_signal_strength,
1212 .read_snr = nxt200x_read_snr,
1213 .read_ucblocks = nxt200x_read_ucblocks,
1216 module_param(debug, int, 0644);
1217 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1219 MODULE_DESCRIPTION("NXT200X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
1220 MODULE_AUTHOR("Kirk Lapray, Michael Krufky, Jean-Francois Thibert, and Taylor Jacob");
1221 MODULE_LICENSE("GPL");
1223 EXPORT_SYMBOL(nxt200x_attach);