0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/moduleparam.h>
0011 #include <linux/init.h>
0012 #include <linux/delay.h>
0013 #include <linux/firmware.h>
0014 #include <linux/i2c.h>
0015 #include <asm/div64.h>
0016
0017 #include "stv6111.h"
0018
0019 #include <media/dvb_frontend.h>
0020
0021 struct stv {
0022 struct i2c_adapter *i2c;
0023 u8 adr;
0024
0025 u8 reg[11];
0026 u32 ref_freq;
0027 u32 frequency;
0028 };
0029
0030 struct slookup {
0031 s16 value;
0032 u16 reg_value;
0033 };
0034
0035 static const struct slookup lnagain_nf_lookup[] = {
0036
0037 { 2572, 0 },
0038 { 2575, 1 },
0039 { 2580, 2 },
0040 { 2588, 3 },
0041 { 2596, 4 },
0042 { 2611, 5 },
0043 { 2633, 6 },
0044 { 2664, 7 },
0045 { 2701, 8 },
0046 { 2753, 9 },
0047 { 2816, 10 },
0048 { 2902, 11 },
0049 { 2995, 12 },
0050 { 3104, 13 },
0051 { 3215, 14 },
0052 { 3337, 15 },
0053 { 3492, 16 },
0054 { 3614, 17 },
0055 { 3731, 18 },
0056 { 3861, 19 },
0057 { 3988, 20 },
0058 { 4124, 21 },
0059 { 4253, 22 },
0060 { 4386, 23 },
0061 { 4505, 24 },
0062 { 4623, 25 },
0063 { 4726, 26 },
0064 { 4821, 27 },
0065 { 4903, 28 },
0066 { 4979, 29 },
0067 { 5045, 30 },
0068 { 5102, 31 }
0069 };
0070
0071 static const struct slookup lnagain_iip3_lookup[] = {
0072
0073 { 1548, 0 },
0074 { 1552, 1 },
0075 { 1569, 2 },
0076 { 1565, 3 },
0077 { 1577, 4 },
0078 { 1594, 5 },
0079 { 1627, 6 },
0080 { 1656, 7 },
0081 { 1700, 8 },
0082 { 1748, 9 },
0083 { 1805, 10 },
0084 { 1896, 11 },
0085 { 1995, 12 },
0086 { 2113, 13 },
0087 { 2233, 14 },
0088 { 2366, 15 },
0089 { 2543, 16 },
0090 { 2687, 17 },
0091 { 2842, 18 },
0092 { 2999, 19 },
0093 { 3167, 20 },
0094 { 3342, 21 },
0095 { 3507, 22 },
0096 { 3679, 23 },
0097 { 3827, 24 },
0098 { 3970, 25 },
0099 { 4094, 26 },
0100 { 4210, 27 },
0101 { 4308, 28 },
0102 { 4396, 29 },
0103 { 4468, 30 },
0104 { 4535, 31 }
0105 };
0106
0107 static const struct slookup gain_rfagc_lookup[] = {
0108
0109 { 4870, 0x3000 },
0110 { 4850, 0x3C00 },
0111 { 4800, 0x4500 },
0112 { 4750, 0x4800 },
0113 { 4700, 0x4B00 },
0114 { 4650, 0x4D00 },
0115 { 4600, 0x4F00 },
0116 { 4550, 0x5100 },
0117 { 4500, 0x5200 },
0118 { 4420, 0x5500 },
0119 { 4316, 0x5800 },
0120 { 4200, 0x5B00 },
0121 { 4119, 0x5D00 },
0122 { 3999, 0x6000 },
0123 { 3950, 0x6100 },
0124 { 3876, 0x6300 },
0125 { 3755, 0x6600 },
0126 { 3641, 0x6900 },
0127 { 3567, 0x6B00 },
0128 { 3425, 0x6F00 },
0129 { 3350, 0x7100 },
0130 { 3236, 0x7400 },
0131 { 3118, 0x7700 },
0132 { 3004, 0x7A00 },
0133 { 2917, 0x7C00 },
0134 { 2776, 0x7F00 },
0135 { 2635, 0x8200 },
0136 { 2516, 0x8500 },
0137 { 2406, 0x8800 },
0138 { 2290, 0x8B00 },
0139 { 2170, 0x8E00 },
0140 { 2073, 0x9100 },
0141 { 1949, 0x9400 },
0142 { 1836, 0x9700 },
0143 { 1712, 0x9A00 },
0144 { 1631, 0x9C00 },
0145 { 1515, 0x9F00 },
0146 { 1400, 0xA200 },
0147 { 1323, 0xA400 },
0148 { 1203, 0xA700 },
0149 { 1091, 0xAA00 },
0150 { 1011, 0xAC00 },
0151 { 904, 0xAF00 },
0152 { 787, 0xB200 },
0153 { 685, 0xB500 },
0154 { 571, 0xB800 },
0155 { 464, 0xBB00 },
0156 { 374, 0xBE00 },
0157 { 275, 0xC200 },
0158 { 181, 0xC600 },
0159 { 102, 0xCC00 },
0160 { 49, 0xD900 }
0161 };
0162
0163
0164
0165
0166
0167 static const struct slookup gain_channel_agc_nf_lookup[] = {
0168
0169 { 7082, 0x3000 },
0170 { 7052, 0x4000 },
0171 { 7007, 0x4600 },
0172 { 6954, 0x4A00 },
0173 { 6909, 0x4D00 },
0174 { 6833, 0x5100 },
0175 { 6753, 0x5400 },
0176 { 6659, 0x5700 },
0177 { 6561, 0x5A00 },
0178 { 6472, 0x5C00 },
0179 { 6366, 0x5F00 },
0180 { 6259, 0x6100 },
0181 { 6151, 0x6400 },
0182 { 6026, 0x6700 },
0183 { 5920, 0x6900 },
0184 { 5835, 0x6B00 },
0185 { 5770, 0x6C00 },
0186 { 5681, 0x6E00 },
0187 { 5596, 0x7000 },
0188 { 5503, 0x7200 },
0189 { 5429, 0x7300 },
0190 { 5319, 0x7500 },
0191 { 5220, 0x7700 },
0192 { 5111, 0x7900 },
0193 { 4983, 0x7B00 },
0194 { 4876, 0x7D00 },
0195 { 4755, 0x7F00 },
0196 { 4635, 0x8100 },
0197 { 4499, 0x8300 },
0198 { 4405, 0x8500 },
0199 { 4323, 0x8600 },
0200 { 4233, 0x8800 },
0201 { 4156, 0x8A00 },
0202 { 4038, 0x8C00 },
0203 { 3935, 0x8E00 },
0204 { 3823, 0x9000 },
0205 { 3712, 0x9200 },
0206 { 3601, 0x9500 },
0207 { 3511, 0x9700 },
0208 { 3413, 0x9900 },
0209 { 3309, 0x9B00 },
0210 { 3213, 0x9D00 },
0211 { 3088, 0x9F00 },
0212 { 2992, 0xA100 },
0213 { 2878, 0xA400 },
0214 { 2769, 0xA700 },
0215 { 2645, 0xAA00 },
0216 { 2538, 0xAD00 },
0217 { 2441, 0xB000 },
0218 { 2350, 0xB600 },
0219 { 2237, 0xBA00 },
0220 { 2137, 0xBF00 },
0221 { 2039, 0xC500 },
0222 { 1938, 0xDF00 },
0223 { 1927, 0xFF00 }
0224 };
0225
0226 static const struct slookup gain_channel_agc_iip3_lookup[] = {
0227
0228 { 7070, 0x3000 },
0229 { 7028, 0x4000 },
0230 { 7019, 0x4600 },
0231 { 6900, 0x4A00 },
0232 { 6811, 0x4D00 },
0233 { 6763, 0x5100 },
0234 { 6690, 0x5400 },
0235 { 6644, 0x5700 },
0236 { 6617, 0x5A00 },
0237 { 6598, 0x5C00 },
0238 { 6462, 0x5F00 },
0239 { 6348, 0x6100 },
0240 { 6197, 0x6400 },
0241 { 6154, 0x6700 },
0242 { 6098, 0x6900 },
0243 { 5893, 0x6B00 },
0244 { 5812, 0x6C00 },
0245 { 5773, 0x6E00 },
0246 { 5723, 0x7000 },
0247 { 5661, 0x7200 },
0248 { 5579, 0x7300 },
0249 { 5460, 0x7500 },
0250 { 5308, 0x7700 },
0251 { 5099, 0x7900 },
0252 { 4910, 0x7B00 },
0253 { 4800, 0x7D00 },
0254 { 4785, 0x7F00 },
0255 { 4635, 0x8100 },
0256 { 4466, 0x8300 },
0257 { 4314, 0x8500 },
0258 { 4295, 0x8600 },
0259 { 4144, 0x8800 },
0260 { 3920, 0x8A00 },
0261 { 3889, 0x8C00 },
0262 { 3771, 0x8E00 },
0263 { 3655, 0x9000 },
0264 { 3446, 0x9200 },
0265 { 3298, 0x9500 },
0266 { 3083, 0x9700 },
0267 { 3015, 0x9900 },
0268 { 2833, 0x9B00 },
0269 { 2746, 0x9D00 },
0270 { 2632, 0x9F00 },
0271 { 2598, 0xA100 },
0272 { 2480, 0xA400 },
0273 { 2236, 0xA700 },
0274 { 2171, 0xAA00 },
0275 { 2060, 0xAD00 },
0276 { 1999, 0xB000 },
0277 { 1974, 0xB600 },
0278 { 1820, 0xBA00 },
0279 { 1741, 0xBF00 },
0280 { 1655, 0xC500 },
0281 { 1444, 0xDF00 },
0282 { 1325, 0xFF00 },
0283 };
0284
0285 static inline u32 muldiv32(u32 a, u32 b, u32 c)
0286 {
0287 u64 tmp64;
0288
0289 tmp64 = (u64)a * (u64)b;
0290 do_div(tmp64, c);
0291
0292 return (u32)tmp64;
0293 }
0294
0295 static int i2c_read(struct i2c_adapter *adap,
0296 u8 adr, u8 *msg, int len, u8 *answ, int alen)
0297 {
0298 struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0,
0299 .buf = msg, .len = len},
0300 { .addr = adr, .flags = I2C_M_RD,
0301 .buf = answ, .len = alen } };
0302 if (i2c_transfer(adap, msgs, 2) != 2) {
0303 dev_err(&adap->dev, "i2c read error\n");
0304 return -EIO;
0305 }
0306 return 0;
0307 }
0308
0309 static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 *data, int len)
0310 {
0311 struct i2c_msg msg = {.addr = adr, .flags = 0,
0312 .buf = data, .len = len};
0313
0314 if (i2c_transfer(adap, &msg, 1) != 1) {
0315 dev_err(&adap->dev, "i2c write error\n");
0316 return -EIO;
0317 }
0318 return 0;
0319 }
0320
0321 static int write_regs(struct stv *state, int reg, int len)
0322 {
0323 u8 d[12];
0324
0325 memcpy(&d[1], &state->reg[reg], len);
0326 d[0] = reg;
0327 return i2c_write(state->i2c, state->adr, d, len + 1);
0328 }
0329
0330 static int write_reg(struct stv *state, u8 reg, u8 val)
0331 {
0332 u8 d[2] = {reg, val};
0333
0334 return i2c_write(state->i2c, state->adr, d, 2);
0335 }
0336
0337 static int read_reg(struct stv *state, u8 reg, u8 *val)
0338 {
0339 return i2c_read(state->i2c, state->adr, ®, 1, val, 1);
0340 }
0341
0342 static int wait_for_call_done(struct stv *state, u8 mask)
0343 {
0344 int status = 0;
0345 u32 lock_retry_count = 10;
0346
0347 while (lock_retry_count > 0) {
0348 u8 regval;
0349
0350 status = read_reg(state, 9, ®val);
0351 if (status < 0)
0352 return status;
0353
0354 if ((regval & mask) == 0)
0355 break;
0356 usleep_range(4000, 6000);
0357 lock_retry_count -= 1;
0358
0359 status = -EIO;
0360 }
0361 return status;
0362 }
0363
0364 static void init_state(struct stv *state)
0365 {
0366 u32 clkdiv = 0;
0367 u32 agcmode = 0;
0368 u32 agcref = 2;
0369 u32 agcset = 0xffffffff;
0370 u32 bbmode = 0xffffffff;
0371
0372 state->reg[0] = 0x08;
0373 state->reg[1] = 0x41;
0374 state->reg[2] = 0x8f;
0375 state->reg[3] = 0x00;
0376 state->reg[4] = 0xce;
0377 state->reg[5] = 0x54;
0378 state->reg[6] = 0x55;
0379 state->reg[7] = 0x45;
0380 state->reg[8] = 0x46;
0381 state->reg[9] = 0xbd;
0382 state->reg[10] = 0x11;
0383
0384 state->ref_freq = 16000;
0385
0386 if (clkdiv <= 3)
0387 state->reg[0x00] |= (clkdiv & 0x03);
0388 if (agcmode <= 3) {
0389 state->reg[0x03] |= (agcmode << 5);
0390 if (agcmode == 0x01)
0391 state->reg[0x01] |= 0x30;
0392 }
0393 if (bbmode <= 3)
0394 state->reg[0x01] = (state->reg[0x01] & ~0x30) | (bbmode << 4);
0395 if (agcref <= 7)
0396 state->reg[0x03] |= agcref;
0397 if (agcset <= 31)
0398 state->reg[0x02] = (state->reg[0x02] & ~0x1F) | agcset | 0x40;
0399 }
0400
0401 static int attach_init(struct stv *state)
0402 {
0403 if (write_regs(state, 0, 11))
0404 return -ENODEV;
0405 return 0;
0406 }
0407
0408 static void release(struct dvb_frontend *fe)
0409 {
0410 kfree(fe->tuner_priv);
0411 fe->tuner_priv = NULL;
0412 }
0413
0414 static int set_bandwidth(struct dvb_frontend *fe, u32 cutoff_frequency)
0415 {
0416 struct stv *state = fe->tuner_priv;
0417 u32 index = (cutoff_frequency + 999999) / 1000000;
0418 int stat = 0;
0419
0420 if (index < 6)
0421 index = 6;
0422 if (index > 50)
0423 index = 50;
0424 if ((state->reg[0x08] & ~0xFC) == ((index - 6) << 2))
0425 return 0;
0426
0427 state->reg[0x08] = (state->reg[0x08] & ~0xFC) | ((index - 6) << 2);
0428 state->reg[0x09] = (state->reg[0x09] & ~0x0C) | 0x08;
0429 if (fe->ops.i2c_gate_ctrl)
0430 stat = fe->ops.i2c_gate_ctrl(fe, 1);
0431 if (!stat) {
0432 write_regs(state, 0x08, 2);
0433 wait_for_call_done(state, 0x08);
0434 }
0435 if (fe->ops.i2c_gate_ctrl && !stat)
0436 fe->ops.i2c_gate_ctrl(fe, 0);
0437 return stat;
0438 }
0439
0440 static int set_lof(struct stv *state, u32 local_frequency, u32 cutoff_frequency)
0441 {
0442 u32 index = (cutoff_frequency + 999999) / 1000000;
0443 u32 frequency = (local_frequency + 500) / 1000;
0444 u32 p = 1, psel = 0, fvco, div, frac;
0445 u8 icp, tmp;
0446
0447 if (index < 6)
0448 index = 6;
0449 if (index > 50)
0450 index = 50;
0451
0452 if (frequency <= 1300000) {
0453 p = 4;
0454 psel = 1;
0455 } else {
0456 p = 2;
0457 psel = 0;
0458 }
0459 fvco = frequency * p;
0460 div = fvco / state->ref_freq;
0461 frac = fvco % state->ref_freq;
0462 frac = muldiv32(frac, 0x40000, state->ref_freq);
0463
0464 icp = 0;
0465 if (fvco < 2700000)
0466 icp = 0;
0467 else if (fvco < 2950000)
0468 icp = 1;
0469 else if (fvco < 3300000)
0470 icp = 2;
0471 else if (fvco < 3700000)
0472 icp = 3;
0473 else if (fvco < 4200000)
0474 icp = 5;
0475 else if (fvco < 4800000)
0476 icp = 6;
0477 else
0478 icp = 7;
0479
0480 state->reg[0x02] |= 0x80;
0481
0482 state->reg[0x03] = (state->reg[0x03] & ~0x80) | (psel << 7);
0483 state->reg[0x04] = (div & 0xFF);
0484 state->reg[0x05] = (((div >> 8) & 0x01) | ((frac & 0x7F) << 1)) & 0xff;
0485 state->reg[0x06] = ((frac >> 7) & 0xFF);
0486 state->reg[0x07] = (state->reg[0x07] & ~0x07) | ((frac >> 15) & 0x07);
0487 state->reg[0x07] = (state->reg[0x07] & ~0xE0) | (icp << 5);
0488
0489 state->reg[0x08] = (state->reg[0x08] & ~0xFC) | ((index - 6) << 2);
0490
0491 state->reg[0x09] = (state->reg[0x09] & ~0x0C) | 0x0C;
0492 write_regs(state, 2, 8);
0493
0494 wait_for_call_done(state, 0x0C);
0495
0496 usleep_range(10000, 12000);
0497
0498 read_reg(state, 0x03, &tmp);
0499 if (tmp & 0x10) {
0500 state->reg[0x02] &= ~0x80;
0501 write_regs(state, 2, 1);
0502 }
0503 read_reg(state, 0x08, &tmp);
0504
0505 state->frequency = frequency;
0506
0507 return 0;
0508 }
0509
0510 static int set_params(struct dvb_frontend *fe)
0511 {
0512 struct stv *state = fe->tuner_priv;
0513 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
0514 u32 freq, cutoff;
0515 int stat = 0;
0516
0517 if (p->delivery_system != SYS_DVBS && p->delivery_system != SYS_DVBS2)
0518 return -EINVAL;
0519
0520 freq = p->frequency * 1000;
0521 cutoff = 5000000 + muldiv32(p->symbol_rate, 135, 200);
0522
0523 if (fe->ops.i2c_gate_ctrl)
0524 stat = fe->ops.i2c_gate_ctrl(fe, 1);
0525 if (!stat)
0526 set_lof(state, freq, cutoff);
0527 if (fe->ops.i2c_gate_ctrl && !stat)
0528 fe->ops.i2c_gate_ctrl(fe, 0);
0529 return 0;
0530 }
0531
0532 static s32 table_lookup(const struct slookup *table,
0533 int table_size, u16 reg_value)
0534 {
0535 s32 gain;
0536 s32 reg_diff;
0537 int imin = 0;
0538 int imax = table_size - 1;
0539 int i;
0540
0541
0542 if (reg_value <= table[0].reg_value) {
0543 gain = table[0].value;
0544 } else if (reg_value >= table[imax].reg_value) {
0545 gain = table[imax].value;
0546 } else {
0547 while ((imax - imin) > 1) {
0548 i = (imax + imin) / 2;
0549 if ((table[imin].reg_value <= reg_value) &&
0550 (reg_value <= table[i].reg_value))
0551 imax = i;
0552 else
0553 imin = i;
0554 }
0555 reg_diff = table[imax].reg_value - table[imin].reg_value;
0556 gain = table[imin].value;
0557 if (reg_diff != 0)
0558 gain += ((s32)(reg_value - table[imin].reg_value) *
0559 (s32)(table[imax].value
0560 - table[imin].value)) / reg_diff;
0561 }
0562 return gain;
0563 }
0564
0565 static int get_rf_strength(struct dvb_frontend *fe, u16 *st)
0566 {
0567 struct stv *state = fe->tuner_priv;
0568 u16 rfagc = *st;
0569 s32 gain;
0570
0571 if ((state->reg[0x03] & 0x60) == 0) {
0572
0573 u8 reg = 0;
0574 int stat = 0;
0575
0576 if (fe->ops.i2c_gate_ctrl)
0577 stat = fe->ops.i2c_gate_ctrl(fe, 1);
0578 if (!stat) {
0579 write_reg(state, 0x02, state->reg[0x02] | 0x20);
0580 read_reg(state, 2, ®);
0581 if (reg & 0x20)
0582 read_reg(state, 2, ®);
0583 }
0584 if (fe->ops.i2c_gate_ctrl && !stat)
0585 fe->ops.i2c_gate_ctrl(fe, 0);
0586
0587 if ((state->reg[0x02] & 0x80) == 0)
0588
0589 gain = table_lookup(lnagain_nf_lookup,
0590 ARRAY_SIZE(lnagain_nf_lookup),
0591 reg & 0x1F);
0592 else
0593
0594 gain = table_lookup(lnagain_iip3_lookup,
0595 ARRAY_SIZE(lnagain_iip3_lookup),
0596 reg & 0x1F);
0597
0598 gain += table_lookup(gain_rfagc_lookup,
0599 ARRAY_SIZE(gain_rfagc_lookup), rfagc);
0600
0601 gain -= 2400;
0602 } else {
0603
0604 if ((state->reg[0x02] & 0x80) == 0) {
0605
0606 gain = table_lookup(
0607 gain_channel_agc_nf_lookup,
0608 ARRAY_SIZE(gain_channel_agc_nf_lookup), rfagc);
0609
0610 gain += 600;
0611 } else {
0612
0613 gain = table_lookup(
0614 gain_channel_agc_iip3_lookup,
0615 ARRAY_SIZE(gain_channel_agc_iip3_lookup),
0616 rfagc);
0617 }
0618 }
0619
0620 if (state->frequency > 0)
0621
0622 gain -= ((((s32)(state->frequency / 1000) - 1550) * 2) / 12);
0623
0624
0625 gain += (s32)((state->reg[0x01] & 0xC0) >> 6) * 600 - 1300;
0626
0627 if (gain < 0)
0628 gain = 0;
0629 else if (gain > 10000)
0630 gain = 10000;
0631
0632 *st = 10000 - gain;
0633
0634 return 0;
0635 }
0636
0637 static const struct dvb_tuner_ops tuner_ops = {
0638 .info = {
0639 .name = "ST STV6111",
0640 .frequency_min_hz = 950 * MHz,
0641 .frequency_max_hz = 2150 * MHz,
0642 },
0643 .set_params = set_params,
0644 .release = release,
0645 .get_rf_strength = get_rf_strength,
0646 .set_bandwidth = set_bandwidth,
0647 };
0648
0649 struct dvb_frontend *stv6111_attach(struct dvb_frontend *fe,
0650 struct i2c_adapter *i2c, u8 adr)
0651 {
0652 struct stv *state;
0653 int stat = -ENODEV;
0654 int gatestat = 0;
0655
0656 state = kzalloc(sizeof(*state), GFP_KERNEL);
0657 if (!state)
0658 return NULL;
0659 state->adr = adr;
0660 state->i2c = i2c;
0661 memcpy(&fe->ops.tuner_ops, &tuner_ops, sizeof(struct dvb_tuner_ops));
0662 init_state(state);
0663
0664 if (fe->ops.i2c_gate_ctrl)
0665 gatestat = fe->ops.i2c_gate_ctrl(fe, 1);
0666 if (!gatestat)
0667 stat = attach_init(state);
0668 if (fe->ops.i2c_gate_ctrl && !gatestat)
0669 fe->ops.i2c_gate_ctrl(fe, 0);
0670 if (stat < 0) {
0671 kfree(state);
0672 return NULL;
0673 }
0674 fe->tuner_priv = state;
0675 return fe;
0676 }
0677 EXPORT_SYMBOL_GPL(stv6111_attach);
0678
0679 MODULE_DESCRIPTION("ST STV6111 satellite tuner driver");
0680 MODULE_AUTHOR("Ralph Metzler, Manfred Voelkel");
0681 MODULE_LICENSE("GPL v2");