Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | Driver for STV0297 demodulator |
| 3 | |
| 4 | Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net> |
| 5 | Copyright (C) 2003-2004 Dennis Noermann <dennis.noermann@noernet.de> |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/delay.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 27 | #include <linux/jiffies.h> |
| 28 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include "dvb_frontend.h" |
| 31 | #include "stv0297.h" |
| 32 | |
| 33 | struct stv0297_state { |
| 34 | struct i2c_adapter *i2c; |
| 35 | struct dvb_frontend_ops ops; |
| 36 | const struct stv0297_config *config; |
| 37 | struct dvb_frontend frontend; |
| 38 | |
| 39 | unsigned long base_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | #if 1 |
| 43 | #define dprintk(x...) printk(x) |
| 44 | #else |
| 45 | #define dprintk(x...) |
| 46 | #endif |
| 47 | |
| 48 | #define STV0297_CLOCK_KHZ 28900 |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | static int stv0297_writereg(struct stv0297_state *state, u8 reg, u8 data) |
| 52 | { |
| 53 | int ret; |
| 54 | u8 buf[] = { reg, data }; |
| 55 | struct i2c_msg msg = {.addr = state->config->demod_address,.flags = 0,.buf = buf,.len = 2 }; |
| 56 | |
| 57 | ret = i2c_transfer(state->i2c, &msg, 1); |
| 58 | |
| 59 | if (ret != 1) |
| 60 | dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, " |
| 61 | "ret == %i)\n", __FUNCTION__, reg, data, ret); |
| 62 | |
| 63 | return (ret != 1) ? -1 : 0; |
| 64 | } |
| 65 | |
| 66 | static int stv0297_readreg(struct stv0297_state *state, u8 reg) |
| 67 | { |
| 68 | int ret; |
| 69 | u8 b0[] = { reg }; |
| 70 | u8 b1[] = { 0 }; |
| 71 | struct i2c_msg msg[] = { {.addr = state->config->demod_address,.flags = 0,.buf = b0,.len = |
| 72 | 1}, |
| 73 | {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b1,.len = 1} |
| 74 | }; |
| 75 | |
| 76 | // this device needs a STOP between the register and data |
| 77 | if ((ret = i2c_transfer(state->i2c, &msg[0], 1)) != 1) { |
| 78 | dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg, ret); |
| 79 | return -1; |
| 80 | } |
| 81 | if ((ret = i2c_transfer(state->i2c, &msg[1], 1)) != 1) { |
| 82 | dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg, ret); |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | return b1[0]; |
| 87 | } |
| 88 | |
| 89 | static int stv0297_writereg_mask(struct stv0297_state *state, u8 reg, u8 mask, u8 data) |
| 90 | { |
| 91 | int val; |
| 92 | |
| 93 | val = stv0297_readreg(state, reg); |
| 94 | val &= ~mask; |
| 95 | val |= (data & mask); |
| 96 | stv0297_writereg(state, reg, val); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int stv0297_readregs(struct stv0297_state *state, u8 reg1, u8 * b, u8 len) |
| 102 | { |
| 103 | int ret; |
| 104 | struct i2c_msg msg[] = { {.addr = state->config->demod_address,.flags = 0,.buf = |
| 105 | ®1,.len = 1}, |
| 106 | {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b,.len = len} |
| 107 | }; |
| 108 | |
| 109 | // this device needs a STOP between the register and data |
| 110 | if ((ret = i2c_transfer(state->i2c, &msg[0], 1)) != 1) { |
| 111 | dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg1, ret); |
| 112 | return -1; |
| 113 | } |
| 114 | if ((ret = i2c_transfer(state->i2c, &msg[1], 1)) != 1) { |
| 115 | dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg1, ret); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static u32 stv0297_get_symbolrate(struct stv0297_state *state) |
| 123 | { |
| 124 | u64 tmp; |
| 125 | |
| 126 | tmp = stv0297_readreg(state, 0x55); |
| 127 | tmp |= stv0297_readreg(state, 0x56) << 8; |
| 128 | tmp |= stv0297_readreg(state, 0x57) << 16; |
| 129 | tmp |= stv0297_readreg(state, 0x58) << 24; |
| 130 | |
| 131 | tmp *= STV0297_CLOCK_KHZ; |
| 132 | tmp >>= 32; |
| 133 | |
| 134 | return (u32) tmp; |
| 135 | } |
| 136 | |
| 137 | static void stv0297_set_symbolrate(struct stv0297_state *state, u32 srate) |
| 138 | { |
| 139 | long tmp; |
| 140 | |
| 141 | tmp = 131072L * srate; /* 131072 = 2^17 */ |
| 142 | tmp = tmp / (STV0297_CLOCK_KHZ / 4); /* 1/4 = 2^-2 */ |
| 143 | tmp = tmp * 8192L; /* 8192 = 2^13 */ |
| 144 | |
| 145 | stv0297_writereg(state, 0x55, (unsigned char) (tmp & 0xFF)); |
| 146 | stv0297_writereg(state, 0x56, (unsigned char) (tmp >> 8)); |
| 147 | stv0297_writereg(state, 0x57, (unsigned char) (tmp >> 16)); |
| 148 | stv0297_writereg(state, 0x58, (unsigned char) (tmp >> 24)); |
| 149 | } |
| 150 | |
| 151 | static void stv0297_set_sweeprate(struct stv0297_state *state, short fshift, long symrate) |
| 152 | { |
| 153 | long tmp; |
| 154 | |
| 155 | tmp = (long) fshift *262144L; /* 262144 = 2*18 */ |
| 156 | tmp /= symrate; |
| 157 | tmp *= 1024; /* 1024 = 2*10 */ |
| 158 | |
| 159 | // adjust |
| 160 | if (tmp >= 0) { |
| 161 | tmp += 500000; |
| 162 | } else { |
| 163 | tmp -= 500000; |
| 164 | } |
| 165 | tmp /= 1000000; |
| 166 | |
| 167 | stv0297_writereg(state, 0x60, tmp & 0xFF); |
| 168 | stv0297_writereg_mask(state, 0x69, 0xF0, (tmp >> 4) & 0xf0); |
| 169 | } |
| 170 | |
| 171 | static void stv0297_set_carrieroffset(struct stv0297_state *state, long offset) |
| 172 | { |
| 173 | long tmp; |
| 174 | |
| 175 | /* symrate is hardcoded to 10000 */ |
| 176 | tmp = offset * 26844L; /* (2**28)/10000 */ |
| 177 | if (tmp < 0) |
| 178 | tmp += 0x10000000; |
| 179 | tmp &= 0x0FFFFFFF; |
| 180 | |
| 181 | stv0297_writereg(state, 0x66, (unsigned char) (tmp & 0xFF)); |
| 182 | stv0297_writereg(state, 0x67, (unsigned char) (tmp >> 8)); |
| 183 | stv0297_writereg(state, 0x68, (unsigned char) (tmp >> 16)); |
| 184 | stv0297_writereg_mask(state, 0x69, 0x0F, (tmp >> 24) & 0x0f); |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | static long stv0297_get_carrieroffset(struct stv0297_state *state) |
| 189 | { |
| 190 | s64 tmp; |
| 191 | |
| 192 | stv0297_writereg(state, 0x6B, 0x00); |
| 193 | |
| 194 | tmp = stv0297_readreg(state, 0x66); |
| 195 | tmp |= (stv0297_readreg(state, 0x67) << 8); |
| 196 | tmp |= (stv0297_readreg(state, 0x68) << 16); |
| 197 | tmp |= (stv0297_readreg(state, 0x69) & 0x0F) << 24; |
| 198 | |
| 199 | tmp *= stv0297_get_symbolrate(state); |
| 200 | tmp >>= 28; |
| 201 | |
| 202 | return (s32) tmp; |
| 203 | } |
| 204 | */ |
| 205 | |
| 206 | static void stv0297_set_initialdemodfreq(struct stv0297_state *state, long freq) |
| 207 | { |
| 208 | s32 tmp; |
| 209 | |
| 210 | if (freq > 10000) |
| 211 | freq -= STV0297_CLOCK_KHZ; |
| 212 | |
| 213 | tmp = (STV0297_CLOCK_KHZ * 1000) / (1 << 16); |
| 214 | tmp = (freq * 1000) / tmp; |
| 215 | if (tmp > 0xffff) |
| 216 | tmp = 0xffff; |
| 217 | |
| 218 | stv0297_writereg_mask(state, 0x25, 0x80, 0x80); |
| 219 | stv0297_writereg(state, 0x21, tmp >> 8); |
| 220 | stv0297_writereg(state, 0x20, tmp); |
| 221 | } |
| 222 | |
| 223 | static int stv0297_set_qam(struct stv0297_state *state, fe_modulation_t modulation) |
| 224 | { |
| 225 | int val = 0; |
| 226 | |
| 227 | switch (modulation) { |
| 228 | case QAM_16: |
| 229 | val = 0; |
| 230 | break; |
| 231 | |
| 232 | case QAM_32: |
| 233 | val = 1; |
| 234 | break; |
| 235 | |
| 236 | case QAM_64: |
| 237 | val = 4; |
| 238 | break; |
| 239 | |
| 240 | case QAM_128: |
| 241 | val = 2; |
| 242 | break; |
| 243 | |
| 244 | case QAM_256: |
| 245 | val = 3; |
| 246 | break; |
| 247 | |
| 248 | default: |
| 249 | return -EINVAL; |
| 250 | } |
| 251 | |
| 252 | stv0297_writereg_mask(state, 0x00, 0x70, val << 4); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int stv0297_set_inversion(struct stv0297_state *state, fe_spectral_inversion_t inversion) |
| 258 | { |
| 259 | int val = 0; |
| 260 | |
| 261 | switch (inversion) { |
| 262 | case INVERSION_OFF: |
| 263 | val = 0; |
| 264 | break; |
| 265 | |
| 266 | case INVERSION_ON: |
| 267 | val = 1; |
| 268 | break; |
| 269 | |
| 270 | default: |
| 271 | return -EINVAL; |
| 272 | } |
| 273 | |
| 274 | stv0297_writereg_mask(state, 0x83, 0x08, val << 3); |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | int stv0297_enable_plli2c(struct dvb_frontend *fe) |
| 280 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 281 | struct stv0297_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | stv0297_writereg(state, 0x87, 0x78); |
| 284 | stv0297_writereg(state, 0x86, 0xc8); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int stv0297_init(struct dvb_frontend *fe) |
| 290 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 291 | struct stv0297_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | int i; |
| 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | /* load init table */ |
Andrew de Quincey | dc27a16 | 2005-09-09 13:03:07 -0700 | [diff] [blame] | 295 | for (i=0; !(state->config->inittab[i] == 0xff && state->config->inittab[i+1] == 0xff); i+=2) |
| 296 | stv0297_writereg(state, state->config->inittab[i], state->config->inittab[i+1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | msleep(200); |
| 298 | |
| 299 | if (state->config->pll_init) |
| 300 | state->config->pll_init(fe); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int stv0297_sleep(struct dvb_frontend *fe) |
| 306 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 307 | struct stv0297_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | stv0297_writereg_mask(state, 0x80, 1, 1); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static int stv0297_read_status(struct dvb_frontend *fe, fe_status_t * status) |
| 315 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 316 | struct stv0297_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
| 318 | u8 sync = stv0297_readreg(state, 0xDF); |
| 319 | |
| 320 | *status = 0; |
| 321 | if (sync & 0x80) |
| 322 | *status |= |
| 323 | FE_HAS_SYNC | FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_LOCK; |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static int stv0297_read_ber(struct dvb_frontend *fe, u32 * ber) |
| 328 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 329 | struct stv0297_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | u8 BER[3]; |
| 331 | |
| 332 | stv0297_writereg(state, 0xA0, 0x80); // Start Counting bit errors for 4096 Bytes |
| 333 | mdelay(25); // Hopefully got 4096 Bytes |
| 334 | stv0297_readregs(state, 0xA0, BER, 3); |
| 335 | mdelay(25); |
| 336 | *ber = (BER[2] << 8 | BER[1]) / (8 * 4096); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | static int stv0297_read_signal_strength(struct dvb_frontend *fe, u16 * strength) |
| 343 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 344 | struct stv0297_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | u8 STRENGTH[2]; |
| 346 | |
| 347 | stv0297_readregs(state, 0x41, STRENGTH, 2); |
| 348 | *strength = (STRENGTH[1] & 0x03) << 8 | STRENGTH[0]; |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int stv0297_read_snr(struct dvb_frontend *fe, u16 * snr) |
| 354 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 355 | struct stv0297_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | u8 SNR[2]; |
| 357 | |
| 358 | stv0297_readregs(state, 0x07, SNR, 2); |
| 359 | *snr = SNR[1] << 8 | SNR[0]; |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int stv0297_read_ucblocks(struct dvb_frontend *fe, u32 * ucblocks) |
| 365 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 366 | struct stv0297_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | *ucblocks = (stv0297_readreg(state, 0xD5) << 8) |
| 369 | | stv0297_readreg(state, 0xD4); |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int stv0297_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *p) |
| 375 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 376 | struct stv0297_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | int u_threshold; |
| 378 | int initial_u; |
| 379 | int blind_u; |
| 380 | int delay; |
| 381 | int sweeprate; |
| 382 | int carrieroffset; |
| 383 | unsigned long starttime; |
| 384 | unsigned long timeout; |
| 385 | fe_spectral_inversion_t inversion; |
| 386 | |
| 387 | switch (p->u.qam.modulation) { |
| 388 | case QAM_16: |
| 389 | case QAM_32: |
| 390 | case QAM_64: |
| 391 | delay = 100; |
| 392 | sweeprate = 1500; |
| 393 | break; |
| 394 | |
| 395 | case QAM_128: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | case QAM_256: |
| 397 | delay = 200; |
| 398 | sweeprate = 500; |
| 399 | break; |
| 400 | |
| 401 | default: |
| 402 | return -EINVAL; |
| 403 | } |
| 404 | |
| 405 | // determine inversion dependant parameters |
| 406 | inversion = p->inversion; |
| 407 | if (state->config->invert) |
| 408 | inversion = (inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON; |
| 409 | carrieroffset = -330; |
| 410 | switch (inversion) { |
| 411 | case INVERSION_OFF: |
| 412 | break; |
| 413 | |
| 414 | case INVERSION_ON: |
| 415 | sweeprate = -sweeprate; |
| 416 | carrieroffset = -carrieroffset; |
| 417 | break; |
| 418 | |
| 419 | default: |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | |
| 423 | stv0297_init(fe); |
| 424 | state->config->pll_set(fe, p); |
| 425 | |
| 426 | /* clear software interrupts */ |
| 427 | stv0297_writereg(state, 0x82, 0x0); |
| 428 | |
| 429 | /* set initial demodulation frequency */ |
| 430 | stv0297_set_initialdemodfreq(state, 7250); |
| 431 | |
| 432 | /* setup AGC */ |
| 433 | stv0297_writereg_mask(state, 0x43, 0x10, 0x00); |
| 434 | stv0297_writereg(state, 0x41, 0x00); |
| 435 | stv0297_writereg_mask(state, 0x42, 0x03, 0x01); |
| 436 | stv0297_writereg_mask(state, 0x36, 0x60, 0x00); |
| 437 | stv0297_writereg_mask(state, 0x36, 0x18, 0x00); |
| 438 | stv0297_writereg_mask(state, 0x71, 0x80, 0x80); |
| 439 | stv0297_writereg(state, 0x72, 0x00); |
| 440 | stv0297_writereg(state, 0x73, 0x00); |
| 441 | stv0297_writereg_mask(state, 0x74, 0x0F, 0x00); |
| 442 | stv0297_writereg_mask(state, 0x43, 0x08, 0x00); |
| 443 | stv0297_writereg_mask(state, 0x71, 0x80, 0x00); |
| 444 | |
| 445 | /* setup STL */ |
| 446 | stv0297_writereg_mask(state, 0x5a, 0x20, 0x20); |
| 447 | stv0297_writereg_mask(state, 0x5b, 0x02, 0x02); |
| 448 | stv0297_writereg_mask(state, 0x5b, 0x02, 0x00); |
| 449 | stv0297_writereg_mask(state, 0x5b, 0x01, 0x00); |
| 450 | stv0297_writereg_mask(state, 0x5a, 0x40, 0x40); |
| 451 | |
| 452 | /* disable frequency sweep */ |
| 453 | stv0297_writereg_mask(state, 0x6a, 0x01, 0x00); |
| 454 | |
| 455 | /* reset deinterleaver */ |
| 456 | stv0297_writereg_mask(state, 0x81, 0x01, 0x01); |
| 457 | stv0297_writereg_mask(state, 0x81, 0x01, 0x00); |
| 458 | |
| 459 | /* ??? */ |
| 460 | stv0297_writereg_mask(state, 0x83, 0x20, 0x20); |
| 461 | stv0297_writereg_mask(state, 0x83, 0x20, 0x00); |
| 462 | |
| 463 | /* reset equaliser */ |
| 464 | u_threshold = stv0297_readreg(state, 0x00) & 0xf; |
| 465 | initial_u = stv0297_readreg(state, 0x01) >> 4; |
| 466 | blind_u = stv0297_readreg(state, 0x01) & 0xf; |
| 467 | stv0297_writereg_mask(state, 0x84, 0x01, 0x01); |
| 468 | stv0297_writereg_mask(state, 0x84, 0x01, 0x00); |
| 469 | stv0297_writereg_mask(state, 0x00, 0x0f, u_threshold); |
| 470 | stv0297_writereg_mask(state, 0x01, 0xf0, initial_u << 4); |
| 471 | stv0297_writereg_mask(state, 0x01, 0x0f, blind_u); |
| 472 | |
| 473 | /* data comes from internal A/D */ |
| 474 | stv0297_writereg_mask(state, 0x87, 0x80, 0x00); |
| 475 | |
| 476 | /* clear phase registers */ |
| 477 | stv0297_writereg(state, 0x63, 0x00); |
| 478 | stv0297_writereg(state, 0x64, 0x00); |
| 479 | stv0297_writereg(state, 0x65, 0x00); |
| 480 | stv0297_writereg(state, 0x66, 0x00); |
| 481 | stv0297_writereg(state, 0x67, 0x00); |
| 482 | stv0297_writereg(state, 0x68, 0x00); |
| 483 | stv0297_writereg_mask(state, 0x69, 0x0f, 0x00); |
| 484 | |
| 485 | /* set parameters */ |
| 486 | stv0297_set_qam(state, p->u.qam.modulation); |
| 487 | stv0297_set_symbolrate(state, p->u.qam.symbol_rate / 1000); |
| 488 | stv0297_set_sweeprate(state, sweeprate, p->u.qam.symbol_rate / 1000); |
| 489 | stv0297_set_carrieroffset(state, carrieroffset); |
| 490 | stv0297_set_inversion(state, inversion); |
| 491 | |
| 492 | /* kick off lock */ |
Patrick Boettcher | 593cbf3 | 2005-09-09 13:02:38 -0700 | [diff] [blame] | 493 | /* Disable corner detection for higher QAMs */ |
| 494 | if (p->u.qam.modulation == QAM_128 || |
| 495 | p->u.qam.modulation == QAM_256) |
| 496 | stv0297_writereg_mask(state, 0x88, 0x08, 0x00); |
| 497 | else |
| 498 | stv0297_writereg_mask(state, 0x88, 0x08, 0x08); |
| 499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | stv0297_writereg_mask(state, 0x5a, 0x20, 0x00); |
| 501 | stv0297_writereg_mask(state, 0x6a, 0x01, 0x01); |
| 502 | stv0297_writereg_mask(state, 0x43, 0x40, 0x40); |
| 503 | stv0297_writereg_mask(state, 0x5b, 0x30, 0x00); |
| 504 | stv0297_writereg_mask(state, 0x03, 0x0c, 0x0c); |
| 505 | stv0297_writereg_mask(state, 0x03, 0x03, 0x03); |
| 506 | stv0297_writereg_mask(state, 0x43, 0x10, 0x10); |
| 507 | |
| 508 | /* wait for WGAGC lock */ |
| 509 | starttime = jiffies; |
Johannes Stezenbach | 48e4cc2 | 2005-07-07 17:57:45 -0700 | [diff] [blame] | 510 | timeout = jiffies + msecs_to_jiffies(2000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | while (time_before(jiffies, timeout)) { |
| 512 | msleep(10); |
| 513 | if (stv0297_readreg(state, 0x43) & 0x08) |
| 514 | break; |
| 515 | } |
| 516 | if (time_after(jiffies, timeout)) { |
| 517 | goto timeout; |
| 518 | } |
| 519 | msleep(20); |
| 520 | |
| 521 | /* wait for equaliser partial convergence */ |
Johannes Stezenbach | 48e4cc2 | 2005-07-07 17:57:45 -0700 | [diff] [blame] | 522 | timeout = jiffies + msecs_to_jiffies(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | while (time_before(jiffies, timeout)) { |
| 524 | msleep(10); |
| 525 | |
| 526 | if (stv0297_readreg(state, 0x82) & 0x04) { |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | if (time_after(jiffies, timeout)) { |
| 531 | goto timeout; |
| 532 | } |
| 533 | |
| 534 | /* wait for equaliser full convergence */ |
Johannes Stezenbach | 48e4cc2 | 2005-07-07 17:57:45 -0700 | [diff] [blame] | 535 | timeout = jiffies + msecs_to_jiffies(delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | while (time_before(jiffies, timeout)) { |
| 537 | msleep(10); |
| 538 | |
| 539 | if (stv0297_readreg(state, 0x82) & 0x08) { |
| 540 | break; |
| 541 | } |
| 542 | } |
| 543 | if (time_after(jiffies, timeout)) { |
| 544 | goto timeout; |
| 545 | } |
| 546 | |
| 547 | /* disable sweep */ |
| 548 | stv0297_writereg_mask(state, 0x6a, 1, 0); |
| 549 | stv0297_writereg_mask(state, 0x88, 8, 0); |
| 550 | |
| 551 | /* wait for main lock */ |
Johannes Stezenbach | 48e4cc2 | 2005-07-07 17:57:45 -0700 | [diff] [blame] | 552 | timeout = jiffies + msecs_to_jiffies(20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | while (time_before(jiffies, timeout)) { |
| 554 | msleep(10); |
| 555 | |
| 556 | if (stv0297_readreg(state, 0xDF) & 0x80) { |
| 557 | break; |
| 558 | } |
| 559 | } |
| 560 | if (time_after(jiffies, timeout)) { |
| 561 | goto timeout; |
| 562 | } |
| 563 | msleep(100); |
| 564 | |
| 565 | /* is it still locked after that delay? */ |
| 566 | if (!(stv0297_readreg(state, 0xDF) & 0x80)) { |
| 567 | goto timeout; |
| 568 | } |
| 569 | |
| 570 | /* success!! */ |
| 571 | stv0297_writereg_mask(state, 0x5a, 0x40, 0x00); |
| 572 | state->base_freq = p->frequency; |
| 573 | return 0; |
| 574 | |
| 575 | timeout: |
| 576 | stv0297_writereg_mask(state, 0x6a, 0x01, 0x00); |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static int stv0297_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *p) |
| 581 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 582 | struct stv0297_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | int reg_00, reg_83; |
| 584 | |
| 585 | reg_00 = stv0297_readreg(state, 0x00); |
| 586 | reg_83 = stv0297_readreg(state, 0x83); |
| 587 | |
| 588 | p->frequency = state->base_freq; |
| 589 | p->inversion = (reg_83 & 0x08) ? INVERSION_ON : INVERSION_OFF; |
| 590 | if (state->config->invert) |
| 591 | p->inversion = (p->inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON; |
| 592 | p->u.qam.symbol_rate = stv0297_get_symbolrate(state) * 1000; |
| 593 | p->u.qam.fec_inner = FEC_NONE; |
| 594 | |
| 595 | switch ((reg_00 >> 4) & 0x7) { |
| 596 | case 0: |
| 597 | p->u.qam.modulation = QAM_16; |
| 598 | break; |
| 599 | case 1: |
| 600 | p->u.qam.modulation = QAM_32; |
| 601 | break; |
| 602 | case 2: |
| 603 | p->u.qam.modulation = QAM_128; |
| 604 | break; |
| 605 | case 3: |
| 606 | p->u.qam.modulation = QAM_256; |
| 607 | break; |
| 608 | case 4: |
| 609 | p->u.qam.modulation = QAM_64; |
| 610 | break; |
| 611 | } |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | static void stv0297_release(struct dvb_frontend *fe) |
| 617 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 618 | struct stv0297_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | kfree(state); |
| 620 | } |
| 621 | |
| 622 | static struct dvb_frontend_ops stv0297_ops; |
| 623 | |
| 624 | struct dvb_frontend *stv0297_attach(const struct stv0297_config *config, |
Andrew de Quincey | dc27a16 | 2005-09-09 13:03:07 -0700 | [diff] [blame] | 625 | struct i2c_adapter *i2c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | { |
| 627 | struct stv0297_state *state = NULL; |
| 628 | |
| 629 | /* allocate memory for the internal state */ |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 630 | state = kmalloc(sizeof(struct stv0297_state), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | if (state == NULL) |
| 632 | goto error; |
| 633 | |
| 634 | /* setup the state */ |
| 635 | state->config = config; |
| 636 | state->i2c = i2c; |
| 637 | memcpy(&state->ops, &stv0297_ops, sizeof(struct dvb_frontend_ops)); |
| 638 | state->base_freq = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
| 640 | /* check if the demod is there */ |
| 641 | if ((stv0297_readreg(state, 0x80) & 0x70) != 0x20) |
| 642 | goto error; |
| 643 | |
| 644 | /* create dvb_frontend */ |
| 645 | state->frontend.ops = &state->ops; |
| 646 | state->frontend.demodulator_priv = state; |
| 647 | return &state->frontend; |
| 648 | |
| 649 | error: |
| 650 | kfree(state); |
| 651 | return NULL; |
| 652 | } |
| 653 | |
| 654 | static struct dvb_frontend_ops stv0297_ops = { |
| 655 | |
| 656 | .info = { |
| 657 | .name = "ST STV0297 DVB-C", |
| 658 | .type = FE_QAM, |
| 659 | .frequency_min = 64000000, |
| 660 | .frequency_max = 1300000000, |
| 661 | .frequency_stepsize = 62500, |
| 662 | .symbol_rate_min = 870000, |
| 663 | .symbol_rate_max = 11700000, |
| 664 | .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 | |
| 665 | FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO}, |
| 666 | |
| 667 | .release = stv0297_release, |
| 668 | |
| 669 | .init = stv0297_init, |
| 670 | .sleep = stv0297_sleep, |
| 671 | |
| 672 | .set_frontend = stv0297_set_frontend, |
| 673 | .get_frontend = stv0297_get_frontend, |
| 674 | |
| 675 | .read_status = stv0297_read_status, |
| 676 | .read_ber = stv0297_read_ber, |
| 677 | .read_signal_strength = stv0297_read_signal_strength, |
| 678 | .read_snr = stv0297_read_snr, |
| 679 | .read_ucblocks = stv0297_read_ucblocks, |
| 680 | }; |
| 681 | |
| 682 | MODULE_DESCRIPTION("ST STV0297 DVB-C Demodulator driver"); |
| 683 | MODULE_AUTHOR("Dennis Noermann and Andrew de Quincey"); |
| 684 | MODULE_LICENSE("GPL"); |
| 685 | |
| 686 | EXPORT_SYMBOL(stv0297_attach); |
| 687 | EXPORT_SYMBOL(stv0297_enable_plli2c); |