1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Driver for the ST STV6111 tuner 4 * 5 * Copyright (C) 2014 Digital Devices GmbH 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * version 2 only, as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 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 <media/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 int stat = 0; 428 429 if (index < 6) 430 index = 6; 431 if (index > 50) 432 index = 50; 433 if ((state->reg[0x08] & ~0xFC) == ((index - 6) << 2)) 434 return 0; 435 436 state->reg[0x08] = (state->reg[0x08] & ~0xFC) | ((index - 6) << 2); 437 state->reg[0x09] = (state->reg[0x09] & ~0x0C) | 0x08; 438 if (fe->ops.i2c_gate_ctrl) 439 stat = fe->ops.i2c_gate_ctrl(fe, 1); 440 if (!stat) { 441 write_regs(state, 0x08, 2); 442 wait_for_call_done(state, 0x08); 443 } 444 if (fe->ops.i2c_gate_ctrl && !stat) 445 fe->ops.i2c_gate_ctrl(fe, 0); 446 return stat; 447 } 448 449 static int set_lof(struct stv *state, u32 local_frequency, u32 cutoff_frequency) 450 { 451 u32 index = (cutoff_frequency + 999999) / 1000000; 452 u32 frequency = (local_frequency + 500) / 1000; 453 u32 p = 1, psel = 0, fvco, div, frac; 454 u8 icp, tmp; 455 456 if (index < 6) 457 index = 6; 458 if (index > 50) 459 index = 50; 460 461 if (frequency <= 1300000) { 462 p = 4; 463 psel = 1; 464 } else { 465 p = 2; 466 psel = 0; 467 } 468 fvco = frequency * p; 469 div = fvco / state->ref_freq; 470 frac = fvco % state->ref_freq; 471 frac = muldiv32(frac, 0x40000, state->ref_freq); 472 473 icp = 0; 474 if (fvco < 2700000) 475 icp = 0; 476 else if (fvco < 2950000) 477 icp = 1; 478 else if (fvco < 3300000) 479 icp = 2; 480 else if (fvco < 3700000) 481 icp = 3; 482 else if (fvco < 4200000) 483 icp = 5; 484 else if (fvco < 4800000) 485 icp = 6; 486 else 487 icp = 7; 488 489 state->reg[0x02] |= 0x80; /* LNA IIP3 Mode */ 490 491 state->reg[0x03] = (state->reg[0x03] & ~0x80) | (psel << 7); 492 state->reg[0x04] = (div & 0xFF); 493 state->reg[0x05] = (((div >> 8) & 0x01) | ((frac & 0x7F) << 1)) & 0xff; 494 state->reg[0x06] = ((frac >> 7) & 0xFF); 495 state->reg[0x07] = (state->reg[0x07] & ~0x07) | ((frac >> 15) & 0x07); 496 state->reg[0x07] = (state->reg[0x07] & ~0xE0) | (icp << 5); 497 498 state->reg[0x08] = (state->reg[0x08] & ~0xFC) | ((index - 6) << 2); 499 /* Start cal vco,CF */ 500 state->reg[0x09] = (state->reg[0x09] & ~0x0C) | 0x0C; 501 write_regs(state, 2, 8); 502 503 wait_for_call_done(state, 0x0C); 504 505 usleep_range(10000, 12000); 506 507 read_reg(state, 0x03, &tmp); 508 if (tmp & 0x10) { 509 state->reg[0x02] &= ~0x80; /* LNA NF Mode */ 510 write_regs(state, 2, 1); 511 } 512 read_reg(state, 0x08, &tmp); 513 514 state->frequency = frequency; 515 516 return 0; 517 } 518 519 static int set_params(struct dvb_frontend *fe) 520 { 521 struct stv *state = fe->tuner_priv; 522 struct dtv_frontend_properties *p = &fe->dtv_property_cache; 523 u32 freq, cutoff; 524 int stat = 0; 525 526 if (p->delivery_system != SYS_DVBS && p->delivery_system != SYS_DVBS2) 527 return -EINVAL; 528 529 freq = p->frequency * 1000; 530 cutoff = 5000000 + muldiv32(p->symbol_rate, 135, 200); 531 532 if (fe->ops.i2c_gate_ctrl) 533 stat = fe->ops.i2c_gate_ctrl(fe, 1); 534 if (!stat) 535 set_lof(state, freq, cutoff); 536 if (fe->ops.i2c_gate_ctrl && !stat) 537 fe->ops.i2c_gate_ctrl(fe, 0); 538 return 0; 539 } 540 541 static s32 table_lookup(const struct slookup *table, 542 int table_size, u16 reg_value) 543 { 544 s32 gain; 545 s32 reg_diff; 546 int imin = 0; 547 int imax = table_size - 1; 548 int i; 549 550 /* Assumes Table[0].RegValue < Table[imax].RegValue */ 551 if (reg_value <= table[0].reg_value) { 552 gain = table[0].value; 553 } else if (reg_value >= table[imax].reg_value) { 554 gain = table[imax].value; 555 } else { 556 while ((imax - imin) > 1) { 557 i = (imax + imin) / 2; 558 if ((table[imin].reg_value <= reg_value) && 559 (reg_value <= table[i].reg_value)) 560 imax = i; 561 else 562 imin = i; 563 } 564 reg_diff = table[imax].reg_value - table[imin].reg_value; 565 gain = table[imin].value; 566 if (reg_diff != 0) 567 gain += ((s32)(reg_value - table[imin].reg_value) * 568 (s32)(table[imax].value 569 - table[imin].value)) / reg_diff; 570 } 571 return gain; 572 } 573 574 static int get_rf_strength(struct dvb_frontend *fe, u16 *st) 575 { 576 struct stv *state = fe->tuner_priv; 577 u16 rfagc = *st; 578 s32 gain; 579 580 if ((state->reg[0x03] & 0x60) == 0) { 581 /* RF Mode, Read AGC ADC */ 582 u8 reg = 0; 583 int stat = 0; 584 585 if (fe->ops.i2c_gate_ctrl) 586 stat = fe->ops.i2c_gate_ctrl(fe, 1); 587 if (!stat) { 588 write_reg(state, 0x02, state->reg[0x02] | 0x20); 589 read_reg(state, 2, ®); 590 if (reg & 0x20) 591 read_reg(state, 2, ®); 592 } 593 if (fe->ops.i2c_gate_ctrl && !stat) 594 fe->ops.i2c_gate_ctrl(fe, 0); 595 596 if ((state->reg[0x02] & 0x80) == 0) 597 /* NF */ 598 gain = table_lookup(lnagain_nf_lookup, 599 ARRAY_SIZE(lnagain_nf_lookup), 600 reg & 0x1F); 601 else 602 /* IIP3 */ 603 gain = table_lookup(lnagain_iip3_lookup, 604 ARRAY_SIZE(lnagain_iip3_lookup), 605 reg & 0x1F); 606 607 gain += table_lookup(gain_rfagc_lookup, 608 ARRAY_SIZE(gain_rfagc_lookup), rfagc); 609 610 gain -= 2400; 611 } else { 612 /* Channel Mode */ 613 if ((state->reg[0x02] & 0x80) == 0) { 614 /* NF */ 615 gain = table_lookup( 616 gain_channel_agc_nf_lookup, 617 ARRAY_SIZE(gain_channel_agc_nf_lookup), rfagc); 618 619 gain += 600; 620 } else { 621 /* IIP3 */ 622 gain = table_lookup( 623 gain_channel_agc_iip3_lookup, 624 ARRAY_SIZE(gain_channel_agc_iip3_lookup), 625 rfagc); 626 } 627 } 628 629 if (state->frequency > 0) 630 /* Tilt correction ( 0.00016 dB/MHz ) */ 631 gain -= ((((s32)(state->frequency / 1000) - 1550) * 2) / 12); 632 633 /* + (BBGain * 10); */ 634 gain += (s32)((state->reg[0x01] & 0xC0) >> 6) * 600 - 1300; 635 636 if (gain < 0) 637 gain = 0; 638 else if (gain > 10000) 639 gain = 10000; 640 641 *st = 10000 - gain; 642 643 return 0; 644 } 645 646 static const struct dvb_tuner_ops tuner_ops = { 647 .info = { 648 .name = "ST STV6111", 649 .frequency_min_hz = 950 * MHz, 650 .frequency_max_hz = 2150 * MHz, 651 }, 652 .set_params = set_params, 653 .release = release, 654 .get_rf_strength = get_rf_strength, 655 .set_bandwidth = set_bandwidth, 656 }; 657 658 struct dvb_frontend *stv6111_attach(struct dvb_frontend *fe, 659 struct i2c_adapter *i2c, u8 adr) 660 { 661 struct stv *state; 662 int stat = -ENODEV; 663 int gatestat = 0; 664 665 state = kzalloc(sizeof(*state), GFP_KERNEL); 666 if (!state) 667 return NULL; 668 state->adr = adr; 669 state->i2c = i2c; 670 memcpy(&fe->ops.tuner_ops, &tuner_ops, sizeof(struct dvb_tuner_ops)); 671 init_state(state); 672 673 if (fe->ops.i2c_gate_ctrl) 674 gatestat = fe->ops.i2c_gate_ctrl(fe, 1); 675 if (!gatestat) 676 stat = attach_init(state); 677 if (fe->ops.i2c_gate_ctrl && !gatestat) 678 fe->ops.i2c_gate_ctrl(fe, 0); 679 if (stat < 0) { 680 kfree(state); 681 return NULL; 682 } 683 fe->tuner_priv = state; 684 return fe; 685 } 686 EXPORT_SYMBOL_GPL(stv6111_attach); 687 688 MODULE_DESCRIPTION("ST STV6111 satellite tuner driver"); 689 MODULE_AUTHOR("Ralph Metzler, Manfred Voelkel"); 690 MODULE_LICENSE("GPL v2"); 691