2 Driver for the Microtune 7202D Frontend
6 This driver needs a copy of the Avermedia firmware. The version tested
7 is part of the Avermedia DVB-T 1.3.26.3 Application. If the software is
8 installed in Windows the file will be in the /Program Files/AVerTV DVB-T/
9 directory and is called sc_main.mc. Alternatively it can "extracted" from
10 the install cab files. Copy this file to '/usr/lib/hotplug/firmware/sc_main.mc'.
11 With this version of the file the first 10 bytes are discarded and the
12 next 0x4000 loaded. This may change in future versions.
15 #include <linux/kernel.h>
16 #include <linux/vmalloc.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/string.h>
20 #include <linux/slab.h>
22 #include <linux/unistd.h>
23 #include <linux/fcntl.h>
24 #include <linux/errno.h>
25 #include <linux/i2c.h>
26 #include <linux/syscalls.h>
28 #include "dvb_frontend.h"
29 #include "dvb_functions.h"
31 #ifndef DVB_SP887X_FIRMWARE_FILE
32 #define DVB_SP887X_FIRMWARE_FILE "/usr/lib/hotplug/firmware/sc_main.mc"
35 static char *sp887x_firmware = DVB_SP887X_FIRMWARE_FILE;
38 #define dprintk(x...) printk(x)
44 #define LOG(dir,addr,buf,len) \
47 printk("%s (%02x):", dir, addr & 0xff); \
48 for (i=0; i<len; i++) \
49 printk(" 0x%02x,", buf[i] & 0xff); \
53 #define LOG(dir,addr,buf,len)
58 struct dvb_frontend_info sp887x_info = {
59 .name = "Microtune MT7202DTF",
61 .frequency_min = 50500000,
62 .frequency_max = 858000000,
63 .frequency_stepsize = 166666,
64 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
65 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
66 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
73 int i2c_writebytes (struct dvb_frontend *fe, u8 addr, u8 *buf, u8 len)
75 struct dvb_i2c_bus *i2c = fe->i2c;
76 struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = len };
79 LOG("i2c_writebytes", msg.addr, msg.buf, msg.len);
81 if ((err = i2c->xfer (i2c, &msg, 1)) != 1) {
82 printk ("%s: i2c write error (addr %02x, err == %i)\n",
83 __FUNCTION__, addr, err);
93 int sp887x_writereg (struct dvb_frontend *fe, u16 reg, u16 data)
95 struct dvb_i2c_bus *i2c = fe->i2c;
96 u8 b0 [] = { reg >> 8 , reg & 0xff, data >> 8, data & 0xff };
97 struct i2c_msg msg = { .addr = 0x70, .flags = 0, .buf = b0, .len = 4 };
100 LOG("sp887x_writereg", msg.addr, msg.buf, msg.len);
102 if ((ret = i2c->xfer(i2c, &msg, 1)) != 1) {
104 * in case of soft reset we ignore ACK errors...
106 if (!(reg == 0xf1a && data == 0x000 &&
107 (ret == -EREMOTEIO || ret == -EFAULT)))
109 printk("%s: writereg error "
110 "(reg %03x, data %03x, ret == %i)\n",
111 __FUNCTION__, reg & 0xffff, data & 0xffff, ret);
121 u16 sp887x_readreg (struct dvb_frontend *fe, u16 reg)
123 struct dvb_i2c_bus *i2c = fe->i2c;
124 u8 b0 [] = { reg >> 8 , reg & 0xff };
127 struct i2c_msg msg[] = {{ .addr = 0x70, .flags = 0, .buf = b0, .len = 2 },
128 { .addr = 0x70, .flags = I2C_M_RD, .buf = b1, .len = 2 }};
130 LOG("sp887x_readreg (w)", msg[0].addr, msg[0].buf, msg[0].len);
131 LOG("sp887x_readreg (r)", msg[1].addr, msg[1].buf, msg[1].len);
133 if ((ret = i2c->xfer(i2c, msg, 2)) != 2)
134 printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
136 return (((b1[0] << 8) | b1[1]) & 0xfff);
141 void sp887x_microcontroller_stop (struct dvb_frontend *fe)
143 dprintk("%s\n", __FUNCTION__);
144 sp887x_writereg(fe, 0xf08, 0x000);
145 sp887x_writereg(fe, 0xf09, 0x000);
147 /* microcontroller STOP */
148 sp887x_writereg(fe, 0xf00, 0x000);
153 void sp887x_microcontroller_start (struct dvb_frontend *fe)
155 dprintk("%s\n", __FUNCTION__);
156 sp887x_writereg(fe, 0xf08, 0x000);
157 sp887x_writereg(fe, 0xf09, 0x000);
159 /* microcontroller START */
160 sp887x_writereg(fe, 0xf00, 0x001);
165 void sp887x_setup_agc (struct dvb_frontend *fe)
167 /* setup AGC parameters */
168 dprintk("%s\n", __FUNCTION__);
169 sp887x_writereg(fe, 0x33c, 0x054);
170 sp887x_writereg(fe, 0x33b, 0x04c);
171 sp887x_writereg(fe, 0x328, 0x000);
172 sp887x_writereg(fe, 0x327, 0x005);
173 sp887x_writereg(fe, 0x326, 0x001);
174 sp887x_writereg(fe, 0x325, 0x001);
175 sp887x_writereg(fe, 0x324, 0x001);
176 sp887x_writereg(fe, 0x318, 0x050);
177 sp887x_writereg(fe, 0x317, 0x3fe);
178 sp887x_writereg(fe, 0x316, 0x001);
179 sp887x_writereg(fe, 0x313, 0x005);
180 sp887x_writereg(fe, 0x312, 0x002);
181 sp887x_writereg(fe, 0x306, 0x000);
182 sp887x_writereg(fe, 0x303, 0x000);
189 * load firmware and setup MPEG interface...
192 int sp887x_initial_setup (struct dvb_frontend *fe)
194 u8 buf [BLOCKSIZE+2];
195 unsigned char *firmware = NULL;
202 dprintk("%s\n", __FUNCTION__);
205 sp887x_writereg(fe, 0xf1a, 0x000);
207 sp887x_microcontroller_stop (fe);
213 fd = sys_open(sp887x_firmware, 0, 0);
215 printk(KERN_WARNING "%s: Unable to open firmware %s\n", __FUNCTION__,
219 filesize = sys_lseek(fd, 0L, 2);
221 printk(KERN_WARNING "%s: Firmware %s is empty\n", __FUNCTION__,
229 // allocate buffer for it
230 firmware = vmalloc(fw_size);
231 if (firmware == NULL) {
232 printk(KERN_WARNING "%s: Out of memory loading firmware\n",
239 // read the first 16384 bytes from the file
240 // ignore the first 10 bytes
241 sys_lseek(fd, 10, 0);
242 if (sys_read(fd, firmware, fw_size) != fw_size) {
243 printk(KERN_WARNING "%s: Failed to read firmware\n", __FUNCTION__);
251 printk ("%s: firmware upload... ", __FUNCTION__);
253 /* setup write pointer to -1 (end of memory) */
254 /* bit 0x8000 in address is set to enable 13bit mode */
255 sp887x_writereg(fe, 0x8f08, 0x1fff);
257 /* dummy write (wrap around to start of memory) */
258 sp887x_writereg(fe, 0x8f0a, 0x0000);
260 for (i=0; i<fw_size; i+=BLOCKSIZE) {
267 /* bit 0x8000 in address is set to enable 13bit mode */
268 /* bit 0x4000 enables multibyte read/write transfers */
269 /* write register is 0xf0a */
273 memcpy(&buf[2], firmware + i, c);
275 if ((err = i2c_writebytes (fe, 0x70, buf, c+2)) < 0) {
276 printk ("failed.\n");
277 printk ("%s: i2c error (err == %i)\n", __FUNCTION__, err);
285 /* don't write RS bytes between packets */
286 sp887x_writereg(fe, 0xc13, 0x001);
288 /* suppress clock if (!data_valid) */
289 sp887x_writereg(fe, 0xc14, 0x000);
291 /* setup MPEG interface... */
292 sp887x_writereg(fe, 0xc1a, 0x872);
293 sp887x_writereg(fe, 0xc1b, 0x001);
294 sp887x_writereg(fe, 0xc1c, 0x000); /* parallel mode (serial mode == 1) */
295 sp887x_writereg(fe, 0xc1a, 0x871);
297 /* ADC mode, 2 for MT8872, 3 for SP8870/SP8871 */
298 sp887x_writereg(fe, 0x301, 0x002);
300 sp887x_setup_agc(fe);
302 /* bit 0x010: enable data valid signal */
303 sp887x_writereg(fe, 0xd00, 0x010);
304 sp887x_writereg(fe, 0x0d1, 0x000);
312 * returns the actual tuned center frequency which can be used
313 * to initialise the AFC registers
316 int tsa5060_setup_pll (struct dvb_frontend *fe, int freq)
318 u8 cfg, cpump, band_select;
322 div = (36000000 + freq + 83333) / 166666;
325 cpump = freq < 175000000 ? 2 : freq < 390000000 ? 1 :
326 freq < 470000000 ? 2 : freq < 750000000 ? 2 : 3;
328 band_select = freq < 175000000 ? 0x0e : freq < 470000000 ? 0x05 : 0x03;
330 buf [0] = (div >> 8) & 0x7f;
331 buf [1] = div & 0xff;
332 buf [2] = ((div >> 10) & 0x60) | cfg;
333 buf [3] = cpump | band_select;
335 /* open i2c gate for PLL message transmission... */
336 sp887x_writereg(fe, 0x206, 0x001);
337 i2c_writebytes(fe, 0x60, buf, 4);
338 sp887x_writereg(fe, 0x206, 0x000);
340 return (div * 166666 - 36000000);
346 int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05)
348 int known_parameters = 1;
352 switch (p->u.ofdm.constellation) {
356 *reg0xc05 |= (1 << 10);
359 *reg0xc05 |= (2 << 10);
362 known_parameters = 0;
368 switch (p->u.ofdm.hierarchy_information) {
372 *reg0xc05 |= (1 << 7);
375 *reg0xc05 |= (2 << 7);
378 *reg0xc05 |= (3 << 7);
381 known_parameters = 0;
387 switch (p->u.ofdm.code_rate_HP) {
391 *reg0xc05 |= (1 << 3);
394 *reg0xc05 |= (2 << 3);
397 *reg0xc05 |= (3 << 3);
400 *reg0xc05 |= (4 << 3);
403 known_parameters = 0;
409 if (known_parameters)
410 *reg0xc05 |= (2 << 1); /* use specified parameters */
412 *reg0xc05 |= (1 << 1); /* enable autoprobing */
419 * estimates division of two 24bit numbers,
420 * derived from the ves1820/stv0299 driver code
423 void divide (int n, int d, int *quotient_i, int *quotient_f)
435 q = (q << 8) | (r / d);
437 *quotient_f = (q << 8) | (r / d);
443 void sp887x_correct_offsets (struct dvb_frontend *fe,
444 struct dvb_frontend_parameters *p,
447 static const u32 srate_correction [] = { 1879617, 4544878, 8098561 };
448 int bw_index = p->u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
449 int freq_offset = actual_freq - p->frequency;
450 int sysclock = 61003; //[kHz]
451 int ifreq = 36000000;
455 if (p->inversion == INVERSION_ON)
456 freq = ifreq - freq_offset;
458 freq = ifreq + freq_offset;
460 divide(freq / 333, sysclock, NULL, &frequency_shift);
462 if (p->inversion == INVERSION_ON)
463 frequency_shift = -frequency_shift;
465 /* sample rate correction */
466 sp887x_writereg(fe, 0x319, srate_correction[bw_index] >> 12);
467 sp887x_writereg(fe, 0x31a, srate_correction[bw_index] & 0xfff);
469 /* carrier offset correction */
470 sp887x_writereg(fe, 0x309, frequency_shift >> 12);
471 sp887x_writereg(fe, 0x30a, frequency_shift & 0xfff);
476 int sp887x_setup_frontend_parameters (struct dvb_frontend *fe,
477 struct dvb_frontend_parameters *p)
479 int actual_freq, err;
482 if (p->u.ofdm.bandwidth != BANDWIDTH_8_MHZ &&
483 p->u.ofdm.bandwidth != BANDWIDTH_7_MHZ &&
484 p->u.ofdm.bandwidth != BANDWIDTH_6_MHZ)
487 if ((err = configure_reg0xc05(p, ®0xc05)))
490 sp887x_microcontroller_stop(fe);
492 actual_freq = tsa5060_setup_pll(fe, p->frequency);
494 /* read status reg in order to clear pending irqs */
495 sp887x_readreg(fe, 0x200);
497 sp887x_correct_offsets(fe, p, actual_freq);
499 /* filter for 6/7/8 Mhz channel */
500 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
502 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
507 sp887x_writereg(fe, 0x311, val);
509 /* scan order: 2k first = 0, 8k first = 1 */
510 if (p->u.ofdm.transmission_mode == TRANSMISSION_MODE_2K)
511 sp887x_writereg(fe, 0x338, 0x000);
513 sp887x_writereg(fe, 0x338, 0x001);
515 sp887x_writereg(fe, 0xc05, reg0xc05);
517 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
519 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
524 /* enable OFDM and SAW bits as lock indicators in sync register 0xf17,
525 * optimize algorithm for given bandwidth...
527 sp887x_writereg(fe, 0xf14, 0x160 | val);
528 sp887x_writereg(fe, 0xf15, 0x000);
530 sp887x_microcontroller_start(fe);
536 int sp887x_ioctl (struct dvb_frontend *fe, unsigned int cmd, void *arg)
540 memcpy (arg, &sp887x_info, sizeof(struct dvb_frontend_info));
545 u16 snr12 = sp887x_readreg(fe, 0xf16);
546 u16 sync0x200 = sp887x_readreg(fe, 0x200);
547 u16 sync0xf17 = sp887x_readreg(fe, 0xf17);
548 fe_status_t *status = arg;
553 *status |= FE_HAS_SIGNAL;
555 //if (sync0x200 & 0x004)
556 // *status |= FE_HAS_SYNC | FE_HAS_CARRIER;
558 //if (sync0x200 & 0x008)
559 // *status |= FE_HAS_VITERBI;
561 if ((sync0xf17 & 0x00f) == 0x002) {
562 *status |= FE_HAS_LOCK;
563 *status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_CARRIER;
566 if (sync0x200 & 0x001) { /* tuner adjustment requested...*/
567 int steps = (sync0x200 >> 4) & 0x00f;
570 dprintk("sp887x: implement tuner adjustment (%+i steps)!!\n",
581 *ber = (sp887x_readreg(fe, 0xc08) & 0x3f) |
582 (sp887x_readreg(fe, 0xc07) << 6);
583 sp887x_writereg(fe, 0xc08, 0x000);
584 sp887x_writereg(fe, 0xc07, 0x000);
591 case FE_READ_SIGNAL_STRENGTH: // FIXME: correct registers ?
593 u16 snr12 = sp887x_readreg(fe, 0xf16);
594 u32 signal = 3 * (snr12 << 4);
595 *((u16*) arg) = (signal < 0xffff) ? signal : 0xffff;
601 u16 snr12 = sp887x_readreg(fe, 0xf16);
602 *(u16*) arg = (snr12 << 4) | (snr12 >> 8);
606 case FE_READ_UNCORRECTED_BLOCKS:
608 u32 *ublocks = (u32 *) arg;
609 *ublocks = sp887x_readreg(fe, 0xc0c);
610 if (*ublocks == 0xfff)
615 case FE_SET_FRONTEND:
616 return sp887x_setup_frontend_parameters(fe, arg);
618 case FE_GET_FRONTEND: // FIXME: read known values back from Hardware...
622 /* tristate TS output and disable interface pins */
623 sp887x_writereg(fe, 0xc18, 0x000);
627 if (fe->data == NULL) { /* first time initialisation... */
628 fe->data = (void*) ~0;
629 sp887x_initial_setup (fe);
631 /* enable TS output and interface pins */
632 sp887x_writereg(fe, 0xc18, 0x00d);
635 case FE_GET_TUNE_SETTINGS:
637 struct dvb_frontend_tune_settings* fesettings = (struct dvb_frontend_tune_settings*) arg;
638 fesettings->min_delay_ms = 50;
639 fesettings->step_size = 0;
640 fesettings->max_drift = 0;
654 int sp887x_attach (struct dvb_i2c_bus *i2c, void **data)
656 struct i2c_msg msg = {.addr = 0x70, .flags = 0, .buf = NULL, .len = 0 };
658 dprintk ("%s\n", __FUNCTION__);
660 if (i2c->xfer (i2c, &msg, 1) != 1)
663 return dvb_register_frontend (sp887x_ioctl, i2c, NULL, &sp887x_info);
668 void sp887x_detach (struct dvb_i2c_bus *i2c, void *data)
670 dprintk ("%s\n", __FUNCTION__);
671 dvb_unregister_frontend (sp887x_ioctl, i2c);
676 int __init init_sp887x (void)
678 dprintk ("%s\n", __FUNCTION__);
679 return dvb_register_i2c_device (NULL, sp887x_attach, sp887x_detach);
684 void __exit exit_sp887x (void)
686 dprintk ("%s\n", __FUNCTION__);
687 dvb_unregister_i2c_device (sp887x_attach);
691 module_init(init_sp887x);
692 module_exit(exit_sp887x);
695 MODULE_DESCRIPTION("sp887x DVB-T demodulator driver");
696 MODULE_LICENSE("GPL");