Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | Driver for ST STV0299 demodulator |
| 3 | |
| 4 | Copyright (C) 2001-2002 Convergence Integrated Media GmbH |
| 5 | <ralph@convergence.de>, |
| 6 | <holger@convergence.de>, |
| 7 | <js@convergence.de> |
| 8 | |
| 9 | |
| 10 | Philips SU1278/SH |
| 11 | |
| 12 | Copyright (C) 2002 by Peter Schildmann <peter.schildmann@web.de> |
| 13 | |
| 14 | |
| 15 | LG TDQF-S001F |
| 16 | |
| 17 | Copyright (C) 2002 Felix Domke <tmbinc@elitedvb.net> |
| 18 | & Andreas Oberritter <obi@linuxtv.org> |
| 19 | |
| 20 | |
| 21 | Support for Samsung TBMU24112IMB used on Technisat SkyStar2 rev. 2.6B |
| 22 | |
| 23 | Copyright (C) 2003 Vadim Catana <skystar@moldova.cc>: |
| 24 | |
| 25 | Support for Philips SU1278 on Technotrend hardware |
| 26 | |
| 27 | Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net> |
| 28 | |
| 29 | This program is free software; you can redistribute it and/or modify |
| 30 | it under the terms of the GNU General Public License as published by |
| 31 | the Free Software Foundation; either version 2 of the License, or |
| 32 | (at your option) any later version. |
| 33 | |
| 34 | This program is distributed in the hope that it will be useful, |
| 35 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | GNU General Public License for more details. |
| 38 | |
| 39 | You should have received a copy of the GNU General Public License |
| 40 | along with this program; if not, write to the Free Software |
| 41 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 42 | |
| 43 | */ |
| 44 | |
| 45 | #include <linux/init.h> |
| 46 | #include <linux/kernel.h> |
| 47 | #include <linux/module.h> |
| 48 | #include <linux/moduleparam.h> |
| 49 | #include <linux/string.h> |
| 50 | #include <linux/slab.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame^] | 51 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <asm/div64.h> |
| 53 | |
| 54 | #include "dvb_frontend.h" |
| 55 | #include "stv0299.h" |
| 56 | |
| 57 | struct stv0299_state { |
| 58 | struct i2c_adapter* i2c; |
| 59 | struct dvb_frontend_ops ops; |
| 60 | const struct stv0299_config* config; |
| 61 | struct dvb_frontend frontend; |
| 62 | |
| 63 | u8 initialised:1; |
| 64 | u32 tuner_frequency; |
| 65 | u32 symbol_rate; |
| 66 | fe_code_rate_t fec_inner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | static int debug; |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 70 | static int debug_legacy_dish_switch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #define dprintk(args...) \ |
| 72 | do { \ |
| 73 | if (debug) printk(KERN_DEBUG "stv0299: " args); \ |
| 74 | } while (0) |
| 75 | |
| 76 | |
| 77 | static int stv0299_writeregI (struct stv0299_state* state, u8 reg, u8 data) |
| 78 | { |
| 79 | int ret; |
| 80 | u8 buf [] = { reg, data }; |
| 81 | struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 }; |
| 82 | |
| 83 | ret = i2c_transfer (state->i2c, &msg, 1); |
| 84 | |
| 85 | if (ret != 1) |
| 86 | dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, " |
| 87 | "ret == %i)\n", __FUNCTION__, reg, data, ret); |
| 88 | |
| 89 | return (ret != 1) ? -EREMOTEIO : 0; |
| 90 | } |
| 91 | |
| 92 | int stv0299_writereg (struct dvb_frontend* fe, u8 reg, u8 data) |
| 93 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 94 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | return stv0299_writeregI(state, reg, data); |
| 97 | } |
| 98 | |
| 99 | static u8 stv0299_readreg (struct stv0299_state* state, u8 reg) |
| 100 | { |
| 101 | int ret; |
| 102 | u8 b0 [] = { reg }; |
| 103 | u8 b1 [] = { 0 }; |
| 104 | struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 }, |
| 105 | { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } }; |
| 106 | |
| 107 | ret = i2c_transfer (state->i2c, msg, 2); |
| 108 | |
| 109 | if (ret != 2) |
| 110 | dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", |
| 111 | __FUNCTION__, reg, ret); |
| 112 | |
| 113 | return b1[0]; |
| 114 | } |
| 115 | |
| 116 | static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len) |
| 117 | { |
| 118 | int ret; |
| 119 | struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = ®1, .len = 1 }, |
| 120 | { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } }; |
| 121 | |
| 122 | ret = i2c_transfer (state->i2c, msg, 2); |
| 123 | |
| 124 | if (ret != 2) |
| 125 | dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret); |
| 126 | |
| 127 | return ret == 2 ? 0 : ret; |
| 128 | } |
| 129 | |
| 130 | static int stv0299_set_FEC (struct stv0299_state* state, fe_code_rate_t fec) |
| 131 | { |
| 132 | dprintk ("%s\n", __FUNCTION__); |
| 133 | |
| 134 | switch (fec) { |
| 135 | case FEC_AUTO: |
| 136 | { |
| 137 | return stv0299_writeregI (state, 0x31, 0x1f); |
| 138 | } |
| 139 | case FEC_1_2: |
| 140 | { |
| 141 | return stv0299_writeregI (state, 0x31, 0x01); |
| 142 | } |
| 143 | case FEC_2_3: |
| 144 | { |
| 145 | return stv0299_writeregI (state, 0x31, 0x02); |
| 146 | } |
| 147 | case FEC_3_4: |
| 148 | { |
| 149 | return stv0299_writeregI (state, 0x31, 0x04); |
| 150 | } |
| 151 | case FEC_5_6: |
| 152 | { |
| 153 | return stv0299_writeregI (state, 0x31, 0x08); |
| 154 | } |
| 155 | case FEC_7_8: |
| 156 | { |
| 157 | return stv0299_writeregI (state, 0x31, 0x10); |
| 158 | } |
| 159 | default: |
| 160 | { |
| 161 | return -EINVAL; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static fe_code_rate_t stv0299_get_fec (struct stv0299_state* state) |
| 167 | { |
| 168 | static fe_code_rate_t fec_tab [] = { FEC_2_3, FEC_3_4, FEC_5_6, |
| 169 | FEC_7_8, FEC_1_2 }; |
| 170 | u8 index; |
| 171 | |
| 172 | dprintk ("%s\n", __FUNCTION__); |
| 173 | |
| 174 | index = stv0299_readreg (state, 0x1b); |
| 175 | index &= 0x7; |
| 176 | |
| 177 | if (index > 4) |
| 178 | return FEC_AUTO; |
| 179 | |
| 180 | return fec_tab [index]; |
| 181 | } |
| 182 | |
| 183 | static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout) |
| 184 | { |
| 185 | unsigned long start = jiffies; |
| 186 | |
| 187 | dprintk ("%s\n", __FUNCTION__); |
| 188 | |
| 189 | while (stv0299_readreg(state, 0x0a) & 1) { |
| 190 | if (jiffies - start > timeout) { |
| 191 | dprintk ("%s: timeout!!\n", __FUNCTION__); |
| 192 | return -ETIMEDOUT; |
| 193 | } |
| 194 | msleep(10); |
| 195 | }; |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout) |
| 201 | { |
| 202 | unsigned long start = jiffies; |
| 203 | |
| 204 | dprintk ("%s\n", __FUNCTION__); |
| 205 | |
| 206 | while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) { |
| 207 | if (jiffies - start > timeout) { |
| 208 | dprintk ("%s: timeout!!\n", __FUNCTION__); |
| 209 | return -ETIMEDOUT; |
| 210 | } |
| 211 | msleep(10); |
| 212 | }; |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate) |
| 218 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 219 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | u64 big = srate; |
| 221 | u32 ratio; |
| 222 | |
| 223 | // check rate is within limits |
| 224 | if ((srate < 1000000) || (srate > 45000000)) return -EINVAL; |
| 225 | |
| 226 | // calculate value to program |
| 227 | big = big << 20; |
| 228 | big += (state->config->mclk-1); // round correctly |
| 229 | do_div(big, state->config->mclk); |
| 230 | ratio = big << 4; |
| 231 | |
| 232 | return state->config->set_symbol_rate(fe, srate, ratio); |
| 233 | } |
| 234 | |
| 235 | static int stv0299_get_symbolrate (struct stv0299_state* state) |
| 236 | { |
| 237 | u32 Mclk = state->config->mclk / 4096L; |
| 238 | u32 srate; |
| 239 | s32 offset; |
| 240 | u8 sfr[3]; |
| 241 | s8 rtf; |
| 242 | |
| 243 | dprintk ("%s\n", __FUNCTION__); |
| 244 | |
| 245 | stv0299_readregs (state, 0x1f, sfr, 3); |
| 246 | stv0299_readregs (state, 0x1a, &rtf, 1); |
| 247 | |
| 248 | srate = (sfr[0] << 8) | sfr[1]; |
| 249 | srate *= Mclk; |
| 250 | srate /= 16; |
| 251 | srate += (sfr[2] >> 4) * Mclk / 256; |
| 252 | offset = (s32) rtf * (srate / 4096L); |
| 253 | offset /= 128; |
| 254 | |
| 255 | dprintk ("%s : srate = %i\n", __FUNCTION__, srate); |
| 256 | dprintk ("%s : ofset = %i\n", __FUNCTION__, offset); |
| 257 | |
| 258 | srate += offset; |
| 259 | |
| 260 | srate += 1000; |
| 261 | srate /= 2000; |
| 262 | srate *= 2000; |
| 263 | |
| 264 | return srate; |
| 265 | } |
| 266 | |
| 267 | static int stv0299_send_diseqc_msg (struct dvb_frontend* fe, |
| 268 | struct dvb_diseqc_master_cmd *m) |
| 269 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 270 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | u8 val; |
| 272 | int i; |
| 273 | |
| 274 | dprintk ("%s\n", __FUNCTION__); |
| 275 | |
| 276 | if (stv0299_wait_diseqc_idle (state, 100) < 0) |
| 277 | return -ETIMEDOUT; |
| 278 | |
| 279 | val = stv0299_readreg (state, 0x08); |
| 280 | |
| 281 | if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6)) /* DiSEqC mode */ |
| 282 | return -EREMOTEIO; |
| 283 | |
| 284 | for (i=0; i<m->msg_len; i++) { |
| 285 | if (stv0299_wait_diseqc_fifo (state, 100) < 0) |
| 286 | return -ETIMEDOUT; |
| 287 | |
| 288 | if (stv0299_writeregI (state, 0x09, m->msg[i])) |
| 289 | return -EREMOTEIO; |
| 290 | } |
| 291 | |
| 292 | if (stv0299_wait_diseqc_idle (state, 100) < 0) |
| 293 | return -ETIMEDOUT; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int stv0299_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst) |
| 299 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 300 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | u8 val; |
| 302 | |
| 303 | dprintk ("%s\n", __FUNCTION__); |
| 304 | |
| 305 | if (stv0299_wait_diseqc_idle (state, 100) < 0) |
| 306 | return -ETIMEDOUT; |
| 307 | |
| 308 | val = stv0299_readreg (state, 0x08); |
| 309 | |
| 310 | if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2)) /* burst mode */ |
| 311 | return -EREMOTEIO; |
| 312 | |
| 313 | if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff)) |
| 314 | return -EREMOTEIO; |
| 315 | |
| 316 | if (stv0299_wait_diseqc_idle (state, 100) < 0) |
| 317 | return -ETIMEDOUT; |
| 318 | |
| 319 | if (stv0299_writeregI (state, 0x08, val)) |
| 320 | return -EREMOTEIO; |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static int stv0299_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone) |
| 326 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 327 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | u8 val; |
| 329 | |
| 330 | if (stv0299_wait_diseqc_idle (state, 100) < 0) |
| 331 | return -ETIMEDOUT; |
| 332 | |
| 333 | val = stv0299_readreg (state, 0x08); |
| 334 | |
| 335 | switch (tone) { |
| 336 | case SEC_TONE_ON: |
| 337 | return stv0299_writeregI (state, 0x08, val | 0x3); |
| 338 | |
| 339 | case SEC_TONE_OFF: |
| 340 | return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02); |
| 341 | |
| 342 | default: |
| 343 | return -EINVAL; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | static int stv0299_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage) |
| 348 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 349 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | u8 reg0x08; |
| 351 | u8 reg0x0c; |
| 352 | |
| 353 | dprintk("%s: %s\n", __FUNCTION__, |
| 354 | voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" : |
| 355 | voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??"); |
| 356 | |
| 357 | reg0x08 = stv0299_readreg (state, 0x08); |
| 358 | reg0x0c = stv0299_readreg (state, 0x0c); |
| 359 | |
| 360 | /** |
| 361 | * H/V switching over OP0, OP1 and OP2 are LNB power enable bits |
| 362 | */ |
| 363 | reg0x0c &= 0x0f; |
| 364 | |
| 365 | if (voltage == SEC_VOLTAGE_OFF) { |
| 366 | stv0299_writeregI (state, 0x0c, 0x00); /* LNB power off! */ |
| 367 | return stv0299_writeregI (state, 0x08, 0x00); /* LNB power off! */ |
| 368 | } |
| 369 | |
| 370 | stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6)); |
| 371 | |
| 372 | switch (voltage) { |
| 373 | case SEC_VOLTAGE_13: |
| 374 | if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0) reg0x0c |= 0x10; |
| 375 | else reg0x0c |= 0x40; |
| 376 | |
| 377 | return stv0299_writeregI(state, 0x0c, reg0x0c); |
| 378 | |
| 379 | case SEC_VOLTAGE_18: |
| 380 | return stv0299_writeregI(state, 0x0c, reg0x0c | 0x50); |
| 381 | default: |
| 382 | return -EINVAL; |
| 383 | }; |
| 384 | } |
| 385 | |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 386 | static inline s32 stv0299_calc_usec_delay (struct timeval lasttime, struct timeval curtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 388 | return ((curtime.tv_usec < lasttime.tv_usec) ? |
| 389 | 1000000 - lasttime.tv_usec + curtime.tv_usec : |
| 390 | curtime.tv_usec - lasttime.tv_usec); |
| 391 | } |
| 392 | |
| 393 | static void stv0299_sleep_until (struct timeval *waketime, u32 add_usec) |
| 394 | { |
| 395 | struct timeval lasttime; |
| 396 | s32 delta, newdelta; |
| 397 | |
| 398 | waketime->tv_usec += add_usec; |
| 399 | if (waketime->tv_usec >= 1000000) { |
| 400 | waketime->tv_usec -= 1000000; |
| 401 | waketime->tv_sec++; |
| 402 | } |
| 403 | |
| 404 | do_gettimeofday (&lasttime); |
| 405 | delta = stv0299_calc_usec_delay (lasttime, *waketime); |
| 406 | if (delta > 2500) { |
| 407 | msleep ((delta - 1500) / 1000); |
| 408 | do_gettimeofday (&lasttime); |
| 409 | newdelta = stv0299_calc_usec_delay (lasttime, *waketime); |
| 410 | delta = (newdelta > delta) ? 0 : newdelta; |
| 411 | } |
| 412 | if (delta > 0) |
| 413 | udelay (delta); |
| 414 | } |
| 415 | |
| 416 | static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, u32 cmd) |
| 417 | { |
| 418 | struct stv0299_state* state = fe->demodulator_priv; |
| 419 | u8 reg0x08; |
| 420 | u8 reg0x0c; |
| 421 | u8 lv_mask = 0x40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | u8 last = 1; |
| 423 | int i; |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 424 | struct timeval nexttime; |
| 425 | struct timeval tv[10]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 427 | reg0x08 = stv0299_readreg (state, 0x08); |
| 428 | reg0x0c = stv0299_readreg (state, 0x0c); |
| 429 | reg0x0c &= 0x0f; |
| 430 | stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6)); |
| 431 | if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0) |
| 432 | lv_mask = 0x10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | cmd = cmd << 1; |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 435 | if (debug_legacy_dish_switch) |
| 436 | printk ("%s switch command: 0x%04x\n",__FUNCTION__, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 438 | do_gettimeofday (&nexttime); |
| 439 | if (debug_legacy_dish_switch) |
| 440 | memcpy (&tv[0], &nexttime, sizeof (struct timeval)); |
| 441 | stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */ |
| 442 | |
| 443 | stv0299_sleep_until (&nexttime, 32000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
| 445 | for (i=0; i<9; i++) { |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 446 | if (debug_legacy_dish_switch) |
| 447 | do_gettimeofday (&tv[i+1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | if((cmd & 0x01) != last) { |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 449 | /* set voltage to (last ? 13V : 18V) */ |
| 450 | stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | last = (last) ? 0 : 1; |
| 452 | } |
| 453 | |
| 454 | cmd = cmd >> 1; |
| 455 | |
| 456 | if (i != 8) |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 457 | stv0299_sleep_until (&nexttime, 8000); |
| 458 | } |
| 459 | if (debug_legacy_dish_switch) { |
| 460 | printk ("%s(%d): switch delay (should be 32k followed by all 8k\n", |
| 461 | __FUNCTION__, fe->dvb->num); |
| 462 | for (i=1; i < 10; i++) |
| 463 | printk ("%d: %d\n", i, stv0299_calc_usec_delay (tv[i-1] , tv[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int stv0299_init (struct dvb_frontend* fe) |
| 470 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 471 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | int i; |
| 473 | |
| 474 | dprintk("stv0299: init chip\n"); |
| 475 | |
| 476 | for (i=0; !(state->config->inittab[i] == 0xff && state->config->inittab[i+1] == 0xff); i+=2) |
| 477 | stv0299_writeregI(state, state->config->inittab[i], state->config->inittab[i+1]); |
| 478 | |
| 479 | if (state->config->pll_init) { |
| 480 | stv0299_writeregI(state, 0x05, 0xb5); /* enable i2c repeater on stv0299 */ |
Andreas Oberritter | cfbfce1 | 2005-09-09 13:02:30 -0700 | [diff] [blame] | 481 | state->config->pll_init(fe, state->i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | stv0299_writeregI(state, 0x05, 0x35); /* disable i2c repeater on stv0299 */ |
| 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static int stv0299_read_status(struct dvb_frontend* fe, fe_status_t* status) |
| 489 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 490 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | u8 signal = 0xff - stv0299_readreg (state, 0x18); |
| 493 | u8 sync = stv0299_readreg (state, 0x1b); |
| 494 | |
| 495 | dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __FUNCTION__, sync); |
| 496 | *status = 0; |
| 497 | |
| 498 | if (signal > 10) |
| 499 | *status |= FE_HAS_SIGNAL; |
| 500 | |
| 501 | if (sync & 0x80) |
| 502 | *status |= FE_HAS_CARRIER; |
| 503 | |
| 504 | if (sync & 0x10) |
| 505 | *status |= FE_HAS_VITERBI; |
| 506 | |
| 507 | if (sync & 0x08) |
| 508 | *status |= FE_HAS_SYNC; |
| 509 | |
| 510 | if ((sync & 0x98) == 0x98) |
| 511 | *status |= FE_HAS_LOCK; |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber) |
| 517 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 518 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Andrew de Quincey | 78639a3 | 2005-09-09 13:02:32 -0700 | [diff] [blame] | 520 | stv0299_writeregI(state, 0x34, (stv0299_readreg(state, 0x34) & 0xcf) | 0x10); |
| 521 | msleep(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | *ber = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e); |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength) |
| 528 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 529 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
| 531 | s32 signal = 0xffff - ((stv0299_readreg (state, 0x18) << 8) |
| 532 | | stv0299_readreg (state, 0x19)); |
| 533 | |
| 534 | dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __FUNCTION__, |
| 535 | stv0299_readreg (state, 0x18), |
| 536 | stv0299_readreg (state, 0x19), (int) signal); |
| 537 | |
| 538 | signal = signal * 5 / 4; |
| 539 | *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal; |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr) |
| 545 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 546 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8) |
| 549 | | stv0299_readreg (state, 0x25)); |
| 550 | xsnr = 3 * (xsnr - 0xa100); |
| 551 | *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr; |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks) |
| 557 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 558 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
Andrew de Quincey | 78639a3 | 2005-09-09 13:02:32 -0700 | [diff] [blame] | 560 | stv0299_writeregI(state, 0x34, (stv0299_readreg(state, 0x34) & 0xcf) | 0x30); |
| 561 | msleep(100); |
| 562 | *ucblocks = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static int stv0299_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p) |
| 568 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 569 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | int invval = 0; |
| 571 | |
| 572 | dprintk ("%s : FE_SET_FRONTEND\n", __FUNCTION__); |
| 573 | |
| 574 | // set the inversion |
| 575 | if (p->inversion == INVERSION_OFF) invval = 0; |
| 576 | else if (p->inversion == INVERSION_ON) invval = 1; |
| 577 | else { |
| 578 | printk("stv0299 does not support auto-inversion\n"); |
| 579 | return -EINVAL; |
| 580 | } |
| 581 | if (state->config->invert) invval = (~invval) & 1; |
| 582 | stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval); |
| 583 | |
| 584 | if (state->config->enhanced_tuning) { |
| 585 | /* check if we should do a finetune */ |
| 586 | int frequency_delta = p->frequency - state->tuner_frequency; |
| 587 | int minmax = p->u.qpsk.symbol_rate / 2000; |
| 588 | if (minmax < 5000) minmax = 5000; |
| 589 | |
| 590 | if ((frequency_delta > -minmax) && (frequency_delta < minmax) && (frequency_delta != 0) && |
| 591 | (state->fec_inner == p->u.qpsk.fec_inner) && |
| 592 | (state->symbol_rate == p->u.qpsk.symbol_rate)) { |
| 593 | int Drot_freq = (frequency_delta << 16) / (state->config->mclk / 1000); |
| 594 | |
| 595 | // zap the derotator registers first |
| 596 | stv0299_writeregI(state, 0x22, 0x00); |
| 597 | stv0299_writeregI(state, 0x23, 0x00); |
| 598 | |
| 599 | // now set them as we want |
| 600 | stv0299_writeregI(state, 0x22, Drot_freq >> 8); |
| 601 | stv0299_writeregI(state, 0x23, Drot_freq); |
| 602 | } else { |
| 603 | /* A "normal" tune is requested */ |
| 604 | stv0299_writeregI(state, 0x05, 0xb5); /* enable i2c repeater on stv0299 */ |
Andreas Oberritter | cfbfce1 | 2005-09-09 13:02:30 -0700 | [diff] [blame] | 605 | state->config->pll_set(fe, state->i2c, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | stv0299_writeregI(state, 0x05, 0x35); /* disable i2c repeater on stv0299 */ |
| 607 | |
| 608 | stv0299_writeregI(state, 0x32, 0x80); |
| 609 | stv0299_writeregI(state, 0x22, 0x00); |
| 610 | stv0299_writeregI(state, 0x23, 0x00); |
| 611 | stv0299_writeregI(state, 0x32, 0x19); |
| 612 | stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate); |
| 613 | stv0299_set_FEC (state, p->u.qpsk.fec_inner); |
| 614 | } |
| 615 | } else { |
| 616 | stv0299_writeregI(state, 0x05, 0xb5); /* enable i2c repeater on stv0299 */ |
Andreas Oberritter | cfbfce1 | 2005-09-09 13:02:30 -0700 | [diff] [blame] | 617 | state->config->pll_set(fe, state->i2c, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | stv0299_writeregI(state, 0x05, 0x35); /* disable i2c repeater on stv0299 */ |
| 619 | |
| 620 | stv0299_set_FEC (state, p->u.qpsk.fec_inner); |
| 621 | stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate); |
| 622 | stv0299_writeregI(state, 0x22, 0x00); |
| 623 | stv0299_writeregI(state, 0x23, 0x00); |
| 624 | stv0299_readreg (state, 0x23); |
| 625 | stv0299_writeregI(state, 0x12, 0xb9); |
| 626 | } |
| 627 | |
| 628 | state->tuner_frequency = p->frequency; |
| 629 | state->fec_inner = p->u.qpsk.fec_inner; |
| 630 | state->symbol_rate = p->u.qpsk.symbol_rate; |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static int stv0299_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p) |
| 636 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 637 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | s32 derot_freq; |
| 639 | int invval; |
| 640 | |
| 641 | derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8) |
| 642 | | stv0299_readreg (state, 0x23)); |
| 643 | |
| 644 | derot_freq *= (state->config->mclk >> 16); |
| 645 | derot_freq += 500; |
| 646 | derot_freq /= 1000; |
| 647 | |
| 648 | p->frequency += derot_freq; |
| 649 | |
| 650 | invval = stv0299_readreg (state, 0x0c) & 1; |
| 651 | if (state->config->invert) invval = (~invval) & 1; |
| 652 | p->inversion = invval ? INVERSION_ON : INVERSION_OFF; |
| 653 | |
| 654 | p->u.qpsk.fec_inner = stv0299_get_fec (state); |
| 655 | p->u.qpsk.symbol_rate = stv0299_get_symbolrate (state); |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static int stv0299_sleep(struct dvb_frontend* fe) |
| 661 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 662 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | stv0299_writeregI(state, 0x02, 0x80); |
| 665 | state->initialised = 0; |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings) |
| 671 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 672 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
| 674 | fesettings->min_delay_ms = state->config->min_delay_ms; |
| 675 | if (fesettings->parameters.u.qpsk.symbol_rate < 10000000) { |
| 676 | fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 32000; |
| 677 | fesettings->max_drift = 5000; |
| 678 | } else { |
| 679 | fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 16000; |
| 680 | fesettings->max_drift = fesettings->parameters.u.qpsk.symbol_rate / 2000; |
| 681 | } |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | static void stv0299_release(struct dvb_frontend* fe) |
| 686 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 687 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | kfree(state); |
| 689 | } |
| 690 | |
| 691 | static struct dvb_frontend_ops stv0299_ops; |
| 692 | |
| 693 | struct dvb_frontend* stv0299_attach(const struct stv0299_config* config, |
| 694 | struct i2c_adapter* i2c) |
| 695 | { |
| 696 | struct stv0299_state* state = NULL; |
| 697 | int id; |
| 698 | |
| 699 | /* allocate memory for the internal state */ |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 700 | state = kmalloc(sizeof(struct stv0299_state), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | if (state == NULL) goto error; |
| 702 | |
| 703 | /* setup the state */ |
| 704 | state->config = config; |
| 705 | state->i2c = i2c; |
| 706 | memcpy(&state->ops, &stv0299_ops, sizeof(struct dvb_frontend_ops)); |
| 707 | state->initialised = 0; |
| 708 | state->tuner_frequency = 0; |
| 709 | state->symbol_rate = 0; |
| 710 | state->fec_inner = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | /* check if the demod is there */ |
| 713 | stv0299_writeregI(state, 0x02, 0x34); /* standby off */ |
| 714 | msleep(200); |
| 715 | id = stv0299_readreg(state, 0x00); |
| 716 | |
| 717 | /* register 0x00 contains 0xa1 for STV0299 and STV0299B */ |
| 718 | /* register 0x00 might contain 0x80 when returning from standby */ |
| 719 | if (id != 0xa1 && id != 0x80) goto error; |
| 720 | |
| 721 | /* create dvb_frontend */ |
| 722 | state->frontend.ops = &state->ops; |
| 723 | state->frontend.demodulator_priv = state; |
| 724 | return &state->frontend; |
| 725 | |
| 726 | error: |
| 727 | kfree(state); |
| 728 | return NULL; |
| 729 | } |
| 730 | |
| 731 | static struct dvb_frontend_ops stv0299_ops = { |
| 732 | |
| 733 | .info = { |
| 734 | .name = "ST STV0299 DVB-S", |
| 735 | .type = FE_QPSK, |
| 736 | .frequency_min = 950000, |
| 737 | .frequency_max = 2150000, |
| 738 | .frequency_stepsize = 125, /* kHz for QPSK frontends */ |
| 739 | .frequency_tolerance = 0, |
| 740 | .symbol_rate_min = 1000000, |
| 741 | .symbol_rate_max = 45000000, |
| 742 | .symbol_rate_tolerance = 500, /* ppm */ |
| 743 | .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | |
| 744 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | |
| 745 | FE_CAN_QPSK | |
| 746 | FE_CAN_FEC_AUTO |
| 747 | }, |
| 748 | |
| 749 | .release = stv0299_release, |
| 750 | |
| 751 | .init = stv0299_init, |
| 752 | .sleep = stv0299_sleep, |
| 753 | |
| 754 | .set_frontend = stv0299_set_frontend, |
| 755 | .get_frontend = stv0299_get_frontend, |
| 756 | .get_tune_settings = stv0299_get_tune_settings, |
| 757 | |
| 758 | .read_status = stv0299_read_status, |
| 759 | .read_ber = stv0299_read_ber, |
| 760 | .read_signal_strength = stv0299_read_signal_strength, |
| 761 | .read_snr = stv0299_read_snr, |
| 762 | .read_ucblocks = stv0299_read_ucblocks, |
| 763 | |
| 764 | .diseqc_send_master_cmd = stv0299_send_diseqc_msg, |
| 765 | .diseqc_send_burst = stv0299_send_diseqc_burst, |
| 766 | .set_tone = stv0299_set_tone, |
| 767 | .set_voltage = stv0299_set_voltage, |
| 768 | .dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd, |
| 769 | }; |
| 770 | |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 771 | module_param(debug_legacy_dish_switch, int, 0444); |
| 772 | MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches"); |
| 773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | module_param(debug, int, 0644); |
| 775 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); |
| 776 | |
| 777 | MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver"); |
| 778 | MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, " |
| 779 | "Andreas Oberritter, Andrew de Quincey, Kenneth Aafløy"); |
| 780 | MODULE_LICENSE("GPL"); |
| 781 | |
| 782 | EXPORT_SYMBOL(stv0299_writereg); |
| 783 | EXPORT_SYMBOL(stv0299_attach); |