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 /* Max transfer size done by I2C transfer functions */
25 #define MAX_XFER_SIZE 64
27 /* write multiple registers */
28 static int e4000_wr_regs(struct e4000_priv *priv, u8 reg, u8 *val, int len)
31 u8 buf[MAX_XFER_SIZE];
32 struct i2c_msg msg[1] = {
34 .addr = priv->client->addr,
41 if (1 + len > sizeof(buf)) {
42 dev_warn(&priv->client->dev,
43 "%s: i2c wr reg=%04x: len=%d is too big!\n",
44 KBUILD_MODNAME, reg, len);
49 memcpy(&buf[1], val, len);
51 ret = i2c_transfer(priv->client->adapter, msg, 1);
55 dev_warn(&priv->client->dev,
56 "%s: i2c wr failed=%d reg=%02x len=%d\n",
57 KBUILD_MODNAME, ret, reg, len);
63 /* read multiple registers */
64 static int e4000_rd_regs(struct e4000_priv *priv, u8 reg, u8 *val, int len)
67 u8 buf[MAX_XFER_SIZE];
68 struct i2c_msg msg[2] = {
70 .addr = priv->client->addr,
75 .addr = priv->client->addr,
82 if (len > sizeof(buf)) {
83 dev_warn(&priv->client->dev,
84 "%s: i2c rd reg=%04x: len=%d is too big!\n",
85 KBUILD_MODNAME, reg, len);
89 ret = i2c_transfer(priv->client->adapter, msg, 2);
91 memcpy(val, buf, len);
94 dev_warn(&priv->client->dev,
95 "%s: i2c rd failed=%d reg=%02x len=%d\n",
96 KBUILD_MODNAME, ret, reg, len);
103 /* write single register */
104 static int e4000_wr_reg(struct e4000_priv *priv, u8 reg, u8 val)
106 return e4000_wr_regs(priv, reg, &val, 1);
109 /* read single register */
110 static int e4000_rd_reg(struct e4000_priv *priv, u8 reg, u8 *val)
112 return e4000_rd_regs(priv, reg, val, 1);
115 static int e4000_init(struct dvb_frontend *fe)
117 struct e4000_priv *priv = fe->tuner_priv;
120 dev_dbg(&priv->client->dev, "%s:\n", __func__);
122 /* dummy I2C to ensure I2C wakes up */
123 ret = e4000_wr_reg(priv, 0x02, 0x40);
126 ret = e4000_wr_reg(priv, 0x00, 0x01);
130 /* disable output clock */
131 ret = e4000_wr_reg(priv, 0x06, 0x00);
135 ret = e4000_wr_reg(priv, 0x7a, 0x96);
139 /* configure gains */
140 ret = e4000_wr_regs(priv, 0x7e, "\x01\xfe", 2);
144 ret = e4000_wr_reg(priv, 0x82, 0x00);
148 ret = e4000_wr_reg(priv, 0x24, 0x05);
152 ret = e4000_wr_regs(priv, 0x87, "\x20\x01", 2);
156 ret = e4000_wr_regs(priv, 0x9f, "\x7f\x07", 2);
160 /* DC offset control */
161 ret = e4000_wr_reg(priv, 0x2d, 0x1f);
165 ret = e4000_wr_regs(priv, 0x70, "\x01\x01", 2);
170 ret = e4000_wr_reg(priv, 0x1a, 0x17);
174 ret = e4000_wr_reg(priv, 0x1f, 0x1a);
181 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
186 static int e4000_sleep(struct dvb_frontend *fe)
188 struct e4000_priv *priv = fe->tuner_priv;
191 dev_dbg(&priv->client->dev, "%s:\n", __func__);
193 priv->active = false;
195 ret = e4000_wr_reg(priv, 0x00, 0x00);
200 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
205 static int e4000_set_params(struct dvb_frontend *fe)
207 struct e4000_priv *priv = fe->tuner_priv;
208 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
209 int ret, i, sigma_delta;
211 u8 buf[5], i_data[4], q_data[4];
213 dev_dbg(&priv->client->dev,
214 "%s: delivery_system=%d frequency=%u bandwidth_hz=%u\n",
215 __func__, c->delivery_system, c->frequency,
218 /* gain control manual */
219 ret = e4000_wr_reg(priv, 0x1a, 0x00);
224 for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) {
225 if (c->frequency <= e4000_pll_lut[i].freq)
229 if (i == ARRAY_SIZE(e4000_pll_lut)) {
234 f_vco = 1ull * c->frequency * e4000_pll_lut[i].mul;
235 sigma_delta = div_u64(0x10000ULL * (f_vco % priv->clock), priv->clock);
236 buf[0] = div_u64(f_vco, priv->clock);
237 buf[1] = (sigma_delta >> 0) & 0xff;
238 buf[2] = (sigma_delta >> 8) & 0xff;
240 buf[4] = e4000_pll_lut[i].div;
242 dev_dbg(&priv->client->dev,
243 "%s: f_vco=%llu pll div=%d sigma_delta=%04x\n",
244 __func__, f_vco, buf[0], sigma_delta);
246 ret = e4000_wr_regs(priv, 0x09, buf, 5);
250 /* LNA filter (RF filter) */
251 for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) {
252 if (c->frequency <= e400_lna_filter_lut[i].freq)
256 if (i == ARRAY_SIZE(e400_lna_filter_lut)) {
261 ret = e4000_wr_reg(priv, 0x10, e400_lna_filter_lut[i].val);
266 for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) {
267 if (c->bandwidth_hz <= e4000_if_filter_lut[i].freq)
271 if (i == ARRAY_SIZE(e4000_if_filter_lut)) {
276 buf[0] = e4000_if_filter_lut[i].reg11_val;
277 buf[1] = e4000_if_filter_lut[i].reg12_val;
279 ret = e4000_wr_regs(priv, 0x11, buf, 2);
284 for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) {
285 if (c->frequency <= e4000_band_lut[i].freq)
289 if (i == ARRAY_SIZE(e4000_band_lut)) {
294 ret = e4000_wr_reg(priv, 0x07, e4000_band_lut[i].reg07_val);
298 ret = e4000_wr_reg(priv, 0x78, e4000_band_lut[i].reg78_val);
303 for (i = 0; i < 4; i++) {
305 ret = e4000_wr_regs(priv, 0x15, "\x00\x7e\x24", 3);
307 ret = e4000_wr_regs(priv, 0x15, "\x00\x7f", 2);
309 ret = e4000_wr_regs(priv, 0x15, "\x01", 1);
311 ret = e4000_wr_regs(priv, 0x16, "\x7e", 1);
316 ret = e4000_wr_reg(priv, 0x29, 0x01);
320 ret = e4000_rd_regs(priv, 0x2a, buf, 3);
324 i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f);
325 q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f);
328 swap(q_data[2], q_data[3]);
329 swap(i_data[2], i_data[3]);
331 ret = e4000_wr_regs(priv, 0x50, q_data, 4);
335 ret = e4000_wr_regs(priv, 0x60, i_data, 4);
339 /* gain control auto */
340 ret = e4000_wr_reg(priv, 0x1a, 0x17);
345 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
350 static int e4000_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
352 struct e4000_priv *priv = fe->tuner_priv;
354 dev_dbg(&priv->client->dev, "%s:\n", __func__);
356 *frequency = 0; /* Zero-IF */
361 static int e4000_set_lna_gain(struct dvb_frontend *fe)
363 struct e4000_priv *priv = fe->tuner_priv;
367 dev_dbg(&priv->client->dev, "%s: lna auto=%d->%d val=%d->%d\n",
368 __func__, priv->lna_gain_auto->cur.val,
369 priv->lna_gain_auto->val, priv->lna_gain->cur.val,
370 priv->lna_gain->val);
372 if (priv->lna_gain_auto->val && priv->if_gain_auto->cur.val)
374 else if (priv->lna_gain_auto->val)
376 else if (priv->if_gain_auto->cur.val)
381 ret = e4000_wr_reg(priv, 0x1a, u8tmp);
385 if (priv->lna_gain_auto->val == false) {
386 ret = e4000_wr_reg(priv, 0x14, priv->lna_gain->val);
392 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
397 static int e4000_set_mixer_gain(struct dvb_frontend *fe)
399 struct e4000_priv *priv = fe->tuner_priv;
403 dev_dbg(&priv->client->dev, "%s: mixer auto=%d->%d val=%d->%d\n",
404 __func__, priv->mixer_gain_auto->cur.val,
405 priv->mixer_gain_auto->val, priv->mixer_gain->cur.val,
406 priv->mixer_gain->val);
408 if (priv->mixer_gain_auto->val)
413 ret = e4000_wr_reg(priv, 0x20, u8tmp);
417 if (priv->mixer_gain_auto->val == false) {
418 ret = e4000_wr_reg(priv, 0x15, priv->mixer_gain->val);
424 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
429 static int e4000_set_if_gain(struct dvb_frontend *fe)
431 struct e4000_priv *priv = fe->tuner_priv;
436 dev_dbg(&priv->client->dev, "%s: if auto=%d->%d val=%d->%d\n",
437 __func__, priv->if_gain_auto->cur.val,
438 priv->if_gain_auto->val, priv->if_gain->cur.val,
441 if (priv->if_gain_auto->val && priv->lna_gain_auto->cur.val)
443 else if (priv->lna_gain_auto->cur.val)
445 else if (priv->if_gain_auto->val)
450 ret = e4000_wr_reg(priv, 0x1a, u8tmp);
454 if (priv->if_gain_auto->val == false) {
455 buf[0] = e4000_if_gain_lut[priv->if_gain->val].reg16_val;
456 buf[1] = e4000_if_gain_lut[priv->if_gain->val].reg17_val;
457 ret = e4000_wr_regs(priv, 0x16, buf, 2);
463 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
468 static int e4000_pll_lock(struct dvb_frontend *fe)
470 struct e4000_priv *priv = fe->tuner_priv;
474 if (priv->active == false)
477 ret = e4000_rd_reg(priv, 0x07, &u8tmp);
481 priv->pll_lock->val = (u8tmp & 0x01);
484 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
489 static int e4000_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
491 struct e4000_priv *priv =
492 container_of(ctrl->handler, struct e4000_priv, hdl);
496 case V4L2_CID_RF_TUNER_PLL_LOCK:
497 ret = e4000_pll_lock(priv->fe);
506 static int e4000_s_ctrl(struct v4l2_ctrl *ctrl)
508 struct e4000_priv *priv =
509 container_of(ctrl->handler, struct e4000_priv, hdl);
510 struct dvb_frontend *fe = priv->fe;
511 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
514 dev_dbg(&priv->client->dev,
515 "%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
516 __func__, ctrl->id, ctrl->name, ctrl->val,
517 ctrl->minimum, ctrl->maximum, ctrl->step);
520 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
521 case V4L2_CID_RF_TUNER_BANDWIDTH:
522 c->bandwidth_hz = priv->bandwidth->val;
523 ret = e4000_set_params(priv->fe);
525 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
526 case V4L2_CID_RF_TUNER_LNA_GAIN:
527 ret = e4000_set_lna_gain(priv->fe);
529 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
530 case V4L2_CID_RF_TUNER_MIXER_GAIN:
531 ret = e4000_set_mixer_gain(priv->fe);
533 case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
534 case V4L2_CID_RF_TUNER_IF_GAIN:
535 ret = e4000_set_if_gain(priv->fe);
544 static const struct v4l2_ctrl_ops e4000_ctrl_ops = {
545 .g_volatile_ctrl = e4000_g_volatile_ctrl,
546 .s_ctrl = e4000_s_ctrl,
549 static const struct dvb_tuner_ops e4000_tuner_ops = {
551 .name = "Elonics E4000",
552 .frequency_min = 174000000,
553 .frequency_max = 862000000,
557 .sleep = e4000_sleep,
558 .set_params = e4000_set_params,
560 .get_if_frequency = e4000_get_if_frequency,
564 * Use V4L2 subdev to carry V4L2 control handler, even we don't implement
565 * subdev itself, just to avoid reinventing the wheel.
567 static int e4000_probe(struct i2c_client *client,
568 const struct i2c_device_id *id)
570 struct e4000_config *cfg = client->dev.platform_data;
571 struct dvb_frontend *fe = cfg->fe;
572 struct e4000_priv *priv;
576 priv = kzalloc(sizeof(struct e4000_priv), GFP_KERNEL);
579 dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
583 priv->clock = cfg->clock;
584 priv->client = client;
587 /* check if the tuner is there */
588 ret = e4000_rd_reg(priv, 0x02, &chip_id);
592 dev_dbg(&priv->client->dev,
593 "%s: chip_id=%02x\n", __func__, chip_id);
595 if (chip_id != 0x40) {
600 /* put sleep as chip seems to be in normal mode by default */
601 ret = e4000_wr_reg(priv, 0x00, 0x00);
605 /* Register controls */
606 v4l2_ctrl_handler_init(&priv->hdl, 9);
607 priv->bandwidth_auto = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
608 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
609 priv->bandwidth = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
610 V4L2_CID_RF_TUNER_BANDWIDTH, 4300000, 11000000, 100000, 4300000);
611 v4l2_ctrl_auto_cluster(2, &priv->bandwidth_auto, 0, false);
612 priv->lna_gain_auto = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
613 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 1);
614 priv->lna_gain = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
615 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 15, 1, 10);
616 v4l2_ctrl_auto_cluster(2, &priv->lna_gain_auto, 0, false);
617 priv->mixer_gain_auto = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
618 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 1);
619 priv->mixer_gain = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
620 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
621 v4l2_ctrl_auto_cluster(2, &priv->mixer_gain_auto, 0, false);
622 priv->if_gain_auto = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
623 V4L2_CID_RF_TUNER_IF_GAIN_AUTO, 0, 1, 1, 1);
624 priv->if_gain = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
625 V4L2_CID_RF_TUNER_IF_GAIN, 0, 54, 1, 0);
626 v4l2_ctrl_auto_cluster(2, &priv->if_gain_auto, 0, false);
627 priv->pll_lock = v4l2_ctrl_new_std(&priv->hdl, &e4000_ctrl_ops,
628 V4L2_CID_RF_TUNER_PLL_LOCK, 0, 1, 1, 0);
629 if (priv->hdl.error) {
630 ret = priv->hdl.error;
631 dev_err(&priv->client->dev, "Could not initialize controls\n");
632 v4l2_ctrl_handler_free(&priv->hdl);
636 priv->sd.ctrl_handler = &priv->hdl;
638 dev_info(&priv->client->dev,
639 "%s: Elonics E4000 successfully identified\n",
642 fe->tuner_priv = priv;
643 memcpy(&fe->ops.tuner_ops, &e4000_tuner_ops,
644 sizeof(struct dvb_tuner_ops));
646 v4l2_set_subdevdata(&priv->sd, client);
647 i2c_set_clientdata(client, &priv->sd);
652 dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret);
659 static int e4000_remove(struct i2c_client *client)
661 struct v4l2_subdev *sd = i2c_get_clientdata(client);
662 struct e4000_priv *priv = container_of(sd, struct e4000_priv, sd);
663 struct dvb_frontend *fe = priv->fe;
665 dev_dbg(&client->dev, "%s:\n", __func__);
667 v4l2_ctrl_handler_free(&priv->hdl);
668 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
669 fe->tuner_priv = NULL;
675 static const struct i2c_device_id e4000_id[] = {
679 MODULE_DEVICE_TABLE(i2c, e4000_id);
681 static struct i2c_driver e4000_driver = {
683 .owner = THIS_MODULE,
686 .probe = e4000_probe,
687 .remove = e4000_remove,
688 .id_table = e4000_id,
691 module_i2c_driver(e4000_driver);
693 MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
694 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
695 MODULE_LICENSE("GPL");