[media] ds3000: lock led procedure added
[linux-2.6-block.git] / drivers / media / dvb-frontends / ts2020.c
CommitLineData
6fef4fc7
KD
1/*
2 Montage Technology TS2020 - Silicon Tuner driver
3 Copyright (C) 2009-2012 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
4
5 Copyright (C) 2009-2012 TurboSight.com
6
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.
11
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.
16
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.
20 */
21
22#include "dvb_frontend.h"
23#include "ts2020.h"
24
25#define TS2020_XTAL_FREQ 27000 /* in kHz */
26
27struct ts2020_state {
28 u8 tuner_address;
29 struct i2c_adapter *i2c;
30};
31
32static int ts2020_readreg(struct dvb_frontend *fe, u8 reg)
33{
34 struct ts2020_state *state = fe->tuner_priv;
35
36 int ret;
37 u8 b0[] = { reg };
38 u8 b1[] = { 0 };
39 struct i2c_msg msg[] = {
40 {
41 .addr = state->tuner_address,
42 .flags = 0,
43 .buf = b0,
44 .len = 1
45 }, {
46 .addr = state->tuner_address,
47 .flags = I2C_M_RD,
48 .buf = b1,
49 .len = 1
50 }
51 };
52
53 if (fe->ops.i2c_gate_ctrl)
54 fe->ops.i2c_gate_ctrl(fe, 1);
55
56 ret = i2c_transfer(state->i2c, msg, 2);
57
58 if (fe->ops.i2c_gate_ctrl)
59 fe->ops.i2c_gate_ctrl(fe, 0);
60
61 if (ret != 2) {
62 printk(KERN_ERR "%s: reg=0x%x(error=%d)\n", __func__, reg, ret);
63 return ret;
64 }
65
66 return b1[0];
67}
68
69static int ts2020_writereg(struct dvb_frontend *fe, int reg, int data)
70{
71 struct ts2020_state *state = fe->tuner_priv;
72
73 u8 buf[] = { reg, data };
74 struct i2c_msg msg = { .addr = state->tuner_address,
75 .flags = 0, .buf = buf, .len = 2 };
76 int err;
77
78
79 if (fe->ops.i2c_gate_ctrl)
80 fe->ops.i2c_gate_ctrl(fe, 1);
81
82 err = i2c_transfer(state->i2c, &msg, 1);
83
84 if (fe->ops.i2c_gate_ctrl)
85 fe->ops.i2c_gate_ctrl(fe, 0);
86
87 if (err != 1) {
88 printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x,"
89 " value == 0x%02x)\n", __func__, err, reg, data);
90 return -EREMOTEIO;
91 }
92
93 return 0;
94}
95
96static int ts2020_init(struct dvb_frontend *fe)
97{
98 ts2020_writereg(fe, 0x42, 0x73);
99 ts2020_writereg(fe, 0x05, 0x01);
100 ts2020_writereg(fe, 0x62, 0xf5);
101 return 0;
102}
103
104static int ts2020_get_frequency(struct dvb_frontend *fe, u32 *frequency)
105{
106 u16 ndiv, div4;
107
108 div4 = (ts2020_readreg(fe, 0x10) & 0x10) >> 4;
109
110 ndiv = ts2020_readreg(fe, 0x01);
111 ndiv &= 0x0f;
112 ndiv <<= 8;
113 ndiv |= ts2020_readreg(fe, 0x02);
114
115 /* actual tuned frequency, i.e. including the offset */
116 *frequency = (ndiv - ndiv % 2 + 1024) * TS2020_XTAL_FREQ
117 / (6 + 8) / (div4 + 1) / 2;
118
119 return 0;
120}
121
122static int ts2020_set_params(struct dvb_frontend *fe)
123{
124 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
125
f8c30b6f 126 u8 mlpf, mlpf_new, mlpf_max, mlpf_min, nlpf;
6fef4fc7
KD
127 u16 value, ndiv;
128 u32 srate = 0, f3db;
129
130 ts2020_init(fe);
131
132 /* unknown */
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);
6fef4fc7
KD
138
139 /* calculate and set freq divider */
140 if (c->frequency < 1146000) {
141 ts2020_writereg(fe, 0x10, 0x11);
6fef4fc7
KD
142 ndiv = ((c->frequency * (6 + 8) * 4) +
143 (TS2020_XTAL_FREQ / 2)) /
144 TS2020_XTAL_FREQ - 1024;
145 } else {
146 ts2020_writereg(fe, 0x10, 0x01);
147 ndiv = ((c->frequency * (6 + 8) * 2) +
148 (TS2020_XTAL_FREQ / 2)) /
149 TS2020_XTAL_FREQ - 1024;
150 }
151
152 ts2020_writereg(fe, 0x01, (ndiv & 0x0f00) >> 8);
153 ts2020_writereg(fe, 0x02, ndiv & 0x00ff);
154
155 /* set pll */
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);
161 msleep(5);
162
163 /* unknown */
164 ts2020_writereg(fe, 0x51, 0x17);
165 ts2020_writereg(fe, 0x51, 0x1f);
166 ts2020_writereg(fe, 0x50, 0x08);
167 ts2020_writereg(fe, 0x50, 0x00);
168 msleep(5);
169
170 value = ts2020_readreg(fe, 0x3d);
171 value &= 0x0f;
172 if ((value > 4) && (value < 15)) {
173 value -= 3;
174 if (value < 4)
175 value = 4;
176 value = ((value << 3) | 0x01) & 0x79;
177 }
178
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);
184
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);
191 msleep(5);
192
193 srate = c->symbol_rate / 1000;
194
195 f3db = (srate << 2) / 5 + 2000;
196 if (srate < 5000)
197 f3db += 3000;
198 if (f3db < 7000)
199 f3db = 7000;
200 if (f3db > 40000)
201 f3db = 40000;
202
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;
208 if (mlpf_max > 63)
209 mlpf_max = 63;
210
211 /* rounded to the closest integer */
212 nlpf = ((mlpf * f3db * 1000) + (2766 * TS2020_XTAL_FREQ / 2))
213 / (2766 * TS2020_XTAL_FREQ);
214 if (nlpf > 23)
215 nlpf = 23;
216 if (nlpf < 1)
217 nlpf = 1;
218
219 /* rounded to the closest integer */
220 mlpf_new = ((TS2020_XTAL_FREQ * nlpf * 2766) +
221 (1000 * f3db / 2)) / (1000 * f3db);
222
223 if (mlpf_new < mlpf_min) {
224 nlpf++;
225 mlpf_new = ((TS2020_XTAL_FREQ * nlpf * 2766) +
226 (1000 * f3db / 2)) / (1000 * f3db);
227 }
228
229 if (mlpf_new > mlpf_max)
230 mlpf_new = mlpf_max;
231
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);
238 msleep(5);
239
240 /* unknown */
241 ts2020_writereg(fe, 0x51, 0x1e);
242 ts2020_writereg(fe, 0x51, 0x1f);
243 ts2020_writereg(fe, 0x50, 0x01);
244 ts2020_writereg(fe, 0x50, 0x00);
245 msleep(60);
246
247 return 0;
248}
249
250static int ts2020_release(struct dvb_frontend *fe)
251{
252 struct ts2020_state *state = fe->tuner_priv;
253
254 fe->tuner_priv = NULL;
255 kfree(state);
256
257 return 0;
258}
259
f8c30b6f 260static int ts2020_get_signal_strength(struct dvb_frontend *fe,
6fef4fc7
KD
261 u16 *signal_strength)
262{
263 u16 sig_reading, sig_strength;
264 u8 rfgain, bbgain;
265
266 rfgain = ts2020_readreg(fe, 0x3d) & 0x1f;
267 bbgain = ts2020_readreg(fe, 0x21) & 0x1f;
268
269 if (rfgain > 15)
270 rfgain = 15;
271 if (bbgain > 13)
272 bbgain = 13;
273
274 sig_reading = rfgain * 2 + bbgain * 3;
275
276 sig_strength = 40 + (64 - sig_reading) * 50 / 64 ;
277
278 /* cook the value to be suitable for szap-s2 human readable output */
279 *signal_strength = sig_strength * 1000;
280
281 return 0;
282}
283
284static struct dvb_tuner_ops ts2020_ops = {
285 .info = {
286 .name = "Montage Technology TS2020 Silicon Tuner",
287 .frequency_min = 950000,
288 .frequency_max = 2150000,
289 },
290
291 .init = ts2020_init,
292 .release = ts2020_release,
293 .set_params = ts2020_set_params,
294 .get_frequency = ts2020_get_frequency,
295 .get_rf_strength = ts2020_get_signal_strength
296};
297
298struct dvb_frontend *ts2020_attach(struct dvb_frontend *fe,
299 const struct ts2020_config *config, struct i2c_adapter *i2c)
300{
301 struct ts2020_state *state = NULL;
302
303 /* allocate memory for the internal state */
304 state = kzalloc(sizeof(struct ts2020_state), GFP_KERNEL);
305 if (!state)
306 return NULL;
307
308 /* setup the state */
309 state->tuner_address = config->tuner_address;
310 state->i2c = i2c;
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;
314
315 return fe;
316}
317EXPORT_SYMBOL(ts2020_attach);
318
319MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
320MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
321MODULE_LICENSE("GPL");