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 #define __KERNEL_SYSCALLS__
16 #include <linux/kernel.h>
17 #include <linux/vmalloc.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/string.h>
21 #include <linux/slab.h>
23 #include <linux/unistd.h>
24 #include <linux/fcntl.h>
25 #include <linux/errno.h>
26 #include <linux/i2c.h>
27 #include <linux/syscalls.h>
29 #include "dvb_frontend.h"
30 #include "dvb_functions.h"
32 #ifndef DVB_SP887X_FIRMWARE_FILE
33 #define DVB_SP887X_FIRMWARE_FILE "/usr/lib/hotplug/firmware/sc_main.mc"
36 static char *sp887x_firmware = DVB_SP887X_FIRMWARE_FILE;
39 #define dprintk(x...) printk(x)
45 #define LOG(dir,addr,buf,len) \
48 printk("%s (%02x):", dir, addr & 0xff); \
49 for (i=0; i<len; i++) \
50 printk(" 0x%02x,", buf[i] & 0xff); \
54 #define LOG(dir,addr,buf,len)
59 struct dvb_frontend_info sp887x_info = {
60 .name = "Microtune MT7202DTF",
62 .frequency_min = 50500000,
63 .frequency_max = 858000000,
64 .frequency_stepsize = 166666,
65 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
66 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
67 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
74 int i2c_writebytes (struct dvb_frontend *fe, u8 addr, u8 *buf, u8 len)
76 struct dvb_i2c_bus *i2c = fe->i2c;
77 struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = len };
80 LOG("i2c_writebytes", msg.addr, msg.buf, msg.len);
82 if ((err = i2c->xfer (i2c, &msg, 1)) != 1) {
83 printk ("%s: i2c write error (addr %02x, err == %i)\n",
84 __FUNCTION__, addr, err);
94 int sp887x_writereg (struct dvb_frontend *fe, u16 reg, u16 data)
96 struct dvb_i2c_bus *i2c = fe->i2c;
97 u8 b0 [] = { reg >> 8 , reg & 0xff, data >> 8, data & 0xff };
98 struct i2c_msg msg = { .addr = 0x70, .flags = 0, .buf = b0, .len = 4 };
101 LOG("sp887x_writereg", msg.addr, msg.buf, msg.len);
103 if ((ret = i2c->xfer(i2c, &msg, 1)) != 1) {
105 * in case of soft reset we ignore ACK errors...
107 if (!(reg == 0xf1a && data == 0x000 &&
108 (ret == -EREMOTEIO || ret == -EFAULT)))
110 printk("%s: writereg error "
111 "(reg %03x, data %03x, ret == %i)\n",
112 __FUNCTION__, reg & 0xffff, data & 0xffff, ret);
122 u16 sp887x_readreg (struct dvb_frontend *fe, u16 reg)
124 struct dvb_i2c_bus *i2c = fe->i2c;
125 u8 b0 [] = { reg >> 8 , reg & 0xff };
128 struct i2c_msg msg[] = {{ .addr = 0x70, .flags = 0, .buf = b0, .len = 2 },
129 { .addr = 0x70, .flags = I2C_M_RD, .buf = b1, .len = 2 }};
131 LOG("sp887x_readreg (w)", msg[0].addr, msg[0].buf, msg[0].len);
132 LOG("sp887x_readreg (r)", msg[1].addr, msg[1].buf, msg[1].len);
134 if ((ret = i2c->xfer(i2c, msg, 2)) != 2)
135 printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
137 return (((b1[0] << 8) | b1[1]) & 0xfff);
142 void sp887x_microcontroller_stop (struct dvb_frontend *fe)
144 dprintk("%s\n", __FUNCTION__);
145 sp887x_writereg(fe, 0xf08, 0x000);
146 sp887x_writereg(fe, 0xf09, 0x000);
148 /* microcontroller STOP */
149 sp887x_writereg(fe, 0xf00, 0x000);
154 void sp887x_microcontroller_start (struct dvb_frontend *fe)
156 dprintk("%s\n", __FUNCTION__);
157 sp887x_writereg(fe, 0xf08, 0x000);
158 sp887x_writereg(fe, 0xf09, 0x000);
160 /* microcontroller START */
161 sp887x_writereg(fe, 0xf00, 0x001);
166 void sp887x_setup_agc (struct dvb_frontend *fe)
168 /* setup AGC parameters */
169 dprintk("%s\n", __FUNCTION__);
170 sp887x_writereg(fe, 0x33c, 0x054);
171 sp887x_writereg(fe, 0x33b, 0x04c);
172 sp887x_writereg(fe, 0x328, 0x000);
173 sp887x_writereg(fe, 0x327, 0x005);
174 sp887x_writereg(fe, 0x326, 0x001);
175 sp887x_writereg(fe, 0x325, 0x001);
176 sp887x_writereg(fe, 0x324, 0x001);
177 sp887x_writereg(fe, 0x318, 0x050);
178 sp887x_writereg(fe, 0x317, 0x3fe);
179 sp887x_writereg(fe, 0x316, 0x001);
180 sp887x_writereg(fe, 0x313, 0x005);
181 sp887x_writereg(fe, 0x312, 0x002);
182 sp887x_writereg(fe, 0x306, 0x000);
183 sp887x_writereg(fe, 0x303, 0x000);
190 * load firmware and setup MPEG interface...
193 int sp887x_initial_setup (struct dvb_frontend *fe)
195 u8 buf [BLOCKSIZE+2];
196 unsigned char *firmware = NULL;
203 dprintk("%s\n", __FUNCTION__);
206 sp887x_writereg(fe, 0xf1a, 0x000);
208 sp887x_microcontroller_stop (fe);
214 fd = open(sp887x_firmware, 0, 0);
216 printk(KERN_WARNING "%s: Unable to open firmware %s\n", __FUNCTION__,
220 filesize = lseek(fd, 0L, 2);
222 printk(KERN_WARNING "%s: Firmware %s is empty\n", __FUNCTION__,
230 // allocate buffer for it
231 firmware = vmalloc(fw_size);
232 if (firmware == NULL) {
233 printk(KERN_WARNING "%s: Out of memory loading firmware\n",
240 // read the first 16384 bytes from the file
241 // ignore the first 10 bytes
243 if (read(fd, firmware, fw_size) != fw_size) {
244 printk(KERN_WARNING "%s: Failed to read firmware\n", __FUNCTION__);
252 printk ("%s: firmware upload... ", __FUNCTION__);
254 /* setup write pointer to -1 (end of memory) */
255 /* bit 0x8000 in address is set to enable 13bit mode */
256 sp887x_writereg(fe, 0x8f08, 0x1fff);
258 /* dummy write (wrap around to start of memory) */
259 sp887x_writereg(fe, 0x8f0a, 0x0000);
261 for (i=0; i<fw_size; i+=BLOCKSIZE) {
268 /* bit 0x8000 in address is set to enable 13bit mode */
269 /* bit 0x4000 enables multibyte read/write transfers */
270 /* write register is 0xf0a */
274 memcpy(&buf[2], firmware + i, c);
276 if ((err = i2c_writebytes (fe, 0x70, buf, c+2)) < 0) {
277 printk ("failed.\n");
278 printk ("%s: i2c error (err == %i)\n", __FUNCTION__, err);
286 /* don't write RS bytes between packets */
287 sp887x_writereg(fe, 0xc13, 0x001);
289 /* suppress clock if (!data_valid) */
290 sp887x_writereg(fe, 0xc14, 0x000);
292 /* setup MPEG interface... */
293 sp887x_writereg(fe, 0xc1a, 0x872);
294 sp887x_writereg(fe, 0xc1b, 0x001);
295 sp887x_writereg(fe, 0xc1c, 0x000); /* parallel mode (serial mode == 1) */
296 sp887x_writereg(fe, 0xc1a, 0x871);
298 /* ADC mode, 2 for MT8872, 3 for SP8870/SP8871 */
299 sp887x_writereg(fe, 0x301, 0x002);
301 sp887x_setup_agc(fe);
303 /* bit 0x010: enable data valid signal */
304 sp887x_writereg(fe, 0xd00, 0x010);
305 sp887x_writereg(fe, 0x0d1, 0x000);
313 * returns the actual tuned center frequency which can be used
314 * to initialise the AFC registers
317 int tsa5060_setup_pll (struct dvb_frontend *fe, int freq)
319 u8 cfg, cpump, band_select;
323 div = (36000000 + freq + 83333) / 166666;
326 cpump = freq < 175000000 ? 2 : freq < 390000000 ? 1 :
327 freq < 470000000 ? 2 : freq < 750000000 ? 2 : 3;
329 band_select = freq < 175000000 ? 0x0e : freq < 470000000 ? 0x05 : 0x03;
331 buf [0] = (div >> 8) & 0x7f;
332 buf [1] = div & 0xff;
333 buf [2] = ((div >> 10) & 0x60) | cfg;
334 buf [3] = cpump | band_select;
336 /* open i2c gate for PLL message transmission... */
337 sp887x_writereg(fe, 0x206, 0x001);
338 i2c_writebytes(fe, 0x60, buf, 4);
339 sp887x_writereg(fe, 0x206, 0x000);
341 return (div * 166666 - 36000000);
347 int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05)
349 int known_parameters = 1;
353 switch (p->u.ofdm.constellation) {
357 *reg0xc05 |= (1 << 10);
360 *reg0xc05 |= (2 << 10);
363 known_parameters = 0;
369 switch (p->u.ofdm.hierarchy_information) {
373 *reg0xc05 |= (1 << 7);
376 *reg0xc05 |= (2 << 7);
379 *reg0xc05 |= (3 << 7);
382 known_parameters = 0;
388 switch (p->u.ofdm.code_rate_HP) {
392 *reg0xc05 |= (1 << 3);
395 *reg0xc05 |= (2 << 3);
398 *reg0xc05 |= (3 << 3);
401 *reg0xc05 |= (4 << 3);
404 known_parameters = 0;
410 if (known_parameters)
411 *reg0xc05 |= (2 << 1); /* use specified parameters */
413 *reg0xc05 |= (1 << 1); /* enable autoprobing */
420 * estimates division of two 24bit numbers,
421 * derived from the ves1820/stv0299 driver code
424 void divide (int n, int d, int *quotient_i, int *quotient_f)
436 q = (q << 8) | (r / d);
438 *quotient_f = (q << 8) | (r / d);
444 void sp887x_correct_offsets (struct dvb_frontend *fe,
445 struct dvb_frontend_parameters *p,
448 static const u32 srate_correction [] = { 1879617, 4544878, 8098561 };
449 int bw_index = p->u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
450 int freq_offset = actual_freq - p->frequency;
451 int sysclock = 61003; //[kHz]
452 int ifreq = 36000000;
456 if (p->inversion == INVERSION_ON)
457 freq = ifreq - freq_offset;
459 freq = ifreq + freq_offset;
461 divide(freq / 333, sysclock, NULL, &frequency_shift);
463 if (p->inversion == INVERSION_ON)
464 frequency_shift = -frequency_shift;
466 /* sample rate correction */
467 sp887x_writereg(fe, 0x319, srate_correction[bw_index] >> 12);
468 sp887x_writereg(fe, 0x31a, srate_correction[bw_index] & 0xfff);
470 /* carrier offset correction */
471 sp887x_writereg(fe, 0x309, frequency_shift >> 12);
472 sp887x_writereg(fe, 0x30a, frequency_shift & 0xfff);
477 int sp887x_setup_frontend_parameters (struct dvb_frontend *fe,
478 struct dvb_frontend_parameters *p)
480 int actual_freq, err;
483 if (p->u.ofdm.bandwidth != BANDWIDTH_8_MHZ &&
484 p->u.ofdm.bandwidth != BANDWIDTH_7_MHZ &&
485 p->u.ofdm.bandwidth != BANDWIDTH_6_MHZ)
488 if ((err = configure_reg0xc05(p, ®0xc05)))
491 sp887x_microcontroller_stop(fe);
493 actual_freq = tsa5060_setup_pll(fe, p->frequency);
495 /* read status reg in order to clear pending irqs */
496 sp887x_readreg(fe, 0x200);
498 sp887x_correct_offsets(fe, p, actual_freq);
500 /* filter for 6/7/8 Mhz channel */
501 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
503 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
508 sp887x_writereg(fe, 0x311, val);
510 /* scan order: 2k first = 0, 8k first = 1 */
511 if (p->u.ofdm.transmission_mode == TRANSMISSION_MODE_2K)
512 sp887x_writereg(fe, 0x338, 0x000);
514 sp887x_writereg(fe, 0x338, 0x001);
516 sp887x_writereg(fe, 0xc05, reg0xc05);
518 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
520 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
525 /* enable OFDM and SAW bits as lock indicators in sync register 0xf17,
526 * optimize algorithm for given bandwidth...
528 sp887x_writereg(fe, 0xf14, 0x160 | val);
529 sp887x_writereg(fe, 0xf15, 0x000);
531 sp887x_microcontroller_start(fe);
537 int sp887x_ioctl (struct dvb_frontend *fe, unsigned int cmd, void *arg)
541 memcpy (arg, &sp887x_info, sizeof(struct dvb_frontend_info));
546 u16 snr12 = sp887x_readreg(fe, 0xf16);
547 u16 sync0x200 = sp887x_readreg(fe, 0x200);
548 u16 sync0xf17 = sp887x_readreg(fe, 0xf17);
549 fe_status_t *status = arg;
554 *status |= FE_HAS_SIGNAL;
556 //if (sync0x200 & 0x004)
557 // *status |= FE_HAS_SYNC | FE_HAS_CARRIER;
559 //if (sync0x200 & 0x008)
560 // *status |= FE_HAS_VITERBI;
562 if ((sync0xf17 & 0x00f) == 0x002) {
563 *status |= FE_HAS_LOCK;
564 *status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_CARRIER;
567 if (sync0x200 & 0x001) { /* tuner adjustment requested...*/
568 int steps = (sync0x200 >> 4) & 0x00f;
571 dprintk("sp887x: implement tuner adjustment (%+i steps)!!\n",
582 *ber = (sp887x_readreg(fe, 0xc08) & 0x3f) |
583 (sp887x_readreg(fe, 0xc07) << 6);
584 sp887x_writereg(fe, 0xc08, 0x000);
585 sp887x_writereg(fe, 0xc07, 0x000);
592 case FE_READ_SIGNAL_STRENGTH: // FIXME: correct registers ?
594 u16 snr12 = sp887x_readreg(fe, 0xf16);
595 u32 signal = 3 * (snr12 << 4);
596 *((u16*) arg) = (signal < 0xffff) ? signal : 0xffff;
602 u16 snr12 = sp887x_readreg(fe, 0xf16);
603 *(u16*) arg = (snr12 << 4) | (snr12 >> 8);
607 case FE_READ_UNCORRECTED_BLOCKS:
609 u32 *ublocks = (u32 *) arg;
610 *ublocks = sp887x_readreg(fe, 0xc0c);
611 if (*ublocks == 0xfff)
616 case FE_SET_FRONTEND:
617 return sp887x_setup_frontend_parameters(fe, arg);
619 case FE_GET_FRONTEND: // FIXME: read known values back from Hardware...
623 /* tristate TS output and disable interface pins */
624 sp887x_writereg(fe, 0xc18, 0x000);
628 if (fe->data == NULL) { /* first time initialisation... */
629 fe->data = (void*) ~0;
630 sp887x_initial_setup (fe);
632 /* enable TS output and interface pins */
633 sp887x_writereg(fe, 0xc18, 0x00d);
636 case FE_GET_TUNE_SETTINGS:
638 struct dvb_frontend_tune_settings* fesettings = (struct dvb_frontend_tune_settings*) arg;
639 fesettings->min_delay_ms = 50;
640 fesettings->step_size = 0;
641 fesettings->max_drift = 0;
655 int sp887x_attach (struct dvb_i2c_bus *i2c, void **data)
657 struct i2c_msg msg = {.addr = 0x70, .flags = 0, .buf = NULL, .len = 0 };
659 dprintk ("%s\n", __FUNCTION__);
661 if (i2c->xfer (i2c, &msg, 1) != 1)
664 return dvb_register_frontend (sp887x_ioctl, i2c, NULL, &sp887x_info);
669 void sp887x_detach (struct dvb_i2c_bus *i2c, void *data)
671 dprintk ("%s\n", __FUNCTION__);
672 dvb_unregister_frontend (sp887x_ioctl, i2c);
677 int __init init_sp887x (void)
679 dprintk ("%s\n", __FUNCTION__);
680 return dvb_register_i2c_device (NULL, sp887x_attach, sp887x_detach);
685 void __exit exit_sp887x (void)
687 dprintk ("%s\n", __FUNCTION__);
688 dvb_unregister_i2c_device (sp887x_attach);
692 module_init(init_sp887x);
693 module_exit(exit_sp887x);
696 MODULE_DESCRIPTION("sp887x DVB-T demodulator driver");
697 MODULE_LICENSE("GPL");