2 * Fitipower FC0012 tuner driver
4 * Copyright (C) 2012 Hans-Frieder Vogt <hfvogt@gmx.net>
6 * modified for use in librtlsdr
7 * Copyright (C) 2012 Steve Markgraf <steve@steve-m.de>
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.
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.
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.
27 #include "rtlsdr_i2c.h"
28 #include "tuner_fc0012.h"
30 static int fc0012_writereg(void *dev, uint8_t reg, uint8_t val)
36 if (rtlsdr_i2c_write_fn(dev, FC0012_I2C_ADDR, data, 2) < 0)
42 static int fc0012_readreg(void *dev, uint8_t reg, uint8_t *val)
46 if (rtlsdr_i2c_write_fn(dev, FC0012_I2C_ADDR, &data, 1) < 0)
49 if (rtlsdr_i2c_read_fn(dev, FC0012_I2C_ADDR, &data, 1) < 0)
57 /* Incomplete list of register settings:
60 * CHIP_ID 0x00 0-7 Chip ID (constant 0xA1)
61 * RF_A 0x01 0-3 Number of count-to-9 cycles in RF
62 * divider (suggested: 2..9)
63 * RF_M 0x02 0-7 Total number of cycles (to-8 and to-9)
65 * RF_K_HIGH 0x03 0-6 Bits 8..14 of fractional divider
66 * RF_K_LOW 0x04 0-7 Bits 0..7 of fractional RF divider
67 * RF_OUTDIV_A 0x05 3-7 Power of two required?
68 * LNA_POWER_DOWN 0x06 0 Set to 1 to switch off low noise amp
69 * RF_OUTDIV_B 0x06 1 Set to select 3 instead of 2 for the
71 * VCO_SPEED 0x06 3 Select tuning range of VCO:
72 * 0 = Low range, (ca. 1.1 - 1.5GHz)
73 * 1 = High range (ca. 1.4 - 1.8GHz)
74 * BANDWIDTH 0x06 6-7 Set bandwidth. 6MHz = 0x80, 7MHz=0x40
76 * XTAL_SPEED 0x07 5 Set to 1 for 28.8MHz Crystal input
78 * <agc params> 0x08 0-7
79 * EN_CAL_RSSI 0x09 4 Enable calibrate RSSI
80 * (Receive Signal Strength Indicator)
83 * LNA_GAIN 0x13 3-4 Low noise amp gain
85 * VCO_CALIB 0x0e 7 Set high then low to calibrate VCO
87 * VCO_VOLTAGE 0x0e 0-6 Read Control voltage of VCO
88 * (big value -> low freq)
91 int fc0012_init(void *dev)
96 0x00, /* dummy reg. 0 */
100 0x00, /* reg. 0x04 */
101 0x0f, /* reg. 0x05: may also be 0x0a */
102 0x00, /* reg. 0x06: divider 2, VCO slow */
103 0x00, /* reg. 0x07: may also be 0x0f */
104 0xff, /* reg. 0x08: AGC Clock divide by 256, AGC gain 1/256,
106 0x6e, /* reg. 0x09: Disable LoopThrough, Enable LoopThrough: 0x6f */
107 0xb8, /* reg. 0x0a: Disable LO Test Buffer */
108 0x82, /* reg. 0x0b: Output Clock is same as clock frequency,
110 0xfc, /* reg. 0x0c: depending on AGC Up-Down mode, may need 0xf8 */
111 0x02, /* reg. 0x0d: AGC Not Forcing & LNA Forcing, 0x02 for DVB-T */
112 0x00, /* reg. 0x0e */
113 0x00, /* reg. 0x0f */
114 0x00, /* reg. 0x10: may also be 0x0d */
115 0x00, /* reg. 0x11 */
116 0x1f, /* reg. 0x12: Set to maximum gain */
117 0x08, /* reg. 0x13: Set to Middle Gain: 0x08,
118 Low Gain: 0x00, High Gain: 0x10, enable IX2: 0x80 */
119 0x00, /* reg. 0x14 */
120 0x04, /* reg. 0x15: Enable LNA COMPS */
124 switch (rtlsdr_get_tuner_clock(dev)) {
126 case FC_XTAL_28_8_MHZ:
136 // if (priv->dual_master)
139 for (i = 1; i < sizeof(reg); i++) {
140 ret = fc0012_writereg(dev, i, reg[i]);
148 int fc0012_set_params(void *dev, uint32_t freq, uint32_t bandwidth)
151 uint8_t reg[7], am, pm, multi, tmp;
153 uint32_t xtal_freq_div_2;
157 xtal_freq_div_2 = rtlsdr_get_tuner_clock(dev) / 2;
159 /* select frequency divider and the frequency of VCO */
160 if (freq < 37084000) { /* freq * 96 < 3560000000 */
164 } else if (freq < 55625000) { /* freq * 64 < 3560000000 */
168 } else if (freq < 74167000) { /* freq * 48 < 3560000000 */
172 } else if (freq < 111250000) { /* freq * 32 < 3560000000 */
176 } else if (freq < 148334000) { /* freq * 24 < 3560000000 */
180 } else if (freq < 222500000) { /* freq * 16 < 3560000000 */
184 } else if (freq < 296667000) { /* freq * 12 < 3560000000 */
188 } else if (freq < 445000000) { /* freq * 8 < 3560000000 */
192 } else if (freq < 593334000) { /* freq * 6 < 3560000000 */
202 f_vco = freq * multi;
204 if (f_vco >= 3060000000U) {
209 /* From divided value (XDIV) determined the FA and FP value */
210 xdiv = (uint16_t)(f_vco / xtal_freq_div_2);
211 if ((f_vco - xdiv * xtal_freq_div_2) >= (xtal_freq_div_2 / 2))
214 pm = (uint8_t)(xdiv / 8);
215 am = (uint8_t)(xdiv - (8 * pm));
223 reg[1] = am + (8 * (pm - 31));
230 if ((reg[1] > 15) || (reg[2] < 0x0b)) {
231 fprintf(stderr, "[FC0012] no valid PLL combination "
232 "found for %u Hz!\n", freq);
239 /* From VCO frequency determines the XIN ( fractional part of Delta
240 Sigma PLL) and divided value (XDIV) */
241 xin = (uint16_t)((f_vco - (f_vco / xtal_freq_div_2) * xtal_freq_div_2) / 1000);
242 xin = (xin << 15) / (xtal_freq_div_2 / 1000);
246 reg[3] = xin >> 8; /* xin with 9 bit resolution */
249 reg[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */
262 /* modified for Realtek demod */
265 for (i = 1; i <= 6; i++) {
266 ret = fc0012_writereg(dev, i, reg[i]);
271 /* VCO Calibration */
272 ret = fc0012_writereg(dev, 0x0e, 0x80);
274 ret = fc0012_writereg(dev, 0x0e, 0x00);
276 /* VCO Re-Calibration if needed */
278 ret = fc0012_writereg(dev, 0x0e, 0x00);
282 ret = fc0012_readreg(dev, 0x0e, &tmp);
293 ret = fc0012_writereg(dev, 0x06, reg[6]);
295 ret = fc0012_writereg(dev, 0x0e, 0x80);
297 ret = fc0012_writereg(dev, 0x0e, 0x00);
302 ret = fc0012_writereg(dev, 0x06, reg[6]);
304 ret = fc0012_writereg(dev, 0x0e, 0x80);
306 ret = fc0012_writereg(dev, 0x0e, 0x00);
314 int fc0012_set_gain(void *dev, int gain)
319 ret = fc0012_readreg(dev, 0x13, &tmp);
325 case -99: /* -9.9 dB */
328 case -40: /* -4 dB */
331 tmp |= 0x08; /* 7.1 dB */
334 tmp |= 0x17; /* 17.9 dB */
338 tmp |= 0x10; /* 19.2 dB */
342 ret = fc0012_writereg(dev, 0x13, tmp);