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"
25 #define TS2020_XTAL_FREQ 27000 /* in kHz */
26 #define FREQ_OFFSET_LOW_SYM_RATE 3000
29 struct dvb_frontend *fe;
32 struct i2c_adapter *i2c;
37 #define TS2020_M88TS2020 0
38 #define TS2020_M88TS2022 1
43 struct ts2020_reg_val {
48 static int ts2020_release(struct dvb_frontend *fe)
50 kfree(fe->tuner_priv);
51 fe->tuner_priv = NULL;
55 static int ts2020_writereg(struct dvb_frontend *fe, int reg, int data)
57 struct ts2020_priv *priv = fe->tuner_priv;
58 u8 buf[] = { reg, data };
59 struct i2c_msg msg[] = {
61 .addr = priv->i2c_address,
69 if (fe->ops.i2c_gate_ctrl)
70 fe->ops.i2c_gate_ctrl(fe, 1);
72 err = i2c_transfer(priv->i2c, msg, 1);
75 "%s: writereg error(err == %i, reg == 0x%02x, value == 0x%02x)\n",
76 __func__, err, reg, data);
80 if (fe->ops.i2c_gate_ctrl)
81 fe->ops.i2c_gate_ctrl(fe, 0);
86 static int ts2020_readreg(struct dvb_frontend *fe, u8 reg)
88 struct ts2020_priv *priv = fe->tuner_priv;
92 struct i2c_msg msg[] = {
94 .addr = priv->i2c_address,
99 .addr = priv->i2c_address,
106 if (fe->ops.i2c_gate_ctrl)
107 fe->ops.i2c_gate_ctrl(fe, 1);
109 ret = i2c_transfer(priv->i2c, msg, 2);
112 printk(KERN_ERR "%s: reg=0x%x(error=%d)\n",
117 if (fe->ops.i2c_gate_ctrl)
118 fe->ops.i2c_gate_ctrl(fe, 0);
123 static int ts2020_sleep(struct dvb_frontend *fe)
125 struct ts2020_priv *priv = fe->tuner_priv;
128 if (priv->tuner == TS2020_M88TS2020)
129 u8tmp = 0x0a; /* XXX: probably wrong */
133 return ts2020_writereg(fe, u8tmp, 0x00);
136 static int ts2020_init(struct dvb_frontend *fe)
138 struct ts2020_priv *priv = fe->tuner_priv;
142 if (priv->tuner == TS2020_M88TS2020) {
143 ts2020_writereg(fe, 0x42, 0x73);
144 ts2020_writereg(fe, 0x05, priv->clk_out_div);
145 ts2020_writereg(fe, 0x20, 0x27);
146 ts2020_writereg(fe, 0x07, 0x02);
147 ts2020_writereg(fe, 0x11, 0xff);
148 ts2020_writereg(fe, 0x60, 0xf9);
149 ts2020_writereg(fe, 0x08, 0x01);
150 ts2020_writereg(fe, 0x00, 0x41);
152 static const struct ts2020_reg_val reg_vals[] = {
166 ts2020_writereg(fe, 0x00, 0x01);
167 ts2020_writereg(fe, 0x00, 0x03);
169 switch (priv->clk_out) {
170 case TS2020_CLK_OUT_DISABLED:
173 case TS2020_CLK_OUT_ENABLED:
175 ts2020_writereg(fe, 0x05, priv->clk_out_div);
177 case TS2020_CLK_OUT_ENABLED_XTALOUT:
185 ts2020_writereg(fe, 0x42, u8tmp);
187 if (priv->loop_through)
192 ts2020_writereg(fe, 0x62, u8tmp);
194 for (i = 0; i < ARRAY_SIZE(reg_vals); i++)
195 ts2020_writereg(fe, reg_vals[i].reg, reg_vals[i].val);
201 static int ts2020_tuner_gate_ctrl(struct dvb_frontend *fe, u8 offset)
204 ret = ts2020_writereg(fe, 0x51, 0x1f - offset);
205 ret |= ts2020_writereg(fe, 0x51, 0x1f);
206 ret |= ts2020_writereg(fe, 0x50, offset);
207 ret |= ts2020_writereg(fe, 0x50, 0x00);
212 static int ts2020_set_tuner_rf(struct dvb_frontend *fe)
216 reg = ts2020_readreg(fe, 0x3d);
220 else if (reg == 0x16)
225 ts2020_writereg(fe, 0x60, reg);
226 reg = ts2020_tuner_gate_ctrl(fe, 0x08);
231 static int ts2020_set_params(struct dvb_frontend *fe)
233 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
234 struct ts2020_priv *priv = fe->tuner_priv;
236 u32 frequency = c->frequency;
238 u32 symbol_rate = (c->symbol_rate / 1000);
240 u16 value, ndiv, lpf_coeff;
241 u8 lpf_mxdiv, mlpf_max, mlpf_min, nlpf;
242 u8 lo = 0x01, div4 = 0x0;
244 /* Calculate frequency divider */
245 if (frequency < priv->frequency_div) {
248 ndiv = (frequency * 14 * 4) / TS2020_XTAL_FREQ;
250 ndiv = (frequency * 14 * 2) / TS2020_XTAL_FREQ;
251 ndiv = ndiv + ndiv % 2;
254 if (priv->tuner == TS2020_M88TS2020) {
256 ret = ts2020_writereg(fe, 0x10, 0x80 | lo);
259 ret = ts2020_writereg(fe, 0x10, 0x0b);
260 ret |= ts2020_writereg(fe, 0x11, 0x40);
263 /* Set frequency divider */
264 ret |= ts2020_writereg(fe, 0x01, (ndiv >> 8) & 0xf);
265 ret |= ts2020_writereg(fe, 0x02, ndiv & 0xff);
267 ret |= ts2020_writereg(fe, 0x03, 0x06);
268 ret |= ts2020_tuner_gate_ctrl(fe, 0x10);
272 /* Tuner Frequency Range */
273 ret = ts2020_writereg(fe, 0x10, lo);
275 ret |= ts2020_tuner_gate_ctrl(fe, 0x08);
278 if (priv->tuner == TS2020_M88TS2020)
279 ret |= ts2020_set_tuner_rf(fe);
281 gdiv28 = (TS2020_XTAL_FREQ / 1000 * 1694 + 500) / 1000;
282 ret |= ts2020_writereg(fe, 0x04, gdiv28 & 0xff);
283 ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
287 if (priv->tuner == TS2020_M88TS2022) {
288 ret = ts2020_writereg(fe, 0x25, 0x00);
289 ret |= ts2020_writereg(fe, 0x27, 0x70);
290 ret |= ts2020_writereg(fe, 0x41, 0x09);
291 ret |= ts2020_writereg(fe, 0x08, 0x0b);
296 value = ts2020_readreg(fe, 0x26);
298 f3db = (symbol_rate * 135) / 200 + 2000;
299 f3db += FREQ_OFFSET_LOW_SYM_RATE;
305 gdiv28 = gdiv28 * 207 / (value * 2 + 151);
306 mlpf_max = gdiv28 * 135 / 100;
307 mlpf_min = gdiv28 * 78 / 100;
311 nlpf = (f3db * gdiv28 * 2 / lpf_coeff /
312 (TS2020_XTAL_FREQ / 1000) + 1) / 2;
318 lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
319 * lpf_coeff * 2 / f3db + 1) / 2;
321 if (lpf_mxdiv < mlpf_min) {
323 lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
324 * lpf_coeff * 2 / f3db + 1) / 2;
327 if (lpf_mxdiv > mlpf_max)
328 lpf_mxdiv = mlpf_max;
330 ret = ts2020_writereg(fe, 0x04, lpf_mxdiv);
331 ret |= ts2020_writereg(fe, 0x06, nlpf);
333 ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
335 ret |= ts2020_tuner_gate_ctrl(fe, 0x01);
338 /* calculate offset assuming 96000kHz*/
339 offset_khz = (ndiv - ndiv % 2 + 1024) * TS2020_XTAL_FREQ
340 / (6 + 8) / (div4 + 1) / 2;
342 priv->frequency = offset_khz;
344 return (ret < 0) ? -EINVAL : 0;
347 static int ts2020_get_frequency(struct dvb_frontend *fe, u32 *frequency)
349 struct ts2020_priv *priv = fe->tuner_priv;
350 *frequency = priv->frequency;
355 static int ts2020_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
357 *frequency = 0; /* Zero-IF */
361 /* read TS2020 signal strength */
362 static int ts2020_read_signal_strength(struct dvb_frontend *fe,
363 u16 *signal_strength)
365 u16 sig_reading, sig_strength;
368 rfgain = ts2020_readreg(fe, 0x3d) & 0x1f;
369 bbgain = ts2020_readreg(fe, 0x21) & 0x1f;
376 sig_reading = rfgain * 2 + bbgain * 3;
378 sig_strength = 40 + (64 - sig_reading) * 50 / 64 ;
380 /* cook the value to be suitable for szap-s2 human readable output */
381 *signal_strength = sig_strength * 1000;
386 static struct dvb_tuner_ops ts2020_tuner_ops = {
389 .frequency_min = 950000,
390 .frequency_max = 2150000
393 .release = ts2020_release,
394 .sleep = ts2020_sleep,
395 .set_params = ts2020_set_params,
396 .get_frequency = ts2020_get_frequency,
397 .get_if_frequency = ts2020_get_if_frequency,
398 .get_rf_strength = ts2020_read_signal_strength,
401 struct dvb_frontend *ts2020_attach(struct dvb_frontend *fe,
402 const struct ts2020_config *config,
403 struct i2c_adapter *i2c)
405 struct ts2020_priv *priv = NULL;
408 priv = kzalloc(sizeof(struct ts2020_priv), GFP_KERNEL);
412 priv->i2c_address = config->tuner_address;
414 priv->clk_out = config->clk_out;
415 priv->clk_out_div = config->clk_out_div;
416 priv->frequency_div = config->frequency_div;
418 fe->tuner_priv = priv;
420 if (!priv->frequency_div)
421 priv->frequency_div = 1060000;
423 /* Wake Up the tuner */
424 if ((0x03 & ts2020_readreg(fe, 0x00)) == 0x00) {
425 ts2020_writereg(fe, 0x00, 0x01);
429 ts2020_writereg(fe, 0x00, 0x03);
432 /* Check the tuner version */
433 buf = ts2020_readreg(fe, 0x00);
434 if ((buf == 0x01) || (buf == 0x41) || (buf == 0x81)) {
435 printk(KERN_INFO "%s: Find tuner TS2020!\n", __func__);
436 priv->tuner = TS2020_M88TS2020;
437 } else if ((buf == 0x83) || (buf == 0xc3)) {
438 printk(KERN_INFO "%s: Find tuner TS2022!\n", __func__);
439 priv->tuner = TS2020_M88TS2022;
441 printk(KERN_ERR "%s: Read tuner reg[0] = %d\n", __func__, buf);
446 memcpy(&fe->ops.tuner_ops, &ts2020_tuner_ops,
447 sizeof(struct dvb_tuner_ops));
451 EXPORT_SYMBOL(ts2020_attach);
453 static int ts2020_probe(struct i2c_client *client,
454 const struct i2c_device_id *id)
456 struct ts2020_config *pdata = client->dev.platform_data;
457 struct dvb_frontend *fe = pdata->fe;
458 struct ts2020_priv *dev;
464 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
470 dev->i2c = client->adapter;
471 dev->i2c_address = client->addr;
472 dev->clk_out = pdata->clk_out;
473 dev->clk_out_div = pdata->clk_out_div;
474 dev->frequency_div = pdata->frequency_div;
476 fe->tuner_priv = dev;
478 /* check if the tuner is there */
479 ret = ts2020_readreg(fe, 0x00);
484 if ((utmp & 0x03) == 0x00) {
485 ret = ts2020_writereg(fe, 0x00, 0x01);
489 usleep_range(2000, 50000);
492 ret = ts2020_writereg(fe, 0x00, 0x03);
496 usleep_range(2000, 50000);
498 ret = ts2020_readreg(fe, 0x00);
503 dev_dbg(&client->dev, "chip_id=%02x\n", utmp);
509 dev->tuner = TS2020_M88TS2020;
511 if (!dev->frequency_div)
512 dev->frequency_div = 1060000;
516 dev->tuner = TS2020_M88TS2022;
518 if (!dev->frequency_div)
519 dev->frequency_div = 1103000;
526 if (dev->tuner == TS2020_M88TS2022) {
527 switch (dev->clk_out) {
528 case TS2020_CLK_OUT_DISABLED:
531 case TS2020_CLK_OUT_ENABLED:
533 ret = ts2020_writereg(fe, 0x05, dev->clk_out_div);
537 case TS2020_CLK_OUT_ENABLED_XTALOUT:
545 ret = ts2020_writereg(fe, 0x42, u8tmp);
549 if (dev->loop_through)
554 ret = ts2020_writereg(fe, 0x62, u8tmp);
560 ret = ts2020_writereg(fe, 0x00, 0x00);
564 dev_info(&client->dev,
565 "Montage Technology %s successfully identified\n", chip_str);
567 memcpy(&fe->ops.tuner_ops, &ts2020_tuner_ops,
568 sizeof(struct dvb_tuner_ops));
569 fe->ops.tuner_ops.release = NULL;
571 i2c_set_clientdata(client, dev);
574 dev_dbg(&client->dev, "failed=%d\n", ret);
579 static int ts2020_remove(struct i2c_client *client)
581 struct ts2020_priv *dev = i2c_get_clientdata(client);
582 struct dvb_frontend *fe = dev->fe;
584 dev_dbg(&client->dev, "\n");
586 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
587 fe->tuner_priv = NULL;
593 static const struct i2c_device_id ts2020_id_table[] = {
598 MODULE_DEVICE_TABLE(i2c, ts2020_id_table);
600 static struct i2c_driver ts2020_driver = {
602 .owner = THIS_MODULE,
605 .probe = ts2020_probe,
606 .remove = ts2020_remove,
607 .id_table = ts2020_id_table,
610 module_i2c_driver(ts2020_driver);
612 MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
613 MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
614 MODULE_LICENSE("GPL");