tuner_fc0012.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Fitipower FC0012 tuner driver
  3. *
  4. * Copyright (C) 2012 Hans-Frieder Vogt <hfvogt@gmx.net>
  5. *
  6. * modified for use in librtlsdr
  7. * Copyright (C) 2012 Steve Markgraf <steve@steve-m.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include "rtlsdr_i2c.h"
  26. #include "tuner_fc0012.h"
  27. static int fc0012_writereg(void *dev, uint8_t reg, uint8_t val)
  28. {
  29. uint8_t data[2];
  30. data[0] = reg;
  31. data[1] = val;
  32. if (rtlsdr_i2c_write_fn(dev, FC0012_I2C_ADDR, data, 2) < 0)
  33. return -1;
  34. return 0;
  35. }
  36. static int fc0012_readreg(void *dev, uint8_t reg, uint8_t *val)
  37. {
  38. uint8_t data = reg;
  39. if (rtlsdr_i2c_write_fn(dev, FC0012_I2C_ADDR, &data, 1) < 0)
  40. return -1;
  41. if (rtlsdr_i2c_read_fn(dev, FC0012_I2C_ADDR, &data, 1) < 0)
  42. return -1;
  43. *val = data;
  44. return 0;
  45. }
  46. /* Incomplete list of register settings:
  47. *
  48. * Name Reg Bits Desc
  49. * CHIP_ID 0x00 0-7 Chip ID (constant 0xA1)
  50. * RF_A 0x01 0-3 Number of count-to-9 cycles in RF
  51. * divider (suggested: 2..9)
  52. * RF_M 0x02 0-7 Total number of cycles (to-8 and to-9)
  53. * in RF divider
  54. * RF_K_HIGH 0x03 0-6 Bits 8..14 of fractional divider
  55. * RF_K_LOW 0x04 0-7 Bits 0..7 of fractional RF divider
  56. * RF_OUTDIV_A 0x05 3-7 Power of two required?
  57. * LNA_POWER_DOWN 0x06 0 Set to 1 to switch off low noise amp
  58. * RF_OUTDIV_B 0x06 1 Set to select 3 instead of 2 for the
  59. * RF output divider
  60. * VCO_SPEED 0x06 3 Select tuning range of VCO:
  61. * 0 = Low range, (ca. 1.1 - 1.5GHz)
  62. * 1 = High range (ca. 1.4 - 1.8GHz)
  63. * BANDWIDTH 0x06 6-7 Set bandwidth. 6MHz = 0x80, 7MHz=0x40
  64. * 8MHz=0x00
  65. * XTAL_SPEED 0x07 5 Set to 1 for 28.8MHz Crystal input
  66. * or 0 for 36MHz
  67. * <agc params> 0x08 0-7
  68. * EN_CAL_RSSI 0x09 4 Enable calibrate RSSI
  69. * (Receive Signal Strength Indicator)
  70. * LNA_FORCE 0x0d 0
  71. * AGC_FORCE 0x0d ?
  72. * LNA_GAIN 0x13 3-4 Low noise amp gain
  73. * LNA_COMPS 0x15 3 ?
  74. * VCO_CALIB 0x0e 7 Set high then low to calibrate VCO
  75. * (fast lock?)
  76. * VCO_VOLTAGE 0x0e 0-6 Read Control voltage of VCO
  77. * (big value -> low freq)
  78. */
  79. int fc0012_init(void *dev)
  80. {
  81. int ret = 0;
  82. unsigned int i;
  83. uint8_t reg[] = {
  84. 0x00, /* dummy reg. 0 */
  85. 0x05, /* reg. 0x01 */
  86. 0x10, /* reg. 0x02 */
  87. 0x00, /* reg. 0x03 */
  88. 0x00, /* reg. 0x04 */
  89. 0x0f, /* reg. 0x05: may also be 0x0a */
  90. 0x00, /* reg. 0x06: divider 2, VCO slow */
  91. 0x00, /* reg. 0x07: may also be 0x0f */
  92. 0xff, /* reg. 0x08: AGC Clock divide by 256, AGC gain 1/256,
  93. Loop Bw 1/8 */
  94. 0x6e, /* reg. 0x09: Disable LoopThrough, Enable LoopThrough: 0x6f */
  95. 0xb8, /* reg. 0x0a: Disable LO Test Buffer */
  96. 0x82, /* reg. 0x0b: Output Clock is same as clock frequency,
  97. may also be 0x83 */
  98. 0xfc, /* reg. 0x0c: depending on AGC Up-Down mode, may need 0xf8 */
  99. 0x02, /* reg. 0x0d: AGC Not Forcing & LNA Forcing, 0x02 for DVB-T */
  100. 0x00, /* reg. 0x0e */
  101. 0x00, /* reg. 0x0f */
  102. 0x00, /* reg. 0x10: may also be 0x0d */
  103. 0x00, /* reg. 0x11 */
  104. 0x1f, /* reg. 0x12: Set to maximum gain */
  105. 0x08, /* reg. 0x13: Set to Middle Gain: 0x08,
  106. Low Gain: 0x00, High Gain: 0x10, enable IX2: 0x80 */
  107. 0x00, /* reg. 0x14 */
  108. 0x04, /* reg. 0x15: Enable LNA COMPS */
  109. };
  110. #if 0
  111. switch (rtlsdr_get_tuner_clock(dev)) {
  112. case FC_XTAL_27_MHZ:
  113. case FC_XTAL_28_8_MHZ:
  114. reg[0x07] |= 0x20;
  115. break;
  116. case FC_XTAL_36_MHZ:
  117. default:
  118. break;
  119. }
  120. #endif
  121. reg[0x07] |= 0x20;
  122. // if (priv->dual_master)
  123. reg[0x0c] |= 0x02;
  124. for (i = 1; i < sizeof(reg); i++) {
  125. ret = fc0012_writereg(dev, i, reg[i]);
  126. if (ret)
  127. break;
  128. }
  129. return ret;
  130. }
  131. int fc0012_set_params(void *dev, uint32_t freq, uint32_t bandwidth)
  132. {
  133. int i, ret = 0;
  134. uint8_t reg[7], am, pm, multi, tmp;
  135. uint64_t f_vco;
  136. uint32_t xtal_freq_div_2;
  137. uint16_t xin, xdiv;
  138. int vco_select = 0;
  139. xtal_freq_div_2 = rtlsdr_get_tuner_clock(dev) / 2;
  140. /* select frequency divider and the frequency of VCO */
  141. if (freq < 37084000) { /* freq * 96 < 3560000000 */
  142. multi = 96;
  143. reg[5] = 0x82;
  144. reg[6] = 0x00;
  145. } else if (freq < 55625000) { /* freq * 64 < 3560000000 */
  146. multi = 64;
  147. reg[5] = 0x82;
  148. reg[6] = 0x02;
  149. } else if (freq < 74167000) { /* freq * 48 < 3560000000 */
  150. multi = 48;
  151. reg[5] = 0x42;
  152. reg[6] = 0x00;
  153. } else if (freq < 111250000) { /* freq * 32 < 3560000000 */
  154. multi = 32;
  155. reg[5] = 0x42;
  156. reg[6] = 0x02;
  157. } else if (freq < 148334000) { /* freq * 24 < 3560000000 */
  158. multi = 24;
  159. reg[5] = 0x22;
  160. reg[6] = 0x00;
  161. } else if (freq < 222500000) { /* freq * 16 < 3560000000 */
  162. multi = 16;
  163. reg[5] = 0x22;
  164. reg[6] = 0x02;
  165. } else if (freq < 296667000) { /* freq * 12 < 3560000000 */
  166. multi = 12;
  167. reg[5] = 0x12;
  168. reg[6] = 0x00;
  169. } else if (freq < 445000000) { /* freq * 8 < 3560000000 */
  170. multi = 8;
  171. reg[5] = 0x12;
  172. reg[6] = 0x02;
  173. } else if (freq < 593334000) { /* freq * 6 < 3560000000 */
  174. multi = 6;
  175. reg[5] = 0x0a;
  176. reg[6] = 0x00;
  177. } else {
  178. multi = 4;
  179. reg[5] = 0x0a;
  180. reg[6] = 0x02;
  181. }
  182. f_vco = freq * multi;
  183. if (f_vco >= 3060000000U) {
  184. reg[6] |= 0x08;
  185. vco_select = 1;
  186. }
  187. if (freq >= 45000000) {
  188. /* From divided value (XDIV) determined the FA and FP value */
  189. xdiv = (uint16_t)(f_vco / xtal_freq_div_2);
  190. if ((f_vco - xdiv * xtal_freq_div_2) >= (xtal_freq_div_2 / 2))
  191. xdiv++;
  192. pm = (uint8_t)(xdiv / 8);
  193. am = (uint8_t)(xdiv - (8 * pm));
  194. if (am < 2) {
  195. am += 8;
  196. pm--;
  197. }
  198. if (pm > 31) {
  199. reg[1] = am + (8 * (pm - 31));
  200. reg[2] = 31;
  201. } else {
  202. reg[1] = am;
  203. reg[2] = pm;
  204. }
  205. if (reg[1] > 15) {
  206. fprintf(stderr, "[FC0012] no valid PLL combination "
  207. "found for %u Hz!\n", freq);
  208. return -1;
  209. }
  210. } else {
  211. /* fix for frequency less than 45 MHz */
  212. reg[1] = 0x06;
  213. reg[2] = 0x11;
  214. }
  215. /* fix clock out */
  216. reg[6] |= 0x20;
  217. /* From VCO frequency determines the XIN ( fractional part of Delta
  218. Sigma PLL) and divided value (XDIV) */
  219. xin = (uint16_t)((f_vco - (f_vco / xtal_freq_div_2) * xtal_freq_div_2) / 1000);
  220. xin = (xin << 15) / (xtal_freq_div_2 / 1000);
  221. if (xin >= 16384)
  222. xin += 32768;
  223. reg[3] = xin >> 8; /* xin with 9 bit resolution */
  224. reg[4] = xin & 0xff;
  225. reg[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */
  226. switch (bandwidth) {
  227. case 6000000:
  228. reg[6] |= 0x80;
  229. break;
  230. case 7000000:
  231. reg[6] |= 0x40;
  232. break;
  233. case 8000000:
  234. default:
  235. break;
  236. }
  237. /* modified for Realtek demod */
  238. reg[5] |= 0x07;
  239. for (i = 1; i <= 6; i++) {
  240. ret = fc0012_writereg(dev, i, reg[i]);
  241. if (ret)
  242. goto exit;
  243. }
  244. /* VCO Calibration */
  245. ret = fc0012_writereg(dev, 0x0e, 0x80);
  246. if (!ret)
  247. ret = fc0012_writereg(dev, 0x0e, 0x00);
  248. /* VCO Re-Calibration if needed */
  249. if (!ret)
  250. ret = fc0012_writereg(dev, 0x0e, 0x00);
  251. if (!ret) {
  252. // msleep(10);
  253. ret = fc0012_readreg(dev, 0x0e, &tmp);
  254. }
  255. if (ret)
  256. goto exit;
  257. /* vco selection */
  258. tmp &= 0x3f;
  259. if (vco_select) {
  260. if (tmp > 0x3c) {
  261. reg[6] &= ~0x08;
  262. ret = fc0012_writereg(dev, 0x06, reg[6]);
  263. if (!ret)
  264. ret = fc0012_writereg(dev, 0x0e, 0x80);
  265. if (!ret)
  266. ret = fc0012_writereg(dev, 0x0e, 0x00);
  267. }
  268. } else {
  269. if (tmp < 0x02) {
  270. reg[6] |= 0x08;
  271. ret = fc0012_writereg(dev, 0x06, reg[6]);
  272. if (!ret)
  273. ret = fc0012_writereg(dev, 0x0e, 0x80);
  274. if (!ret)
  275. ret = fc0012_writereg(dev, 0x0e, 0x00);
  276. }
  277. }
  278. exit:
  279. return ret;
  280. }
  281. int fc0012_set_gain(void *dev, int gain)
  282. {
  283. int ret;
  284. uint8_t tmp = 0;
  285. ret = fc0012_readreg(dev, 0x13, &tmp);
  286. /* mask bits off */
  287. tmp &= 0xe0;
  288. switch (gain) {
  289. case -99: /* -9.9 dB */
  290. tmp |= 0x02;
  291. break;
  292. case -40: /* -4 dB */
  293. break;
  294. case 71:
  295. tmp |= 0x08; /* 7.1 dB */
  296. break;
  297. case 179:
  298. tmp |= 0x17; /* 17.9 dB */
  299. break;
  300. case 192:
  301. default:
  302. tmp |= 0x10; /* 19.2 dB */
  303. break;
  304. }
  305. ret = fc0012_writereg(dev, 0x13, tmp);
  306. return ret;
  307. }