fc8150_tun.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*****************************************************************************
  2. Copyright(c) 2012 FCI Inc. All Rights Reserved
  3. File name : fc8150_tun.c (BGA & QFN)
  4. Description : fc8150 tuner driver
  5. History :
  6. ----------------------------------------------------------------------
  7. 2012/01/20 initial 0.1 version
  8. 2012/01/25 initial 0.3 version
  9. 2012/01/27 initial 0.5 version
  10. 2012/01/31 initial 1.0 version
  11. 2012/01/31 initial 1.1 version
  12. 2012/02/06 initial 1.2 version
  13. 2012/02/09 initial 1.3 Version
  14. 2012/02/15 initial 1.4 Version
  15. 2012/02/15 initial 2.0 Version
  16. 2012/02/24 initial 2.01 Version
  17. 2012/03/30 initial 3.0 Version
  18. 2012/06/07 pre SLR Version
  19. 2012/06/11 pre SLR Version
  20. 2012/06/15
  21. 2012/06/17 SLR 0.3 version
  22. 2012/06/19 SLR 0.4 version
  23. 2012/06/20
  24. 2012/07/04
  25. 2012/07/09
  26. 2012/07/10
  27. 2012/07/15
  28. 2012/07/16
  29. 2012/07/16
  30. 2012/07/30
  31. 2012/08/02
  32. 2012/08/10
  33. 2012/08/13
  34. *******************************************************************************/
  35. #include "fci_types.h"
  36. #include "fci_oal.h"
  37. #include "fci_tun.h"
  38. #include "fc8150_regs.h"
  39. #include "fci_hal.h"
  40. #define FC8150_FREQ_XTAL BBM_XTAL_FREQ
  41. static int fc8150_write(HANDLE hDevice, u8 addr, u8 data)
  42. {
  43. int res;
  44. u8 tmp;
  45. tmp = data;
  46. res = tuner_i2c_write(hDevice, addr, 1, &tmp, 1);
  47. return res;
  48. }
  49. static int fc8150_read(HANDLE hDevice, u8 addr, u8 *data)
  50. {
  51. int res;
  52. res = tuner_i2c_read(hDevice, addr, 1, data, 1);
  53. return res;
  54. }
  55. static int fc8150_bb_read(HANDLE hDevice, u16 addr, u8 *data)
  56. {
  57. int res;
  58. res = bbm_read(hDevice, addr, data);
  59. return res;
  60. }
  61. #if 0
  62. static int fc8150_bb_write(HANDLE hDevice, u16 addr, u8 data)
  63. {
  64. int res;
  65. res = bbm_write(hDevice, addr, data);
  66. return res;
  67. }
  68. #endif
  69. static int fc8150_set_filter(HANDLE hDevice)
  70. {
  71. int i;
  72. u8 cal_mon = 0;
  73. #if (FC8150_FREQ_XTAL == 16000)
  74. fc8150_write(hDevice, 0x3B, 0x01);
  75. fc8150_write(hDevice, 0x3D, 0x20);
  76. fc8150_write(hDevice, 0x3B, 0x00);
  77. #elif (FC8150_FREQ_XTAL == 16384)
  78. fc8150_write(hDevice, 0x3B, 0x01);
  79. fc8150_write(hDevice, 0x3D, 0x21);
  80. fc8150_write(hDevice, 0x3B, 0x00);
  81. #elif (FC8150_FREQ_XTAL == 18000)
  82. fc8150_write(hDevice, 0x3B, 0x01);
  83. fc8150_write(hDevice, 0x3D, 0x24);
  84. fc8150_write(hDevice, 0x3B, 0x00);
  85. #elif (FC8150_FREQ_XTAL == 19200)
  86. fc8150_write(hDevice, 0x3B, 0x01);
  87. fc8150_write(hDevice, 0x3D, 0x26);
  88. fc8150_write(hDevice, 0x3B, 0x00);
  89. #elif (FC8150_FREQ_XTAL == 24000)
  90. fc8150_write(hDevice, 0x3B, 0x01);
  91. fc8150_write(hDevice, 0x3D, 0x30);
  92. fc8150_write(hDevice, 0x3B, 0x00);
  93. #elif (FC8150_FREQ_XTAL == 24576)
  94. fc8150_write(hDevice, 0x3B, 0x01);
  95. fc8150_write(hDevice, 0x3D, 0x31);
  96. fc8150_write(hDevice, 0x3B, 0x00);
  97. #elif (FC8150_FREQ_XTAL == 26000)
  98. fc8150_write(hDevice, 0x3B, 0x01);
  99. fc8150_write(hDevice, 0x3D, 0x34);
  100. fc8150_write(hDevice, 0x3B, 0x00);
  101. #elif (FC8150_FREQ_XTAL == 27000)
  102. fc8150_write(hDevice, 0x3B, 0x01);
  103. fc8150_write(hDevice, 0x3D, 0x36);
  104. fc8150_write(hDevice, 0x3B, 0x00);
  105. #elif (FC8150_FREQ_XTAL == 27120)
  106. fc8150_write(hDevice, 0x3B, 0x01);
  107. fc8150_write(hDevice, 0x3D, 0x36);
  108. fc8150_write(hDevice, 0x3B, 0x00);
  109. #elif (FC8150_FREQ_XTAL == 32000)
  110. fc8150_write(hDevice, 0x3B, 0x01);
  111. fc8150_write(hDevice, 0x3D, 0x40);
  112. fc8150_write(hDevice, 0x3B, 0x00);
  113. #elif (FC8150_FREQ_XTAL == 37400)
  114. fc8150_write(hDevice, 0x3B, 0x01);
  115. fc8150_write(hDevice, 0x3D, 0x4B);
  116. fc8150_write(hDevice, 0x3B, 0x00);
  117. #elif (FC8150_FREQ_XTAL == 38400)
  118. fc8150_write(hDevice, 0x3B, 0x01);
  119. fc8150_write(hDevice, 0x3D, 0x4D);
  120. fc8150_write(hDevice, 0x3B, 0x00);
  121. #else
  122. return BBM_NOK;
  123. #endif
  124. for (i = 0; i < 10; i++) {
  125. msWait(5);
  126. fc8150_read(hDevice, 0x33, &cal_mon);
  127. if ((cal_mon & 0xC0) == 0xC0)
  128. break;
  129. fc8150_write(hDevice, 0x32, 0x01);
  130. fc8150_write(hDevice, 0x32, 0x09);
  131. }
  132. fc8150_write(hDevice, 0x32, 0x01);
  133. return BBM_OK;
  134. }
  135. int fc8150_tuner_init(HANDLE hDevice, u32 band)
  136. {
  137. int i;
  138. int n_RFAGC_PD1_AVG, n_RFAGC_PD2_AVG;
  139. u8 RFPD_REF;
  140. u8 RFAGC_PD2[6], RFAGC_PD2_AVG, RFAGC_PD2_MAX, RFAGC_PD2_MIN;
  141. u8 RFAGC_PD1[6], RFAGC_PD1_AVG, RFAGC_PD1_MAX, RFAGC_PD1_MIN;
  142. PRINTF(hDevice, "fc8150_tuner_init\n");
  143. fc8150_write(hDevice, 0x00, 0x00);
  144. fc8150_write(hDevice, 0x02, 0x81);
  145. fc8150_write(hDevice, 0x15, 0x02);
  146. fc8150_write(hDevice, 0x20, 0x33);
  147. fc8150_write(hDevice, 0x28, 0x62);
  148. fc8150_write(hDevice, 0x35, 0xAA);
  149. fc8150_write(hDevice, 0x38, 0x28);
  150. fc8150_write(hDevice, 0x3B, 0x01);
  151. fc8150_set_filter(hDevice);
  152. fc8150_write(hDevice, 0x3B, 0x00);
  153. fc8150_write(hDevice, 0x56, 0x01);
  154. fc8150_write(hDevice, 0x57, 0x86);
  155. fc8150_write(hDevice, 0x58, 0xA7);
  156. fc8150_write(hDevice, 0x59, 0x4D);
  157. fc8150_write(hDevice, 0x80, 0x17);
  158. fc8150_write(hDevice, 0xAB, 0x48);
  159. fc8150_write(hDevice, 0xA0, 0xC0);
  160. fc8150_write(hDevice, 0xD0, 0x00);
  161. RFAGC_PD1[0] = 0;
  162. RFAGC_PD1[1] = 0;
  163. RFAGC_PD1[2] = 0;
  164. RFAGC_PD1[3] = 0;
  165. RFAGC_PD1[4] = 0;
  166. RFAGC_PD1[5] = 0;
  167. RFAGC_PD1_MAX = 0;
  168. RFAGC_PD1_MIN = 255;
  169. for (i = 0; i < 6 ; i++) {
  170. fc8150_read(hDevice, 0xD8 , &RFAGC_PD1[i]);
  171. if (RFAGC_PD1[i] >= RFAGC_PD1_MAX)
  172. RFAGC_PD1_MAX = RFAGC_PD1[i];
  173. if (RFAGC_PD1[i] <= RFAGC_PD1_MIN)
  174. RFAGC_PD1_MIN = RFAGC_PD1[i];
  175. }
  176. n_RFAGC_PD1_AVG = (RFAGC_PD1[0] + RFAGC_PD1[1] + RFAGC_PD1[2]
  177. + RFAGC_PD1[3] + RFAGC_PD1[4] + RFAGC_PD1[5]
  178. - RFAGC_PD1_MAX - RFAGC_PD1_MIN) / 4;
  179. RFAGC_PD1_AVG = (unsigned char) n_RFAGC_PD1_AVG;
  180. fc8150_write(hDevice, 0x7F , RFAGC_PD1_AVG);
  181. RFAGC_PD2[0] = 0;
  182. RFAGC_PD2[1] = 0;
  183. RFAGC_PD2[2] = 0;
  184. RFAGC_PD2[3] = 0;
  185. RFAGC_PD2[4] = 0;
  186. RFAGC_PD2[5] = 0;
  187. RFAGC_PD2_MAX = 0;
  188. RFAGC_PD2_MIN = 255;
  189. for (i = 0; i < 6; i++) {
  190. fc8150_read(hDevice, 0xD6, &RFAGC_PD2[i]);
  191. if (RFAGC_PD2[i] >= RFAGC_PD2_MAX)
  192. RFAGC_PD2_MAX = RFAGC_PD2[i];
  193. if (RFAGC_PD2[i] <= RFAGC_PD2_MIN)
  194. RFAGC_PD2_MIN = RFAGC_PD2[i];
  195. }
  196. n_RFAGC_PD2_AVG = (RFAGC_PD2[0] + RFAGC_PD2[1] + RFAGC_PD2[2]
  197. + RFAGC_PD2[3] + RFAGC_PD2[4] + RFAGC_PD2[5]
  198. - RFAGC_PD2_MAX - RFAGC_PD2_MIN) / 4;
  199. RFAGC_PD2_AVG = (unsigned char) n_RFAGC_PD2_AVG;
  200. fc8150_write(hDevice, 0x7E , RFAGC_PD2_AVG);
  201. fc8150_read(hDevice, 0xD6, &RFPD_REF);
  202. if (0x80 <= RFPD_REF)
  203. fc8150_write(hDevice, 0x7B, 0x8F);
  204. else if (RFPD_REF < 0x80)
  205. fc8150_write(hDevice, 0x7B, 0x88);
  206. fc8150_write(hDevice, 0x79, 0x32);
  207. fc8150_write(hDevice, 0x7A, 0x2C);
  208. fc8150_write(hDevice, 0x7C, 0x12);
  209. fc8150_write(hDevice, 0x7D, 0x0C);
  210. fc8150_write(hDevice, 0x81, 0x0A);
  211. fc8150_write(hDevice, 0x84, 0x00);
  212. fc8150_write(hDevice, 0x92, 0x00);
  213. fc8150_write(hDevice, 0x93, 0x0C);
  214. fc8150_write(hDevice, 0xA5, 0x69);
  215. fc8150_write(hDevice, 0xA6, 0x25);
  216. fc8150_write(hDevice, 0x02, 0x81);
  217. return BBM_OK;
  218. }
  219. int fc8150_set_freq(HANDLE hDevice, enum band_type band, u32 rf_kHz)
  220. {
  221. unsigned long f_diff, f_diff_shifted, n_val, k_val;
  222. unsigned long f_vco, f_comp;
  223. unsigned char r_val, data_0x56;
  224. unsigned char pre_shift_bits = 4;
  225. f_vco = (rf_kHz) << 2;
  226. if (f_vco < FC8150_FREQ_XTAL*40)
  227. r_val = 2;
  228. else
  229. r_val = 1;
  230. f_comp = FC8150_FREQ_XTAL / r_val;
  231. n_val = f_vco / f_comp;
  232. f_diff = f_vco - f_comp * n_val;
  233. f_diff_shifted = f_diff << (20 - pre_shift_bits);
  234. k_val = (f_diff_shifted) / (f_comp >> pre_shift_bits);
  235. k_val = (k_val | 1);
  236. if (470000 < rf_kHz && rf_kHz <= 473143) {
  237. fc8150_write(hDevice, 0x1E, 0x04);
  238. fc8150_write(hDevice, 0x1F, 0x36);
  239. fc8150_write(hDevice, 0x14, 0x84);
  240. } else if (473143 < rf_kHz && rf_kHz <= 485143) {
  241. fc8150_write(hDevice, 0x1E, 0x03);
  242. fc8150_write(hDevice, 0x1F, 0x3E);
  243. fc8150_write(hDevice, 0x14, 0x84);
  244. } else if (485143 < rf_kHz && rf_kHz <= 551143) {
  245. fc8150_write(hDevice, 0x1E, 0x04);
  246. fc8150_write(hDevice, 0x1F, 0x36);
  247. fc8150_write(hDevice, 0x14, 0x84);
  248. } else if (551143 < rf_kHz && rf_kHz <= 557143) {
  249. fc8150_write(hDevice, 0x1E, 0x04);
  250. fc8150_write(hDevice, 0x1F, 0x3E);
  251. fc8150_write(hDevice, 0x14, 0x84);
  252. } else if (557143 < rf_kHz && rf_kHz <= 563143) {
  253. fc8150_write(hDevice, 0x1E, 0x03);
  254. fc8150_write(hDevice, 0x1F, 0x3E);
  255. fc8150_write(hDevice, 0x14, 0x84);
  256. } else if (563143 < rf_kHz && rf_kHz <= 593143) {
  257. fc8150_write(hDevice, 0x1E, 0x04);
  258. fc8150_write(hDevice, 0x1F, 0x3E);
  259. fc8150_write(hDevice, 0x14, 0x84);
  260. } else if (593143 < rf_kHz && rf_kHz <= 659143) {
  261. fc8150_write(hDevice, 0x1E, 0x02);
  262. fc8150_write(hDevice, 0x1F, 0x36);
  263. fc8150_write(hDevice, 0x14, 0x84);
  264. } else if (659143 < rf_kHz && rf_kHz <= 767143) {
  265. fc8150_write(hDevice, 0x1E, 0x01);
  266. fc8150_write(hDevice, 0x1F, 0x36);
  267. fc8150_write(hDevice, 0x14, 0x84);
  268. } else if (767143 < rf_kHz) {
  269. fc8150_write(hDevice, 0x1E, 0x00);
  270. fc8150_write(hDevice, 0x1F, 0x36);
  271. fc8150_write(hDevice, 0x14, 0x84);
  272. } else {
  273. fc8150_write(hDevice, 0x1E, 0x05);
  274. fc8150_write(hDevice, 0x1F, 0x36);
  275. fc8150_write(hDevice, 0x14, 0x84);
  276. }
  277. data_0x56 = ((r_val == 1) ? 0 : 0x10) + (unsigned char)(k_val>>16);
  278. fc8150_write(hDevice, 0x56, data_0x56);
  279. fc8150_write(hDevice, 0x57, (unsigned char)((k_val>>8)&0xFF));
  280. fc8150_write(hDevice, 0x58, (unsigned char)(((k_val)&0xFF)));
  281. fc8150_write(hDevice, 0x59, (unsigned char) n_val);
  282. if (rf_kHz < 525000)
  283. fc8150_write(hDevice, 0x55, 0x0E);
  284. else if (525000 <= rf_kHz && rf_kHz < 600000)
  285. fc8150_write(hDevice, 0x55, 0x0C);
  286. else if (600000 <= rf_kHz && rf_kHz < 700000)
  287. fc8150_write(hDevice, 0x55, 0x08);
  288. else if (700000 < rf_kHz)
  289. fc8150_write(hDevice, 0x55, 0x06);
  290. if (rf_kHz <= 491143) {
  291. fc8150_write(hDevice, 0x79, 0x2a);
  292. fc8150_write(hDevice, 0x7A, 0x26);
  293. } else if (491143 < rf_kHz && rf_kHz <= 659143) {
  294. fc8150_write(hDevice, 0x79, 0x2A);
  295. fc8150_write(hDevice, 0x7A, 0x26);
  296. } else if (659143 < rf_kHz && rf_kHz <= 773143) {
  297. fc8150_write(hDevice, 0x79, 0x2C);
  298. fc8150_write(hDevice, 0x7A, 0x28);
  299. } else if (773143 < rf_kHz) {
  300. fc8150_write(hDevice, 0x79, 0x2F);
  301. fc8150_write(hDevice, 0x7A, 0x2B);
  302. }
  303. if (rf_kHz <= 707143) {
  304. fc8150_write(hDevice, 0x54, 0x01);
  305. fc8150_write(hDevice, 0x53, 0x9F);
  306. } else if (707143 < rf_kHz) {
  307. fc8150_write(hDevice, 0x54, 0x04);
  308. fc8150_write(hDevice, 0x53, 0x9F);
  309. }
  310. return BBM_OK;
  311. }
  312. int fc8150_get_rssi(HANDLE hDevice, int *rssi)
  313. {
  314. int res = BBM_OK;
  315. u8 LNA, RFVGA, CSF, PREAMP_PGA, CRNTMODE1, CRNTMODE0 = 0x00;
  316. int K = -105;
  317. int PGA = 0;
  318. int A2 = 5;
  319. int A3 = 2;
  320. res = fc8150_read(hDevice, 0xA3, &LNA);
  321. res = fc8150_read(hDevice, 0xA4, &RFVGA);
  322. res = fc8150_read(hDevice, 0xA8, &CSF);
  323. res = fc8150_bb_read(hDevice, 0x106E, &PREAMP_PGA);
  324. if (res != BBM_OK)
  325. return res;
  326. if (127 < PREAMP_PGA)
  327. PGA = -1 * ((256 - PREAMP_PGA) + 1);
  328. else if (PREAMP_PGA <= 127)
  329. PGA = PREAMP_PGA;
  330. CRNTMODE1 = (LNA >> 6) & 0x01;
  331. CRNTMODE0 = (LNA >> 5) & 0x01;
  332. /* *rssi = (LNA & 0x07) * 6 + (RFVGA & 0x1F)
  333. + ((CSF & 0x03)+((CSF & 0x70) >> 4) ) * 6 - PGA * 0.25f
  334. + A2 * CRNTMODE1 + A3 * CRNTMODE0 + K ; */
  335. *rssi = (LNA & 0x07) * 6 + (RFVGA & 0x1F)
  336. + ((CSF & 0x03) + ((CSF & 0x70) >> 4)) * 6 - PGA / 4
  337. + A2 * CRNTMODE1 + A3 * CRNTMODE0 + K;
  338. return BBM_OK;
  339. }
  340. int fc8150_tuner_deinit(HANDLE hDevice)
  341. {
  342. return BBM_OK;
  343. }