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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | const struct stv0299_config* config; |
| 60 | struct dvb_frontend frontend; |
| 61 | |
| 62 | u8 initialised:1; |
| 63 | u32 tuner_frequency; |
| 64 | u32 symbol_rate; |
| 65 | fe_code_rate_t fec_inner; |
Andrew de Quincey | 3765022 | 2005-11-08 21:35:11 -0800 | [diff] [blame] | 66 | int errmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Andrew de Quincey | 3765022 | 2005-11-08 21:35:11 -0800 | [diff] [blame] | 69 | #define STATUS_BER 0 |
| 70 | #define STATUS_UCBLOCKS 1 |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | static int debug; |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 73 | static int debug_legacy_dish_switch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #define dprintk(args...) \ |
| 75 | do { \ |
| 76 | if (debug) printk(KERN_DEBUG "stv0299: " args); \ |
| 77 | } while (0) |
| 78 | |
| 79 | |
| 80 | static int stv0299_writeregI (struct stv0299_state* state, u8 reg, u8 data) |
| 81 | { |
| 82 | int ret; |
| 83 | u8 buf [] = { reg, data }; |
| 84 | struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 }; |
| 85 | |
| 86 | ret = i2c_transfer (state->i2c, &msg, 1); |
| 87 | |
| 88 | if (ret != 1) |
| 89 | dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, " |
| 90 | "ret == %i)\n", __FUNCTION__, reg, data, ret); |
| 91 | |
| 92 | return (ret != 1) ? -EREMOTEIO : 0; |
| 93 | } |
| 94 | |
| 95 | int stv0299_writereg (struct dvb_frontend* fe, u8 reg, u8 data) |
| 96 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 97 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | return stv0299_writeregI(state, reg, data); |
| 100 | } |
| 101 | |
| 102 | static u8 stv0299_readreg (struct stv0299_state* state, u8 reg) |
| 103 | { |
| 104 | int ret; |
| 105 | u8 b0 [] = { reg }; |
| 106 | u8 b1 [] = { 0 }; |
| 107 | struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 }, |
| 108 | { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } }; |
| 109 | |
| 110 | ret = i2c_transfer (state->i2c, msg, 2); |
| 111 | |
| 112 | if (ret != 2) |
| 113 | dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", |
| 114 | __FUNCTION__, reg, ret); |
| 115 | |
| 116 | return b1[0]; |
| 117 | } |
| 118 | |
| 119 | static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len) |
| 120 | { |
| 121 | int ret; |
| 122 | struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = ®1, .len = 1 }, |
| 123 | { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } }; |
| 124 | |
| 125 | ret = i2c_transfer (state->i2c, msg, 2); |
| 126 | |
| 127 | if (ret != 2) |
| 128 | dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret); |
| 129 | |
| 130 | return ret == 2 ? 0 : ret; |
| 131 | } |
| 132 | |
| 133 | static int stv0299_set_FEC (struct stv0299_state* state, fe_code_rate_t fec) |
| 134 | { |
| 135 | dprintk ("%s\n", __FUNCTION__); |
| 136 | |
| 137 | switch (fec) { |
| 138 | case FEC_AUTO: |
| 139 | { |
| 140 | return stv0299_writeregI (state, 0x31, 0x1f); |
| 141 | } |
| 142 | case FEC_1_2: |
| 143 | { |
| 144 | return stv0299_writeregI (state, 0x31, 0x01); |
| 145 | } |
| 146 | case FEC_2_3: |
| 147 | { |
| 148 | return stv0299_writeregI (state, 0x31, 0x02); |
| 149 | } |
| 150 | case FEC_3_4: |
| 151 | { |
| 152 | return stv0299_writeregI (state, 0x31, 0x04); |
| 153 | } |
| 154 | case FEC_5_6: |
| 155 | { |
| 156 | return stv0299_writeregI (state, 0x31, 0x08); |
| 157 | } |
| 158 | case FEC_7_8: |
| 159 | { |
| 160 | return stv0299_writeregI (state, 0x31, 0x10); |
| 161 | } |
| 162 | default: |
| 163 | { |
| 164 | return -EINVAL; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static fe_code_rate_t stv0299_get_fec (struct stv0299_state* state) |
| 170 | { |
| 171 | static fe_code_rate_t fec_tab [] = { FEC_2_3, FEC_3_4, FEC_5_6, |
| 172 | FEC_7_8, FEC_1_2 }; |
| 173 | u8 index; |
| 174 | |
| 175 | dprintk ("%s\n", __FUNCTION__); |
| 176 | |
| 177 | index = stv0299_readreg (state, 0x1b); |
| 178 | index &= 0x7; |
| 179 | |
| 180 | if (index > 4) |
| 181 | return FEC_AUTO; |
| 182 | |
| 183 | return fec_tab [index]; |
| 184 | } |
| 185 | |
| 186 | static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout) |
| 187 | { |
| 188 | unsigned long start = jiffies; |
| 189 | |
| 190 | dprintk ("%s\n", __FUNCTION__); |
| 191 | |
| 192 | while (stv0299_readreg(state, 0x0a) & 1) { |
| 193 | if (jiffies - start > timeout) { |
| 194 | dprintk ("%s: timeout!!\n", __FUNCTION__); |
| 195 | return -ETIMEDOUT; |
| 196 | } |
| 197 | msleep(10); |
| 198 | }; |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout) |
| 204 | { |
| 205 | unsigned long start = jiffies; |
| 206 | |
| 207 | dprintk ("%s\n", __FUNCTION__); |
| 208 | |
| 209 | while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) { |
| 210 | if (jiffies - start > timeout) { |
| 211 | dprintk ("%s: timeout!!\n", __FUNCTION__); |
| 212 | return -ETIMEDOUT; |
| 213 | } |
| 214 | msleep(10); |
| 215 | }; |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate) |
| 221 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 222 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | u64 big = srate; |
| 224 | u32 ratio; |
| 225 | |
| 226 | // check rate is within limits |
| 227 | if ((srate < 1000000) || (srate > 45000000)) return -EINVAL; |
| 228 | |
| 229 | // calculate value to program |
| 230 | big = big << 20; |
| 231 | big += (state->config->mclk-1); // round correctly |
| 232 | do_div(big, state->config->mclk); |
| 233 | ratio = big << 4; |
| 234 | |
| 235 | return state->config->set_symbol_rate(fe, srate, ratio); |
| 236 | } |
| 237 | |
| 238 | static int stv0299_get_symbolrate (struct stv0299_state* state) |
| 239 | { |
| 240 | u32 Mclk = state->config->mclk / 4096L; |
| 241 | u32 srate; |
| 242 | s32 offset; |
| 243 | u8 sfr[3]; |
| 244 | s8 rtf; |
| 245 | |
| 246 | dprintk ("%s\n", __FUNCTION__); |
| 247 | |
| 248 | stv0299_readregs (state, 0x1f, sfr, 3); |
| 249 | stv0299_readregs (state, 0x1a, &rtf, 1); |
| 250 | |
| 251 | srate = (sfr[0] << 8) | sfr[1]; |
| 252 | srate *= Mclk; |
| 253 | srate /= 16; |
| 254 | srate += (sfr[2] >> 4) * Mclk / 256; |
| 255 | offset = (s32) rtf * (srate / 4096L); |
| 256 | offset /= 128; |
| 257 | |
| 258 | dprintk ("%s : srate = %i\n", __FUNCTION__, srate); |
| 259 | dprintk ("%s : ofset = %i\n", __FUNCTION__, offset); |
| 260 | |
| 261 | srate += offset; |
| 262 | |
| 263 | srate += 1000; |
| 264 | srate /= 2000; |
| 265 | srate *= 2000; |
| 266 | |
| 267 | return srate; |
| 268 | } |
| 269 | |
| 270 | static int stv0299_send_diseqc_msg (struct dvb_frontend* fe, |
| 271 | struct dvb_diseqc_master_cmd *m) |
| 272 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 273 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | u8 val; |
| 275 | int i; |
| 276 | |
| 277 | dprintk ("%s\n", __FUNCTION__); |
| 278 | |
| 279 | if (stv0299_wait_diseqc_idle (state, 100) < 0) |
| 280 | return -ETIMEDOUT; |
| 281 | |
| 282 | val = stv0299_readreg (state, 0x08); |
| 283 | |
| 284 | if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6)) /* DiSEqC mode */ |
| 285 | return -EREMOTEIO; |
| 286 | |
| 287 | for (i=0; i<m->msg_len; i++) { |
| 288 | if (stv0299_wait_diseqc_fifo (state, 100) < 0) |
| 289 | return -ETIMEDOUT; |
| 290 | |
| 291 | if (stv0299_writeregI (state, 0x09, m->msg[i])) |
| 292 | return -EREMOTEIO; |
| 293 | } |
| 294 | |
| 295 | if (stv0299_wait_diseqc_idle (state, 100) < 0) |
| 296 | return -ETIMEDOUT; |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int stv0299_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst) |
| 302 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 303 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | u8 val; |
| 305 | |
| 306 | dprintk ("%s\n", __FUNCTION__); |
| 307 | |
| 308 | if (stv0299_wait_diseqc_idle (state, 100) < 0) |
| 309 | return -ETIMEDOUT; |
| 310 | |
| 311 | val = stv0299_readreg (state, 0x08); |
| 312 | |
| 313 | if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2)) /* burst mode */ |
| 314 | return -EREMOTEIO; |
| 315 | |
| 316 | if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff)) |
| 317 | return -EREMOTEIO; |
| 318 | |
| 319 | if (stv0299_wait_diseqc_idle (state, 100) < 0) |
| 320 | return -ETIMEDOUT; |
| 321 | |
| 322 | if (stv0299_writeregI (state, 0x08, val)) |
| 323 | return -EREMOTEIO; |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int stv0299_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone) |
| 329 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 330 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | u8 val; |
| 332 | |
| 333 | if (stv0299_wait_diseqc_idle (state, 100) < 0) |
| 334 | return -ETIMEDOUT; |
| 335 | |
| 336 | val = stv0299_readreg (state, 0x08); |
| 337 | |
| 338 | switch (tone) { |
| 339 | case SEC_TONE_ON: |
| 340 | return stv0299_writeregI (state, 0x08, val | 0x3); |
| 341 | |
| 342 | case SEC_TONE_OFF: |
| 343 | return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02); |
| 344 | |
| 345 | default: |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | static int stv0299_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage) |
| 351 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 352 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | u8 reg0x08; |
| 354 | u8 reg0x0c; |
| 355 | |
| 356 | dprintk("%s: %s\n", __FUNCTION__, |
| 357 | voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" : |
| 358 | voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??"); |
| 359 | |
| 360 | reg0x08 = stv0299_readreg (state, 0x08); |
| 361 | reg0x0c = stv0299_readreg (state, 0x0c); |
| 362 | |
| 363 | /** |
| 364 | * H/V switching over OP0, OP1 and OP2 are LNB power enable bits |
| 365 | */ |
| 366 | reg0x0c &= 0x0f; |
| 367 | |
| 368 | if (voltage == SEC_VOLTAGE_OFF) { |
| 369 | stv0299_writeregI (state, 0x0c, 0x00); /* LNB power off! */ |
| 370 | return stv0299_writeregI (state, 0x08, 0x00); /* LNB power off! */ |
| 371 | } |
| 372 | |
| 373 | stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6)); |
| 374 | |
| 375 | switch (voltage) { |
| 376 | case SEC_VOLTAGE_13: |
| 377 | if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0) reg0x0c |= 0x10; |
| 378 | else reg0x0c |= 0x40; |
| 379 | |
| 380 | return stv0299_writeregI(state, 0x0c, reg0x0c); |
| 381 | |
| 382 | case SEC_VOLTAGE_18: |
| 383 | return stv0299_writeregI(state, 0x0c, reg0x0c | 0x50); |
| 384 | default: |
| 385 | return -EINVAL; |
| 386 | }; |
| 387 | } |
| 388 | |
Peter Beutner | 400b708 | 2006-01-09 15:32:43 -0200 | [diff] [blame] | 389 | static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long cmd) |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 390 | { |
| 391 | struct stv0299_state* state = fe->demodulator_priv; |
| 392 | u8 reg0x08; |
| 393 | u8 reg0x0c; |
| 394 | u8 lv_mask = 0x40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | u8 last = 1; |
| 396 | int i; |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 397 | struct timeval nexttime; |
| 398 | struct timeval tv[10]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 400 | reg0x08 = stv0299_readreg (state, 0x08); |
| 401 | reg0x0c = stv0299_readreg (state, 0x0c); |
| 402 | reg0x0c &= 0x0f; |
| 403 | stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6)); |
| 404 | if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0) |
| 405 | lv_mask = 0x10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | cmd = cmd << 1; |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 408 | if (debug_legacy_dish_switch) |
Peter Beutner | 400b708 | 2006-01-09 15:32:43 -0200 | [diff] [blame] | 409 | printk ("%s switch command: 0x%04lx\n",__FUNCTION__, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 411 | do_gettimeofday (&nexttime); |
| 412 | if (debug_legacy_dish_switch) |
| 413 | memcpy (&tv[0], &nexttime, sizeof (struct timeval)); |
| 414 | stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */ |
| 415 | |
NooneImportant | 83b75b0 | 2005-11-08 21:35:27 -0800 | [diff] [blame] | 416 | dvb_frontend_sleep_until(&nexttime, 32000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
| 418 | for (i=0; i<9; i++) { |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 419 | if (debug_legacy_dish_switch) |
| 420 | do_gettimeofday (&tv[i+1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | if((cmd & 0x01) != last) { |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 422 | /* set voltage to (last ? 13V : 18V) */ |
| 423 | stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | last = (last) ? 0 : 1; |
| 425 | } |
| 426 | |
| 427 | cmd = cmd >> 1; |
| 428 | |
| 429 | if (i != 8) |
NooneImportant | 83b75b0 | 2005-11-08 21:35:27 -0800 | [diff] [blame] | 430 | dvb_frontend_sleep_until(&nexttime, 8000); |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 431 | } |
| 432 | if (debug_legacy_dish_switch) { |
| 433 | printk ("%s(%d): switch delay (should be 32k followed by all 8k\n", |
| 434 | __FUNCTION__, fe->dvb->num); |
NooneImportant | 83b75b0 | 2005-11-08 21:35:27 -0800 | [diff] [blame] | 435 | for (i = 1; i < 10; i++) |
| 436 | printk ("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int stv0299_init (struct dvb_frontend* fe) |
| 443 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 444 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | int i; |
| 446 | |
| 447 | dprintk("stv0299: init chip\n"); |
| 448 | |
| 449 | for (i=0; !(state->config->inittab[i] == 0xff && state->config->inittab[i+1] == 0xff); i+=2) |
| 450 | stv0299_writeregI(state, state->config->inittab[i], state->config->inittab[i+1]); |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int stv0299_read_status(struct dvb_frontend* fe, fe_status_t* status) |
| 456 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 457 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | u8 signal = 0xff - stv0299_readreg (state, 0x18); |
| 460 | u8 sync = stv0299_readreg (state, 0x1b); |
| 461 | |
| 462 | dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __FUNCTION__, sync); |
| 463 | *status = 0; |
| 464 | |
| 465 | if (signal > 10) |
| 466 | *status |= FE_HAS_SIGNAL; |
| 467 | |
| 468 | if (sync & 0x80) |
| 469 | *status |= FE_HAS_CARRIER; |
| 470 | |
| 471 | if (sync & 0x10) |
| 472 | *status |= FE_HAS_VITERBI; |
| 473 | |
| 474 | if (sync & 0x08) |
| 475 | *status |= FE_HAS_SYNC; |
| 476 | |
| 477 | if ((sync & 0x98) == 0x98) |
| 478 | *status |= FE_HAS_LOCK; |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber) |
| 484 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 485 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Andrew de Quincey | 3765022 | 2005-11-08 21:35:11 -0800 | [diff] [blame] | 487 | if (state->errmode != STATUS_BER) return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | *ber = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e); |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength) |
| 494 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 495 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | s32 signal = 0xffff - ((stv0299_readreg (state, 0x18) << 8) |
| 498 | | stv0299_readreg (state, 0x19)); |
| 499 | |
| 500 | dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __FUNCTION__, |
| 501 | stv0299_readreg (state, 0x18), |
| 502 | stv0299_readreg (state, 0x19), (int) signal); |
| 503 | |
| 504 | signal = signal * 5 / 4; |
| 505 | *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal; |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr) |
| 511 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 512 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8) |
| 515 | | stv0299_readreg (state, 0x25)); |
| 516 | xsnr = 3 * (xsnr - 0xa100); |
| 517 | *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr; |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks) |
| 523 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 524 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Andrew de Quincey | 3765022 | 2005-11-08 21:35:11 -0800 | [diff] [blame] | 526 | if (state->errmode != STATUS_UCBLOCKS) *ucblocks = 0; |
| 527 | else *ucblocks = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int stv0299_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p) |
| 533 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 534 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | int invval = 0; |
| 536 | |
| 537 | dprintk ("%s : FE_SET_FRONTEND\n", __FUNCTION__); |
| 538 | |
| 539 | // set the inversion |
| 540 | if (p->inversion == INVERSION_OFF) invval = 0; |
| 541 | else if (p->inversion == INVERSION_ON) invval = 1; |
| 542 | else { |
| 543 | printk("stv0299 does not support auto-inversion\n"); |
| 544 | return -EINVAL; |
| 545 | } |
| 546 | if (state->config->invert) invval = (~invval) & 1; |
| 547 | stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval); |
| 548 | |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 549 | if (fe->ops.tuner_ops.set_params) { |
| 550 | fe->ops.tuner_ops.set_params(fe, p); |
| 551 | if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0); |
Andrew de Quincey | 53a8ee3 | 2006-04-18 17:47:10 -0300 | [diff] [blame] | 552 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
Andrew de Quincy | 3528cc4e | 2005-11-08 21:35:28 -0800 | [diff] [blame] | 554 | stv0299_set_FEC (state, p->u.qpsk.fec_inner); |
| 555 | stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate); |
| 556 | stv0299_writeregI(state, 0x22, 0x00); |
| 557 | stv0299_writeregI(state, 0x23, 0x00); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | state->tuner_frequency = p->frequency; |
| 560 | state->fec_inner = p->u.qpsk.fec_inner; |
| 561 | state->symbol_rate = p->u.qpsk.symbol_rate; |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static int stv0299_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p) |
| 567 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 568 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | s32 derot_freq; |
| 570 | int invval; |
| 571 | |
| 572 | derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8) |
| 573 | | stv0299_readreg (state, 0x23)); |
| 574 | |
| 575 | derot_freq *= (state->config->mclk >> 16); |
| 576 | derot_freq += 500; |
| 577 | derot_freq /= 1000; |
| 578 | |
| 579 | p->frequency += derot_freq; |
| 580 | |
| 581 | invval = stv0299_readreg (state, 0x0c) & 1; |
| 582 | if (state->config->invert) invval = (~invval) & 1; |
| 583 | p->inversion = invval ? INVERSION_ON : INVERSION_OFF; |
| 584 | |
| 585 | p->u.qpsk.fec_inner = stv0299_get_fec (state); |
| 586 | p->u.qpsk.symbol_rate = stv0299_get_symbolrate (state); |
| 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static int stv0299_sleep(struct dvb_frontend* fe) |
| 592 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 593 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
| 595 | stv0299_writeregI(state, 0x02, 0x80); |
| 596 | state->initialised = 0; |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Andrew de Quincey | 53a8ee3 | 2006-04-18 17:47:10 -0300 | [diff] [blame] | 601 | static int stv0299_i2c_gate_ctrl(struct dvb_frontend* fe, int enable) |
| 602 | { |
| 603 | struct stv0299_state* state = fe->demodulator_priv; |
| 604 | |
| 605 | if (enable) { |
Andrew de Quincey | a9686e0 | 2006-05-22 10:31:40 -0300 | [diff] [blame] | 606 | stv0299_writeregI(state, 0x05, 0xb5); |
Andrew de Quincey | 53a8ee3 | 2006-04-18 17:47:10 -0300 | [diff] [blame] | 607 | } else { |
Andrew de Quincey | a9686e0 | 2006-05-22 10:31:40 -0300 | [diff] [blame] | 608 | stv0299_writeregI(state, 0x05, 0x35); |
Andrew de Quincey | 53a8ee3 | 2006-04-18 17:47:10 -0300 | [diff] [blame] | 609 | } |
Andrew de Quincey | a9686e0 | 2006-05-22 10:31:40 -0300 | [diff] [blame] | 610 | udelay(1); |
| 611 | return 0; |
Andrew de Quincey | 53a8ee3 | 2006-04-18 17:47:10 -0300 | [diff] [blame] | 612 | } |
| 613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings) |
| 615 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 616 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
| 618 | fesettings->min_delay_ms = state->config->min_delay_ms; |
| 619 | if (fesettings->parameters.u.qpsk.symbol_rate < 10000000) { |
| 620 | fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 32000; |
| 621 | fesettings->max_drift = 5000; |
| 622 | } else { |
| 623 | fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 16000; |
| 624 | fesettings->max_drift = fesettings->parameters.u.qpsk.symbol_rate / 2000; |
| 625 | } |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | static void stv0299_release(struct dvb_frontend* fe) |
| 630 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 631 | struct stv0299_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | kfree(state); |
| 633 | } |
| 634 | |
| 635 | static struct dvb_frontend_ops stv0299_ops; |
| 636 | |
| 637 | struct dvb_frontend* stv0299_attach(const struct stv0299_config* config, |
| 638 | struct i2c_adapter* i2c) |
| 639 | { |
| 640 | struct stv0299_state* state = NULL; |
| 641 | int id; |
| 642 | |
| 643 | /* allocate memory for the internal state */ |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 644 | state = kmalloc(sizeof(struct stv0299_state), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if (state == NULL) goto error; |
| 646 | |
| 647 | /* setup the state */ |
| 648 | state->config = config; |
| 649 | state->i2c = i2c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | state->initialised = 0; |
| 651 | state->tuner_frequency = 0; |
| 652 | state->symbol_rate = 0; |
| 653 | state->fec_inner = 0; |
Andrew de Quincey | 3765022 | 2005-11-08 21:35:11 -0800 | [diff] [blame] | 654 | state->errmode = STATUS_BER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
| 656 | /* check if the demod is there */ |
| 657 | stv0299_writeregI(state, 0x02, 0x34); /* standby off */ |
| 658 | msleep(200); |
| 659 | id = stv0299_readreg(state, 0x00); |
| 660 | |
| 661 | /* register 0x00 contains 0xa1 for STV0299 and STV0299B */ |
| 662 | /* register 0x00 might contain 0x80 when returning from standby */ |
| 663 | if (id != 0xa1 && id != 0x80) goto error; |
| 664 | |
| 665 | /* create dvb_frontend */ |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 666 | memcpy(&state->frontend.ops, &stv0299_ops, sizeof(struct dvb_frontend_ops)); |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 667 | state->frontend.demodulator_priv = state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | return &state->frontend; |
| 669 | |
| 670 | error: |
| 671 | kfree(state); |
| 672 | return NULL; |
| 673 | } |
| 674 | |
| 675 | static struct dvb_frontend_ops stv0299_ops = { |
| 676 | |
| 677 | .info = { |
| 678 | .name = "ST STV0299 DVB-S", |
| 679 | .type = FE_QPSK, |
| 680 | .frequency_min = 950000, |
| 681 | .frequency_max = 2150000, |
| 682 | .frequency_stepsize = 125, /* kHz for QPSK frontends */ |
| 683 | .frequency_tolerance = 0, |
| 684 | .symbol_rate_min = 1000000, |
| 685 | .symbol_rate_max = 45000000, |
| 686 | .symbol_rate_tolerance = 500, /* ppm */ |
| 687 | .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | |
| 688 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | |
| 689 | FE_CAN_QPSK | |
| 690 | FE_CAN_FEC_AUTO |
| 691 | }, |
| 692 | |
| 693 | .release = stv0299_release, |
| 694 | |
| 695 | .init = stv0299_init, |
| 696 | .sleep = stv0299_sleep, |
Andrew de Quincey | 53a8ee3 | 2006-04-18 17:47:10 -0300 | [diff] [blame] | 697 | .i2c_gate_ctrl = stv0299_i2c_gate_ctrl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
| 699 | .set_frontend = stv0299_set_frontend, |
| 700 | .get_frontend = stv0299_get_frontend, |
| 701 | .get_tune_settings = stv0299_get_tune_settings, |
| 702 | |
| 703 | .read_status = stv0299_read_status, |
| 704 | .read_ber = stv0299_read_ber, |
| 705 | .read_signal_strength = stv0299_read_signal_strength, |
| 706 | .read_snr = stv0299_read_snr, |
| 707 | .read_ucblocks = stv0299_read_ucblocks, |
| 708 | |
| 709 | .diseqc_send_master_cmd = stv0299_send_diseqc_msg, |
| 710 | .diseqc_send_burst = stv0299_send_diseqc_burst, |
| 711 | .set_tone = stv0299_set_tone, |
| 712 | .set_voltage = stv0299_set_voltage, |
| 713 | .dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd, |
| 714 | }; |
| 715 | |
Johannes Stezenbach | 591ad98 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 716 | module_param(debug_legacy_dish_switch, int, 0444); |
| 717 | MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches"); |
| 718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | module_param(debug, int, 0644); |
| 720 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); |
| 721 | |
| 722 | MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver"); |
| 723 | MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, " |
Andrew de Quincey | 53a8ee3 | 2006-04-18 17:47:10 -0300 | [diff] [blame] | 724 | "Andreas Oberritter, Andrew de Quincey, Kenneth Aafly"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | MODULE_LICENSE("GPL"); |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | EXPORT_SYMBOL(stv0299_writereg); |
| 728 | EXPORT_SYMBOL(stv0299_attach); |