40 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
42 #define NXT2002_DEFAULT_FIRMWARE "dvb-fe-nxt2002.fw"
43 #define NXT2004_DEFAULT_FIRMWARE "dvb-fe-nxt2004.fw"
44 #define CRC_CCIT_MASK 0x1021
46 #include <linux/kernel.h>
48 #include <linux/module.h>
49 #include <linux/slab.h>
50 #include <linux/string.h>
67 #define dprintk(args...) do { if (debug) pr_debug(args); } while (0)
75 pr_warn(
"%s: i2c write error (addr 0x%02x, err == %i)\n",
88 pr_warn(
"%s: i2c read error (addr 0x%02x, err == %i)\n",
96 const u8 *buf,
u8 len)
100 struct i2c_msg msg = { .
addr = state->
config->demod_address, .flags = 0, .buf = buf2, .len = len + 1 };
103 memcpy(&buf2[1], buf, len);
106 pr_warn(
"%s: i2c write error (addr 0x%02x, err == %i)\n",
107 __func__, state->
config->demod_address, err);
117 struct i2c_msg msg [] = { { .
addr = state->
config->demod_address, .flags = 0, .buf =
reg2, .len = 1 },
118 { .addr = state->
config->demod_address, .flags =
I2C_M_RD, .buf =
buf, .len = len } };
123 pr_warn(
"%s: i2c read error (addr 0x%02x, err == %i)\n",
124 __func__, state->
config->demod_address, err);
137 if((crc^input) & 0x8000)
152 nxt200x_writebytes(state, 0x35, ®, 1);
155 nxt200x_writebytes(state, 0x36, data, len);
171 len2 = ((attr << 4) | 0x10) |
len;
180 nxt200x_writebytes(state, 0x34, &len2, 1);
183 nxt200x_writebytes(state, 0x21, &buf, 1);
185 nxt200x_readbytes(state, 0x21, &buf, 1);
189 if ((buf & 0x02) == 0)
201 pr_warn(
"Error writing multireg register 0x%02X\n", reg);
206 static int nxt200x_readreg_multibyte (
struct nxt200x_state* state,
u8 reg,
u8* data,
u8 len)
213 nxt200x_writebytes(state, 0x35, ®, 1);
219 nxt200x_writebytes(state, 0x34, &len2, 1);
222 nxt200x_readbytes(state, reg, data, len);
235 len2 = (attr << 4) | len;
236 nxt200x_writebytes(state, 0x34, &len2, 1);
240 nxt200x_writebytes(state, 0x21, &buf, 1);
243 for(i = 0; i <
len; i++) {
244 nxt200x_readbytes(state, 0x36 + i, &data[i], 1);
254 static void nxt200x_microcontroller_stop (
struct nxt200x_state* state)
273 nxt200x_writebytes(state, 0x22, &buf, 1);
275 while (counter < 20) {
276 nxt200x_readbytes(state, 0x31, &buf, 1);
283 pr_warn(
"Timeout waiting for nxt200x to stop. This is ok after "
284 "firmware upload.\n");
288 static void nxt200x_microcontroller_start (
struct nxt200x_state* state)
294 nxt200x_writebytes(state, 0x22, &buf, 1);
297 static void nxt2004_microcontroller_init (
struct nxt200x_state* state)
304 nxt200x_writebytes(state, 0x2b, buf, 1);
306 nxt200x_writebytes(state, 0x34, buf, 1);
308 nxt200x_writebytes(state, 0x35, buf, 1);
309 buf[0] = 0x01; buf[1] = 0x23; buf[2] = 0x45; buf[3] = 0x67; buf[4] = 0x89;
310 buf[5] = 0xAB; buf[6] = 0xCD; buf[7] = 0xEF; buf[8] = 0xC0;
311 nxt200x_writebytes(state, 0x36, buf, 9);
313 nxt200x_writebytes(state, 0x21, buf, 1);
315 while (counter < 20) {
316 nxt200x_readbytes(state, 0x21, buf, 1);
323 pr_warn(
"Timeout waiting for nxt2004 to init.\n");
334 dprintk(
"Tuner Bytes: %*ph\n", 4, data + 1);
340 if (i2c_writebytes(state, data[0], data+1, 4))
341 pr_warn(
"error writing to tuner\n");
344 i2c_readbytes(state, data[0], &buf, 1);
350 pr_warn(
"timeout waiting for tuner lock\n");
355 nxt200x_writebytes(state, 0x20, &buf, 1);
359 nxt200x_writebytes(state, 0x34, &buf, 1);
362 nxt200x_writebytes(state, 0x36, data+1, 4);
366 nxt200x_writebytes(state, 0x35, &buf, 1);
370 nxt200x_writebytes(state, 0x21, &buf, 1);
373 nxt200x_readbytes(state, 0x21, &buf, 1);
374 if ((buf & 0x80)== 0x00)
379 pr_warn(
"timeout error writing to tuner\n");
396 nxt200x_writebytes(state, 0x08, &buf, 1);
398 nxt200x_writebytes(state, 0x08, &buf, 1);
401 nxt200x_readreg_multibyte(state, 0x08, &buf, 1);
403 nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
405 nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
417 u8 buf[3], written = 0, chunkpos = 0;
418 u16 rambase, position, crc = 0;
424 nxt200x_readbytes(state, 0x10, buf, 1);
431 dprintk(
"rambase on this nxt2002 is %04X\n", rambase);
435 nxt200x_writebytes(state, 0x2B, buf, 1);
437 for (position = 0; position < fw->
size; position++) {
441 buf[0] = ((rambase + position) >> 8);
442 buf[1] = (rambase + position) & 0xFF;
445 nxt200x_writebytes(state, 0x29, buf, 3);
450 if ((written % 4) == 0)
451 nxt200x_writebytes(state, chunkpos, &fw->
data[position-3], 4);
453 crc = nxt200x_crc(crc, fw->
data[position]);
455 if ((written == 255) || (position+1 == fw->
size)) {
457 nxt200x_writebytes(state, chunkpos+4-(written %4),
458 &fw->
data[position-(written %4) + 1],
464 nxt200x_writebytes(state, 0x2C, buf, 2);
467 nxt200x_readbytes(state, 0x2A, buf, 1);
471 nxt200x_writebytes(state, 0x2B, buf, 1);
485 u16 rambase, position, crc=0;
495 nxt200x_writebytes(state, 0x2B, buf,1);
498 for (position = 0; position < fw->
size; position++) {
499 crc = nxt200x_crc(crc, fw->
data[position]);
502 buf[0] = rambase >> 8;
503 buf[1] = rambase & 0xFF;
506 nxt200x_writebytes(state,0x29,buf,3);
508 for (position = 0; position < fw->
size;) {
509 nxt200x_writebytes(state, 0x2C, &fw->
data[position],
510 fw->
size-position > 255 ? 255 : fw->
size-position);
511 position += (fw->
size-position > 255 ? 255 : fw->
size-position);
516 dprintk(
"firmware crc is 0x%02X 0x%02X\n", buf[0], buf[1]);
519 nxt200x_writebytes(state, 0x2C, buf,2);
522 nxt200x_readbytes(state, 0x2C, buf, 1);
526 nxt200x_writebytes(state, 0x2B, buf,1);
531 static int nxt200x_setup_frontend_parameters(
struct dvb_frontend *fe)
538 nxt200x_microcontroller_stop(state);
543 nxt200x_writebytes(state, 0x14, buf, 1);
545 nxt200x_writebytes(state, 0x17, buf, 1);
554 if (state->
config->set_ts_params)
555 state->
config->set_ts_params(fe, 1);
559 if (state->
config->set_ts_params)
560 state->
config->set_ts_params(fe, 0);
567 if (fe->
ops.tuner_ops.calc_regs) {
569 fe->
ops.tuner_ops.calc_regs(fe, buf, 5);
572 nxt200x_writetuner(state, buf);
576 nxt200x_agc_reset(state);
591 nxt200x_writebytes(state, 0x42, buf, 1);
605 nxt200x_writebytes(state, 0x57, buf, 1);
612 nxt200x_writereg_multibyte(state, 0x58, buf, 2);
615 nxt200x_writebytes(state, 0x58, buf, 2);
640 nxt200x_writereg_multibyte(state, 0x5C, buf, 2);
643 nxt200x_writebytes(state, 0x5C, buf, 2);
652 nxt200x_writebytes(state, 0x43, buf, 1);
658 nxt200x_writebytes(state, 0x46, buf, 2);
666 nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
669 nxt200x_writebytes(state, 0x4B, buf, 2);
678 nxt200x_writebytes(state, 0x4D, buf, 1);
682 nxt200x_writebytes(state, 0x55, buf, 1);
686 nxt200x_writebytes(state, 0x41, buf, 1);
689 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
691 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
694 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
696 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
697 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
699 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
701 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
703 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
705 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
706 buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
707 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
708 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
710 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
711 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
713 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
731 nxt200x_writebytes(state, 0x30, buf, 1);
735 nxt200x_writebytes(state, 0x41, buf, 1);
742 nxt200x_writereg_multibyte(state, 0x49, buf, 2);
743 nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
746 nxt200x_writebytes(state, 0x49, buf, 2);
747 nxt200x_writebytes(state, 0x4B, buf, 2);
756 nxt200x_writebytes(state, 0x41, buf, 1);
758 nxt200x_microcontroller_start(state);
761 nxt2004_microcontroller_init(state);
766 nxt200x_writebytes(state, 0x5C, buf, 2);
779 nxt200x_readbytes(state, 0x31, &lock, 1);
797 nxt200x_readreg_multibyte(state, 0xE6, b, 3);
799 *ber = ((b[0] << 8) + b[1]) * 8;
804 static int nxt200x_read_signal_strength(
struct dvb_frontend* fe,
u16* strength)
812 nxt200x_writebytes(state, 0xA1, b, 1);
815 nxt200x_readreg_multibyte(state, 0xA6, b, 2);
817 temp = (b[0] << 8) | b[1];
818 *strength = ((0x7FFF -
temp) & 0x0FFF) * 16;
833 nxt200x_writebytes(state, 0xA1, b, 1);
836 nxt200x_readreg_multibyte(state, 0xA6, b, 2);
838 temp = (b[0] << 8) | b[1];
843 snrdb = 1000*24 + ( 1000*(30-24) * (
temp2 - 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
844 else if (
temp2 > 0x7EC0)
845 snrdb = 1000*18 + ( 1000*(24-18) * (
temp2 - 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
846 else if (
temp2 > 0x7C00)
847 snrdb = 1000*12 + ( 1000*(18-12) * (
temp2 - 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
849 snrdb = 1000*0 + ( 1000*(12-0) * (
temp2 - 0 ) / ( 0x7C00 - 0 ) );
852 *snr = snrdb * (0xFFFF/32000);
857 static int nxt200x_read_ucblocks(
struct dvb_frontend* fe,
u32* ucblocks)
862 nxt200x_readreg_multibyte(state, 0xE6, b, 3);
881 pr_debug(
"%s: Waiting for firmware upload (%s)...\n",
884 state->
i2c->dev.parent);
885 pr_debug(
"%s: Waiting for firmware upload(2)...\n", __func__);
887 pr_err(
"%s: No firmware uploaded (timeout or file not found?)"
892 ret = nxt2002_load_firmware(fe, fw);
895 pr_err(
"%s: Writing firmware to device failed\n", __func__);
898 pr_info(
"%s: Firmware upload complete\n", __func__);
901 nxt200x_microcontroller_stop(state);
905 nxt200x_writebytes(state, 0x2B, buf, 1);
908 nxt200x_microcontroller_stop(state);
912 nxt200x_writebytes(state, 0x08, buf, 1);
914 nxt200x_writebytes(state, 0x08, buf, 1);
918 nxt200x_writebytes(state, 0x57, buf, 1);
922 nxt200x_writebytes(state, 0x09, buf, 1);
927 nxt200x_writebytes(state, 0xE9, buf, 2);
931 nxt200x_writebytes(state, 0xCC, buf, 1);
945 nxt200x_writebytes(state, 0x1E, buf, 1);
948 pr_debug(
"%s: Waiting for firmware upload (%s)...\n",
951 state->
i2c->dev.parent);
952 pr_debug(
"%s: Waiting for firmware upload(2)...\n", __func__);
954 pr_err(
"%s: No firmware uploaded (timeout or file not found?)"
959 ret = nxt2004_load_firmware(fe, fw);
962 pr_err(
"%s: Writing firmware to device failed\n", __func__);
965 pr_info(
"%s: Firmware upload complete\n", __func__);
969 nxt200x_writebytes(state, 0x19, buf, 1);
971 nxt2004_microcontroller_init(state);
972 nxt200x_microcontroller_stop(state);
973 nxt200x_microcontroller_stop(state);
974 nxt2004_microcontroller_init(state);
975 nxt200x_microcontroller_stop(state);
979 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
981 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
985 nxt200x_writebytes(state, 0x57, buf, 1);
990 nxt200x_writebytes(state, 0x35, buf, 2);
992 nxt200x_writebytes(state, 0x34, buf, 1);
994 nxt200x_writebytes(state, 0x21, buf, 1);
998 nxt200x_writebytes(state, 0x0A, buf, 1);
1002 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1007 nxt200x_writebytes(state, 0xE9, buf, 2);
1011 nxt200x_writebytes(state, 0xCC, buf, 1);
1014 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1016 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1019 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1021 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1022 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1024 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1027 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1029 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1031 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1032 buf[0] = 0x31; buf[1] = 0x5E; buf[2] = 0x66;
1033 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1035 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1037 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1038 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1040 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1042 nxt200x_readbytes(state, 0x10, buf, 1);
1044 nxt200x_writebytes(state, 0x10, buf, 1);
1045 nxt200x_readbytes(state, 0x0A, buf, 1);
1047 nxt200x_writebytes(state, 0x0A, buf, 1);
1049 nxt2004_microcontroller_init(state);
1052 nxt200x_writebytes(state, 0x0A, buf, 1);
1054 nxt200x_writebytes(state, 0xE9, buf, 1);
1056 nxt200x_writebytes(state, 0xEA, buf, 1);
1058 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1060 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1061 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1063 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1066 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1068 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1069 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1071 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1073 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1075 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1077 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1078 buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
1079 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1081 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1083 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1085 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1087 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1090 nxt200x_readbytes(state, 0x10, buf, 1);
1092 nxt200x_writebytes(state, 0x10, buf, 1);
1094 nxt200x_writebytes(state, 0x13, buf, 1);
1096 nxt200x_writebytes(state, 0x16, buf, 1);
1098 nxt200x_writebytes(state, 0x14, buf, 1);
1100 nxt200x_writebytes(state, 0x14, buf, 1);
1101 nxt200x_writebytes(state, 0x17, buf, 1);
1102 nxt200x_writebytes(state, 0x14, buf, 1);
1103 nxt200x_writebytes(state, 0x17, buf, 1);
1116 ret = nxt2002_init(fe);
1119 ret = nxt2004_init(fe);
1150 u8 buf [] = {0,0,0,0,0};
1163 nxt200x_readbytes(state, 0x00, buf, 5);
1164 dprintk(
"NXT info: %*ph\n", 5, buf);
1170 pr_info(
"NXT2002 Detected\n");
1174 pr_info(
"NXT2004 Detected\n");
1183 if (buf[0] != 0x04)
goto error;
1184 if (buf[1] != 0x02)
goto error;
1185 if (buf[2] != 0x11)
goto error;
1186 if (buf[3] != 0x20)
goto error;
1187 if (buf[4] != 0x00)
goto error;
1190 if (buf[0] != 0x05)
goto error;
1203 pr_err(
"Unknown/Unsupported NXT chip: %*ph\n", 5, buf);
1210 .name =
"Nextwave NXT200X VSB/QAM frontend",
1211 .frequency_min = 54000000,
1212 .frequency_max = 860000000,
1213 .frequency_stepsize = 166666,
1219 .release = nxt200x_release,
1221 .init = nxt200x_init,
1222 .sleep = nxt200x_sleep,
1224 .set_frontend = nxt200x_setup_frontend_parameters,
1225 .get_tune_settings = nxt200x_get_tune_settings,
1227 .read_status = nxt200x_read_status,
1228 .read_ber = nxt200x_read_ber,
1229 .read_signal_strength = nxt200x_read_signal_strength,
1230 .read_snr = nxt200x_read_snr,
1231 .read_ucblocks = nxt200x_read_ucblocks,
1237 MODULE_DESCRIPTION(
"NXT200X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
1238 MODULE_AUTHOR(
"Kirk Lapray, Michael Krufky, Jean-Francois Thibert, and Taylor Jacob");