1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Fitipower FC0013 tuner driver
5 * Copyright (C) 2012 Hans-Frieder Vogt <hfvogt@gmx.net>
6 * partially based on driver code from Fitipower
7 * Copyright (C) 2010 Fitipower Integrated Technology Inc
11 #include "fc0013-priv.h"
13 static int fc0013_writereg(struct fc0013_priv
*priv
, u8 reg
, u8 val
)
15 u8 buf
[2] = {reg
, val
};
16 struct i2c_msg msg
= {
17 .addr
= priv
->addr
, .flags
= 0, .buf
= buf
, .len
= 2
20 if (i2c_transfer(priv
->i2c
, &msg
, 1) != 1) {
21 err("I2C write reg failed, reg: %02x, val: %02x", reg
, val
);
27 static int fc0013_readreg(struct fc0013_priv
*priv
, u8 reg
, u8
*val
)
29 struct i2c_msg msg
[2] = {
30 { .addr
= priv
->addr
, .flags
= 0, .buf
= ®
, .len
= 1 },
31 { .addr
= priv
->addr
, .flags
= I2C_M_RD
, .buf
= val
, .len
= 1 },
34 if (i2c_transfer(priv
->i2c
, msg
, 2) != 2) {
35 err("I2C read reg failed, reg: %02x", reg
);
41 static void fc0013_release(struct dvb_frontend
*fe
)
43 kfree(fe
->tuner_priv
);
44 fe
->tuner_priv
= NULL
;
47 static int fc0013_init(struct dvb_frontend
*fe
)
49 struct fc0013_priv
*priv
= fe
->tuner_priv
;
51 unsigned char reg
[] = {
52 0x00, /* reg. 0x00: dummy */
59 0x0a, /* reg. 0x07: CHECK */
60 0xff, /* reg. 0x08: AGC Clock divide by 256, AGC gain 1/256,
62 0x6f, /* reg. 0x09: enable LoopThrough */
63 0xb8, /* reg. 0x0a: Disable LO Test Buffer */
64 0x82, /* reg. 0x0b: CHECK */
65 0xfc, /* reg. 0x0c: depending on AGC Up-Down mode, may need 0xf8 */
66 0x01, /* reg. 0x0d: AGC Not Forcing & LNA Forcing, may need 0x02 */
73 0x50, /* reg. 0x14: DVB-t High Gain, UHF.
74 Middle Gain: 0x48, Low Gain: 0x40 */
78 switch (priv
->xtal_freq
) {
80 case FC_XTAL_28_8_MHZ
:
88 if (priv
->dual_master
)
91 if (fe
->ops
.i2c_gate_ctrl
)
92 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
94 for (i
= 1; i
< sizeof(reg
); i
++) {
95 ret
= fc0013_writereg(priv
, i
, reg
[i
]);
100 if (fe
->ops
.i2c_gate_ctrl
)
101 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
104 err("fc0013_writereg failed: %d", ret
);
109 static int fc0013_sleep(struct dvb_frontend
*fe
)
111 /* nothing to do here */
115 int fc0013_rc_cal_add(struct dvb_frontend
*fe
, int rc_val
)
117 struct fc0013_priv
*priv
= fe
->tuner_priv
;
122 if (fe
->ops
.i2c_gate_ctrl
)
123 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
125 /* push rc_cal value, get rc_cal value */
126 ret
= fc0013_writereg(priv
, 0x10, 0x00);
130 /* get rc_cal value */
131 ret
= fc0013_readreg(priv
, 0x10, &rc_cal
);
137 val
= (int)rc_cal
+ rc_val
;
140 ret
= fc0013_writereg(priv
, 0x0d, 0x11);
144 /* modify rc_cal value */
146 ret
= fc0013_writereg(priv
, 0x10, 0x0f);
148 ret
= fc0013_writereg(priv
, 0x10, 0x00);
150 ret
= fc0013_writereg(priv
, 0x10, (u8
)val
);
153 if (fe
->ops
.i2c_gate_ctrl
)
154 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
158 EXPORT_SYMBOL(fc0013_rc_cal_add
);
160 int fc0013_rc_cal_reset(struct dvb_frontend
*fe
)
162 struct fc0013_priv
*priv
= fe
->tuner_priv
;
165 if (fe
->ops
.i2c_gate_ctrl
)
166 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
168 ret
= fc0013_writereg(priv
, 0x0d, 0x01);
170 ret
= fc0013_writereg(priv
, 0x10, 0x00);
172 if (fe
->ops
.i2c_gate_ctrl
)
173 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
177 EXPORT_SYMBOL(fc0013_rc_cal_reset
);
179 static int fc0013_set_vhf_track(struct fc0013_priv
*priv
, u32 freq
)
184 ret
= fc0013_readreg(priv
, 0x1d, &tmp
);
188 if (freq
<= 177500) { /* VHF Track: 7 */
189 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x1c);
190 } else if (freq
<= 184500) { /* VHF Track: 6 */
191 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x18);
192 } else if (freq
<= 191500) { /* VHF Track: 5 */
193 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x14);
194 } else if (freq
<= 198500) { /* VHF Track: 4 */
195 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x10);
196 } else if (freq
<= 205500) { /* VHF Track: 3 */
197 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x0c);
198 } else if (freq
<= 219500) { /* VHF Track: 2 */
199 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x08);
200 } else if (freq
< 300000) { /* VHF Track: 1 */
201 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x04);
202 } else { /* UHF and GPS */
203 ret
= fc0013_writereg(priv
, 0x1d, tmp
| 0x1c);
209 static int fc0013_set_params(struct dvb_frontend
*fe
)
211 struct fc0013_priv
*priv
= fe
->tuner_priv
;
213 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
214 u32 freq
= p
->frequency
/ 1000;
215 u32 delsys
= p
->delivery_system
;
216 unsigned char reg
[7], am
, pm
, multi
, tmp
;
218 unsigned short xtal_freq_khz_2
, xin
, xdiv
;
219 bool vco_select
= false;
222 ret
= fe
->callback(priv
->i2c
, DVB_FRONTEND_COMPONENT_TUNER
,
223 FC_FE_CALLBACK_VHF_ENABLE
, (freq
> 300000 ? 0 : 1));
228 switch (priv
->xtal_freq
) {
230 xtal_freq_khz_2
= 27000 / 2;
233 xtal_freq_khz_2
= 36000 / 2;
235 case FC_XTAL_28_8_MHZ
:
237 xtal_freq_khz_2
= 28800 / 2;
241 if (fe
->ops
.i2c_gate_ctrl
)
242 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
245 ret
= fc0013_set_vhf_track(priv
, freq
);
250 /* enable VHF filter */
251 ret
= fc0013_readreg(priv
, 0x07, &tmp
);
254 ret
= fc0013_writereg(priv
, 0x07, tmp
| 0x10);
258 /* disable UHF & disable GPS */
259 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
262 ret
= fc0013_writereg(priv
, 0x14, tmp
& 0x1f);
265 } else if (freq
<= 862000) {
266 /* disable VHF filter */
267 ret
= fc0013_readreg(priv
, 0x07, &tmp
);
270 ret
= fc0013_writereg(priv
, 0x07, tmp
& 0xef);
274 /* enable UHF & disable GPS */
275 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
278 ret
= fc0013_writereg(priv
, 0x14, (tmp
& 0x1f) | 0x40);
282 /* disable VHF filter */
283 ret
= fc0013_readreg(priv
, 0x07, &tmp
);
286 ret
= fc0013_writereg(priv
, 0x07, tmp
& 0xef);
290 /* disable UHF & enable GPS */
291 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
294 ret
= fc0013_writereg(priv
, 0x14, (tmp
& 0x1f) | 0x20);
299 /* select frequency divider and the frequency of VCO */
300 if (freq
< 37084) { /* freq * 96 < 3560000 */
304 } else if (freq
< 55625) { /* freq * 64 < 3560000 */
308 } else if (freq
< 74167) { /* freq * 48 < 3560000 */
312 } else if (freq
< 111250) { /* freq * 32 < 3560000 */
316 } else if (freq
< 148334) { /* freq * 24 < 3560000 */
320 } else if (freq
< 222500) { /* freq * 16 < 3560000 */
324 } else if (freq
< 296667) { /* freq * 12 < 3560000 */
328 } else if (freq
< 445000) { /* freq * 8 < 3560000 */
332 } else if (freq
< 593334) { /* freq * 6 < 3560000 */
336 } else if (freq
< 950000) { /* freq * 4 < 3800000 */
346 f_vco
= freq
* multi
;
348 if (f_vco
>= 3060000) {
354 /* From divided value (XDIV) determined the FA and FP value */
355 xdiv
= (unsigned short)(f_vco
/ xtal_freq_khz_2
);
356 if ((f_vco
- xdiv
* xtal_freq_khz_2
) >= (xtal_freq_khz_2
/ 2))
359 pm
= (unsigned char)(xdiv
/ 8);
360 am
= (unsigned char)(xdiv
- (8 * pm
));
370 /* fix for frequency less than 45 MHz */
378 /* From VCO frequency determines the XIN ( fractional part of Delta
379 Sigma PLL) and divided value (XDIV) */
380 xin
= (unsigned short)(f_vco
- (f_vco
/ xtal_freq_khz_2
) * xtal_freq_khz_2
);
381 xin
= (xin
<< 15) / xtal_freq_khz_2
;
388 if (delsys
== SYS_DVBT
) {
389 reg
[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */
390 switch (p
->bandwidth_hz
) {
402 err("%s: modulation type not supported!", __func__
);
406 /* modified for Realtek demod */
409 for (i
= 1; i
<= 6; i
++) {
410 ret
= fc0013_writereg(priv
, i
, reg
[i
]);
415 ret
= fc0013_readreg(priv
, 0x11, &tmp
);
419 ret
= fc0013_writereg(priv
, 0x11, tmp
| 0x04);
421 ret
= fc0013_writereg(priv
, 0x11, tmp
& 0xfb);
425 /* VCO Calibration */
426 ret
= fc0013_writereg(priv
, 0x0e, 0x80);
428 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
430 /* VCO Re-Calibration if needed */
432 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
436 ret
= fc0013_readreg(priv
, 0x0e, &tmp
);
447 ret
= fc0013_writereg(priv
, 0x06, reg
[6]);
449 ret
= fc0013_writereg(priv
, 0x0e, 0x80);
451 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
456 ret
= fc0013_writereg(priv
, 0x06, reg
[6]);
458 ret
= fc0013_writereg(priv
, 0x0e, 0x80);
460 ret
= fc0013_writereg(priv
, 0x0e, 0x00);
464 priv
->frequency
= p
->frequency
;
465 priv
->bandwidth
= p
->bandwidth_hz
;
468 if (fe
->ops
.i2c_gate_ctrl
)
469 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
471 warn("%s: failed: %d", __func__
, ret
);
475 static int fc0013_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
477 struct fc0013_priv
*priv
= fe
->tuner_priv
;
478 *frequency
= priv
->frequency
;
482 static int fc0013_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
489 static int fc0013_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
491 struct fc0013_priv
*priv
= fe
->tuner_priv
;
492 *bandwidth
= priv
->bandwidth
;
496 #define INPUT_ADC_LEVEL -8
498 static int fc0013_get_rf_strength(struct dvb_frontend
*fe
, u16
*strength
)
500 struct fc0013_priv
*priv
= fe
->tuner_priv
;
503 int int_temp
, lna_gain
, int_lna
, tot_agc_gain
, power
;
504 static const int fc0013_lna_gain_table
[] = {
516 if (fe
->ops
.i2c_gate_ctrl
)
517 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
519 ret
= fc0013_writereg(priv
, 0x13, 0x00);
523 ret
= fc0013_readreg(priv
, 0x13, &tmp
);
528 ret
= fc0013_readreg(priv
, 0x14, &tmp
);
531 lna_gain
= tmp
& 0x1f;
533 if (fe
->ops
.i2c_gate_ctrl
)
534 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
536 if (lna_gain
< ARRAY_SIZE(fc0013_lna_gain_table
)) {
537 int_lna
= fc0013_lna_gain_table
[lna_gain
];
538 tot_agc_gain
= (abs((int_temp
>> 5) - 7) - 2 +
539 (int_temp
& 0x1f)) * 2;
540 power
= INPUT_ADC_LEVEL
- tot_agc_gain
- int_lna
/ 10;
543 *strength
= 255; /* 100% */
544 else if (power
< -95)
547 *strength
= (power
+ 95) * 255 / 140;
549 *strength
|= *strength
<< 8;
557 if (fe
->ops
.i2c_gate_ctrl
)
558 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
561 warn("%s: failed: %d", __func__
, ret
);
565 static const struct dvb_tuner_ops fc0013_tuner_ops
= {
567 .name
= "Fitipower FC0013",
569 .frequency_min_hz
= 37 * MHz
, /* estimate */
570 .frequency_max_hz
= 1680 * MHz
, /* CHECK */
573 .release
= fc0013_release
,
576 .sleep
= fc0013_sleep
,
578 .set_params
= fc0013_set_params
,
580 .get_frequency
= fc0013_get_frequency
,
581 .get_if_frequency
= fc0013_get_if_frequency
,
582 .get_bandwidth
= fc0013_get_bandwidth
,
584 .get_rf_strength
= fc0013_get_rf_strength
,
587 struct dvb_frontend
*fc0013_attach(struct dvb_frontend
*fe
,
588 struct i2c_adapter
*i2c
, u8 i2c_address
, int dual_master
,
589 enum fc001x_xtal_freq xtal_freq
)
591 struct fc0013_priv
*priv
= NULL
;
593 priv
= kzalloc(sizeof(struct fc0013_priv
), GFP_KERNEL
);
598 priv
->dual_master
= dual_master
;
599 priv
->addr
= i2c_address
;
600 priv
->xtal_freq
= xtal_freq
;
602 info("Fitipower FC0013 successfully attached.");
604 fe
->tuner_priv
= priv
;
606 memcpy(&fe
->ops
.tuner_ops
, &fc0013_tuner_ops
,
607 sizeof(struct dvb_tuner_ops
));
611 EXPORT_SYMBOL(fc0013_attach
);
613 MODULE_DESCRIPTION("Fitipower FC0013 silicon tuner driver");
614 MODULE_AUTHOR("Hans-Frieder Vogt <hfvogt@gmx.net>");
615 MODULE_LICENSE("GPL");
616 MODULE_VERSION("0.2");