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"
36 struct SStandardParam
{
70 EP1
, EP2
, EP3
, EP4
, EP5
,
73 EB1
, EB2
, EB3
, EB4
, EB5
, EB6
, EB7
, EB8
, EB9
, EB10
,
74 EB11
, EB12
, EB13
, EB14
, EB15
, EB16
, EB17
, EB18
, EB19
, EB20
,
80 struct i2c_adapter
*i2c
;
100 /* Tracking filter settings for band 0..6 */
109 u8 m_TMValue_RFCal
; /* Calibration temperatur */
111 bool m_bFMInput
; /* true to use Pin 8 for FM Radio */
115 static int PowerScan(struct tda_state
*state
,
116 u8 RFBand
, u32 RF_in
,
117 u32
*pRF_Out
, bool *pbcal
);
119 static int i2c_readn(struct i2c_adapter
*adapter
, u8 adr
, u8
*data
, int len
)
121 struct i2c_msg msgs
[1] = {{.addr
= adr
, .flags
= I2C_M_RD
,
122 .buf
= data
, .len
= len
} };
123 return (i2c_transfer(adapter
, msgs
, 1) == 1) ? 0 : -1;
126 static int i2c_write(struct i2c_adapter
*adap
, u8 adr
, u8
*data
, int len
)
128 struct i2c_msg msg
= {.addr
= adr
, .flags
= 0,
129 .buf
= data
, .len
= len
};
131 if (i2c_transfer(adap
, &msg
, 1) != 1) {
132 printk(KERN_ERR
"tda18271c2dd: i2c write error at addr %i\n", adr
);
138 static int WriteRegs(struct tda_state
*state
,
139 u8 SubAddr
, u8
*Regs
, u16 nRegs
)
144 memcpy(data
+ 1, Regs
, nRegs
);
145 return i2c_write(state
->i2c
, state
->adr
, data
, nRegs
+1);
148 static int WriteReg(struct tda_state
*state
, u8 SubAddr
, u8 Reg
)
150 u8 msg
[2] = {SubAddr
, Reg
};
152 return i2c_write(state
->i2c
, state
->adr
, msg
, 2);
155 static int Read(struct tda_state
*state
, u8
* Regs
)
157 return i2c_readn(state
->i2c
, state
->adr
, Regs
, 16);
160 static int ReadExtented(struct tda_state
*state
, u8
* Regs
)
162 return i2c_readn(state
->i2c
, state
->adr
, Regs
, NUM_REGS
);
165 static int UpdateRegs(struct tda_state
*state
, u8 RegFrom
, u8 RegTo
)
167 return WriteRegs(state
, RegFrom
,
168 &state
->m_Regs
[RegFrom
], RegTo
-RegFrom
+1);
170 static int UpdateReg(struct tda_state
*state
, u8 Reg
)
172 return WriteReg(state
, Reg
, state
->m_Regs
[Reg
]);
175 #include "tda18271c2dd_maps.h"
177 static void reset(struct tda_state
*state
)
179 u32 ulIFLevelAnalog
= 0;
180 u32 ulIFLevelDigital
= 2;
181 u32 ulIFLevelDVBC
= 7;
182 u32 ulIFLevelDVBT
= 6;
184 u32 ulStandbyMode
= 0x06; /* Send in stdb, but leave osc on */
187 u32 ulSettlingTime
= 100;
189 state
->m_Frequency
= 0;
190 state
->m_SettlingTime
= 100;
191 state
->m_IFLevelAnalog
= (ulIFLevelAnalog
& 0x07) << 2;
192 state
->m_IFLevelDigital
= (ulIFLevelDigital
& 0x07) << 2;
193 state
->m_IFLevelDVBC
= (ulIFLevelDVBC
& 0x07) << 2;
194 state
->m_IFLevelDVBT
= (ulIFLevelDVBT
& 0x07) << 2;
198 state
->m_EP4
|= 0x40;
200 state
->m_EP3_Standby
= ((ulStandbyMode
& 0x07) << 5) | 0x0F;
201 state
->m_bMaster
= (ulSlave
== 0);
203 state
->m_SettlingTime
= ulSettlingTime
;
205 state
->m_bFMInput
= (ulFMInput
== 2);
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)
217 *pParam
= Map
[i
].m_Param
;
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)
231 *pParam
= Map
[i
].m_Param
;
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)
245 *pParam1
= Map
[i
].m_Param1
;
246 *pParam2
= Map
[i
].m_Param2
;
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
)
269 state
->m_Regs
[TM
] |= 0x10;
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)) {
278 state
->m_Regs
[TM
] ^= 0x20;
279 status
= UpdateReg(state
, TM
);
283 status
= Read(state
, Regs
);
287 *pTM_Value
= (Regs
[TM
] & 0x20)
288 ? m_Thermometer_Map_2
[Regs
[TM
] & 0x0F]
289 : m_Thermometer_Map_1
[Regs
[TM
] & 0x0F] ;
290 state
->m_Regs
[TM
] &= ~0x10; /* Thermometer off */
291 status
= UpdateReg(state
, TM
);
294 state
->m_Regs
[EP4
] &= ~0x03; /* CAL_mode = 0 ????????? */
295 status
= UpdateReg(state
, EP4
);
303 static int StandBy(struct tda_state
*state
)
307 state
->m_Regs
[EB12
] &= ~0x20; /* PD_AGC1_Det = 0 */
308 status
= UpdateReg(state
, EB12
);
311 state
->m_Regs
[EB18
] &= ~0x83; /* AGC1_loop_off = 0, AGC1_Gain = 6 dB */
312 status
= UpdateReg(state
, EB18
);
315 state
->m_Regs
[EB21
] |= 0x03; /* AGC2_Gain = -6 dB */
316 state
->m_Regs
[EP3
] = state
->m_EP3_Standby
;
317 status
= UpdateReg(state
, EP3
);
320 state
->m_Regs
[EB23
] &= ~0x06; /* ForceLP_Fc2_En = 0, LP_Fc[2] = 0 */
321 status
= UpdateRegs(state
, EB21
, EB23
);
328 static int CalcMainPLL(struct tda_state
*state
, u32 freq
)
336 if (!SearchMap3(m_Main_PLL_Map
, freq
, &PostDiv
, &Div
))
339 OscFreq
= (u64
) freq
* (u64
) Div
;
340 OscFreq
*= (u64
) 16384;
341 do_div(OscFreq
, (u64
)16000000);
344 state
->m_Regs
[MPD
] = PostDiv
& 0x77;
345 state
->m_Regs
[MD1
] = ((MainDiv
>> 16) & 0x7F);
346 state
->m_Regs
[MD2
] = ((MainDiv
>> 8) & 0xFF);
347 state
->m_Regs
[MD3
] = (MainDiv
& 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
;
363 /* CalDiv = u32( OscFreq * 16384 / 16000000 ); */
364 OscFreq
*= (u64
)16384;
365 do_div(OscFreq
, (u64
)16000000);
368 state
->m_Regs
[CPD
] = PostDiv
;
369 state
->m_Regs
[CD1
] = ((CalDiv
>> 16) & 0xFF);
370 state
->m_Regs
[CD2
] = ((CalDiv
>> 8) & 0xFF);
371 state
->m_Regs
[CD3
] = (CalDiv
& 0xFF);
373 return UpdateRegs(state
, CPD
, CD3
);
376 static int CalibrateRF(struct tda_state
*state
,
377 u8 RFBand
, u32 freq
, s32
*pCprog
)
387 state
->m_Regs
[EP4
] &= ~0x03; /* CAL_mode = 0 */
388 status
= UpdateReg(state
, EP4
);
391 state
->m_Regs
[EB18
] |= 0x03; /* AGC1_Gain = 3 */
392 status
= UpdateReg(state
, EB18
);
396 /* Switching off LT (as datasheet says) causes calibration on C1 to fail */
397 /* (Readout of Cprog is allways 255) */
398 if (state
->m_Regs
[ID
] != 0x83) /* C1: ID == 83, C2: ID == 84 */
399 state
->m_Regs
[EP3
] |= 0x40; /* SM_LT = 1 */
401 if (!(SearchMap1(m_BP_Filter_Map
, freq
, &BP_Filter
) &&
402 SearchMap1(m_GainTaper_Map
, freq
, &GainTaper
) &&
403 SearchMap3(m_KM_Map
, freq
, &RFC_K
, &RFC_M
)))
406 state
->m_Regs
[EP1
] = (state
->m_Regs
[EP1
] & ~0x07) | BP_Filter
;
407 state
->m_Regs
[EP2
] = (RFBand
<< 5) | GainTaper
;
409 state
->m_Regs
[EB13
] = (state
->m_Regs
[EB13
] & ~0x7C) | (RFC_K
<< 4) | (RFC_M
<< 2);
411 status
= UpdateRegs(state
, EP1
, EP3
);
414 status
= UpdateReg(state
, EB13
);
418 state
->m_Regs
[EB4
] |= 0x20; /* LO_ForceSrce = 1 */
419 status
= UpdateReg(state
, EB4
);
423 state
->m_Regs
[EB7
] |= 0x20; /* CAL_ForceSrce = 1 */
424 status
= UpdateReg(state
, EB7
);
428 state
->m_Regs
[EB14
] = 0; /* RFC_Cprog = 0 */
429 status
= UpdateReg(state
, EB14
);
433 state
->m_Regs
[EB20
] &= ~0x20; /* ForceLock = 0; */
434 status
= UpdateReg(state
, EB20
);
438 state
->m_Regs
[EP4
] |= 0x03; /* CAL_Mode = 3 */
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
);
464 state
->m_Regs
[EB4
] &= ~0x20; /* LO_ForceSrce = 0 */
465 status
= UpdateReg(state
, EB4
);
469 state
->m_Regs
[EB7
] &= ~0x20; /* CAL_ForceSrce = 0 */
470 status
= UpdateReg(state
, EB7
);
475 state
->m_Regs
[EB20
] |= 0x20; /* ForceLock = 1; */
476 status
= UpdateReg(state
, EB20
);
481 state
->m_Regs
[EP4
] &= ~0x03; /* CAL_Mode = 0 */
482 state
->m_Regs
[EP3
] &= ~0x40; /* SM_LT = 0 */
483 state
->m_Regs
[EB18
] &= ~0x03; /* AGC1_Gain = 0 */
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;
521 state
->m_RF_A1
[RFBand
] = 0;
522 state
->m_RF_B1
[RFBand
] = 0;
523 state
->m_RF_A2
[RFBand
] = 0;
524 state
->m_RF_B2
[RFBand
] = 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
;
539 /* state->m_RF_A1[RF_Band] = ???? */
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
;
556 state
->m_RF_A1
[RFBand
] =
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__
,
585 RFBand
, RF1
, state
->m_RF_A1
[RFBand
], state
->m_RF_B1
[RFBand
], RF2
,
586 state
->m_RF_A2
[RFBand
], state
->m_RF_B2
[RFBand
], RF3
);
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
;
617 state
->m_Regs
[EB14
] = (RFC_Cprog
);
618 status
= UpdateReg(state
, EP2
);
621 status
= UpdateReg(state
, EB14
);
625 freq_MainPLL
= RF_in
+ 1000000;
626 status
= CalcMainPLL(state
, freq_MainPLL
);
630 state
->m_Regs
[EP4
] = (state
->m_Regs
[EP4
] & ~0x03) | 1; /* CAL_mode = 1 */
631 status
= UpdateReg(state
, EP4
);
634 status
= UpdateReg(state
, EP2
); /* Launch power measurement */
637 status
= ReadExtented(state
, Regs
);
640 CID_Gain
= Regs
[EB10
] & 0x3F;
641 state
->m_Regs
[ID
] = Regs
[ID
]; /* Chip version, (needed for C1 workarround in CalibrateRF) */
645 while (CID_Gain
< CID_Target
) {
646 freq_MainPLL
= RF_in
+ sign
* Count
+ 1000000;
647 status
= CalcMainPLL(state
, freq_MainPLL
);
650 msleep(wait
? 5 : 1);
652 status
= UpdateReg(state
, EP2
); /* Launch power measurement */
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
)
687 state
->m_Regs
[EP3
] = (state
->m_Regs
[EP3
] & ~0x1F) | 0x12;
688 state
->m_Regs
[EP4
] = (state
->m_Regs
[EP4
] & ~0x1F); /* If level = 0, Cal mode = 0 */
689 status
= UpdateRegs(state
, EP3
, EP4
);
692 state
->m_Regs
[EB18
] = (state
->m_Regs
[EB18
] & ~0x03); /* AGC 1 Gain = 0 */
693 status
= UpdateReg(state
, EB18
);
696 state
->m_Regs
[EB21
] = (state
->m_Regs
[EB21
] & ~0x03); /* AGC 2 Gain = 0 (Datasheet = 3) */
697 state
->m_Regs
[EB23
] = (state
->m_Regs
[EB23
] | 0x06); /* ForceLP_Fc2_En = 1, LPFc[2] = 1 */
698 status
= UpdateRegs(state
, EB21
, EB23
);
705 static int CalcRFFilterCurve(struct tda_state
*state
)
709 msleep(200); /* Temperature stabilisation */
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);
734 status
= ThermometerRead(state
, &state
->m_TMValue_RFCal
); /* also switches off Cal mode !!! */
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,
757 memcpy(&state
->m_Regs
[TM
], InitRegs
, EB23
- TM
+ 1);
759 status
= UpdateRegs(state
, TM
, EB23
);
763 /* AGC1 gain setup */
764 state
->m_Regs
[EB17
] = 0x00;
765 status
= UpdateReg(state
, EB17
);
768 state
->m_Regs
[EB17
] = 0x03;
769 status
= UpdateReg(state
, EB17
);
772 state
->m_Regs
[EB17
] = 0x43;
773 status
= UpdateReg(state
, EB17
);
776 state
->m_Regs
[EB17
] = 0x4C;
777 status
= UpdateReg(state
, EB17
);
781 /* IRC Cal Low band */
782 state
->m_Regs
[EP3
] = 0x1F;
783 state
->m_Regs
[EP4
] = 0x66;
784 state
->m_Regs
[EP5
] = 0x81;
785 state
->m_Regs
[CPD
] = 0xCC;
786 state
->m_Regs
[CD1
] = 0x6C;
787 state
->m_Regs
[CD2
] = 0x00;
788 state
->m_Regs
[CD3
] = 0x00;
789 state
->m_Regs
[MPD
] = 0xC5;
790 state
->m_Regs
[MD1
] = 0x77;
791 state
->m_Regs
[MD2
] = 0x08;
792 state
->m_Regs
[MD3
] = 0x00;
793 status
= UpdateRegs(state
, EP2
, MD3
); /* diff between sw and datasheet (ep3-md3) */
798 state
->m_Regs
[EB4
] = 0x61; /* missing in sw */
799 status
= UpdateReg(state
, EB4
);
803 state
->m_Regs
[EB4
] = 0x41;
804 status
= UpdateReg(state
, EB4
);
810 status
= UpdateReg(state
, EP1
);
815 state
->m_Regs
[EP5
] = 0x85;
816 state
->m_Regs
[CPD
] = 0xCB;
817 state
->m_Regs
[CD1
] = 0x66;
818 state
->m_Regs
[CD2
] = 0x70;
819 status
= UpdateRegs(state
, EP3
, CD3
);
823 status
= UpdateReg(state
, EP2
);
828 /* IRC Cal mid band */
829 state
->m_Regs
[EP5
] = 0x82;
830 state
->m_Regs
[CPD
] = 0xA8;
831 state
->m_Regs
[CD2
] = 0x00;
832 state
->m_Regs
[MPD
] = 0xA1; /* Datasheet = 0xA9 */
833 state
->m_Regs
[MD1
] = 0x73;
834 state
->m_Regs
[MD2
] = 0x1A;
835 status
= UpdateRegs(state
, EP3
, MD3
);
840 status
= UpdateReg(state
, EP1
);
845 state
->m_Regs
[EP5
] = 0x86;
846 state
->m_Regs
[CPD
] = 0xA8;
847 state
->m_Regs
[CD1
] = 0x66;
848 state
->m_Regs
[CD2
] = 0xA0;
849 status
= UpdateRegs(state
, EP3
, CD3
);
853 status
= UpdateReg(state
, EP2
);
858 /* IRC Cal high band */
859 state
->m_Regs
[EP5
] = 0x83;
860 state
->m_Regs
[CPD
] = 0x98;
861 state
->m_Regs
[CD1
] = 0x65;
862 state
->m_Regs
[CD2
] = 0x00;
863 state
->m_Regs
[MPD
] = 0x91; /* Datasheet = 0x91 */
864 state
->m_Regs
[MD1
] = 0x71;
865 state
->m_Regs
[MD2
] = 0xCD;
866 status
= UpdateRegs(state
, EP3
, MD3
);
870 status
= UpdateReg(state
, EP1
);
874 state
->m_Regs
[EP5
] = 0x87;
875 state
->m_Regs
[CD1
] = 0x65;
876 state
->m_Regs
[CD2
] = 0x50;
877 status
= UpdateRegs(state
, EP3
, CD3
);
881 status
= UpdateReg(state
, EP2
);
887 state
->m_Regs
[EP4
] = 0x64;
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
);
913 /* m_bInitDone = true; */
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
))
934 u32 RF1
= state
->m_RF1
[RFBand
];
935 u32 RF2
= state
->m_RF1
[RFBand
];
936 u32 RF3
= state
->m_RF1
[RFBand
];
937 s32 RF_A1
= state
->m_RF_A1
[RFBand
];
938 s32 RF_B1
= state
->m_RF_B1
[RFBand
];
939 s32 RF_A2
= state
->m_RF_A2
[RFBand
];
940 s32 RF_B2
= state
->m_RF_B2
[RFBand
];
944 state
->m_Regs
[EP3
] &= ~0xE0; /* Power up */
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
;
958 TComp
= (int)(dCoverdT
) * ((int)(TMValue_Current
) - (int)(state
->m_TMValue_RFCal
))/1000;
964 else if (Capprox
> 255)
968 /* TODO Temperature compensation. There is defenitely a scale factor */
969 /* missing in the datasheet, so leave it out for now. */
970 state
->m_Regs
[EB14
] = Capprox
;
972 status
= UpdateReg(state
, EB14
);
980 static int ChannelConfiguration(struct tda_state
*state
,
981 u32 Frequency
, int Standard
)
984 s32 IntermediateFrequency
= m_StandardTable
[Standard
].m_IFFrequency
;
992 state
->IF
= IntermediateFrequency
;
993 /* printk("tda18271c2dd: %s Freq = %d Standard = %d IF = %d\n", __func__, Frequency, Standard, IntermediateFrequency); */
994 /* get values from tables */
996 if (!(SearchMap1(m_BP_Filter_Map
, Frequency
, &BP_Filter
) &&
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__
);
1006 state
->m_Regs
[EP3
] = (state
->m_Regs
[EP3
] & ~0x1F) | m_StandardTable
[Standard
].m_EP3_4_0
;
1007 state
->m_Regs
[EP3
] &= ~0x04; /* switch RFAGC to high speed mode */
1009 /* m_EP4 default for XToutOn, CAL_Mode (0) */
1010 state
->m_Regs
[EP4
] = state
->m_EP4
| ((Standard
> HF_AnalogMax
) ? state
->m_IFLevelDigital
: state
->m_IFLevelAnalog
);
1011 /* state->m_Regs[EP4] = state->m_EP4 | state->m_IFLevelDigital; */
1012 if (Standard
<= HF_AnalogMax
)
1013 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelAnalog
;
1014 else if (Standard
<= HF_ATSC
)
1015 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelDVBT
;
1016 else if (Standard
<= HF_DVBC
)
1017 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelDVBC
;
1019 state
->m_Regs
[EP4
] = state
->m_EP4
| state
->m_IFLevelDigital
;
1021 if ((Standard
== HF_FM_Radio
) && state
->m_bFMInput
)
1022 state
->m_Regs
[EP4
] |= 80;
1024 state
->m_Regs
[MPD
] &= ~0x80;
1025 if (Standard
> HF_AnalogMax
)
1026 state
->m_Regs
[MPD
] |= 0x80; /* Add IF_notch for digital */
1028 state
->m_Regs
[EB22
] = m_StandardTable
[Standard
].m_EB22
;
1030 /* Note: This is missing from flowchart in TDA18271 specification ( 1.5 MHz cutoff for FM ) */
1031 if (Standard
== HF_FM_Radio
)
1032 state
->m_Regs
[EB23
] |= 0x06; /* ForceLP_Fc2_En = 1, LPFc[2] = 1 */
1034 state
->m_Regs
[EB23
] &= ~0x06; /* ForceLP_Fc2_En = 0, LPFc[2] = 0 */
1036 status
= UpdateRegs(state
, EB22
, EB23
);
1040 state
->m_Regs
[EP1
] = (state
->m_Regs
[EP1
] & ~0x07) | 0x40 | BP_Filter
; /* Dis_Power_level = 1, Filter */
1041 state
->m_Regs
[EP5
] = (state
->m_Regs
[EP5
] & ~0x07) | IR_Meas
;
1042 state
->m_Regs
[EP2
] = (RF_Band
<< 5) | GainTaper
;
1044 state
->m_Regs
[EB1
] = (state
->m_Regs
[EB1
] & ~0x07) |
1045 (state
->m_bMaster
? 0x04 : 0x00); /* CALVCO_FortLOn = MS */
1046 /* AGC1_always_master = 0 */
1047 /* AGC_firstn = 0 */
1048 status
= UpdateReg(state
, EB1
);
1052 if (state
->m_bMaster
) {
1053 status
= CalcMainPLL(state
, Frequency
+ IntermediateFrequency
);
1056 status
= UpdateRegs(state
, TM
, EP5
);
1059 state
->m_Regs
[EB4
] |= 0x20; /* LO_forceSrce = 1 */
1060 status
= UpdateReg(state
, EB4
);
1064 state
->m_Regs
[EB4
] &= ~0x20; /* LO_forceSrce = 0 */
1065 status
= UpdateReg(state
, EB4
);
1071 status
= CalcCalPLL(state
, Frequency
+ IntermediateFrequency
);
1075 SearchMap3(m_Cal_PLL_Map
, Frequency
+ IntermediateFrequency
, &PostDiv
, &Div
);
1076 state
->m_Regs
[MPD
] = (state
->m_Regs
[MPD
] & ~0x7F) | (PostDiv
& 0x77);
1077 status
= UpdateReg(state
, MPD
);
1080 status
= UpdateRegs(state
, TM
, EP5
);
1084 state
->m_Regs
[EB7
] |= 0x20; /* CAL_forceSrce = 1 */
1085 status
= UpdateReg(state
, EB7
);
1089 state
->m_Regs
[EB7
] &= ~0x20; /* CAL_forceSrce = 0 */
1090 status
= UpdateReg(state
, EB7
);
1095 if (Standard
!= HF_FM_Radio
)
1096 state
->m_Regs
[EP3
] |= 0x04; /* RFAGC to normal mode */
1097 status
= UpdateReg(state
, EP3
);
1105 static int sleep(struct dvb_frontend
*fe
)
1107 struct tda_state
*state
= fe
->tuner_priv
;
1113 static int init(struct dvb_frontend
*fe
)
1118 static int release(struct dvb_frontend
*fe
)
1120 kfree(fe
->tuner_priv
);
1121 fe
->tuner_priv
= NULL
;
1126 static int set_params(struct dvb_frontend
*fe
)
1128 struct tda_state
*state
= fe
->tuner_priv
;
1131 u32 bw
= fe
->dtv_property_cache
.bandwidth_hz
;
1132 u32 delsys
= fe
->dtv_property_cache
.delivery_system
;
1134 state
->m_Frequency
= fe
->dtv_property_cache
.frequency
;
1141 Standard
= HF_DVBT_6MHZ
;
1144 Standard
= HF_DVBT_7MHZ
;
1147 Standard
= HF_DVBT_8MHZ
;
1152 case SYS_DVBC_ANNEX_A
:
1153 case SYS_DVBC_ANNEX_C
:
1155 Standard
= HF_DVBC_6MHZ
;
1156 else if (bw
<= 7000000)
1157 Standard
= HF_DVBC_7MHZ
;
1159 Standard
= HF_DVBC_8MHZ
;
1165 status
= RFTrackingFiltersCorrection(state
, state
->m_Frequency
);
1168 status
= ChannelConfiguration(state
, state
->m_Frequency
,
1173 msleep(state
->m_SettlingTime
); /* Allow AGC's to settle down */
1179 static int GetSignalStrength(s32
*pSignalStrength
, u32 RFAgc
, u32 IFAgc
)
1182 /* Scale this from 0 to 50000 */
1183 *pSignalStrength
= IFAgc
* 100;
1185 /* Scale range 500-1500 to 50000-80000 */
1186 *pSignalStrength
= 50000 + (IFAgc
- 500) * 30;
1193 static int get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
1195 struct tda_state
*state
= fe
->tuner_priv
;
1197 *frequency
= state
->IF
;
1201 static int get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
1203 /* struct tda_state *state = fe->tuner_priv; */
1204 /* *bandwidth = priv->bandwidth; */
1209 static struct dvb_tuner_ops tuner_ops
= {
1211 .name
= "NXP TDA18271C2D",
1212 .frequency_min
= 47125000,
1213 .frequency_max
= 865000000,
1214 .frequency_step
= 62500
1218 .set_params
= set_params
,
1220 .get_if_frequency
= get_if_frequency
,
1221 .get_bandwidth
= get_bandwidth
,
1224 struct dvb_frontend
*tda18271c2dd_attach(struct dvb_frontend
*fe
,
1225 struct i2c_adapter
*i2c
, u8 adr
)
1227 struct tda_state
*state
;
1229 state
= kzalloc(sizeof(struct tda_state
), GFP_KERNEL
);
1233 fe
->tuner_priv
= state
;
1236 memcpy(&fe
->ops
.tuner_ops
, &tuner_ops
, sizeof(struct dvb_tuner_ops
));
1242 EXPORT_SYMBOL_GPL(tda18271c2dd_attach
);
1244 MODULE_DESCRIPTION("TDA18271C2 driver");
1245 MODULE_AUTHOR("DD");
1246 MODULE_LICENSE("GPL");