Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | Driver for Zarlink VP310/MT312 Satellite Channel Decoder |
| 3 | |
| 4 | Copyright (C) 2003 Andreas Oberritter <obi@linuxtv.org> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | |
| 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 | References: |
| 22 | http://products.zarlink.com/product_profiles/MT312.htm |
| 23 | http://products.zarlink.com/product_profiles/SL1935.htm |
| 24 | */ |
| 25 | |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/moduleparam.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 32 | #include <linux/string.h> |
| 33 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #include "dvb_frontend.h" |
| 36 | #include "mt312_priv.h" |
| 37 | #include "mt312.h" |
| 38 | |
| 39 | |
| 40 | struct mt312_state { |
| 41 | struct i2c_adapter* i2c; |
| 42 | struct dvb_frontend_ops ops; |
| 43 | /* configuration settings */ |
| 44 | const struct mt312_config* config; |
| 45 | struct dvb_frontend frontend; |
| 46 | |
| 47 | u8 id; |
| 48 | u8 frequency; |
| 49 | }; |
| 50 | |
| 51 | static int debug; |
| 52 | #define dprintk(args...) \ |
| 53 | do { \ |
| 54 | if (debug) printk(KERN_DEBUG "mt312: " args); \ |
| 55 | } while (0) |
| 56 | |
| 57 | #define MT312_SYS_CLK 90000000UL /* 90 MHz */ |
| 58 | #define MT312_LPOWER_SYS_CLK 60000000UL /* 60 MHz */ |
| 59 | #define MT312_PLL_CLK 10000000UL /* 10 MHz */ |
| 60 | |
| 61 | static int mt312_read(struct mt312_state* state, const enum mt312_reg_addr reg, |
| 62 | void *buf, const size_t count) |
| 63 | { |
| 64 | int ret; |
| 65 | struct i2c_msg msg[2]; |
| 66 | u8 regbuf[1] = { reg }; |
| 67 | |
| 68 | msg[0].addr = state->config->demod_address; |
| 69 | msg[0].flags = 0; |
| 70 | msg[0].buf = regbuf; |
| 71 | msg[0].len = 1; |
| 72 | msg[1].addr = state->config->demod_address; |
| 73 | msg[1].flags = I2C_M_RD; |
| 74 | msg[1].buf = buf; |
| 75 | msg[1].len = count; |
| 76 | |
| 77 | ret = i2c_transfer(state->i2c, msg, 2); |
| 78 | |
| 79 | if (ret != 2) { |
| 80 | printk(KERN_ERR "%s: ret == %d\n", __FUNCTION__, ret); |
| 81 | return -EREMOTEIO; |
| 82 | } |
| 83 | |
| 84 | if(debug) { |
| 85 | int i; |
| 86 | dprintk("R(%d):", reg & 0x7f); |
| 87 | for (i = 0; i < count; i++) |
| 88 | printk(" %02x", ((const u8 *) buf)[i]); |
| 89 | printk("\n"); |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int mt312_write(struct mt312_state* state, const enum mt312_reg_addr reg, |
| 96 | const void *src, const size_t count) |
| 97 | { |
| 98 | int ret; |
| 99 | u8 buf[count + 1]; |
| 100 | struct i2c_msg msg; |
| 101 | |
| 102 | if(debug) { |
| 103 | int i; |
| 104 | dprintk("W(%d):", reg & 0x7f); |
| 105 | for (i = 0; i < count; i++) |
| 106 | printk(" %02x", ((const u8 *) src)[i]); |
| 107 | printk("\n"); |
| 108 | } |
| 109 | |
| 110 | buf[0] = reg; |
| 111 | memcpy(&buf[1], src, count); |
| 112 | |
| 113 | msg.addr = state->config->demod_address; |
| 114 | msg.flags = 0; |
| 115 | msg.buf = buf; |
| 116 | msg.len = count + 1; |
| 117 | |
| 118 | ret = i2c_transfer(state->i2c, &msg, 1); |
| 119 | |
| 120 | if (ret != 1) { |
| 121 | dprintk("%s: ret == %d\n", __FUNCTION__, ret); |
| 122 | return -EREMOTEIO; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static inline int mt312_readreg(struct mt312_state* state, |
| 129 | const enum mt312_reg_addr reg, u8 *val) |
| 130 | { |
| 131 | return mt312_read(state, reg, val, 1); |
| 132 | } |
| 133 | |
| 134 | static inline int mt312_writereg(struct mt312_state* state, |
| 135 | const enum mt312_reg_addr reg, const u8 val) |
| 136 | { |
| 137 | return mt312_write(state, reg, &val, 1); |
| 138 | } |
| 139 | |
| 140 | static inline u32 mt312_div(u32 a, u32 b) |
| 141 | { |
| 142 | return (a + (b / 2)) / b; |
| 143 | } |
| 144 | |
| 145 | static int mt312_reset(struct mt312_state* state, const u8 full) |
| 146 | { |
| 147 | return mt312_writereg(state, RESET, full ? 0x80 : 0x40); |
| 148 | } |
| 149 | |
| 150 | static int mt312_get_inversion(struct mt312_state* state, |
| 151 | fe_spectral_inversion_t *i) |
| 152 | { |
| 153 | int ret; |
| 154 | u8 vit_mode; |
| 155 | |
| 156 | if ((ret = mt312_readreg(state, VIT_MODE, &vit_mode)) < 0) |
| 157 | return ret; |
| 158 | |
| 159 | if (vit_mode & 0x80) /* auto inversion was used */ |
| 160 | *i = (vit_mode & 0x40) ? INVERSION_ON : INVERSION_OFF; |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int mt312_get_symbol_rate(struct mt312_state* state, u32 *sr) |
| 166 | { |
| 167 | int ret; |
| 168 | u8 sym_rate_h; |
| 169 | u8 dec_ratio; |
| 170 | u16 sym_rat_op; |
| 171 | u16 monitor; |
| 172 | u8 buf[2]; |
| 173 | |
| 174 | if ((ret = mt312_readreg(state, SYM_RATE_H, &sym_rate_h)) < 0) |
| 175 | return ret; |
| 176 | |
| 177 | if (sym_rate_h & 0x80) { /* symbol rate search was used */ |
| 178 | if ((ret = mt312_writereg(state, MON_CTRL, 0x03)) < 0) |
| 179 | return ret; |
| 180 | |
| 181 | if ((ret = mt312_read(state, MONITOR_H, buf, sizeof(buf))) < 0) |
| 182 | return ret; |
| 183 | |
| 184 | monitor = (buf[0] << 8) | buf[1]; |
| 185 | |
| 186 | dprintk(KERN_DEBUG "sr(auto) = %u\n", |
| 187 | mt312_div(monitor * 15625, 4)); |
| 188 | } else { |
| 189 | if ((ret = mt312_writereg(state, MON_CTRL, 0x05)) < 0) |
| 190 | return ret; |
| 191 | |
| 192 | if ((ret = mt312_read(state, MONITOR_H, buf, sizeof(buf))) < 0) |
| 193 | return ret; |
| 194 | |
| 195 | dec_ratio = ((buf[0] >> 5) & 0x07) * 32; |
| 196 | |
| 197 | if ((ret = mt312_read(state, SYM_RAT_OP_H, buf, sizeof(buf))) < 0) |
| 198 | return ret; |
| 199 | |
| 200 | sym_rat_op = (buf[0] << 8) | buf[1]; |
| 201 | |
| 202 | dprintk(KERN_DEBUG "sym_rat_op=%d dec_ratio=%d\n", |
| 203 | sym_rat_op, dec_ratio); |
| 204 | dprintk(KERN_DEBUG "*sr(manual) = %lu\n", |
| 205 | (((MT312_PLL_CLK * 8192) / (sym_rat_op + 8192)) * |
| 206 | 2) - dec_ratio); |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int mt312_get_code_rate(struct mt312_state* state, fe_code_rate_t *cr) |
| 213 | { |
| 214 | const fe_code_rate_t fec_tab[8] = |
| 215 | { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_6_7, FEC_7_8, |
| 216 | FEC_AUTO, FEC_AUTO }; |
| 217 | |
| 218 | int ret; |
| 219 | u8 fec_status; |
| 220 | |
| 221 | if ((ret = mt312_readreg(state, FEC_STATUS, &fec_status)) < 0) |
| 222 | return ret; |
| 223 | |
| 224 | *cr = fec_tab[(fec_status >> 4) & 0x07]; |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int mt312_initfe(struct dvb_frontend* fe) |
| 230 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 231 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | int ret; |
| 233 | u8 buf[2]; |
| 234 | |
| 235 | /* wake up */ |
| 236 | if ((ret = mt312_writereg(state, CONFIG, (state->frequency == 60 ? 0x88 : 0x8c))) < 0) |
| 237 | return ret; |
| 238 | |
| 239 | /* wait at least 150 usec */ |
| 240 | udelay(150); |
| 241 | |
| 242 | /* full reset */ |
| 243 | if ((ret = mt312_reset(state, 1)) < 0) |
| 244 | return ret; |
| 245 | |
| 246 | // Per datasheet, write correct values. 09/28/03 ACCJr. |
| 247 | // If we don't do this, we won't get FE_HAS_VITERBI in the VP310. |
| 248 | { |
| 249 | u8 buf_def[8]={0x14, 0x12, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00}; |
| 250 | |
| 251 | if ((ret = mt312_write(state, VIT_SETUP, buf_def, sizeof(buf_def))) < 0) |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | /* SYS_CLK */ |
| 256 | buf[0] = mt312_div((state->frequency == 60 ? MT312_LPOWER_SYS_CLK : MT312_SYS_CLK) * 2, 1000000); |
| 257 | |
| 258 | /* DISEQC_RATIO */ |
| 259 | buf[1] = mt312_div(MT312_PLL_CLK, 15000 * 4); |
| 260 | |
| 261 | if ((ret = mt312_write(state, SYS_CLK, buf, sizeof(buf))) < 0) |
| 262 | return ret; |
| 263 | |
| 264 | if ((ret = mt312_writereg(state, SNR_THS_HIGH, 0x32)) < 0) |
| 265 | return ret; |
| 266 | |
| 267 | if ((ret = mt312_writereg(state, OP_CTRL, 0x53)) < 0) |
| 268 | return ret; |
| 269 | |
| 270 | /* TS_SW_LIM */ |
| 271 | buf[0] = 0x8c; |
| 272 | buf[1] = 0x98; |
| 273 | |
| 274 | if ((ret = mt312_write(state, TS_SW_LIM_L, buf, sizeof(buf))) < 0) |
| 275 | return ret; |
| 276 | |
| 277 | if ((ret = mt312_writereg(state, CS_SW_LIM, 0x69)) < 0) |
| 278 | return ret; |
| 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int mt312_send_master_cmd(struct dvb_frontend* fe, |
| 284 | struct dvb_diseqc_master_cmd *c) |
| 285 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 286 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | int ret; |
| 288 | u8 diseqc_mode; |
| 289 | |
| 290 | if ((c->msg_len == 0) || (c->msg_len > sizeof(c->msg))) |
| 291 | return -EINVAL; |
| 292 | |
| 293 | if ((ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode)) < 0) |
| 294 | return ret; |
| 295 | |
| 296 | if ((ret = |
| 297 | mt312_write(state, (0x80 | DISEQC_INSTR), c->msg, c->msg_len)) < 0) |
| 298 | return ret; |
| 299 | |
| 300 | if ((ret = |
| 301 | mt312_writereg(state, DISEQC_MODE, |
| 302 | (diseqc_mode & 0x40) | ((c->msg_len - 1) << 3) |
| 303 | | 0x04)) < 0) |
| 304 | return ret; |
| 305 | |
| 306 | /* set DISEQC_MODE[2:0] to zero if a return message is expected */ |
| 307 | if (c->msg[0] & 0x02) |
| 308 | if ((ret = |
| 309 | mt312_writereg(state, DISEQC_MODE, (diseqc_mode & 0x40))) < 0) |
| 310 | return ret; |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static int mt312_send_burst(struct dvb_frontend* fe, const fe_sec_mini_cmd_t c) |
| 316 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 317 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | const u8 mini_tab[2] = { 0x02, 0x03 }; |
| 319 | |
| 320 | int ret; |
| 321 | u8 diseqc_mode; |
| 322 | |
| 323 | if (c > SEC_MINI_B) |
| 324 | return -EINVAL; |
| 325 | |
| 326 | if ((ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode)) < 0) |
| 327 | return ret; |
| 328 | |
| 329 | if ((ret = |
| 330 | mt312_writereg(state, DISEQC_MODE, |
| 331 | (diseqc_mode & 0x40) | mini_tab[c])) < 0) |
| 332 | return ret; |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static int mt312_set_tone(struct dvb_frontend* fe, const fe_sec_tone_mode_t t) |
| 338 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 339 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | const u8 tone_tab[2] = { 0x01, 0x00 }; |
| 341 | |
| 342 | int ret; |
| 343 | u8 diseqc_mode; |
| 344 | |
| 345 | if (t > SEC_TONE_OFF) |
| 346 | return -EINVAL; |
| 347 | |
| 348 | if ((ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode)) < 0) |
| 349 | return ret; |
| 350 | |
| 351 | if ((ret = |
| 352 | mt312_writereg(state, DISEQC_MODE, |
| 353 | (diseqc_mode & 0x40) | tone_tab[t])) < 0) |
| 354 | return ret; |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static int mt312_set_voltage(struct dvb_frontend* fe, const fe_sec_voltage_t v) |
| 360 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 361 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | const u8 volt_tab[3] = { 0x00, 0x40, 0x00 }; |
| 363 | |
| 364 | if (v > SEC_VOLTAGE_OFF) |
| 365 | return -EINVAL; |
| 366 | |
| 367 | return mt312_writereg(state, DISEQC_MODE, volt_tab[v]); |
| 368 | } |
| 369 | |
| 370 | static int mt312_read_status(struct dvb_frontend* fe, fe_status_t *s) |
| 371 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 372 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | int ret; |
| 374 | u8 status[3]; |
| 375 | |
| 376 | *s = 0; |
| 377 | |
| 378 | if ((ret = mt312_read(state, QPSK_STAT_H, status, sizeof(status))) < 0) |
| 379 | return ret; |
| 380 | |
| 381 | dprintk(KERN_DEBUG "QPSK_STAT_H: 0x%02x, QPSK_STAT_L: 0x%02x, FEC_STATUS: 0x%02x\n", status[0], status[1], status[2]); |
| 382 | |
| 383 | if (status[0] & 0xc0) |
| 384 | *s |= FE_HAS_SIGNAL; /* signal noise ratio */ |
| 385 | if (status[0] & 0x04) |
| 386 | *s |= FE_HAS_CARRIER; /* qpsk carrier lock */ |
| 387 | if (status[2] & 0x02) |
| 388 | *s |= FE_HAS_VITERBI; /* viterbi lock */ |
| 389 | if (status[2] & 0x04) |
| 390 | *s |= FE_HAS_SYNC; /* byte align lock */ |
| 391 | if (status[0] & 0x01) |
| 392 | *s |= FE_HAS_LOCK; /* qpsk lock */ |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int mt312_read_ber(struct dvb_frontend* fe, u32 *ber) |
| 398 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 399 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | int ret; |
| 401 | u8 buf[3]; |
| 402 | |
| 403 | if ((ret = mt312_read(state, RS_BERCNT_H, buf, 3)) < 0) |
| 404 | return ret; |
| 405 | |
| 406 | *ber = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) * 64; |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int mt312_read_signal_strength(struct dvb_frontend* fe, u16 *signal_strength) |
| 412 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 413 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | int ret; |
| 415 | u8 buf[3]; |
| 416 | u16 agc; |
| 417 | s16 err_db; |
| 418 | |
| 419 | if ((ret = mt312_read(state, AGC_H, buf, sizeof(buf))) < 0) |
| 420 | return ret; |
| 421 | |
| 422 | agc = (buf[0] << 6) | (buf[1] >> 2); |
| 423 | err_db = (s16) (((buf[1] & 0x03) << 14) | buf[2] << 6) >> 6; |
| 424 | |
| 425 | *signal_strength = agc; |
| 426 | |
| 427 | dprintk(KERN_DEBUG "agc=%08x err_db=%hd\n", agc, err_db); |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static int mt312_read_snr(struct dvb_frontend* fe, u16 *snr) |
| 433 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 434 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | int ret; |
| 436 | u8 buf[2]; |
| 437 | |
| 438 | if ((ret = mt312_read(state, M_SNR_H, &buf, sizeof(buf))) < 0) |
| 439 | return ret; |
| 440 | |
| 441 | *snr = 0xFFFF - ((((buf[0] & 0x7f) << 8) | buf[1]) << 1); |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int mt312_read_ucblocks(struct dvb_frontend* fe, u32 *ubc) |
| 447 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 448 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | int ret; |
| 450 | u8 buf[2]; |
| 451 | |
| 452 | if ((ret = mt312_read(state, RS_UBC_H, &buf, sizeof(buf))) < 0) |
| 453 | return ret; |
| 454 | |
| 455 | *ubc = (buf[0] << 8) | buf[1]; |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static int mt312_set_frontend(struct dvb_frontend* fe, |
| 461 | struct dvb_frontend_parameters *p) |
| 462 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 463 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | int ret; |
| 465 | u8 buf[5], config_val; |
| 466 | u16 sr; |
| 467 | |
| 468 | const u8 fec_tab[10] = |
| 469 | { 0x00, 0x01, 0x02, 0x04, 0x3f, 0x08, 0x10, 0x20, 0x3f, 0x3f }; |
| 470 | const u8 inv_tab[3] = { 0x00, 0x40, 0x80 }; |
| 471 | |
| 472 | dprintk("%s: Freq %d\n", __FUNCTION__, p->frequency); |
| 473 | |
| 474 | if ((p->frequency < fe->ops->info.frequency_min) |
| 475 | || (p->frequency > fe->ops->info.frequency_max)) |
| 476 | return -EINVAL; |
| 477 | |
| 478 | if ((p->inversion < INVERSION_OFF) |
| 479 | || (p->inversion > INVERSION_ON)) |
| 480 | return -EINVAL; |
| 481 | |
| 482 | if ((p->u.qpsk.symbol_rate < fe->ops->info.symbol_rate_min) |
| 483 | || (p->u.qpsk.symbol_rate > fe->ops->info.symbol_rate_max)) |
| 484 | return -EINVAL; |
| 485 | |
| 486 | if ((p->u.qpsk.fec_inner < FEC_NONE) |
| 487 | || (p->u.qpsk.fec_inner > FEC_AUTO)) |
| 488 | return -EINVAL; |
| 489 | |
| 490 | if ((p->u.qpsk.fec_inner == FEC_4_5) |
| 491 | || (p->u.qpsk.fec_inner == FEC_8_9)) |
| 492 | return -EINVAL; |
| 493 | |
| 494 | switch (state->id) { |
| 495 | case ID_VP310: |
| 496 | // For now we will do this only for the VP310. |
| 497 | // It should be better for the mt312 as well, but tunning will be slower. ACCJr 09/29/03 |
Alexey Dobriyan | 682e852 | 2006-01-10 00:09:16 +0300 | [diff] [blame] | 498 | ret = mt312_readreg(state, CONFIG, &config_val); |
| 499 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | return ret; |
| 501 | if (p->u.qpsk.symbol_rate >= 30000000) //Note that 30MS/s should use 90MHz |
| 502 | { |
| 503 | if ((config_val & 0x0c) == 0x08) { //We are running 60MHz |
| 504 | state->frequency = 90; |
| 505 | if ((ret = mt312_initfe(fe)) < 0) |
| 506 | return ret; |
| 507 | } |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | if ((config_val & 0x0c) == 0x0C) { //We are running 90MHz |
| 512 | state->frequency = 60; |
| 513 | if ((ret = mt312_initfe(fe)) < 0) |
| 514 | return ret; |
| 515 | } |
| 516 | } |
| 517 | break; |
| 518 | |
| 519 | case ID_MT312: |
| 520 | break; |
| 521 | |
| 522 | default: |
| 523 | return -EINVAL; |
| 524 | } |
| 525 | |
Andrew de Quincey | a81870e | 2006-04-18 17:47:09 -0300 | [diff] [blame^] | 526 | if (fe->ops->tuner_ops.set_params) { |
| 527 | fe->ops->tuner_ops.set_params(fe, p); |
| 528 | if (fe->ops->i2c_gate_ctrl) fe->ops->i2c_gate_ctrl(fe, 0); |
| 529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
| 531 | /* sr = (u16)(sr * 256.0 / 1000000.0) */ |
| 532 | sr = mt312_div(p->u.qpsk.symbol_rate * 4, 15625); |
| 533 | |
| 534 | /* SYM_RATE */ |
| 535 | buf[0] = (sr >> 8) & 0x3f; |
| 536 | buf[1] = (sr >> 0) & 0xff; |
| 537 | |
| 538 | /* VIT_MODE */ |
| 539 | buf[2] = inv_tab[p->inversion] | fec_tab[p->u.qpsk.fec_inner]; |
| 540 | |
| 541 | /* QPSK_CTRL */ |
| 542 | buf[3] = 0x40; /* swap I and Q before QPSK demodulation */ |
| 543 | |
| 544 | if (p->u.qpsk.symbol_rate < 10000000) |
| 545 | buf[3] |= 0x04; /* use afc mode */ |
| 546 | |
| 547 | /* GO */ |
| 548 | buf[4] = 0x01; |
| 549 | |
| 550 | if ((ret = mt312_write(state, SYM_RATE_H, buf, sizeof(buf))) < 0) |
| 551 | return ret; |
| 552 | |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 553 | mt312_reset(state, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int mt312_get_frontend(struct dvb_frontend* fe, |
| 559 | struct dvb_frontend_parameters *p) |
| 560 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 561 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | int ret; |
| 563 | |
| 564 | if ((ret = mt312_get_inversion(state, &p->inversion)) < 0) |
| 565 | return ret; |
| 566 | |
| 567 | if ((ret = mt312_get_symbol_rate(state, &p->u.qpsk.symbol_rate)) < 0) |
| 568 | return ret; |
| 569 | |
| 570 | if ((ret = mt312_get_code_rate(state, &p->u.qpsk.fec_inner)) < 0) |
| 571 | return ret; |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
Andrew de Quincey | a81870e | 2006-04-18 17:47:09 -0300 | [diff] [blame^] | 576 | static int mt312_i2c_gate_ctrl(struct dvb_frontend* fe, int enable) |
| 577 | { |
| 578 | struct mt312_state* state = fe->demodulator_priv; |
| 579 | |
| 580 | if (enable) { |
| 581 | return mt312_writereg(state, GPP_CTRL, 0x40); |
| 582 | } else { |
| 583 | return mt312_writereg(state, GPP_CTRL, 0x00); |
| 584 | } |
| 585 | } |
| 586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | static int mt312_sleep(struct dvb_frontend* fe) |
| 588 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 589 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | int ret; |
| 591 | u8 config; |
| 592 | |
| 593 | /* reset all registers to defaults */ |
| 594 | if ((ret = mt312_reset(state, 1)) < 0) |
| 595 | return ret; |
| 596 | |
| 597 | if ((ret = mt312_readreg(state, CONFIG, &config)) < 0) |
| 598 | return ret; |
| 599 | |
| 600 | /* enter standby */ |
| 601 | if ((ret = mt312_writereg(state, CONFIG, config & 0x7f)) < 0) |
| 602 | return ret; |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static int mt312_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings) |
| 608 | { |
| 609 | fesettings->min_delay_ms = 50; |
| 610 | fesettings->step_size = 0; |
| 611 | fesettings->max_drift = 0; |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static void mt312_release(struct dvb_frontend* fe) |
| 616 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 617 | struct mt312_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | kfree(state); |
| 619 | } |
| 620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | static struct dvb_frontend_ops vp310_mt312_ops = { |
| 622 | |
| 623 | .info = { |
| 624 | .name = "Zarlink ???? DVB-S", |
| 625 | .type = FE_QPSK, |
| 626 | .frequency_min = 950000, |
| 627 | .frequency_max = 2150000, |
| 628 | .frequency_stepsize = (MT312_PLL_CLK / 1000) / 128, |
| 629 | .symbol_rate_min = MT312_SYS_CLK / 128, |
| 630 | .symbol_rate_max = MT312_SYS_CLK / 2, |
| 631 | .caps = |
| 632 | FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | |
| 633 | FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | |
| 634 | FE_CAN_FEC_AUTO | FE_CAN_QPSK | FE_CAN_MUTE_TS | |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 635 | FE_CAN_RECOVER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | }, |
| 637 | |
| 638 | .release = mt312_release, |
| 639 | |
| 640 | .init = mt312_initfe, |
| 641 | .sleep = mt312_sleep, |
Andrew de Quincey | a81870e | 2006-04-18 17:47:09 -0300 | [diff] [blame^] | 642 | .i2c_gate_ctrl = mt312_i2c_gate_ctrl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
| 644 | .set_frontend = mt312_set_frontend, |
| 645 | .get_frontend = mt312_get_frontend, |
| 646 | .get_tune_settings = mt312_get_tune_settings, |
| 647 | |
| 648 | .read_status = mt312_read_status, |
| 649 | .read_ber = mt312_read_ber, |
| 650 | .read_signal_strength = mt312_read_signal_strength, |
| 651 | .read_snr = mt312_read_snr, |
| 652 | .read_ucblocks = mt312_read_ucblocks, |
| 653 | |
| 654 | .diseqc_send_master_cmd = mt312_send_master_cmd, |
| 655 | .diseqc_send_burst = mt312_send_burst, |
| 656 | .set_tone = mt312_set_tone, |
| 657 | .set_voltage = mt312_set_voltage, |
| 658 | }; |
| 659 | |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 660 | struct dvb_frontend* vp310_mt312_attach(const struct mt312_config* config, |
| 661 | struct i2c_adapter* i2c) |
| 662 | { |
| 663 | struct mt312_state* state = NULL; |
| 664 | |
| 665 | /* allocate memory for the internal state */ |
| 666 | state = kmalloc(sizeof(struct mt312_state), GFP_KERNEL); |
| 667 | if (state == NULL) |
| 668 | goto error; |
| 669 | |
| 670 | /* setup the state */ |
| 671 | state->config = config; |
| 672 | state->i2c = i2c; |
| 673 | memcpy(&state->ops, &vp310_mt312_ops, sizeof(struct dvb_frontend_ops)); |
| 674 | |
| 675 | /* check if the demod is there */ |
| 676 | if (mt312_readreg(state, ID, &state->id) < 0) |
| 677 | goto error; |
| 678 | |
| 679 | switch (state->id) { |
| 680 | case ID_VP310: |
| 681 | strcpy(state->ops.info.name, "Zarlink VP310 DVB-S"); |
| 682 | state->frequency = 90; |
| 683 | break; |
| 684 | case ID_MT312: |
| 685 | strcpy(state->ops.info.name, "Zarlink MT312 DVB-S"); |
| 686 | state->frequency = 60; |
| 687 | break; |
| 688 | default: |
| 689 | printk (KERN_WARNING "Only Zarlink VP310/MT312 are supported chips.\n"); |
| 690 | goto error; |
| 691 | } |
| 692 | |
| 693 | /* create dvb_frontend */ |
| 694 | state->frontend.ops = &state->ops; |
| 695 | state->frontend.demodulator_priv = state; |
| 696 | return &state->frontend; |
| 697 | |
| 698 | error: |
| 699 | kfree(state); |
| 700 | return NULL; |
| 701 | } |
| 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | module_param(debug, int, 0644); |
| 704 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); |
| 705 | |
| 706 | MODULE_DESCRIPTION("Zarlink VP310/MT312 DVB-S Demodulator driver"); |
| 707 | MODULE_AUTHOR("Andreas Oberritter <obi@linuxtv.org>"); |
| 708 | MODULE_LICENSE("GPL"); |
| 709 | |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 710 | EXPORT_SYMBOL(vp310_mt312_attach); |