blob: e1d4647b1a9942c05fcb96606b5a2c53db60d3ff [file] [log] [blame]
Manu Abrahame415c682009-04-06 15:45:20 -03001/*
2 STV0900/0903 Multistandard Broadcast Frontend driver
3 Copyright (C) Manu Abraham <abraham.manu@gmail.com>
4
5 Copyright (C) ST Microelectronics
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 <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/string.h>
26#include <linux/mutex.h>
27
28#include <linux/dvb/frontend.h>
29#include "dvb_frontend.h"
30
31#include "stv6110x.h" /* for demodulator internal modes */
32
33#include "stv090x_reg.h"
34#include "stv090x.h"
35#include "stv090x_priv.h"
36
37static unsigned int verbose;
38module_param(verbose, int, 0644);
39
Andreas Regel97f7a2a2010-01-05 19:19:43 -030040/* internal params node */
41struct stv090x_dev {
42 /* pointer for internal params, one for each pair of demods */
43 struct stv090x_internal *internal;
44 struct stv090x_dev *next_dev;
45};
46
47/* first internal params */
48static struct stv090x_dev *stv090x_first_dev;
49
50/* find chip by i2c adapter and i2c address */
51static struct stv090x_dev *find_dev(struct i2c_adapter *i2c_adap,
52 u8 i2c_addr)
53{
54 struct stv090x_dev *temp_dev = stv090x_first_dev;
55
56 /*
57 Search of the last stv0900 chip or
58 find it by i2c adapter and i2c address */
59 while ((temp_dev != NULL) &&
60 ((temp_dev->internal->i2c_adap != i2c_adap) ||
61 (temp_dev->internal->i2c_addr != i2c_addr))) {
62
63 temp_dev = temp_dev->next_dev;
64 }
65
66 return temp_dev;
67}
68
69/* deallocating chip */
70static void remove_dev(struct stv090x_internal *internal)
71{
72 struct stv090x_dev *prev_dev = stv090x_first_dev;
73 struct stv090x_dev *del_dev = find_dev(internal->i2c_adap,
74 internal->i2c_addr);
75
76 if (del_dev != NULL) {
77 if (del_dev == stv090x_first_dev) {
78 stv090x_first_dev = del_dev->next_dev;
79 } else {
80 while (prev_dev->next_dev != del_dev)
81 prev_dev = prev_dev->next_dev;
82
83 prev_dev->next_dev = del_dev->next_dev;
84 }
85
86 kfree(del_dev);
87 }
88}
89
90/* allocating new chip */
91static struct stv090x_dev *append_internal(struct stv090x_internal *internal)
92{
93 struct stv090x_dev *new_dev;
94 struct stv090x_dev *temp_dev;
95
96 new_dev = kmalloc(sizeof(struct stv090x_dev), GFP_KERNEL);
97 if (new_dev != NULL) {
98 new_dev->internal = internal;
99 new_dev->next_dev = NULL;
100
101 /* append to list */
102 if (stv090x_first_dev == NULL) {
103 stv090x_first_dev = new_dev;
104 } else {
105 temp_dev = stv090x_first_dev;
106 while (temp_dev->next_dev != NULL)
107 temp_dev = temp_dev->next_dev;
108
109 temp_dev->next_dev = new_dev;
110 }
111 }
112
113 return new_dev;
114}
115
Manu Abrahame415c682009-04-06 15:45:20 -0300116
117/* DVBS1 and DSS C/N Lookup table */
118static const struct stv090x_tab stv090x_s1cn_tab[] = {
119 { 0, 8917 }, /* 0.0dB */
120 { 5, 8801 }, /* 0.5dB */
121 { 10, 8667 }, /* 1.0dB */
122 { 15, 8522 }, /* 1.5dB */
123 { 20, 8355 }, /* 2.0dB */
124 { 25, 8175 }, /* 2.5dB */
125 { 30, 7979 }, /* 3.0dB */
126 { 35, 7763 }, /* 3.5dB */
127 { 40, 7530 }, /* 4.0dB */
128 { 45, 7282 }, /* 4.5dB */
129 { 50, 7026 }, /* 5.0dB */
130 { 55, 6781 }, /* 5.5dB */
131 { 60, 6514 }, /* 6.0dB */
132 { 65, 6241 }, /* 6.5dB */
133 { 70, 5965 }, /* 7.0dB */
134 { 75, 5690 }, /* 7.5dB */
135 { 80, 5424 }, /* 8.0dB */
136 { 85, 5161 }, /* 8.5dB */
137 { 90, 4902 }, /* 9.0dB */
138 { 95, 4654 }, /* 9.5dB */
139 { 100, 4417 }, /* 10.0dB */
140 { 105, 4186 }, /* 10.5dB */
141 { 110, 3968 }, /* 11.0dB */
142 { 115, 3757 }, /* 11.5dB */
143 { 120, 3558 }, /* 12.0dB */
144 { 125, 3366 }, /* 12.5dB */
145 { 130, 3185 }, /* 13.0dB */
146 { 135, 3012 }, /* 13.5dB */
147 { 140, 2850 }, /* 14.0dB */
148 { 145, 2698 }, /* 14.5dB */
149 { 150, 2550 }, /* 15.0dB */
150 { 160, 2283 }, /* 16.0dB */
151 { 170, 2042 }, /* 17.0dB */
152 { 180, 1827 }, /* 18.0dB */
153 { 190, 1636 }, /* 19.0dB */
154 { 200, 1466 }, /* 20.0dB */
155 { 210, 1315 }, /* 21.0dB */
156 { 220, 1181 }, /* 22.0dB */
157 { 230, 1064 }, /* 23.0dB */
158 { 240, 960 }, /* 24.0dB */
159 { 250, 869 }, /* 25.0dB */
160 { 260, 792 }, /* 26.0dB */
161 { 270, 724 }, /* 27.0dB */
162 { 280, 665 }, /* 28.0dB */
163 { 290, 616 }, /* 29.0dB */
164 { 300, 573 }, /* 30.0dB */
165 { 310, 537 }, /* 31.0dB */
166 { 320, 507 }, /* 32.0dB */
167 { 330, 483 }, /* 33.0dB */
168 { 400, 398 }, /* 40.0dB */
169 { 450, 381 }, /* 45.0dB */
170 { 500, 377 } /* 50.0dB */
171};
172
173/* DVBS2 C/N Lookup table */
174static const struct stv090x_tab stv090x_s2cn_tab[] = {
175 { -30, 13348 }, /* -3.0dB */
176 { -20, 12640 }, /* -2d.0B */
177 { -10, 11883 }, /* -1.0dB */
178 { 0, 11101 }, /* -0.0dB */
179 { 5, 10718 }, /* 0.5dB */
180 { 10, 10339 }, /* 1.0dB */
181 { 15, 9947 }, /* 1.5dB */
182 { 20, 9552 }, /* 2.0dB */
183 { 25, 9183 }, /* 2.5dB */
184 { 30, 8799 }, /* 3.0dB */
185 { 35, 8422 }, /* 3.5dB */
186 { 40, 8062 }, /* 4.0dB */
187 { 45, 7707 }, /* 4.5dB */
188 { 50, 7353 }, /* 5.0dB */
189 { 55, 7025 }, /* 5.5dB */
190 { 60, 6684 }, /* 6.0dB */
191 { 65, 6331 }, /* 6.5dB */
192 { 70, 6036 }, /* 7.0dB */
193 { 75, 5727 }, /* 7.5dB */
194 { 80, 5437 }, /* 8.0dB */
195 { 85, 5164 }, /* 8.5dB */
196 { 90, 4902 }, /* 9.0dB */
197 { 95, 4653 }, /* 9.5dB */
198 { 100, 4408 }, /* 10.0dB */
199 { 105, 4187 }, /* 10.5dB */
200 { 110, 3961 }, /* 11.0dB */
201 { 115, 3751 }, /* 11.5dB */
202 { 120, 3558 }, /* 12.0dB */
203 { 125, 3368 }, /* 12.5dB */
204 { 130, 3191 }, /* 13.0dB */
205 { 135, 3017 }, /* 13.5dB */
206 { 140, 2862 }, /* 14.0dB */
207 { 145, 2710 }, /* 14.5dB */
208 { 150, 2565 }, /* 15.0dB */
209 { 160, 2300 }, /* 16.0dB */
210 { 170, 2058 }, /* 17.0dB */
211 { 180, 1849 }, /* 18.0dB */
212 { 190, 1663 }, /* 19.0dB */
213 { 200, 1495 }, /* 20.0dB */
214 { 210, 1349 }, /* 21.0dB */
215 { 220, 1222 }, /* 22.0dB */
216 { 230, 1110 }, /* 23.0dB */
217 { 240, 1011 }, /* 24.0dB */
218 { 250, 925 }, /* 25.0dB */
219 { 260, 853 }, /* 26.0dB */
220 { 270, 789 }, /* 27.0dB */
221 { 280, 734 }, /* 28.0dB */
222 { 290, 690 }, /* 29.0dB */
223 { 300, 650 }, /* 30.0dB */
224 { 310, 619 }, /* 31.0dB */
225 { 320, 593 }, /* 32.0dB */
226 { 330, 571 }, /* 33.0dB */
227 { 400, 498 }, /* 40.0dB */
228 { 450, 484 }, /* 45.0dB */
229 { 500, 481 } /* 50.0dB */
230};
231
232/* RF level C/N lookup table */
233static const struct stv090x_tab stv090x_rf_tab[] = {
234 { -5, 0xcaa1 }, /* -5dBm */
235 { -10, 0xc229 }, /* -10dBm */
236 { -15, 0xbb08 }, /* -15dBm */
237 { -20, 0xb4bc }, /* -20dBm */
238 { -25, 0xad5a }, /* -25dBm */
239 { -30, 0xa298 }, /* -30dBm */
240 { -35, 0x98a8 }, /* -35dBm */
241 { -40, 0x8389 }, /* -40dBm */
242 { -45, 0x59be }, /* -45dBm */
243 { -50, 0x3a14 }, /* -50dBm */
244 { -55, 0x2d11 }, /* -55dBm */
245 { -60, 0x210d }, /* -60dBm */
246 { -65, 0xa14f }, /* -65dBm */
247 { -70, 0x07aa } /* -70dBm */
248};
249
250
251static struct stv090x_reg stv0900_initval[] = {
252
253 { STV090x_OUTCFG, 0x00 },
Manu Abraham56571502009-04-07 16:08:26 -0300254 { STV090x_MODECFG, 0xff },
Manu Abrahame415c682009-04-06 15:45:20 -0300255 { STV090x_AGCRF1CFG, 0x11 },
256 { STV090x_AGCRF2CFG, 0x13 },
Manu Abraham56571502009-04-07 16:08:26 -0300257 { STV090x_TSGENERAL1X, 0x14 },
Manu Abrahame415c682009-04-06 15:45:20 -0300258 { STV090x_TSTTNR2, 0x21 },
259 { STV090x_TSTTNR4, 0x21 },
260 { STV090x_P2_DISTXCTL, 0x22 },
261 { STV090x_P2_F22TX, 0xc0 },
262 { STV090x_P2_F22RX, 0xc0 },
263 { STV090x_P2_DISRXCTL, 0x00 },
264 { STV090x_P2_DMDCFGMD, 0xF9 },
265 { STV090x_P2_DEMOD, 0x08 },
266 { STV090x_P2_DMDCFG3, 0xc4 },
267 { STV090x_P2_CARFREQ, 0xed },
268 { STV090x_P2_LDT, 0xd0 },
269 { STV090x_P2_LDT2, 0xb8 },
270 { STV090x_P2_TMGCFG, 0xd2 },
271 { STV090x_P2_TMGTHRISE, 0x20 },
272 { STV090x_P1_TMGCFG, 0xd2 },
273
274 { STV090x_P2_TMGTHFALL, 0x00 },
275 { STV090x_P2_FECSPY, 0x88 },
276 { STV090x_P2_FSPYDATA, 0x3a },
277 { STV090x_P2_FBERCPT4, 0x00 },
278 { STV090x_P2_FSPYBER, 0x10 },
279 { STV090x_P2_ERRCTRL1, 0x35 },
280 { STV090x_P2_ERRCTRL2, 0xc1 },
281 { STV090x_P2_CFRICFG, 0xf8 },
282 { STV090x_P2_NOSCFG, 0x1c },
Manu Abraham56571502009-04-07 16:08:26 -0300283 { STV090x_P2_DMDTOM, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300284 { STV090x_P2_CORRELMANT, 0x70 },
285 { STV090x_P2_CORRELABS, 0x88 },
Manu Abraham56571502009-04-07 16:08:26 -0300286 { STV090x_P2_AGC2O, 0x5b },
Manu Abrahame415c682009-04-06 15:45:20 -0300287 { STV090x_P2_AGC2REF, 0x38 },
288 { STV090x_P2_CARCFG, 0xe4 },
289 { STV090x_P2_ACLC, 0x1A },
290 { STV090x_P2_BCLC, 0x09 },
291 { STV090x_P2_CARHDR, 0x08 },
292 { STV090x_P2_KREFTMG, 0xc1 },
293 { STV090x_P2_SFRUPRATIO, 0xf0 },
294 { STV090x_P2_SFRLOWRATIO, 0x70 },
295 { STV090x_P2_SFRSTEP, 0x58 },
296 { STV090x_P2_TMGCFG2, 0x01 },
297 { STV090x_P2_CAR2CFG, 0x26 },
298 { STV090x_P2_BCLC2S2Q, 0x86 },
299 { STV090x_P2_BCLC2S28, 0x86 },
300 { STV090x_P2_SMAPCOEF7, 0x77 },
301 { STV090x_P2_SMAPCOEF6, 0x85 },
302 { STV090x_P2_SMAPCOEF5, 0x77 },
303 { STV090x_P2_TSCFGL, 0x20 },
304 { STV090x_P2_DMDCFG2, 0x3b },
305 { STV090x_P2_MODCODLST0, 0xff },
306 { STV090x_P2_MODCODLST1, 0xff },
307 { STV090x_P2_MODCODLST2, 0xff },
308 { STV090x_P2_MODCODLST3, 0xff },
309 { STV090x_P2_MODCODLST4, 0xff },
310 { STV090x_P2_MODCODLST5, 0xff },
311 { STV090x_P2_MODCODLST6, 0xff },
312 { STV090x_P2_MODCODLST7, 0xcc },
313 { STV090x_P2_MODCODLST8, 0xcc },
314 { STV090x_P2_MODCODLST9, 0xcc },
315 { STV090x_P2_MODCODLSTA, 0xcc },
316 { STV090x_P2_MODCODLSTB, 0xcc },
317 { STV090x_P2_MODCODLSTC, 0xcc },
318 { STV090x_P2_MODCODLSTD, 0xcc },
319 { STV090x_P2_MODCODLSTE, 0xcc },
320 { STV090x_P2_MODCODLSTF, 0xcf },
321 { STV090x_P1_DISTXCTL, 0x22 },
322 { STV090x_P1_F22TX, 0xc0 },
323 { STV090x_P1_F22RX, 0xc0 },
324 { STV090x_P1_DISRXCTL, 0x00 },
325 { STV090x_P1_DMDCFGMD, 0xf9 },
326 { STV090x_P1_DEMOD, 0x08 },
327 { STV090x_P1_DMDCFG3, 0xc4 },
Manu Abraham56571502009-04-07 16:08:26 -0300328 { STV090x_P1_DMDTOM, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300329 { STV090x_P1_CARFREQ, 0xed },
330 { STV090x_P1_LDT, 0xd0 },
331 { STV090x_P1_LDT2, 0xb8 },
332 { STV090x_P1_TMGCFG, 0xd2 },
333 { STV090x_P1_TMGTHRISE, 0x20 },
334 { STV090x_P1_TMGTHFALL, 0x00 },
335 { STV090x_P1_SFRUPRATIO, 0xf0 },
336 { STV090x_P1_SFRLOWRATIO, 0x70 },
337 { STV090x_P1_TSCFGL, 0x20 },
338 { STV090x_P1_FECSPY, 0x88 },
339 { STV090x_P1_FSPYDATA, 0x3a },
340 { STV090x_P1_FBERCPT4, 0x00 },
341 { STV090x_P1_FSPYBER, 0x10 },
342 { STV090x_P1_ERRCTRL1, 0x35 },
343 { STV090x_P1_ERRCTRL2, 0xc1 },
344 { STV090x_P1_CFRICFG, 0xf8 },
345 { STV090x_P1_NOSCFG, 0x1c },
346 { STV090x_P1_CORRELMANT, 0x70 },
347 { STV090x_P1_CORRELABS, 0x88 },
Manu Abraham56571502009-04-07 16:08:26 -0300348 { STV090x_P1_AGC2O, 0x5b },
Manu Abrahame415c682009-04-06 15:45:20 -0300349 { STV090x_P1_AGC2REF, 0x38 },
350 { STV090x_P1_CARCFG, 0xe4 },
351 { STV090x_P1_ACLC, 0x1A },
352 { STV090x_P1_BCLC, 0x09 },
353 { STV090x_P1_CARHDR, 0x08 },
354 { STV090x_P1_KREFTMG, 0xc1 },
355 { STV090x_P1_SFRSTEP, 0x58 },
356 { STV090x_P1_TMGCFG2, 0x01 },
357 { STV090x_P1_CAR2CFG, 0x26 },
358 { STV090x_P1_BCLC2S2Q, 0x86 },
359 { STV090x_P1_BCLC2S28, 0x86 },
360 { STV090x_P1_SMAPCOEF7, 0x77 },
361 { STV090x_P1_SMAPCOEF6, 0x85 },
362 { STV090x_P1_SMAPCOEF5, 0x77 },
363 { STV090x_P1_DMDCFG2, 0x3b },
364 { STV090x_P1_MODCODLST0, 0xff },
365 { STV090x_P1_MODCODLST1, 0xff },
366 { STV090x_P1_MODCODLST2, 0xff },
367 { STV090x_P1_MODCODLST3, 0xff },
368 { STV090x_P1_MODCODLST4, 0xff },
369 { STV090x_P1_MODCODLST5, 0xff },
370 { STV090x_P1_MODCODLST6, 0xff },
371 { STV090x_P1_MODCODLST7, 0xcc },
372 { STV090x_P1_MODCODLST8, 0xcc },
373 { STV090x_P1_MODCODLST9, 0xcc },
374 { STV090x_P1_MODCODLSTA, 0xcc },
375 { STV090x_P1_MODCODLSTB, 0xcc },
376 { STV090x_P1_MODCODLSTC, 0xcc },
377 { STV090x_P1_MODCODLSTD, 0xcc },
378 { STV090x_P1_MODCODLSTE, 0xcc },
379 { STV090x_P1_MODCODLSTF, 0xcf },
380 { STV090x_GENCFG, 0x1d },
381 { STV090x_NBITER_NF4, 0x37 },
382 { STV090x_NBITER_NF5, 0x29 },
383 { STV090x_NBITER_NF6, 0x37 },
384 { STV090x_NBITER_NF7, 0x33 },
385 { STV090x_NBITER_NF8, 0x31 },
386 { STV090x_NBITER_NF9, 0x2f },
387 { STV090x_NBITER_NF10, 0x39 },
388 { STV090x_NBITER_NF11, 0x3a },
389 { STV090x_NBITER_NF12, 0x29 },
390 { STV090x_NBITER_NF13, 0x37 },
391 { STV090x_NBITER_NF14, 0x33 },
392 { STV090x_NBITER_NF15, 0x2f },
393 { STV090x_NBITER_NF16, 0x39 },
394 { STV090x_NBITER_NF17, 0x3a },
395 { STV090x_NBITERNOERR, 0x04 },
396 { STV090x_GAINLLR_NF4, 0x0C },
397 { STV090x_GAINLLR_NF5, 0x0F },
398 { STV090x_GAINLLR_NF6, 0x11 },
399 { STV090x_GAINLLR_NF7, 0x14 },
400 { STV090x_GAINLLR_NF8, 0x17 },
401 { STV090x_GAINLLR_NF9, 0x19 },
402 { STV090x_GAINLLR_NF10, 0x20 },
403 { STV090x_GAINLLR_NF11, 0x21 },
404 { STV090x_GAINLLR_NF12, 0x0D },
405 { STV090x_GAINLLR_NF13, 0x0F },
406 { STV090x_GAINLLR_NF14, 0x13 },
407 { STV090x_GAINLLR_NF15, 0x1A },
408 { STV090x_GAINLLR_NF16, 0x1F },
409 { STV090x_GAINLLR_NF17, 0x21 },
Manu Abraham56571502009-04-07 16:08:26 -0300410 { STV090x_RCCFGH, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300411 { STV090x_P1_FECM, 0x01 }, /* disable DSS modes */
412 { STV090x_P2_FECM, 0x01 }, /* disable DSS modes */
413 { STV090x_P1_PRVIT, 0x2F }, /* disable PR 6/7 */
414 { STV090x_P2_PRVIT, 0x2F }, /* disable PR 6/7 */
415};
416
417static struct stv090x_reg stv0903_initval[] = {
418 { STV090x_OUTCFG, 0x00 },
419 { STV090x_AGCRF1CFG, 0x11 },
420 { STV090x_STOPCLK1, 0x48 },
421 { STV090x_STOPCLK2, 0x14 },
422 { STV090x_TSTTNR1, 0x27 },
423 { STV090x_TSTTNR2, 0x21 },
424 { STV090x_P1_DISTXCTL, 0x22 },
425 { STV090x_P1_F22TX, 0xc0 },
426 { STV090x_P1_F22RX, 0xc0 },
427 { STV090x_P1_DISRXCTL, 0x00 },
428 { STV090x_P1_DMDCFGMD, 0xF9 },
429 { STV090x_P1_DEMOD, 0x08 },
430 { STV090x_P1_DMDCFG3, 0xc4 },
431 { STV090x_P1_CARFREQ, 0xed },
432 { STV090x_P1_TNRCFG2, 0x82 },
433 { STV090x_P1_LDT, 0xd0 },
434 { STV090x_P1_LDT2, 0xb8 },
435 { STV090x_P1_TMGCFG, 0xd2 },
436 { STV090x_P1_TMGTHRISE, 0x20 },
437 { STV090x_P1_TMGTHFALL, 0x00 },
438 { STV090x_P1_SFRUPRATIO, 0xf0 },
439 { STV090x_P1_SFRLOWRATIO, 0x70 },
440 { STV090x_P1_TSCFGL, 0x20 },
441 { STV090x_P1_FECSPY, 0x88 },
442 { STV090x_P1_FSPYDATA, 0x3a },
443 { STV090x_P1_FBERCPT4, 0x00 },
444 { STV090x_P1_FSPYBER, 0x10 },
445 { STV090x_P1_ERRCTRL1, 0x35 },
446 { STV090x_P1_ERRCTRL2, 0xc1 },
447 { STV090x_P1_CFRICFG, 0xf8 },
448 { STV090x_P1_NOSCFG, 0x1c },
Manu Abraham56571502009-04-07 16:08:26 -0300449 { STV090x_P1_DMDTOM, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300450 { STV090x_P1_CORRELMANT, 0x70 },
451 { STV090x_P1_CORRELABS, 0x88 },
Manu Abraham56571502009-04-07 16:08:26 -0300452 { STV090x_P1_AGC2O, 0x5b },
453 { STV090x_P1_AGC2REF, 0x38 },
Manu Abrahame415c682009-04-06 15:45:20 -0300454 { STV090x_P1_CARCFG, 0xe4 },
455 { STV090x_P1_ACLC, 0x1A },
Manu Abraham56571502009-04-07 16:08:26 -0300456 { STV090x_P1_BCLC, 0x09 },
Manu Abrahame415c682009-04-06 15:45:20 -0300457 { STV090x_P1_CARHDR, 0x08 },
458 { STV090x_P1_KREFTMG, 0xc1 },
459 { STV090x_P1_SFRSTEP, 0x58 },
460 { STV090x_P1_TMGCFG2, 0x01 },
461 { STV090x_P1_CAR2CFG, 0x26 },
462 { STV090x_P1_BCLC2S2Q, 0x86 },
463 { STV090x_P1_BCLC2S28, 0x86 },
464 { STV090x_P1_SMAPCOEF7, 0x77 },
465 { STV090x_P1_SMAPCOEF6, 0x85 },
466 { STV090x_P1_SMAPCOEF5, 0x77 },
467 { STV090x_P1_DMDCFG2, 0x3b },
468 { STV090x_P1_MODCODLST0, 0xff },
469 { STV090x_P1_MODCODLST1, 0xff },
470 { STV090x_P1_MODCODLST2, 0xff },
471 { STV090x_P1_MODCODLST3, 0xff },
472 { STV090x_P1_MODCODLST4, 0xff },
473 { STV090x_P1_MODCODLST5, 0xff },
474 { STV090x_P1_MODCODLST6, 0xff },
475 { STV090x_P1_MODCODLST7, 0xcc },
476 { STV090x_P1_MODCODLST8, 0xcc },
477 { STV090x_P1_MODCODLST9, 0xcc },
478 { STV090x_P1_MODCODLSTA, 0xcc },
479 { STV090x_P1_MODCODLSTB, 0xcc },
480 { STV090x_P1_MODCODLSTC, 0xcc },
481 { STV090x_P1_MODCODLSTD, 0xcc },
482 { STV090x_P1_MODCODLSTE, 0xcc },
483 { STV090x_P1_MODCODLSTF, 0xcf },
484 { STV090x_GENCFG, 0x1c },
485 { STV090x_NBITER_NF4, 0x37 },
486 { STV090x_NBITER_NF5, 0x29 },
487 { STV090x_NBITER_NF6, 0x37 },
488 { STV090x_NBITER_NF7, 0x33 },
489 { STV090x_NBITER_NF8, 0x31 },
490 { STV090x_NBITER_NF9, 0x2f },
491 { STV090x_NBITER_NF10, 0x39 },
492 { STV090x_NBITER_NF11, 0x3a },
493 { STV090x_NBITER_NF12, 0x29 },
494 { STV090x_NBITER_NF13, 0x37 },
495 { STV090x_NBITER_NF14, 0x33 },
496 { STV090x_NBITER_NF15, 0x2f },
497 { STV090x_NBITER_NF16, 0x39 },
498 { STV090x_NBITER_NF17, 0x3a },
499 { STV090x_NBITERNOERR, 0x04 },
500 { STV090x_GAINLLR_NF4, 0x0C },
501 { STV090x_GAINLLR_NF5, 0x0F },
502 { STV090x_GAINLLR_NF6, 0x11 },
503 { STV090x_GAINLLR_NF7, 0x14 },
504 { STV090x_GAINLLR_NF8, 0x17 },
505 { STV090x_GAINLLR_NF9, 0x19 },
506 { STV090x_GAINLLR_NF10, 0x20 },
507 { STV090x_GAINLLR_NF11, 0x21 },
508 { STV090x_GAINLLR_NF12, 0x0D },
509 { STV090x_GAINLLR_NF13, 0x0F },
510 { STV090x_GAINLLR_NF14, 0x13 },
511 { STV090x_GAINLLR_NF15, 0x1A },
512 { STV090x_GAINLLR_NF16, 0x1F },
513 { STV090x_GAINLLR_NF17, 0x21 },
Manu Abraham56571502009-04-07 16:08:26 -0300514 { STV090x_RCCFGH, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300515 { STV090x_P1_FECM, 0x01 }, /*disable the DSS mode */
516 { STV090x_P1_PRVIT, 0x2f } /*disable puncture rate 6/7*/
517};
518
519static struct stv090x_reg stv0900_cut20_val[] = {
520
521 { STV090x_P2_DMDCFG3, 0xe8 },
Manu Abraham56571502009-04-07 16:08:26 -0300522 { STV090x_P2_DMDCFG4, 0x10 },
Manu Abrahame415c682009-04-06 15:45:20 -0300523 { STV090x_P2_CARFREQ, 0x38 },
524 { STV090x_P2_CARHDR, 0x20 },
525 { STV090x_P2_KREFTMG, 0x5a },
526 { STV090x_P2_SMAPCOEF7, 0x06 },
527 { STV090x_P2_SMAPCOEF6, 0x00 },
528 { STV090x_P2_SMAPCOEF5, 0x04 },
529 { STV090x_P2_NOSCFG, 0x0c },
530 { STV090x_P1_DMDCFG3, 0xe8 },
Manu Abraham56571502009-04-07 16:08:26 -0300531 { STV090x_P1_DMDCFG4, 0x10 },
Manu Abrahame415c682009-04-06 15:45:20 -0300532 { STV090x_P1_CARFREQ, 0x38 },
533 { STV090x_P1_CARHDR, 0x20 },
534 { STV090x_P1_KREFTMG, 0x5a },
535 { STV090x_P1_SMAPCOEF7, 0x06 },
536 { STV090x_P1_SMAPCOEF6, 0x00 },
537 { STV090x_P1_SMAPCOEF5, 0x04 },
538 { STV090x_P1_NOSCFG, 0x0c },
539 { STV090x_GAINLLR_NF4, 0x21 },
540 { STV090x_GAINLLR_NF5, 0x21 },
541 { STV090x_GAINLLR_NF6, 0x20 },
542 { STV090x_GAINLLR_NF7, 0x1F },
543 { STV090x_GAINLLR_NF8, 0x1E },
544 { STV090x_GAINLLR_NF9, 0x1E },
545 { STV090x_GAINLLR_NF10, 0x1D },
546 { STV090x_GAINLLR_NF11, 0x1B },
547 { STV090x_GAINLLR_NF12, 0x20 },
548 { STV090x_GAINLLR_NF13, 0x20 },
549 { STV090x_GAINLLR_NF14, 0x20 },
550 { STV090x_GAINLLR_NF15, 0x20 },
551 { STV090x_GAINLLR_NF16, 0x20 },
552 { STV090x_GAINLLR_NF17, 0x21 },
553};
554
555static struct stv090x_reg stv0903_cut20_val[] = {
556 { STV090x_P1_DMDCFG3, 0xe8 },
Manu Abraham56571502009-04-07 16:08:26 -0300557 { STV090x_P1_DMDCFG4, 0x10 },
Manu Abrahame415c682009-04-06 15:45:20 -0300558 { STV090x_P1_CARFREQ, 0x38 },
559 { STV090x_P1_CARHDR, 0x20 },
560 { STV090x_P1_KREFTMG, 0x5a },
561 { STV090x_P1_SMAPCOEF7, 0x06 },
562 { STV090x_P1_SMAPCOEF6, 0x00 },
563 { STV090x_P1_SMAPCOEF5, 0x04 },
564 { STV090x_P1_NOSCFG, 0x0c },
565 { STV090x_GAINLLR_NF4, 0x21 },
566 { STV090x_GAINLLR_NF5, 0x21 },
567 { STV090x_GAINLLR_NF6, 0x20 },
568 { STV090x_GAINLLR_NF7, 0x1F },
569 { STV090x_GAINLLR_NF8, 0x1E },
570 { STV090x_GAINLLR_NF9, 0x1E },
571 { STV090x_GAINLLR_NF10, 0x1D },
572 { STV090x_GAINLLR_NF11, 0x1B },
573 { STV090x_GAINLLR_NF12, 0x20 },
574 { STV090x_GAINLLR_NF13, 0x20 },
575 { STV090x_GAINLLR_NF14, 0x20 },
576 { STV090x_GAINLLR_NF15, 0x20 },
577 { STV090x_GAINLLR_NF16, 0x20 },
578 { STV090x_GAINLLR_NF17, 0x21 }
579};
580
Manu Abrahame415c682009-04-06 15:45:20 -0300581/* Cut 2.0 Long Frame Tracking CR loop */
582static struct stv090x_long_frame_crloop stv090x_s2_crl_cut20[] = {
583 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
584 { STV090x_QPSK_12, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x1e },
585 { STV090x_QPSK_35, 0x2f, 0x3f, 0x2e, 0x2f, 0x3d, 0x0f, 0x0e, 0x2e, 0x3d, 0x0e },
586 { STV090x_QPSK_23, 0x2f, 0x3f, 0x2e, 0x2f, 0x0e, 0x0f, 0x0e, 0x1e, 0x3d, 0x3d },
587 { STV090x_QPSK_34, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
588 { STV090x_QPSK_45, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
589 { STV090x_QPSK_56, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
590 { STV090x_QPSK_89, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
591 { STV090x_QPSK_910, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
592 { STV090x_8PSK_35, 0x3c, 0x3e, 0x1c, 0x2e, 0x0c, 0x1e, 0x2b, 0x2d, 0x1b, 0x1d },
593 { STV090x_8PSK_23, 0x1d, 0x3e, 0x3c, 0x2e, 0x2c, 0x1e, 0x0c, 0x2d, 0x2b, 0x1d },
594 { STV090x_8PSK_34, 0x0e, 0x3e, 0x3d, 0x2e, 0x0d, 0x1e, 0x2c, 0x2d, 0x0c, 0x1d },
595 { STV090x_8PSK_56, 0x2e, 0x3e, 0x1e, 0x2e, 0x2d, 0x1e, 0x3c, 0x2d, 0x2c, 0x1d },
596 { STV090x_8PSK_89, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x0d, 0x2d, 0x3c, 0x1d },
597 { STV090x_8PSK_910, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x1d, 0x2d, 0x0d, 0x1d }
598};
599
Manu Abraham27d40322009-05-02 18:26:58 -0300600/* Cut 3.0 Long Frame Tracking CR loop */
601static struct stv090x_long_frame_crloop stv090x_s2_crl_cut30[] = {
602 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
603 { STV090x_QPSK_12, 0x3c, 0x2c, 0x0c, 0x2c, 0x1b, 0x2c, 0x1b, 0x1c, 0x0b, 0x3b },
604 { STV090x_QPSK_35, 0x0d, 0x0d, 0x0c, 0x0d, 0x1b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
605 { STV090x_QPSK_23, 0x1d, 0x0d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
606 { STV090x_QPSK_34, 0x1d, 0x1d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
607 { STV090x_QPSK_45, 0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
608 { STV090x_QPSK_56, 0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
609 { STV090x_QPSK_89, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
610 { STV090x_QPSK_910, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
611 { STV090x_8PSK_35, 0x39, 0x29, 0x39, 0x19, 0x19, 0x19, 0x19, 0x19, 0x09, 0x19 },
612 { STV090x_8PSK_23, 0x2a, 0x39, 0x1a, 0x0a, 0x39, 0x0a, 0x29, 0x39, 0x29, 0x0a },
613 { STV090x_8PSK_34, 0x2b, 0x3a, 0x1b, 0x1b, 0x3a, 0x1b, 0x1a, 0x0b, 0x1a, 0x3a },
614 { STV090x_8PSK_56, 0x0c, 0x1b, 0x3b, 0x3b, 0x1b, 0x3b, 0x3a, 0x3b, 0x3a, 0x1b },
615 { STV090x_8PSK_89, 0x0d, 0x3c, 0x2c, 0x2c, 0x2b, 0x0c, 0x0b, 0x3b, 0x0b, 0x1b },
616 { STV090x_8PSK_910, 0x0d, 0x0d, 0x2c, 0x3c, 0x3b, 0x1c, 0x0b, 0x3b, 0x0b, 0x1b }
617};
Manu Abrahame415c682009-04-06 15:45:20 -0300618
619/* Cut 2.0 Long Frame Tracking CR Loop */
620static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut20[] = {
621 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
622 { STV090x_16APSK_23, 0x0c, 0x0c, 0x0c, 0x0c, 0x1d, 0x0c, 0x3c, 0x0c, 0x2c, 0x0c },
623 { STV090x_16APSK_34, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0c, 0x2d, 0x0c, 0x1d, 0x0c },
624 { STV090x_16APSK_45, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
625 { STV090x_16APSK_56, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
626 { STV090x_16APSK_89, 0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
627 { STV090x_16APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
628 { STV090x_32APSK_34, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
629 { STV090x_32APSK_45, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
630 { STV090x_32APSK_56, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
631 { STV090x_32APSK_89, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
632 { STV090x_32APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c }
633};
634
Manu Abraham27d40322009-05-02 18:26:58 -0300635/* Cut 3.0 Long Frame Tracking CR Loop */
636static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut30[] = {
637 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
638 { STV090x_16APSK_23, 0x0a, 0x0a, 0x0a, 0x0a, 0x1a, 0x0a, 0x3a, 0x0a, 0x2a, 0x0a },
639 { STV090x_16APSK_34, 0x0a, 0x0a, 0x0a, 0x0a, 0x0b, 0x0a, 0x3b, 0x0a, 0x1b, 0x0a },
640 { STV090x_16APSK_45, 0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
641 { STV090x_16APSK_56, 0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
642 { STV090x_16APSK_89, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
643 { STV090x_16APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
644 { STV090x_32APSK_34, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
645 { STV090x_32APSK_45, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
646 { STV090x_32APSK_56, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
647 { STV090x_32APSK_89, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
648 { STV090x_32APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a }
649};
Manu Abrahame415c682009-04-06 15:45:20 -0300650
651static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut20[] = {
652 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
653 { STV090x_QPSK_14, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x2d, 0x1f, 0x3d, 0x3e },
654 { STV090x_QPSK_13, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x3d, 0x0f, 0x3d, 0x2e },
655 { STV090x_QPSK_25, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x2e }
656};
657
Manu Abraham27d40322009-05-02 18:26:58 -0300658static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut30[] = {
659 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
660 { STV090x_QPSK_14, 0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x2a, 0x1c, 0x3a, 0x3b },
661 { STV090x_QPSK_13, 0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x3a, 0x0c, 0x3a, 0x2b },
662 { STV090x_QPSK_25, 0x1c, 0x3c, 0x1b, 0x3c, 0x3a, 0x1c, 0x3a, 0x3b, 0x3a, 0x2b }
Manu Abrahame415c682009-04-06 15:45:20 -0300663};
664
Manu Abraham27d40322009-05-02 18:26:58 -0300665/* Cut 2.0 Short Frame Tracking CR Loop */
666static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut20[] = {
667 /* MODCOD 2M 5M 10M 20M 30M */
668 { STV090x_QPSK, 0x2f, 0x2e, 0x0e, 0x0e, 0x3d },
669 { STV090x_8PSK, 0x3e, 0x0e, 0x2d, 0x0d, 0x3c },
670 { STV090x_16APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d },
671 { STV090x_32APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d }
672};
673
674/* Cut 3.0 Short Frame Tracking CR Loop */
675static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut30[] = {
676 /* MODCOD 2M 5M 10M 20M 30M */
677 { STV090x_QPSK, 0x2C, 0x2B, 0x0B, 0x0B, 0x3A },
678 { STV090x_8PSK, 0x3B, 0x0B, 0x2A, 0x0A, 0x39 },
679 { STV090x_16APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A },
680 { STV090x_32APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A }
681};
Manu Abrahame415c682009-04-06 15:45:20 -0300682
683static inline s32 comp2(s32 __x, s32 __width)
684{
685 if (__width == 32)
686 return __x;
687 else
688 return (__x >= (1 << (__width - 1))) ? (__x - (1 << __width)) : __x;
689}
690
691static int stv090x_read_reg(struct stv090x_state *state, unsigned int reg)
692{
693 const struct stv090x_config *config = state->config;
694 int ret;
695
696 u8 b0[] = { reg >> 8, reg & 0xff };
697 u8 buf;
698
699 struct i2c_msg msg[] = {
700 { .addr = config->address, .flags = 0, .buf = b0, .len = 2 },
701 { .addr = config->address, .flags = I2C_M_RD, .buf = &buf, .len = 1 }
702 };
703
704 ret = i2c_transfer(state->i2c, msg, 2);
705 if (ret != 2) {
706 if (ret != -ERESTARTSYS)
707 dprintk(FE_ERROR, 1,
708 "Read error, Reg=[0x%02x], Status=%d",
709 reg, ret);
710
711 return ret < 0 ? ret : -EREMOTEIO;
712 }
713 if (unlikely(*state->verbose >= FE_DEBUGREG))
714 dprintk(FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
715 reg, buf);
716
717 return (unsigned int) buf;
718}
719
720static int stv090x_write_regs(struct stv090x_state *state, unsigned int reg, u8 *data, u32 count)
721{
722 const struct stv090x_config *config = state->config;
723 int ret;
724 u8 buf[2 + count];
725 struct i2c_msg i2c_msg = { .addr = config->address, .flags = 0, .buf = buf, .len = 2 + count };
726
727 buf[0] = reg >> 8;
728 buf[1] = reg & 0xff;
729 memcpy(&buf[2], data, count);
730
731 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
732 int i;
733
734 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
735 for (i = 0; i < count; i++)
736 printk(" %02x", data[i]);
737 printk("\n");
738 }
739
740 ret = i2c_transfer(state->i2c, &i2c_msg, 1);
741 if (ret != 1) {
742 if (ret != -ERESTARTSYS)
743 dprintk(FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
744 reg, data[0], count, ret);
745 return ret < 0 ? ret : -EREMOTEIO;
746 }
747
748 return 0;
749}
750
751static int stv090x_write_reg(struct stv090x_state *state, unsigned int reg, u8 data)
752{
753 return stv090x_write_regs(state, reg, &data, 1);
754}
755
756static int stv090x_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
757{
758 struct stv090x_state *state = fe->demodulator_priv;
759 u32 reg;
760
Andreas Regel96506a52010-01-05 19:20:21 -0300761 if (enable)
762 mutex_lock(&state->internal->tuner_lock);
763
Manu Abrahame415c682009-04-06 15:45:20 -0300764 reg = STV090x_READ_DEMOD(state, I2CRPT);
Manu Abrahame415c682009-04-06 15:45:20 -0300765 if (enable) {
Manu Abraham017eb0382009-04-07 05:19:54 -0300766 dprintk(FE_DEBUG, 1, "Enable Gate");
Manu Abrahame415c682009-04-06 15:45:20 -0300767 STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 1);
768 if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0)
769 goto err;
770
771 } else {
Manu Abraham017eb0382009-04-07 05:19:54 -0300772 dprintk(FE_DEBUG, 1, "Disable Gate");
Manu Abrahame415c682009-04-06 15:45:20 -0300773 STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 0);
774 if ((STV090x_WRITE_DEMOD(state, I2CRPT, reg)) < 0)
775 goto err;
776 }
Andreas Regel96506a52010-01-05 19:20:21 -0300777
778 if (!enable)
779 mutex_unlock(&state->internal->tuner_lock);
780
Manu Abrahame415c682009-04-06 15:45:20 -0300781 return 0;
782err:
783 dprintk(FE_ERROR, 1, "I/O error");
Andreas Regel96506a52010-01-05 19:20:21 -0300784 mutex_unlock(&state->internal->tuner_lock);
Manu Abrahame415c682009-04-06 15:45:20 -0300785 return -1;
786}
787
788static void stv090x_get_lock_tmg(struct stv090x_state *state)
789{
790 switch (state->algo) {
791 case STV090x_BLIND_SEARCH:
792 dprintk(FE_DEBUG, 1, "Blind Search");
793 if (state->srate <= 1500000) { /*10Msps< SR <=15Msps*/
794 state->DemodTimeout = 1500;
795 state->FecTimeout = 400;
796 } else if (state->srate <= 5000000) { /*10Msps< SR <=15Msps*/
797 state->DemodTimeout = 1000;
798 state->FecTimeout = 300;
799 } else { /*SR >20Msps*/
800 state->DemodTimeout = 700;
801 state->FecTimeout = 100;
802 }
803 break;
804
805 case STV090x_COLD_SEARCH:
806 case STV090x_WARM_SEARCH:
807 default:
808 dprintk(FE_DEBUG, 1, "Normal Search");
809 if (state->srate <= 1000000) { /*SR <=1Msps*/
810 state->DemodTimeout = 4500;
811 state->FecTimeout = 1700;
812 } else if (state->srate <= 2000000) { /*1Msps < SR <= 2Msps */
813 state->DemodTimeout = 2500;
814 state->FecTimeout = 1100;
815 } else if (state->srate <= 5000000) { /*2Msps < SR <= 5Msps */
816 state->DemodTimeout = 1000;
817 state->FecTimeout = 550;
818 } else if (state->srate <= 10000000) { /*5Msps < SR <= 10Msps */
819 state->DemodTimeout = 700;
820 state->FecTimeout = 250;
821 } else if (state->srate <= 20000000) { /*10Msps < SR <= 20Msps */
822 state->DemodTimeout = 400;
823 state->FecTimeout = 130;
824 } else { /*SR >20Msps*/
825 state->DemodTimeout = 300;
826 state->FecTimeout = 100;
827 }
828 break;
829 }
830
831 if (state->algo == STV090x_WARM_SEARCH)
832 state->DemodTimeout /= 2;
833}
834
835static int stv090x_set_srate(struct stv090x_state *state, u32 srate)
836{
837 u32 sym;
838
Manu Abraham15bb3662009-04-08 20:14:00 -0300839 if (srate > 60000000) {
840 sym = (srate << 4); /* SR * 2^16 / master_clk */
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300841 sym /= (state->internal->mclk >> 12);
Manu Abraham15bb3662009-04-08 20:14:00 -0300842 } else if (srate > 6000000) {
843 sym = (srate << 6);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300844 sym /= (state->internal->mclk >> 10);
Manu Abrahame415c682009-04-06 15:45:20 -0300845 } else {
Manu Abraham15bb3662009-04-08 20:14:00 -0300846 sym = (srate << 9);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300847 sym /= (state->internal->mclk >> 7);
Manu Abrahame415c682009-04-06 15:45:20 -0300848 }
849
Manu Abraham15bb3662009-04-08 20:14:00 -0300850 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0x7f) < 0) /* MSB */
Manu Abrahame415c682009-04-06 15:45:20 -0300851 goto err;
852 if (STV090x_WRITE_DEMOD(state, SFRINIT0, (sym & 0xff)) < 0) /* LSB */
853 goto err;
Manu Abraham15bb3662009-04-08 20:14:00 -0300854
Manu Abrahame415c682009-04-06 15:45:20 -0300855 return 0;
856err:
857 dprintk(FE_ERROR, 1, "I/O error");
858 return -1;
859}
860
861static int stv090x_set_max_srate(struct stv090x_state *state, u32 clk, u32 srate)
862{
863 u32 sym;
864
865 srate = 105 * (srate / 100);
Manu Abraham15bb3662009-04-08 20:14:00 -0300866 if (srate > 60000000) {
867 sym = (srate << 4); /* SR * 2^16 / master_clk */
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300868 sym /= (state->internal->mclk >> 12);
Manu Abraham15bb3662009-04-08 20:14:00 -0300869 } else if (srate > 6000000) {
870 sym = (srate << 6);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300871 sym /= (state->internal->mclk >> 10);
Manu Abrahame415c682009-04-06 15:45:20 -0300872 } else {
Manu Abraham15bb3662009-04-08 20:14:00 -0300873 sym = (srate << 9);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300874 sym /= (state->internal->mclk >> 7);
Manu Abrahame415c682009-04-06 15:45:20 -0300875 }
Manu Abraham15bb3662009-04-08 20:14:00 -0300876
877 if (sym < 0x7fff) {
878 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) /* MSB */
879 goto err;
880 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) /* LSB */
881 goto err;
882 } else {
883 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x7f) < 0) /* MSB */
884 goto err;
885 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xff) < 0) /* LSB */
886 goto err;
887 }
888
Manu Abrahame415c682009-04-06 15:45:20 -0300889 return 0;
890err:
891 dprintk(FE_ERROR, 1, "I/O error");
892 return -1;
893}
894
895static int stv090x_set_min_srate(struct stv090x_state *state, u32 clk, u32 srate)
896{
897 u32 sym;
898
899 srate = 95 * (srate / 100);
Manu Abraham15bb3662009-04-08 20:14:00 -0300900 if (srate > 60000000) {
901 sym = (srate << 4); /* SR * 2^16 / master_clk */
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300902 sym /= (state->internal->mclk >> 12);
Manu Abraham15bb3662009-04-08 20:14:00 -0300903 } else if (srate > 6000000) {
904 sym = (srate << 6);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300905 sym /= (state->internal->mclk >> 10);
Manu Abrahame415c682009-04-06 15:45:20 -0300906 } else {
Manu Abraham15bb3662009-04-08 20:14:00 -0300907 sym = (srate << 9);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300908 sym /= (state->internal->mclk >> 7);
Manu Abrahame415c682009-04-06 15:45:20 -0300909 }
Manu Abraham15bb3662009-04-08 20:14:00 -0300910
Andreas Regelb671a8d2009-11-13 18:15:27 -0300911 if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0x7f)) < 0) /* MSB */
Manu Abrahame415c682009-04-06 15:45:20 -0300912 goto err;
913 if (STV090x_WRITE_DEMOD(state, SFRLOW0, (sym & 0xff)) < 0) /* LSB */
914 goto err;
915 return 0;
916err:
917 dprintk(FE_ERROR, 1, "I/O error");
918 return -1;
919}
920
Andreas Regel4e58a682009-04-16 08:40:36 -0300921static u32 stv090x_car_width(u32 srate, enum stv090x_rolloff rolloff)
Manu Abrahame415c682009-04-06 15:45:20 -0300922{
Andreas Regel4e58a682009-04-16 08:40:36 -0300923 u32 ro;
924
925 switch (rolloff) {
926 case STV090x_RO_20:
927 ro = 20;
928 break;
929 case STV090x_RO_25:
930 ro = 25;
931 break;
932 case STV090x_RO_35:
933 default:
934 ro = 35;
935 break;
936 }
937
938 return srate + (srate * ro) / 100;
Manu Abrahame415c682009-04-06 15:45:20 -0300939}
940
941static int stv090x_set_vit_thacq(struct stv090x_state *state)
942{
943 if (STV090x_WRITE_DEMOD(state, VTH12, 0x96) < 0)
944 goto err;
945 if (STV090x_WRITE_DEMOD(state, VTH23, 0x64) < 0)
946 goto err;
947 if (STV090x_WRITE_DEMOD(state, VTH34, 0x36) < 0)
948 goto err;
949 if (STV090x_WRITE_DEMOD(state, VTH56, 0x23) < 0)
950 goto err;
951 if (STV090x_WRITE_DEMOD(state, VTH67, 0x1e) < 0)
952 goto err;
953 if (STV090x_WRITE_DEMOD(state, VTH78, 0x19) < 0)
954 goto err;
955 return 0;
956err:
957 dprintk(FE_ERROR, 1, "I/O error");
958 return -1;
959}
960
961static int stv090x_set_vit_thtracq(struct stv090x_state *state)
962{
963 if (STV090x_WRITE_DEMOD(state, VTH12, 0xd0) < 0)
964 goto err;
965 if (STV090x_WRITE_DEMOD(state, VTH23, 0x7d) < 0)
966 goto err;
967 if (STV090x_WRITE_DEMOD(state, VTH34, 0x53) < 0)
968 goto err;
969 if (STV090x_WRITE_DEMOD(state, VTH56, 0x2f) < 0)
970 goto err;
971 if (STV090x_WRITE_DEMOD(state, VTH67, 0x24) < 0)
972 goto err;
973 if (STV090x_WRITE_DEMOD(state, VTH78, 0x1f) < 0)
974 goto err;
975 return 0;
976err:
977 dprintk(FE_ERROR, 1, "I/O error");
978 return -1;
979}
980
981static int stv090x_set_viterbi(struct stv090x_state *state)
982{
983 switch (state->search_mode) {
984 case STV090x_SEARCH_AUTO:
985 if (STV090x_WRITE_DEMOD(state, FECM, 0x10) < 0) /* DVB-S and DVB-S2 */
986 goto err;
987 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x3f) < 0) /* all puncture rate */
988 goto err;
989 break;
990 case STV090x_SEARCH_DVBS1:
991 if (STV090x_WRITE_DEMOD(state, FECM, 0x00) < 0) /* disable DSS */
992 goto err;
993 switch (state->fec) {
994 case STV090x_PR12:
995 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
996 goto err;
997 break;
998
999 case STV090x_PR23:
1000 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
1001 goto err;
1002 break;
1003
1004 case STV090x_PR34:
1005 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x04) < 0)
1006 goto err;
1007 break;
1008
1009 case STV090x_PR56:
1010 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x08) < 0)
1011 goto err;
1012 break;
1013
1014 case STV090x_PR78:
1015 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x20) < 0)
1016 goto err;
1017 break;
1018
1019 default:
1020 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x2f) < 0) /* all */
1021 goto err;
1022 break;
1023 }
1024 break;
1025 case STV090x_SEARCH_DSS:
1026 if (STV090x_WRITE_DEMOD(state, FECM, 0x80) < 0)
1027 goto err;
1028 switch (state->fec) {
1029 case STV090x_PR12:
1030 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
1031 goto err;
1032 break;
1033
1034 case STV090x_PR23:
1035 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
1036 goto err;
1037 break;
1038
1039 case STV090x_PR67:
1040 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x10) < 0)
1041 goto err;
1042 break;
1043
1044 default:
1045 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x13) < 0) /* 1/2, 2/3, 6/7 */
1046 goto err;
1047 break;
1048 }
1049 break;
1050 default:
1051 break;
1052 }
1053 return 0;
1054err:
1055 dprintk(FE_ERROR, 1, "I/O error");
1056 return -1;
1057}
1058
1059static int stv090x_stop_modcod(struct stv090x_state *state)
1060{
1061 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1062 goto err;
1063 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
1064 goto err;
1065 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
1066 goto err;
1067 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
1068 goto err;
1069 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
1070 goto err;
1071 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
1072 goto err;
1073 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
1074 goto err;
1075 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xff) < 0)
1076 goto err;
1077 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xff) < 0)
1078 goto err;
1079 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xff) < 0)
1080 goto err;
1081 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xff) < 0)
1082 goto err;
1083 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xff) < 0)
1084 goto err;
1085 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xff) < 0)
1086 goto err;
1087 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xff) < 0)
1088 goto err;
1089 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
1090 goto err;
1091 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xff) < 0)
1092 goto err;
1093 return 0;
1094err:
1095 dprintk(FE_ERROR, 1, "I/O error");
1096 return -1;
1097}
1098
1099static int stv090x_activate_modcod(struct stv090x_state *state)
1100{
Manu Abraham27d40322009-05-02 18:26:58 -03001101 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1102 goto err;
1103 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xfc) < 0)
1104 goto err;
1105 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xcc) < 0)
1106 goto err;
1107 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xcc) < 0)
1108 goto err;
1109 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xcc) < 0)
1110 goto err;
1111 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xcc) < 0)
1112 goto err;
1113 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xcc) < 0)
1114 goto err;
1115 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
1116 goto err;
1117 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
1118 goto err;
1119 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
1120 goto err;
1121 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
1122 goto err;
1123 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
1124 goto err;
1125 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
1126 goto err;
1127 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
1128 goto err;
1129 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xcc) < 0)
1130 goto err;
1131 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
1132 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001133
Manu Abrahame415c682009-04-06 15:45:20 -03001134 return 0;
1135err:
1136 dprintk(FE_ERROR, 1, "I/O error");
1137 return -1;
1138}
1139
Manu Abraham27d40322009-05-02 18:26:58 -03001140static int stv090x_activate_modcod_single(struct stv090x_state *state)
1141{
1142
1143 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1144 goto err;
1145 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xf0) < 0)
1146 goto err;
1147 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0x00) < 0)
1148 goto err;
1149 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0x00) < 0)
1150 goto err;
1151 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0x00) < 0)
1152 goto err;
1153 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0x00) < 0)
1154 goto err;
1155 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0x00) < 0)
1156 goto err;
1157 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0x00) < 0)
1158 goto err;
1159 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0x00) < 0)
1160 goto err;
1161 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0x00) < 0)
1162 goto err;
1163 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0x00) < 0)
1164 goto err;
1165 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0x00) < 0)
1166 goto err;
1167 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0x00) < 0)
1168 goto err;
1169 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0x00) < 0)
1170 goto err;
1171 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0x00) < 0)
1172 goto err;
1173 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0x0f) < 0)
1174 goto err;
1175
1176 return 0;
1177
1178err:
1179 dprintk(FE_ERROR, 1, "I/O error");
1180 return -1;
1181}
1182
Manu Abrahame415c682009-04-06 15:45:20 -03001183static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
1184{
1185 u32 reg;
1186
1187 switch (state->demod) {
1188 case STV090x_DEMODULATOR_0:
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001189 mutex_lock(&state->internal->demod_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03001190 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1191 STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, enable);
1192 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1193 goto err;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001194 mutex_unlock(&state->internal->demod_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03001195 break;
1196
1197 case STV090x_DEMODULATOR_1:
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001198 mutex_lock(&state->internal->demod_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03001199 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1200 STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, enable);
1201 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1202 goto err;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001203 mutex_unlock(&state->internal->demod_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03001204 break;
1205
1206 default:
1207 dprintk(FE_ERROR, 1, "Wrong demodulator!");
1208 break;
1209 }
1210 return 0;
1211err:
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001212 mutex_unlock(&state->internal->demod_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03001213 dprintk(FE_ERROR, 1, "I/O error");
1214 return -1;
1215}
1216
Manu Abraham27d40322009-05-02 18:26:58 -03001217static int stv090x_dvbs_track_crl(struct stv090x_state *state)
1218{
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001219 if (state->internal->dev_ver >= 0x30) {
Manu Abraham27d40322009-05-02 18:26:58 -03001220 /* Set ACLC BCLC optimised value vs SR */
1221 if (state->srate >= 15000000) {
1222 if (STV090x_WRITE_DEMOD(state, ACLC, 0x2b) < 0)
1223 goto err;
1224 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1a) < 0)
1225 goto err;
1226 } else if ((state->srate >= 7000000) && (15000000 > state->srate)) {
1227 if (STV090x_WRITE_DEMOD(state, ACLC, 0x0c) < 0)
1228 goto err;
1229 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1b) < 0)
1230 goto err;
1231 } else if (state->srate < 7000000) {
1232 if (STV090x_WRITE_DEMOD(state, ACLC, 0x2c) < 0)
1233 goto err;
1234 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1c) < 0)
1235 goto err;
1236 }
1237
1238 } else {
1239 /* Cut 2.0 */
1240 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
1241 goto err;
1242 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1243 goto err;
1244 }
1245 return 0;
1246err:
1247 dprintk(FE_ERROR, 1, "I/O error");
1248 return -1;
1249}
1250
Manu Abrahame415c682009-04-06 15:45:20 -03001251static int stv090x_delivery_search(struct stv090x_state *state)
1252{
1253 u32 reg;
1254
1255 switch (state->search_mode) {
1256 case STV090x_SEARCH_DVBS1:
1257 case STV090x_SEARCH_DSS:
1258 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1259 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1260 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1261 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1262 goto err;
1263
Manu Abraham27d40322009-05-02 18:26:58 -03001264 /* Activate Viterbi decoder in legacy search,
1265 * do not use FRESVIT1, might impact VITERBI2
1266 */
Manu Abrahame415c682009-04-06 15:45:20 -03001267 if (stv090x_vitclk_ctl(state, 0) < 0)
1268 goto err;
1269
Manu Abraham27d40322009-05-02 18:26:58 -03001270 if (stv090x_dvbs_track_crl(state) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001271 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001272
Manu Abrahame415c682009-04-06 15:45:20 -03001273 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x22) < 0) /* disable DVB-S2 */
1274 goto err;
1275
Manu Abraham27d40322009-05-02 18:26:58 -03001276 if (stv090x_set_vit_thacq(state) < 0)
1277 goto err;
1278 if (stv090x_set_viterbi(state) < 0)
1279 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001280 break;
1281
1282 case STV090x_SEARCH_DVBS2:
1283 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1284 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
1285 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1286 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1287 goto err;
1288 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1289 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
1290 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1291 goto err;
1292
1293 if (stv090x_vitclk_ctl(state, 1) < 0)
1294 goto err;
1295
1296 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0) /* stop DVB-S CR loop */
1297 goto err;
1298 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1299 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001300
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001301 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001302 /* enable S2 carrier loop */
1303 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1304 goto err;
1305 } else {
1306 /* > Cut 3: Stop carrier 3 */
1307 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1308 goto err;
1309 }
Manu Abrahame415c682009-04-06 15:45:20 -03001310
1311 if (state->demod_mode != STV090x_SINGLE) {
Manu Abraham27d40322009-05-02 18:26:58 -03001312 /* Cut 2: enable link during search */
1313 if (stv090x_activate_modcod(state) < 0)
1314 goto err;
1315 } else {
1316 /* Single demodulator
1317 * Authorize SHORT and LONG frames,
1318 * QPSK, 8PSK, 16APSK and 32APSK
1319 */
1320 if (stv090x_activate_modcod_single(state) < 0)
1321 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001322 }
Manu Abraham27d40322009-05-02 18:26:58 -03001323
Andreas Regel7b035da2009-11-13 18:20:54 -03001324 if (stv090x_set_vit_thtracq(state) < 0)
1325 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001326 break;
1327
1328 case STV090x_SEARCH_AUTO:
1329 default:
Manu Abraham27d40322009-05-02 18:26:58 -03001330 /* enable DVB-S2 and DVB-S2 in Auto MODE */
Manu Abrahame415c682009-04-06 15:45:20 -03001331 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
Andreas Regelb79c6df2010-01-05 19:18:52 -03001332 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
1333 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1334 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1335 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001336 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1337 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
1338 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1339 goto err;
1340
Andreas Regel4e58a682009-04-16 08:40:36 -03001341 if (stv090x_vitclk_ctl(state, 0) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001342 goto err;
1343
Manu Abraham27d40322009-05-02 18:26:58 -03001344 if (stv090x_dvbs_track_crl(state) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001345 goto err;
1346
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001347 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001348 /* enable S2 carrier loop */
1349 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1350 goto err;
1351 } else {
1352 /* > Cut 3: Stop carrier 3 */
1353 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1354 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001355 }
Manu Abraham27d40322009-05-02 18:26:58 -03001356
1357 if (state->demod_mode != STV090x_SINGLE) {
1358 /* Cut 2: enable link during search */
1359 if (stv090x_activate_modcod(state) < 0)
1360 goto err;
1361 } else {
1362 /* Single demodulator
1363 * Authorize SHORT and LONG frames,
1364 * QPSK, 8PSK, 16APSK and 32APSK
1365 */
1366 if (stv090x_activate_modcod_single(state) < 0)
1367 goto err;
1368 }
1369
Andreas Regel7b035da2009-11-13 18:20:54 -03001370 if (stv090x_set_vit_thacq(state) < 0)
1371 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001372
1373 if (stv090x_set_viterbi(state) < 0)
1374 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001375 break;
1376 }
1377 return 0;
1378err:
1379 dprintk(FE_ERROR, 1, "I/O error");
1380 return -1;
1381}
1382
1383static int stv090x_start_search(struct stv090x_state *state)
1384{
Manu Abraham27d40322009-05-02 18:26:58 -03001385 u32 reg, freq_abs;
1386 s16 freq;
Manu Abrahame415c682009-04-06 15:45:20 -03001387
Manu Abraham27d40322009-05-02 18:26:58 -03001388 /* Reset demodulator */
Manu Abrahame415c682009-04-06 15:45:20 -03001389 reg = STV090x_READ_DEMOD(state, DMDISTATE);
1390 STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f);
1391 if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
1392 goto err;
1393
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001394 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001395 if (state->srate <= 5000000) {
1396 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0)
1397 goto err;
1398 if (STV090x_WRITE_DEMOD(state, CFRUP1, 0x0f) < 0)
1399 goto err;
Andreas Regelb671a8d2009-11-13 18:15:27 -03001400 if (STV090x_WRITE_DEMOD(state, CFRUP0, 0xff) < 0)
Manu Abraham27d40322009-05-02 18:26:58 -03001401 goto err;
1402 if (STV090x_WRITE_DEMOD(state, CFRLOW1, 0xf0) < 0)
1403 goto err;
1404 if (STV090x_WRITE_DEMOD(state, CFRLOW0, 0x00) < 0)
1405 goto err;
1406
1407 /*enlarge the timing bandwith for Low SR*/
1408 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0)
1409 goto err;
1410 } else {
1411 /* If the symbol rate is >5 Msps
1412 Set The carrier search up and low to auto mode */
1413 if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
1414 goto err;
1415 /*reduce the timing bandwith for high SR*/
1416 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
1417 goto err;
1418 }
1419 } else {
1420 /* >= Cut 3 */
1421 if (state->srate <= 5000000) {
1422 /* enlarge the timing bandwith for Low SR */
1423 STV090x_WRITE_DEMOD(state, RTCS2, 0x68);
1424 } else {
1425 /* reduce timing bandwith for high SR */
1426 STV090x_WRITE_DEMOD(state, RTCS2, 0x44);
1427 }
1428
1429 /* Set CFR min and max to manual mode */
1430 STV090x_WRITE_DEMOD(state, CARCFG, 0x46);
1431
1432 if (state->algo == STV090x_WARM_SEARCH) {
1433 /* WARM Start
1434 * CFR min = -1MHz,
1435 * CFR max = +1MHz
1436 */
1437 freq_abs = 1000 << 16;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001438 freq_abs /= (state->internal->mclk / 1000);
Manu Abraham27d40322009-05-02 18:26:58 -03001439 freq = (s16) freq_abs;
1440 } else {
1441 /* COLD Start
1442 * CFR min =- (SearchRange / 2 + 600KHz)
1443 * CFR max = +(SearchRange / 2 + 600KHz)
1444 * (600KHz for the tuner step size)
1445 */
1446 freq_abs = (state->search_range / 2000) + 600;
1447 freq_abs = freq_abs << 16;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001448 freq_abs /= (state->internal->mclk / 1000);
Manu Abraham27d40322009-05-02 18:26:58 -03001449 freq = (s16) freq_abs;
1450 }
1451
1452 if (STV090x_WRITE_DEMOD(state, CFRUP1, MSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001453 goto err;
Andreas Regelb671a8d2009-11-13 18:15:27 -03001454 if (STV090x_WRITE_DEMOD(state, CFRUP0, LSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001455 goto err;
1456
Manu Abraham27d40322009-05-02 18:26:58 -03001457 freq *= -1;
1458
1459 if (STV090x_WRITE_DEMOD(state, CFRLOW1, MSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001460 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001461 if (STV090x_WRITE_DEMOD(state, CFRLOW0, LSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001462 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001463
Manu Abrahame415c682009-04-06 15:45:20 -03001464 }
Manu Abraham27d40322009-05-02 18:26:58 -03001465
Manu Abrahame415c682009-04-06 15:45:20 -03001466 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0) < 0)
1467 goto err;
1468 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0) < 0)
1469 goto err;
1470
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001471 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03001472 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1473 goto err;
1474 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
1475 goto err;
1476
1477 if ((state->search_mode == STV090x_DVBS1) ||
1478 (state->search_mode == STV090x_DSS) ||
1479 (state->search_mode == STV090x_SEARCH_AUTO)) {
1480
1481 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
1482 goto err;
1483 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0)
1484 goto err;
1485 }
1486 }
1487
1488 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00) < 0)
1489 goto err;
1490 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xe0) < 0)
1491 goto err;
1492 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xc0) < 0)
1493 goto err;
1494
1495 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1496 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
1497 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
1498 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1499 goto err;
1500 reg = STV090x_READ_DEMOD(state, DMDCFG2);
1501 STV090x_SETFIELD_Px(reg, S1S2_SEQUENTIAL_FIELD, 0x0);
1502 if (STV090x_WRITE_DEMOD(state, DMDCFG2, reg) < 0)
1503 goto err;
1504
Andreas Regel7b035da2009-11-13 18:20:54 -03001505 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0)
1506 goto err;
1507
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001508 if (state->internal->dev_ver >= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001509 /*Frequency offset detector setting*/
1510 if (state->srate < 2000000) {
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001511 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001512 /* Cut 2 */
1513 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x39) < 0)
1514 goto err;
1515 } else {
Andreas Regel7b035da2009-11-13 18:20:54 -03001516 /* Cut 3 */
Manu Abraham27d40322009-05-02 18:26:58 -03001517 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x89) < 0)
1518 goto err;
1519 }
1520 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x40) < 0)
1521 goto err;
Andreas Regela4978a82009-11-13 18:17:45 -03001522 } else if (state->srate < 10000000) {
Manu Abrahame415c682009-04-06 15:45:20 -03001523 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4c) < 0)
1524 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001525 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1526 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001527 } else {
1528 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4b) < 0)
1529 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001530 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1531 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001532 }
1533 } else {
1534 if (state->srate < 10000000) {
1535 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
1536 goto err;
1537 } else {
1538 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
1539 goto err;
1540 }
1541 }
1542
1543 switch (state->algo) {
Manu Abraham27d40322009-05-02 18:26:58 -03001544 case STV090x_WARM_SEARCH:
1545 /* The symbol rate and the exact
1546 * carrier Frequency are known
1547 */
Manu Abrahame415c682009-04-06 15:45:20 -03001548 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
1549 goto err;
1550 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
1551 goto err;
1552 break;
1553
Manu Abraham27d40322009-05-02 18:26:58 -03001554 case STV090x_COLD_SEARCH:
1555 /* The symbol rate is known */
Manu Abrahame415c682009-04-06 15:45:20 -03001556 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
1557 goto err;
1558 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
1559 goto err;
1560 break;
1561
1562 default:
1563 break;
1564 }
1565 return 0;
1566err:
1567 dprintk(FE_ERROR, 1, "I/O error");
1568 return -1;
1569}
1570
1571static int stv090x_get_agc2_min_level(struct stv090x_state *state)
1572{
Andreas Regelb4a42482009-11-13 18:16:44 -03001573 u32 agc2_min = 0xffff, agc2 = 0, freq_init, freq_step, reg;
Manu Abrahame415c682009-04-06 15:45:20 -03001574 s32 i, j, steps, dir;
1575
1576 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1577 goto err;
1578 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
Andreas Regel7b035da2009-11-13 18:20:54 -03001579 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
1580 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
Manu Abrahame415c682009-04-06 15:45:20 -03001581 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1582 goto err;
1583
1584 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0) /* SR = 65 Msps Max */
1585 goto err;
1586 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1587 goto err;
1588 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0) /* SR= 400 ksps Min */
1589 goto err;
1590 if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1591 goto err;
1592 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0) /* stop acq @ coarse carrier state */
1593 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001594 if (stv090x_set_srate(state, 1000000) < 0)
1595 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001596
Andreas Regel7b035da2009-11-13 18:20:54 -03001597 steps = state->search_range / 1000000;
1598 if (steps <= 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001599 steps = 1;
1600
1601 dir = 1;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001602 freq_step = (1000000 * 256) / (state->internal->mclk / 256);
Manu Abrahame415c682009-04-06 15:45:20 -03001603 freq_init = 0;
1604
1605 for (i = 0; i < steps; i++) {
1606 if (dir > 0)
1607 freq_init = freq_init + (freq_step * i);
1608 else
1609 freq_init = freq_init - (freq_step * i);
1610
Andreas Regelb671a8d2009-11-13 18:15:27 -03001611 dir *= -1;
Manu Abrahame415c682009-04-06 15:45:20 -03001612
1613 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod RESET */
1614 goto err;
1615 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_init >> 8) & 0xff) < 0)
1616 goto err;
1617 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_init & 0xff) < 0)
1618 goto err;
1619 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x58) < 0) /* Demod RESET */
1620 goto err;
1621 msleep(10);
Andreas Regelb4a42482009-11-13 18:16:44 -03001622
1623 agc2 = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03001624 for (j = 0; j < 10; j++) {
Andreas Regelb4a42482009-11-13 18:16:44 -03001625 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1626 STV090x_READ_DEMOD(state, AGC2I0);
Manu Abrahame415c682009-04-06 15:45:20 -03001627 }
1628 agc2 /= 10;
Andreas Regelb4a42482009-11-13 18:16:44 -03001629 if (agc2 < agc2_min)
Manu Abrahame415c682009-04-06 15:45:20 -03001630 agc2_min = agc2;
1631 }
1632
1633 return agc2_min;
1634err:
1635 dprintk(FE_ERROR, 1, "I/O error");
1636 return -1;
1637}
1638
1639static u32 stv090x_get_srate(struct stv090x_state *state, u32 clk)
1640{
1641 u8 r3, r2, r1, r0;
1642 s32 srate, int_1, int_2, tmp_1, tmp_2;
Manu Abrahame415c682009-04-06 15:45:20 -03001643
1644 r3 = STV090x_READ_DEMOD(state, SFR3);
1645 r2 = STV090x_READ_DEMOD(state, SFR2);
1646 r1 = STV090x_READ_DEMOD(state, SFR1);
1647 r0 = STV090x_READ_DEMOD(state, SFR0);
1648
1649 srate = ((r3 << 24) | (r2 << 16) | (r1 << 8) | r0);
1650
Manu Abrahamf430fff2009-04-08 20:18:50 -03001651 int_1 = clk >> 16;
1652 int_2 = srate >> 16;
Manu Abrahame415c682009-04-06 15:45:20 -03001653
Manu Abrahamf430fff2009-04-08 20:18:50 -03001654 tmp_1 = clk % 0x10000;
1655 tmp_2 = srate % 0x10000;
Manu Abrahame415c682009-04-06 15:45:20 -03001656
1657 srate = (int_1 * int_2) +
Manu Abrahamf430fff2009-04-08 20:18:50 -03001658 ((int_1 * tmp_2) >> 16) +
1659 ((int_2 * tmp_1) >> 16);
Manu Abrahame415c682009-04-06 15:45:20 -03001660
1661 return srate;
1662}
1663
1664static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
1665{
1666 struct dvb_frontend *fe = &state->frontend;
1667
1668 int tmg_lock = 0, i;
1669 s32 tmg_cpt = 0, dir = 1, steps, cur_step = 0, freq;
1670 u32 srate_coarse = 0, agc2 = 0, car_step = 1200, reg;
Andreas Regelb4a42482009-11-13 18:16:44 -03001671 u32 agc2th;
1672
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001673 if (state->internal->dev_ver >= 0x30)
Andreas Regelb4a42482009-11-13 18:16:44 -03001674 agc2th = 0x2e00;
1675 else
1676 agc2th = 0x1f00;
Manu Abrahame415c682009-04-06 15:45:20 -03001677
1678 reg = STV090x_READ_DEMOD(state, DMDISTATE);
1679 STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f); /* Demod RESET */
1680 if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
1681 goto err;
1682 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0x12) < 0)
1683 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001684 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0)
1685 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001686 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xf0) < 0)
1687 goto err;
1688 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xe0) < 0)
1689 goto err;
1690 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1691 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 1);
Andreas Regel7b035da2009-11-13 18:20:54 -03001692 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
Manu Abrahame415c682009-04-06 15:45:20 -03001693 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1694 goto err;
1695
1696 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0)
1697 goto err;
1698 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1699 goto err;
1700 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0)
1701 goto err;
1702 if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1703 goto err;
1704 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0)
1705 goto err;
Andreas Regelb4a42482009-11-13 18:16:44 -03001706 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x50) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001707 goto err;
1708
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001709 if (state->internal->dev_ver >= 0x30) {
Manu Abraham27d40322009-05-02 18:26:58 -03001710 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x99) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001711 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001712 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x98) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001713 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001714
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001715 } else if (state->internal->dev_ver >= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001716 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001717 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001718 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001719 goto err;
1720 }
1721
1722 if (state->srate <= 2000000)
1723 car_step = 1000;
1724 else if (state->srate <= 5000000)
1725 car_step = 2000;
1726 else if (state->srate <= 12000000)
1727 car_step = 3000;
1728 else
1729 car_step = 5000;
1730
1731 steps = -1 + ((state->search_range / 1000) / car_step);
1732 steps /= 2;
1733 steps = (2 * steps) + 1;
1734 if (steps < 0)
1735 steps = 1;
1736 else if (steps > 10) {
1737 steps = 11;
1738 car_step = (state->search_range / 1000) / 10;
1739 }
1740 cur_step = 0;
1741 dir = 1;
1742 freq = state->frequency;
1743
1744 while ((!tmg_lock) && (cur_step < steps)) {
1745 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5f) < 0) /* Demod RESET */
1746 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001747 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
1748 goto err;
1749 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
1750 goto err;
1751 if (STV090x_WRITE_DEMOD(state, SFRINIT1, 0x00) < 0)
1752 goto err;
1753 if (STV090x_WRITE_DEMOD(state, SFRINIT0, 0x00) < 0)
1754 goto err;
1755 /* trigger acquisition */
1756 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x40) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001757 goto err;
1758 msleep(50);
1759 for (i = 0; i < 10; i++) {
1760 reg = STV090x_READ_DEMOD(state, DSTATUS);
1761 if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
1762 tmg_cpt++;
Andreas Regelb4a42482009-11-13 18:16:44 -03001763 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1764 STV090x_READ_DEMOD(state, AGC2I0);
Manu Abrahame415c682009-04-06 15:45:20 -03001765 }
1766 agc2 /= 10;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001767 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
Manu Abrahame415c682009-04-06 15:45:20 -03001768 cur_step++;
1769 dir *= -1;
Andreas Regelb4a42482009-11-13 18:16:44 -03001770 if ((tmg_cpt >= 5) && (agc2 < agc2th) &&
1771 (srate_coarse < 50000000) && (srate_coarse > 850000))
Manu Abrahame415c682009-04-06 15:45:20 -03001772 tmg_lock = 1;
1773 else if (cur_step < steps) {
1774 if (dir > 0)
1775 freq += cur_step * car_step;
1776 else
1777 freq -= cur_step * car_step;
1778
1779 /* Setup tuner */
Manu Abraham27d40322009-05-02 18:26:58 -03001780 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
1781 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001782
Manu Abraham27d40322009-05-02 18:26:58 -03001783 if (state->config->tuner_set_frequency) {
Andreas Regela4978a82009-11-13 18:17:45 -03001784 if (state->config->tuner_set_frequency(fe, freq) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03001785 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03001786 }
Manu Abrahame415c682009-04-06 15:45:20 -03001787
Manu Abraham27d40322009-05-02 18:26:58 -03001788 if (state->config->tuner_set_bandwidth) {
1789 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03001790 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03001791 }
Manu Abrahame415c682009-04-06 15:45:20 -03001792
Manu Abraham27d40322009-05-02 18:26:58 -03001793 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
1794 goto err;
1795
Manu Abrahame415c682009-04-06 15:45:20 -03001796 msleep(50);
Manu Abrahame415c682009-04-06 15:45:20 -03001797
Manu Abraham27d40322009-05-02 18:26:58 -03001798 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
1799 goto err;
1800
1801 if (state->config->tuner_get_status) {
1802 if (state->config->tuner_get_status(fe, &reg) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03001803 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03001804 }
Manu Abrahame415c682009-04-06 15:45:20 -03001805
1806 if (reg)
1807 dprintk(FE_DEBUG, 1, "Tuner phase locked");
1808 else
1809 dprintk(FE_DEBUG, 1, "Tuner unlocked");
1810
Manu Abraham27d40322009-05-02 18:26:58 -03001811 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
1812 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001813
1814 }
1815 }
1816 if (!tmg_lock)
1817 srate_coarse = 0;
1818 else
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001819 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
Manu Abrahame415c682009-04-06 15:45:20 -03001820
1821 return srate_coarse;
Oliver Endriss2c1f7502010-01-10 15:38:38 -03001822
1823err_gateoff:
1824 stv090x_i2c_gate_ctrl(fe, 0);
Manu Abrahame415c682009-04-06 15:45:20 -03001825err:
1826 dprintk(FE_ERROR, 1, "I/O error");
1827 return -1;
1828}
1829
1830static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
1831{
1832 u32 srate_coarse, freq_coarse, sym, reg;
1833
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001834 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
Manu Abrahame415c682009-04-06 15:45:20 -03001835 freq_coarse = STV090x_READ_DEMOD(state, CFR2) << 8;
1836 freq_coarse |= STV090x_READ_DEMOD(state, CFR1);
1837 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1838
1839 if (sym < state->srate)
1840 srate_coarse = 0;
1841 else {
1842 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0) /* Demod RESET */
1843 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001844 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001845 goto err;
1846 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
1847 goto err;
1848 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
1849 goto err;
1850 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
1851 goto err;
1852 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1853 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
1854 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1855 goto err;
1856
Andreas Regelb4a42482009-11-13 18:16:44 -03001857 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1858 goto err;
1859
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001860 if (state->internal->dev_ver >= 0x30) {
Manu Abraham27d40322009-05-02 18:26:58 -03001861 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x79) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001862 goto err;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001863 } else if (state->internal->dev_ver >= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001864 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001865 goto err;
1866 }
1867
1868 if (srate_coarse > 3000000) {
1869 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1870 sym = (sym / 1000) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001871 sym /= (state->internal->mclk / 1000);
Manu Abrahame415c682009-04-06 15:45:20 -03001872 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1873 goto err;
1874 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1875 goto err;
1876 sym = 10 * (srate_coarse / 13); /* SFRLOW = SFR - 30% */
1877 sym = (sym / 1000) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001878 sym /= (state->internal->mclk / 1000);
Manu Abrahame415c682009-04-06 15:45:20 -03001879 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1880 goto err;
1881 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1882 goto err;
1883 sym = (srate_coarse / 1000) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001884 sym /= (state->internal->mclk / 1000);
Manu Abrahame415c682009-04-06 15:45:20 -03001885 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1886 goto err;
1887 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
1888 goto err;
1889 } else {
1890 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1891 sym = (sym / 100) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001892 sym /= (state->internal->mclk / 100);
Manu Abrahame415c682009-04-06 15:45:20 -03001893 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1894 goto err;
1895 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1896 goto err;
1897 sym = 10 * (srate_coarse / 14); /* SFRLOW = SFR - 30% */
1898 sym = (sym / 100) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001899 sym /= (state->internal->mclk / 100);
Manu Abrahame415c682009-04-06 15:45:20 -03001900 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1901 goto err;
1902 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1903 goto err;
1904 sym = (srate_coarse / 100) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001905 sym /= (state->internal->mclk / 100);
Manu Abrahame415c682009-04-06 15:45:20 -03001906 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1907 goto err;
1908 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
1909 goto err;
1910 }
1911 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
1912 goto err;
1913 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_coarse >> 8) & 0xff) < 0)
1914 goto err;
1915 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_coarse & 0xff) < 0)
1916 goto err;
1917 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0) /* trigger acquisition */
1918 goto err;
1919 }
1920
1921 return srate_coarse;
1922
1923err:
1924 dprintk(FE_ERROR, 1, "I/O error");
1925 return -1;
1926}
1927
1928static int stv090x_get_dmdlock(struct stv090x_state *state, s32 timeout)
1929{
1930 s32 timer = 0, lock = 0;
1931 u32 reg;
1932 u8 stat;
1933
1934 while ((timer < timeout) && (!lock)) {
1935 reg = STV090x_READ_DEMOD(state, DMDSTATE);
1936 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
1937
1938 switch (stat) {
1939 case 0: /* searching */
1940 case 1: /* first PLH detected */
1941 default:
1942 dprintk(FE_DEBUG, 1, "Demodulator searching ..");
1943 lock = 0;
1944 break;
1945 case 2: /* DVB-S2 mode */
1946 case 3: /* DVB-S1/legacy mode */
1947 reg = STV090x_READ_DEMOD(state, DSTATUS);
1948 lock = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
1949 break;
1950 }
1951
1952 if (!lock)
1953 msleep(10);
1954 else
1955 dprintk(FE_DEBUG, 1, "Demodulator acquired LOCK");
1956
1957 timer += 10;
1958 }
1959 return lock;
1960}
1961
1962static int stv090x_blind_search(struct stv090x_state *state)
1963{
1964 u32 agc2, reg, srate_coarse;
Andreas Regela4978a82009-11-13 18:17:45 -03001965 s32 cpt_fail, agc2_ovflw, i;
Manu Abrahame415c682009-04-06 15:45:20 -03001966 u8 k_ref, k_max, k_min;
1967 int coarse_fail, lock;
1968
Andreas Regel7b035da2009-11-13 18:20:54 -03001969 k_max = 110;
1970 k_min = 10;
Manu Abrahame415c682009-04-06 15:45:20 -03001971
1972 agc2 = stv090x_get_agc2_min_level(state);
1973
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001974 if (agc2 > STV090x_SEARCH_AGC2_TH(state->internal->dev_ver)) {
Manu Abrahame415c682009-04-06 15:45:20 -03001975 lock = 0;
1976 } else {
Manu Abraham27d40322009-05-02 18:26:58 -03001977
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001978 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001979 if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001980 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001981 } else {
1982 /* > Cut 3 */
1983 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x06) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001984 goto err;
1985 }
1986
Manu Abrahame415c682009-04-06 15:45:20 -03001987 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
1988 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001989
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001990 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03001991 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1992 goto err;
1993 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
1994 goto err;
1995 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
1996 goto err;
1997 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0) /* set viterbi hysteresis */
1998 goto err;
1999 }
2000
2001 k_ref = k_max;
2002 do {
2003 if (STV090x_WRITE_DEMOD(state, KREFTMG, k_ref) < 0)
2004 goto err;
2005 if (stv090x_srate_srch_coarse(state) != 0) {
2006 srate_coarse = stv090x_srate_srch_fine(state);
2007 if (srate_coarse != 0) {
2008 stv090x_get_lock_tmg(state);
Andreas Regela4978a82009-11-13 18:17:45 -03002009 lock = stv090x_get_dmdlock(state,
2010 state->DemodTimeout);
Manu Abrahame415c682009-04-06 15:45:20 -03002011 } else {
2012 lock = 0;
2013 }
2014 } else {
2015 cpt_fail = 0;
2016 agc2_ovflw = 0;
2017 for (i = 0; i < 10; i++) {
Andreas Regelb4a42482009-11-13 18:16:44 -03002018 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
2019 STV090x_READ_DEMOD(state, AGC2I0);
Manu Abrahame415c682009-04-06 15:45:20 -03002020 if (agc2 >= 0xff00)
2021 agc2_ovflw++;
2022 reg = STV090x_READ_DEMOD(state, DSTATUS2);
2023 if ((STV090x_GETFIELD_Px(reg, CFR_OVERFLOW_FIELD) == 0x01) &&
2024 (STV090x_GETFIELD_Px(reg, DEMOD_DELOCK_FIELD) == 0x01))
2025
2026 cpt_fail++;
2027 }
2028 if ((cpt_fail > 7) || (agc2_ovflw > 7))
2029 coarse_fail = 1;
2030
2031 lock = 0;
2032 }
Andreas Regel7b035da2009-11-13 18:20:54 -03002033 k_ref -= 20;
Manu Abrahame415c682009-04-06 15:45:20 -03002034 } while ((k_ref >= k_min) && (!lock) && (!coarse_fail));
2035 }
2036
2037 return lock;
2038
2039err:
2040 dprintk(FE_ERROR, 1, "I/O error");
2041 return -1;
2042}
2043
2044static int stv090x_chk_tmg(struct stv090x_state *state)
2045{
2046 u32 reg;
Manu Abraham27d40322009-05-02 18:26:58 -03002047 s32 tmg_cpt = 0, i;
Manu Abrahame415c682009-04-06 15:45:20 -03002048 u8 freq, tmg_thh, tmg_thl;
2049 int tmg_lock;
2050
2051 freq = STV090x_READ_DEMOD(state, CARFREQ);
2052 tmg_thh = STV090x_READ_DEMOD(state, TMGTHRISE);
2053 tmg_thl = STV090x_READ_DEMOD(state, TMGTHFALL);
2054 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
2055 goto err;
2056 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
2057 goto err;
2058
2059 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2060 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00); /* stop carrier offset search */
2061 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2062 goto err;
2063 if (STV090x_WRITE_DEMOD(state, RTC, 0x80) < 0)
2064 goto err;
2065
2066 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x40) < 0)
2067 goto err;
2068 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x00) < 0)
2069 goto err;
2070
2071 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0) /* set car ofset to 0 */
2072 goto err;
2073 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
2074 goto err;
2075 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x65) < 0)
2076 goto err;
2077
2078 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* trigger acquisition */
2079 goto err;
2080 msleep(10);
2081
2082 for (i = 0; i < 10; i++) {
2083 reg = STV090x_READ_DEMOD(state, DSTATUS);
2084 if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
2085 tmg_cpt++;
2086 msleep(1);
2087 }
2088 if (tmg_cpt >= 3)
2089 tmg_lock = 1;
2090
2091 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
2092 goto err;
2093 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0) /* DVB-S1 timing */
2094 goto err;
2095 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0) /* DVB-S2 timing */
2096 goto err;
2097
2098 if (STV090x_WRITE_DEMOD(state, CARFREQ, freq) < 0)
2099 goto err;
2100 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, tmg_thh) < 0)
2101 goto err;
2102 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, tmg_thl) < 0)
2103 goto err;
2104
2105 return tmg_lock;
2106
2107err:
2108 dprintk(FE_ERROR, 1, "I/O error");
2109 return -1;
2110}
2111
2112static int stv090x_get_coldlock(struct stv090x_state *state, s32 timeout_dmd)
2113{
2114 struct dvb_frontend *fe = &state->frontend;
2115
2116 u32 reg;
2117 s32 car_step, steps, cur_step, dir, freq, timeout_lock;
2118 int lock = 0;
2119
2120 if (state->srate >= 10000000)
2121 timeout_lock = timeout_dmd / 3;
2122 else
2123 timeout_lock = timeout_dmd / 2;
2124
2125 lock = stv090x_get_dmdlock(state, timeout_lock); /* cold start wait */
2126 if (!lock) {
2127 if (state->srate >= 10000000) {
2128 if (stv090x_chk_tmg(state)) {
2129 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2130 goto err;
2131 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2132 goto err;
2133 lock = stv090x_get_dmdlock(state, timeout_dmd);
2134 } else {
2135 lock = 0;
2136 }
2137 } else {
2138 if (state->srate <= 4000000)
2139 car_step = 1000;
2140 else if (state->srate <= 7000000)
2141 car_step = 2000;
2142 else if (state->srate <= 10000000)
2143 car_step = 3000;
2144 else
2145 car_step = 5000;
2146
2147 steps = (state->search_range / 1000) / car_step;
2148 steps /= 2;
2149 steps = 2 * (steps + 1);
2150 if (steps < 0)
2151 steps = 2;
2152 else if (steps > 12)
2153 steps = 12;
2154
2155 cur_step = 1;
2156 dir = 1;
2157
2158 if (!lock) {
2159 freq = state->frequency;
2160 state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + state->srate;
2161 while ((cur_step <= steps) && (!lock)) {
2162 if (dir > 0)
2163 freq += cur_step * car_step;
2164 else
2165 freq -= cur_step * car_step;
2166
2167 /* Setup tuner */
Manu Abraham27d40322009-05-02 18:26:58 -03002168 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2169 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002170
Manu Abraham27d40322009-05-02 18:26:58 -03002171 if (state->config->tuner_set_frequency) {
Andreas Regela4978a82009-11-13 18:17:45 -03002172 if (state->config->tuner_set_frequency(fe, freq) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03002173 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03002174 }
Manu Abrahame415c682009-04-06 15:45:20 -03002175
Manu Abraham27d40322009-05-02 18:26:58 -03002176 if (state->config->tuner_set_bandwidth) {
2177 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03002178 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03002179 }
Manu Abrahame415c682009-04-06 15:45:20 -03002180
Manu Abraham27d40322009-05-02 18:26:58 -03002181 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2182 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002183
2184 msleep(50);
2185
Manu Abraham27d40322009-05-02 18:26:58 -03002186 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2187 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002188
Manu Abraham27d40322009-05-02 18:26:58 -03002189 if (state->config->tuner_get_status) {
2190 if (state->config->tuner_get_status(fe, &reg) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03002191 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03002192 }
Manu Abrahame415c682009-04-06 15:45:20 -03002193
2194 if (reg)
2195 dprintk(FE_DEBUG, 1, "Tuner phase locked");
2196 else
2197 dprintk(FE_DEBUG, 1, "Tuner unlocked");
2198
Manu Abraham27d40322009-05-02 18:26:58 -03002199 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2200 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002201
2202 STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c);
Manu Abrahame415c682009-04-06 15:45:20 -03002203 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
2204 goto err;
2205 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
2206 goto err;
2207 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2208 goto err;
2209 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2210 goto err;
2211 lock = stv090x_get_dmdlock(state, (timeout_dmd / 3));
2212
2213 dir *= -1;
2214 cur_step++;
2215 }
2216 }
2217 }
2218 }
2219
2220 return lock;
2221
Oliver Endriss2c1f7502010-01-10 15:38:38 -03002222err_gateoff:
2223 stv090x_i2c_gate_ctrl(fe, 0);
Manu Abrahame415c682009-04-06 15:45:20 -03002224err:
2225 dprintk(FE_ERROR, 1, "I/O error");
2226 return -1;
2227}
2228
2229static int stv090x_get_loop_params(struct stv090x_state *state, s32 *freq_inc, s32 *timeout_sw, s32 *steps)
2230{
2231 s32 timeout, inc, steps_max, srate, car_max;
2232
2233 srate = state->srate;
2234 car_max = state->search_range / 1000;
Andreas Regel2f5914b2009-04-23 14:56:41 -03002235 car_max += car_max / 10;
Manu Abrahame415c682009-04-06 15:45:20 -03002236 car_max = 65536 * (car_max / 2);
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002237 car_max /= (state->internal->mclk / 1000);
Manu Abrahame415c682009-04-06 15:45:20 -03002238
2239 if (car_max > 0x4000)
2240 car_max = 0x4000 ; /* maxcarrier should be<= +-1/4 Mclk */
2241
2242 inc = srate;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002243 inc /= state->internal->mclk / 1000;
Manu Abrahame415c682009-04-06 15:45:20 -03002244 inc *= 256;
2245 inc *= 256;
2246 inc /= 1000;
2247
Andreas Regel72982f72009-04-08 17:28:41 -03002248 switch (state->search_mode) {
Manu Abrahame415c682009-04-06 15:45:20 -03002249 case STV090x_SEARCH_DVBS1:
2250 case STV090x_SEARCH_DSS:
2251 inc *= 3; /* freq step = 3% of srate */
2252 timeout = 20;
2253 break;
2254
2255 case STV090x_SEARCH_DVBS2:
2256 inc *= 4;
2257 timeout = 25;
2258 break;
2259
2260 case STV090x_SEARCH_AUTO:
2261 default:
2262 inc *= 3;
2263 timeout = 25;
2264 break;
2265 }
2266 inc /= 100;
2267 if ((inc > car_max) || (inc < 0))
2268 inc = car_max / 2; /* increment <= 1/8 Mclk */
2269
2270 timeout *= 27500; /* 27.5 Msps reference */
2271 if (srate > 0)
2272 timeout /= (srate / 1000);
2273
2274 if ((timeout > 100) || (timeout < 0))
2275 timeout = 100;
2276
2277 steps_max = (car_max / inc) + 1; /* min steps = 3 */
2278 if ((steps_max > 100) || (steps_max < 0)) {
2279 steps_max = 100; /* max steps <= 100 */
2280 inc = car_max / steps_max;
2281 }
2282 *freq_inc = inc;
2283 *timeout_sw = timeout;
2284 *steps = steps_max;
2285
2286 return 0;
2287}
2288
2289static int stv090x_chk_signal(struct stv090x_state *state)
2290{
2291 s32 offst_car, agc2, car_max;
2292 int no_signal;
2293
2294 offst_car = STV090x_READ_DEMOD(state, CFR2) << 8;
2295 offst_car |= STV090x_READ_DEMOD(state, CFR1);
Andreas Regel2f5914b2009-04-23 14:56:41 -03002296 offst_car = comp2(offst_car, 16);
Manu Abrahame415c682009-04-06 15:45:20 -03002297
2298 agc2 = STV090x_READ_DEMOD(state, AGC2I1) << 8;
2299 agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
2300 car_max = state->search_range / 1000;
2301
2302 car_max += (car_max / 10); /* 10% margin */
2303 car_max = (65536 * car_max / 2);
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002304 car_max /= state->internal->mclk / 1000;
Manu Abrahame415c682009-04-06 15:45:20 -03002305
2306 if (car_max > 0x4000)
2307 car_max = 0x4000;
2308
2309 if ((agc2 > 0x2000) || (offst_car > 2 * car_max) || (offst_car < -2 * car_max)) {
2310 no_signal = 1;
2311 dprintk(FE_DEBUG, 1, "No Signal");
2312 } else {
2313 no_signal = 0;
2314 dprintk(FE_DEBUG, 1, "Found Signal");
2315 }
2316
2317 return no_signal;
2318}
2319
2320static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 timeout, int zigzag, s32 steps_max)
2321{
2322 int no_signal, lock = 0;
Manu Abraham27d40322009-05-02 18:26:58 -03002323 s32 cpt_step = 0, offst_freq, car_max;
Manu Abrahame415c682009-04-06 15:45:20 -03002324 u32 reg;
2325
2326 car_max = state->search_range / 1000;
2327 car_max += (car_max / 10);
2328 car_max = (65536 * car_max / 2);
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002329 car_max /= (state->internal->mclk / 1000);
Manu Abrahame415c682009-04-06 15:45:20 -03002330 if (car_max > 0x4000)
2331 car_max = 0x4000;
2332
2333 if (zigzag)
2334 offst_freq = 0;
2335 else
2336 offst_freq = -car_max + inc;
2337
Manu Abrahame415c682009-04-06 15:45:20 -03002338 do {
2339 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0)
2340 goto err;
2341 if (STV090x_WRITE_DEMOD(state, CFRINIT1, ((offst_freq / 256) & 0xff)) < 0)
2342 goto err;
2343 if (STV090x_WRITE_DEMOD(state, CFRINIT0, offst_freq & 0xff) < 0)
2344 goto err;
2345 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
2346 goto err;
2347
2348 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
2349 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x1); /* stop DVB-S2 packet delin */
2350 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
2351 goto err;
2352
Manu Abrahame415c682009-04-06 15:45:20 -03002353 if (zigzag) {
2354 if (offst_freq >= 0)
2355 offst_freq = -offst_freq - 2 * inc;
2356 else
2357 offst_freq = -offst_freq;
2358 } else {
2359 offst_freq += 2 * inc;
2360 }
2361
Andreas Regel2f5914b2009-04-23 14:56:41 -03002362 cpt_step++;
2363
Manu Abrahame415c682009-04-06 15:45:20 -03002364 lock = stv090x_get_dmdlock(state, timeout);
2365 no_signal = stv090x_chk_signal(state);
2366
2367 } while ((!lock) &&
2368 (!no_signal) &&
2369 ((offst_freq - inc) < car_max) &&
2370 ((offst_freq + inc) > -car_max) &&
2371 (cpt_step < steps_max));
2372
2373 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
2374 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0);
2375 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
2376 goto err;
2377
2378 return lock;
2379err:
2380 dprintk(FE_ERROR, 1, "I/O error");
2381 return -1;
2382}
2383
2384static int stv090x_sw_algo(struct stv090x_state *state)
2385{
2386 int no_signal, zigzag, lock = 0;
2387 u32 reg;
2388
2389 s32 dvbs2_fly_wheel;
2390 s32 inc, timeout_step, trials, steps_max;
2391
Manu Abraham27d40322009-05-02 18:26:58 -03002392 /* get params */
2393 stv090x_get_loop_params(state, &inc, &timeout_step, &steps_max);
Manu Abrahame415c682009-04-06 15:45:20 -03002394
Andreas Regel72982f72009-04-08 17:28:41 -03002395 switch (state->search_mode) {
Manu Abrahame415c682009-04-06 15:45:20 -03002396 case STV090x_SEARCH_DVBS1:
2397 case STV090x_SEARCH_DSS:
2398 /* accelerate the frequency detector */
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002399 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002400 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3B) < 0)
2401 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002402 }
Manu Abraham27d40322009-05-02 18:26:58 -03002403
Manu Abrahame415c682009-04-06 15:45:20 -03002404 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x49) < 0)
2405 goto err;
2406 zigzag = 0;
2407 break;
2408
2409 case STV090x_SEARCH_DVBS2:
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002410 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002411 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2412 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002413 }
Manu Abraham27d40322009-05-02 18:26:58 -03002414
Manu Abrahame415c682009-04-06 15:45:20 -03002415 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2416 goto err;
2417 zigzag = 1;
2418 break;
2419
2420 case STV090x_SEARCH_AUTO:
2421 default:
2422 /* accelerate the frequency detector */
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002423 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002424 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3b) < 0)
2425 goto err;
2426 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2427 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002428 }
Manu Abraham27d40322009-05-02 18:26:58 -03002429
Andreas Regel2f5914b2009-04-23 14:56:41 -03002430 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0xc9) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03002431 goto err;
2432 zigzag = 0;
2433 break;
2434 }
2435
2436 trials = 0;
2437 do {
2438 lock = stv090x_search_car_loop(state, inc, timeout_step, zigzag, steps_max);
2439 no_signal = stv090x_chk_signal(state);
2440 trials++;
2441
2442 /*run the SW search 2 times maximum*/
2443 if (lock || no_signal || (trials == 2)) {
2444 /*Check if the demod is not losing lock in DVBS2*/
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002445 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002446 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
2447 goto err;
2448 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
2449 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002450 }
2451
2452 reg = STV090x_READ_DEMOD(state, DMDSTATE);
2453 if ((lock) && (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == STV090x_DVBS2)) {
2454 /*Check if the demod is not losing lock in DVBS2*/
2455 msleep(timeout_step);
2456 reg = STV090x_READ_DEMOD(state, DMDFLYW);
2457 dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
2458 if (dvbs2_fly_wheel < 0xd) { /*if correct frames is decrementing */
2459 msleep(timeout_step);
2460 reg = STV090x_READ_DEMOD(state, DMDFLYW);
2461 dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
2462 }
2463 if (dvbs2_fly_wheel < 0xd) {
2464 /*FALSE lock, The demod is loosing lock */
2465 lock = 0;
2466 if (trials < 2) {
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002467 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002468 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2469 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002470 }
Manu Abraham27d40322009-05-02 18:26:58 -03002471
Manu Abrahame415c682009-04-06 15:45:20 -03002472 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2473 goto err;
2474 }
2475 }
2476 }
2477 }
2478 } while ((!lock) && (trials < 2) && (!no_signal));
2479
2480 return lock;
2481err:
2482 dprintk(FE_ERROR, 1, "I/O error");
2483 return -1;
2484}
2485
2486static enum stv090x_delsys stv090x_get_std(struct stv090x_state *state)
2487{
2488 u32 reg;
2489 enum stv090x_delsys delsys;
2490
2491 reg = STV090x_READ_DEMOD(state, DMDSTATE);
2492 if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 2)
2493 delsys = STV090x_DVBS2;
2494 else if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 3) {
2495 reg = STV090x_READ_DEMOD(state, FECM);
2496 if (STV090x_GETFIELD_Px(reg, DSS_DVB_FIELD) == 1)
2497 delsys = STV090x_DSS;
2498 else
2499 delsys = STV090x_DVBS1;
2500 } else {
2501 delsys = STV090x_ERROR;
2502 }
2503
2504 return delsys;
2505}
2506
2507/* in Hz */
2508static s32 stv090x_get_car_freq(struct stv090x_state *state, u32 mclk)
2509{
2510 s32 derot, int_1, int_2, tmp_1, tmp_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002511
2512 derot = STV090x_READ_DEMOD(state, CFR2) << 16;
2513 derot |= STV090x_READ_DEMOD(state, CFR1) << 8;
2514 derot |= STV090x_READ_DEMOD(state, CFR0);
2515
2516 derot = comp2(derot, 24);
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002517 int_1 = mclk >> 12;
Manu Abrahamda4b9052009-04-08 20:30:29 -03002518 int_2 = derot >> 12;
Manu Abrahame415c682009-04-06 15:45:20 -03002519
Manu Abrahamda4b9052009-04-08 20:30:29 -03002520 /* carrier_frequency = MasterClock * Reg / 2^24 */
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002521 tmp_1 = mclk % 0x1000;
Manu Abrahamda4b9052009-04-08 20:30:29 -03002522 tmp_2 = derot % 0x1000;
Manu Abrahame415c682009-04-06 15:45:20 -03002523
2524 derot = (int_1 * int_2) +
Manu Abrahamda4b9052009-04-08 20:30:29 -03002525 ((int_1 * tmp_2) >> 12) +
Andreas Regelb671a8d2009-11-13 18:15:27 -03002526 ((int_2 * tmp_1) >> 12);
Manu Abrahame415c682009-04-06 15:45:20 -03002527
2528 return derot;
2529}
2530
2531static int stv090x_get_viterbi(struct stv090x_state *state)
2532{
2533 u32 reg, rate;
2534
2535 reg = STV090x_READ_DEMOD(state, VITCURPUN);
2536 rate = STV090x_GETFIELD_Px(reg, VIT_CURPUN_FIELD);
2537
2538 switch (rate) {
2539 case 13:
2540 state->fec = STV090x_PR12;
2541 break;
2542
2543 case 18:
2544 state->fec = STV090x_PR23;
2545 break;
2546
2547 case 21:
2548 state->fec = STV090x_PR34;
2549 break;
2550
2551 case 24:
2552 state->fec = STV090x_PR56;
2553 break;
2554
2555 case 25:
2556 state->fec = STV090x_PR67;
2557 break;
2558
2559 case 26:
2560 state->fec = STV090x_PR78;
2561 break;
2562
2563 default:
2564 state->fec = STV090x_PRERR;
2565 break;
2566 }
2567
2568 return 0;
2569}
2570
2571static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *state)
2572{
2573 struct dvb_frontend *fe = &state->frontend;
2574
2575 u8 tmg;
2576 u32 reg;
2577 s32 i = 0, offst_freq;
2578
2579 msleep(5);
2580
2581 if (state->algo == STV090x_BLIND_SEARCH) {
2582 tmg = STV090x_READ_DEMOD(state, TMGREG2);
2583 STV090x_WRITE_DEMOD(state, SFRSTEP, 0x5c);
Andreas Regel2f5914b2009-04-23 14:56:41 -03002584 while ((i <= 50) && (tmg != 0) && (tmg != 0xff)) {
Manu Abrahame415c682009-04-06 15:45:20 -03002585 tmg = STV090x_READ_DEMOD(state, TMGREG2);
2586 msleep(5);
2587 i += 5;
2588 }
2589 }
2590 state->delsys = stv090x_get_std(state);
2591
Manu Abraham27d40322009-05-02 18:26:58 -03002592 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2593 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002594
Manu Abraham27d40322009-05-02 18:26:58 -03002595 if (state->config->tuner_get_frequency) {
2596 if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03002597 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03002598 }
Manu Abrahame415c682009-04-06 15:45:20 -03002599
Manu Abraham27d40322009-05-02 18:26:58 -03002600 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2601 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002602
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002603 offst_freq = stv090x_get_car_freq(state, state->internal->mclk) / 1000;
Manu Abrahame415c682009-04-06 15:45:20 -03002604 state->frequency += offst_freq;
Manu Abraham27d40322009-05-02 18:26:58 -03002605
2606 if (stv090x_get_viterbi(state) < 0)
2607 goto err;
2608
Manu Abrahame415c682009-04-06 15:45:20 -03002609 reg = STV090x_READ_DEMOD(state, DMDMODCOD);
2610 state->modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
2611 state->pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
2612 state->frame_len = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) >> 1;
2613 reg = STV090x_READ_DEMOD(state, TMGOBS);
2614 state->rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
2615 reg = STV090x_READ_DEMOD(state, FECM);
2616 state->inversion = STV090x_GETFIELD_Px(reg, IQINV_FIELD);
2617
2618 if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000)) {
2619
Manu Abraham27d40322009-05-02 18:26:58 -03002620 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2621 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002622
Manu Abraham27d40322009-05-02 18:26:58 -03002623 if (state->config->tuner_get_frequency) {
2624 if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03002625 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03002626 }
Manu Abrahame415c682009-04-06 15:45:20 -03002627
Manu Abraham27d40322009-05-02 18:26:58 -03002628 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2629 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002630
2631 if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
Andreas Regel2f5914b2009-04-23 14:56:41 -03002632 return STV090x_RANGEOK;
Manu Abrahame415c682009-04-06 15:45:20 -03002633 else if (abs(offst_freq) <= (stv090x_car_width(state->srate, state->rolloff) / 2000))
2634 return STV090x_RANGEOK;
2635 else
2636 return STV090x_OUTOFRANGE; /* Out of Range */
2637 } else {
2638 if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
2639 return STV090x_RANGEOK;
2640 else
2641 return STV090x_OUTOFRANGE;
2642 }
2643
2644 return STV090x_OUTOFRANGE;
Oliver Endriss2c1f7502010-01-10 15:38:38 -03002645
2646err_gateoff:
2647 stv090x_i2c_gate_ctrl(fe, 0);
Manu Abraham27d40322009-05-02 18:26:58 -03002648err:
2649 dprintk(FE_ERROR, 1, "I/O error");
2650 return -1;
Manu Abrahame415c682009-04-06 15:45:20 -03002651}
2652
2653static u32 stv090x_get_tmgoffst(struct stv090x_state *state, u32 srate)
2654{
2655 s32 offst_tmg;
Manu Abrahame415c682009-04-06 15:45:20 -03002656
2657 offst_tmg = STV090x_READ_DEMOD(state, TMGREG2) << 16;
2658 offst_tmg |= STV090x_READ_DEMOD(state, TMGREG1) << 8;
2659 offst_tmg |= STV090x_READ_DEMOD(state, TMGREG0);
2660
Manu Abrahame415c682009-04-06 15:45:20 -03002661 offst_tmg = comp2(offst_tmg, 24); /* 2's complement */
2662 if (!offst_tmg)
2663 offst_tmg = 1;
2664
Manu Abraham5f99feff2009-04-08 20:24:53 -03002665 offst_tmg = ((s32) srate * 10) / ((s32) 0x1000000 / offst_tmg);
Manu Abrahame415c682009-04-06 15:45:20 -03002666 offst_tmg /= 320;
2667
2668 return offst_tmg;
2669}
2670
2671static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_modcod modcod, s32 pilots)
2672{
2673 u8 aclc = 0x29;
2674 s32 i;
Manu Abraham27d40322009-05-02 18:26:58 -03002675 struct stv090x_long_frame_crloop *car_loop, *car_loop_qpsk_low, *car_loop_apsk_low;
Manu Abrahame415c682009-04-06 15:45:20 -03002676
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002677 if (state->internal->dev_ver == 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03002678 car_loop = stv090x_s2_crl_cut20;
2679 car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut20;
2680 car_loop_apsk_low = stv090x_s2_apsk_crl_cut20;
2681 } else {
2682 /* >= Cut 3 */
2683 car_loop = stv090x_s2_crl_cut30;
2684 car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut30;
2685 car_loop_apsk_low = stv090x_s2_apsk_crl_cut30;
2686 }
Manu Abrahame415c682009-04-06 15:45:20 -03002687
2688 if (modcod < STV090x_QPSK_12) {
2689 i = 0;
Manu Abraham27d40322009-05-02 18:26:58 -03002690 while ((i < 3) && (modcod != car_loop_qpsk_low[i].modcod))
Manu Abrahame415c682009-04-06 15:45:20 -03002691 i++;
2692
2693 if (i >= 3)
2694 i = 2;
2695
2696 } else {
2697 i = 0;
2698 while ((i < 14) && (modcod != car_loop[i].modcod))
2699 i++;
2700
2701 if (i >= 14) {
2702 i = 0;
Manu Abraham27d40322009-05-02 18:26:58 -03002703 while ((i < 11) && (modcod != car_loop_apsk_low[i].modcod))
Manu Abrahame415c682009-04-06 15:45:20 -03002704 i++;
2705
2706 if (i >= 11)
2707 i = 10;
2708 }
2709 }
2710
2711 if (modcod <= STV090x_QPSK_25) {
2712 if (pilots) {
2713 if (state->srate <= 3000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002714 aclc = car_loop_qpsk_low[i].crl_pilots_on_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002715 else if (state->srate <= 7000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002716 aclc = car_loop_qpsk_low[i].crl_pilots_on_5;
Manu Abrahame415c682009-04-06 15:45:20 -03002717 else if (state->srate <= 15000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002718 aclc = car_loop_qpsk_low[i].crl_pilots_on_10;
Manu Abrahame415c682009-04-06 15:45:20 -03002719 else if (state->srate <= 25000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002720 aclc = car_loop_qpsk_low[i].crl_pilots_on_20;
Manu Abrahame415c682009-04-06 15:45:20 -03002721 else
Manu Abraham27d40322009-05-02 18:26:58 -03002722 aclc = car_loop_qpsk_low[i].crl_pilots_on_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002723 } else {
2724 if (state->srate <= 3000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002725 aclc = car_loop_qpsk_low[i].crl_pilots_off_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002726 else if (state->srate <= 7000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002727 aclc = car_loop_qpsk_low[i].crl_pilots_off_5;
Manu Abrahame415c682009-04-06 15:45:20 -03002728 else if (state->srate <= 15000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002729 aclc = car_loop_qpsk_low[i].crl_pilots_off_10;
Manu Abrahame415c682009-04-06 15:45:20 -03002730 else if (state->srate <= 25000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002731 aclc = car_loop_qpsk_low[i].crl_pilots_off_20;
Manu Abrahame415c682009-04-06 15:45:20 -03002732 else
Manu Abraham27d40322009-05-02 18:26:58 -03002733 aclc = car_loop_qpsk_low[i].crl_pilots_off_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002734 }
2735
2736 } else if (modcod <= STV090x_8PSK_910) {
2737 if (pilots) {
2738 if (state->srate <= 3000000)
2739 aclc = car_loop[i].crl_pilots_on_2;
2740 else if (state->srate <= 7000000)
2741 aclc = car_loop[i].crl_pilots_on_5;
2742 else if (state->srate <= 15000000)
2743 aclc = car_loop[i].crl_pilots_on_10;
2744 else if (state->srate <= 25000000)
2745 aclc = car_loop[i].crl_pilots_on_20;
2746 else
2747 aclc = car_loop[i].crl_pilots_on_30;
2748 } else {
2749 if (state->srate <= 3000000)
2750 aclc = car_loop[i].crl_pilots_off_2;
2751 else if (state->srate <= 7000000)
2752 aclc = car_loop[i].crl_pilots_off_5;
2753 else if (state->srate <= 15000000)
2754 aclc = car_loop[i].crl_pilots_off_10;
2755 else if (state->srate <= 25000000)
2756 aclc = car_loop[i].crl_pilots_off_20;
2757 else
2758 aclc = car_loop[i].crl_pilots_off_30;
2759 }
2760 } else { /* 16APSK and 32APSK */
2761 if (state->srate <= 3000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002762 aclc = car_loop_apsk_low[i].crl_pilots_on_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002763 else if (state->srate <= 7000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002764 aclc = car_loop_apsk_low[i].crl_pilots_on_5;
Manu Abrahame415c682009-04-06 15:45:20 -03002765 else if (state->srate <= 15000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002766 aclc = car_loop_apsk_low[i].crl_pilots_on_10;
Manu Abrahame415c682009-04-06 15:45:20 -03002767 else if (state->srate <= 25000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002768 aclc = car_loop_apsk_low[i].crl_pilots_on_20;
Manu Abrahame415c682009-04-06 15:45:20 -03002769 else
Manu Abraham27d40322009-05-02 18:26:58 -03002770 aclc = car_loop_apsk_low[i].crl_pilots_on_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002771 }
2772
2773 return aclc;
2774}
2775
2776static u8 stv090x_optimize_carloop_short(struct stv090x_state *state)
2777{
Manu Abraham0a5ded52009-06-17 16:48:17 -03002778 struct stv090x_short_frame_crloop *short_crl = NULL;
Manu Abrahame415c682009-04-06 15:45:20 -03002779 s32 index = 0;
2780 u8 aclc = 0x0b;
2781
2782 switch (state->modulation) {
2783 case STV090x_QPSK:
2784 default:
2785 index = 0;
2786 break;
2787 case STV090x_8PSK:
2788 index = 1;
2789 break;
2790 case STV090x_16APSK:
2791 index = 2;
2792 break;
2793 case STV090x_32APSK:
2794 index = 3;
2795 break;
2796 }
2797
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002798 if (state->internal->dev_ver >= 0x30) {
Manu Abrahameebf8d82009-06-18 04:50:53 -03002799 /* Cut 3.0 and up */
Manu Abraham27d40322009-05-02 18:26:58 -03002800 short_crl = stv090x_s2_short_crl_cut30;
Manu Abrahameebf8d82009-06-18 04:50:53 -03002801 } else {
2802 /* Cut 2.0 and up: we don't support cuts older than 2.0 */
2803 short_crl = stv090x_s2_short_crl_cut20;
2804 }
Manu Abrahame415c682009-04-06 15:45:20 -03002805
Manu Abraham27d40322009-05-02 18:26:58 -03002806 if (state->srate <= 3000000)
2807 aclc = short_crl[index].crl_2;
2808 else if (state->srate <= 7000000)
2809 aclc = short_crl[index].crl_5;
2810 else if (state->srate <= 15000000)
2811 aclc = short_crl[index].crl_10;
2812 else if (state->srate <= 25000000)
2813 aclc = short_crl[index].crl_20;
2814 else
2815 aclc = short_crl[index].crl_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002816
2817 return aclc;
2818}
2819
2820static int stv090x_optimize_track(struct stv090x_state *state)
2821{
2822 struct dvb_frontend *fe = &state->frontend;
2823
2824 enum stv090x_rolloff rolloff;
2825 enum stv090x_modcod modcod;
2826
2827 s32 srate, pilots, aclc, f_1, f_0, i = 0, blind_tune = 0;
2828 u32 reg;
2829
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002830 srate = stv090x_get_srate(state, state->internal->mclk);
Manu Abrahame415c682009-04-06 15:45:20 -03002831 srate += stv090x_get_tmgoffst(state, srate);
2832
2833 switch (state->delsys) {
2834 case STV090x_DVBS1:
2835 case STV090x_DSS:
Andreas Regelb671a8d2009-11-13 18:15:27 -03002836 if (state->search_mode == STV090x_SEARCH_AUTO) {
Manu Abrahame415c682009-04-06 15:45:20 -03002837 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2838 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
2839 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
2840 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2841 goto err;
2842 }
2843 reg = STV090x_READ_DEMOD(state, DEMOD);
2844 STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
Manu Abraham27d40322009-05-02 18:26:58 -03002845 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x01);
Manu Abrahame415c682009-04-06 15:45:20 -03002846 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
2847 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03002848
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002849 if (state->internal->dev_ver >= 0x30) {
Manu Abraham27d40322009-05-02 18:26:58 -03002850 if (stv090x_get_viterbi(state) < 0)
2851 goto err;
2852
2853 if (state->fec == STV090x_PR12) {
2854 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x98) < 0)
2855 goto err;
2856 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2857 goto err;
2858 } else {
2859 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x18) < 0)
2860 goto err;
2861 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2862 goto err;
2863 }
2864 }
2865
Manu Abrahame415c682009-04-06 15:45:20 -03002866 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
2867 goto err;
2868 break;
2869
2870 case STV090x_DVBS2:
2871 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
Andreas Regel2f5914b2009-04-23 14:56:41 -03002872 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
2873 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
Manu Abrahame415c682009-04-06 15:45:20 -03002874 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2875 goto err;
2876 if (STV090x_WRITE_DEMOD(state, ACLC, 0) < 0)
2877 goto err;
2878 if (STV090x_WRITE_DEMOD(state, BCLC, 0) < 0)
2879 goto err;
2880 if (state->frame_len == STV090x_LONG_FRAME) {
2881 reg = STV090x_READ_DEMOD(state, DMDMODCOD);
2882 modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
2883 pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
2884 aclc = stv090x_optimize_carloop(state, modcod, pilots);
2885 if (modcod <= STV090x_QPSK_910) {
2886 STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc);
2887 } else if (modcod <= STV090x_8PSK_910) {
2888 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2889 goto err;
2890 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
2891 goto err;
2892 }
2893 if ((state->demod_mode == STV090x_SINGLE) && (modcod > STV090x_8PSK_910)) {
2894 if (modcod <= STV090x_16APSK_910) {
2895 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2896 goto err;
2897 if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2898 goto err;
2899 } else {
2900 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2901 goto err;
2902 if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
2903 goto err;
2904 }
2905 }
2906 } else {
2907 /*Carrier loop setting for short frame*/
2908 aclc = stv090x_optimize_carloop_short(state);
2909 if (state->modulation == STV090x_QPSK) {
2910 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc) < 0)
2911 goto err;
2912 } else if (state->modulation == STV090x_8PSK) {
2913 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2914 goto err;
2915 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
2916 goto err;
2917 } else if (state->modulation == STV090x_16APSK) {
2918 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2919 goto err;
2920 if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2921 goto err;
2922 } else if (state->modulation == STV090x_32APSK) {
2923 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2924 goto err;
2925 if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
2926 goto err;
2927 }
2928 }
Manu Abraham27d40322009-05-02 18:26:58 -03002929
Manu Abrahame415c682009-04-06 15:45:20 -03002930 STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67); /* PER */
2931 break;
2932
2933 case STV090x_UNKNOWN:
2934 default:
2935 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2936 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
2937 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
2938 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2939 goto err;
2940 break;
2941 }
2942
2943 f_1 = STV090x_READ_DEMOD(state, CFR2);
2944 f_0 = STV090x_READ_DEMOD(state, CFR1);
2945 reg = STV090x_READ_DEMOD(state, TMGOBS);
2946 rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
2947
2948 if (state->algo == STV090x_BLIND_SEARCH) {
2949 STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00);
2950 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2951 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0x00);
2952 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
2953 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2954 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03002955 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03002956 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03002957
2958 if (stv090x_set_srate(state, srate) < 0)
2959 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002960 blind_tune = 1;
Andreas Regel7b035da2009-11-13 18:20:54 -03002961
2962 if (stv090x_dvbs_track_crl(state) < 0)
2963 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002964 }
2965
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002966 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002967 if ((state->search_mode == STV090x_SEARCH_DVBS1) ||
2968 (state->search_mode == STV090x_SEARCH_DSS) ||
2969 (state->search_mode == STV090x_SEARCH_AUTO)) {
2970
2971 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x0a) < 0)
2972 goto err;
2973 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x00) < 0)
2974 goto err;
2975 }
2976 }
2977
Manu Abrahame415c682009-04-06 15:45:20 -03002978 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
2979 goto err;
2980
Manu Abraham27d40322009-05-02 18:26:58 -03002981 /* AUTO tracking MODE */
2982 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x80) < 0)
2983 goto err;
2984 /* AUTO tracking MODE */
2985 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x80) < 0)
2986 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002987
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002988 if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1) ||
2989 (state->srate < 10000000)) {
Manu Abraham27d40322009-05-02 18:26:58 -03002990 /* update initial carrier freq with the found freq offset */
Manu Abrahame415c682009-04-06 15:45:20 -03002991 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
2992 goto err;
2993 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
2994 goto err;
2995 state->tuner_bw = stv090x_car_width(srate, state->rolloff) + 10000000;
2996
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002997 if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1)) {
Manu Abrahame415c682009-04-06 15:45:20 -03002998
2999 if (state->algo != STV090x_WARM_SEARCH) {
3000
Manu Abraham27d40322009-05-02 18:26:58 -03003001 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
3002 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003003
Manu Abraham27d40322009-05-02 18:26:58 -03003004 if (state->config->tuner_set_bandwidth) {
3005 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03003006 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03003007 }
Manu Abrahame415c682009-04-06 15:45:20 -03003008
Manu Abraham27d40322009-05-02 18:26:58 -03003009 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
3010 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003011
3012 }
3013 }
3014 if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000))
3015 msleep(50); /* blind search: wait 50ms for SR stabilization */
3016 else
3017 msleep(5);
3018
3019 stv090x_get_lock_tmg(state);
3020
3021 if (!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) {
3022 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
3023 goto err;
3024 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
3025 goto err;
3026 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
3027 goto err;
3028 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
3029 goto err;
3030
3031 i = 0;
3032
3033 while ((!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) && (i <= 2)) {
3034
3035 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
3036 goto err;
3037 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
3038 goto err;
3039 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
3040 goto err;
3041 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
3042 goto err;
3043 i++;
3044 }
3045 }
3046
3047 }
3048
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003049 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03003050 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
3051 goto err;
3052 }
Manu Abraham27d40322009-05-02 18:26:58 -03003053
Manu Abrahame415c682009-04-06 15:45:20 -03003054 if ((state->delsys == STV090x_DVBS1) || (state->delsys == STV090x_DSS))
3055 stv090x_set_vit_thtracq(state);
3056
3057 return 0;
Oliver Endriss2c1f7502010-01-10 15:38:38 -03003058
3059err_gateoff:
3060 stv090x_i2c_gate_ctrl(fe, 0);
Manu Abrahame415c682009-04-06 15:45:20 -03003061err:
3062 dprintk(FE_ERROR, 1, "I/O error");
3063 return -1;
3064}
3065
3066static int stv090x_get_feclock(struct stv090x_state *state, s32 timeout)
3067{
3068 s32 timer = 0, lock = 0, stat;
3069 u32 reg;
3070
3071 while ((timer < timeout) && (!lock)) {
3072 reg = STV090x_READ_DEMOD(state, DMDSTATE);
3073 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
3074
3075 switch (stat) {
3076 case 0: /* searching */
3077 case 1: /* first PLH detected */
3078 default:
3079 lock = 0;
3080 break;
3081
3082 case 2: /* DVB-S2 mode */
3083 reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
3084 lock = STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD);
3085 break;
3086
3087 case 3: /* DVB-S1/legacy mode */
3088 reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
3089 lock = STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD);
3090 break;
3091 }
3092 if (!lock) {
3093 msleep(10);
3094 timer += 10;
3095 }
3096 }
3097 return lock;
3098}
3099
3100static int stv090x_get_lock(struct stv090x_state *state, s32 timeout_dmd, s32 timeout_fec)
3101{
3102 u32 reg;
3103 s32 timer = 0;
3104 int lock;
3105
3106 lock = stv090x_get_dmdlock(state, timeout_dmd);
3107 if (lock)
3108 lock = stv090x_get_feclock(state, timeout_fec);
3109
3110 if (lock) {
3111 lock = 0;
3112
3113 while ((timer < timeout_fec) && (!lock)) {
3114 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3115 lock = STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD);
3116 msleep(1);
3117 timer++;
3118 }
3119 }
3120
3121 return lock;
3122}
3123
3124static int stv090x_set_s2rolloff(struct stv090x_state *state)
3125{
Manu Abrahame415c682009-04-06 15:45:20 -03003126 u32 reg;
3127
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003128 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03003129 /* rolloff to auto mode if DVBS2 */
Manu Abrahame415c682009-04-06 15:45:20 -03003130 reg = STV090x_READ_DEMOD(state, DEMOD);
Manu Abraham27d40322009-05-02 18:26:58 -03003131 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x00);
Manu Abrahame415c682009-04-06 15:45:20 -03003132 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3133 goto err;
3134 } else {
Manu Abraham27d40322009-05-02 18:26:58 -03003135 /* DVB-S2 rolloff to auto mode if DVBS2 */
Manu Abrahame415c682009-04-06 15:45:20 -03003136 reg = STV090x_READ_DEMOD(state, DEMOD);
Manu Abraham27d40322009-05-02 18:26:58 -03003137 STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 0x00);
Manu Abrahame415c682009-04-06 15:45:20 -03003138 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3139 goto err;
3140 }
3141 return 0;
3142err:
3143 dprintk(FE_ERROR, 1, "I/O error");
3144 return -1;
3145}
3146
Manu Abrahame415c682009-04-06 15:45:20 -03003147
3148static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
3149{
3150 struct dvb_frontend *fe = &state->frontend;
3151 enum stv090x_signal_state signal_state = STV090x_NOCARRIER;
3152 u32 reg;
Andreas Regela4978a82009-11-13 18:17:45 -03003153 s32 agc1_power, power_iq = 0, i;
Andreas Regel4e58a682009-04-16 08:40:36 -03003154 int lock = 0, low_sr = 0, no_signal = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003155
3156 reg = STV090x_READ_DEMOD(state, TSCFGH);
3157 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* Stop path 1 stream merger */
3158 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3159 goto err;
3160
3161 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod stop */
3162 goto err;
3163
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003164 if (state->internal->dev_ver >= 0x20) {
Andreas Regel7b035da2009-11-13 18:20:54 -03003165 if (state->srate > 5000000) {
3166 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
3167 goto err;
3168 } else {
3169 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x82) < 0)
3170 goto err;
3171 }
Manu Abrahame415c682009-04-06 15:45:20 -03003172 }
3173
3174 stv090x_get_lock_tmg(state);
3175
3176 if (state->algo == STV090x_BLIND_SEARCH) {
3177 state->tuner_bw = 2 * 36000000; /* wide bw for unknown srate */
Manu Abraham27d40322009-05-02 18:26:58 -03003178 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0) /* wider srate scan */
Manu Abrahame415c682009-04-06 15:45:20 -03003179 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003180 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
3181 goto err;
3182 if (stv090x_set_srate(state, 1000000) < 0) /* inital srate = 1Msps */
3183 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003184 } else {
3185 /* known srate */
3186 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
3187 goto err;
3188 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
3189 goto err;
3190
Manu Abraham27d40322009-05-02 18:26:58 -03003191 if (state->srate < 2000000) {
3192 /* SR < 2MSPS */
3193 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x63) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003194 goto err;
3195 } else {
Manu Abraham27d40322009-05-02 18:26:58 -03003196 /* SR >= 2Msps */
3197 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003198 goto err;
3199 }
3200
Manu Abraham27d40322009-05-02 18:26:58 -03003201 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
3202 goto err;
3203
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003204 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03003205 if (STV090x_WRITE_DEMOD(state, KREFTMG, 0x5a) < 0)
3206 goto err;
3207 if (state->algo == STV090x_COLD_SEARCH)
Andreas Regel4e58a682009-04-16 08:40:36 -03003208 state->tuner_bw = (15 * (stv090x_car_width(state->srate, state->rolloff) + 10000000)) / 10;
Manu Abrahame415c682009-04-06 15:45:20 -03003209 else if (state->algo == STV090x_WARM_SEARCH)
3210 state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + 10000000;
Manu Abrahame415c682009-04-06 15:45:20 -03003211 }
Manu Abraham27d40322009-05-02 18:26:58 -03003212
3213 /* if cold start or warm (Symbolrate is known)
3214 * use a Narrow symbol rate scan range
3215 */
3216 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0) /* narrow srate scan */
Manu Abrahame415c682009-04-06 15:45:20 -03003217 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003218
3219 if (stv090x_set_srate(state, state->srate) < 0)
3220 goto err;
3221
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003222 if (stv090x_set_max_srate(state, state->internal->mclk,
3223 state->srate) < 0)
Manu Abraham27d40322009-05-02 18:26:58 -03003224 goto err;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003225 if (stv090x_set_min_srate(state, state->internal->mclk,
3226 state->srate) < 0)
Manu Abraham27d40322009-05-02 18:26:58 -03003227 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003228
3229 if (state->srate >= 10000000)
Andreas Regel4e58a682009-04-16 08:40:36 -03003230 low_sr = 0;
3231 else
Manu Abrahame415c682009-04-06 15:45:20 -03003232 low_sr = 1;
3233 }
3234
3235 /* Setup tuner */
Manu Abraham27d40322009-05-02 18:26:58 -03003236 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
3237 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003238
Manu Abraham27d40322009-05-02 18:26:58 -03003239 if (state->config->tuner_set_bbgain) {
Oliver Endrissd8b5a8e2010-01-10 15:40:28 -03003240 reg = state->config->tuner_bbgain;
3241 if (reg == 0)
3242 reg = 10; /* default: 10dB */
3243 if (state->config->tuner_set_bbgain(fe, reg) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03003244 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03003245 }
Manu Abrahame415c682009-04-06 15:45:20 -03003246
Manu Abraham27d40322009-05-02 18:26:58 -03003247 if (state->config->tuner_set_frequency) {
3248 if (state->config->tuner_set_frequency(fe, state->frequency) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03003249 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03003250 }
Manu Abrahame415c682009-04-06 15:45:20 -03003251
Manu Abraham27d40322009-05-02 18:26:58 -03003252 if (state->config->tuner_set_bandwidth) {
3253 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03003254 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03003255 }
Manu Abrahame415c682009-04-06 15:45:20 -03003256
Manu Abraham27d40322009-05-02 18:26:58 -03003257 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
3258 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003259
3260 msleep(50);
3261
Manu Abraham27d40322009-05-02 18:26:58 -03003262 if (state->config->tuner_get_status) {
Oliver Endriss41894b92010-01-10 15:39:45 -03003263 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
3264 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003265 if (state->config->tuner_get_status(fe, &reg) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03003266 goto err_gateoff;
Oliver Endriss41894b92010-01-10 15:39:45 -03003267 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
3268 goto err;
3269
3270 if (reg)
3271 dprintk(FE_DEBUG, 1, "Tuner phase locked");
3272 else {
3273 dprintk(FE_DEBUG, 1, "Tuner unlocked");
3274 return STV090x_NOCARRIER;
3275 }
Manu Abraham27d40322009-05-02 18:26:58 -03003276 }
Manu Abrahame415c682009-04-06 15:45:20 -03003277
Manu Abraham27d40322009-05-02 18:26:58 -03003278 msleep(10);
3279 agc1_power = MAKEWORD16(STV090x_READ_DEMOD(state, AGCIQIN1),
3280 STV090x_READ_DEMOD(state, AGCIQIN0));
3281
3282 if (agc1_power == 0) {
3283 /* If AGC1 integrator value is 0
3284 * then read POWERI, POWERQ
3285 */
3286 for (i = 0; i < 5; i++) {
3287 power_iq += (STV090x_READ_DEMOD(state, POWERI) +
3288 STV090x_READ_DEMOD(state, POWERQ)) >> 1;
3289 }
3290 power_iq /= 5;
Manu Abrahame415c682009-04-06 15:45:20 -03003291 }
3292
Manu Abraham27d40322009-05-02 18:26:58 -03003293 if ((agc1_power == 0) && (power_iq < STV090x_IQPOWER_THRESHOLD)) {
3294 dprintk(FE_ERROR, 1, "No Signal: POWER_IQ=0x%02x", power_iq);
3295 lock = 0;
Andreas Regelc4fa6492009-11-13 18:18:53 -03003296 signal_state = STV090x_NOAGC1;
Manu Abraham27d40322009-05-02 18:26:58 -03003297 } else {
3298 reg = STV090x_READ_DEMOD(state, DEMOD);
3299 STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, state->inversion);
3300
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003301 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03003302 /* rolloff to auto mode if DVBS2 */
3303 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 1);
3304 } else {
3305 /* DVB-S2 rolloff to auto mode if DVBS2 */
3306 STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 1);
3307 }
3308 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3309 goto err;
3310
3311 if (stv090x_delivery_search(state) < 0)
3312 goto err;
3313
3314 if (state->algo != STV090x_BLIND_SEARCH) {
3315 if (stv090x_start_search(state) < 0)
3316 goto err;
3317 }
3318 }
3319
Andreas Regelc4fa6492009-11-13 18:18:53 -03003320 if (signal_state == STV090x_NOAGC1)
3321 return signal_state;
Manu Abraham27d40322009-05-02 18:26:58 -03003322
Manu Abrahame415c682009-04-06 15:45:20 -03003323 if (state->algo == STV090x_BLIND_SEARCH)
3324 lock = stv090x_blind_search(state);
Manu Abraham27d40322009-05-02 18:26:58 -03003325
Manu Abrahame415c682009-04-06 15:45:20 -03003326 else if (state->algo == STV090x_COLD_SEARCH)
Andreas Regela4978a82009-11-13 18:17:45 -03003327 lock = stv090x_get_coldlock(state, state->DemodTimeout);
Manu Abraham27d40322009-05-02 18:26:58 -03003328
Manu Abrahame415c682009-04-06 15:45:20 -03003329 else if (state->algo == STV090x_WARM_SEARCH)
Andreas Regela4978a82009-11-13 18:17:45 -03003330 lock = stv090x_get_dmdlock(state, state->DemodTimeout);
Manu Abrahame415c682009-04-06 15:45:20 -03003331
3332 if ((!lock) && (state->algo == STV090x_COLD_SEARCH)) {
3333 if (!low_sr) {
3334 if (stv090x_chk_tmg(state))
3335 lock = stv090x_sw_algo(state);
3336 }
3337 }
3338
3339 if (lock)
3340 signal_state = stv090x_get_sig_params(state);
3341
3342 if ((lock) && (signal_state == STV090x_RANGEOK)) { /* signal within Range */
3343 stv090x_optimize_track(state);
Manu Abraham27d40322009-05-02 18:26:58 -03003344
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003345 if (state->internal->dev_ver >= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03003346 /* >= Cut 2.0 :release TS reset after
3347 * demod lock and optimized Tracking
3348 */
Manu Abrahame415c682009-04-06 15:45:20 -03003349 reg = STV090x_READ_DEMOD(state, TSCFGH);
3350 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
3351 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3352 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003353
Manu Abrahame415c682009-04-06 15:45:20 -03003354 msleep(3);
Manu Abraham27d40322009-05-02 18:26:58 -03003355
Manu Abrahame415c682009-04-06 15:45:20 -03003356 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
3357 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3358 goto err;
3359
3360 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
3361 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3362 goto err;
3363 }
3364
Andreas Regela4978a82009-11-13 18:17:45 -03003365 lock = stv090x_get_lock(state, state->FecTimeout,
3366 state->FecTimeout);
3367 if (lock) {
Manu Abrahame415c682009-04-06 15:45:20 -03003368 if (state->delsys == STV090x_DVBS2) {
3369 stv090x_set_s2rolloff(state);
Manu Abraham27d40322009-05-02 18:26:58 -03003370
3371 reg = STV090x_READ_DEMOD(state, PDELCTRL2);
3372 STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 1);
3373 if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003374 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003375 /* Reset DVBS2 packet delinator error counter */
3376 reg = STV090x_READ_DEMOD(state, PDELCTRL2);
3377 STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 0);
3378 if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003379 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003380
Manu Abrahame415c682009-04-06 15:45:20 -03003381 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67) < 0) /* PER */
3382 goto err;
3383 } else {
3384 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
3385 goto err;
3386 }
Manu Abraham27d40322009-05-02 18:26:58 -03003387 /* Reset the Total packet counter */
Manu Abrahame415c682009-04-06 15:45:20 -03003388 if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0x00) < 0)
3389 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003390 /* Reset the packet Error counter2 */
Manu Abrahame415c682009-04-06 15:45:20 -03003391 if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3392 goto err;
3393 } else {
Manu Abrahame415c682009-04-06 15:45:20 -03003394 signal_state = STV090x_NODATA;
3395 no_signal = stv090x_chk_signal(state);
3396 }
3397 }
Manu Abrahame415c682009-04-06 15:45:20 -03003398 return signal_state;
3399
Oliver Endriss2c1f7502010-01-10 15:38:38 -03003400err_gateoff:
3401 stv090x_i2c_gate_ctrl(fe, 0);
Manu Abrahame415c682009-04-06 15:45:20 -03003402err:
3403 dprintk(FE_ERROR, 1, "I/O error");
3404 return -1;
3405}
3406
3407static enum dvbfe_search stv090x_search(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
3408{
3409 struct stv090x_state *state = fe->demodulator_priv;
3410 struct dtv_frontend_properties *props = &fe->dtv_property_cache;
3411
Andreas Regel729cbaf2010-01-05 19:21:02 -03003412 if (p->frequency == 0)
3413 return DVBFE_ALGO_SEARCH_INVALID;
3414
Manu Abrahame415c682009-04-06 15:45:20 -03003415 state->delsys = props->delivery_system;
3416 state->frequency = p->frequency;
3417 state->srate = p->u.qpsk.symbol_rate;
Andreas Regel4e58a682009-04-16 08:40:36 -03003418 state->search_mode = STV090x_SEARCH_AUTO;
3419 state->algo = STV090x_COLD_SEARCH;
3420 state->fec = STV090x_PRERR;
Andreas Regelc879d8c2009-11-13 18:11:26 -03003421 if (state->srate > 10000000) {
3422 dprintk(FE_DEBUG, 1, "Search range: 10 MHz");
3423 state->search_range = 10000000;
3424 } else {
3425 dprintk(FE_DEBUG, 1, "Search range: 5 MHz");
3426 state->search_range = 5000000;
3427 }
Manu Abrahame415c682009-04-06 15:45:20 -03003428
Andreas Regel2f5914b2009-04-23 14:56:41 -03003429 if (stv090x_algo(state) == STV090x_RANGEOK) {
Manu Abrahame415c682009-04-06 15:45:20 -03003430 dprintk(FE_DEBUG, 1, "Search success!");
3431 return DVBFE_ALGO_SEARCH_SUCCESS;
3432 } else {
3433 dprintk(FE_DEBUG, 1, "Search failed!");
3434 return DVBFE_ALGO_SEARCH_FAILED;
3435 }
3436
3437 return DVBFE_ALGO_SEARCH_ERROR;
3438}
3439
Manu Abrahame415c682009-04-06 15:45:20 -03003440static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
3441{
3442 struct stv090x_state *state = fe->demodulator_priv;
3443 u32 reg;
3444 u8 search_state;
Manu Abrahame415c682009-04-06 15:45:20 -03003445
3446 reg = STV090x_READ_DEMOD(state, DMDSTATE);
3447 search_state = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
3448
3449 switch (search_state) {
3450 case 0: /* searching */
3451 case 1: /* first PLH detected */
3452 default:
3453 dprintk(FE_DEBUG, 1, "Status: Unlocked (Searching ..)");
Andreas Regel9629c5b2009-04-23 14:58:36 -03003454 *status = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003455 break;
3456
3457 case 2: /* DVB-S2 mode */
3458 dprintk(FE_DEBUG, 1, "Delivery system: DVB-S2");
3459 reg = STV090x_READ_DEMOD(state, DSTATUS);
3460 if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
Andreas Regel1d436172009-11-13 18:19:54 -03003461 reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
3462 if (STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD)) {
3463 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3464 if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
Andreas Regelc369b7c2010-01-05 19:21:30 -03003465 *status = FE_HAS_SIGNAL |
3466 FE_HAS_CARRIER |
Andreas Regel1d436172009-11-13 18:19:54 -03003467 FE_HAS_VITERBI |
3468 FE_HAS_SYNC |
3469 FE_HAS_LOCK;
3470 }
Manu Abrahame415c682009-04-06 15:45:20 -03003471 }
3472 }
3473 break;
3474
3475 case 3: /* DVB-S1/legacy mode */
3476 dprintk(FE_DEBUG, 1, "Delivery system: DVB-S");
3477 reg = STV090x_READ_DEMOD(state, DSTATUS);
3478 if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
3479 reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
3480 if (STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD)) {
3481 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3482 if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
Andreas Regelc369b7c2010-01-05 19:21:30 -03003483 *status = FE_HAS_SIGNAL |
3484 FE_HAS_CARRIER |
3485 FE_HAS_VITERBI |
3486 FE_HAS_SYNC |
3487 FE_HAS_LOCK;
Manu Abrahame415c682009-04-06 15:45:20 -03003488 }
3489 }
3490 }
3491 break;
3492 }
3493
Andreas Regel9629c5b2009-04-23 14:58:36 -03003494 return 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003495}
3496
3497static int stv090x_read_per(struct dvb_frontend *fe, u32 *per)
3498{
3499 struct stv090x_state *state = fe->demodulator_priv;
3500
3501 s32 count_4, count_3, count_2, count_1, count_0, count;
3502 u32 reg, h, m, l;
3503 enum fe_status status;
3504
Andreas Regel9629c5b2009-04-23 14:58:36 -03003505 stv090x_read_status(fe, &status);
3506 if (!(status & FE_HAS_LOCK)) {
Manu Abrahame415c682009-04-06 15:45:20 -03003507 *per = 1 << 23; /* Max PER */
3508 } else {
3509 /* Counter 2 */
3510 reg = STV090x_READ_DEMOD(state, ERRCNT22);
3511 h = STV090x_GETFIELD_Px(reg, ERR_CNT2_FIELD);
3512
3513 reg = STV090x_READ_DEMOD(state, ERRCNT21);
3514 m = STV090x_GETFIELD_Px(reg, ERR_CNT21_FIELD);
3515
3516 reg = STV090x_READ_DEMOD(state, ERRCNT20);
3517 l = STV090x_GETFIELD_Px(reg, ERR_CNT20_FIELD);
3518
3519 *per = ((h << 16) | (m << 8) | l);
3520
3521 count_4 = STV090x_READ_DEMOD(state, FBERCPT4);
3522 count_3 = STV090x_READ_DEMOD(state, FBERCPT3);
3523 count_2 = STV090x_READ_DEMOD(state, FBERCPT2);
3524 count_1 = STV090x_READ_DEMOD(state, FBERCPT1);
3525 count_0 = STV090x_READ_DEMOD(state, FBERCPT0);
3526
3527 if ((!count_4) && (!count_3)) {
3528 count = (count_2 & 0xff) << 16;
3529 count |= (count_1 & 0xff) << 8;
3530 count |= count_0 & 0xff;
3531 } else {
3532 count = 1 << 24;
3533 }
3534 if (count == 0)
3535 *per = 1;
3536 }
3537 if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0) < 0)
3538 goto err;
3539 if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3540 goto err;
3541
3542 return 0;
3543err:
3544 dprintk(FE_ERROR, 1, "I/O error");
3545 return -1;
3546}
3547
3548static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
3549{
3550 int res = 0;
3551 int min = 0, med;
3552
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003553 if ((val >= tab[min].read && val < tab[max].read) ||
3554 (val >= tab[max].read && val < tab[min].read)) {
Manu Abrahame415c682009-04-06 15:45:20 -03003555 while ((max - min) > 1) {
3556 med = (max + min) / 2;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003557 if ((val >= tab[min].read && val < tab[med].read) ||
3558 (val >= tab[med].read && val < tab[min].read))
Manu Abrahame415c682009-04-06 15:45:20 -03003559 max = med;
3560 else
3561 min = med;
3562 }
3563 res = ((val - tab[min].read) *
3564 (tab[max].real - tab[min].real) /
3565 (tab[max].read - tab[min].read)) +
3566 tab[min].real;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003567 } else {
3568 if (tab[min].read < tab[max].read) {
3569 if (val < tab[min].read)
3570 res = tab[min].real;
3571 else if (val >= tab[max].read)
3572 res = tab[max].real;
3573 } else {
3574 if (val >= tab[min].read)
3575 res = tab[min].real;
3576 else if (val < tab[max].read)
3577 res = tab[max].real;
3578 }
Manu Abrahame415c682009-04-06 15:45:20 -03003579 }
3580
3581 return res;
3582}
3583
3584static int stv090x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
3585{
3586 struct stv090x_state *state = fe->demodulator_priv;
3587 u32 reg;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003588 s32 agc_0, agc_1, agc;
3589 s32 str;
Manu Abrahame415c682009-04-06 15:45:20 -03003590
3591 reg = STV090x_READ_DEMOD(state, AGCIQIN1);
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003592 agc_1 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
3593 reg = STV090x_READ_DEMOD(state, AGCIQIN0);
3594 agc_0 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
3595 agc = MAKEWORD16(agc_1, agc_0);
Manu Abrahame415c682009-04-06 15:45:20 -03003596
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003597 str = stv090x_table_lookup(stv090x_rf_tab,
3598 ARRAY_SIZE(stv090x_rf_tab) - 1, agc);
Manu Abrahame415c682009-04-06 15:45:20 -03003599 if (agc > stv090x_rf_tab[0].read)
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003600 str = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003601 else if (agc < stv090x_rf_tab[ARRAY_SIZE(stv090x_rf_tab) - 1].read)
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003602 str = -100;
3603 *strength = (str + 100) * 0xFFFF / 100;
Manu Abrahame415c682009-04-06 15:45:20 -03003604
3605 return 0;
3606}
3607
3608static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
3609{
3610 struct stv090x_state *state = fe->demodulator_priv;
3611 u32 reg_0, reg_1, reg, i;
3612 s32 val_0, val_1, val = 0;
3613 u8 lock_f;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003614 s32 div;
3615 u32 last;
Manu Abrahame415c682009-04-06 15:45:20 -03003616
3617 switch (state->delsys) {
3618 case STV090x_DVBS2:
3619 reg = STV090x_READ_DEMOD(state, DSTATUS);
3620 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
3621 if (lock_f) {
3622 msleep(5);
3623 for (i = 0; i < 16; i++) {
3624 reg_1 = STV090x_READ_DEMOD(state, NNOSPLHT1);
3625 val_1 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
3626 reg_0 = STV090x_READ_DEMOD(state, NNOSPLHT0);
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003627 val_0 = STV090x_GETFIELD_Px(reg_0, NOSPLHT_NORMED_FIELD);
Manu Abrahame415c682009-04-06 15:45:20 -03003628 val += MAKEWORD16(val_1, val_0);
3629 msleep(1);
3630 }
3631 val /= 16;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003632 last = ARRAY_SIZE(stv090x_s2cn_tab) - 1;
3633 div = stv090x_s2cn_tab[0].read -
3634 stv090x_s2cn_tab[last].read;
3635 *cnr = 0xFFFF - ((val * 0xFFFF) / div);
Manu Abrahame415c682009-04-06 15:45:20 -03003636 }
3637 break;
3638
3639 case STV090x_DVBS1:
3640 case STV090x_DSS:
3641 reg = STV090x_READ_DEMOD(state, DSTATUS);
3642 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
3643 if (lock_f) {
3644 msleep(5);
3645 for (i = 0; i < 16; i++) {
3646 reg_1 = STV090x_READ_DEMOD(state, NOSDATAT1);
3647 val_1 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
3648 reg_0 = STV090x_READ_DEMOD(state, NOSDATAT0);
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003649 val_0 = STV090x_GETFIELD_Px(reg_0, NOSDATAT_UNNORMED_FIELD);
Manu Abrahame415c682009-04-06 15:45:20 -03003650 val += MAKEWORD16(val_1, val_0);
3651 msleep(1);
3652 }
3653 val /= 16;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003654 last = ARRAY_SIZE(stv090x_s1cn_tab) - 1;
3655 div = stv090x_s1cn_tab[0].read -
3656 stv090x_s1cn_tab[last].read;
3657 *cnr = 0xFFFF - ((val * 0xFFFF) / div);
Manu Abrahame415c682009-04-06 15:45:20 -03003658 }
3659 break;
3660 default:
3661 break;
3662 }
3663
3664 return 0;
3665}
3666
3667static int stv090x_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
3668{
3669 struct stv090x_state *state = fe->demodulator_priv;
3670 u32 reg;
3671
3672 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3673 switch (tone) {
3674 case SEC_TONE_ON:
3675 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
3676 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3677 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3678 goto err;
3679 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3680 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3681 goto err;
3682 break;
3683
3684 case SEC_TONE_OFF:
3685 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
3686 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3687 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3688 goto err;
3689 break;
3690 default:
3691 return -EINVAL;
3692 }
3693
3694 return 0;
3695err:
3696 dprintk(FE_ERROR, 1, "I/O error");
3697 return -1;
3698}
3699
3700
3701static enum dvbfe_algo stv090x_frontend_algo(struct dvb_frontend *fe)
3702{
3703 return DVBFE_ALGO_CUSTOM;
3704}
3705
3706static int stv090x_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
3707{
3708 struct stv090x_state *state = fe->demodulator_priv;
3709 u32 reg, idle = 0, fifo_full = 1;
3710 int i;
3711
3712 reg = STV090x_READ_DEMOD(state, DISTXCTL);
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003713
Oliver Endriss30e8ca22009-12-13 09:02:39 -03003714 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD,
3715 (state->config->diseqc_envelope_mode) ? 4 : 2);
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003716 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3717 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3718 goto err;
3719 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3720 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3721 goto err;
3722
Manu Abrahame415c682009-04-06 15:45:20 -03003723 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3724 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3725 goto err;
3726
3727 for (i = 0; i < cmd->msg_len; i++) {
3728
3729 while (fifo_full) {
3730 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3731 fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
3732 }
3733
3734 if (STV090x_WRITE_DEMOD(state, DISTXDATA, cmd->msg[i]) < 0)
3735 goto err;
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003736 }
3737 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3738 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
3739 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3740 goto err;
3741
3742 i = 0;
3743
3744 while ((!idle) && (i < 10)) {
3745 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3746 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3747 msleep(10);
Manu Abrahame415c682009-04-06 15:45:20 -03003748 i++;
3749 }
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003750
3751 return 0;
3752err:
3753 dprintk(FE_ERROR, 1, "I/O error");
3754 return -1;
3755}
3756
3757static int stv090x_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
3758{
3759 struct stv090x_state *state = fe->demodulator_priv;
3760 u32 reg, idle = 0, fifo_full = 1;
3761 u8 mode, value;
3762 int i;
3763
3764 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3765
3766 if (burst == SEC_MINI_A) {
Oliver Endriss30e8ca22009-12-13 09:02:39 -03003767 mode = (state->config->diseqc_envelope_mode) ? 5 : 3;
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003768 value = 0x00;
3769 } else {
Oliver Endriss30e8ca22009-12-13 09:02:39 -03003770 mode = (state->config->diseqc_envelope_mode) ? 4 : 2;
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003771 value = 0xFF;
3772 }
3773
3774 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, mode);
3775 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3776 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3777 goto err;
3778 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3779 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3780 goto err;
3781
3782 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3783 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3784 goto err;
3785
3786 while (fifo_full) {
3787 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3788 fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
3789 }
3790
3791 if (STV090x_WRITE_DEMOD(state, DISTXDATA, value) < 0)
3792 goto err;
3793
Manu Abrahame415c682009-04-06 15:45:20 -03003794 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3795 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
3796 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3797 goto err;
3798
3799 i = 0;
3800
3801 while ((!idle) && (i < 10)) {
3802 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3803 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3804 msleep(10);
3805 i++;
3806 }
3807
3808 return 0;
3809err:
3810 dprintk(FE_ERROR, 1, "I/O error");
3811 return -1;
3812}
3813
3814static int stv090x_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
3815{
3816 struct stv090x_state *state = fe->demodulator_priv;
3817 u32 reg = 0, i = 0, rx_end = 0;
3818
3819 while ((rx_end != 1) && (i < 10)) {
3820 msleep(10);
3821 i++;
3822 reg = STV090x_READ_DEMOD(state, DISRX_ST0);
3823 rx_end = STV090x_GETFIELD_Px(reg, RX_END_FIELD);
3824 }
3825
3826 if (rx_end) {
3827 reply->msg_len = STV090x_GETFIELD_Px(reg, FIFO_BYTENBR_FIELD);
3828 for (i = 0; i < reply->msg_len; i++)
3829 reply->msg[i] = STV090x_READ_DEMOD(state, DISRXDATA);
3830 }
3831
3832 return 0;
3833}
3834
3835static int stv090x_sleep(struct dvb_frontend *fe)
3836{
3837 struct stv090x_state *state = fe->demodulator_priv;
3838 u32 reg;
3839
3840 dprintk(FE_DEBUG, 1, "Set %s to sleep",
3841 state->device == STV0900 ? "STV0900" : "STV0903");
3842
3843 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3844 STV090x_SETFIELD(reg, STANDBY_FIELD, 0x01);
3845 if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
3846 goto err;
3847
Manu Abraham26b03bc2009-04-08 18:27:10 -03003848 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
3849 STV090x_SETFIELD(reg, ADC1_PON_FIELD, 0);
3850 if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
3851 goto err;
3852
Manu Abrahame415c682009-04-06 15:45:20 -03003853 return 0;
3854err:
3855 dprintk(FE_ERROR, 1, "I/O error");
3856 return -1;
3857}
3858
3859static int stv090x_wakeup(struct dvb_frontend *fe)
3860{
3861 struct stv090x_state *state = fe->demodulator_priv;
3862 u32 reg;
3863
3864 dprintk(FE_DEBUG, 1, "Wake %s from standby",
3865 state->device == STV0900 ? "STV0900" : "STV0903");
3866
3867 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3868 STV090x_SETFIELD(reg, STANDBY_FIELD, 0x00);
3869 if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
3870 goto err;
3871
Manu Abraham26b03bc2009-04-08 18:27:10 -03003872 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
3873 STV090x_SETFIELD(reg, ADC1_PON_FIELD, 1);
3874 if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
3875 goto err;
3876
Manu Abrahame415c682009-04-06 15:45:20 -03003877 return 0;
3878err:
3879 dprintk(FE_ERROR, 1, "I/O error");
3880 return -1;
3881}
3882
3883static void stv090x_release(struct dvb_frontend *fe)
3884{
3885 struct stv090x_state *state = fe->demodulator_priv;
3886
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003887 state->internal->num_used--;
3888 if (state->internal->num_used <= 0) {
3889
3890 dprintk(FE_ERROR, 1, "Actually removing");
3891
3892 remove_dev(state->internal);
3893 kfree(state->internal);
3894 }
3895
Manu Abrahame415c682009-04-06 15:45:20 -03003896 kfree(state);
3897}
3898
3899static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc_mode)
3900{
Manu Abraham27d40322009-05-02 18:26:58 -03003901 u32 reg = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003902
Andreas Regela4978a82009-11-13 18:17:45 -03003903 reg = stv090x_read_reg(state, STV090x_GENCFG);
3904
Manu Abrahame415c682009-04-06 15:45:20 -03003905 switch (ldpc_mode) {
3906 case STV090x_DUAL:
3907 default:
Manu Abrahame415c682009-04-06 15:45:20 -03003908 if ((state->demod_mode != STV090x_DUAL) || (STV090x_GETFIELD(reg, DDEMOD_FIELD) != 1)) {
Manu Abraham27d40322009-05-02 18:26:58 -03003909 /* set LDPC to dual mode */
3910 if (stv090x_write_reg(state, STV090x_GENCFG, 0x1d) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003911 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003912
Manu Abrahame415c682009-04-06 15:45:20 -03003913 state->demod_mode = STV090x_DUAL;
Manu Abraham27d40322009-05-02 18:26:58 -03003914
Manu Abrahame415c682009-04-06 15:45:20 -03003915 reg = stv090x_read_reg(state, STV090x_TSTRES0);
3916 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
3917 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3918 goto err;
3919 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
3920 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3921 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003922
3923 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
3924 goto err;
3925 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
3926 goto err;
3927 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
3928 goto err;
3929 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
3930 goto err;
3931 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
3932 goto err;
3933 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
3934 goto err;
3935 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
3936 goto err;
3937
3938 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
3939 goto err;
3940 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
3941 goto err;
3942 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
3943 goto err;
3944 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
3945 goto err;
3946 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
3947 goto err;
3948 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
3949 goto err;
3950 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
3951 goto err;
3952
3953 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
3954 goto err;
3955 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
3956 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003957 }
3958 break;
3959
3960 case STV090x_SINGLE:
Manu Abraham27d40322009-05-02 18:26:58 -03003961 if (stv090x_stop_modcod(state) < 0)
3962 goto err;
3963 if (stv090x_activate_modcod_single(state) < 0)
3964 goto err;
3965
Manu Abrahame415c682009-04-06 15:45:20 -03003966 if (state->demod == STV090x_DEMODULATOR_1) {
3967 if (stv090x_write_reg(state, STV090x_GENCFG, 0x06) < 0) /* path 2 */
3968 goto err;
3969 } else {
3970 if (stv090x_write_reg(state, STV090x_GENCFG, 0x04) < 0) /* path 1 */
3971 goto err;
3972 }
3973
3974 reg = stv090x_read_reg(state, STV090x_TSTRES0);
3975 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
3976 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3977 goto err;
3978 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
3979 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3980 goto err;
3981
3982 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
3983 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x01);
3984 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
3985 goto err;
3986 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x00);
3987 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
3988 goto err;
3989 break;
3990 }
3991
3992 return 0;
3993err:
3994 dprintk(FE_ERROR, 1, "I/O error");
3995 return -1;
3996}
3997
3998/* return (Hz), clk in Hz*/
3999static u32 stv090x_get_mclk(struct stv090x_state *state)
4000{
4001 const struct stv090x_config *config = state->config;
4002 u32 div, reg;
4003 u8 ratio;
4004
4005 div = stv090x_read_reg(state, STV090x_NCOARSE);
4006 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
4007 ratio = STV090x_GETFIELD(reg, SELX1RATIO_FIELD) ? 4 : 6;
4008
4009 return (div + 1) * config->xtal / ratio; /* kHz */
4010}
4011
4012static int stv090x_set_mclk(struct stv090x_state *state, u32 mclk, u32 clk)
4013{
4014 const struct stv090x_config *config = state->config;
4015 u32 reg, div, clk_sel;
4016
4017 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
4018 clk_sel = ((STV090x_GETFIELD(reg, SELX1RATIO_FIELD) == 1) ? 4 : 6);
4019
4020 div = ((clk_sel * mclk) / config->xtal) - 1;
4021
4022 reg = stv090x_read_reg(state, STV090x_NCOARSE);
4023 STV090x_SETFIELD(reg, M_DIV_FIELD, div);
4024 if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0)
4025 goto err;
4026
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004027 state->internal->mclk = stv090x_get_mclk(state);
Manu Abrahame415c682009-04-06 15:45:20 -03004028
Manu Abraham94a80912009-04-08 19:45:43 -03004029 /*Set the DiseqC frequency to 22KHz */
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004030 div = state->internal->mclk / 704000;
Manu Abraham94a80912009-04-08 19:45:43 -03004031 if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0)
4032 goto err;
4033 if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
4034 goto err;
4035
Manu Abrahame415c682009-04-06 15:45:20 -03004036 return 0;
4037err:
4038 dprintk(FE_ERROR, 1, "I/O error");
4039 return -1;
4040}
4041
4042static int stv090x_set_tspath(struct stv090x_state *state)
4043{
4044 u32 reg;
4045
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004046 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03004047 switch (state->config->ts1_mode) {
4048 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4049 case STV090x_TSMODE_DVBCI:
4050 switch (state->config->ts2_mode) {
4051 case STV090x_TSMODE_SERIAL_PUNCTURED:
4052 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4053 default:
4054 stv090x_write_reg(state, STV090x_TSGENERAL, 0x00);
4055 break;
4056
4057 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4058 case STV090x_TSMODE_DVBCI:
4059 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x06) < 0) /* Mux'd stream mode */
4060 goto err;
4061 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4062 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4063 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4064 goto err;
4065 reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
4066 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4067 if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
4068 goto err;
4069 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
4070 goto err;
4071 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
4072 goto err;
4073 break;
4074 }
4075 break;
4076
4077 case STV090x_TSMODE_SERIAL_PUNCTURED:
4078 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4079 default:
4080 switch (state->config->ts2_mode) {
4081 case STV090x_TSMODE_SERIAL_PUNCTURED:
4082 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4083 default:
4084 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
4085 goto err;
4086 break;
4087
4088 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4089 case STV090x_TSMODE_DVBCI:
4090 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0a) < 0)
4091 goto err;
4092 break;
4093 }
4094 break;
4095 }
4096 } else {
4097 switch (state->config->ts1_mode) {
4098 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4099 case STV090x_TSMODE_DVBCI:
4100 switch (state->config->ts2_mode) {
4101 case STV090x_TSMODE_SERIAL_PUNCTURED:
4102 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4103 default:
Manu Abraham56571502009-04-07 16:08:26 -03004104 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x10);
Manu Abrahame415c682009-04-06 15:45:20 -03004105 break;
4106
4107 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4108 case STV090x_TSMODE_DVBCI:
Manu Abraham56571502009-04-07 16:08:26 -03004109 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x16);
Manu Abrahame415c682009-04-06 15:45:20 -03004110 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4111 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4112 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4113 goto err;
4114 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4115 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 0);
4116 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4117 goto err;
4118 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
4119 goto err;
4120 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
4121 goto err;
4122 break;
4123 }
4124 break;
4125
4126 case STV090x_TSMODE_SERIAL_PUNCTURED:
4127 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4128 default:
4129 switch (state->config->ts2_mode) {
4130 case STV090x_TSMODE_SERIAL_PUNCTURED:
4131 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4132 default:
Manu Abraham56571502009-04-07 16:08:26 -03004133 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
Manu Abrahame415c682009-04-06 15:45:20 -03004134 break;
4135
4136 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4137 case STV090x_TSMODE_DVBCI:
Manu Abraham56571502009-04-07 16:08:26 -03004138 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x12);
Manu Abrahame415c682009-04-06 15:45:20 -03004139 break;
4140 }
4141 break;
4142 }
4143 }
4144
4145 switch (state->config->ts1_mode) {
4146 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4147 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4148 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4149 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4150 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4151 goto err;
4152 break;
4153
4154 case STV090x_TSMODE_DVBCI:
4155 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4156 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4157 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4158 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4159 goto err;
4160 break;
4161
4162 case STV090x_TSMODE_SERIAL_PUNCTURED:
4163 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4164 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4165 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4166 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4167 goto err;
4168 break;
4169
4170 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4171 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4172 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4173 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4174 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4175 goto err;
4176 break;
4177
4178 default:
4179 break;
4180 }
4181
4182 switch (state->config->ts2_mode) {
4183 case STV090x_TSMODE_PARALLEL_PUNCTURED:
Andreas Regel64104dc2009-04-16 08:43:41 -03004184 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004185 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4186 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
Andreas Regel64104dc2009-04-16 08:43:41 -03004187 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004188 goto err;
4189 break;
4190
4191 case STV090x_TSMODE_DVBCI:
Andreas Regel64104dc2009-04-16 08:43:41 -03004192 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004193 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4194 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
Andreas Regel64104dc2009-04-16 08:43:41 -03004195 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004196 goto err;
4197 break;
4198
4199 case STV090x_TSMODE_SERIAL_PUNCTURED:
Andreas Regel64104dc2009-04-16 08:43:41 -03004200 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004201 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4202 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
Andreas Regel64104dc2009-04-16 08:43:41 -03004203 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004204 goto err;
4205 break;
4206
4207 case STV090x_TSMODE_SERIAL_CONTINUOUS:
Andreas Regel64104dc2009-04-16 08:43:41 -03004208 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004209 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4210 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
Andreas Regel64104dc2009-04-16 08:43:41 -03004211 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004212 goto err;
4213 break;
4214
4215 default:
4216 break;
4217 }
Andreas Regelf91e59c2010-01-05 19:22:07 -03004218
4219 if (state->config->ts1_clk > 0) {
4220 u32 speed;
4221
4222 switch (state->config->ts1_mode) {
4223 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4224 case STV090x_TSMODE_DVBCI:
4225 default:
4226 speed = state->internal->mclk /
4227 (state->config->ts1_clk / 4);
4228 if (speed < 0x08)
4229 speed = 0x08;
4230 if (speed > 0xFF)
4231 speed = 0xFF;
4232 break;
4233 case STV090x_TSMODE_SERIAL_PUNCTURED:
4234 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4235 speed = state->internal->mclk /
4236 (state->config->ts1_clk / 32);
4237 if (speed < 0x20)
4238 speed = 0x20;
4239 if (speed > 0xFF)
4240 speed = 0xFF;
4241 break;
4242 }
4243 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4244 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4245 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4246 goto err;
4247 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, speed) < 0)
4248 goto err;
4249 }
4250
4251 if (state->config->ts2_clk > 0) {
4252 u32 speed;
4253
4254 switch (state->config->ts2_mode) {
4255 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4256 case STV090x_TSMODE_DVBCI:
4257 default:
4258 speed = state->internal->mclk /
4259 (state->config->ts2_clk / 4);
4260 if (speed < 0x08)
4261 speed = 0x08;
4262 if (speed > 0xFF)
4263 speed = 0xFF;
4264 break;
4265 case STV090x_TSMODE_SERIAL_PUNCTURED:
4266 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4267 speed = state->internal->mclk /
4268 (state->config->ts2_clk / 32);
4269 if (speed < 0x20)
4270 speed = 0x20;
4271 if (speed > 0xFF)
4272 speed = 0xFF;
4273 break;
4274 }
4275 reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
4276 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4277 if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
4278 goto err;
4279 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, speed) < 0)
4280 goto err;
4281 }
4282
Manu Abrahame415c682009-04-06 15:45:20 -03004283 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4284 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
4285 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4286 goto err;
4287 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4288 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4289 goto err;
4290
4291 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4292 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
4293 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4294 goto err;
4295 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4296 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4297 goto err;
4298
4299 return 0;
4300err:
4301 dprintk(FE_ERROR, 1, "I/O error");
4302 return -1;
4303}
4304
4305static int stv090x_init(struct dvb_frontend *fe)
4306{
4307 struct stv090x_state *state = fe->demodulator_priv;
4308 const struct stv090x_config *config = state->config;
4309 u32 reg;
4310
Andreas Regel9045e722010-01-05 19:23:41 -03004311 if (state->internal->mclk == 0) {
4312 stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
4313 msleep(5);
4314 if (stv090x_write_reg(state, STV090x_SYNTCTRL,
4315 0x20 | config->clk_mode) < 0)
4316 goto err;
4317 stv090x_get_mclk(state);
4318 }
4319
Andreas Regelcbc320d2009-04-23 14:59:03 -03004320 if (stv090x_wakeup(fe) < 0) {
4321 dprintk(FE_ERROR, 1, "Error waking device");
4322 goto err;
4323 }
4324
Manu Abraham27d40322009-05-02 18:26:58 -03004325 if (stv090x_ldpc_mode(state, state->demod_mode) < 0)
4326 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03004327
4328 reg = STV090x_READ_DEMOD(state, TNRCFG2);
4329 STV090x_SETFIELD_Px(reg, TUN_IQSWAP_FIELD, state->inversion);
4330 if (STV090x_WRITE_DEMOD(state, TNRCFG2, reg) < 0)
4331 goto err;
4332 reg = STV090x_READ_DEMOD(state, DEMOD);
4333 STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
4334 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
4335 goto err;
4336
Manu Abraham27d40322009-05-02 18:26:58 -03004337 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
4338 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03004339
Manu Abraham27d40322009-05-02 18:26:58 -03004340 if (config->tuner_set_mode) {
4341 if (config->tuner_set_mode(fe, TUNER_WAKE) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03004342 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03004343 }
Manu Abrahame415c682009-04-06 15:45:20 -03004344
Manu Abraham27d40322009-05-02 18:26:58 -03004345 if (config->tuner_init) {
4346 if (config->tuner_init(fe) < 0)
Oliver Endriss2c1f7502010-01-10 15:38:38 -03004347 goto err_gateoff;
Manu Abraham27d40322009-05-02 18:26:58 -03004348 }
Manu Abrahame415c682009-04-06 15:45:20 -03004349
Manu Abraham27d40322009-05-02 18:26:58 -03004350 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
4351 goto err;
4352
4353 if (stv090x_set_tspath(state) < 0)
4354 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03004355
4356 return 0;
Oliver Endriss2c1f7502010-01-10 15:38:38 -03004357
4358err_gateoff:
4359 stv090x_i2c_gate_ctrl(fe, 0);
Manu Abrahame415c682009-04-06 15:45:20 -03004360err:
4361 dprintk(FE_ERROR, 1, "I/O error");
4362 return -1;
4363}
4364
4365static int stv090x_setup(struct dvb_frontend *fe)
4366{
4367 struct stv090x_state *state = fe->demodulator_priv;
4368 const struct stv090x_config *config = state->config;
4369 const struct stv090x_reg *stv090x_initval = NULL;
4370 const struct stv090x_reg *stv090x_cut20_val = NULL;
4371 unsigned long t1_size = 0, t2_size = 0;
Manu Abraham017eb0382009-04-07 05:19:54 -03004372 u32 reg = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03004373
4374 int i;
4375
4376 if (state->device == STV0900) {
4377 dprintk(FE_DEBUG, 1, "Initializing STV0900");
4378 stv090x_initval = stv0900_initval;
4379 t1_size = ARRAY_SIZE(stv0900_initval);
4380 stv090x_cut20_val = stv0900_cut20_val;
4381 t2_size = ARRAY_SIZE(stv0900_cut20_val);
4382 } else if (state->device == STV0903) {
4383 dprintk(FE_DEBUG, 1, "Initializing STV0903");
4384 stv090x_initval = stv0903_initval;
4385 t1_size = ARRAY_SIZE(stv0903_initval);
4386 stv090x_cut20_val = stv0903_cut20_val;
4387 t2_size = ARRAY_SIZE(stv0903_cut20_val);
4388 }
4389
4390 /* STV090x init */
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004391
4392 /* Stop Demod */
4393 if (stv090x_write_reg(state, STV090x_P1_DMDISTATE, 0x5c) < 0)
4394 goto err;
4395 if (stv090x_write_reg(state, STV090x_P2_DMDISTATE, 0x5c) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004396 goto err;
4397
4398 msleep(5);
4399
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004400 /* Set No Tuner Mode */
4401 if (stv090x_write_reg(state, STV090x_P1_TNRCFG, 0x6c) < 0)
4402 goto err;
4403 if (stv090x_write_reg(state, STV090x_P2_TNRCFG, 0x6c) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004404 goto err;
4405
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004406 /* I2C repeater OFF */
Manu Abraham017eb0382009-04-07 05:19:54 -03004407 STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level);
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004408 if (stv090x_write_reg(state, STV090x_P1_I2CRPT, reg) < 0)
4409 goto err;
4410 if (stv090x_write_reg(state, STV090x_P2_I2CRPT, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004411 goto err;
4412
4413 if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
4414 goto err;
4415 msleep(5);
4416 if (stv090x_write_reg(state, STV090x_I2CCFG, 0x08) < 0) /* 1/41 oversampling */
4417 goto err;
4418 if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0) /* enable PLL */
4419 goto err;
4420 msleep(5);
4421
4422 /* write initval */
Andreas Regel2f5914b2009-04-23 14:56:41 -03004423 dprintk(FE_DEBUG, 1, "Setting up initial values");
Manu Abrahame415c682009-04-06 15:45:20 -03004424 for (i = 0; i < t1_size; i++) {
Manu Abrahame415c682009-04-06 15:45:20 -03004425 if (stv090x_write_reg(state, stv090x_initval[i].addr, stv090x_initval[i].data) < 0)
4426 goto err;
4427 }
4428
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004429 state->internal->dev_ver = stv090x_read_reg(state, STV090x_MID);
4430 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03004431 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
4432 goto err;
4433
4434 /* write cut20_val*/
4435 dprintk(FE_DEBUG, 1, "Setting up Cut 2.0 initial values");
4436 for (i = 0; i < t2_size; i++) {
4437 if (stv090x_write_reg(state, stv090x_cut20_val[i].addr, stv090x_cut20_val[i].data) < 0)
4438 goto err;
4439 }
Manu Abraham27d40322009-05-02 18:26:58 -03004440
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004441 } else if (state->internal->dev_ver < 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03004442 dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!",
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004443 state->internal->dev_ver);
Manu Abraham27d40322009-05-02 18:26:58 -03004444
4445 goto err;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004446 } else if (state->internal->dev_ver > 0x30) {
Manu Abraham27d40322009-05-02 18:26:58 -03004447 /* we shouldn't bail out from here */
4448 dprintk(FE_ERROR, 1, "INFO: Cut: 0x%02x probably incomplete support!",
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004449 state->internal->dev_ver);
Manu Abrahame415c682009-04-06 15:45:20 -03004450 }
4451
Oliver Endrissd8b5a8e2010-01-10 15:40:28 -03004452 /* ADC1 range */
4453 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
4454 STV090x_SETFIELD(reg, ADC1_INMODE_FIELD,
4455 (config->adc1_range == STV090x_ADC_1Vpp) ? 0 : 1);
4456 if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
4457 goto err;
4458
4459 /* ADC2 range */
4460 reg = stv090x_read_reg(state, STV090x_TSTTNR3);
4461 STV090x_SETFIELD(reg, ADC2_INMODE_FIELD,
4462 (config->adc2_range == STV090x_ADC_1Vpp) ? 0 : 1);
4463 if (stv090x_write_reg(state, STV090x_TSTTNR3, reg) < 0)
4464 goto err;
4465
Manu Abrahame415c682009-04-06 15:45:20 -03004466 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
4467 goto err;
4468 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x00) < 0)
4469 goto err;
4470
Manu Abrahame415c682009-04-06 15:45:20 -03004471 return 0;
4472err:
4473 dprintk(FE_ERROR, 1, "I/O error");
4474 return -1;
4475}
4476
4477static struct dvb_frontend_ops stv090x_ops = {
4478
4479 .info = {
4480 .name = "STV090x Multistandard",
Andreas Regel7fc08bb2009-04-23 15:00:40 -03004481 .type = FE_QPSK,
4482 .frequency_min = 950000,
4483 .frequency_max = 2150000,
4484 .frequency_stepsize = 0,
4485 .frequency_tolerance = 0,
4486 .symbol_rate_min = 1000000,
4487 .symbol_rate_max = 45000000,
4488 .caps = FE_CAN_INVERSION_AUTO |
4489 FE_CAN_FEC_AUTO |
4490 FE_CAN_QPSK |
4491 FE_CAN_2G_MODULATION
Manu Abrahame415c682009-04-06 15:45:20 -03004492 },
4493
4494 .release = stv090x_release,
4495 .init = stv090x_init,
4496
4497 .sleep = stv090x_sleep,
4498 .get_frontend_algo = stv090x_frontend_algo,
4499
4500 .i2c_gate_ctrl = stv090x_i2c_gate_ctrl,
4501
4502 .diseqc_send_master_cmd = stv090x_send_diseqc_msg,
Andreas Regelf9ed95d2009-04-08 17:27:51 -03004503 .diseqc_send_burst = stv090x_send_diseqc_burst,
Manu Abrahame415c682009-04-06 15:45:20 -03004504 .diseqc_recv_slave_reply = stv090x_recv_slave_reply,
4505 .set_tone = stv090x_set_tone,
4506
4507 .search = stv090x_search,
4508 .read_status = stv090x_read_status,
4509 .read_ber = stv090x_read_per,
4510 .read_signal_strength = stv090x_read_signal_strength,
4511 .read_snr = stv090x_read_cnr
4512};
4513
4514
4515struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
4516 struct i2c_adapter *i2c,
4517 enum stv090x_demodulator demod)
4518{
4519 struct stv090x_state *state = NULL;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004520 struct stv090x_dev *temp_int;
Manu Abrahame415c682009-04-06 15:45:20 -03004521
4522 state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL);
4523 if (state == NULL)
4524 goto error;
4525
4526 state->verbose = &verbose;
4527 state->config = config;
4528 state->i2c = i2c;
4529 state->frontend.ops = stv090x_ops;
4530 state->frontend.demodulator_priv = state;
Manu Abraham56571502009-04-07 16:08:26 -03004531 state->demod = demod;
Manu Abrahame415c682009-04-06 15:45:20 -03004532 state->demod_mode = config->demod_mode; /* Single or Dual mode */
4533 state->device = config->device;
Andreas Regel4e58a682009-04-16 08:40:36 -03004534 state->rolloff = STV090x_RO_35; /* default */
Manu Abrahame415c682009-04-06 15:45:20 -03004535
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004536 temp_int = find_dev(state->i2c,
4537 state->config->address);
4538
4539 if ((temp_int != NULL) && (state->demod_mode == STV090x_DUAL)) {
4540 state->internal = temp_int->internal;
4541 state->internal->num_used++;
4542 dprintk(FE_INFO, 1, "Found Internal Structure!");
4543 dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
4544 state->device == STV0900 ? "STV0900" : "STV0903",
4545 demod,
4546 state->internal->dev_ver);
4547 return &state->frontend;
4548 } else {
4549 state->internal = kmalloc(sizeof(struct stv090x_internal),
4550 GFP_KERNEL);
4551 temp_int = append_internal(state->internal);
4552 state->internal->num_used = 1;
Andreas Regel76b9ef92010-01-05 19:24:56 -03004553 state->internal->mclk = 0;
4554 state->internal->dev_ver = 0;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004555 state->internal->i2c_adap = state->i2c;
4556 state->internal->i2c_addr = state->config->address;
4557 dprintk(FE_INFO, 1, "Create New Internal Structure!");
4558 }
4559
4560 mutex_init(&state->internal->demod_lock);
Andreas Regel96506a52010-01-05 19:20:21 -03004561 mutex_init(&state->internal->tuner_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03004562
4563 if (stv090x_sleep(&state->frontend) < 0) {
4564 dprintk(FE_ERROR, 1, "Error putting device to sleep");
4565 goto error;
4566 }
4567
4568 if (stv090x_setup(&state->frontend) < 0) {
4569 dprintk(FE_ERROR, 1, "Error setting up device");
4570 goto error;
4571 }
4572 if (stv090x_wakeup(&state->frontend) < 0) {
4573 dprintk(FE_ERROR, 1, "Error waking device");
4574 goto error;
4575 }
Manu Abrahame415c682009-04-06 15:45:20 -03004576
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004577 dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
Manu Abrahame415c682009-04-06 15:45:20 -03004578 state->device == STV0900 ? "STV0900" : "STV0903",
4579 demod,
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004580 state->internal->dev_ver);
Manu Abrahame415c682009-04-06 15:45:20 -03004581
4582 return &state->frontend;
4583
4584error:
4585 kfree(state);
4586 return NULL;
4587}
4588EXPORT_SYMBOL(stv090x_attach);
4589MODULE_PARM_DESC(verbose, "Set Verbosity level");
4590MODULE_AUTHOR("Manu Abraham");
4591MODULE_DESCRIPTION("STV090x Multi-Std Broadcast frontend");
4592MODULE_LICENSE("GPL");