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 */
29 struct i2c_adapter
*i2c
;
32 static int ts2020_readreg(struct dvb_frontend
*fe
, u8 reg
)
34 struct ts2020_state
*state
= fe
->tuner_priv
;
39 struct i2c_msg msg
[] = {
41 .addr
= state
->tuner_address
,
46 .addr
= state
->tuner_address
,
53 if (fe
->ops
.i2c_gate_ctrl
)
54 fe
->ops
.i2c_gate_ctrl(fe
, 1);
56 ret
= i2c_transfer(state
->i2c
, msg
, 2);
58 if (fe
->ops
.i2c_gate_ctrl
)
59 fe
->ops
.i2c_gate_ctrl(fe
, 0);
62 printk(KERN_ERR
"%s: reg=0x%x(error=%d)\n", __func__
, reg
, ret
);
69 static int ts2020_writereg(struct dvb_frontend
*fe
, int reg
, int data
)
71 struct ts2020_state
*state
= fe
->tuner_priv
;
73 u8 buf
[] = { reg
, data
};
74 struct i2c_msg msg
= { .addr
= state
->tuner_address
,
75 .flags
= 0, .buf
= buf
, .len
= 2 };
79 if (fe
->ops
.i2c_gate_ctrl
)
80 fe
->ops
.i2c_gate_ctrl(fe
, 1);
82 err
= i2c_transfer(state
->i2c
, &msg
, 1);
84 if (fe
->ops
.i2c_gate_ctrl
)
85 fe
->ops
.i2c_gate_ctrl(fe
, 0);
88 printk(KERN_ERR
"%s: writereg error(err == %i, reg == 0x%02x,"
89 " value == 0x%02x)\n", __func__
, err
, reg
, data
);
96 static int ts2020_init(struct dvb_frontend
*fe
)
98 ts2020_writereg(fe
, 0x42, 0x73);
99 ts2020_writereg(fe
, 0x05, 0x01);
100 ts2020_writereg(fe
, 0x62, 0xf5);
104 static int ts2020_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
108 div4
= (ts2020_readreg(fe
, 0x10) & 0x10) >> 4;
110 ndiv
= ts2020_readreg(fe
, 0x01);
113 ndiv
|= ts2020_readreg(fe
, 0x02);
115 /* actual tuned frequency, i.e. including the offset */
116 *frequency
= (ndiv
- ndiv
% 2 + 1024) * TS2020_XTAL_FREQ
117 / (6 + 8) / (div4
+ 1) / 2;
122 static int ts2020_set_params(struct dvb_frontend
*fe
)
124 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
126 u8 mlpf
, mlpf_new
, mlpf_max
, mlpf_min
, nlpf
;
133 ts2020_writereg(fe
, 0x07, 0x02);
134 ts2020_writereg(fe
, 0x10, 0x00);
135 ts2020_writereg(fe
, 0x60, 0x79);
136 ts2020_writereg(fe
, 0x08, 0x01);
137 ts2020_writereg(fe
, 0x00, 0x01);
139 /* calculate and set freq divider */
140 if (c
->frequency
< 1146000) {
141 ts2020_writereg(fe
, 0x10, 0x11);
142 ndiv
= ((c
->frequency
* (6 + 8) * 4) +
143 (TS2020_XTAL_FREQ
/ 2)) /
144 TS2020_XTAL_FREQ
- 1024;
146 ts2020_writereg(fe
, 0x10, 0x01);
147 ndiv
= ((c
->frequency
* (6 + 8) * 2) +
148 (TS2020_XTAL_FREQ
/ 2)) /
149 TS2020_XTAL_FREQ
- 1024;
152 ts2020_writereg(fe
, 0x01, (ndiv
& 0x0f00) >> 8);
153 ts2020_writereg(fe
, 0x02, ndiv
& 0x00ff);
156 ts2020_writereg(fe
, 0x03, 0x06);
157 ts2020_writereg(fe
, 0x51, 0x0f);
158 ts2020_writereg(fe
, 0x51, 0x1f);
159 ts2020_writereg(fe
, 0x50, 0x10);
160 ts2020_writereg(fe
, 0x50, 0x00);
164 ts2020_writereg(fe
, 0x51, 0x17);
165 ts2020_writereg(fe
, 0x51, 0x1f);
166 ts2020_writereg(fe
, 0x50, 0x08);
167 ts2020_writereg(fe
, 0x50, 0x00);
170 value
= ts2020_readreg(fe
, 0x3d);
172 if ((value
> 4) && (value
< 15)) {
176 value
= ((value
<< 3) | 0x01) & 0x79;
179 ts2020_writereg(fe
, 0x60, value
);
180 ts2020_writereg(fe
, 0x51, 0x17);
181 ts2020_writereg(fe
, 0x51, 0x1f);
182 ts2020_writereg(fe
, 0x50, 0x08);
183 ts2020_writereg(fe
, 0x50, 0x00);
185 /* set low-pass filter period */
186 ts2020_writereg(fe
, 0x04, 0x2e);
187 ts2020_writereg(fe
, 0x51, 0x1b);
188 ts2020_writereg(fe
, 0x51, 0x1f);
189 ts2020_writereg(fe
, 0x50, 0x04);
190 ts2020_writereg(fe
, 0x50, 0x00);
193 srate
= c
->symbol_rate
/ 1000;
195 f3db
= (srate
<< 2) / 5 + 2000;
203 /* set low-pass filter baseband */
204 value
= ts2020_readreg(fe
, 0x26);
205 mlpf
= 0x2e * 207 / ((value
<< 1) + 151);
206 mlpf_max
= mlpf
* 135 / 100;
207 mlpf_min
= mlpf
* 78 / 100;
211 /* rounded to the closest integer */
212 nlpf
= ((mlpf
* f3db
* 1000) + (2766 * TS2020_XTAL_FREQ
/ 2))
213 / (2766 * TS2020_XTAL_FREQ
);
219 /* rounded to the closest integer */
220 mlpf_new
= ((TS2020_XTAL_FREQ
* nlpf
* 2766) +
221 (1000 * f3db
/ 2)) / (1000 * f3db
);
223 if (mlpf_new
< mlpf_min
) {
225 mlpf_new
= ((TS2020_XTAL_FREQ
* nlpf
* 2766) +
226 (1000 * f3db
/ 2)) / (1000 * f3db
);
229 if (mlpf_new
> mlpf_max
)
232 ts2020_writereg(fe
, 0x04, mlpf_new
);
233 ts2020_writereg(fe
, 0x06, nlpf
);
234 ts2020_writereg(fe
, 0x51, 0x1b);
235 ts2020_writereg(fe
, 0x51, 0x1f);
236 ts2020_writereg(fe
, 0x50, 0x04);
237 ts2020_writereg(fe
, 0x50, 0x00);
241 ts2020_writereg(fe
, 0x51, 0x1e);
242 ts2020_writereg(fe
, 0x51, 0x1f);
243 ts2020_writereg(fe
, 0x50, 0x01);
244 ts2020_writereg(fe
, 0x50, 0x00);
250 static int ts2020_release(struct dvb_frontend
*fe
)
252 struct ts2020_state
*state
= fe
->tuner_priv
;
254 fe
->tuner_priv
= NULL
;
260 static int ts2020_get_signal_strength(struct dvb_frontend
*fe
,
261 u16
*signal_strength
)
263 u16 sig_reading
, sig_strength
;
266 rfgain
= ts2020_readreg(fe
, 0x3d) & 0x1f;
267 bbgain
= ts2020_readreg(fe
, 0x21) & 0x1f;
274 sig_reading
= rfgain
* 2 + bbgain
* 3;
276 sig_strength
= 40 + (64 - sig_reading
) * 50 / 64 ;
278 /* cook the value to be suitable for szap-s2 human readable output */
279 *signal_strength
= sig_strength
* 1000;
284 static struct dvb_tuner_ops ts2020_ops
= {
286 .name
= "Montage Technology TS2020 Silicon Tuner",
287 .frequency_min
= 950000,
288 .frequency_max
= 2150000,
292 .release
= ts2020_release
,
293 .set_params
= ts2020_set_params
,
294 .get_frequency
= ts2020_get_frequency
,
295 .get_rf_strength
= ts2020_get_signal_strength
298 struct dvb_frontend
*ts2020_attach(struct dvb_frontend
*fe
,
299 const struct ts2020_config
*config
, struct i2c_adapter
*i2c
)
301 struct ts2020_state
*state
= NULL
;
303 /* allocate memory for the internal state */
304 state
= kzalloc(sizeof(struct ts2020_state
), GFP_KERNEL
);
308 /* setup the state */
309 state
->tuner_address
= config
->tuner_address
;
311 fe
->tuner_priv
= state
;
312 fe
->ops
.tuner_ops
= ts2020_ops
;
313 fe
->ops
.read_signal_strength
= fe
->ops
.tuner_ops
.get_rf_strength
;
317 EXPORT_SYMBOL(ts2020_attach
);
319 MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
320 MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
321 MODULE_LICENSE("GPL");