fc8151_tun.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*****************************************************************************
  2. Copyright(c) 2012 FCI Inc. All Rights Reserved
  3. File name : fc8151_tun.c (WLCSP)
  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. *******************************************************************************/
  19. #include "fci_types.h"
  20. #include "fci_oal.h"
  21. #include "fci_tun.h"
  22. #include "fc8150_regs.h"
  23. #include "fci_hal.h"
  24. #define FC8150_FREQ_XTAL BBM_XTAL_FREQ /* 26MHZ */
  25. static int high_crnt_mode = 1;
  26. static int fc8151_write(HANDLE hDevice, u8 addr, u8 data)
  27. {
  28. int res;
  29. u8 tmp;
  30. tmp = data;
  31. res = tuner_i2c_write(hDevice, addr, 1, &tmp, 1);
  32. return res;
  33. }
  34. static int fc8151_read(HANDLE hDevice, u8 addr, u8 *data)
  35. {
  36. int res;
  37. res = tuner_i2c_read(hDevice, addr, 1, data, 1);
  38. return res;
  39. }
  40. static int fc8151_bb_read(HANDLE hDevice, u16 addr, u8 *data)
  41. {
  42. int res;
  43. res = bbm_read(hDevice, addr, data);
  44. return res;
  45. }
  46. static int fc8151_bb_write(HANDLE hDevice, u16 addr, u8 data)
  47. {
  48. return BBM_OK;
  49. }
  50. static int KbdFunc(HANDLE hDevice)
  51. {
  52. int i = 0;
  53. u8 CSF = 0x00;
  54. int res = BBM_OK;
  55. int crnt_mode[5] = {0, 0, 0, 0, 0};
  56. int pre_crnt_mode = 0;
  57. high_crnt_mode = 2;
  58. fc8151_write(hDevice, 0x13, 0xF4);
  59. fc8151_write(hDevice, 0x1F, 0x06);
  60. fc8151_write(hDevice, 0x33, 0x08);
  61. fc8151_write(hDevice, 0x34, 0x68);
  62. fc8151_write(hDevice, 0x35, 0x0A);
  63. while (1) {
  64. while (1) {
  65. for (i = 0; i < 5; i++) {
  66. msWait(100);
  67. res = fc8151_read(hDevice, 0xA6, &CSF);
  68. if (CSF < 4)
  69. crnt_mode[i] = 2;
  70. if (CSF == 4)
  71. crnt_mode[i] = 1;
  72. if (4 < CSF)
  73. crnt_mode[i] = 0;
  74. }
  75. pre_crnt_mode = high_crnt_mode;
  76. if ((crnt_mode[0] + crnt_mode[1] + crnt_mode[2]
  77. + crnt_mode[3] + crnt_mode[4]) == 10)
  78. high_crnt_mode = 2;
  79. else if ((crnt_mode[0] + crnt_mode[1] + crnt_mode[2]
  80. + crnt_mode[3] + crnt_mode[4]) == 5)
  81. high_crnt_mode = 1;
  82. else if ((crnt_mode[0] + crnt_mode[1] + crnt_mode[2]
  83. + crnt_mode[3] + crnt_mode[4]) == 0)
  84. high_crnt_mode = 0;
  85. else
  86. high_crnt_mode = pre_crnt_mode;
  87. if (!(high_crnt_mode == pre_crnt_mode))
  88. break;
  89. }
  90. if (high_crnt_mode == 2) {
  91. fc8151_write(hDevice, 0x13, 0xF4);
  92. fc8151_write(hDevice, 0x1F, 0x06);
  93. fc8151_write(hDevice, 0x33, 0x08);
  94. fc8151_write(hDevice, 0x34, 0x68);
  95. fc8151_write(hDevice, 0x35, 0x0A);
  96. } else if (high_crnt_mode == 1) {
  97. fc8151_write(hDevice, 0x13, 0x44);
  98. fc8151_write(hDevice, 0x1F, 0x06);
  99. fc8151_write(hDevice, 0x33, 0x08);
  100. fc8151_write(hDevice, 0x34, 0x68);
  101. fc8151_write(hDevice, 0x35, 0x0A);
  102. } else if (high_crnt_mode == 0) {
  103. fc8151_write(hDevice, 0x13, 0x44);
  104. fc8151_write(hDevice, 0x1F, 0x02);
  105. fc8151_write(hDevice, 0x33, 0x04);
  106. fc8151_write(hDevice, 0x34, 0x48);
  107. fc8151_write(hDevice, 0x35, 0x0C);
  108. }
  109. }
  110. return res;
  111. }
  112. static int fc8151_set_filter(HANDLE hDevice)
  113. {
  114. int i;
  115. u8 cal_mon = 0;
  116. #if (FC8151_FREQ_XTAL == 16000)
  117. fc8151_write(hDevice, 0x3B, 0x01);
  118. fc8151_write(hDevice, 0x3D, 0x20);
  119. fc8151_write(hDevice, 0x3B, 0x00);
  120. #elif (FC8150_FREQ_XTAL == 16384)
  121. fc8151_write(hDevice, 0x3B, 0x01);
  122. fc8151_write(hDevice, 0x3D, 0x21);
  123. fc8151_write(hDevice, 0x3B, 0x00);
  124. #elif (FC8150_FREQ_XTAL == 18000)
  125. fc8151_write(hDevice, 0x3B, 0x01);
  126. fc8151_write(hDevice, 0x3D, 0x24);
  127. fc8151_write(hDevice, 0x3B, 0x00);
  128. #elif (FC8150_FREQ_XTAL == 19200)
  129. fc8151_write(hDevice, 0x3B, 0x01);
  130. fc8151_write(hDevice, 0x3D, 0x26);
  131. fc8151_write(hDevice, 0x3B, 0x00);
  132. #elif (FC8150_FREQ_XTAL == 24000)
  133. fc8151_write(hDevice, 0x3B, 0x01);
  134. fc8151_write(hDevice, 0x3D, 0x30);
  135. fc8151_write(hDevice, 0x3B, 0x00);
  136. #elif (FC8150_FREQ_XTAL == 24576)
  137. fc8151_write(hDevice, 0x3B, 0x01);
  138. fc8151_write(hDevice, 0x3D, 0x31);
  139. fc8151_write(hDevice, 0x3B, 0x00);
  140. #elif (FC8150_FREQ_XTAL == 26000)
  141. fc8151_write(hDevice, 0x3B, 0x01);
  142. fc8151_write(hDevice, 0x3D, 0x34);
  143. fc8151_write(hDevice, 0x3B, 0x00);
  144. #elif (FC8150_FREQ_XTAL == 27000)
  145. fc8151_write(hDevice, 0x3B, 0x01);
  146. fc8151_write(hDevice, 0x3D, 0x36);
  147. fc8151_write(hDevice, 0x3B, 0x00);
  148. #elif (FC8150_FREQ_XTAL == 27120)
  149. fc8151_write(hDevice, 0x3B, 0x01);
  150. fc8151_write(hDevice, 0x3D, 0x36);
  151. fc8151_write(hDevice, 0x3B, 0x00);
  152. #elif (FC8150_FREQ_XTAL == 32000)
  153. fc8151_write(hDevice, 0x3B, 0x01);
  154. fc8151_write(hDevice, 0x3D, 0x40);
  155. fc8151_write(hDevice, 0x3B, 0x00);
  156. #elif (FC8150_FREQ_XTAL == 37400)
  157. fc8151_write(hDevice, 0x3B, 0x01);
  158. fc8151_write(hDevice, 0x3D, 0x4B);
  159. fc8151_write(hDevice, 0x3B, 0x00);
  160. #elif (FC8150_FREQ_XTAL == 38400)
  161. fc8151_write(hDevice, 0x3B, 0x01);
  162. fc8151_write(hDevice, 0x3D, 0x4D);
  163. fc8151_write(hDevice, 0x3B, 0x00);
  164. #else
  165. return BBM_NOK;
  166. #endif
  167. for (i = 0; i < 10; i++) {
  168. msWait(5);
  169. fc8151_read(hDevice, 0x33, &cal_mon);
  170. if ((cal_mon & 0xC0) == 0xC0)
  171. break;
  172. fc8151_write(hDevice, 0x32, 0x01);
  173. fc8151_write(hDevice, 0x32, 0x09);
  174. }
  175. fc8151_write(hDevice, 0x32, 0x01);
  176. return BBM_OK;
  177. }
  178. int fc8151_tuner_init(HANDLE hDevice, u32 band)
  179. {
  180. u8 RFPD_REF, MIXPD_REF;
  181. int res = BBM_OK;
  182. PRINTF(hDevice, "fc8151_init\n");
  183. fc8151_write(hDevice, 0x00, 0x00);
  184. fc8151_write(hDevice, 0x02, 0x81);
  185. fc8151_write(hDevice, 0x13, 0xF4);
  186. fc8151_write(hDevice, 0x30, 0x0A);
  187. fc8151_write(hDevice, 0x3B, 0x01);
  188. fc8151_set_filter(hDevice);
  189. fc8151_write(hDevice, 0x3B, 0x00);
  190. fc8151_write(hDevice, 0x34, 0x68);
  191. fc8151_write(hDevice, 0x36, 0xFF);
  192. fc8151_write(hDevice, 0x37, 0xFF);
  193. fc8151_write(hDevice, 0x39, 0x11);
  194. fc8151_write(hDevice, 0x3A, 0x00);
  195. fc8151_write(hDevice, 0x52, 0x20);
  196. fc8151_write(hDevice, 0x53, 0x5F);
  197. fc8151_write(hDevice, 0x54, 0x00);
  198. fc8151_write(hDevice, 0x5E, 0x00);
  199. fc8151_write(hDevice, 0x63, 0x30);
  200. fc8151_write(hDevice, 0x56, 0x0F);
  201. fc8151_write(hDevice, 0x57, 0x1F);
  202. fc8151_write(hDevice, 0x58, 0x09);
  203. fc8151_write(hDevice, 0x59, 0x5E);
  204. fc8151_write(hDevice, 0x29, 0x00);
  205. fc8151_write(hDevice, 0x94, 0x00);
  206. fc8151_write(hDevice, 0x95, 0x01);
  207. fc8151_write(hDevice, 0x96, 0x11);
  208. fc8151_write(hDevice, 0x97, 0x21);
  209. fc8151_write(hDevice, 0x98, 0x31);
  210. fc8151_write(hDevice, 0x99, 0x32);
  211. fc8151_write(hDevice, 0x9A, 0x42);
  212. fc8151_write(hDevice, 0x9B, 0x52);
  213. fc8151_write(hDevice, 0x9C, 0x53);
  214. fc8151_write(hDevice, 0x9D, 0x63);
  215. fc8151_write(hDevice, 0x9E, 0x63);
  216. fc8151_write(hDevice, 0x9F, 0x63);
  217. fc8151_write(hDevice, 0x79, 0x2A);
  218. fc8151_write(hDevice, 0x7A, 0x24);
  219. fc8151_write(hDevice, 0x7B, 0xFF);
  220. fc8151_write(hDevice, 0x7C, 0x16);
  221. fc8151_write(hDevice, 0x7D, 0x12);
  222. fc8151_write(hDevice, 0x84, 0x00);
  223. fc8151_write(hDevice, 0x85, 0x08);
  224. fc8151_write(hDevice, 0x86, 0x00);
  225. fc8151_write(hDevice, 0x87, 0x08);
  226. fc8151_write(hDevice, 0x88, 0x00);
  227. fc8151_write(hDevice, 0x89, 0x08);
  228. fc8151_write(hDevice, 0x8A, 0x00);
  229. fc8151_write(hDevice, 0x8B, 0x08);
  230. fc8151_write(hDevice, 0x8C, 0x00);
  231. fc8151_write(hDevice, 0x8D, 0x1D);
  232. fc8151_write(hDevice, 0x8E, 0x13);
  233. fc8151_write(hDevice, 0x8F, 0x1D);
  234. fc8151_write(hDevice, 0x90, 0x13);
  235. fc8151_write(hDevice, 0x91, 0x1D);
  236. fc8151_write(hDevice, 0x92, 0x13);
  237. fc8151_write(hDevice, 0x93, 0x1D);
  238. fc8151_write(hDevice, 0x80, 0x1F);
  239. fc8151_write(hDevice, 0x81, 0x0A);
  240. fc8151_write(hDevice, 0x82, 0x40);
  241. fc8151_write(hDevice, 0x83, 0x0A);
  242. fc8151_write(hDevice, 0xA0, 0xC0);
  243. fc8151_write(hDevice, 0x7E, 0x7F);
  244. fc8151_write(hDevice, 0x7F, 0x7F);
  245. fc8151_write(hDevice, 0xD0, 0x0A);
  246. fc8151_write(hDevice, 0xD2, 0x28);
  247. fc8151_write(hDevice, 0xD4, 0x28);
  248. /* _beginthread(KbdFunc,0,&x); */
  249. fc8151_write(hDevice, 0xA0, 0x17);
  250. fc8151_write(hDevice, 0xD0, 0x00);
  251. fc8151_write(hDevice, 0xA1, 0x1D);
  252. msWait(100);
  253. res = fc8151_read(hDevice, 0xD6, &RFPD_REF);
  254. res = fc8151_read(hDevice, 0xD8, &MIXPD_REF);
  255. fc8151_write(hDevice, 0xA0, 0xD7);
  256. fc8151_write(hDevice, 0xD0, 0x0A);
  257. fc8151_write(hDevice, 0x7E, RFPD_REF);
  258. fc8151_write(hDevice, 0x7F, MIXPD_REF);
  259. fc8151_write(hDevice, 0xA0, 0xC0);
  260. fc8151_write(hDevice, 0xA1, 0x00);
  261. return res;
  262. }
  263. int fc8151_set_freq(HANDLE hDevice, band_type band, u32 rf_kHz)
  264. {
  265. int res = BBM_OK;
  266. int n_captune = 0;
  267. unsigned long f_diff, f_diff_shifted, n_val, k_val;
  268. unsigned long f_vco, f_comp;
  269. unsigned char r_val, data_0x56;
  270. unsigned char pre_shift_bits = 4;
  271. f_vco = (rf_kHz) << 2;
  272. if (f_vco < FC8150_FREQ_XTAL*40)
  273. r_val = 2;
  274. else
  275. r_val = 1;
  276. f_comp = FC8150_FREQ_XTAL / r_val;
  277. n_val = f_vco / f_comp;
  278. f_diff = f_vco - f_comp * n_val;
  279. f_diff_shifted = f_diff << (20 - pre_shift_bits);
  280. k_val = (f_diff_shifted + (f_comp >> (pre_shift_bits+1)))
  281. / (f_comp >> pre_shift_bits);
  282. k_val = (k_val | 1);
  283. if (470000 < rf_kHz && rf_kHz <= 505000)
  284. n_captune = 4;
  285. else if (505000 < rf_kHz && rf_kHz <= 545000)
  286. n_captune = 3;
  287. else if (545000 < rf_kHz && rf_kHz <= 610000)
  288. n_captune = 2;
  289. else if (610000 < rf_kHz && rf_kHz <= 695000)
  290. n_captune = 1;
  291. else if (695000 < rf_kHz)
  292. n_captune = 0;
  293. fc8151_write(hDevice, 0x1E, (unsigned char)n_captune);
  294. data_0x56 = ((r_val == 1) ? 0 : 0x10) + (unsigned char)(k_val >> 16);
  295. fc8151_write(hDevice, 0x56, data_0x56);
  296. fc8151_write(hDevice, 0x57, (unsigned char)((k_val >> 8) & 0xFF));
  297. fc8151_write(hDevice, 0x58, (unsigned char)(((k_val) & 0xFF)));
  298. fc8151_write(hDevice, 0x59, (unsigned char) n_val);
  299. if (rf_kHz <= 600000)
  300. fc8151_write(hDevice, 0x55, 0x07);
  301. else
  302. fc8151_write(hDevice, 0x55, 0x05);
  303. if ((490000 < rf_kHz) && (560000 >= rf_kHz))
  304. fc8151_write(hDevice, 0x1F, 0x0E);
  305. else
  306. fc8151_write(hDevice, 0x1F, 0x06);
  307. return res;
  308. }
  309. int fc8151_get_rssi(HANDLE hDevice, int *rssi)
  310. {
  311. int res = BBM_OK;
  312. u8 LNA, RFVGA, CSF, PREAMP_PGA = 0x00;
  313. int K = -101.25;
  314. float Gain_diff = 0;
  315. int PGA = 0;
  316. res |= fc8151_read(hDevice, 0xA3, &LNA);
  317. res |= fc8151_read(hDevice, 0xA4, &RFVGA);
  318. res |= fc8151_read(hDevice, 0xA6, &CSF);
  319. res |= fc8151_bb_read(hDevice, 0x106E, &PREAMP_PGA);
  320. if (res != BBM_OK)
  321. return res;
  322. if (127 < PREAMP_PGA)
  323. PGA = -1 * ((256 - PREAMP_PGA) + 1);
  324. else if (PREAMP_PGA <= 127)
  325. PGA = PREAMP_PGA;
  326. if (high_crnt_mode == 2)
  327. Gain_diff = 0;
  328. else if (high_crnt_mode == 1)
  329. Gain_diff = 0;
  330. else if (high_crnt_mode == 0)
  331. Gain_diff = -3.5;
  332. *rssi = (LNA & 0x07) * 6 + (RFVGA)
  333. + (CSF & 0x0F) * 6 - PGA * 0.25f + K - Gain_diff;
  334. return BBM_OK;
  335. }
  336. int fc8151_tuner_deinit(HANDLE hDevice)
  337. {
  338. return BBM_OK;
  339. }