tuner_fc0012.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 "rtlsdr_i2c.h"
  25. #include "tuner_fc0012.h"
  26. static int fc0012_writereg(void *dev, uint8_t reg, uint8_t val)
  27. {
  28. uint8_t data[2];
  29. data[0] = reg;
  30. data[1] = val;
  31. if (rtlsdr_i2c_write_fn(dev, FC0012_I2C_ADDR, data, 2) < 0)
  32. return -1;
  33. return 0;
  34. }
  35. static int fc0012_readreg(void *dev, uint8_t reg, uint8_t *val)
  36. {
  37. uint8_t data = reg;
  38. if (rtlsdr_i2c_write_fn(dev, FC0012_I2C_ADDR, &data, 1) < 0)
  39. return -1;
  40. if (rtlsdr_i2c_read_fn(dev, FC0012_I2C_ADDR, &data, 1) < 0)
  41. return -1;
  42. *val = data;
  43. return 0;
  44. }
  45. /* Incomplete list of register settings:
  46. *
  47. * Name Reg Bits Desc
  48. * CHIP_ID 0x00 0-7 Chip ID (constant 0xA1)
  49. * RF_A 0x01 0-3 Number of count-to-9 cycles in RF
  50. * divider (suggested: 2..9)
  51. * RF_M 0x02 0-7 Total number of cycles (to-8 and to-9)
  52. * in RF divider
  53. * RF_K_HIGH 0x03 0-6 Bits 8..14 of fractional divider
  54. * RF_K_LOW 0x04 0-7 Bits 0..7 of fractional RF divider
  55. * RF_OUTDIV_A 0x05 3-7 Power of two required?
  56. * LNA_POWER_DOWN 0x06 0 Set to 1 to switch off low noise amp
  57. * RF_OUTDIV_B 0x06 1 Set to select 3 instead of 2 for the
  58. * RF output divider
  59. * VCO_SPEED 0x06 3 Select tuning range of VCO:
  60. * 0 = Low range, (ca. 1.1 - 1.5GHz)
  61. * 1 = High range (ca. 1.4 - 1.8GHz)
  62. * BANDWIDTH 0x06 6-7 Set bandwidth. 6MHz = 0x80, 7MHz=0x40
  63. * 8MHz=0x00
  64. * XTAL_SPEED 0x07 5 Set to 1 for 28.8MHz Crystal input
  65. * or 0 for 36MHz
  66. * <agc params> 0x08 0-7
  67. * EN_CAL_RSSI 0x09 4 Enable calibrate RSSI
  68. * (Receive Signal Strength Indicator)
  69. * LNA_FORCE 0x0d 0
  70. * AGC_FORCE 0x0d ?
  71. * LNA_GAIN 0x13 3-4 Low noise amp gain
  72. * LNA_COMPS 0x15 3 ?
  73. * VCO_CALIB 0x0e 7 Set high then low to calibrate VCO
  74. * (fast lock?)
  75. * VCO_VOLTAGE 0x0e 0-6 Read Control voltage of VCO
  76. * (big value -> low freq)
  77. */
  78. int fc0012_init(void *dev)
  79. {
  80. int ret = 0;
  81. unsigned int i;
  82. uint8_t reg[] = {
  83. 0x00, /* dummy reg. 0 */
  84. 0x05, /* reg. 0x01 */
  85. 0x10, /* reg. 0x02 */
  86. 0x00, /* reg. 0x03 */
  87. 0x00, /* reg. 0x04 */
  88. 0x0f, /* reg. 0x05: may also be 0x0a */
  89. 0x00, /* reg. 0x06: divider 2, VCO slow */
  90. 0x00, /* reg. 0x07: may also be 0x0f */
  91. 0xff, /* reg. 0x08: AGC Clock divide by 256, AGC gain 1/256,
  92. Loop Bw 1/8 */
  93. 0x6e, /* reg. 0x09: Disable LoopThrough, Enable LoopThrough: 0x6f */
  94. 0xb8, /* reg. 0x0a: Disable LO Test Buffer */
  95. 0x82, /* reg. 0x0b: Output Clock is same as clock frequency,
  96. may also be 0x83 */
  97. 0xfc, /* reg. 0x0c: depending on AGC Up-Down mode, may need 0xf8 */
  98. 0x02, /* reg. 0x0d: AGC Not Forcing & LNA Forcing, 0x02 for DVB-T */
  99. 0x00, /* reg. 0x0e */
  100. 0x00, /* reg. 0x0f */
  101. 0x00, /* reg. 0x10: may also be 0x0d */
  102. 0x00, /* reg. 0x11 */
  103. 0x1f, /* reg. 0x12: Set to maximum gain */
  104. 0x08, /* reg. 0x13: Set to Middle Gain: 0x08,
  105. Low Gain: 0x00, High Gain: 0x10, enable IX2: 0x80 */
  106. 0x00, /* reg. 0x14 */
  107. 0x04, /* reg. 0x15: Enable LNA COMPS */
  108. };
  109. #if 0
  110. switch (rtlsdr_get_tuner_clock(dev)) {
  111. case FC_XTAL_27_MHZ:
  112. case FC_XTAL_28_8_MHZ:
  113. reg[0x07] |= 0x20;
  114. break;
  115. case FC_XTAL_36_MHZ:
  116. default:
  117. break;
  118. }
  119. #endif
  120. reg[0x07] |= 0x20;
  121. // if (priv->dual_master)
  122. reg[0x0c] |= 0x02;
  123. for (i = 1; i < sizeof(reg); i++) {
  124. ret = fc0012_writereg(dev, i, reg[i]);
  125. if (ret)
  126. break;
  127. }
  128. return ret;
  129. }
  130. int fc0012_set_params(void *dev, uint32_t freq, uint32_t bandwidth)
  131. {
  132. int i, ret = 0;
  133. uint8_t reg[7], am, pm, multi, tmp;
  134. uint64_t f_vco;
  135. uint32_t xtal_freq_div_2;
  136. uint16_t xin, xdiv;
  137. int vco_select = 0;
  138. xtal_freq_div_2 = rtlsdr_get_tuner_clock(dev) / 2;
  139. /* select frequency divider and the frequency of VCO */
  140. if (freq < 37084000) { /* freq * 96 < 3560000000 */
  141. multi = 96;
  142. reg[5] = 0x82;
  143. reg[6] = 0x00;
  144. } else if (freq < 55625000) { /* freq * 64 < 3560000000 */
  145. multi = 64;
  146. reg[5] = 0x82;
  147. reg[6] = 0x02;
  148. } else if (freq < 74167000) { /* freq * 48 < 3560000000 */
  149. multi = 48;
  150. reg[5] = 0x42;
  151. reg[6] = 0x00;
  152. } else if (freq < 111250000) { /* freq * 32 < 3560000000 */
  153. multi = 32;
  154. reg[5] = 0x42;
  155. reg[6] = 0x02;
  156. } else if (freq < 148334000) { /* freq * 24 < 3560000000 */
  157. multi = 24;
  158. reg[5] = 0x22;
  159. reg[6] = 0x00;
  160. } else if (freq < 222500000) { /* freq * 16 < 3560000000 */
  161. multi = 16;
  162. reg[5] = 0x22;
  163. reg[6] = 0x02;
  164. } else if (freq < 296667000) { /* freq * 12 < 3560000000 */
  165. multi = 12;
  166. reg[5] = 0x12;
  167. reg[6] = 0x00;
  168. } else if (freq < 445000000) { /* freq * 8 < 3560000000 */
  169. multi = 8;
  170. reg[5] = 0x12;
  171. reg[6] = 0x02;
  172. } else if (freq < 593334000) { /* freq * 6 < 3560000000 */
  173. multi = 6;
  174. reg[5] = 0x0a;
  175. reg[6] = 0x00;
  176. } else {
  177. multi = 4;
  178. reg[5] = 0x0a;
  179. reg[6] = 0x02;
  180. }
  181. f_vco = freq * multi;
  182. if (f_vco >= 3060000000U) {
  183. reg[6] |= 0x08;
  184. vco_select = 1;
  185. }
  186. if (freq >= 45000000) {
  187. /* From divided value (XDIV) determined the FA and FP value */
  188. xdiv = (uint16_t)(f_vco / xtal_freq_div_2);
  189. if ((f_vco - xdiv * xtal_freq_div_2) >= (xtal_freq_div_2 / 2))
  190. xdiv++;
  191. pm = (uint8_t)(xdiv / 8);
  192. am = (uint8_t)(xdiv - (8 * pm));
  193. if (am < 2) {
  194. reg[1] = am + 8;
  195. reg[2] = pm - 1;
  196. } else {
  197. reg[1] = am;
  198. reg[2] = pm;
  199. }
  200. } else {
  201. /* fix for frequency less than 45 MHz */
  202. reg[1] = 0x06;
  203. reg[2] = 0x11;
  204. }
  205. /* fix clock out */
  206. reg[6] |= 0x20;
  207. /* From VCO frequency determines the XIN ( fractional part of Delta
  208. Sigma PLL) and divided value (XDIV) */
  209. xin = (uint16_t)((f_vco - (f_vco / xtal_freq_div_2) * xtal_freq_div_2) / 1000);
  210. xin = (xin << 15) / (xtal_freq_div_2 / 1000);
  211. if (xin >= 16384)
  212. xin += 32768;
  213. reg[3] = xin >> 8; /* xin with 9 bit resolution */
  214. reg[4] = xin & 0xff;
  215. reg[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */
  216. switch (bandwidth) {
  217. case 6000000:
  218. reg[6] |= 0x80;
  219. break;
  220. case 7000000:
  221. reg[6] |= 0x40;
  222. break;
  223. case 8000000:
  224. default:
  225. break;
  226. }
  227. /* modified for Realtek demod */
  228. reg[5] |= 0x07;
  229. for (i = 1; i <= 6; i++) {
  230. ret = fc0012_writereg(dev, i, reg[i]);
  231. if (ret)
  232. goto exit;
  233. }
  234. /* VCO Calibration */
  235. ret = fc0012_writereg(dev, 0x0e, 0x80);
  236. if (!ret)
  237. ret = fc0012_writereg(dev, 0x0e, 0x00);
  238. /* VCO Re-Calibration if needed */
  239. if (!ret)
  240. ret = fc0012_writereg(dev, 0x0e, 0x00);
  241. if (!ret) {
  242. // msleep(10);
  243. ret = fc0012_readreg(dev, 0x0e, &tmp);
  244. }
  245. if (ret)
  246. goto exit;
  247. /* vco selection */
  248. tmp &= 0x3f;
  249. if (vco_select) {
  250. if (tmp > 0x3c) {
  251. reg[6] &= ~0x08;
  252. ret = fc0012_writereg(dev, 0x06, reg[6]);
  253. if (!ret)
  254. ret = fc0012_writereg(dev, 0x0e, 0x80);
  255. if (!ret)
  256. ret = fc0012_writereg(dev, 0x0e, 0x00);
  257. }
  258. } else {
  259. if (tmp < 0x02) {
  260. reg[6] |= 0x08;
  261. ret = fc0012_writereg(dev, 0x06, reg[6]);
  262. if (!ret)
  263. ret = fc0012_writereg(dev, 0x0e, 0x80);
  264. if (!ret)
  265. ret = fc0012_writereg(dev, 0x0e, 0x00);
  266. }
  267. }
  268. exit:
  269. return ret;
  270. }
  271. int fc0012_set_gain(void *dev, int gain)
  272. {
  273. /* TODO add gain regulation */
  274. return 0;
  275. }