fc8080_tun.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*****************************************************************************
  2. Copyright(c) 2013 FCI Inc. All Rights Reserved
  3. File name : fc8080_tun.c
  4. Description : fc8080 tuner driver source file
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. History :
  17. ----------------------------------------------------------------------
  18. 20130409 v0p4
  19. 20130412 v0p5
  20. 20130415 v0p6
  21. 20130509 v1p0
  22. 20130520 v1p1
  23. 20130524 v1p2
  24. 20130524 v1p3
  25. 20131125 v1p4
  26. *******************************************************************************/
  27. #include "fci_types.h"
  28. #include "fci_oal.h"
  29. #include "fci_tun.h"
  30. #include "fci_hal.h"
  31. #include "fc8080_regs.h"
  32. static s32 fc8080_write(HANDLE handle, u8 addr, u8 data)
  33. {
  34. s32 res;
  35. u8 tmp;
  36. tmp = data;
  37. res = tuner_i2c_write(handle, addr, 1, &tmp, 1);
  38. return res;
  39. }
  40. static s32 fc8080_read(HANDLE handle, u8 addr, u8 *data)
  41. {
  42. s32 res;
  43. res = tuner_i2c_read(handle, addr, 1, data, 1);
  44. return res;
  45. }
  46. /*static s32 fc8080_bb_write(HANDLE handle, u16 addr, u8 data)
  47. {
  48. s32 res;
  49. res = bbm_write(handle, addr, data);
  50. return res;
  51. }*/
  52. static s32 fc8080_bb_read(HANDLE handle, u16 addr, u8 *data)
  53. {
  54. s32 res;
  55. res = bbm_read(handle, addr, data);
  56. return res;
  57. }
  58. static s32 fc8080_set_filter(HANDLE handle)
  59. {
  60. u8 cal = 0;
  61. u32 tcxo = FC8080_FREQ_XTAL * 1000;
  62. u32 csfbw = 780000;
  63. cal = (unsigned int) (tcxo * 78 * 2 / csfbw) / 100;
  64. fc8080_write(handle, 0x3c, 0x01);
  65. fc8080_write(handle, 0x3d, cal);
  66. fc8080_write(handle, 0x3c, 0x00);
  67. fc8080_write(handle, 0x32, 0x07);
  68. return BBM_OK;
  69. }
  70. s32 fc8080_tuner_init(HANDLE handle, u32 band)
  71. {
  72. s32 i;
  73. s32 n_rfagc_pd1_avg, n_rfagc_pd2_avg;
  74. u8 rfagc_pd2[6], rfagc_pd2_avg, rfagc_pd2_max, rfagc_pd2_min;
  75. u8 rfagc_pd1[6], rfagc_pd1_avg, rfagc_pd1_max, rfagc_pd1_min;
  76. fc8080_write(handle, 0x00, 0x00);
  77. fc8080_write(handle, 0x02, 0x11);
  78. fc8080_set_filter(handle);
  79. fc8080_write(handle, 0xb3, 0x78);
  80. fc8080_write(handle, 0x9c, 0x00);
  81. fc8080_write(handle, 0x9d, 0x01);
  82. fc8080_write(handle, 0x9e, 0x02);
  83. fc8080_write(handle, 0x9f, 0x0a);
  84. fc8080_write(handle, 0xa0, 0x2a);
  85. fc8080_write(handle, 0xa1, 0x32);
  86. fc8080_write(handle, 0xa2, 0x52);
  87. fc8080_write(handle, 0xa3, 0x56);
  88. fc8080_write(handle, 0xa4, 0x76);
  89. fc8080_write(handle, 0xa5, 0x7e);
  90. fc8080_write(handle, 0xa6, 0x7f);
  91. fc8080_write(handle, 0xa7, 0x9f);
  92. fc8080_write(handle, 0xa8, 0xc0);
  93. fc8080_write(handle, 0xd0, 0x00);
  94. rfagc_pd1[0] = 0;
  95. rfagc_pd1[1] = 0;
  96. rfagc_pd1[2] = 0;
  97. rfagc_pd1[3] = 0;
  98. rfagc_pd1[4] = 0;
  99. rfagc_pd1[5] = 0;
  100. rfagc_pd1_max = 0;
  101. rfagc_pd1_min = 255;
  102. for (i = 0; i < 6; i++) {
  103. fc8080_read(handle, 0xd8, &rfagc_pd1[i]);
  104. if (rfagc_pd1[i] >= rfagc_pd1_max)
  105. rfagc_pd1_max = rfagc_pd1[i];
  106. if (rfagc_pd1[i] <= rfagc_pd1_min)
  107. rfagc_pd1_min = rfagc_pd1[i];
  108. }
  109. n_rfagc_pd1_avg = (rfagc_pd1[0] + rfagc_pd1[1] + rfagc_pd1[2] +
  110. rfagc_pd1[3] + rfagc_pd1[4] + rfagc_pd1[5] -
  111. rfagc_pd1_max - rfagc_pd1_min) / 4;
  112. rfagc_pd1_avg = (u8) n_rfagc_pd1_avg;
  113. fc8080_write(handle, 0x7f , rfagc_pd1_avg);
  114. rfagc_pd2[0] = 0;
  115. rfagc_pd2[1] = 0;
  116. rfagc_pd2[2] = 0;
  117. rfagc_pd2[3] = 0;
  118. rfagc_pd2[4] = 0;
  119. rfagc_pd2[5] = 0;
  120. rfagc_pd2_max = 0;
  121. rfagc_pd2_min = 255;
  122. for (i = 0; i < 6; i++) {
  123. fc8080_read(handle, 0xd6 , &rfagc_pd2[i]);
  124. if (rfagc_pd2[i] >= rfagc_pd2_max)
  125. rfagc_pd2_max = rfagc_pd2[i];
  126. if (rfagc_pd2[i] <= rfagc_pd2_min)
  127. rfagc_pd2_min = rfagc_pd2[i];
  128. }
  129. n_rfagc_pd2_avg = (rfagc_pd2[0] + rfagc_pd2[1] + rfagc_pd2[2] +
  130. rfagc_pd2[3] + rfagc_pd2[4] + rfagc_pd2[5] -
  131. rfagc_pd2_max - rfagc_pd2_min) / 4;
  132. rfagc_pd2_avg = (u8) n_rfagc_pd2_avg;
  133. fc8080_write(handle, 0x7e, rfagc_pd2_avg);
  134. fc8080_write(handle, 0x79, 0x26);
  135. fc8080_write(handle, 0x7a, 0x21);
  136. fc8080_write(handle, 0x7b, 0xff);
  137. fc8080_write(handle, 0x7c, 0x1b);
  138. fc8080_write(handle, 0x7d, 0x18);
  139. fc8080_write(handle, 0x84, 0x00);
  140. fc8080_write(handle, 0x85, 0x0d);
  141. fc8080_write(handle, 0x86, 0x00);
  142. fc8080_write(handle, 0x87, 0x10);
  143. fc8080_write(handle, 0x88, 0x00);
  144. fc8080_write(handle, 0x89, 0x0e);
  145. fc8080_write(handle, 0x8a, 0x00);
  146. fc8080_write(handle, 0x8b, 0x0e);
  147. fc8080_write(handle, 0x8c, 0x03);
  148. fc8080_write(handle, 0x8d, 0x10);
  149. fc8080_write(handle, 0x8e, 0x00);
  150. fc8080_write(handle, 0x8f, 0x10);
  151. fc8080_write(handle, 0x90, 0x04);
  152. fc8080_write(handle, 0x91, 0x10);
  153. fc8080_write(handle, 0x92, 0x00);
  154. fc8080_write(handle, 0x93, 0x00);
  155. fc8080_write(handle, 0x94, 0x00);
  156. fc8080_write(handle, 0x95, 0x0e);
  157. fc8080_write(handle, 0x96, 0x00);
  158. fc8080_write(handle, 0x97, 0x0e);
  159. fc8080_write(handle, 0x98, 0x00);
  160. fc8080_write(handle, 0x99, 0x0e);
  161. fc8080_write(handle, 0x9a, 0x00);
  162. fc8080_write(handle, 0x9b, 0x0e);
  163. fc8080_write(handle, 0x80, 0x3d);
  164. fc8080_write(handle, 0x81, 0x20);
  165. fc8080_write(handle, 0x82, 0x25);
  166. fc8080_write(handle, 0x83, 0x0a);
  167. fc8080_write(handle, 0xd0, 0x00);
  168. #if (FC8080_FREQ_XTAL == 16384)
  169. fc8080_write(handle, 0xd2, 0x18);
  170. fc8080_write(handle, 0xd4, 0x2a);
  171. #elif (FC8080_FREQ_XTAL == 19200)
  172. fc8080_write(handle, 0xd2, 0x1d);
  173. fc8080_write(handle, 0xd4, 0x33);
  174. #elif (FC8080_FREQ_XTAL == 24000)
  175. fc8080_write(handle, 0xd2, 0x28);
  176. fc8080_write(handle, 0xd4, 0x44);
  177. #elif (FC8080_FREQ_XTAL == 24576)
  178. fc8080_write(handle, 0xd2, 0x28);
  179. fc8080_write(handle, 0xd4, 0x44);
  180. #elif (FC8080_FREQ_XTAL == 27120)
  181. fc8080_write(handle, 0xd2, 0x2d);
  182. fc8080_write(handle, 0xd4, 0x4c);
  183. #elif (FC8080_FREQ_XTAL == 38400)
  184. fc8080_write(handle, 0xd2, 0x44);
  185. fc8080_write(handle, 0xd4, 0x6f);
  186. #endif
  187. fc8080_write(handle, 0xae, 0x36);
  188. fc8080_write(handle, 0xad, 0x8b);
  189. fc8080_write(handle, 0x14, 0x63);
  190. fc8080_write(handle, 0x15, 0x02);
  191. fc8080_write(handle, 0x18, 0x43);
  192. fc8080_write(handle, 0x19, 0x21);
  193. fc8080_write(handle, 0x27, 0x82);
  194. fc8080_write(handle, 0x28, 0x33);
  195. fc8080_write(handle, 0x33, 0x43);
  196. fc8080_write(handle, 0x34, 0x41);
  197. fc8080_write(handle, 0x53, 0x41);
  198. fc8080_write(handle, 0x54, 0x01);
  199. fc8080_write(handle, 0x66, 0xf6);
  200. fc8080_write(handle, 0xaf, 0x14);
  201. fc8080_write(handle, 0xb6, 0x00);
  202. fc8080_write(handle, 0xb8, 0xb0);
  203. fc8080_write(handle, 0xb9, 0xc4);
  204. return BBM_OK;
  205. }
  206. s32 fc8080_set_freq(HANDLE handle, u32 band, u32 freq)
  207. {
  208. u32 f_diff, f_diff_shifted, n_val, k_val;
  209. u32 f_vco, f_comp;
  210. u8 div_ratio, lo_mode, r_val, data_0x55;
  211. u8 pre_shift_bits = 4;
  212. div_ratio = 16;
  213. lo_mode = 0;
  214. f_vco = freq * div_ratio;
  215. if (2800000 < f_vco) {
  216. div_ratio = 12;
  217. lo_mode = 1;
  218. f_vco = freq * div_ratio;
  219. }
  220. if (f_vco < FC8080_FREQ_XTAL * 35)
  221. r_val = 2;
  222. else
  223. r_val = 1;
  224. f_comp = FC8080_FREQ_XTAL / r_val;
  225. n_val = f_vco / f_comp;
  226. f_diff = f_vco - f_comp * n_val;
  227. f_diff_shifted = f_diff << (20 - pre_shift_bits);
  228. k_val = f_diff_shifted / (f_comp >> pre_shift_bits);
  229. k_val = k_val | 1;
  230. data_0x55 = ((r_val == 1) ? 0x00 : 0x10) + (u8) (k_val >> 16);
  231. fc8080_write(handle, 0x51, 7 + 8 * lo_mode);
  232. fc8080_write(handle, 0x55, data_0x55);
  233. fc8080_write(handle, 0x56, (u8) ((k_val >> 8) & 0xff));
  234. fc8080_write(handle, 0x57, (u8) (((k_val) & 0xff)));
  235. fc8080_write(handle, 0x58, (n_val) & 0xff);
  236. if (freq < 207000)
  237. fc8080_write(handle, 0x6a, 0x0b);
  238. else
  239. fc8080_write(handle, 0x6a, 0x03);
  240. fc8080_write(handle, 0x04, 0x04);
  241. fc8080_write(handle, 0x50, 0xf3);
  242. fc8080_write(handle, 0x50, 0xff);
  243. fc8080_write(handle, 0x04, 0x00);
  244. return BBM_OK;
  245. }
  246. s32 fc8080_get_rssi(HANDLE handle, s32 *rssi)
  247. {
  248. s32 res = BBM_OK;
  249. s32 pga = 0;
  250. u8 lna, rfvga, filter, preamp_pga, a1, ext_lna , a2 , a3, k, crntmode1,
  251. crntmode0;
  252. u8 temp = 0;
  253. res = fc8080_read(handle, 0xab, &temp);
  254. lna = temp & 0x0f;
  255. ext_lna = temp >> 7;
  256. res = fc8080_read(handle, 0xac, &rfvga);
  257. rfvga = rfvga & 0x1f;
  258. res = fc8080_bb_read(handle, 0x014d, &filter);
  259. filter = filter & 0x1f;
  260. res = fc8080_bb_read(handle, 0x014e, &preamp_pga);
  261. if (127 < preamp_pga)
  262. pga = -1 * ((256 - preamp_pga) + 1);
  263. else if (preamp_pga <= 127)
  264. pga = preamp_pga;
  265. res = fc8080_read(handle, 0xaf, &a1);
  266. res = fc8080_read(handle, 0xae, &temp);
  267. a2 = temp & 0x0f;
  268. a3 = temp >> 4;
  269. res = fc8080_read(handle, 0xad, &k);
  270. if (filter < 7) {
  271. crntmode1 = 0;
  272. crntmode0 = 0;
  273. } else if (filter == 7) {
  274. crntmode1 = 0;
  275. crntmode0 = 1;
  276. } else {
  277. crntmode1 = 1;
  278. crntmode0 = 0;
  279. }
  280. *rssi = (5 * lna + (rfvga / 2) + (6 * filter) - (pga / 4) +
  281. (a1 * ext_lna) + (a2 * crntmode1) + (a3 * crntmode0) - k);
  282. return res;
  283. }
  284. s32 fc8080_tuner_deinit(HANDLE handle)
  285. {
  286. return BBM_OK;
  287. }