1/* 2 Driver for Philips TDA8083 based QPSK Demodulator 3 4 Copyright (C) 2001 Convergence Integrated Media GmbH 5 6 written by Ralph Metzler <ralph@convergence.de> 7 8 adoption to the new DVB frontend API and diagnostic ioctl's 9 by Holger Waechtler <holger@convergence.de> 10 11 This program is free software; you can redistribute it and/or modify 12 it under the terms of the GNU General Public License as published by 13 the Free Software Foundation; either version 2 of the License, or 14 (at your option) any later version. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with this program; if not, write to the Free Software 23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 25*/ 26 27#include <linux/init.h> 28#include <linux/kernel.h> 29#include <linux/module.h> 30#include <linux/string.h> 31#include <linux/slab.h> 32#include <linux/jiffies.h> 33#include "dvb_frontend.h" 34#include "tda8083.h" 35 36 37struct tda8083_state { 38 struct i2c_adapter* i2c; 39 /* configuration settings */ 40 const struct tda8083_config* config; 41 struct dvb_frontend frontend; 42}; 43 44static int debug; 45#define dprintk(args...) \ 46 do { \ 47 if (debug) printk(KERN_DEBUG "tda8083: " args); \ 48 } while (0) 49 50 51static u8 tda8083_init_tab [] = { 52 0x04, 0x00, 0x4a, 0x79, 0x04, 0x00, 0xff, 0xea, 53 0x48, 0x42, 0x79, 0x60, 0x70, 0x52, 0x9a, 0x10, 54 0x0e, 0x10, 0xf2, 0xa7, 0x93, 0x0b, 0x05, 0xc8, 55 0x9d, 0x00, 0x42, 0x80, 0x00, 0x60, 0x40, 0x00, 56 0x00, 0x75, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 57 0x00, 0x00, 0x00, 0x00 58}; 59 60 61static int tda8083_writereg (struct tda8083_state* state, u8 reg, u8 data) 62{ 63 int ret; 64 u8 buf [] = { reg, data }; 65 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 }; 66 67 ret = i2c_transfer(state->i2c, &msg, 1); 68 69 if (ret != 1) 70 dprintk ("%s: writereg error (reg %02x, ret == %i)\n", 71 __func__, reg, ret); 72 73 return (ret != 1) ? -1 : 0; 74} 75 76static int tda8083_readregs (struct tda8083_state* state, u8 reg1, u8 *b, u8 len) 77{ 78 int ret; 79 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = ®1, .len = 1 }, 80 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } }; 81 82 ret = i2c_transfer(state->i2c, msg, 2); 83 84 if (ret != 2) 85 dprintk ("%s: readreg error (reg %02x, ret == %i)\n", 86 __func__, reg1, ret); 87 88 return ret == 2 ? 0 : -1; 89} 90 91static inline u8 tda8083_readreg (struct tda8083_state* state, u8 reg) 92{ 93 u8 val; 94 95 tda8083_readregs (state, reg, &val, 1); 96 97 return val; 98} 99 100static int tda8083_set_inversion (struct tda8083_state* state, fe_spectral_inversion_t inversion) 101{ 102 if (inversion == INVERSION_AUTO) 103 return 0; 104 105 return -EINVAL; 106} 107 108static int tda8083_set_fec (struct tda8083_state* state, fe_code_rate_t fec) 109{ 110 if (fec == FEC_AUTO) 111 return tda8083_writereg (state, 0x07, 0xff); 112 113 if (fec >= FEC_1_2 && fec <= FEC_8_9) 114 return tda8083_writereg (state, 0x07, 1 << (FEC_8_9 - fec)); 115 116 return -EINVAL; 117} 118 119static fe_code_rate_t tda8083_get_fec (struct tda8083_state* state) 120{ 121 u8 index; 122 static fe_code_rate_t fec_tab [] = { FEC_8_9, FEC_1_2, FEC_2_3, FEC_3_4, 123 FEC_4_5, FEC_5_6, FEC_6_7, FEC_7_8 }; 124 125 index = tda8083_readreg(state, 0x0e) & 0x07; 126 127 return fec_tab [index]; 128} 129 130static int tda8083_set_symbolrate (struct tda8083_state* state, u32 srate) 131{ 132 u32 ratio; 133 u32 tmp; 134 u8 filter; 135 136 if (srate > 32000000) 137 srate = 32000000; 138 if (srate < 500000) 139 srate = 500000; 140 141 filter = 0; 142 if (srate < 24000000) 143 filter = 2; 144 if (srate < 16000000) 145 filter = 3; 146 147 tmp = 31250 << 16; 148 ratio = tmp / srate; 149 150 tmp = (tmp % srate) << 8; 151 ratio = (ratio << 8) + tmp / srate; 152 153 tmp = (tmp % srate) << 8; 154 ratio = (ratio << 8) + tmp / srate; 155 156 dprintk("tda8083: ratio == %08x\n", (unsigned int) ratio); 157 158 tda8083_writereg (state, 0x05, filter); 159 tda8083_writereg (state, 0x02, (ratio >> 16) & 0xff); 160 tda8083_writereg (state, 0x03, (ratio >> 8) & 0xff); 161 tda8083_writereg (state, 0x04, (ratio ) & 0xff); 162 163 tda8083_writereg (state, 0x00, 0x3c); 164 tda8083_writereg (state, 0x00, 0x04); 165 166 return 1; 167} 168 169static void tda8083_wait_diseqc_fifo (struct tda8083_state* state, int timeout) 170{ 171 unsigned long start = jiffies; 172 173 while (jiffies - start < timeout && 174 !(tda8083_readreg(state, 0x02) & 0x80)) 175 { 176 msleep(50); 177 }; 178} 179 180static int tda8083_set_tone (struct tda8083_state* state, fe_sec_tone_mode_t tone) 181{ 182 tda8083_writereg (state, 0x26, 0xf1); 183 184 switch (tone) { 185 case SEC_TONE_OFF: 186 return tda8083_writereg (state, 0x29, 0x00); 187 case SEC_TONE_ON: 188 return tda8083_writereg (state, 0x29, 0x80); 189 default: 190 return -EINVAL; 191 }; 192} 193 194static int tda8083_set_voltage (struct tda8083_state* state, fe_sec_voltage_t voltage) 195{ 196 switch (voltage) { 197 case SEC_VOLTAGE_13: 198 return tda8083_writereg (state, 0x20, 0x00); 199 case SEC_VOLTAGE_18: 200 return tda8083_writereg (state, 0x20, 0x11); 201 default: 202 return -EINVAL; 203 }; 204} 205 206static int tda8083_send_diseqc_burst (struct tda8083_state* state, fe_sec_mini_cmd_t burst) 207{ 208 switch (burst) { 209 case SEC_MINI_A: 210 tda8083_writereg (state, 0x29, (5 << 2)); /* send burst A */ 211 break; 212 case SEC_MINI_B: 213 tda8083_writereg (state, 0x29, (7 << 2)); /* send B */ 214 break; 215 default: 216 return -EINVAL; 217 }; 218 219 tda8083_wait_diseqc_fifo (state, 100); 220 221 return 0; 222} 223 224static int tda8083_send_diseqc_msg (struct dvb_frontend* fe, 225 struct dvb_diseqc_master_cmd *m) 226{ 227 struct tda8083_state* state = fe->demodulator_priv; 228 int i; 229 230 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (1 << 2)); /* enable */ 231 232 for (i=0; i<m->msg_len; i++) 233 tda8083_writereg (state, 0x23 + i, m->msg[i]); 234 235 tda8083_writereg (state, 0x29, (m->msg_len - 3) | (3 << 2)); /* send!! */ 236 237 tda8083_wait_diseqc_fifo (state, 100); 238 239 return 0; 240} 241 242static int tda8083_read_status(struct dvb_frontend* fe, fe_status_t* status) 243{ 244 struct tda8083_state* state = fe->demodulator_priv; 245 246 u8 signal = ~tda8083_readreg (state, 0x01); 247 u8 sync = tda8083_readreg (state, 0x02); 248 249 *status = 0; 250 251 if (signal > 10) 252 *status |= FE_HAS_SIGNAL; 253 254 if (sync & 0x01) 255 *status |= FE_HAS_CARRIER; 256 257 if (sync & 0x02) 258 *status |= FE_HAS_VITERBI; 259 260 if (sync & 0x10) 261 *status |= FE_HAS_SYNC; 262 263 if (sync & 0x20) /* frontend can not lock */ 264 *status |= FE_TIMEDOUT; 265 266 if ((sync & 0x1f) == 0x1f) 267 *status |= FE_HAS_LOCK; 268 269 return 0; 270} 271 272static int tda8083_read_ber(struct dvb_frontend* fe, u32* ber) 273{ 274 struct tda8083_state* state = fe->demodulator_priv; 275 int ret; 276 u8 buf[3]; 277 278 if ((ret = tda8083_readregs(state, 0x0b, buf, sizeof(buf)))) 279 return ret; 280 281 *ber = ((buf[0] & 0x1f) << 16) | (buf[1] << 8) | buf[2]; 282 283 return 0; 284} 285 286static int tda8083_read_signal_strength(struct dvb_frontend* fe, u16* strength) 287{ 288 struct tda8083_state* state = fe->demodulator_priv; 289 290 u8 signal = ~tda8083_readreg (state, 0x01); 291 *strength = (signal << 8) | signal; 292 293 return 0; 294} 295 296static int tda8083_read_snr(struct dvb_frontend* fe, u16* snr) 297{ 298 struct tda8083_state* state = fe->demodulator_priv; 299 300 u8 _snr = tda8083_readreg (state, 0x08); 301 *snr = (_snr << 8) | _snr; 302 303 return 0; 304} 305 306static int tda8083_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks) 307{ 308 struct tda8083_state* state = fe->demodulator_priv; 309 310 *ucblocks = tda8083_readreg(state, 0x0f); 311 if (*ucblocks == 0xff) 312 *ucblocks = 0xffffffff; 313 314 return 0; 315} 316 317static int tda8083_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p) 318{ 319 struct tda8083_state* state = fe->demodulator_priv; 320 321 if (fe->ops.tuner_ops.set_params) { 322 fe->ops.tuner_ops.set_params(fe, p); 323 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0); 324 } 325 326 tda8083_set_inversion (state, p->inversion); 327 tda8083_set_fec (state, p->u.qpsk.fec_inner); 328 tda8083_set_symbolrate (state, p->u.qpsk.symbol_rate); 329 330 tda8083_writereg (state, 0x00, 0x3c); 331 tda8083_writereg (state, 0x00, 0x04); 332 333 return 0; 334} 335 336static int tda8083_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p) 337{ 338 struct tda8083_state* state = fe->demodulator_priv; 339 340 /*p->frequency = ???;*/ 341 p->inversion = (tda8083_readreg (state, 0x0e) & 0x80) ? 342 INVERSION_ON : INVERSION_OFF; 343 p->u.qpsk.fec_inner = tda8083_get_fec (state); 344 /*p->u.qpsk.symbol_rate = tda8083_get_symbolrate (state);*/ 345 346 return 0; 347} 348 349static int tda8083_sleep(struct dvb_frontend* fe) 350{ 351 struct tda8083_state* state = fe->demodulator_priv; 352 353 tda8083_writereg (state, 0x00, 0x02); 354 return 0; 355} 356 357static int tda8083_init(struct dvb_frontend* fe) 358{ 359 struct tda8083_state* state = fe->demodulator_priv; 360 int i; 361 362 for (i=0; i<44; i++) 363 tda8083_writereg (state, i, tda8083_init_tab[i]); 364 365 tda8083_writereg (state, 0x00, 0x3c); 366 tda8083_writereg (state, 0x00, 0x04); 367 368 return 0; 369} 370 371static int tda8083_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst) 372{ 373 struct tda8083_state* state = fe->demodulator_priv; 374 375 tda8083_send_diseqc_burst (state, burst); 376 tda8083_writereg (state, 0x00, 0x3c); 377 tda8083_writereg (state, 0x00, 0x04); 378 379 return 0; 380} 381 382static int tda8083_diseqc_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone) 383{ 384 struct tda8083_state* state = fe->demodulator_priv; 385 386 tda8083_set_tone (state, tone); 387 tda8083_writereg (state, 0x00, 0x3c); 388 tda8083_writereg (state, 0x00, 0x04); 389 390 return 0; 391} 392 393static int tda8083_diseqc_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage) 394{ 395 struct tda8083_state* state = fe->demodulator_priv; 396 397 tda8083_set_voltage (state, voltage); 398 tda8083_writereg (state, 0x00, 0x3c); 399 tda8083_writereg (state, 0x00, 0x04); 400 401 return 0; 402} 403 404static void tda8083_release(struct dvb_frontend* fe) 405{ 406 struct tda8083_state* state = fe->demodulator_priv; 407 kfree(state); 408} 409 410static struct dvb_frontend_ops tda8083_ops; 411 412struct dvb_frontend* tda8083_attach(const struct tda8083_config* config, 413 struct i2c_adapter* i2c) 414{ 415 struct tda8083_state* state = NULL; 416 417 /* allocate memory for the internal state */ 418 state = kzalloc(sizeof(struct tda8083_state), GFP_KERNEL); 419 if (state == NULL) goto error; 420 421 /* setup the state */ 422 state->config = config; 423 state->i2c = i2c; 424 425 /* check if the demod is there */ 426 if ((tda8083_readreg(state, 0x00)) != 0x05) goto error; 427 428 /* create dvb_frontend */ 429 memcpy(&state->frontend.ops, &tda8083_ops, sizeof(struct dvb_frontend_ops)); 430 state->frontend.demodulator_priv = state; 431 return &state->frontend; 432 433error: 434 kfree(state); 435 return NULL; 436} 437 438static struct dvb_frontend_ops tda8083_ops = { 439 440 .info = { 441 .name = "Philips TDA8083 DVB-S", 442 .type = FE_QPSK, 443 .frequency_min = 920000, /* TDA8060 */ 444 .frequency_max = 2200000, /* TDA8060 */ 445 .frequency_stepsize = 125, /* kHz for QPSK frontends */ 446 /* .frequency_tolerance = ???,*/ 447 .symbol_rate_min = 12000000, 448 .symbol_rate_max = 30000000, 449 /* .symbol_rate_tolerance = ???,*/ 450 .caps = FE_CAN_INVERSION_AUTO | 451 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | 452 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 | 453 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO | 454 FE_CAN_QPSK | FE_CAN_MUTE_TS 455 }, 456 457 .release = tda8083_release, 458 459 .init = tda8083_init, 460 .sleep = tda8083_sleep, 461 462 .set_frontend = tda8083_set_frontend, 463 .get_frontend = tda8083_get_frontend, 464 465 .read_status = tda8083_read_status, 466 .read_signal_strength = tda8083_read_signal_strength, 467 .read_snr = tda8083_read_snr, 468 .read_ber = tda8083_read_ber, 469 .read_ucblocks = tda8083_read_ucblocks, 470 471 .diseqc_send_master_cmd = tda8083_send_diseqc_msg, 472 .diseqc_send_burst = tda8083_diseqc_send_burst, 473 .set_tone = tda8083_diseqc_set_tone, 474 .set_voltage = tda8083_diseqc_set_voltage, 475}; 476 477module_param(debug, int, 0644); 478MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); 479 480MODULE_DESCRIPTION("Philips TDA8083 DVB-S Demodulator"); 481MODULE_AUTHOR("Ralph Metzler, Holger Waechtler"); 482MODULE_LICENSE("GPL"); 483 484EXPORT_SYMBOL(tda8083_attach); 485