25 #include <linux/kernel.h>
26 #include <linux/module.h>
31 #include <linux/i2c.h>
32 #include <asm/div64.h>
73 EB1,
EB2,
EB3,
EB4,
EB5,
EB6,
EB7,
EB8,
EB9,
EB10,
74 EB11,
EB12,
EB13,
EB14,
EB15,
EB16,
EB17,
EB18,
EB19,
EB20,
116 u8 RFBand,
u32 RF_in,
117 u32 *pRF_Out,
bool *pbcal);
122 .buf =
data, .len = len} };
128 struct i2c_msg msg = {.addr = adr, .flags = 0,
129 .buf =
data, .len = len};
132 printk(
KERN_ERR "tda18271c2dd: i2c write error at addr %i\n", adr);
139 u8 SubAddr,
u8 *Regs,
u16 nRegs)
144 memcpy(data + 1, Regs, nRegs);
150 u8 msg[2] = {SubAddr, Reg};
157 return i2c_readn(state->
i2c, state->
adr, Regs, 16);
167 return WriteRegs(state, RegFrom,
168 &state->
m_Regs[RegFrom], RegTo-RegFrom+1);
172 return WriteReg(state, Reg, state->
m_Regs[Reg]);
179 u32 ulIFLevelAnalog = 0;
180 u32 ulIFLevelDigital = 2;
181 u32 ulIFLevelDVBC = 7;
182 u32 ulIFLevelDVBT = 6;
184 u32 ulStandbyMode = 0x06;
187 u32 ulSettlingTime = 100;
198 state->
m_EP4 |= 0x40;
208 static bool SearchMap1(
struct SMap Map[],
209 u32 Frequency,
u8 *pParam)
213 while ((Map[i].m_Frequency != 0) && (Frequency > Map[i].m_Frequency))
215 if (Map[i].m_Frequency == 0)
221 static bool SearchMap2(
struct SMapI Map[],
222 u32 Frequency,
s32 *pParam)
226 while ((Map[i].m_Frequency != 0) &&
227 (Frequency > Map[i].m_Frequency))
229 if (Map[i].m_Frequency == 0)
235 static bool SearchMap3(
struct SMap2 Map[],
u32 Frequency,
236 u8 *pParam1,
u8 *pParam2)
240 while ((Map[i].m_Frequency != 0) &&
241 (Frequency > Map[i].m_Frequency))
243 if (Map[i].m_Frequency == 0)
250 static bool SearchMap4(
struct SRFBandMap Map[],
251 u32 Frequency,
u8 *pRFBand)
255 while (i < 7 && (Frequency > Map[i].m_RF_max))
263 static int ThermometerRead(
struct tda_state *state,
u8 *pTM_Value)
270 status = UpdateReg(state,
TM);
273 status =
Read(state, Regs);
276 if (((Regs[
TM] & 0x0F) == 0 && (Regs[
TM] & 0x20) == 0x20) ||
277 ((Regs[
TM] & 0x0F) == 8 && (Regs[
TM] & 0x20) == 0x00)) {
279 status = UpdateReg(state,
TM);
283 status =
Read(state, Regs);
287 *pTM_Value = (Regs[
TM] & 0x20)
291 status = UpdateReg(state,
TM);
295 status = UpdateReg(state,
EP4);
303 static int StandBy(
struct tda_state *state)
308 status = UpdateReg(state,
EB12);
312 status = UpdateReg(state,
EB18);
317 status = UpdateReg(state,
EP3);
321 status = UpdateRegs(state,
EB21,
EB23);
336 if (!SearchMap3(m_Main_PLL_Map, freq, &PostDiv, &Div))
339 OscFreq = (
u64) freq * (
u64) Div;
340 OscFreq *= (
u64) 16384;
345 state->
m_Regs[
MD1] = ((MainDiv >> 16) & 0x7F);
346 state->
m_Regs[
MD2] = ((MainDiv >> 8) & 0xFF);
349 return UpdateRegs(state,
MPD,
MD3);
352 static int CalcCalPLL(
struct tda_state *state,
u32 freq)
359 if (!SearchMap3(m_Cal_PLL_Map, freq, &PostDiv, &Div))
362 OscFreq = (
u64)freq * (
u64)Div;
364 OscFreq *= (
u64)16384;
369 state->
m_Regs[
CD1] = ((CalDiv >> 16) & 0xFF);
370 state->
m_Regs[
CD2] = ((CalDiv >> 8) & 0xFF);
373 return UpdateRegs(state,
CPD,
CD3);
376 static int CalibrateRF(
struct tda_state *state,
388 status = UpdateReg(state,
EP4);
392 status = UpdateReg(state,
EB18);
398 if (state->
m_Regs[ID] != 0x83)
402 SearchMap1(m_GainTaper_Map, freq, &GainTaper) &&
403 SearchMap3(m_KM_Map, freq, &RFC_K, &RFC_M)))
407 state->
m_Regs[
EP2] = (RFBand << 5) | GainTaper;
411 status = UpdateRegs(state,
EP1,
EP3);
414 status = UpdateReg(state,
EB13);
419 status = UpdateReg(state,
EB4);
424 status = UpdateReg(state,
EB7);
429 status = UpdateReg(state,
EB14);
434 status = UpdateReg(state,
EB20);
439 status = UpdateRegs(state,
EP4,
EP5);
443 status = CalcCalPLL(state, freq);
446 status = CalcMainPLL(state, freq + 1000000);
451 status = UpdateReg(state,
EP2);
454 status = UpdateReg(state,
EP1);
457 status = UpdateReg(state,
EP2);
460 status = UpdateReg(state,
EP1);
465 status = UpdateReg(state,
EB4);
470 status = UpdateReg(state,
EB7);
476 status = UpdateReg(state,
EB20);
484 status = UpdateReg(state,
EB18);
487 status = UpdateRegs(state,
EP3,
EP4);
490 status = UpdateReg(state,
EP1);
494 status = ReadExtented(state, Regs);
498 *pCprog = Regs[
EB14];
504 static int RFTrackingFiltersInit(
struct tda_state *state,
509 u32 RF1 = m_RF_Band_Map[RFBand].m_RF1_Default;
510 u32 RF2 = m_RF_Band_Map[RFBand].m_RF2_Default;
511 u32 RF3 = m_RF_Band_Map[RFBand].m_RF3_Default;
515 s32 Cprog_table1 = 0;
517 s32 Cprog_table2 = 0;
519 s32 Cprog_table3 = 0;
527 status = PowerScan(state, RFBand, RF1, &RF1, &bcal);
531 status = CalibrateRF(state, RFBand, RF1, &Cprog_cal1);
535 SearchMap2(m_RF_Cal_Map, RF1, &Cprog_table1);
537 Cprog_cal1 = Cprog_table1;
538 state->
m_RF_B1[RFBand] = Cprog_cal1 - Cprog_table1;
544 status = PowerScan(state, RFBand, RF2, &RF2, &bcal);
548 status = CalibrateRF(state, RFBand, RF2, &Cprog_cal2);
552 SearchMap2(m_RF_Cal_Map, RF2, &Cprog_table2);
554 Cprog_cal2 = Cprog_table2;
557 (Cprog_cal2 - Cprog_table2 - Cprog_cal1 + Cprog_table1) /
558 ((
s32)(RF2) - (
s32)(RF1));
563 status = PowerScan(state, RFBand, RF3, &RF3, &bcal);
567 status = CalibrateRF(state, RFBand, RF3, &Cprog_cal3);
571 SearchMap2(m_RF_Cal_Map, RF3, &Cprog_table3);
573 Cprog_cal3 = Cprog_table3;
574 state->
m_RF_A2[RFBand] = (Cprog_cal3 - Cprog_table3 - Cprog_cal2 + Cprog_table2) / ((
s32)(RF3) - (
s32)(RF2));
575 state->
m_RF_B2[RFBand] = Cprog_cal2 - Cprog_table2;
579 state->
m_RF1[RFBand] = RF1;
580 state->
m_RF2[RFBand] = RF2;
581 state->
m_RF3[RFBand] = RF3;
584 printk(
KERN_ERR "tda18271c2dd: %s %d RF1 = %d A1 = %d B1 = %d RF2 = %d A2 = %d B2 = %d RF3 = %d\n", __func__,
592 static int PowerScan(
struct tda_state *state,
593 u8 RFBand,
u32 RF_in,
u32 *pRF_Out,
bool *pbcal)
608 if (!(SearchMap2(m_RF_Cal_Map, RF_in, &RFC_Cprog) &&
609 SearchMap1(m_GainTaper_Map, RF_in, &Gain_Taper) &&
610 SearchMap3(m_CID_Target_Map, RF_in, &CID_Target, &CountLimit))) {
612 printk(
KERN_ERR "tda18271c2dd: %s Search map failed\n", __func__);
616 state->
m_Regs[
EP2] = (RFBand << 5) | Gain_Taper;
618 status = UpdateReg(state,
EP2);
621 status = UpdateReg(state,
EB14);
625 freq_MainPLL = RF_in + 1000000;
626 status = CalcMainPLL(state, freq_MainPLL);
631 status = UpdateReg(state,
EP4);
634 status = UpdateReg(state,
EP2);
637 status = ReadExtented(state, Regs);
640 CID_Gain = Regs[
EB10] & 0x3F;
645 while (CID_Gain < CID_Target) {
646 freq_MainPLL = RF_in + sign * Count + 1000000;
647 status = CalcMainPLL(state, freq_MainPLL);
652 status = UpdateReg(state,
EP2);
655 status = ReadExtented(state, Regs);
658 CID_Gain = Regs[
EB10] & 0x3F;
661 if (Count < CountLimit * 100000)
673 if (CID_Gain >= CID_Target) {
675 *pRF_Out = freq_MainPLL - 1000000;
683 static int PowerScanInit(
struct tda_state *state)
689 status = UpdateRegs(state,
EP3,
EP4);
693 status = UpdateReg(state,
EB18);
698 status = UpdateRegs(state,
EB21,
EB23);
705 static int CalcRFFilterCurve(
struct tda_state *state)
710 status = PowerScanInit(state);
713 status = RFTrackingFiltersInit(state, 0);
716 status = RFTrackingFiltersInit(state, 1);
719 status = RFTrackingFiltersInit(state, 2);
722 status = RFTrackingFiltersInit(state, 3);
725 status = RFTrackingFiltersInit(state, 4);
728 status = RFTrackingFiltersInit(state, 5);
731 status = RFTrackingFiltersInit(state, 6);
742 static int FixedContentsI2CUpdate(
struct tda_state *state)
744 static u8 InitRegs[] = {
746 0xDF, 0x16, 0x60, 0x80,
747 0x80, 0x00, 0x00, 0x00,
748 0x00, 0x00, 0x00, 0x00,
749 0xFC, 0x01, 0x84, 0x41,
750 0x01, 0x84, 0x40, 0x07,
751 0x00, 0x00, 0x96, 0x3F,
752 0xC1, 0x00, 0x8F, 0x00,
753 0x00, 0x8C, 0x00, 0x20,
759 status = UpdateRegs(state,
TM,
EB23);
765 status = UpdateReg(state,
EB17);
769 status = UpdateReg(state,
EB17);
773 status = UpdateReg(state,
EB17);
777 status = UpdateReg(state,
EB17);
793 status = UpdateRegs(state,
EP2,
MD3);
799 status = UpdateReg(state,
EB4);
804 status = UpdateReg(state,
EB4);
810 status = UpdateReg(state,
EP1);
819 status = UpdateRegs(state,
EP3,
CD3);
823 status = UpdateReg(state,
EP2);
835 status = UpdateRegs(state,
EP3,
MD3);
840 status = UpdateReg(state,
EP1);
849 status = UpdateRegs(state,
EP3,
CD3);
853 status = UpdateReg(state,
EP2);
866 status = UpdateRegs(state,
EP3,
MD3);
870 status = UpdateReg(state,
EP1);
877 status = UpdateRegs(state,
EP3,
CD3);
881 status = UpdateReg(state,
EP2);
888 status = UpdateReg(state,
EP4);
891 status = UpdateReg(state,
EP1);
899 static int InitCal(
struct tda_state *state)
904 status = FixedContentsI2CUpdate(state);
907 status = CalcRFFilterCurve(state);
910 status = StandBy(state);
918 static int RFTrackingFiltersCorrection(
struct tda_state *state,
926 if (!SearchMap2(m_RF_Cal_Map, Frequency, &Cprog_table) ||
927 !SearchMap4(m_RF_Band_Map, Frequency, &RFBand) ||
928 !SearchMap1(m_RF_Cal_DC_Over_DT_Map, Frequency, &dCoverdT))
945 status = UpdateReg(state,
EP3);
949 status = ThermometerRead(state, &TMValue_Current);
953 if (RF3 == 0 || Frequency < RF2)
954 Capprox = RF_A1 * ((
s32)(Frequency) - (
s32)(RF1)) + RF_B1 + Cprog_table;
956 Capprox = RF_A2 * ((
s32)(Frequency) - (
s32)(RF2)) + RF_B2 + Cprog_table;
964 else if (Capprox > 255)
972 status = UpdateReg(state,
EB14);
980 static int ChannelConfiguration(
struct tda_state *state,
981 u32 Frequency,
int Standard)
992 state->
IF = IntermediateFrequency;
997 SearchMap1(m_GainTaper_Map, Frequency, &GainTaper) &&
998 SearchMap1(m_IR_Meas_Map, Frequency, &IR_Meas) &&
999 SearchMap4(m_RF_Band_Map, Frequency, &RF_Band))) {
1001 printk(
KERN_ERR "tda18271c2dd: %s SearchMap failed\n", __func__);
1036 status = UpdateRegs(state,
EB22,
EB23);
1042 state->
m_Regs[
EP2] = (RF_Band << 5) | GainTaper;
1048 status = UpdateReg(state,
EB1);
1053 status = CalcMainPLL(state, Frequency + IntermediateFrequency);
1056 status = UpdateRegs(state,
TM,
EP5);
1060 status = UpdateReg(state,
EB4);
1065 status = UpdateReg(state,
EB4);
1071 status = CalcCalPLL(state, Frequency + IntermediateFrequency);
1075 SearchMap3(m_Cal_PLL_Map, Frequency + IntermediateFrequency, &PostDiv, &Div);
1077 status = UpdateReg(state,
MPD);
1080 status = UpdateRegs(state,
TM,
EP5);
1085 status = UpdateReg(state,
EB7);
1090 status = UpdateReg(state,
EB7);
1097 status = UpdateReg(state,
EP3);
1156 else if (bw <= 7000000)
1165 status = RFTrackingFiltersCorrection(state, state->
m_Frequency);
1168 status = ChannelConfiguration(state, state->
m_Frequency,
1179 static int GetSignalStrength(
s32 *pSignalStrength,
u32 RFAgc,
u32 IFAgc)
1183 *pSignalStrength = IFAgc * 100;
1186 *pSignalStrength = 50000 + (IFAgc - 500) * 30;
1197 *frequency = state->
IF;
1211 .name =
"NXP TDA18271C2D",
1212 .frequency_min = 47125000,
1213 .frequency_max = 865000000,
1214 .frequency_step = 62500