2 Montage Technology TS2020 - Silicon Tuner driver
3 Copyright (C) 2009-2012 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
5 Copyright (C) 2009-2012 TurboSight.com
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "dvb_frontend.h"
24 #include <linux/regmap.h>
26 #define TS2020_XTAL_FREQ 27000 /* in kHz */
27 #define FREQ_OFFSET_LOW_SYM_RATE 3000
30 struct i2c_client *client;
31 struct mutex regmap_mutex;
32 struct regmap_config regmap_config;
33 struct regmap *regmap;
34 struct dvb_frontend *fe;
35 struct delayed_work stat_work;
36 int (*get_agc_pwm)(struct dvb_frontend *fe, u8 *_agc_pwm);
38 struct i2c_adapter *i2c;
42 u32 frequency_div; /* LO output divider switch frequency */
43 u32 frequency_khz; /* actual used LO frequency */
44 #define TS2020_M88TS2020 0
45 #define TS2020_M88TS2022 1
50 struct ts2020_reg_val {
55 static int ts2020_release(struct dvb_frontend *fe)
57 struct ts2020_priv *priv = fe->tuner_priv;
58 struct i2c_client *client = priv->client;
60 dev_dbg(&client->dev, "\n");
62 i2c_unregister_device(client);
66 static int ts2020_sleep(struct dvb_frontend *fe)
68 struct ts2020_priv *priv = fe->tuner_priv;
72 if (priv->tuner == TS2020_M88TS2020)
73 u8tmp = 0x0a; /* XXX: probably wrong */
77 ret = regmap_write(priv->regmap, u8tmp, 0x00);
81 /* stop statistics polling */
82 cancel_delayed_work_sync(&priv->stat_work);
86 static int ts2020_init(struct dvb_frontend *fe)
88 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
89 struct ts2020_priv *priv = fe->tuner_priv;
93 if (priv->tuner == TS2020_M88TS2020) {
94 regmap_write(priv->regmap, 0x42, 0x73);
95 regmap_write(priv->regmap, 0x05, priv->clk_out_div);
96 regmap_write(priv->regmap, 0x20, 0x27);
97 regmap_write(priv->regmap, 0x07, 0x02);
98 regmap_write(priv->regmap, 0x11, 0xff);
99 regmap_write(priv->regmap, 0x60, 0xf9);
100 regmap_write(priv->regmap, 0x08, 0x01);
101 regmap_write(priv->regmap, 0x00, 0x41);
103 static const struct ts2020_reg_val reg_vals[] = {
117 regmap_write(priv->regmap, 0x00, 0x01);
118 regmap_write(priv->regmap, 0x00, 0x03);
120 switch (priv->clk_out) {
121 case TS2020_CLK_OUT_DISABLED:
124 case TS2020_CLK_OUT_ENABLED:
126 regmap_write(priv->regmap, 0x05, priv->clk_out_div);
128 case TS2020_CLK_OUT_ENABLED_XTALOUT:
136 regmap_write(priv->regmap, 0x42, u8tmp);
138 if (priv->loop_through)
143 regmap_write(priv->regmap, 0x62, u8tmp);
145 for (i = 0; i < ARRAY_SIZE(reg_vals); i++)
146 regmap_write(priv->regmap, reg_vals[i].reg,
150 /* Initialise v5 stats here */
152 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
153 c->strength.stat[0].uvalue = 0;
155 /* Start statistics polling */
156 schedule_delayed_work(&priv->stat_work, 0);
160 static int ts2020_tuner_gate_ctrl(struct dvb_frontend *fe, u8 offset)
162 struct ts2020_priv *priv = fe->tuner_priv;
164 ret = regmap_write(priv->regmap, 0x51, 0x1f - offset);
165 ret |= regmap_write(priv->regmap, 0x51, 0x1f);
166 ret |= regmap_write(priv->regmap, 0x50, offset);
167 ret |= regmap_write(priv->regmap, 0x50, 0x00);
172 static int ts2020_set_tuner_rf(struct dvb_frontend *fe)
174 struct ts2020_priv *dev = fe->tuner_priv;
178 ret = regmap_read(dev->regmap, 0x3d, &utmp);
182 else if (utmp == 0x16)
187 regmap_write(dev->regmap, 0x60, utmp);
188 ret = ts2020_tuner_gate_ctrl(fe, 0x08);
193 static int ts2020_set_params(struct dvb_frontend *fe)
195 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
196 struct ts2020_priv *priv = fe->tuner_priv;
200 u16 u16tmp, value, lpf_coeff;
201 u8 buf[3], reg10, lpf_mxdiv, mlpf_max, mlpf_min, nlpf;
202 unsigned int f_ref_khz, f_vco_khz, div_ref, div_out, pll_n;
203 unsigned int frequency_khz = c->frequency;
206 * Integer-N PLL synthesizer
207 * kHz is used for all calculations to keep calculations within 32-bit
209 f_ref_khz = TS2020_XTAL_FREQ;
210 div_ref = DIV_ROUND_CLOSEST(f_ref_khz, 2000);
212 /* select LO output divider */
213 if (frequency_khz < priv->frequency_div) {
221 f_vco_khz = frequency_khz * div_out;
222 pll_n = f_vco_khz * div_ref / f_ref_khz;
224 priv->frequency_khz = pll_n * f_ref_khz / div_ref / div_out;
226 pr_debug("frequency=%u offset=%d f_vco_khz=%u pll_n=%u div_ref=%u div_out=%u\n",
227 priv->frequency_khz, priv->frequency_khz - c->frequency,
228 f_vco_khz, pll_n, div_ref, div_out);
230 if (priv->tuner == TS2020_M88TS2020) {
233 ret = regmap_write(priv->regmap, 0x10, reg10);
237 ret = regmap_write(priv->regmap, 0x10, reg10);
238 ret |= regmap_write(priv->regmap, 0x11, 0x40);
241 u16tmp = pll_n - 1024;
242 buf[0] = (u16tmp >> 8) & 0xff;
243 buf[1] = (u16tmp >> 0) & 0xff;
244 buf[2] = div_ref - 8;
246 ret |= regmap_write(priv->regmap, 0x01, buf[0]);
247 ret |= regmap_write(priv->regmap, 0x02, buf[1]);
248 ret |= regmap_write(priv->regmap, 0x03, buf[2]);
250 ret |= ts2020_tuner_gate_ctrl(fe, 0x10);
254 ret |= ts2020_tuner_gate_ctrl(fe, 0x08);
257 if (priv->tuner == TS2020_M88TS2020)
258 ret |= ts2020_set_tuner_rf(fe);
260 gdiv28 = (TS2020_XTAL_FREQ / 1000 * 1694 + 500) / 1000;
261 ret |= regmap_write(priv->regmap, 0x04, gdiv28 & 0xff);
262 ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
266 if (priv->tuner == TS2020_M88TS2022) {
267 ret = regmap_write(priv->regmap, 0x25, 0x00);
268 ret |= regmap_write(priv->regmap, 0x27, 0x70);
269 ret |= regmap_write(priv->regmap, 0x41, 0x09);
270 ret |= regmap_write(priv->regmap, 0x08, 0x0b);
275 regmap_read(priv->regmap, 0x26, &utmp);
278 f3db = (c->bandwidth_hz / 1000 / 2) + 2000;
279 f3db += FREQ_OFFSET_LOW_SYM_RATE; /* FIXME: ~always too wide filter */
280 f3db = clamp(f3db, 7000U, 40000U);
282 gdiv28 = gdiv28 * 207 / (value * 2 + 151);
283 mlpf_max = gdiv28 * 135 / 100;
284 mlpf_min = gdiv28 * 78 / 100;
288 nlpf = (f3db * gdiv28 * 2 / lpf_coeff /
289 (TS2020_XTAL_FREQ / 1000) + 1) / 2;
295 lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
296 * lpf_coeff * 2 / f3db + 1) / 2;
298 if (lpf_mxdiv < mlpf_min) {
300 lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
301 * lpf_coeff * 2 / f3db + 1) / 2;
304 if (lpf_mxdiv > mlpf_max)
305 lpf_mxdiv = mlpf_max;
307 ret = regmap_write(priv->regmap, 0x04, lpf_mxdiv);
308 ret |= regmap_write(priv->regmap, 0x06, nlpf);
310 ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
312 ret |= ts2020_tuner_gate_ctrl(fe, 0x01);
316 return (ret < 0) ? -EINVAL : 0;
319 static int ts2020_get_frequency(struct dvb_frontend *fe, u32 *frequency)
321 struct ts2020_priv *priv = fe->tuner_priv;
323 *frequency = priv->frequency_khz;
327 static int ts2020_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
329 *frequency = 0; /* Zero-IF */
334 * Get the tuner gain.
335 * @fe: The front end for which we're determining the gain
336 * @v_agc: The voltage of the AGC from the demodulator (0-2600mV)
337 * @_gain: Where to store the gain (in 0.001dB units)
339 * Returns 0 or a negative error code.
341 static int ts2020_read_tuner_gain(struct dvb_frontend *fe, unsigned v_agc,
344 struct ts2020_priv *priv = fe->tuner_priv;
345 unsigned long gain1, gain2, gain3;
349 /* Read the RF gain */
350 ret = regmap_read(priv->regmap, 0x3d, &utmp);
355 /* Read the baseband gain */
356 ret = regmap_read(priv->regmap, 0x21, &utmp);
361 switch (priv->tuner) {
362 case TS2020_M88TS2020:
363 gain1 = clamp_t(long, gain1, 0, 15);
364 gain2 = clamp_t(long, gain2, 0, 13);
365 v_agc = clamp_t(long, v_agc, 400, 1100);
367 *_gain = -(gain1 * 2330 +
369 v_agc * 24 / 10 * 10 +
371 /* gain in range -19600 to -116850 in units of 0.001dB */
374 case TS2020_M88TS2022:
375 ret = regmap_read(priv->regmap, 0x66, &utmp);
378 gain3 = (utmp >> 3) & 0x07;
380 gain1 = clamp_t(long, gain1, 0, 15);
381 gain2 = clamp_t(long, gain2, 2, 16);
382 gain3 = clamp_t(long, gain3, 0, 6);
383 v_agc = clamp_t(long, v_agc, 600, 1600);
385 *_gain = -(gain1 * 2650 +
388 v_agc * 176 / 100 * 10 -
390 /* gain in range -47320 to -158950 in units of 0.001dB */
398 * Get the AGC information from the demodulator and use that to calculate the
401 static int ts2020_get_tuner_gain(struct dvb_frontend *fe, __s64 *_gain)
403 struct ts2020_priv *priv = fe->tuner_priv;
407 /* Read the AGC PWM rate from the demodulator */
408 if (priv->get_agc_pwm) {
409 ret = priv->get_agc_pwm(fe, &agc_pwm);
413 switch (priv->tuner) {
414 case TS2020_M88TS2020:
415 v_agc = (int)agc_pwm * 20 - 1166;
417 case TS2020_M88TS2022:
418 v_agc = (int)agc_pwm * 16 - 670;
426 return ts2020_read_tuner_gain(fe, v_agc, _gain);
430 * Gather statistics on a regular basis
432 static void ts2020_stat_work(struct work_struct *work)
434 struct ts2020_priv *priv = container_of(work, struct ts2020_priv,
436 struct i2c_client *client = priv->client;
437 struct dtv_frontend_properties *c = &priv->fe->dtv_property_cache;
440 dev_dbg(&client->dev, "\n");
442 ret = ts2020_get_tuner_gain(priv->fe, &c->strength.stat[0].svalue);
446 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
448 schedule_delayed_work(&priv->stat_work, msecs_to_jiffies(2000));
451 dev_dbg(&client->dev, "failed=%d\n", ret);
455 * Read TS2020 signal strength in v3 format.
457 static int ts2020_read_signal_strength(struct dvb_frontend *fe,
458 u16 *_signal_strength)
460 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
464 if (c->strength.stat[0].scale == FE_SCALE_NOT_AVAILABLE) {
465 *_signal_strength = 0;
469 gain = c->strength.stat[0].svalue;
471 /* Calculate the signal strength based on the total gain of the tuner */
473 /* 0%: no signal or weak signal */
475 else if (gain < -65000)
476 /* 0% - 60%: weak signal */
477 strength = 0 + (85000 + gain) * 3 / 1000;
478 else if (gain < -45000)
479 /* 60% - 90%: normal signal */
480 strength = 60 + (65000 + gain) * 3 / 2000;
482 /* 90% - 99%: strong signal */
483 strength = 90 + (45000 + gain) / 5000;
485 *_signal_strength = strength * 65535 / 100;
489 static struct dvb_tuner_ops ts2020_tuner_ops = {
492 .frequency_min = 950000,
493 .frequency_max = 2150000
496 .release = ts2020_release,
497 .sleep = ts2020_sleep,
498 .set_params = ts2020_set_params,
499 .get_frequency = ts2020_get_frequency,
500 .get_if_frequency = ts2020_get_if_frequency,
501 .get_rf_strength = ts2020_read_signal_strength,
504 struct dvb_frontend *ts2020_attach(struct dvb_frontend *fe,
505 const struct ts2020_config *config,
506 struct i2c_adapter *i2c)
508 struct i2c_client *client;
509 struct i2c_board_info board_info;
511 /* This is only used by ts2020_probe() so can be on the stack */
512 struct ts2020_config pdata;
514 memcpy(&pdata, config, sizeof(pdata));
516 pdata.attach_in_use = true;
518 memset(&board_info, 0, sizeof(board_info));
519 strlcpy(board_info.type, "ts2020", I2C_NAME_SIZE);
520 board_info.addr = config->tuner_address;
521 board_info.platform_data = &pdata;
522 client = i2c_new_device(i2c, &board_info);
523 if (!client || !client->dev.driver)
528 EXPORT_SYMBOL(ts2020_attach);
531 * We implement own regmap locking due to legacy DVB attach which uses frontend
532 * gate control callback to control I2C bus access. We can open / close gate and
533 * serialize whole open / I2C-operation / close sequence at the same.
535 static void ts2020_regmap_lock(void *__dev)
537 struct ts2020_priv *dev = __dev;
539 mutex_lock(&dev->regmap_mutex);
540 if (dev->fe->ops.i2c_gate_ctrl)
541 dev->fe->ops.i2c_gate_ctrl(dev->fe, 1);
544 static void ts2020_regmap_unlock(void *__dev)
546 struct ts2020_priv *dev = __dev;
548 if (dev->fe->ops.i2c_gate_ctrl)
549 dev->fe->ops.i2c_gate_ctrl(dev->fe, 0);
550 mutex_unlock(&dev->regmap_mutex);
553 static int ts2020_probe(struct i2c_client *client,
554 const struct i2c_device_id *id)
556 struct ts2020_config *pdata = client->dev.platform_data;
557 struct dvb_frontend *fe = pdata->fe;
558 struct ts2020_priv *dev;
564 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
571 mutex_init(&dev->regmap_mutex);
572 dev->regmap_config.reg_bits = 8,
573 dev->regmap_config.val_bits = 8,
574 dev->regmap_config.lock = ts2020_regmap_lock,
575 dev->regmap_config.unlock = ts2020_regmap_unlock,
576 dev->regmap_config.lock_arg = dev,
577 dev->regmap = regmap_init_i2c(client, &dev->regmap_config);
578 if (IS_ERR(dev->regmap)) {
579 ret = PTR_ERR(dev->regmap);
583 dev->i2c = client->adapter;
584 dev->i2c_address = client->addr;
585 dev->clk_out = pdata->clk_out;
586 dev->clk_out_div = pdata->clk_out_div;
587 dev->frequency_div = pdata->frequency_div;
589 dev->get_agc_pwm = pdata->get_agc_pwm;
590 fe->tuner_priv = dev;
591 dev->client = client;
592 INIT_DELAYED_WORK(&dev->stat_work, ts2020_stat_work);
594 /* check if the tuner is there */
595 ret = regmap_read(dev->regmap, 0x00, &utmp);
597 goto err_regmap_exit;
599 if ((utmp & 0x03) == 0x00) {
600 ret = regmap_write(dev->regmap, 0x00, 0x01);
602 goto err_regmap_exit;
604 usleep_range(2000, 50000);
607 ret = regmap_write(dev->regmap, 0x00, 0x03);
609 goto err_regmap_exit;
611 usleep_range(2000, 50000);
613 ret = regmap_read(dev->regmap, 0x00, &utmp);
615 goto err_regmap_exit;
617 dev_dbg(&client->dev, "chip_id=%02x\n", utmp);
623 dev->tuner = TS2020_M88TS2020;
625 if (!dev->frequency_div)
626 dev->frequency_div = 1060000;
630 dev->tuner = TS2020_M88TS2022;
632 if (!dev->frequency_div)
633 dev->frequency_div = 1103000;
637 goto err_regmap_exit;
640 if (dev->tuner == TS2020_M88TS2022) {
641 switch (dev->clk_out) {
642 case TS2020_CLK_OUT_DISABLED:
645 case TS2020_CLK_OUT_ENABLED:
647 ret = regmap_write(dev->regmap, 0x05, dev->clk_out_div);
649 goto err_regmap_exit;
651 case TS2020_CLK_OUT_ENABLED_XTALOUT:
656 goto err_regmap_exit;
659 ret = regmap_write(dev->regmap, 0x42, u8tmp);
661 goto err_regmap_exit;
663 if (dev->loop_through)
668 ret = regmap_write(dev->regmap, 0x62, u8tmp);
670 goto err_regmap_exit;
674 ret = regmap_write(dev->regmap, 0x00, 0x00);
676 goto err_regmap_exit;
678 dev_info(&client->dev,
679 "Montage Technology %s successfully identified\n", chip_str);
681 memcpy(&fe->ops.tuner_ops, &ts2020_tuner_ops,
682 sizeof(struct dvb_tuner_ops));
683 if (!pdata->attach_in_use)
684 fe->ops.tuner_ops.release = NULL;
686 i2c_set_clientdata(client, dev);
689 regmap_exit(dev->regmap);
693 dev_dbg(&client->dev, "failed=%d\n", ret);
697 static int ts2020_remove(struct i2c_client *client)
699 struct ts2020_priv *dev = i2c_get_clientdata(client);
701 dev_dbg(&client->dev, "\n");
703 regmap_exit(dev->regmap);
708 static const struct i2c_device_id ts2020_id_table[] = {
713 MODULE_DEVICE_TABLE(i2c, ts2020_id_table);
715 static struct i2c_driver ts2020_driver = {
717 .owner = THIS_MODULE,
720 .probe = ts2020_probe,
721 .remove = ts2020_remove,
722 .id_table = ts2020_id_table,
725 module_i2c_driver(ts2020_driver);
727 MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
728 MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
729 MODULE_LICENSE("GPL");