Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * MaxLinear MxL301RF OFDM tuner driver
0004  *
0005  * Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
0006  */
0007 
0008 /*
0009  * NOTICE:
0010  * This driver is incomplete and lacks init/config of the chips,
0011  * as the necessary info is not disclosed.
0012  * Other features like get_if_frequency() are missing as well.
0013  * It assumes that users of this driver (such as a PCI bridge of
0014  * DTV receiver cards) properly init and configure the chip
0015  * via I2C *before* calling this driver's init() function.
0016  *
0017  * Currently, PT3 driver is the only one that uses this driver,
0018  * and contains init/config code in its firmware.
0019  * Thus some part of the code might be dependent on PT3 specific config.
0020  */
0021 
0022 #include <linux/kernel.h>
0023 #include "mxl301rf.h"
0024 
0025 struct mxl301rf_state {
0026     struct mxl301rf_config cfg;
0027     struct i2c_client *i2c;
0028 };
0029 
0030 static struct mxl301rf_state *cfg_to_state(struct mxl301rf_config *c)
0031 {
0032     return container_of(c, struct mxl301rf_state, cfg);
0033 }
0034 
0035 static int raw_write(struct mxl301rf_state *state, const u8 *buf, int len)
0036 {
0037     int ret;
0038 
0039     ret = i2c_master_send(state->i2c, buf, len);
0040     if (ret >= 0 && ret < len)
0041         ret = -EIO;
0042     return (ret == len) ? 0 : ret;
0043 }
0044 
0045 static int reg_write(struct mxl301rf_state *state, u8 reg, u8 val)
0046 {
0047     u8 buf[2] = { reg, val };
0048 
0049     return raw_write(state, buf, 2);
0050 }
0051 
0052 static int reg_read(struct mxl301rf_state *state, u8 reg, u8 *val)
0053 {
0054     u8 wbuf[2] = { 0xfb, reg };
0055     int ret;
0056 
0057     ret = raw_write(state, wbuf, sizeof(wbuf));
0058     if (ret == 0)
0059         ret = i2c_master_recv(state->i2c, val, 1);
0060     if (ret >= 0 && ret < 1)
0061         ret = -EIO;
0062     return (ret == 1) ? 0 : ret;
0063 }
0064 
0065 /* tuner_ops */
0066 
0067 /* get RSSI and update propery cache, set to *out in % */
0068 static int mxl301rf_get_rf_strength(struct dvb_frontend *fe, u16 *out)
0069 {
0070     struct mxl301rf_state *state;
0071     int ret;
0072     u8  rf_in1, rf_in2, rf_off1, rf_off2;
0073     u16 rf_in, rf_off;
0074     s64 level;
0075     struct dtv_fe_stats *rssi;
0076 
0077     rssi = &fe->dtv_property_cache.strength;
0078     rssi->len = 1;
0079     rssi->stat[0].scale = FE_SCALE_NOT_AVAILABLE;
0080     *out = 0;
0081 
0082     state = fe->tuner_priv;
0083     ret = reg_write(state, 0x14, 0x01);
0084     if (ret < 0)
0085         return ret;
0086     usleep_range(1000, 2000);
0087 
0088     ret = reg_read(state, 0x18, &rf_in1);
0089     if (ret == 0)
0090         ret = reg_read(state, 0x19, &rf_in2);
0091     if (ret == 0)
0092         ret = reg_read(state, 0xd6, &rf_off1);
0093     if (ret == 0)
0094         ret = reg_read(state, 0xd7, &rf_off2);
0095     if (ret != 0)
0096         return ret;
0097 
0098     rf_in = (rf_in2 & 0x07) << 8 | rf_in1;
0099     rf_off = (rf_off2 & 0x0f) << 5 | (rf_off1 >> 3);
0100     level = rf_in - rf_off - (113 << 3); /* x8 dBm */
0101     level = level * 1000 / 8;
0102     rssi->stat[0].svalue = level;
0103     rssi->stat[0].scale = FE_SCALE_DECIBEL;
0104     /* *out = (level - min) * 100 / (max - min) */
0105     *out = (rf_in - rf_off + (1 << 9) - 1) * 100 / ((5 << 9) - 2);
0106     return 0;
0107 }
0108 
0109 /* spur shift parameters */
0110 struct shf {
0111     u32 freq;       /* Channel center frequency */
0112     u32 ofst_th;    /* Offset frequency threshold */
0113     u8  shf_val;    /* Spur shift value */
0114     u8  shf_dir;    /* Spur shift direction */
0115 };
0116 
0117 static const struct shf shf_tab[] = {
0118     {  64500, 500, 0x92, 0x07 },
0119     { 191500, 300, 0xe2, 0x07 },
0120     { 205500, 500, 0x2c, 0x04 },
0121     { 212500, 500, 0x1e, 0x04 },
0122     { 226500, 500, 0xd4, 0x07 },
0123     {  99143, 500, 0x9c, 0x07 },
0124     { 173143, 500, 0xd4, 0x07 },
0125     { 191143, 300, 0xd4, 0x07 },
0126     { 207143, 500, 0xce, 0x07 },
0127     { 225143, 500, 0xce, 0x07 },
0128     { 243143, 500, 0xd4, 0x07 },
0129     { 261143, 500, 0xd4, 0x07 },
0130     { 291143, 500, 0xd4, 0x07 },
0131     { 339143, 500, 0x2c, 0x04 },
0132     { 117143, 500, 0x7a, 0x07 },
0133     { 135143, 300, 0x7a, 0x07 },
0134     { 153143, 500, 0x01, 0x07 }
0135 };
0136 
0137 struct reg_val {
0138     u8 reg;
0139     u8 val;
0140 } __attribute__ ((__packed__));
0141 
0142 static const struct reg_val set_idac[] = {
0143     { 0x0d, 0x00 },
0144     { 0x0c, 0x67 },
0145     { 0x6f, 0x89 },
0146     { 0x70, 0x0c },
0147     { 0x6f, 0x8a },
0148     { 0x70, 0x0e },
0149     { 0x6f, 0x8b },
0150     { 0x70, 0x1c },
0151 };
0152 
0153 static int mxl301rf_set_params(struct dvb_frontend *fe)
0154 {
0155     struct reg_val tune0[] = {
0156         { 0x13, 0x00 },     /* abort tuning */
0157         { 0x3b, 0xc0 },
0158         { 0x3b, 0x80 },
0159         { 0x10, 0x95 },     /* BW */
0160         { 0x1a, 0x05 },
0161         { 0x61, 0x00 },     /* spur shift value (placeholder) */
0162         { 0x62, 0xa0 }      /* spur shift direction (placeholder) */
0163     };
0164 
0165     struct reg_val tune1[] = {
0166         { 0x11, 0x40 },     /* RF frequency L (placeholder) */
0167         { 0x12, 0x0e },     /* RF frequency H (placeholder) */
0168         { 0x13, 0x01 }      /* start tune */
0169     };
0170 
0171     struct mxl301rf_state *state;
0172     u32 freq;
0173     u16 f;
0174     u32 tmp, div;
0175     int i, ret;
0176 
0177     state = fe->tuner_priv;
0178     freq = fe->dtv_property_cache.frequency;
0179 
0180     /* spur shift function (for analog) */
0181     for (i = 0; i < ARRAY_SIZE(shf_tab); i++) {
0182         if (freq >= (shf_tab[i].freq - shf_tab[i].ofst_th) * 1000 &&
0183             freq <= (shf_tab[i].freq + shf_tab[i].ofst_th) * 1000) {
0184             tune0[5].val = shf_tab[i].shf_val;
0185             tune0[6].val = 0xa0 | shf_tab[i].shf_dir;
0186             break;
0187         }
0188     }
0189     ret = raw_write(state, (u8 *) tune0, sizeof(tune0));
0190     if (ret < 0)
0191         goto failed;
0192     usleep_range(3000, 4000);
0193 
0194     /* convert freq to 10.6 fixed point float [MHz] */
0195     f = freq / 1000000;
0196     tmp = freq % 1000000;
0197     div = 1000000;
0198     for (i = 0; i < 6; i++) {
0199         f <<= 1;
0200         div >>= 1;
0201         if (tmp > div) {
0202             tmp -= div;
0203             f |= 1;
0204         }
0205     }
0206     if (tmp > 7812)
0207         f++;
0208     tune1[0].val = f & 0xff;
0209     tune1[1].val = f >> 8;
0210     ret = raw_write(state, (u8 *) tune1, sizeof(tune1));
0211     if (ret < 0)
0212         goto failed;
0213     msleep(31);
0214 
0215     ret = reg_write(state, 0x1a, 0x0d);
0216     if (ret < 0)
0217         goto failed;
0218     ret = raw_write(state, (u8 *) set_idac, sizeof(set_idac));
0219     if (ret < 0)
0220         goto failed;
0221     return 0;
0222 
0223 failed:
0224     dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
0225         __func__, fe->dvb->num, fe->id);
0226     return ret;
0227 }
0228 
0229 static const struct reg_val standby_data[] = {
0230     { 0x01, 0x00 },
0231     { 0x13, 0x00 }
0232 };
0233 
0234 static int mxl301rf_sleep(struct dvb_frontend *fe)
0235 {
0236     struct mxl301rf_state *state;
0237     int ret;
0238 
0239     state = fe->tuner_priv;
0240     ret = raw_write(state, (u8 *)standby_data, sizeof(standby_data));
0241     if (ret < 0)
0242         dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
0243             __func__, fe->dvb->num, fe->id);
0244     return ret;
0245 }
0246 
0247 
0248 /* init sequence is not public.
0249  * the parent must have init'ed the device.
0250  * just wake up here.
0251  */
0252 static int mxl301rf_init(struct dvb_frontend *fe)
0253 {
0254     struct mxl301rf_state *state;
0255     int ret;
0256 
0257     state = fe->tuner_priv;
0258 
0259     ret = reg_write(state, 0x01, 0x01);
0260     if (ret < 0) {
0261         dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
0262              __func__, fe->dvb->num, fe->id);
0263         return ret;
0264     }
0265     return 0;
0266 }
0267 
0268 /* I2C driver functions */
0269 
0270 static const struct dvb_tuner_ops mxl301rf_ops = {
0271     .info = {
0272         .name = "MaxLinear MxL301RF",
0273 
0274         .frequency_min_hz =  93 * MHz,
0275         .frequency_max_hz = 803 * MHz + 142857,
0276     },
0277 
0278     .init = mxl301rf_init,
0279     .sleep = mxl301rf_sleep,
0280 
0281     .set_params = mxl301rf_set_params,
0282     .get_rf_strength = mxl301rf_get_rf_strength,
0283 };
0284 
0285 
0286 static int mxl301rf_probe(struct i2c_client *client,
0287               const struct i2c_device_id *id)
0288 {
0289     struct mxl301rf_state *state;
0290     struct mxl301rf_config *cfg;
0291     struct dvb_frontend *fe;
0292 
0293     state = kzalloc(sizeof(*state), GFP_KERNEL);
0294     if (!state)
0295         return -ENOMEM;
0296 
0297     state->i2c = client;
0298     cfg = client->dev.platform_data;
0299 
0300     memcpy(&state->cfg, cfg, sizeof(state->cfg));
0301     fe = cfg->fe;
0302     fe->tuner_priv = state;
0303     memcpy(&fe->ops.tuner_ops, &mxl301rf_ops, sizeof(mxl301rf_ops));
0304 
0305     i2c_set_clientdata(client, &state->cfg);
0306     dev_info(&client->dev, "MaxLinear MxL301RF attached.\n");
0307     return 0;
0308 }
0309 
0310 static int mxl301rf_remove(struct i2c_client *client)
0311 {
0312     struct mxl301rf_state *state;
0313 
0314     state = cfg_to_state(i2c_get_clientdata(client));
0315     state->cfg.fe->tuner_priv = NULL;
0316     kfree(state);
0317     return 0;
0318 }
0319 
0320 
0321 static const struct i2c_device_id mxl301rf_id[] = {
0322     {"mxl301rf", 0},
0323     {}
0324 };
0325 MODULE_DEVICE_TABLE(i2c, mxl301rf_id);
0326 
0327 static struct i2c_driver mxl301rf_driver = {
0328     .driver = {
0329         .name   = "mxl301rf",
0330     },
0331     .probe      = mxl301rf_probe,
0332     .remove     = mxl301rf_remove,
0333     .id_table   = mxl301rf_id,
0334 };
0335 
0336 module_i2c_driver(mxl301rf_driver);
0337 
0338 MODULE_DESCRIPTION("MaxLinear MXL301RF tuner");
0339 MODULE_AUTHOR("Akihiro TSUKADA");
0340 MODULE_LICENSE("GPL");