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);
226 static int fc0013_set_params(struct dvb_frontend
*fe
)
228 struct fc0013_priv
*priv
= fe
->tuner_priv
;
230 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
231 u32 freq
= p
->frequency
/ 1000;
232 u32 delsys
= p
->delivery_system
;
233 unsigned char reg
[7], am
, pm
, multi
, tmp
;
235 unsigned short xtal_freq_khz_2
, xin
, xdiv
;
236 bool vco_select
= false;
239 ret
= fe
->callback(priv
->i2c
, DVB_FRONTEND_COMPONENT_TUNER
,
240 FC_FE_CALLBACK_VHF_ENABLE
, (freq
> 300000 ? 0 : 1));
245 switch (priv
->xtal_freq
) {
247 xtal_freq_khz_2
= 27000 / 2;
250 xtal_freq_khz_2
= 36000 / 2;
252 case FC_XTAL_28_8_MHZ
:
254 xtal_freq_khz_2
= 28800 / 2;
258 if (fe
->ops
.i2c_gate_ctrl
)
259 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
262 ret
= fc0013_set_vhf_track(priv
, freq
);
267 /* enable VHF filter */
268 ret
= fc0013_readreg(priv
, 0x07, &tmp
);
271 ret
= fc0013_writereg(priv
, 0x07, tmp
| 0x10);
275 /* disable UHF & disable GPS */
276 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
279 ret
= fc0013_writereg(priv
, 0x14, tmp
& 0x1f);
282 } else if (freq
<= 862000) {
283 /* disable VHF filter */
284 ret
= fc0013_readreg(priv
, 0x07, &tmp
);
287 ret
= fc0013_writereg(priv
, 0x07, tmp
& 0xef);
291 /* enable UHF & disable GPS */
292 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
295 ret
= fc0013_writereg(priv
, 0x14, (tmp
& 0x1f) | 0x40);
299 /* disable VHF filter */
300 ret
= fc0013_readreg(priv
, 0x07, &tmp
);
303 ret
= fc0013_writereg(priv
, 0x07, tmp
& 0xef);
307 /* disable UHF & enable GPS */
308 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
311 ret
= fc0013_writereg(priv
, 0x14, (tmp
& 0x1f) | 0x20);
316 /* select frequency divider and the frequency of VCO */
317 if (freq
< 37084) { /* freq * 96 < 3560000 */
321 } else if (freq
< 55625) { /* freq * 64 < 3560000 */
325 } else if (freq
< 74167) { /* freq * 48 < 3560000 */
329 } else if (freq
< 111250) { /* freq * 32 < 3560000 */
333 } else if (freq
< 148334) { /* freq * 24 < 3560000 */
337 } else if (freq
< 222500) { /* freq * 16 < 3560000 */
341 } else if (freq
< 296667) { /* freq * 12 < 3560000 */
345 } else if (freq
< 445000) { /* freq * 8 < 3560000 */
349 } else if (freq
< 593334) { /* freq * 6 < 3560000 */
353 } else if (freq
< 950000) { /* freq * 4 < 3800000 */
363 f_vco
= freq
* multi
;
365 if (f_vco
>= 3060000) {
371 /* From divided value (XDIV) determined the FA and FP value */
372 xdiv
= (unsigned short)(f_vco
/ xtal_freq_khz_2
);
373 if ((f_vco
- xdiv
* xtal_freq_khz_2
) >= (xtal_freq_khz_2
/ 2))
376 pm
= (unsigned char)(xdiv
/ 8);
377 am
= (unsigned char)(xdiv
- (8 * pm
));
387 /* fix for frequency less than 45 MHz */
395 /* From VCO frequency determines the XIN ( fractional part of Delta
396 Sigma PLL) and divided value (XDIV) */
397 xin
= (unsigned short)(f_vco
- (f_vco
/ xtal_freq_khz_2
) * xtal_freq_khz_2
);
398 xin
= (xin
<< 15) / xtal_freq_khz_2
;
405 if (delsys
== SYS_DVBT
) {
406 reg
[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */
407 switch (p
->bandwidth_hz
) {
419 err("%s: modulation type not supported!", __func__
);
423 /* modified for Realtek demod */
426 for (i
= 1; i
<= 6; i
++) {
427 ret
= fc0013_writereg(priv
, i
, reg
[i
]);
432 ret
= fc0013_readreg(priv
, 0x11, &tmp
);
436 ret
= fc0013_writereg(priv
, 0x11, tmp
| 0x04);
438 ret
= fc0013_writereg(priv
, 0x11, tmp
& 0xfb);
442 /* VCO Calibration */
443 ret
= fc0013_writereg(priv
, 0x0e, 0x80);
445 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
447 /* VCO Re-Calibration if needed */
449 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
453 ret
= fc0013_readreg(priv
, 0x0e, &tmp
);
464 ret
= fc0013_writereg(priv
, 0x06, reg
[6]);
466 ret
= fc0013_writereg(priv
, 0x0e, 0x80);
468 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
473 ret
= fc0013_writereg(priv
, 0x06, reg
[6]);
475 ret
= fc0013_writereg(priv
, 0x0e, 0x80);
477 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
481 priv
->frequency
= p
->frequency
;
482 priv
->bandwidth
= p
->bandwidth_hz
;
485 if (fe
->ops
.i2c_gate_ctrl
)
486 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
488 warn("%s: failed: %d", __func__
, ret
);
492 static int fc0013_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
494 struct fc0013_priv
*priv
= fe
->tuner_priv
;
495 *frequency
= priv
->frequency
;
499 static int fc0013_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
506 static int fc0013_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
508 struct fc0013_priv
*priv
= fe
->tuner_priv
;
509 *bandwidth
= priv
->bandwidth
;
513 #define INPUT_ADC_LEVEL -8
515 static int fc0013_get_rf_strength(struct dvb_frontend
*fe
, u16
*strength
)
517 struct fc0013_priv
*priv
= fe
->tuner_priv
;
520 int int_temp
, lna_gain
, int_lna
, tot_agc_gain
, power
;
521 const int fc0013_lna_gain_table
[] = {
533 if (fe
->ops
.i2c_gate_ctrl
)
534 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
536 ret
= fc0013_writereg(priv
, 0x13, 0x00);
540 ret
= fc0013_readreg(priv
, 0x13, &tmp
);
545 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
548 lna_gain
= tmp
& 0x1f;
550 if (fe
->ops
.i2c_gate_ctrl
)
551 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
553 if (lna_gain
< ARRAY_SIZE(fc0013_lna_gain_table
)) {
554 int_lna
= fc0013_lna_gain_table
[lna_gain
];
555 tot_agc_gain
= (abs((int_temp
>> 5) - 7) - 2 +
556 (int_temp
& 0x1f)) * 2;
557 power
= INPUT_ADC_LEVEL
- tot_agc_gain
- int_lna
/ 10;
560 *strength
= 255; /* 100% */
561 else if (power
< -95)
564 *strength
= (power
+ 95) * 255 / 140;
566 *strength
|= *strength
<< 8;
574 if (fe
->ops
.i2c_gate_ctrl
)
575 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
578 warn("%s: failed: %d", __func__
, ret
);
582 static const struct dvb_tuner_ops fc0013_tuner_ops
= {
584 .name
= "Fitipower FC0013",
586 .frequency_min
= 37000000, /* estimate */
587 .frequency_max
= 1680000000, /* CHECK */
591 .release
= fc0013_release
,
594 .sleep
= fc0013_sleep
,
596 .set_params
= fc0013_set_params
,
598 .get_frequency
= fc0013_get_frequency
,
599 .get_if_frequency
= fc0013_get_if_frequency
,
600 .get_bandwidth
= fc0013_get_bandwidth
,
602 .get_rf_strength
= fc0013_get_rf_strength
,
605 struct dvb_frontend
*fc0013_attach(struct dvb_frontend
*fe
,
606 struct i2c_adapter
*i2c
, u8 i2c_address
, int dual_master
,
607 enum fc001x_xtal_freq xtal_freq
)
609 struct fc0013_priv
*priv
= NULL
;
611 priv
= kzalloc(sizeof(struct fc0013_priv
), GFP_KERNEL
);
616 priv
->dual_master
= dual_master
;
617 priv
->addr
= i2c_address
;
618 priv
->xtal_freq
= xtal_freq
;
620 info("Fitipower FC0013 successfully attached.");
622 fe
->tuner_priv
= priv
;
624 memcpy(&fe
->ops
.tuner_ops
, &fc0013_tuner_ops
,
625 sizeof(struct dvb_tuner_ops
));
629 EXPORT_SYMBOL(fc0013_attach
);
631 MODULE_DESCRIPTION("Fitipower FC0013 silicon tuner driver");
632 MODULE_AUTHOR("Hans-Frieder Vogt <hfvogt@gmx.net>");
633 MODULE_LICENSE("GPL");
634 MODULE_VERSION("0.2");