Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * stv6110.c
0004  *
0005  * Driver for ST STV6110 satellite tuner IC.
0006  *
0007  * Copyright (C) 2009 NetUP Inc.
0008  * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
0009  */
0010 
0011 #include <linux/slab.h>
0012 #include <linux/module.h>
0013 #include <linux/dvb/frontend.h>
0014 
0015 #include <linux/types.h>
0016 
0017 #include "stv6110.h"
0018 
0019 /* Max transfer size done by I2C transfer functions */
0020 #define MAX_XFER_SIZE  64
0021 
0022 static int debug;
0023 
0024 struct stv6110_priv {
0025     int i2c_address;
0026     struct i2c_adapter *i2c;
0027 
0028     u32 mclk;
0029     u8 clk_div;
0030     u8 gain;
0031     u8 regs[8];
0032 };
0033 
0034 #define dprintk(args...) \
0035     do { \
0036         if (debug) \
0037             printk(KERN_DEBUG args); \
0038     } while (0)
0039 
0040 static s32 abssub(s32 a, s32 b)
0041 {
0042     if (a > b)
0043         return a - b;
0044     else
0045         return b - a;
0046 };
0047 
0048 static void stv6110_release(struct dvb_frontend *fe)
0049 {
0050     kfree(fe->tuner_priv);
0051     fe->tuner_priv = NULL;
0052 }
0053 
0054 static int stv6110_write_regs(struct dvb_frontend *fe, u8 buf[],
0055                             int start, int len)
0056 {
0057     struct stv6110_priv *priv = fe->tuner_priv;
0058     int rc;
0059     u8 cmdbuf[MAX_XFER_SIZE];
0060     struct i2c_msg msg = {
0061         .addr   = priv->i2c_address,
0062         .flags  = 0,
0063         .buf    = cmdbuf,
0064         .len    = len + 1
0065     };
0066 
0067     dprintk("%s\n", __func__);
0068 
0069     if (1 + len > sizeof(cmdbuf)) {
0070         printk(KERN_WARNING
0071                "%s: i2c wr: len=%d is too big!\n",
0072                KBUILD_MODNAME, len);
0073         return -EINVAL;
0074     }
0075 
0076     if (start + len > 8)
0077         return -EINVAL;
0078 
0079     memcpy(&cmdbuf[1], buf, len);
0080     cmdbuf[0] = start;
0081 
0082     if (fe->ops.i2c_gate_ctrl)
0083         fe->ops.i2c_gate_ctrl(fe, 1);
0084 
0085     rc = i2c_transfer(priv->i2c, &msg, 1);
0086     if (rc != 1)
0087         dprintk("%s: i2c error\n", __func__);
0088 
0089     if (fe->ops.i2c_gate_ctrl)
0090         fe->ops.i2c_gate_ctrl(fe, 0);
0091 
0092     return 0;
0093 }
0094 
0095 static int stv6110_read_regs(struct dvb_frontend *fe, u8 regs[],
0096                             int start, int len)
0097 {
0098     struct stv6110_priv *priv = fe->tuner_priv;
0099     int rc;
0100     u8 reg[] = { start };
0101     struct i2c_msg msg[] = {
0102         {
0103             .addr   = priv->i2c_address,
0104             .flags  = 0,
0105             .buf    = reg,
0106             .len    = 1,
0107         }, {
0108             .addr   = priv->i2c_address,
0109             .flags  = I2C_M_RD,
0110             .buf    = regs,
0111             .len    = len,
0112         },
0113     };
0114 
0115     if (fe->ops.i2c_gate_ctrl)
0116         fe->ops.i2c_gate_ctrl(fe, 1);
0117 
0118     rc = i2c_transfer(priv->i2c, msg, 2);
0119     if (rc != 2)
0120         dprintk("%s: i2c error\n", __func__);
0121 
0122     if (fe->ops.i2c_gate_ctrl)
0123         fe->ops.i2c_gate_ctrl(fe, 0);
0124 
0125     memcpy(&priv->regs[start], regs, len);
0126 
0127     return 0;
0128 }
0129 
0130 static int stv6110_read_reg(struct dvb_frontend *fe, int start)
0131 {
0132     u8 buf[] = { 0 };
0133     stv6110_read_regs(fe, buf, start, 1);
0134 
0135     return buf[0];
0136 }
0137 
0138 static int stv6110_sleep(struct dvb_frontend *fe)
0139 {
0140     u8 reg[] = { 0 };
0141     stv6110_write_regs(fe, reg, 0, 1);
0142 
0143     return 0;
0144 }
0145 
0146 static u32 carrier_width(u32 symbol_rate, enum fe_rolloff rolloff)
0147 {
0148     u32 rlf;
0149 
0150     switch (rolloff) {
0151     case ROLLOFF_20:
0152         rlf = 20;
0153         break;
0154     case ROLLOFF_25:
0155         rlf = 25;
0156         break;
0157     default:
0158         rlf = 35;
0159         break;
0160     }
0161 
0162     return symbol_rate  + ((symbol_rate * rlf) / 100);
0163 }
0164 
0165 static int stv6110_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
0166 {
0167     struct stv6110_priv *priv = fe->tuner_priv;
0168     u8 r8, ret = 0x04;
0169     int i;
0170 
0171     if ((bandwidth / 2) > 36000000) /*BW/2 max=31+5=36 mhz for r8=31*/
0172         r8 = 31;
0173     else if ((bandwidth / 2) < 5000000) /* BW/2 min=5Mhz for F=0 */
0174         r8 = 0;
0175     else /*if 5 < BW/2 < 36*/
0176         r8 = (bandwidth / 2) / 1000000 - 5;
0177 
0178     /* ctrl3, RCCLKOFF = 0 Activate the calibration Clock */
0179     /* ctrl3, CF = r8 Set the LPF value */
0180     priv->regs[RSTV6110_CTRL3] &= ~((1 << 6) | 0x1f);
0181     priv->regs[RSTV6110_CTRL3] |= (r8 & 0x1f);
0182     stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
0183     /* stat1, CALRCSTRT = 1 Start LPF auto calibration*/
0184     priv->regs[RSTV6110_STAT1] |= 0x02;
0185     stv6110_write_regs(fe, &priv->regs[RSTV6110_STAT1], RSTV6110_STAT1, 1);
0186 
0187     i = 0;
0188     /* Wait for CALRCSTRT == 0 */
0189     while ((i < 10) && (ret != 0)) {
0190         ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x02);
0191         mdelay(1);  /* wait for LPF auto calibration */
0192         i++;
0193     }
0194 
0195     /* RCCLKOFF = 1 calibration done, deactivate the calibration Clock */
0196     priv->regs[RSTV6110_CTRL3] |= (1 << 6);
0197     stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
0198     return 0;
0199 }
0200 
0201 static int stv6110_init(struct dvb_frontend *fe)
0202 {
0203     struct stv6110_priv *priv = fe->tuner_priv;
0204     u8 buf0[] = { 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
0205 
0206     memcpy(priv->regs, buf0, 8);
0207     /* K = (Reference / 1000000) - 16 */
0208     priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
0209     priv->regs[RSTV6110_CTRL1] |=
0210                 ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
0211 
0212     /* divisor value for the output clock */
0213     priv->regs[RSTV6110_CTRL2] &= ~0xc0;
0214     priv->regs[RSTV6110_CTRL2] |= (priv->clk_div << 6);
0215 
0216     stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1], RSTV6110_CTRL1, 8);
0217     msleep(1);
0218     stv6110_set_bandwidth(fe, 72000000);
0219 
0220     return 0;
0221 }
0222 
0223 static int stv6110_get_frequency(struct dvb_frontend *fe, u32 *frequency)
0224 {
0225     struct stv6110_priv *priv = fe->tuner_priv;
0226     u32 nbsteps, divider, psd2, freq;
0227     u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
0228 
0229     stv6110_read_regs(fe, regs, 0, 8);
0230     /*N*/
0231     divider = (priv->regs[RSTV6110_TUNING2] & 0x0f) << 8;
0232     divider += priv->regs[RSTV6110_TUNING1];
0233 
0234     /*R*/
0235     nbsteps  = (priv->regs[RSTV6110_TUNING2] >> 6) & 3;
0236     /*p*/
0237     psd2  = (priv->regs[RSTV6110_TUNING2] >> 4) & 1;
0238 
0239     freq = divider * (priv->mclk / 1000);
0240     freq /= (1 << (nbsteps + psd2));
0241     freq /= 4;
0242 
0243     *frequency = freq;
0244 
0245     return 0;
0246 }
0247 
0248 static int stv6110_set_frequency(struct dvb_frontend *fe, u32 frequency)
0249 {
0250     struct stv6110_priv *priv = fe->tuner_priv;
0251     u8 ret = 0x04;
0252     u32 divider, ref, p, presc, i, result_freq, vco_freq;
0253     s32 p_calc, p_calc_opt = 1000, r_div, r_div_opt = 0, p_val;
0254 
0255     dprintk("%s, freq=%d kHz, mclk=%d Hz\n", __func__,
0256                         frequency, priv->mclk);
0257 
0258     /* K = (Reference / 1000000) - 16 */
0259     priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
0260     priv->regs[RSTV6110_CTRL1] |=
0261                 ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
0262 
0263     /* BB_GAIN = db/2 */
0264     priv->regs[RSTV6110_CTRL2] &= ~0x0f;
0265     priv->regs[RSTV6110_CTRL2] |= (priv->gain & 0x0f);
0266 
0267     if (frequency <= 1023000) {
0268         p = 1;
0269         presc = 0;
0270     } else if (frequency <= 1300000) {
0271         p = 1;
0272         presc = 1;
0273     } else if (frequency <= 2046000) {
0274         p = 0;
0275         presc = 0;
0276     } else {
0277         p = 0;
0278         presc = 1;
0279     }
0280     /* DIV4SEL = p*/
0281     priv->regs[RSTV6110_TUNING2] &= ~(1 << 4);
0282     priv->regs[RSTV6110_TUNING2] |= (p << 4);
0283 
0284     /* PRESC32ON = presc */
0285     priv->regs[RSTV6110_TUNING2] &= ~(1 << 5);
0286     priv->regs[RSTV6110_TUNING2] |= (presc << 5);
0287 
0288     p_val = (int)(1 << (p + 1)) * 10;/* P = 2 or P = 4 */
0289     for (r_div = 0; r_div <= 3; r_div++) {
0290         p_calc = (priv->mclk / 100000);
0291         p_calc /= (1 << (r_div + 1));
0292         if ((abssub(p_calc, p_val)) < (abssub(p_calc_opt, p_val)))
0293             r_div_opt = r_div;
0294 
0295         p_calc_opt = (priv->mclk / 100000);
0296         p_calc_opt /= (1 << (r_div_opt + 1));
0297     }
0298 
0299     ref = priv->mclk / ((1 << (r_div_opt + 1))  * (1 << (p + 1)));
0300     divider = (((frequency * 1000) + (ref >> 1)) / ref);
0301 
0302     /* RDIV = r_div_opt */
0303     priv->regs[RSTV6110_TUNING2] &= ~(3 << 6);
0304     priv->regs[RSTV6110_TUNING2] |= (((r_div_opt) & 3) << 6);
0305 
0306     /* NDIV_MSB = MSB(divider) */
0307     priv->regs[RSTV6110_TUNING2] &= ~0x0f;
0308     priv->regs[RSTV6110_TUNING2] |= (((divider) >> 8) & 0x0f);
0309 
0310     /* NDIV_LSB, LSB(divider) */
0311     priv->regs[RSTV6110_TUNING1] = (divider & 0xff);
0312 
0313     /* CALVCOSTRT = 1 VCO Auto Calibration */
0314     priv->regs[RSTV6110_STAT1] |= 0x04;
0315     stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1],
0316                         RSTV6110_CTRL1, 8);
0317 
0318     i = 0;
0319     /* Wait for CALVCOSTRT == 0 */
0320     while ((i < 10) && (ret != 0)) {
0321         ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x04);
0322         msleep(1); /* wait for VCO auto calibration */
0323         i++;
0324     }
0325 
0326     ret = stv6110_read_reg(fe, RSTV6110_STAT1);
0327     stv6110_get_frequency(fe, &result_freq);
0328 
0329     vco_freq = divider * ((priv->mclk / 1000) / ((1 << (r_div_opt + 1))));
0330     dprintk("%s, stat1=%x, lo_freq=%d kHz, vco_frec=%d kHz\n", __func__,
0331                         ret, result_freq, vco_freq);
0332 
0333     return 0;
0334 }
0335 
0336 static int stv6110_set_params(struct dvb_frontend *fe)
0337 {
0338     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0339     u32 bandwidth = carrier_width(c->symbol_rate, c->rolloff);
0340 
0341     stv6110_set_frequency(fe, c->frequency);
0342     stv6110_set_bandwidth(fe, bandwidth);
0343 
0344     return 0;
0345 }
0346 
0347 static int stv6110_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
0348 {
0349     struct stv6110_priv *priv = fe->tuner_priv;
0350     u8 r8 = 0;
0351     u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
0352     stv6110_read_regs(fe, regs, 0, 8);
0353 
0354     /* CF */
0355     r8 = priv->regs[RSTV6110_CTRL3] & 0x1f;
0356     *bandwidth = (r8 + 5) * 2000000;/* x2 for ZIF tuner BW/2 = F+5 Mhz */
0357 
0358     return 0;
0359 }
0360 
0361 static const struct dvb_tuner_ops stv6110_tuner_ops = {
0362     .info = {
0363         .name = "ST STV6110",
0364         .frequency_min_hz  =  950 * MHz,
0365         .frequency_max_hz  = 2150 * MHz,
0366         .frequency_step_hz =    1 * MHz,
0367     },
0368     .init = stv6110_init,
0369     .release = stv6110_release,
0370     .sleep = stv6110_sleep,
0371     .set_params = stv6110_set_params,
0372     .get_frequency = stv6110_get_frequency,
0373     .set_frequency = stv6110_set_frequency,
0374     .get_bandwidth = stv6110_get_bandwidth,
0375     .set_bandwidth = stv6110_set_bandwidth,
0376 
0377 };
0378 
0379 struct dvb_frontend *stv6110_attach(struct dvb_frontend *fe,
0380                     const struct stv6110_config *config,
0381                     struct i2c_adapter *i2c)
0382 {
0383     struct stv6110_priv *priv = NULL;
0384     u8 reg0[] = { 0x00, 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
0385 
0386     struct i2c_msg msg[] = {
0387         {
0388             .addr = config->i2c_address,
0389             .flags = 0,
0390             .buf = reg0,
0391             .len = 9
0392         }
0393     };
0394     int ret;
0395 
0396     /* divisor value for the output clock */
0397     reg0[2] &= ~0xc0;
0398     reg0[2] |= (config->clk_div << 6);
0399 
0400     if (fe->ops.i2c_gate_ctrl)
0401         fe->ops.i2c_gate_ctrl(fe, 1);
0402 
0403     ret = i2c_transfer(i2c, msg, 1);
0404 
0405     if (fe->ops.i2c_gate_ctrl)
0406         fe->ops.i2c_gate_ctrl(fe, 0);
0407 
0408     if (ret != 1)
0409         return NULL;
0410 
0411     priv = kzalloc(sizeof(struct stv6110_priv), GFP_KERNEL);
0412     if (priv == NULL)
0413         return NULL;
0414 
0415     priv->i2c_address = config->i2c_address;
0416     priv->i2c = i2c;
0417     priv->mclk = config->mclk;
0418     priv->clk_div = config->clk_div;
0419     priv->gain = config->gain;
0420 
0421     memcpy(&priv->regs, &reg0[1], 8);
0422 
0423     memcpy(&fe->ops.tuner_ops, &stv6110_tuner_ops,
0424                 sizeof(struct dvb_tuner_ops));
0425     fe->tuner_priv = priv;
0426     printk(KERN_INFO "STV6110 attached on addr=%x!\n", priv->i2c_address);
0427 
0428     return fe;
0429 }
0430 EXPORT_SYMBOL(stv6110_attach);
0431 
0432 module_param(debug, int, 0644);
0433 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
0434 
0435 MODULE_DESCRIPTION("ST STV6110 driver");
0436 MODULE_AUTHOR("Igor M. Liplianin");
0437 MODULE_LICENSE("GPL");