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
, "\n");
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
, "failed=%d\n", ret
);
95 static int e4000_sleep(struct dvb_frontend
*fe
)
97 struct e4000
*s
= fe
->tuner_priv
;
100 dev_dbg(&s
->client
->dev
, "\n");
104 ret
= regmap_write(s
->regmap
, 0x00, 0x00);
109 dev_dbg(&s
->client
->dev
, "failed=%d\n", 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 "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
125 c
->delivery_system
, c
->frequency
, c
->bandwidth_hz
);
127 /* gain control manual */
128 ret
= regmap_write(s
->regmap
, 0x1a, 0x00);
133 for (i
= 0; i
< ARRAY_SIZE(e4000_pll_lut
); i
++) {
134 if (c
->frequency
<= e4000_pll_lut
[i
].freq
)
138 if (i
== ARRAY_SIZE(e4000_pll_lut
)) {
143 f_vco
= 1ull * c
->frequency
* e4000_pll_lut
[i
].mul
;
144 pll_n
= div_u64_rem(f_vco
, s
->clock
, &pll_f
);
145 sigma_delta
= div_u64(0x10000ULL
* pll_f
, s
->clock
);
147 buf
[1] = (sigma_delta
>> 0) & 0xff;
148 buf
[2] = (sigma_delta
>> 8) & 0xff;
150 buf
[4] = e4000_pll_lut
[i
].div
;
152 dev_dbg(&s
->client
->dev
, "f_vco=%llu pll div=%d sigma_delta=%04x\n",
153 f_vco
, buf
[0], sigma_delta
);
155 ret
= regmap_bulk_write(s
->regmap
, 0x09, buf
, 5);
159 /* LNA filter (RF filter) */
160 for (i
= 0; i
< ARRAY_SIZE(e400_lna_filter_lut
); i
++) {
161 if (c
->frequency
<= e400_lna_filter_lut
[i
].freq
)
165 if (i
== ARRAY_SIZE(e400_lna_filter_lut
)) {
170 ret
= regmap_write(s
->regmap
, 0x10, e400_lna_filter_lut
[i
].val
);
175 for (i
= 0; i
< ARRAY_SIZE(e4000_if_filter_lut
); i
++) {
176 if (c
->bandwidth_hz
<= e4000_if_filter_lut
[i
].freq
)
180 if (i
== ARRAY_SIZE(e4000_if_filter_lut
)) {
185 buf
[0] = e4000_if_filter_lut
[i
].reg11_val
;
186 buf
[1] = e4000_if_filter_lut
[i
].reg12_val
;
188 ret
= regmap_bulk_write(s
->regmap
, 0x11, buf
, 2);
193 for (i
= 0; i
< ARRAY_SIZE(e4000_band_lut
); i
++) {
194 if (c
->frequency
<= e4000_band_lut
[i
].freq
)
198 if (i
== ARRAY_SIZE(e4000_band_lut
)) {
203 ret
= regmap_write(s
->regmap
, 0x07, e4000_band_lut
[i
].reg07_val
);
207 ret
= regmap_write(s
->regmap
, 0x78, e4000_band_lut
[i
].reg78_val
);
212 for (i
= 0; i
< 4; i
++) {
214 ret
= regmap_bulk_write(s
->regmap
, 0x15, "\x00\x7e\x24", 3);
216 ret
= regmap_bulk_write(s
->regmap
, 0x15, "\x00\x7f", 2);
218 ret
= regmap_bulk_write(s
->regmap
, 0x15, "\x01", 1);
220 ret
= regmap_bulk_write(s
->regmap
, 0x16, "\x7e", 1);
225 ret
= regmap_write(s
->regmap
, 0x29, 0x01);
229 ret
= regmap_bulk_read(s
->regmap
, 0x2a, buf
, 3);
233 i_data
[i
] = (((buf
[2] >> 0) & 0x3) << 6) | (buf
[0] & 0x3f);
234 q_data
[i
] = (((buf
[2] >> 4) & 0x3) << 6) | (buf
[1] & 0x3f);
237 swap(q_data
[2], q_data
[3]);
238 swap(i_data
[2], i_data
[3]);
240 ret
= regmap_bulk_write(s
->regmap
, 0x50, q_data
, 4);
244 ret
= regmap_bulk_write(s
->regmap
, 0x60, i_data
, 4);
248 /* gain control auto */
249 ret
= regmap_write(s
->regmap
, 0x1a, 0x17);
254 dev_dbg(&s
->client
->dev
, "failed=%d\n", ret
);
259 static int e4000_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
261 struct e4000
*s
= fe
->tuner_priv
;
263 dev_dbg(&s
->client
->dev
, "\n");
265 *frequency
= 0; /* Zero-IF */
270 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
271 static int e4000_set_lna_gain(struct dvb_frontend
*fe
)
273 struct e4000
*s
= fe
->tuner_priv
;
277 dev_dbg(&s
->client
->dev
, "lna auto=%d->%d val=%d->%d\n",
278 s
->lna_gain_auto
->cur
.val
, s
->lna_gain_auto
->val
,
279 s
->lna_gain
->cur
.val
, s
->lna_gain
->val
);
281 if (s
->lna_gain_auto
->val
&& s
->if_gain_auto
->cur
.val
)
283 else if (s
->lna_gain_auto
->val
)
285 else if (s
->if_gain_auto
->cur
.val
)
290 ret
= regmap_write(s
->regmap
, 0x1a, u8tmp
);
294 if (s
->lna_gain_auto
->val
== false) {
295 ret
= regmap_write(s
->regmap
, 0x14, s
->lna_gain
->val
);
301 dev_dbg(&s
->client
->dev
, "failed=%d\n", ret
);
306 static int e4000_set_mixer_gain(struct dvb_frontend
*fe
)
308 struct e4000
*s
= fe
->tuner_priv
;
312 dev_dbg(&s
->client
->dev
, "mixer auto=%d->%d val=%d->%d\n",
313 s
->mixer_gain_auto
->cur
.val
, s
->mixer_gain_auto
->val
,
314 s
->mixer_gain
->cur
.val
, s
->mixer_gain
->val
);
316 if (s
->mixer_gain_auto
->val
)
321 ret
= regmap_write(s
->regmap
, 0x20, u8tmp
);
325 if (s
->mixer_gain_auto
->val
== false) {
326 ret
= regmap_write(s
->regmap
, 0x15, s
->mixer_gain
->val
);
332 dev_dbg(&s
->client
->dev
, "failed=%d\n", ret
);
337 static int e4000_set_if_gain(struct dvb_frontend
*fe
)
339 struct e4000
*s
= fe
->tuner_priv
;
344 dev_dbg(&s
->client
->dev
, "if auto=%d->%d val=%d->%d\n",
345 s
->if_gain_auto
->cur
.val
, s
->if_gain_auto
->val
,
346 s
->if_gain
->cur
.val
, s
->if_gain
->val
);
348 if (s
->if_gain_auto
->val
&& s
->lna_gain_auto
->cur
.val
)
350 else if (s
->lna_gain_auto
->cur
.val
)
352 else if (s
->if_gain_auto
->val
)
357 ret
= regmap_write(s
->regmap
, 0x1a, u8tmp
);
361 if (s
->if_gain_auto
->val
== false) {
362 buf
[0] = e4000_if_gain_lut
[s
->if_gain
->val
].reg16_val
;
363 buf
[1] = e4000_if_gain_lut
[s
->if_gain
->val
].reg17_val
;
364 ret
= regmap_bulk_write(s
->regmap
, 0x16, buf
, 2);
370 dev_dbg(&s
->client
->dev
, "failed=%d\n", ret
);
375 static int e4000_pll_lock(struct dvb_frontend
*fe
)
377 struct e4000
*s
= fe
->tuner_priv
;
381 ret
= regmap_read(s
->regmap
, 0x07, &utmp
);
385 s
->pll_lock
->val
= (utmp
& 0x01);
388 dev_dbg(&s
->client
->dev
, "failed=%d\n", ret
);
393 static int e4000_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
395 struct e4000
*s
= container_of(ctrl
->handler
, struct e4000
, hdl
);
402 case V4L2_CID_RF_TUNER_PLL_LOCK
:
403 ret
= e4000_pll_lock(s
->fe
);
406 dev_dbg(&s
->client
->dev
, "unknown ctrl: id=%d name=%s\n",
407 ctrl
->id
, ctrl
->name
);
414 static int e4000_s_ctrl(struct v4l2_ctrl
*ctrl
)
416 struct e4000
*s
= container_of(ctrl
->handler
, struct e4000
, hdl
);
417 struct dvb_frontend
*fe
= s
->fe
;
418 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
425 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
:
426 case V4L2_CID_RF_TUNER_BANDWIDTH
:
427 c
->bandwidth_hz
= s
->bandwidth
->val
;
428 ret
= e4000_set_params(s
->fe
);
430 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO
:
431 case V4L2_CID_RF_TUNER_LNA_GAIN
:
432 ret
= e4000_set_lna_gain(s
->fe
);
434 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO
:
435 case V4L2_CID_RF_TUNER_MIXER_GAIN
:
436 ret
= e4000_set_mixer_gain(s
->fe
);
438 case V4L2_CID_RF_TUNER_IF_GAIN_AUTO
:
439 case V4L2_CID_RF_TUNER_IF_GAIN
:
440 ret
= e4000_set_if_gain(s
->fe
);
443 dev_dbg(&s
->client
->dev
, "unknown ctrl: id=%d name=%s\n",
444 ctrl
->id
, ctrl
->name
);
451 static const struct v4l2_ctrl_ops e4000_ctrl_ops
= {
452 .g_volatile_ctrl
= e4000_g_volatile_ctrl
,
453 .s_ctrl
= e4000_s_ctrl
,
457 static const struct dvb_tuner_ops e4000_tuner_ops
= {
459 .name
= "Elonics E4000",
460 .frequency_min
= 174000000,
461 .frequency_max
= 862000000,
465 .sleep
= e4000_sleep
,
466 .set_params
= e4000_set_params
,
468 .get_if_frequency
= e4000_get_if_frequency
,
472 * Use V4L2 subdev to carry V4L2 control handler, even we don't implement
473 * subdev itself, just to avoid reinventing the wheel.
475 static int e4000_probe(struct i2c_client
*client
,
476 const struct i2c_device_id
*id
)
478 struct e4000_config
*cfg
= client
->dev
.platform_data
;
479 struct dvb_frontend
*fe
= cfg
->fe
;
483 static const struct regmap_config regmap_config
= {
486 .max_register
= 0xff,
489 s
= kzalloc(sizeof(struct e4000
), GFP_KERNEL
);
492 dev_err(&client
->dev
, "kzalloc() failed\n");
496 s
->clock
= cfg
->clock
;
499 s
->regmap
= devm_regmap_init_i2c(client
, ®map_config
);
500 if (IS_ERR(s
->regmap
)) {
501 ret
= PTR_ERR(s
->regmap
);
505 /* check if the tuner is there */
506 ret
= regmap_read(s
->regmap
, 0x02, &utmp
);
510 dev_dbg(&s
->client
->dev
, "chip id=%02x\n", utmp
);
517 /* put sleep as chip seems to be in normal mode by default */
518 ret
= regmap_write(s
->regmap
, 0x00, 0x00);
522 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
523 /* Register controls */
524 v4l2_ctrl_handler_init(&s
->hdl
, 9);
525 s
->bandwidth_auto
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
526 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
, 0, 1, 1, 1);
527 s
->bandwidth
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
528 V4L2_CID_RF_TUNER_BANDWIDTH
, 4300000, 11000000, 100000, 4300000);
529 v4l2_ctrl_auto_cluster(2, &s
->bandwidth_auto
, 0, false);
530 s
->lna_gain_auto
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
531 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO
, 0, 1, 1, 1);
532 s
->lna_gain
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
533 V4L2_CID_RF_TUNER_LNA_GAIN
, 0, 15, 1, 10);
534 v4l2_ctrl_auto_cluster(2, &s
->lna_gain_auto
, 0, false);
535 s
->mixer_gain_auto
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
536 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO
, 0, 1, 1, 1);
537 s
->mixer_gain
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
538 V4L2_CID_RF_TUNER_MIXER_GAIN
, 0, 1, 1, 1);
539 v4l2_ctrl_auto_cluster(2, &s
->mixer_gain_auto
, 0, false);
540 s
->if_gain_auto
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
541 V4L2_CID_RF_TUNER_IF_GAIN_AUTO
, 0, 1, 1, 1);
542 s
->if_gain
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
543 V4L2_CID_RF_TUNER_IF_GAIN
, 0, 54, 1, 0);
544 v4l2_ctrl_auto_cluster(2, &s
->if_gain_auto
, 0, false);
545 s
->pll_lock
= v4l2_ctrl_new_std(&s
->hdl
, &e4000_ctrl_ops
,
546 V4L2_CID_RF_TUNER_PLL_LOCK
, 0, 1, 1, 0);
549 dev_err(&s
->client
->dev
, "Could not initialize controls\n");
550 v4l2_ctrl_handler_free(&s
->hdl
);
554 s
->sd
.ctrl_handler
= &s
->hdl
;
557 dev_info(&s
->client
->dev
, "Elonics E4000 successfully identified\n");
560 memcpy(&fe
->ops
.tuner_ops
, &e4000_tuner_ops
,
561 sizeof(struct dvb_tuner_ops
));
563 v4l2_set_subdevdata(&s
->sd
, client
);
564 i2c_set_clientdata(client
, &s
->sd
);
569 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
576 static int e4000_remove(struct i2c_client
*client
)
578 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
579 struct e4000
*s
= container_of(sd
, struct e4000
, sd
);
580 struct dvb_frontend
*fe
= s
->fe
;
582 dev_dbg(&client
->dev
, "\n");
584 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
585 v4l2_ctrl_handler_free(&s
->hdl
);
587 memset(&fe
->ops
.tuner_ops
, 0, sizeof(struct dvb_tuner_ops
));
588 fe
->tuner_priv
= NULL
;
594 static const struct i2c_device_id e4000_id
[] = {
598 MODULE_DEVICE_TABLE(i2c
, e4000_id
);
600 static struct i2c_driver e4000_driver
= {
602 .owner
= THIS_MODULE
,
605 .probe
= e4000_probe
,
606 .remove
= e4000_remove
,
607 .id_table
= e4000_id
,
610 module_i2c_driver(e4000_driver
);
612 MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
613 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
614 MODULE_LICENSE("GPL");