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
31 struct i2c_adapter
*i2c
;
36 static int ts2020_release(struct dvb_frontend
*fe
)
38 kfree(fe
->tuner_priv
);
39 fe
->tuner_priv
= NULL
;
43 static int ts2020_writereg(struct dvb_frontend
*fe
, int reg
, int data
)
45 struct ts2020_priv
*priv
= fe
->tuner_priv
;
46 u8 buf
[] = { reg
, data
};
47 struct i2c_msg msg
[] = {
49 .addr
= priv
->i2c_address
,
57 if (fe
->ops
.i2c_gate_ctrl
)
58 fe
->ops
.i2c_gate_ctrl(fe
, 1);
60 err
= i2c_transfer(priv
->i2c
, msg
, 1);
63 "%s: writereg error(err == %i, reg == 0x%02x, value == 0x%02x)\n",
64 __func__
, err
, reg
, data
);
68 if (fe
->ops
.i2c_gate_ctrl
)
69 fe
->ops
.i2c_gate_ctrl(fe
, 0);
74 static int ts2020_readreg(struct dvb_frontend
*fe
, u8 reg
)
76 struct ts2020_priv
*priv
= fe
->tuner_priv
;
80 struct i2c_msg msg
[] = {
82 .addr
= priv
->i2c_address
,
87 .addr
= priv
->i2c_address
,
94 if (fe
->ops
.i2c_gate_ctrl
)
95 fe
->ops
.i2c_gate_ctrl(fe
, 1);
97 ret
= i2c_transfer(priv
->i2c
, msg
, 2);
100 printk(KERN_ERR
"%s: reg=0x%x(error=%d)\n",
105 if (fe
->ops
.i2c_gate_ctrl
)
106 fe
->ops
.i2c_gate_ctrl(fe
, 0);
111 static int ts2020_sleep(struct dvb_frontend
*fe
)
113 struct ts2020_priv
*priv
= fe
->tuner_priv
;
115 u8 buf
[] = { 10, 0 };
116 struct i2c_msg msg
= {
117 .addr
= priv
->i2c_address
,
123 if (fe
->ops
.i2c_gate_ctrl
)
124 fe
->ops
.i2c_gate_ctrl(fe
, 1);
126 ret
= i2c_transfer(priv
->i2c
, &msg
, 1);
128 printk(KERN_ERR
"%s: i2c error\n", __func__
);
130 if (fe
->ops
.i2c_gate_ctrl
)
131 fe
->ops
.i2c_gate_ctrl(fe
, 0);
133 return (ret
== 1) ? 0 : ret
;
136 static int ts2020_init(struct dvb_frontend
*fe
)
138 struct ts2020_priv
*priv
= fe
->tuner_priv
;
140 ts2020_writereg(fe
, 0x42, 0x73);
141 ts2020_writereg(fe
, 0x05, priv
->clk_out_div
);
142 ts2020_writereg(fe
, 0x20, 0x27);
143 ts2020_writereg(fe
, 0x07, 0x02);
144 ts2020_writereg(fe
, 0x11, 0xff);
145 ts2020_writereg(fe
, 0x60, 0xf9);
146 ts2020_writereg(fe
, 0x08, 0x01);
147 ts2020_writereg(fe
, 0x00, 0x41);
152 static int ts2020_tuner_gate_ctrl(struct dvb_frontend
*fe
, u8 offset
)
155 ret
= ts2020_writereg(fe
, 0x51, 0x1f - offset
);
156 ret
|= ts2020_writereg(fe
, 0x51, 0x1f);
157 ret
|= ts2020_writereg(fe
, 0x50, offset
);
158 ret
|= ts2020_writereg(fe
, 0x50, 0x00);
163 static int ts2020_set_tuner_rf(struct dvb_frontend
*fe
)
167 reg
= ts2020_readreg(fe
, 0x3d);
171 else if (reg
== 0x16)
176 ts2020_writereg(fe
, 0x60, reg
);
177 reg
= ts2020_tuner_gate_ctrl(fe
, 0x08);
182 static int ts2020_set_params(struct dvb_frontend
*fe
)
184 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
185 struct ts2020_priv
*priv
= fe
->tuner_priv
;
187 u32 frequency
= c
->frequency
;
189 u32 symbol_rate
= (c
->symbol_rate
/ 1000);
191 u16 value
, ndiv
, lpf_coeff
;
192 u8 lpf_mxdiv
, mlpf_max
, mlpf_min
, nlpf
;
193 u8 lo
= 0x01, div4
= 0x0;
195 /* Calculate frequency divider */
196 if (frequency
< 1060000) {
199 ndiv
= (frequency
* 14 * 4) / TS2020_XTAL_FREQ
;
201 ndiv
= (frequency
* 14 * 2) / TS2020_XTAL_FREQ
;
202 ndiv
= ndiv
+ ndiv
% 2;
205 ret
= ts2020_writereg(fe
, 0x10, 0x80 | lo
);
207 /* Set frequency divider */
208 ret
|= ts2020_writereg(fe
, 0x01, (ndiv
>> 8) & 0xf);
209 ret
|= ts2020_writereg(fe
, 0x02, ndiv
& 0xff);
211 ret
|= ts2020_writereg(fe
, 0x03, 0x06);
212 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x10);
216 /* Tuner Frequency Range */
217 ret
= ts2020_writereg(fe
, 0x10, lo
);
219 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x08);
222 ret
|= ts2020_set_tuner_rf(fe
);
224 gdiv28
= (TS2020_XTAL_FREQ
/ 1000 * 1694 + 500) / 1000;
225 ret
|= ts2020_writereg(fe
, 0x04, gdiv28
& 0xff);
226 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x04);
230 value
= ts2020_readreg(fe
, 0x26);
232 f3db
= (symbol_rate
* 135) / 200 + 2000;
233 f3db
+= FREQ_OFFSET_LOW_SYM_RATE
;
239 gdiv28
= gdiv28
* 207 / (value
* 2 + 151);
240 mlpf_max
= gdiv28
* 135 / 100;
241 mlpf_min
= gdiv28
* 78 / 100;
247 nlpf
= (f3db
* gdiv28
* 2 / lpf_coeff
/
248 (TS2020_XTAL_FREQ
/ 1000) + 1) / 2;
254 lpf_mxdiv
= (nlpf
* (TS2020_XTAL_FREQ
/ 1000)
255 * lpf_coeff
* 2 / f3db
+ 1) / 2;
257 if (lpf_mxdiv
< mlpf_min
) {
259 lpf_mxdiv
= (nlpf
* (TS2020_XTAL_FREQ
/ 1000)
260 * lpf_coeff
* 2 / f3db
+ 1) / 2;
263 if (lpf_mxdiv
> mlpf_max
)
264 lpf_mxdiv
= mlpf_max
;
266 ret
= ts2020_writereg(fe
, 0x04, lpf_mxdiv
);
267 ret
|= ts2020_writereg(fe
, 0x06, nlpf
);
269 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x04);
271 ret
|= ts2020_tuner_gate_ctrl(fe
, 0x01);
274 /* calculate offset assuming 96000kHz*/
275 offset_khz
= (ndiv
- ndiv
% 2 + 1024) * TS2020_XTAL_FREQ
276 / (6 + 8) / (div4
+ 1) / 2;
278 priv
->frequency
= offset_khz
;
280 return (ret
< 0) ? -EINVAL
: 0;
283 static int ts2020_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
285 struct ts2020_priv
*priv
= fe
->tuner_priv
;
286 *frequency
= priv
->frequency
;
290 /* read TS2020 signal strength */
291 static int ts2020_read_signal_strength(struct dvb_frontend
*fe
,
292 u16
*signal_strength
)
294 u16 sig_reading
, sig_strength
;
297 rfgain
= ts2020_readreg(fe
, 0x3d) & 0x1f;
298 bbgain
= ts2020_readreg(fe
, 0x21) & 0x1f;
305 sig_reading
= rfgain
* 2 + bbgain
* 3;
307 sig_strength
= 40 + (64 - sig_reading
) * 50 / 64 ;
309 /* cook the value to be suitable for szap-s2 human readable output */
310 *signal_strength
= sig_strength
* 1000;
315 static struct dvb_tuner_ops ts2020_tuner_ops
= {
318 .frequency_min
= 950000,
319 .frequency_max
= 2150000
322 .release
= ts2020_release
,
323 .sleep
= ts2020_sleep
,
324 .set_params
= ts2020_set_params
,
325 .get_frequency
= ts2020_get_frequency
,
326 .get_rf_strength
= ts2020_read_signal_strength
,
329 struct dvb_frontend
*ts2020_attach(struct dvb_frontend
*fe
,
330 const struct ts2020_config
*config
,
331 struct i2c_adapter
*i2c
)
333 struct ts2020_priv
*priv
= NULL
;
336 priv
= kzalloc(sizeof(struct ts2020_priv
), GFP_KERNEL
);
340 priv
->i2c_address
= config
->tuner_address
;
342 priv
->clk_out_div
= config
->clk_out_div
;
343 fe
->tuner_priv
= priv
;
345 /* Wake Up the tuner */
346 if ((0x03 & ts2020_readreg(fe
, 0x00)) == 0x00) {
347 ts2020_writereg(fe
, 0x00, 0x01);
351 ts2020_writereg(fe
, 0x00, 0x03);
354 /* Check the tuner version */
355 buf
= ts2020_readreg(fe
, 0x00);
356 if ((buf
== 0x01) || (buf
== 0x41) || (buf
== 0x81))
357 printk(KERN_INFO
"%s: Find tuner TS2020!\n", __func__
);
359 printk(KERN_ERR
"%s: Read tuner reg[0] = %d\n", __func__
, buf
);
364 memcpy(&fe
->ops
.tuner_ops
, &ts2020_tuner_ops
,
365 sizeof(struct dvb_tuner_ops
));
369 EXPORT_SYMBOL(ts2020_attach
);
371 MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
372 MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
373 MODULE_LICENSE("GPL");