1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Elonics E4000 silicon tuner driver
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
8 #include "e4000_priv.h"
10 static int e4000_init(struct e4000_dev
*dev
)
12 struct i2c_client
*client
= dev
->client
;
15 dev_dbg(&client
->dev
, "\n");
18 ret
= regmap_write(dev
->regmap
, 0x00, 0x01);
22 /* disable output clock */
23 ret
= regmap_write(dev
->regmap
, 0x06, 0x00);
27 ret
= regmap_write(dev
->regmap
, 0x7a, 0x96);
32 ret
= regmap_bulk_write(dev
->regmap
, 0x7e, "\x01\xfe", 2);
36 ret
= regmap_write(dev
->regmap
, 0x82, 0x00);
40 ret
= regmap_write(dev
->regmap
, 0x24, 0x05);
44 ret
= regmap_bulk_write(dev
->regmap
, 0x87, "\x20\x01", 2);
48 ret
= regmap_bulk_write(dev
->regmap
, 0x9f, "\x7f\x07", 2);
52 /* DC offset control */
53 ret
= regmap_write(dev
->regmap
, 0x2d, 0x1f);
57 ret
= regmap_bulk_write(dev
->regmap
, 0x70, "\x01\x01", 2);
62 ret
= regmap_write(dev
->regmap
, 0x1a, 0x17);
66 ret
= regmap_write(dev
->regmap
, 0x1f, 0x1a);
74 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
78 static int e4000_sleep(struct e4000_dev
*dev
)
80 struct i2c_client
*client
= dev
->client
;
83 dev_dbg(&client
->dev
, "\n");
87 ret
= regmap_write(dev
->regmap
, 0x00, 0x00);
93 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
97 static int e4000_set_params(struct e4000_dev
*dev
)
99 struct i2c_client
*client
= dev
->client
;
101 unsigned int div_n
, k
, k_cw
, div_out
;
103 u8 buf
[5], i_data
[4], q_data
[4];
106 dev_dbg(&client
->dev
, "tuner is sleeping\n");
110 /* gain control manual */
111 ret
= regmap_write(dev
->regmap
, 0x1a, 0x00);
116 * Fractional-N synthesizer
118 * +----------------------------+
120 * Fref +----+ +-------+ +------+ +---+
121 * ------> | PD | --> | VCO | ------> | /N.F | <-- | K |
122 * +----+ +-------+ +------+ +---+
130 for (i
= 0; i
< ARRAY_SIZE(e4000_pll_lut
); i
++) {
131 if (dev
->f_frequency
<= e4000_pll_lut
[i
].freq
)
134 if (i
== ARRAY_SIZE(e4000_pll_lut
)) {
139 #define F_REF dev->clk
140 div_out
= e4000_pll_lut
[i
].div_out
;
141 f_vco
= (u64
) dev
->f_frequency
* div_out
;
142 /* calculate PLL integer and fractional control word */
143 div_n
= div_u64_rem(f_vco
, F_REF
, &k
);
144 k_cw
= div_u64((u64
) k
* 0x10000, F_REF
);
146 dev_dbg(&client
->dev
,
147 "frequency=%u bandwidth=%u f_vco=%llu F_REF=%u div_n=%u k=%u k_cw=%04x div_out=%u\n",
148 dev
->f_frequency
, dev
->f_bandwidth
, f_vco
, F_REF
, div_n
, k
,
152 buf
[1] = (k_cw
>> 0) & 0xff;
153 buf
[2] = (k_cw
>> 8) & 0xff;
155 buf
[4] = e4000_pll_lut
[i
].div_out_reg
;
156 ret
= regmap_bulk_write(dev
->regmap
, 0x09, buf
, 5);
160 /* LNA filter (RF filter) */
161 for (i
= 0; i
< ARRAY_SIZE(e400_lna_filter_lut
); i
++) {
162 if (dev
->f_frequency
<= e400_lna_filter_lut
[i
].freq
)
165 if (i
== ARRAY_SIZE(e400_lna_filter_lut
)) {
170 ret
= regmap_write(dev
->regmap
, 0x10, e400_lna_filter_lut
[i
].val
);
175 for (i
= 0; i
< ARRAY_SIZE(e4000_if_filter_lut
); i
++) {
176 if (dev
->f_bandwidth
<= e4000_if_filter_lut
[i
].freq
)
179 if (i
== ARRAY_SIZE(e4000_if_filter_lut
)) {
184 buf
[0] = e4000_if_filter_lut
[i
].reg11_val
;
185 buf
[1] = e4000_if_filter_lut
[i
].reg12_val
;
187 ret
= regmap_bulk_write(dev
->regmap
, 0x11, buf
, 2);
192 for (i
= 0; i
< ARRAY_SIZE(e4000_band_lut
); i
++) {
193 if (dev
->f_frequency
<= e4000_band_lut
[i
].freq
)
196 if (i
== ARRAY_SIZE(e4000_band_lut
)) {
201 ret
= regmap_write(dev
->regmap
, 0x07, e4000_band_lut
[i
].reg07_val
);
205 ret
= regmap_write(dev
->regmap
, 0x78, e4000_band_lut
[i
].reg78_val
);
210 for (i
= 0; i
< 4; i
++) {
212 ret
= regmap_bulk_write(dev
->regmap
, 0x15, "\x00\x7e\x24", 3);
214 ret
= regmap_bulk_write(dev
->regmap
, 0x15, "\x00\x7f", 2);
216 ret
= regmap_bulk_write(dev
->regmap
, 0x15, "\x01", 1);
218 ret
= regmap_bulk_write(dev
->regmap
, 0x16, "\x7e", 1);
223 ret
= regmap_write(dev
->regmap
, 0x29, 0x01);
227 ret
= regmap_bulk_read(dev
->regmap
, 0x2a, buf
, 3);
231 i_data
[i
] = (((buf
[2] >> 0) & 0x3) << 6) | (buf
[0] & 0x3f);
232 q_data
[i
] = (((buf
[2] >> 4) & 0x3) << 6) | (buf
[1] & 0x3f);
235 swap(q_data
[2], q_data
[3]);
236 swap(i_data
[2], i_data
[3]);
238 ret
= regmap_bulk_write(dev
->regmap
, 0x50, q_data
, 4);
242 ret
= regmap_bulk_write(dev
->regmap
, 0x60, i_data
, 4);
246 /* gain control auto */
247 ret
= regmap_write(dev
->regmap
, 0x1a, 0x17);
253 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
260 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
261 static const struct v4l2_frequency_band bands
[] = {
263 .type
= V4L2_TUNER_RF
,
265 .capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
,
266 .rangelow
= 59000000,
267 .rangehigh
= 1105000000,
270 .type
= V4L2_TUNER_RF
,
272 .capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
,
273 .rangelow
= 1249000000,
274 .rangehigh
= 2208000000UL,
278 static inline struct e4000_dev
*e4000_subdev_to_dev(struct v4l2_subdev
*sd
)
280 return container_of(sd
, struct e4000_dev
, sd
);
283 static int e4000_standby(struct v4l2_subdev
*sd
)
285 struct e4000_dev
*dev
= e4000_subdev_to_dev(sd
);
288 ret
= e4000_sleep(dev
);
292 return e4000_set_params(dev
);
295 static int e4000_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*v
)
297 struct e4000_dev
*dev
= e4000_subdev_to_dev(sd
);
298 struct i2c_client
*client
= dev
->client
;
300 dev_dbg(&client
->dev
, "index=%d\n", v
->index
);
302 strscpy(v
->name
, "Elonics E4000", sizeof(v
->name
));
303 v
->type
= V4L2_TUNER_RF
;
304 v
->capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
;
305 v
->rangelow
= bands
[0].rangelow
;
306 v
->rangehigh
= bands
[1].rangehigh
;
310 static int e4000_s_tuner(struct v4l2_subdev
*sd
, const struct v4l2_tuner
*v
)
312 struct e4000_dev
*dev
= e4000_subdev_to_dev(sd
);
313 struct i2c_client
*client
= dev
->client
;
315 dev_dbg(&client
->dev
, "index=%d\n", v
->index
);
319 static int e4000_g_frequency(struct v4l2_subdev
*sd
, struct v4l2_frequency
*f
)
321 struct e4000_dev
*dev
= e4000_subdev_to_dev(sd
);
322 struct i2c_client
*client
= dev
->client
;
324 dev_dbg(&client
->dev
, "tuner=%d\n", f
->tuner
);
325 f
->frequency
= dev
->f_frequency
;
329 static int e4000_s_frequency(struct v4l2_subdev
*sd
,
330 const struct v4l2_frequency
*f
)
332 struct e4000_dev
*dev
= e4000_subdev_to_dev(sd
);
333 struct i2c_client
*client
= dev
->client
;
335 dev_dbg(&client
->dev
, "tuner=%d type=%d frequency=%u\n",
336 f
->tuner
, f
->type
, f
->frequency
);
338 dev
->f_frequency
= clamp_t(unsigned int, f
->frequency
,
339 bands
[0].rangelow
, bands
[1].rangehigh
);
340 return e4000_set_params(dev
);
343 static int e4000_enum_freq_bands(struct v4l2_subdev
*sd
,
344 struct v4l2_frequency_band
*band
)
346 struct e4000_dev
*dev
= e4000_subdev_to_dev(sd
);
347 struct i2c_client
*client
= dev
->client
;
349 dev_dbg(&client
->dev
, "tuner=%d type=%d index=%d\n",
350 band
->tuner
, band
->type
, band
->index
);
352 if (band
->index
>= ARRAY_SIZE(bands
))
355 band
->capability
= bands
[band
->index
].capability
;
356 band
->rangelow
= bands
[band
->index
].rangelow
;
357 band
->rangehigh
= bands
[band
->index
].rangehigh
;
361 static const struct v4l2_subdev_tuner_ops e4000_subdev_tuner_ops
= {
362 .standby
= e4000_standby
,
363 .g_tuner
= e4000_g_tuner
,
364 .s_tuner
= e4000_s_tuner
,
365 .g_frequency
= e4000_g_frequency
,
366 .s_frequency
= e4000_s_frequency
,
367 .enum_freq_bands
= e4000_enum_freq_bands
,
370 static const struct v4l2_subdev_ops e4000_subdev_ops
= {
371 .tuner
= &e4000_subdev_tuner_ops
,
374 static int e4000_set_lna_gain(struct dvb_frontend
*fe
)
376 struct e4000_dev
*dev
= fe
->tuner_priv
;
377 struct i2c_client
*client
= dev
->client
;
381 dev_dbg(&client
->dev
, "lna auto=%d->%d val=%d->%d\n",
382 dev
->lna_gain_auto
->cur
.val
, dev
->lna_gain_auto
->val
,
383 dev
->lna_gain
->cur
.val
, dev
->lna_gain
->val
);
385 if (dev
->lna_gain_auto
->val
&& dev
->if_gain_auto
->cur
.val
)
387 else if (dev
->lna_gain_auto
->val
)
389 else if (dev
->if_gain_auto
->cur
.val
)
394 ret
= regmap_write(dev
->regmap
, 0x1a, u8tmp
);
398 if (dev
->lna_gain_auto
->val
== false) {
399 ret
= regmap_write(dev
->regmap
, 0x14, dev
->lna_gain
->val
);
406 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
410 static int e4000_set_mixer_gain(struct dvb_frontend
*fe
)
412 struct e4000_dev
*dev
= fe
->tuner_priv
;
413 struct i2c_client
*client
= dev
->client
;
417 dev_dbg(&client
->dev
, "mixer auto=%d->%d val=%d->%d\n",
418 dev
->mixer_gain_auto
->cur
.val
, dev
->mixer_gain_auto
->val
,
419 dev
->mixer_gain
->cur
.val
, dev
->mixer_gain
->val
);
421 if (dev
->mixer_gain_auto
->val
)
426 ret
= regmap_write(dev
->regmap
, 0x20, u8tmp
);
430 if (dev
->mixer_gain_auto
->val
== false) {
431 ret
= regmap_write(dev
->regmap
, 0x15, dev
->mixer_gain
->val
);
438 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
442 static int e4000_set_if_gain(struct dvb_frontend
*fe
)
444 struct e4000_dev
*dev
= fe
->tuner_priv
;
445 struct i2c_client
*client
= dev
->client
;
450 dev_dbg(&client
->dev
, "if auto=%d->%d val=%d->%d\n",
451 dev
->if_gain_auto
->cur
.val
, dev
->if_gain_auto
->val
,
452 dev
->if_gain
->cur
.val
, dev
->if_gain
->val
);
454 if (dev
->if_gain_auto
->val
&& dev
->lna_gain_auto
->cur
.val
)
456 else if (dev
->lna_gain_auto
->cur
.val
)
458 else if (dev
->if_gain_auto
->val
)
463 ret
= regmap_write(dev
->regmap
, 0x1a, u8tmp
);
467 if (dev
->if_gain_auto
->val
== false) {
468 buf
[0] = e4000_if_gain_lut
[dev
->if_gain
->val
].reg16_val
;
469 buf
[1] = e4000_if_gain_lut
[dev
->if_gain
->val
].reg17_val
;
470 ret
= regmap_bulk_write(dev
->regmap
, 0x16, buf
, 2);
477 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
481 static int e4000_pll_lock(struct dvb_frontend
*fe
)
483 struct e4000_dev
*dev
= fe
->tuner_priv
;
484 struct i2c_client
*client
= dev
->client
;
488 ret
= regmap_read(dev
->regmap
, 0x07, &uitmp
);
492 dev
->pll_lock
->val
= (uitmp
& 0x01);
496 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
500 static int e4000_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
502 struct e4000_dev
*dev
= container_of(ctrl
->handler
, struct e4000_dev
, hdl
);
503 struct i2c_client
*client
= dev
->client
;
510 case V4L2_CID_RF_TUNER_PLL_LOCK
:
511 ret
= e4000_pll_lock(dev
->fe
);
514 dev_dbg(&client
->dev
, "unknown ctrl: id=%d name=%s\n",
515 ctrl
->id
, ctrl
->name
);
522 static int e4000_s_ctrl(struct v4l2_ctrl
*ctrl
)
524 struct e4000_dev
*dev
= container_of(ctrl
->handler
, struct e4000_dev
, hdl
);
525 struct i2c_client
*client
= dev
->client
;
532 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
:
533 case V4L2_CID_RF_TUNER_BANDWIDTH
:
535 * TODO: Auto logic does not work 100% correctly as tuner driver
536 * do not have information to calculate maximum suitable
537 * bandwidth. Calculating it is responsible of master driver.
539 dev
->f_bandwidth
= dev
->bandwidth
->val
;
540 ret
= e4000_set_params(dev
);
542 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO
:
543 case V4L2_CID_RF_TUNER_LNA_GAIN
:
544 ret
= e4000_set_lna_gain(dev
->fe
);
546 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO
:
547 case V4L2_CID_RF_TUNER_MIXER_GAIN
:
548 ret
= e4000_set_mixer_gain(dev
->fe
);
550 case V4L2_CID_RF_TUNER_IF_GAIN_AUTO
:
551 case V4L2_CID_RF_TUNER_IF_GAIN
:
552 ret
= e4000_set_if_gain(dev
->fe
);
555 dev_dbg(&client
->dev
, "unknown ctrl: id=%d name=%s\n",
556 ctrl
->id
, ctrl
->name
);
563 static const struct v4l2_ctrl_ops e4000_ctrl_ops
= {
564 .g_volatile_ctrl
= e4000_g_volatile_ctrl
,
565 .s_ctrl
= e4000_s_ctrl
,
572 static int e4000_dvb_set_params(struct dvb_frontend
*fe
)
574 struct e4000_dev
*dev
= fe
->tuner_priv
;
575 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
577 dev
->f_frequency
= c
->frequency
;
578 dev
->f_bandwidth
= c
->bandwidth_hz
;
579 return e4000_set_params(dev
);
582 static int e4000_dvb_init(struct dvb_frontend
*fe
)
584 return e4000_init(fe
->tuner_priv
);
587 static int e4000_dvb_sleep(struct dvb_frontend
*fe
)
589 return e4000_sleep(fe
->tuner_priv
);
592 static int e4000_dvb_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
594 *frequency
= 0; /* Zero-IF */
598 static const struct dvb_tuner_ops e4000_dvb_tuner_ops
= {
600 .name
= "Elonics E4000",
601 .frequency_min_hz
= 174 * MHz
,
602 .frequency_max_hz
= 862 * MHz
,
605 .init
= e4000_dvb_init
,
606 .sleep
= e4000_dvb_sleep
,
607 .set_params
= e4000_dvb_set_params
,
609 .get_if_frequency
= e4000_dvb_get_if_frequency
,
612 static int e4000_probe(struct i2c_client
*client
,
613 const struct i2c_device_id
*id
)
615 struct e4000_dev
*dev
;
616 struct e4000_config
*cfg
= client
->dev
.platform_data
;
617 struct dvb_frontend
*fe
= cfg
->fe
;
620 static const struct regmap_config regmap_config
= {
625 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
631 dev
->clk
= cfg
->clock
;
632 dev
->client
= client
;
634 dev
->regmap
= devm_regmap_init_i2c(client
, ®map_config
);
635 if (IS_ERR(dev
->regmap
)) {
636 ret
= PTR_ERR(dev
->regmap
);
640 /* check if the tuner is there */
641 ret
= regmap_read(dev
->regmap
, 0x02, &uitmp
);
645 dev_dbg(&client
->dev
, "chip id=%02x\n", uitmp
);
652 /* put sleep as chip seems to be in normal mode by default */
653 ret
= regmap_write(dev
->regmap
, 0x00, 0x00);
657 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
658 /* Register controls */
659 v4l2_ctrl_handler_init(&dev
->hdl
, 9);
660 dev
->bandwidth_auto
= v4l2_ctrl_new_std(&dev
->hdl
, &e4000_ctrl_ops
,
661 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
, 0, 1, 1, 1);
662 dev
->bandwidth
= v4l2_ctrl_new_std(&dev
->hdl
, &e4000_ctrl_ops
,
663 V4L2_CID_RF_TUNER_BANDWIDTH
, 4300000, 11000000, 100000, 4300000);
664 v4l2_ctrl_auto_cluster(2, &dev
->bandwidth_auto
, 0, false);
665 dev
->lna_gain_auto
= v4l2_ctrl_new_std(&dev
->hdl
, &e4000_ctrl_ops
,
666 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO
, 0, 1, 1, 1);
667 dev
->lna_gain
= v4l2_ctrl_new_std(&dev
->hdl
, &e4000_ctrl_ops
,
668 V4L2_CID_RF_TUNER_LNA_GAIN
, 0, 15, 1, 10);
669 v4l2_ctrl_auto_cluster(2, &dev
->lna_gain_auto
, 0, false);
670 dev
->mixer_gain_auto
= v4l2_ctrl_new_std(&dev
->hdl
, &e4000_ctrl_ops
,
671 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO
, 0, 1, 1, 1);
672 dev
->mixer_gain
= v4l2_ctrl_new_std(&dev
->hdl
, &e4000_ctrl_ops
,
673 V4L2_CID_RF_TUNER_MIXER_GAIN
, 0, 1, 1, 1);
674 v4l2_ctrl_auto_cluster(2, &dev
->mixer_gain_auto
, 0, false);
675 dev
->if_gain_auto
= v4l2_ctrl_new_std(&dev
->hdl
, &e4000_ctrl_ops
,
676 V4L2_CID_RF_TUNER_IF_GAIN_AUTO
, 0, 1, 1, 1);
677 dev
->if_gain
= v4l2_ctrl_new_std(&dev
->hdl
, &e4000_ctrl_ops
,
678 V4L2_CID_RF_TUNER_IF_GAIN
, 0, 54, 1, 0);
679 v4l2_ctrl_auto_cluster(2, &dev
->if_gain_auto
, 0, false);
680 dev
->pll_lock
= v4l2_ctrl_new_std(&dev
->hdl
, &e4000_ctrl_ops
,
681 V4L2_CID_RF_TUNER_PLL_LOCK
, 0, 1, 1, 0);
682 if (dev
->hdl
.error
) {
683 ret
= dev
->hdl
.error
;
684 dev_err(&client
->dev
, "Could not initialize controls\n");
685 v4l2_ctrl_handler_free(&dev
->hdl
);
689 dev
->sd
.ctrl_handler
= &dev
->hdl
;
690 dev
->f_frequency
= bands
[0].rangelow
;
691 dev
->f_bandwidth
= dev
->bandwidth
->val
;
692 v4l2_i2c_subdev_init(&dev
->sd
, client
, &e4000_subdev_ops
);
694 fe
->tuner_priv
= dev
;
695 memcpy(&fe
->ops
.tuner_ops
, &e4000_dvb_tuner_ops
,
696 sizeof(fe
->ops
.tuner_ops
));
697 v4l2_set_subdevdata(&dev
->sd
, client
);
698 i2c_set_clientdata(client
, &dev
->sd
);
700 dev_info(&client
->dev
, "Elonics E4000 successfully identified\n");
705 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
709 static int e4000_remove(struct i2c_client
*client
)
711 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
712 struct e4000_dev
*dev
= container_of(sd
, struct e4000_dev
, sd
);
714 dev_dbg(&client
->dev
, "\n");
716 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
717 v4l2_ctrl_handler_free(&dev
->hdl
);
724 static const struct i2c_device_id e4000_id_table
[] = {
728 MODULE_DEVICE_TABLE(i2c
, e4000_id_table
);
730 static struct i2c_driver e4000_driver
= {
733 .suppress_bind_attrs
= true,
735 .probe
= e4000_probe
,
736 .remove
= e4000_remove
,
737 .id_table
= e4000_id_table
,
740 module_i2c_driver(e4000_driver
);
742 MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
743 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
744 MODULE_LICENSE("GPL");