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 void fc0013_release(struct dvb_frontend
*fe
)
57 kfree(fe
->tuner_priv
);
58 fe
->tuner_priv
= NULL
;
61 static int fc0013_init(struct dvb_frontend
*fe
)
63 struct fc0013_priv
*priv
= fe
->tuner_priv
;
65 unsigned char reg
[] = {
66 0x00, /* reg. 0x00: dummy */
73 0x0a, /* reg. 0x07: CHECK */
74 0xff, /* reg. 0x08: AGC Clock divide by 256, AGC gain 1/256,
76 0x6f, /* reg. 0x09: enable LoopThrough */
77 0xb8, /* reg. 0x0a: Disable LO Test Buffer */
78 0x82, /* reg. 0x0b: CHECK */
79 0xfc, /* reg. 0x0c: depending on AGC Up-Down mode, may need 0xf8 */
80 0x01, /* reg. 0x0d: AGC Not Forcing & LNA Forcing, may need 0x02 */
87 0x50, /* reg. 0x14: DVB-t High Gain, UHF.
88 Middle Gain: 0x48, Low Gain: 0x40 */
92 switch (priv
->xtal_freq
) {
94 case FC_XTAL_28_8_MHZ
:
102 if (priv
->dual_master
)
105 if (fe
->ops
.i2c_gate_ctrl
)
106 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
108 for (i
= 1; i
< sizeof(reg
); i
++) {
109 ret
= fc0013_writereg(priv
, i
, reg
[i
]);
114 if (fe
->ops
.i2c_gate_ctrl
)
115 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
118 err("fc0013_writereg failed: %d", ret
);
123 static int fc0013_sleep(struct dvb_frontend
*fe
)
125 /* nothing to do here */
129 int fc0013_rc_cal_add(struct dvb_frontend
*fe
, int rc_val
)
131 struct fc0013_priv
*priv
= fe
->tuner_priv
;
136 if (fe
->ops
.i2c_gate_ctrl
)
137 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
139 /* push rc_cal value, get rc_cal value */
140 ret
= fc0013_writereg(priv
, 0x10, 0x00);
144 /* get rc_cal value */
145 ret
= fc0013_readreg(priv
, 0x10, &rc_cal
);
151 val
= (int)rc_cal
+ rc_val
;
154 ret
= fc0013_writereg(priv
, 0x0d, 0x11);
158 /* modify rc_cal value */
160 ret
= fc0013_writereg(priv
, 0x10, 0x0f);
162 ret
= fc0013_writereg(priv
, 0x10, 0x00);
164 ret
= fc0013_writereg(priv
, 0x10, (u8
)val
);
167 if (fe
->ops
.i2c_gate_ctrl
)
168 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
172 EXPORT_SYMBOL(fc0013_rc_cal_add
);
174 int fc0013_rc_cal_reset(struct dvb_frontend
*fe
)
176 struct fc0013_priv
*priv
= fe
->tuner_priv
;
179 if (fe
->ops
.i2c_gate_ctrl
)
180 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
182 ret
= fc0013_writereg(priv
, 0x0d, 0x01);
184 ret
= fc0013_writereg(priv
, 0x10, 0x00);
186 if (fe
->ops
.i2c_gate_ctrl
)
187 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
191 EXPORT_SYMBOL(fc0013_rc_cal_reset
);
193 static int fc0013_set_vhf_track(struct fc0013_priv
*priv
, u32 freq
)
198 ret
= fc0013_readreg(priv
, 0x1d, &tmp
);
202 if (freq
<= 177500) { /* VHF Track: 7 */
203 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x1c);
204 } else if (freq
<= 184500) { /* VHF Track: 6 */
205 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x18);
206 } else if (freq
<= 191500) { /* VHF Track: 5 */
207 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x14);
208 } else if (freq
<= 198500) { /* VHF Track: 4 */
209 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x10);
210 } else if (freq
<= 205500) { /* VHF Track: 3 */
211 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x0c);
212 } else if (freq
<= 219500) { /* VHF Track: 2 */
213 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x08);
214 } else if (freq
< 300000) { /* VHF Track: 1 */
215 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x04);
216 } else { /* UHF and GPS */
217 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x1c);
223 static int fc0013_set_params(struct dvb_frontend
*fe
)
225 struct fc0013_priv
*priv
= fe
->tuner_priv
;
227 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
228 u32 freq
= p
->frequency
/ 1000;
229 u32 delsys
= p
->delivery_system
;
230 unsigned char reg
[7], am
, pm
, multi
, tmp
;
232 unsigned short xtal_freq_khz_2
, xin
, xdiv
;
233 bool vco_select
= false;
236 ret
= fe
->callback(priv
->i2c
, DVB_FRONTEND_COMPONENT_TUNER
,
237 FC_FE_CALLBACK_VHF_ENABLE
, (freq
> 300000 ? 0 : 1));
242 switch (priv
->xtal_freq
) {
244 xtal_freq_khz_2
= 27000 / 2;
247 xtal_freq_khz_2
= 36000 / 2;
249 case FC_XTAL_28_8_MHZ
:
251 xtal_freq_khz_2
= 28800 / 2;
255 if (fe
->ops
.i2c_gate_ctrl
)
256 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
259 ret
= fc0013_set_vhf_track(priv
, freq
);
264 /* enable VHF filter */
265 ret
= fc0013_readreg(priv
, 0x07, &tmp
);
268 ret
= fc0013_writereg(priv
, 0x07, tmp
| 0x10);
272 /* disable UHF & disable GPS */
273 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
276 ret
= fc0013_writereg(priv
, 0x14, tmp
& 0x1f);
279 } else if (freq
<= 862000) {
280 /* disable VHF filter */
281 ret
= fc0013_readreg(priv
, 0x07, &tmp
);
284 ret
= fc0013_writereg(priv
, 0x07, tmp
& 0xef);
288 /* enable UHF & disable GPS */
289 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
292 ret
= fc0013_writereg(priv
, 0x14, (tmp
& 0x1f) | 0x40);
296 /* disable VHF filter */
297 ret
= fc0013_readreg(priv
, 0x07, &tmp
);
300 ret
= fc0013_writereg(priv
, 0x07, tmp
& 0xef);
304 /* disable UHF & enable GPS */
305 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
308 ret
= fc0013_writereg(priv
, 0x14, (tmp
& 0x1f) | 0x20);
313 /* select frequency divider and the frequency of VCO */
314 if (freq
< 37084) { /* freq * 96 < 3560000 */
318 } else if (freq
< 55625) { /* freq * 64 < 3560000 */
322 } else if (freq
< 74167) { /* freq * 48 < 3560000 */
326 } else if (freq
< 111250) { /* freq * 32 < 3560000 */
330 } else if (freq
< 148334) { /* freq * 24 < 3560000 */
334 } else if (freq
< 222500) { /* freq * 16 < 3560000 */
338 } else if (freq
< 296667) { /* freq * 12 < 3560000 */
342 } else if (freq
< 445000) { /* freq * 8 < 3560000 */
346 } else if (freq
< 593334) { /* freq * 6 < 3560000 */
350 } else if (freq
< 950000) { /* freq * 4 < 3800000 */
360 f_vco
= freq
* multi
;
362 if (f_vco
>= 3060000) {
368 /* From divided value (XDIV) determined the FA and FP value */
369 xdiv
= (unsigned short)(f_vco
/ xtal_freq_khz_2
);
370 if ((f_vco
- xdiv
* xtal_freq_khz_2
) >= (xtal_freq_khz_2
/ 2))
373 pm
= (unsigned char)(xdiv
/ 8);
374 am
= (unsigned char)(xdiv
- (8 * pm
));
384 /* fix for frequency less than 45 MHz */
392 /* From VCO frequency determines the XIN ( fractional part of Delta
393 Sigma PLL) and divided value (XDIV) */
394 xin
= (unsigned short)(f_vco
- (f_vco
/ xtal_freq_khz_2
) * xtal_freq_khz_2
);
395 xin
= (xin
<< 15) / xtal_freq_khz_2
;
402 if (delsys
== SYS_DVBT
) {
403 reg
[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */
404 switch (p
->bandwidth_hz
) {
416 err("%s: modulation type not supported!", __func__
);
420 /* modified for Realtek demod */
423 for (i
= 1; i
<= 6; i
++) {
424 ret
= fc0013_writereg(priv
, i
, reg
[i
]);
429 ret
= fc0013_readreg(priv
, 0x11, &tmp
);
433 ret
= fc0013_writereg(priv
, 0x11, tmp
| 0x04);
435 ret
= fc0013_writereg(priv
, 0x11, tmp
& 0xfb);
439 /* VCO Calibration */
440 ret
= fc0013_writereg(priv
, 0x0e, 0x80);
442 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
444 /* VCO Re-Calibration if needed */
446 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
450 ret
= fc0013_readreg(priv
, 0x0e, &tmp
);
461 ret
= fc0013_writereg(priv
, 0x06, reg
[6]);
463 ret
= fc0013_writereg(priv
, 0x0e, 0x80);
465 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
470 ret
= fc0013_writereg(priv
, 0x06, reg
[6]);
472 ret
= fc0013_writereg(priv
, 0x0e, 0x80);
474 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
478 priv
->frequency
= p
->frequency
;
479 priv
->bandwidth
= p
->bandwidth_hz
;
482 if (fe
->ops
.i2c_gate_ctrl
)
483 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
485 warn("%s: failed: %d", __func__
, ret
);
489 static int fc0013_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
491 struct fc0013_priv
*priv
= fe
->tuner_priv
;
492 *frequency
= priv
->frequency
;
496 static int fc0013_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
503 static int fc0013_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
505 struct fc0013_priv
*priv
= fe
->tuner_priv
;
506 *bandwidth
= priv
->bandwidth
;
510 #define INPUT_ADC_LEVEL -8
512 static int fc0013_get_rf_strength(struct dvb_frontend
*fe
, u16
*strength
)
514 struct fc0013_priv
*priv
= fe
->tuner_priv
;
517 int int_temp
, lna_gain
, int_lna
, tot_agc_gain
, power
;
518 const int fc0013_lna_gain_table
[] = {
530 if (fe
->ops
.i2c_gate_ctrl
)
531 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
533 ret
= fc0013_writereg(priv
, 0x13, 0x00);
537 ret
= fc0013_readreg(priv
, 0x13, &tmp
);
542 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
545 lna_gain
= tmp
& 0x1f;
547 if (fe
->ops
.i2c_gate_ctrl
)
548 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
550 if (lna_gain
< ARRAY_SIZE(fc0013_lna_gain_table
)) {
551 int_lna
= fc0013_lna_gain_table
[lna_gain
];
552 tot_agc_gain
= (abs((int_temp
>> 5) - 7) - 2 +
553 (int_temp
& 0x1f)) * 2;
554 power
= INPUT_ADC_LEVEL
- tot_agc_gain
- int_lna
/ 10;
557 *strength
= 255; /* 100% */
558 else if (power
< -95)
561 *strength
= (power
+ 95) * 255 / 140;
563 *strength
|= *strength
<< 8;
571 if (fe
->ops
.i2c_gate_ctrl
)
572 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
575 warn("%s: failed: %d", __func__
, ret
);
579 static const struct dvb_tuner_ops fc0013_tuner_ops
= {
581 .name
= "Fitipower FC0013",
583 .frequency_min
= 37000000, /* estimate */
584 .frequency_max
= 1680000000, /* CHECK */
588 .release
= fc0013_release
,
591 .sleep
= fc0013_sleep
,
593 .set_params
= fc0013_set_params
,
595 .get_frequency
= fc0013_get_frequency
,
596 .get_if_frequency
= fc0013_get_if_frequency
,
597 .get_bandwidth
= fc0013_get_bandwidth
,
599 .get_rf_strength
= fc0013_get_rf_strength
,
602 struct dvb_frontend
*fc0013_attach(struct dvb_frontend
*fe
,
603 struct i2c_adapter
*i2c
, u8 i2c_address
, int dual_master
,
604 enum fc001x_xtal_freq xtal_freq
)
606 struct fc0013_priv
*priv
= NULL
;
608 priv
= kzalloc(sizeof(struct fc0013_priv
), GFP_KERNEL
);
613 priv
->dual_master
= dual_master
;
614 priv
->addr
= i2c_address
;
615 priv
->xtal_freq
= xtal_freq
;
617 info("Fitipower FC0013 successfully attached.");
619 fe
->tuner_priv
= priv
;
621 memcpy(&fe
->ops
.tuner_ops
, &fc0013_tuner_ops
,
622 sizeof(struct dvb_tuner_ops
));
626 EXPORT_SYMBOL(fc0013_attach
);
628 MODULE_DESCRIPTION("Fitipower FC0013 silicon tuner driver");
629 MODULE_AUTHOR("Hans-Frieder Vogt <hfvogt@gmx.net>");
630 MODULE_LICENSE("GPL");
631 MODULE_VERSION("0.2");