2 * FCI FC2580 silicon tuner driver
4 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "fc2580_priv.h"
25 * I2C write and read works only for one single register. Multiple registers
26 * could not be accessed using normal register address auto-increment.
27 * There could be (very likely) register to change that behavior....
30 /* write single register conditionally only when value differs from 0xff
31 * XXX: This is special routine meant only for writing fc2580_freq_regs_lut[]
32 * values. Do not use for the other purposes. */
33 static int fc2580_wr_reg_ff(struct fc2580_dev *dev, u8 reg, u8 val)
38 return regmap_write(dev->regmap, reg, val);
41 static int fc2580_set_params(struct dvb_frontend *fe)
43 struct fc2580_dev *dev = fe->tuner_priv;
44 struct i2c_client *client = dev->client;
45 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
47 unsigned int uitmp, div_ref, div_ref_val, div_n, k, k_cw, div_out;
49 u8 u8tmp, synth_config;
50 unsigned long timeout;
53 "delivery_system=%u frequency=%u bandwidth_hz=%u\n",
54 c->delivery_system, c->frequency, c->bandwidth_hz);
57 * Fractional-N synthesizer
59 * +---------------------------------------+
61 * Fref +----+ +----+ +-------+ +----+ +------+ +---+
62 * ------> | /R | --> | PD | --> | VCO | ------> | /2 | --> | /N.F | <-- | K |
63 * +----+ +----+ +-------+ +----+ +------+ +---+
71 for (i = 0; i < ARRAY_SIZE(fc2580_pll_lut); i++) {
72 if (c->frequency <= fc2580_pll_lut[i].freq)
75 if (i == ARRAY_SIZE(fc2580_pll_lut)) {
81 #define F_REF dev->clk
82 div_out = fc2580_pll_lut[i].div_out;
83 f_vco = (u64) c->frequency * div_out;
84 synth_config = fc2580_pll_lut[i].band;
85 if (f_vco < 2600000000ULL)
90 /* select reference divider R (keep PLL div N in valid range) */
92 if (f_vco >= div_u64((u64) DIV_PRE_N * DIV_N_MIN * F_REF, 1)) {
95 } else if (f_vco >= div_u64((u64) DIV_PRE_N * DIV_N_MIN * F_REF, 2)) {
103 /* calculate PLL integer and fractional control word */
104 uitmp = DIV_PRE_N * F_REF / div_ref;
105 div_n = div_u64_rem(f_vco, uitmp, &k);
106 k_cw = div_u64((u64) k * 0x100000, uitmp);
108 dev_dbg(&client->dev,
109 "frequency=%u f_vco=%llu F_REF=%u div_ref=%u div_n=%u k=%u div_out=%u k_cw=%0x\n",
110 c->frequency, f_vco, F_REF, div_ref, div_n, k, div_out, k_cw);
112 ret = regmap_write(dev->regmap, 0x02, synth_config);
116 ret = regmap_write(dev->regmap, 0x18, div_ref_val << 0 | k_cw >> 16);
120 ret = regmap_write(dev->regmap, 0x1a, (k_cw >> 8) & 0xff);
124 ret = regmap_write(dev->regmap, 0x1b, (k_cw >> 0) & 0xff);
128 ret = regmap_write(dev->regmap, 0x1c, div_n);
133 for (i = 0; i < ARRAY_SIZE(fc2580_freq_regs_lut); i++) {
134 if (c->frequency <= fc2580_freq_regs_lut[i].freq)
137 if (i == ARRAY_SIZE(fc2580_freq_regs_lut)) {
142 ret = fc2580_wr_reg_ff(dev, 0x25, fc2580_freq_regs_lut[i].r25_val);
146 ret = fc2580_wr_reg_ff(dev, 0x27, fc2580_freq_regs_lut[i].r27_val);
150 ret = fc2580_wr_reg_ff(dev, 0x28, fc2580_freq_regs_lut[i].r28_val);
154 ret = fc2580_wr_reg_ff(dev, 0x29, fc2580_freq_regs_lut[i].r29_val);
158 ret = fc2580_wr_reg_ff(dev, 0x2b, fc2580_freq_regs_lut[i].r2b_val);
162 ret = fc2580_wr_reg_ff(dev, 0x2c, fc2580_freq_regs_lut[i].r2c_val);
166 ret = fc2580_wr_reg_ff(dev, 0x2d, fc2580_freq_regs_lut[i].r2d_val);
170 ret = fc2580_wr_reg_ff(dev, 0x30, fc2580_freq_regs_lut[i].r30_val);
174 ret = fc2580_wr_reg_ff(dev, 0x44, fc2580_freq_regs_lut[i].r44_val);
178 ret = fc2580_wr_reg_ff(dev, 0x50, fc2580_freq_regs_lut[i].r50_val);
182 ret = fc2580_wr_reg_ff(dev, 0x53, fc2580_freq_regs_lut[i].r53_val);
186 ret = fc2580_wr_reg_ff(dev, 0x5f, fc2580_freq_regs_lut[i].r5f_val);
190 ret = fc2580_wr_reg_ff(dev, 0x61, fc2580_freq_regs_lut[i].r61_val);
194 ret = fc2580_wr_reg_ff(dev, 0x62, fc2580_freq_regs_lut[i].r62_val);
198 ret = fc2580_wr_reg_ff(dev, 0x63, fc2580_freq_regs_lut[i].r63_val);
202 ret = fc2580_wr_reg_ff(dev, 0x67, fc2580_freq_regs_lut[i].r67_val);
206 ret = fc2580_wr_reg_ff(dev, 0x68, fc2580_freq_regs_lut[i].r68_val);
210 ret = fc2580_wr_reg_ff(dev, 0x69, fc2580_freq_regs_lut[i].r69_val);
214 ret = fc2580_wr_reg_ff(dev, 0x6a, fc2580_freq_regs_lut[i].r6a_val);
218 ret = fc2580_wr_reg_ff(dev, 0x6b, fc2580_freq_regs_lut[i].r6b_val);
222 ret = fc2580_wr_reg_ff(dev, 0x6c, fc2580_freq_regs_lut[i].r6c_val);
226 ret = fc2580_wr_reg_ff(dev, 0x6d, fc2580_freq_regs_lut[i].r6d_val);
230 ret = fc2580_wr_reg_ff(dev, 0x6e, fc2580_freq_regs_lut[i].r6e_val);
234 ret = fc2580_wr_reg_ff(dev, 0x6f, fc2580_freq_regs_lut[i].r6f_val);
239 for (i = 0; i < ARRAY_SIZE(fc2580_if_filter_lut); i++) {
240 if (c->bandwidth_hz <= fc2580_if_filter_lut[i].freq)
243 if (i == ARRAY_SIZE(fc2580_if_filter_lut)) {
248 ret = regmap_write(dev->regmap, 0x36, fc2580_if_filter_lut[i].r36_val);
252 u8tmp = div_u64((u64) dev->clk * fc2580_if_filter_lut[i].mul,
254 ret = regmap_write(dev->regmap, 0x37, u8tmp);
258 ret = regmap_write(dev->regmap, 0x39, fc2580_if_filter_lut[i].r39_val);
262 timeout = jiffies + msecs_to_jiffies(30);
263 for (uitmp = ~0xc0; !time_after(jiffies, timeout) && uitmp != 0xc0;) {
265 ret = regmap_write(dev->regmap, 0x2e, 0x09);
269 /* locked when [7:6] are set (val: d7 6MHz, d5 7MHz, cd 8MHz) */
270 ret = regmap_read(dev->regmap, 0x2f, &uitmp);
275 ret = regmap_write(dev->regmap, 0x2e, 0x01);
280 dev_dbg(&client->dev, "filter did not lock %02x\n", uitmp);
284 dev_dbg(&client->dev, "failed=%d\n", ret);
288 static int fc2580_init(struct dvb_frontend *fe)
290 struct fc2580_dev *dev = fe->tuner_priv;
291 struct i2c_client *client = dev->client;
294 dev_dbg(&client->dev, "\n");
296 for (i = 0; i < ARRAY_SIZE(fc2580_init_reg_vals); i++) {
297 ret = regmap_write(dev->regmap, fc2580_init_reg_vals[i].reg,
298 fc2580_init_reg_vals[i].val);
305 dev_dbg(&client->dev, "failed=%d\n", ret);
309 static int fc2580_sleep(struct dvb_frontend *fe)
311 struct fc2580_dev *dev = fe->tuner_priv;
312 struct i2c_client *client = dev->client;
315 dev_dbg(&client->dev, "\n");
317 ret = regmap_write(dev->regmap, 0x02, 0x0a);
323 dev_dbg(&client->dev, "failed=%d\n", ret);
327 static int fc2580_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
329 struct fc2580_dev *dev = fe->tuner_priv;
330 struct i2c_client *client = dev->client;
332 dev_dbg(&client->dev, "\n");
334 *frequency = 0; /* Zero-IF */
339 static const struct dvb_tuner_ops fc2580_tuner_ops = {
341 .name = "FCI FC2580",
342 .frequency_min = 174000000,
343 .frequency_max = 862000000,
347 .sleep = fc2580_sleep,
348 .set_params = fc2580_set_params,
350 .get_if_frequency = fc2580_get_if_frequency,
353 static int fc2580_probe(struct i2c_client *client,
354 const struct i2c_device_id *id)
356 struct fc2580_dev *dev;
357 struct fc2580_platform_data *pdata = client->dev.platform_data;
358 struct dvb_frontend *fe = pdata->dvb_frontend;
361 static const struct regmap_config regmap_config = {
366 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
373 dev->clk = pdata->clk;
375 dev->clk = 16384000; /* internal clock */
376 dev->client = client;
377 dev->regmap = devm_regmap_init_i2c(client, ®map_config);
378 if (IS_ERR(dev->regmap)) {
379 ret = PTR_ERR(dev->regmap);
383 /* check if the tuner is there */
384 ret = regmap_read(dev->regmap, 0x01, &uitmp);
388 dev_dbg(&client->dev, "chip_id=%02x\n", uitmp);
398 fe->tuner_priv = dev;
399 memcpy(&fe->ops.tuner_ops, &fc2580_tuner_ops,
400 sizeof(struct dvb_tuner_ops));
401 i2c_set_clientdata(client, dev);
403 dev_info(&client->dev, "FCI FC2580 successfully identified\n");
408 dev_dbg(&client->dev, "failed=%d\n", ret);
412 static int fc2580_remove(struct i2c_client *client)
414 struct fc2580_dev *dev = i2c_get_clientdata(client);
416 dev_dbg(&client->dev, "\n");
422 static const struct i2c_device_id fc2580_id_table[] = {
426 MODULE_DEVICE_TABLE(i2c, fc2580_id_table);
428 static struct i2c_driver fc2580_driver = {
430 .owner = THIS_MODULE,
432 .suppress_bind_attrs = true,
434 .probe = fc2580_probe,
435 .remove = fc2580_remove,
436 .id_table = fc2580_id_table,
439 module_i2c_driver(fc2580_driver);
441 MODULE_DESCRIPTION("FCI FC2580 silicon tuner driver");
442 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
443 MODULE_LICENSE("GPL");