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"
23 static int e4000_init(struct dvb_frontend *fe)
25 struct e4000_dev *dev = fe->tuner_priv;
26 struct i2c_client *client = dev->client;
29 dev_dbg(&client->dev, "\n");
32 ret = regmap_write(dev->regmap, 0x00, 0x01);
36 /* disable output clock */
37 ret = regmap_write(dev->regmap, 0x06, 0x00);
41 ret = regmap_write(dev->regmap, 0x7a, 0x96);
46 ret = regmap_bulk_write(dev->regmap, 0x7e, "\x01\xfe", 2);
50 ret = regmap_write(dev->regmap, 0x82, 0x00);
54 ret = regmap_write(dev->regmap, 0x24, 0x05);
58 ret = regmap_bulk_write(dev->regmap, 0x87, "\x20\x01", 2);
62 ret = regmap_bulk_write(dev->regmap, 0x9f, "\x7f\x07", 2);
66 /* DC offset control */
67 ret = regmap_write(dev->regmap, 0x2d, 0x1f);
71 ret = regmap_bulk_write(dev->regmap, 0x70, "\x01\x01", 2);
76 ret = regmap_write(dev->regmap, 0x1a, 0x17);
80 ret = regmap_write(dev->regmap, 0x1f, 0x1a);
88 dev_dbg(&client->dev, "failed=%d\n", ret);
92 static int e4000_sleep(struct dvb_frontend *fe)
94 struct e4000_dev *dev = fe->tuner_priv;
95 struct i2c_client *client = dev->client;
98 dev_dbg(&client->dev, "\n");
102 ret = regmap_write(dev->regmap, 0x00, 0x00);
108 dev_dbg(&client->dev, "failed=%d\n", ret);
112 static int e4000_set_params(struct dvb_frontend *fe)
114 struct e4000_dev *dev = fe->tuner_priv;
115 struct i2c_client *client = dev->client;
116 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
118 unsigned int div_n, k, k_cw, div_out;
120 u8 buf[5], i_data[4], q_data[4];
122 dev_dbg(&client->dev,
123 "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
124 c->delivery_system, c->frequency, c->bandwidth_hz);
126 /* gain control manual */
127 ret = regmap_write(dev->regmap, 0x1a, 0x00);
132 * Fractional-N synthesizer
134 * +----------------------------+
136 * Fref +----+ +-------+ +------+ +---+
137 * ------> | PD | --> | VCO | ------> | /N.F | <-- | K |
138 * +----+ +-------+ +------+ +---+
146 for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) {
147 if (c->frequency <= e4000_pll_lut[i].freq)
150 if (i == ARRAY_SIZE(e4000_pll_lut)) {
155 #define F_REF dev->clk
156 div_out = e4000_pll_lut[i].div_out;
157 f_vco = (u64) c->frequency * div_out;
158 /* calculate PLL integer and fractional control word */
159 div_n = div_u64_rem(f_vco, F_REF, &k);
160 k_cw = div_u64((u64) k * 0x10000, F_REF);
162 dev_dbg(&client->dev,
163 "frequency=%u f_vco=%llu F_REF=%u div_n=%u k=%u k_cw=%04x div_out=%u\n",
164 c->frequency, f_vco, F_REF, div_n, k, k_cw, div_out);
167 buf[1] = (k_cw >> 0) & 0xff;
168 buf[2] = (k_cw >> 8) & 0xff;
170 buf[4] = e4000_pll_lut[i].div_out_reg;
171 ret = regmap_bulk_write(dev->regmap, 0x09, buf, 5);
175 /* LNA filter (RF filter) */
176 for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) {
177 if (c->frequency <= e400_lna_filter_lut[i].freq)
180 if (i == ARRAY_SIZE(e400_lna_filter_lut)) {
185 ret = regmap_write(dev->regmap, 0x10, e400_lna_filter_lut[i].val);
190 for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) {
191 if (c->bandwidth_hz <= e4000_if_filter_lut[i].freq)
194 if (i == ARRAY_SIZE(e4000_if_filter_lut)) {
199 buf[0] = e4000_if_filter_lut[i].reg11_val;
200 buf[1] = e4000_if_filter_lut[i].reg12_val;
202 ret = regmap_bulk_write(dev->regmap, 0x11, buf, 2);
207 for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) {
208 if (c->frequency <= e4000_band_lut[i].freq)
211 if (i == ARRAY_SIZE(e4000_band_lut)) {
216 ret = regmap_write(dev->regmap, 0x07, e4000_band_lut[i].reg07_val);
220 ret = regmap_write(dev->regmap, 0x78, e4000_band_lut[i].reg78_val);
225 for (i = 0; i < 4; i++) {
227 ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7e\x24", 3);
229 ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7f", 2);
231 ret = regmap_bulk_write(dev->regmap, 0x15, "\x01", 1);
233 ret = regmap_bulk_write(dev->regmap, 0x16, "\x7e", 1);
238 ret = regmap_write(dev->regmap, 0x29, 0x01);
242 ret = regmap_bulk_read(dev->regmap, 0x2a, buf, 3);
246 i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f);
247 q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f);
250 swap(q_data[2], q_data[3]);
251 swap(i_data[2], i_data[3]);
253 ret = regmap_bulk_write(dev->regmap, 0x50, q_data, 4);
257 ret = regmap_bulk_write(dev->regmap, 0x60, i_data, 4);
261 /* gain control auto */
262 ret = regmap_write(dev->regmap, 0x1a, 0x17);
268 dev_dbg(&client->dev, "failed=%d\n", ret);
272 static int e4000_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
274 struct e4000_dev *dev = fe->tuner_priv;
275 struct i2c_client *client = dev->client;
277 dev_dbg(&client->dev, "\n");
279 *frequency = 0; /* Zero-IF */
284 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
285 static int e4000_set_lna_gain(struct dvb_frontend *fe)
287 struct e4000_dev *dev = fe->tuner_priv;
288 struct i2c_client *client = dev->client;
292 dev_dbg(&client->dev, "lna auto=%d->%d val=%d->%d\n",
293 dev->lna_gain_auto->cur.val, dev->lna_gain_auto->val,
294 dev->lna_gain->cur.val, dev->lna_gain->val);
296 if (dev->lna_gain_auto->val && dev->if_gain_auto->cur.val)
298 else if (dev->lna_gain_auto->val)
300 else if (dev->if_gain_auto->cur.val)
305 ret = regmap_write(dev->regmap, 0x1a, u8tmp);
309 if (dev->lna_gain_auto->val == false) {
310 ret = regmap_write(dev->regmap, 0x14, dev->lna_gain->val);
317 dev_dbg(&client->dev, "failed=%d\n", ret);
321 static int e4000_set_mixer_gain(struct dvb_frontend *fe)
323 struct e4000_dev *dev = fe->tuner_priv;
324 struct i2c_client *client = dev->client;
328 dev_dbg(&client->dev, "mixer auto=%d->%d val=%d->%d\n",
329 dev->mixer_gain_auto->cur.val, dev->mixer_gain_auto->val,
330 dev->mixer_gain->cur.val, dev->mixer_gain->val);
332 if (dev->mixer_gain_auto->val)
337 ret = regmap_write(dev->regmap, 0x20, u8tmp);
341 if (dev->mixer_gain_auto->val == false) {
342 ret = regmap_write(dev->regmap, 0x15, dev->mixer_gain->val);
349 dev_dbg(&client->dev, "failed=%d\n", ret);
353 static int e4000_set_if_gain(struct dvb_frontend *fe)
355 struct e4000_dev *dev = fe->tuner_priv;
356 struct i2c_client *client = dev->client;
361 dev_dbg(&client->dev, "if auto=%d->%d val=%d->%d\n",
362 dev->if_gain_auto->cur.val, dev->if_gain_auto->val,
363 dev->if_gain->cur.val, dev->if_gain->val);
365 if (dev->if_gain_auto->val && dev->lna_gain_auto->cur.val)
367 else if (dev->lna_gain_auto->cur.val)
369 else if (dev->if_gain_auto->val)
374 ret = regmap_write(dev->regmap, 0x1a, u8tmp);
378 if (dev->if_gain_auto->val == false) {
379 buf[0] = e4000_if_gain_lut[dev->if_gain->val].reg16_val;
380 buf[1] = e4000_if_gain_lut[dev->if_gain->val].reg17_val;
381 ret = regmap_bulk_write(dev->regmap, 0x16, buf, 2);
388 dev_dbg(&client->dev, "failed=%d\n", ret);
392 static int e4000_pll_lock(struct dvb_frontend *fe)
394 struct e4000_dev *dev = fe->tuner_priv;
395 struct i2c_client *client = dev->client;
399 ret = regmap_read(dev->regmap, 0x07, &uitmp);
403 dev->pll_lock->val = (uitmp & 0x01);
407 dev_dbg(&client->dev, "failed=%d\n", ret);
411 static int e4000_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
413 struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl);
414 struct i2c_client *client = dev->client;
421 case V4L2_CID_RF_TUNER_PLL_LOCK:
422 ret = e4000_pll_lock(dev->fe);
425 dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n",
426 ctrl->id, ctrl->name);
433 static int e4000_s_ctrl(struct v4l2_ctrl *ctrl)
435 struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl);
436 struct i2c_client *client = dev->client;
437 struct dtv_frontend_properties *c = &dev->fe->dtv_property_cache;
444 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
445 case V4L2_CID_RF_TUNER_BANDWIDTH:
446 c->bandwidth_hz = dev->bandwidth->val;
447 ret = e4000_set_params(dev->fe);
449 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
450 case V4L2_CID_RF_TUNER_LNA_GAIN:
451 ret = e4000_set_lna_gain(dev->fe);
453 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
454 case V4L2_CID_RF_TUNER_MIXER_GAIN:
455 ret = e4000_set_mixer_gain(dev->fe);
457 case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
458 case V4L2_CID_RF_TUNER_IF_GAIN:
459 ret = e4000_set_if_gain(dev->fe);
462 dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n",
463 ctrl->id, ctrl->name);
470 static const struct v4l2_ctrl_ops e4000_ctrl_ops = {
471 .g_volatile_ctrl = e4000_g_volatile_ctrl,
472 .s_ctrl = e4000_s_ctrl,
476 static const struct dvb_tuner_ops e4000_tuner_ops = {
478 .name = "Elonics E4000",
479 .frequency_min = 174000000,
480 .frequency_max = 862000000,
484 .sleep = e4000_sleep,
485 .set_params = e4000_set_params,
487 .get_if_frequency = e4000_get_if_frequency,
491 * Use V4L2 subdev to carry V4L2 control handler, even we don't implement
492 * subdev itself, just to avoid reinventing the wheel.
494 static int e4000_probe(struct i2c_client *client,
495 const struct i2c_device_id *id)
497 struct e4000_dev *dev;
498 struct e4000_config *cfg = client->dev.platform_data;
499 struct dvb_frontend *fe = cfg->fe;
502 static const struct regmap_config regmap_config = {
507 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
513 dev->clk = cfg->clock;
514 dev->client = client;
516 dev->regmap = devm_regmap_init_i2c(client, ®map_config);
517 if (IS_ERR(dev->regmap)) {
518 ret = PTR_ERR(dev->regmap);
522 /* check if the tuner is there */
523 ret = regmap_read(dev->regmap, 0x02, &uitmp);
527 dev_dbg(&client->dev, "chip id=%02x\n", uitmp);
534 /* put sleep as chip seems to be in normal mode by default */
535 ret = regmap_write(dev->regmap, 0x00, 0x00);
539 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
540 /* Register controls */
541 v4l2_ctrl_handler_init(&dev->hdl, 9);
542 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
543 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
544 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
545 V4L2_CID_RF_TUNER_BANDWIDTH, 4300000, 11000000, 100000, 4300000);
546 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
547 dev->lna_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
548 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 1);
549 dev->lna_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
550 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 15, 1, 10);
551 v4l2_ctrl_auto_cluster(2, &dev->lna_gain_auto, 0, false);
552 dev->mixer_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
553 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 1);
554 dev->mixer_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
555 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
556 v4l2_ctrl_auto_cluster(2, &dev->mixer_gain_auto, 0, false);
557 dev->if_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
558 V4L2_CID_RF_TUNER_IF_GAIN_AUTO, 0, 1, 1, 1);
559 dev->if_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
560 V4L2_CID_RF_TUNER_IF_GAIN, 0, 54, 1, 0);
561 v4l2_ctrl_auto_cluster(2, &dev->if_gain_auto, 0, false);
562 dev->pll_lock = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
563 V4L2_CID_RF_TUNER_PLL_LOCK, 0, 1, 1, 0);
564 if (dev->hdl.error) {
565 ret = dev->hdl.error;
566 dev_err(&client->dev, "Could not initialize controls\n");
567 v4l2_ctrl_handler_free(&dev->hdl);
571 dev->sd.ctrl_handler = &dev->hdl;
573 fe->tuner_priv = dev;
574 memcpy(&fe->ops.tuner_ops, &e4000_tuner_ops,
575 sizeof(struct dvb_tuner_ops));
576 v4l2_set_subdevdata(&dev->sd, client);
577 i2c_set_clientdata(client, &dev->sd);
579 dev_info(&client->dev, "Elonics E4000 successfully identified\n");
584 dev_dbg(&client->dev, "failed=%d\n", ret);
588 static int e4000_remove(struct i2c_client *client)
590 struct v4l2_subdev *sd = i2c_get_clientdata(client);
591 struct e4000_dev *dev = container_of(sd, struct e4000_dev, sd);
593 dev_dbg(&client->dev, "\n");
595 #if IS_ENABLED(CONFIG_VIDEO_V4L2)
596 v4l2_ctrl_handler_free(&dev->hdl);
603 static const struct i2c_device_id e4000_id_table[] = {
607 MODULE_DEVICE_TABLE(i2c, e4000_id_table);
609 static struct i2c_driver e4000_driver = {
611 .owner = THIS_MODULE,
613 .suppress_bind_attrs = true,
615 .probe = e4000_probe,
616 .remove = e4000_remove,
617 .id_table = e4000_id_table,
620 module_i2c_driver(e4000_driver);
622 MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
623 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
624 MODULE_LICENSE("GPL");