22 #include <linux/slab.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
33 static int force_fw_upload;
35 #define dprintk(args...) \
43 #define DS3000_DEFAULT_FIRMWARE "dvb-fe-ds3000.fw"
45 #define DS3000_SAMPLE_RATE 96000
46 #define DS3000_XTAL_FREQ 27000
49 static u8 ds3000_dvbs_init_tab[] = {
134 static u8 ds3000_dvbs2_init_tab[] = {
246 .flags = 0, .buf =
buf, .len = 2 };
249 dprintk(
"%s: write reg 0x%02x, value 0x%02x\n", __func__, reg, data);
254 " value == 0x%02x)\n", __func__, err, reg, data);
261 static int ds3000_tuner_writereg(
struct ds3000_state *state,
int reg,
int data)
263 u8 buf[] = {
reg, data };
265 .flags = 0, .buf =
buf, .len = 2 };
268 dprintk(
"%s: write reg 0x%02x, value 0x%02x\n", __func__, reg, data);
270 ds3000_writereg(state, 0x03, 0x11);
273 printk(
"%s: writereg error(err == %i, reg == 0x%02x,"
274 " value == 0x%02x)\n", __func__, err, reg, data);
282 static int ds3000_writeFW(
struct ds3000_state *state,
int reg,
303 for (i = 0; i <
len; i += 32) {
304 memcpy(buf + 1, data + i, 32);
306 dprintk(
"%s: write reg 0x%02x, len = %d\n", __func__, reg, len);
311 "reg == 0x%02x\n", __func__, ret, reg);
334 .addr = state->
config->demod_address,
348 dprintk(
"%s: read reg 0x%02x, value 0x%02x\n", __func__, reg, b1[0]);
353 static int ds3000_tuner_readreg(
struct ds3000_state *state,
u8 reg)
372 ds3000_writereg(state, 0x03, 0x12);
380 dprintk(
"%s: read reg 0x%02x, value 0x%02x\n", __func__, reg, b1[0]);
385 static int ds3000_load_firmware(
struct dvb_frontend *fe,
388 static int ds3000_firmware_ondemand(
struct dvb_frontend *fe)
396 ret = ds3000_readreg(state, 0xb2);
405 printk(
KERN_INFO "%s: Waiting for firmware upload (%s)...\n", __func__,
408 state->
i2c->dev.parent);
412 "found?)\n", __func__);
419 ret = ds3000_load_firmware(fe, fw);
421 printk(
"%s: Writing firmware to device failed\n", __func__);
425 dprintk(
"%s: Firmware upload %s\n", __func__,
426 ret == 0 ?
"complete" :
"failed");
434 static int ds3000_load_firmware(
struct dvb_frontend *fe,
440 dprintk(
"Firmware is %zu bytes (%02x %02x .. %02x %02x)\n",
448 ds3000_writereg(state, 0xb2, 0x01);
450 ds3000_writeFW(state, 0xb0, fw->
data, fw->
size);
451 ds3000_writereg(state, 0xb2, 0x00);
461 dprintk(
"%s(%d)\n", __func__, voltage);
463 data = ds3000_readreg(state, 0xa2);
478 ds3000_writereg(state, 0xa2, data);
493 lock = ds3000_readreg(state, 0xd1);
494 if ((lock & 0x07) == 0x07)
501 lock = ds3000_readreg(state, 0x0d);
502 if ((lock & 0x8f) == 0x8f)
512 dprintk(
"%s: status = 0x%02x\n", __func__, lock);
523 u32 ber_reading, lpdc_frames;
531 ds3000_writereg(state, 0xf9, 0x04);
533 data = ds3000_readreg(state, 0xf8);
535 if ((data & 0x10) == 0) {
539 *ber = (ds3000_readreg(state, 0xf7) << 8) |
540 ds3000_readreg(state, 0xf6);
545 ds3000_writereg(state, 0xf8, data);
546 ds3000_writereg(state, 0xf8, data);
554 lpdc_frames = (ds3000_readreg(state, 0xd7) << 16) |
555 (ds3000_readreg(state, 0xd6) << 8) |
556 ds3000_readreg(state, 0xd5);
558 ber_reading = (ds3000_readreg(state, 0xf8) << 8) |
559 ds3000_readreg(state, 0xf7);
560 if (lpdc_frames > 750) {
562 ds3000_writereg(state, 0xd1, 0x01);
564 ds3000_writereg(state, 0xf9, 0x01);
566 ds3000_writereg(state, 0xf9, 0x00);
568 ds3000_writereg(state, 0xd1, 0x00);
583 static int ds3000_read_signal_strength(
struct dvb_frontend *fe,
584 u16 *signal_strength)
587 u16 sig_reading, sig_strength;
592 rfgain = ds3000_tuner_readreg(state, 0x3d) & 0x1f;
593 bbgain = ds3000_tuner_readreg(state, 0x21) & 0x1f;
600 sig_reading = rfgain * 2 + bbgain * 3;
602 sig_strength = 40 + (64 - sig_reading) * 50 / 64 ;
605 *signal_strength = sig_strength * 1000;
607 dprintk(
"%s: raw / cooked = 0x%04x / 0x%04x\n", __func__,
608 sig_reading, *signal_strength);
618 u8 snr_reading, snr_value;
619 u32 dvbs2_signal_reading, dvbs2_noise_reading,
tmp;
620 static const u16 dvbs_snr_tab[] = {
621 0x0000, 0x1b13, 0x2aea, 0x3627, 0x3ede, 0x45fe, 0x4c03,
622 0x513a, 0x55d4, 0x59f2, 0x5dab, 0x6111, 0x6431, 0x6717,
623 0x69c9, 0x6c4e, 0x6eac, 0x70e8, 0x7304, 0x7505
625 static const u16 dvbs2_snr_tab[] = {
626 0x0000, 0x0bc2, 0x12a3, 0x1785, 0x1b4e, 0x1e65, 0x2103,
627 0x2347, 0x2546, 0x2710, 0x28ae, 0x2a28, 0x2b83, 0x2cc5,
628 0x2df1, 0x2f09, 0x3010, 0x3109, 0x31f4, 0x32d2, 0x33a6,
629 0x3470, 0x3531, 0x35ea, 0x369b, 0x3746, 0x37ea, 0x3888,
630 0x3920, 0x39b3, 0x3a42, 0x3acc, 0x3b51, 0x3bd3, 0x3c51,
631 0x3ccb, 0x3d42, 0x3db6, 0x3e27, 0x3e95, 0x3f00, 0x3f68,
632 0x3fcf, 0x4033, 0x4094, 0x40f4, 0x4151, 0x41ac, 0x4206,
633 0x425e, 0x42b4, 0x4308, 0x435b, 0x43ac, 0x43fc, 0x444a,
634 0x4497, 0x44e2, 0x452d, 0x4576, 0x45bd, 0x4604, 0x4649,
635 0x468e, 0x46d1, 0x4713, 0x4755, 0x4795, 0x47d4, 0x4813,
636 0x4851, 0x488d, 0x48c9, 0x4904, 0x493f, 0x4978, 0x49b1,
637 0x49e9, 0x4a20, 0x4a57
644 snr_reading = ds3000_readreg(state, 0xff);
646 if (snr_reading == 0)
649 if (snr_reading > 20)
651 snr_value = dvbs_snr_tab[snr_reading - 1] * 10 / 23026;
654 *snr = snr_value * 8 * 655;
656 dprintk(
"%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
660 dvbs2_noise_reading = (ds3000_readreg(state, 0x8c) & 0x3f) +
661 (ds3000_readreg(state, 0x8d) << 4);
662 dvbs2_signal_reading = ds3000_readreg(state, 0x8e);
663 tmp = dvbs2_signal_reading * dvbs2_signal_reading >> 1;
668 if (dvbs2_noise_reading == 0) {
675 if (tmp > dvbs2_noise_reading) {
676 snr_reading = tmp / dvbs2_noise_reading;
677 if (snr_reading > 80)
679 snr_value = dvbs2_snr_tab[snr_reading - 1] / 1000;
682 *snr = snr_value * 5 * 655;
684 snr_reading = dvbs2_noise_reading /
tmp;
685 if (snr_reading > 80)
687 *snr = -(dvbs2_snr_tab[snr_reading] / 1000);
689 dprintk(
"%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
700 static int ds3000_read_ucblocks(
struct dvb_frontend *fe,
u32 *ucblocks)
711 *ucblocks = (ds3000_readreg(state, 0xf5) << 8) |
712 ds3000_readreg(state, 0xf4);
713 data = ds3000_readreg(state, 0xf8);
716 ds3000_writereg(state, 0xf8, data);
719 ds3000_writereg(state, 0xf8, data);
722 _ucblocks = (ds3000_readreg(state, 0xe2) << 8) |
723 ds3000_readreg(state, 0xe1);
725 *ucblocks = _ucblocks - state->
prevUCBS2;
727 *ucblocks = state->
prevUCBS2 - _ucblocks;
742 dprintk(
"%s(%d)\n", __func__, tone);
748 data = ds3000_readreg(state, 0xa2);
750 ds3000_writereg(state, 0xa2, data);
754 dprintk(
"%s: setting tone on\n", __func__);
755 data = ds3000_readreg(state, 0xa1);
758 ds3000_writereg(state, 0xa1, data);
761 dprintk(
"%s: setting tone off\n", __func__);
762 data = ds3000_readreg(state, 0xa2);
764 ds3000_writereg(state, 0xa2, data);
771 static int ds3000_send_diseqc_msg(
struct dvb_frontend *fe,
780 for (i = 0 ; i < d->
msg_len;) {
787 data = ds3000_readreg(state, 0xa2);
789 ds3000_writereg(state, 0xa2, data);
792 for (i = 0; i < d->
msg_len; i++)
793 ds3000_writereg(state, 0xa3 + i, d->
msg[i]);
795 data = ds3000_readreg(state, 0xa1);
801 data |= ((d->
msg_len - 1) << 3) | 0x07;
802 ds3000_writereg(state, 0xa1, data);
805 for (i = 0; i < 15; i++) {
806 data = ds3000_readreg(state, 0xa1);
807 if ((data & 0x40) == 0)
814 data = ds3000_readreg(state, 0xa1);
817 ds3000_writereg(state, 0xa1, data);
819 data = ds3000_readreg(state, 0xa2);
822 ds3000_writereg(state, 0xa2, data);
827 data = ds3000_readreg(state, 0xa2);
830 ds3000_writereg(state, 0xa2, data);
836 static int ds3000_diseqc_send_burst(
struct dvb_frontend *fe,
845 data = ds3000_readreg(state, 0xa2);
847 ds3000_writereg(state, 0xa2, data);
852 ds3000_writereg(state, 0xa1, 0x02);
855 ds3000_writereg(state, 0xa1, 0x01);
860 for (i = 0; i < 5; i++) {
861 data = ds3000_readreg(state, 0xa1);
862 if ((data & 0x40) == 0)
868 data = ds3000_readreg(state, 0xa1);
871 ds3000_writereg(state, 0xa1, data);
873 data = ds3000_readreg(state, 0xa2);
876 ds3000_writereg(state, 0xa2, data);
881 data = ds3000_readreg(state, 0xa2);
884 ds3000_writereg(state, 0xa2, data);
918 ret = ds3000_readreg(state, 0x00) & 0xfe;
925 ds3000_readreg(state, 0x02),
926 ds3000_readreg(state, 0x01));
940 static int ds3000_set_carrier_offset(
struct dvb_frontend *fe,
941 s32 carrier_offset_khz)
946 tmp = carrier_offset_khz;
953 ds3000_writereg(state, 0x5f, tmp >> 8);
954 ds3000_writereg(state, 0x5e, tmp & 0xff);
966 u8 mlpf, mlpf_new, mlpf_max, mlpf_min, nlpf,
div4;
973 if (state->
config->set_ts_params)
974 state->
config->set_ts_params(fe, 0);
977 ds3000_tuner_writereg(state, 0x07, 0x02);
978 ds3000_tuner_writereg(state, 0x10, 0x00);
979 ds3000_tuner_writereg(state, 0x60, 0x79);
980 ds3000_tuner_writereg(state, 0x08, 0x01);
981 ds3000_tuner_writereg(state, 0x00, 0x01);
986 ds3000_tuner_writereg(state, 0x10, 0x11);
992 ds3000_tuner_writereg(state, 0x10, 0x01);
998 ds3000_tuner_writereg(state, 0x01, (ndiv & 0x0f00) >> 8);
999 ds3000_tuner_writereg(state, 0x02, ndiv & 0x00ff);
1002 ds3000_tuner_writereg(state, 0x03, 0x06);
1003 ds3000_tuner_writereg(state, 0x51, 0x0f);
1004 ds3000_tuner_writereg(state, 0x51, 0x1f);
1005 ds3000_tuner_writereg(state, 0x50, 0x10);
1006 ds3000_tuner_writereg(state, 0x50, 0x00);
1010 ds3000_tuner_writereg(state, 0x51, 0x17);
1011 ds3000_tuner_writereg(state, 0x51, 0x1f);
1012 ds3000_tuner_writereg(state, 0x50, 0x08);
1013 ds3000_tuner_writereg(state, 0x50, 0x00);
1016 value = ds3000_tuner_readreg(state, 0x3d);
1018 if ((value > 4) && (value < 15)) {
1022 value = ((value << 3) | 0x01) & 0x79;
1025 ds3000_tuner_writereg(state, 0x60, value);
1026 ds3000_tuner_writereg(state, 0x51, 0x17);
1027 ds3000_tuner_writereg(state, 0x51, 0x1f);
1028 ds3000_tuner_writereg(state, 0x50, 0x08);
1029 ds3000_tuner_writereg(state, 0x50, 0x00);
1032 ds3000_tuner_writereg(state, 0x04, 0x2e);
1033 ds3000_tuner_writereg(state, 0x51, 0x1b);
1034 ds3000_tuner_writereg(state, 0x51, 0x1f);
1035 ds3000_tuner_writereg(state, 0x50, 0x04);
1036 ds3000_tuner_writereg(state, 0x50, 0x00);
1048 value = ds3000_tuner_readreg(state, 0x26);
1049 mlpf = 0x2e * 207 / ((value << 1) + 151);
1050 mlpf_max = mlpf * 135 / 100;
1051 mlpf_min = mlpf * 78 / 100;
1065 (1000 * f3db / 2)) / (1000 * f3db);
1067 if (mlpf_new < mlpf_min) {
1070 (1000 * f3db / 2)) / (1000 * f3db);
1073 if (mlpf_new > mlpf_max)
1074 mlpf_new = mlpf_max;
1076 ds3000_tuner_writereg(state, 0x04, mlpf_new);
1077 ds3000_tuner_writereg(state, 0x06, nlpf);
1078 ds3000_tuner_writereg(state, 0x51, 0x1b);
1079 ds3000_tuner_writereg(state, 0x51, 0x1f);
1080 ds3000_tuner_writereg(state, 0x50, 0x04);
1081 ds3000_tuner_writereg(state, 0x50, 0x00);
1085 ds3000_tuner_writereg(state, 0x51, 0x1e);
1086 ds3000_tuner_writereg(state, 0x51, 0x1f);
1087 ds3000_tuner_writereg(state, 0x50, 0x01);
1088 ds3000_tuner_writereg(state, 0x50, 0x00);
1092 / (6 + 8) / (div4 + 1) / 2 - c->
frequency;
1095 ds3000_writereg(state, 0x07, 0x80);
1096 ds3000_writereg(state, 0x07, 0x00);
1098 ds3000_writereg(state, 0xb2, 0x01);
1100 ds3000_writereg(state, 0x00, 0x01);
1105 for (i = 0; i <
sizeof(ds3000_dvbs_init_tab); i += 2)
1106 ds3000_writereg(state,
1107 ds3000_dvbs_init_tab[i],
1108 ds3000_dvbs_init_tab[i + 1]);
1109 value = ds3000_readreg(state, 0xfe);
1112 ds3000_writereg(state, 0xfe, value);
1116 for (i = 0; i <
sizeof(ds3000_dvbs2_init_tab); i += 2)
1117 ds3000_writereg(state,
1118 ds3000_dvbs2_init_tab[i],
1119 ds3000_dvbs2_init_tab[i + 1]);
1121 ds3000_writereg(state, 0xfe, 0x54);
1123 ds3000_writereg(state, 0xfe, 0x98);
1130 ds3000_writereg(state, 0x29, 0x80);
1132 ds3000_writereg(state, 0x25, 0x8a);
1139 ds3000_writereg(state, 0xc3, 0x0d);
1140 ds3000_writereg(state, 0xc8, value);
1141 ds3000_writereg(state, 0xc4, 0x10);
1142 ds3000_writereg(state, 0xc7, 0x0e);
1147 ds3000_writereg(state, 0xc3, 0x07);
1148 ds3000_writereg(state, 0xc8, value);
1149 ds3000_writereg(state, 0xc4, 0x09);
1150 ds3000_writereg(state, 0xc7, 0x12);
1153 ds3000_writereg(state, 0xc3, value);
1154 ds3000_writereg(state, 0xc8, 0x0e);
1155 ds3000_writereg(state, 0xc4, 0x07);
1156 ds3000_writereg(state, 0xc7, 0x18);
1159 ds3000_writereg(state, 0xc3, value);
1160 ds3000_writereg(state, 0xc8, 0x0a);
1161 ds3000_writereg(state, 0xc4, 0x05);
1162 ds3000_writereg(state, 0xc7, 0x24);
1168 ds3000_writereg(state, 0x61, value & 0x00ff);
1169 ds3000_writereg(state, 0x62, (value & 0xff00) >> 8);
1172 ds3000_writereg(state, 0x56, 0x00);
1175 ds3000_writereg(state, 0x76, 0x00);
1183 if (state->
config->ci_mode) {
1187 ds3000_writereg(state, 0xfd, 0x80);
1190 ds3000_writereg(state, 0xfd, 0x01);
1196 ds3000_writereg(state, 0x00, 0x00);
1198 ds3000_writereg(state, 0xb2, 0x00);
1200 ds3000_set_carrier_offset(fe, offset_khz);
1202 for (i = 0; i < 30 ; i++) {
1203 ds3000_read_status(fe, &status);
1215 unsigned int mode_flags,
1216 unsigned int *
delay,
1220 int ret = ds3000_set_frontend(fe);
1227 return ds3000_read_status(fe, status);
1248 ds3000_writereg(state, 0x08, 0x01 | ds3000_readreg(state, 0x08));
1252 ds3000_tuner_writereg(state, 0x42, 0x73);
1253 ds3000_tuner_writereg(state, 0x05, 0x01);
1254 ds3000_tuner_writereg(state, 0x62, 0xf5);
1256 ret = ds3000_firmware_ondemand(fe);
1275 .name =
"Montage Technology DS3000/TS2020",
1276 .frequency_min = 950000,
1277 .frequency_max = 2150000,
1278 .frequency_stepsize = 1011,
1279 .frequency_tolerance = 5000,
1280 .symbol_rate_min = 1000000,
1281 .symbol_rate_max = 45000000,
1290 .release = ds3000_release,
1292 .init = ds3000_initfe,
1293 .sleep = ds3000_sleep,
1294 .read_status = ds3000_read_status,
1295 .read_ber = ds3000_read_ber,
1296 .read_signal_strength = ds3000_read_signal_strength,
1297 .read_snr = ds3000_read_snr,
1298 .read_ucblocks = ds3000_read_ucblocks,
1299 .set_voltage = ds3000_set_voltage,
1300 .set_tone = ds3000_set_tone,
1301 .diseqc_send_master_cmd = ds3000_send_diseqc_msg,
1302 .diseqc_send_burst = ds3000_diseqc_send_burst,
1303 .get_frontend_algo = ds3000_get_algo,
1305 .set_frontend = ds3000_set_frontend,
1306 .tune = ds3000_tune,
1316 "DS3000/TS2020 hardware");