2 * tda18271c2dd: Driver for the TDA18271C2 tuner
4 * Copyright (C) 2010 Digital Devices GmbH
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 only, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/firmware.h>
31 #include <linux/i2c.h>
32 #include <asm/div64.h>
34 #include "dvb_frontend.h"
35 #include "tda18271c2dd.h"
37 /* Max transfer size done by I2C transfer functions */
38 #define MAX_XFER_SIZE 64
40 struct SStandardParam
{
74 EP1
, EP2
, EP3
, EP4
, EP5
,
77 EB1
, EB2
, EB3
, EB4
, EB5
, EB6
, EB7
, EB8
, EB9
, EB10
,
78 EB11
, EB12
, EB13
, EB14
, EB15
, EB16
, EB17
, EB18
, EB19
, EB20
,
84 struct i2c_adapter
*i2c
;
104 /* Tracking filter settings for band 0..6 */
113 u8 m_TMValue_RFCal
; /* Calibration temperatur */
115 bool m_bFMInput
; /* true to use Pin 8 for FM Radio */
119 static int PowerScan(struct tda_state
*state
,
120 u8 RFBand
, u32 RF_in
,
121 u32
*pRF_Out
, bool *pbcal
);
123 static int i2c_readn(struct i2c_adapter
*adapter
, u8 adr
, u8
*data
, int len
)
125 struct i2c_msg msgs
[1] = {{.addr
= adr
, .flags
= I2C_M_RD
,
126 .buf
= data
, .len
= len
} };
127 return (i2c_transfer(adapter
, msgs
, 1) == 1) ? 0 : -1;
130 static int i2c_write(struct i2c_adapter
*adap
, u8 adr
, u8
*data
, int len
)
132 struct i2c_msg msg
= {.addr
= adr
, .flags
= 0,
133 .buf
= data
, .len
= len
};
135 if (i2c_transfer(adap
, &msg
, 1) != 1) {
136 printk(KERN_ERR
"tda18271c2dd: i2c write error at addr %i\n", adr
);
142 static int WriteRegs(struct tda_state
*state
,
143 u8 SubAddr
, u8
*Regs
, u16 nRegs
)
145 u8 data
[MAX_XFER_SIZE
];
147 if (1 + nRegs
> sizeof(data
)) {
149 "%s: i2c wr: len=%d is too big!\n",
150 KBUILD_MODNAME
, nRegs
);
155 memcpy(data
+ 1, Regs
, nRegs
);
156 return i2c_write(state
->i2c
, state
->adr
, data
, nRegs
+ 1);
159 static int WriteReg(struct tda_state
*state
, u8 SubAddr
, u8 Reg
)
161 u8 msg
[2] = {SubAddr
, Reg
};
163 return i2c_write(state
->i2c
, state
->adr
, msg
, 2);
166 static int Read(struct tda_state
*state
, u8
* Regs
)
168 return i2c_readn(state
->i2c
, state
->adr
, Regs
, 16);
171 static int ReadExtented(struct tda_state
*state
, u8
* Regs
)
173 return i2c_readn(state
->i2c
, state
->adr
, Regs
, NUM_REGS
);
176 static int UpdateRegs(struct tda_state
*state
, u8 RegFrom
, u8 RegTo
)
178 return WriteRegs(state
, RegFrom
,
179 &state
->m_Regs
[RegFrom
], RegTo
-RegFrom
+1);
181 static int UpdateReg(struct tda_state
*state
, u8 Reg
)
183 return WriteReg(state
, Reg
, state
->m_Regs
[Reg
]);
186 #include "tda18271c2dd_maps.h"
188 static void reset(struct tda_state
*state
)
190 u32 ulIFLevelAnalog
= 0;
191 u32 ulIFLevelDigital
= 2;
192 u32 ulIFLevelDVBC
= 7;
193 u32 ulIFLevelDVBT
= 6;
195 u32 ulStandbyMode
= 0x06; /* Send in stdb, but leave osc on */
198 u32 ulSettlingTime
= 100;
200 state
->m_Frequency
= 0;
201 state
->m_SettlingTime
= 100;
202 state
->m_IFLevelAnalog
= (ulIFLevelAnalog
& 0x07) << 2;
203 state
->m_IFLevelDigital
= (ulIFLevelDigital
& 0x07) << 2;
204 state
->m_IFLevelDVBC
= (ulIFLevelDVBC
& 0x07) << 2;
205 state
->m_IFLevelDVBT
= (ulIFLevelDVBT
& 0x07) << 2;
209 state
->m_EP4
|= 0x40;
211 state
->m_EP3_Standby
= ((ulStandbyMode
& 0x07) << 5) | 0x0F;
212 state
->m_bMaster
= (ulSlave
== 0);
214 state
->m_SettlingTime
= ulSettlingTime
;
216 state
->m_bFMInput
= (ulFMInput
== 2);
219 static bool SearchMap1(struct SMap Map
[],
220 u32 Frequency
, u8
*pParam
)
224 while ((Map
[i
].m_Frequency
!= 0) && (Frequency
> Map
[i
].m_Frequency
))
226 if (Map
[i
].m_Frequency
== 0)
228 *pParam
= Map
[i
].m_Param
;
232 static bool SearchMap2(struct SMapI Map
[],
233 u32 Frequency
, s32
*pParam
)
237 while ((Map
[i
].m_Frequency
!= 0) &&
238 (Frequency
> Map
[i
].m_Frequency
))
240 if (Map
[i
].m_Frequency
== 0)
242 *pParam
= Map
[i
].m_Param
;
246 static bool SearchMap3(struct SMap2 Map
[], u32 Frequency
,
247 u8
*pParam1
, u8
*pParam2
)
251 while ((Map
[i
].m_Frequency
!= 0) &&
252 (Frequency
> Map
[i
].m_Frequency
))
254 if (Map
[i
].m_Frequency
== 0)
256 *pParam1
= Map
[i
].m_Param1
;
257 *pParam2
= Map
[i
].m_Param2
;
261 static bool SearchMap4(struct SRFBandMap Map
[],
262 u32 Frequency
, u8
*pRFBand
)
266 while (i
< 7 && (Frequency
> Map
[i
].m_RF_max
))
274 static int ThermometerRead(struct tda_state
*state
, u8
*pTM_Value
)
280 state
->m_Regs
[TM
] |= 0x10;
281 status
= UpdateReg(state
, TM
);
284 status
= Read(state
, Regs
);
287 if (((Regs
[TM
] & 0x0F) == 0 && (Regs
[TM
] & 0x20) == 0x20) ||
288 ((Regs
[TM
] & 0x0F) == 8 && (Regs
[TM
] & 0x20) == 0x00)) {
289 state
->m_Regs
[TM
] ^= 0x20;
290 status
= UpdateReg(state
, TM
);
294 status
= Read(state
, Regs
);
298 *pTM_Value
= (Regs
[TM
] & 0x20)
299 ? m_Thermometer_Map_2
[Regs
[TM
] & 0x0F]
300 : m_Thermometer_Map_1
[Regs
[TM
] & 0x0F] ;
301 state
->m_Regs
[TM
] &= ~0x10; /* Thermometer off */
302 status
= UpdateReg(state
, TM
);
305 state
->m_Regs
[EP4
] &= ~0x03; /* CAL_mode = 0 ????????? */
306 status
= UpdateReg(state
, EP4
);
314 static int StandBy(struct tda_state
*state
)
318 state
->m_Regs
[EB12
] &= ~0x20; /* PD_AGC1_Det = 0 */
319 status
= UpdateReg(state
, EB12
);
322 state
->m_Regs
[EB18
] &= ~0x83; /* AGC1_loop_off = 0, AGC1_Gain = 6 dB */
323 status
= UpdateReg(state
, EB18
);
326 state
->m_Regs
[EB21
] |= 0x03; /* AGC2_Gain = -6 dB */
327 state
->m_Regs
[EP3
] = state
->m_EP3_Standby
;
328 status
= UpdateReg(state
, EP3
);
331 state
->m_Regs
[EB23
] &= ~0x06; /* ForceLP_Fc2_En = 0, LP_Fc[2] = 0 */
332 status
= UpdateRegs(state
, EB21
, EB23
);
339 static int CalcMainPLL(struct tda_state
*state
, u32 freq
)
347 if (!SearchMap3(m_Main_PLL_Map
, freq
, &PostDiv
, &Div
))
350 OscFreq
= (u64
) freq
* (u64
) Div
;
351 OscFreq
*= (u64
) 16384;
352 do_div(OscFreq
, (u64
)16000000);
355 state
->m_Regs
[MPD
] = PostDiv
& 0x77;
356 state
->m_Regs
[MD1
] = ((MainDiv
>> 16) & 0x7F);
357 state
->m_Regs
[MD2
] = ((MainDiv
>> 8) & 0xFF);
358 state
->m_Regs
[MD3
] = (MainDiv
& 0xFF);
360 return UpdateRegs(state
, MPD
, MD3
);
363 static int CalcCalPLL(struct tda_state
*state
, u32 freq
)
370 if (!SearchMap3(m_Cal_PLL_Map
, freq
, &PostDiv
, &Div
))
373 OscFreq
= (u64
)freq
* (u64
)Div
;
374 /* CalDiv = u32( OscFreq * 16384 / 16000000 ); */
375 OscFreq
*= (u64
)16384;
376 do_div(OscFreq
, (u64
)16000000);
379 state
->m_Regs
[CPD
] = PostDiv
;
380 state
->m_Regs
[CD1
] = ((CalDiv
>> 16) & 0xFF);
381 state
->m_Regs
[CD2
] = ((CalDiv
>> 8) & 0xFF);
382 state
->m_Regs
[CD3
] = (CalDiv
& 0xFF);
384 return UpdateRegs(state
, CPD
, CD3
);
387 static int CalibrateRF(struct tda_state
*state
,
388 u8 RFBand
, u32 freq
, s32
*pCprog
)
398 state
->m_Regs
[EP4
] &= ~0x03; /* CAL_mode = 0 */
399 status
= UpdateReg(state
, EP4
);
402 state
->m_Regs
[EB18
] |= 0x03; /* AGC1_Gain = 3 */
403 status
= UpdateReg(state
, EB18
);
407 /* Switching off LT (as datasheet says) causes calibration on C1 to fail */
408 /* (Readout of Cprog is allways 255) */
409 if (state
->m_Regs
[ID
] != 0x83) /* C1: ID == 83, C2: ID == 84 */
410 state
->m_Regs
[EP3
] |= 0x40; /* SM_LT = 1 */
412 if (!(SearchMap1(m_BP_Filter_Map
, freq
, &BP_Filter
) &&
413 SearchMap1(m_GainTaper_Map
, freq
, &GainTaper
) &&
414 SearchMap3(m_KM_Map
, freq
, &RFC_K
, &RFC_M
)))
417 state
->m_Regs
[EP1
] = (state
->m_Regs
[EP1
] & ~0x07) | BP_Filter
;
418 state
->m_Regs
[EP2
] = (RFBand
<< 5) | GainTaper
;
420 state
->m_Regs
[EB13
] = (state
->m_Regs
[EB13
] & ~0x7C) | (RFC_K
<< 4) | (RFC_M
<< 2);
422 status
= UpdateRegs(state
, EP1
, EP3
);
425 status
= UpdateReg(state
, EB13
);
429 state
->m_Regs
[EB4
] |= 0x20; /* LO_ForceSrce = 1 */
430 status
= UpdateReg(state
, EB4
);
434 state
->m_Regs
[EB7
] |= 0x20; /* CAL_ForceSrce = 1 */
435 status
= UpdateReg(state
, EB7
);
439 state
->m_Regs
[EB14
] = 0; /* RFC_Cprog = 0 */
440 status
= UpdateReg(state
, EB14
);
444 state
->m_Regs
[EB20
] &= ~0x20; /* ForceLock = 0; */
445 status
= UpdateReg(state
, EB20
);
449 state
->m_Regs
[EP4
] |= 0x03; /* CAL_Mode = 3 */
450 status
= UpdateRegs(state
, EP4
, EP5
);
454 status
= CalcCalPLL(state
, freq
);
457 status
= CalcMainPLL(state
, freq
+ 1000000);
462 status
= UpdateReg(state
, EP2
);
465 status
= UpdateReg(state
, EP1
);
468 status
= UpdateReg(state
, EP2
);
471 status
= UpdateReg(state
, EP1
);
475 state
->m_Regs
[EB4
] &= ~0x20; /* LO_ForceSrce = 0 */
476 status
= UpdateReg(state
, EB4
);
480 state
->m_Regs
[EB7
] &= ~0x20; /* CAL_ForceSrce = 0 */
481 status
= UpdateReg(state
, EB7
);
486 state
->m_Regs
[EB20
] |= 0x20; /* ForceLock = 1; */
487 status
= UpdateReg(state
, EB20
);
492 state
->m_Regs
[EP4
] &= ~0x03; /* CAL_Mode = 0 */
493 state
->m_Regs
[EP3
] &= ~0x40; /* SM_LT = 0 */
494 state
->m_Regs
[EB18
] &= ~0x03; /* AGC1_Gain = 0 */
495 status
= UpdateReg(state
, EB18
);
498 status
= UpdateRegs(state
, EP3
, EP4
);
501 status
= UpdateReg(state
, EP1
);
505 status
= ReadExtented(state
, Regs
);
509 *pCprog
= Regs
[EB14
];
515 static int RFTrackingFiltersInit(struct tda_state
*state
,
520 u32 RF1
= m_RF_Band_Map
[RFBand
].m_RF1_Default
;
521 u32 RF2
= m_RF_Band_Map
[RFBand
].m_RF2_Default
;
522 u32 RF3
= m_RF_Band_Map
[RFBand
].m_RF3_Default
;
526 s32 Cprog_table1
= 0;
528 s32 Cprog_table2
= 0;
530 s32 Cprog_table3
= 0;
532 state
->m_RF_A1
[RFBand
] = 0;
533 state
->m_RF_B1
[RFBand
] = 0;
534 state
->m_RF_A2
[RFBand
] = 0;
535 state
->m_RF_B2
[RFBand
] = 0;
538 status
= PowerScan(state
, RFBand
, RF1
, &RF1
, &bcal
);
542 status
= CalibrateRF(state
, RFBand
, RF1
, &Cprog_cal1
);
546 SearchMap2(m_RF_Cal_Map
, RF1
, &Cprog_table1
);
548 Cprog_cal1
= Cprog_table1
;
549 state
->m_RF_B1
[RFBand
] = Cprog_cal1
- Cprog_table1
;
550 /* state->m_RF_A1[RF_Band] = ???? */
555 status
= PowerScan(state
, RFBand
, RF2
, &RF2
, &bcal
);
559 status
= CalibrateRF(state
, RFBand
, RF2
, &Cprog_cal2
);
563 SearchMap2(m_RF_Cal_Map
, RF2
, &Cprog_table2
);
565 Cprog_cal2
= Cprog_table2
;
567 state
->m_RF_A1
[RFBand
] =
568 (Cprog_cal2
- Cprog_table2
- Cprog_cal1
+ Cprog_table1
) /
569 ((s32
)(RF2
) - (s32
)(RF1
));
574 status
= PowerScan(state
, RFBand
, RF3
, &RF3
, &bcal
);
578 status
= CalibrateRF(state
, RFBand
, RF3
, &Cprog_cal3
);
582 SearchMap2(m_RF_Cal_Map
, RF3
, &Cprog_table3
);
584 Cprog_cal3
= Cprog_table3
;
585 state
->m_RF_A2
[RFBand
] = (Cprog_cal3
- Cprog_table3
- Cprog_cal2
+ Cprog_table2
) / ((s32
)(RF3
) - (s32
)(RF2
));
586 state
->m_RF_B2
[RFBand
] = Cprog_cal2
- Cprog_table2
;
590 state
->m_RF1
[RFBand
] = RF1
;
591 state
->m_RF2
[RFBand
] = RF2
;
592 state
->m_RF3
[RFBand
] = RF3
;
595 printk(KERN_ERR
"tda18271c2dd: %s %d RF1 = %d A1 = %d B1 = %d RF2 = %d A2 = %d B2 = %d RF3 = %d\n", __func__
,
596 RFBand
, RF1
, state
->m_RF_A1
[RFBand
], state
->m_RF_B1
[RFBand
], RF2
,
597 state
->m_RF_A2
[RFBand
], state
->m_RF_B2
[RFBand
], RF3
);
603 static int PowerScan(struct tda_state
*state
,
604 u8 RFBand
, u32 RF_in
, u32
*pRF_Out
, bool *pbcal
)
619 if (!(SearchMap2(m_RF_Cal_Map
, RF_in
, &RFC_Cprog
) &&
620 SearchMap1(m_GainTaper_Map
, RF_in
, &Gain_Taper
) &&
621 SearchMap3(m_CID_Target_Map
, RF_in
, &CID_Target
, &CountLimit
))) {
623 printk(KERN_ERR
"tda18271c2dd: %s Search map failed\n", __func__
);
627 state
->m_Regs
[EP2
] = (RFBand
<< 5) | Gain_Taper
;
628 state
->m_Regs
[EB14
] = (RFC_Cprog
);
629 status
= UpdateReg(state
, EP2
);
632 status
= UpdateReg(state
, EB14
);
636 freq_MainPLL
= RF_in
+ 1000000;
637 status
= CalcMainPLL(state
, freq_MainPLL
);
641 state
->m_Regs
[EP4
] = (state
->m_Regs
[EP4
] & ~0x03) | 1; /* CAL_mode = 1 */
642 status
= UpdateReg(state
, EP4
);
645 status
= UpdateReg(state
, EP2
); /* Launch power measurement */
648 status
= ReadExtented(state
, Regs
);
651 CID_Gain
= Regs
[EB10
] & 0x3F;
652 state
->m_Regs
[ID
] = Regs
[ID
]; /* Chip version, (needed for C1 workarround in CalibrateRF) */
656 while (CID_Gain
< CID_Target
) {
657 freq_MainPLL
= RF_in
+ sign
* Count
+ 1000000;
658 status
= CalcMainPLL(state
, freq_MainPLL
);
661 msleep(wait
? 5 : 1);
663 status
= UpdateReg(state
, EP2
); /* Launch power measurement */
666 status
= ReadExtented(state
, Regs
);
669 CID_Gain
= Regs
[EB10
] & 0x3F;
672 if (Count
< CountLimit
* 100000)
684 if (CID_Gain
>= CID_Target
) {
686 *pRF_Out
= freq_MainPLL
- 1000000;
694 static int PowerScanInit(struct tda_state
*state
)
698 state
->m_Regs
[EP3
] = (state
->m_Regs
[EP3
] & ~0x1F) | 0x12;
699 state
->m_Regs
[EP4
] = (state
->m_Regs
[EP4
] & ~0x1F); /* If level = 0, Cal mode = 0 */
700 status
= UpdateRegs(state
, EP3
, EP4
);
703 state
->m_Regs
[EB18
] = (state
->m_Regs
[EB18
] & ~0x03); /* AGC 1 Gain = 0 */
704 status
= UpdateReg(state
, EB18
);
707 state
->m_Regs
[EB21
] = (state
->m_Regs
[EB21
] & ~0x03); /* AGC 2 Gain = 0 (Datasheet = 3) */
708 state
->m_Regs
[EB23
] = (state
->m_Regs
[EB23
] | 0x06); /* ForceLP_Fc2_En = 1, LPFc[2] = 1 */
709 status
= UpdateRegs(state
, EB21
, EB23
);
716 static int CalcRFFilterCurve(struct tda_state
*state
)
720 msleep(200); /* Temperature stabilisation */
721 status
= PowerScanInit(state
);
724 status
= RFTrackingFiltersInit(state
, 0);
727 status
= RFTrackingFiltersInit(state
, 1);
730 status
= RFTrackingFiltersInit(state
, 2);
733 status
= RFTrackingFiltersInit(state
, 3);
736 status
= RFTrackingFiltersInit(state
, 4);
739 status
= RFTrackingFiltersInit(state
, 5);
742 status
= RFTrackingFiltersInit(state
, 6);
745 status
= ThermometerRead(state
, &state
->m_TMValue_RFCal
); /* also switches off Cal mode !!! */
753 static int FixedContentsI2CUpdate(struct tda_state
*state
)
755 static u8 InitRegs
[] = {
757 0xDF, 0x16, 0x60, 0x80,
758 0x80, 0x00, 0x00, 0x00,
759 0x00, 0x00, 0x00, 0x00,
760 0xFC, 0x01, 0x84, 0x41,
761 0x01, 0x84, 0x40, 0x07,
762 0x00, 0x00, 0x96, 0x3F,
763 0xC1, 0x00, 0x8F, 0x00,
764 0x00, 0x8C, 0x00, 0x20,
768 memcpy(&state
->m_Regs
[TM
], InitRegs
, EB23
- TM
+ 1);
770 status
= UpdateRegs(state
, TM
, EB23
);
774 /* AGC1 gain setup */
775 state
->m_Regs
[EB17
] = 0x00;
776 status
= UpdateReg(state
, EB17
);
779 state
->m_Regs
[EB17
] = 0x03;
780 status
= UpdateReg(state
, EB17
);
783 state
->m_Regs
[EB17
] = 0x43;
784 status
= UpdateReg(state
, EB17
);
787 state
->m_Regs
[EB17
] = 0x4C;
788 status
= UpdateReg(state
, EB17
);
792 /* IRC Cal Low band */
793 state
->m_Regs
[EP3
] = 0x1F;
794 state
->m_Regs
[EP4
] = 0x66;
795 state
->m_Regs
[EP5
] = 0x81;
796 state
->m_Regs
[CPD
] = 0xCC;
797 state
->m_Regs
[CD1
] = 0x6C;
798 state
->m_Regs
[CD2
] = 0x00;
799 state
->m_Regs
[CD3
] = 0x00;
800 state
->m_Regs
[MPD
] = 0xC5;
801 state
->m_Regs
[MD1
] = 0x77;
802 state
->m_Regs
[MD2
] = 0x08;
803 state
->m_Regs
[MD3
] = 0x00;
804 status
= UpdateRegs(state
, EP2
, MD3
); /* diff between sw and datasheet (ep3-md3) */
809 state
->m_Regs
[EB4
] = 0x61; /* missing in sw */
810 status
= UpdateReg(state
, EB4
);
814 state
->m_Regs
[EB4
] = 0x41;
815 status
= UpdateReg(state
, EB4
);
821 status
= UpdateReg(state
, EP1
);
826 state
->m_Regs
[EP5
] = 0x85;
827 state
->m_Regs
[CPD
] = 0xCB;
828 state
->m_Regs
[CD1
] = 0x66;
829 state
->m_Regs
[CD2
] = 0x70;
830 status
= UpdateRegs(state
, EP3
, CD3
);
834 status
= UpdateReg(state
, EP2
);
839 /* IRC Cal mid band */
840 state
->m_Regs
[EP5
] = 0x82;
841 state
->m_Regs
[CPD
] = 0xA8;
842 state
->m_Regs
[CD2
] = 0x00;
843 state
->m_Regs
[MPD
] = 0xA1; /* Datasheet = 0xA9 */
844 state
->m_Regs
[MD1
] = 0x73;
845 state
->m_Regs
[MD2
] = 0x1A;
846 status
= UpdateRegs(state
, EP3
, MD3
);
851 status
= UpdateReg(state
, EP1
);
856 state
->m_Regs
[EP5
] = 0x86;
857 state
->m_Regs
[CPD
] = 0xA8;
858 state
->m_Regs
[CD1
] = 0x66;
859 state
->m_Regs
[CD2
] = 0xA0;
860 status
= UpdateRegs(state
, EP3
, CD3
);
864 status
= UpdateReg(state
, EP2
);
869 /* IRC Cal high band */
870 state
->m_Regs
[EP5
] = 0x83;
871 state
->m_Regs
[CPD
] = 0x98;
872 state
->m_Regs
[CD1
] = 0x65;
873 state
->m_Regs
[CD2
] = 0x00;
874 state
->m_Regs
[MPD
] = 0x91; /* Datasheet = 0x91 */
875 state
->m_Regs
[MD1
] = 0x71;
876 state
->m_Regs
[MD2
] = 0xCD;
877 status
= UpdateRegs(state
, EP3
, MD3
);
881 status
= UpdateReg(state
, EP1
);
885 state
->m_Regs
[EP5
] = 0x87;
886 state
->m_Regs
[CD1
] = 0x65;
887 state
->m_Regs
[CD2
] = 0x50;
888 status
= UpdateRegs(state
, EP3
, CD3
);
892 status
= UpdateReg(state
, EP2
);
898 state
->m_Regs
[EP4
] = 0x64;
899 status
= UpdateReg(state
, EP4
);
902 status
= UpdateReg(state
, EP1
);
910 static int InitCal(struct tda_state
*state
)
915 status
= FixedContentsI2CUpdate(state
);
918 status
= CalcRFFilterCurve(state
);
921 status
= StandBy(state
);
924 /* m_bInitDone = true; */
929 static int RFTrackingFiltersCorrection(struct tda_state
*state
,
937 if (!SearchMap2(m_RF_Cal_Map
, Frequency
, &Cprog_table
) ||
938 !SearchMap4(m_RF_Band_Map
, Frequency
, &RFBand
) ||
939 !SearchMap1(m_RF_Cal_DC_Over_DT_Map
, Frequency
, &dCoverdT
))
945 u32 RF1
= state
->m_RF1
[RFBand
];
946 u32 RF2
= state
->m_RF1
[RFBand
];
947 u32 RF3
= state
->m_RF1
[RFBand
];
948 s32 RF_A1
= state
->m_RF_A1
[RFBand
];
949 s32 RF_B1
= state
->m_RF_B1
[RFBand
];
950 s32 RF_A2
= state
->m_RF_A2
[RFBand
];
951 s32 RF_B2
= state
->m_RF_B2
[RFBand
];
955 state
->m_Regs
[EP3
] &= ~0xE0; /* Power up */
956 status
= UpdateReg(state
, EP3
);
960 status
= ThermometerRead(state
, &TMValue_Current
);
964 if (RF3
== 0 || Frequency
< RF2
)
965 Capprox
= RF_A1
* ((s32
)(Frequency
) - (s32
)(RF1
)) + RF_B1
+ Cprog_table
;
967 Capprox
= RF_A2
* ((s32
)(Frequency
) - (s32
)(RF2
)) + RF_B2
+ Cprog_table
;
969 TComp
= (int)(dCoverdT
) * ((int)(TMValue_Current
) - (int)(state
->m_TMValue_RFCal
))/1000;
975 else if (Capprox
> 255)
979 /* TODO Temperature compensation. There is defenitely a scale factor */
980 /* missing in the datasheet, so leave it out for now. */
981 state
->m_Regs
[EB14
] = Capprox
;
983 status
= UpdateReg(state
, EB14
);
991 static int ChannelConfiguration(struct tda_state
*state
,
992 u32 Frequency
, int Standard
)
995 s32 IntermediateFrequency
= m_StandardTable
[Standard
].m_IFFrequency
;
1003 state
->IF
= IntermediateFrequency
;
1004 /* printk("tda18271c2dd: %s Freq = %d Standard = %d IF = %d\n", __func__, Frequency, Standard, IntermediateFrequency); */
1005 /* get values from tables */
1007 if (!(SearchMap1(m_BP_Filter_Map
, Frequency
, &BP_Filter
) &&
1008 SearchMap1(m_GainTaper_Map
, Frequency
, &GainTaper
) &&
1009 SearchMap1(m_IR_Meas_Map
, Frequency
, &IR_Meas
) &&
1010 SearchMap4(m_RF_Band_Map
, Frequency
, &RF_Band
))) {
1012 printk(KERN_ERR
"tda18271c2dd: %s SearchMap failed\n", __func__
);
1017 state
->m_Regs
[EP3
] = (state
->m_Regs
[EP3
] & ~0x1F) | m_StandardTable
[Standard
].m_EP3_4_0
;
1018 state
->m_Regs
[EP3
] &= ~0x04; /* switch RFAGC to high speed mode */
1020 /* m_EP4 default for XToutOn, CAL_Mode (0) */
1021 state
->m_Regs
[EP4
] = state
->m_EP4
| ((Standard
> HF_AnalogMax
) ? state
->m_IFLevelDigital
: state
->m_IFLevelAnalog
);
1022 /* state->m_Regs[EP4] = state->m_EP4 | state->m_IFLevelDigital; */
1023 if (Standard
<= HF_AnalogMax
)
1024 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelAnalog
;
1025 else if (Standard
<= HF_ATSC
)
1026 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelDVBT
;
1027 else if (Standard
<= HF_DVBC
)
1028 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelDVBC
;
1030 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelDigital
;
1032 if ((Standard
== HF_FM_Radio
) && state
->m_bFMInput
)
1033 state
->m_Regs
[EP4
] |= 80;
1035 state
->m_Regs
[MPD
] &= ~0x80;
1036 if (Standard
> HF_AnalogMax
)
1037 state
->m_Regs
[MPD
] |= 0x80; /* Add IF_notch for digital */
1039 state
->m_Regs
[EB22
] = m_StandardTable
[Standard
].m_EB22
;
1041 /* Note: This is missing from flowchart in TDA18271 specification ( 1.5 MHz cutoff for FM ) */
1042 if (Standard
== HF_FM_Radio
)
1043 state
->m_Regs
[EB23
] |= 0x06; /* ForceLP_Fc2_En = 1, LPFc[2] = 1 */
1045 state
->m_Regs
[EB23
] &= ~0x06; /* ForceLP_Fc2_En = 0, LPFc[2] = 0 */
1047 status
= UpdateRegs(state
, EB22
, EB23
);
1051 state
->m_Regs
[EP1
] = (state
->m_Regs
[EP1
] & ~0x07) | 0x40 | BP_Filter
; /* Dis_Power_level = 1, Filter */
1052 state
->m_Regs
[EP5
] = (state
->m_Regs
[EP5
] & ~0x07) | IR_Meas
;
1053 state
->m_Regs
[EP2
] = (RF_Band
<< 5) | GainTaper
;
1055 state
->m_Regs
[EB1
] = (state
->m_Regs
[EB1
] & ~0x07) |
1056 (state
->m_bMaster
? 0x04 : 0x00); /* CALVCO_FortLOn = MS */
1057 /* AGC1_always_master = 0 */
1058 /* AGC_firstn = 0 */
1059 status
= UpdateReg(state
, EB1
);
1063 if (state
->m_bMaster
) {
1064 status
= CalcMainPLL(state
, Frequency
+ IntermediateFrequency
);
1067 status
= UpdateRegs(state
, TM
, EP5
);
1070 state
->m_Regs
[EB4
] |= 0x20; /* LO_forceSrce = 1 */
1071 status
= UpdateReg(state
, EB4
);
1075 state
->m_Regs
[EB4
] &= ~0x20; /* LO_forceSrce = 0 */
1076 status
= UpdateReg(state
, EB4
);
1082 status
= CalcCalPLL(state
, Frequency
+ IntermediateFrequency
);
1086 SearchMap3(m_Cal_PLL_Map
, Frequency
+ IntermediateFrequency
, &PostDiv
, &Div
);
1087 state
->m_Regs
[MPD
] = (state
->m_Regs
[MPD
] & ~0x7F) | (PostDiv
& 0x77);
1088 status
= UpdateReg(state
, MPD
);
1091 status
= UpdateRegs(state
, TM
, EP5
);
1095 state
->m_Regs
[EB7
] |= 0x20; /* CAL_forceSrce = 1 */
1096 status
= UpdateReg(state
, EB7
);
1100 state
->m_Regs
[EB7
] &= ~0x20; /* CAL_forceSrce = 0 */
1101 status
= UpdateReg(state
, EB7
);
1106 if (Standard
!= HF_FM_Radio
)
1107 state
->m_Regs
[EP3
] |= 0x04; /* RFAGC to normal mode */
1108 status
= UpdateReg(state
, EP3
);
1116 static int sleep(struct dvb_frontend
*fe
)
1118 struct tda_state
*state
= fe
->tuner_priv
;
1124 static int init(struct dvb_frontend
*fe
)
1129 static int release(struct dvb_frontend
*fe
)
1131 kfree(fe
->tuner_priv
);
1132 fe
->tuner_priv
= NULL
;
1137 static int set_params(struct dvb_frontend
*fe
)
1139 struct tda_state
*state
= fe
->tuner_priv
;
1142 u32 bw
= fe
->dtv_property_cache
.bandwidth_hz
;
1143 u32 delsys
= fe
->dtv_property_cache
.delivery_system
;
1145 state
->m_Frequency
= fe
->dtv_property_cache
.frequency
;
1152 Standard
= HF_DVBT_6MHZ
;
1155 Standard
= HF_DVBT_7MHZ
;
1158 Standard
= HF_DVBT_8MHZ
;
1163 case SYS_DVBC_ANNEX_A
:
1164 case SYS_DVBC_ANNEX_C
:
1166 Standard
= HF_DVBC_6MHZ
;
1167 else if (bw
<= 7000000)
1168 Standard
= HF_DVBC_7MHZ
;
1170 Standard
= HF_DVBC_8MHZ
;
1176 status
= RFTrackingFiltersCorrection(state
, state
->m_Frequency
);
1179 status
= ChannelConfiguration(state
, state
->m_Frequency
,
1184 msleep(state
->m_SettlingTime
); /* Allow AGC's to settle down */
1190 static int GetSignalStrength(s32
*pSignalStrength
, u32 RFAgc
, u32 IFAgc
)
1193 /* Scale this from 0 to 50000 */
1194 *pSignalStrength
= IFAgc
* 100;
1196 /* Scale range 500-1500 to 50000-80000 */
1197 *pSignalStrength
= 50000 + (IFAgc
- 500) * 30;
1204 static int get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
1206 struct tda_state
*state
= fe
->tuner_priv
;
1208 *frequency
= state
->IF
;
1212 static int get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
1214 /* struct tda_state *state = fe->tuner_priv; */
1215 /* *bandwidth = priv->bandwidth; */
1220 static struct dvb_tuner_ops tuner_ops
= {
1222 .name
= "NXP TDA18271C2D",
1223 .frequency_min
= 47125000,
1224 .frequency_max
= 865000000,
1225 .frequency_step
= 62500
1229 .set_params
= set_params
,
1231 .get_if_frequency
= get_if_frequency
,
1232 .get_bandwidth
= get_bandwidth
,
1235 struct dvb_frontend
*tda18271c2dd_attach(struct dvb_frontend
*fe
,
1236 struct i2c_adapter
*i2c
, u8 adr
)
1238 struct tda_state
*state
;
1240 state
= kzalloc(sizeof(struct tda_state
), GFP_KERNEL
);
1244 fe
->tuner_priv
= state
;
1247 memcpy(&fe
->ops
.tuner_ops
, &tuner_ops
, sizeof(struct dvb_tuner_ops
));
1253 EXPORT_SYMBOL_GPL(tda18271c2dd_attach
);
1255 MODULE_DESCRIPTION("TDA18271C2 driver");
1256 MODULE_AUTHOR("DD");
1257 MODULE_LICENSE("GPL");