linux/drivers/media/dvb-frontends/stv6110.c
<<
>>
Prefs
   1/*
   2 * stv6110.c
   3 *
   4 * Driver for ST STV6110 satellite tuner IC.
   5 *
   6 * Copyright (C) 2009 NetUP Inc.
   7 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; either version 2 of the License, or
  12 * (at your option) any later version.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 *
  18 * GNU General Public License for more details.
  19 */
  20
  21#include <linux/slab.h>
  22#include <linux/module.h>
  23#include <linux/dvb/frontend.h>
  24
  25#include <linux/types.h>
  26
  27#include "stv6110.h"
  28
  29/* Max transfer size done by I2C transfer functions */
  30#define MAX_XFER_SIZE  64
  31
  32static int debug;
  33
  34struct stv6110_priv {
  35        int i2c_address;
  36        struct i2c_adapter *i2c;
  37
  38        u32 mclk;
  39        u8 clk_div;
  40        u8 gain;
  41        u8 regs[8];
  42};
  43
  44#define dprintk(args...) \
  45        do { \
  46                if (debug) \
  47                        printk(KERN_DEBUG args); \
  48        } while (0)
  49
  50static s32 abssub(s32 a, s32 b)
  51{
  52        if (a > b)
  53                return a - b;
  54        else
  55                return b - a;
  56};
  57
  58static void stv6110_release(struct dvb_frontend *fe)
  59{
  60        kfree(fe->tuner_priv);
  61        fe->tuner_priv = NULL;
  62}
  63
  64static int stv6110_write_regs(struct dvb_frontend *fe, u8 buf[],
  65                                                        int start, int len)
  66{
  67        struct stv6110_priv *priv = fe->tuner_priv;
  68        int rc;
  69        u8 cmdbuf[MAX_XFER_SIZE];
  70        struct i2c_msg msg = {
  71                .addr   = priv->i2c_address,
  72                .flags  = 0,
  73                .buf    = cmdbuf,
  74                .len    = len + 1
  75        };
  76
  77        dprintk("%s\n", __func__);
  78
  79        if (1 + len > sizeof(cmdbuf)) {
  80                printk(KERN_WARNING
  81                       "%s: i2c wr: len=%d is too big!\n",
  82                       KBUILD_MODNAME, len);
  83                return -EINVAL;
  84        }
  85
  86        if (start + len > 8)
  87                return -EINVAL;
  88
  89        memcpy(&cmdbuf[1], buf, len);
  90        cmdbuf[0] = start;
  91
  92        if (fe->ops.i2c_gate_ctrl)
  93                fe->ops.i2c_gate_ctrl(fe, 1);
  94
  95        rc = i2c_transfer(priv->i2c, &msg, 1);
  96        if (rc != 1)
  97                dprintk("%s: i2c error\n", __func__);
  98
  99        if (fe->ops.i2c_gate_ctrl)
 100                fe->ops.i2c_gate_ctrl(fe, 0);
 101
 102        return 0;
 103}
 104
 105static int stv6110_read_regs(struct dvb_frontend *fe, u8 regs[],
 106                                                        int start, int len)
 107{
 108        struct stv6110_priv *priv = fe->tuner_priv;
 109        int rc;
 110        u8 reg[] = { start };
 111        struct i2c_msg msg[] = {
 112                {
 113                        .addr   = priv->i2c_address,
 114                        .flags  = 0,
 115                        .buf    = reg,
 116                        .len    = 1,
 117                }, {
 118                        .addr   = priv->i2c_address,
 119                        .flags  = I2C_M_RD,
 120                        .buf    = regs,
 121                        .len    = len,
 122                },
 123        };
 124
 125        if (fe->ops.i2c_gate_ctrl)
 126                fe->ops.i2c_gate_ctrl(fe, 1);
 127
 128        rc = i2c_transfer(priv->i2c, msg, 2);
 129        if (rc != 2)
 130                dprintk("%s: i2c error\n", __func__);
 131
 132        if (fe->ops.i2c_gate_ctrl)
 133                fe->ops.i2c_gate_ctrl(fe, 0);
 134
 135        memcpy(&priv->regs[start], regs, len);
 136
 137        return 0;
 138}
 139
 140static int stv6110_read_reg(struct dvb_frontend *fe, int start)
 141{
 142        u8 buf[] = { 0 };
 143        stv6110_read_regs(fe, buf, start, 1);
 144
 145        return buf[0];
 146}
 147
 148static int stv6110_sleep(struct dvb_frontend *fe)
 149{
 150        u8 reg[] = { 0 };
 151        stv6110_write_regs(fe, reg, 0, 1);
 152
 153        return 0;
 154}
 155
 156static u32 carrier_width(u32 symbol_rate, enum fe_rolloff rolloff)
 157{
 158        u32 rlf;
 159
 160        switch (rolloff) {
 161        case ROLLOFF_20:
 162                rlf = 20;
 163                break;
 164        case ROLLOFF_25:
 165                rlf = 25;
 166                break;
 167        default:
 168                rlf = 35;
 169                break;
 170        }
 171
 172        return symbol_rate  + ((symbol_rate * rlf) / 100);
 173}
 174
 175static int stv6110_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
 176{
 177        struct stv6110_priv *priv = fe->tuner_priv;
 178        u8 r8, ret = 0x04;
 179        int i;
 180
 181        if ((bandwidth / 2) > 36000000) /*BW/2 max=31+5=36 mhz for r8=31*/
 182                r8 = 31;
 183        else if ((bandwidth / 2) < 5000000) /* BW/2 min=5Mhz for F=0 */
 184                r8 = 0;
 185        else /*if 5 < BW/2 < 36*/
 186                r8 = (bandwidth / 2) / 1000000 - 5;
 187
 188        /* ctrl3, RCCLKOFF = 0 Activate the calibration Clock */
 189        /* ctrl3, CF = r8 Set the LPF value */
 190        priv->regs[RSTV6110_CTRL3] &= ~((1 << 6) | 0x1f);
 191        priv->regs[RSTV6110_CTRL3] |= (r8 & 0x1f);
 192        stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
 193        /* stat1, CALRCSTRT = 1 Start LPF auto calibration*/
 194        priv->regs[RSTV6110_STAT1] |= 0x02;
 195        stv6110_write_regs(fe, &priv->regs[RSTV6110_STAT1], RSTV6110_STAT1, 1);
 196
 197        i = 0;
 198        /* Wait for CALRCSTRT == 0 */
 199        while ((i < 10) && (ret != 0)) {
 200                ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x02);
 201                mdelay(1);      /* wait for LPF auto calibration */
 202                i++;
 203        }
 204
 205        /* RCCLKOFF = 1 calibration done, desactivate the calibration Clock */
 206        priv->regs[RSTV6110_CTRL3] |= (1 << 6);
 207        stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
 208        return 0;
 209}
 210
 211static int stv6110_init(struct dvb_frontend *fe)
 212{
 213        struct stv6110_priv *priv = fe->tuner_priv;
 214        u8 buf0[] = { 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
 215
 216        memcpy(priv->regs, buf0, 8);
 217        /* K = (Reference / 1000000) - 16 */
 218        priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
 219        priv->regs[RSTV6110_CTRL1] |=
 220                                ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
 221
 222        /* divisor value for the output clock */
 223        priv->regs[RSTV6110_CTRL2] &= ~0xc0;
 224        priv->regs[RSTV6110_CTRL2] |= (priv->clk_div << 6);
 225
 226        stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1], RSTV6110_CTRL1, 8);
 227        msleep(1);
 228        stv6110_set_bandwidth(fe, 72000000);
 229
 230        return 0;
 231}
 232
 233static int stv6110_get_frequency(struct dvb_frontend *fe, u32 *frequency)
 234{
 235        struct stv6110_priv *priv = fe->tuner_priv;
 236        u32 nbsteps, divider, psd2, freq;
 237        u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 238
 239        stv6110_read_regs(fe, regs, 0, 8);
 240        /*N*/
 241        divider = (priv->regs[RSTV6110_TUNING2] & 0x0f) << 8;
 242        divider += priv->regs[RSTV6110_TUNING1];
 243
 244        /*R*/
 245        nbsteps  = (priv->regs[RSTV6110_TUNING2] >> 6) & 3;
 246        /*p*/
 247        psd2  = (priv->regs[RSTV6110_TUNING2] >> 4) & 1;
 248
 249        freq = divider * (priv->mclk / 1000);
 250        freq /= (1 << (nbsteps + psd2));
 251        freq /= 4;
 252
 253        *frequency = freq;
 254
 255        return 0;
 256}
 257
 258static int stv6110_set_frequency(struct dvb_frontend *fe, u32 frequency)
 259{
 260        struct stv6110_priv *priv = fe->tuner_priv;
 261        u8 ret = 0x04;
 262        u32 divider, ref, p, presc, i, result_freq, vco_freq;
 263        s32 p_calc, p_calc_opt = 1000, r_div, r_div_opt = 0, p_val;
 264
 265        dprintk("%s, freq=%d kHz, mclk=%d Hz\n", __func__,
 266                                                frequency, priv->mclk);
 267
 268        /* K = (Reference / 1000000) - 16 */
 269        priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
 270        priv->regs[RSTV6110_CTRL1] |=
 271                                ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
 272
 273        /* BB_GAIN = db/2 */
 274        priv->regs[RSTV6110_CTRL2] &= ~0x0f;
 275        priv->regs[RSTV6110_CTRL2] |= (priv->gain & 0x0f);
 276
 277        if (frequency <= 1023000) {
 278                p = 1;
 279                presc = 0;
 280        } else if (frequency <= 1300000) {
 281                p = 1;
 282                presc = 1;
 283        } else if (frequency <= 2046000) {
 284                p = 0;
 285                presc = 0;
 286        } else {
 287                p = 0;
 288                presc = 1;
 289        }
 290        /* DIV4SEL = p*/
 291        priv->regs[RSTV6110_TUNING2] &= ~(1 << 4);
 292        priv->regs[RSTV6110_TUNING2] |= (p << 4);
 293
 294        /* PRESC32ON = presc */
 295        priv->regs[RSTV6110_TUNING2] &= ~(1 << 5);
 296        priv->regs[RSTV6110_TUNING2] |= (presc << 5);
 297
 298        p_val = (int)(1 << (p + 1)) * 10;/* P = 2 or P = 4 */
 299        for (r_div = 0; r_div <= 3; r_div++) {
 300                p_calc = (priv->mclk / 100000);
 301                p_calc /= (1 << (r_div + 1));
 302                if ((abssub(p_calc, p_val)) < (abssub(p_calc_opt, p_val)))
 303                        r_div_opt = r_div;
 304
 305                p_calc_opt = (priv->mclk / 100000);
 306                p_calc_opt /= (1 << (r_div_opt + 1));
 307        }
 308
 309        ref = priv->mclk / ((1 << (r_div_opt + 1))  * (1 << (p + 1)));
 310        divider = (((frequency * 1000) + (ref >> 1)) / ref);
 311
 312        /* RDIV = r_div_opt */
 313        priv->regs[RSTV6110_TUNING2] &= ~(3 << 6);
 314        priv->regs[RSTV6110_TUNING2] |= (((r_div_opt) & 3) << 6);
 315
 316        /* NDIV_MSB = MSB(divider) */
 317        priv->regs[RSTV6110_TUNING2] &= ~0x0f;
 318        priv->regs[RSTV6110_TUNING2] |= (((divider) >> 8) & 0x0f);
 319
 320        /* NDIV_LSB, LSB(divider) */
 321        priv->regs[RSTV6110_TUNING1] = (divider & 0xff);
 322
 323        /* CALVCOSTRT = 1 VCO Auto Calibration */
 324        priv->regs[RSTV6110_STAT1] |= 0x04;
 325        stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1],
 326                                                RSTV6110_CTRL1, 8);
 327
 328        i = 0;
 329        /* Wait for CALVCOSTRT == 0 */
 330        while ((i < 10) && (ret != 0)) {
 331                ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x04);
 332                msleep(1); /* wait for VCO auto calibration */
 333                i++;
 334        }
 335
 336        ret = stv6110_read_reg(fe, RSTV6110_STAT1);
 337        stv6110_get_frequency(fe, &result_freq);
 338
 339        vco_freq = divider * ((priv->mclk / 1000) / ((1 << (r_div_opt + 1))));
 340        dprintk("%s, stat1=%x, lo_freq=%d kHz, vco_frec=%d kHz\n", __func__,
 341                                                ret, result_freq, vco_freq);
 342
 343        return 0;
 344}
 345
 346static int stv6110_set_params(struct dvb_frontend *fe)
 347{
 348        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
 349        u32 bandwidth = carrier_width(c->symbol_rate, c->rolloff);
 350
 351        stv6110_set_frequency(fe, c->frequency);
 352        stv6110_set_bandwidth(fe, bandwidth);
 353
 354        return 0;
 355}
 356
 357static int stv6110_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
 358{
 359        struct stv6110_priv *priv = fe->tuner_priv;
 360        u8 r8 = 0;
 361        u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 362        stv6110_read_regs(fe, regs, 0, 8);
 363
 364        /* CF */
 365        r8 = priv->regs[RSTV6110_CTRL3] & 0x1f;
 366        *bandwidth = (r8 + 5) * 2000000;/* x2 for ZIF tuner BW/2 = F+5 Mhz */
 367
 368        return 0;
 369}
 370
 371static const struct dvb_tuner_ops stv6110_tuner_ops = {
 372        .info = {
 373                .name = "ST STV6110",
 374                .frequency_min_hz  =  950 * MHz,
 375                .frequency_max_hz  = 2150 * MHz,
 376                .frequency_step_hz =    1 * MHz,
 377        },
 378        .init = stv6110_init,
 379        .release = stv6110_release,
 380        .sleep = stv6110_sleep,
 381        .set_params = stv6110_set_params,
 382        .get_frequency = stv6110_get_frequency,
 383        .set_frequency = stv6110_set_frequency,
 384        .get_bandwidth = stv6110_get_bandwidth,
 385        .set_bandwidth = stv6110_set_bandwidth,
 386
 387};
 388
 389struct dvb_frontend *stv6110_attach(struct dvb_frontend *fe,
 390                                        const struct stv6110_config *config,
 391                                        struct i2c_adapter *i2c)
 392{
 393        struct stv6110_priv *priv = NULL;
 394        u8 reg0[] = { 0x00, 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
 395
 396        struct i2c_msg msg[] = {
 397                {
 398                        .addr = config->i2c_address,
 399                        .flags = 0,
 400                        .buf = reg0,
 401                        .len = 9
 402                }
 403        };
 404        int ret;
 405
 406        /* divisor value for the output clock */
 407        reg0[2] &= ~0xc0;
 408        reg0[2] |= (config->clk_div << 6);
 409
 410        if (fe->ops.i2c_gate_ctrl)
 411                fe->ops.i2c_gate_ctrl(fe, 1);
 412
 413        ret = i2c_transfer(i2c, msg, 1);
 414
 415        if (fe->ops.i2c_gate_ctrl)
 416                fe->ops.i2c_gate_ctrl(fe, 0);
 417
 418        if (ret != 1)
 419                return NULL;
 420
 421        priv = kzalloc(sizeof(struct stv6110_priv), GFP_KERNEL);
 422        if (priv == NULL)
 423                return NULL;
 424
 425        priv->i2c_address = config->i2c_address;
 426        priv->i2c = i2c;
 427        priv->mclk = config->mclk;
 428        priv->clk_div = config->clk_div;
 429        priv->gain = config->gain;
 430
 431        memcpy(&priv->regs, &reg0[1], 8);
 432
 433        memcpy(&fe->ops.tuner_ops, &stv6110_tuner_ops,
 434                                sizeof(struct dvb_tuner_ops));
 435        fe->tuner_priv = priv;
 436        printk(KERN_INFO "STV6110 attached on addr=%x!\n", priv->i2c_address);
 437
 438        return fe;
 439}
 440EXPORT_SYMBOL(stv6110_attach);
 441
 442module_param(debug, int, 0644);
 443MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
 444
 445MODULE_DESCRIPTION("ST STV6110 driver");
 446MODULE_AUTHOR("Igor M. Liplianin");
 447MODULE_LICENSE("GPL");
 448