19a0bf528SMauro Carvalho Chehab /* 29a0bf528SMauro Carvalho Chehab Driver for the Spase sp887x demodulator 39a0bf528SMauro Carvalho Chehab */ 49a0bf528SMauro Carvalho Chehab 59a0bf528SMauro Carvalho Chehab /* 69a0bf528SMauro Carvalho Chehab * This driver needs external firmware. Please use the command 7*fe63a1a6SMauro Carvalho Chehab * "<kerneldir>/scripts/get_dvb_firmware sp887x" to 89a0bf528SMauro Carvalho Chehab * download/extract it, and then copy it to /usr/lib/hotplug/firmware 99a0bf528SMauro Carvalho Chehab * or /lib/firmware (depending on configuration of firmware hotplug). 109a0bf528SMauro Carvalho Chehab */ 119a0bf528SMauro Carvalho Chehab #define SP887X_DEFAULT_FIRMWARE "dvb-fe-sp887x.fw" 129a0bf528SMauro Carvalho Chehab 139a0bf528SMauro Carvalho Chehab #include <linux/init.h> 149a0bf528SMauro Carvalho Chehab #include <linux/module.h> 159a0bf528SMauro Carvalho Chehab #include <linux/device.h> 169a0bf528SMauro Carvalho Chehab #include <linux/firmware.h> 179a0bf528SMauro Carvalho Chehab #include <linux/string.h> 189a0bf528SMauro Carvalho Chehab #include <linux/slab.h> 199a0bf528SMauro Carvalho Chehab 20fada1935SMauro Carvalho Chehab #include <media/dvb_frontend.h> 219a0bf528SMauro Carvalho Chehab #include "sp887x.h" 229a0bf528SMauro Carvalho Chehab 239a0bf528SMauro Carvalho Chehab 249a0bf528SMauro Carvalho Chehab struct sp887x_state { 259a0bf528SMauro Carvalho Chehab struct i2c_adapter* i2c; 269a0bf528SMauro Carvalho Chehab const struct sp887x_config* config; 279a0bf528SMauro Carvalho Chehab struct dvb_frontend frontend; 289a0bf528SMauro Carvalho Chehab 299a0bf528SMauro Carvalho Chehab /* demodulator private data */ 309a0bf528SMauro Carvalho Chehab u8 initialised:1; 319a0bf528SMauro Carvalho Chehab }; 329a0bf528SMauro Carvalho Chehab 339a0bf528SMauro Carvalho Chehab static int debug; 349a0bf528SMauro Carvalho Chehab #define dprintk(args...) \ 359a0bf528SMauro Carvalho Chehab do { \ 369a0bf528SMauro Carvalho Chehab if (debug) printk(KERN_DEBUG "sp887x: " args); \ 379a0bf528SMauro Carvalho Chehab } while (0) 389a0bf528SMauro Carvalho Chehab 399a0bf528SMauro Carvalho Chehab static int i2c_writebytes (struct sp887x_state* state, u8 *buf, u8 len) 409a0bf528SMauro Carvalho Chehab { 419a0bf528SMauro Carvalho Chehab struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = len }; 429a0bf528SMauro Carvalho Chehab int err; 439a0bf528SMauro Carvalho Chehab 449a0bf528SMauro Carvalho Chehab if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) { 459a0bf528SMauro Carvalho Chehab printk ("%s: i2c write error (addr %02x, err == %i)\n", 469a0bf528SMauro Carvalho Chehab __func__, state->config->demod_address, err); 479a0bf528SMauro Carvalho Chehab return -EREMOTEIO; 489a0bf528SMauro Carvalho Chehab } 499a0bf528SMauro Carvalho Chehab 509a0bf528SMauro Carvalho Chehab return 0; 519a0bf528SMauro Carvalho Chehab } 529a0bf528SMauro Carvalho Chehab 539a0bf528SMauro Carvalho Chehab static int sp887x_writereg (struct sp887x_state* state, u16 reg, u16 data) 549a0bf528SMauro Carvalho Chehab { 559a0bf528SMauro Carvalho Chehab u8 b0 [] = { reg >> 8 , reg & 0xff, data >> 8, data & 0xff }; 569a0bf528SMauro Carvalho Chehab struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 4 }; 579a0bf528SMauro Carvalho Chehab int ret; 589a0bf528SMauro Carvalho Chehab 599a0bf528SMauro Carvalho Chehab if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1) { 60cba862dcSMauro Carvalho Chehab /* 619a0bf528SMauro Carvalho Chehab * in case of soft reset we ignore ACK errors... 629a0bf528SMauro Carvalho Chehab */ 639a0bf528SMauro Carvalho Chehab if (!(reg == 0xf1a && data == 0x000 && 649a0bf528SMauro Carvalho Chehab (ret == -EREMOTEIO || ret == -EFAULT))) 659a0bf528SMauro Carvalho Chehab { 664bd69e7bSMauro Carvalho Chehab printk("%s: writereg error (reg %03x, data %03x, ret == %i)\n", 679a0bf528SMauro Carvalho Chehab __func__, reg & 0xffff, data & 0xffff, ret); 689a0bf528SMauro Carvalho Chehab return ret; 699a0bf528SMauro Carvalho Chehab } 709a0bf528SMauro Carvalho Chehab } 719a0bf528SMauro Carvalho Chehab 729a0bf528SMauro Carvalho Chehab return 0; 739a0bf528SMauro Carvalho Chehab } 749a0bf528SMauro Carvalho Chehab 759a0bf528SMauro Carvalho Chehab static int sp887x_readreg (struct sp887x_state* state, u16 reg) 769a0bf528SMauro Carvalho Chehab { 779a0bf528SMauro Carvalho Chehab u8 b0 [] = { reg >> 8 , reg & 0xff }; 789a0bf528SMauro Carvalho Chehab u8 b1 [2]; 799a0bf528SMauro Carvalho Chehab int ret; 809a0bf528SMauro Carvalho Chehab struct i2c_msg msg[] = {{ .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 2 }, 819a0bf528SMauro Carvalho Chehab { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 2 }}; 829a0bf528SMauro Carvalho Chehab 839a0bf528SMauro Carvalho Chehab if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) { 849a0bf528SMauro Carvalho Chehab printk("%s: readreg error (ret == %i)\n", __func__, ret); 859a0bf528SMauro Carvalho Chehab return -1; 869a0bf528SMauro Carvalho Chehab } 879a0bf528SMauro Carvalho Chehab 889a0bf528SMauro Carvalho Chehab return (((b1[0] << 8) | b1[1]) & 0xfff); 899a0bf528SMauro Carvalho Chehab } 909a0bf528SMauro Carvalho Chehab 919a0bf528SMauro Carvalho Chehab static void sp887x_microcontroller_stop (struct sp887x_state* state) 929a0bf528SMauro Carvalho Chehab { 939a0bf528SMauro Carvalho Chehab dprintk("%s\n", __func__); 949a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xf08, 0x000); 959a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xf09, 0x000); 969a0bf528SMauro Carvalho Chehab 979a0bf528SMauro Carvalho Chehab /* microcontroller STOP */ 989a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xf00, 0x000); 999a0bf528SMauro Carvalho Chehab } 1009a0bf528SMauro Carvalho Chehab 1019a0bf528SMauro Carvalho Chehab static void sp887x_microcontroller_start (struct sp887x_state* state) 1029a0bf528SMauro Carvalho Chehab { 1039a0bf528SMauro Carvalho Chehab dprintk("%s\n", __func__); 1049a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xf08, 0x000); 1059a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xf09, 0x000); 1069a0bf528SMauro Carvalho Chehab 1079a0bf528SMauro Carvalho Chehab /* microcontroller START */ 1089a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xf00, 0x001); 1099a0bf528SMauro Carvalho Chehab } 1109a0bf528SMauro Carvalho Chehab 1119a0bf528SMauro Carvalho Chehab static void sp887x_setup_agc (struct sp887x_state* state) 1129a0bf528SMauro Carvalho Chehab { 1139a0bf528SMauro Carvalho Chehab /* setup AGC parameters */ 1149a0bf528SMauro Carvalho Chehab dprintk("%s\n", __func__); 1159a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x33c, 0x054); 1169a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x33b, 0x04c); 1179a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x328, 0x000); 1189a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x327, 0x005); 1199a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x326, 0x001); 1209a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x325, 0x001); 1219a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x324, 0x001); 1229a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x318, 0x050); 1239a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x317, 0x3fe); 1249a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x316, 0x001); 1259a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x313, 0x005); 1269a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x312, 0x002); 1279a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x306, 0x000); 1289a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x303, 0x000); 1299a0bf528SMauro Carvalho Chehab } 1309a0bf528SMauro Carvalho Chehab 1319a0bf528SMauro Carvalho Chehab #define BLOCKSIZE 30 1329a0bf528SMauro Carvalho Chehab #define FW_SIZE 0x4000 133cba862dcSMauro Carvalho Chehab /* 1349a0bf528SMauro Carvalho Chehab * load firmware and setup MPEG interface... 1359a0bf528SMauro Carvalho Chehab */ 1369a0bf528SMauro Carvalho Chehab static int sp887x_initial_setup (struct dvb_frontend* fe, const struct firmware *fw) 1379a0bf528SMauro Carvalho Chehab { 1389a0bf528SMauro Carvalho Chehab struct sp887x_state* state = fe->demodulator_priv; 1399a0bf528SMauro Carvalho Chehab u8 buf [BLOCKSIZE + 2]; 1409a0bf528SMauro Carvalho Chehab int i; 1419a0bf528SMauro Carvalho Chehab int fw_size = fw->size; 1429a0bf528SMauro Carvalho Chehab const unsigned char *mem = fw->data; 1439a0bf528SMauro Carvalho Chehab 1449a0bf528SMauro Carvalho Chehab dprintk("%s\n", __func__); 1459a0bf528SMauro Carvalho Chehab 1469a0bf528SMauro Carvalho Chehab /* ignore the first 10 bytes, then we expect 0x4000 bytes of firmware */ 1479a0bf528SMauro Carvalho Chehab if (fw_size < FW_SIZE + 10) 1489a0bf528SMauro Carvalho Chehab return -ENODEV; 1499a0bf528SMauro Carvalho Chehab 1509a0bf528SMauro Carvalho Chehab mem = fw->data + 10; 1519a0bf528SMauro Carvalho Chehab 1529a0bf528SMauro Carvalho Chehab /* soft reset */ 1539a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xf1a, 0x000); 1549a0bf528SMauro Carvalho Chehab 1559a0bf528SMauro Carvalho Chehab sp887x_microcontroller_stop (state); 1569a0bf528SMauro Carvalho Chehab 1579a0bf528SMauro Carvalho Chehab printk ("%s: firmware upload... ", __func__); 1589a0bf528SMauro Carvalho Chehab 1599a0bf528SMauro Carvalho Chehab /* setup write pointer to -1 (end of memory) */ 1609a0bf528SMauro Carvalho Chehab /* bit 0x8000 in address is set to enable 13bit mode */ 1619a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x8f08, 0x1fff); 1629a0bf528SMauro Carvalho Chehab 1639a0bf528SMauro Carvalho Chehab /* dummy write (wrap around to start of memory) */ 1649a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x8f0a, 0x0000); 1659a0bf528SMauro Carvalho Chehab 1669a0bf528SMauro Carvalho Chehab for (i = 0; i < FW_SIZE; i += BLOCKSIZE) { 1679a0bf528SMauro Carvalho Chehab int c = BLOCKSIZE; 1689a0bf528SMauro Carvalho Chehab int err; 1699a0bf528SMauro Carvalho Chehab 17089d6e45cSMauro Carvalho Chehab if (c > FW_SIZE - i) 1719a0bf528SMauro Carvalho Chehab c = FW_SIZE - i; 1729a0bf528SMauro Carvalho Chehab 1739a0bf528SMauro Carvalho Chehab /* bit 0x8000 in address is set to enable 13bit mode */ 1749a0bf528SMauro Carvalho Chehab /* bit 0x4000 enables multibyte read/write transfers */ 1759a0bf528SMauro Carvalho Chehab /* write register is 0xf0a */ 1769a0bf528SMauro Carvalho Chehab buf[0] = 0xcf; 1779a0bf528SMauro Carvalho Chehab buf[1] = 0x0a; 1789a0bf528SMauro Carvalho Chehab 1799a0bf528SMauro Carvalho Chehab memcpy(&buf[2], mem + i, c); 1809a0bf528SMauro Carvalho Chehab 1819a0bf528SMauro Carvalho Chehab if ((err = i2c_writebytes (state, buf, c+2)) < 0) { 1829a0bf528SMauro Carvalho Chehab printk ("failed.\n"); 1839a0bf528SMauro Carvalho Chehab printk ("%s: i2c error (err == %i)\n", __func__, err); 1849a0bf528SMauro Carvalho Chehab return err; 1859a0bf528SMauro Carvalho Chehab } 1869a0bf528SMauro Carvalho Chehab } 1879a0bf528SMauro Carvalho Chehab 1889a0bf528SMauro Carvalho Chehab /* don't write RS bytes between packets */ 1899a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xc13, 0x001); 1909a0bf528SMauro Carvalho Chehab 1919a0bf528SMauro Carvalho Chehab /* suppress clock if (!data_valid) */ 1929a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xc14, 0x000); 1939a0bf528SMauro Carvalho Chehab 1949a0bf528SMauro Carvalho Chehab /* setup MPEG interface... */ 1959a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xc1a, 0x872); 1969a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xc1b, 0x001); 1979a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xc1c, 0x000); /* parallel mode (serial mode == 1) */ 1989a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xc1a, 0x871); 1999a0bf528SMauro Carvalho Chehab 2009a0bf528SMauro Carvalho Chehab /* ADC mode, 2 for MT8872, 3 for SP8870/SP8871 */ 2019a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x301, 0x002); 2029a0bf528SMauro Carvalho Chehab 2039a0bf528SMauro Carvalho Chehab sp887x_setup_agc(state); 2049a0bf528SMauro Carvalho Chehab 2059a0bf528SMauro Carvalho Chehab /* bit 0x010: enable data valid signal */ 2069a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xd00, 0x010); 2079a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x0d1, 0x000); 2089a0bf528SMauro Carvalho Chehab return 0; 2099a0bf528SMauro Carvalho Chehab }; 2109a0bf528SMauro Carvalho Chehab 2119a0bf528SMauro Carvalho Chehab static int configure_reg0xc05(struct dtv_frontend_properties *p, u16 *reg0xc05) 2129a0bf528SMauro Carvalho Chehab { 2139a0bf528SMauro Carvalho Chehab int known_parameters = 1; 2149a0bf528SMauro Carvalho Chehab 2159a0bf528SMauro Carvalho Chehab *reg0xc05 = 0x000; 2169a0bf528SMauro Carvalho Chehab 2179a0bf528SMauro Carvalho Chehab switch (p->modulation) { 2189a0bf528SMauro Carvalho Chehab case QPSK: 2199a0bf528SMauro Carvalho Chehab break; 2209a0bf528SMauro Carvalho Chehab case QAM_16: 2219a0bf528SMauro Carvalho Chehab *reg0xc05 |= (1 << 10); 2229a0bf528SMauro Carvalho Chehab break; 2239a0bf528SMauro Carvalho Chehab case QAM_64: 2249a0bf528SMauro Carvalho Chehab *reg0xc05 |= (2 << 10); 2259a0bf528SMauro Carvalho Chehab break; 2269a0bf528SMauro Carvalho Chehab case QAM_AUTO: 2279a0bf528SMauro Carvalho Chehab known_parameters = 0; 2289a0bf528SMauro Carvalho Chehab break; 2299a0bf528SMauro Carvalho Chehab default: 2309a0bf528SMauro Carvalho Chehab return -EINVAL; 231c2c1b415SPeter Senna Tschudin } 2329a0bf528SMauro Carvalho Chehab 2339a0bf528SMauro Carvalho Chehab switch (p->hierarchy) { 2349a0bf528SMauro Carvalho Chehab case HIERARCHY_NONE: 2359a0bf528SMauro Carvalho Chehab break; 2369a0bf528SMauro Carvalho Chehab case HIERARCHY_1: 2379a0bf528SMauro Carvalho Chehab *reg0xc05 |= (1 << 7); 2389a0bf528SMauro Carvalho Chehab break; 2399a0bf528SMauro Carvalho Chehab case HIERARCHY_2: 2409a0bf528SMauro Carvalho Chehab *reg0xc05 |= (2 << 7); 2419a0bf528SMauro Carvalho Chehab break; 2429a0bf528SMauro Carvalho Chehab case HIERARCHY_4: 2439a0bf528SMauro Carvalho Chehab *reg0xc05 |= (3 << 7); 2449a0bf528SMauro Carvalho Chehab break; 2459a0bf528SMauro Carvalho Chehab case HIERARCHY_AUTO: 2469a0bf528SMauro Carvalho Chehab known_parameters = 0; 2479a0bf528SMauro Carvalho Chehab break; 2489a0bf528SMauro Carvalho Chehab default: 2499a0bf528SMauro Carvalho Chehab return -EINVAL; 250c2c1b415SPeter Senna Tschudin } 2519a0bf528SMauro Carvalho Chehab 2529a0bf528SMauro Carvalho Chehab switch (p->code_rate_HP) { 2539a0bf528SMauro Carvalho Chehab case FEC_1_2: 2549a0bf528SMauro Carvalho Chehab break; 2559a0bf528SMauro Carvalho Chehab case FEC_2_3: 2569a0bf528SMauro Carvalho Chehab *reg0xc05 |= (1 << 3); 2579a0bf528SMauro Carvalho Chehab break; 2589a0bf528SMauro Carvalho Chehab case FEC_3_4: 2599a0bf528SMauro Carvalho Chehab *reg0xc05 |= (2 << 3); 2609a0bf528SMauro Carvalho Chehab break; 2619a0bf528SMauro Carvalho Chehab case FEC_5_6: 2629a0bf528SMauro Carvalho Chehab *reg0xc05 |= (3 << 3); 2639a0bf528SMauro Carvalho Chehab break; 2649a0bf528SMauro Carvalho Chehab case FEC_7_8: 2659a0bf528SMauro Carvalho Chehab *reg0xc05 |= (4 << 3); 2669a0bf528SMauro Carvalho Chehab break; 2679a0bf528SMauro Carvalho Chehab case FEC_AUTO: 2689a0bf528SMauro Carvalho Chehab known_parameters = 0; 2699a0bf528SMauro Carvalho Chehab break; 2709a0bf528SMauro Carvalho Chehab default: 2719a0bf528SMauro Carvalho Chehab return -EINVAL; 272c2c1b415SPeter Senna Tschudin } 2739a0bf528SMauro Carvalho Chehab 2749a0bf528SMauro Carvalho Chehab if (known_parameters) 2759a0bf528SMauro Carvalho Chehab *reg0xc05 |= (2 << 1); /* use specified parameters */ 2769a0bf528SMauro Carvalho Chehab else 2779a0bf528SMauro Carvalho Chehab *reg0xc05 |= (1 << 1); /* enable autoprobing */ 2789a0bf528SMauro Carvalho Chehab 2799a0bf528SMauro Carvalho Chehab return 0; 2809a0bf528SMauro Carvalho Chehab } 2819a0bf528SMauro Carvalho Chehab 282cba862dcSMauro Carvalho Chehab /* 2839a0bf528SMauro Carvalho Chehab * estimates division of two 24bit numbers, 2849a0bf528SMauro Carvalho Chehab * derived from the ves1820/stv0299 driver code 2859a0bf528SMauro Carvalho Chehab */ 2869a0bf528SMauro Carvalho Chehab static void divide (int n, int d, int *quotient_i, int *quotient_f) 2879a0bf528SMauro Carvalho Chehab { 2889a0bf528SMauro Carvalho Chehab unsigned int q, r; 2899a0bf528SMauro Carvalho Chehab 2909a0bf528SMauro Carvalho Chehab r = (n % d) << 8; 2919a0bf528SMauro Carvalho Chehab q = (r / d); 2929a0bf528SMauro Carvalho Chehab 2939a0bf528SMauro Carvalho Chehab if (quotient_i) 2949a0bf528SMauro Carvalho Chehab *quotient_i = q; 2959a0bf528SMauro Carvalho Chehab 2969a0bf528SMauro Carvalho Chehab if (quotient_f) { 2979a0bf528SMauro Carvalho Chehab r = (r % d) << 8; 2989a0bf528SMauro Carvalho Chehab q = (q << 8) | (r / d); 2999a0bf528SMauro Carvalho Chehab r = (r % d) << 8; 3009a0bf528SMauro Carvalho Chehab *quotient_f = (q << 8) | (r / d); 3019a0bf528SMauro Carvalho Chehab } 3029a0bf528SMauro Carvalho Chehab } 3039a0bf528SMauro Carvalho Chehab 3049a0bf528SMauro Carvalho Chehab static void sp887x_correct_offsets (struct sp887x_state* state, 3059a0bf528SMauro Carvalho Chehab struct dtv_frontend_properties *p, 3069a0bf528SMauro Carvalho Chehab int actual_freq) 3079a0bf528SMauro Carvalho Chehab { 3089a0bf528SMauro Carvalho Chehab static const u32 srate_correction [] = { 1879617, 4544878, 8098561 }; 3099a0bf528SMauro Carvalho Chehab int bw_index; 3109a0bf528SMauro Carvalho Chehab int freq_offset = actual_freq - p->frequency; 3119a0bf528SMauro Carvalho Chehab int sysclock = 61003; //[kHz] 3129a0bf528SMauro Carvalho Chehab int ifreq = 36000000; 3139a0bf528SMauro Carvalho Chehab int freq; 3149a0bf528SMauro Carvalho Chehab int frequency_shift; 3159a0bf528SMauro Carvalho Chehab 3169a0bf528SMauro Carvalho Chehab switch (p->bandwidth_hz) { 3179a0bf528SMauro Carvalho Chehab default: 3189a0bf528SMauro Carvalho Chehab case 8000000: 3199a0bf528SMauro Carvalho Chehab bw_index = 0; 3209a0bf528SMauro Carvalho Chehab break; 3219a0bf528SMauro Carvalho Chehab case 7000000: 3229a0bf528SMauro Carvalho Chehab bw_index = 1; 3239a0bf528SMauro Carvalho Chehab break; 3249a0bf528SMauro Carvalho Chehab case 6000000: 3259a0bf528SMauro Carvalho Chehab bw_index = 2; 3269a0bf528SMauro Carvalho Chehab break; 3279a0bf528SMauro Carvalho Chehab } 3289a0bf528SMauro Carvalho Chehab 3299a0bf528SMauro Carvalho Chehab if (p->inversion == INVERSION_ON) 3309a0bf528SMauro Carvalho Chehab freq = ifreq - freq_offset; 3319a0bf528SMauro Carvalho Chehab else 3329a0bf528SMauro Carvalho Chehab freq = ifreq + freq_offset; 3339a0bf528SMauro Carvalho Chehab 3349a0bf528SMauro Carvalho Chehab divide(freq / 333, sysclock, NULL, &frequency_shift); 3359a0bf528SMauro Carvalho Chehab 3369a0bf528SMauro Carvalho Chehab if (p->inversion == INVERSION_ON) 3379a0bf528SMauro Carvalho Chehab frequency_shift = -frequency_shift; 3389a0bf528SMauro Carvalho Chehab 3399a0bf528SMauro Carvalho Chehab /* sample rate correction */ 3409a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x319, srate_correction[bw_index] >> 12); 3419a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x31a, srate_correction[bw_index] & 0xfff); 3429a0bf528SMauro Carvalho Chehab 3439a0bf528SMauro Carvalho Chehab /* carrier offset correction */ 3449a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x309, frequency_shift >> 12); 3459a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x30a, frequency_shift & 0xfff); 3469a0bf528SMauro Carvalho Chehab } 3479a0bf528SMauro Carvalho Chehab 3489a0bf528SMauro Carvalho Chehab static int sp887x_setup_frontend_parameters(struct dvb_frontend *fe) 3499a0bf528SMauro Carvalho Chehab { 3509a0bf528SMauro Carvalho Chehab struct dtv_frontend_properties *p = &fe->dtv_property_cache; 3519a0bf528SMauro Carvalho Chehab struct sp887x_state* state = fe->demodulator_priv; 3529a0bf528SMauro Carvalho Chehab unsigned actual_freq; 3539a0bf528SMauro Carvalho Chehab int err; 3549a0bf528SMauro Carvalho Chehab u16 val, reg0xc05; 3559a0bf528SMauro Carvalho Chehab 3569a0bf528SMauro Carvalho Chehab if (p->bandwidth_hz != 8000000 && 3579a0bf528SMauro Carvalho Chehab p->bandwidth_hz != 7000000 && 3589a0bf528SMauro Carvalho Chehab p->bandwidth_hz != 6000000) 3599a0bf528SMauro Carvalho Chehab return -EINVAL; 3609a0bf528SMauro Carvalho Chehab 3619a0bf528SMauro Carvalho Chehab if ((err = configure_reg0xc05(p, ®0xc05))) 3629a0bf528SMauro Carvalho Chehab return err; 3639a0bf528SMauro Carvalho Chehab 3649a0bf528SMauro Carvalho Chehab sp887x_microcontroller_stop(state); 3659a0bf528SMauro Carvalho Chehab 3669a0bf528SMauro Carvalho Chehab /* setup the PLL */ 3679a0bf528SMauro Carvalho Chehab if (fe->ops.tuner_ops.set_params) { 3689a0bf528SMauro Carvalho Chehab fe->ops.tuner_ops.set_params(fe); 3699a0bf528SMauro Carvalho Chehab if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0); 3709a0bf528SMauro Carvalho Chehab } 3719a0bf528SMauro Carvalho Chehab if (fe->ops.tuner_ops.get_frequency) { 3729a0bf528SMauro Carvalho Chehab fe->ops.tuner_ops.get_frequency(fe, &actual_freq); 3739a0bf528SMauro Carvalho Chehab if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0); 3749a0bf528SMauro Carvalho Chehab } else { 3759a0bf528SMauro Carvalho Chehab actual_freq = p->frequency; 3769a0bf528SMauro Carvalho Chehab } 3779a0bf528SMauro Carvalho Chehab 3789a0bf528SMauro Carvalho Chehab /* read status reg in order to clear <pending irqs */ 3799a0bf528SMauro Carvalho Chehab sp887x_readreg(state, 0x200); 3809a0bf528SMauro Carvalho Chehab 3819a0bf528SMauro Carvalho Chehab sp887x_correct_offsets(state, p, actual_freq); 3829a0bf528SMauro Carvalho Chehab 3839a0bf528SMauro Carvalho Chehab /* filter for 6/7/8 Mhz channel */ 3849a0bf528SMauro Carvalho Chehab if (p->bandwidth_hz == 6000000) 3859a0bf528SMauro Carvalho Chehab val = 2; 3869a0bf528SMauro Carvalho Chehab else if (p->bandwidth_hz == 7000000) 3879a0bf528SMauro Carvalho Chehab val = 1; 3889a0bf528SMauro Carvalho Chehab else 3899a0bf528SMauro Carvalho Chehab val = 0; 3909a0bf528SMauro Carvalho Chehab 3919a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x311, val); 3929a0bf528SMauro Carvalho Chehab 3939a0bf528SMauro Carvalho Chehab /* scan order: 2k first = 0, 8k first = 1 */ 3949a0bf528SMauro Carvalho Chehab if (p->transmission_mode == TRANSMISSION_MODE_2K) 3959a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x338, 0x000); 3969a0bf528SMauro Carvalho Chehab else 3979a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0x338, 0x001); 3989a0bf528SMauro Carvalho Chehab 3999a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xc05, reg0xc05); 4009a0bf528SMauro Carvalho Chehab 4019a0bf528SMauro Carvalho Chehab if (p->bandwidth_hz == 6000000) 4029a0bf528SMauro Carvalho Chehab val = 2 << 3; 4039a0bf528SMauro Carvalho Chehab else if (p->bandwidth_hz == 7000000) 4049a0bf528SMauro Carvalho Chehab val = 3 << 3; 4059a0bf528SMauro Carvalho Chehab else 4069a0bf528SMauro Carvalho Chehab val = 0 << 3; 4079a0bf528SMauro Carvalho Chehab 4089a0bf528SMauro Carvalho Chehab /* enable OFDM and SAW bits as lock indicators in sync register 0xf17, 4099a0bf528SMauro Carvalho Chehab * optimize algorithm for given bandwidth... 4109a0bf528SMauro Carvalho Chehab */ 4119a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xf14, 0x160 | val); 4129a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xf15, 0x000); 4139a0bf528SMauro Carvalho Chehab 4149a0bf528SMauro Carvalho Chehab sp887x_microcontroller_start(state); 4159a0bf528SMauro Carvalho Chehab return 0; 4169a0bf528SMauro Carvalho Chehab } 4179a0bf528SMauro Carvalho Chehab 4180df289a2SMauro Carvalho Chehab static int sp887x_read_status(struct dvb_frontend *fe, enum fe_status *status) 4199a0bf528SMauro Carvalho Chehab { 4209a0bf528SMauro Carvalho Chehab struct sp887x_state* state = fe->demodulator_priv; 4219a0bf528SMauro Carvalho Chehab u16 snr12 = sp887x_readreg(state, 0xf16); 4229a0bf528SMauro Carvalho Chehab u16 sync0x200 = sp887x_readreg(state, 0x200); 4239a0bf528SMauro Carvalho Chehab u16 sync0xf17 = sp887x_readreg(state, 0xf17); 4249a0bf528SMauro Carvalho Chehab 4259a0bf528SMauro Carvalho Chehab *status = 0; 4269a0bf528SMauro Carvalho Chehab 4279a0bf528SMauro Carvalho Chehab if (snr12 > 0x00f) 4289a0bf528SMauro Carvalho Chehab *status |= FE_HAS_SIGNAL; 4299a0bf528SMauro Carvalho Chehab 4309a0bf528SMauro Carvalho Chehab //if (sync0x200 & 0x004) 4319a0bf528SMauro Carvalho Chehab // *status |= FE_HAS_SYNC | FE_HAS_CARRIER; 4329a0bf528SMauro Carvalho Chehab 4339a0bf528SMauro Carvalho Chehab //if (sync0x200 & 0x008) 4349a0bf528SMauro Carvalho Chehab // *status |= FE_HAS_VITERBI; 4359a0bf528SMauro Carvalho Chehab 4369a0bf528SMauro Carvalho Chehab if ((sync0xf17 & 0x00f) == 0x002) { 4379a0bf528SMauro Carvalho Chehab *status |= FE_HAS_LOCK; 4389a0bf528SMauro Carvalho Chehab *status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_CARRIER; 4399a0bf528SMauro Carvalho Chehab } 4409a0bf528SMauro Carvalho Chehab 4419a0bf528SMauro Carvalho Chehab if (sync0x200 & 0x001) { /* tuner adjustment requested...*/ 4429a0bf528SMauro Carvalho Chehab int steps = (sync0x200 >> 4) & 0x00f; 4439a0bf528SMauro Carvalho Chehab if (steps & 0x008) 4449a0bf528SMauro Carvalho Chehab steps = -steps; 4459a0bf528SMauro Carvalho Chehab dprintk("sp887x: implement tuner adjustment (%+i steps)!!\n", 4469a0bf528SMauro Carvalho Chehab steps); 4479a0bf528SMauro Carvalho Chehab } 4489a0bf528SMauro Carvalho Chehab 4499a0bf528SMauro Carvalho Chehab return 0; 4509a0bf528SMauro Carvalho Chehab } 4519a0bf528SMauro Carvalho Chehab 4529a0bf528SMauro Carvalho Chehab static int sp887x_read_ber(struct dvb_frontend* fe, u32* ber) 4539a0bf528SMauro Carvalho Chehab { 4549a0bf528SMauro Carvalho Chehab struct sp887x_state* state = fe->demodulator_priv; 4559a0bf528SMauro Carvalho Chehab 4569a0bf528SMauro Carvalho Chehab *ber = (sp887x_readreg(state, 0xc08) & 0x3f) | 4579a0bf528SMauro Carvalho Chehab (sp887x_readreg(state, 0xc07) << 6); 4589a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xc08, 0x000); 4599a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xc07, 0x000); 4609a0bf528SMauro Carvalho Chehab if (*ber >= 0x3fff0) 4619a0bf528SMauro Carvalho Chehab *ber = ~0; 4629a0bf528SMauro Carvalho Chehab 4639a0bf528SMauro Carvalho Chehab return 0; 4649a0bf528SMauro Carvalho Chehab } 4659a0bf528SMauro Carvalho Chehab 4669a0bf528SMauro Carvalho Chehab static int sp887x_read_signal_strength(struct dvb_frontend* fe, u16* strength) 4679a0bf528SMauro Carvalho Chehab { 4689a0bf528SMauro Carvalho Chehab struct sp887x_state* state = fe->demodulator_priv; 4699a0bf528SMauro Carvalho Chehab 4709a0bf528SMauro Carvalho Chehab u16 snr12 = sp887x_readreg(state, 0xf16); 4719a0bf528SMauro Carvalho Chehab u32 signal = 3 * (snr12 << 4); 4729a0bf528SMauro Carvalho Chehab *strength = (signal < 0xffff) ? signal : 0xffff; 4739a0bf528SMauro Carvalho Chehab 4749a0bf528SMauro Carvalho Chehab return 0; 4759a0bf528SMauro Carvalho Chehab } 4769a0bf528SMauro Carvalho Chehab 4779a0bf528SMauro Carvalho Chehab static int sp887x_read_snr(struct dvb_frontend* fe, u16* snr) 4789a0bf528SMauro Carvalho Chehab { 4799a0bf528SMauro Carvalho Chehab struct sp887x_state* state = fe->demodulator_priv; 4809a0bf528SMauro Carvalho Chehab 4819a0bf528SMauro Carvalho Chehab u16 snr12 = sp887x_readreg(state, 0xf16); 4829a0bf528SMauro Carvalho Chehab *snr = (snr12 << 4) | (snr12 >> 8); 4839a0bf528SMauro Carvalho Chehab 4849a0bf528SMauro Carvalho Chehab return 0; 4859a0bf528SMauro Carvalho Chehab } 4869a0bf528SMauro Carvalho Chehab 4879a0bf528SMauro Carvalho Chehab static int sp887x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks) 4889a0bf528SMauro Carvalho Chehab { 4899a0bf528SMauro Carvalho Chehab struct sp887x_state* state = fe->demodulator_priv; 4909a0bf528SMauro Carvalho Chehab 4919a0bf528SMauro Carvalho Chehab *ucblocks = sp887x_readreg(state, 0xc0c); 4929a0bf528SMauro Carvalho Chehab if (*ucblocks == 0xfff) 4939a0bf528SMauro Carvalho Chehab *ucblocks = ~0; 4949a0bf528SMauro Carvalho Chehab 4959a0bf528SMauro Carvalho Chehab return 0; 4969a0bf528SMauro Carvalho Chehab } 4979a0bf528SMauro Carvalho Chehab 4989a0bf528SMauro Carvalho Chehab static int sp887x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable) 4999a0bf528SMauro Carvalho Chehab { 5009a0bf528SMauro Carvalho Chehab struct sp887x_state* state = fe->demodulator_priv; 5019a0bf528SMauro Carvalho Chehab 5029a0bf528SMauro Carvalho Chehab if (enable) { 5039a0bf528SMauro Carvalho Chehab return sp887x_writereg(state, 0x206, 0x001); 5049a0bf528SMauro Carvalho Chehab } else { 5059a0bf528SMauro Carvalho Chehab return sp887x_writereg(state, 0x206, 0x000); 5069a0bf528SMauro Carvalho Chehab } 5079a0bf528SMauro Carvalho Chehab } 5089a0bf528SMauro Carvalho Chehab 5099a0bf528SMauro Carvalho Chehab static int sp887x_sleep(struct dvb_frontend* fe) 5109a0bf528SMauro Carvalho Chehab { 5119a0bf528SMauro Carvalho Chehab struct sp887x_state* state = fe->demodulator_priv; 5129a0bf528SMauro Carvalho Chehab 5139a0bf528SMauro Carvalho Chehab /* tristate TS output and disable interface pins */ 5149a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xc18, 0x000); 5159a0bf528SMauro Carvalho Chehab 5169a0bf528SMauro Carvalho Chehab return 0; 5179a0bf528SMauro Carvalho Chehab } 5189a0bf528SMauro Carvalho Chehab 5199a0bf528SMauro Carvalho Chehab static int sp887x_init(struct dvb_frontend* fe) 5209a0bf528SMauro Carvalho Chehab { 5219a0bf528SMauro Carvalho Chehab struct sp887x_state* state = fe->demodulator_priv; 5229a0bf528SMauro Carvalho Chehab const struct firmware *fw = NULL; 5239a0bf528SMauro Carvalho Chehab int ret; 5249a0bf528SMauro Carvalho Chehab 5259a0bf528SMauro Carvalho Chehab if (!state->initialised) { 5269a0bf528SMauro Carvalho Chehab /* request the firmware, this will block until someone uploads it */ 5279a0bf528SMauro Carvalho Chehab printk("sp887x: waiting for firmware upload (%s)...\n", SP887X_DEFAULT_FIRMWARE); 5289a0bf528SMauro Carvalho Chehab ret = state->config->request_firmware(fe, &fw, SP887X_DEFAULT_FIRMWARE); 5299a0bf528SMauro Carvalho Chehab if (ret) { 5309a0bf528SMauro Carvalho Chehab printk("sp887x: no firmware upload (timeout or file not found?)\n"); 5319a0bf528SMauro Carvalho Chehab return ret; 5329a0bf528SMauro Carvalho Chehab } 5339a0bf528SMauro Carvalho Chehab 5349a0bf528SMauro Carvalho Chehab ret = sp887x_initial_setup(fe, fw); 5359a0bf528SMauro Carvalho Chehab release_firmware(fw); 5369a0bf528SMauro Carvalho Chehab if (ret) { 5379a0bf528SMauro Carvalho Chehab printk("sp887x: writing firmware to device failed\n"); 5389a0bf528SMauro Carvalho Chehab return ret; 5399a0bf528SMauro Carvalho Chehab } 5409a0bf528SMauro Carvalho Chehab printk("sp887x: firmware upload complete\n"); 5419a0bf528SMauro Carvalho Chehab state->initialised = 1; 5429a0bf528SMauro Carvalho Chehab } 5439a0bf528SMauro Carvalho Chehab 5449a0bf528SMauro Carvalho Chehab /* enable TS output and interface pins */ 5459a0bf528SMauro Carvalho Chehab sp887x_writereg(state, 0xc18, 0x00d); 5469a0bf528SMauro Carvalho Chehab 5479a0bf528SMauro Carvalho Chehab return 0; 5489a0bf528SMauro Carvalho Chehab } 5499a0bf528SMauro Carvalho Chehab 5509a0bf528SMauro Carvalho Chehab static int sp887x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings) 5519a0bf528SMauro Carvalho Chehab { 5529a0bf528SMauro Carvalho Chehab fesettings->min_delay_ms = 350; 5539a0bf528SMauro Carvalho Chehab fesettings->step_size = 166666*2; 5549a0bf528SMauro Carvalho Chehab fesettings->max_drift = (166666*2)+1; 5559a0bf528SMauro Carvalho Chehab return 0; 5569a0bf528SMauro Carvalho Chehab } 5579a0bf528SMauro Carvalho Chehab 5589a0bf528SMauro Carvalho Chehab static void sp887x_release(struct dvb_frontend* fe) 5599a0bf528SMauro Carvalho Chehab { 5609a0bf528SMauro Carvalho Chehab struct sp887x_state* state = fe->demodulator_priv; 5619a0bf528SMauro Carvalho Chehab kfree(state); 5629a0bf528SMauro Carvalho Chehab } 5639a0bf528SMauro Carvalho Chehab 564bd336e63SMax Kellermann static const struct dvb_frontend_ops sp887x_ops; 5659a0bf528SMauro Carvalho Chehab 5669a0bf528SMauro Carvalho Chehab struct dvb_frontend* sp887x_attach(const struct sp887x_config* config, 5679a0bf528SMauro Carvalho Chehab struct i2c_adapter* i2c) 5689a0bf528SMauro Carvalho Chehab { 5699a0bf528SMauro Carvalho Chehab struct sp887x_state* state = NULL; 5709a0bf528SMauro Carvalho Chehab 5719a0bf528SMauro Carvalho Chehab /* allocate memory for the internal state */ 5729a0bf528SMauro Carvalho Chehab state = kzalloc(sizeof(struct sp887x_state), GFP_KERNEL); 5739a0bf528SMauro Carvalho Chehab if (state == NULL) goto error; 5749a0bf528SMauro Carvalho Chehab 5759a0bf528SMauro Carvalho Chehab /* setup the state */ 5769a0bf528SMauro Carvalho Chehab state->config = config; 5779a0bf528SMauro Carvalho Chehab state->i2c = i2c; 5789a0bf528SMauro Carvalho Chehab state->initialised = 0; 5799a0bf528SMauro Carvalho Chehab 5809a0bf528SMauro Carvalho Chehab /* check if the demod is there */ 5819a0bf528SMauro Carvalho Chehab if (sp887x_readreg(state, 0x0200) < 0) goto error; 5829a0bf528SMauro Carvalho Chehab 5839a0bf528SMauro Carvalho Chehab /* create dvb_frontend */ 5849a0bf528SMauro Carvalho Chehab memcpy(&state->frontend.ops, &sp887x_ops, sizeof(struct dvb_frontend_ops)); 5859a0bf528SMauro Carvalho Chehab state->frontend.demodulator_priv = state; 5869a0bf528SMauro Carvalho Chehab return &state->frontend; 5879a0bf528SMauro Carvalho Chehab 5889a0bf528SMauro Carvalho Chehab error: 5899a0bf528SMauro Carvalho Chehab kfree(state); 5909a0bf528SMauro Carvalho Chehab return NULL; 5919a0bf528SMauro Carvalho Chehab } 5929a0bf528SMauro Carvalho Chehab 593bd336e63SMax Kellermann static const struct dvb_frontend_ops sp887x_ops = { 5949a0bf528SMauro Carvalho Chehab .delsys = { SYS_DVBT }, 5959a0bf528SMauro Carvalho Chehab .info = { 5969a0bf528SMauro Carvalho Chehab .name = "Spase SP887x DVB-T", 5979a0bf528SMauro Carvalho Chehab .frequency_min = 50500000, 5989a0bf528SMauro Carvalho Chehab .frequency_max = 858000000, 5999a0bf528SMauro Carvalho Chehab .frequency_stepsize = 166666, 6009a0bf528SMauro Carvalho Chehab .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | 6019a0bf528SMauro Carvalho Chehab FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO | 6029a0bf528SMauro Carvalho Chehab FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | 6039a0bf528SMauro Carvalho Chehab FE_CAN_RECOVER 6049a0bf528SMauro Carvalho Chehab }, 6059a0bf528SMauro Carvalho Chehab 6069a0bf528SMauro Carvalho Chehab .release = sp887x_release, 6079a0bf528SMauro Carvalho Chehab 6089a0bf528SMauro Carvalho Chehab .init = sp887x_init, 6099a0bf528SMauro Carvalho Chehab .sleep = sp887x_sleep, 6109a0bf528SMauro Carvalho Chehab .i2c_gate_ctrl = sp887x_i2c_gate_ctrl, 6119a0bf528SMauro Carvalho Chehab 6129a0bf528SMauro Carvalho Chehab .set_frontend = sp887x_setup_frontend_parameters, 6139a0bf528SMauro Carvalho Chehab .get_tune_settings = sp887x_get_tune_settings, 6149a0bf528SMauro Carvalho Chehab 6159a0bf528SMauro Carvalho Chehab .read_status = sp887x_read_status, 6169a0bf528SMauro Carvalho Chehab .read_ber = sp887x_read_ber, 6179a0bf528SMauro Carvalho Chehab .read_signal_strength = sp887x_read_signal_strength, 6189a0bf528SMauro Carvalho Chehab .read_snr = sp887x_read_snr, 6199a0bf528SMauro Carvalho Chehab .read_ucblocks = sp887x_read_ucblocks, 6209a0bf528SMauro Carvalho Chehab }; 6219a0bf528SMauro Carvalho Chehab 6229a0bf528SMauro Carvalho Chehab module_param(debug, int, 0644); 6239a0bf528SMauro Carvalho Chehab MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); 6249a0bf528SMauro Carvalho Chehab 6259a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("Spase sp887x DVB-T demodulator driver"); 6269a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL"); 6279a0bf528SMauro Carvalho Chehab 6289a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(sp887x_attach); 629