Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 | // SPDX-License-Identifier: GPL-2.0-only /* * tda18271c2dd: Driver for the TDA18271C2 tuner * * Copyright (C) 2010 Digital Devices GmbH */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/delay.h> #include <linux/firmware.h> #include <linux/i2c.h> #include <asm/div64.h> #include <media/dvb_frontend.h> #include "tda18271c2dd.h" /* Max transfer size done by I2C transfer functions */ #define MAX_XFER_SIZE 64 struct SStandardParam { s32 m_IFFrequency; u32 m_BandWidth; u8 m_EP3_4_0; u8 m_EB22; }; struct SMap { u32 m_Frequency; u8 m_Param; }; struct SMapI { u32 m_Frequency; s32 m_Param; }; struct SMap2 { u32 m_Frequency; u8 m_Param1; u8 m_Param2; }; struct SRFBandMap { u32 m_RF_max; u32 m_RF1_Default; u32 m_RF2_Default; u32 m_RF3_Default; }; enum ERegister { ID = 0, TM, PL, EP1, EP2, EP3, EP4, EP5, CPD, CD1, CD2, CD3, MPD, MD1, MD2, MD3, EB1, EB2, EB3, EB4, EB5, EB6, EB7, EB8, EB9, EB10, EB11, EB12, EB13, EB14, EB15, EB16, EB17, EB18, EB19, EB20, EB21, EB22, EB23, NUM_REGS }; struct tda_state { struct i2c_adapter *i2c; u8 adr; u32 m_Frequency; u32 IF; u8 m_IFLevelAnalog; u8 m_IFLevelDigital; u8 m_IFLevelDVBC; u8 m_IFLevelDVBT; u8 m_EP4; u8 m_EP3_Standby; bool m_bMaster; s32 m_SettlingTime; u8 m_Regs[NUM_REGS]; /* Tracking filter settings for band 0..6 */ u32 m_RF1[7]; s32 m_RF_A1[7]; s32 m_RF_B1[7]; u32 m_RF2[7]; s32 m_RF_A2[7]; s32 m_RF_B2[7]; u32 m_RF3[7]; u8 m_TMValue_RFCal; /* Calibration temperature */ bool m_bFMInput; /* true to use Pin 8 for FM Radio */ }; static int PowerScan(struct tda_state *state, u8 RFBand, u32 RF_in, u32 *pRF_Out, bool *pbcal); static int i2c_readn(struct i2c_adapter *adapter, u8 adr, u8 *data, int len) { struct i2c_msg msgs[1] = {{.addr = adr, .flags = I2C_M_RD, .buf = data, .len = len} }; return (i2c_transfer(adapter, msgs, 1) == 1) ? 0 : -1; } static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 *data, int len) { struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = data, .len = len}; if (i2c_transfer(adap, &msg, 1) != 1) { printk(KERN_ERR "tda18271c2dd: i2c write error at addr %i\n", adr); return -1; } return 0; } static int WriteRegs(struct tda_state *state, u8 SubAddr, u8 *Regs, u16 nRegs) { u8 data[MAX_XFER_SIZE]; if (1 + nRegs > sizeof(data)) { printk(KERN_WARNING "%s: i2c wr: len=%d is too big!\n", KBUILD_MODNAME, nRegs); return -EINVAL; } data[0] = SubAddr; memcpy(data + 1, Regs, nRegs); return i2c_write(state->i2c, state->adr, data, nRegs + 1); } static int WriteReg(struct tda_state *state, u8 SubAddr, u8 Reg) { u8 msg[2] = {SubAddr, Reg}; return i2c_write(state->i2c, state->adr, msg, 2); } static int Read(struct tda_state *state, u8 * Regs) { return i2c_readn(state->i2c, state->adr, Regs, 16); } static int ReadExtented(struct tda_state *state, u8 * Regs) { return i2c_readn(state->i2c, state->adr, Regs, NUM_REGS); } static int UpdateRegs(struct tda_state *state, u8 RegFrom, u8 RegTo) { return WriteRegs(state, RegFrom, &state->m_Regs[RegFrom], RegTo-RegFrom+1); } static int UpdateReg(struct tda_state *state, u8 Reg) { return WriteReg(state, Reg, state->m_Regs[Reg]); } #include "tda18271c2dd_maps.h" static void reset(struct tda_state *state) { u32 ulIFLevelAnalog = 0; u32 ulIFLevelDigital = 2; u32 ulIFLevelDVBC = 7; u32 ulIFLevelDVBT = 6; u32 ulXTOut = 0; u32 ulStandbyMode = 0x06; /* Send in stdb, but leave osc on */ u32 ulSlave = 0; u32 ulFMInput = 0; u32 ulSettlingTime = 100; state->m_Frequency = 0; state->m_SettlingTime = 100; state->m_IFLevelAnalog = (ulIFLevelAnalog & 0x07) << 2; state->m_IFLevelDigital = (ulIFLevelDigital & 0x07) << 2; state->m_IFLevelDVBC = (ulIFLevelDVBC & 0x07) << 2; state->m_IFLevelDVBT = (ulIFLevelDVBT & 0x07) << 2; state->m_EP4 = 0x20; if (ulXTOut != 0) state->m_EP4 |= 0x40; state->m_EP3_Standby = ((ulStandbyMode & 0x07) << 5) | 0x0F; state->m_bMaster = (ulSlave == 0); state->m_SettlingTime = ulSettlingTime; state->m_bFMInput = (ulFMInput == 2); } static bool SearchMap1(const struct SMap map[], u32 frequency, u8 *param) { int i = 0; while ((map[i].m_Frequency != 0) && (frequency > map[i].m_Frequency)) i += 1; if (map[i].m_Frequency == 0) return false; *param = map[i].m_Param; return true; } static bool SearchMap2(const struct SMapI map[], u32 frequency, s32 *param) { int i = 0; while ((map[i].m_Frequency != 0) && (frequency > map[i].m_Frequency)) i += 1; if (map[i].m_Frequency == 0) return false; *param = map[i].m_Param; return true; } static bool SearchMap3(const struct SMap2 map[], u32 frequency, u8 *param1, u8 *param2) { int i = 0; while ((map[i].m_Frequency != 0) && (frequency > map[i].m_Frequency)) i += 1; if (map[i].m_Frequency == 0) return false; *param1 = map[i].m_Param1; *param2 = map[i].m_Param2; return true; } static bool SearchMap4(const struct SRFBandMap map[], u32 frequency, u8 *rfband) { int i = 0; while (i < 7 && (frequency > map[i].m_RF_max)) i += 1; if (i == 7) return false; *rfband = i; return true; } static int ThermometerRead(struct tda_state *state, u8 *pTM_Value) { int status = 0; do { u8 Regs[16]; state->m_Regs[TM] |= 0x10; status = UpdateReg(state, TM); if (status < 0) break; status = Read(state, Regs); if (status < 0) break; if (((Regs[TM] & 0x0F) == 0 && (Regs[TM] & 0x20) == 0x20) || ((Regs[TM] & 0x0F) == 8 && (Regs[TM] & 0x20) == 0x00)) { state->m_Regs[TM] ^= 0x20; status = UpdateReg(state, TM); if (status < 0) break; msleep(10); status = Read(state, Regs); if (status < 0) break; } *pTM_Value = (Regs[TM] & 0x20) ? m_Thermometer_Map_2[Regs[TM] & 0x0F] : m_Thermometer_Map_1[Regs[TM] & 0x0F] ; state->m_Regs[TM] &= ~0x10; /* Thermometer off */ status = UpdateReg(state, TM); if (status < 0) break; state->m_Regs[EP4] &= ~0x03; /* CAL_mode = 0 ????????? */ status = UpdateReg(state, EP4); if (status < 0) break; } while (0); return status; } static int StandBy(struct tda_state *state) { int status = 0; do { state->m_Regs[EB12] &= ~0x20; /* PD_AGC1_Det = 0 */ status = UpdateReg(state, EB12); if (status < 0) break; state->m_Regs[EB18] &= ~0x83; /* AGC1_loop_off = 0, AGC1_Gain = 6 dB */ status = UpdateReg(state, EB18); if (status < 0) break; state->m_Regs[EB21] |= 0x03; /* AGC2_Gain = -6 dB */ state->m_Regs[EP3] = state->m_EP3_Standby; status = UpdateReg(state, EP3); if (status < 0) break; state->m_Regs[EB23] &= ~0x06; /* ForceLP_Fc2_En = 0, LP_Fc[2] = 0 */ status = UpdateRegs(state, EB21, EB23); if (status < 0) break; } while (0); return status; } static int CalcMainPLL(struct tda_state *state, u32 freq) { u8 PostDiv; u8 Div; u64 OscFreq; u32 MainDiv; if (!SearchMap3(m_Main_PLL_Map, freq, &PostDiv, &Div)) return -EINVAL; OscFreq = (u64) freq * (u64) Div; OscFreq *= (u64) 16384; do_div(OscFreq, (u64)16000000); MainDiv = OscFreq; state->m_Regs[MPD] = PostDiv & 0x77; state->m_Regs[MD1] = ((MainDiv >> 16) & 0x7F); state->m_Regs[MD2] = ((MainDiv >> 8) & 0xFF); state->m_Regs[MD3] = (MainDiv & 0xFF); return UpdateRegs(state, MPD, MD3); } static int CalcCalPLL(struct tda_state *state, u32 freq) { u8 PostDiv; u8 Div; u64 OscFreq; u32 CalDiv; if (!SearchMap3(m_Cal_PLL_Map, freq, &PostDiv, &Div)) return -EINVAL; OscFreq = (u64)freq * (u64)Div; /* CalDiv = u32( OscFreq * 16384 / 16000000 ); */ OscFreq *= (u64)16384; do_div(OscFreq, (u64)16000000); CalDiv = OscFreq; state->m_Regs[CPD] = PostDiv; state->m_Regs[CD1] = ((CalDiv >> 16) & 0xFF); state->m_Regs[CD2] = ((CalDiv >> 8) & 0xFF); state->m_Regs[CD3] = (CalDiv & 0xFF); return UpdateRegs(state, CPD, CD3); } static int CalibrateRF(struct tda_state *state, u8 RFBand, u32 freq, s32 *pCprog) { int status = 0; u8 Regs[NUM_REGS]; do { u8 BP_Filter = 0; u8 GainTaper = 0; u8 RFC_K = 0; u8 RFC_M = 0; state->m_Regs[EP4] &= ~0x03; /* CAL_mode = 0 */ status = UpdateReg(state, EP4); if (status < 0) break; state->m_Regs[EB18] |= 0x03; /* AGC1_Gain = 3 */ status = UpdateReg(state, EB18); if (status < 0) break; /* Switching off LT (as datasheet says) causes calibration on C1 to fail */ /* (Readout of Cprog is always 255) */ if (state->m_Regs[ID] != 0x83) /* C1: ID == 83, C2: ID == 84 */ state->m_Regs[EP3] |= 0x40; /* SM_LT = 1 */ if (!(SearchMap1(m_BP_Filter_Map, freq, &BP_Filter) && SearchMap1(m_GainTaper_Map, freq, &GainTaper) && SearchMap3(m_KM_Map, freq, &RFC_K, &RFC_M))) return -EINVAL; state->m_Regs[EP1] = (state->m_Regs[EP1] & ~0x07) | BP_Filter; state->m_Regs[EP2] = (RFBand << 5) | GainTaper; state->m_Regs[EB13] = (state->m_Regs[EB13] & ~0x7C) | (RFC_K << 4) | (RFC_M << 2); status = UpdateRegs(state, EP1, EP3); if (status < 0) break; status = UpdateReg(state, EB13); if (status < 0) break; state->m_Regs[EB4] |= 0x20; /* LO_ForceSrce = 1 */ status = UpdateReg(state, EB4); if (status < 0) break; state->m_Regs[EB7] |= 0x20; /* CAL_ForceSrce = 1 */ status = UpdateReg(state, EB7); if (status < 0) break; state->m_Regs[EB14] = 0; /* RFC_Cprog = 0 */ status = UpdateReg(state, EB14); if (status < 0) break; state->m_Regs[EB20] &= ~0x20; /* ForceLock = 0; */ status = UpdateReg(state, EB20); if (status < 0) break; state->m_Regs[EP4] |= 0x03; /* CAL_Mode = 3 */ status = UpdateRegs(state, EP4, EP5); if (status < 0) break; status = CalcCalPLL(state, freq); if (status < 0) break; status = CalcMainPLL(state, freq + 1000000); if (status < 0) break; msleep(5); status = UpdateReg(state, EP2); if (status < 0) break; status = UpdateReg(state, EP1); if (status < 0) break; status = UpdateReg(state, EP2); if (status < 0) break; status = UpdateReg(state, EP1); if (status < 0) break; state->m_Regs[EB4] &= ~0x20; /* LO_ForceSrce = 0 */ status = UpdateReg(state, EB4); if (status < 0) break; state->m_Regs[EB7] &= ~0x20; /* CAL_ForceSrce = 0 */ status = UpdateReg(state, EB7); if (status < 0) break; msleep(10); state->m_Regs[EB20] |= 0x20; /* ForceLock = 1; */ status = UpdateReg(state, EB20); if (status < 0) break; msleep(60); state->m_Regs[EP4] &= ~0x03; /* CAL_Mode = 0 */ state->m_Regs[EP3] &= ~0x40; /* SM_LT = 0 */ state->m_Regs[EB18] &= ~0x03; /* AGC1_Gain = 0 */ status = UpdateReg(state, EB18); if (status < 0) break; status = UpdateRegs(state, EP3, EP4); if (status < 0) break; status = UpdateReg(state, EP1); if (status < 0) break; status = ReadExtented(state, Regs); if (status < 0) break; *pCprog = Regs[EB14]; } while (0); return status; } static int RFTrackingFiltersInit(struct tda_state *state, u8 RFBand) { int status = 0; u32 RF1 = m_RF_Band_Map[RFBand].m_RF1_Default; u32 RF2 = m_RF_Band_Map[RFBand].m_RF2_Default; u32 RF3 = m_RF_Band_Map[RFBand].m_RF3_Default; bool bcal = false; s32 Cprog_cal1 = 0; s32 Cprog_table1 = 0; s32 Cprog_cal2 = 0; s32 Cprog_table2 = 0; s32 Cprog_cal3 = 0; s32 Cprog_table3 = 0; state->m_RF_A1[RFBand] = 0; state->m_RF_B1[RFBand] = 0; state->m_RF_A2[RFBand] = 0; state->m_RF_B2[RFBand] = 0; do { status = PowerScan(state, RFBand, RF1, &RF1, &bcal); if (status < 0) break; if (bcal) { status = CalibrateRF(state, RFBand, RF1, &Cprog_cal1); if (status < 0) break; } SearchMap2(m_RF_Cal_Map, RF1, &Cprog_table1); if (!bcal) Cprog_cal1 = Cprog_table1; state->m_RF_B1[RFBand] = Cprog_cal1 - Cprog_table1; /* state->m_RF_A1[RF_Band] = ???? */ if (RF2 == 0) break; status = PowerScan(state, RFBand, RF2, &RF2, &bcal); if (status < 0) break; if (bcal) { status = CalibrateRF(state, RFBand, RF2, &Cprog_cal2); if (status < 0) break; } SearchMap2(m_RF_Cal_Map, RF2, &Cprog_table2); if (!bcal) Cprog_cal2 = Cprog_table2; state->m_RF_A1[RFBand] = (Cprog_cal2 - Cprog_table2 - Cprog_cal1 + Cprog_table1) / ((s32)(RF2) - (s32)(RF1)); if (RF3 == 0) break; status = PowerScan(state, RFBand, RF3, &RF3, &bcal); if (status < 0) break; if (bcal) { status = CalibrateRF(state, RFBand, RF3, &Cprog_cal3); if (status < 0) break; } SearchMap2(m_RF_Cal_Map, RF3, &Cprog_table3); if (!bcal) Cprog_cal3 = Cprog_table3; state->m_RF_A2[RFBand] = (Cprog_cal3 - Cprog_table3 - Cprog_cal2 + Cprog_table2) / ((s32)(RF3) - (s32)(RF2)); state->m_RF_B2[RFBand] = Cprog_cal2 - Cprog_table2; } while (0); state->m_RF1[RFBand] = RF1; state->m_RF2[RFBand] = RF2; state->m_RF3[RFBand] = RF3; #if 0 printk(KERN_ERR "tda18271c2dd: %s %d RF1 = %d A1 = %d B1 = %d RF2 = %d A2 = %d B2 = %d RF3 = %d\n", __func__, RFBand, RF1, state->m_RF_A1[RFBand], state->m_RF_B1[RFBand], RF2, state->m_RF_A2[RFBand], state->m_RF_B2[RFBand], RF3); #endif return status; } static int PowerScan(struct tda_state *state, u8 RFBand, u32 RF_in, u32 *pRF_Out, bool *pbcal) { int status = 0; do { u8 Gain_Taper = 0; s32 RFC_Cprog = 0; u8 CID_Target = 0; u8 CountLimit = 0; u32 freq_MainPLL; u8 Regs[NUM_REGS]; u8 CID_Gain; s32 Count = 0; int sign = 1; bool wait = false; if (!(SearchMap2(m_RF_Cal_Map, RF_in, &RFC_Cprog) && SearchMap1(m_GainTaper_Map, RF_in, &Gain_Taper) && SearchMap3(m_CID_Target_Map, RF_in, &CID_Target, &CountLimit))) { printk(KERN_ERR "tda18271c2dd: %s Search map failed\n", __func__); return -EINVAL; } state->m_Regs[EP2] = (RFBand << 5) | Gain_Taper; state->m_Regs[EB14] = (RFC_Cprog); status = UpdateReg(state, EP2); if (status < 0) break; status = UpdateReg(state, EB14); if (status < 0) break; freq_MainPLL = RF_in + 1000000; status = CalcMainPLL(state, freq_MainPLL); if (status < 0) break; msleep(5); state->m_Regs[EP4] = (state->m_Regs[EP4] & ~0x03) | 1; /* CAL_mode = 1 */ status = UpdateReg(state, EP4); if (status < 0) break; status = UpdateReg(state, EP2); /* Launch power measurement */ if (status < 0) break; status = ReadExtented(state, Regs); if (status < 0) break; CID_Gain = Regs[EB10] & 0x3F; state->m_Regs[ID] = Regs[ID]; /* Chip version, (needed for C1 workaround in CalibrateRF) */ *pRF_Out = RF_in; while (CID_Gain < CID_Target) { freq_MainPLL = RF_in + sign * Count + 1000000; status = CalcMainPLL(state, freq_MainPLL); if (status < 0) break; msleep(wait ? 5 : 1); wait = false; status = UpdateReg(state, EP2); /* Launch power measurement */ if (status < 0) break; status = ReadExtented(state, Regs); if (status < 0) break; CID_Gain = Regs[EB10] & 0x3F; Count += 200000; if (Count < CountLimit * 100000) continue; if (sign < 0) break; sign = -sign; Count = 200000; wait = true; } if (status < 0) break; if (CID_Gain >= CID_Target) { *pbcal = true; *pRF_Out = freq_MainPLL - 1000000; } else *pbcal = false; } while (0); return status; } static int PowerScanInit(struct tda_state *state) { int status = 0; do { state->m_Regs[EP3] = (state->m_Regs[EP3] & ~0x1F) | 0x12; state->m_Regs[EP4] = (state->m_Regs[EP4] & ~0x1F); /* If level = 0, Cal mode = 0 */ status = UpdateRegs(state, EP3, EP4); if (status < 0) break; state->m_Regs[EB18] = (state->m_Regs[EB18] & ~0x03); /* AGC 1 Gain = 0 */ status = UpdateReg(state, EB18); if (status < 0) break; state->m_Regs[EB21] = (state->m_Regs[EB21] & ~0x03); /* AGC 2 Gain = 0 (Datasheet = 3) */ state->m_Regs[EB23] = (state->m_Regs[EB23] | 0x06); /* ForceLP_Fc2_En = 1, LPFc[2] = 1 */ status = UpdateRegs(state, EB21, EB23); if (status < 0) break; } while (0); return status; } static int CalcRFFilterCurve(struct tda_state *state) { int status = 0; do { msleep(200); /* Temperature stabilisation */ status = PowerScanInit(state); if (status < 0) break; status = RFTrackingFiltersInit(state, 0); if (status < 0) break; status = RFTrackingFiltersInit(state, 1); if (status < 0) break; status = RFTrackingFiltersInit(state, 2); if (status < 0) break; status = RFTrackingFiltersInit(state, 3); if (status < 0) break; status = RFTrackingFiltersInit(state, 4); if (status < 0) break; status = RFTrackingFiltersInit(state, 5); if (status < 0) break; status = RFTrackingFiltersInit(state, 6); if (status < 0) break; status = ThermometerRead(state, &state->m_TMValue_RFCal); /* also switches off Cal mode !!! */ if (status < 0) break; } while (0); return status; } static int FixedContentsI2CUpdate(struct tda_state *state) { static u8 InitRegs[] = { 0x08, 0x80, 0xC6, 0xDF, 0x16, 0x60, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x01, 0x84, 0x41, 0x01, 0x84, 0x40, 0x07, 0x00, 0x00, 0x96, 0x3F, 0xC1, 0x00, 0x8F, 0x00, 0x00, 0x8C, 0x00, 0x20, 0xB3, 0x48, 0xB0, }; int status = 0; memcpy(&state->m_Regs[TM], InitRegs, EB23 - TM + 1); do { status = UpdateRegs(state, TM, EB23); if (status < 0) break; /* AGC1 gain setup */ state->m_Regs[EB17] = 0x00; status = UpdateReg(state, EB17); if (status < 0) break; state->m_Regs[EB17] = 0x03; status = UpdateReg(state, EB17); if (status < 0) break; state->m_Regs[EB17] = 0x43; status = UpdateReg(state, EB17); if (status < 0) break; state->m_Regs[EB17] = 0x4C; status = UpdateReg(state, EB17); if (status < 0) break; /* IRC Cal Low band */ state->m_Regs[EP3] = 0x1F; state->m_Regs[EP4] = 0x66; state->m_Regs[EP5] = 0x81; state->m_Regs[CPD] = 0xCC; state->m_Regs[CD1] = 0x6C; state->m_Regs[CD2] = 0x00; state->m_Regs[CD3] = 0x00; state->m_Regs[MPD] = 0xC5; state->m_Regs[MD1] = 0x77; state->m_Regs[MD2] = 0x08; state->m_Regs[MD3] = 0x00; status = UpdateRegs(state, EP2, MD3); /* diff between sw and datasheet (ep3-md3) */ if (status < 0) break; #if 0 state->m_Regs[EB4] = 0x61; /* missing in sw */ status = UpdateReg(state, EB4); if (status < 0) break; msleep(1); state->m_Regs[EB4] = 0x41; status = UpdateReg(state, EB4); if (status < 0) break; #endif msleep(5); status = UpdateReg(state, EP1); if (status < 0) break; msleep(5); state->m_Regs[EP5] = 0x85; state->m_Regs[CPD] = 0xCB; state->m_Regs[CD1] = 0x66; state->m_Regs[CD2] = 0x70; status = UpdateRegs(state, EP3, CD3); if (status < 0) break; msleep(5); status = UpdateReg(state, EP2); if (status < 0) break; msleep(30); /* IRC Cal mid band */ state->m_Regs[EP5] = 0x82; state->m_Regs[CPD] = 0xA8; state->m_Regs[CD2] = 0x00; state->m_Regs[MPD] = 0xA1; /* Datasheet = 0xA9 */ state->m_Regs[MD1] = 0x73; state->m_Regs[MD2] = 0x1A; status = UpdateRegs(state, EP3, MD3); if (status < 0) break; msleep(5); status = UpdateReg(state, EP1); if (status < 0) break; msleep(5); state->m_Regs[EP5] = 0x86; state->m_Regs[CPD] = 0xA8; state->m_Regs[CD1] = 0x66; state->m_Regs[CD2] = 0xA0; status = UpdateRegs(state, EP3, CD3); if (status < 0) break; msleep(5); status = UpdateReg(state, EP2); if (status < 0) break; msleep(30); /* IRC Cal high band */ state->m_Regs[EP5] = 0x83; state->m_Regs[CPD] = 0x98; state->m_Regs[CD1] = 0x65; state->m_Regs[CD2] = 0x00; state->m_Regs[MPD] = 0x91; /* Datasheet = 0x91 */ state->m_Regs[MD1] = 0x71; state->m_Regs[MD2] = 0xCD; status = UpdateRegs(state, EP3, MD3); if (status < 0) break; msleep(5); status = UpdateReg(state, EP1); if (status < 0) break; msleep(5); state->m_Regs[EP5] = 0x87; state->m_Regs[CD1] = 0x65; state->m_Regs[CD2] = 0x50; status = UpdateRegs(state, EP3, CD3); if (status < 0) break; msleep(5); status = UpdateReg(state, EP2); if (status < 0) break; msleep(30); /* Back to normal */ state->m_Regs[EP4] = 0x64; status = UpdateReg(state, EP4); if (status < 0) break; status = UpdateReg(state, EP1); if (status < 0) break; } while (0); return status; } static int InitCal(struct tda_state *state) { int status = 0; do { status = FixedContentsI2CUpdate(state); if (status < 0) break; status = CalcRFFilterCurve(state); if (status < 0) break; status = StandBy(state); if (status < 0) break; /* m_bInitDone = true; */ } while (0); return status; }; static int RFTrackingFiltersCorrection(struct tda_state *state, u32 Frequency) { int status = 0; s32 Cprog_table; u8 RFBand; u8 dCoverdT; if (!SearchMap2(m_RF_Cal_Map, Frequency, &Cprog_table) || !SearchMap4(m_RF_Band_Map, Frequency, &RFBand) || !SearchMap1(m_RF_Cal_DC_Over_DT_Map, Frequency, &dCoverdT)) return -EINVAL; do { u8 TMValue_Current; u32 RF1 = state->m_RF1[RFBand]; u32 RF2 = state->m_RF1[RFBand]; u32 RF3 = state->m_RF1[RFBand]; s32 RF_A1 = state->m_RF_A1[RFBand]; s32 RF_B1 = state->m_RF_B1[RFBand]; s32 RF_A2 = state->m_RF_A2[RFBand]; s32 RF_B2 = state->m_RF_B2[RFBand]; s32 Capprox = 0; int TComp; state->m_Regs[EP3] &= ~0xE0; /* Power up */ status = UpdateReg(state, EP3); if (status < 0) break; status = ThermometerRead(state, &TMValue_Current); if (status < 0) break; if (RF3 == 0 || Frequency < RF2) Capprox = RF_A1 * ((s32)(Frequency) - (s32)(RF1)) + RF_B1 + Cprog_table; else Capprox = RF_A2 * ((s32)(Frequency) - (s32)(RF2)) + RF_B2 + Cprog_table; TComp = (int)(dCoverdT) * ((int)(TMValue_Current) - (int)(state->m_TMValue_RFCal))/1000; Capprox += TComp; if (Capprox < 0) Capprox = 0; else if (Capprox > 255) Capprox = 255; /* TODO Temperature compensation. There is defenitely a scale factor */ /* missing in the datasheet, so leave it out for now. */ state->m_Regs[EB14] = Capprox; status = UpdateReg(state, EB14); if (status < 0) break; } while (0); return status; } static int ChannelConfiguration(struct tda_state *state, u32 Frequency, int Standard) { s32 IntermediateFrequency = m_StandardTable[Standard].m_IFFrequency; int status = 0; u8 BP_Filter = 0; u8 RF_Band = 0; u8 GainTaper = 0; u8 IR_Meas = 0; state->IF = IntermediateFrequency; /* printk("tda18271c2dd: %s Freq = %d Standard = %d IF = %d\n", __func__, Frequency, Standard, IntermediateFrequency); */ /* get values from tables */ if (!(SearchMap1(m_BP_Filter_Map, Frequency, &BP_Filter) && SearchMap1(m_GainTaper_Map, Frequency, &GainTaper) && SearchMap1(m_IR_Meas_Map, Frequency, &IR_Meas) && SearchMap4(m_RF_Band_Map, Frequency, &RF_Band))) { printk(KERN_ERR "tda18271c2dd: %s SearchMap failed\n", __func__); return -EINVAL; } do { state->m_Regs[EP3] = (state->m_Regs[EP3] & ~0x1F) | m_StandardTable[Standard].m_EP3_4_0; state->m_Regs[EP3] &= ~0x04; /* switch RFAGC to high speed mode */ /* m_EP4 default for XToutOn, CAL_Mode (0) */ state->m_Regs[EP4] = state->m_EP4 | ((Standard > HF_AnalogMax) ? state->m_IFLevelDigital : state->m_IFLevelAnalog); /* state->m_Regs[EP4] = state->m_EP4 | state->m_IFLevelDigital; */ if (Standard <= HF_AnalogMax) state->m_Regs[EP4] = state->m_EP4 | state->m_IFLevelAnalog; else if (Standard <= HF_ATSC) state->m_Regs[EP4] = state->m_EP4 | state->m_IFLevelDVBT; else if (Standard <= HF_DVBC) state->m_Regs[EP4] = state->m_EP4 | state->m_IFLevelDVBC; else state->m_Regs[EP4] = state->m_EP4 | state->m_IFLevelDigital; if ((Standard == HF_FM_Radio) && state->m_bFMInput) state->m_Regs[EP4] |= 0x80; state->m_Regs[MPD] &= ~0x80; if (Standard > HF_AnalogMax) state->m_Regs[MPD] |= 0x80; /* Add IF_notch for digital */ state->m_Regs[EB22] = m_StandardTable[Standard].m_EB22; /* Note: This is missing from flowchart in TDA18271 specification ( 1.5 MHz cutoff for FM ) */ if (Standard == HF_FM_Radio) state->m_Regs[EB23] |= 0x06; /* ForceLP_Fc2_En = 1, LPFc[2] = 1 */ else state->m_Regs[EB23] &= ~0x06; /* ForceLP_Fc2_En = 0, LPFc[2] = 0 */ status = UpdateRegs(state, EB22, EB23); if (status < 0) break; state->m_Regs[EP1] = (state->m_Regs[EP1] & ~0x07) | 0x40 | BP_Filter; /* Dis_Power_level = 1, Filter */ state->m_Regs[EP5] = (state->m_Regs[EP5] & ~0x07) | IR_Meas; state->m_Regs[EP2] = (RF_Band << 5) | GainTaper; state->m_Regs[EB1] = (state->m_Regs[EB1] & ~0x07) | (state->m_bMaster ? 0x04 : 0x00); /* CALVCO_FortLOn = MS */ /* AGC1_always_master = 0 */ /* AGC_firstn = 0 */ status = UpdateReg(state, EB1); if (status < 0) break; if (state->m_bMaster) { status = CalcMainPLL(state, Frequency + IntermediateFrequency); if (status < 0) break; status = UpdateRegs(state, TM, EP5); if (status < 0) break; state->m_Regs[EB4] |= 0x20; /* LO_forceSrce = 1 */ status = UpdateReg(state, EB4); if (status < 0) break; msleep(1); state->m_Regs[EB4] &= ~0x20; /* LO_forceSrce = 0 */ status = UpdateReg(state, EB4); if (status < 0) break; } else { u8 PostDiv = 0; u8 Div; status = CalcCalPLL(state, Frequency + IntermediateFrequency); if (status < 0) break; SearchMap3(m_Cal_PLL_Map, Frequency + IntermediateFrequency, &PostDiv, &Div); state->m_Regs[MPD] = (state->m_Regs[MPD] & ~0x7F) | (PostDiv & 0x77); status = UpdateReg(state, MPD); if (status < 0) break; status = UpdateRegs(state, TM, EP5); if (status < 0) break; state->m_Regs[EB7] |= 0x20; /* CAL_forceSrce = 1 */ status = UpdateReg(state, EB7); if (status < 0) break; msleep(1); state->m_Regs[EB7] &= ~0x20; /* CAL_forceSrce = 0 */ status = UpdateReg(state, EB7); if (status < 0) break; } msleep(20); if (Standard != HF_FM_Radio) state->m_Regs[EP3] |= 0x04; /* RFAGC to normal mode */ status = UpdateReg(state, EP3); if (status < 0) break; } while (0); return status; } static int sleep(struct dvb_frontend *fe) { struct tda_state *state = fe->tuner_priv; StandBy(state); return 0; } static int init(struct dvb_frontend *fe) { return 0; } static void release(struct dvb_frontend *fe) { kfree(fe->tuner_priv); fe->tuner_priv = NULL; } static int set_params(struct dvb_frontend *fe) { struct tda_state *state = fe->tuner_priv; int status = 0; int Standard; u32 bw = fe->dtv_property_cache.bandwidth_hz; u32 delsys = fe->dtv_property_cache.delivery_system; state->m_Frequency = fe->dtv_property_cache.frequency; switch (delsys) { case SYS_DVBT: case SYS_DVBT2: switch (bw) { case 6000000: Standard = HF_DVBT_6MHZ; break; case 7000000: Standard = HF_DVBT_7MHZ; break; case 8000000: Standard = HF_DVBT_8MHZ; break; default: return -EINVAL; } break; case SYS_DVBC_ANNEX_A: case SYS_DVBC_ANNEX_C: if (bw <= 6000000) Standard = HF_DVBC_6MHZ; else if (bw <= 7000000) Standard = HF_DVBC_7MHZ; else Standard = HF_DVBC_8MHZ; break; default: return -EINVAL; } do { status = RFTrackingFiltersCorrection(state, state->m_Frequency); if (status < 0) break; status = ChannelConfiguration(state, state->m_Frequency, Standard); if (status < 0) break; msleep(state->m_SettlingTime); /* Allow AGC's to settle down */ } while (0); return status; } #if 0 static int GetSignalStrength(s32 *pSignalStrength, u32 RFAgc, u32 IFAgc) { if (IFAgc < 500) { /* Scale this from 0 to 50000 */ *pSignalStrength = IFAgc * 100; } else { /* Scale range 500-1500 to 50000-80000 */ *pSignalStrength = 50000 + (IFAgc - 500) * 30; } return 0; } #endif static int get_if_frequency(struct dvb_frontend *fe, u32 *frequency) { struct tda_state *state = fe->tuner_priv; *frequency = state->IF; return 0; } static int get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) { /* struct tda_state *state = fe->tuner_priv; */ /* *bandwidth = priv->bandwidth; */ return 0; } static const struct dvb_tuner_ops tuner_ops = { .info = { .name = "NXP TDA18271C2D", .frequency_min_hz = 47125 * kHz, .frequency_max_hz = 865 * MHz, .frequency_step_hz = 62500 }, .init = init, .sleep = sleep, .set_params = set_params, .release = release, .get_if_frequency = get_if_frequency, .get_bandwidth = get_bandwidth, }; struct dvb_frontend *tda18271c2dd_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, u8 adr) { struct tda_state *state; state = kzalloc(sizeof(struct tda_state), GFP_KERNEL); if (!state) return NULL; fe->tuner_priv = state; state->adr = adr; state->i2c = i2c; memcpy(&fe->ops.tuner_ops, &tuner_ops, sizeof(struct dvb_tuner_ops)); reset(state); InitCal(state); return fe; } EXPORT_SYMBOL_GPL(tda18271c2dd_attach); MODULE_DESCRIPTION("TDA18271C2 driver"); MODULE_AUTHOR("DD"); MODULE_LICENSE("GPL"); |