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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 FC11_REG_XINHI
, /* XIN high 8 bit */
33 FC11_REG_XINLO
, /* XIN low 8 bit */
34 FC11_REG_VCO
, /* VCO */
35 FC11_REG_VCOSEL
, /* VCO select */
36 FC11_REG_7
, /* Unknown tuner reg 7 */
37 FC11_REG_8
, /* Unknown tuner reg 8 */
39 FC11_REG_10
, /* Unknown tuner reg 10 */
40 FC11_REG_11
, /* Unknown tuner reg 11 */
42 FC11_REG_RCCAL
, /* RC calibrate */
43 FC11_REG_VCOCAL
, /* VCO calibrate */
45 FC11_REG_16
, /* Unknown tuner reg 16 */
48 FC11_NR_REGS
, /* Number of registers */
51 enum FC11_REG_VCOSEL_bits
{
52 FC11_VCOSEL_2
= 0x08, /* VCO select 2 */
53 FC11_VCOSEL_1
= 0x10, /* VCO select 1 */
54 FC11_VCOSEL_CLKOUT
= 0x20, /* Fix clock out */
55 FC11_VCOSEL_BW7M
= 0x40, /* 7MHz bw */
56 FC11_VCOSEL_BW6M
= 0x80, /* 6MHz bw */
59 enum FC11_REG_RCCAL_bits
{
60 FC11_RCCAL_FORCE
= 0x10, /* force */
63 enum FC11_REG_VCOCAL_bits
{
64 FC11_VCOCAL_RUN
= 0, /* VCO calibration run */
65 FC11_VCOCAL_VALUEMASK
= 0x3F, /* VCO calibration value mask */
66 FC11_VCOCAL_OK
= 0x40, /* VCO calibration Ok */
67 FC11_VCOCAL_RESET
= 0x80, /* VCO calibration reset */
72 struct i2c_adapter
*i2c
;
80 static int fc0011_writereg(struct fc0011_priv
*priv
, u8 reg
, u8 val
)
82 u8 buf
[2] = { reg
, val
};
83 struct i2c_msg msg
= { .addr
= priv
->addr
,
84 .flags
= 0, .buf
= buf
, .len
= 2 };
86 if (i2c_transfer(priv
->i2c
, &msg
, 1) != 1) {
87 dev_err(&priv
->i2c
->dev
,
88 "I2C write reg failed, reg: %02x, val: %02x\n",
96 static int fc0011_readreg(struct fc0011_priv
*priv
, u8 reg
, u8
*val
)
99 struct i2c_msg msg
[2] = {
100 { .addr
= priv
->addr
,
101 .flags
= 0, .buf
= ®
, .len
= 1 },
102 { .addr
= priv
->addr
,
103 .flags
= I2C_M_RD
, .buf
= val
? : &dummy
, .len
= 1 },
106 if (i2c_transfer(priv
->i2c
, msg
, 2) != 2) {
107 dev_err(&priv
->i2c
->dev
,
108 "I2C read failed, reg: %02x\n", reg
);
115 static int fc0011_release(struct dvb_frontend
*fe
)
117 kfree(fe
->tuner_priv
);
118 fe
->tuner_priv
= NULL
;
123 static int fc0011_init(struct dvb_frontend
*fe
)
125 struct fc0011_priv
*priv
= fe
->tuner_priv
;
128 if (WARN_ON(!fe
->callback
))
131 err
= fe
->callback(priv
->i2c
, DVB_FRONTEND_COMPONENT_TUNER
,
132 FC0011_FE_CALLBACK_POWER
, priv
->addr
);
134 dev_err(&priv
->i2c
->dev
, "Power-on callback failed\n");
137 err
= fe
->callback(priv
->i2c
, DVB_FRONTEND_COMPONENT_TUNER
,
138 FC0011_FE_CALLBACK_RESET
, priv
->addr
);
140 dev_err(&priv
->i2c
->dev
, "Reset callback failed\n");
147 /* Initiate VCO calibration */
148 static int fc0011_vcocal_trigger(struct fc0011_priv
*priv
)
152 err
= fc0011_writereg(priv
, FC11_REG_VCOCAL
, FC11_VCOCAL_RESET
);
155 err
= fc0011_writereg(priv
, FC11_REG_VCOCAL
, FC11_VCOCAL_RUN
);
162 /* Read VCO calibration value */
163 static int fc0011_vcocal_read(struct fc0011_priv
*priv
, u8
*value
)
167 err
= fc0011_writereg(priv
, FC11_REG_VCOCAL
, FC11_VCOCAL_RUN
);
170 usleep_range(10000, 20000);
171 err
= fc0011_readreg(priv
, FC11_REG_VCOCAL
, value
);
178 static int fc0011_set_params(struct dvb_frontend
*fe
)
180 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
181 struct fc0011_priv
*priv
= fe
->tuner_priv
;
183 unsigned int i
, vco_retries
;
184 u32 freq
= p
->frequency
/ 1000;
185 u32 bandwidth
= p
->bandwidth_hz
/ 1000;
186 u32 fvco
, xin
, xdiv
, xdivr
;
188 u8 fa
, fp
, vco_sel
, vco_cal
;
189 u8 regs
[FC11_NR_REGS
] = { };
191 regs
[FC11_REG_7
] = 0x0F;
192 regs
[FC11_REG_8
] = 0x3E;
193 regs
[FC11_REG_10
] = 0xB8;
194 regs
[FC11_REG_11
] = 0x80;
195 regs
[FC11_REG_RCCAL
] = 0x04;
196 err
= fc0011_writereg(priv
, FC11_REG_7
, regs
[FC11_REG_7
]);
197 err
|= fc0011_writereg(priv
, FC11_REG_8
, regs
[FC11_REG_8
]);
198 err
|= fc0011_writereg(priv
, FC11_REG_10
, regs
[FC11_REG_10
]);
199 err
|= fc0011_writereg(priv
, FC11_REG_11
, regs
[FC11_REG_11
]);
200 err
|= fc0011_writereg(priv
, FC11_REG_RCCAL
, regs
[FC11_REG_RCCAL
]);
204 /* Set VCO freq and VCO div */
207 regs
[FC11_REG_VCO
] = 0x82;
208 } else if (freq
< 108000) {
210 regs
[FC11_REG_VCO
] = 0x42;
211 } else if (freq
< 216000) {
213 regs
[FC11_REG_VCO
] = 0x22;
214 } else if (freq
< 432000) {
216 regs
[FC11_REG_VCO
] = 0x12;
219 regs
[FC11_REG_VCO
] = 0x0A;
222 /* Calc XIN. The PLL reference frequency is 18 MHz. */
224 frac
= fvco
- xdiv
* 18000;
225 frac
= (frac
<< 15) / 18000;
232 else if (frac
< 65026)
236 regs
[FC11_REG_XINHI
] = xin
>> 8;
237 regs
[FC11_REG_XINLO
] = xin
;
241 if (fvco
- xdiv
* 18000 >= 9000)
242 xdivr
+= 1; /* round */
254 dev_warn(&priv
->i2c
->dev
,
255 "fa %02X >= fp %02X, but trying to continue\n",
256 (unsigned int)(u8
)fa
, (unsigned int)(u8
)fp
);
258 regs
[FC11_REG_FA
] = fa
;
259 regs
[FC11_REG_FP
] = fp
;
261 /* Select bandwidth */
266 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_BW7M
;
269 dev_warn(&priv
->i2c
->dev
, "Unsupported bandwidth %u kHz. "
275 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_BW6M
;
280 if (fvco
< 2320000) {
282 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
283 } else if (fvco
< 3080000) {
285 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
286 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_1
;
289 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
290 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_2
;
293 /* Fix for low freqs */
295 regs
[FC11_REG_FA
] = 0x6;
296 regs
[FC11_REG_FP
] = 0x11;
300 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_CLKOUT
;
302 /* Write the cached registers */
303 for (i
= FC11_REG_FA
; i
<= FC11_REG_VCOSEL
; i
++) {
304 err
= fc0011_writereg(priv
, i
, regs
[i
]);
309 /* VCO calibration */
310 err
= fc0011_vcocal_trigger(priv
);
313 err
= fc0011_vcocal_read(priv
, &vco_cal
);
317 while (!(vco_cal
& FC11_VCOCAL_OK
) && vco_retries
< 3) {
318 /* Reset the tuner and try again */
319 err
= fe
->callback(priv
->i2c
, DVB_FRONTEND_COMPONENT_TUNER
,
320 FC0011_FE_CALLBACK_RESET
, priv
->addr
);
322 dev_err(&priv
->i2c
->dev
, "Failed to reset tuner\n");
325 /* Reinit tuner config */
327 for (i
= FC11_REG_FA
; i
<= FC11_REG_VCOSEL
; i
++)
328 err
|= fc0011_writereg(priv
, i
, regs
[i
]);
329 err
|= fc0011_writereg(priv
, FC11_REG_7
, regs
[FC11_REG_7
]);
330 err
|= fc0011_writereg(priv
, FC11_REG_8
, regs
[FC11_REG_8
]);
331 err
|= fc0011_writereg(priv
, FC11_REG_10
, regs
[FC11_REG_10
]);
332 err
|= fc0011_writereg(priv
, FC11_REG_11
, regs
[FC11_REG_11
]);
333 err
|= fc0011_writereg(priv
, FC11_REG_RCCAL
, regs
[FC11_REG_RCCAL
]);
336 /* VCO calibration */
337 err
= fc0011_vcocal_trigger(priv
);
340 err
= fc0011_vcocal_read(priv
, &vco_cal
);
345 if (!(vco_cal
& FC11_VCOCAL_OK
)) {
346 dev_err(&priv
->i2c
->dev
,
347 "Failed to read VCO calibration value (got %02X)\n",
348 (unsigned int)vco_cal
);
351 vco_cal
&= FC11_VCOCAL_VALUEMASK
;
356 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
357 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_1
;
358 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
359 regs
[FC11_REG_VCOSEL
]);
362 err
= fc0011_vcocal_trigger(priv
);
366 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
367 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
368 regs
[FC11_REG_VCOSEL
]);
375 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
376 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_2
;
377 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
378 regs
[FC11_REG_VCOSEL
]);
381 err
= fc0011_vcocal_trigger(priv
);
384 } else if (vco_cal
<= 48) {
385 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
386 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_1
;
387 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
388 regs
[FC11_REG_VCOSEL
]);
392 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
393 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
394 regs
[FC11_REG_VCOSEL
]);
397 err
= fc0011_vcocal_trigger(priv
);
404 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
405 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_1
;
406 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
407 regs
[FC11_REG_VCOSEL
]);
410 err
= fc0011_vcocal_trigger(priv
);
414 regs
[FC11_REG_VCOSEL
] &= ~(FC11_VCOSEL_1
| FC11_VCOSEL_2
);
415 regs
[FC11_REG_VCOSEL
] |= FC11_VCOSEL_2
;
416 err
= fc0011_writereg(priv
, FC11_REG_VCOSEL
,
417 regs
[FC11_REG_VCOSEL
]);
423 err
= fc0011_vcocal_read(priv
, NULL
);
426 usleep_range(10000, 50000);
428 err
= fc0011_readreg(priv
, FC11_REG_RCCAL
, ®s
[FC11_REG_RCCAL
]);
431 regs
[FC11_REG_RCCAL
] |= FC11_RCCAL_FORCE
;
432 err
= fc0011_writereg(priv
, FC11_REG_RCCAL
, regs
[FC11_REG_RCCAL
]);
435 err
= fc0011_writereg(priv
, FC11_REG_16
, 0xB);
439 dev_dbg(&priv
->i2c
->dev
, "Tuned to "
440 "fa=%02X fp=%02X xin=%02X%02X vco=%02X vcosel=%02X "
441 "vcocal=%02X(%u) bw=%u\n",
442 (unsigned int)regs
[FC11_REG_FA
],
443 (unsigned int)regs
[FC11_REG_FP
],
444 (unsigned int)regs
[FC11_REG_XINHI
],
445 (unsigned int)regs
[FC11_REG_XINLO
],
446 (unsigned int)regs
[FC11_REG_VCO
],
447 (unsigned int)regs
[FC11_REG_VCOSEL
],
448 (unsigned int)vco_cal
, vco_retries
,
449 (unsigned int)bandwidth
);
451 priv
->frequency
= p
->frequency
;
452 priv
->bandwidth
= p
->bandwidth_hz
;
457 static int fc0011_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
459 struct fc0011_priv
*priv
= fe
->tuner_priv
;
461 *frequency
= priv
->frequency
;
466 static int fc0011_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
473 static int fc0011_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
475 struct fc0011_priv
*priv
= fe
->tuner_priv
;
477 *bandwidth
= priv
->bandwidth
;
482 static const struct dvb_tuner_ops fc0011_tuner_ops
= {
484 .name
= "Fitipower FC0011",
486 .frequency_min
= 45000000,
487 .frequency_max
= 1000000000,
490 .release
= fc0011_release
,
493 .set_params
= fc0011_set_params
,
495 .get_frequency
= fc0011_get_frequency
,
496 .get_if_frequency
= fc0011_get_if_frequency
,
497 .get_bandwidth
= fc0011_get_bandwidth
,
500 struct dvb_frontend
*fc0011_attach(struct dvb_frontend
*fe
,
501 struct i2c_adapter
*i2c
,
502 const struct fc0011_config
*config
)
504 struct fc0011_priv
*priv
;
506 priv
= kzalloc(sizeof(struct fc0011_priv
), GFP_KERNEL
);
511 priv
->addr
= config
->i2c_address
;
513 fe
->tuner_priv
= priv
;
514 fe
->ops
.tuner_ops
= fc0011_tuner_ops
;
516 dev_info(&priv
->i2c
->dev
, "Fitipower FC0011 tuner attached\n");
520 EXPORT_SYMBOL(fc0011_attach
);
522 MODULE_DESCRIPTION("Fitipower FC0011 silicon tuner driver");
523 MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
524 MODULE_LICENSE("GPL");