1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Montage Technology TS2020 - Silicon Tuner driver
4 Copyright (C) 2009-2012 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
6 Copyright (C) 2009-2012 TurboSight.com
10 #include <media/dvb_frontend.h>
12 #include <linux/regmap.h>
13 #include <linux/math64.h>
15 #define TS2020_XTAL_FREQ 27000 /* in kHz */
16 #define FREQ_OFFSET_LOW_SYM_RATE 3000
19 struct i2c_client
*client
;
20 struct mutex regmap_mutex
;
21 struct regmap_config regmap_config
;
22 struct regmap
*regmap
;
23 struct dvb_frontend
*fe
;
24 struct delayed_work stat_work
;
25 int (*get_agc_pwm
)(struct dvb_frontend
*fe
, u8
*_agc_pwm
);
27 struct i2c_adapter
*i2c
;
33 u32 frequency_div
; /* LO output divider switch frequency */
34 u32 frequency_khz
; /* actual used LO frequency */
35 #define TS2020_M88TS2020 0
36 #define TS2020_M88TS2022 1
40 struct ts2020_reg_val
{
45 static void ts2020_stat_work(struct work_struct
*work
);
47 static void ts2020_release(struct dvb_frontend
*fe
)
49 struct ts2020_priv
*priv
= fe
->tuner_priv
;
50 struct i2c_client
*client
= priv
->client
;
52 dev_dbg(&client
->dev
, "\n");
54 i2c_unregister_device(client
);
57 static int ts2020_sleep(struct dvb_frontend
*fe
)
59 struct ts2020_priv
*priv
= fe
->tuner_priv
;
63 if (priv
->tuner
== TS2020_M88TS2020
)
64 u8tmp
= 0x0a; /* XXX: probably wrong */
68 ret
= regmap_write(priv
->regmap
, u8tmp
, 0x00);
72 /* stop statistics polling */
74 cancel_delayed_work_sync(&priv
->stat_work
);
78 static int ts2020_init(struct dvb_frontend
*fe
)
80 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
81 struct ts2020_priv
*priv
= fe
->tuner_priv
;
85 if (priv
->tuner
== TS2020_M88TS2020
) {
86 regmap_write(priv
->regmap
, 0x42, 0x73);
87 regmap_write(priv
->regmap
, 0x05, priv
->clk_out_div
);
88 regmap_write(priv
->regmap
, 0x20, 0x27);
89 regmap_write(priv
->regmap
, 0x07, 0x02);
90 regmap_write(priv
->regmap
, 0x11, 0xff);
91 regmap_write(priv
->regmap
, 0x60, 0xf9);
92 regmap_write(priv
->regmap
, 0x08, 0x01);
93 regmap_write(priv
->regmap
, 0x00, 0x41);
95 static const struct ts2020_reg_val reg_vals
[] = {
109 regmap_write(priv
->regmap
, 0x00, 0x01);
110 regmap_write(priv
->regmap
, 0x00, 0x03);
112 switch (priv
->clk_out
) {
113 case TS2020_CLK_OUT_DISABLED
:
116 case TS2020_CLK_OUT_ENABLED
:
118 regmap_write(priv
->regmap
, 0x05, priv
->clk_out_div
);
120 case TS2020_CLK_OUT_ENABLED_XTALOUT
:
128 regmap_write(priv
->regmap
, 0x42, u8tmp
);
130 if (priv
->loop_through
)
135 regmap_write(priv
->regmap
, 0x62, u8tmp
);
137 for (i
= 0; i
< ARRAY_SIZE(reg_vals
); i
++)
138 regmap_write(priv
->regmap
, reg_vals
[i
].reg
,
142 /* Initialise v5 stats here */
144 c
->strength
.stat
[0].scale
= FE_SCALE_DECIBEL
;
145 c
->strength
.stat
[0].uvalue
= 0;
147 /* Start statistics polling by invoking the work function */
148 ts2020_stat_work(&priv
->stat_work
.work
);
152 static int ts2020_tuner_gate_ctrl(struct dvb_frontend
*fe
, u8 offset
)
154 struct ts2020_priv
*priv
= fe
->tuner_priv
;
156 ret
= regmap_write(priv
->regmap
, 0x51, 0x1f - offset
);
157 ret
|= regmap_write(priv
->regmap
, 0x51, 0x1f);
158 ret
|= regmap_write(priv
->regmap
, 0x50, offset
);
159 ret
|= regmap_write(priv
->regmap
, 0x50, 0x00);
164 static int ts2020_set_tuner_rf(struct dvb_frontend
*fe
)
166 struct ts2020_priv
*dev
= fe
->tuner_priv
;
170 ret
= regmap_read(dev
->regmap
, 0x3d, &utmp
);
177 else if (utmp
== 0x16)
182 regmap_write(dev
->regmap
, 0x60, utmp
);
183 ret
= ts2020_tuner_gate_ctrl(fe
, 0x08);
188 static int ts2020_set_params(struct dvb_frontend
*fe
)
190 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
191 struct ts2020_priv
*priv
= fe
->tuner_priv
;
195 u16 u16tmp
, value
, lpf_coeff
;
196 u8 buf
[3], reg10
, lpf_mxdiv
, mlpf_max
, mlpf_min
, nlpf
;
197 unsigned int f_ref_khz
, f_vco_khz
, div_ref
, div_out
, pll_n
;
198 unsigned int frequency_khz
= c
->frequency
;
201 * Integer-N PLL synthesizer
202 * kHz is used for all calculations to keep calculations within 32-bit
204 f_ref_khz
= TS2020_XTAL_FREQ
;
205 div_ref
= DIV_ROUND_CLOSEST(f_ref_khz
, 2000);
207 /* select LO output divider */
208 if (frequency_khz
< priv
->frequency_div
) {
216 f_vco_khz
= frequency_khz
* div_out
;
217 pll_n
= f_vco_khz
* div_ref
/ f_ref_khz
;
219 priv
->frequency_khz
= pll_n
* f_ref_khz
/ div_ref
/ div_out
;
221 pr_debug("frequency=%u offset=%d f_vco_khz=%u pll_n=%u div_ref=%u div_out=%u\n",
222 priv
->frequency_khz
, priv
->frequency_khz
- c
->frequency
,
223 f_vco_khz
, pll_n
, div_ref
, div_out
);
225 if (priv
->tuner
== TS2020_M88TS2020
) {
228 ret
= regmap_write(priv
->regmap
, 0x10, reg10
);
232 ret
= regmap_write(priv
->regmap
, 0x10, reg10
);
233 ret
|= regmap_write(priv
->regmap
, 0x11, 0x40);
236 u16tmp
= pll_n
- 1024;
237 buf
[0] = (u16tmp
>> 8) & 0xff;
238 buf
[1] = (u16tmp
>> 0) & 0xff;
239 buf
[2] = div_ref
- 8;
241 ret
|= regmap_write(priv
->regmap
, 0x01, buf
[0]);
242 ret
|= regmap_write(priv
->regmap
, 0x02, buf
[1]);
243 ret
|= regmap_write(priv
->regmap
, 0x03, buf
[2]);
245 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x10);
249 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x08);
252 if (priv
->tuner
== TS2020_M88TS2020
)
253 ret
|= ts2020_set_tuner_rf(fe
);
255 gdiv28
= (TS2020_XTAL_FREQ
/ 1000 * 1694 + 500) / 1000;
256 ret
|= regmap_write(priv
->regmap
, 0x04, gdiv28
& 0xff);
257 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x04);
261 if (priv
->tuner
== TS2020_M88TS2022
) {
262 ret
= regmap_write(priv
->regmap
, 0x25, 0x00);
263 ret
|= regmap_write(priv
->regmap
, 0x27, 0x70);
264 ret
|= regmap_write(priv
->regmap
, 0x41, 0x09);
265 ret
|= regmap_write(priv
->regmap
, 0x08, 0x0b);
270 regmap_read(priv
->regmap
, 0x26, &utmp
);
273 f3db
= (c
->bandwidth_hz
/ 1000 / 2) + 2000;
274 f3db
+= FREQ_OFFSET_LOW_SYM_RATE
; /* FIXME: ~always too wide filter */
275 f3db
= clamp(f3db
, 7000U, 40000U);
277 gdiv28
= gdiv28
* 207 / (value
* 2 + 151);
278 mlpf_max
= gdiv28
* 135 / 100;
279 mlpf_min
= gdiv28
* 78 / 100;
283 nlpf
= (f3db
* gdiv28
* 2 / lpf_coeff
/
284 (TS2020_XTAL_FREQ
/ 1000) + 1) / 2;
290 lpf_mxdiv
= (nlpf
* (TS2020_XTAL_FREQ
/ 1000)
291 * lpf_coeff
* 2 / f3db
+ 1) / 2;
293 if (lpf_mxdiv
< mlpf_min
) {
295 lpf_mxdiv
= (nlpf
* (TS2020_XTAL_FREQ
/ 1000)
296 * lpf_coeff
* 2 / f3db
+ 1) / 2;
299 if (lpf_mxdiv
> mlpf_max
)
300 lpf_mxdiv
= mlpf_max
;
302 ret
= regmap_write(priv
->regmap
, 0x04, lpf_mxdiv
);
303 ret
|= regmap_write(priv
->regmap
, 0x06, nlpf
);
305 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x04);
307 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x01);
311 return (ret
< 0) ? -EINVAL
: 0;
314 static int ts2020_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
316 struct ts2020_priv
*priv
= fe
->tuner_priv
;
318 *frequency
= priv
->frequency_khz
;
322 static int ts2020_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
324 *frequency
= 0; /* Zero-IF */
329 * Get the tuner gain.
330 * @fe: The front end for which we're determining the gain
331 * @v_agc: The voltage of the AGC from the demodulator (0-2600mV)
332 * @_gain: Where to store the gain (in 0.001dB units)
334 * Returns 0 or a negative error code.
336 static int ts2020_read_tuner_gain(struct dvb_frontend
*fe
, unsigned v_agc
,
339 struct ts2020_priv
*priv
= fe
->tuner_priv
;
340 unsigned long gain1
, gain2
, gain3
;
344 /* Read the RF gain */
345 ret
= regmap_read(priv
->regmap
, 0x3d, &utmp
);
350 /* Read the baseband gain */
351 ret
= regmap_read(priv
->regmap
, 0x21, &utmp
);
356 switch (priv
->tuner
) {
357 case TS2020_M88TS2020
:
358 gain1
= clamp_t(long, gain1
, 0, 15);
359 gain2
= clamp_t(long, gain2
, 0, 13);
360 v_agc
= clamp_t(long, v_agc
, 400, 1100);
362 *_gain
= -((__s64
)gain1
* 2330 +
364 v_agc
* 24 / 10 * 10 +
366 /* gain in range -19600 to -116850 in units of 0.001dB */
369 case TS2020_M88TS2022
:
370 ret
= regmap_read(priv
->regmap
, 0x66, &utmp
);
373 gain3
= (utmp
>> 3) & 0x07;
375 gain1
= clamp_t(long, gain1
, 0, 15);
376 gain2
= clamp_t(long, gain2
, 2, 16);
377 gain3
= clamp_t(long, gain3
, 0, 6);
378 v_agc
= clamp_t(long, v_agc
, 600, 1600);
380 *_gain
= -((__s64
)gain1
* 2650 +
383 v_agc
* 176 / 100 * 10 -
385 /* gain in range -47320 to -158950 in units of 0.001dB */
393 * Get the AGC information from the demodulator and use that to calculate the
396 static int ts2020_get_tuner_gain(struct dvb_frontend
*fe
, __s64
*_gain
)
398 struct ts2020_priv
*priv
= fe
->tuner_priv
;
402 /* Read the AGC PWM rate from the demodulator */
403 if (priv
->get_agc_pwm
) {
404 ret
= priv
->get_agc_pwm(fe
, &agc_pwm
);
408 switch (priv
->tuner
) {
409 case TS2020_M88TS2020
:
410 v_agc
= (int)agc_pwm
* 20 - 1166;
412 case TS2020_M88TS2022
:
413 v_agc
= (int)agc_pwm
* 16 - 670;
421 return ts2020_read_tuner_gain(fe
, v_agc
, _gain
);
425 * Gather statistics on a regular basis
427 static void ts2020_stat_work(struct work_struct
*work
)
429 struct ts2020_priv
*priv
= container_of(work
, struct ts2020_priv
,
431 struct i2c_client
*client
= priv
->client
;
432 struct dtv_frontend_properties
*c
= &priv
->fe
->dtv_property_cache
;
435 dev_dbg(&client
->dev
, "\n");
437 ret
= ts2020_get_tuner_gain(priv
->fe
, &c
->strength
.stat
[0].svalue
);
441 c
->strength
.stat
[0].scale
= FE_SCALE_DECIBEL
;
443 if (!priv
->dont_poll
)
444 schedule_delayed_work(&priv
->stat_work
, msecs_to_jiffies(2000));
447 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
451 * Read TS2020 signal strength in v3 format.
453 static int ts2020_read_signal_strength(struct dvb_frontend
*fe
,
454 u16
*_signal_strength
)
456 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
457 struct ts2020_priv
*priv
= fe
->tuner_priv
;
462 ts2020_stat_work(&priv
->stat_work
.work
);
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 + div64_s64((85000 + gain
) * 3, 1000);
478 else if (gain
< -45000)
479 /* 60% - 90%: normal signal */
480 strength
= 60 + div64_s64((65000 + gain
) * 3, 2000);
482 /* 90% - 99%: strong signal */
483 strength
= 90 + div64_s64((45000 + gain
), 5000);
485 *_signal_strength
= strength
* 65535 / 100;
489 static const struct dvb_tuner_ops ts2020_tuner_ops
= {
492 .frequency_min_hz
= 950 * MHz
,
493 .frequency_max_hz
= 2150 * MHz
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 strscpy(board_info
.type
, "ts2020", I2C_NAME_SIZE
);
520 board_info
.addr
= config
->tuner_address
;
521 board_info
.platform_data
= &pdata
;
522 client
= i2c_new_client_device(i2c
, &board_info
);
523 if (!i2c_client_has_driver(client
))
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
->loop_through
= pdata
->loop_through
;
586 dev
->clk_out
= pdata
->clk_out
;
587 dev
->clk_out_div
= pdata
->clk_out_div
;
588 dev
->dont_poll
= pdata
->dont_poll
;
589 dev
->frequency_div
= pdata
->frequency_div
;
591 dev
->get_agc_pwm
= pdata
->get_agc_pwm
;
592 fe
->tuner_priv
= dev
;
593 dev
->client
= client
;
594 INIT_DELAYED_WORK(&dev
->stat_work
, ts2020_stat_work
);
596 /* check if the tuner is there */
597 ret
= regmap_read(dev
->regmap
, 0x00, &utmp
);
599 goto err_regmap_exit
;
601 if ((utmp
& 0x03) == 0x00) {
602 ret
= regmap_write(dev
->regmap
, 0x00, 0x01);
604 goto err_regmap_exit
;
606 usleep_range(2000, 50000);
609 ret
= regmap_write(dev
->regmap
, 0x00, 0x03);
611 goto err_regmap_exit
;
613 usleep_range(2000, 50000);
615 ret
= regmap_read(dev
->regmap
, 0x00, &utmp
);
617 goto err_regmap_exit
;
619 dev_dbg(&client
->dev
, "chip_id=%02x\n", utmp
);
625 dev
->tuner
= TS2020_M88TS2020
;
627 if (!dev
->frequency_div
)
628 dev
->frequency_div
= 1060000;
632 dev
->tuner
= TS2020_M88TS2022
;
634 if (!dev
->frequency_div
)
635 dev
->frequency_div
= 1103000;
639 goto err_regmap_exit
;
642 if (dev
->tuner
== TS2020_M88TS2022
) {
643 switch (dev
->clk_out
) {
644 case TS2020_CLK_OUT_DISABLED
:
647 case TS2020_CLK_OUT_ENABLED
:
649 ret
= regmap_write(dev
->regmap
, 0x05, dev
->clk_out_div
);
651 goto err_regmap_exit
;
653 case TS2020_CLK_OUT_ENABLED_XTALOUT
:
658 goto err_regmap_exit
;
661 ret
= regmap_write(dev
->regmap
, 0x42, u8tmp
);
663 goto err_regmap_exit
;
665 if (dev
->loop_through
)
670 ret
= regmap_write(dev
->regmap
, 0x62, u8tmp
);
672 goto err_regmap_exit
;
676 ret
= regmap_write(dev
->regmap
, 0x00, 0x00);
678 goto err_regmap_exit
;
680 dev_info(&client
->dev
,
681 "Montage Technology %s successfully identified\n", chip_str
);
683 memcpy(&fe
->ops
.tuner_ops
, &ts2020_tuner_ops
,
684 sizeof(struct dvb_tuner_ops
));
685 if (!pdata
->attach_in_use
)
686 fe
->ops
.tuner_ops
.release
= NULL
;
688 i2c_set_clientdata(client
, dev
);
691 regmap_exit(dev
->regmap
);
695 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
699 static int ts2020_remove(struct i2c_client
*client
)
701 struct ts2020_priv
*dev
= i2c_get_clientdata(client
);
703 dev_dbg(&client
->dev
, "\n");
705 /* stop statistics polling */
707 cancel_delayed_work_sync(&dev
->stat_work
);
709 regmap_exit(dev
->regmap
);
714 static const struct i2c_device_id ts2020_id_table
[] = {
719 MODULE_DEVICE_TABLE(i2c
, ts2020_id_table
);
721 static struct i2c_driver ts2020_driver
= {
725 .probe
= ts2020_probe
,
726 .remove
= ts2020_remove
,
727 .id_table
= ts2020_id_table
,
730 module_i2c_driver(ts2020_driver
);
732 MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
733 MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
734 MODULE_LICENSE("GPL");