| 1 | /* |
|---|
| 2 | * $Id: TunerDTT7611.c,v 1.1 2011/07/08 03:43:59 megakiss Exp $ |
|---|
| 3 | * $Revision: 1.1 $ |
|---|
| 4 | * $DateTime: 2005/08/24 19:03:17 $ |
|---|
| 5 | * $Change: 28028 $ |
|---|
| 6 | * $Author: megakiss $ |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | #include <stdio.h> |
|---|
| 10 | #include "TunerDTT7611.h" |
|---|
| 11 | #include "IicComm.h" |
|---|
| 12 | |
|---|
| 13 | #include "dsthalcommon.h" |
|---|
| 14 | #include "dsthalcfg.h" |
|---|
| 15 | #include "S5H1409Core.h" |
|---|
| 16 | |
|---|
| 17 | #ifndef NIMTN_TYPE |
|---|
| 18 | #error NIMTN_TYPE shall be defined! |
|---|
| 19 | #endif |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #if 1//NIMTN_TYPE==NIMTN_DTT7611 |
|---|
| 23 | |
|---|
| 24 | static BOOLEAN DTT7611SetN(unsigned long ulFrequency,TYPE_TUNERDTT7611_REF_DIVIDER_RATIO iRefDividerRatio, unsigned char* pucDb1,unsigned char* pucDb2); |
|---|
| 25 | static void DTT7611SetCP(unsigned int uiChargePump,unsigned char* pucCb); |
|---|
| 26 | static void DTT7611SetStepSize(TYPE_TUNERDTT7611_REF_DIVIDER_RATIO iRefDividerRatio, unsigned char* pucCb,unsigned char* pucCb2); |
|---|
| 27 | static void DTT7611SetAGC(unsigned int uiAgc, unsigned char* pucAb); |
|---|
| 28 | static void DTT7611SetATC(unsigned int uiAgcTimeConstant,unsigned char* pucAb); |
|---|
| 29 | static unsigned int DTT7611SendControlData(unsigned int uiAgcMode,unsigned char Db1,unsigned char Db2,unsigned char Cb,unsigned char Bb,unsigned char Ab,unsigned char Cb2); |
|---|
| 30 | static BOOLEAN TunerDTT7611BeforeSendBytes(void); |
|---|
| 31 | static BOOLEAN TunerDTT7611AfterSendBytes(void); |
|---|
| 32 | static void DTT7611InitBytes(unsigned char* pucDb1,unsigned char* pucDb2,unsigned char* pucCb,unsigned char* pucBb,unsigned char* pucAb,unsigned char* pucCb2); |
|---|
| 33 | |
|---|
| 34 | extern UINT32 gdhlExtDbgLvl; |
|---|
| 35 | |
|---|
| 36 | #undef DTT7611_DEVICE_ADDRESS_WRITE |
|---|
| 37 | #define DTT7611_DEVICE_ADDRESS_WRITE 0xC2 |
|---|
| 38 | extern int S5H1409_IicTransmit(unsigned char chipAddr, unsigned char *pBuffer, int len ); |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | #define TRACE_DTT7611_DEBUG 0 |
|---|
| 42 | |
|---|
| 43 | #define SOUND_TRAP_BYPASS 1 |
|---|
| 44 | |
|---|
| 45 | int Tuner_DTT7611SendFreq(unsigned long ulFrequency,TYPE_TUNERDTT7611_REF_DIVIDER_RATIO iRefDividerRatio,unsigned int uiAgc, unsigned int uiAgcMode,unsigned int uiChargePump,unsigned int uiAgcTimeConstant) |
|---|
| 46 | { |
|---|
| 47 | unsigned char Db1; |
|---|
| 48 | unsigned char Db2; |
|---|
| 49 | unsigned char Cb; |
|---|
| 50 | unsigned char Bb; |
|---|
| 51 | unsigned char Ab; |
|---|
| 52 | unsigned char Cb2; |
|---|
| 53 | |
|---|
| 54 | DTT7611InitBytes(&Db1,&Db2,&Cb,&Bb,&Ab,&Cb2); |
|---|
| 55 | |
|---|
| 56 | printf("Freq=%ld, ref=%d, uiAgc=%d, AgcMode=%d, Charge=%d, ATC=%d\n", ulFrequency, iRefDividerRatio, uiAgc, uiAgcMode, uiChargePump, uiAgcTimeConstant ); |
|---|
| 57 | |
|---|
| 58 | if(FALSE == Tuner_DTT7611SetRFFreq(ulFrequency,uiAgcMode,&Bb)) |
|---|
| 59 | return DTT7611_FREQUENCY_VALUE_OUT_OF_RANGE; |
|---|
| 60 | |
|---|
| 61 | if(FALSE == DTT7611SetN( ulFrequency,iRefDividerRatio, &Db1,&Db2)) |
|---|
| 62 | return DTT7611_COULD_NOT_SET_N; |
|---|
| 63 | |
|---|
| 64 | DTT7611SetCP(uiChargePump,&Cb); |
|---|
| 65 | DTT7611SetStepSize( iRefDividerRatio, &Cb,&Cb2); |
|---|
| 66 | DTT7611SetAGC(uiAgc,&Ab); |
|---|
| 67 | DTT7611SetATC(uiAgcTimeConstant,&Ab); |
|---|
| 68 | |
|---|
| 69 | return DTT7611SendControlData(uiAgcMode,Db1,Db2,Cb,Bb,Ab,Cb2); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | BOOLEAN Tuner_DTT7611SetRFFreq(unsigned long ulFrequency,unsigned int uiAgcMode,unsigned char* pucBb) |
|---|
| 73 | { |
|---|
| 74 | *pucBb = 0; |
|---|
| 75 | if ((ulFrequency >= DTT7611_LOW_BAND_MIN) && (ulFrequency <= DTT7611_LOW_BAND_MAX) ) { |
|---|
| 76 | if(1 == uiAgcMode){ |
|---|
| 77 | *pucBb = 0x39; |
|---|
| 78 | } |
|---|
| 79 | else if(2 == uiAgcMode){ |
|---|
| 80 | *pucBb = 0x11; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | else if((ulFrequency >= DTT7611_MID_BAND_MIN) && (ulFrequency <= DTT7611_MID_BAND_MAX)){ |
|---|
| 84 | if(1 == uiAgcMode){ |
|---|
| 85 | *pucBb = 0x3a; |
|---|
| 86 | } |
|---|
| 87 | else if(2 == uiAgcMode){ |
|---|
| 88 | *pucBb = 0x12; |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | else if((ulFrequency >= DTT7611_HIGH_BAND_MIN) && (ulFrequency <= DTT7611_HIGH_BAND_MAX)){ |
|---|
| 92 | if(1 == uiAgcMode){ |
|---|
| 93 | *pucBb = 0x3c; |
|---|
| 94 | } |
|---|
| 95 | else if(2 == uiAgcMode){ |
|---|
| 96 | *pucBb = 0x14; |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | else |
|---|
| 100 | return FALSE; |
|---|
| 101 | return TRUE; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | static BOOLEAN DTT7611SetN(unsigned long ulFrequency,TYPE_TUNERDTT7611_REF_DIVIDER_RATIO iRefDividerRatio, unsigned char* pucDb1,unsigned char* pucDb2) |
|---|
| 105 | { |
|---|
| 106 | // calc N |
|---|
| 107 | unsigned short usNval,usMultiplier=0; |
|---|
| 108 | unsigned char ucNlow8,ucNbits8_14; |
|---|
| 109 | |
|---|
| 110 | //1/(dRefDividerRatio/1000) |
|---|
| 111 | switch(iRefDividerRatio){ |
|---|
| 112 | case TUNERDTT7611_REF_DIVIDER_RATIO_31_25: |
|---|
| 113 | usMultiplier = 32; |
|---|
| 114 | break; |
|---|
| 115 | case TUNERDTT7611_REF_DIVIDER_RATIO_50: |
|---|
| 116 | usMultiplier = 20; |
|---|
| 117 | break; |
|---|
| 118 | case TUNERDTT7611_REF_DIVIDER_RATIO_62_5: |
|---|
| 119 | usMultiplier = 16; |
|---|
| 120 | break; |
|---|
| 121 | case TUNERDTT7611_REF_DIVIDER_RATIO_166_7: |
|---|
| 122 | usMultiplier = 6; |
|---|
| 123 | break; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | usNval = (unsigned short)(((ulFrequency + DTT7611_F_IF_KHZ) * usMultiplier)/1000); |
|---|
| 127 | if(usNval >= DTT7611_N_MAX){ |
|---|
| 128 | //max n in 14 bits |
|---|
| 129 | usNval = usNval/2 ; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | // set N |
|---|
| 133 | |
|---|
| 134 | if (usNval == 0 || usNval >= DTT7611_N_MAX) { //15 bits |
|---|
| 135 | return FALSE; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | // clear N bits |
|---|
| 139 | *pucDb1 = 0x00; |
|---|
| 140 | *pucDb2 = 0x00; |
|---|
| 141 | |
|---|
| 142 | ucNlow8 = usNval & 0x00FF; |
|---|
| 143 | ucNbits8_14 = (usNval & 0x07f00) >> 8; |
|---|
| 144 | |
|---|
| 145 | *pucDb2 = ucNlow8; |
|---|
| 146 | *pucDb1 |= ucNbits8_14 ; |
|---|
| 147 | |
|---|
| 148 | return TRUE; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | static void DTT7611SetCP(unsigned int uiChargePump, unsigned char* pucCb) |
|---|
| 152 | { |
|---|
| 153 | *pucCb &= 0xbf; |
|---|
| 154 | if(60 == uiChargePump){ |
|---|
| 155 | *pucCb &= 0xbf; |
|---|
| 156 | } |
|---|
| 157 | else{//280 |
|---|
| 158 | *pucCb |= 0x40; |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | static void DTT7611SetStepSize(TYPE_TUNERDTT7611_REF_DIVIDER_RATIO iRefDividerRatio, unsigned char* pucCb,unsigned char* pucCb2) |
|---|
| 163 | { |
|---|
| 164 | *pucCb &= 0xf9; |
|---|
| 165 | *pucCb2 &= 0xf9; |
|---|
| 166 | if (TUNERDTT7611_REF_DIVIDER_RATIO_50 == iRefDividerRatio) { |
|---|
| 167 | *pucCb &= 0xf9; |
|---|
| 168 | *pucCb2 &= 0xf9; |
|---|
| 169 | } |
|---|
| 170 | else if (TUNERDTT7611_REF_DIVIDER_RATIO_31_25 == iRefDividerRatio) { |
|---|
| 171 | *pucCb |= 0x2; |
|---|
| 172 | *pucCb2 |= 0x2; |
|---|
| 173 | } |
|---|
| 174 | else if (TUNERDTT7611_REF_DIVIDER_RATIO_166_7 == iRefDividerRatio) { |
|---|
| 175 | *pucCb |= 0x4; |
|---|
| 176 | *pucCb2 |= 0x4; |
|---|
| 177 | } |
|---|
| 178 | else if (TUNERDTT7611_REF_DIVIDER_RATIO_62_5 == iRefDividerRatio) { |
|---|
| 179 | *pucCb |= 0x6; |
|---|
| 180 | *pucCb2 |= 0x6; |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | static void DTT7611SetAGC(unsigned int uiAgc, unsigned char* pucAb) |
|---|
| 185 | { |
|---|
| 186 | *pucAb &= 0x8f; |
|---|
| 187 | |
|---|
| 188 | if (0 == uiAgc) { |
|---|
| 189 | *pucAb &= 0x8f; |
|---|
| 190 | } |
|---|
| 191 | else if (1== uiAgc) { |
|---|
| 192 | *pucAb |= 0x10; |
|---|
| 193 | } |
|---|
| 194 | else if (2 == uiAgc) { |
|---|
| 195 | *pucAb |= 0x20; |
|---|
| 196 | } |
|---|
| 197 | else if (3 == uiAgc) { |
|---|
| 198 | *pucAb |= 0x30; |
|---|
| 199 | } |
|---|
| 200 | else if (4 == uiAgc) { |
|---|
| 201 | *pucAb |= 0x40; |
|---|
| 202 | } |
|---|
| 203 | else if (5 == uiAgc) { |
|---|
| 204 | *pucAb |= 0x50; |
|---|
| 205 | } |
|---|
| 206 | else if (6 == uiAgc) { |
|---|
| 207 | *pucAb |= 0x60; |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | static void DTT7611SetATC(unsigned int uiAgcTimeConstant,unsigned char* pucAb) |
|---|
| 212 | { |
|---|
| 213 | if (2 == uiAgcTimeConstant) |
|---|
| 214 | *pucAb &= ~0x80; |
|---|
| 215 | else if (50== uiAgcTimeConstant) |
|---|
| 216 | *pucAb |= 0x80; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | static BOOLEAN TunerDTT7611BeforeSendBytes() |
|---|
| 220 | { |
|---|
| 221 | if ( Sdm_1409xRepeaterEnable() ) |
|---|
| 222 | return 0; |
|---|
| 223 | |
|---|
| 224 | return 1; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | static BOOLEAN TunerDTT7611AfterSendBytes() |
|---|
| 228 | { |
|---|
| 229 | if ( Sdm_1409xRepeaterDisable() ) |
|---|
| 230 | return 0; |
|---|
| 231 | |
|---|
| 232 | return 1; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | static unsigned int DTT7611SendControlData(unsigned int uiAgcMode,unsigned char Db1,unsigned char Db2,unsigned char Cb,unsigned char Bb,unsigned char Ab,unsigned char Cb2) |
|---|
| 237 | { |
|---|
| 238 | unsigned char str[10]; |
|---|
| 239 | #if 0 |
|---|
| 240 | unsigned long ulWait = 1000; |
|---|
| 241 | #endif |
|---|
| 242 | |
|---|
| 243 | //open 2nd IIC channel |
|---|
| 244 | if( !TunerDTT7611BeforeSendBytes()) |
|---|
| 245 | return DTT7611_BEFORE_SEND_BYTES_FAILED; |
|---|
| 246 | |
|---|
| 247 | str[0] = Db1; |
|---|
| 248 | str[1] = Db2; |
|---|
| 249 | str[2] = Cb; |
|---|
| 250 | str[3] = Bb; |
|---|
| 251 | str[4]= '\0'; |
|---|
| 252 | |
|---|
| 253 | if (S5H1409_IicTransmit(DTT7611_DEVICE_ADDRESS_WRITE, str, 4)) { |
|---|
| 254 | return DTT7611_SEND_CONTROL_DATA_FAILED; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | if(1 == uiAgcMode){ |
|---|
| 258 | //wait |
|---|
| 259 | #if 0 |
|---|
| 260 | while(ulWait != 0) |
|---|
| 261 | ulWait --; |
|---|
| 262 | #else |
|---|
| 263 | OS_mDelay(1); /* 1 ms */ |
|---|
| 264 | #endif |
|---|
| 265 | |
|---|
| 266 | str[0] = Cb2; |
|---|
| 267 | str[1] = Ab; |
|---|
| 268 | str[2]= '\0'; |
|---|
| 269 | if (S5H1409_IicTransmit(DTT7611_DEVICE_ADDRESS_WRITE, str, 2)) { |
|---|
| 270 | return DTT7611_SEND_CONTROL_DATA_FAILED; |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | if(2 == uiAgcMode){ |
|---|
| 274 | //wait |
|---|
| 275 | #if 0 |
|---|
| 276 | while(ulWait != 0) |
|---|
| 277 | ulWait --; |
|---|
| 278 | #else |
|---|
| 279 | OS_mDelay(1); /* 1 ms */ |
|---|
| 280 | #endif |
|---|
| 281 | |
|---|
| 282 | str[0] = Cb2; |
|---|
| 283 | str[1] = 0xE0; |
|---|
| 284 | str[2]= '\0'; |
|---|
| 285 | if (S5H1409_IicTransmit(DTT7611_DEVICE_ADDRESS_WRITE, str, 2)) { |
|---|
| 286 | return DTT7611_SEND_CONTROL_DATA_FAILED; |
|---|
| 287 | } |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | //Send data byte if internal analog AGC mode. |
|---|
| 292 | if(2 == uiAgcMode){ |
|---|
| 293 | str[0] = 0x00; |
|---|
| 294 | #if SOUND_TRAP_BYPASS |
|---|
| 295 | str[1] = 0xD5; |
|---|
| 296 | #else |
|---|
| 297 | str[1] = 0xD4; |
|---|
| 298 | #endif |
|---|
| 299 | |
|---|
| 300 | if (S5H1409_IicTransmit(DTT7611_SLAVE_ADDRESS, str, 2)) { |
|---|
| 301 | return DTT7611_SEND_CONTROL_DATA_FAILED; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | str[0] = 0x01; |
|---|
| 305 | str[1] = 0x30; |
|---|
| 306 | if (S5H1409_IicTransmit(DTT7611_SLAVE_ADDRESS, str, 2)) { |
|---|
| 307 | return DTT7611_SEND_CONTROL_DATA_FAILED; |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | str[0] = 0x02; |
|---|
| 311 | str[1] = 0x04; |
|---|
| 312 | if (S5H1409_IicTransmit(DTT7611_SLAVE_ADDRESS, str, 2)) { |
|---|
| 313 | return DTT7611_SEND_CONTROL_DATA_FAILED; |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | //Send data byte if internal digital AGC mode. |
|---|
| 318 | if(1 == uiAgcMode){ |
|---|
| 319 | str[0] = 0x00; |
|---|
| 320 | #if SOUND_TRAP_BYPASS |
|---|
| 321 | str[1] = 0xD5; |
|---|
| 322 | #else |
|---|
| 323 | str[1] = 0xD4; |
|---|
| 324 | #endif |
|---|
| 325 | if (S5H1409_IicTransmit(DTT7611_SLAVE_ADDRESS, str, 2)) { |
|---|
| 326 | return DTT7611_SEND_CONTROL_DATA_FAILED; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | str[0] = 0x01; |
|---|
| 330 | str[1] = 0x30; |
|---|
| 331 | if (S5H1409_IicTransmit(DTT7611_SLAVE_ADDRESS, str, 2)) { |
|---|
| 332 | return DTT7611_SEND_CONTROL_DATA_FAILED; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | str[0] = 0x02; |
|---|
| 336 | str[1] = 0x24; |
|---|
| 337 | if (S5H1409_IicTransmit(DTT7611_SLAVE_ADDRESS, str, 2)) { |
|---|
| 338 | return DTT7611_SEND_CONTROL_DATA_FAILED; |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | //close 2nd IIC channel |
|---|
| 342 | if( !TunerDTT7611AfterSendBytes()) |
|---|
| 343 | return DTT7611_AFTER_SEND_BYTES_FAILED; |
|---|
| 344 | else |
|---|
| 345 | return DTT7611_SUCCESS; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | static void DTT7611InitBytes(unsigned char* pucDb1,unsigned char* pucDb2,unsigned char* pucCb,unsigned char* pucBb,unsigned char* pucAb,unsigned char* pucCb2) |
|---|
| 349 | { |
|---|
| 350 | *pucDb1 = 0; |
|---|
| 351 | *pucDb2 = 0; |
|---|
| 352 | *pucCb = 0x88; |
|---|
| 353 | *pucBb = 0; |
|---|
| 354 | *pucAb = 0; |
|---|
| 355 | *pucCb2 = 0x98; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | int Tuner_GetTDA9885(void) |
|---|
| 359 | { |
|---|
| 360 | int stat; |
|---|
| 361 | |
|---|
| 362 | if ( TunerDTT7611BeforeSendBytes() == 0 ) |
|---|
| 363 | return -1; |
|---|
| 364 | |
|---|
| 365 | stat = S5H_IicReadOneRegister( DTT7611_SLAVE_ADDRESS, 0x00 ); |
|---|
| 366 | //printf("|%s| stat=0x%x\n", __FUNCTION__, stat); |
|---|
| 367 | |
|---|
| 368 | if ( TunerDTT7611AfterSendBytes() == 0 ) |
|---|
| 369 | return -1; |
|---|
| 370 | |
|---|
| 371 | return stat; |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | #if 0 |
|---|
| 375 | unsigned int DTT7611GetTunerStatusReg(unsigned char * pucStatusReg) |
|---|
| 376 | { |
|---|
| 377 | |
|---|
| 378 | unsigned int uiError; |
|---|
| 379 | #if 0 |
|---|
| 380 | unsigned long ulWait = 1000; |
|---|
| 381 | #endif |
|---|
| 382 | |
|---|
| 383 | if(NULL == pucStatusReg) |
|---|
| 384 | return FUNCTION_RECEIVED_NULL_PARAMETER; |
|---|
| 385 | |
|---|
| 386 | *pucStatusReg = 0; |
|---|
| 387 | |
|---|
| 388 | //open 2nd IIC channel |
|---|
| 389 | if( !TunerDTT7611BeforeSendBytes()){ |
|---|
| 390 | return DTT7611_BEFORE_SEND_BYTES_FAILED; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | //wait |
|---|
| 394 | #if 0 |
|---|
| 395 | while(ulWait != 0) |
|---|
| 396 | ulWait --; |
|---|
| 397 | #else |
|---|
| 398 | OS_mDelay(1); /* 1 ms */ |
|---|
| 399 | #endif |
|---|
| 400 | |
|---|
| 401 | *pucStatusReg = ReceiveByteOfDataFromAddress(DTT7611_SLAVE_ADDRESS,&uiError); |
|---|
| 402 | |
|---|
| 403 | //close 2nd IIC channel |
|---|
| 404 | if( !TunerDTT7611AfterSendBytes()) |
|---|
| 405 | return DTT7611_AFTER_SEND_BYTES_FAILED; |
|---|
| 406 | |
|---|
| 407 | return uiError; |
|---|
| 408 | } |
|---|
| 409 | #endif |
|---|
| 410 | |
|---|
| 411 | #endif//#if NIMTN_TYPE==NIMTN_DTT7611 |
|---|