2 * Fitipower FC0011 tuner driver
4 * Copyright (C) 2012 Michael Buesch <m@bues.ch>
6 * Derived from FC0012 tuner driver:
7 * Copyright (C) 2012 Hans-Frieder Vogt <hfvogt@gmx.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
28 FC11_REG_XINHI
, /* XIN high 8 bit */
29 FC11_REG_XINLO
, /* XIN low 8 bit */
30 FC11_REG_VCO
, /* VCO */
31 FC11_REG_VCOSEL
, /* VCO select */
32 FC11_REG_7
, /* Unknown tuner reg 7 */
33 FC11_REG_8
, /* Unknown tuner reg 8 */
35 FC11_REG_10
, /* Unknown tuner reg 10 */
36 FC11_REG_11
, /* Unknown tuner reg 11 */
38 FC11_REG_RCCAL
, /* RC calibrate */
39 FC11_REG_VCOCAL
, /* VCO calibrate */
41 FC11_REG_16
, /* Unknown tuner reg 16 */
44 FC11_NR_REGS
, /* Number of registers */
47 enum FC11_REG_VCOSEL_bits
{
48 FC11_VCOSEL_2
= 0x08, /* VCO select 2 */
49 FC11_VCOSEL_1
= 0x10, /* VCO select 1 */
50 FC11_VCOSEL_CLKOUT
= 0x20, /* Fix clock out */
51 FC11_VCOSEL_BW7M
= 0x40, /* 7MHz bw */
52 FC11_VCOSEL_BW6M
= 0x80, /* 6MHz bw */
55 enum FC11_REG_RCCAL_bits
{
56 FC11_RCCAL_FORCE
= 0x10, /* force */
59 enum FC11_REG_VCOCAL_bits
{
60 FC11_VCOCAL_RUN
= 0, /* VCO calibration run */
61 FC11_VCOCAL_VALUEMASK
= 0x3F, /* VCO calibration value mask */
62 FC11_VCOCAL_OK
= 0x40, /* VCO calibration Ok */
63 FC11_VCOCAL_RESET
= 0x80, /* VCO calibration reset */
68 struct i2c_adapter
*i2c
;
76 static int fc0011_writereg(struct fc0011_priv
*priv
, u8 reg
, u8 val
)
78 u8 buf
[2] = { reg
, val
};
79 struct i2c_msg msg
= { .addr
= priv
->addr
,
80 .flags
= 0, .buf
= buf
, .len
= 2 };
82 if (i2c_transfer(priv
->i2c
, &msg
, 1) != 1) {
83 dev_err(&priv
->i2c
->dev
,
84 "I2C write reg failed, reg: %02x, val: %02x\n",
92 static int fc0011_readreg(struct fc0011_priv
*priv
, u8 reg
, u8
*val
)
95 struct i2c_msg msg
[2] = {
97 .flags
= 0, .buf
= ®
, .len
= 1 },
99 .flags
= I2C_M_RD
, .buf
= val
? : &dummy
, .len
= 1 },
102 if (i2c_transfer(priv
->i2c
, msg
, 2) != 2) {
103 dev_err(&priv
->i2c
->dev
,
104 "I2C read failed, reg: %02x\n", reg
);
111 static void fc0011_release(struct dvb_frontend
*fe
)
113 kfree(fe
->tuner_priv
);
114 fe
->tuner_priv
= NULL
;
117 static int fc0011_init(struct dvb_frontend
*fe
)
119 struct fc0011_priv
*priv
= fe
->tuner_priv
;
122 if (WARN_ON(!fe
->callback
))
125 err
= fe
->callback(priv
->i2c
, DVB_FRONTEND_COMPONENT_TUNER
,
126 FC0011_FE_CALLBACK_POWER
, priv
->addr
);
128 dev_err(&priv
->i2c
->dev
, "Power-on callback failed\n");
131 err
= fe
->callback(priv
->i2c
, DVB_FRONTEND_COMPONENT_TUNER
,
132 FC0011_FE_CALLBACK_RESET
, priv
->addr
);
134 dev_err(&priv
->i2c
->dev
, "Reset callback failed\n");
141 /* Initiate VCO calibration */
142 static int fc0011_vcocal_trigger(struct fc0011_priv
*priv
)
146 err
= fc0011_writereg(priv
, FC11_REG_VCOCAL
, FC11_VCOCAL_RESET
);
149 err
= fc0011_writereg(priv
, FC11_REG_VCOCAL
, FC11_VCOCAL_RUN
);
156 /* Read VCO calibration value */
157 static int fc0011_vcocal_read(struct fc0011_priv
*priv
, u8
*value
)
161 err
= fc0011_writereg(priv
, FC11_REG_VCOCAL
, FC11_VCOCAL_RUN
);
164 usleep_range(10000, 20000);
165 err
= fc0011_readreg(priv
, FC11_REG_VCOCAL
, value
);
172 static int fc0011_set_params(struct dvb_frontend
*fe
)
174 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
175 struct fc0011_priv
*priv
= fe
->tuner_priv
;
177 unsigned int i
, vco_retries
;
178 u32 freq
= p
->frequency
/ 1000;
179 u32 bandwidth
= p
->bandwidth_hz
/ 1000;
180 u32 fvco
, xin
, frac
, xdiv
, xdivr
;
181 u8 fa
, fp
, vco_sel
, vco_cal
;
182 u8 regs
[FC11_NR_REGS
] = { };
184 regs
[FC11_REG_7
] = 0x0F;
185 regs
[FC11_REG_8
] = 0x3E;
186 regs
[FC11_REG_10
] = 0xB8;
187 regs
[FC11_REG_11
] = 0x80;
188 regs
[FC11_REG_RCCAL
] = 0x04;
189 err
= fc0011_writereg(priv
, FC11_REG_7
, regs
[FC11_REG_7
]);
190 err
|= fc0011_writereg(priv
, FC11_REG_8
, regs
[FC11_REG_8
]);
191 err
|= fc0011_writereg(priv
, FC11_REG_10
, regs
[FC11_REG_10
]);
192 err
|= fc0011_writereg(priv
, FC11_REG_11
, regs
[FC11_REG_11
]);
193 err
|= fc0011_writereg(priv
, FC11_REG_RCCAL
, regs
[FC11_REG_RCCAL
]);
197 /* Set VCO freq and VCO div */
200 regs
[FC11_REG_VCO
] = 0x82;
201 } else if (freq
< 108000) {
203 regs
[FC11_REG_VCO
] = 0x42;
204 } else if (freq
< 216000) {
206 regs
[FC11_REG_VCO
] = 0x22;
207 } else if (freq
< 432000) {
209 regs
[FC11_REG_VCO
] = 0x12;
212 regs
[FC11_REG_VCO
] = 0x0A;
215 /* Calc XIN. The PLL reference frequency is 18 MHz. */
217 WARN_ON(xdiv
> 0xFF);
218 frac
= fvco
- xdiv
* 18000;
219 frac
= (frac
<< 15) / 18000;
225 xin
= clamp_t(u32
, frac
, 512, 65024);
226 regs
[FC11_REG_XINHI
] = xin
>> 8;
227 regs
[FC11_REG_XINLO
] = xin
;
231 if (fvco
- xdiv
* 18000 >= 9000)
232 xdivr
+= 1; /* round */
244 dev_warn(&priv
->i2c
->dev
,
245 "fa %02X >= fp %02X, but trying to continue\n",
246 (unsigned int)(u8
)fa
, (unsigned int)(u8
)fp
);
248 regs
[FC11_REG_FA
] = fa
;
249 regs
[FC11_REG_FP
] = fp
;
251 /* Select bandwidth */
256 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_BW7M
;
259 dev_warn(&priv
->i2c
->dev
, "Unsupported bandwidth %u kHz. Using 6000 kHz.\n",
264 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_BW6M
;
269 if (fvco
< 2320000) {
271 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
272 } else if (fvco
< 3080000) {
274 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
275 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_1
;
278 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
279 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_2
;
282 /* Fix for low freqs */
284 regs
[FC11_REG_FA
] = 0x6;
285 regs
[FC11_REG_FP
] = 0x11;
289 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_CLKOUT
;
291 /* Write the cached registers */
292 for (i
= FC11_REG_FA
; i
<= FC11_REG_VCOSEL
; i
++) {
293 err
= fc0011_writereg(priv
, i
, regs
[i
]);
298 /* VCO calibration */
299 err
= fc0011_vcocal_trigger(priv
);
302 err
= fc0011_vcocal_read(priv
, &vco_cal
);
306 while (!(vco_cal
& FC11_VCOCAL_OK
) && vco_retries
< 3) {
307 /* Reset the tuner and try again */
308 err
= fe
->callback(priv
->i2c
, DVB_FRONTEND_COMPONENT_TUNER
,
309 FC0011_FE_CALLBACK_RESET
, priv
->addr
);
311 dev_err(&priv
->i2c
->dev
, "Failed to reset tuner\n");
314 /* Reinit tuner config */
316 for (i
= FC11_REG_FA
; i
<= FC11_REG_VCOSEL
; i
++)
317 err
|= fc0011_writereg(priv
, i
, regs
[i
]);
318 err
|= fc0011_writereg(priv
, FC11_REG_7
, regs
[FC11_REG_7
]);
319 err
|= fc0011_writereg(priv
, FC11_REG_8
, regs
[FC11_REG_8
]);
320 err
|= fc0011_writereg(priv
, FC11_REG_10
, regs
[FC11_REG_10
]);
321 err
|= fc0011_writereg(priv
, FC11_REG_11
, regs
[FC11_REG_11
]);
322 err
|= fc0011_writereg(priv
, FC11_REG_RCCAL
, regs
[FC11_REG_RCCAL
]);
325 /* VCO calibration */
326 err
= fc0011_vcocal_trigger(priv
);
329 err
= fc0011_vcocal_read(priv
, &vco_cal
);
334 if (!(vco_cal
& FC11_VCOCAL_OK
)) {
335 dev_err(&priv
->i2c
->dev
,
336 "Failed to read VCO calibration value (got %02X)\n",
337 (unsigned int)vco_cal
);
340 vco_cal
&= FC11_VCOCAL_VALUEMASK
;
348 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
349 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_1
;
350 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
351 regs
[FC11_REG_VCOSEL
]);
354 err
= fc0011_vcocal_trigger(priv
);
358 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
359 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
360 regs
[FC11_REG_VCOSEL
]);
367 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
368 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_2
;
369 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
370 regs
[FC11_REG_VCOSEL
]);
373 err
= fc0011_vcocal_trigger(priv
);
376 } else if (vco_cal
<= 48) {
377 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
378 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_1
;
379 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
380 regs
[FC11_REG_VCOSEL
]);
384 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
385 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
386 regs
[FC11_REG_VCOSEL
]);
389 err
= fc0011_vcocal_trigger(priv
);
396 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
397 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_1
;
398 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
399 regs
[FC11_REG_VCOSEL
]);
402 err
= fc0011_vcocal_trigger(priv
);
406 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
407 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_2
;
408 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
409 regs
[FC11_REG_VCOSEL
]);
415 err
= fc0011_vcocal_read(priv
, NULL
);
418 usleep_range(10000, 50000);
420 err
= fc0011_readreg(priv
, FC11_REG_RCCAL
, ®s
[FC11_REG_RCCAL
]);
423 regs
[FC11_REG_RCCAL
] |= FC11_RCCAL_FORCE
;
424 err
= fc0011_writereg(priv
, FC11_REG_RCCAL
, regs
[FC11_REG_RCCAL
]);
427 regs
[FC11_REG_16
] = 0xB;
428 err
= fc0011_writereg(priv
, FC11_REG_16
, regs
[FC11_REG_16
]);
432 dev_dbg(&priv
->i2c
->dev
, "Tuned to fa=%02X fp=%02X xin=%02X%02X vco=%02X vcosel=%02X vcocal=%02X(%u) bw=%u\n",
433 (unsigned int)regs
[FC11_REG_FA
],
434 (unsigned int)regs
[FC11_REG_FP
],
435 (unsigned int)regs
[FC11_REG_XINHI
],
436 (unsigned int)regs
[FC11_REG_XINLO
],
437 (unsigned int)regs
[FC11_REG_VCO
],
438 (unsigned int)regs
[FC11_REG_VCOSEL
],
439 (unsigned int)vco_cal
, vco_retries
,
440 (unsigned int)bandwidth
);
442 priv
->frequency
= p
->frequency
;
443 priv
->bandwidth
= p
->bandwidth_hz
;
448 static int fc0011_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
450 struct fc0011_priv
*priv
= fe
->tuner_priv
;
452 *frequency
= priv
->frequency
;
457 static int fc0011_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
464 static int fc0011_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
466 struct fc0011_priv
*priv
= fe
->tuner_priv
;
468 *bandwidth
= priv
->bandwidth
;
473 static const struct dvb_tuner_ops fc0011_tuner_ops
= {
475 .name
= "Fitipower FC0011",
477 .frequency_min_hz
= 45 * MHz
,
478 .frequency_max_hz
= 1000 * MHz
,
481 .release
= fc0011_release
,
484 .set_params
= fc0011_set_params
,
486 .get_frequency
= fc0011_get_frequency
,
487 .get_if_frequency
= fc0011_get_if_frequency
,
488 .get_bandwidth
= fc0011_get_bandwidth
,
491 struct dvb_frontend
*fc0011_attach(struct dvb_frontend
*fe
,
492 struct i2c_adapter
*i2c
,
493 const struct fc0011_config
*config
)
495 struct fc0011_priv
*priv
;
497 priv
= kzalloc(sizeof(struct fc0011_priv
), GFP_KERNEL
);
502 priv
->addr
= config
->i2c_address
;
504 fe
->tuner_priv
= priv
;
505 fe
->ops
.tuner_ops
= fc0011_tuner_ops
;
507 dev_info(&priv
->i2c
->dev
, "Fitipower FC0011 tuner attached\n");
511 EXPORT_SYMBOL(fc0011_attach
);
513 MODULE_DESCRIPTION("Fitipower FC0011 silicon tuner driver");
514 MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
515 MODULE_LICENSE("GPL");