Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | Driver for the Spase sp887x demodulator |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * This driver needs external firmware. Please use the command |
| 7 | * "<kerneldir>/Documentation/dvb/get_dvb_firmware sp887x" to |
Ville Skytt\รค | 12e66f6 | 2006-01-09 15:25:38 -0200 | [diff] [blame] | 8 | * download/extract it, and then copy it to /usr/lib/hotplug/firmware |
| 9 | * or /lib/firmware (depending on configuration of firmware hotplug). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | #define SP887X_DEFAULT_FIRMWARE "dvb-fe-sp887x.fw" |
| 12 | |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/firmware.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 18 | #include <linux/string.h> |
| 19 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include "dvb_frontend.h" |
| 22 | #include "sp887x.h" |
| 23 | |
| 24 | |
| 25 | struct sp887x_state { |
| 26 | struct i2c_adapter* i2c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | const struct sp887x_config* config; |
| 28 | struct dvb_frontend frontend; |
| 29 | |
| 30 | /* demodulator private data */ |
| 31 | u8 initialised:1; |
| 32 | }; |
| 33 | |
| 34 | static int debug; |
| 35 | #define dprintk(args...) \ |
| 36 | do { \ |
| 37 | if (debug) printk(KERN_DEBUG "sp887x: " args); \ |
| 38 | } while (0) |
| 39 | |
| 40 | static int i2c_writebytes (struct sp887x_state* state, u8 *buf, u8 len) |
| 41 | { |
| 42 | struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = len }; |
| 43 | int err; |
| 44 | |
| 45 | if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) { |
| 46 | printk ("%s: i2c write error (addr %02x, err == %i)\n", |
| 47 | __FUNCTION__, state->config->demod_address, err); |
| 48 | return -EREMOTEIO; |
| 49 | } |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int sp887x_writereg (struct sp887x_state* state, u16 reg, u16 data) |
| 55 | { |
| 56 | u8 b0 [] = { reg >> 8 , reg & 0xff, data >> 8, data & 0xff }; |
| 57 | struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 4 }; |
| 58 | int ret; |
| 59 | |
| 60 | if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1) { |
| 61 | /** |
| 62 | * in case of soft reset we ignore ACK errors... |
| 63 | */ |
| 64 | if (!(reg == 0xf1a && data == 0x000 && |
| 65 | (ret == -EREMOTEIO || ret == -EFAULT))) |
| 66 | { |
| 67 | printk("%s: writereg error " |
| 68 | "(reg %03x, data %03x, ret == %i)\n", |
| 69 | __FUNCTION__, reg & 0xffff, data & 0xffff, ret); |
| 70 | return ret; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int sp887x_readreg (struct sp887x_state* state, u16 reg) |
| 78 | { |
| 79 | u8 b0 [] = { reg >> 8 , reg & 0xff }; |
| 80 | u8 b1 [2]; |
| 81 | int ret; |
| 82 | struct i2c_msg msg[] = {{ .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 2 }, |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 83 | { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 2 }}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) { |
| 86 | printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | return (((b1[0] << 8) | b1[1]) & 0xfff); |
| 91 | } |
| 92 | |
| 93 | static void sp887x_microcontroller_stop (struct sp887x_state* state) |
| 94 | { |
| 95 | dprintk("%s\n", __FUNCTION__); |
| 96 | sp887x_writereg(state, 0xf08, 0x000); |
| 97 | sp887x_writereg(state, 0xf09, 0x000); |
| 98 | |
| 99 | /* microcontroller STOP */ |
| 100 | sp887x_writereg(state, 0xf00, 0x000); |
| 101 | } |
| 102 | |
| 103 | static void sp887x_microcontroller_start (struct sp887x_state* state) |
| 104 | { |
| 105 | dprintk("%s\n", __FUNCTION__); |
| 106 | sp887x_writereg(state, 0xf08, 0x000); |
| 107 | sp887x_writereg(state, 0xf09, 0x000); |
| 108 | |
| 109 | /* microcontroller START */ |
| 110 | sp887x_writereg(state, 0xf00, 0x001); |
| 111 | } |
| 112 | |
| 113 | static void sp887x_setup_agc (struct sp887x_state* state) |
| 114 | { |
| 115 | /* setup AGC parameters */ |
| 116 | dprintk("%s\n", __FUNCTION__); |
| 117 | sp887x_writereg(state, 0x33c, 0x054); |
| 118 | sp887x_writereg(state, 0x33b, 0x04c); |
| 119 | sp887x_writereg(state, 0x328, 0x000); |
| 120 | sp887x_writereg(state, 0x327, 0x005); |
| 121 | sp887x_writereg(state, 0x326, 0x001); |
| 122 | sp887x_writereg(state, 0x325, 0x001); |
| 123 | sp887x_writereg(state, 0x324, 0x001); |
| 124 | sp887x_writereg(state, 0x318, 0x050); |
| 125 | sp887x_writereg(state, 0x317, 0x3fe); |
| 126 | sp887x_writereg(state, 0x316, 0x001); |
| 127 | sp887x_writereg(state, 0x313, 0x005); |
| 128 | sp887x_writereg(state, 0x312, 0x002); |
| 129 | sp887x_writereg(state, 0x306, 0x000); |
| 130 | sp887x_writereg(state, 0x303, 0x000); |
| 131 | } |
| 132 | |
| 133 | #define BLOCKSIZE 30 |
| 134 | #define FW_SIZE 0x4000 |
| 135 | /** |
| 136 | * load firmware and setup MPEG interface... |
| 137 | */ |
| 138 | static int sp887x_initial_setup (struct dvb_frontend* fe, const struct firmware *fw) |
| 139 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 140 | struct sp887x_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | u8 buf [BLOCKSIZE+2]; |
| 142 | int i; |
| 143 | int fw_size = fw->size; |
| 144 | unsigned char *mem = fw->data; |
| 145 | |
| 146 | dprintk("%s\n", __FUNCTION__); |
| 147 | |
| 148 | /* ignore the first 10 bytes, then we expect 0x4000 bytes of firmware */ |
| 149 | if (fw_size < FW_SIZE+10) |
| 150 | return -ENODEV; |
| 151 | |
| 152 | mem = fw->data + 10; |
| 153 | |
| 154 | /* soft reset */ |
| 155 | sp887x_writereg(state, 0xf1a, 0x000); |
| 156 | |
| 157 | sp887x_microcontroller_stop (state); |
| 158 | |
| 159 | printk ("%s: firmware upload... ", __FUNCTION__); |
| 160 | |
| 161 | /* setup write pointer to -1 (end of memory) */ |
| 162 | /* bit 0x8000 in address is set to enable 13bit mode */ |
| 163 | sp887x_writereg(state, 0x8f08, 0x1fff); |
| 164 | |
| 165 | /* dummy write (wrap around to start of memory) */ |
| 166 | sp887x_writereg(state, 0x8f0a, 0x0000); |
| 167 | |
| 168 | for (i = 0; i < FW_SIZE; i += BLOCKSIZE) { |
| 169 | int c = BLOCKSIZE; |
| 170 | int err; |
| 171 | |
| 172 | if (i+c > FW_SIZE) |
| 173 | c = FW_SIZE - i; |
| 174 | |
| 175 | /* bit 0x8000 in address is set to enable 13bit mode */ |
| 176 | /* bit 0x4000 enables multibyte read/write transfers */ |
| 177 | /* write register is 0xf0a */ |
| 178 | buf[0] = 0xcf; |
| 179 | buf[1] = 0x0a; |
| 180 | |
| 181 | memcpy(&buf[2], mem + i, c); |
| 182 | |
| 183 | if ((err = i2c_writebytes (state, buf, c+2)) < 0) { |
| 184 | printk ("failed.\n"); |
| 185 | printk ("%s: i2c error (err == %i)\n", __FUNCTION__, err); |
| 186 | return err; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /* don't write RS bytes between packets */ |
| 191 | sp887x_writereg(state, 0xc13, 0x001); |
| 192 | |
| 193 | /* suppress clock if (!data_valid) */ |
| 194 | sp887x_writereg(state, 0xc14, 0x000); |
| 195 | |
| 196 | /* setup MPEG interface... */ |
| 197 | sp887x_writereg(state, 0xc1a, 0x872); |
| 198 | sp887x_writereg(state, 0xc1b, 0x001); |
| 199 | sp887x_writereg(state, 0xc1c, 0x000); /* parallel mode (serial mode == 1) */ |
| 200 | sp887x_writereg(state, 0xc1a, 0x871); |
| 201 | |
| 202 | /* ADC mode, 2 for MT8872, 3 for SP8870/SP8871 */ |
| 203 | sp887x_writereg(state, 0x301, 0x002); |
| 204 | |
| 205 | sp887x_setup_agc(state); |
| 206 | |
| 207 | /* bit 0x010: enable data valid signal */ |
| 208 | sp887x_writereg(state, 0xd00, 0x010); |
| 209 | sp887x_writereg(state, 0x0d1, 0x000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | return 0; |
| 211 | }; |
| 212 | |
| 213 | static int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05) |
| 214 | { |
| 215 | int known_parameters = 1; |
| 216 | |
| 217 | *reg0xc05 = 0x000; |
| 218 | |
| 219 | switch (p->u.ofdm.constellation) { |
| 220 | case QPSK: |
| 221 | break; |
| 222 | case QAM_16: |
| 223 | *reg0xc05 |= (1 << 10); |
| 224 | break; |
| 225 | case QAM_64: |
| 226 | *reg0xc05 |= (2 << 10); |
| 227 | break; |
| 228 | case QAM_AUTO: |
| 229 | known_parameters = 0; |
| 230 | break; |
| 231 | default: |
| 232 | return -EINVAL; |
| 233 | }; |
| 234 | |
| 235 | switch (p->u.ofdm.hierarchy_information) { |
| 236 | case HIERARCHY_NONE: |
| 237 | break; |
| 238 | case HIERARCHY_1: |
| 239 | *reg0xc05 |= (1 << 7); |
| 240 | break; |
| 241 | case HIERARCHY_2: |
| 242 | *reg0xc05 |= (2 << 7); |
| 243 | break; |
| 244 | case HIERARCHY_4: |
| 245 | *reg0xc05 |= (3 << 7); |
| 246 | break; |
| 247 | case HIERARCHY_AUTO: |
| 248 | known_parameters = 0; |
| 249 | break; |
| 250 | default: |
| 251 | return -EINVAL; |
| 252 | }; |
| 253 | |
| 254 | switch (p->u.ofdm.code_rate_HP) { |
| 255 | case FEC_1_2: |
| 256 | break; |
| 257 | case FEC_2_3: |
| 258 | *reg0xc05 |= (1 << 3); |
| 259 | break; |
| 260 | case FEC_3_4: |
| 261 | *reg0xc05 |= (2 << 3); |
| 262 | break; |
| 263 | case FEC_5_6: |
| 264 | *reg0xc05 |= (3 << 3); |
| 265 | break; |
| 266 | case FEC_7_8: |
| 267 | *reg0xc05 |= (4 << 3); |
| 268 | break; |
| 269 | case FEC_AUTO: |
| 270 | known_parameters = 0; |
| 271 | break; |
| 272 | default: |
| 273 | return -EINVAL; |
| 274 | }; |
| 275 | |
| 276 | if (known_parameters) |
| 277 | *reg0xc05 |= (2 << 1); /* use specified parameters */ |
| 278 | else |
| 279 | *reg0xc05 |= (1 << 1); /* enable autoprobing */ |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * estimates division of two 24bit numbers, |
| 286 | * derived from the ves1820/stv0299 driver code |
| 287 | */ |
| 288 | static void divide (int n, int d, int *quotient_i, int *quotient_f) |
| 289 | { |
| 290 | unsigned int q, r; |
| 291 | |
| 292 | r = (n % d) << 8; |
| 293 | q = (r / d); |
| 294 | |
| 295 | if (quotient_i) |
| 296 | *quotient_i = q; |
| 297 | |
| 298 | if (quotient_f) { |
| 299 | r = (r % d) << 8; |
| 300 | q = (q << 8) | (r / d); |
| 301 | r = (r % d) << 8; |
| 302 | *quotient_f = (q << 8) | (r / d); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static void sp887x_correct_offsets (struct sp887x_state* state, |
| 307 | struct dvb_frontend_parameters *p, |
| 308 | int actual_freq) |
| 309 | { |
| 310 | static const u32 srate_correction [] = { 1879617, 4544878, 8098561 }; |
| 311 | int bw_index = p->u.ofdm.bandwidth - BANDWIDTH_8_MHZ; |
| 312 | int freq_offset = actual_freq - p->frequency; |
| 313 | int sysclock = 61003; //[kHz] |
| 314 | int ifreq = 36000000; |
| 315 | int freq; |
| 316 | int frequency_shift; |
| 317 | |
| 318 | if (p->inversion == INVERSION_ON) |
| 319 | freq = ifreq - freq_offset; |
| 320 | else |
| 321 | freq = ifreq + freq_offset; |
| 322 | |
| 323 | divide(freq / 333, sysclock, NULL, &frequency_shift); |
| 324 | |
| 325 | if (p->inversion == INVERSION_ON) |
| 326 | frequency_shift = -frequency_shift; |
| 327 | |
| 328 | /* sample rate correction */ |
| 329 | sp887x_writereg(state, 0x319, srate_correction[bw_index] >> 12); |
| 330 | sp887x_writereg(state, 0x31a, srate_correction[bw_index] & 0xfff); |
| 331 | |
| 332 | /* carrier offset correction */ |
| 333 | sp887x_writereg(state, 0x309, frequency_shift >> 12); |
| 334 | sp887x_writereg(state, 0x30a, frequency_shift & 0xfff); |
| 335 | } |
| 336 | |
| 337 | static int sp887x_setup_frontend_parameters (struct dvb_frontend* fe, |
| 338 | struct dvb_frontend_parameters *p) |
| 339 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 340 | struct sp887x_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | int actual_freq, err; |
| 342 | u16 val, reg0xc05; |
| 343 | |
| 344 | if (p->u.ofdm.bandwidth != BANDWIDTH_8_MHZ && |
| 345 | p->u.ofdm.bandwidth != BANDWIDTH_7_MHZ && |
| 346 | p->u.ofdm.bandwidth != BANDWIDTH_6_MHZ) |
| 347 | return -EINVAL; |
| 348 | |
| 349 | if ((err = configure_reg0xc05(p, ®0xc05))) |
| 350 | return err; |
| 351 | |
| 352 | sp887x_microcontroller_stop(state); |
| 353 | |
| 354 | /* setup the PLL */ |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 355 | if (fe->ops.tuner_ops.set_params) { |
| 356 | fe->ops.tuner_ops.set_params(fe, p); |
| 357 | if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0); |
Andrew de Quincey | 085542d | 2006-04-18 17:47:10 -0300 | [diff] [blame] | 358 | } |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 359 | if (fe->ops.tuner_ops.get_frequency) { |
| 360 | fe->ops.tuner_ops.get_frequency(fe, &actual_freq); |
| 361 | if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0); |
Andrew de Quincey | 085542d | 2006-04-18 17:47:10 -0300 | [diff] [blame] | 362 | } else { |
| 363 | actual_freq = p->frequency; |
| 364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | /* read status reg in order to clear <pending irqs */ |
| 367 | sp887x_readreg(state, 0x200); |
| 368 | |
| 369 | sp887x_correct_offsets(state, p, actual_freq); |
| 370 | |
| 371 | /* filter for 6/7/8 Mhz channel */ |
| 372 | if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ) |
| 373 | val = 2; |
| 374 | else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ) |
| 375 | val = 1; |
| 376 | else |
| 377 | val = 0; |
| 378 | |
| 379 | sp887x_writereg(state, 0x311, val); |
| 380 | |
| 381 | /* scan order: 2k first = 0, 8k first = 1 */ |
| 382 | if (p->u.ofdm.transmission_mode == TRANSMISSION_MODE_2K) |
| 383 | sp887x_writereg(state, 0x338, 0x000); |
| 384 | else |
| 385 | sp887x_writereg(state, 0x338, 0x001); |
| 386 | |
| 387 | sp887x_writereg(state, 0xc05, reg0xc05); |
| 388 | |
| 389 | if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ) |
| 390 | val = 2 << 3; |
| 391 | else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ) |
| 392 | val = 3 << 3; |
| 393 | else |
| 394 | val = 0 << 3; |
| 395 | |
| 396 | /* enable OFDM and SAW bits as lock indicators in sync register 0xf17, |
| 397 | * optimize algorithm for given bandwidth... |
| 398 | */ |
| 399 | sp887x_writereg(state, 0xf14, 0x160 | val); |
| 400 | sp887x_writereg(state, 0xf15, 0x000); |
| 401 | |
| 402 | sp887x_microcontroller_start(state); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int sp887x_read_status(struct dvb_frontend* fe, fe_status_t* status) |
| 407 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 408 | struct sp887x_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | u16 snr12 = sp887x_readreg(state, 0xf16); |
| 410 | u16 sync0x200 = sp887x_readreg(state, 0x200); |
| 411 | u16 sync0xf17 = sp887x_readreg(state, 0xf17); |
| 412 | |
| 413 | *status = 0; |
| 414 | |
| 415 | if (snr12 > 0x00f) |
| 416 | *status |= FE_HAS_SIGNAL; |
| 417 | |
| 418 | //if (sync0x200 & 0x004) |
| 419 | // *status |= FE_HAS_SYNC | FE_HAS_CARRIER; |
| 420 | |
| 421 | //if (sync0x200 & 0x008) |
| 422 | // *status |= FE_HAS_VITERBI; |
| 423 | |
| 424 | if ((sync0xf17 & 0x00f) == 0x002) { |
| 425 | *status |= FE_HAS_LOCK; |
| 426 | *status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_CARRIER; |
| 427 | } |
| 428 | |
| 429 | if (sync0x200 & 0x001) { /* tuner adjustment requested...*/ |
| 430 | int steps = (sync0x200 >> 4) & 0x00f; |
| 431 | if (steps & 0x008) |
| 432 | steps = -steps; |
| 433 | dprintk("sp887x: implement tuner adjustment (%+i steps)!!\n", |
| 434 | steps); |
| 435 | } |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static int sp887x_read_ber(struct dvb_frontend* fe, u32* ber) |
| 441 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 442 | struct sp887x_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
| 444 | *ber = (sp887x_readreg(state, 0xc08) & 0x3f) | |
| 445 | (sp887x_readreg(state, 0xc07) << 6); |
| 446 | sp887x_writereg(state, 0xc08, 0x000); |
| 447 | sp887x_writereg(state, 0xc07, 0x000); |
| 448 | if (*ber >= 0x3fff0) |
| 449 | *ber = ~0; |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static int sp887x_read_signal_strength(struct dvb_frontend* fe, u16* strength) |
| 455 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 456 | struct sp887x_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | u16 snr12 = sp887x_readreg(state, 0xf16); |
| 459 | u32 signal = 3 * (snr12 << 4); |
| 460 | *strength = (signal < 0xffff) ? signal : 0xffff; |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static int sp887x_read_snr(struct dvb_frontend* fe, u16* snr) |
| 466 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 467 | struct sp887x_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
| 469 | u16 snr12 = sp887x_readreg(state, 0xf16); |
| 470 | *snr = (snr12 << 4) | (snr12 >> 8); |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static int sp887x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks) |
| 476 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 477 | struct sp887x_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | *ucblocks = sp887x_readreg(state, 0xc0c); |
| 480 | if (*ucblocks == 0xfff) |
| 481 | *ucblocks = ~0; |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
Andrew de Quincey | 085542d | 2006-04-18 17:47:10 -0300 | [diff] [blame] | 486 | static int sp887x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable) |
| 487 | { |
| 488 | struct sp887x_state* state = fe->demodulator_priv; |
| 489 | |
| 490 | if (enable) { |
| 491 | return sp887x_writereg(state, 0x206, 0x001); |
| 492 | } else { |
| 493 | return sp887x_writereg(state, 0x206, 0x000); |
| 494 | } |
| 495 | } |
| 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | static int sp887x_sleep(struct dvb_frontend* fe) |
| 498 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 499 | struct sp887x_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | /* tristate TS output and disable interface pins */ |
| 502 | sp887x_writereg(state, 0xc18, 0x000); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int sp887x_init(struct dvb_frontend* fe) |
| 508 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 509 | struct sp887x_state* state = fe->demodulator_priv; |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 510 | const struct firmware *fw = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | int ret; |
| 512 | |
| 513 | if (!state->initialised) { |
| 514 | /* request the firmware, this will block until someone uploads it */ |
| 515 | printk("sp887x: waiting for firmware upload (%s)...\n", SP887X_DEFAULT_FIRMWARE); |
| 516 | ret = state->config->request_firmware(fe, &fw, SP887X_DEFAULT_FIRMWARE); |
| 517 | if (ret) { |
| 518 | printk("sp887x: no firmware upload (timeout or file not found?)\n"); |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | ret = sp887x_initial_setup(fe, fw); |
| 523 | if (ret) { |
| 524 | printk("sp887x: writing firmware to device failed\n"); |
| 525 | release_firmware(fw); |
| 526 | return ret; |
| 527 | } |
| 528 | printk("sp887x: firmware upload complete\n"); |
| 529 | state->initialised = 1; |
| 530 | } |
| 531 | |
| 532 | /* enable TS output and interface pins */ |
| 533 | sp887x_writereg(state, 0xc18, 0x00d); |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static int sp887x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings) |
| 539 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 540 | fesettings->min_delay_ms = 350; |
| 541 | fesettings->step_size = 166666*2; |
| 542 | fesettings->max_drift = (166666*2)+1; |
| 543 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static void sp887x_release(struct dvb_frontend* fe) |
| 547 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 548 | struct sp887x_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | kfree(state); |
| 550 | } |
| 551 | |
| 552 | static struct dvb_frontend_ops sp887x_ops; |
| 553 | |
| 554 | struct dvb_frontend* sp887x_attach(const struct sp887x_config* config, |
| 555 | struct i2c_adapter* i2c) |
| 556 | { |
| 557 | struct sp887x_state* state = NULL; |
| 558 | |
| 559 | /* allocate memory for the internal state */ |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 560 | state = kmalloc(sizeof(struct sp887x_state), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | if (state == NULL) goto error; |
| 562 | |
| 563 | /* setup the state */ |
| 564 | state->config = config; |
| 565 | state->i2c = i2c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | state->initialised = 0; |
| 567 | |
| 568 | /* check if the demod is there */ |
| 569 | if (sp887x_readreg(state, 0x0200) < 0) goto error; |
| 570 | |
| 571 | /* create dvb_frontend */ |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 572 | memcpy(&state->frontend.ops, &sp887x_ops, sizeof(struct dvb_frontend_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | state->frontend.demodulator_priv = state; |
| 574 | return &state->frontend; |
| 575 | |
| 576 | error: |
| 577 | kfree(state); |
| 578 | return NULL; |
| 579 | } |
| 580 | |
| 581 | static struct dvb_frontend_ops sp887x_ops = { |
| 582 | |
| 583 | .info = { |
| 584 | .name = "Spase SP887x DVB-T", |
| 585 | .type = FE_OFDM, |
| 586 | .frequency_min = 50500000, |
| 587 | .frequency_max = 858000000, |
| 588 | .frequency_stepsize = 166666, |
| 589 | .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | |
| 590 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO | |
| 591 | FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 592 | FE_CAN_RECOVER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | }, |
| 594 | |
| 595 | .release = sp887x_release, |
| 596 | |
| 597 | .init = sp887x_init, |
| 598 | .sleep = sp887x_sleep, |
Andrew de Quincey | 085542d | 2006-04-18 17:47:10 -0300 | [diff] [blame] | 599 | .i2c_gate_ctrl = sp887x_i2c_gate_ctrl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
| 601 | .set_frontend = sp887x_setup_frontend_parameters, |
| 602 | .get_tune_settings = sp887x_get_tune_settings, |
| 603 | |
| 604 | .read_status = sp887x_read_status, |
| 605 | .read_ber = sp887x_read_ber, |
| 606 | .read_signal_strength = sp887x_read_signal_strength, |
| 607 | .read_snr = sp887x_read_snr, |
| 608 | .read_ucblocks = sp887x_read_ucblocks, |
| 609 | }; |
| 610 | |
| 611 | module_param(debug, int, 0644); |
| 612 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); |
| 613 | |
| 614 | MODULE_DESCRIPTION("Spase sp887x DVB-T demodulator driver"); |
| 615 | MODULE_LICENSE("GPL"); |
| 616 | |
| 617 | EXPORT_SYMBOL(sp887x_attach); |