1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Support for NXT2002 and NXT2004 - VSB/QAM
5 * Copyright (C) 2005 Kirk Lapray <kirk.lapray@gmail.com>
6 * Copyright (C) 2006-2014 Michael Krufky <mkrufky@linuxtv.org>
7 * based on nxt2002 by Taylor Jacob <rtjacob@earthlink.net>
8 * and nxt2004 by Jean-Francois Thibert <jeanfrancois@sagetv.com>
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;
174 /* set multi register length */
175 nxt200x_writebytes(state, 0x34, &len2, 1);
177 /* toggle the multireg write bit */
178 nxt200x_writebytes(state, 0x21, &buf, 1);
180 nxt200x_readbytes(state, 0x21, &buf, 1);
182 switch (state->demod_chip) {
184 if ((buf & 0x02) == 0)
196 pr_warn("Error writing multireg register 0x%02X\n", reg);
201 static int nxt200x_readreg_multibyte (struct nxt200x_state* state, u8 reg, u8* data, u8 len)
205 dprintk("%s\n", __func__);
207 /* set multi register register */
208 nxt200x_writebytes(state, 0x35, ®, 1);
210 switch (state->demod_chip) {
212 /* set multi register length */
214 nxt200x_writebytes(state, 0x34, &len2, 1);
216 /* read the actual data */
217 nxt200x_readbytes(state, reg, data, len);
221 /* probably not right, but gives correct values */
229 /* set multi register length */
230 len2 = (attr << 4) | len;
231 nxt200x_writebytes(state, 0x34, &len2, 1);
233 /* toggle the multireg bit*/
235 nxt200x_writebytes(state, 0x21, &buf, 1);
237 /* read the actual data */
238 for(i = 0; i < len; i++) {
239 nxt200x_readbytes(state, 0x36 + i, &data[i], 1);
249 static void nxt200x_microcontroller_stop (struct nxt200x_state* state)
251 u8 buf, stopval, counter = 0;
252 dprintk("%s\n", __func__);
254 /* set correct stop value */
255 switch (state->demod_chip) {
268 nxt200x_writebytes(state, 0x22, &buf, 1);
270 while (counter < 20) {
271 nxt200x_readbytes(state, 0x31, &buf, 1);
278 pr_warn("Timeout waiting for nxt200x to stop. This is ok after firmware upload.\n");
282 static void nxt200x_microcontroller_start (struct nxt200x_state* state)
285 dprintk("%s\n", __func__);
288 nxt200x_writebytes(state, 0x22, &buf, 1);
291 static void nxt2004_microcontroller_init (struct nxt200x_state* state)
295 dprintk("%s\n", __func__);
298 nxt200x_writebytes(state, 0x2b, buf, 1);
300 nxt200x_writebytes(state, 0x34, buf, 1);
302 nxt200x_writebytes(state, 0x35, buf, 1);
303 buf[0] = 0x01; buf[1] = 0x23; buf[2] = 0x45; buf[3] = 0x67; buf[4] = 0x89;
304 buf[5] = 0xAB; buf[6] = 0xCD; buf[7] = 0xEF; buf[8] = 0xC0;
305 nxt200x_writebytes(state, 0x36, buf, 9);
307 nxt200x_writebytes(state, 0x21, buf, 1);
309 while (counter < 20) {
310 nxt200x_readbytes(state, 0x21, buf, 1);
317 pr_warn("Timeout waiting for nxt2004 to init.\n");
322 static int nxt200x_writetuner (struct nxt200x_state* state, u8* data)
326 dprintk("%s\n", __func__);
328 dprintk("Tuner Bytes: %*ph\n", 4, data + 1);
330 /* if NXT2004, write directly to tuner. if NXT2002, write through NXT chip.
331 * direct write is required for Philips TUV1236D and ALPS TDHU2 */
332 switch (state->demod_chip) {
334 if (i2c_writebytes(state, data[0], data+1, 4))
335 pr_warn("error writing to tuner\n");
336 /* wait until we have a lock */
338 i2c_readbytes(state, data[0], &buf, 1);
344 pr_warn("timeout waiting for tuner lock\n");
347 /* set the i2c transfer speed to the tuner */
349 nxt200x_writebytes(state, 0x20, &buf, 1);
351 /* setup to transfer 4 bytes via i2c */
353 nxt200x_writebytes(state, 0x34, &buf, 1);
355 /* write actual tuner bytes */
356 nxt200x_writebytes(state, 0x36, data+1, 4);
358 /* set tuner i2c address */
360 nxt200x_writebytes(state, 0x35, &buf, 1);
362 /* write UC Opmode to begin transfer */
364 nxt200x_writebytes(state, 0x21, &buf, 1);
367 nxt200x_readbytes(state, 0x21, &buf, 1);
368 if ((buf & 0x80)== 0x00)
373 pr_warn("timeout error writing to tuner\n");
382 static void nxt200x_agc_reset(struct nxt200x_state* state)
385 dprintk("%s\n", __func__);
387 switch (state->demod_chip) {
390 nxt200x_writebytes(state, 0x08, &buf, 1);
392 nxt200x_writebytes(state, 0x08, &buf, 1);
395 nxt200x_readreg_multibyte(state, 0x08, &buf, 1);
397 nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
399 nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
407 static int nxt2002_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
410 struct nxt200x_state* state = fe->demodulator_priv;
411 u8 buf[3], written = 0, chunkpos = 0;
412 u16 rambase, position, crc = 0;
414 dprintk("%s\n", __func__);
415 dprintk("Firmware is %zu bytes\n", fw->size);
417 /* Get the RAM base for this nxt2002 */
418 nxt200x_readbytes(state, 0x10, buf, 1);
425 dprintk("rambase on this nxt2002 is %04X\n", rambase);
427 /* Hold the micro in reset while loading firmware */
429 nxt200x_writebytes(state, 0x2B, buf, 1);
431 for (position = 0; position < fw->size; position++) {
435 buf[0] = ((rambase + position) >> 8);
436 buf[1] = (rambase + position) & 0xFF;
438 /* write starting address */
439 nxt200x_writebytes(state, 0x29, buf, 3);
444 if ((written % 4) == 0)
445 nxt200x_writebytes(state, chunkpos, &fw->data[position-3], 4);
447 crc = nxt200x_crc(crc, fw->data[position]);
449 if ((written == 255) || (position+1 == fw->size)) {
450 /* write remaining bytes of firmware */
451 nxt200x_writebytes(state, chunkpos+4-(written %4),
452 &fw->data[position-(written %4) + 1],
458 nxt200x_writebytes(state, 0x2C, buf, 2);
460 /* do a read to stop things */
461 nxt200x_readbytes(state, 0x2A, buf, 1);
463 /* set transfer mode to complete */
465 nxt200x_writebytes(state, 0x2B, buf, 1);
474 static int nxt2004_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
477 struct nxt200x_state* state = fe->demodulator_priv;
479 u16 rambase, position, crc=0;
481 dprintk("%s\n", __func__);
482 dprintk("Firmware is %zu bytes\n", fw->size);
487 /* hold the micro in reset while loading firmware */
489 nxt200x_writebytes(state, 0x2B, buf,1);
491 /* calculate firmware CRC */
492 for (position = 0; position < fw->size; position++) {
493 crc = nxt200x_crc(crc, fw->data[position]);
496 buf[0] = rambase >> 8;
497 buf[1] = rambase & 0xFF;
499 /* write starting address */
500 nxt200x_writebytes(state,0x29,buf,3);
502 for (position = 0; position < fw->size;) {
503 nxt200x_writebytes(state, 0x2C, &fw->data[position],
504 fw->size-position > 255 ? 255 : fw->size-position);
505 position += (fw->size-position > 255 ? 255 : fw->size-position);
510 dprintk("firmware crc is 0x%02X 0x%02X\n", buf[0], buf[1]);
513 nxt200x_writebytes(state, 0x2C, buf,2);
515 /* do a read to stop things */
516 nxt200x_readbytes(state, 0x2C, buf, 1);
518 /* set transfer mode to complete */
520 nxt200x_writebytes(state, 0x2B, buf,1);
525 static int nxt200x_setup_frontend_parameters(struct dvb_frontend *fe)
527 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
528 struct nxt200x_state* state = fe->demodulator_priv;
531 /* stop the micro first */
532 nxt200x_microcontroller_stop(state);
534 if (state->demod_chip == NXT2004) {
535 /* make sure demod is set to digital */
537 nxt200x_writebytes(state, 0x14, buf, 1);
539 nxt200x_writebytes(state, 0x17, buf, 1);
542 /* set additional params */
543 switch (p->modulation) {
546 /* Set punctured clock for QAM */
547 /* This is just a guess since I am unable to test it */
548 if (state->config->set_ts_params)
549 state->config->set_ts_params(fe, 1);
552 /* Set non-punctured clock for VSB */
553 if (state->config->set_ts_params)
554 state->config->set_ts_params(fe, 0);
561 if (fe->ops.tuner_ops.calc_regs) {
562 /* get tuning information */
563 fe->ops.tuner_ops.calc_regs(fe, buf, 5);
565 /* write frequency information */
566 nxt200x_writetuner(state, buf);
569 /* reset the agc now that tuning has been completed */
570 nxt200x_agc_reset(state);
572 /* set target power level */
573 switch (p->modulation) {
585 nxt200x_writebytes(state, 0x42, buf, 1);
588 switch (state->demod_chip) {
599 nxt200x_writebytes(state, 0x57, buf, 1);
601 /* write sdm1 input */
604 switch (state->demod_chip) {
606 nxt200x_writereg_multibyte(state, 0x58, buf, 2);
609 nxt200x_writebytes(state, 0x58, buf, 2);
616 /* write sdmx input */
617 switch (p->modulation) {
632 switch (state->demod_chip) {
634 nxt200x_writereg_multibyte(state, 0x5C, buf, 2);
637 nxt200x_writebytes(state, 0x5C, buf, 2);
644 /* write adc power lpf fc */
646 nxt200x_writebytes(state, 0x43, buf, 1);
648 if (state->demod_chip == NXT2004) {
652 nxt200x_writebytes(state, 0x46, buf, 2);
655 /* write accumulator2 input */
658 switch (state->demod_chip) {
660 nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
663 nxt200x_writebytes(state, 0x4B, buf, 2);
672 nxt200x_writebytes(state, 0x4D, buf, 1);
674 /* write sdm12 lpf fc */
676 nxt200x_writebytes(state, 0x55, buf, 1);
678 /* write agc control reg */
680 nxt200x_writebytes(state, 0x41, buf, 1);
682 if (state->demod_chip == NXT2004) {
683 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
685 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
688 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
690 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
691 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
693 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
695 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
697 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
699 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
700 buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
701 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
702 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
704 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
705 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
707 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
710 /* write agc ucgp0 */
711 switch (p->modulation) {
725 nxt200x_writebytes(state, 0x30, buf, 1);
727 /* write agc control reg */
729 nxt200x_writebytes(state, 0x41, buf, 1);
731 /* write accumulator2 input */
734 switch (state->demod_chip) {
736 nxt200x_writereg_multibyte(state, 0x49, buf, 2);
737 nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
740 nxt200x_writebytes(state, 0x49, buf, 2);
741 nxt200x_writebytes(state, 0x4B, buf, 2);
748 /* write agc control reg */
750 nxt200x_writebytes(state, 0x41, buf, 1);
752 nxt200x_microcontroller_start(state);
754 if (state->demod_chip == NXT2004) {
755 nxt2004_microcontroller_init(state);
760 nxt200x_writebytes(state, 0x5C, buf, 2);
763 /* adjacent channel detection should be done here, but I don't
764 have any stations with this need so I cannot test it */
769 static int nxt200x_read_status(struct dvb_frontend *fe, enum fe_status *status)
771 struct nxt200x_state* state = fe->demodulator_priv;
773 nxt200x_readbytes(state, 0x31, &lock, 1);
777 *status |= FE_HAS_SIGNAL;
778 *status |= FE_HAS_CARRIER;
779 *status |= FE_HAS_VITERBI;
780 *status |= FE_HAS_SYNC;
781 *status |= FE_HAS_LOCK;
786 static int nxt200x_read_ber(struct dvb_frontend* fe, u32* ber)
788 struct nxt200x_state* state = fe->demodulator_priv;
791 nxt200x_readreg_multibyte(state, 0xE6, b, 3);
793 *ber = ((b[0] << 8) + b[1]) * 8;
798 static int nxt200x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
800 struct nxt200x_state* state = fe->demodulator_priv;
804 /* setup to read cluster variance */
806 nxt200x_writebytes(state, 0xA1, b, 1);
808 /* get multreg val */
809 nxt200x_readreg_multibyte(state, 0xA6, b, 2);
811 temp = (b[0] << 8) | b[1];
812 *strength = ((0x7FFF - temp) & 0x0FFF) * 16;
817 static int nxt200x_read_snr(struct dvb_frontend* fe, u16* snr)
820 struct nxt200x_state* state = fe->demodulator_priv;
825 /* setup to read cluster variance */
827 nxt200x_writebytes(state, 0xA1, b, 1);
829 /* get multreg val from 0xA6 */
830 nxt200x_readreg_multibyte(state, 0xA6, b, 2);
832 temp = (b[0] << 8) | b[1];
833 temp2 = 0x7FFF - temp;
835 /* snr will be in db */
837 snrdb = 1000*24 + ( 1000*(30-24) * ( temp2 - 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
838 else if (temp2 > 0x7EC0)
839 snrdb = 1000*18 + ( 1000*(24-18) * ( temp2 - 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
840 else if (temp2 > 0x7C00)
841 snrdb = 1000*12 + ( 1000*(18-12) * ( temp2 - 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
843 snrdb = 1000*0 + ( 1000*(12-0) * ( temp2 - 0 ) / ( 0x7C00 - 0 ) );
845 /* the value reported back from the frontend will be FFFF=32db 0000=0db */
846 *snr = snrdb * (0xFFFF/32000);
851 static int nxt200x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
853 struct nxt200x_state* state = fe->demodulator_priv;
856 nxt200x_readreg_multibyte(state, 0xE6, b, 3);
862 static int nxt200x_sleep(struct dvb_frontend* fe)
867 static int nxt2002_init(struct dvb_frontend* fe)
869 struct nxt200x_state* state = fe->demodulator_priv;
870 const struct firmware *fw;
874 /* request the firmware, this will block until someone uploads it */
875 pr_debug("%s: Waiting for firmware upload (%s)...\n",
876 __func__, NXT2002_DEFAULT_FIRMWARE);
877 ret = request_firmware(&fw, NXT2002_DEFAULT_FIRMWARE,
878 state->i2c->dev.parent);
879 pr_debug("%s: Waiting for firmware upload(2)...\n", __func__);
881 pr_err("%s: No firmware uploaded (timeout or file not found?)\n",
886 ret = nxt2002_load_firmware(fe, fw);
887 release_firmware(fw);
889 pr_err("%s: Writing firmware to device failed\n", __func__);
892 pr_info("%s: Firmware upload complete\n", __func__);
894 /* Put the micro into reset */
895 nxt200x_microcontroller_stop(state);
897 /* ensure transfer is complete */
899 nxt200x_writebytes(state, 0x2B, buf, 1);
901 /* Put the micro into reset for real this time */
902 nxt200x_microcontroller_stop(state);
904 /* soft reset everything (agc,frontend,eq,fec)*/
906 nxt200x_writebytes(state, 0x08, buf, 1);
908 nxt200x_writebytes(state, 0x08, buf, 1);
910 /* write agc sdm configure */
912 nxt200x_writebytes(state, 0x57, buf, 1);
914 /* write mod output format */
916 nxt200x_writebytes(state, 0x09, buf, 1);
918 /* write fec mpeg mode */
921 nxt200x_writebytes(state, 0xE9, buf, 2);
923 /* write mux selection */
925 nxt200x_writebytes(state, 0xCC, buf, 1);
930 static int nxt2004_init(struct dvb_frontend* fe)
932 struct nxt200x_state* state = fe->demodulator_priv;
933 const struct firmware *fw;
939 nxt200x_writebytes(state, 0x1E, buf, 1);
941 /* request the firmware, this will block until someone uploads it */
942 pr_debug("%s: Waiting for firmware upload (%s)...\n",
943 __func__, NXT2004_DEFAULT_FIRMWARE);
944 ret = request_firmware(&fw, NXT2004_DEFAULT_FIRMWARE,
945 state->i2c->dev.parent);
946 pr_debug("%s: Waiting for firmware upload(2)...\n", __func__);
948 pr_err("%s: No firmware uploaded (timeout or file not found?)\n",
953 ret = nxt2004_load_firmware(fe, fw);
954 release_firmware(fw);
956 pr_err("%s: Writing firmware to device failed\n", __func__);
959 pr_info("%s: Firmware upload complete\n", __func__);
961 /* ensure transfer is complete */
963 nxt200x_writebytes(state, 0x19, buf, 1);
965 nxt2004_microcontroller_init(state);
966 nxt200x_microcontroller_stop(state);
967 nxt200x_microcontroller_stop(state);
968 nxt2004_microcontroller_init(state);
969 nxt200x_microcontroller_stop(state);
971 /* soft reset everything (agc,frontend,eq,fec)*/
973 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
975 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
977 /* write agc sdm configure */
979 nxt200x_writebytes(state, 0x57, buf, 1);
984 nxt200x_writebytes(state, 0x35, buf, 2);
986 nxt200x_writebytes(state, 0x34, buf, 1);
988 nxt200x_writebytes(state, 0x21, buf, 1);
992 nxt200x_writebytes(state, 0x0A, buf, 1);
996 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
998 /* write fec mpeg mode */
1001 nxt200x_writebytes(state, 0xE9, buf, 2);
1003 /* write mux selection */
1005 nxt200x_writebytes(state, 0xCC, buf, 1);
1008 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1010 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1013 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1015 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1016 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1018 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1021 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1023 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1025 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1026 buf[0] = 0x31; buf[1] = 0x5E; buf[2] = 0x66;
1027 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1029 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1031 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1032 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1034 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1036 nxt200x_readbytes(state, 0x10, buf, 1);
1038 nxt200x_writebytes(state, 0x10, buf, 1);
1039 nxt200x_readbytes(state, 0x0A, buf, 1);
1041 nxt200x_writebytes(state, 0x0A, buf, 1);
1043 nxt2004_microcontroller_init(state);
1046 nxt200x_writebytes(state, 0x0A, buf, 1);
1048 nxt200x_writebytes(state, 0xE9, buf, 1);
1050 nxt200x_writebytes(state, 0xEA, buf, 1);
1052 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1054 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1055 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1057 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1060 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1062 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1063 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1065 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1067 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1069 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1071 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1072 buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
1073 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1075 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1077 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1079 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1081 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1083 /* initialize tuner */
1084 nxt200x_readbytes(state, 0x10, buf, 1);
1086 nxt200x_writebytes(state, 0x10, buf, 1);
1088 nxt200x_writebytes(state, 0x13, buf, 1);
1090 nxt200x_writebytes(state, 0x16, buf, 1);
1092 nxt200x_writebytes(state, 0x14, buf, 1);
1094 nxt200x_writebytes(state, 0x14, buf, 1);
1095 nxt200x_writebytes(state, 0x17, buf, 1);
1096 nxt200x_writebytes(state, 0x14, buf, 1);
1097 nxt200x_writebytes(state, 0x17, buf, 1);
1102 static int nxt200x_init(struct dvb_frontend* fe)
1104 struct nxt200x_state* state = fe->demodulator_priv;
1107 if (!state->initialised) {
1108 switch (state->demod_chip) {
1110 ret = nxt2002_init(fe);
1113 ret = nxt2004_init(fe);
1119 state->initialised = 1;
1124 static int nxt200x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
1126 fesettings->min_delay_ms = 500;
1127 fesettings->step_size = 0;
1128 fesettings->max_drift = 0;
1132 static void nxt200x_release(struct dvb_frontend* fe)
1134 struct nxt200x_state* state = fe->demodulator_priv;
1138 static const struct dvb_frontend_ops nxt200x_ops;
1140 struct dvb_frontend* nxt200x_attach(const struct nxt200x_config* config,
1141 struct i2c_adapter* i2c)
1143 struct nxt200x_state* state = NULL;
1144 u8 buf [] = {0,0,0,0,0};
1146 /* allocate memory for the internal state */
1147 state = kzalloc(sizeof(struct nxt200x_state), GFP_KERNEL);
1151 /* setup the state */
1152 state->config = config;
1154 state->initialised = 0;
1157 nxt200x_readbytes(state, 0x00, buf, 5);
1158 dprintk("NXT info: %*ph\n", 5, buf);
1160 /* set demod chip */
1163 state->demod_chip = NXT2002;
1164 pr_info("NXT2002 Detected\n");
1167 state->demod_chip = NXT2004;
1168 pr_info("NXT2004 Detected\n");
1174 /* make sure demod chip is supported */
1175 switch (state->demod_chip) {
1177 if (buf[0] != 0x04) goto error; /* device id */
1178 if (buf[1] != 0x02) goto error; /* fab id */
1179 if (buf[2] != 0x11) goto error; /* month */
1180 if (buf[3] != 0x20) goto error; /* year msb */
1181 if (buf[4] != 0x00) goto error; /* year lsb */
1184 if (buf[0] != 0x05) goto error; /* device id */
1190 /* create dvb_frontend */
1191 memcpy(&state->frontend.ops, &nxt200x_ops, sizeof(struct dvb_frontend_ops));
1192 state->frontend.demodulator_priv = state;
1193 return &state->frontend;
1197 pr_err("Unknown/Unsupported NXT chip: %*ph\n", 5, buf);
1201 static const struct dvb_frontend_ops nxt200x_ops = {
1202 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
1204 .name = "Nextwave NXT200X VSB/QAM frontend",
1205 .frequency_min_hz = 54 * MHz,
1206 .frequency_max_hz = 860 * MHz,
1207 .frequency_stepsize_hz = 166666, /* stepsize is just a guess */
1208 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1209 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1210 FE_CAN_8VSB | FE_CAN_QAM_64 | FE_CAN_QAM_256
1213 .release = nxt200x_release,
1215 .init = nxt200x_init,
1216 .sleep = nxt200x_sleep,
1218 .set_frontend = nxt200x_setup_frontend_parameters,
1219 .get_tune_settings = nxt200x_get_tune_settings,
1221 .read_status = nxt200x_read_status,
1222 .read_ber = nxt200x_read_ber,
1223 .read_signal_strength = nxt200x_read_signal_strength,
1224 .read_snr = nxt200x_read_snr,
1225 .read_ucblocks = nxt200x_read_ucblocks,
1228 module_param(debug, int, 0644);
1229 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1231 MODULE_DESCRIPTION("NXT200X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
1232 MODULE_AUTHOR("Kirk Lapray, Michael Krufky, Jean-Francois Thibert, and Taylor Jacob");
1233 MODULE_LICENSE("GPL");
1235 EXPORT_SYMBOL(nxt200x_attach);