2 Montage Technology TS2020 - Silicon Tuner driver
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
31 struct i2c_adapter *i2c;
37 static int ts2020_release(struct dvb_frontend *fe)
39 kfree(fe->tuner_priv);
40 fe->tuner_priv = NULL;
44 static int ts2020_writereg(struct dvb_frontend *fe, int reg, int data)
46 struct ts2020_priv *priv = fe->tuner_priv;
47 u8 buf[] = { reg, data };
48 struct i2c_msg msg[] = {
50 .addr = priv->i2c_address,
58 if (fe->ops.i2c_gate_ctrl)
59 fe->ops.i2c_gate_ctrl(fe, 1);
61 err = i2c_transfer(priv->i2c, msg, 1);
64 "%s: writereg error(err == %i, reg == 0x%02x, value == 0x%02x)\n",
65 __func__, err, reg, data);
69 if (fe->ops.i2c_gate_ctrl)
70 fe->ops.i2c_gate_ctrl(fe, 0);
75 static int ts2020_readreg(struct dvb_frontend *fe, u8 reg)
77 struct ts2020_priv *priv = fe->tuner_priv;
81 struct i2c_msg msg[] = {
83 .addr = priv->i2c_address,
88 .addr = priv->i2c_address,
95 if (fe->ops.i2c_gate_ctrl)
96 fe->ops.i2c_gate_ctrl(fe, 1);
98 ret = i2c_transfer(priv->i2c, msg, 2);
101 printk(KERN_ERR "%s: reg=0x%x(error=%d)\n",
106 if (fe->ops.i2c_gate_ctrl)
107 fe->ops.i2c_gate_ctrl(fe, 0);
112 static int ts2020_sleep(struct dvb_frontend *fe)
114 struct ts2020_priv *priv = fe->tuner_priv;
116 u8 buf[] = { 10, 0 };
117 struct i2c_msg msg = {
118 .addr = priv->i2c_address,
124 if (fe->ops.i2c_gate_ctrl)
125 fe->ops.i2c_gate_ctrl(fe, 1);
127 ret = i2c_transfer(priv->i2c, &msg, 1);
129 printk(KERN_ERR "%s: i2c error\n", __func__);
131 if (fe->ops.i2c_gate_ctrl)
132 fe->ops.i2c_gate_ctrl(fe, 0);
134 return (ret == 1) ? 0 : ret;
137 static int ts2020_init(struct dvb_frontend *fe)
139 struct ts2020_priv *priv = fe->tuner_priv;
141 ts2020_writereg(fe, 0x42, 0x73);
142 ts2020_writereg(fe, 0x05, priv->clk_out_div);
143 ts2020_writereg(fe, 0x20, 0x27);
144 ts2020_writereg(fe, 0x07, 0x02);
145 ts2020_writereg(fe, 0x11, 0xff);
146 ts2020_writereg(fe, 0x60, 0xf9);
147 ts2020_writereg(fe, 0x08, 0x01);
148 ts2020_writereg(fe, 0x00, 0x41);
153 static int ts2020_tuner_gate_ctrl(struct dvb_frontend *fe, u8 offset)
156 ret = ts2020_writereg(fe, 0x51, 0x1f - offset);
157 ret |= ts2020_writereg(fe, 0x51, 0x1f);
158 ret |= ts2020_writereg(fe, 0x50, offset);
159 ret |= ts2020_writereg(fe, 0x50, 0x00);
164 static int ts2020_set_tuner_rf(struct dvb_frontend *fe)
168 reg = ts2020_readreg(fe, 0x3d);
172 else if (reg == 0x16)
177 ts2020_writereg(fe, 0x60, reg);
178 reg = ts2020_tuner_gate_ctrl(fe, 0x08);
183 static int ts2020_set_params(struct dvb_frontend *fe)
185 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
186 struct ts2020_priv *priv = fe->tuner_priv;
188 u32 frequency = c->frequency;
190 u32 symbol_rate = (c->symbol_rate / 1000);
192 u16 value, ndiv, lpf_coeff;
193 u8 lpf_mxdiv, mlpf_max, mlpf_min, nlpf;
194 u8 lo = 0x01, div4 = 0x0;
196 /* Calculate frequency divider */
197 if (frequency < priv->frequency_div) {
200 ndiv = (frequency * 14 * 4) / TS2020_XTAL_FREQ;
202 ndiv = (frequency * 14 * 2) / TS2020_XTAL_FREQ;
203 ndiv = ndiv + ndiv % 2;
206 ret = ts2020_writereg(fe, 0x10, 0x80 | lo);
208 /* Set frequency divider */
209 ret |= ts2020_writereg(fe, 0x01, (ndiv >> 8) & 0xf);
210 ret |= ts2020_writereg(fe, 0x02, ndiv & 0xff);
212 ret |= ts2020_writereg(fe, 0x03, 0x06);
213 ret |= ts2020_tuner_gate_ctrl(fe, 0x10);
217 /* Tuner Frequency Range */
218 ret = ts2020_writereg(fe, 0x10, lo);
220 ret |= ts2020_tuner_gate_ctrl(fe, 0x08);
223 ret |= ts2020_set_tuner_rf(fe);
225 gdiv28 = (TS2020_XTAL_FREQ / 1000 * 1694 + 500) / 1000;
226 ret |= ts2020_writereg(fe, 0x04, gdiv28 & 0xff);
227 ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
231 value = ts2020_readreg(fe, 0x26);
233 f3db = (symbol_rate * 135) / 200 + 2000;
234 f3db += FREQ_OFFSET_LOW_SYM_RATE;
240 gdiv28 = gdiv28 * 207 / (value * 2 + 151);
241 mlpf_max = gdiv28 * 135 / 100;
242 mlpf_min = gdiv28 * 78 / 100;
248 nlpf = (f3db * gdiv28 * 2 / lpf_coeff /
249 (TS2020_XTAL_FREQ / 1000) + 1) / 2;
255 lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
256 * lpf_coeff * 2 / f3db + 1) / 2;
258 if (lpf_mxdiv < mlpf_min) {
260 lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
261 * lpf_coeff * 2 / f3db + 1) / 2;
264 if (lpf_mxdiv > mlpf_max)
265 lpf_mxdiv = mlpf_max;
267 ret = ts2020_writereg(fe, 0x04, lpf_mxdiv);
268 ret |= ts2020_writereg(fe, 0x06, nlpf);
270 ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
272 ret |= ts2020_tuner_gate_ctrl(fe, 0x01);
275 /* calculate offset assuming 96000kHz*/
276 offset_khz = (ndiv - ndiv % 2 + 1024) * TS2020_XTAL_FREQ
277 / (6 + 8) / (div4 + 1) / 2;
279 priv->frequency = offset_khz;
281 return (ret < 0) ? -EINVAL : 0;
284 static int ts2020_get_frequency(struct dvb_frontend *fe, u32 *frequency)
286 struct ts2020_priv *priv = fe->tuner_priv;
287 *frequency = priv->frequency;
291 /* read TS2020 signal strength */
292 static int ts2020_read_signal_strength(struct dvb_frontend *fe,
293 u16 *signal_strength)
295 u16 sig_reading, sig_strength;
298 rfgain = ts2020_readreg(fe, 0x3d) & 0x1f;
299 bbgain = ts2020_readreg(fe, 0x21) & 0x1f;
306 sig_reading = rfgain * 2 + bbgain * 3;
308 sig_strength = 40 + (64 - sig_reading) * 50 / 64 ;
310 /* cook the value to be suitable for szap-s2 human readable output */
311 *signal_strength = sig_strength * 1000;
316 static struct dvb_tuner_ops ts2020_tuner_ops = {
319 .frequency_min = 950000,
320 .frequency_max = 2150000
323 .release = ts2020_release,
324 .sleep = ts2020_sleep,
325 .set_params = ts2020_set_params,
326 .get_frequency = ts2020_get_frequency,
327 .get_rf_strength = ts2020_read_signal_strength,
330 struct dvb_frontend *ts2020_attach(struct dvb_frontend *fe,
331 const struct ts2020_config *config,
332 struct i2c_adapter *i2c)
334 struct ts2020_priv *priv = NULL;
337 priv = kzalloc(sizeof(struct ts2020_priv), GFP_KERNEL);
341 priv->i2c_address = config->tuner_address;
343 priv->clk_out_div = config->clk_out_div;
344 priv->frequency_div = config->frequency_div;
345 fe->tuner_priv = priv;
347 if (!priv->frequency_div)
348 priv->frequency_div = 1060000;
350 /* Wake Up the tuner */
351 if ((0x03 & ts2020_readreg(fe, 0x00)) == 0x00) {
352 ts2020_writereg(fe, 0x00, 0x01);
356 ts2020_writereg(fe, 0x00, 0x03);
359 /* Check the tuner version */
360 buf = ts2020_readreg(fe, 0x00);
361 if ((buf == 0x01) || (buf == 0x41) || (buf == 0x81))
362 printk(KERN_INFO "%s: Find tuner TS2020!\n", __func__);
364 printk(KERN_ERR "%s: Read tuner reg[0] = %d\n", __func__, buf);
369 memcpy(&fe->ops.tuner_ops, &ts2020_tuner_ops,
370 sizeof(struct dvb_tuner_ops));
374 EXPORT_SYMBOL(ts2020_attach);
377 MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
378 MODULE_LICENSE("GPL");