2 * Elonics E4000 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 "e4000_priv.h"
22 #include <linux/math64.h>
24 static int e4000_init(struct dvb_frontend
*fe
)
26 struct e4000
*s
= fe
->tuner_priv
;
29 dev_dbg(&s
->client
->dev
, "%s:\n", __func__
);
31 /* dummy I2C to ensure I2C wakes up */
32 ret
= regmap_write(s
->regmap
, 0x02, 0x40);
35 ret
= regmap_write(s
->regmap
, 0x00, 0x01);
39 /* disable output clock */
40 ret
= regmap_write(s
->regmap
, 0x06, 0x00);
44 ret
= regmap_write(s
->regmap
, 0x7a, 0x96);
49 ret
= regmap_bulk_write(s
->regmap
, 0x7e, "\x01\xfe", 2);
53 ret
= regmap_write(s
->regmap
, 0x82, 0x00);
57 ret
= regmap_write(s
->regmap
, 0x24, 0x05);
61 ret
= regmap_bulk_write(s
->regmap
, 0x87, "\x20\x01", 2);
65 ret
= regmap_bulk_write(s
->regmap
, 0x9f, "\x7f\x07", 2);
69 /* DC offset control */
70 ret
= regmap_write(s
->regmap
, 0x2d, 0x1f);
74 ret
= regmap_bulk_write(s
->regmap
, 0x70, "\x01\x01", 2);
79 ret
= regmap_write(s
->regmap
, 0x1a, 0x17);
83 ret
= regmap_write(s
->regmap
, 0x1f, 0x1a);
90 dev_dbg(&s
->client
->dev
, "%s: failed=%d\n", __func__
, ret
);
95 static int e4000_sleep(struct dvb_frontend
*fe
)
97 struct e4000
*s
= fe
->tuner_priv
;
100 dev_dbg(&s
->client
->dev
, "%s:\n", __func__
);
104 ret
= regmap_write(s
->regmap
, 0x00, 0x00);
109 dev_dbg(&s
->client
->dev
, "%s: failed=%d\n", __func__
, ret
);
114 static int e4000_set_params(struct dvb_frontend
*fe
)
116 struct e4000
*s
= fe
->tuner_priv
;
117 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
118 int ret
, i
, sigma_delta
;
119 unsigned int pll_n
, pll_f
;
121 u8 buf
[5], i_data
[4], q_data
[4];
123 dev_dbg(&s
->client
->dev
,
124 "%s: delivery_system=%d frequency=%u bandwidth_hz=%u\n",
125 __func__
, c
->delivery_system
, c
->frequency
,
128 /* gain control manual */
129 ret
= regmap_write(s
->regmap
, 0x1a, 0x00);
134 for (i
= 0; i
< ARRAY_SIZE(e4000_pll_lut
); i
++) {
135 if (c
->frequency
<= e4000_pll_lut
[i
].freq
)
139 if (i
== ARRAY_SIZE(e4000_pll_lut
)) {
144 f_vco
= 1ull * c
->frequency
* e4000_pll_lut
[i
].mul
;
145 pll_n
= div_u64_rem(f_vco
, s
->clock
, &pll_f
);
146 sigma_delta
= div_u64(0x10000ULL
* pll_f
, s
->clock
);
148 buf
[1] = (sigma_delta
>> 0) & 0xff;
149 buf
[2] = (sigma_delta
>> 8) & 0xff;
151 buf
[4] = e4000_pll_lut
[i
].div
;
153 dev_dbg(&s
->client
->dev
,
154 "%s: f_vco=%llu pll div=%d sigma_delta=%04x\n",
155 __func__
, f_vco
, buf
[0], sigma_delta
);
157 ret
= regmap_bulk_write(s
->regmap
, 0x09, buf
, 5);
161 /* LNA filter (RF filter) */
162 for (i
= 0; i
< ARRAY_SIZE(e400_lna_filter_lut
); i
++) {
163 if (c
->frequency
<= e400_lna_filter_lut
[i
].freq
)
167 if (i
== ARRAY_SIZE(e400_lna_filter_lut
)) {
172 ret
= regmap_write(s
->regmap
, 0x10, e400_lna_filter_lut
[i
].val
);
177 for (i
= 0; i
< ARRAY_SIZE(e4000_if_filter_lut
); i
++) {
178 if (c
->bandwidth_hz
<= e4000_if_filter_lut
[i
].freq
)
182 if (i
== ARRAY_SIZE(e4000_if_filter_lut
)) {
187 buf
[0] = e4000_if_filter_lut
[i
].reg11_val
;
188 buf
[1] = e4000_if_filter_lut
[i
].reg12_val
;
190 ret
= regmap_bulk_write(s
->regmap
, 0x11, buf
, 2);
195 for (i
= 0; i
< ARRAY_SIZE(e4000_band_lut
); i
++) {
196 if (c
->frequency
<= e4000_band_lut
[i
].freq
)
200 if (i
== ARRAY_SIZE(e4000_band_lut
)) {
205 ret
= regmap_write(s
->regmap
, 0x07, e4000_band_lut
[i
].reg07_val
);
209 ret
= regmap_write(s
->regmap
, 0x78, e4000_band_lut
[i
].reg78_val
);
214 for (i
= 0; i
< 4; i
++) {
216 ret
= regmap_bulk_write(s
->regmap
, 0x15, "\x00\x7e\x24", 3);
218 ret
= regmap_bulk_write(s
->regmap
, 0x15, "\x00\x7f", 2);
220 ret
= regmap_bulk_write(s
->regmap
, 0x15, "\x01", 1);
222 ret
= regmap_bulk_write(s
->regmap
, 0x16, "\x7e", 1);
227 ret
= regmap_write(s
->regmap
, 0x29, 0x01);
231 ret
= regmap_bulk_read(s
->regmap
, 0x2a, buf
, 3);
235 i_data
[i
] = (((buf
[2] >> 0) & 0x3) << 6) | (buf
[0] & 0x3f);
236 q_data
[i
] = (((buf
[2] >> 4) & 0x3) << 6) | (buf
[1] & 0x3f);
239 swap(q_data
[2], q_data
[3]);
240 swap(i_data
[2], i_data
[3]);
242 ret
= regmap_bulk_write(s
->regmap
, 0x50, q_data
, 4);
246 ret
= regmap_bulk_write(s
->regmap
, 0x60, i_data
, 4);
250 /* gain control auto */
251 ret
= regmap_write(s
->regmap
, 0x1a, 0x17);
256 dev_dbg(&s
->client
->dev
, "%s: failed=%d\n", __func__
, ret
);
261 static int e4000_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
263 struct e4000
*s
= fe
->tuner_priv
;
265 dev_dbg(&s
->client
->dev
, "%s:\n", __func__
);
267 *frequency
= 0; /* Zero-IF */
272 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
273 static int e4000_set_lna_gain(struct dvb_frontend
*fe
)
275 struct e4000
*s
= fe
->tuner_priv
;
279 dev_dbg(&s
->client
->dev
, "%s: lna auto=%d->%d val=%d->%d\n",
280 __func__
, s
->lna_gain_auto
->cur
.val
,
281 s
->lna_gain_auto
->val
, s
->lna_gain
->cur
.val
,
284 if (s
->lna_gain_auto
->val
&& s
->if_gain_auto
->cur
.val
)
286 else if (s
->lna_gain_auto
->val
)
288 else if (s
->if_gain_auto
->cur
.val
)
293 ret
= regmap_write(s
->regmap
, 0x1a, u8tmp
);
297 if (s
->lna_gain_auto
->val
== false) {
298 ret
= regmap_write(s
->regmap
, 0x14, s
->lna_gain
->val
);
304 dev_dbg(&s
->client
->dev
, "%s: failed=%d\n", __func__
, ret
);
309 static int e4000_set_mixer_gain(struct dvb_frontend
*fe
)
311 struct e4000
*s
= fe
->tuner_priv
;
315 dev_dbg(&s
->client
->dev
, "%s: mixer auto=%d->%d val=%d->%d\n",
316 __func__
, s
->mixer_gain_auto
->cur
.val
,
317 s
->mixer_gain_auto
->val
, s
->mixer_gain
->cur
.val
,
320 if (s
->mixer_gain_auto
->val
)
325 ret
= regmap_write(s
->regmap
, 0x20, u8tmp
);
329 if (s
->mixer_gain_auto
->val
== false) {
330 ret
= regmap_write(s
->regmap
, 0x15, s
->mixer_gain
->val
);
336 dev_dbg(&s
->client
->dev
, "%s: failed=%d\n", __func__
, ret
);
341 static int e4000_set_if_gain(struct dvb_frontend
*fe
)
343 struct e4000
*s
= fe
->tuner_priv
;
348 dev_dbg(&s
->client
->dev
, "%s: if auto=%d->%d val=%d->%d\n",
349 __func__
, s
->if_gain_auto
->cur
.val
,
350 s
->if_gain_auto
->val
, s
->if_gain
->cur
.val
,
353 if (s
->if_gain_auto
->val
&& s
->lna_gain_auto
->cur
.val
)
355 else if (s
->lna_gain_auto
->cur
.val
)
357 else if (s
->if_gain_auto
->val
)
362 ret
= regmap_write(s
->regmap
, 0x1a, u8tmp
);
366 if (s
->if_gain_auto
->val
== false) {
367 buf
[0] = e4000_if_gain_lut
[s
->if_gain
->val
].reg16_val
;
368 buf
[1] = e4000_if_gain_lut
[s
->if_gain
->val
].reg17_val
;
369 ret
= regmap_bulk_write(s
->regmap
, 0x16, buf
, 2);
375 dev_dbg(&s
->client
->dev
, "%s: failed=%d\n", __func__
, ret
);
380 static int e4000_pll_lock(struct dvb_frontend
*fe
)
382 struct e4000
*s
= fe
->tuner_priv
;
386 ret
= regmap_read(s
->regmap
, 0x07, &utmp
);
390 s
->pll_lock
->val
= (utmp
& 0x01);
393 dev_dbg(&s
->client
->dev
, "%s: failed=%d\n", __func__
, ret
);
398 static int e4000_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
400 struct e4000
*s
= container_of(ctrl
->handler
, struct e4000
, hdl
);
403 if (s
->active
== false)
407 case V4L2_CID_RF_TUNER_PLL_LOCK
:
408 ret
= e4000_pll_lock(s
->fe
);
411 dev_dbg(&s
->client
->dev
, "%s: unknown ctrl: id=%d name=%s\n",
412 __func__
, ctrl
->id
, ctrl
->name
);
419 static int e4000_s_ctrl(struct v4l2_ctrl
*ctrl
)
421 struct e4000
*s
= container_of(ctrl
->handler
, struct e4000
, hdl
);
422 struct dvb_frontend
*fe
= s
->fe
;
423 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
426 if (s
->active
== false)
430 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
:
431 case V4L2_CID_RF_TUNER_BANDWIDTH
:
432 c
->bandwidth_hz
= s
->bandwidth
->val
;
433 ret
= e4000_set_params(s
->fe
);
435 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO
:
436 case V4L2_CID_RF_TUNER_LNA_GAIN
:
437 ret
= e4000_set_lna_gain(s
->fe
);
439 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO
:
440 case V4L2_CID_RF_TUNER_MIXER_GAIN
:
441 ret
= e4000_set_mixer_gain(s
->fe
);
443 case V4L2_CID_RF_TUNER_IF_GAIN_AUTO
:
444 case V4L2_CID_RF_TUNER_IF_GAIN
:
445 ret
= e4000_set_if_gain(s
->fe
);
448 dev_dbg(&s
->client
->dev
, "%s: unknown ctrl: id=%d name=%s\n",
449 __func__
, ctrl
->id
, ctrl
->name
);
456 static const struct v4l2_ctrl_ops e4000_ctrl_ops
= {
457 .g_volatile_ctrl
= e4000_g_volatile_ctrl
,
458 .s_ctrl
= e4000_s_ctrl
,
462 static const struct dvb_tuner_ops e4000_tuner_ops
= {
464 .name
= "Elonics E4000",
465 .frequency_min
= 174000000,
466 .frequency_max
= 862000000,
470 .sleep
= e4000_sleep
,
471 .set_params
= e4000_set_params
,
473 .get_if_frequency
= e4000_get_if_frequency
,
477 * Use V4L2 subdev to carry V4L2 control handler, even we don't implement
478 * subdev itself, just to avoid reinventing the wheel.
480 static int e4000_probe(struct i2c_client
*client
,
481 const struct i2c_device_id
*id
)
483 struct e4000_config
*cfg
= client
->dev
.platform_data
;
484 struct dvb_frontend
*fe
= cfg
->fe
;
488 static const struct regmap_config regmap_config
= {
491 .max_register
= 0xff,
494 s
= kzalloc(sizeof(struct e4000
), GFP_KERNEL
);
497 dev_err(&client
->dev
, "%s: kzalloc() failed\n", KBUILD_MODNAME
);
501 s
->clock
= cfg
->clock
;
504 s
->regmap
= devm_regmap_init_i2c(client
, ®map_config
);
505 if (IS_ERR(s
->regmap
)) {
506 ret
= PTR_ERR(s
->regmap
);
510 /* check if the tuner is there */
511 ret
= regmap_read(s
->regmap
, 0x02, &utmp
);
515 dev_dbg(&s
->client
->dev
, "%s: chip id=%02x\n", __func__
, utmp
);
522 /* put sleep as chip seems to be in normal mode by default */
523 ret
= regmap_write(s
->regmap
, 0x00, 0x00);
527 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
528 /* Register controls */
529 v4l2_ctrl_handler_init(&s
->hdl
, 9);
530 s
->bandwidth_auto
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
531 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
, 0, 1, 1, 1);
532 s
->bandwidth
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
533 V4L2_CID_RF_TUNER_BANDWIDTH
, 4300000, 11000000, 100000, 4300000);
534 v4l2_ctrl_auto_cluster(2, &s
->bandwidth_auto
, 0, false);
535 s
->lna_gain_auto
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
536 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO
, 0, 1, 1, 1);
537 s
->lna_gain
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
538 V4L2_CID_RF_TUNER_LNA_GAIN
, 0, 15, 1, 10);
539 v4l2_ctrl_auto_cluster(2, &s
->lna_gain_auto
, 0, false);
540 s
->mixer_gain_auto
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
541 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO
, 0, 1, 1, 1);
542 s
->mixer_gain
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
543 V4L2_CID_RF_TUNER_MIXER_GAIN
, 0, 1, 1, 1);
544 v4l2_ctrl_auto_cluster(2, &s
->mixer_gain_auto
, 0, false);
545 s
->if_gain_auto
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
546 V4L2_CID_RF_TUNER_IF_GAIN_AUTO
, 0, 1, 1, 1);
547 s
->if_gain
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
548 V4L2_CID_RF_TUNER_IF_GAIN
, 0, 54, 1, 0);
549 v4l2_ctrl_auto_cluster(2, &s
->if_gain_auto
, 0, false);
550 s
->pll_lock
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
551 V4L2_CID_RF_TUNER_PLL_LOCK
, 0, 1, 1, 0);
554 dev_err(&s
->client
->dev
, "Could not initialize controls\n");
555 v4l2_ctrl_handler_free(&s
->hdl
);
559 s
->sd
.ctrl_handler
= &s
->hdl
;
562 dev_info(&s
->client
->dev
,
563 "%s: Elonics E4000 successfully identified\n",
567 memcpy(&fe
->ops
.tuner_ops
, &e4000_tuner_ops
,
568 sizeof(struct dvb_tuner_ops
));
570 v4l2_set_subdevdata(&s
->sd
, client
);
571 i2c_set_clientdata(client
, &s
->sd
);
576 dev_dbg(&client
->dev
, "%s: failed=%d\n", __func__
, ret
);
583 static int e4000_remove(struct i2c_client
*client
)
585 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
586 struct e4000
*s
= container_of(sd
, struct e4000
, sd
);
587 struct dvb_frontend
*fe
= s
->fe
;
589 dev_dbg(&client
->dev
, "%s:\n", __func__
);
591 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
592 v4l2_ctrl_handler_free(&s
->hdl
);
594 memset(&fe
->ops
.tuner_ops
, 0, sizeof(struct dvb_tuner_ops
));
595 fe
->tuner_priv
= NULL
;
601 static const struct i2c_device_id e4000_id
[] = {
605 MODULE_DEVICE_TABLE(i2c
, e4000_id
);
607 static struct i2c_driver e4000_driver
= {
609 .owner
= THIS_MODULE
,
612 .probe
= e4000_probe
,
613 .remove
= e4000_remove
,
614 .id_table
= e4000_id
,
617 module_i2c_driver(e4000_driver
);
619 MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
620 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
621 MODULE_LICENSE("GPL");