2 * Fitipower FC0012 tuner driver
4 * Copyright (C) 2012 Hans-Frieder Vogt <hfvogt@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "fc0012-priv.h"
24 static int fc0012_writereg(struct fc0012_priv
*priv
, u8 reg
, u8 val
)
26 u8 buf
[2] = {reg
, val
};
27 struct i2c_msg msg
= {
28 .addr
= priv
->addr
, .flags
= 0, .buf
= buf
, .len
= 2
31 if (i2c_transfer(priv
->i2c
, &msg
, 1) != 1) {
32 err("I2C write reg failed, reg: %02x, val: %02x", reg
, val
);
38 static int fc0012_readreg(struct fc0012_priv
*priv
, u8 reg
, u8
*val
)
40 struct i2c_msg msg
[2] = {
41 { .addr
= priv
->addr
, .flags
= 0, .buf
= ®
, .len
= 1 },
42 { .addr
= priv
->addr
, .flags
= I2C_M_RD
, .buf
= val
, .len
= 1 },
45 if (i2c_transfer(priv
->i2c
, msg
, 2) != 2) {
46 err("I2C read reg failed, reg: %02x", reg
);
52 static int fc0012_release(struct dvb_frontend
*fe
)
54 kfree(fe
->tuner_priv
);
55 fe
->tuner_priv
= NULL
;
59 static int fc0012_init(struct dvb_frontend
*fe
)
61 struct fc0012_priv
*priv
= fe
->tuner_priv
;
63 unsigned char reg
[] = {
64 0x00, /* dummy reg. 0 */
69 0x0f, /* reg. 0x05: may also be 0x0a */
70 0x00, /* reg. 0x06: divider 2, VCO slow */
71 0x00, /* reg. 0x07: may also be 0x0f */
72 0xff, /* reg. 0x08: AGC Clock divide by 256, AGC gain 1/256,
74 0x6e, /* reg. 0x09: Disable LoopThrough, Enable LoopThrough: 0x6f */
75 0xb8, /* reg. 0x0a: Disable LO Test Buffer */
76 0x82, /* reg. 0x0b: Output Clock is same as clock frequency,
78 0xfc, /* reg. 0x0c: depending on AGC Up-Down mode, may need 0xf8 */
79 0x02, /* reg. 0x0d: AGC Not Forcing & LNA Forcing, 0x02 for DVB-T */
82 0x00, /* reg. 0x10: may also be 0x0d */
84 0x1f, /* reg. 0x12: Set to maximum gain */
85 0x08, /* reg. 0x13: Set to Middle Gain: 0x08,
86 Low Gain: 0x00, High Gain: 0x10, enable IX2: 0x80 */
88 0x04, /* reg. 0x15: Enable LNA COMPS */
91 switch (priv
->xtal_freq
) {
93 case FC_XTAL_28_8_MHZ
:
101 if (priv
->dual_master
)
104 if (fe
->ops
.i2c_gate_ctrl
)
105 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
107 for (i
= 1; i
< sizeof(reg
); i
++) {
108 ret
= fc0012_writereg(priv
, i
, reg
[i
]);
113 if (fe
->ops
.i2c_gate_ctrl
)
114 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
117 err("fc0012_writereg failed: %d", ret
);
122 static int fc0012_sleep(struct dvb_frontend
*fe
)
124 /* nothing to do here */
128 static int fc0012_set_params(struct dvb_frontend
*fe
)
130 struct fc0012_priv
*priv
= fe
->tuner_priv
;
132 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
133 u32 freq
= p
->frequency
/ 1000;
134 u32 delsys
= p
->delivery_system
;
135 unsigned char reg
[7], am
, pm
, multi
, tmp
;
137 unsigned short xtal_freq_khz_2
, xin
, xdiv
;
138 int vco_select
= false;
141 ret
= fe
->callback(priv
->i2c
, DVB_FRONTEND_COMPONENT_TUNER
,
142 FC_FE_CALLBACK_VHF_ENABLE
, (freq
> 300000 ? 0 : 1));
147 switch (priv
->xtal_freq
) {
149 xtal_freq_khz_2
= 27000 / 2;
152 xtal_freq_khz_2
= 36000 / 2;
154 case FC_XTAL_28_8_MHZ
:
156 xtal_freq_khz_2
= 28800 / 2;
160 /* select frequency divider and the frequency of VCO */
161 if (freq
< 37084) { /* freq * 96 < 3560000 */
165 } else if (freq
< 55625) { /* freq * 64 < 3560000 */
169 } else if (freq
< 74167) { /* freq * 48 < 3560000 */
173 } else if (freq
< 111250) { /* freq * 32 < 3560000 */
177 } else if (freq
< 148334) { /* freq * 24 < 3560000 */
181 } else if (freq
< 222500) { /* freq * 16 < 3560000 */
185 } else if (freq
< 296667) { /* freq * 12 < 3560000 */
189 } else if (freq
< 445000) { /* freq * 8 < 3560000 */
193 } else if (freq
< 593334) { /* freq * 6 < 3560000 */
203 f_vco
= freq
* multi
;
205 if (f_vco
>= 3060000) {
211 /* From divided value (XDIV) determined the FA and FP value */
212 xdiv
= (unsigned short)(f_vco
/ xtal_freq_khz_2
);
213 if ((f_vco
- xdiv
* xtal_freq_khz_2
) >= (xtal_freq_khz_2
/ 2))
216 pm
= (unsigned char)(xdiv
/ 8);
217 am
= (unsigned char)(xdiv
- (8 * pm
));
227 /* fix for frequency less than 45 MHz */
235 /* From VCO frequency determines the XIN ( fractional part of Delta
236 Sigma PLL) and divided value (XDIV) */
237 xin
= (unsigned short)(f_vco
- (f_vco
/ xtal_freq_khz_2
) * xtal_freq_khz_2
);
238 xin
= (xin
<< 15) / xtal_freq_khz_2
;
242 reg
[3] = xin
>> 8; /* xin with 9 bit resolution */
245 if (delsys
== SYS_DVBT
) {
246 reg
[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */
247 switch (p
->bandwidth_hz
) {
259 err("%s: modulation type not supported!", __func__
);
263 /* modified for Realtek demod */
266 if (fe
->ops
.i2c_gate_ctrl
)
267 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
269 for (i
= 1; i
<= 6; i
++) {
270 ret
= fc0012_writereg(priv
, i
, reg
[i
]);
275 /* VCO Calibration */
276 ret
= fc0012_writereg(priv
, 0x0e, 0x80);
278 ret
= fc0012_writereg(priv
, 0x0e, 0x00);
280 /* VCO Re-Calibration if needed */
282 ret
= fc0012_writereg(priv
, 0x0e, 0x00);
286 ret
= fc0012_readreg(priv
, 0x0e, &tmp
);
297 ret
= fc0012_writereg(priv
, 0x06, reg
[6]);
299 ret
= fc0012_writereg(priv
, 0x0e, 0x80);
301 ret
= fc0012_writereg(priv
, 0x0e, 0x00);
306 ret
= fc0012_writereg(priv
, 0x06, reg
[6]);
308 ret
= fc0012_writereg(priv
, 0x0e, 0x80);
310 ret
= fc0012_writereg(priv
, 0x0e, 0x00);
314 priv
->frequency
= p
->frequency
;
315 priv
->bandwidth
= p
->bandwidth_hz
;
318 if (fe
->ops
.i2c_gate_ctrl
)
319 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
321 warn("%s: failed: %d", __func__
, ret
);
325 static int fc0012_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
327 struct fc0012_priv
*priv
= fe
->tuner_priv
;
328 *frequency
= priv
->frequency
;
332 static int fc0012_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
334 /* CHECK: always ? */
339 static int fc0012_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
341 struct fc0012_priv
*priv
= fe
->tuner_priv
;
342 *bandwidth
= priv
->bandwidth
;
346 #define INPUT_ADC_LEVEL -8
348 static int fc0012_get_rf_strength(struct dvb_frontend
*fe
, u16
*strength
)
350 struct fc0012_priv
*priv
= fe
->tuner_priv
;
353 int int_temp
, lna_gain
, int_lna
, tot_agc_gain
, power
;
354 const int fc0012_lna_gain_table
[] = {
366 if (fe
->ops
.i2c_gate_ctrl
)
367 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
369 ret
= fc0012_writereg(priv
, 0x12, 0x00);
373 ret
= fc0012_readreg(priv
, 0x12, &tmp
);
378 ret
= fc0012_readreg(priv
, 0x13, &tmp
);
381 lna_gain
= tmp
& 0x1f;
383 if (fe
->ops
.i2c_gate_ctrl
)
384 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
386 if (lna_gain
< ARRAY_SIZE(fc0012_lna_gain_table
)) {
387 int_lna
= fc0012_lna_gain_table
[lna_gain
];
388 tot_agc_gain
= (abs((int_temp
>> 5) - 7) - 2 +
389 (int_temp
& 0x1f)) * 2;
390 power
= INPUT_ADC_LEVEL
- tot_agc_gain
- int_lna
/ 10;
393 *strength
= 255; /* 100% */
394 else if (power
< -95)
397 *strength
= (power
+ 95) * 255 / 140;
399 *strength
|= *strength
<< 8;
407 if (fe
->ops
.i2c_gate_ctrl
)
408 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
411 warn("%s: failed: %d", __func__
, ret
);
415 static const struct dvb_tuner_ops fc0012_tuner_ops
= {
417 .name
= "Fitipower FC0012",
419 .frequency_min
= 37000000, /* estimate */
420 .frequency_max
= 862000000, /* estimate */
424 .release
= fc0012_release
,
427 .sleep
= fc0012_sleep
,
429 .set_params
= fc0012_set_params
,
431 .get_frequency
= fc0012_get_frequency
,
432 .get_if_frequency
= fc0012_get_if_frequency
,
433 .get_bandwidth
= fc0012_get_bandwidth
,
435 .get_rf_strength
= fc0012_get_rf_strength
,
438 struct dvb_frontend
*fc0012_attach(struct dvb_frontend
*fe
,
439 struct i2c_adapter
*i2c
, u8 i2c_address
, int dual_master
,
440 enum fc001x_xtal_freq xtal_freq
)
442 struct fc0012_priv
*priv
= NULL
;
444 priv
= kzalloc(sizeof(struct fc0012_priv
), GFP_KERNEL
);
449 priv
->dual_master
= dual_master
;
450 priv
->addr
= i2c_address
;
451 priv
->xtal_freq
= xtal_freq
;
453 info("Fitipower FC0012 successfully attached.");
455 fe
->tuner_priv
= priv
;
457 memcpy(&fe
->ops
.tuner_ops
, &fc0012_tuner_ops
,
458 sizeof(struct dvb_tuner_ops
));
462 EXPORT_SYMBOL(fc0012_attach
);
464 MODULE_DESCRIPTION("Fitipower FC0012 silicon tuner driver");
465 MODULE_AUTHOR("Hans-Frieder Vogt <hfvogt@gmx.net>");
466 MODULE_LICENSE("GPL");
467 MODULE_VERSION("0.6");