1 /* 2 * Driver for the ST STV6111 tuner 3 * 4 * Copyright (C) 2014 Digital Devices GmbH 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * version 2 only, as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 */ 16 17 #include <linux/kernel.h> 18 #include <linux/module.h> 19 #include <linux/moduleparam.h> 20 #include <linux/init.h> 21 #include <linux/delay.h> 22 #include <linux/firmware.h> 23 #include <linux/i2c.h> 24 #include <asm/div64.h> 25 26 #include "stv6111.h" 27 28 #include "dvb_frontend.h" 29 30 struct stv { 31 struct i2c_adapter *i2c; 32 u8 adr; 33 34 u8 reg[11]; 35 u32 ref_freq; 36 u32 frequency; 37 }; 38 39 struct slookup { 40 s16 value; 41 u16 reg_value; 42 }; 43 44 static const struct slookup lnagain_nf_lookup[] = { 45 /* Gain *100dB // Reg */ 46 { 2572, 0 }, 47 { 2575, 1 }, 48 { 2580, 2 }, 49 { 2588, 3 }, 50 { 2596, 4 }, 51 { 2611, 5 }, 52 { 2633, 6 }, 53 { 2664, 7 }, 54 { 2701, 8 }, 55 { 2753, 9 }, 56 { 2816, 10 }, 57 { 2902, 11 }, 58 { 2995, 12 }, 59 { 3104, 13 }, 60 { 3215, 14 }, 61 { 3337, 15 }, 62 { 3492, 16 }, 63 { 3614, 17 }, 64 { 3731, 18 }, 65 { 3861, 19 }, 66 { 3988, 20 }, 67 { 4124, 21 }, 68 { 4253, 22 }, 69 { 4386, 23 }, 70 { 4505, 24 }, 71 { 4623, 25 }, 72 { 4726, 26 }, 73 { 4821, 27 }, 74 { 4903, 28 }, 75 { 4979, 29 }, 76 { 5045, 30 }, 77 { 5102, 31 } 78 }; 79 80 static const struct slookup lnagain_iip3_lookup[] = { 81 /* Gain *100dB // reg */ 82 { 1548, 0 }, 83 { 1552, 1 }, 84 { 1569, 2 }, 85 { 1565, 3 }, 86 { 1577, 4 }, 87 { 1594, 5 }, 88 { 1627, 6 }, 89 { 1656, 7 }, 90 { 1700, 8 }, 91 { 1748, 9 }, 92 { 1805, 10 }, 93 { 1896, 11 }, 94 { 1995, 12 }, 95 { 2113, 13 }, 96 { 2233, 14 }, 97 { 2366, 15 }, 98 { 2543, 16 }, 99 { 2687, 17 }, 100 { 2842, 18 }, 101 { 2999, 19 }, 102 { 3167, 20 }, 103 { 3342, 21 }, 104 { 3507, 22 }, 105 { 3679, 23 }, 106 { 3827, 24 }, 107 { 3970, 25 }, 108 { 4094, 26 }, 109 { 4210, 27 }, 110 { 4308, 28 }, 111 { 4396, 29 }, 112 { 4468, 30 }, 113 { 4535, 31 } 114 }; 115 116 static const struct slookup gain_rfagc_lookup[] = { 117 /* Gain *100dB // reg */ 118 { 4870, 0x3000 }, 119 { 4850, 0x3C00 }, 120 { 4800, 0x4500 }, 121 { 4750, 0x4800 }, 122 { 4700, 0x4B00 }, 123 { 4650, 0x4D00 }, 124 { 4600, 0x4F00 }, 125 { 4550, 0x5100 }, 126 { 4500, 0x5200 }, 127 { 4420, 0x5500 }, 128 { 4316, 0x5800 }, 129 { 4200, 0x5B00 }, 130 { 4119, 0x5D00 }, 131 { 3999, 0x6000 }, 132 { 3950, 0x6100 }, 133 { 3876, 0x6300 }, 134 { 3755, 0x6600 }, 135 { 3641, 0x6900 }, 136 { 3567, 0x6B00 }, 137 { 3425, 0x6F00 }, 138 { 3350, 0x7100 }, 139 { 3236, 0x7400 }, 140 { 3118, 0x7700 }, 141 { 3004, 0x7A00 }, 142 { 2917, 0x7C00 }, 143 { 2776, 0x7F00 }, 144 { 2635, 0x8200 }, 145 { 2516, 0x8500 }, 146 { 2406, 0x8800 }, 147 { 2290, 0x8B00 }, 148 { 2170, 0x8E00 }, 149 { 2073, 0x9100 }, 150 { 1949, 0x9400 }, 151 { 1836, 0x9700 }, 152 { 1712, 0x9A00 }, 153 { 1631, 0x9C00 }, 154 { 1515, 0x9F00 }, 155 { 1400, 0xA200 }, 156 { 1323, 0xA400 }, 157 { 1203, 0xA700 }, 158 { 1091, 0xAA00 }, 159 { 1011, 0xAC00 }, 160 { 904, 0xAF00 }, 161 { 787, 0xB200 }, 162 { 685, 0xB500 }, 163 { 571, 0xB800 }, 164 { 464, 0xBB00 }, 165 { 374, 0xBE00 }, 166 { 275, 0xC200 }, 167 { 181, 0xC600 }, 168 { 102, 0xCC00 }, 169 { 49, 0xD900 } 170 }; 171 172 /* 173 * This table is 6 dB too low comapred to the others (probably created with 174 * a different BB_MAG setting) 175 */ 176 static const struct slookup gain_channel_agc_nf_lookup[] = { 177 /* Gain *100dB // reg */ 178 { 7082, 0x3000 }, 179 { 7052, 0x4000 }, 180 { 7007, 0x4600 }, 181 { 6954, 0x4A00 }, 182 { 6909, 0x4D00 }, 183 { 6833, 0x5100 }, 184 { 6753, 0x5400 }, 185 { 6659, 0x5700 }, 186 { 6561, 0x5A00 }, 187 { 6472, 0x5C00 }, 188 { 6366, 0x5F00 }, 189 { 6259, 0x6100 }, 190 { 6151, 0x6400 }, 191 { 6026, 0x6700 }, 192 { 5920, 0x6900 }, 193 { 5835, 0x6B00 }, 194 { 5770, 0x6C00 }, 195 { 5681, 0x6E00 }, 196 { 5596, 0x7000 }, 197 { 5503, 0x7200 }, 198 { 5429, 0x7300 }, 199 { 5319, 0x7500 }, 200 { 5220, 0x7700 }, 201 { 5111, 0x7900 }, 202 { 4983, 0x7B00 }, 203 { 4876, 0x7D00 }, 204 { 4755, 0x7F00 }, 205 { 4635, 0x8100 }, 206 { 4499, 0x8300 }, 207 { 4405, 0x8500 }, 208 { 4323, 0x8600 }, 209 { 4233, 0x8800 }, 210 { 4156, 0x8A00 }, 211 { 4038, 0x8C00 }, 212 { 3935, 0x8E00 }, 213 { 3823, 0x9000 }, 214 { 3712, 0x9200 }, 215 { 3601, 0x9500 }, 216 { 3511, 0x9700 }, 217 { 3413, 0x9900 }, 218 { 3309, 0x9B00 }, 219 { 3213, 0x9D00 }, 220 { 3088, 0x9F00 }, 221 { 2992, 0xA100 }, 222 { 2878, 0xA400 }, 223 { 2769, 0xA700 }, 224 { 2645, 0xAA00 }, 225 { 2538, 0xAD00 }, 226 { 2441, 0xB000 }, 227 { 2350, 0xB600 }, 228 { 2237, 0xBA00 }, 229 { 2137, 0xBF00 }, 230 { 2039, 0xC500 }, 231 { 1938, 0xDF00 }, 232 { 1927, 0xFF00 } 233 }; 234 235 static const struct slookup gain_channel_agc_iip3_lookup[] = { 236 /* Gain *100dB // reg */ 237 { 7070, 0x3000 }, 238 { 7028, 0x4000 }, 239 { 7019, 0x4600 }, 240 { 6900, 0x4A00 }, 241 { 6811, 0x4D00 }, 242 { 6763, 0x5100 }, 243 { 6690, 0x5400 }, 244 { 6644, 0x5700 }, 245 { 6617, 0x5A00 }, 246 { 6598, 0x5C00 }, 247 { 6462, 0x5F00 }, 248 { 6348, 0x6100 }, 249 { 6197, 0x6400 }, 250 { 6154, 0x6700 }, 251 { 6098, 0x6900 }, 252 { 5893, 0x6B00 }, 253 { 5812, 0x6C00 }, 254 { 5773, 0x6E00 }, 255 { 5723, 0x7000 }, 256 { 5661, 0x7200 }, 257 { 5579, 0x7300 }, 258 { 5460, 0x7500 }, 259 { 5308, 0x7700 }, 260 { 5099, 0x7900 }, 261 { 4910, 0x7B00 }, 262 { 4800, 0x7D00 }, 263 { 4785, 0x7F00 }, 264 { 4635, 0x8100 }, 265 { 4466, 0x8300 }, 266 { 4314, 0x8500 }, 267 { 4295, 0x8600 }, 268 { 4144, 0x8800 }, 269 { 3920, 0x8A00 }, 270 { 3889, 0x8C00 }, 271 { 3771, 0x8E00 }, 272 { 3655, 0x9000 }, 273 { 3446, 0x9200 }, 274 { 3298, 0x9500 }, 275 { 3083, 0x9700 }, 276 { 3015, 0x9900 }, 277 { 2833, 0x9B00 }, 278 { 2746, 0x9D00 }, 279 { 2632, 0x9F00 }, 280 { 2598, 0xA100 }, 281 { 2480, 0xA400 }, 282 { 2236, 0xA700 }, 283 { 2171, 0xAA00 }, 284 { 2060, 0xAD00 }, 285 { 1999, 0xB000 }, 286 { 1974, 0xB600 }, 287 { 1820, 0xBA00 }, 288 { 1741, 0xBF00 }, 289 { 1655, 0xC500 }, 290 { 1444, 0xDF00 }, 291 { 1325, 0xFF00 }, 292 }; 293 294 static inline u32 muldiv32(u32 a, u32 b, u32 c) 295 { 296 u64 tmp64; 297 298 tmp64 = (u64)a * (u64)b; 299 do_div(tmp64, c); 300 301 return (u32)tmp64; 302 } 303 304 static int i2c_read(struct i2c_adapter *adap, 305 u8 adr, u8 *msg, int len, u8 *answ, int alen) 306 { 307 struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0, 308 .buf = msg, .len = len}, 309 { .addr = adr, .flags = I2C_M_RD, 310 .buf = answ, .len = alen } }; 311 if (i2c_transfer(adap, msgs, 2) != 2) { 312 dev_err(&adap->dev, "i2c read error\n"); 313 return -EIO; 314 } 315 return 0; 316 } 317 318 static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 *data, int len) 319 { 320 struct i2c_msg msg = {.addr = adr, .flags = 0, 321 .buf = data, .len = len}; 322 323 if (i2c_transfer(adap, &msg, 1) != 1) { 324 dev_err(&adap->dev, "i2c write error\n"); 325 return -EIO; 326 } 327 return 0; 328 } 329 330 static int write_regs(struct stv *state, int reg, int len) 331 { 332 u8 d[12]; 333 334 memcpy(&d[1], &state->reg[reg], len); 335 d[0] = reg; 336 return i2c_write(state->i2c, state->adr, d, len + 1); 337 } 338 339 static int write_reg(struct stv *state, u8 reg, u8 val) 340 { 341 u8 d[2] = {reg, val}; 342 343 return i2c_write(state->i2c, state->adr, d, 2); 344 } 345 346 static int read_reg(struct stv *state, u8 reg, u8 *val) 347 { 348 return i2c_read(state->i2c, state->adr, ®, 1, val, 1); 349 } 350 351 static int wait_for_call_done(struct stv *state, u8 mask) 352 { 353 int status = 0; 354 u32 lock_retry_count = 10; 355 356 while (lock_retry_count > 0) { 357 u8 regval; 358 359 status = read_reg(state, 9, ®val); 360 if (status < 0) 361 return status; 362 363 if ((regval & mask) == 0) 364 break; 365 usleep_range(4000, 6000); 366 lock_retry_count -= 1; 367 368 status = -EIO; 369 } 370 return status; 371 } 372 373 static void init_state(struct stv *state) 374 { 375 u32 clkdiv = 0; 376 u32 agcmode = 0; 377 u32 agcref = 2; 378 u32 agcset = 0xffffffff; 379 u32 bbmode = 0xffffffff; 380 381 state->reg[0] = 0x08; 382 state->reg[1] = 0x41; 383 state->reg[2] = 0x8f; 384 state->reg[3] = 0x00; 385 state->reg[4] = 0xce; 386 state->reg[5] = 0x54; 387 state->reg[6] = 0x55; 388 state->reg[7] = 0x45; 389 state->reg[8] = 0x46; 390 state->reg[9] = 0xbd; 391 state->reg[10] = 0x11; 392 393 state->ref_freq = 16000; 394 395 if (clkdiv <= 3) 396 state->reg[0x00] |= (clkdiv & 0x03); 397 if (agcmode <= 3) { 398 state->reg[0x03] |= (agcmode << 5); 399 if (agcmode == 0x01) 400 state->reg[0x01] |= 0x30; 401 } 402 if (bbmode <= 3) 403 state->reg[0x01] = (state->reg[0x01] & ~0x30) | (bbmode << 4); 404 if (agcref <= 7) 405 state->reg[0x03] |= agcref; 406 if (agcset <= 31) 407 state->reg[0x02] = (state->reg[0x02] & ~0x1F) | agcset | 0x40; 408 } 409 410 static int attach_init(struct stv *state) 411 { 412 if (write_regs(state, 0, 11)) 413 return -ENODEV; 414 return 0; 415 } 416 417 static void release(struct dvb_frontend *fe) 418 { 419 kfree(fe->tuner_priv); 420 fe->tuner_priv = NULL; 421 } 422 423 static int set_bandwidth(struct dvb_frontend *fe, u32 cutoff_frequency) 424 { 425 struct stv *state = fe->tuner_priv; 426 u32 index = (cutoff_frequency + 999999) / 1000000; 427 428 if (index < 6) 429 index = 6; 430 if (index > 50) 431 index = 50; 432 if ((state->reg[0x08] & ~0xFC) == ((index - 6) << 2)) 433 return 0; 434 435 state->reg[0x08] = (state->reg[0x08] & ~0xFC) | ((index - 6) << 2); 436 state->reg[0x09] = (state->reg[0x09] & ~0x0C) | 0x08; 437 if (fe->ops.i2c_gate_ctrl) 438 fe->ops.i2c_gate_ctrl(fe, 1); 439 write_regs(state, 0x08, 2); 440 wait_for_call_done(state, 0x08); 441 if (fe->ops.i2c_gate_ctrl) 442 fe->ops.i2c_gate_ctrl(fe, 0); 443 return 0; 444 } 445 446 static int set_lof(struct stv *state, u32 local_frequency, u32 cutoff_frequency) 447 { 448 u32 index = (cutoff_frequency + 999999) / 1000000; 449 u32 frequency = (local_frequency + 500) / 1000; 450 u32 p = 1, psel = 0, fvco, div, frac; 451 u8 icp, tmp; 452 453 if (index < 6) 454 index = 6; 455 if (index > 50) 456 index = 50; 457 458 if (frequency <= 1300000) { 459 p = 4; 460 psel = 1; 461 } else { 462 p = 2; 463 psel = 0; 464 } 465 fvco = frequency * p; 466 div = fvco / state->ref_freq; 467 frac = fvco % state->ref_freq; 468 frac = muldiv32(frac, 0x40000, state->ref_freq); 469 470 icp = 0; 471 if (fvco < 2700000) 472 icp = 0; 473 else if (fvco < 2950000) 474 icp = 1; 475 else if (fvco < 3300000) 476 icp = 2; 477 else if (fvco < 3700000) 478 icp = 3; 479 else if (fvco < 4200000) 480 icp = 5; 481 else if (fvco < 4800000) 482 icp = 6; 483 else 484 icp = 7; 485 486 state->reg[0x02] |= 0x80; /* LNA IIP3 Mode */ 487 488 state->reg[0x03] = (state->reg[0x03] & ~0x80) | (psel << 7); 489 state->reg[0x04] = (div & 0xFF); 490 state->reg[0x05] = (((div >> 8) & 0x01) | ((frac & 0x7F) << 1)) & 0xff; 491 state->reg[0x06] = ((frac >> 7) & 0xFF); 492 state->reg[0x07] = (state->reg[0x07] & ~0x07) | ((frac >> 15) & 0x07); 493 state->reg[0x07] = (state->reg[0x07] & ~0xE0) | (icp << 5); 494 495 state->reg[0x08] = (state->reg[0x08] & ~0xFC) | ((index - 6) << 2); 496 /* Start cal vco,CF */ 497 state->reg[0x09] = (state->reg[0x09] & ~0x0C) | 0x0C; 498 write_regs(state, 2, 8); 499 500 wait_for_call_done(state, 0x0C); 501 502 usleep_range(10000, 12000); 503 504 read_reg(state, 0x03, &tmp); 505 if (tmp & 0x10) { 506 state->reg[0x02] &= ~0x80; /* LNA NF Mode */ 507 write_regs(state, 2, 1); 508 } 509 read_reg(state, 0x08, &tmp); 510 511 state->frequency = frequency; 512 513 return 0; 514 } 515 516 static int set_params(struct dvb_frontend *fe) 517 { 518 struct stv *state = fe->tuner_priv; 519 struct dtv_frontend_properties *p = &fe->dtv_property_cache; 520 u32 freq, cutoff; 521 522 if (p->delivery_system != SYS_DVBS && p->delivery_system != SYS_DVBS2) 523 return -EINVAL; 524 525 freq = p->frequency * 1000; 526 cutoff = 5000000 + muldiv32(p->symbol_rate, 135, 200); 527 528 if (fe->ops.i2c_gate_ctrl) 529 fe->ops.i2c_gate_ctrl(fe, 1); 530 set_lof(state, freq, cutoff); 531 if (fe->ops.i2c_gate_ctrl) 532 fe->ops.i2c_gate_ctrl(fe, 0); 533 return 0; 534 } 535 536 static s32 table_lookup(const struct slookup *table, 537 int table_size, u16 reg_value) 538 { 539 s32 gain; 540 s32 reg_diff; 541 int imin = 0; 542 int imax = table_size - 1; 543 int i; 544 545 /* Assumes Table[0].RegValue < Table[imax].RegValue */ 546 if (reg_value <= table[0].reg_value) { 547 gain = table[0].value; 548 } else if (reg_value >= table[imax].reg_value) { 549 gain = table[imax].value; 550 } else { 551 while ((imax - imin) > 1) { 552 i = (imax + imin) / 2; 553 if ((table[imin].reg_value <= reg_value) && 554 (reg_value <= table[i].reg_value)) 555 imax = i; 556 else 557 imin = i; 558 } 559 reg_diff = table[imax].reg_value - table[imin].reg_value; 560 gain = table[imin].value; 561 if (reg_diff != 0) 562 gain += ((s32)(reg_value - table[imin].reg_value) * 563 (s32)(table[imax].value 564 - table[imin].value)) / reg_diff; 565 } 566 return gain; 567 } 568 569 static int get_rf_strength(struct dvb_frontend *fe, u16 *st) 570 { 571 struct stv *state = fe->tuner_priv; 572 u16 rfagc = *st; 573 s32 gain; 574 575 if ((state->reg[0x03] & 0x60) == 0) { 576 /* RF Mode, Read AGC ADC */ 577 u8 reg = 0; 578 579 if (fe->ops.i2c_gate_ctrl) 580 fe->ops.i2c_gate_ctrl(fe, 1); 581 write_reg(state, 0x02, state->reg[0x02] | 0x20); 582 read_reg(state, 2, ®); 583 if (reg & 0x20) 584 read_reg(state, 2, ®); 585 if (fe->ops.i2c_gate_ctrl) 586 fe->ops.i2c_gate_ctrl(fe, 0); 587 588 if ((state->reg[0x02] & 0x80) == 0) 589 /* NF */ 590 gain = table_lookup(lnagain_nf_lookup, 591 ARRAY_SIZE(lnagain_nf_lookup), 592 reg & 0x1F); 593 else 594 /* IIP3 */ 595 gain = table_lookup(lnagain_iip3_lookup, 596 ARRAY_SIZE(lnagain_iip3_lookup), 597 reg & 0x1F); 598 599 gain += table_lookup(gain_rfagc_lookup, 600 ARRAY_SIZE(gain_rfagc_lookup), rfagc); 601 602 gain -= 2400; 603 } else { 604 /* Channel Mode */ 605 if ((state->reg[0x02] & 0x80) == 0) { 606 /* NF */ 607 gain = table_lookup( 608 gain_channel_agc_nf_lookup, 609 ARRAY_SIZE(gain_channel_agc_nf_lookup), rfagc); 610 611 gain += 600; 612 } else { 613 /* IIP3 */ 614 gain = table_lookup( 615 gain_channel_agc_iip3_lookup, 616 ARRAY_SIZE(gain_channel_agc_iip3_lookup), 617 rfagc); 618 } 619 } 620 621 if (state->frequency > 0) 622 /* Tilt correction ( 0.00016 dB/MHz ) */ 623 gain -= ((((s32)(state->frequency / 1000) - 1550) * 2) / 12); 624 625 /* + (BBGain * 10); */ 626 gain += (s32)((state->reg[0x01] & 0xC0) >> 6) * 600 - 1300; 627 628 if (gain < 0) 629 gain = 0; 630 else if (gain > 10000) 631 gain = 10000; 632 633 *st = 10000 - gain; 634 635 return 0; 636 } 637 638 static const struct dvb_tuner_ops tuner_ops = { 639 .info = { 640 .name = "ST STV6111", 641 .frequency_min = 950000, 642 .frequency_max = 2150000, 643 .frequency_step = 0 644 }, 645 .set_params = set_params, 646 .release = release, 647 .get_rf_strength = get_rf_strength, 648 .set_bandwidth = set_bandwidth, 649 }; 650 651 struct dvb_frontend *stv6111_attach(struct dvb_frontend *fe, 652 struct i2c_adapter *i2c, u8 adr) 653 { 654 struct stv *state; 655 int stat; 656 657 state = kzalloc(sizeof(*state), GFP_KERNEL); 658 if (!state) 659 return NULL; 660 state->adr = adr; 661 state->i2c = i2c; 662 memcpy(&fe->ops.tuner_ops, &tuner_ops, sizeof(struct dvb_tuner_ops)); 663 init_state(state); 664 665 if (fe->ops.i2c_gate_ctrl) 666 fe->ops.i2c_gate_ctrl(fe, 1); 667 stat = attach_init(state); 668 if (fe->ops.i2c_gate_ctrl) 669 fe->ops.i2c_gate_ctrl(fe, 0); 670 if (stat < 0) { 671 kfree(state); 672 return NULL; 673 } 674 fe->tuner_priv = state; 675 return fe; 676 } 677 EXPORT_SYMBOL_GPL(stv6111_attach); 678 679 MODULE_DESCRIPTION("ST STV6111 satellite tuner driver"); 680 MODULE_AUTHOR("Ralph Metzler, Manfred Voelkel"); 681 MODULE_LICENSE("GPL"); 682