2 * Fitipower FC0013 tuner driver
4 * Copyright (C) 2012 Hans-Frieder Vogt <hfvogt@gmx.net>
5 * partially based on driver code from Fitipower
6 * Copyright (C) 2010 Fitipower Integrated Technology Inc
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "fc0013-priv.h"
27 static int fc0013_writereg(struct fc0013_priv *priv, u8 reg, u8 val)
29 u8 buf[2] = {reg, val};
30 struct i2c_msg msg = {
31 .addr = priv->addr, .flags = 0, .buf = buf, .len = 2
34 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
35 err("I2C write reg failed, reg: %02x, val: %02x", reg, val);
41 static int fc0013_readreg(struct fc0013_priv *priv, u8 reg, u8 *val)
43 struct i2c_msg msg[2] = {
44 { .addr = priv->addr, .flags = 0, .buf = ®, .len = 1 },
45 { .addr = priv->addr, .flags = I2C_M_RD, .buf = val, .len = 1 },
48 if (i2c_transfer(priv->i2c, msg, 2) != 2) {
49 err("I2C read reg failed, reg: %02x", reg);
55 static int fc0013_release(struct dvb_frontend *fe)
57 kfree(fe->tuner_priv);
58 fe->tuner_priv = NULL;
62 static int fc0013_init(struct dvb_frontend *fe)
64 struct fc0013_priv *priv = fe->tuner_priv;
66 unsigned char reg[] = {
67 0x00, /* reg. 0x00: dummy */
74 0x0a, /* reg. 0x07: CHECK */
75 0xff, /* reg. 0x08: AGC Clock divide by 256, AGC gain 1/256,
77 0x6f, /* reg. 0x09: enable LoopThrough */
78 0xb8, /* reg. 0x0a: Disable LO Test Buffer */
79 0x82, /* reg. 0x0b: CHECK */
80 0xfc, /* reg. 0x0c: depending on AGC Up-Down mode, may need 0xf8 */
81 0x01, /* reg. 0x0d: AGC Not Forcing & LNA Forcing, may need 0x02 */
88 0x50, /* reg. 0x14: DVB-t High Gain, UHF.
89 Middle Gain: 0x48, Low Gain: 0x40 */
93 switch (priv->xtal_freq) {
95 case FC_XTAL_28_8_MHZ:
103 if (priv->dual_master)
106 if (fe->ops.i2c_gate_ctrl)
107 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
109 for (i = 1; i < sizeof(reg); i++) {
110 ret = fc0013_writereg(priv, i, reg[i]);
115 if (fe->ops.i2c_gate_ctrl)
116 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
119 err("fc0013_writereg failed: %d", ret);
124 static int fc0013_sleep(struct dvb_frontend *fe)
126 /* nothing to do here */
130 int fc0013_rc_cal_add(struct dvb_frontend *fe, int rc_val)
132 struct fc0013_priv *priv = fe->tuner_priv;
137 if (fe->ops.i2c_gate_ctrl)
138 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
140 /* push rc_cal value, get rc_cal value */
141 ret = fc0013_writereg(priv, 0x10, 0x00);
145 /* get rc_cal value */
146 ret = fc0013_readreg(priv, 0x10, &rc_cal);
152 val = (int)rc_cal + rc_val;
155 ret = fc0013_writereg(priv, 0x0d, 0x11);
159 /* modify rc_cal value */
161 ret = fc0013_writereg(priv, 0x10, 0x0f);
163 ret = fc0013_writereg(priv, 0x10, 0x00);
165 ret = fc0013_writereg(priv, 0x10, (u8)val);
168 if (fe->ops.i2c_gate_ctrl)
169 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
173 EXPORT_SYMBOL(fc0013_rc_cal_add);
175 int fc0013_rc_cal_reset(struct dvb_frontend *fe)
177 struct fc0013_priv *priv = fe->tuner_priv;
180 if (fe->ops.i2c_gate_ctrl)
181 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
183 ret = fc0013_writereg(priv, 0x0d, 0x01);
185 ret = fc0013_writereg(priv, 0x10, 0x00);
187 if (fe->ops.i2c_gate_ctrl)
188 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
192 EXPORT_SYMBOL(fc0013_rc_cal_reset);
194 static int fc0013_set_vhf_track(struct fc0013_priv *priv, u32 freq)
199 ret = fc0013_readreg(priv, 0x1d, &tmp);
203 if (freq <= 177500) { /* VHF Track: 7 */
204 ret = fc0013_writereg(priv, 0x1d, tmp | 0x1c);
205 } else if (freq <= 184500) { /* VHF Track: 6 */
206 ret = fc0013_writereg(priv, 0x1d, tmp | 0x18);
207 } else if (freq <= 191500) { /* VHF Track: 5 */
208 ret = fc0013_writereg(priv, 0x1d, tmp | 0x14);
209 } else if (freq <= 198500) { /* VHF Track: 4 */
210 ret = fc0013_writereg(priv, 0x1d, tmp | 0x10);
211 } else if (freq <= 205500) { /* VHF Track: 3 */
212 ret = fc0013_writereg(priv, 0x1d, tmp | 0x0c);
213 } else if (freq <= 219500) { /* VHF Track: 2 */
214 ret = fc0013_writereg(priv, 0x1d, tmp | 0x08);
215 } else if (freq < 300000) { /* VHF Track: 1 */
216 ret = fc0013_writereg(priv, 0x1d, tmp | 0x04);
217 } else { /* UHF and GPS */
218 ret = fc0013_writereg(priv, 0x1d, tmp | 0x1c);
224 static int fc0013_set_params(struct dvb_frontend *fe)
226 struct fc0013_priv *priv = fe->tuner_priv;
228 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
229 u32 freq = p->frequency / 1000;
230 u32 delsys = p->delivery_system;
231 unsigned char reg[7], am, pm, multi, tmp;
233 unsigned short xtal_freq_khz_2, xin, xdiv;
234 bool vco_select = false;
237 ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
238 FC_FE_CALLBACK_VHF_ENABLE, (freq > 300000 ? 0 : 1));
243 switch (priv->xtal_freq) {
245 xtal_freq_khz_2 = 27000 / 2;
248 xtal_freq_khz_2 = 36000 / 2;
250 case FC_XTAL_28_8_MHZ:
252 xtal_freq_khz_2 = 28800 / 2;
256 if (fe->ops.i2c_gate_ctrl)
257 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
260 ret = fc0013_set_vhf_track(priv, freq);
265 /* enable VHF filter */
266 ret = fc0013_readreg(priv, 0x07, &tmp);
269 ret = fc0013_writereg(priv, 0x07, tmp | 0x10);
273 /* disable UHF & disable GPS */
274 ret = fc0013_readreg(priv, 0x14, &tmp);
277 ret = fc0013_writereg(priv, 0x14, tmp & 0x1f);
280 } else if (freq <= 862000) {
281 /* disable VHF filter */
282 ret = fc0013_readreg(priv, 0x07, &tmp);
285 ret = fc0013_writereg(priv, 0x07, tmp & 0xef);
289 /* enable UHF & disable GPS */
290 ret = fc0013_readreg(priv, 0x14, &tmp);
293 ret = fc0013_writereg(priv, 0x14, (tmp & 0x1f) | 0x40);
297 /* disable VHF filter */
298 ret = fc0013_readreg(priv, 0x07, &tmp);
301 ret = fc0013_writereg(priv, 0x07, tmp & 0xef);
305 /* disable UHF & enable GPS */
306 ret = fc0013_readreg(priv, 0x14, &tmp);
309 ret = fc0013_writereg(priv, 0x14, (tmp & 0x1f) | 0x20);
314 /* select frequency divider and the frequency of VCO */
315 if (freq < 37084) { /* freq * 96 < 3560000 */
319 } else if (freq < 55625) { /* freq * 64 < 3560000 */
323 } else if (freq < 74167) { /* freq * 48 < 3560000 */
327 } else if (freq < 111250) { /* freq * 32 < 3560000 */
331 } else if (freq < 148334) { /* freq * 24 < 3560000 */
335 } else if (freq < 222500) { /* freq * 16 < 3560000 */
339 } else if (freq < 296667) { /* freq * 12 < 3560000 */
343 } else if (freq < 445000) { /* freq * 8 < 3560000 */
347 } else if (freq < 593334) { /* freq * 6 < 3560000 */
351 } else if (freq < 950000) { /* freq * 4 < 3800000 */
361 f_vco = freq * multi;
363 if (f_vco >= 3060000) {
369 /* From divided value (XDIV) determined the FA and FP value */
370 xdiv = (unsigned short)(f_vco / xtal_freq_khz_2);
371 if ((f_vco - xdiv * xtal_freq_khz_2) >= (xtal_freq_khz_2 / 2))
374 pm = (unsigned char)(xdiv / 8);
375 am = (unsigned char)(xdiv - (8 * pm));
385 /* fix for frequency less than 45 MHz */
393 /* From VCO frequency determines the XIN ( fractional part of Delta
394 Sigma PLL) and divided value (XDIV) */
395 xin = (unsigned short)(f_vco - (f_vco / xtal_freq_khz_2) * xtal_freq_khz_2);
396 xin = (xin << 15) / xtal_freq_khz_2;
403 if (delsys == SYS_DVBT) {
404 reg[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */
405 switch (p->bandwidth_hz) {
417 err("%s: modulation type not supported!", __func__);
421 /* modified for Realtek demod */
424 for (i = 1; i <= 6; i++) {
425 ret = fc0013_writereg(priv, i, reg[i]);
430 ret = fc0013_readreg(priv, 0x11, &tmp);
434 ret = fc0013_writereg(priv, 0x11, tmp | 0x04);
436 ret = fc0013_writereg(priv, 0x11, tmp & 0xfb);
440 /* VCO Calibration */
441 ret = fc0013_writereg(priv, 0x0e, 0x80);
443 ret = fc0013_writereg(priv, 0x0e, 0x00);
445 /* VCO Re-Calibration if needed */
447 ret = fc0013_writereg(priv, 0x0e, 0x00);
451 ret = fc0013_readreg(priv, 0x0e, &tmp);
462 ret = fc0013_writereg(priv, 0x06, reg[6]);
464 ret = fc0013_writereg(priv, 0x0e, 0x80);
466 ret = fc0013_writereg(priv, 0x0e, 0x00);
471 ret = fc0013_writereg(priv, 0x06, reg[6]);
473 ret = fc0013_writereg(priv, 0x0e, 0x80);
475 ret = fc0013_writereg(priv, 0x0e, 0x00);
479 priv->frequency = p->frequency;
480 priv->bandwidth = p->bandwidth_hz;
483 if (fe->ops.i2c_gate_ctrl)
484 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
486 warn("%s: failed: %d", __func__, ret);
490 static int fc0013_get_frequency(struct dvb_frontend *fe, u32 *frequency)
492 struct fc0013_priv *priv = fe->tuner_priv;
493 *frequency = priv->frequency;
497 static int fc0013_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
504 static int fc0013_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
506 struct fc0013_priv *priv = fe->tuner_priv;
507 *bandwidth = priv->bandwidth;
511 #define INPUT_ADC_LEVEL -8
513 static int fc0013_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
515 struct fc0013_priv *priv = fe->tuner_priv;
518 int int_temp, lna_gain, int_lna, tot_agc_gain, power;
519 const int fc0013_lna_gain_table[] = {
531 if (fe->ops.i2c_gate_ctrl)
532 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
534 ret = fc0013_writereg(priv, 0x13, 0x00);
538 ret = fc0013_readreg(priv, 0x13, &tmp);
543 ret = fc0013_readreg(priv, 0x14, &tmp);
546 lna_gain = tmp & 0x1f;
548 if (fe->ops.i2c_gate_ctrl)
549 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
551 if (lna_gain < ARRAY_SIZE(fc0013_lna_gain_table)) {
552 int_lna = fc0013_lna_gain_table[lna_gain];
553 tot_agc_gain = (abs((int_temp >> 5) - 7) - 2 +
554 (int_temp & 0x1f)) * 2;
555 power = INPUT_ADC_LEVEL - tot_agc_gain - int_lna / 10;
558 *strength = 255; /* 100% */
559 else if (power < -95)
562 *strength = (power + 95) * 255 / 140;
564 *strength |= *strength << 8;
572 if (fe->ops.i2c_gate_ctrl)
573 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
576 warn("%s: failed: %d", __func__, ret);
580 static const struct dvb_tuner_ops fc0013_tuner_ops = {
582 .name = "Fitipower FC0013",
584 .frequency_min = 37000000, /* estimate */
585 .frequency_max = 1680000000, /* CHECK */
589 .release = fc0013_release,
592 .sleep = fc0013_sleep,
594 .set_params = fc0013_set_params,
596 .get_frequency = fc0013_get_frequency,
597 .get_if_frequency = fc0013_get_if_frequency,
598 .get_bandwidth = fc0013_get_bandwidth,
600 .get_rf_strength = fc0013_get_rf_strength,
603 struct dvb_frontend *fc0013_attach(struct dvb_frontend *fe,
604 struct i2c_adapter *i2c, u8 i2c_address, int dual_master,
605 enum fc001x_xtal_freq xtal_freq)
607 struct fc0013_priv *priv = NULL;
609 priv = kzalloc(sizeof(struct fc0013_priv), GFP_KERNEL);
614 priv->dual_master = dual_master;
615 priv->addr = i2c_address;
616 priv->xtal_freq = xtal_freq;
618 info("Fitipower FC0013 successfully attached.");
620 fe->tuner_priv = priv;
622 memcpy(&fe->ops.tuner_ops, &fc0013_tuner_ops,
623 sizeof(struct dvb_tuner_ops));
627 EXPORT_SYMBOL(fc0013_attach);
629 MODULE_DESCRIPTION("Fitipower FC0013 silicon tuner driver");
630 MODULE_AUTHOR("Hans-Frieder Vogt <hfvogt@gmx.net>");
631 MODULE_LICENSE("GPL");
632 MODULE_VERSION("0.2");