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>
25 #include <linux/math64.h>
27 #define TS2020_XTAL_FREQ 27000 /* in kHz */
28 #define FREQ_OFFSET_LOW_SYM_RATE 3000
31 struct i2c_client
*client
;
32 struct mutex regmap_mutex
;
33 struct regmap_config regmap_config
;
34 struct regmap
*regmap
;
35 struct dvb_frontend
*fe
;
36 struct delayed_work stat_work
;
37 int (*get_agc_pwm
)(struct dvb_frontend
*fe
, u8
*_agc_pwm
);
39 struct i2c_adapter
*i2c
;
45 u32 frequency_div
; /* LO output divider switch frequency */
46 u32 frequency_khz
; /* actual used LO frequency */
47 #define TS2020_M88TS2020 0
48 #define TS2020_M88TS2022 1
52 struct ts2020_reg_val
{
57 static void ts2020_stat_work(struct work_struct
*work
);
59 static int ts2020_release(struct dvb_frontend
*fe
)
61 struct ts2020_priv
*priv
= fe
->tuner_priv
;
62 struct i2c_client
*client
= priv
->client
;
64 dev_dbg(&client
->dev
, "\n");
66 i2c_unregister_device(client
);
70 static int ts2020_sleep(struct dvb_frontend
*fe
)
72 struct ts2020_priv
*priv
= fe
->tuner_priv
;
76 if (priv
->tuner
== TS2020_M88TS2020
)
77 u8tmp
= 0x0a; /* XXX: probably wrong */
81 ret
= regmap_write(priv
->regmap
, u8tmp
, 0x00);
85 /* stop statistics polling */
87 cancel_delayed_work_sync(&priv
->stat_work
);
91 static int ts2020_init(struct dvb_frontend
*fe
)
93 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
94 struct ts2020_priv
*priv
= fe
->tuner_priv
;
98 if (priv
->tuner
== TS2020_M88TS2020
) {
99 regmap_write(priv
->regmap
, 0x42, 0x73);
100 regmap_write(priv
->regmap
, 0x05, priv
->clk_out_div
);
101 regmap_write(priv
->regmap
, 0x20, 0x27);
102 regmap_write(priv
->regmap
, 0x07, 0x02);
103 regmap_write(priv
->regmap
, 0x11, 0xff);
104 regmap_write(priv
->regmap
, 0x60, 0xf9);
105 regmap_write(priv
->regmap
, 0x08, 0x01);
106 regmap_write(priv
->regmap
, 0x00, 0x41);
108 static const struct ts2020_reg_val reg_vals
[] = {
122 regmap_write(priv
->regmap
, 0x00, 0x01);
123 regmap_write(priv
->regmap
, 0x00, 0x03);
125 switch (priv
->clk_out
) {
126 case TS2020_CLK_OUT_DISABLED
:
129 case TS2020_CLK_OUT_ENABLED
:
131 regmap_write(priv
->regmap
, 0x05, priv
->clk_out_div
);
133 case TS2020_CLK_OUT_ENABLED_XTALOUT
:
141 regmap_write(priv
->regmap
, 0x42, u8tmp
);
143 if (priv
->loop_through
)
148 regmap_write(priv
->regmap
, 0x62, u8tmp
);
150 for (i
= 0; i
< ARRAY_SIZE(reg_vals
); i
++)
151 regmap_write(priv
->regmap
, reg_vals
[i
].reg
,
155 /* Initialise v5 stats here */
157 c
->strength
.stat
[0].scale
= FE_SCALE_DECIBEL
;
158 c
->strength
.stat
[0].uvalue
= 0;
160 /* Start statistics polling by invoking the work function */
161 ts2020_stat_work(&priv
->stat_work
.work
);
165 static int ts2020_tuner_gate_ctrl(struct dvb_frontend
*fe
, u8 offset
)
167 struct ts2020_priv
*priv
= fe
->tuner_priv
;
169 ret
= regmap_write(priv
->regmap
, 0x51, 0x1f - offset
);
170 ret
|= regmap_write(priv
->regmap
, 0x51, 0x1f);
171 ret
|= regmap_write(priv
->regmap
, 0x50, offset
);
172 ret
|= regmap_write(priv
->regmap
, 0x50, 0x00);
177 static int ts2020_set_tuner_rf(struct dvb_frontend
*fe
)
179 struct ts2020_priv
*dev
= fe
->tuner_priv
;
183 ret
= regmap_read(dev
->regmap
, 0x3d, &utmp
);
187 else if (utmp
== 0x16)
192 regmap_write(dev
->regmap
, 0x60, utmp
);
193 ret
= ts2020_tuner_gate_ctrl(fe
, 0x08);
198 static int ts2020_set_params(struct dvb_frontend
*fe
)
200 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
201 struct ts2020_priv
*priv
= fe
->tuner_priv
;
205 u16 u16tmp
, value
, lpf_coeff
;
206 u8 buf
[3], reg10
, lpf_mxdiv
, mlpf_max
, mlpf_min
, nlpf
;
207 unsigned int f_ref_khz
, f_vco_khz
, div_ref
, div_out
, pll_n
;
208 unsigned int frequency_khz
= c
->frequency
;
211 * Integer-N PLL synthesizer
212 * kHz is used for all calculations to keep calculations within 32-bit
214 f_ref_khz
= TS2020_XTAL_FREQ
;
215 div_ref
= DIV_ROUND_CLOSEST(f_ref_khz
, 2000);
217 /* select LO output divider */
218 if (frequency_khz
< priv
->frequency_div
) {
226 f_vco_khz
= frequency_khz
* div_out
;
227 pll_n
= f_vco_khz
* div_ref
/ f_ref_khz
;
229 priv
->frequency_khz
= pll_n
* f_ref_khz
/ div_ref
/ div_out
;
231 pr_debug("frequency=%u offset=%d f_vco_khz=%u pll_n=%u div_ref=%u div_out=%u\n",
232 priv
->frequency_khz
, priv
->frequency_khz
- c
->frequency
,
233 f_vco_khz
, pll_n
, div_ref
, div_out
);
235 if (priv
->tuner
== TS2020_M88TS2020
) {
238 ret
= regmap_write(priv
->regmap
, 0x10, reg10
);
242 ret
= regmap_write(priv
->regmap
, 0x10, reg10
);
243 ret
|= regmap_write(priv
->regmap
, 0x11, 0x40);
246 u16tmp
= pll_n
- 1024;
247 buf
[0] = (u16tmp
>> 8) & 0xff;
248 buf
[1] = (u16tmp
>> 0) & 0xff;
249 buf
[2] = div_ref
- 8;
251 ret
|= regmap_write(priv
->regmap
, 0x01, buf
[0]);
252 ret
|= regmap_write(priv
->regmap
, 0x02, buf
[1]);
253 ret
|= regmap_write(priv
->regmap
, 0x03, buf
[2]);
255 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x10);
259 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x08);
262 if (priv
->tuner
== TS2020_M88TS2020
)
263 ret
|= ts2020_set_tuner_rf(fe
);
265 gdiv28
= (TS2020_XTAL_FREQ
/ 1000 * 1694 + 500) / 1000;
266 ret
|= regmap_write(priv
->regmap
, 0x04, gdiv28
& 0xff);
267 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x04);
271 if (priv
->tuner
== TS2020_M88TS2022
) {
272 ret
= regmap_write(priv
->regmap
, 0x25, 0x00);
273 ret
|= regmap_write(priv
->regmap
, 0x27, 0x70);
274 ret
|= regmap_write(priv
->regmap
, 0x41, 0x09);
275 ret
|= regmap_write(priv
->regmap
, 0x08, 0x0b);
280 regmap_read(priv
->regmap
, 0x26, &utmp
);
283 f3db
= (c
->bandwidth_hz
/ 1000 / 2) + 2000;
284 f3db
+= FREQ_OFFSET_LOW_SYM_RATE
; /* FIXME: ~always too wide filter */
285 f3db
= clamp(f3db
, 7000U, 40000U);
287 gdiv28
= gdiv28
* 207 / (value
* 2 + 151);
288 mlpf_max
= gdiv28
* 135 / 100;
289 mlpf_min
= gdiv28
* 78 / 100;
293 nlpf
= (f3db
* gdiv28
* 2 / lpf_coeff
/
294 (TS2020_XTAL_FREQ
/ 1000) + 1) / 2;
300 lpf_mxdiv
= (nlpf
* (TS2020_XTAL_FREQ
/ 1000)
301 * lpf_coeff
* 2 / f3db
+ 1) / 2;
303 if (lpf_mxdiv
< mlpf_min
) {
305 lpf_mxdiv
= (nlpf
* (TS2020_XTAL_FREQ
/ 1000)
306 * lpf_coeff
* 2 / f3db
+ 1) / 2;
309 if (lpf_mxdiv
> mlpf_max
)
310 lpf_mxdiv
= mlpf_max
;
312 ret
= regmap_write(priv
->regmap
, 0x04, lpf_mxdiv
);
313 ret
|= regmap_write(priv
->regmap
, 0x06, nlpf
);
315 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x04);
317 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x01);
321 return (ret
< 0) ? -EINVAL
: 0;
324 static int ts2020_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
326 struct ts2020_priv
*priv
= fe
->tuner_priv
;
328 *frequency
= priv
->frequency_khz
;
332 static int ts2020_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
334 *frequency
= 0; /* Zero-IF */
339 * Get the tuner gain.
340 * @fe: The front end for which we're determining the gain
341 * @v_agc: The voltage of the AGC from the demodulator (0-2600mV)
342 * @_gain: Where to store the gain (in 0.001dB units)
344 * Returns 0 or a negative error code.
346 static int ts2020_read_tuner_gain(struct dvb_frontend
*fe
, unsigned v_agc
,
349 struct ts2020_priv
*priv
= fe
->tuner_priv
;
350 unsigned long gain1
, gain2
, gain3
;
354 /* Read the RF gain */
355 ret
= regmap_read(priv
->regmap
, 0x3d, &utmp
);
360 /* Read the baseband gain */
361 ret
= regmap_read(priv
->regmap
, 0x21, &utmp
);
366 switch (priv
->tuner
) {
367 case TS2020_M88TS2020
:
368 gain1
= clamp_t(long, gain1
, 0, 15);
369 gain2
= clamp_t(long, gain2
, 0, 13);
370 v_agc
= clamp_t(long, v_agc
, 400, 1100);
372 *_gain
= -(gain1
* 2330 +
374 v_agc
* 24 / 10 * 10 +
376 /* gain in range -19600 to -116850 in units of 0.001dB */
379 case TS2020_M88TS2022
:
380 ret
= regmap_read(priv
->regmap
, 0x66, &utmp
);
383 gain3
= (utmp
>> 3) & 0x07;
385 gain1
= clamp_t(long, gain1
, 0, 15);
386 gain2
= clamp_t(long, gain2
, 2, 16);
387 gain3
= clamp_t(long, gain3
, 0, 6);
388 v_agc
= clamp_t(long, v_agc
, 600, 1600);
390 *_gain
= -(gain1
* 2650 +
393 v_agc
* 176 / 100 * 10 -
395 /* gain in range -47320 to -158950 in units of 0.001dB */
403 * Get the AGC information from the demodulator and use that to calculate the
406 static int ts2020_get_tuner_gain(struct dvb_frontend
*fe
, __s64
*_gain
)
408 struct ts2020_priv
*priv
= fe
->tuner_priv
;
412 /* Read the AGC PWM rate from the demodulator */
413 if (priv
->get_agc_pwm
) {
414 ret
= priv
->get_agc_pwm(fe
, &agc_pwm
);
418 switch (priv
->tuner
) {
419 case TS2020_M88TS2020
:
420 v_agc
= (int)agc_pwm
* 20 - 1166;
422 case TS2020_M88TS2022
:
423 v_agc
= (int)agc_pwm
* 16 - 670;
431 return ts2020_read_tuner_gain(fe
, v_agc
, _gain
);
435 * Gather statistics on a regular basis
437 static void ts2020_stat_work(struct work_struct
*work
)
439 struct ts2020_priv
*priv
= container_of(work
, struct ts2020_priv
,
441 struct i2c_client
*client
= priv
->client
;
442 struct dtv_frontend_properties
*c
= &priv
->fe
->dtv_property_cache
;
445 dev_dbg(&client
->dev
, "\n");
447 ret
= ts2020_get_tuner_gain(priv
->fe
, &c
->strength
.stat
[0].svalue
);
451 c
->strength
.stat
[0].scale
= FE_SCALE_DECIBEL
;
453 if (!priv
->dont_poll
)
454 schedule_delayed_work(&priv
->stat_work
, msecs_to_jiffies(2000));
457 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
461 * Read TS2020 signal strength in v3 format.
463 static int ts2020_read_signal_strength(struct dvb_frontend
*fe
,
464 u16
*_signal_strength
)
466 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
467 struct ts2020_priv
*priv
= fe
->tuner_priv
;
472 ts2020_stat_work(&priv
->stat_work
.work
);
474 if (c
->strength
.stat
[0].scale
== FE_SCALE_NOT_AVAILABLE
) {
475 *_signal_strength
= 0;
479 gain
= c
->strength
.stat
[0].svalue
;
481 /* Calculate the signal strength based on the total gain of the tuner */
483 /* 0%: no signal or weak signal */
485 else if (gain
< -65000)
486 /* 0% - 60%: weak signal */
487 strength
= 0 + div64_s64((85000 + gain
) * 3, 1000);
488 else if (gain
< -45000)
489 /* 60% - 90%: normal signal */
490 strength
= 60 + div64_s64((65000 + gain
) * 3, 2000);
492 /* 90% - 99%: strong signal */
493 strength
= 90 + div64_s64((45000 + gain
), 5000);
495 *_signal_strength
= strength
* 65535 / 100;
499 static struct dvb_tuner_ops ts2020_tuner_ops
= {
502 .frequency_min
= 950000,
503 .frequency_max
= 2150000
506 .release
= ts2020_release
,
507 .sleep
= ts2020_sleep
,
508 .set_params
= ts2020_set_params
,
509 .get_frequency
= ts2020_get_frequency
,
510 .get_if_frequency
= ts2020_get_if_frequency
,
511 .get_rf_strength
= ts2020_read_signal_strength
,
514 struct dvb_frontend
*ts2020_attach(struct dvb_frontend
*fe
,
515 const struct ts2020_config
*config
,
516 struct i2c_adapter
*i2c
)
518 struct i2c_client
*client
;
519 struct i2c_board_info board_info
;
521 /* This is only used by ts2020_probe() so can be on the stack */
522 struct ts2020_config pdata
;
524 memcpy(&pdata
, config
, sizeof(pdata
));
526 pdata
.attach_in_use
= true;
528 memset(&board_info
, 0, sizeof(board_info
));
529 strlcpy(board_info
.type
, "ts2020", I2C_NAME_SIZE
);
530 board_info
.addr
= config
->tuner_address
;
531 board_info
.platform_data
= &pdata
;
532 client
= i2c_new_device(i2c
, &board_info
);
533 if (!client
|| !client
->dev
.driver
)
538 EXPORT_SYMBOL(ts2020_attach
);
541 * We implement own regmap locking due to legacy DVB attach which uses frontend
542 * gate control callback to control I2C bus access. We can open / close gate and
543 * serialize whole open / I2C-operation / close sequence at the same.
545 static void ts2020_regmap_lock(void *__dev
)
547 struct ts2020_priv
*dev
= __dev
;
549 mutex_lock(&dev
->regmap_mutex
);
550 if (dev
->fe
->ops
.i2c_gate_ctrl
)
551 dev
->fe
->ops
.i2c_gate_ctrl(dev
->fe
, 1);
554 static void ts2020_regmap_unlock(void *__dev
)
556 struct ts2020_priv
*dev
= __dev
;
558 if (dev
->fe
->ops
.i2c_gate_ctrl
)
559 dev
->fe
->ops
.i2c_gate_ctrl(dev
->fe
, 0);
560 mutex_unlock(&dev
->regmap_mutex
);
563 static int ts2020_probe(struct i2c_client
*client
,
564 const struct i2c_device_id
*id
)
566 struct ts2020_config
*pdata
= client
->dev
.platform_data
;
567 struct dvb_frontend
*fe
= pdata
->fe
;
568 struct ts2020_priv
*dev
;
574 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
581 mutex_init(&dev
->regmap_mutex
);
582 dev
->regmap_config
.reg_bits
= 8,
583 dev
->regmap_config
.val_bits
= 8,
584 dev
->regmap_config
.lock
= ts2020_regmap_lock
,
585 dev
->regmap_config
.unlock
= ts2020_regmap_unlock
,
586 dev
->regmap_config
.lock_arg
= dev
,
587 dev
->regmap
= regmap_init_i2c(client
, &dev
->regmap_config
);
588 if (IS_ERR(dev
->regmap
)) {
589 ret
= PTR_ERR(dev
->regmap
);
593 dev
->i2c
= client
->adapter
;
594 dev
->i2c_address
= client
->addr
;
595 dev
->loop_through
= pdata
->loop_through
;
596 dev
->clk_out
= pdata
->clk_out
;
597 dev
->clk_out_div
= pdata
->clk_out_div
;
598 dev
->dont_poll
= pdata
->dont_poll
;
599 dev
->frequency_div
= pdata
->frequency_div
;
601 dev
->get_agc_pwm
= pdata
->get_agc_pwm
;
602 fe
->tuner_priv
= dev
;
603 dev
->client
= client
;
604 INIT_DELAYED_WORK(&dev
->stat_work
, ts2020_stat_work
);
606 /* check if the tuner is there */
607 ret
= regmap_read(dev
->regmap
, 0x00, &utmp
);
609 goto err_regmap_exit
;
611 if ((utmp
& 0x03) == 0x00) {
612 ret
= regmap_write(dev
->regmap
, 0x00, 0x01);
614 goto err_regmap_exit
;
616 usleep_range(2000, 50000);
619 ret
= regmap_write(dev
->regmap
, 0x00, 0x03);
621 goto err_regmap_exit
;
623 usleep_range(2000, 50000);
625 ret
= regmap_read(dev
->regmap
, 0x00, &utmp
);
627 goto err_regmap_exit
;
629 dev_dbg(&client
->dev
, "chip_id=%02x\n", utmp
);
635 dev
->tuner
= TS2020_M88TS2020
;
637 if (!dev
->frequency_div
)
638 dev
->frequency_div
= 1060000;
642 dev
->tuner
= TS2020_M88TS2022
;
644 if (!dev
->frequency_div
)
645 dev
->frequency_div
= 1103000;
649 goto err_regmap_exit
;
652 if (dev
->tuner
== TS2020_M88TS2022
) {
653 switch (dev
->clk_out
) {
654 case TS2020_CLK_OUT_DISABLED
:
657 case TS2020_CLK_OUT_ENABLED
:
659 ret
= regmap_write(dev
->regmap
, 0x05, dev
->clk_out_div
);
661 goto err_regmap_exit
;
663 case TS2020_CLK_OUT_ENABLED_XTALOUT
:
668 goto err_regmap_exit
;
671 ret
= regmap_write(dev
->regmap
, 0x42, u8tmp
);
673 goto err_regmap_exit
;
675 if (dev
->loop_through
)
680 ret
= regmap_write(dev
->regmap
, 0x62, u8tmp
);
682 goto err_regmap_exit
;
686 ret
= regmap_write(dev
->regmap
, 0x00, 0x00);
688 goto err_regmap_exit
;
690 dev_info(&client
->dev
,
691 "Montage Technology %s successfully identified\n", chip_str
);
693 memcpy(&fe
->ops
.tuner_ops
, &ts2020_tuner_ops
,
694 sizeof(struct dvb_tuner_ops
));
695 if (!pdata
->attach_in_use
)
696 fe
->ops
.tuner_ops
.release
= NULL
;
698 i2c_set_clientdata(client
, dev
);
701 regmap_exit(dev
->regmap
);
705 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
709 static int ts2020_remove(struct i2c_client
*client
)
711 struct ts2020_priv
*dev
= i2c_get_clientdata(client
);
713 dev_dbg(&client
->dev
, "\n");
715 regmap_exit(dev
->regmap
);
720 static const struct i2c_device_id ts2020_id_table
[] = {
725 MODULE_DEVICE_TABLE(i2c
, ts2020_id_table
);
727 static struct i2c_driver ts2020_driver
= {
731 .probe
= ts2020_probe
,
732 .remove
= ts2020_remove
,
733 .id_table
= ts2020_id_table
,
736 module_i2c_driver(ts2020_driver
);
738 MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
739 MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
740 MODULE_LICENSE("GPL");