1 // SPDX-License-Identifier: GPL-2.0-only
3 * tda18271c2dd: Driver for the TDA18271C2 tuner
5 * Copyright (C) 2010 Digital Devices GmbH
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/delay.h>
12 #include <linux/firmware.h>
13 #include <linux/i2c.h>
14 #include <asm/div64.h>
16 #include <media/dvb_frontend.h>
17 #include "tda18271c2dd.h"
19 /* Max transfer size done by I2C transfer functions */
20 #define MAX_XFER_SIZE 64
22 struct SStandardParam
{
56 EP1
, EP2
, EP3
, EP4
, EP5
,
59 EB1
, EB2
, EB3
, EB4
, EB5
, EB6
, EB7
, EB8
, EB9
, EB10
,
60 EB11
, EB12
, EB13
, EB14
, EB15
, EB16
, EB17
, EB18
, EB19
, EB20
,
66 struct i2c_adapter
*i2c
;
86 /* Tracking filter settings for band 0..6 */
95 u8 m_TMValue_RFCal
; /* Calibration temperature */
97 bool m_bFMInput
; /* true to use Pin 8 for FM Radio */
101 static int PowerScan(struct tda_state
*state
,
102 u8 RFBand
, u32 RF_in
,
103 u32
*pRF_Out
, bool *pbcal
);
105 static int i2c_readn(struct i2c_adapter
*adapter
, u8 adr
, u8
*data
, int len
)
107 struct i2c_msg msgs
[1] = {{.addr
= adr
, .flags
= I2C_M_RD
,
108 .buf
= data
, .len
= len
} };
109 return (i2c_transfer(adapter
, msgs
, 1) == 1) ? 0 : -1;
112 static int i2c_write(struct i2c_adapter
*adap
, u8 adr
, u8
*data
, int len
)
114 struct i2c_msg msg
= {.addr
= adr
, .flags
= 0,
115 .buf
= data
, .len
= len
};
117 if (i2c_transfer(adap
, &msg
, 1) != 1) {
118 printk(KERN_ERR
"tda18271c2dd: i2c write error at addr %i\n", adr
);
124 static int WriteRegs(struct tda_state
*state
,
125 u8 SubAddr
, u8
*Regs
, u16 nRegs
)
127 u8 data
[MAX_XFER_SIZE
];
129 if (1 + nRegs
> sizeof(data
)) {
131 "%s: i2c wr: len=%d is too big!\n",
132 KBUILD_MODNAME
, nRegs
);
137 memcpy(data
+ 1, Regs
, nRegs
);
138 return i2c_write(state
->i2c
, state
->adr
, data
, nRegs
+ 1);
141 static int WriteReg(struct tda_state
*state
, u8 SubAddr
, u8 Reg
)
143 u8 msg
[2] = {SubAddr
, Reg
};
145 return i2c_write(state
->i2c
, state
->adr
, msg
, 2);
148 static int Read(struct tda_state
*state
, u8
* Regs
)
150 return i2c_readn(state
->i2c
, state
->adr
, Regs
, 16);
153 static int ReadExtented(struct tda_state
*state
, u8
* Regs
)
155 return i2c_readn(state
->i2c
, state
->adr
, Regs
, NUM_REGS
);
158 static int UpdateRegs(struct tda_state
*state
, u8 RegFrom
, u8 RegTo
)
160 return WriteRegs(state
, RegFrom
,
161 &state
->m_Regs
[RegFrom
], RegTo
-RegFrom
+1);
163 static int UpdateReg(struct tda_state
*state
, u8 Reg
)
165 return WriteReg(state
, Reg
, state
->m_Regs
[Reg
]);
168 #include "tda18271c2dd_maps.h"
170 static void reset(struct tda_state
*state
)
172 u32 ulIFLevelAnalog
= 0;
173 u32 ulIFLevelDigital
= 2;
174 u32 ulIFLevelDVBC
= 7;
175 u32 ulIFLevelDVBT
= 6;
177 u32 ulStandbyMode
= 0x06; /* Send in stdb, but leave osc on */
180 u32 ulSettlingTime
= 100;
182 state
->m_Frequency
= 0;
183 state
->m_SettlingTime
= 100;
184 state
->m_IFLevelAnalog
= (ulIFLevelAnalog
& 0x07) << 2;
185 state
->m_IFLevelDigital
= (ulIFLevelDigital
& 0x07) << 2;
186 state
->m_IFLevelDVBC
= (ulIFLevelDVBC
& 0x07) << 2;
187 state
->m_IFLevelDVBT
= (ulIFLevelDVBT
& 0x07) << 2;
191 state
->m_EP4
|= 0x40;
193 state
->m_EP3_Standby
= ((ulStandbyMode
& 0x07) << 5) | 0x0F;
194 state
->m_bMaster
= (ulSlave
== 0);
196 state
->m_SettlingTime
= ulSettlingTime
;
198 state
->m_bFMInput
= (ulFMInput
== 2);
201 static bool SearchMap1(const struct SMap map
[], u32 frequency
, u8
*param
)
205 while ((map
[i
].m_Frequency
!= 0) && (frequency
> map
[i
].m_Frequency
))
207 if (map
[i
].m_Frequency
== 0)
209 *param
= map
[i
].m_Param
;
213 static bool SearchMap2(const struct SMapI map
[], u32 frequency
, s32
*param
)
217 while ((map
[i
].m_Frequency
!= 0) &&
218 (frequency
> map
[i
].m_Frequency
))
220 if (map
[i
].m_Frequency
== 0)
222 *param
= map
[i
].m_Param
;
226 static bool SearchMap3(const struct SMap2 map
[], u32 frequency
, u8
*param1
,
231 while ((map
[i
].m_Frequency
!= 0) &&
232 (frequency
> map
[i
].m_Frequency
))
234 if (map
[i
].m_Frequency
== 0)
236 *param1
= map
[i
].m_Param1
;
237 *param2
= map
[i
].m_Param2
;
241 static bool SearchMap4(const struct SRFBandMap map
[], u32 frequency
, u8
*rfband
)
245 while (i
< 7 && (frequency
> map
[i
].m_RF_max
))
253 static int ThermometerRead(struct tda_state
*state
, u8
*pTM_Value
)
259 state
->m_Regs
[TM
] |= 0x10;
260 status
= UpdateReg(state
, TM
);
263 status
= Read(state
, Regs
);
266 if (((Regs
[TM
] & 0x0F) == 0 && (Regs
[TM
] & 0x20) == 0x20) ||
267 ((Regs
[TM
] & 0x0F) == 8 && (Regs
[TM
] & 0x20) == 0x00)) {
268 state
->m_Regs
[TM
] ^= 0x20;
269 status
= UpdateReg(state
, TM
);
273 status
= Read(state
, Regs
);
277 *pTM_Value
= (Regs
[TM
] & 0x20)
278 ? m_Thermometer_Map_2
[Regs
[TM
] & 0x0F]
279 : m_Thermometer_Map_1
[Regs
[TM
] & 0x0F] ;
280 state
->m_Regs
[TM
] &= ~0x10; /* Thermometer off */
281 status
= UpdateReg(state
, TM
);
284 state
->m_Regs
[EP4
] &= ~0x03; /* CAL_mode = 0 ????????? */
285 status
= UpdateReg(state
, EP4
);
293 static int StandBy(struct tda_state
*state
)
297 state
->m_Regs
[EB12
] &= ~0x20; /* PD_AGC1_Det = 0 */
298 status
= UpdateReg(state
, EB12
);
301 state
->m_Regs
[EB18
] &= ~0x83; /* AGC1_loop_off = 0, AGC1_Gain = 6 dB */
302 status
= UpdateReg(state
, EB18
);
305 state
->m_Regs
[EB21
] |= 0x03; /* AGC2_Gain = -6 dB */
306 state
->m_Regs
[EP3
] = state
->m_EP3_Standby
;
307 status
= UpdateReg(state
, EP3
);
310 state
->m_Regs
[EB23
] &= ~0x06; /* ForceLP_Fc2_En = 0, LP_Fc[2] = 0 */
311 status
= UpdateRegs(state
, EB21
, EB23
);
318 static int CalcMainPLL(struct tda_state
*state
, u32 freq
)
326 if (!SearchMap3(m_Main_PLL_Map
, freq
, &PostDiv
, &Div
))
329 OscFreq
= (u64
) freq
* (u64
) Div
;
330 OscFreq
*= (u64
) 16384;
331 do_div(OscFreq
, (u64
)16000000);
334 state
->m_Regs
[MPD
] = PostDiv
& 0x77;
335 state
->m_Regs
[MD1
] = ((MainDiv
>> 16) & 0x7F);
336 state
->m_Regs
[MD2
] = ((MainDiv
>> 8) & 0xFF);
337 state
->m_Regs
[MD3
] = (MainDiv
& 0xFF);
339 return UpdateRegs(state
, MPD
, MD3
);
342 static int CalcCalPLL(struct tda_state
*state
, u32 freq
)
349 if (!SearchMap3(m_Cal_PLL_Map
, freq
, &PostDiv
, &Div
))
352 OscFreq
= (u64
)freq
* (u64
)Div
;
353 /* CalDiv = u32( OscFreq * 16384 / 16000000 ); */
354 OscFreq
*= (u64
)16384;
355 do_div(OscFreq
, (u64
)16000000);
358 state
->m_Regs
[CPD
] = PostDiv
;
359 state
->m_Regs
[CD1
] = ((CalDiv
>> 16) & 0xFF);
360 state
->m_Regs
[CD2
] = ((CalDiv
>> 8) & 0xFF);
361 state
->m_Regs
[CD3
] = (CalDiv
& 0xFF);
363 return UpdateRegs(state
, CPD
, CD3
);
366 static int CalibrateRF(struct tda_state
*state
,
367 u8 RFBand
, u32 freq
, s32
*pCprog
)
377 state
->m_Regs
[EP4
] &= ~0x03; /* CAL_mode = 0 */
378 status
= UpdateReg(state
, EP4
);
381 state
->m_Regs
[EB18
] |= 0x03; /* AGC1_Gain = 3 */
382 status
= UpdateReg(state
, EB18
);
386 /* Switching off LT (as datasheet says) causes calibration on C1 to fail */
387 /* (Readout of Cprog is always 255) */
388 if (state
->m_Regs
[ID
] != 0x83) /* C1: ID == 83, C2: ID == 84 */
389 state
->m_Regs
[EP3
] |= 0x40; /* SM_LT = 1 */
391 if (!(SearchMap1(m_BP_Filter_Map
, freq
, &BP_Filter
) &&
392 SearchMap1(m_GainTaper_Map
, freq
, &GainTaper
) &&
393 SearchMap3(m_KM_Map
, freq
, &RFC_K
, &RFC_M
)))
396 state
->m_Regs
[EP1
] = (state
->m_Regs
[EP1
] & ~0x07) | BP_Filter
;
397 state
->m_Regs
[EP2
] = (RFBand
<< 5) | GainTaper
;
399 state
->m_Regs
[EB13
] = (state
->m_Regs
[EB13
] & ~0x7C) | (RFC_K
<< 4) | (RFC_M
<< 2);
401 status
= UpdateRegs(state
, EP1
, EP3
);
404 status
= UpdateReg(state
, EB13
);
408 state
->m_Regs
[EB4
] |= 0x20; /* LO_ForceSrce = 1 */
409 status
= UpdateReg(state
, EB4
);
413 state
->m_Regs
[EB7
] |= 0x20; /* CAL_ForceSrce = 1 */
414 status
= UpdateReg(state
, EB7
);
418 state
->m_Regs
[EB14
] = 0; /* RFC_Cprog = 0 */
419 status
= UpdateReg(state
, EB14
);
423 state
->m_Regs
[EB20
] &= ~0x20; /* ForceLock = 0; */
424 status
= UpdateReg(state
, EB20
);
428 state
->m_Regs
[EP4
] |= 0x03; /* CAL_Mode = 3 */
429 status
= UpdateRegs(state
, EP4
, EP5
);
433 status
= CalcCalPLL(state
, freq
);
436 status
= CalcMainPLL(state
, freq
+ 1000000);
441 status
= UpdateReg(state
, EP2
);
444 status
= UpdateReg(state
, EP1
);
447 status
= UpdateReg(state
, EP2
);
450 status
= UpdateReg(state
, EP1
);
454 state
->m_Regs
[EB4
] &= ~0x20; /* LO_ForceSrce = 0 */
455 status
= UpdateReg(state
, EB4
);
459 state
->m_Regs
[EB7
] &= ~0x20; /* CAL_ForceSrce = 0 */
460 status
= UpdateReg(state
, EB7
);
465 state
->m_Regs
[EB20
] |= 0x20; /* ForceLock = 1; */
466 status
= UpdateReg(state
, EB20
);
471 state
->m_Regs
[EP4
] &= ~0x03; /* CAL_Mode = 0 */
472 state
->m_Regs
[EP3
] &= ~0x40; /* SM_LT = 0 */
473 state
->m_Regs
[EB18
] &= ~0x03; /* AGC1_Gain = 0 */
474 status
= UpdateReg(state
, EB18
);
477 status
= UpdateRegs(state
, EP3
, EP4
);
480 status
= UpdateReg(state
, EP1
);
484 status
= ReadExtented(state
, Regs
);
488 *pCprog
= Regs
[EB14
];
494 static int RFTrackingFiltersInit(struct tda_state
*state
,
499 u32 RF1
= m_RF_Band_Map
[RFBand
].m_RF1_Default
;
500 u32 RF2
= m_RF_Band_Map
[RFBand
].m_RF2_Default
;
501 u32 RF3
= m_RF_Band_Map
[RFBand
].m_RF3_Default
;
505 s32 Cprog_table1
= 0;
507 s32 Cprog_table2
= 0;
509 s32 Cprog_table3
= 0;
511 state
->m_RF_A1
[RFBand
] = 0;
512 state
->m_RF_B1
[RFBand
] = 0;
513 state
->m_RF_A2
[RFBand
] = 0;
514 state
->m_RF_B2
[RFBand
] = 0;
517 status
= PowerScan(state
, RFBand
, RF1
, &RF1
, &bcal
);
521 status
= CalibrateRF(state
, RFBand
, RF1
, &Cprog_cal1
);
525 SearchMap2(m_RF_Cal_Map
, RF1
, &Cprog_table1
);
527 Cprog_cal1
= Cprog_table1
;
528 state
->m_RF_B1
[RFBand
] = Cprog_cal1
- Cprog_table1
;
529 /* state->m_RF_A1[RF_Band] = ???? */
534 status
= PowerScan(state
, RFBand
, RF2
, &RF2
, &bcal
);
538 status
= CalibrateRF(state
, RFBand
, RF2
, &Cprog_cal2
);
542 SearchMap2(m_RF_Cal_Map
, RF2
, &Cprog_table2
);
544 Cprog_cal2
= Cprog_table2
;
546 state
->m_RF_A1
[RFBand
] =
547 (Cprog_cal2
- Cprog_table2
- Cprog_cal1
+ Cprog_table1
) /
548 ((s32
)(RF2
) - (s32
)(RF1
));
553 status
= PowerScan(state
, RFBand
, RF3
, &RF3
, &bcal
);
557 status
= CalibrateRF(state
, RFBand
, RF3
, &Cprog_cal3
);
561 SearchMap2(m_RF_Cal_Map
, RF3
, &Cprog_table3
);
563 Cprog_cal3
= Cprog_table3
;
564 state
->m_RF_A2
[RFBand
] = (Cprog_cal3
- Cprog_table3
- Cprog_cal2
+ Cprog_table2
) / ((s32
)(RF3
) - (s32
)(RF2
));
565 state
->m_RF_B2
[RFBand
] = Cprog_cal2
- Cprog_table2
;
569 state
->m_RF1
[RFBand
] = RF1
;
570 state
->m_RF2
[RFBand
] = RF2
;
571 state
->m_RF3
[RFBand
] = RF3
;
574 printk(KERN_ERR
"tda18271c2dd: %s %d RF1 = %d A1 = %d B1 = %d RF2 = %d A2 = %d B2 = %d RF3 = %d\n", __func__
,
575 RFBand
, RF1
, state
->m_RF_A1
[RFBand
], state
->m_RF_B1
[RFBand
], RF2
,
576 state
->m_RF_A2
[RFBand
], state
->m_RF_B2
[RFBand
], RF3
);
582 static int PowerScan(struct tda_state
*state
,
583 u8 RFBand
, u32 RF_in
, u32
*pRF_Out
, bool *pbcal
)
598 if (!(SearchMap2(m_RF_Cal_Map
, RF_in
, &RFC_Cprog
) &&
599 SearchMap1(m_GainTaper_Map
, RF_in
, &Gain_Taper
) &&
600 SearchMap3(m_CID_Target_Map
, RF_in
, &CID_Target
, &CountLimit
))) {
602 printk(KERN_ERR
"tda18271c2dd: %s Search map failed\n", __func__
);
606 state
->m_Regs
[EP2
] = (RFBand
<< 5) | Gain_Taper
;
607 state
->m_Regs
[EB14
] = (RFC_Cprog
);
608 status
= UpdateReg(state
, EP2
);
611 status
= UpdateReg(state
, EB14
);
615 freq_MainPLL
= RF_in
+ 1000000;
616 status
= CalcMainPLL(state
, freq_MainPLL
);
620 state
->m_Regs
[EP4
] = (state
->m_Regs
[EP4
] & ~0x03) | 1; /* CAL_mode = 1 */
621 status
= UpdateReg(state
, EP4
);
624 status
= UpdateReg(state
, EP2
); /* Launch power measurement */
627 status
= ReadExtented(state
, Regs
);
630 CID_Gain
= Regs
[EB10
] & 0x3F;
631 state
->m_Regs
[ID
] = Regs
[ID
]; /* Chip version, (needed for C1 workaround in CalibrateRF) */
635 while (CID_Gain
< CID_Target
) {
636 freq_MainPLL
= RF_in
+ sign
* Count
+ 1000000;
637 status
= CalcMainPLL(state
, freq_MainPLL
);
640 msleep(wait
? 5 : 1);
642 status
= UpdateReg(state
, EP2
); /* Launch power measurement */
645 status
= ReadExtented(state
, Regs
);
648 CID_Gain
= Regs
[EB10
] & 0x3F;
651 if (Count
< CountLimit
* 100000)
662 if (CID_Gain
>= CID_Target
) {
664 *pRF_Out
= freq_MainPLL
- 1000000;
672 static int PowerScanInit(struct tda_state
*state
)
676 state
->m_Regs
[EP3
] = (state
->m_Regs
[EP3
] & ~0x1F) | 0x12;
677 state
->m_Regs
[EP4
] = (state
->m_Regs
[EP4
] & ~0x1F); /* If level = 0, Cal mode = 0 */
678 status
= UpdateRegs(state
, EP3
, EP4
);
681 state
->m_Regs
[EB18
] = (state
->m_Regs
[EB18
] & ~0x03); /* AGC 1 Gain = 0 */
682 status
= UpdateReg(state
, EB18
);
685 state
->m_Regs
[EB21
] = (state
->m_Regs
[EB21
] & ~0x03); /* AGC 2 Gain = 0 (Datasheet = 3) */
686 state
->m_Regs
[EB23
] = (state
->m_Regs
[EB23
] | 0x06); /* ForceLP_Fc2_En = 1, LPFc[2] = 1 */
687 status
= UpdateRegs(state
, EB21
, EB23
);
694 static int CalcRFFilterCurve(struct tda_state
*state
)
698 msleep(200); /* Temperature stabilisation */
699 status
= PowerScanInit(state
);
702 status
= RFTrackingFiltersInit(state
, 0);
705 status
= RFTrackingFiltersInit(state
, 1);
708 status
= RFTrackingFiltersInit(state
, 2);
711 status
= RFTrackingFiltersInit(state
, 3);
714 status
= RFTrackingFiltersInit(state
, 4);
717 status
= RFTrackingFiltersInit(state
, 5);
720 status
= RFTrackingFiltersInit(state
, 6);
723 status
= ThermometerRead(state
, &state
->m_TMValue_RFCal
); /* also switches off Cal mode !!! */
731 static int FixedContentsI2CUpdate(struct tda_state
*state
)
733 static u8 InitRegs
[] = {
735 0xDF, 0x16, 0x60, 0x80,
736 0x80, 0x00, 0x00, 0x00,
737 0x00, 0x00, 0x00, 0x00,
738 0xFC, 0x01, 0x84, 0x41,
739 0x01, 0x84, 0x40, 0x07,
740 0x00, 0x00, 0x96, 0x3F,
741 0xC1, 0x00, 0x8F, 0x00,
742 0x00, 0x8C, 0x00, 0x20,
746 memcpy(&state
->m_Regs
[TM
], InitRegs
, EB23
- TM
+ 1);
748 status
= UpdateRegs(state
, TM
, EB23
);
752 /* AGC1 gain setup */
753 state
->m_Regs
[EB17
] = 0x00;
754 status
= UpdateReg(state
, EB17
);
757 state
->m_Regs
[EB17
] = 0x03;
758 status
= UpdateReg(state
, EB17
);
761 state
->m_Regs
[EB17
] = 0x43;
762 status
= UpdateReg(state
, EB17
);
765 state
->m_Regs
[EB17
] = 0x4C;
766 status
= UpdateReg(state
, EB17
);
770 /* IRC Cal Low band */
771 state
->m_Regs
[EP3
] = 0x1F;
772 state
->m_Regs
[EP4
] = 0x66;
773 state
->m_Regs
[EP5
] = 0x81;
774 state
->m_Regs
[CPD
] = 0xCC;
775 state
->m_Regs
[CD1
] = 0x6C;
776 state
->m_Regs
[CD2
] = 0x00;
777 state
->m_Regs
[CD3
] = 0x00;
778 state
->m_Regs
[MPD
] = 0xC5;
779 state
->m_Regs
[MD1
] = 0x77;
780 state
->m_Regs
[MD2
] = 0x08;
781 state
->m_Regs
[MD3
] = 0x00;
782 status
= UpdateRegs(state
, EP2
, MD3
); /* diff between sw and datasheet (ep3-md3) */
787 state
->m_Regs
[EB4
] = 0x61; /* missing in sw */
788 status
= UpdateReg(state
, EB4
);
792 state
->m_Regs
[EB4
] = 0x41;
793 status
= UpdateReg(state
, EB4
);
799 status
= UpdateReg(state
, EP1
);
804 state
->m_Regs
[EP5
] = 0x85;
805 state
->m_Regs
[CPD
] = 0xCB;
806 state
->m_Regs
[CD1
] = 0x66;
807 state
->m_Regs
[CD2
] = 0x70;
808 status
= UpdateRegs(state
, EP3
, CD3
);
812 status
= UpdateReg(state
, EP2
);
817 /* IRC Cal mid band */
818 state
->m_Regs
[EP5
] = 0x82;
819 state
->m_Regs
[CPD
] = 0xA8;
820 state
->m_Regs
[CD2
] = 0x00;
821 state
->m_Regs
[MPD
] = 0xA1; /* Datasheet = 0xA9 */
822 state
->m_Regs
[MD1
] = 0x73;
823 state
->m_Regs
[MD2
] = 0x1A;
824 status
= UpdateRegs(state
, EP3
, MD3
);
829 status
= UpdateReg(state
, EP1
);
834 state
->m_Regs
[EP5
] = 0x86;
835 state
->m_Regs
[CPD
] = 0xA8;
836 state
->m_Regs
[CD1
] = 0x66;
837 state
->m_Regs
[CD2
] = 0xA0;
838 status
= UpdateRegs(state
, EP3
, CD3
);
842 status
= UpdateReg(state
, EP2
);
847 /* IRC Cal high band */
848 state
->m_Regs
[EP5
] = 0x83;
849 state
->m_Regs
[CPD
] = 0x98;
850 state
->m_Regs
[CD1
] = 0x65;
851 state
->m_Regs
[CD2
] = 0x00;
852 state
->m_Regs
[MPD
] = 0x91; /* Datasheet = 0x91 */
853 state
->m_Regs
[MD1
] = 0x71;
854 state
->m_Regs
[MD2
] = 0xCD;
855 status
= UpdateRegs(state
, EP3
, MD3
);
859 status
= UpdateReg(state
, EP1
);
863 state
->m_Regs
[EP5
] = 0x87;
864 state
->m_Regs
[CD1
] = 0x65;
865 state
->m_Regs
[CD2
] = 0x50;
866 status
= UpdateRegs(state
, EP3
, CD3
);
870 status
= UpdateReg(state
, EP2
);
876 state
->m_Regs
[EP4
] = 0x64;
877 status
= UpdateReg(state
, EP4
);
880 status
= UpdateReg(state
, EP1
);
888 static int InitCal(struct tda_state
*state
)
893 status
= FixedContentsI2CUpdate(state
);
896 status
= CalcRFFilterCurve(state
);
899 status
= StandBy(state
);
902 /* m_bInitDone = true; */
907 static int RFTrackingFiltersCorrection(struct tda_state
*state
,
915 if (!SearchMap2(m_RF_Cal_Map
, Frequency
, &Cprog_table
) ||
916 !SearchMap4(m_RF_Band_Map
, Frequency
, &RFBand
) ||
917 !SearchMap1(m_RF_Cal_DC_Over_DT_Map
, Frequency
, &dCoverdT
))
923 u32 RF1
= state
->m_RF1
[RFBand
];
924 u32 RF2
= state
->m_RF1
[RFBand
];
925 u32 RF3
= state
->m_RF1
[RFBand
];
926 s32 RF_A1
= state
->m_RF_A1
[RFBand
];
927 s32 RF_B1
= state
->m_RF_B1
[RFBand
];
928 s32 RF_A2
= state
->m_RF_A2
[RFBand
];
929 s32 RF_B2
= state
->m_RF_B2
[RFBand
];
933 state
->m_Regs
[EP3
] &= ~0xE0; /* Power up */
934 status
= UpdateReg(state
, EP3
);
938 status
= ThermometerRead(state
, &TMValue_Current
);
942 if (RF3
== 0 || Frequency
< RF2
)
943 Capprox
= RF_A1
* ((s32
)(Frequency
) - (s32
)(RF1
)) + RF_B1
+ Cprog_table
;
945 Capprox
= RF_A2
* ((s32
)(Frequency
) - (s32
)(RF2
)) + RF_B2
+ Cprog_table
;
947 TComp
= (int)(dCoverdT
) * ((int)(TMValue_Current
) - (int)(state
->m_TMValue_RFCal
))/1000;
953 else if (Capprox
> 255)
957 /* TODO Temperature compensation. There is defenitely a scale factor */
958 /* missing in the datasheet, so leave it out for now. */
959 state
->m_Regs
[EB14
] = Capprox
;
961 status
= UpdateReg(state
, EB14
);
969 static int ChannelConfiguration(struct tda_state
*state
,
970 u32 Frequency
, int Standard
)
973 s32 IntermediateFrequency
= m_StandardTable
[Standard
].m_IFFrequency
;
981 state
->IF
= IntermediateFrequency
;
982 /* printk("tda18271c2dd: %s Freq = %d Standard = %d IF = %d\n", __func__, Frequency, Standard, IntermediateFrequency); */
983 /* get values from tables */
985 if (!(SearchMap1(m_BP_Filter_Map
, Frequency
, &BP_Filter
) &&
986 SearchMap1(m_GainTaper_Map
, Frequency
, &GainTaper
) &&
987 SearchMap1(m_IR_Meas_Map
, Frequency
, &IR_Meas
) &&
988 SearchMap4(m_RF_Band_Map
, Frequency
, &RF_Band
))) {
990 printk(KERN_ERR
"tda18271c2dd: %s SearchMap failed\n", __func__
);
995 state
->m_Regs
[EP3
] = (state
->m_Regs
[EP3
] & ~0x1F) | m_StandardTable
[Standard
].m_EP3_4_0
;
996 state
->m_Regs
[EP3
] &= ~0x04; /* switch RFAGC to high speed mode */
998 /* m_EP4 default for XToutOn, CAL_Mode (0) */
999 state
->m_Regs
[EP4
] = state
->m_EP4
| ((Standard
> HF_AnalogMax
) ? state
->m_IFLevelDigital
: state
->m_IFLevelAnalog
);
1000 /* state->m_Regs[EP4] = state->m_EP4 | state->m_IFLevelDigital; */
1001 if (Standard
<= HF_AnalogMax
)
1002 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelAnalog
;
1003 else if (Standard
<= HF_ATSC
)
1004 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelDVBT
;
1005 else if (Standard
<= HF_DVBC
)
1006 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelDVBC
;
1008 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelDigital
;
1010 if ((Standard
== HF_FM_Radio
) && state
->m_bFMInput
)
1011 state
->m_Regs
[EP4
] |= 0x80;
1013 state
->m_Regs
[MPD
] &= ~0x80;
1014 if (Standard
> HF_AnalogMax
)
1015 state
->m_Regs
[MPD
] |= 0x80; /* Add IF_notch for digital */
1017 state
->m_Regs
[EB22
] = m_StandardTable
[Standard
].m_EB22
;
1019 /* Note: This is missing from flowchart in TDA18271 specification ( 1.5 MHz cutoff for FM ) */
1020 if (Standard
== HF_FM_Radio
)
1021 state
->m_Regs
[EB23
] |= 0x06; /* ForceLP_Fc2_En = 1, LPFc[2] = 1 */
1023 state
->m_Regs
[EB23
] &= ~0x06; /* ForceLP_Fc2_En = 0, LPFc[2] = 0 */
1025 status
= UpdateRegs(state
, EB22
, EB23
);
1029 state
->m_Regs
[EP1
] = (state
->m_Regs
[EP1
] & ~0x07) | 0x40 | BP_Filter
; /* Dis_Power_level = 1, Filter */
1030 state
->m_Regs
[EP5
] = (state
->m_Regs
[EP5
] & ~0x07) | IR_Meas
;
1031 state
->m_Regs
[EP2
] = (RF_Band
<< 5) | GainTaper
;
1033 state
->m_Regs
[EB1
] = (state
->m_Regs
[EB1
] & ~0x07) |
1034 (state
->m_bMaster
? 0x04 : 0x00); /* CALVCO_FortLOn = MS */
1035 /* AGC1_always_master = 0 */
1036 /* AGC_firstn = 0 */
1037 status
= UpdateReg(state
, EB1
);
1041 if (state
->m_bMaster
) {
1042 status
= CalcMainPLL(state
, Frequency
+ IntermediateFrequency
);
1045 status
= UpdateRegs(state
, TM
, EP5
);
1048 state
->m_Regs
[EB4
] |= 0x20; /* LO_forceSrce = 1 */
1049 status
= UpdateReg(state
, EB4
);
1053 state
->m_Regs
[EB4
] &= ~0x20; /* LO_forceSrce = 0 */
1054 status
= UpdateReg(state
, EB4
);
1060 status
= CalcCalPLL(state
, Frequency
+ IntermediateFrequency
);
1064 SearchMap3(m_Cal_PLL_Map
, Frequency
+ IntermediateFrequency
, &PostDiv
, &Div
);
1065 state
->m_Regs
[MPD
] = (state
->m_Regs
[MPD
] & ~0x7F) | (PostDiv
& 0x77);
1066 status
= UpdateReg(state
, MPD
);
1069 status
= UpdateRegs(state
, TM
, EP5
);
1073 state
->m_Regs
[EB7
] |= 0x20; /* CAL_forceSrce = 1 */
1074 status
= UpdateReg(state
, EB7
);
1078 state
->m_Regs
[EB7
] &= ~0x20; /* CAL_forceSrce = 0 */
1079 status
= UpdateReg(state
, EB7
);
1084 if (Standard
!= HF_FM_Radio
)
1085 state
->m_Regs
[EP3
] |= 0x04; /* RFAGC to normal mode */
1086 status
= UpdateReg(state
, EP3
);
1094 static int sleep(struct dvb_frontend
*fe
)
1096 struct tda_state
*state
= fe
->tuner_priv
;
1102 static int init(struct dvb_frontend
*fe
)
1107 static void release(struct dvb_frontend
*fe
)
1109 kfree(fe
->tuner_priv
);
1110 fe
->tuner_priv
= NULL
;
1114 static int set_params(struct dvb_frontend
*fe
)
1116 struct tda_state
*state
= fe
->tuner_priv
;
1119 u32 bw
= fe
->dtv_property_cache
.bandwidth_hz
;
1120 u32 delsys
= fe
->dtv_property_cache
.delivery_system
;
1122 state
->m_Frequency
= fe
->dtv_property_cache
.frequency
;
1129 Standard
= HF_DVBT_6MHZ
;
1132 Standard
= HF_DVBT_7MHZ
;
1135 Standard
= HF_DVBT_8MHZ
;
1141 case SYS_DVBC_ANNEX_A
:
1142 case SYS_DVBC_ANNEX_C
:
1144 Standard
= HF_DVBC_6MHZ
;
1145 else if (bw
<= 7000000)
1146 Standard
= HF_DVBC_7MHZ
;
1148 Standard
= HF_DVBC_8MHZ
;
1154 status
= RFTrackingFiltersCorrection(state
, state
->m_Frequency
);
1157 status
= ChannelConfiguration(state
, state
->m_Frequency
,
1162 msleep(state
->m_SettlingTime
); /* Allow AGC's to settle down */
1168 static int GetSignalStrength(s32
*pSignalStrength
, u32 RFAgc
, u32 IFAgc
)
1171 /* Scale this from 0 to 50000 */
1172 *pSignalStrength
= IFAgc
* 100;
1174 /* Scale range 500-1500 to 50000-80000 */
1175 *pSignalStrength
= 50000 + (IFAgc
- 500) * 30;
1182 static int get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
1184 struct tda_state
*state
= fe
->tuner_priv
;
1186 *frequency
= state
->IF
;
1190 static int get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
1192 /* struct tda_state *state = fe->tuner_priv; */
1193 /* *bandwidth = priv->bandwidth; */
1198 static const struct dvb_tuner_ops tuner_ops
= {
1200 .name
= "NXP TDA18271C2D",
1201 .frequency_min_hz
= 47125 * kHz
,
1202 .frequency_max_hz
= 865 * MHz
,
1203 .frequency_step_hz
= 62500
1207 .set_params
= set_params
,
1209 .get_if_frequency
= get_if_frequency
,
1210 .get_bandwidth
= get_bandwidth
,
1213 struct dvb_frontend
*tda18271c2dd_attach(struct dvb_frontend
*fe
,
1214 struct i2c_adapter
*i2c
, u8 adr
)
1216 struct tda_state
*state
;
1218 state
= kzalloc(sizeof(struct tda_state
), GFP_KERNEL
);
1222 fe
->tuner_priv
= state
;
1225 memcpy(&fe
->ops
.tuner_ops
, &tuner_ops
, sizeof(struct dvb_tuner_ops
));
1231 EXPORT_SYMBOL_GPL(tda18271c2dd_attach
);
1233 MODULE_DESCRIPTION("TDA18271C2 driver");
1234 MODULE_AUTHOR("DD");
1235 MODULE_LICENSE("GPL");