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, frac, xdiv, xdivr;
187 u8 fa, fp, vco_sel, vco_cal;
188 u8 regs[FC11_NR_REGS] = { };
190 regs[FC11_REG_7] = 0x0F;
191 regs[FC11_REG_8] = 0x3E;
192 regs[FC11_REG_10] = 0xB8;
193 regs[FC11_REG_11] = 0x80;
194 regs[FC11_REG_RCCAL] = 0x04;
195 err = fc0011_writereg(priv, FC11_REG_7, regs[FC11_REG_7]);
196 err |= fc0011_writereg(priv, FC11_REG_8, regs[FC11_REG_8]);
197 err |= fc0011_writereg(priv, FC11_REG_10, regs[FC11_REG_10]);
198 err |= fc0011_writereg(priv, FC11_REG_11, regs[FC11_REG_11]);
199 err |= fc0011_writereg(priv, FC11_REG_RCCAL, regs[FC11_REG_RCCAL]);
203 /* Set VCO freq and VCO div */
206 regs[FC11_REG_VCO] = 0x82;
207 } else if (freq < 108000) {
209 regs[FC11_REG_VCO] = 0x42;
210 } else if (freq < 216000) {
212 regs[FC11_REG_VCO] = 0x22;
213 } else if (freq < 432000) {
215 regs[FC11_REG_VCO] = 0x12;
218 regs[FC11_REG_VCO] = 0x0A;
221 /* Calc XIN. The PLL reference frequency is 18 MHz. */
223 WARN_ON(xdiv > 0xFF);
224 frac = fvco - xdiv * 18000;
225 frac = (frac << 15) / 18000;
231 xin = clamp_t(u32, frac, 512, 65024);
232 regs[FC11_REG_XINHI] = xin >> 8;
233 regs[FC11_REG_XINLO] = xin;
237 if (fvco - xdiv * 18000 >= 9000)
238 xdivr += 1; /* round */
250 dev_warn(&priv->i2c->dev,
251 "fa %02X >= fp %02X, but trying to continue\n",
252 (unsigned int)(u8)fa, (unsigned int)(u8)fp);
254 regs[FC11_REG_FA] = fa;
255 regs[FC11_REG_FP] = fp;
257 /* Select bandwidth */
262 regs[FC11_REG_VCOSEL] |= FC11_VCOSEL_BW7M;
265 dev_warn(&priv->i2c->dev, "Unsupported bandwidth %u kHz. "
271 regs[FC11_REG_VCOSEL] |= FC11_VCOSEL_BW6M;
276 if (fvco < 2320000) {
278 regs[FC11_REG_VCOSEL] &= ~(FC11_VCOSEL_1 | FC11_VCOSEL_2);
279 } else if (fvco < 3080000) {
281 regs[FC11_REG_VCOSEL] &= ~(FC11_VCOSEL_1 | FC11_VCOSEL_2);
282 regs[FC11_REG_VCOSEL] |= FC11_VCOSEL_1;
285 regs[FC11_REG_VCOSEL] &= ~(FC11_VCOSEL_1 | FC11_VCOSEL_2);
286 regs[FC11_REG_VCOSEL] |= FC11_VCOSEL_2;
289 /* Fix for low freqs */
291 regs[FC11_REG_FA] = 0x6;
292 regs[FC11_REG_FP] = 0x11;
296 regs[FC11_REG_VCOSEL] |= FC11_VCOSEL_CLKOUT;
298 /* Write the cached registers */
299 for (i = FC11_REG_FA; i <= FC11_REG_VCOSEL; i++) {
300 err = fc0011_writereg(priv, i, regs[i]);
305 /* VCO calibration */
306 err = fc0011_vcocal_trigger(priv);
309 err = fc0011_vcocal_read(priv, &vco_cal);
313 while (!(vco_cal & FC11_VCOCAL_OK) && vco_retries < 3) {
314 /* Reset the tuner and try again */
315 err = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
316 FC0011_FE_CALLBACK_RESET, priv->addr);
318 dev_err(&priv->i2c->dev, "Failed to reset tuner\n");
321 /* Reinit tuner config */
323 for (i = FC11_REG_FA; i <= FC11_REG_VCOSEL; i++)
324 err |= fc0011_writereg(priv, i, regs[i]);
325 err |= fc0011_writereg(priv, FC11_REG_7, regs[FC11_REG_7]);
326 err |= fc0011_writereg(priv, FC11_REG_8, regs[FC11_REG_8]);
327 err |= fc0011_writereg(priv, FC11_REG_10, regs[FC11_REG_10]);
328 err |= fc0011_writereg(priv, FC11_REG_11, regs[FC11_REG_11]);
329 err |= fc0011_writereg(priv, FC11_REG_RCCAL, regs[FC11_REG_RCCAL]);
332 /* VCO calibration */
333 err = fc0011_vcocal_trigger(priv);
336 err = fc0011_vcocal_read(priv, &vco_cal);
341 if (!(vco_cal & FC11_VCOCAL_OK)) {
342 dev_err(&priv->i2c->dev,
343 "Failed to read VCO calibration value (got %02X)\n",
344 (unsigned int)vco_cal);
347 vco_cal &= FC11_VCOCAL_VALUEMASK;
354 regs[FC11_REG_VCOSEL] &= ~(FC11_VCOSEL_1 | FC11_VCOSEL_2);
355 regs[FC11_REG_VCOSEL] |= FC11_VCOSEL_1;
356 err = fc0011_writereg(priv, FC11_REG_VCOSEL,
357 regs[FC11_REG_VCOSEL]);
360 err = fc0011_vcocal_trigger(priv);
364 regs[FC11_REG_VCOSEL] &= ~(FC11_VCOSEL_1 | FC11_VCOSEL_2);
365 err = fc0011_writereg(priv, FC11_REG_VCOSEL,
366 regs[FC11_REG_VCOSEL]);
373 regs[FC11_REG_VCOSEL] &= ~(FC11_VCOSEL_1 | FC11_VCOSEL_2);
374 regs[FC11_REG_VCOSEL] |= FC11_VCOSEL_2;
375 err = fc0011_writereg(priv, FC11_REG_VCOSEL,
376 regs[FC11_REG_VCOSEL]);
379 err = fc0011_vcocal_trigger(priv);
382 } else if (vco_cal <= 48) {
383 regs[FC11_REG_VCOSEL] &= ~(FC11_VCOSEL_1 | FC11_VCOSEL_2);
384 regs[FC11_REG_VCOSEL] |= FC11_VCOSEL_1;
385 err = fc0011_writereg(priv, FC11_REG_VCOSEL,
386 regs[FC11_REG_VCOSEL]);
390 regs[FC11_REG_VCOSEL] &= ~(FC11_VCOSEL_1 | FC11_VCOSEL_2);
391 err = fc0011_writereg(priv, FC11_REG_VCOSEL,
392 regs[FC11_REG_VCOSEL]);
395 err = fc0011_vcocal_trigger(priv);
402 regs[FC11_REG_VCOSEL] &= ~(FC11_VCOSEL_1 | FC11_VCOSEL_2);
403 regs[FC11_REG_VCOSEL] |= FC11_VCOSEL_1;
404 err = fc0011_writereg(priv, FC11_REG_VCOSEL,
405 regs[FC11_REG_VCOSEL]);
408 err = fc0011_vcocal_trigger(priv);
412 regs[FC11_REG_VCOSEL] &= ~(FC11_VCOSEL_1 | FC11_VCOSEL_2);
413 regs[FC11_REG_VCOSEL] |= FC11_VCOSEL_2;
414 err = fc0011_writereg(priv, FC11_REG_VCOSEL,
415 regs[FC11_REG_VCOSEL]);
421 err = fc0011_vcocal_read(priv, NULL);
424 usleep_range(10000, 50000);
426 err = fc0011_readreg(priv, FC11_REG_RCCAL, ®s[FC11_REG_RCCAL]);
429 regs[FC11_REG_RCCAL] |= FC11_RCCAL_FORCE;
430 err = fc0011_writereg(priv, FC11_REG_RCCAL, regs[FC11_REG_RCCAL]);
433 regs[FC11_REG_16] = 0xB;
434 err = fc0011_writereg(priv, FC11_REG_16, regs[FC11_REG_16]);
438 dev_dbg(&priv->i2c->dev, "Tuned to "
439 "fa=%02X fp=%02X xin=%02X%02X vco=%02X vcosel=%02X "
440 "vcocal=%02X(%u) bw=%u\n",
441 (unsigned int)regs[FC11_REG_FA],
442 (unsigned int)regs[FC11_REG_FP],
443 (unsigned int)regs[FC11_REG_XINHI],
444 (unsigned int)regs[FC11_REG_XINLO],
445 (unsigned int)regs[FC11_REG_VCO],
446 (unsigned int)regs[FC11_REG_VCOSEL],
447 (unsigned int)vco_cal, vco_retries,
448 (unsigned int)bandwidth);
450 priv->frequency = p->frequency;
451 priv->bandwidth = p->bandwidth_hz;
456 static int fc0011_get_frequency(struct dvb_frontend *fe, u32 *frequency)
458 struct fc0011_priv *priv = fe->tuner_priv;
460 *frequency = priv->frequency;
465 static int fc0011_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
472 static int fc0011_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
474 struct fc0011_priv *priv = fe->tuner_priv;
476 *bandwidth = priv->bandwidth;
481 static const struct dvb_tuner_ops fc0011_tuner_ops = {
483 .name = "Fitipower FC0011",
485 .frequency_min = 45000000,
486 .frequency_max = 1000000000,
489 .release = fc0011_release,
492 .set_params = fc0011_set_params,
494 .get_frequency = fc0011_get_frequency,
495 .get_if_frequency = fc0011_get_if_frequency,
496 .get_bandwidth = fc0011_get_bandwidth,
499 struct dvb_frontend *fc0011_attach(struct dvb_frontend *fe,
500 struct i2c_adapter *i2c,
501 const struct fc0011_config *config)
503 struct fc0011_priv *priv;
505 priv = kzalloc(sizeof(struct fc0011_priv), GFP_KERNEL);
510 priv->addr = config->i2c_address;
512 fe->tuner_priv = priv;
513 fe->ops.tuner_ops = fc0011_tuner_ops;
515 dev_info(&priv->i2c->dev, "Fitipower FC0011 tuner attached\n");
519 EXPORT_SYMBOL(fc0011_attach);
521 MODULE_DESCRIPTION("Fitipower FC0011 silicon tuner driver");
522 MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
523 MODULE_LICENSE("GPL");