1 /* 2 * Support for LG2160 - ATSC/MH 3 * 4 * Copyright (C) 2010 Michael Krufky <mkrufky@linuxtv.org> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 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 18 #include <linux/jiffies.h> 19 #include <linux/dvb/frontend.h> 20 #include "lg2160.h" 21 22 static int debug; 23 module_param(debug, int, 0644); 24 MODULE_PARM_DESC(debug, "set debug level (info=1, reg=2 (or-able))"); 25 26 #define DBG_INFO 1 27 #define DBG_REG 2 28 29 #define lg_printk(kern, fmt, arg...) \ 30 printk(kern "%s: " fmt, __func__, ##arg) 31 32 #define lg_info(fmt, arg...) printk(KERN_INFO "lg2160: " fmt, ##arg) 33 #define lg_warn(fmt, arg...) lg_printk(KERN_WARNING, fmt, ##arg) 34 #define lg_err(fmt, arg...) lg_printk(KERN_ERR, fmt, ##arg) 35 #define lg_dbg(fmt, arg...) if (debug & DBG_INFO) \ 36 lg_printk(KERN_DEBUG, fmt, ##arg) 37 #define lg_reg(fmt, arg...) if (debug & DBG_REG) \ 38 lg_printk(KERN_DEBUG, fmt, ##arg) 39 40 #define lg_fail(ret) \ 41 ({ \ 42 int __ret; \ 43 __ret = (ret < 0); \ 44 if (__ret) \ 45 lg_err("error %d on line %d\n", ret, __LINE__); \ 46 __ret; \ 47 }) 48 49 struct lg216x_state { 50 struct i2c_adapter *i2c_adap; 51 const struct lg2160_config *cfg; 52 53 struct dvb_frontend frontend; 54 55 u32 current_frequency; 56 u8 parade_id; 57 u8 fic_ver; 58 unsigned int last_reset; 59 }; 60 61 /* ------------------------------------------------------------------------ */ 62 63 static int lg216x_write_reg(struct lg216x_state *state, u16 reg, u8 val) 64 { 65 int ret; 66 u8 buf[] = { reg >> 8, reg & 0xff, val }; 67 struct i2c_msg msg = { 68 .addr = state->cfg->i2c_addr, .flags = 0, 69 .buf = buf, .len = 3, 70 }; 71 72 lg_reg("reg: 0x%04x, val: 0x%02x\n", reg, val); 73 74 ret = i2c_transfer(state->i2c_adap, &msg, 1); 75 76 if (ret != 1) { 77 lg_err("error (addr %02x %02x <- %02x, err = %i)\n", 78 msg.buf[0], msg.buf[1], msg.buf[2], ret); 79 if (ret < 0) 80 return ret; 81 else 82 return -EREMOTEIO; 83 } 84 return 0; 85 } 86 87 static int lg216x_read_reg(struct lg216x_state *state, u16 reg, u8 *val) 88 { 89 int ret; 90 u8 reg_buf[] = { reg >> 8, reg & 0xff }; 91 struct i2c_msg msg[] = { 92 { .addr = state->cfg->i2c_addr, 93 .flags = 0, .buf = reg_buf, .len = 2 }, 94 { .addr = state->cfg->i2c_addr, 95 .flags = I2C_M_RD, .buf = val, .len = 1 }, 96 }; 97 98 lg_reg("reg: 0x%04x\n", reg); 99 100 ret = i2c_transfer(state->i2c_adap, msg, 2); 101 102 if (ret != 2) { 103 lg_err("error (addr %02x reg %04x error (ret == %i)\n", 104 state->cfg->i2c_addr, reg, ret); 105 if (ret < 0) 106 return ret; 107 else 108 return -EREMOTEIO; 109 } 110 return 0; 111 } 112 113 struct lg216x_reg { 114 u16 reg; 115 u8 val; 116 }; 117 118 static int lg216x_write_regs(struct lg216x_state *state, 119 struct lg216x_reg *regs, int len) 120 { 121 int i, ret; 122 123 lg_reg("writing %d registers...\n", len); 124 125 for (i = 0; i < len; i++) { 126 ret = lg216x_write_reg(state, regs[i].reg, regs[i].val); 127 if (lg_fail(ret)) 128 return ret; 129 } 130 return 0; 131 } 132 133 static int lg216x_set_reg_bit(struct lg216x_state *state, 134 u16 reg, int bit, int onoff) 135 { 136 u8 val; 137 int ret; 138 139 lg_reg("reg: 0x%04x, bit: %d, level: %d\n", reg, bit, onoff); 140 141 ret = lg216x_read_reg(state, reg, &val); 142 if (lg_fail(ret)) 143 goto fail; 144 145 val &= ~(1 << bit); 146 val |= (onoff & 1) << bit; 147 148 ret = lg216x_write_reg(state, reg, val); 149 lg_fail(ret); 150 fail: 151 return ret; 152 } 153 154 /* ------------------------------------------------------------------------ */ 155 156 static int lg216x_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) 157 { 158 struct lg216x_state *state = fe->demodulator_priv; 159 int ret; 160 161 if (state->cfg->deny_i2c_rptr) 162 return 0; 163 164 lg_dbg("(%d)\n", enable); 165 166 ret = lg216x_set_reg_bit(state, 0x0000, 0, enable ? 0 : 1); 167 168 msleep(1); 169 170 return ret; 171 } 172 173 static int lg216x_soft_reset(struct lg216x_state *state) 174 { 175 int ret; 176 177 lg_dbg("\n"); 178 179 ret = lg216x_write_reg(state, 0x0002, 0x00); 180 if (lg_fail(ret)) 181 goto fail; 182 183 msleep(20); 184 ret = lg216x_write_reg(state, 0x0002, 0x01); 185 if (lg_fail(ret)) 186 goto fail; 187 188 state->last_reset = jiffies_to_msecs(jiffies); 189 fail: 190 return ret; 191 } 192 193 static int lg216x_initialize(struct lg216x_state *state) 194 { 195 int ret; 196 197 static struct lg216x_reg lg2160_init[] = { 198 #if 0 199 { .reg = 0x0015, .val = 0xe6 }, 200 #else 201 { .reg = 0x0015, .val = 0xf7 }, 202 { .reg = 0x001b, .val = 0x52 }, 203 { .reg = 0x0208, .val = 0x00 }, 204 { .reg = 0x0209, .val = 0x82 }, 205 { .reg = 0x0210, .val = 0xf9 }, 206 { .reg = 0x020a, .val = 0x00 }, 207 { .reg = 0x020b, .val = 0x82 }, 208 { .reg = 0x020d, .val = 0x28 }, 209 { .reg = 0x020f, .val = 0x14 }, 210 #endif 211 }; 212 213 static struct lg216x_reg lg2161_init[] = { 214 { .reg = 0x0000, .val = 0x41 }, 215 { .reg = 0x0001, .val = 0xfb }, 216 { .reg = 0x0216, .val = 0x00 }, 217 { .reg = 0x0219, .val = 0x00 }, 218 { .reg = 0x021b, .val = 0x55 }, 219 { .reg = 0x0606, .val = 0x0a }, 220 }; 221 222 switch (state->cfg->lg_chip) { 223 case LG2160: 224 ret = lg216x_write_regs(state, 225 lg2160_init, ARRAY_SIZE(lg2160_init)); 226 break; 227 case LG2161: 228 ret = lg216x_write_regs(state, 229 lg2161_init, ARRAY_SIZE(lg2161_init)); 230 break; 231 default: 232 ret = -EINVAL; 233 break; 234 } 235 if (lg_fail(ret)) 236 goto fail; 237 238 ret = lg216x_soft_reset(state); 239 lg_fail(ret); 240 fail: 241 return ret; 242 } 243 244 /* ------------------------------------------------------------------------ */ 245 246 static int lg216x_set_if(struct lg216x_state *state) 247 { 248 u8 val; 249 int ret; 250 251 lg_dbg("%d KHz\n", state->cfg->if_khz); 252 253 ret = lg216x_read_reg(state, 0x0132, &val); 254 if (lg_fail(ret)) 255 goto fail; 256 257 val &= 0xfb; 258 val |= (0 == state->cfg->if_khz) ? 0x04 : 0x00; 259 260 ret = lg216x_write_reg(state, 0x0132, val); 261 lg_fail(ret); 262 263 /* if NOT zero IF, 6 MHz is the default */ 264 fail: 265 return ret; 266 } 267 268 /* ------------------------------------------------------------------------ */ 269 270 static int lg2160_agc_fix(struct lg216x_state *state, 271 int if_agc_fix, int rf_agc_fix) 272 { 273 u8 val; 274 int ret; 275 276 ret = lg216x_read_reg(state, 0x0100, &val); 277 if (lg_fail(ret)) 278 goto fail; 279 280 val &= 0xf3; 281 val |= (if_agc_fix) ? 0x08 : 0x00; 282 val |= (rf_agc_fix) ? 0x04 : 0x00; 283 284 ret = lg216x_write_reg(state, 0x0100, val); 285 lg_fail(ret); 286 fail: 287 return ret; 288 } 289 290 #if 0 291 static int lg2160_agc_freeze(struct lg216x_state *state, 292 int if_agc_freeze, int rf_agc_freeze) 293 { 294 u8 val; 295 int ret; 296 297 ret = lg216x_read_reg(state, 0x0100, &val); 298 if (lg_fail(ret)) 299 goto fail; 300 301 val &= 0xcf; 302 val |= (if_agc_freeze) ? 0x20 : 0x00; 303 val |= (rf_agc_freeze) ? 0x10 : 0x00; 304 305 ret = lg216x_write_reg(state, 0x0100, val); 306 lg_fail(ret); 307 fail: 308 return ret; 309 } 310 #endif 311 312 static int lg2160_agc_polarity(struct lg216x_state *state, 313 int if_agc_polarity, int rf_agc_polarity) 314 { 315 u8 val; 316 int ret; 317 318 ret = lg216x_read_reg(state, 0x0100, &val); 319 if (lg_fail(ret)) 320 goto fail; 321 322 val &= 0xfc; 323 val |= (if_agc_polarity) ? 0x02 : 0x00; 324 val |= (rf_agc_polarity) ? 0x01 : 0x00; 325 326 ret = lg216x_write_reg(state, 0x0100, val); 327 lg_fail(ret); 328 fail: 329 return ret; 330 } 331 332 static int lg2160_tuner_pwr_save_polarity(struct lg216x_state *state, 333 int polarity) 334 { 335 u8 val; 336 int ret; 337 338 ret = lg216x_read_reg(state, 0x0008, &val); 339 if (lg_fail(ret)) 340 goto fail; 341 342 val &= 0xfe; 343 val |= (polarity) ? 0x01 : 0x00; 344 345 ret = lg216x_write_reg(state, 0x0008, val); 346 lg_fail(ret); 347 fail: 348 return ret; 349 } 350 351 static int lg2160_spectrum_polarity(struct lg216x_state *state, 352 int inverted) 353 { 354 u8 val; 355 int ret; 356 357 ret = lg216x_read_reg(state, 0x0132, &val); 358 if (lg_fail(ret)) 359 goto fail; 360 361 val &= 0xfd; 362 val |= (inverted) ? 0x02 : 0x00; 363 364 ret = lg216x_write_reg(state, 0x0132, val); 365 lg_fail(ret); 366 fail: 367 return lg216x_soft_reset(state); 368 } 369 370 static int lg2160_tuner_pwr_save(struct lg216x_state *state, int onoff) 371 { 372 u8 val; 373 int ret; 374 375 ret = lg216x_read_reg(state, 0x0007, &val); 376 if (lg_fail(ret)) 377 goto fail; 378 379 val &= 0xbf; 380 val |= (onoff) ? 0x40 : 0x00; 381 382 ret = lg216x_write_reg(state, 0x0007, val); 383 lg_fail(ret); 384 fail: 385 return ret; 386 } 387 388 static int lg216x_set_parade(struct lg216x_state *state, int id) 389 { 390 int ret; 391 392 ret = lg216x_write_reg(state, 0x013e, id & 0x7f); 393 if (lg_fail(ret)) 394 goto fail; 395 396 state->parade_id = id & 0x7f; 397 fail: 398 return ret; 399 } 400 401 static int lg216x_set_ensemble(struct lg216x_state *state, int id) 402 { 403 int ret; 404 u16 reg; 405 u8 val; 406 407 switch (state->cfg->lg_chip) { 408 case LG2160: 409 reg = 0x0400; 410 break; 411 case LG2161: 412 default: 413 reg = 0x0500; 414 break; 415 } 416 417 ret = lg216x_read_reg(state, reg, &val); 418 if (lg_fail(ret)) 419 goto fail; 420 421 val &= 0xfe; 422 val |= (id) ? 0x01 : 0x00; 423 424 ret = lg216x_write_reg(state, reg, val); 425 lg_fail(ret); 426 fail: 427 return ret; 428 } 429 430 static int lg2160_set_spi_clock(struct lg216x_state *state) 431 { 432 u8 val; 433 int ret; 434 435 ret = lg216x_read_reg(state, 0x0014, &val); 436 if (lg_fail(ret)) 437 goto fail; 438 439 val &= 0xf3; 440 val |= (state->cfg->spi_clock << 2); 441 442 ret = lg216x_write_reg(state, 0x0014, val); 443 lg_fail(ret); 444 fail: 445 return ret; 446 } 447 448 static int lg2161_set_output_interface(struct lg216x_state *state) 449 { 450 u8 val; 451 int ret; 452 453 ret = lg216x_read_reg(state, 0x0014, &val); 454 if (lg_fail(ret)) 455 goto fail; 456 457 val &= ~0x07; 458 val |= state->cfg->output_if; /* FIXME: needs sanity check */ 459 460 ret = lg216x_write_reg(state, 0x0014, val); 461 lg_fail(ret); 462 fail: 463 return ret; 464 } 465 466 static int lg216x_enable_fic(struct lg216x_state *state, int onoff) 467 { 468 int ret; 469 470 ret = lg216x_write_reg(state, 0x0017, 0x23); 471 if (lg_fail(ret)) 472 goto fail; 473 474 ret = lg216x_write_reg(state, 0x0016, 0xfc); 475 if (lg_fail(ret)) 476 goto fail; 477 478 switch (state->cfg->lg_chip) { 479 case LG2160: 480 ret = lg216x_write_reg(state, 0x0016, 481 0xfc | ((onoff) ? 0x02 : 0x00)); 482 break; 483 case LG2161: 484 ret = lg216x_write_reg(state, 0x0016, (onoff) ? 0x10 : 0x00); 485 break; 486 } 487 if (lg_fail(ret)) 488 goto fail; 489 490 ret = lg216x_initialize(state); 491 if (lg_fail(ret)) 492 goto fail; 493 494 if (onoff) { 495 ret = lg216x_write_reg(state, 0x0017, 0x03); 496 lg_fail(ret); 497 } 498 fail: 499 return ret; 500 } 501 502 /* ------------------------------------------------------------------------ */ 503 504 static int lg216x_get_fic_version(struct lg216x_state *state, u8 *ficver) 505 { 506 u8 val; 507 int ret; 508 509 *ficver = 0xff; /* invalid value */ 510 511 ret = lg216x_read_reg(state, 0x0128, &val); 512 if (lg_fail(ret)) 513 goto fail; 514 515 *ficver = (val >> 3) & 0x1f; 516 fail: 517 return ret; 518 } 519 520 #if 0 521 static int lg2160_get_parade_id(struct lg216x_state *state, u8 *id) 522 { 523 u8 val; 524 int ret; 525 526 *id = 0xff; /* invalid value */ 527 528 ret = lg216x_read_reg(state, 0x0123, &val); 529 if (lg_fail(ret)) 530 goto fail; 531 532 *id = val & 0x7f; 533 fail: 534 return ret; 535 } 536 #endif 537 538 static int lg216x_get_nog(struct lg216x_state *state, u8 *nog) 539 { 540 u8 val; 541 int ret; 542 543 *nog = 0xff; /* invalid value */ 544 545 ret = lg216x_read_reg(state, 0x0124, &val); 546 if (lg_fail(ret)) 547 goto fail; 548 549 *nog = ((val >> 4) & 0x07) + 1; 550 fail: 551 return ret; 552 } 553 554 static int lg216x_get_tnog(struct lg216x_state *state, u8 *tnog) 555 { 556 u8 val; 557 int ret; 558 559 *tnog = 0xff; /* invalid value */ 560 561 ret = lg216x_read_reg(state, 0x0125, &val); 562 if (lg_fail(ret)) 563 goto fail; 564 565 *tnog = val & 0x1f; 566 fail: 567 return ret; 568 } 569 570 static int lg216x_get_sgn(struct lg216x_state *state, u8 *sgn) 571 { 572 u8 val; 573 int ret; 574 575 *sgn = 0xff; /* invalid value */ 576 577 ret = lg216x_read_reg(state, 0x0124, &val); 578 if (lg_fail(ret)) 579 goto fail; 580 581 *sgn = val & 0x0f; 582 fail: 583 return ret; 584 } 585 586 static int lg216x_get_prc(struct lg216x_state *state, u8 *prc) 587 { 588 u8 val; 589 int ret; 590 591 *prc = 0xff; /* invalid value */ 592 593 ret = lg216x_read_reg(state, 0x0125, &val); 594 if (lg_fail(ret)) 595 goto fail; 596 597 *prc = ((val >> 5) & 0x07) + 1; 598 fail: 599 return ret; 600 } 601 602 /* ------------------------------------------------------------------------ */ 603 604 static int lg216x_get_rs_frame_mode(struct lg216x_state *state, 605 enum atscmh_rs_frame_mode *rs_framemode) 606 { 607 u8 val; 608 int ret; 609 610 switch (state->cfg->lg_chip) { 611 case LG2160: 612 ret = lg216x_read_reg(state, 0x0410, &val); 613 break; 614 case LG2161: 615 ret = lg216x_read_reg(state, 0x0513, &val); 616 break; 617 default: 618 ret = -EINVAL; 619 } 620 if (lg_fail(ret)) 621 goto fail; 622 623 switch ((val >> 4) & 0x03) { 624 #if 1 625 default: 626 #endif 627 case 0x00: 628 *rs_framemode = ATSCMH_RSFRAME_PRI_ONLY; 629 break; 630 case 0x01: 631 *rs_framemode = ATSCMH_RSFRAME_PRI_SEC; 632 break; 633 #if 0 634 default: 635 *rs_framemode = ATSCMH_RSFRAME_RES; 636 break; 637 #endif 638 } 639 fail: 640 return ret; 641 } 642 643 static 644 int lg216x_get_rs_frame_ensemble(struct lg216x_state *state, 645 enum atscmh_rs_frame_ensemble *rs_frame_ens) 646 { 647 u8 val; 648 int ret; 649 650 switch (state->cfg->lg_chip) { 651 case LG2160: 652 ret = lg216x_read_reg(state, 0x0400, &val); 653 break; 654 case LG2161: 655 ret = lg216x_read_reg(state, 0x0500, &val); 656 break; 657 default: 658 ret = -EINVAL; 659 } 660 if (lg_fail(ret)) 661 goto fail; 662 663 val &= 0x01; 664 *rs_frame_ens = (enum atscmh_rs_frame_ensemble) val; 665 fail: 666 return ret; 667 } 668 669 static int lg216x_get_rs_code_mode(struct lg216x_state *state, 670 enum atscmh_rs_code_mode *rs_code_pri, 671 enum atscmh_rs_code_mode *rs_code_sec) 672 { 673 u8 val; 674 int ret; 675 676 switch (state->cfg->lg_chip) { 677 case LG2160: 678 ret = lg216x_read_reg(state, 0x0410, &val); 679 break; 680 case LG2161: 681 ret = lg216x_read_reg(state, 0x0513, &val); 682 break; 683 default: 684 ret = -EINVAL; 685 } 686 if (lg_fail(ret)) 687 goto fail; 688 689 *rs_code_pri = (enum atscmh_rs_code_mode) ((val >> 2) & 0x03); 690 *rs_code_sec = (enum atscmh_rs_code_mode) (val & 0x03); 691 fail: 692 return ret; 693 } 694 695 static int lg216x_get_sccc_block_mode(struct lg216x_state *state, 696 enum atscmh_sccc_block_mode *sccc_block) 697 { 698 u8 val; 699 int ret; 700 701 switch (state->cfg->lg_chip) { 702 case LG2160: 703 ret = lg216x_read_reg(state, 0x0315, &val); 704 break; 705 case LG2161: 706 ret = lg216x_read_reg(state, 0x0511, &val); 707 break; 708 default: 709 ret = -EINVAL; 710 } 711 if (lg_fail(ret)) 712 goto fail; 713 714 switch (val & 0x03) { 715 case 0x00: 716 *sccc_block = ATSCMH_SCCC_BLK_SEP; 717 break; 718 case 0x01: 719 *sccc_block = ATSCMH_SCCC_BLK_COMB; 720 break; 721 default: 722 *sccc_block = ATSCMH_SCCC_BLK_RES; 723 break; 724 } 725 fail: 726 return ret; 727 } 728 729 static int lg216x_get_sccc_code_mode(struct lg216x_state *state, 730 enum atscmh_sccc_code_mode *mode_a, 731 enum atscmh_sccc_code_mode *mode_b, 732 enum atscmh_sccc_code_mode *mode_c, 733 enum atscmh_sccc_code_mode *mode_d) 734 { 735 u8 val; 736 int ret; 737 738 switch (state->cfg->lg_chip) { 739 case LG2160: 740 ret = lg216x_read_reg(state, 0x0316, &val); 741 break; 742 case LG2161: 743 ret = lg216x_read_reg(state, 0x0512, &val); 744 break; 745 default: 746 ret = -EINVAL; 747 } 748 if (lg_fail(ret)) 749 goto fail; 750 751 switch ((val >> 6) & 0x03) { 752 case 0x00: 753 *mode_a = ATSCMH_SCCC_CODE_HLF; 754 break; 755 case 0x01: 756 *mode_a = ATSCMH_SCCC_CODE_QTR; 757 break; 758 default: 759 *mode_a = ATSCMH_SCCC_CODE_RES; 760 break; 761 } 762 763 switch ((val >> 4) & 0x03) { 764 case 0x00: 765 *mode_b = ATSCMH_SCCC_CODE_HLF; 766 break; 767 case 0x01: 768 *mode_b = ATSCMH_SCCC_CODE_QTR; 769 break; 770 default: 771 *mode_b = ATSCMH_SCCC_CODE_RES; 772 break; 773 } 774 775 switch ((val >> 2) & 0x03) { 776 case 0x00: 777 *mode_c = ATSCMH_SCCC_CODE_HLF; 778 break; 779 case 0x01: 780 *mode_c = ATSCMH_SCCC_CODE_QTR; 781 break; 782 default: 783 *mode_c = ATSCMH_SCCC_CODE_RES; 784 break; 785 } 786 787 switch (val & 0x03) { 788 case 0x00: 789 *mode_d = ATSCMH_SCCC_CODE_HLF; 790 break; 791 case 0x01: 792 *mode_d = ATSCMH_SCCC_CODE_QTR; 793 break; 794 default: 795 *mode_d = ATSCMH_SCCC_CODE_RES; 796 break; 797 } 798 fail: 799 return ret; 800 } 801 802 /* ------------------------------------------------------------------------ */ 803 804 #if 0 805 static int lg216x_read_fic_err_count(struct lg216x_state *state, u8 *err) 806 { 807 u8 fic_err; 808 int ret; 809 810 *err = 0; 811 812 switch (state->cfg->lg_chip) { 813 case LG2160: 814 ret = lg216x_read_reg(state, 0x0012, &fic_err); 815 break; 816 case LG2161: 817 ret = lg216x_read_reg(state, 0x001e, &fic_err); 818 break; 819 } 820 if (lg_fail(ret)) 821 goto fail; 822 823 *err = fic_err; 824 fail: 825 return ret; 826 } 827 828 static int lg2160_read_crc_err_count(struct lg216x_state *state, u16 *err) 829 { 830 u8 crc_err1, crc_err2; 831 int ret; 832 833 *err = 0; 834 835 ret = lg216x_read_reg(state, 0x0411, &crc_err1); 836 if (lg_fail(ret)) 837 goto fail; 838 839 ret = lg216x_read_reg(state, 0x0412, &crc_err2); 840 if (lg_fail(ret)) 841 goto fail; 842 843 *err = (u16)(((crc_err2 & 0x0f) << 8) | crc_err1); 844 fail: 845 return ret; 846 } 847 848 static int lg2161_read_crc_err_count(struct lg216x_state *state, u16 *err) 849 { 850 u8 crc_err; 851 int ret; 852 853 *err = 0; 854 855 ret = lg216x_read_reg(state, 0x0612, &crc_err); 856 if (lg_fail(ret)) 857 goto fail; 858 859 *err = (u16)crc_err; 860 fail: 861 return ret; 862 } 863 864 static int lg216x_read_crc_err_count(struct lg216x_state *state, u16 *err) 865 { 866 int ret; 867 switch (state->cfg->lg_chip) { 868 case LG2160: 869 ret = lg2160_read_crc_err_count(state, err); 870 break; 871 case LG2161: 872 ret = lg2161_read_crc_err_count(state, err); 873 break; 874 default: 875 ret = -EINVAL; 876 break; 877 } 878 return ret; 879 } 880 881 static int lg2160_read_rs_err_count(struct lg216x_state *state, u16 *err) 882 { 883 u8 rs_err1, rs_err2; 884 int ret; 885 886 *err = 0; 887 888 ret = lg216x_read_reg(state, 0x0413, &rs_err1); 889 if (lg_fail(ret)) 890 goto fail; 891 892 ret = lg216x_read_reg(state, 0x0414, &rs_err2); 893 if (lg_fail(ret)) 894 goto fail; 895 896 *err = (u16)(((rs_err2 & 0x0f) << 8) | rs_err1); 897 fail: 898 return ret; 899 } 900 901 static int lg2161_read_rs_err_count(struct lg216x_state *state, u16 *err) 902 { 903 u8 rs_err1, rs_err2; 904 int ret; 905 906 *err = 0; 907 908 ret = lg216x_read_reg(state, 0x0613, &rs_err1); 909 if (lg_fail(ret)) 910 goto fail; 911 912 ret = lg216x_read_reg(state, 0x0614, &rs_err2); 913 if (lg_fail(ret)) 914 goto fail; 915 916 *err = (u16)((rs_err1 << 8) | rs_err2); 917 fail: 918 return ret; 919 } 920 921 static int lg216x_read_rs_err_count(struct lg216x_state *state, u16 *err) 922 { 923 int ret; 924 switch (state->cfg->lg_chip) { 925 case LG2160: 926 ret = lg2160_read_rs_err_count(state, err); 927 break; 928 case LG2161: 929 ret = lg2161_read_rs_err_count(state, err); 930 break; 931 default: 932 ret = -EINVAL; 933 break; 934 } 935 return ret; 936 } 937 #endif 938 939 /* ------------------------------------------------------------------------ */ 940 941 static int lg216x_get_frontend(struct dvb_frontend *fe, 942 struct dtv_frontend_properties *c) 943 { 944 struct lg216x_state *state = fe->demodulator_priv; 945 int ret; 946 947 lg_dbg("\n"); 948 949 c->modulation = VSB_8; 950 c->frequency = state->current_frequency; 951 c->delivery_system = SYS_ATSCMH; 952 953 ret = lg216x_get_fic_version(state, 954 &c->atscmh_fic_ver); 955 if (lg_fail(ret)) 956 goto fail; 957 if (state->fic_ver != c->atscmh_fic_ver) { 958 state->fic_ver = c->atscmh_fic_ver; 959 960 #if 0 961 ret = lg2160_get_parade_id(state, 962 &c->atscmh_parade_id); 963 if (lg_fail(ret)) 964 goto fail; 965 /* #else */ 966 c->atscmh_parade_id = state->parade_id; 967 #endif 968 ret = lg216x_get_nog(state, 969 &c->atscmh_nog); 970 if (lg_fail(ret)) 971 goto fail; 972 ret = lg216x_get_tnog(state, 973 &c->atscmh_tnog); 974 if (lg_fail(ret)) 975 goto fail; 976 ret = lg216x_get_sgn(state, 977 &c->atscmh_sgn); 978 if (lg_fail(ret)) 979 goto fail; 980 ret = lg216x_get_prc(state, 981 &c->atscmh_prc); 982 if (lg_fail(ret)) 983 goto fail; 984 985 ret = lg216x_get_rs_frame_mode(state, 986 (enum atscmh_rs_frame_mode *) 987 &c->atscmh_rs_frame_mode); 988 if (lg_fail(ret)) 989 goto fail; 990 ret = lg216x_get_rs_frame_ensemble(state, 991 (enum atscmh_rs_frame_ensemble *) 992 &c->atscmh_rs_frame_ensemble); 993 if (lg_fail(ret)) 994 goto fail; 995 ret = lg216x_get_rs_code_mode(state, 996 (enum atscmh_rs_code_mode *) 997 &c->atscmh_rs_code_mode_pri, 998 (enum atscmh_rs_code_mode *) 999 &c->atscmh_rs_code_mode_sec); 1000 if (lg_fail(ret)) 1001 goto fail; 1002 ret = lg216x_get_sccc_block_mode(state, 1003 (enum atscmh_sccc_block_mode *) 1004 &c->atscmh_sccc_block_mode); 1005 if (lg_fail(ret)) 1006 goto fail; 1007 ret = lg216x_get_sccc_code_mode(state, 1008 (enum atscmh_sccc_code_mode *) 1009 &c->atscmh_sccc_code_mode_a, 1010 (enum atscmh_sccc_code_mode *) 1011 &c->atscmh_sccc_code_mode_b, 1012 (enum atscmh_sccc_code_mode *) 1013 &c->atscmh_sccc_code_mode_c, 1014 (enum atscmh_sccc_code_mode *) 1015 &c->atscmh_sccc_code_mode_d); 1016 if (lg_fail(ret)) 1017 goto fail; 1018 } 1019 #if 0 1020 ret = lg216x_read_fic_err_count(state, 1021 (u8 *)&c->atscmh_fic_err); 1022 if (lg_fail(ret)) 1023 goto fail; 1024 ret = lg216x_read_crc_err_count(state, 1025 &c->atscmh_crc_err); 1026 if (lg_fail(ret)) 1027 goto fail; 1028 ret = lg216x_read_rs_err_count(state, 1029 &c->atscmh_rs_err); 1030 if (lg_fail(ret)) 1031 goto fail; 1032 1033 switch (state->cfg->lg_chip) { 1034 case LG2160: 1035 if (((c->atscmh_rs_err >= 240) && 1036 (c->atscmh_crc_err >= 240)) && 1037 ((jiffies_to_msecs(jiffies) - state->last_reset) > 6000)) 1038 ret = lg216x_soft_reset(state); 1039 break; 1040 case LG2161: 1041 /* no fix needed here (as far as we know) */ 1042 ret = 0; 1043 break; 1044 } 1045 lg_fail(ret); 1046 #endif 1047 fail: 1048 return ret; 1049 } 1050 1051 static int lg216x_get_property(struct dvb_frontend *fe, 1052 struct dtv_property *tvp) 1053 { 1054 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1055 1056 return (DTV_ATSCMH_FIC_VER == tvp->cmd) ? 1057 lg216x_get_frontend(fe, c) : 0; 1058 } 1059 1060 1061 static int lg2160_set_frontend(struct dvb_frontend *fe) 1062 { 1063 struct lg216x_state *state = fe->demodulator_priv; 1064 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1065 int ret; 1066 1067 lg_dbg("(%d)\n", fe->dtv_property_cache.frequency); 1068 1069 if (fe->ops.tuner_ops.set_params) { 1070 ret = fe->ops.tuner_ops.set_params(fe); 1071 if (fe->ops.i2c_gate_ctrl) 1072 fe->ops.i2c_gate_ctrl(fe, 0); 1073 if (lg_fail(ret)) 1074 goto fail; 1075 state->current_frequency = fe->dtv_property_cache.frequency; 1076 } 1077 1078 ret = lg2160_agc_fix(state, 0, 0); 1079 if (lg_fail(ret)) 1080 goto fail; 1081 ret = lg2160_agc_polarity(state, 0, 0); 1082 if (lg_fail(ret)) 1083 goto fail; 1084 ret = lg2160_tuner_pwr_save_polarity(state, 1); 1085 if (lg_fail(ret)) 1086 goto fail; 1087 ret = lg216x_set_if(state); 1088 if (lg_fail(ret)) 1089 goto fail; 1090 ret = lg2160_spectrum_polarity(state, state->cfg->spectral_inversion); 1091 if (lg_fail(ret)) 1092 goto fail; 1093 1094 /* be tuned before this point */ 1095 ret = lg216x_soft_reset(state); 1096 if (lg_fail(ret)) 1097 goto fail; 1098 1099 ret = lg2160_tuner_pwr_save(state, 0); 1100 if (lg_fail(ret)) 1101 goto fail; 1102 1103 switch (state->cfg->lg_chip) { 1104 case LG2160: 1105 ret = lg2160_set_spi_clock(state); 1106 if (lg_fail(ret)) 1107 goto fail; 1108 break; 1109 case LG2161: 1110 ret = lg2161_set_output_interface(state); 1111 if (lg_fail(ret)) 1112 goto fail; 1113 break; 1114 } 1115 1116 ret = lg216x_set_parade(state, fe->dtv_property_cache.atscmh_parade_id); 1117 if (lg_fail(ret)) 1118 goto fail; 1119 1120 ret = lg216x_set_ensemble(state, 1121 fe->dtv_property_cache.atscmh_rs_frame_ensemble); 1122 if (lg_fail(ret)) 1123 goto fail; 1124 1125 ret = lg216x_initialize(state); 1126 if (lg_fail(ret)) 1127 goto fail; 1128 1129 ret = lg216x_enable_fic(state, 1); 1130 lg_fail(ret); 1131 1132 lg216x_get_frontend(fe, c); 1133 fail: 1134 return ret; 1135 } 1136 1137 /* ------------------------------------------------------------------------ */ 1138 1139 static int lg2160_read_lock_status(struct lg216x_state *state, 1140 int *acq_lock, int *sync_lock) 1141 { 1142 u8 val; 1143 int ret; 1144 1145 *acq_lock = 0; 1146 *sync_lock = 0; 1147 1148 ret = lg216x_read_reg(state, 0x011b, &val); 1149 if (lg_fail(ret)) 1150 goto fail; 1151 1152 *sync_lock = (val & 0x20) ? 0 : 1; 1153 *acq_lock = (val & 0x40) ? 0 : 1; 1154 fail: 1155 return ret; 1156 } 1157 1158 #ifdef USE_LG2161_LOCK_BITS 1159 static int lg2161_read_lock_status(struct lg216x_state *state, 1160 int *acq_lock, int *sync_lock) 1161 { 1162 u8 val; 1163 int ret; 1164 1165 *acq_lock = 0; 1166 *sync_lock = 0; 1167 1168 ret = lg216x_read_reg(state, 0x0304, &val); 1169 if (lg_fail(ret)) 1170 goto fail; 1171 1172 *sync_lock = (val & 0x80) ? 0 : 1; 1173 1174 ret = lg216x_read_reg(state, 0x011b, &val); 1175 if (lg_fail(ret)) 1176 goto fail; 1177 1178 *acq_lock = (val & 0x40) ? 0 : 1; 1179 fail: 1180 return ret; 1181 } 1182 #endif 1183 1184 static int lg216x_read_lock_status(struct lg216x_state *state, 1185 int *acq_lock, int *sync_lock) 1186 { 1187 #ifdef USE_LG2161_LOCK_BITS 1188 int ret; 1189 switch (state->cfg->lg_chip) { 1190 case LG2160: 1191 ret = lg2160_read_lock_status(state, acq_lock, sync_lock); 1192 break; 1193 case LG2161: 1194 ret = lg2161_read_lock_status(state, acq_lock, sync_lock); 1195 break; 1196 default: 1197 ret = -EINVAL; 1198 break; 1199 } 1200 return ret; 1201 #else 1202 return lg2160_read_lock_status(state, acq_lock, sync_lock); 1203 #endif 1204 } 1205 1206 static int lg216x_read_status(struct dvb_frontend *fe, enum fe_status *status) 1207 { 1208 struct lg216x_state *state = fe->demodulator_priv; 1209 int ret, acq_lock, sync_lock; 1210 1211 *status = 0; 1212 1213 ret = lg216x_read_lock_status(state, &acq_lock, &sync_lock); 1214 if (lg_fail(ret)) 1215 goto fail; 1216 1217 lg_dbg("%s%s\n", 1218 acq_lock ? "SIGNALEXIST " : "", 1219 sync_lock ? "SYNCLOCK" : ""); 1220 1221 if (acq_lock) 1222 *status |= FE_HAS_SIGNAL; 1223 if (sync_lock) 1224 *status |= FE_HAS_SYNC; 1225 1226 if (*status) 1227 *status |= FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_LOCK; 1228 1229 fail: 1230 return ret; 1231 } 1232 1233 /* ------------------------------------------------------------------------ */ 1234 1235 static int lg2160_read_snr(struct dvb_frontend *fe, u16 *snr) 1236 { 1237 struct lg216x_state *state = fe->demodulator_priv; 1238 u8 snr1, snr2; 1239 int ret; 1240 1241 *snr = 0; 1242 1243 ret = lg216x_read_reg(state, 0x0202, &snr1); 1244 if (lg_fail(ret)) 1245 goto fail; 1246 1247 ret = lg216x_read_reg(state, 0x0203, &snr2); 1248 if (lg_fail(ret)) 1249 goto fail; 1250 1251 if ((snr1 == 0xba) || (snr2 == 0xdf)) 1252 *snr = 0; 1253 else 1254 #if 1 1255 *snr = ((snr1 >> 4) * 100) + ((snr1 & 0x0f) * 10) + (snr2 >> 4); 1256 #else /* BCD */ 1257 *snr = (snr2 | (snr1 << 8)); 1258 #endif 1259 fail: 1260 return ret; 1261 } 1262 1263 static int lg2161_read_snr(struct dvb_frontend *fe, u16 *snr) 1264 { 1265 struct lg216x_state *state = fe->demodulator_priv; 1266 u8 snr1, snr2; 1267 int ret; 1268 1269 *snr = 0; 1270 1271 ret = lg216x_read_reg(state, 0x0302, &snr1); 1272 if (lg_fail(ret)) 1273 goto fail; 1274 1275 ret = lg216x_read_reg(state, 0x0303, &snr2); 1276 if (lg_fail(ret)) 1277 goto fail; 1278 1279 if ((snr1 == 0xba) || (snr2 == 0xfd)) 1280 *snr = 0; 1281 else 1282 1283 *snr = ((snr1 >> 4) * 100) + ((snr1 & 0x0f) * 10) + (snr2 & 0x0f); 1284 fail: 1285 return ret; 1286 } 1287 1288 static int lg216x_read_signal_strength(struct dvb_frontend *fe, 1289 u16 *strength) 1290 { 1291 #if 0 1292 /* borrowed from lgdt330x.c 1293 * 1294 * Calculate strength from SNR up to 35dB 1295 * Even though the SNR can go higher than 35dB, 1296 * there is some comfort factor in having a range of 1297 * strong signals that can show at 100% 1298 */ 1299 struct lg216x_state *state = fe->demodulator_priv; 1300 u16 snr; 1301 int ret; 1302 #endif 1303 *strength = 0; 1304 #if 0 1305 ret = fe->ops.read_snr(fe, &snr); 1306 if (lg_fail(ret)) 1307 goto fail; 1308 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */ 1309 /* scale the range 0 - 35*2^24 into 0 - 65535 */ 1310 if (state->snr >= 8960 * 0x10000) 1311 *strength = 0xffff; 1312 else 1313 *strength = state->snr / 8960; 1314 fail: 1315 return ret; 1316 #else 1317 return 0; 1318 #endif 1319 } 1320 1321 /* ------------------------------------------------------------------------ */ 1322 1323 static int lg216x_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) 1324 { 1325 #if 0 1326 struct lg216x_state *state = fe->demodulator_priv; 1327 int ret; 1328 1329 ret = lg216x_read_rs_err_count(state, 1330 &fe->dtv_property_cache.atscmh_rs_err); 1331 if (lg_fail(ret)) 1332 goto fail; 1333 1334 *ucblocks = fe->dtv_property_cache.atscmh_rs_err; 1335 fail: 1336 #else 1337 *ucblocks = 0; 1338 #endif 1339 return 0; 1340 } 1341 1342 static int lg216x_get_tune_settings(struct dvb_frontend *fe, 1343 struct dvb_frontend_tune_settings 1344 *fe_tune_settings) 1345 { 1346 fe_tune_settings->min_delay_ms = 500; 1347 lg_dbg("\n"); 1348 return 0; 1349 } 1350 1351 static void lg216x_release(struct dvb_frontend *fe) 1352 { 1353 struct lg216x_state *state = fe->demodulator_priv; 1354 lg_dbg("\n"); 1355 kfree(state); 1356 } 1357 1358 static const struct dvb_frontend_ops lg2160_ops = { 1359 .delsys = { SYS_ATSCMH }, 1360 .info = { 1361 .name = "LG Electronics LG2160 ATSC/MH Frontend", 1362 .frequency_min = 54000000, 1363 .frequency_max = 858000000, 1364 .frequency_stepsize = 62500, 1365 }, 1366 .i2c_gate_ctrl = lg216x_i2c_gate_ctrl, 1367 #if 0 1368 .init = lg216x_init, 1369 .sleep = lg216x_sleep, 1370 #endif 1371 .get_property = lg216x_get_property, 1372 1373 .set_frontend = lg2160_set_frontend, 1374 .get_frontend = lg216x_get_frontend, 1375 .get_tune_settings = lg216x_get_tune_settings, 1376 .read_status = lg216x_read_status, 1377 #if 0 1378 .read_ber = lg216x_read_ber, 1379 #endif 1380 .read_signal_strength = lg216x_read_signal_strength, 1381 .read_snr = lg2160_read_snr, 1382 .read_ucblocks = lg216x_read_ucblocks, 1383 .release = lg216x_release, 1384 }; 1385 1386 static const struct dvb_frontend_ops lg2161_ops = { 1387 .delsys = { SYS_ATSCMH }, 1388 .info = { 1389 .name = "LG Electronics LG2161 ATSC/MH Frontend", 1390 .frequency_min = 54000000, 1391 .frequency_max = 858000000, 1392 .frequency_stepsize = 62500, 1393 }, 1394 .i2c_gate_ctrl = lg216x_i2c_gate_ctrl, 1395 #if 0 1396 .init = lg216x_init, 1397 .sleep = lg216x_sleep, 1398 #endif 1399 .get_property = lg216x_get_property, 1400 1401 .set_frontend = lg2160_set_frontend, 1402 .get_frontend = lg216x_get_frontend, 1403 .get_tune_settings = lg216x_get_tune_settings, 1404 .read_status = lg216x_read_status, 1405 #if 0 1406 .read_ber = lg216x_read_ber, 1407 #endif 1408 .read_signal_strength = lg216x_read_signal_strength, 1409 .read_snr = lg2161_read_snr, 1410 .read_ucblocks = lg216x_read_ucblocks, 1411 .release = lg216x_release, 1412 }; 1413 1414 struct dvb_frontend *lg2160_attach(const struct lg2160_config *config, 1415 struct i2c_adapter *i2c_adap) 1416 { 1417 struct lg216x_state *state = NULL; 1418 1419 lg_dbg("(%d-%04x)\n", 1420 i2c_adap ? i2c_adapter_id(i2c_adap) : 0, 1421 config ? config->i2c_addr : 0); 1422 1423 state = kzalloc(sizeof(struct lg216x_state), GFP_KERNEL); 1424 if (!state) 1425 return NULL; 1426 1427 state->cfg = config; 1428 state->i2c_adap = i2c_adap; 1429 state->fic_ver = 0xff; 1430 state->parade_id = 0xff; 1431 1432 switch (config->lg_chip) { 1433 default: 1434 lg_warn("invalid chip requested, defaulting to LG2160"); 1435 /* fall-thru */ 1436 case LG2160: 1437 memcpy(&state->frontend.ops, &lg2160_ops, 1438 sizeof(struct dvb_frontend_ops)); 1439 break; 1440 case LG2161: 1441 memcpy(&state->frontend.ops, &lg2161_ops, 1442 sizeof(struct dvb_frontend_ops)); 1443 break; 1444 } 1445 1446 state->frontend.demodulator_priv = state; 1447 state->current_frequency = -1; 1448 /* parade 1 by default */ 1449 state->frontend.dtv_property_cache.atscmh_parade_id = 1; 1450 1451 return &state->frontend; 1452 } 1453 EXPORT_SYMBOL(lg2160_attach); 1454 1455 MODULE_DESCRIPTION("LG Electronics LG216x ATSC/MH Demodulator Driver"); 1456 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>"); 1457 MODULE_LICENSE("GPL"); 1458 MODULE_VERSION("0.3"); 1459