1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * FCI FC2580 silicon tuner driver
8 #include "fc2580_priv.h"
12 * I2C write and read works only for one single register. Multiple registers
13 * could not be accessed using normal register address auto-increment.
14 * There could be (very likely) register to change that behavior....
17 /* write single register conditionally only when value differs from 0xff
18 * XXX: This is special routine meant only for writing fc2580_freq_regs_lut[]
19 * values. Do not use for the other purposes. */
20 static int fc2580_wr_reg_ff(struct fc2580_dev *dev, u8 reg, u8 val)
25 return regmap_write(dev->regmap, reg, val);
28 static int fc2580_set_params(struct fc2580_dev *dev)
30 struct i2c_client *client = dev->client;
32 unsigned int uitmp, div_ref, div_ref_val, div_n, k, k_cw, div_out;
35 unsigned long timeout;
38 dev_dbg(&client->dev, "tuner is sleeping\n");
43 * Fractional-N synthesizer
45 * +---------------------------------------+
47 * Fref +----+ +----+ +-------+ +----+ +------+ +---+
48 * ------> | /R | --> | PD | --> | VCO | ------> | /2 | --> | /N.F | <-- | K |
49 * +----+ +----+ +-------+ +----+ +------+ +---+
57 for (i = 0; i < ARRAY_SIZE(fc2580_pll_lut); i++) {
58 if (dev->f_frequency <= fc2580_pll_lut[i].freq)
61 if (i == ARRAY_SIZE(fc2580_pll_lut)) {
67 #define F_REF dev->clk
68 div_out = fc2580_pll_lut[i].div_out;
69 f_vco = (u64) dev->f_frequency * div_out;
70 synth_config = fc2580_pll_lut[i].band;
71 if (f_vco < 2600000000ULL)
76 /* select reference divider R (keep PLL div N in valid range) */
78 if (f_vco >= div_u64((u64) DIV_PRE_N * DIV_N_MIN * F_REF, 1)) {
81 } else if (f_vco >= div_u64((u64) DIV_PRE_N * DIV_N_MIN * F_REF, 2)) {
89 /* calculate PLL integer and fractional control word */
90 uitmp = DIV_PRE_N * F_REF / div_ref;
91 div_n = div_u64_rem(f_vco, uitmp, &k);
92 k_cw = div_u64((u64) k * 0x100000, uitmp);
95 "frequency=%u bandwidth=%u f_vco=%llu F_REF=%u div_ref=%u div_n=%u k=%u div_out=%u k_cw=%0x\n",
96 dev->f_frequency, dev->f_bandwidth, f_vco, F_REF, div_ref,
97 div_n, k, div_out, k_cw);
99 ret = regmap_write(dev->regmap, 0x02, synth_config);
103 ret = regmap_write(dev->regmap, 0x18, div_ref_val << 0 | k_cw >> 16);
107 ret = regmap_write(dev->regmap, 0x1a, (k_cw >> 8) & 0xff);
111 ret = regmap_write(dev->regmap, 0x1b, (k_cw >> 0) & 0xff);
115 ret = regmap_write(dev->regmap, 0x1c, div_n);
120 for (i = 0; i < ARRAY_SIZE(fc2580_freq_regs_lut); i++) {
121 if (dev->f_frequency <= fc2580_freq_regs_lut[i].freq)
124 if (i == ARRAY_SIZE(fc2580_freq_regs_lut)) {
129 ret = fc2580_wr_reg_ff(dev, 0x25, fc2580_freq_regs_lut[i].r25_val);
133 ret = fc2580_wr_reg_ff(dev, 0x27, fc2580_freq_regs_lut[i].r27_val);
137 ret = fc2580_wr_reg_ff(dev, 0x28, fc2580_freq_regs_lut[i].r28_val);
141 ret = fc2580_wr_reg_ff(dev, 0x29, fc2580_freq_regs_lut[i].r29_val);
145 ret = fc2580_wr_reg_ff(dev, 0x2b, fc2580_freq_regs_lut[i].r2b_val);
149 ret = fc2580_wr_reg_ff(dev, 0x2c, fc2580_freq_regs_lut[i].r2c_val);
153 ret = fc2580_wr_reg_ff(dev, 0x2d, fc2580_freq_regs_lut[i].r2d_val);
157 ret = fc2580_wr_reg_ff(dev, 0x30, fc2580_freq_regs_lut[i].r30_val);
161 ret = fc2580_wr_reg_ff(dev, 0x44, fc2580_freq_regs_lut[i].r44_val);
165 ret = fc2580_wr_reg_ff(dev, 0x50, fc2580_freq_regs_lut[i].r50_val);
169 ret = fc2580_wr_reg_ff(dev, 0x53, fc2580_freq_regs_lut[i].r53_val);
173 ret = fc2580_wr_reg_ff(dev, 0x5f, fc2580_freq_regs_lut[i].r5f_val);
177 ret = fc2580_wr_reg_ff(dev, 0x61, fc2580_freq_regs_lut[i].r61_val);
181 ret = fc2580_wr_reg_ff(dev, 0x62, fc2580_freq_regs_lut[i].r62_val);
185 ret = fc2580_wr_reg_ff(dev, 0x63, fc2580_freq_regs_lut[i].r63_val);
189 ret = fc2580_wr_reg_ff(dev, 0x67, fc2580_freq_regs_lut[i].r67_val);
193 ret = fc2580_wr_reg_ff(dev, 0x68, fc2580_freq_regs_lut[i].r68_val);
197 ret = fc2580_wr_reg_ff(dev, 0x69, fc2580_freq_regs_lut[i].r69_val);
201 ret = fc2580_wr_reg_ff(dev, 0x6a, fc2580_freq_regs_lut[i].r6a_val);
205 ret = fc2580_wr_reg_ff(dev, 0x6b, fc2580_freq_regs_lut[i].r6b_val);
209 ret = fc2580_wr_reg_ff(dev, 0x6c, fc2580_freq_regs_lut[i].r6c_val);
213 ret = fc2580_wr_reg_ff(dev, 0x6d, fc2580_freq_regs_lut[i].r6d_val);
217 ret = fc2580_wr_reg_ff(dev, 0x6e, fc2580_freq_regs_lut[i].r6e_val);
221 ret = fc2580_wr_reg_ff(dev, 0x6f, fc2580_freq_regs_lut[i].r6f_val);
226 for (i = 0; i < ARRAY_SIZE(fc2580_if_filter_lut); i++) {
227 if (dev->f_bandwidth <= fc2580_if_filter_lut[i].freq)
230 if (i == ARRAY_SIZE(fc2580_if_filter_lut)) {
235 ret = regmap_write(dev->regmap, 0x36, fc2580_if_filter_lut[i].r36_val);
239 uitmp = (unsigned int) 8058000 - (dev->f_bandwidth * 122 / 100 / 2);
240 uitmp = div64_u64((u64) dev->clk * uitmp, 1000000000000ULL);
241 ret = regmap_write(dev->regmap, 0x37, uitmp);
245 ret = regmap_write(dev->regmap, 0x39, fc2580_if_filter_lut[i].r39_val);
249 timeout = jiffies + msecs_to_jiffies(30);
250 for (uitmp = ~0xc0; !time_after(jiffies, timeout) && uitmp != 0xc0;) {
252 ret = regmap_write(dev->regmap, 0x2e, 0x09);
256 /* locked when [7:6] are set (val: d7 6MHz, d5 7MHz, cd 8MHz) */
257 ret = regmap_read(dev->regmap, 0x2f, &uitmp);
262 ret = regmap_write(dev->regmap, 0x2e, 0x01);
267 dev_dbg(&client->dev, "filter did not lock %02x\n", uitmp);
271 dev_dbg(&client->dev, "failed=%d\n", ret);
275 static int fc2580_init(struct fc2580_dev *dev)
277 struct i2c_client *client = dev->client;
280 dev_dbg(&client->dev, "\n");
282 for (i = 0; i < ARRAY_SIZE(fc2580_init_reg_vals); i++) {
283 ret = regmap_write(dev->regmap, fc2580_init_reg_vals[i].reg,
284 fc2580_init_reg_vals[i].val);
292 dev_dbg(&client->dev, "failed=%d\n", ret);
296 static int fc2580_sleep(struct fc2580_dev *dev)
298 struct i2c_client *client = dev->client;
301 dev_dbg(&client->dev, "\n");
305 ret = regmap_write(dev->regmap, 0x02, 0x0a);
310 dev_dbg(&client->dev, "failed=%d\n", ret);
317 static int fc2580_dvb_set_params(struct dvb_frontend *fe)
319 struct fc2580_dev *dev = fe->tuner_priv;
320 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
322 dev->f_frequency = c->frequency;
323 dev->f_bandwidth = c->bandwidth_hz;
324 return fc2580_set_params(dev);
327 static int fc2580_dvb_init(struct dvb_frontend *fe)
329 return fc2580_init(fe->tuner_priv);
332 static int fc2580_dvb_sleep(struct dvb_frontend *fe)
334 return fc2580_sleep(fe->tuner_priv);
337 static int fc2580_dvb_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
339 *frequency = 0; /* Zero-IF */
343 static const struct dvb_tuner_ops fc2580_dvb_tuner_ops = {
345 .name = "FCI FC2580",
346 .frequency_min_hz = 174 * MHz,
347 .frequency_max_hz = 862 * MHz,
350 .init = fc2580_dvb_init,
351 .sleep = fc2580_dvb_sleep,
352 .set_params = fc2580_dvb_set_params,
354 .get_if_frequency = fc2580_dvb_get_if_frequency,
360 #if IS_ENABLED(CONFIG_VIDEO_DEV)
361 static const struct v4l2_frequency_band bands[] = {
363 .type = V4L2_TUNER_RF,
365 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
366 .rangelow = 130000000,
367 .rangehigh = 2000000000,
371 static inline struct fc2580_dev *fc2580_subdev_to_dev(struct v4l2_subdev *sd)
373 return container_of(sd, struct fc2580_dev, subdev);
376 static int fc2580_standby(struct v4l2_subdev *sd)
378 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
381 ret = fc2580_sleep(dev);
385 return fc2580_set_params(dev);
388 static int fc2580_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
390 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
391 struct i2c_client *client = dev->client;
393 dev_dbg(&client->dev, "index=%d\n", v->index);
395 strscpy(v->name, "FCI FC2580", sizeof(v->name));
396 v->type = V4L2_TUNER_RF;
397 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
398 v->rangelow = bands[0].rangelow;
399 v->rangehigh = bands[0].rangehigh;
403 static int fc2580_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
405 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
406 struct i2c_client *client = dev->client;
408 dev_dbg(&client->dev, "index=%d\n", v->index);
412 static int fc2580_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
414 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
415 struct i2c_client *client = dev->client;
417 dev_dbg(&client->dev, "tuner=%d\n", f->tuner);
418 f->frequency = dev->f_frequency;
422 static int fc2580_s_frequency(struct v4l2_subdev *sd,
423 const struct v4l2_frequency *f)
425 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
426 struct i2c_client *client = dev->client;
428 dev_dbg(&client->dev, "tuner=%d type=%d frequency=%u\n",
429 f->tuner, f->type, f->frequency);
431 dev->f_frequency = clamp_t(unsigned int, f->frequency,
432 bands[0].rangelow, bands[0].rangehigh);
433 return fc2580_set_params(dev);
436 static int fc2580_enum_freq_bands(struct v4l2_subdev *sd,
437 struct v4l2_frequency_band *band)
439 struct fc2580_dev *dev = fc2580_subdev_to_dev(sd);
440 struct i2c_client *client = dev->client;
442 dev_dbg(&client->dev, "tuner=%d type=%d index=%d\n",
443 band->tuner, band->type, band->index);
445 if (band->index >= ARRAY_SIZE(bands))
448 band->capability = bands[band->index].capability;
449 band->rangelow = bands[band->index].rangelow;
450 band->rangehigh = bands[band->index].rangehigh;
454 static const struct v4l2_subdev_tuner_ops fc2580_subdev_tuner_ops = {
455 .standby = fc2580_standby,
456 .g_tuner = fc2580_g_tuner,
457 .s_tuner = fc2580_s_tuner,
458 .g_frequency = fc2580_g_frequency,
459 .s_frequency = fc2580_s_frequency,
460 .enum_freq_bands = fc2580_enum_freq_bands,
463 static const struct v4l2_subdev_ops fc2580_subdev_ops = {
464 .tuner = &fc2580_subdev_tuner_ops,
467 static int fc2580_s_ctrl(struct v4l2_ctrl *ctrl)
469 struct fc2580_dev *dev = container_of(ctrl->handler, struct fc2580_dev, hdl);
470 struct i2c_client *client = dev->client;
473 dev_dbg(&client->dev, "ctrl: id=%d name=%s cur.val=%d val=%d\n",
474 ctrl->id, ctrl->name, ctrl->cur.val, ctrl->val);
477 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
478 case V4L2_CID_RF_TUNER_BANDWIDTH:
480 * TODO: Auto logic does not work 100% correctly as tuner driver
481 * do not have information to calculate maximum suitable
482 * bandwidth. Calculating it is responsible of master driver.
484 dev->f_bandwidth = dev->bandwidth->val;
485 ret = fc2580_set_params(dev);
488 dev_dbg(&client->dev, "unknown ctrl");
494 static const struct v4l2_ctrl_ops fc2580_ctrl_ops = {
495 .s_ctrl = fc2580_s_ctrl,
499 static struct v4l2_subdev *fc2580_get_v4l2_subdev(struct i2c_client *client)
501 struct fc2580_dev *dev = i2c_get_clientdata(client);
509 static int fc2580_probe(struct i2c_client *client)
511 struct fc2580_dev *dev;
512 struct fc2580_platform_data *pdata = client->dev.platform_data;
513 struct dvb_frontend *fe = pdata->dvb_frontend;
516 static const struct regmap_config regmap_config = {
521 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
528 dev->clk = pdata->clk;
530 dev->clk = 16384000; /* internal clock */
531 dev->client = client;
532 dev->regmap = devm_regmap_init_i2c(client, ®map_config);
533 if (IS_ERR(dev->regmap)) {
534 ret = PTR_ERR(dev->regmap);
538 /* check if the tuner is there */
539 ret = regmap_read(dev->regmap, 0x01, &uitmp);
543 dev_dbg(&client->dev, "chip_id=%02x\n", uitmp);
554 #if IS_ENABLED(CONFIG_VIDEO_DEV)
555 /* Register controls */
556 v4l2_ctrl_handler_init(&dev->hdl, 2);
557 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &fc2580_ctrl_ops,
558 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
560 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &fc2580_ctrl_ops,
561 V4L2_CID_RF_TUNER_BANDWIDTH,
562 3000, 10000000, 1, 3000);
563 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
564 if (dev->hdl.error) {
565 ret = dev->hdl.error;
566 dev_err(&client->dev, "Could not initialize controls\n");
567 v4l2_ctrl_handler_free(&dev->hdl);
570 dev->subdev.ctrl_handler = &dev->hdl;
571 dev->f_frequency = bands[0].rangelow;
572 dev->f_bandwidth = dev->bandwidth->val;
573 v4l2_i2c_subdev_init(&dev->subdev, client, &fc2580_subdev_ops);
575 fe->tuner_priv = dev;
576 memcpy(&fe->ops.tuner_ops, &fc2580_dvb_tuner_ops,
577 sizeof(fe->ops.tuner_ops));
578 pdata->get_v4l2_subdev = fc2580_get_v4l2_subdev;
579 i2c_set_clientdata(client, dev);
581 dev_info(&client->dev, "FCI FC2580 successfully identified\n");
586 dev_dbg(&client->dev, "failed=%d\n", ret);
590 static void fc2580_remove(struct i2c_client *client)
592 struct fc2580_dev *dev = i2c_get_clientdata(client);
594 dev_dbg(&client->dev, "\n");
596 #if IS_ENABLED(CONFIG_VIDEO_DEV)
597 v4l2_ctrl_handler_free(&dev->hdl);
602 static const struct i2c_device_id fc2580_id_table[] = {
606 MODULE_DEVICE_TABLE(i2c, fc2580_id_table);
608 static struct i2c_driver fc2580_driver = {
611 .suppress_bind_attrs = true,
613 .probe_new = fc2580_probe,
614 .remove = fc2580_remove,
615 .id_table = fc2580_id_table,
618 module_i2c_driver(fc2580_driver);
620 MODULE_DESCRIPTION("FCI FC2580 silicon tuner driver");
622 MODULE_LICENSE("GPL");