0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/init.h>
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/string.h>
0015
0016 #include <media/dvb_frontend.h>
0017 #include "stb6100.h"
0018
0019 static unsigned int verbose;
0020 module_param(verbose, int, 0644);
0021
0022
0023 #define MAX_XFER_SIZE 64
0024
0025 #define FE_ERROR 0
0026 #define FE_NOTICE 1
0027 #define FE_INFO 2
0028 #define FE_DEBUG 3
0029
0030 #define dprintk(x, y, z, format, arg...) do { \
0031 if (z) { \
0032 if ((x > FE_ERROR) && (x > y)) \
0033 printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
0034 else if ((x > FE_NOTICE) && (x > y)) \
0035 printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
0036 else if ((x > FE_INFO) && (x > y)) \
0037 printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
0038 else if ((x > FE_DEBUG) && (x > y)) \
0039 printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
0040 } else { \
0041 if (x > y) \
0042 printk(format, ##arg); \
0043 } \
0044 } while (0)
0045
0046 struct stb6100_lkup {
0047 u32 val_low;
0048 u32 val_high;
0049 u8 reg;
0050 };
0051
0052 static void stb6100_release(struct dvb_frontend *fe);
0053
0054 static const struct stb6100_lkup lkup[] = {
0055 { 0, 950000, 0x0a },
0056 { 950000, 1000000, 0x0a },
0057 { 1000000, 1075000, 0x0c },
0058 { 1075000, 1200000, 0x00 },
0059 { 1200000, 1300000, 0x01 },
0060 { 1300000, 1370000, 0x02 },
0061 { 1370000, 1470000, 0x04 },
0062 { 1470000, 1530000, 0x05 },
0063 { 1530000, 1650000, 0x06 },
0064 { 1650000, 1800000, 0x08 },
0065 { 1800000, 1950000, 0x0a },
0066 { 1950000, 2150000, 0x0c },
0067 { 2150000, 9999999, 0x0c },
0068 { 0, 0, 0x00 }
0069 };
0070
0071
0072 static const char *stb6100_regnames[] = {
0073 [STB6100_LD] = "LD",
0074 [STB6100_VCO] = "VCO",
0075 [STB6100_NI] = "NI",
0076 [STB6100_NF_LSB] = "NF",
0077 [STB6100_K] = "K",
0078 [STB6100_G] = "G",
0079 [STB6100_F] = "F",
0080 [STB6100_DLB] = "DLB",
0081 [STB6100_TEST1] = "TEST1",
0082 [STB6100_FCCK] = "FCCK",
0083 [STB6100_LPEN] = "LPEN",
0084 [STB6100_TEST3] = "TEST3",
0085 };
0086
0087
0088
0089
0090 struct stb6100_regmask {
0091 u8 mask;
0092 u8 set;
0093 };
0094
0095 static const struct stb6100_regmask stb6100_template[] = {
0096 [STB6100_LD] = { 0xff, 0x00 },
0097 [STB6100_VCO] = { 0xff, 0x00 },
0098 [STB6100_NI] = { 0xff, 0x00 },
0099 [STB6100_NF_LSB] = { 0xff, 0x00 },
0100 [STB6100_K] = { 0xc7, 0x38 },
0101 [STB6100_G] = { 0xef, 0x10 },
0102 [STB6100_F] = { 0x1f, 0xc0 },
0103 [STB6100_DLB] = { 0x38, 0xc4 },
0104 [STB6100_TEST1] = { 0x00, 0x8f },
0105 [STB6100_FCCK] = { 0x40, 0x0d },
0106 [STB6100_LPEN] = { 0xf0, 0x0b },
0107 [STB6100_TEST3] = { 0x00, 0xde },
0108 };
0109
0110
0111
0112
0113 static __always_unused inline void stb6100_normalise_regs(u8 regs[])
0114 {
0115 int i;
0116
0117 for (i = 0; i < STB6100_NUMREGS; i++)
0118 regs[i] = (regs[i] & stb6100_template[i].mask) | stb6100_template[i].set;
0119 }
0120
0121 static int stb6100_read_regs(struct stb6100_state *state, u8 regs[])
0122 {
0123 int rc;
0124 struct i2c_msg msg = {
0125 .addr = state->config->tuner_address,
0126 .flags = I2C_M_RD,
0127 .buf = regs,
0128 .len = STB6100_NUMREGS
0129 };
0130
0131 rc = i2c_transfer(state->i2c, &msg, 1);
0132 if (unlikely(rc != 1)) {
0133 dprintk(verbose, FE_ERROR, 1, "Read (0x%x) err, rc=[%d]",
0134 state->config->tuner_address, rc);
0135
0136 return -EREMOTEIO;
0137 }
0138 if (unlikely(verbose > FE_DEBUG)) {
0139 int i;
0140
0141 dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address);
0142 for (i = 0; i < STB6100_NUMREGS; i++)
0143 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[i], regs[i]);
0144 }
0145 return 0;
0146 }
0147
0148 static int stb6100_read_reg(struct stb6100_state *state, u8 reg)
0149 {
0150 u8 regs[STB6100_NUMREGS];
0151
0152 struct i2c_msg msg = {
0153 .addr = state->config->tuner_address + reg,
0154 .flags = I2C_M_RD,
0155 .buf = regs,
0156 .len = 1
0157 };
0158
0159 i2c_transfer(state->i2c, &msg, 1);
0160
0161 if (unlikely(reg >= STB6100_NUMREGS)) {
0162 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
0163 return -EINVAL;
0164 }
0165 if (unlikely(verbose > FE_DEBUG)) {
0166 dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address);
0167 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[reg], regs[0]);
0168 }
0169
0170 return (unsigned int)regs[0];
0171 }
0172
0173 static int stb6100_write_reg_range(struct stb6100_state *state, u8 buf[], int start, int len)
0174 {
0175 int rc;
0176 u8 cmdbuf[MAX_XFER_SIZE];
0177 struct i2c_msg msg = {
0178 .addr = state->config->tuner_address,
0179 .flags = 0,
0180 .buf = cmdbuf,
0181 .len = len + 1
0182 };
0183
0184 if (1 + len > sizeof(cmdbuf)) {
0185 printk(KERN_WARNING
0186 "%s: i2c wr: len=%d is too big!\n",
0187 KBUILD_MODNAME, len);
0188 return -EINVAL;
0189 }
0190
0191 if (unlikely(start < 1 || start + len > STB6100_NUMREGS)) {
0192 dprintk(verbose, FE_ERROR, 1, "Invalid register range %d:%d",
0193 start, len);
0194 return -EINVAL;
0195 }
0196 memcpy(&cmdbuf[1], buf, len);
0197 cmdbuf[0] = start;
0198
0199 if (unlikely(verbose > FE_DEBUG)) {
0200 int i;
0201
0202 dprintk(verbose, FE_DEBUG, 1, " Write @ 0x%02x: [%d:%d]", state->config->tuner_address, start, len);
0203 for (i = 0; i < len; i++)
0204 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[start + i], buf[i]);
0205 }
0206 rc = i2c_transfer(state->i2c, &msg, 1);
0207 if (unlikely(rc != 1)) {
0208 dprintk(verbose, FE_ERROR, 1, "(0x%x) write err [%d:%d], rc=[%d]",
0209 (unsigned int)state->config->tuner_address, start, len, rc);
0210 return -EREMOTEIO;
0211 }
0212 return 0;
0213 }
0214
0215 static int stb6100_write_reg(struct stb6100_state *state, u8 reg, u8 data)
0216 {
0217 u8 tmp = data;
0218
0219 if (unlikely(reg >= STB6100_NUMREGS)) {
0220 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
0221 return -EREMOTEIO;
0222 }
0223 tmp = (tmp & stb6100_template[reg].mask) | stb6100_template[reg].set;
0224 return stb6100_write_reg_range(state, &tmp, reg, 1);
0225 }
0226
0227
0228 static int stb6100_get_status(struct dvb_frontend *fe, u32 *status)
0229 {
0230 int rc;
0231 struct stb6100_state *state = fe->tuner_priv;
0232
0233 rc = stb6100_read_reg(state, STB6100_LD);
0234 if (rc < 0) {
0235 dprintk(verbose, FE_ERROR, 1, "%s failed", __func__);
0236 return rc;
0237 }
0238 return (rc & STB6100_LD_LOCK) ? TUNER_STATUS_LOCKED : 0;
0239 }
0240
0241 static int stb6100_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
0242 {
0243 int rc;
0244 u8 f;
0245 u32 bw;
0246 struct stb6100_state *state = fe->tuner_priv;
0247
0248 rc = stb6100_read_reg(state, STB6100_F);
0249 if (rc < 0)
0250 return rc;
0251 f = rc & STB6100_F_F;
0252
0253 bw = (f + 5) * 2000;
0254
0255 *bandwidth = state->bandwidth = bw * 1000;
0256 dprintk(verbose, FE_DEBUG, 1, "bandwidth = %u Hz", state->bandwidth);
0257 return 0;
0258 }
0259
0260 static int stb6100_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
0261 {
0262 u32 tmp;
0263 int rc;
0264 struct stb6100_state *state = fe->tuner_priv;
0265
0266 dprintk(verbose, FE_DEBUG, 1, "set bandwidth to %u Hz", bandwidth);
0267
0268 bandwidth /= 2;
0269
0270 if (bandwidth >= 36000000)
0271 tmp = 31;
0272 else if (bandwidth <= 5000000)
0273 tmp = 0;
0274 else
0275 tmp = (bandwidth + 500000) / 1000000 - 5;
0276
0277
0278
0279
0280 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d | STB6100_FCCK_FCCK);
0281 if (rc < 0)
0282 return rc;
0283 rc = stb6100_write_reg(state, STB6100_F, 0xc0 | tmp);
0284 if (rc < 0)
0285 return rc;
0286
0287 msleep(5);
0288
0289 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d);
0290 if (rc < 0)
0291 return rc;
0292
0293 msleep(10);
0294
0295 return 0;
0296 }
0297
0298 static int stb6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
0299 {
0300 int rc;
0301 u32 nint, nfrac, fvco;
0302 int psd2, odiv;
0303 struct stb6100_state *state = fe->tuner_priv;
0304 u8 regs[STB6100_NUMREGS];
0305
0306 rc = stb6100_read_regs(state, regs);
0307 if (rc < 0)
0308 return rc;
0309
0310 odiv = (regs[STB6100_VCO] & STB6100_VCO_ODIV) >> STB6100_VCO_ODIV_SHIFT;
0311 psd2 = (regs[STB6100_K] & STB6100_K_PSD2) >> STB6100_K_PSD2_SHIFT;
0312 nint = regs[STB6100_NI];
0313 nfrac = ((regs[STB6100_K] & STB6100_K_NF_MSB) << 8) | regs[STB6100_NF_LSB];
0314 fvco = (nfrac * state->reference >> (9 - psd2)) + (nint * state->reference << psd2);
0315 *frequency = state->frequency = fvco >> (odiv + 1);
0316
0317 dprintk(verbose, FE_DEBUG, 1,
0318 "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u",
0319 state->frequency, odiv, psd2, state->reference, fvco, nint, nfrac);
0320 return 0;
0321 }
0322
0323
0324 static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency)
0325 {
0326 int rc;
0327 const struct stb6100_lkup *ptr;
0328 struct stb6100_state *state = fe->tuner_priv;
0329 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
0330
0331 u32 srate = 0, fvco, nint, nfrac;
0332 u8 regs[STB6100_NUMREGS];
0333 u8 g, psd2, odiv;
0334
0335 dprintk(verbose, FE_DEBUG, 1, "Version 2010-8-14 13:51");
0336
0337 if (fe->ops.get_frontend) {
0338 dprintk(verbose, FE_DEBUG, 1, "Get frontend parameters");
0339 fe->ops.get_frontend(fe, p);
0340 }
0341 srate = p->symbol_rate;
0342
0343
0344 rc = stb6100_write_reg(state, STB6100_FCCK, 0x4d | STB6100_FCCK_FCCK);
0345 if (rc < 0)
0346 return rc;
0347
0348
0349 regs[STB6100_LPEN] = 0xeb;
0350 rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]);
0351 if (rc < 0)
0352 return rc;
0353
0354
0355
0356
0357 if (frequency <= 1075000)
0358 odiv = 1;
0359 else
0360 odiv = 0;
0361
0362
0363 regs[STB6100_VCO] = 0xe0 | (odiv << STB6100_VCO_ODIV_SHIFT);
0364
0365
0366 for (ptr = lkup;
0367 (ptr->val_high != 0) && !CHKRANGE(frequency, ptr->val_low, ptr->val_high);
0368 ptr++);
0369
0370 if (ptr->val_high == 0) {
0371 printk(KERN_ERR "%s: frequency out of range: %u kHz\n", __func__, frequency);
0372 return -EINVAL;
0373 }
0374 regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_OSM) | ptr->reg;
0375 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
0376 if (rc < 0)
0377 return rc;
0378
0379 if ((frequency > 1075000) && (frequency <= 1325000))
0380 psd2 = 0;
0381 else
0382 psd2 = 1;
0383
0384 fvco = frequency << (1 + odiv);
0385
0386 nint = fvco / (state->reference << psd2);
0387
0388 nfrac = DIV_ROUND_CLOSEST((fvco - (nint * state->reference << psd2))
0389 << (9 - psd2), state->reference);
0390
0391
0392 regs[STB6100_NI] = nint;
0393 rc = stb6100_write_reg(state, STB6100_NI, regs[STB6100_NI]);
0394 if (rc < 0)
0395 return rc;
0396
0397
0398 regs[STB6100_NF_LSB] = nfrac;
0399 rc = stb6100_write_reg(state, STB6100_NF_LSB, regs[STB6100_NF_LSB]);
0400 if (rc < 0)
0401 return rc;
0402
0403
0404 regs[STB6100_K] = (0x38 & ~STB6100_K_PSD2) | (psd2 << STB6100_K_PSD2_SHIFT);
0405 regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_NF_MSB) | ((nfrac >> 8) & STB6100_K_NF_MSB);
0406 rc = stb6100_write_reg(state, STB6100_K, regs[STB6100_K]);
0407 if (rc < 0)
0408 return rc;
0409
0410
0411 if (srate >= 15000000)
0412 g = 9;
0413 else if (srate >= 5000000)
0414 g = 11;
0415 else
0416 g = 14;
0417
0418 regs[STB6100_G] = (0x10 & ~STB6100_G_G) | g;
0419 regs[STB6100_G] &= ~STB6100_G_GCT;
0420 regs[STB6100_G] |= (1 << 5);
0421 rc = stb6100_write_reg(state, STB6100_G, regs[STB6100_G]);
0422 if (rc < 0)
0423 return rc;
0424
0425
0426
0427
0428 regs[STB6100_DLB] = 0xcc;
0429 rc = stb6100_write_reg(state, STB6100_DLB, regs[STB6100_DLB]);
0430 if (rc < 0)
0431 return rc;
0432
0433 dprintk(verbose, FE_DEBUG, 1,
0434 "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u",
0435 frequency, srate, (unsigned int)g, (unsigned int)odiv,
0436 (unsigned int)psd2, state->reference,
0437 ptr->reg, fvco, nint, nfrac);
0438
0439
0440 regs[STB6100_TEST1] = 0x8f;
0441 rc = stb6100_write_reg(state, STB6100_TEST1, regs[STB6100_TEST1]);
0442 if (rc < 0)
0443 return rc;
0444 regs[STB6100_TEST3] = 0xde;
0445 rc = stb6100_write_reg(state, STB6100_TEST3, regs[STB6100_TEST3]);
0446 if (rc < 0)
0447 return rc;
0448
0449
0450 regs[STB6100_LPEN] = 0xfb;
0451 rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]);
0452 if (rc < 0)
0453 return rc;
0454
0455 msleep(2);
0456
0457
0458 regs[STB6100_VCO] &= ~STB6100_VCO_OCK;
0459 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
0460 if (rc < 0)
0461 return rc;
0462
0463 msleep(10);
0464
0465 regs[STB6100_VCO] &= ~STB6100_VCO_OSCH;
0466 regs[STB6100_VCO] |= STB6100_VCO_OCK;
0467 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
0468 if (rc < 0)
0469 return rc;
0470
0471 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d);
0472 if (rc < 0)
0473 return rc;
0474
0475 msleep(10);
0476
0477 return 0;
0478 }
0479
0480 static int stb6100_sleep(struct dvb_frontend *fe)
0481 {
0482
0483 return 0;
0484 }
0485
0486 static int stb6100_init(struct dvb_frontend *fe)
0487 {
0488 struct stb6100_state *state = fe->tuner_priv;
0489 int refclk = 27000000;
0490
0491
0492
0493
0494
0495 state->bandwidth = 36000000;
0496 state->reference = refclk / 1000;
0497
0498
0499 return 0;
0500 }
0501
0502 static int stb6100_set_params(struct dvb_frontend *fe)
0503 {
0504 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0505
0506 if (c->frequency > 0)
0507 stb6100_set_frequency(fe, c->frequency);
0508
0509 if (c->bandwidth_hz > 0)
0510 stb6100_set_bandwidth(fe, c->bandwidth_hz);
0511
0512 return 0;
0513 }
0514
0515 static const struct dvb_tuner_ops stb6100_ops = {
0516 .info = {
0517 .name = "STB6100 Silicon Tuner",
0518 .frequency_min_hz = 950 * MHz,
0519 .frequency_max_hz = 2150 * MHz,
0520 },
0521
0522 .init = stb6100_init,
0523 .sleep = stb6100_sleep,
0524 .get_status = stb6100_get_status,
0525 .set_params = stb6100_set_params,
0526 .get_frequency = stb6100_get_frequency,
0527 .get_bandwidth = stb6100_get_bandwidth,
0528 .release = stb6100_release
0529 };
0530
0531 struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe,
0532 const struct stb6100_config *config,
0533 struct i2c_adapter *i2c)
0534 {
0535 struct stb6100_state *state = NULL;
0536
0537 state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL);
0538 if (!state)
0539 return NULL;
0540
0541 state->config = config;
0542 state->i2c = i2c;
0543 state->frontend = fe;
0544 state->reference = config->refclock / 1000;
0545 fe->tuner_priv = state;
0546 fe->ops.tuner_ops = stb6100_ops;
0547
0548 printk("%s: Attaching STB6100 \n", __func__);
0549 return fe;
0550 }
0551
0552 static void stb6100_release(struct dvb_frontend *fe)
0553 {
0554 struct stb6100_state *state = fe->tuner_priv;
0555
0556 fe->tuner_priv = NULL;
0557 kfree(state);
0558 }
0559
0560 EXPORT_SYMBOL(stb6100_attach);
0561 MODULE_PARM_DESC(verbose, "Set Verbosity level");
0562
0563 MODULE_AUTHOR("Manu Abraham");
0564 MODULE_DESCRIPTION("STB6100 Silicon tuner");
0565 MODULE_LICENSE("GPL");