2      Driver for Philips tda10086 DVBS Demodulator
 
   4      (c) 2006 Andrew de Quincey
 
   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.
 
  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
 
  15      GNU General Public License for more details.
 
  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.
 
  23 #include <linux/init.h>
 
  24 #include <linux/module.h>
 
  25 #include <linux/device.h>
 
  26 #include <linux/jiffies.h>
 
  27 #include <linux/string.h>
 
  28 #include <linux/slab.h>
 
  30 #include "dvb_frontend.h"
 
  33 #define SACLK 96000000
 
  35 struct tda10086_state {
 
  36         struct i2c_adapter* i2c;
 
  37         const struct tda10086_config* config;
 
  38         struct dvb_frontend frontend;
 
  40         /* private demod data */
 
  47 #define dprintk(args...) \
 
  49                 if (debug) printk(KERN_DEBUG "tda10086: " args); \
 
  52 static int tda10086_write_byte(struct tda10086_state *state, int reg, int data)
 
  55         u8 b0[] = { reg, data };
 
  56         struct i2c_msg msg = { .flags = 0, .buf = b0, .len = 2 };
 
  58         msg.addr = state->config->demod_address;
 
  59         ret = i2c_transfer(state->i2c, &msg, 1);
 
  62                 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
 
  63                         __func__, reg, data, ret);
 
  65         return (ret != 1) ? ret : 0;
 
  68 static int tda10086_read_byte(struct tda10086_state *state, int reg)
 
  73         struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
 
  74                                 { .flags = I2C_M_RD, .buf = b1, .len = 1 }};
 
  76         msg[0].addr = state->config->demod_address;
 
  77         msg[1].addr = state->config->demod_address;
 
  78         ret = i2c_transfer(state->i2c, msg, 2);
 
  81                 dprintk("%s: error reg=0x%x, ret=%i\n", __func__, reg,
 
  89 static int tda10086_write_mask(struct tda10086_state *state, int reg, int mask, int data)
 
  93         /* read a byte and check */
 
  94         val = tda10086_read_byte(state, reg);
 
 102         /* write it out again */
 
 103         return tda10086_write_byte(state, reg, val);
 
 106 static int tda10086_init(struct dvb_frontend* fe)
 
 108         struct tda10086_state* state = fe->demodulator_priv;
 
 111         dprintk ("%s\n", __func__);
 
 113         if (state->config->diseqc_tone)
 
 116         tda10086_write_byte(state, 0x00, 0x00);
 
 120         tda10086_write_byte(state, 0x01, 0x94);
 
 121         tda10086_write_byte(state, 0x02, 0x35); /* NOTE: TT drivers appear to disable CSWP */
 
 122         tda10086_write_byte(state, 0x03, 0xe4);
 
 123         tda10086_write_byte(state, 0x04, 0x43);
 
 124         tda10086_write_byte(state, 0x0c, 0x0c);
 
 125         tda10086_write_byte(state, 0x1b, 0xb0); /* noise threshold */
 
 126         tda10086_write_byte(state, 0x20, 0x89); /* misc */
 
 127         tda10086_write_byte(state, 0x30, 0x04); /* acquisition period length */
 
 128         tda10086_write_byte(state, 0x32, 0x00); /* irq off */
 
 129         tda10086_write_byte(state, 0x31, 0x56); /* setup AFC */
 
 131         /* setup PLL (this assumes SACLK = 96MHz) */
 
 132         tda10086_write_byte(state, 0x55, 0x2c); /* misc PLL setup */
 
 133         if (state->config->xtal_freq == TDA10086_XTAL_16M) {
 
 134                 tda10086_write_byte(state, 0x3a, 0x0b); /* M=12 */
 
 135                 tda10086_write_byte(state, 0x3b, 0x01); /* P=2 */
 
 137                 tda10086_write_byte(state, 0x3a, 0x17); /* M=24 */
 
 138                 tda10086_write_byte(state, 0x3b, 0x00); /* P=1 */
 
 140         tda10086_write_mask(state, 0x55, 0x20, 0x00); /* powerup PLL */
 
 142         /* setup TS interface */
 
 143         tda10086_write_byte(state, 0x11, 0x81);
 
 144         tda10086_write_byte(state, 0x12, 0x81);
 
 145         tda10086_write_byte(state, 0x19, 0x40); /* parallel mode A + MSBFIRST */
 
 146         tda10086_write_byte(state, 0x56, 0x80); /* powerdown WPLL - unused in the mode we use */
 
 147         tda10086_write_byte(state, 0x57, 0x08); /* bypass WPLL - unused in the mode we use */
 
 148         tda10086_write_byte(state, 0x10, 0x2a);
 
 151         tda10086_write_byte(state, 0x58, 0x61); /* ADC setup */
 
 152         tda10086_write_mask(state, 0x58, 0x01, 0x00); /* powerup ADC */
 
 155         tda10086_write_byte(state, 0x05, 0x0B);
 
 156         tda10086_write_byte(state, 0x37, 0x63);
 
 157         tda10086_write_byte(state, 0x3f, 0x0a); /* NOTE: flydvb varies it */
 
 158         tda10086_write_byte(state, 0x40, 0x64);
 
 159         tda10086_write_byte(state, 0x41, 0x4f);
 
 160         tda10086_write_byte(state, 0x42, 0x43);
 
 163         tda10086_write_byte(state, 0x1a, 0x11); /* VBER 10^6, DVB, QPSK */
 
 165         /* setup carrier recovery */
 
 166         tda10086_write_byte(state, 0x3d, 0x80);
 
 169         tda10086_write_byte(state, 0x36, t22k_off); /* all SEC off, 22k tone */
 
 170         tda10086_write_byte(state, 0x34, (((1<<19) * (22000/1000)) / (SACLK/1000)));
 
 171         tda10086_write_byte(state, 0x35, (((1<<19) * (22000/1000)) / (SACLK/1000)) >> 8);
 
 176 static void tda10086_diseqc_wait(struct tda10086_state *state)
 
 178         unsigned long timeout = jiffies + msecs_to_jiffies(200);
 
 179         while (!(tda10086_read_byte(state, 0x50) & 0x01)) {
 
 180                 if(time_after(jiffies, timeout)) {
 
 181                         printk("%s: diseqc queue not ready, command may be lost.\n", __func__);
 
 188 static int tda10086_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
 
 190         struct tda10086_state* state = fe->demodulator_priv;
 
 193         dprintk ("%s\n", __func__);
 
 195         if (state->config->diseqc_tone)
 
 200                 tda10086_write_byte(state, 0x36, t22k_off);
 
 204                 tda10086_write_byte(state, 0x36, 0x01 + t22k_off);
 
 211 static int tda10086_send_master_cmd (struct dvb_frontend* fe,
 
 212                                     struct dvb_diseqc_master_cmd* cmd)
 
 214         struct tda10086_state* state = fe->demodulator_priv;
 
 219         dprintk ("%s\n", __func__);
 
 221         if (state->config->diseqc_tone)
 
 224         if (cmd->msg_len > 6)
 
 226         oldval = tda10086_read_byte(state, 0x36);
 
 228         for(i=0; i< cmd->msg_len; i++) {
 
 229                 tda10086_write_byte(state, 0x48+i, cmd->msg[i]);
 
 231         tda10086_write_byte(state, 0x36, (0x08 + t22k_off)
 
 232                                         | ((cmd->msg_len - 1) << 4));
 
 234         tda10086_diseqc_wait(state);
 
 236         tda10086_write_byte(state, 0x36, oldval);
 
 241 static int tda10086_send_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
 
 243         struct tda10086_state* state = fe->demodulator_priv;
 
 244         u8 oldval = tda10086_read_byte(state, 0x36);
 
 247         dprintk ("%s\n", __func__);
 
 249         if (state->config->diseqc_tone)
 
 254                 tda10086_write_byte(state, 0x36, 0x04 + t22k_off);
 
 258                 tda10086_write_byte(state, 0x36, 0x06 + t22k_off);
 
 262         tda10086_diseqc_wait(state);
 
 264         tda10086_write_byte(state, 0x36, oldval);
 
 269 static int tda10086_set_inversion(struct tda10086_state *state,
 
 270                                   struct dvb_frontend_parameters *fe_params)
 
 274         dprintk ("%s %i %i\n", __func__, fe_params->inversion, state->config->invert);
 
 276         switch(fe_params->inversion) {
 
 278                 if (state->config->invert)
 
 282                 if (!state->config->invert)
 
 289         tda10086_write_mask(state, 0x0c, 0xc0, invval);
 
 294 static int tda10086_set_symbol_rate(struct tda10086_state *state,
 
 295                                     struct dvb_frontend_parameters *fe_params)
 
 306         u32 symbol_rate = fe_params->u.qpsk.symbol_rate;
 
 308         dprintk ("%s %i\n", __func__, symbol_rate);
 
 310         /* setup the decimation and anti-aliasing filters.. */
 
 311         if (symbol_rate < (u32) (SACLK * 0.0137)) {
 
 314         } else if (symbol_rate < (u32) (SACLK * 0.0208)) {
 
 317         } else if (symbol_rate < (u32) (SACLK * 0.0270)) {
 
 320         } else if (symbol_rate < (u32) (SACLK * 0.0416)) {
 
 323         } else if (symbol_rate < (u32) (SACLK * 0.0550)) {
 
 326         } else if (symbol_rate < (u32) (SACLK * 0.0833)) {
 
 329         } else if (symbol_rate < (u32) (SACLK * 0.1100)) {
 
 332         } else if (symbol_rate < (u32) (SACLK * 0.1666)) {
 
 335         } else if (symbol_rate < (u32) (SACLK * 0.2200)) {
 
 338         } else if (symbol_rate < (u32) (SACLK * 0.3333)) {
 
 348         big = (1ULL<<21) * ((u64) symbol_rate/1000ULL) * (1ULL<<dfn);
 
 349         big += ((SACLK/1000ULL)-1ULL);
 
 350         do_div(big, (SACLK/1000ULL));
 
 354         tmp = (1<<dfn)*(symbol_rate/1000);
 
 355         bdri = ((32 * (SACLK/1000)) + (tmp-1)) / tmp;
 
 357         tda10086_write_byte(state, 0x21, (afs << 7) | dfn);
 
 358         tda10086_write_mask(state, 0x20, 0x08, byp << 3);
 
 359         tda10086_write_byte(state, 0x06, bdr);
 
 360         tda10086_write_byte(state, 0x07, bdr >> 8);
 
 361         tda10086_write_byte(state, 0x08, bdr >> 16);
 
 362         tda10086_write_byte(state, 0x09, bdri);
 
 363         tda10086_write_byte(state, 0x37, reg37);
 
 364         tda10086_write_byte(state, 0x42, reg42);
 
 369 static int tda10086_set_fec(struct tda10086_state *state,
 
 370                             struct dvb_frontend_parameters *fe_params)
 
 374         dprintk ("%s %i\n", __func__, fe_params->u.qpsk.fec_inner);
 
 376         switch(fe_params->u.qpsk.fec_inner) {
 
 407         tda10086_write_byte(state, 0x0d, fecval);
 
 412 static int tda10086_set_frontend(struct dvb_frontend* fe,
 
 413                                  struct dvb_frontend_parameters *fe_params)
 
 415         struct tda10086_state *state = fe->demodulator_priv;
 
 420         dprintk ("%s\n", __func__);
 
 422         /* modify parameters for tuning */
 
 423         tda10086_write_byte(state, 0x02, 0x35);
 
 424         state->has_lock = false;
 
 427         if (fe->ops.tuner_ops.set_params) {
 
 428                 fe->ops.tuner_ops.set_params(fe, fe_params);
 
 429                 if (fe->ops.i2c_gate_ctrl)
 
 430                         fe->ops.i2c_gate_ctrl(fe, 0);
 
 432                 if (fe->ops.tuner_ops.get_frequency)
 
 433                         fe->ops.tuner_ops.get_frequency(fe, &freq);
 
 434                 if (fe->ops.i2c_gate_ctrl)
 
 435                         fe->ops.i2c_gate_ctrl(fe, 0);
 
 438         /* calcluate the frequency offset (in *Hz* not kHz) */
 
 439         freqoff = fe_params->frequency - freq;
 
 440         freqoff = ((1<<16) * freqoff) / (SACLK/1000);
 
 441         tda10086_write_byte(state, 0x3d, 0x80 | ((freqoff >> 8) & 0x7f));
 
 442         tda10086_write_byte(state, 0x3e, freqoff);
 
 444         if ((ret = tda10086_set_inversion(state, fe_params)) < 0)
 
 446         if ((ret = tda10086_set_symbol_rate(state, fe_params)) < 0)
 
 448         if ((ret = tda10086_set_fec(state, fe_params)) < 0)
 
 451         /* soft reset + disable TS output until lock */
 
 452         tda10086_write_mask(state, 0x10, 0x40, 0x40);
 
 453         tda10086_write_mask(state, 0x00, 0x01, 0x00);
 
 455         state->symbol_rate = fe_params->u.qpsk.symbol_rate;
 
 456         state->frequency = fe_params->frequency;
 
 460 static int tda10086_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *fe_params)
 
 462         struct tda10086_state* state = fe->demodulator_priv;
 
 467         dprintk ("%s\n", __func__);
 
 469         /* check for invalid symbol rate */
 
 470         if (fe_params->u.qpsk.symbol_rate < 500000)
 
 473         /* calculate the updated frequency (note: we convert from Hz->kHz) */
 
 474         tmp64 = tda10086_read_byte(state, 0x52);
 
 475         tmp64 |= (tda10086_read_byte(state, 0x51) << 8);
 
 477                 tmp64 |= 0xffffffffffff0000ULL;
 
 478         tmp64 = (tmp64 * (SACLK/1000ULL));
 
 479         do_div(tmp64, (1ULL<<15) * (1ULL<<1));
 
 480         fe_params->frequency = (int) state->frequency + (int) tmp64;
 
 483         val = tda10086_read_byte(state, 0x0c);
 
 487                         fe_params->inversion = INVERSION_OFF;
 
 488                         if (state->config->invert)
 
 489                                 fe_params->inversion = INVERSION_ON;
 
 492                         fe_params->inversion = INVERSION_ON;
 
 493                         if (state->config->invert)
 
 494                                 fe_params->inversion = INVERSION_OFF;
 
 498                 tda10086_read_byte(state, 0x0f);
 
 501                         fe_params->inversion = INVERSION_OFF;
 
 502                         if (state->config->invert)
 
 503                                 fe_params->inversion = INVERSION_ON;
 
 506                         fe_params->inversion = INVERSION_ON;
 
 507                         if (state->config->invert)
 
 508                                 fe_params->inversion = INVERSION_OFF;
 
 513         /* calculate the updated symbol rate */
 
 514         tmp = tda10086_read_byte(state, 0x1d);
 
 517         tmp = (tmp * 480 * (1<<1)) / 128;
 
 518         tmp = ((state->symbol_rate/1000) * tmp) / (1000000/1000);
 
 519         fe_params->u.qpsk.symbol_rate = state->symbol_rate + tmp;
 
 522         val = (tda10086_read_byte(state, 0x0d) & 0x70) >> 4;
 
 525                 fe_params->u.qpsk.fec_inner = FEC_1_2;
 
 528                 fe_params->u.qpsk.fec_inner = FEC_2_3;
 
 531                 fe_params->u.qpsk.fec_inner = FEC_3_4;
 
 534                 fe_params->u.qpsk.fec_inner = FEC_4_5;
 
 537                 fe_params->u.qpsk.fec_inner = FEC_5_6;
 
 540                 fe_params->u.qpsk.fec_inner = FEC_6_7;
 
 543                 fe_params->u.qpsk.fec_inner = FEC_7_8;
 
 546                 fe_params->u.qpsk.fec_inner = FEC_8_9;
 
 553 static int tda10086_read_status(struct dvb_frontend* fe, fe_status_t *fe_status)
 
 555         struct tda10086_state* state = fe->demodulator_priv;
 
 558         dprintk ("%s\n", __func__);
 
 560         val = tda10086_read_byte(state, 0x0e);
 
 563                 *fe_status |= FE_HAS_SIGNAL;
 
 565                 *fe_status |= FE_HAS_CARRIER;
 
 567                 *fe_status |= FE_HAS_VITERBI;
 
 569                 *fe_status |= FE_HAS_SYNC;
 
 571                 *fe_status |= FE_HAS_LOCK;
 
 572                 if (!state->has_lock) {
 
 573                         state->has_lock = true;
 
 574                         /* modify parameters for stable reception */
 
 575                         tda10086_write_byte(state, 0x02, 0x00);
 
 582 static int tda10086_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
 
 584         struct tda10086_state* state = fe->demodulator_priv;
 
 587         dprintk ("%s\n", __func__);
 
 589         _str = 0xff - tda10086_read_byte(state, 0x43);
 
 590         *signal = (_str << 8) | _str;
 
 595 static int tda10086_read_snr(struct dvb_frontend* fe, u16 * snr)
 
 597         struct tda10086_state* state = fe->demodulator_priv;
 
 600         dprintk ("%s\n", __func__);
 
 602         _snr = 0xff - tda10086_read_byte(state, 0x1c);
 
 603         *snr = (_snr << 8) | _snr;
 
 608 static int tda10086_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
 
 610         struct tda10086_state* state = fe->demodulator_priv;
 
 612         dprintk ("%s\n", __func__);
 
 615         *ucblocks = tda10086_read_byte(state, 0x18) & 0x7f;
 
 618         tda10086_write_byte(state, 0x18, 0x00);
 
 619         tda10086_write_byte(state, 0x18, 0x80);
 
 624 static int tda10086_read_ber(struct dvb_frontend* fe, u32* ber)
 
 626         struct tda10086_state* state = fe->demodulator_priv;
 
 628         dprintk ("%s\n", __func__);
 
 632         *ber |= tda10086_read_byte(state, 0x15);
 
 633         *ber |= tda10086_read_byte(state, 0x16) << 8;
 
 634         *ber |= (tda10086_read_byte(state, 0x17) & 0xf) << 16;
 
 639 static int tda10086_sleep(struct dvb_frontend* fe)
 
 641         struct tda10086_state* state = fe->demodulator_priv;
 
 643         dprintk ("%s\n", __func__);
 
 645         tda10086_write_mask(state, 0x00, 0x08, 0x08);
 
 650 static int tda10086_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
 
 652         struct tda10086_state* state = fe->demodulator_priv;
 
 654         dprintk ("%s\n", __func__);
 
 657                 tda10086_write_mask(state, 0x00, 0x10, 0x10);
 
 659                 tda10086_write_mask(state, 0x00, 0x10, 0x00);
 
 665 static int tda10086_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
 
 667         if (fesettings->parameters.u.qpsk.symbol_rate > 20000000) {
 
 668                 fesettings->min_delay_ms = 50;
 
 669                 fesettings->step_size = 2000;
 
 670                 fesettings->max_drift = 8000;
 
 671         } else if (fesettings->parameters.u.qpsk.symbol_rate > 12000000) {
 
 672                 fesettings->min_delay_ms = 100;
 
 673                 fesettings->step_size = 1500;
 
 674                 fesettings->max_drift = 9000;
 
 675         } else if (fesettings->parameters.u.qpsk.symbol_rate > 8000000) {
 
 676                 fesettings->min_delay_ms = 100;
 
 677                 fesettings->step_size = 1000;
 
 678                 fesettings->max_drift = 8000;
 
 679         } else if (fesettings->parameters.u.qpsk.symbol_rate > 4000000) {
 
 680                 fesettings->min_delay_ms = 100;
 
 681                 fesettings->step_size = 500;
 
 682                 fesettings->max_drift = 7000;
 
 683         } else if (fesettings->parameters.u.qpsk.symbol_rate > 2000000) {
 
 684                 fesettings->min_delay_ms = 200;
 
 685                 fesettings->step_size = (fesettings->parameters.u.qpsk.symbol_rate / 8000);
 
 686                 fesettings->max_drift = 14 * fesettings->step_size;
 
 688                 fesettings->min_delay_ms = 200;
 
 689                 fesettings->step_size = (fesettings->parameters.u.qpsk.symbol_rate / 8000);
 
 690                 fesettings->max_drift = 18 * fesettings->step_size;
 
 696 static void tda10086_release(struct dvb_frontend* fe)
 
 698         struct tda10086_state *state = fe->demodulator_priv;
 
 703 static struct dvb_frontend_ops tda10086_ops = {
 
 706                 .name     = "Philips TDA10086 DVB-S",
 
 708                 .frequency_min    = 950000,
 
 709                 .frequency_max    = 2150000,
 
 710                 .frequency_stepsize = 125,     /* kHz for QPSK frontends */
 
 711                 .symbol_rate_min  = 1000000,
 
 712                 .symbol_rate_max  = 45000000,
 
 713                 .caps = FE_CAN_INVERSION_AUTO |
 
 714                         FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
 
 715                         FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
 
 719         .release = tda10086_release,
 
 721         .init = tda10086_init,
 
 722         .sleep = tda10086_sleep,
 
 723         .i2c_gate_ctrl = tda10086_i2c_gate_ctrl,
 
 725         .set_frontend = tda10086_set_frontend,
 
 726         .get_frontend = tda10086_get_frontend,
 
 727         .get_tune_settings = tda10086_get_tune_settings,
 
 729         .read_status = tda10086_read_status,
 
 730         .read_ber = tda10086_read_ber,
 
 731         .read_signal_strength = tda10086_read_signal_strength,
 
 732         .read_snr = tda10086_read_snr,
 
 733         .read_ucblocks = tda10086_read_ucblocks,
 
 735         .diseqc_send_master_cmd = tda10086_send_master_cmd,
 
 736         .diseqc_send_burst = tda10086_send_burst,
 
 737         .set_tone = tda10086_set_tone,
 
 740 struct dvb_frontend* tda10086_attach(const struct tda10086_config* config,
 
 741                                      struct i2c_adapter* i2c)
 
 743         struct tda10086_state *state;
 
 745         dprintk ("%s\n", __func__);
 
 747         /* allocate memory for the internal state */
 
 748         state = kmalloc(sizeof(struct tda10086_state), GFP_KERNEL);
 
 752         /* setup the state */
 
 753         state->config = config;
 
 756         /* check if the demod is there */
 
 757         if (tda10086_read_byte(state, 0x1e) != 0xe1) {
 
 762         /* create dvb_frontend */
 
 763         memcpy(&state->frontend.ops, &tda10086_ops, sizeof(struct dvb_frontend_ops));
 
 764         state->frontend.demodulator_priv = state;
 
 765         return &state->frontend;
 
 768 module_param(debug, int, 0644);
 
 769 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
 
 771 MODULE_DESCRIPTION("Philips TDA10086 DVB-S Demodulator");
 
 772 MODULE_AUTHOR("Andrew de Quincey");
 
 773 MODULE_LICENSE("GPL");
 
 775 EXPORT_SYMBOL(tda10086_attach);