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 fc2580_dev
*dev
)
43 struct i2c_client
*client
= dev
->client
;
45 unsigned int uitmp
, div_ref
, div_ref_val
, div_n
, k
, k_cw
, div_out
;
48 unsigned long timeout
;
51 dev_dbg(&client
->dev
, "tuner is sleeping\n");
56 * Fractional-N synthesizer
58 * +---------------------------------------+
60 * Fref +----+ +----+ +-------+ +----+ +------+ +---+
61 * ------> | /R | --> | PD | --> | VCO | ------> | /2 | --> | /N.F | <-- | K |
62 * +----+ +----+ +-------+ +----+ +------+ +---+
70 for (i
= 0; i
< ARRAY_SIZE(fc2580_pll_lut
); i
++) {
71 if (dev
->f_frequency
<= fc2580_pll_lut
[i
].freq
)
74 if (i
== ARRAY_SIZE(fc2580_pll_lut
)) {
80 #define F_REF dev->clk
81 div_out
= fc2580_pll_lut
[i
].div_out
;
82 f_vco
= (u64
) dev
->f_frequency
* div_out
;
83 synth_config
= fc2580_pll_lut
[i
].band
;
84 if (f_vco
< 2600000000ULL)
89 /* select reference divider R (keep PLL div N in valid range) */
91 if (f_vco
>= div_u64((u64
) DIV_PRE_N
* DIV_N_MIN
* F_REF
, 1)) {
94 } else if (f_vco
>= div_u64((u64
) DIV_PRE_N
* DIV_N_MIN
* F_REF
, 2)) {
102 /* calculate PLL integer and fractional control word */
103 uitmp
= DIV_PRE_N
* F_REF
/ div_ref
;
104 div_n
= div_u64_rem(f_vco
, uitmp
, &k
);
105 k_cw
= div_u64((u64
) k
* 0x100000, uitmp
);
107 dev_dbg(&client
->dev
,
108 "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",
109 dev
->f_frequency
, dev
->f_bandwidth
, f_vco
, F_REF
, div_ref
,
110 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 (dev
->f_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 (dev
->f_bandwidth
<= 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 uitmp
= (unsigned int) 8058000 - (dev
->f_bandwidth
* 122 / 100 / 2);
253 uitmp
= div64_u64((u64
) dev
->clk
* uitmp
, 1000000000000ULL);
254 ret
= regmap_write(dev
->regmap
, 0x37, uitmp
);
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 fc2580_dev
*dev
)
290 struct i2c_client
*client
= dev
->client
;
293 dev_dbg(&client
->dev
, "\n");
295 for (i
= 0; i
< ARRAY_SIZE(fc2580_init_reg_vals
); i
++) {
296 ret
= regmap_write(dev
->regmap
, fc2580_init_reg_vals
[i
].reg
,
297 fc2580_init_reg_vals
[i
].val
);
305 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
309 static int fc2580_sleep(struct fc2580_dev
*dev
)
311 struct i2c_client
*client
= dev
->client
;
314 dev_dbg(&client
->dev
, "\n");
318 ret
= regmap_write(dev
->regmap
, 0x02, 0x0a);
323 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
330 static int fc2580_dvb_set_params(struct dvb_frontend
*fe
)
332 struct fc2580_dev
*dev
= fe
->tuner_priv
;
333 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
335 dev
->f_frequency
= c
->frequency
;
336 dev
->f_bandwidth
= c
->bandwidth_hz
;
337 return fc2580_set_params(dev
);
340 static int fc2580_dvb_init(struct dvb_frontend
*fe
)
342 return fc2580_init(fe
->tuner_priv
);
345 static int fc2580_dvb_sleep(struct dvb_frontend
*fe
)
347 return fc2580_sleep(fe
->tuner_priv
);
350 static int fc2580_dvb_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
352 *frequency
= 0; /* Zero-IF */
356 static const struct dvb_tuner_ops fc2580_dvb_tuner_ops
= {
358 .name
= "FCI FC2580",
359 .frequency_min
= 174000000,
360 .frequency_max
= 862000000,
363 .init
= fc2580_dvb_init
,
364 .sleep
= fc2580_dvb_sleep
,
365 .set_params
= fc2580_dvb_set_params
,
367 .get_if_frequency
= fc2580_dvb_get_if_frequency
,
373 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
374 static const struct v4l2_frequency_band bands
[] = {
376 .type
= V4L2_TUNER_RF
,
378 .capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
,
379 .rangelow
= 130000000,
380 .rangehigh
= 2000000000,
384 static inline struct fc2580_dev
*fc2580_subdev_to_dev(struct v4l2_subdev
*sd
)
386 return container_of(sd
, struct fc2580_dev
, subdev
);
389 static int fc2580_s_power(struct v4l2_subdev
*sd
, int on
)
391 struct fc2580_dev
*dev
= fc2580_subdev_to_dev(sd
);
392 struct i2c_client
*client
= dev
->client
;
395 dev_dbg(&client
->dev
, "on=%d\n", on
);
398 ret
= fc2580_init(dev
);
400 ret
= fc2580_sleep(dev
);
404 return fc2580_set_params(dev
);
407 static const struct v4l2_subdev_core_ops fc2580_subdev_core_ops
= {
408 .s_power
= fc2580_s_power
,
411 static int fc2580_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*v
)
413 struct fc2580_dev
*dev
= fc2580_subdev_to_dev(sd
);
414 struct i2c_client
*client
= dev
->client
;
416 dev_dbg(&client
->dev
, "index=%d\n", v
->index
);
418 strlcpy(v
->name
, "FCI FC2580", sizeof(v
->name
));
419 v
->type
= V4L2_TUNER_RF
;
420 v
->capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
;
421 v
->rangelow
= bands
[0].rangelow
;
422 v
->rangehigh
= bands
[0].rangehigh
;
426 static int fc2580_s_tuner(struct v4l2_subdev
*sd
, const struct v4l2_tuner
*v
)
428 struct fc2580_dev
*dev
= fc2580_subdev_to_dev(sd
);
429 struct i2c_client
*client
= dev
->client
;
431 dev_dbg(&client
->dev
, "index=%d\n", v
->index
);
435 static int fc2580_g_frequency(struct v4l2_subdev
*sd
, struct v4l2_frequency
*f
)
437 struct fc2580_dev
*dev
= fc2580_subdev_to_dev(sd
);
438 struct i2c_client
*client
= dev
->client
;
440 dev_dbg(&client
->dev
, "tuner=%d\n", f
->tuner
);
441 f
->frequency
= dev
->f_frequency
;
445 static int fc2580_s_frequency(struct v4l2_subdev
*sd
,
446 const struct v4l2_frequency
*f
)
448 struct fc2580_dev
*dev
= fc2580_subdev_to_dev(sd
);
449 struct i2c_client
*client
= dev
->client
;
451 dev_dbg(&client
->dev
, "tuner=%d type=%d frequency=%u\n",
452 f
->tuner
, f
->type
, f
->frequency
);
454 dev
->f_frequency
= clamp_t(unsigned int, f
->frequency
,
455 bands
[0].rangelow
, bands
[0].rangehigh
);
456 return fc2580_set_params(dev
);
459 static int fc2580_enum_freq_bands(struct v4l2_subdev
*sd
,
460 struct v4l2_frequency_band
*band
)
462 struct fc2580_dev
*dev
= fc2580_subdev_to_dev(sd
);
463 struct i2c_client
*client
= dev
->client
;
465 dev_dbg(&client
->dev
, "tuner=%d type=%d index=%d\n",
466 band
->tuner
, band
->type
, band
->index
);
468 if (band
->index
>= ARRAY_SIZE(bands
))
471 band
->capability
= bands
[band
->index
].capability
;
472 band
->rangelow
= bands
[band
->index
].rangelow
;
473 band
->rangehigh
= bands
[band
->index
].rangehigh
;
477 static const struct v4l2_subdev_tuner_ops fc2580_subdev_tuner_ops
= {
478 .g_tuner
= fc2580_g_tuner
,
479 .s_tuner
= fc2580_s_tuner
,
480 .g_frequency
= fc2580_g_frequency
,
481 .s_frequency
= fc2580_s_frequency
,
482 .enum_freq_bands
= fc2580_enum_freq_bands
,
485 static const struct v4l2_subdev_ops fc2580_subdev_ops
= {
486 .core
= &fc2580_subdev_core_ops
,
487 .tuner
= &fc2580_subdev_tuner_ops
,
490 static int fc2580_s_ctrl(struct v4l2_ctrl
*ctrl
)
492 struct fc2580_dev
*dev
= container_of(ctrl
->handler
, struct fc2580_dev
, hdl
);
493 struct i2c_client
*client
= dev
->client
;
496 dev_dbg(&client
->dev
, "ctrl: id=%d name=%s cur.val=%d val=%d\n",
497 ctrl
->id
, ctrl
->name
, ctrl
->cur
.val
, ctrl
->val
);
500 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
:
501 case V4L2_CID_RF_TUNER_BANDWIDTH
:
503 * TODO: Auto logic does not work 100% correctly as tuner driver
504 * do not have information to calculate maximum suitable
505 * bandwidth. Calculating it is responsible of master driver.
507 dev
->f_bandwidth
= dev
->bandwidth
->val
;
508 ret
= fc2580_set_params(dev
);
511 dev_dbg(&client
->dev
, "unknown ctrl");
517 static const struct v4l2_ctrl_ops fc2580_ctrl_ops
= {
518 .s_ctrl
= fc2580_s_ctrl
,
522 static struct v4l2_subdev
*fc2580_get_v4l2_subdev(struct i2c_client
*client
)
524 struct fc2580_dev
*dev
= i2c_get_clientdata(client
);
532 static int fc2580_probe(struct i2c_client
*client
,
533 const struct i2c_device_id
*id
)
535 struct fc2580_dev
*dev
;
536 struct fc2580_platform_data
*pdata
= client
->dev
.platform_data
;
537 struct dvb_frontend
*fe
= pdata
->dvb_frontend
;
540 static const struct regmap_config regmap_config
= {
545 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
552 dev
->clk
= pdata
->clk
;
554 dev
->clk
= 16384000; /* internal clock */
555 dev
->client
= client
;
556 dev
->regmap
= devm_regmap_init_i2c(client
, ®map_config
);
557 if (IS_ERR(dev
->regmap
)) {
558 ret
= PTR_ERR(dev
->regmap
);
562 /* check if the tuner is there */
563 ret
= regmap_read(dev
->regmap
, 0x01, &uitmp
);
567 dev_dbg(&client
->dev
, "chip_id=%02x\n", uitmp
);
578 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
579 /* Register controls */
580 v4l2_ctrl_handler_init(&dev
->hdl
, 2);
581 dev
->bandwidth_auto
= v4l2_ctrl_new_std(&dev
->hdl
, &fc2580_ctrl_ops
,
582 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
,
584 dev
->bandwidth
= v4l2_ctrl_new_std(&dev
->hdl
, &fc2580_ctrl_ops
,
585 V4L2_CID_RF_TUNER_BANDWIDTH
,
586 3000, 10000000, 1, 3000);
587 v4l2_ctrl_auto_cluster(2, &dev
->bandwidth_auto
, 0, false);
588 if (dev
->hdl
.error
) {
589 ret
= dev
->hdl
.error
;
590 dev_err(&client
->dev
, "Could not initialize controls\n");
591 v4l2_ctrl_handler_free(&dev
->hdl
);
594 dev
->subdev
.ctrl_handler
= &dev
->hdl
;
595 dev
->f_frequency
= bands
[0].rangelow
;
596 dev
->f_bandwidth
= dev
->bandwidth
->val
;
597 v4l2_i2c_subdev_init(&dev
->subdev
, client
, &fc2580_subdev_ops
);
599 fe
->tuner_priv
= dev
;
600 memcpy(&fe
->ops
.tuner_ops
, &fc2580_dvb_tuner_ops
,
601 sizeof(fe
->ops
.tuner_ops
));
602 pdata
->get_v4l2_subdev
= fc2580_get_v4l2_subdev
;
603 i2c_set_clientdata(client
, dev
);
605 dev_info(&client
->dev
, "FCI FC2580 successfully identified\n");
610 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
614 static int fc2580_remove(struct i2c_client
*client
)
616 struct fc2580_dev
*dev
= i2c_get_clientdata(client
);
618 dev_dbg(&client
->dev
, "\n");
620 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
621 v4l2_ctrl_handler_free(&dev
->hdl
);
627 static const struct i2c_device_id fc2580_id_table
[] = {
631 MODULE_DEVICE_TABLE(i2c
, fc2580_id_table
);
633 static struct i2c_driver fc2580_driver
= {
635 .owner
= THIS_MODULE
,
637 .suppress_bind_attrs
= true,
639 .probe
= fc2580_probe
,
640 .remove
= fc2580_remove
,
641 .id_table
= fc2580_id_table
,
644 module_i2c_driver(fc2580_driver
);
646 MODULE_DESCRIPTION("FCI FC2580 silicon tuner driver");
647 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
648 MODULE_LICENSE("GPL");