1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Support for NXT2002 and NXT2004 - VSB/QAM
12 * NOTES ABOUT THIS DRIVER
14 * This Linux driver supports:
15 * B2C2/BBTI Technisat Air2PC - ATSC (NXT2002)
16 * AverTVHD MCE A180 (NXT2004)
17 * ATI HDTV Wonder (NXT2004)
19 * This driver needs external firmware. Please use the command
20 * "<kerneldir>/scripts/get_dvb_firmware nxt2002" or
21 * "<kerneldir>/scripts/get_dvb_firmware nxt2004" to
22 * download/extract the appropriate firmware, and then copy it to
23 * /usr/lib/hotplug/firmware/ or /lib/firmware/
24 * (depending on configuration of firmware hotplug).
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 /* Max transfer size done by I2C transfer functions */
29 #define MAX_XFER_SIZE 256
31 #define NXT2002_DEFAULT_FIRMWARE "dvb-fe-nxt2002.fw"
32 #define NXT2004_DEFAULT_FIRMWARE "dvb-fe-nxt2004.fw"
33 #define CRC_CCIT_MASK 0x1021
35 #include <linux/kernel.h>
36 #include <linux/init.h>
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/string.h>
41 #include <media/dvb_frontend.h>
44 struct nxt200x_state {
46 struct i2c_adapter* i2c;
47 const struct nxt200x_config* config;
48 struct dvb_frontend frontend;
50 /* demodulator private data */
51 nxt_chip_type demod_chip;
56 #define dprintk(args...) do { if (debug) pr_debug(args); } while (0)
58 static int i2c_writebytes (struct nxt200x_state* state, u8 addr, u8 *buf, u8 len)
61 struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = len };
63 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
64 pr_warn("%s: i2c write error (addr 0x%02x, err == %i)\n",
71 static int i2c_readbytes(struct nxt200x_state *state, u8 addr, u8 *buf, u8 len)
74 struct i2c_msg msg = { .addr = addr, .flags = I2C_M_RD, .buf = buf, .len = len };
76 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
77 pr_warn("%s: i2c read error (addr 0x%02x, err == %i)\n",
84 static int nxt200x_writebytes (struct nxt200x_state* state, u8 reg,
85 const u8 *buf, u8 len)
87 u8 buf2[MAX_XFER_SIZE];
89 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf2, .len = len + 1 };
91 if (1 + len > sizeof(buf2)) {
92 pr_warn("%s: i2c wr reg=%04x: len=%d is too big!\n",
98 memcpy(&buf2[1], buf, len);
100 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
101 pr_warn("%s: i2c write error (addr 0x%02x, err == %i)\n",
102 __func__, state->config->demod_address, err);
108 static int nxt200x_readbytes(struct nxt200x_state *state, u8 reg, u8 *buf, u8 len)
110 u8 reg2 [] = { reg };
112 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = reg2, .len = 1 },
113 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = buf, .len = len } };
117 if ((err = i2c_transfer (state->i2c, msg, 2)) != 2) {
118 pr_warn("%s: i2c read error (addr 0x%02x, err == %i)\n",
119 __func__, state->config->demod_address, err);
125 static u16 nxt200x_crc(u16 crc, u8 c)
128 u16 input = (u16) c & 0xFF;
132 if((crc^input) & 0x8000)
133 crc=(crc<<1)^CRC_CCIT_MASK;
141 static int nxt200x_writereg_multibyte (struct nxt200x_state* state, u8 reg, u8* data, u8 len)
144 dprintk("%s\n", __func__);
146 /* set multi register register */
147 nxt200x_writebytes(state, 0x35, ®, 1);
149 /* send the actual data */
150 nxt200x_writebytes(state, 0x36, data, len);
152 switch (state->demod_chip) {
158 /* probably not right, but gives correct values */
166 len2 = ((attr << 4) | 0x10) | len;
173 /* set multi register length */
174 nxt200x_writebytes(state, 0x34, &len2, 1);
176 /* toggle the multireg write bit */
177 nxt200x_writebytes(state, 0x21, &buf, 1);
179 nxt200x_readbytes(state, 0x21, &buf, 1);
181 switch (state->demod_chip) {
183 if ((buf & 0x02) == 0)
194 pr_warn("Error writing multireg register 0x%02X\n", reg);
199 static int nxt200x_readreg_multibyte (struct nxt200x_state* state, u8 reg, u8* data, u8 len)
203 dprintk("%s\n", __func__);
205 /* set multi register register */
206 nxt200x_writebytes(state, 0x35, ®, 1);
208 switch (state->demod_chip) {
210 /* set multi register length */
212 nxt200x_writebytes(state, 0x34, &len2, 1);
214 /* read the actual data */
215 nxt200x_readbytes(state, reg, data, len);
218 /* probably not right, but gives correct values */
226 /* set multi register length */
227 len2 = (attr << 4) | len;
228 nxt200x_writebytes(state, 0x34, &len2, 1);
230 /* toggle the multireg bit*/
232 nxt200x_writebytes(state, 0x21, &buf, 1);
234 /* read the actual data */
235 for(i = 0; i < len; i++) {
236 nxt200x_readbytes(state, 0x36 + i, &data[i], 1);
244 static void nxt200x_microcontroller_stop (struct nxt200x_state* state)
246 u8 buf, stopval, counter = 0;
247 dprintk("%s\n", __func__);
249 /* set correct stop value */
250 switch (state->demod_chip) {
263 nxt200x_writebytes(state, 0x22, &buf, 1);
265 while (counter < 20) {
266 nxt200x_readbytes(state, 0x31, &buf, 1);
273 pr_warn("Timeout waiting for nxt200x to stop. This is ok after firmware upload.\n");
277 static void nxt200x_microcontroller_start (struct nxt200x_state* state)
280 dprintk("%s\n", __func__);
283 nxt200x_writebytes(state, 0x22, &buf, 1);
286 static void nxt2004_microcontroller_init (struct nxt200x_state* state)
290 dprintk("%s\n", __func__);
293 nxt200x_writebytes(state, 0x2b, buf, 1);
295 nxt200x_writebytes(state, 0x34, buf, 1);
297 nxt200x_writebytes(state, 0x35, buf, 1);
298 buf[0] = 0x01; buf[1] = 0x23; buf[2] = 0x45; buf[3] = 0x67; buf[4] = 0x89;
299 buf[5] = 0xAB; buf[6] = 0xCD; buf[7] = 0xEF; buf[8] = 0xC0;
300 nxt200x_writebytes(state, 0x36, buf, 9);
302 nxt200x_writebytes(state, 0x21, buf, 1);
304 while (counter < 20) {
305 nxt200x_readbytes(state, 0x21, buf, 1);
312 pr_warn("Timeout waiting for nxt2004 to init.\n");
317 static int nxt200x_writetuner (struct nxt200x_state* state, u8* data)
321 dprintk("%s\n", __func__);
323 dprintk("Tuner Bytes: %*ph\n", 4, data + 1);
325 /* if NXT2004, write directly to tuner. if NXT2002, write through NXT chip.
326 * direct write is required for Philips TUV1236D and ALPS TDHU2 */
327 switch (state->demod_chip) {
329 if (i2c_writebytes(state, data[0], data+1, 4))
330 pr_warn("error writing to tuner\n");
331 /* wait until we have a lock */
333 i2c_readbytes(state, data[0], &buf, 1);
339 pr_warn("timeout waiting for tuner lock\n");
342 /* set the i2c transfer speed to the tuner */
344 nxt200x_writebytes(state, 0x20, &buf, 1);
346 /* setup to transfer 4 bytes via i2c */
348 nxt200x_writebytes(state, 0x34, &buf, 1);
350 /* write actual tuner bytes */
351 nxt200x_writebytes(state, 0x36, data+1, 4);
353 /* set tuner i2c address */
355 nxt200x_writebytes(state, 0x35, &buf, 1);
357 /* write UC Opmode to begin transfer */
359 nxt200x_writebytes(state, 0x21, &buf, 1);
362 nxt200x_readbytes(state, 0x21, &buf, 1);
363 if ((buf & 0x80)== 0x00)
368 pr_warn("timeout error writing to tuner\n");
376 static void nxt200x_agc_reset(struct nxt200x_state* state)
379 dprintk("%s\n", __func__);
381 switch (state->demod_chip) {
384 nxt200x_writebytes(state, 0x08, &buf, 1);
386 nxt200x_writebytes(state, 0x08, &buf, 1);
389 nxt200x_readreg_multibyte(state, 0x08, &buf, 1);
391 nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
393 nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
401 static int nxt2002_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
404 struct nxt200x_state* state = fe->demodulator_priv;
405 u8 buf[3], written = 0, chunkpos = 0;
406 u16 rambase, position, crc = 0;
408 dprintk("%s\n", __func__);
409 dprintk("Firmware is %zu bytes\n", fw->size);
411 /* Get the RAM base for this nxt2002 */
412 nxt200x_readbytes(state, 0x10, buf, 1);
419 dprintk("rambase on this nxt2002 is %04X\n", rambase);
421 /* Hold the micro in reset while loading firmware */
423 nxt200x_writebytes(state, 0x2B, buf, 1);
425 for (position = 0; position < fw->size; position++) {
429 buf[0] = ((rambase + position) >> 8);
430 buf[1] = (rambase + position) & 0xFF;
432 /* write starting address */
433 nxt200x_writebytes(state, 0x29, buf, 3);
438 if ((written % 4) == 0)
439 nxt200x_writebytes(state, chunkpos, &fw->data[position-3], 4);
441 crc = nxt200x_crc(crc, fw->data[position]);
443 if ((written == 255) || (position+1 == fw->size)) {
444 /* write remaining bytes of firmware */
445 nxt200x_writebytes(state, chunkpos+4-(written %4),
446 &fw->data[position-(written %4) + 1],
452 nxt200x_writebytes(state, 0x2C, buf, 2);
454 /* do a read to stop things */
455 nxt200x_readbytes(state, 0x2A, buf, 1);
457 /* set transfer mode to complete */
459 nxt200x_writebytes(state, 0x2B, buf, 1);
468 static int nxt2004_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
471 struct nxt200x_state* state = fe->demodulator_priv;
473 u16 rambase, position, crc=0;
475 dprintk("%s\n", __func__);
476 dprintk("Firmware is %zu bytes\n", fw->size);
481 /* hold the micro in reset while loading firmware */
483 nxt200x_writebytes(state, 0x2B, buf,1);
485 /* calculate firmware CRC */
486 for (position = 0; position < fw->size; position++) {
487 crc = nxt200x_crc(crc, fw->data[position]);
490 buf[0] = rambase >> 8;
491 buf[1] = rambase & 0xFF;
493 /* write starting address */
494 nxt200x_writebytes(state,0x29,buf,3);
496 for (position = 0; position < fw->size;) {
497 nxt200x_writebytes(state, 0x2C, &fw->data[position],
498 fw->size-position > 255 ? 255 : fw->size-position);
499 position += (fw->size-position > 255 ? 255 : fw->size-position);
504 dprintk("firmware crc is 0x%02X 0x%02X\n", buf[0], buf[1]);
507 nxt200x_writebytes(state, 0x2C, buf,2);
509 /* do a read to stop things */
510 nxt200x_readbytes(state, 0x2C, buf, 1);
512 /* set transfer mode to complete */
514 nxt200x_writebytes(state, 0x2B, buf,1);
519 static int nxt200x_setup_frontend_parameters(struct dvb_frontend *fe)
521 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
522 struct nxt200x_state* state = fe->demodulator_priv;
525 /* stop the micro first */
526 nxt200x_microcontroller_stop(state);
528 if (state->demod_chip == NXT2004) {
529 /* make sure demod is set to digital */
531 nxt200x_writebytes(state, 0x14, buf, 1);
533 nxt200x_writebytes(state, 0x17, buf, 1);
536 /* set additional params */
537 switch (p->modulation) {
540 /* Set punctured clock for QAM */
541 /* This is just a guess since I am unable to test it */
542 if (state->config->set_ts_params)
543 state->config->set_ts_params(fe, 1);
546 /* Set non-punctured clock for VSB */
547 if (state->config->set_ts_params)
548 state->config->set_ts_params(fe, 0);
554 if (fe->ops.tuner_ops.calc_regs) {
555 /* get tuning information */
556 fe->ops.tuner_ops.calc_regs(fe, buf, 5);
558 /* write frequency information */
559 nxt200x_writetuner(state, buf);
562 /* reset the agc now that tuning has been completed */
563 nxt200x_agc_reset(state);
565 /* set target power level */
566 switch (p->modulation) {
577 nxt200x_writebytes(state, 0x42, buf, 1);
580 switch (state->demod_chip) {
590 nxt200x_writebytes(state, 0x57, buf, 1);
592 /* write sdm1 input */
595 switch (state->demod_chip) {
597 nxt200x_writereg_multibyte(state, 0x58, buf, 2);
600 nxt200x_writebytes(state, 0x58, buf, 2);
606 /* write sdmx input */
607 switch (p->modulation) {
621 switch (state->demod_chip) {
623 nxt200x_writereg_multibyte(state, 0x5C, buf, 2);
626 nxt200x_writebytes(state, 0x5C, buf, 2);
632 /* write adc power lpf fc */
634 nxt200x_writebytes(state, 0x43, buf, 1);
636 if (state->demod_chip == NXT2004) {
640 nxt200x_writebytes(state, 0x46, buf, 2);
643 /* write accumulator2 input */
646 switch (state->demod_chip) {
648 nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
651 nxt200x_writebytes(state, 0x4B, buf, 2);
659 nxt200x_writebytes(state, 0x4D, buf, 1);
661 /* write sdm12 lpf fc */
663 nxt200x_writebytes(state, 0x55, buf, 1);
665 /* write agc control reg */
667 nxt200x_writebytes(state, 0x41, buf, 1);
669 if (state->demod_chip == NXT2004) {
670 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
672 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
675 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
677 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
678 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
680 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
682 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
684 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
686 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
687 buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
688 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
689 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
691 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
692 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
694 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
697 /* write agc ucgp0 */
698 switch (p->modulation) {
711 nxt200x_writebytes(state, 0x30, buf, 1);
713 /* write agc control reg */
715 nxt200x_writebytes(state, 0x41, buf, 1);
717 /* write accumulator2 input */
720 switch (state->demod_chip) {
722 nxt200x_writereg_multibyte(state, 0x49, buf, 2);
723 nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
726 nxt200x_writebytes(state, 0x49, buf, 2);
727 nxt200x_writebytes(state, 0x4B, buf, 2);
733 /* write agc control reg */
735 nxt200x_writebytes(state, 0x41, buf, 1);
737 nxt200x_microcontroller_start(state);
739 if (state->demod_chip == NXT2004) {
740 nxt2004_microcontroller_init(state);
745 nxt200x_writebytes(state, 0x5C, buf, 2);
748 /* adjacent channel detection should be done here, but I don't
749 have any stations with this need so I cannot test it */
754 static int nxt200x_read_status(struct dvb_frontend *fe, enum fe_status *status)
756 struct nxt200x_state* state = fe->demodulator_priv;
758 nxt200x_readbytes(state, 0x31, &lock, 1);
762 *status |= FE_HAS_SIGNAL;
763 *status |= FE_HAS_CARRIER;
764 *status |= FE_HAS_VITERBI;
765 *status |= FE_HAS_SYNC;
766 *status |= FE_HAS_LOCK;
771 static int nxt200x_read_ber(struct dvb_frontend* fe, u32* ber)
773 struct nxt200x_state* state = fe->demodulator_priv;
776 nxt200x_readreg_multibyte(state, 0xE6, b, 3);
778 *ber = ((b[0] << 8) + b[1]) * 8;
783 static int nxt200x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
785 struct nxt200x_state* state = fe->demodulator_priv;
789 /* setup to read cluster variance */
791 nxt200x_writebytes(state, 0xA1, b, 1);
793 /* get multreg val */
794 nxt200x_readreg_multibyte(state, 0xA6, b, 2);
796 temp = (b[0] << 8) | b[1];
797 *strength = ((0x7FFF - temp) & 0x0FFF) * 16;
802 static int nxt200x_read_snr(struct dvb_frontend* fe, u16* snr)
805 struct nxt200x_state* state = fe->demodulator_priv;
810 /* setup to read cluster variance */
812 nxt200x_writebytes(state, 0xA1, b, 1);
814 /* get multreg val from 0xA6 */
815 nxt200x_readreg_multibyte(state, 0xA6, b, 2);
817 temp = (b[0] << 8) | b[1];
818 temp2 = 0x7FFF - temp;
820 /* snr will be in db */
822 snrdb = 1000*24 + ( 1000*(30-24) * ( temp2 - 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
823 else if (temp2 > 0x7EC0)
824 snrdb = 1000*18 + ( 1000*(24-18) * ( temp2 - 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
825 else if (temp2 > 0x7C00)
826 snrdb = 1000*12 + ( 1000*(18-12) * ( temp2 - 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
828 snrdb = 1000*0 + ( 1000*(12-0) * ( temp2 - 0 ) / ( 0x7C00 - 0 ) );
830 /* the value reported back from the frontend will be FFFF=32db 0000=0db */
831 *snr = snrdb * (0xFFFF/32000);
836 static int nxt200x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
838 struct nxt200x_state* state = fe->demodulator_priv;
841 nxt200x_readreg_multibyte(state, 0xE6, b, 3);
847 static int nxt200x_sleep(struct dvb_frontend* fe)
852 static int nxt2002_init(struct dvb_frontend* fe)
854 struct nxt200x_state* state = fe->demodulator_priv;
855 const struct firmware *fw;
859 /* request the firmware, this will block until someone uploads it */
860 pr_debug("%s: Waiting for firmware upload (%s)...\n",
861 __func__, NXT2002_DEFAULT_FIRMWARE);
862 ret = request_firmware(&fw, NXT2002_DEFAULT_FIRMWARE,
863 state->i2c->dev.parent);
864 pr_debug("%s: Waiting for firmware upload(2)...\n", __func__);
866 pr_err("%s: No firmware uploaded (timeout or file not found?)\n",
871 ret = nxt2002_load_firmware(fe, fw);
872 release_firmware(fw);
874 pr_err("%s: Writing firmware to device failed\n", __func__);
877 pr_info("%s: Firmware upload complete\n", __func__);
879 /* Put the micro into reset */
880 nxt200x_microcontroller_stop(state);
882 /* ensure transfer is complete */
884 nxt200x_writebytes(state, 0x2B, buf, 1);
886 /* Put the micro into reset for real this time */
887 nxt200x_microcontroller_stop(state);
889 /* soft reset everything (agc,frontend,eq,fec)*/
891 nxt200x_writebytes(state, 0x08, buf, 1);
893 nxt200x_writebytes(state, 0x08, buf, 1);
895 /* write agc sdm configure */
897 nxt200x_writebytes(state, 0x57, buf, 1);
899 /* write mod output format */
901 nxt200x_writebytes(state, 0x09, buf, 1);
903 /* write fec mpeg mode */
906 nxt200x_writebytes(state, 0xE9, buf, 2);
908 /* write mux selection */
910 nxt200x_writebytes(state, 0xCC, buf, 1);
915 static int nxt2004_init(struct dvb_frontend* fe)
917 struct nxt200x_state* state = fe->demodulator_priv;
918 const struct firmware *fw;
924 nxt200x_writebytes(state, 0x1E, buf, 1);
926 /* request the firmware, this will block until someone uploads it */
927 pr_debug("%s: Waiting for firmware upload (%s)...\n",
928 __func__, NXT2004_DEFAULT_FIRMWARE);
929 ret = request_firmware(&fw, NXT2004_DEFAULT_FIRMWARE,
930 state->i2c->dev.parent);
931 pr_debug("%s: Waiting for firmware upload(2)...\n", __func__);
933 pr_err("%s: No firmware uploaded (timeout or file not found?)\n",
938 ret = nxt2004_load_firmware(fe, fw);
939 release_firmware(fw);
941 pr_err("%s: Writing firmware to device failed\n", __func__);
944 pr_info("%s: Firmware upload complete\n", __func__);
946 /* ensure transfer is complete */
948 nxt200x_writebytes(state, 0x19, buf, 1);
950 nxt2004_microcontroller_init(state);
951 nxt200x_microcontroller_stop(state);
952 nxt200x_microcontroller_stop(state);
953 nxt2004_microcontroller_init(state);
954 nxt200x_microcontroller_stop(state);
956 /* soft reset everything (agc,frontend,eq,fec)*/
958 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
960 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
962 /* write agc sdm configure */
964 nxt200x_writebytes(state, 0x57, buf, 1);
969 nxt200x_writebytes(state, 0x35, buf, 2);
971 nxt200x_writebytes(state, 0x34, buf, 1);
973 nxt200x_writebytes(state, 0x21, buf, 1);
977 nxt200x_writebytes(state, 0x0A, buf, 1);
981 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
983 /* write fec mpeg mode */
986 nxt200x_writebytes(state, 0xE9, buf, 2);
988 /* write mux selection */
990 nxt200x_writebytes(state, 0xCC, buf, 1);
993 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
995 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
998 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1000 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1001 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1003 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1006 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1008 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1010 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1011 buf[0] = 0x31; buf[1] = 0x5E; buf[2] = 0x66;
1012 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1014 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1016 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1017 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1019 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1021 nxt200x_readbytes(state, 0x10, buf, 1);
1023 nxt200x_writebytes(state, 0x10, buf, 1);
1024 nxt200x_readbytes(state, 0x0A, buf, 1);
1026 nxt200x_writebytes(state, 0x0A, buf, 1);
1028 nxt2004_microcontroller_init(state);
1031 nxt200x_writebytes(state, 0x0A, buf, 1);
1033 nxt200x_writebytes(state, 0xE9, buf, 1);
1035 nxt200x_writebytes(state, 0xEA, buf, 1);
1037 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1039 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1040 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1042 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1045 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1047 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1048 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1050 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1052 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1054 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1056 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1057 buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
1058 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1060 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1062 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1064 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1066 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1068 /* initialize tuner */
1069 nxt200x_readbytes(state, 0x10, buf, 1);
1071 nxt200x_writebytes(state, 0x10, buf, 1);
1073 nxt200x_writebytes(state, 0x13, buf, 1);
1075 nxt200x_writebytes(state, 0x16, buf, 1);
1077 nxt200x_writebytes(state, 0x14, buf, 1);
1079 nxt200x_writebytes(state, 0x14, buf, 1);
1080 nxt200x_writebytes(state, 0x17, buf, 1);
1081 nxt200x_writebytes(state, 0x14, buf, 1);
1082 nxt200x_writebytes(state, 0x17, buf, 1);
1087 static int nxt200x_init(struct dvb_frontend* fe)
1089 struct nxt200x_state* state = fe->demodulator_priv;
1092 if (!state->initialised) {
1093 switch (state->demod_chip) {
1095 ret = nxt2002_init(fe);
1098 ret = nxt2004_init(fe);
1103 state->initialised = 1;
1108 static int nxt200x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
1110 fesettings->min_delay_ms = 500;
1111 fesettings->step_size = 0;
1112 fesettings->max_drift = 0;
1116 static void nxt200x_release(struct dvb_frontend* fe)
1118 struct nxt200x_state* state = fe->demodulator_priv;
1122 static const struct dvb_frontend_ops nxt200x_ops;
1124 struct dvb_frontend* nxt200x_attach(const struct nxt200x_config* config,
1125 struct i2c_adapter* i2c)
1127 struct nxt200x_state* state = NULL;
1128 u8 buf [] = {0,0,0,0,0};
1130 /* allocate memory for the internal state */
1131 state = kzalloc(sizeof(struct nxt200x_state), GFP_KERNEL);
1135 /* setup the state */
1136 state->config = config;
1138 state->initialised = 0;
1141 nxt200x_readbytes(state, 0x00, buf, 5);
1142 dprintk("NXT info: %*ph\n", 5, buf);
1144 /* set demod chip */
1147 state->demod_chip = NXT2002;
1148 pr_info("NXT2002 Detected\n");
1151 state->demod_chip = NXT2004;
1152 pr_info("NXT2004 Detected\n");
1158 /* make sure demod chip is supported */
1159 switch (state->demod_chip) {
1161 if (buf[0] != 0x04) goto error; /* device id */
1162 if (buf[1] != 0x02) goto error; /* fab id */
1163 if (buf[2] != 0x11) goto error; /* month */
1164 if (buf[3] != 0x20) goto error; /* year msb */
1165 if (buf[4] != 0x00) goto error; /* year lsb */
1168 if (buf[0] != 0x05) goto error; /* device id */
1174 /* create dvb_frontend */
1175 memcpy(&state->frontend.ops, &nxt200x_ops, sizeof(struct dvb_frontend_ops));
1176 state->frontend.demodulator_priv = state;
1177 return &state->frontend;
1181 pr_err("Unknown/Unsupported NXT chip: %*ph\n", 5, buf);
1185 static const struct dvb_frontend_ops nxt200x_ops = {
1186 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
1188 .name = "Nextwave NXT200X VSB/QAM frontend",
1189 .frequency_min_hz = 54 * MHz,
1190 .frequency_max_hz = 860 * MHz,
1191 .frequency_stepsize_hz = 166666, /* stepsize is just a guess */
1192 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1193 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1194 FE_CAN_8VSB | FE_CAN_QAM_64 | FE_CAN_QAM_256
1197 .release = nxt200x_release,
1199 .init = nxt200x_init,
1200 .sleep = nxt200x_sleep,
1202 .set_frontend = nxt200x_setup_frontend_parameters,
1203 .get_tune_settings = nxt200x_get_tune_settings,
1205 .read_status = nxt200x_read_status,
1206 .read_ber = nxt200x_read_ber,
1207 .read_signal_strength = nxt200x_read_signal_strength,
1208 .read_snr = nxt200x_read_snr,
1209 .read_ucblocks = nxt200x_read_ucblocks,
1212 module_param(debug, int, 0644);
1213 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1215 MODULE_DESCRIPTION("NXT200X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
1216 MODULE_AUTHOR("Kirk Lapray, Michael Krufky, Jean-Francois Thibert, and Taylor Jacob");
1217 MODULE_LICENSE("GPL");
1219 EXPORT_SYMBOL(nxt200x_attach);