Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[pandora-kernel.git] / drivers / media / dvb / frontends / stv090x.c
1 /*
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/slab.h>
27 #include <linux/mutex.h>
28
29 #include <linux/dvb/frontend.h>
30 #include "dvb_frontend.h"
31
32 #include "stv6110x.h" /* for demodulator internal modes */
33
34 #include "stv090x_reg.h"
35 #include "stv090x.h"
36 #include "stv090x_priv.h"
37
38 static unsigned int verbose;
39 module_param(verbose, int, 0644);
40
41 /* internal params node */
42 struct stv090x_dev {
43         /* pointer for internal params, one for each pair of demods */
44         struct stv090x_internal         *internal;
45         struct stv090x_dev              *next_dev;
46 };
47
48 /* first internal params */
49 static struct stv090x_dev *stv090x_first_dev;
50
51 /* find chip by i2c adapter and i2c address */
52 static struct stv090x_dev *find_dev(struct i2c_adapter *i2c_adap,
53                                         u8 i2c_addr)
54 {
55         struct stv090x_dev *temp_dev = stv090x_first_dev;
56
57         /*
58          Search of the last stv0900 chip or
59          find it by i2c adapter and i2c address */
60         while ((temp_dev != NULL) &&
61                 ((temp_dev->internal->i2c_adap != i2c_adap) ||
62                 (temp_dev->internal->i2c_addr != i2c_addr))) {
63
64                 temp_dev = temp_dev->next_dev;
65         }
66
67         return temp_dev;
68 }
69
70 /* deallocating chip */
71 static void remove_dev(struct stv090x_internal *internal)
72 {
73         struct stv090x_dev *prev_dev = stv090x_first_dev;
74         struct stv090x_dev *del_dev = find_dev(internal->i2c_adap,
75                                                 internal->i2c_addr);
76
77         if (del_dev != NULL) {
78                 if (del_dev == stv090x_first_dev) {
79                         stv090x_first_dev = del_dev->next_dev;
80                 } else {
81                         while (prev_dev->next_dev != del_dev)
82                                 prev_dev = prev_dev->next_dev;
83
84                         prev_dev->next_dev = del_dev->next_dev;
85                 }
86
87                 kfree(del_dev);
88         }
89 }
90
91 /* allocating new chip */
92 static struct stv090x_dev *append_internal(struct stv090x_internal *internal)
93 {
94         struct stv090x_dev *new_dev;
95         struct stv090x_dev *temp_dev;
96
97         new_dev = kmalloc(sizeof(struct stv090x_dev), GFP_KERNEL);
98         if (new_dev != NULL) {
99                 new_dev->internal = internal;
100                 new_dev->next_dev = NULL;
101
102                 /* append to list */
103                 if (stv090x_first_dev == NULL) {
104                         stv090x_first_dev = new_dev;
105                 } else {
106                         temp_dev = stv090x_first_dev;
107                         while (temp_dev->next_dev != NULL)
108                                 temp_dev = temp_dev->next_dev;
109
110                         temp_dev->next_dev = new_dev;
111                 }
112         }
113
114         return new_dev;
115 }
116
117
118 /* DVBS1 and DSS C/N Lookup table */
119 static const struct stv090x_tab stv090x_s1cn_tab[] = {
120         {   0, 8917 }, /*  0.0dB */
121         {   5, 8801 }, /*  0.5dB */
122         {  10, 8667 }, /*  1.0dB */
123         {  15, 8522 }, /*  1.5dB */
124         {  20, 8355 }, /*  2.0dB */
125         {  25, 8175 }, /*  2.5dB */
126         {  30, 7979 }, /*  3.0dB */
127         {  35, 7763 }, /*  3.5dB */
128         {  40, 7530 }, /*  4.0dB */
129         {  45, 7282 }, /*  4.5dB */
130         {  50, 7026 }, /*  5.0dB */
131         {  55, 6781 }, /*  5.5dB */
132         {  60, 6514 }, /*  6.0dB */
133         {  65, 6241 }, /*  6.5dB */
134         {  70, 5965 }, /*  7.0dB */
135         {  75, 5690 }, /*  7.5dB */
136         {  80, 5424 }, /*  8.0dB */
137         {  85, 5161 }, /*  8.5dB */
138         {  90, 4902 }, /*  9.0dB */
139         {  95, 4654 }, /*  9.5dB */
140         { 100, 4417 }, /* 10.0dB */
141         { 105, 4186 }, /* 10.5dB */
142         { 110, 3968 }, /* 11.0dB */
143         { 115, 3757 }, /* 11.5dB */
144         { 120, 3558 }, /* 12.0dB */
145         { 125, 3366 }, /* 12.5dB */
146         { 130, 3185 }, /* 13.0dB */
147         { 135, 3012 }, /* 13.5dB */
148         { 140, 2850 }, /* 14.0dB */
149         { 145, 2698 }, /* 14.5dB */
150         { 150, 2550 }, /* 15.0dB */
151         { 160, 2283 }, /* 16.0dB */
152         { 170, 2042 }, /* 17.0dB */
153         { 180, 1827 }, /* 18.0dB */
154         { 190, 1636 }, /* 19.0dB */
155         { 200, 1466 }, /* 20.0dB */
156         { 210, 1315 }, /* 21.0dB */
157         { 220, 1181 }, /* 22.0dB */
158         { 230, 1064 }, /* 23.0dB */
159         { 240,  960 }, /* 24.0dB */
160         { 250,  869 }, /* 25.0dB */
161         { 260,  792 }, /* 26.0dB */
162         { 270,  724 }, /* 27.0dB */
163         { 280,  665 }, /* 28.0dB */
164         { 290,  616 }, /* 29.0dB */
165         { 300,  573 }, /* 30.0dB */
166         { 310,  537 }, /* 31.0dB */
167         { 320,  507 }, /* 32.0dB */
168         { 330,  483 }, /* 33.0dB */
169         { 400,  398 }, /* 40.0dB */
170         { 450,  381 }, /* 45.0dB */
171         { 500,  377 }  /* 50.0dB */
172 };
173
174 /* DVBS2 C/N Lookup table */
175 static const struct stv090x_tab stv090x_s2cn_tab[] = {
176         { -30, 13348 }, /* -3.0dB */
177         { -20, 12640 }, /* -2d.0B */
178         { -10, 11883 }, /* -1.0dB */
179         {   0, 11101 }, /* -0.0dB */
180         {   5, 10718 }, /*  0.5dB */
181         {  10, 10339 }, /*  1.0dB */
182         {  15,  9947 }, /*  1.5dB */
183         {  20,  9552 }, /*  2.0dB */
184         {  25,  9183 }, /*  2.5dB */
185         {  30,  8799 }, /*  3.0dB */
186         {  35,  8422 }, /*  3.5dB */
187         {  40,  8062 }, /*  4.0dB */
188         {  45,  7707 }, /*  4.5dB */
189         {  50,  7353 }, /*  5.0dB */
190         {  55,  7025 }, /*  5.5dB */
191         {  60,  6684 }, /*  6.0dB */
192         {  65,  6331 }, /*  6.5dB */
193         {  70,  6036 }, /*  7.0dB */
194         {  75,  5727 }, /*  7.5dB */
195         {  80,  5437 }, /*  8.0dB */
196         {  85,  5164 }, /*  8.5dB */
197         {  90,  4902 }, /*  9.0dB */
198         {  95,  4653 }, /*  9.5dB */
199         { 100,  4408 }, /* 10.0dB */
200         { 105,  4187 }, /* 10.5dB */
201         { 110,  3961 }, /* 11.0dB */
202         { 115,  3751 }, /* 11.5dB */
203         { 120,  3558 }, /* 12.0dB */
204         { 125,  3368 }, /* 12.5dB */
205         { 130,  3191 }, /* 13.0dB */
206         { 135,  3017 }, /* 13.5dB */
207         { 140,  2862 }, /* 14.0dB */
208         { 145,  2710 }, /* 14.5dB */
209         { 150,  2565 }, /* 15.0dB */
210         { 160,  2300 }, /* 16.0dB */
211         { 170,  2058 }, /* 17.0dB */
212         { 180,  1849 }, /* 18.0dB */
213         { 190,  1663 }, /* 19.0dB */
214         { 200,  1495 }, /* 20.0dB */
215         { 210,  1349 }, /* 21.0dB */
216         { 220,  1222 }, /* 22.0dB */
217         { 230,  1110 }, /* 23.0dB */
218         { 240,  1011 }, /* 24.0dB */
219         { 250,   925 }, /* 25.0dB */
220         { 260,   853 }, /* 26.0dB */
221         { 270,   789 }, /* 27.0dB */
222         { 280,   734 }, /* 28.0dB */
223         { 290,   690 }, /* 29.0dB */
224         { 300,   650 }, /* 30.0dB */
225         { 310,   619 }, /* 31.0dB */
226         { 320,   593 }, /* 32.0dB */
227         { 330,   571 }, /* 33.0dB */
228         { 400,   498 }, /* 40.0dB */
229         { 450,   484 }, /* 45.0dB */
230         { 500,   481 }  /* 50.0dB */
231 };
232
233 /* RF level C/N lookup table */
234 static const struct stv090x_tab stv090x_rf_tab[] = {
235         {  -5, 0xcaa1 }, /*  -5dBm */
236         { -10, 0xc229 }, /* -10dBm */
237         { -15, 0xbb08 }, /* -15dBm */
238         { -20, 0xb4bc }, /* -20dBm */
239         { -25, 0xad5a }, /* -25dBm */
240         { -30, 0xa298 }, /* -30dBm */
241         { -35, 0x98a8 }, /* -35dBm */
242         { -40, 0x8389 }, /* -40dBm */
243         { -45, 0x59be }, /* -45dBm */
244         { -50, 0x3a14 }, /* -50dBm */
245         { -55, 0x2d11 }, /* -55dBm */
246         { -60, 0x210d }, /* -60dBm */
247         { -65, 0xa14f }, /* -65dBm */
248         { -70, 0x07aa }  /* -70dBm */
249 };
250
251
252 static struct stv090x_reg stv0900_initval[] = {
253
254         { STV090x_OUTCFG,               0x00 },
255         { STV090x_MODECFG,              0xff },
256         { STV090x_AGCRF1CFG,            0x11 },
257         { STV090x_AGCRF2CFG,            0x13 },
258         { STV090x_TSGENERAL1X,          0x14 },
259         { STV090x_TSTTNR2,              0x21 },
260         { STV090x_TSTTNR4,              0x21 },
261         { STV090x_P2_DISTXCTL,          0x22 },
262         { STV090x_P2_F22TX,             0xc0 },
263         { STV090x_P2_F22RX,             0xc0 },
264         { STV090x_P2_DISRXCTL,          0x00 },
265         { STV090x_P2_DMDCFGMD,          0xF9 },
266         { STV090x_P2_DEMOD,             0x08 },
267         { STV090x_P2_DMDCFG3,           0xc4 },
268         { STV090x_P2_CARFREQ,           0xed },
269         { STV090x_P2_LDT,               0xd0 },
270         { STV090x_P2_LDT2,              0xb8 },
271         { STV090x_P2_TMGCFG,            0xd2 },
272         { STV090x_P2_TMGTHRISE,         0x20 },
273         { STV090x_P1_TMGCFG,            0xd2 },
274
275         { STV090x_P2_TMGTHFALL,         0x00 },
276         { STV090x_P2_FECSPY,            0x88 },
277         { STV090x_P2_FSPYDATA,          0x3a },
278         { STV090x_P2_FBERCPT4,          0x00 },
279         { STV090x_P2_FSPYBER,           0x10 },
280         { STV090x_P2_ERRCTRL1,          0x35 },
281         { STV090x_P2_ERRCTRL2,          0xc1 },
282         { STV090x_P2_CFRICFG,           0xf8 },
283         { STV090x_P2_NOSCFG,            0x1c },
284         { STV090x_P2_DMDTOM,            0x20 },
285         { STV090x_P2_CORRELMANT,        0x70 },
286         { STV090x_P2_CORRELABS,         0x88 },
287         { STV090x_P2_AGC2O,             0x5b },
288         { STV090x_P2_AGC2REF,           0x38 },
289         { STV090x_P2_CARCFG,            0xe4 },
290         { STV090x_P2_ACLC,              0x1A },
291         { STV090x_P2_BCLC,              0x09 },
292         { STV090x_P2_CARHDR,            0x08 },
293         { STV090x_P2_KREFTMG,           0xc1 },
294         { STV090x_P2_SFRUPRATIO,        0xf0 },
295         { STV090x_P2_SFRLOWRATIO,       0x70 },
296         { STV090x_P2_SFRSTEP,           0x58 },
297         { STV090x_P2_TMGCFG2,           0x01 },
298         { STV090x_P2_CAR2CFG,           0x26 },
299         { STV090x_P2_BCLC2S2Q,          0x86 },
300         { STV090x_P2_BCLC2S28,          0x86 },
301         { STV090x_P2_SMAPCOEF7,         0x77 },
302         { STV090x_P2_SMAPCOEF6,         0x85 },
303         { STV090x_P2_SMAPCOEF5,         0x77 },
304         { STV090x_P2_TSCFGL,            0x20 },
305         { STV090x_P2_DMDCFG2,           0x3b },
306         { STV090x_P2_MODCODLST0,        0xff },
307         { STV090x_P2_MODCODLST1,        0xff },
308         { STV090x_P2_MODCODLST2,        0xff },
309         { STV090x_P2_MODCODLST3,        0xff },
310         { STV090x_P2_MODCODLST4,        0xff },
311         { STV090x_P2_MODCODLST5,        0xff },
312         { STV090x_P2_MODCODLST6,        0xff },
313         { STV090x_P2_MODCODLST7,        0xcc },
314         { STV090x_P2_MODCODLST8,        0xcc },
315         { STV090x_P2_MODCODLST9,        0xcc },
316         { STV090x_P2_MODCODLSTA,        0xcc },
317         { STV090x_P2_MODCODLSTB,        0xcc },
318         { STV090x_P2_MODCODLSTC,        0xcc },
319         { STV090x_P2_MODCODLSTD,        0xcc },
320         { STV090x_P2_MODCODLSTE,        0xcc },
321         { STV090x_P2_MODCODLSTF,        0xcf },
322         { STV090x_P1_DISTXCTL,          0x22 },
323         { STV090x_P1_F22TX,             0xc0 },
324         { STV090x_P1_F22RX,             0xc0 },
325         { STV090x_P1_DISRXCTL,          0x00 },
326         { STV090x_P1_DMDCFGMD,          0xf9 },
327         { STV090x_P1_DEMOD,             0x08 },
328         { STV090x_P1_DMDCFG3,           0xc4 },
329         { STV090x_P1_DMDTOM,            0x20 },
330         { STV090x_P1_CARFREQ,           0xed },
331         { STV090x_P1_LDT,               0xd0 },
332         { STV090x_P1_LDT2,              0xb8 },
333         { STV090x_P1_TMGCFG,            0xd2 },
334         { STV090x_P1_TMGTHRISE,         0x20 },
335         { STV090x_P1_TMGTHFALL,         0x00 },
336         { STV090x_P1_SFRUPRATIO,        0xf0 },
337         { STV090x_P1_SFRLOWRATIO,       0x70 },
338         { STV090x_P1_TSCFGL,            0x20 },
339         { STV090x_P1_FECSPY,            0x88 },
340         { STV090x_P1_FSPYDATA,          0x3a },
341         { STV090x_P1_FBERCPT4,          0x00 },
342         { STV090x_P1_FSPYBER,           0x10 },
343         { STV090x_P1_ERRCTRL1,          0x35 },
344         { STV090x_P1_ERRCTRL2,          0xc1 },
345         { STV090x_P1_CFRICFG,           0xf8 },
346         { STV090x_P1_NOSCFG,            0x1c },
347         { STV090x_P1_CORRELMANT,        0x70 },
348         { STV090x_P1_CORRELABS,         0x88 },
349         { STV090x_P1_AGC2O,             0x5b },
350         { STV090x_P1_AGC2REF,           0x38 },
351         { STV090x_P1_CARCFG,            0xe4 },
352         { STV090x_P1_ACLC,              0x1A },
353         { STV090x_P1_BCLC,              0x09 },
354         { STV090x_P1_CARHDR,            0x08 },
355         { STV090x_P1_KREFTMG,           0xc1 },
356         { STV090x_P1_SFRSTEP,           0x58 },
357         { STV090x_P1_TMGCFG2,           0x01 },
358         { STV090x_P1_CAR2CFG,           0x26 },
359         { STV090x_P1_BCLC2S2Q,          0x86 },
360         { STV090x_P1_BCLC2S28,          0x86 },
361         { STV090x_P1_SMAPCOEF7,         0x77 },
362         { STV090x_P1_SMAPCOEF6,         0x85 },
363         { STV090x_P1_SMAPCOEF5,         0x77 },
364         { STV090x_P1_DMDCFG2,           0x3b },
365         { STV090x_P1_MODCODLST0,        0xff },
366         { STV090x_P1_MODCODLST1,        0xff },
367         { STV090x_P1_MODCODLST2,        0xff },
368         { STV090x_P1_MODCODLST3,        0xff },
369         { STV090x_P1_MODCODLST4,        0xff },
370         { STV090x_P1_MODCODLST5,        0xff },
371         { STV090x_P1_MODCODLST6,        0xff },
372         { STV090x_P1_MODCODLST7,        0xcc },
373         { STV090x_P1_MODCODLST8,        0xcc },
374         { STV090x_P1_MODCODLST9,        0xcc },
375         { STV090x_P1_MODCODLSTA,        0xcc },
376         { STV090x_P1_MODCODLSTB,        0xcc },
377         { STV090x_P1_MODCODLSTC,        0xcc },
378         { STV090x_P1_MODCODLSTD,        0xcc },
379         { STV090x_P1_MODCODLSTE,        0xcc },
380         { STV090x_P1_MODCODLSTF,        0xcf },
381         { STV090x_GENCFG,               0x1d },
382         { STV090x_NBITER_NF4,           0x37 },
383         { STV090x_NBITER_NF5,           0x29 },
384         { STV090x_NBITER_NF6,           0x37 },
385         { STV090x_NBITER_NF7,           0x33 },
386         { STV090x_NBITER_NF8,           0x31 },
387         { STV090x_NBITER_NF9,           0x2f },
388         { STV090x_NBITER_NF10,          0x39 },
389         { STV090x_NBITER_NF11,          0x3a },
390         { STV090x_NBITER_NF12,          0x29 },
391         { STV090x_NBITER_NF13,          0x37 },
392         { STV090x_NBITER_NF14,          0x33 },
393         { STV090x_NBITER_NF15,          0x2f },
394         { STV090x_NBITER_NF16,          0x39 },
395         { STV090x_NBITER_NF17,          0x3a },
396         { STV090x_NBITERNOERR,          0x04 },
397         { STV090x_GAINLLR_NF4,          0x0C },
398         { STV090x_GAINLLR_NF5,          0x0F },
399         { STV090x_GAINLLR_NF6,          0x11 },
400         { STV090x_GAINLLR_NF7,          0x14 },
401         { STV090x_GAINLLR_NF8,          0x17 },
402         { STV090x_GAINLLR_NF9,          0x19 },
403         { STV090x_GAINLLR_NF10,         0x20 },
404         { STV090x_GAINLLR_NF11,         0x21 },
405         { STV090x_GAINLLR_NF12,         0x0D },
406         { STV090x_GAINLLR_NF13,         0x0F },
407         { STV090x_GAINLLR_NF14,         0x13 },
408         { STV090x_GAINLLR_NF15,         0x1A },
409         { STV090x_GAINLLR_NF16,         0x1F },
410         { STV090x_GAINLLR_NF17,         0x21 },
411         { STV090x_RCCFGH,               0x20 },
412         { STV090x_P1_FECM,              0x01 }, /* disable DSS modes */
413         { STV090x_P2_FECM,              0x01 }, /* disable DSS modes */
414         { STV090x_P1_PRVIT,             0x2F }, /* disable PR 6/7 */
415         { STV090x_P2_PRVIT,             0x2F }, /* disable PR 6/7 */
416 };
417
418 static struct stv090x_reg stv0903_initval[] = {
419         { STV090x_OUTCFG,               0x00 },
420         { STV090x_AGCRF1CFG,            0x11 },
421         { STV090x_STOPCLK1,             0x48 },
422         { STV090x_STOPCLK2,             0x14 },
423         { STV090x_TSTTNR1,              0x27 },
424         { STV090x_TSTTNR2,              0x21 },
425         { STV090x_P1_DISTXCTL,          0x22 },
426         { STV090x_P1_F22TX,             0xc0 },
427         { STV090x_P1_F22RX,             0xc0 },
428         { STV090x_P1_DISRXCTL,          0x00 },
429         { STV090x_P1_DMDCFGMD,          0xF9 },
430         { STV090x_P1_DEMOD,             0x08 },
431         { STV090x_P1_DMDCFG3,           0xc4 },
432         { STV090x_P1_CARFREQ,           0xed },
433         { STV090x_P1_TNRCFG2,           0x82 },
434         { STV090x_P1_LDT,               0xd0 },
435         { STV090x_P1_LDT2,              0xb8 },
436         { STV090x_P1_TMGCFG,            0xd2 },
437         { STV090x_P1_TMGTHRISE,         0x20 },
438         { STV090x_P1_TMGTHFALL,         0x00 },
439         { STV090x_P1_SFRUPRATIO,        0xf0 },
440         { STV090x_P1_SFRLOWRATIO,       0x70 },
441         { STV090x_P1_TSCFGL,            0x20 },
442         { STV090x_P1_FECSPY,            0x88 },
443         { STV090x_P1_FSPYDATA,          0x3a },
444         { STV090x_P1_FBERCPT4,          0x00 },
445         { STV090x_P1_FSPYBER,           0x10 },
446         { STV090x_P1_ERRCTRL1,          0x35 },
447         { STV090x_P1_ERRCTRL2,          0xc1 },
448         { STV090x_P1_CFRICFG,           0xf8 },
449         { STV090x_P1_NOSCFG,            0x1c },
450         { STV090x_P1_DMDTOM,            0x20 },
451         { STV090x_P1_CORRELMANT,        0x70 },
452         { STV090x_P1_CORRELABS,         0x88 },
453         { STV090x_P1_AGC2O,             0x5b },
454         { STV090x_P1_AGC2REF,           0x38 },
455         { STV090x_P1_CARCFG,            0xe4 },
456         { STV090x_P1_ACLC,              0x1A },
457         { STV090x_P1_BCLC,              0x09 },
458         { STV090x_P1_CARHDR,            0x08 },
459         { STV090x_P1_KREFTMG,           0xc1 },
460         { STV090x_P1_SFRSTEP,           0x58 },
461         { STV090x_P1_TMGCFG2,           0x01 },
462         { STV090x_P1_CAR2CFG,           0x26 },
463         { STV090x_P1_BCLC2S2Q,          0x86 },
464         { STV090x_P1_BCLC2S28,          0x86 },
465         { STV090x_P1_SMAPCOEF7,         0x77 },
466         { STV090x_P1_SMAPCOEF6,         0x85 },
467         { STV090x_P1_SMAPCOEF5,         0x77 },
468         { STV090x_P1_DMDCFG2,           0x3b },
469         { STV090x_P1_MODCODLST0,        0xff },
470         { STV090x_P1_MODCODLST1,        0xff },
471         { STV090x_P1_MODCODLST2,        0xff },
472         { STV090x_P1_MODCODLST3,        0xff },
473         { STV090x_P1_MODCODLST4,        0xff },
474         { STV090x_P1_MODCODLST5,        0xff },
475         { STV090x_P1_MODCODLST6,        0xff },
476         { STV090x_P1_MODCODLST7,        0xcc },
477         { STV090x_P1_MODCODLST8,        0xcc },
478         { STV090x_P1_MODCODLST9,        0xcc },
479         { STV090x_P1_MODCODLSTA,        0xcc },
480         { STV090x_P1_MODCODLSTB,        0xcc },
481         { STV090x_P1_MODCODLSTC,        0xcc },
482         { STV090x_P1_MODCODLSTD,        0xcc },
483         { STV090x_P1_MODCODLSTE,        0xcc },
484         { STV090x_P1_MODCODLSTF,        0xcf },
485         { STV090x_GENCFG,               0x1c },
486         { STV090x_NBITER_NF4,           0x37 },
487         { STV090x_NBITER_NF5,           0x29 },
488         { STV090x_NBITER_NF6,           0x37 },
489         { STV090x_NBITER_NF7,           0x33 },
490         { STV090x_NBITER_NF8,           0x31 },
491         { STV090x_NBITER_NF9,           0x2f },
492         { STV090x_NBITER_NF10,          0x39 },
493         { STV090x_NBITER_NF11,          0x3a },
494         { STV090x_NBITER_NF12,          0x29 },
495         { STV090x_NBITER_NF13,          0x37 },
496         { STV090x_NBITER_NF14,          0x33 },
497         { STV090x_NBITER_NF15,          0x2f },
498         { STV090x_NBITER_NF16,          0x39 },
499         { STV090x_NBITER_NF17,          0x3a },
500         { STV090x_NBITERNOERR,          0x04 },
501         { STV090x_GAINLLR_NF4,          0x0C },
502         { STV090x_GAINLLR_NF5,          0x0F },
503         { STV090x_GAINLLR_NF6,          0x11 },
504         { STV090x_GAINLLR_NF7,          0x14 },
505         { STV090x_GAINLLR_NF8,          0x17 },
506         { STV090x_GAINLLR_NF9,          0x19 },
507         { STV090x_GAINLLR_NF10,         0x20 },
508         { STV090x_GAINLLR_NF11,         0x21 },
509         { STV090x_GAINLLR_NF12,         0x0D },
510         { STV090x_GAINLLR_NF13,         0x0F },
511         { STV090x_GAINLLR_NF14,         0x13 },
512         { STV090x_GAINLLR_NF15,         0x1A },
513         { STV090x_GAINLLR_NF16,         0x1F },
514         { STV090x_GAINLLR_NF17,         0x21 },
515         { STV090x_RCCFGH,               0x20 },
516         { STV090x_P1_FECM,              0x01 }, /*disable the DSS mode */
517         { STV090x_P1_PRVIT,             0x2f }  /*disable puncture rate 6/7*/
518 };
519
520 static struct stv090x_reg stv0900_cut20_val[] = {
521
522         { STV090x_P2_DMDCFG3,           0xe8 },
523         { STV090x_P2_DMDCFG4,           0x10 },
524         { STV090x_P2_CARFREQ,           0x38 },
525         { STV090x_P2_CARHDR,            0x20 },
526         { STV090x_P2_KREFTMG,           0x5a },
527         { STV090x_P2_SMAPCOEF7,         0x06 },
528         { STV090x_P2_SMAPCOEF6,         0x00 },
529         { STV090x_P2_SMAPCOEF5,         0x04 },
530         { STV090x_P2_NOSCFG,            0x0c },
531         { STV090x_P1_DMDCFG3,           0xe8 },
532         { STV090x_P1_DMDCFG4,           0x10 },
533         { STV090x_P1_CARFREQ,           0x38 },
534         { STV090x_P1_CARHDR,            0x20 },
535         { STV090x_P1_KREFTMG,           0x5a },
536         { STV090x_P1_SMAPCOEF7,         0x06 },
537         { STV090x_P1_SMAPCOEF6,         0x00 },
538         { STV090x_P1_SMAPCOEF5,         0x04 },
539         { STV090x_P1_NOSCFG,            0x0c },
540         { STV090x_GAINLLR_NF4,          0x21 },
541         { STV090x_GAINLLR_NF5,          0x21 },
542         { STV090x_GAINLLR_NF6,          0x20 },
543         { STV090x_GAINLLR_NF7,          0x1F },
544         { STV090x_GAINLLR_NF8,          0x1E },
545         { STV090x_GAINLLR_NF9,          0x1E },
546         { STV090x_GAINLLR_NF10,         0x1D },
547         { STV090x_GAINLLR_NF11,         0x1B },
548         { STV090x_GAINLLR_NF12,         0x20 },
549         { STV090x_GAINLLR_NF13,         0x20 },
550         { STV090x_GAINLLR_NF14,         0x20 },
551         { STV090x_GAINLLR_NF15,         0x20 },
552         { STV090x_GAINLLR_NF16,         0x20 },
553         { STV090x_GAINLLR_NF17,         0x21 },
554 };
555
556 static struct stv090x_reg stv0903_cut20_val[] = {
557         { STV090x_P1_DMDCFG3,           0xe8 },
558         { STV090x_P1_DMDCFG4,           0x10 },
559         { STV090x_P1_CARFREQ,           0x38 },
560         { STV090x_P1_CARHDR,            0x20 },
561         { STV090x_P1_KREFTMG,           0x5a },
562         { STV090x_P1_SMAPCOEF7,         0x06 },
563         { STV090x_P1_SMAPCOEF6,         0x00 },
564         { STV090x_P1_SMAPCOEF5,         0x04 },
565         { STV090x_P1_NOSCFG,            0x0c },
566         { STV090x_GAINLLR_NF4,          0x21 },
567         { STV090x_GAINLLR_NF5,          0x21 },
568         { STV090x_GAINLLR_NF6,          0x20 },
569         { STV090x_GAINLLR_NF7,          0x1F },
570         { STV090x_GAINLLR_NF8,          0x1E },
571         { STV090x_GAINLLR_NF9,          0x1E },
572         { STV090x_GAINLLR_NF10,         0x1D },
573         { STV090x_GAINLLR_NF11,         0x1B },
574         { STV090x_GAINLLR_NF12,         0x20 },
575         { STV090x_GAINLLR_NF13,         0x20 },
576         { STV090x_GAINLLR_NF14,         0x20 },
577         { STV090x_GAINLLR_NF15,         0x20 },
578         { STV090x_GAINLLR_NF16,         0x20 },
579         { STV090x_GAINLLR_NF17,         0x21 }
580 };
581
582 /* Cut 2.0 Long Frame Tracking CR loop */
583 static struct stv090x_long_frame_crloop stv090x_s2_crl_cut20[] = {
584         /* MODCOD  2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
585         { STV090x_QPSK_12,  0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x1e },
586         { STV090x_QPSK_35,  0x2f, 0x3f, 0x2e, 0x2f, 0x3d, 0x0f, 0x0e, 0x2e, 0x3d, 0x0e },
587         { STV090x_QPSK_23,  0x2f, 0x3f, 0x2e, 0x2f, 0x0e, 0x0f, 0x0e, 0x1e, 0x3d, 0x3d },
588         { STV090x_QPSK_34,  0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
589         { STV090x_QPSK_45,  0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
590         { STV090x_QPSK_56,  0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
591         { STV090x_QPSK_89,  0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
592         { STV090x_QPSK_910, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
593         { STV090x_8PSK_35,  0x3c, 0x3e, 0x1c, 0x2e, 0x0c, 0x1e, 0x2b, 0x2d, 0x1b, 0x1d },
594         { STV090x_8PSK_23,  0x1d, 0x3e, 0x3c, 0x2e, 0x2c, 0x1e, 0x0c, 0x2d, 0x2b, 0x1d },
595         { STV090x_8PSK_34,  0x0e, 0x3e, 0x3d, 0x2e, 0x0d, 0x1e, 0x2c, 0x2d, 0x0c, 0x1d },
596         { STV090x_8PSK_56,  0x2e, 0x3e, 0x1e, 0x2e, 0x2d, 0x1e, 0x3c, 0x2d, 0x2c, 0x1d },
597         { STV090x_8PSK_89,  0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x0d, 0x2d, 0x3c, 0x1d },
598         { STV090x_8PSK_910, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x1d, 0x2d, 0x0d, 0x1d }
599 };
600
601 /* Cut 3.0 Long Frame Tracking CR loop */
602 static  struct stv090x_long_frame_crloop stv090x_s2_crl_cut30[] = {
603         /* MODCOD  2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
604         { STV090x_QPSK_12,  0x3c, 0x2c, 0x0c, 0x2c, 0x1b, 0x2c, 0x1b, 0x1c, 0x0b, 0x3b },
605         { STV090x_QPSK_35,  0x0d, 0x0d, 0x0c, 0x0d, 0x1b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
606         { STV090x_QPSK_23,  0x1d, 0x0d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
607         { STV090x_QPSK_34,  0x1d, 0x1d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
608         { STV090x_QPSK_45,  0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
609         { STV090x_QPSK_56,  0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
610         { STV090x_QPSK_89,  0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
611         { STV090x_QPSK_910, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
612         { STV090x_8PSK_35,  0x39, 0x29, 0x39, 0x19, 0x19, 0x19, 0x19, 0x19, 0x09, 0x19 },
613         { STV090x_8PSK_23,  0x2a, 0x39, 0x1a, 0x0a, 0x39, 0x0a, 0x29, 0x39, 0x29, 0x0a },
614         { STV090x_8PSK_34,  0x2b, 0x3a, 0x1b, 0x1b, 0x3a, 0x1b, 0x1a, 0x0b, 0x1a, 0x3a },
615         { STV090x_8PSK_56,  0x0c, 0x1b, 0x3b, 0x3b, 0x1b, 0x3b, 0x3a, 0x3b, 0x3a, 0x1b },
616         { STV090x_8PSK_89,  0x0d, 0x3c, 0x2c, 0x2c, 0x2b, 0x0c, 0x0b, 0x3b, 0x0b, 0x1b },
617         { STV090x_8PSK_910, 0x0d, 0x0d, 0x2c, 0x3c, 0x3b, 0x1c, 0x0b, 0x3b, 0x0b, 0x1b }
618 };
619
620 /* Cut 2.0 Long Frame Tracking CR Loop */
621 static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut20[] = {
622         /* MODCOD  2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
623         { STV090x_16APSK_23,  0x0c, 0x0c, 0x0c, 0x0c, 0x1d, 0x0c, 0x3c, 0x0c, 0x2c, 0x0c },
624         { STV090x_16APSK_34,  0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0c, 0x2d, 0x0c, 0x1d, 0x0c },
625         { STV090x_16APSK_45,  0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
626         { STV090x_16APSK_56,  0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
627         { STV090x_16APSK_89,  0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
628         { STV090x_16APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
629         { STV090x_32APSK_34,  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
630         { STV090x_32APSK_45,  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
631         { STV090x_32APSK_56,  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
632         { STV090x_32APSK_89,  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
633         { STV090x_32APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c }
634 };
635
636 /* Cut 3.0 Long Frame Tracking CR Loop */
637 static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut30[] = {
638         /* MODCOD  2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
639         { STV090x_16APSK_23,  0x0a, 0x0a, 0x0a, 0x0a, 0x1a, 0x0a, 0x3a, 0x0a, 0x2a, 0x0a },
640         { STV090x_16APSK_34,  0x0a, 0x0a, 0x0a, 0x0a, 0x0b, 0x0a, 0x3b, 0x0a, 0x1b, 0x0a },
641         { STV090x_16APSK_45,  0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
642         { STV090x_16APSK_56,  0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
643         { STV090x_16APSK_89,  0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
644         { STV090x_16APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
645         { STV090x_32APSK_34,  0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
646         { STV090x_32APSK_45,  0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
647         { STV090x_32APSK_56,  0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
648         { STV090x_32APSK_89,  0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
649         { STV090x_32APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a }
650 };
651
652 static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut20[] = {
653         /* MODCOD  2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
654         { STV090x_QPSK_14,  0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x2d, 0x1f, 0x3d, 0x3e },
655         { STV090x_QPSK_13,  0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x3d, 0x0f, 0x3d, 0x2e },
656         { STV090x_QPSK_25,  0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x2e }
657 };
658
659 static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut30[] = {
660         /* MODCOD  2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
661         { STV090x_QPSK_14,  0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x2a, 0x1c, 0x3a, 0x3b },
662         { STV090x_QPSK_13,  0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x3a, 0x0c, 0x3a, 0x2b },
663         { STV090x_QPSK_25,  0x1c, 0x3c, 0x1b, 0x3c, 0x3a, 0x1c, 0x3a, 0x3b, 0x3a, 0x2b }
664 };
665
666 /* Cut 2.0 Short Frame Tracking CR Loop */
667 static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut20[] = {
668         /* MODCOD         2M    5M    10M   20M   30M */
669         { STV090x_QPSK,   0x2f, 0x2e, 0x0e, 0x0e, 0x3d },
670         { STV090x_8PSK,   0x3e, 0x0e, 0x2d, 0x0d, 0x3c },
671         { STV090x_16APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d },
672         { STV090x_32APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d }
673 };
674
675 /* Cut 3.0 Short Frame Tracking CR Loop */
676 static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut30[] = {
677         /* MODCOD         2M    5M    10M   20M   30M */
678         { STV090x_QPSK,   0x2C, 0x2B, 0x0B, 0x0B, 0x3A },
679         { STV090x_8PSK,   0x3B, 0x0B, 0x2A, 0x0A, 0x39 },
680         { STV090x_16APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A },
681         { STV090x_32APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A }
682 };
683
684 static inline s32 comp2(s32 __x, s32 __width)
685 {
686         if (__width == 32)
687                 return __x;
688         else
689                 return (__x >= (1 << (__width - 1))) ? (__x - (1 << __width)) : __x;
690 }
691
692 static int stv090x_read_reg(struct stv090x_state *state, unsigned int reg)
693 {
694         const struct stv090x_config *config = state->config;
695         int ret;
696
697         u8 b0[] = { reg >> 8, reg & 0xff };
698         u8 buf;
699
700         struct i2c_msg msg[] = {
701                 { .addr = config->address, .flags       = 0,            .buf = b0,   .len = 2 },
702                 { .addr = config->address, .flags       = I2C_M_RD,     .buf = &buf, .len = 1 }
703         };
704
705         ret = i2c_transfer(state->i2c, msg, 2);
706         if (ret != 2) {
707                 if (ret != -ERESTARTSYS)
708                         dprintk(FE_ERROR, 1,
709                                 "Read error, Reg=[0x%02x], Status=%d",
710                                 reg, ret);
711
712                 return ret < 0 ? ret : -EREMOTEIO;
713         }
714         if (unlikely(*state->verbose >= FE_DEBUGREG))
715                 dprintk(FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
716                         reg, buf);
717
718         return (unsigned int) buf;
719 }
720
721 static int stv090x_write_regs(struct stv090x_state *state, unsigned int reg, u8 *data, u32 count)
722 {
723         const struct stv090x_config *config = state->config;
724         int ret;
725         u8 buf[2 + count];
726         struct i2c_msg i2c_msg = { .addr = config->address, .flags = 0, .buf = buf, .len = 2 + count };
727
728         buf[0] = reg >> 8;
729         buf[1] = reg & 0xff;
730         memcpy(&buf[2], data, count);
731
732         if (unlikely(*state->verbose >= FE_DEBUGREG)) {
733                 int i;
734
735                 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
736                 for (i = 0; i < count; i++)
737                         printk(" %02x", data[i]);
738                 printk("\n");
739         }
740
741         ret = i2c_transfer(state->i2c, &i2c_msg, 1);
742         if (ret != 1) {
743                 if (ret != -ERESTARTSYS)
744                         dprintk(FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
745                                 reg, data[0], count, ret);
746                 return ret < 0 ? ret : -EREMOTEIO;
747         }
748
749         return 0;
750 }
751
752 static int stv090x_write_reg(struct stv090x_state *state, unsigned int reg, u8 data)
753 {
754         return stv090x_write_regs(state, reg, &data, 1);
755 }
756
757 static int stv090x_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
758 {
759         struct stv090x_state *state = fe->demodulator_priv;
760         u32 reg;
761
762         if (enable)
763                 mutex_lock(&state->internal->tuner_lock);
764
765         reg = STV090x_READ_DEMOD(state, I2CRPT);
766         if (enable) {
767                 dprintk(FE_DEBUG, 1, "Enable Gate");
768                 STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 1);
769                 if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0)
770                         goto err;
771
772         } else {
773                 dprintk(FE_DEBUG, 1, "Disable Gate");
774                 STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 0);
775                 if ((STV090x_WRITE_DEMOD(state, I2CRPT, reg)) < 0)
776                         goto err;
777         }
778
779         if (!enable)
780                 mutex_unlock(&state->internal->tuner_lock);
781
782         return 0;
783 err:
784         dprintk(FE_ERROR, 1, "I/O error");
785         mutex_unlock(&state->internal->tuner_lock);
786         return -1;
787 }
788
789 static void stv090x_get_lock_tmg(struct stv090x_state *state)
790 {
791         switch (state->algo) {
792         case STV090x_BLIND_SEARCH:
793                 dprintk(FE_DEBUG, 1, "Blind Search");
794                 if (state->srate <= 1500000) {  /*10Msps< SR <=15Msps*/
795                         state->DemodTimeout = 1500;
796                         state->FecTimeout = 400;
797                 } else if (state->srate <= 5000000) {  /*10Msps< SR <=15Msps*/
798                         state->DemodTimeout = 1000;
799                         state->FecTimeout = 300;
800                 } else {  /*SR >20Msps*/
801                         state->DemodTimeout = 700;
802                         state->FecTimeout = 100;
803                 }
804                 break;
805
806         case STV090x_COLD_SEARCH:
807         case STV090x_WARM_SEARCH:
808         default:
809                 dprintk(FE_DEBUG, 1, "Normal Search");
810                 if (state->srate <= 1000000) {  /*SR <=1Msps*/
811                         state->DemodTimeout = 4500;
812                         state->FecTimeout = 1700;
813                 } else if (state->srate <= 2000000) { /*1Msps < SR <= 2Msps */
814                         state->DemodTimeout = 2500;
815                         state->FecTimeout = 1100;
816                 } else if (state->srate <= 5000000) { /*2Msps < SR <= 5Msps */
817                         state->DemodTimeout = 1000;
818                         state->FecTimeout = 550;
819                 } else if (state->srate <= 10000000) { /*5Msps < SR <= 10Msps */
820                         state->DemodTimeout = 700;
821                         state->FecTimeout = 250;
822                 } else if (state->srate <= 20000000) { /*10Msps < SR <= 20Msps */
823                         state->DemodTimeout = 400;
824                         state->FecTimeout = 130;
825                 } else {   /*SR >20Msps*/
826                         state->DemodTimeout = 300;
827                         state->FecTimeout = 100;
828                 }
829                 break;
830         }
831
832         if (state->algo == STV090x_WARM_SEARCH)
833                 state->DemodTimeout /= 2;
834 }
835
836 static int stv090x_set_srate(struct stv090x_state *state, u32 srate)
837 {
838         u32 sym;
839
840         if (srate > 60000000) {
841                 sym  = (srate << 4); /* SR * 2^16 / master_clk */
842                 sym /= (state->internal->mclk >> 12);
843         } else if (srate > 6000000) {
844                 sym  = (srate << 6);
845                 sym /= (state->internal->mclk >> 10);
846         } else {
847                 sym  = (srate << 9);
848                 sym /= (state->internal->mclk >> 7);
849         }
850
851         if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0x7f) < 0) /* MSB */
852                 goto err;
853         if (STV090x_WRITE_DEMOD(state, SFRINIT0, (sym & 0xff)) < 0) /* LSB */
854                 goto err;
855
856         return 0;
857 err:
858         dprintk(FE_ERROR, 1, "I/O error");
859         return -1;
860 }
861
862 static int stv090x_set_max_srate(struct stv090x_state *state, u32 clk, u32 srate)
863 {
864         u32 sym;
865
866         srate = 105 * (srate / 100);
867         if (srate > 60000000) {
868                 sym  = (srate << 4); /* SR * 2^16 / master_clk */
869                 sym /= (state->internal->mclk >> 12);
870         } else if (srate > 6000000) {
871                 sym  = (srate << 6);
872                 sym /= (state->internal->mclk >> 10);
873         } else {
874                 sym  = (srate << 9);
875                 sym /= (state->internal->mclk >> 7);
876         }
877
878         if (sym < 0x7fff) {
879                 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) /* MSB */
880                         goto err;
881                 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) /* LSB */
882                         goto err;
883         } else {
884                 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x7f) < 0) /* MSB */
885                         goto err;
886                 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xff) < 0) /* LSB */
887                         goto err;
888         }
889
890         return 0;
891 err:
892         dprintk(FE_ERROR, 1, "I/O error");
893         return -1;
894 }
895
896 static int stv090x_set_min_srate(struct stv090x_state *state, u32 clk, u32 srate)
897 {
898         u32 sym;
899
900         srate = 95 * (srate / 100);
901         if (srate > 60000000) {
902                 sym  = (srate << 4); /* SR * 2^16 / master_clk */
903                 sym /= (state->internal->mclk >> 12);
904         } else if (srate > 6000000) {
905                 sym  = (srate << 6);
906                 sym /= (state->internal->mclk >> 10);
907         } else {
908                 sym  = (srate << 9);
909                 sym /= (state->internal->mclk >> 7);
910         }
911
912         if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0x7f)) < 0) /* MSB */
913                 goto err;
914         if (STV090x_WRITE_DEMOD(state, SFRLOW0, (sym & 0xff)) < 0) /* LSB */
915                 goto err;
916         return 0;
917 err:
918         dprintk(FE_ERROR, 1, "I/O error");
919         return -1;
920 }
921
922 static u32 stv090x_car_width(u32 srate, enum stv090x_rolloff rolloff)
923 {
924         u32 ro;
925
926         switch (rolloff) {
927         case STV090x_RO_20:
928                 ro = 20;
929                 break;
930         case STV090x_RO_25:
931                 ro = 25;
932                 break;
933         case STV090x_RO_35:
934         default:
935                 ro = 35;
936                 break;
937         }
938
939         return srate + (srate * ro) / 100;
940 }
941
942 static int stv090x_set_vit_thacq(struct stv090x_state *state)
943 {
944         if (STV090x_WRITE_DEMOD(state, VTH12, 0x96) < 0)
945                 goto err;
946         if (STV090x_WRITE_DEMOD(state, VTH23, 0x64) < 0)
947                 goto err;
948         if (STV090x_WRITE_DEMOD(state, VTH34, 0x36) < 0)
949                 goto err;
950         if (STV090x_WRITE_DEMOD(state, VTH56, 0x23) < 0)
951                 goto err;
952         if (STV090x_WRITE_DEMOD(state, VTH67, 0x1e) < 0)
953                 goto err;
954         if (STV090x_WRITE_DEMOD(state, VTH78, 0x19) < 0)
955                 goto err;
956         return 0;
957 err:
958         dprintk(FE_ERROR, 1, "I/O error");
959         return -1;
960 }
961
962 static int stv090x_set_vit_thtracq(struct stv090x_state *state)
963 {
964         if (STV090x_WRITE_DEMOD(state, VTH12, 0xd0) < 0)
965                 goto err;
966         if (STV090x_WRITE_DEMOD(state, VTH23, 0x7d) < 0)
967                 goto err;
968         if (STV090x_WRITE_DEMOD(state, VTH34, 0x53) < 0)
969                 goto err;
970         if (STV090x_WRITE_DEMOD(state, VTH56, 0x2f) < 0)
971                 goto err;
972         if (STV090x_WRITE_DEMOD(state, VTH67, 0x24) < 0)
973                 goto err;
974         if (STV090x_WRITE_DEMOD(state, VTH78, 0x1f) < 0)
975                 goto err;
976         return 0;
977 err:
978         dprintk(FE_ERROR, 1, "I/O error");
979         return -1;
980 }
981
982 static int stv090x_set_viterbi(struct stv090x_state *state)
983 {
984         switch (state->search_mode) {
985         case STV090x_SEARCH_AUTO:
986                 if (STV090x_WRITE_DEMOD(state, FECM, 0x10) < 0) /* DVB-S and DVB-S2 */
987                         goto err;
988                 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x3f) < 0) /* all puncture rate */
989                         goto err;
990                 break;
991         case STV090x_SEARCH_DVBS1:
992                 if (STV090x_WRITE_DEMOD(state, FECM, 0x00) < 0) /* disable DSS */
993                         goto err;
994                 switch (state->fec) {
995                 case STV090x_PR12:
996                         if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
997                                 goto err;
998                         break;
999
1000                 case STV090x_PR23:
1001                         if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
1002                                 goto err;
1003                         break;
1004
1005                 case STV090x_PR34:
1006                         if (STV090x_WRITE_DEMOD(state, PRVIT, 0x04) < 0)
1007                                 goto err;
1008                         break;
1009
1010                 case STV090x_PR56:
1011                         if (STV090x_WRITE_DEMOD(state, PRVIT, 0x08) < 0)
1012                                 goto err;
1013                         break;
1014
1015                 case STV090x_PR78:
1016                         if (STV090x_WRITE_DEMOD(state, PRVIT, 0x20) < 0)
1017                                 goto err;
1018                         break;
1019
1020                 default:
1021                         if (STV090x_WRITE_DEMOD(state, PRVIT, 0x2f) < 0) /* all */
1022                                 goto err;
1023                         break;
1024                 }
1025                 break;
1026         case STV090x_SEARCH_DSS:
1027                 if (STV090x_WRITE_DEMOD(state, FECM, 0x80) < 0)
1028                         goto err;
1029                 switch (state->fec) {
1030                 case STV090x_PR12:
1031                         if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
1032                                 goto err;
1033                         break;
1034
1035                 case STV090x_PR23:
1036                         if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
1037                                 goto err;
1038                         break;
1039
1040                 case STV090x_PR67:
1041                         if (STV090x_WRITE_DEMOD(state, PRVIT, 0x10) < 0)
1042                                 goto err;
1043                         break;
1044
1045                 default:
1046                         if (STV090x_WRITE_DEMOD(state, PRVIT, 0x13) < 0) /* 1/2, 2/3, 6/7 */
1047                                 goto err;
1048                         break;
1049                 }
1050                 break;
1051         default:
1052                 break;
1053         }
1054         return 0;
1055 err:
1056         dprintk(FE_ERROR, 1, "I/O error");
1057         return -1;
1058 }
1059
1060 static int stv090x_stop_modcod(struct stv090x_state *state)
1061 {
1062         if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1063                 goto err;
1064         if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
1065                 goto err;
1066         if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
1067                 goto err;
1068         if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
1069                 goto err;
1070         if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
1071                 goto err;
1072         if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
1073                 goto err;
1074         if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
1075                 goto err;
1076         if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xff) < 0)
1077                 goto err;
1078         if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xff) < 0)
1079                 goto err;
1080         if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xff) < 0)
1081                 goto err;
1082         if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xff) < 0)
1083                 goto err;
1084         if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xff) < 0)
1085                 goto err;
1086         if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xff) < 0)
1087                 goto err;
1088         if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xff) < 0)
1089                 goto err;
1090         if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
1091                 goto err;
1092         if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xff) < 0)
1093                 goto err;
1094         return 0;
1095 err:
1096         dprintk(FE_ERROR, 1, "I/O error");
1097         return -1;
1098 }
1099
1100 static int stv090x_activate_modcod(struct stv090x_state *state)
1101 {
1102         if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1103                 goto err;
1104         if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xfc) < 0)
1105                 goto err;
1106         if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xcc) < 0)
1107                 goto err;
1108         if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xcc) < 0)
1109                 goto err;
1110         if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xcc) < 0)
1111                 goto err;
1112         if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xcc) < 0)
1113                 goto err;
1114         if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xcc) < 0)
1115                 goto err;
1116         if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
1117                 goto err;
1118         if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
1119                 goto err;
1120         if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
1121                 goto err;
1122         if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
1123                 goto err;
1124         if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
1125                 goto err;
1126         if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
1127                 goto err;
1128         if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
1129                 goto err;
1130         if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xcc) < 0)
1131                 goto err;
1132         if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
1133                 goto err;
1134
1135         return 0;
1136 err:
1137         dprintk(FE_ERROR, 1, "I/O error");
1138         return -1;
1139 }
1140
1141 static int stv090x_activate_modcod_single(struct stv090x_state *state)
1142 {
1143
1144         if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1145                 goto err;
1146         if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xf0) < 0)
1147                 goto err;
1148         if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0x00) < 0)
1149                 goto err;
1150         if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0x00) < 0)
1151                 goto err;
1152         if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0x00) < 0)
1153                 goto err;
1154         if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0x00) < 0)
1155                 goto err;
1156         if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0x00) < 0)
1157                 goto err;
1158         if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0x00) < 0)
1159                 goto err;
1160         if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0x00) < 0)
1161                 goto err;
1162         if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0x00) < 0)
1163                 goto err;
1164         if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0x00) < 0)
1165                 goto err;
1166         if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0x00) < 0)
1167                 goto err;
1168         if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0x00) < 0)
1169                 goto err;
1170         if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0x00) < 0)
1171                 goto err;
1172         if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0x00) < 0)
1173                 goto err;
1174         if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0x0f) < 0)
1175                 goto err;
1176
1177         return 0;
1178
1179 err:
1180         dprintk(FE_ERROR, 1, "I/O error");
1181         return -1;
1182 }
1183
1184 static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
1185 {
1186         u32 reg;
1187
1188         switch (state->demod) {
1189         case STV090x_DEMODULATOR_0:
1190                 mutex_lock(&state->internal->demod_lock);
1191                 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1192                 STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, enable);
1193                 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1194                         goto err;
1195                 mutex_unlock(&state->internal->demod_lock);
1196                 break;
1197
1198         case STV090x_DEMODULATOR_1:
1199                 mutex_lock(&state->internal->demod_lock);
1200                 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1201                 STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, enable);
1202                 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1203                         goto err;
1204                 mutex_unlock(&state->internal->demod_lock);
1205                 break;
1206
1207         default:
1208                 dprintk(FE_ERROR, 1, "Wrong demodulator!");
1209                 break;
1210         }
1211         return 0;
1212 err:
1213         mutex_unlock(&state->internal->demod_lock);
1214         dprintk(FE_ERROR, 1, "I/O error");
1215         return -1;
1216 }
1217
1218 static int stv090x_dvbs_track_crl(struct stv090x_state *state)
1219 {
1220         if (state->internal->dev_ver >= 0x30) {
1221                 /* Set ACLC BCLC optimised value vs SR */
1222                 if (state->srate >= 15000000) {
1223                         if (STV090x_WRITE_DEMOD(state, ACLC, 0x2b) < 0)
1224                                 goto err;
1225                         if (STV090x_WRITE_DEMOD(state, BCLC, 0x1a) < 0)
1226                                 goto err;
1227                 } else if ((state->srate >= 7000000) && (15000000 > state->srate)) {
1228                         if (STV090x_WRITE_DEMOD(state, ACLC, 0x0c) < 0)
1229                                 goto err;
1230                         if (STV090x_WRITE_DEMOD(state, BCLC, 0x1b) < 0)
1231                                 goto err;
1232                 } else if (state->srate < 7000000) {
1233                         if (STV090x_WRITE_DEMOD(state, ACLC, 0x2c) < 0)
1234                                 goto err;
1235                         if (STV090x_WRITE_DEMOD(state, BCLC, 0x1c) < 0)
1236                                 goto err;
1237                 }
1238
1239         } else {
1240                 /* Cut 2.0 */
1241                 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
1242                         goto err;
1243                 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1244                         goto err;
1245         }
1246         return 0;
1247 err:
1248         dprintk(FE_ERROR, 1, "I/O error");
1249         return -1;
1250 }
1251
1252 static int stv090x_delivery_search(struct stv090x_state *state)
1253 {
1254         u32 reg;
1255
1256         switch (state->search_mode) {
1257         case STV090x_SEARCH_DVBS1:
1258         case STV090x_SEARCH_DSS:
1259                 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1260                 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1261                 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1262                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1263                         goto err;
1264
1265                 /* Activate Viterbi decoder in legacy search,
1266                  * do not use FRESVIT1, might impact VITERBI2
1267                  */
1268                 if (stv090x_vitclk_ctl(state, 0) < 0)
1269                         goto err;
1270
1271                 if (stv090x_dvbs_track_crl(state) < 0)
1272                         goto err;
1273
1274                 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x22) < 0) /* disable DVB-S2 */
1275                         goto err;
1276
1277                 if (stv090x_set_vit_thacq(state) < 0)
1278                         goto err;
1279                 if (stv090x_set_viterbi(state) < 0)
1280                         goto err;
1281                 break;
1282
1283         case STV090x_SEARCH_DVBS2:
1284                 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1285                 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
1286                 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1287                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1288                         goto err;
1289                 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1290                 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
1291                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1292                         goto err;
1293
1294                 if (stv090x_vitclk_ctl(state, 1) < 0)
1295                         goto err;
1296
1297                 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0) /* stop DVB-S CR loop */
1298                         goto err;
1299                 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1300                         goto err;
1301
1302                 if (state->internal->dev_ver <= 0x20) {
1303                         /* enable S2 carrier loop */
1304                         if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1305                                 goto err;
1306                 } else {
1307                         /* > Cut 3: Stop carrier 3 */
1308                         if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1309                                 goto err;
1310                 }
1311
1312                 if (state->demod_mode != STV090x_SINGLE) {
1313                         /* Cut 2: enable link during search */
1314                         if (stv090x_activate_modcod(state) < 0)
1315                                 goto err;
1316                 } else {
1317                         /* Single demodulator
1318                          * Authorize SHORT and LONG frames,
1319                          * QPSK, 8PSK, 16APSK and 32APSK
1320                          */
1321                         if (stv090x_activate_modcod_single(state) < 0)
1322                                 goto err;
1323                 }
1324
1325                 if (stv090x_set_vit_thtracq(state) < 0)
1326                         goto err;
1327                 break;
1328
1329         case STV090x_SEARCH_AUTO:
1330         default:
1331                 /* enable DVB-S2 and DVB-S2 in Auto MODE */
1332                 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1333                 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
1334                 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1335                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1336                         goto err;
1337                 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1338                 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
1339                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1340                         goto err;
1341
1342                 if (stv090x_vitclk_ctl(state, 0) < 0)
1343                         goto err;
1344
1345                 if (stv090x_dvbs_track_crl(state) < 0)
1346                         goto err;
1347
1348                 if (state->internal->dev_ver <= 0x20) {
1349                         /* enable S2 carrier loop */
1350                         if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1351                                 goto err;
1352                 } else {
1353                         /* > Cut 3: Stop carrier 3 */
1354                         if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1355                                 goto err;
1356                 }
1357
1358                 if (state->demod_mode != STV090x_SINGLE) {
1359                         /* Cut 2: enable link during search */
1360                         if (stv090x_activate_modcod(state) < 0)
1361                                 goto err;
1362                 } else {
1363                         /* Single demodulator
1364                          * Authorize SHORT and LONG frames,
1365                          * QPSK, 8PSK, 16APSK and 32APSK
1366                          */
1367                         if (stv090x_activate_modcod_single(state) < 0)
1368                                 goto err;
1369                 }
1370
1371                 if (stv090x_set_vit_thacq(state) < 0)
1372                         goto err;
1373
1374                 if (stv090x_set_viterbi(state) < 0)
1375                         goto err;
1376                 break;
1377         }
1378         return 0;
1379 err:
1380         dprintk(FE_ERROR, 1, "I/O error");
1381         return -1;
1382 }
1383
1384 static int stv090x_start_search(struct stv090x_state *state)
1385 {
1386         u32 reg, freq_abs;
1387         s16 freq;
1388
1389         /* Reset demodulator */
1390         reg = STV090x_READ_DEMOD(state, DMDISTATE);
1391         STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f);
1392         if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
1393                 goto err;
1394
1395         if (state->internal->dev_ver <= 0x20) {
1396                 if (state->srate <= 5000000) {
1397                         if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0)
1398                                 goto err;
1399                         if (STV090x_WRITE_DEMOD(state, CFRUP1, 0x0f) < 0)
1400                                 goto err;
1401                         if (STV090x_WRITE_DEMOD(state, CFRUP0, 0xff) < 0)
1402                                 goto err;
1403                         if (STV090x_WRITE_DEMOD(state, CFRLOW1, 0xf0) < 0)
1404                                 goto err;
1405                         if (STV090x_WRITE_DEMOD(state, CFRLOW0, 0x00) < 0)
1406                                 goto err;
1407
1408                         /*enlarge the timing bandwith for Low SR*/
1409                         if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0)
1410                                 goto err;
1411                 } else {
1412                         /* If the symbol rate is >5 Msps
1413                         Set The carrier search up and low to auto mode */
1414                         if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
1415                                 goto err;
1416                         /*reduce the timing bandwith for high SR*/
1417                         if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
1418                                 goto err;
1419                 }
1420         } else {
1421                 /* >= Cut 3 */
1422                 if (state->srate <= 5000000) {
1423                         /* enlarge the timing bandwith for Low SR */
1424                         STV090x_WRITE_DEMOD(state, RTCS2, 0x68);
1425                 } else {
1426                         /* reduce timing bandwith for high SR */
1427                         STV090x_WRITE_DEMOD(state, RTCS2, 0x44);
1428                 }
1429
1430                 /* Set CFR min and max to manual mode */
1431                 STV090x_WRITE_DEMOD(state, CARCFG, 0x46);
1432
1433                 if (state->algo == STV090x_WARM_SEARCH) {
1434                         /* WARM Start
1435                          * CFR min = -1MHz,
1436                          * CFR max = +1MHz
1437                          */
1438                         freq_abs  = 1000 << 16;
1439                         freq_abs /= (state->internal->mclk / 1000);
1440                         freq      = (s16) freq_abs;
1441                 } else {
1442                         /* COLD Start
1443                          * CFR min =- (SearchRange / 2 + 600KHz)
1444                          * CFR max = +(SearchRange / 2 + 600KHz)
1445                          * (600KHz for the tuner step size)
1446                          */
1447                         freq_abs  = (state->search_range / 2000) + 600;
1448                         freq_abs  = freq_abs << 16;
1449                         freq_abs /= (state->internal->mclk / 1000);
1450                         freq      = (s16) freq_abs;
1451                 }
1452
1453                 if (STV090x_WRITE_DEMOD(state, CFRUP1, MSB(freq)) < 0)
1454                         goto err;
1455                 if (STV090x_WRITE_DEMOD(state, CFRUP0, LSB(freq)) < 0)
1456                         goto err;
1457
1458                 freq *= -1;
1459
1460                 if (STV090x_WRITE_DEMOD(state, CFRLOW1, MSB(freq)) < 0)
1461                         goto err;
1462                 if (STV090x_WRITE_DEMOD(state, CFRLOW0, LSB(freq)) < 0)
1463                         goto err;
1464
1465         }
1466
1467         if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0) < 0)
1468                 goto err;
1469         if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0) < 0)
1470                 goto err;
1471
1472         if (state->internal->dev_ver >= 0x20) {
1473                 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1474                         goto err;
1475                 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
1476                         goto err;
1477
1478                 if ((state->search_mode == STV090x_DVBS1)       ||
1479                         (state->search_mode == STV090x_DSS)     ||
1480                         (state->search_mode == STV090x_SEARCH_AUTO)) {
1481
1482                         if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
1483                                 goto err;
1484                         if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0)
1485                                 goto err;
1486                 }
1487         }
1488
1489         if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00) < 0)
1490                 goto err;
1491         if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xe0) < 0)
1492                 goto err;
1493         if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xc0) < 0)
1494                 goto err;
1495
1496         reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1497         STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
1498         STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
1499         if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1500                 goto err;
1501         reg = STV090x_READ_DEMOD(state, DMDCFG2);
1502         STV090x_SETFIELD_Px(reg, S1S2_SEQUENTIAL_FIELD, 0x0);
1503         if (STV090x_WRITE_DEMOD(state, DMDCFG2, reg) < 0)
1504                 goto err;
1505
1506         if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0)
1507                 goto err;
1508
1509         if (state->internal->dev_ver >= 0x20) {
1510                 /*Frequency offset detector setting*/
1511                 if (state->srate < 2000000) {
1512                         if (state->internal->dev_ver <= 0x20) {
1513                                 /* Cut 2 */
1514                                 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x39) < 0)
1515                                         goto err;
1516                         } else {
1517                                 /* Cut 3 */
1518                                 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x89) < 0)
1519                                         goto err;
1520                         }
1521                         if (STV090x_WRITE_DEMOD(state, CARHDR, 0x40) < 0)
1522                                 goto err;
1523                 } else if (state->srate < 10000000) {
1524                         if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4c) < 0)
1525                                 goto err;
1526                         if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1527                                 goto err;
1528                 } else {
1529                         if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4b) < 0)
1530                                 goto err;
1531                         if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1532                                 goto err;
1533                 }
1534         } else {
1535                 if (state->srate < 10000000) {
1536                         if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
1537                                 goto err;
1538                 } else {
1539                         if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
1540                                 goto err;
1541                 }
1542         }
1543
1544         switch (state->algo) {
1545         case STV090x_WARM_SEARCH:
1546                 /* The symbol rate and the exact
1547                  * carrier Frequency are known
1548                  */
1549                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
1550                         goto err;
1551                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
1552                         goto err;
1553                 break;
1554
1555         case STV090x_COLD_SEARCH:
1556                 /* The symbol rate is known */
1557                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
1558                         goto err;
1559                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
1560                         goto err;
1561                 break;
1562
1563         default:
1564                 break;
1565         }
1566         return 0;
1567 err:
1568         dprintk(FE_ERROR, 1, "I/O error");
1569         return -1;
1570 }
1571
1572 static int stv090x_get_agc2_min_level(struct stv090x_state *state)
1573 {
1574         u32 agc2_min = 0xffff, agc2 = 0, freq_init, freq_step, reg;
1575         s32 i, j, steps, dir;
1576
1577         if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1578                 goto err;
1579         reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1580         STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
1581         STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
1582         if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1583                 goto err;
1584
1585         if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0) /* SR = 65 Msps Max */
1586                 goto err;
1587         if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1588                 goto err;
1589         if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0) /* SR= 400 ksps Min */
1590                 goto err;
1591         if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1592                 goto err;
1593         if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0) /* stop acq @ coarse carrier state */
1594                 goto err;
1595         if (stv090x_set_srate(state, 1000000) < 0)
1596                 goto err;
1597
1598         steps  = state->search_range / 1000000;
1599         if (steps <= 0)
1600                 steps = 1;
1601
1602         dir = 1;
1603         freq_step = (1000000 * 256) / (state->internal->mclk / 256);
1604         freq_init = 0;
1605
1606         for (i = 0; i < steps; i++) {
1607                 if (dir > 0)
1608                         freq_init = freq_init + (freq_step * i);
1609                 else
1610                         freq_init = freq_init - (freq_step * i);
1611
1612                 dir *= -1;
1613
1614                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod RESET */
1615                         goto err;
1616                 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_init >> 8) & 0xff) < 0)
1617                         goto err;
1618                 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_init & 0xff) < 0)
1619                         goto err;
1620                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x58) < 0) /* Demod RESET */
1621                         goto err;
1622                 msleep(10);
1623
1624                 agc2 = 0;
1625                 for (j = 0; j < 10; j++) {
1626                         agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1627                                 STV090x_READ_DEMOD(state, AGC2I0);
1628                 }
1629                 agc2 /= 10;
1630                 if (agc2 < agc2_min)
1631                         agc2_min = agc2;
1632         }
1633
1634         return agc2_min;
1635 err:
1636         dprintk(FE_ERROR, 1, "I/O error");
1637         return -1;
1638 }
1639
1640 static u32 stv090x_get_srate(struct stv090x_state *state, u32 clk)
1641 {
1642         u8 r3, r2, r1, r0;
1643         s32 srate, int_1, int_2, tmp_1, tmp_2;
1644
1645         r3 = STV090x_READ_DEMOD(state, SFR3);
1646         r2 = STV090x_READ_DEMOD(state, SFR2);
1647         r1 = STV090x_READ_DEMOD(state, SFR1);
1648         r0 = STV090x_READ_DEMOD(state, SFR0);
1649
1650         srate = ((r3 << 24) | (r2 << 16) | (r1 <<  8) | r0);
1651
1652         int_1 = clk >> 16;
1653         int_2 = srate >> 16;
1654
1655         tmp_1 = clk % 0x10000;
1656         tmp_2 = srate % 0x10000;
1657
1658         srate = (int_1 * int_2) +
1659                 ((int_1 * tmp_2) >> 16) +
1660                 ((int_2 * tmp_1) >> 16);
1661
1662         return srate;
1663 }
1664
1665 static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
1666 {
1667         struct dvb_frontend *fe = &state->frontend;
1668
1669         int tmg_lock = 0, i;
1670         s32 tmg_cpt = 0, dir = 1, steps, cur_step = 0, freq;
1671         u32 srate_coarse = 0, agc2 = 0, car_step = 1200, reg;
1672         u32 agc2th;
1673
1674         if (state->internal->dev_ver >= 0x30)
1675                 agc2th = 0x2e00;
1676         else
1677                 agc2th = 0x1f00;
1678
1679         reg = STV090x_READ_DEMOD(state, DMDISTATE);
1680         STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f); /* Demod RESET */
1681         if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
1682                 goto err;
1683         if (STV090x_WRITE_DEMOD(state, TMGCFG, 0x12) < 0)
1684                 goto err;
1685         if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0)
1686                 goto err;
1687         if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xf0) < 0)
1688                 goto err;
1689         if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xe0) < 0)
1690                 goto err;
1691         reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1692         STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 1);
1693         STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
1694         if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1695                 goto err;
1696
1697         if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0)
1698                 goto err;
1699         if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1700                 goto err;
1701         if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0)
1702                 goto err;
1703         if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1704                 goto err;
1705         if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0)
1706                 goto err;
1707         if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x50) < 0)
1708                 goto err;
1709
1710         if (state->internal->dev_ver >= 0x30) {
1711                 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x99) < 0)
1712                         goto err;
1713                 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x98) < 0)
1714                         goto err;
1715
1716         } else if (state->internal->dev_ver >= 0x20) {
1717                 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0)
1718                         goto err;
1719                 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
1720                         goto err;
1721         }
1722
1723         if (state->srate <= 2000000)
1724                 car_step = 1000;
1725         else if (state->srate <= 5000000)
1726                 car_step = 2000;
1727         else if (state->srate <= 12000000)
1728                 car_step = 3000;
1729         else
1730                 car_step = 5000;
1731
1732         steps  = -1 + ((state->search_range / 1000) / car_step);
1733         steps /= 2;
1734         steps  = (2 * steps) + 1;
1735         if (steps < 0)
1736                 steps = 1;
1737         else if (steps > 10) {
1738                 steps = 11;
1739                 car_step = (state->search_range / 1000) / 10;
1740         }
1741         cur_step = 0;
1742         dir = 1;
1743         freq = state->frequency;
1744
1745         while ((!tmg_lock) && (cur_step < steps)) {
1746                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5f) < 0) /* Demod RESET */
1747                         goto err;
1748                 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
1749                         goto err;
1750                 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
1751                         goto err;
1752                 if (STV090x_WRITE_DEMOD(state, SFRINIT1, 0x00) < 0)
1753                         goto err;
1754                 if (STV090x_WRITE_DEMOD(state, SFRINIT0, 0x00) < 0)
1755                         goto err;
1756                 /* trigger acquisition */
1757                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x40) < 0)
1758                         goto err;
1759                 msleep(50);
1760                 for (i = 0; i < 10; i++) {
1761                         reg = STV090x_READ_DEMOD(state, DSTATUS);
1762                         if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
1763                                 tmg_cpt++;
1764                         agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1765                                 STV090x_READ_DEMOD(state, AGC2I0);
1766                 }
1767                 agc2 /= 10;
1768                 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
1769                 cur_step++;
1770                 dir *= -1;
1771                 if ((tmg_cpt >= 5) && (agc2 < agc2th) &&
1772                     (srate_coarse < 50000000) && (srate_coarse > 850000))
1773                         tmg_lock = 1;
1774                 else if (cur_step < steps) {
1775                         if (dir > 0)
1776                                 freq += cur_step * car_step;
1777                         else
1778                                 freq -= cur_step * car_step;
1779
1780                         /* Setup tuner */
1781                         if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
1782                                 goto err;
1783
1784                         if (state->config->tuner_set_frequency) {
1785                                 if (state->config->tuner_set_frequency(fe, freq) < 0)
1786                                         goto err_gateoff;
1787                         }
1788
1789                         if (state->config->tuner_set_bandwidth) {
1790                                 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
1791                                         goto err_gateoff;
1792                         }
1793
1794                         if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
1795                                 goto err;
1796
1797                         msleep(50);
1798
1799                         if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
1800                                 goto err;
1801
1802                         if (state->config->tuner_get_status) {
1803                                 if (state->config->tuner_get_status(fe, &reg) < 0)
1804                                         goto err_gateoff;
1805                         }
1806
1807                         if (reg)
1808                                 dprintk(FE_DEBUG, 1, "Tuner phase locked");
1809                         else
1810                                 dprintk(FE_DEBUG, 1, "Tuner unlocked");
1811
1812                         if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
1813                                 goto err;
1814
1815                 }
1816         }
1817         if (!tmg_lock)
1818                 srate_coarse = 0;
1819         else
1820                 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
1821
1822         return srate_coarse;
1823
1824 err_gateoff:
1825         stv090x_i2c_gate_ctrl(fe, 0);
1826 err:
1827         dprintk(FE_ERROR, 1, "I/O error");
1828         return -1;
1829 }
1830
1831 static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
1832 {
1833         u32 srate_coarse, freq_coarse, sym, reg;
1834
1835         srate_coarse = stv090x_get_srate(state, state->internal->mclk);
1836         freq_coarse  = STV090x_READ_DEMOD(state, CFR2) << 8;
1837         freq_coarse |= STV090x_READ_DEMOD(state, CFR1);
1838         sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1839
1840         if (sym < state->srate)
1841                 srate_coarse = 0;
1842         else {
1843                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0) /* Demod RESET */
1844                         goto err;
1845                 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
1846                         goto err;
1847                 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
1848                         goto err;
1849                 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
1850                         goto err;
1851                 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
1852                         goto err;
1853                 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1854                 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
1855                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1856                         goto err;
1857
1858                 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1859                         goto err;
1860
1861                 if (state->internal->dev_ver >= 0x30) {
1862                         if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x79) < 0)
1863                                 goto err;
1864                 } else if (state->internal->dev_ver >= 0x20) {
1865                         if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
1866                                 goto err;
1867                 }
1868
1869                 if (srate_coarse > 3000000) {
1870                         sym  = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1871                         sym  = (sym / 1000) * 65536;
1872                         sym /= (state->internal->mclk / 1000);
1873                         if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1874                                 goto err;
1875                         if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1876                                 goto err;
1877                         sym  = 10 * (srate_coarse / 13); /* SFRLOW = SFR - 30% */
1878                         sym  = (sym / 1000) * 65536;
1879                         sym /= (state->internal->mclk / 1000);
1880                         if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1881                                 goto err;
1882                         if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1883                                 goto err;
1884                         sym  = (srate_coarse / 1000) * 65536;
1885                         sym /= (state->internal->mclk / 1000);
1886                         if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1887                                 goto err;
1888                         if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
1889                                 goto err;
1890                 } else {
1891                         sym  = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1892                         sym  = (sym / 100) * 65536;
1893                         sym /= (state->internal->mclk / 100);
1894                         if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1895                                 goto err;
1896                         if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1897                                 goto err;
1898                         sym  = 10 * (srate_coarse / 14); /* SFRLOW = SFR - 30% */
1899                         sym  = (sym / 100) * 65536;
1900                         sym /= (state->internal->mclk / 100);
1901                         if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1902                                 goto err;
1903                         if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1904                                 goto err;
1905                         sym  = (srate_coarse / 100) * 65536;
1906                         sym /= (state->internal->mclk / 100);
1907                         if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1908                                 goto err;
1909                         if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
1910                                 goto err;
1911                 }
1912                 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
1913                         goto err;
1914                 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_coarse >> 8) & 0xff) < 0)
1915                         goto err;
1916                 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_coarse & 0xff) < 0)
1917                         goto err;
1918                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0) /* trigger acquisition */
1919                         goto err;
1920         }
1921
1922         return srate_coarse;
1923
1924 err:
1925         dprintk(FE_ERROR, 1, "I/O error");
1926         return -1;
1927 }
1928
1929 static int stv090x_get_dmdlock(struct stv090x_state *state, s32 timeout)
1930 {
1931         s32 timer = 0, lock = 0;
1932         u32 reg;
1933         u8 stat;
1934
1935         while ((timer < timeout) && (!lock)) {
1936                 reg = STV090x_READ_DEMOD(state, DMDSTATE);
1937                 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
1938
1939                 switch (stat) {
1940                 case 0: /* searching */
1941                 case 1: /* first PLH detected */
1942                 default:
1943                         dprintk(FE_DEBUG, 1, "Demodulator searching ..");
1944                         lock = 0;
1945                         break;
1946                 case 2: /* DVB-S2 mode */
1947                 case 3: /* DVB-S1/legacy mode */
1948                         reg = STV090x_READ_DEMOD(state, DSTATUS);
1949                         lock = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
1950                         break;
1951                 }
1952
1953                 if (!lock)
1954                         msleep(10);
1955                 else
1956                         dprintk(FE_DEBUG, 1, "Demodulator acquired LOCK");
1957
1958                 timer += 10;
1959         }
1960         return lock;
1961 }
1962
1963 static int stv090x_blind_search(struct stv090x_state *state)
1964 {
1965         u32 agc2, reg, srate_coarse;
1966         s32 cpt_fail, agc2_ovflw, i;
1967         u8 k_ref, k_max, k_min;
1968         int coarse_fail = 0;
1969         int lock;
1970
1971         k_max = 110;
1972         k_min = 10;
1973
1974         agc2 = stv090x_get_agc2_min_level(state);
1975
1976         if (agc2 > STV090x_SEARCH_AGC2_TH(state->internal->dev_ver)) {
1977                 lock = 0;
1978         } else {
1979
1980                 if (state->internal->dev_ver <= 0x20) {
1981                         if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
1982                                 goto err;
1983                 } else {
1984                         /* > Cut 3 */
1985                         if (STV090x_WRITE_DEMOD(state, CARCFG, 0x06) < 0)
1986                                 goto err;
1987                 }
1988
1989                 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
1990                         goto err;
1991
1992                 if (state->internal->dev_ver >= 0x20) {
1993                         if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1994                                 goto err;
1995                         if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
1996                                 goto err;
1997                         if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
1998                                 goto err;
1999                         if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0) /* set viterbi hysteresis */
2000                                 goto err;
2001                 }
2002
2003                 k_ref = k_max;
2004                 do {
2005                         if (STV090x_WRITE_DEMOD(state, KREFTMG, k_ref) < 0)
2006                                 goto err;
2007                         if (stv090x_srate_srch_coarse(state) != 0) {
2008                                 srate_coarse = stv090x_srate_srch_fine(state);
2009                                 if (srate_coarse != 0) {
2010                                         stv090x_get_lock_tmg(state);
2011                                         lock = stv090x_get_dmdlock(state,
2012                                                         state->DemodTimeout);
2013                                 } else {
2014                                         lock = 0;
2015                                 }
2016                         } else {
2017                                 cpt_fail = 0;
2018                                 agc2_ovflw = 0;
2019                                 for (i = 0; i < 10; i++) {
2020                                         agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
2021                                                 STV090x_READ_DEMOD(state, AGC2I0);
2022                                         if (agc2 >= 0xff00)
2023                                                 agc2_ovflw++;
2024                                         reg = STV090x_READ_DEMOD(state, DSTATUS2);
2025                                         if ((STV090x_GETFIELD_Px(reg, CFR_OVERFLOW_FIELD) == 0x01) &&
2026                                             (STV090x_GETFIELD_Px(reg, DEMOD_DELOCK_FIELD) == 0x01))
2027
2028                                                 cpt_fail++;
2029                                 }
2030                                 if ((cpt_fail > 7) || (agc2_ovflw > 7))
2031                                         coarse_fail = 1;
2032
2033                                 lock = 0;
2034                         }
2035                         k_ref -= 20;
2036                 } while ((k_ref >= k_min) && (!lock) && (!coarse_fail));
2037         }
2038
2039         return lock;
2040
2041 err:
2042         dprintk(FE_ERROR, 1, "I/O error");
2043         return -1;
2044 }
2045
2046 static int stv090x_chk_tmg(struct stv090x_state *state)
2047 {
2048         u32 reg;
2049         s32 tmg_cpt = 0, i;
2050         u8 freq, tmg_thh, tmg_thl;
2051         int tmg_lock = 0;
2052
2053         freq = STV090x_READ_DEMOD(state, CARFREQ);
2054         tmg_thh = STV090x_READ_DEMOD(state, TMGTHRISE);
2055         tmg_thl = STV090x_READ_DEMOD(state, TMGTHFALL);
2056         if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
2057                 goto err;
2058         if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
2059                 goto err;
2060
2061         reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2062         STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00); /* stop carrier offset search */
2063         if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2064                 goto err;
2065         if (STV090x_WRITE_DEMOD(state, RTC, 0x80) < 0)
2066                 goto err;
2067
2068         if (STV090x_WRITE_DEMOD(state, RTCS2, 0x40) < 0)
2069                 goto err;
2070         if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x00) < 0)
2071                 goto err;
2072
2073         if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0) /* set car ofset to 0 */
2074                 goto err;
2075         if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
2076                 goto err;
2077         if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x65) < 0)
2078                 goto err;
2079
2080         if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* trigger acquisition */
2081                 goto err;
2082         msleep(10);
2083
2084         for (i = 0; i < 10; i++) {
2085                 reg = STV090x_READ_DEMOD(state, DSTATUS);
2086                 if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
2087                         tmg_cpt++;
2088                 msleep(1);
2089         }
2090         if (tmg_cpt >= 3)
2091                 tmg_lock = 1;
2092
2093         if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
2094                 goto err;
2095         if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0) /* DVB-S1 timing */
2096                 goto err;
2097         if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0) /* DVB-S2 timing */
2098                 goto err;
2099
2100         if (STV090x_WRITE_DEMOD(state, CARFREQ, freq) < 0)
2101                 goto err;
2102         if (STV090x_WRITE_DEMOD(state, TMGTHRISE, tmg_thh) < 0)
2103                 goto err;
2104         if (STV090x_WRITE_DEMOD(state, TMGTHFALL, tmg_thl) < 0)
2105                 goto err;
2106
2107         return  tmg_lock;
2108
2109 err:
2110         dprintk(FE_ERROR, 1, "I/O error");
2111         return -1;
2112 }
2113
2114 static int stv090x_get_coldlock(struct stv090x_state *state, s32 timeout_dmd)
2115 {
2116         struct dvb_frontend *fe = &state->frontend;
2117
2118         u32 reg;
2119         s32 car_step, steps, cur_step, dir, freq, timeout_lock;
2120         int lock = 0;
2121
2122         if (state->srate >= 10000000)
2123                 timeout_lock = timeout_dmd / 3;
2124         else
2125                 timeout_lock = timeout_dmd / 2;
2126
2127         lock = stv090x_get_dmdlock(state, timeout_lock); /* cold start wait */
2128         if (!lock) {
2129                 if (state->srate >= 10000000) {
2130                         if (stv090x_chk_tmg(state)) {
2131                                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2132                                         goto err;
2133                                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2134                                         goto err;
2135                                 lock = stv090x_get_dmdlock(state, timeout_dmd);
2136                         } else {
2137                                 lock = 0;
2138                         }
2139                 } else {
2140                         if (state->srate <= 4000000)
2141                                 car_step = 1000;
2142                         else if (state->srate <= 7000000)
2143                                 car_step = 2000;
2144                         else if (state->srate <= 10000000)
2145                                 car_step = 3000;
2146                         else
2147                                 car_step = 5000;
2148
2149                         steps  = (state->search_range / 1000) / car_step;
2150                         steps /= 2;
2151                         steps  = 2 * (steps + 1);
2152                         if (steps < 0)
2153                                 steps = 2;
2154                         else if (steps > 12)
2155                                 steps = 12;
2156
2157                         cur_step = 1;
2158                         dir = 1;
2159
2160                         if (!lock) {
2161                                 freq = state->frequency;
2162                                 state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + state->srate;
2163                                 while ((cur_step <= steps) && (!lock)) {
2164                                         if (dir > 0)
2165                                                 freq += cur_step * car_step;
2166                                         else
2167                                                 freq -= cur_step * car_step;
2168
2169                                         /* Setup tuner */
2170                                         if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2171                                                 goto err;
2172
2173                                         if (state->config->tuner_set_frequency) {
2174                                                 if (state->config->tuner_set_frequency(fe, freq) < 0)
2175                                                         goto err_gateoff;
2176                                         }
2177
2178                                         if (state->config->tuner_set_bandwidth) {
2179                                                 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
2180                                                         goto err_gateoff;
2181                                         }
2182
2183                                         if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2184                                                 goto err;
2185
2186                                         msleep(50);
2187
2188                                         if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2189                                                 goto err;
2190
2191                                         if (state->config->tuner_get_status) {
2192                                                 if (state->config->tuner_get_status(fe, &reg) < 0)
2193                                                         goto err_gateoff;
2194                                         }
2195
2196                                         if (reg)
2197                                                 dprintk(FE_DEBUG, 1, "Tuner phase locked");
2198                                         else
2199                                                 dprintk(FE_DEBUG, 1, "Tuner unlocked");
2200
2201                                         if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2202                                                 goto err;
2203
2204                                         STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c);
2205                                         if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
2206                                                 goto err;
2207                                         if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
2208                                                 goto err;
2209                                         if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2210                                                 goto err;
2211                                         if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2212                                                 goto err;
2213                                         lock = stv090x_get_dmdlock(state, (timeout_dmd / 3));
2214
2215                                         dir *= -1;
2216                                         cur_step++;
2217                                 }
2218                         }
2219                 }
2220         }
2221
2222         return lock;
2223
2224 err_gateoff:
2225         stv090x_i2c_gate_ctrl(fe, 0);
2226 err:
2227         dprintk(FE_ERROR, 1, "I/O error");
2228         return -1;
2229 }
2230
2231 static int stv090x_get_loop_params(struct stv090x_state *state, s32 *freq_inc, s32 *timeout_sw, s32 *steps)
2232 {
2233         s32 timeout, inc, steps_max, srate, car_max;
2234
2235         srate = state->srate;
2236         car_max = state->search_range / 1000;
2237         car_max += car_max / 10;
2238         car_max  = 65536 * (car_max / 2);
2239         car_max /= (state->internal->mclk / 1000);
2240
2241         if (car_max > 0x4000)
2242                 car_max = 0x4000 ; /* maxcarrier should be<= +-1/4 Mclk */
2243
2244         inc  = srate;
2245         inc /= state->internal->mclk / 1000;
2246         inc *= 256;
2247         inc *= 256;
2248         inc /= 1000;
2249
2250         switch (state->search_mode) {
2251         case STV090x_SEARCH_DVBS1:
2252         case STV090x_SEARCH_DSS:
2253                 inc *= 3; /* freq step = 3% of srate */
2254                 timeout = 20;
2255                 break;
2256
2257         case STV090x_SEARCH_DVBS2:
2258                 inc *= 4;
2259                 timeout = 25;
2260                 break;
2261
2262         case STV090x_SEARCH_AUTO:
2263         default:
2264                 inc *= 3;
2265                 timeout = 25;
2266                 break;
2267         }
2268         inc /= 100;
2269         if ((inc > car_max) || (inc < 0))
2270                 inc = car_max / 2; /* increment <= 1/8 Mclk */
2271
2272         timeout *= 27500; /* 27.5 Msps reference */
2273         if (srate > 0)
2274                 timeout /= (srate / 1000);
2275
2276         if ((timeout > 100) || (timeout < 0))
2277                 timeout = 100;
2278
2279         steps_max = (car_max / inc) + 1; /* min steps = 3 */
2280         if ((steps_max > 100) || (steps_max < 0)) {
2281                 steps_max = 100; /* max steps <= 100 */
2282                 inc = car_max / steps_max;
2283         }
2284         *freq_inc = inc;
2285         *timeout_sw = timeout;
2286         *steps = steps_max;
2287
2288         return 0;
2289 }
2290
2291 static int stv090x_chk_signal(struct stv090x_state *state)
2292 {
2293         s32 offst_car, agc2, car_max;
2294         int no_signal;
2295
2296         offst_car  = STV090x_READ_DEMOD(state, CFR2) << 8;
2297         offst_car |= STV090x_READ_DEMOD(state, CFR1);
2298         offst_car = comp2(offst_car, 16);
2299
2300         agc2  = STV090x_READ_DEMOD(state, AGC2I1) << 8;
2301         agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
2302         car_max = state->search_range / 1000;
2303
2304         car_max += (car_max / 10); /* 10% margin */
2305         car_max  = (65536 * car_max / 2);
2306         car_max /= state->internal->mclk / 1000;
2307
2308         if (car_max > 0x4000)
2309                 car_max = 0x4000;
2310
2311         if ((agc2 > 0x2000) || (offst_car > 2 * car_max) || (offst_car < -2 * car_max)) {
2312                 no_signal = 1;
2313                 dprintk(FE_DEBUG, 1, "No Signal");
2314         } else {
2315                 no_signal = 0;
2316                 dprintk(FE_DEBUG, 1, "Found Signal");
2317         }
2318
2319         return no_signal;
2320 }
2321
2322 static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 timeout, int zigzag, s32 steps_max)
2323 {
2324         int no_signal, lock = 0;
2325         s32 cpt_step = 0, offst_freq, car_max;
2326         u32 reg;
2327
2328         car_max  = state->search_range / 1000;
2329         car_max += (car_max / 10);
2330         car_max  = (65536 * car_max / 2);
2331         car_max /= (state->internal->mclk / 1000);
2332         if (car_max > 0x4000)
2333                 car_max = 0x4000;
2334
2335         if (zigzag)
2336                 offst_freq = 0;
2337         else
2338                 offst_freq = -car_max + inc;
2339
2340         do {
2341                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0)
2342                         goto err;
2343                 if (STV090x_WRITE_DEMOD(state, CFRINIT1, ((offst_freq / 256) & 0xff)) < 0)
2344                         goto err;
2345                 if (STV090x_WRITE_DEMOD(state, CFRINIT0, offst_freq & 0xff) < 0)
2346                         goto err;
2347                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
2348                         goto err;
2349
2350                 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
2351                 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x1); /* stop DVB-S2 packet delin */
2352                 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
2353                         goto err;
2354
2355                 if (zigzag) {
2356                         if (offst_freq >= 0)
2357                                 offst_freq = -offst_freq - 2 * inc;
2358                         else
2359                                 offst_freq = -offst_freq;
2360                 } else {
2361                         offst_freq += 2 * inc;
2362                 }
2363
2364                 cpt_step++;
2365
2366                 lock = stv090x_get_dmdlock(state, timeout);
2367                 no_signal = stv090x_chk_signal(state);
2368
2369         } while ((!lock) &&
2370                  (!no_signal) &&
2371                   ((offst_freq - inc) < car_max) &&
2372                   ((offst_freq + inc) > -car_max) &&
2373                   (cpt_step < steps_max));
2374
2375         reg = STV090x_READ_DEMOD(state, PDELCTRL1);
2376         STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0);
2377         if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
2378                         goto err;
2379
2380         return lock;
2381 err:
2382         dprintk(FE_ERROR, 1, "I/O error");
2383         return -1;
2384 }
2385
2386 static int stv090x_sw_algo(struct stv090x_state *state)
2387 {
2388         int no_signal, zigzag, lock = 0;
2389         u32 reg;
2390
2391         s32 dvbs2_fly_wheel;
2392         s32 inc, timeout_step, trials, steps_max;
2393
2394         /* get params */
2395         stv090x_get_loop_params(state, &inc, &timeout_step, &steps_max);
2396
2397         switch (state->search_mode) {
2398         case STV090x_SEARCH_DVBS1:
2399         case STV090x_SEARCH_DSS:
2400                 /* accelerate the frequency detector */
2401                 if (state->internal->dev_ver >= 0x20) {
2402                         if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3B) < 0)
2403                                 goto err;
2404                 }
2405
2406                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x49) < 0)
2407                         goto err;
2408                 zigzag = 0;
2409                 break;
2410
2411         case STV090x_SEARCH_DVBS2:
2412                 if (state->internal->dev_ver >= 0x20) {
2413                         if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2414                                 goto err;
2415                 }
2416
2417                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2418                         goto err;
2419                 zigzag = 1;
2420                 break;
2421
2422         case STV090x_SEARCH_AUTO:
2423         default:
2424                 /* accelerate the frequency detector */
2425                 if (state->internal->dev_ver >= 0x20) {
2426                         if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3b) < 0)
2427                                 goto err;
2428                         if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2429                                 goto err;
2430                 }
2431
2432                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0xc9) < 0)
2433                         goto err;
2434                 zigzag = 0;
2435                 break;
2436         }
2437
2438         trials = 0;
2439         do {
2440                 lock = stv090x_search_car_loop(state, inc, timeout_step, zigzag, steps_max);
2441                 no_signal = stv090x_chk_signal(state);
2442                 trials++;
2443
2444                 /*run the SW search 2 times maximum*/
2445                 if (lock || no_signal || (trials == 2)) {
2446                         /*Check if the demod is not losing lock in DVBS2*/
2447                         if (state->internal->dev_ver >= 0x20) {
2448                                 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
2449                                         goto err;
2450                                 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
2451                                         goto err;
2452                         }
2453
2454                         reg = STV090x_READ_DEMOD(state, DMDSTATE);
2455                         if ((lock) && (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == STV090x_DVBS2)) {
2456                                 /*Check if the demod is not losing lock in DVBS2*/
2457                                 msleep(timeout_step);
2458                                 reg = STV090x_READ_DEMOD(state, DMDFLYW);
2459                                 dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
2460                                 if (dvbs2_fly_wheel < 0xd) {     /*if correct frames is decrementing */
2461                                         msleep(timeout_step);
2462                                         reg = STV090x_READ_DEMOD(state, DMDFLYW);
2463                                         dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
2464                                 }
2465                                 if (dvbs2_fly_wheel < 0xd) {
2466                                         /*FALSE lock, The demod is loosing lock */
2467                                         lock = 0;
2468                                         if (trials < 2) {
2469                                                 if (state->internal->dev_ver >= 0x20) {
2470                                                         if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2471                                                                 goto err;
2472                                                 }
2473
2474                                                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2475                                                         goto err;
2476                                         }
2477                                 }
2478                         }
2479                 }
2480         } while ((!lock) && (trials < 2) && (!no_signal));
2481
2482         return lock;
2483 err:
2484         dprintk(FE_ERROR, 1, "I/O error");
2485         return -1;
2486 }
2487
2488 static enum stv090x_delsys stv090x_get_std(struct stv090x_state *state)
2489 {
2490         u32 reg;
2491         enum stv090x_delsys delsys;
2492
2493         reg = STV090x_READ_DEMOD(state, DMDSTATE);
2494         if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 2)
2495                 delsys = STV090x_DVBS2;
2496         else if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 3) {
2497                 reg = STV090x_READ_DEMOD(state, FECM);
2498                 if (STV090x_GETFIELD_Px(reg, DSS_DVB_FIELD) == 1)
2499                         delsys = STV090x_DSS;
2500                 else
2501                         delsys = STV090x_DVBS1;
2502         } else {
2503                 delsys = STV090x_ERROR;
2504         }
2505
2506         return delsys;
2507 }
2508
2509 /* in Hz */
2510 static s32 stv090x_get_car_freq(struct stv090x_state *state, u32 mclk)
2511 {
2512         s32 derot, int_1, int_2, tmp_1, tmp_2;
2513
2514         derot  = STV090x_READ_DEMOD(state, CFR2) << 16;
2515         derot |= STV090x_READ_DEMOD(state, CFR1) <<  8;
2516         derot |= STV090x_READ_DEMOD(state, CFR0);
2517
2518         derot = comp2(derot, 24);
2519         int_1 = mclk >> 12;
2520         int_2 = derot >> 12;
2521
2522         /* carrier_frequency = MasterClock * Reg / 2^24 */
2523         tmp_1 = mclk % 0x1000;
2524         tmp_2 = derot % 0x1000;
2525
2526         derot = (int_1 * int_2) +
2527                 ((int_1 * tmp_2) >> 12) +
2528                 ((int_2 * tmp_1) >> 12);
2529
2530         return derot;
2531 }
2532
2533 static int stv090x_get_viterbi(struct stv090x_state *state)
2534 {
2535         u32 reg, rate;
2536
2537         reg = STV090x_READ_DEMOD(state, VITCURPUN);
2538         rate = STV090x_GETFIELD_Px(reg, VIT_CURPUN_FIELD);
2539
2540         switch (rate) {
2541         case 13:
2542                 state->fec = STV090x_PR12;
2543                 break;
2544
2545         case 18:
2546                 state->fec = STV090x_PR23;
2547                 break;
2548
2549         case 21:
2550                 state->fec = STV090x_PR34;
2551                 break;
2552
2553         case 24:
2554                 state->fec = STV090x_PR56;
2555                 break;
2556
2557         case 25:
2558                 state->fec = STV090x_PR67;
2559                 break;
2560
2561         case 26:
2562                 state->fec = STV090x_PR78;
2563                 break;
2564
2565         default:
2566                 state->fec = STV090x_PRERR;
2567                 break;
2568         }
2569
2570         return 0;
2571 }
2572
2573 static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *state)
2574 {
2575         struct dvb_frontend *fe = &state->frontend;
2576
2577         u8 tmg;
2578         u32 reg;
2579         s32 i = 0, offst_freq;
2580
2581         msleep(5);
2582
2583         if (state->algo == STV090x_BLIND_SEARCH) {
2584                 tmg = STV090x_READ_DEMOD(state, TMGREG2);
2585                 STV090x_WRITE_DEMOD(state, SFRSTEP, 0x5c);
2586                 while ((i <= 50) && (tmg != 0) && (tmg != 0xff)) {
2587                         tmg = STV090x_READ_DEMOD(state, TMGREG2);
2588                         msleep(5);
2589                         i += 5;
2590                 }
2591         }
2592         state->delsys = stv090x_get_std(state);
2593
2594         if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2595                 goto err;
2596
2597         if (state->config->tuner_get_frequency) {
2598                 if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
2599                         goto err_gateoff;
2600         }
2601
2602         if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2603                 goto err;
2604
2605         offst_freq = stv090x_get_car_freq(state, state->internal->mclk) / 1000;
2606         state->frequency += offst_freq;
2607
2608         if (stv090x_get_viterbi(state) < 0)
2609                 goto err;
2610
2611         reg = STV090x_READ_DEMOD(state, DMDMODCOD);
2612         state->modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
2613         state->pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
2614         state->frame_len = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) >> 1;
2615         reg = STV090x_READ_DEMOD(state, TMGOBS);
2616         state->rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
2617         reg = STV090x_READ_DEMOD(state, FECM);
2618         state->inversion = STV090x_GETFIELD_Px(reg, IQINV_FIELD);
2619
2620         if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000)) {
2621
2622                 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2623                         goto err;
2624
2625                 if (state->config->tuner_get_frequency) {
2626                         if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
2627                                 goto err_gateoff;
2628                 }
2629
2630                 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2631                         goto err;
2632
2633                 if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
2634                         return STV090x_RANGEOK;
2635                 else if (abs(offst_freq) <= (stv090x_car_width(state->srate, state->rolloff) / 2000))
2636                         return STV090x_RANGEOK;
2637                 else
2638                         return STV090x_OUTOFRANGE; /* Out of Range */
2639         } else {
2640                 if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
2641                         return STV090x_RANGEOK;
2642                 else
2643                         return STV090x_OUTOFRANGE;
2644         }
2645
2646         return STV090x_OUTOFRANGE;
2647
2648 err_gateoff:
2649         stv090x_i2c_gate_ctrl(fe, 0);
2650 err:
2651         dprintk(FE_ERROR, 1, "I/O error");
2652         return -1;
2653 }
2654
2655 static u32 stv090x_get_tmgoffst(struct stv090x_state *state, u32 srate)
2656 {
2657         s32 offst_tmg;
2658
2659         offst_tmg  = STV090x_READ_DEMOD(state, TMGREG2) << 16;
2660         offst_tmg |= STV090x_READ_DEMOD(state, TMGREG1) <<  8;
2661         offst_tmg |= STV090x_READ_DEMOD(state, TMGREG0);
2662
2663         offst_tmg = comp2(offst_tmg, 24); /* 2's complement */
2664         if (!offst_tmg)
2665                 offst_tmg = 1;
2666
2667         offst_tmg  = ((s32) srate * 10) / ((s32) 0x1000000 / offst_tmg);
2668         offst_tmg /= 320;
2669
2670         return offst_tmg;
2671 }
2672
2673 static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_modcod modcod, s32 pilots)
2674 {
2675         u8 aclc = 0x29;
2676         s32 i;
2677         struct stv090x_long_frame_crloop *car_loop, *car_loop_qpsk_low, *car_loop_apsk_low;
2678
2679         if (state->internal->dev_ver == 0x20) {
2680                 car_loop                = stv090x_s2_crl_cut20;
2681                 car_loop_qpsk_low       = stv090x_s2_lowqpsk_crl_cut20;
2682                 car_loop_apsk_low       = stv090x_s2_apsk_crl_cut20;
2683         } else {
2684                 /* >= Cut 3 */
2685                 car_loop                = stv090x_s2_crl_cut30;
2686                 car_loop_qpsk_low       = stv090x_s2_lowqpsk_crl_cut30;
2687                 car_loop_apsk_low       = stv090x_s2_apsk_crl_cut30;
2688         }
2689
2690         if (modcod < STV090x_QPSK_12) {
2691                 i = 0;
2692                 while ((i < 3) && (modcod != car_loop_qpsk_low[i].modcod))
2693                         i++;
2694
2695                 if (i >= 3)
2696                         i = 2;
2697
2698         } else {
2699                 i = 0;
2700                 while ((i < 14) && (modcod != car_loop[i].modcod))
2701                         i++;
2702
2703                 if (i >= 14) {
2704                         i = 0;
2705                         while ((i < 11) && (modcod != car_loop_apsk_low[i].modcod))
2706                                 i++;
2707
2708                         if (i >= 11)
2709                                 i = 10;
2710                 }
2711         }
2712
2713         if (modcod <= STV090x_QPSK_25) {
2714                 if (pilots) {
2715                         if (state->srate <= 3000000)
2716                                 aclc = car_loop_qpsk_low[i].crl_pilots_on_2;
2717                         else if (state->srate <= 7000000)
2718                                 aclc = car_loop_qpsk_low[i].crl_pilots_on_5;
2719                         else if (state->srate <= 15000000)
2720                                 aclc = car_loop_qpsk_low[i].crl_pilots_on_10;
2721                         else if (state->srate <= 25000000)
2722                                 aclc = car_loop_qpsk_low[i].crl_pilots_on_20;
2723                         else
2724                                 aclc = car_loop_qpsk_low[i].crl_pilots_on_30;
2725                 } else {
2726                         if (state->srate <= 3000000)
2727                                 aclc = car_loop_qpsk_low[i].crl_pilots_off_2;
2728                         else if (state->srate <= 7000000)
2729                                 aclc = car_loop_qpsk_low[i].crl_pilots_off_5;
2730                         else if (state->srate <= 15000000)
2731                                 aclc = car_loop_qpsk_low[i].crl_pilots_off_10;
2732                         else if (state->srate <= 25000000)
2733                                 aclc = car_loop_qpsk_low[i].crl_pilots_off_20;
2734                         else
2735                                 aclc = car_loop_qpsk_low[i].crl_pilots_off_30;
2736                 }
2737
2738         } else if (modcod <= STV090x_8PSK_910) {
2739                 if (pilots) {
2740                         if (state->srate <= 3000000)
2741                                 aclc = car_loop[i].crl_pilots_on_2;
2742                         else if (state->srate <= 7000000)
2743                                 aclc = car_loop[i].crl_pilots_on_5;
2744                         else if (state->srate <= 15000000)
2745                                 aclc = car_loop[i].crl_pilots_on_10;
2746                         else if (state->srate <= 25000000)
2747                                 aclc = car_loop[i].crl_pilots_on_20;
2748                         else
2749                                 aclc = car_loop[i].crl_pilots_on_30;
2750                 } else {
2751                         if (state->srate <= 3000000)
2752                                 aclc = car_loop[i].crl_pilots_off_2;
2753                         else if (state->srate <= 7000000)
2754                                 aclc = car_loop[i].crl_pilots_off_5;
2755                         else if (state->srate <= 15000000)
2756                                 aclc = car_loop[i].crl_pilots_off_10;
2757                         else if (state->srate <= 25000000)
2758                                 aclc = car_loop[i].crl_pilots_off_20;
2759                         else
2760                                 aclc = car_loop[i].crl_pilots_off_30;
2761                 }
2762         } else { /* 16APSK and 32APSK */
2763                 if (state->srate <= 3000000)
2764                         aclc = car_loop_apsk_low[i].crl_pilots_on_2;
2765                 else if (state->srate <= 7000000)
2766                         aclc = car_loop_apsk_low[i].crl_pilots_on_5;
2767                 else if (state->srate <= 15000000)
2768                         aclc = car_loop_apsk_low[i].crl_pilots_on_10;
2769                 else if (state->srate <= 25000000)
2770                         aclc = car_loop_apsk_low[i].crl_pilots_on_20;
2771                 else
2772                         aclc = car_loop_apsk_low[i].crl_pilots_on_30;
2773         }
2774
2775         return aclc;
2776 }
2777
2778 static u8 stv090x_optimize_carloop_short(struct stv090x_state *state)
2779 {
2780         struct stv090x_short_frame_crloop *short_crl = NULL;
2781         s32 index = 0;
2782         u8 aclc = 0x0b;
2783
2784         switch (state->modulation) {
2785         case STV090x_QPSK:
2786         default:
2787                 index = 0;
2788                 break;
2789         case STV090x_8PSK:
2790                 index = 1;
2791                 break;
2792         case STV090x_16APSK:
2793                 index = 2;
2794                 break;
2795         case STV090x_32APSK:
2796                 index = 3;
2797                 break;
2798         }
2799
2800         if (state->internal->dev_ver >= 0x30) {
2801                 /* Cut 3.0 and up */
2802                 short_crl = stv090x_s2_short_crl_cut30;
2803         } else {
2804                 /* Cut 2.0 and up: we don't support cuts older than 2.0 */
2805                 short_crl = stv090x_s2_short_crl_cut20;
2806         }
2807
2808         if (state->srate <= 3000000)
2809                 aclc = short_crl[index].crl_2;
2810         else if (state->srate <= 7000000)
2811                 aclc = short_crl[index].crl_5;
2812         else if (state->srate <= 15000000)
2813                 aclc = short_crl[index].crl_10;
2814         else if (state->srate <= 25000000)
2815                 aclc = short_crl[index].crl_20;
2816         else
2817                 aclc = short_crl[index].crl_30;
2818
2819         return aclc;
2820 }
2821
2822 static int stv090x_optimize_track(struct stv090x_state *state)
2823 {
2824         struct dvb_frontend *fe = &state->frontend;
2825
2826         enum stv090x_rolloff rolloff;
2827         enum stv090x_modcod modcod;
2828
2829         s32 srate, pilots, aclc, f_1, f_0, i = 0, blind_tune = 0;
2830         u32 reg;
2831
2832         srate  = stv090x_get_srate(state, state->internal->mclk);
2833         srate += stv090x_get_tmgoffst(state, srate);
2834
2835         switch (state->delsys) {
2836         case STV090x_DVBS1:
2837         case STV090x_DSS:
2838                 if (state->search_mode == STV090x_SEARCH_AUTO) {
2839                         reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2840                         STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
2841                         STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
2842                         if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2843                                 goto err;
2844                 }
2845                 reg = STV090x_READ_DEMOD(state, DEMOD);
2846                 STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
2847                 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x01);
2848                 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
2849                         goto err;
2850
2851                 if (state->internal->dev_ver >= 0x30) {
2852                         if (stv090x_get_viterbi(state) < 0)
2853                                 goto err;
2854
2855                         if (state->fec == STV090x_PR12) {
2856                                 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x98) < 0)
2857                                         goto err;
2858                                 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2859                                         goto err;
2860                         } else {
2861                                 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x18) < 0)
2862                                         goto err;
2863                                 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2864                                         goto err;
2865                         }
2866                 }
2867
2868                 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
2869                         goto err;
2870                 break;
2871
2872         case STV090x_DVBS2:
2873                 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2874                 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
2875                 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
2876                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2877                         goto err;
2878                 if (STV090x_WRITE_DEMOD(state, ACLC, 0) < 0)
2879                         goto err;
2880                 if (STV090x_WRITE_DEMOD(state, BCLC, 0) < 0)
2881                         goto err;
2882                 if (state->frame_len == STV090x_LONG_FRAME) {
2883                         reg = STV090x_READ_DEMOD(state, DMDMODCOD);
2884                         modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
2885                         pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
2886                         aclc = stv090x_optimize_carloop(state, modcod, pilots);
2887                         if (modcod <= STV090x_QPSK_910) {
2888                                 STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc);
2889                         } else if (modcod <= STV090x_8PSK_910) {
2890                                 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2891                                         goto err;
2892                                 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
2893                                         goto err;
2894                         }
2895                         if ((state->demod_mode == STV090x_SINGLE) && (modcod > STV090x_8PSK_910)) {
2896                                 if (modcod <= STV090x_16APSK_910) {
2897                                         if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2898                                                 goto err;
2899                                         if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2900                                                 goto err;
2901                                 } else {
2902                                         if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2903                                                 goto err;
2904                                         if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
2905                                                 goto err;
2906                                 }
2907                         }
2908                 } else {
2909                         /*Carrier loop setting for short frame*/
2910                         aclc = stv090x_optimize_carloop_short(state);
2911                         if (state->modulation == STV090x_QPSK) {
2912                                 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc) < 0)
2913                                         goto err;
2914                         } else if (state->modulation == STV090x_8PSK) {
2915                                 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2916                                         goto err;
2917                                 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
2918                                         goto err;
2919                         } else if (state->modulation == STV090x_16APSK) {
2920                                 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2921                                         goto err;
2922                                 if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2923                                         goto err;
2924                         } else if (state->modulation == STV090x_32APSK)  {
2925                                 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2926                                         goto err;
2927                                 if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
2928                                         goto err;
2929                         }
2930                 }
2931
2932                 STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67); /* PER */
2933                 break;
2934
2935         case STV090x_UNKNOWN:
2936         default:
2937                 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2938                 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
2939                 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
2940                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2941                         goto err;
2942                 break;
2943         }
2944
2945         f_1 = STV090x_READ_DEMOD(state, CFR2);
2946         f_0 = STV090x_READ_DEMOD(state, CFR1);
2947         reg = STV090x_READ_DEMOD(state, TMGOBS);
2948         rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
2949
2950         if (state->algo == STV090x_BLIND_SEARCH) {
2951                 STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00);
2952                 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2953                 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0x00);
2954                 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
2955                 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2956                         goto err;
2957                 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
2958                         goto err;
2959
2960                 if (stv090x_set_srate(state, srate) < 0)
2961                         goto err;
2962                 blind_tune = 1;
2963
2964                 if (stv090x_dvbs_track_crl(state) < 0)
2965                         goto err;
2966         }
2967
2968         if (state->internal->dev_ver >= 0x20) {
2969                 if ((state->search_mode == STV090x_SEARCH_DVBS1)        ||
2970                     (state->search_mode == STV090x_SEARCH_DSS)          ||
2971                     (state->search_mode == STV090x_SEARCH_AUTO)) {
2972
2973                         if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x0a) < 0)
2974                                 goto err;
2975                         if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x00) < 0)
2976                                 goto err;
2977                 }
2978         }
2979
2980         if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
2981                 goto err;
2982
2983         /* AUTO tracking MODE */
2984         if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x80) < 0)
2985                 goto err;
2986         /* AUTO tracking MODE */
2987         if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x80) < 0)
2988                 goto err;
2989
2990         if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1) ||
2991             (state->srate < 10000000)) {
2992                 /* update initial carrier freq with the found freq offset */
2993                 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
2994                         goto err;
2995                 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
2996                         goto err;
2997                 state->tuner_bw = stv090x_car_width(srate, state->rolloff) + 10000000;
2998
2999                 if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1)) {
3000
3001                         if (state->algo != STV090x_WARM_SEARCH) {
3002
3003                                 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
3004                                         goto err;
3005
3006                                 if (state->config->tuner_set_bandwidth) {
3007                                         if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
3008                                                 goto err_gateoff;
3009                                 }
3010
3011                                 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
3012                                         goto err;
3013
3014                         }
3015                 }
3016                 if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000))
3017                         msleep(50); /* blind search: wait 50ms for SR stabilization */
3018                 else
3019                         msleep(5);
3020
3021                 stv090x_get_lock_tmg(state);
3022
3023                 if (!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) {
3024                         if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
3025                                 goto err;
3026                         if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
3027                                 goto err;
3028                         if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
3029                                 goto err;
3030                         if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
3031                                 goto err;
3032
3033                         i = 0;
3034
3035                         while ((!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) && (i <= 2)) {
3036
3037                                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
3038                                         goto err;
3039                                 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
3040                                         goto err;
3041                                 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
3042                                         goto err;
3043                                 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
3044                                         goto err;
3045                                 i++;
3046                         }
3047                 }
3048
3049         }
3050
3051         if (state->internal->dev_ver >= 0x20) {
3052                 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
3053                         goto err;
3054         }
3055
3056         if ((state->delsys == STV090x_DVBS1) || (state->delsys == STV090x_DSS))
3057                 stv090x_set_vit_thtracq(state);
3058
3059         return 0;
3060
3061 err_gateoff:
3062         stv090x_i2c_gate_ctrl(fe, 0);
3063 err:
3064         dprintk(FE_ERROR, 1, "I/O error");
3065         return -1;
3066 }
3067
3068 static int stv090x_get_feclock(struct stv090x_state *state, s32 timeout)
3069 {
3070         s32 timer = 0, lock = 0, stat;
3071         u32 reg;
3072
3073         while ((timer < timeout) && (!lock)) {
3074                 reg = STV090x_READ_DEMOD(state, DMDSTATE);
3075                 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
3076
3077                 switch (stat) {
3078                 case 0: /* searching */
3079                 case 1: /* first PLH detected */
3080                 default:
3081                         lock = 0;
3082                         break;
3083
3084                 case 2: /* DVB-S2 mode */
3085                         reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
3086                         lock = STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD);
3087                         break;
3088
3089                 case 3: /* DVB-S1/legacy mode */
3090                         reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
3091                         lock = STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD);
3092                         break;
3093                 }
3094                 if (!lock) {
3095                         msleep(10);
3096                         timer += 10;
3097                 }
3098         }
3099         return lock;
3100 }
3101
3102 static int stv090x_get_lock(struct stv090x_state *state, s32 timeout_dmd, s32 timeout_fec)
3103 {
3104         u32 reg;
3105         s32 timer = 0;
3106         int lock;
3107
3108         lock = stv090x_get_dmdlock(state, timeout_dmd);
3109         if (lock)
3110                 lock = stv090x_get_feclock(state, timeout_fec);
3111
3112         if (lock) {
3113                 lock = 0;
3114
3115                 while ((timer < timeout_fec) && (!lock)) {
3116                         reg = STV090x_READ_DEMOD(state, TSSTATUS);
3117                         lock = STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD);
3118                         msleep(1);
3119                         timer++;
3120                 }
3121         }
3122
3123         return lock;
3124 }
3125
3126 static int stv090x_set_s2rolloff(struct stv090x_state *state)
3127 {
3128         u32 reg;
3129
3130         if (state->internal->dev_ver <= 0x20) {
3131                 /* rolloff to auto mode if DVBS2 */
3132                 reg = STV090x_READ_DEMOD(state, DEMOD);
3133                 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x00);
3134                 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3135                         goto err;
3136         } else {
3137                 /* DVB-S2 rolloff to auto mode if DVBS2 */
3138                 reg = STV090x_READ_DEMOD(state, DEMOD);
3139                 STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 0x00);
3140                 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3141                         goto err;
3142         }
3143         return 0;
3144 err:
3145         dprintk(FE_ERROR, 1, "I/O error");
3146         return -1;
3147 }
3148
3149
3150 static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
3151 {
3152         struct dvb_frontend *fe = &state->frontend;
3153         enum stv090x_signal_state signal_state = STV090x_NOCARRIER;
3154         u32 reg;
3155         s32 agc1_power, power_iq = 0, i;
3156         int lock = 0, low_sr = 0, no_signal = 0;
3157
3158         reg = STV090x_READ_DEMOD(state, TSCFGH);
3159         STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* Stop path 1 stream merger */
3160         if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3161                 goto err;
3162
3163         if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod stop */
3164                 goto err;
3165
3166         if (state->internal->dev_ver >= 0x20) {
3167                 if (state->srate > 5000000) {
3168                         if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
3169                                 goto err;
3170                 } else {
3171                         if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x82) < 0)
3172                                 goto err;
3173                 }
3174         }
3175
3176         stv090x_get_lock_tmg(state);
3177
3178         if (state->algo == STV090x_BLIND_SEARCH) {
3179                 state->tuner_bw = 2 * 36000000; /* wide bw for unknown srate */
3180                 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0) /* wider srate scan */
3181                         goto err;
3182                 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
3183                         goto err;
3184                 if (stv090x_set_srate(state, 1000000) < 0) /* inital srate = 1Msps */
3185                         goto err;
3186         } else {
3187                 /* known srate */
3188                 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
3189                         goto err;
3190                 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
3191                         goto err;
3192
3193                 if (state->srate < 2000000) {
3194                         /* SR < 2MSPS */
3195                         if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x63) < 0)
3196                                 goto err;
3197                 } else {
3198                         /* SR >= 2Msps */
3199                         if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
3200                                 goto err;
3201                 }
3202
3203                 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
3204                         goto err;
3205
3206                 if (state->internal->dev_ver >= 0x20) {
3207                         if (STV090x_WRITE_DEMOD(state, KREFTMG, 0x5a) < 0)
3208                                 goto err;
3209                         if (state->algo == STV090x_COLD_SEARCH)
3210                                 state->tuner_bw = (15 * (stv090x_car_width(state->srate, state->rolloff) + 10000000)) / 10;
3211                         else if (state->algo == STV090x_WARM_SEARCH)
3212                                 state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + 10000000;
3213                 }
3214
3215                 /* if cold start or warm  (Symbolrate is known)
3216                  * use a Narrow symbol rate scan range
3217                  */
3218                 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0) /* narrow srate scan */
3219                         goto err;
3220
3221                 if (stv090x_set_srate(state, state->srate) < 0)
3222                         goto err;
3223
3224                 if (stv090x_set_max_srate(state, state->internal->mclk,
3225                                           state->srate) < 0)
3226                         goto err;
3227                 if (stv090x_set_min_srate(state, state->internal->mclk,
3228                                           state->srate) < 0)
3229                         goto err;
3230
3231                 if (state->srate >= 10000000)
3232                         low_sr = 0;
3233                 else
3234                         low_sr = 1;
3235         }
3236
3237         /* Setup tuner */
3238         if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
3239                 goto err;
3240
3241         if (state->config->tuner_set_bbgain) {
3242                 reg = state->config->tuner_bbgain;
3243                 if (reg == 0)
3244                         reg = 10; /* default: 10dB */
3245                 if (state->config->tuner_set_bbgain(fe, reg) < 0)
3246                         goto err_gateoff;
3247         }
3248
3249         if (state->config->tuner_set_frequency) {
3250                 if (state->config->tuner_set_frequency(fe, state->frequency) < 0)
3251                         goto err_gateoff;
3252         }
3253
3254         if (state->config->tuner_set_bandwidth) {
3255                 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
3256                         goto err_gateoff;
3257         }
3258
3259         if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
3260                 goto err;
3261
3262         msleep(50);
3263
3264         if (state->config->tuner_get_status) {
3265                 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
3266                         goto err;
3267                 if (state->config->tuner_get_status(fe, &reg) < 0)
3268                         goto err_gateoff;
3269                 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
3270                         goto err;
3271
3272                 if (reg)
3273                         dprintk(FE_DEBUG, 1, "Tuner phase locked");
3274                 else {
3275                         dprintk(FE_DEBUG, 1, "Tuner unlocked");
3276                         return STV090x_NOCARRIER;
3277                 }
3278         }
3279
3280         msleep(10);
3281         agc1_power = MAKEWORD16(STV090x_READ_DEMOD(state, AGCIQIN1),
3282                                 STV090x_READ_DEMOD(state, AGCIQIN0));
3283
3284         if (agc1_power == 0) {
3285                 /* If AGC1 integrator value is 0
3286                  * then read POWERI, POWERQ
3287                  */
3288                 for (i = 0; i < 5; i++) {
3289                         power_iq += (STV090x_READ_DEMOD(state, POWERI) +
3290                                      STV090x_READ_DEMOD(state, POWERQ)) >> 1;
3291                 }
3292                 power_iq /= 5;
3293         }
3294
3295         if ((agc1_power == 0) && (power_iq < STV090x_IQPOWER_THRESHOLD)) {
3296                 dprintk(FE_ERROR, 1, "No Signal: POWER_IQ=0x%02x", power_iq);
3297                 lock = 0;
3298                 signal_state = STV090x_NOAGC1;
3299         } else {
3300                 reg = STV090x_READ_DEMOD(state, DEMOD);
3301                 STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, state->inversion);
3302
3303                 if (state->internal->dev_ver <= 0x20) {
3304                         /* rolloff to auto mode if DVBS2 */
3305                         STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 1);
3306                 } else {
3307                         /* DVB-S2 rolloff to auto mode if DVBS2 */
3308                         STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 1);
3309                 }
3310                 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3311                         goto err;
3312
3313                 if (stv090x_delivery_search(state) < 0)
3314                         goto err;
3315
3316                 if (state->algo != STV090x_BLIND_SEARCH) {
3317                         if (stv090x_start_search(state) < 0)
3318                                 goto err;
3319                 }
3320         }
3321
3322         if (signal_state == STV090x_NOAGC1)
3323                 return signal_state;
3324
3325         if (state->algo == STV090x_BLIND_SEARCH)
3326                 lock = stv090x_blind_search(state);
3327
3328         else if (state->algo == STV090x_COLD_SEARCH)
3329                 lock = stv090x_get_coldlock(state, state->DemodTimeout);
3330
3331         else if (state->algo == STV090x_WARM_SEARCH)
3332                 lock = stv090x_get_dmdlock(state, state->DemodTimeout);
3333
3334         if ((!lock) && (state->algo == STV090x_COLD_SEARCH)) {
3335                 if (!low_sr) {
3336                         if (stv090x_chk_tmg(state))
3337                                 lock = stv090x_sw_algo(state);
3338                 }
3339         }
3340
3341         if (lock)
3342                 signal_state = stv090x_get_sig_params(state);
3343
3344         if ((lock) && (signal_state == STV090x_RANGEOK)) { /* signal within Range */
3345                 stv090x_optimize_track(state);
3346
3347                 if (state->internal->dev_ver >= 0x20) {
3348                         /* >= Cut 2.0 :release TS reset after
3349                          * demod lock and optimized Tracking
3350                          */
3351                         reg = STV090x_READ_DEMOD(state, TSCFGH);
3352                         STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
3353                         if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3354                                 goto err;
3355
3356                         msleep(3);
3357
3358                         STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
3359                         if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3360                                 goto err;
3361
3362                         STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
3363                         if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3364                                 goto err;
3365                 }
3366
3367                 lock = stv090x_get_lock(state, state->FecTimeout,
3368                                 state->FecTimeout);
3369                 if (lock) {
3370                         if (state->delsys == STV090x_DVBS2) {
3371                                 stv090x_set_s2rolloff(state);
3372
3373                                 reg = STV090x_READ_DEMOD(state, PDELCTRL2);
3374                                 STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 1);
3375                                 if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
3376                                         goto err;
3377                                 /* Reset DVBS2 packet delinator error counter */
3378                                 reg = STV090x_READ_DEMOD(state, PDELCTRL2);
3379                                 STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 0);
3380                                 if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
3381                                         goto err;
3382
3383                                 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67) < 0) /* PER */
3384                                         goto err;
3385                         } else {
3386                                 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
3387                                         goto err;
3388                         }
3389                         /* Reset the Total packet counter */
3390                         if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0x00) < 0)
3391                                 goto err;
3392                         /* Reset the packet Error counter2 */
3393                         if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3394                                 goto err;
3395                 } else {
3396                         signal_state = STV090x_NODATA;
3397                         no_signal = stv090x_chk_signal(state);
3398                 }
3399         }
3400         return signal_state;
3401
3402 err_gateoff:
3403         stv090x_i2c_gate_ctrl(fe, 0);
3404 err:
3405         dprintk(FE_ERROR, 1, "I/O error");
3406         return -1;
3407 }
3408
3409 static enum dvbfe_search stv090x_search(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
3410 {
3411         struct stv090x_state *state = fe->demodulator_priv;
3412         struct dtv_frontend_properties *props = &fe->dtv_property_cache;
3413
3414         if (p->frequency == 0)
3415                 return DVBFE_ALGO_SEARCH_INVALID;
3416
3417         state->delsys = props->delivery_system;
3418         state->frequency = p->frequency;
3419         state->srate = p->u.qpsk.symbol_rate;
3420         state->search_mode = STV090x_SEARCH_AUTO;
3421         state->algo = STV090x_COLD_SEARCH;
3422         state->fec = STV090x_PRERR;
3423         if (state->srate > 10000000) {
3424                 dprintk(FE_DEBUG, 1, "Search range: 10 MHz");
3425                 state->search_range = 10000000;
3426         } else {
3427                 dprintk(FE_DEBUG, 1, "Search range: 5 MHz");
3428                 state->search_range = 5000000;
3429         }
3430
3431         if (stv090x_algo(state) == STV090x_RANGEOK) {
3432                 dprintk(FE_DEBUG, 1, "Search success!");
3433                 return DVBFE_ALGO_SEARCH_SUCCESS;
3434         } else {
3435                 dprintk(FE_DEBUG, 1, "Search failed!");
3436                 return DVBFE_ALGO_SEARCH_FAILED;
3437         }
3438
3439         return DVBFE_ALGO_SEARCH_ERROR;
3440 }
3441
3442 static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
3443 {
3444         struct stv090x_state *state = fe->demodulator_priv;
3445         u32 reg;
3446         u8 search_state;
3447
3448         reg = STV090x_READ_DEMOD(state, DMDSTATE);
3449         search_state = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
3450
3451         switch (search_state) {
3452         case 0: /* searching */
3453         case 1: /* first PLH detected */
3454         default:
3455                 dprintk(FE_DEBUG, 1, "Status: Unlocked (Searching ..)");
3456                 *status = 0;
3457                 break;
3458
3459         case 2: /* DVB-S2 mode */
3460                 dprintk(FE_DEBUG, 1, "Delivery system: DVB-S2");
3461                 reg = STV090x_READ_DEMOD(state, DSTATUS);
3462                 if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
3463                         reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
3464                         if (STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD)) {
3465                                 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3466                                 if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
3467                                         *status = FE_HAS_SIGNAL |
3468                                                   FE_HAS_CARRIER |
3469                                                   FE_HAS_VITERBI |
3470                                                   FE_HAS_SYNC |
3471                                                   FE_HAS_LOCK;
3472                                 }
3473                         }
3474                 }
3475                 break;
3476
3477         case 3: /* DVB-S1/legacy mode */
3478                 dprintk(FE_DEBUG, 1, "Delivery system: DVB-S");
3479                 reg = STV090x_READ_DEMOD(state, DSTATUS);
3480                 if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
3481                         reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
3482                         if (STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD)) {
3483                                 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3484                                 if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
3485                                         *status = FE_HAS_SIGNAL |
3486                                                   FE_HAS_CARRIER |
3487                                                   FE_HAS_VITERBI |
3488                                                   FE_HAS_SYNC |
3489                                                   FE_HAS_LOCK;
3490                                 }
3491                         }
3492                 }
3493                 break;
3494         }
3495
3496         return 0;
3497 }
3498
3499 static int stv090x_read_per(struct dvb_frontend *fe, u32 *per)
3500 {
3501         struct stv090x_state *state = fe->demodulator_priv;
3502
3503         s32 count_4, count_3, count_2, count_1, count_0, count;
3504         u32 reg, h, m, l;
3505         enum fe_status status;
3506
3507         stv090x_read_status(fe, &status);
3508         if (!(status & FE_HAS_LOCK)) {
3509                 *per = 1 << 23; /* Max PER */
3510         } else {
3511                 /* Counter 2 */
3512                 reg = STV090x_READ_DEMOD(state, ERRCNT22);
3513                 h = STV090x_GETFIELD_Px(reg, ERR_CNT2_FIELD);
3514
3515                 reg = STV090x_READ_DEMOD(state, ERRCNT21);
3516                 m = STV090x_GETFIELD_Px(reg, ERR_CNT21_FIELD);
3517
3518                 reg = STV090x_READ_DEMOD(state, ERRCNT20);
3519                 l = STV090x_GETFIELD_Px(reg, ERR_CNT20_FIELD);
3520
3521                 *per = ((h << 16) | (m << 8) | l);
3522
3523                 count_4 = STV090x_READ_DEMOD(state, FBERCPT4);
3524                 count_3 = STV090x_READ_DEMOD(state, FBERCPT3);
3525                 count_2 = STV090x_READ_DEMOD(state, FBERCPT2);
3526                 count_1 = STV090x_READ_DEMOD(state, FBERCPT1);
3527                 count_0 = STV090x_READ_DEMOD(state, FBERCPT0);
3528
3529                 if ((!count_4) && (!count_3)) {
3530                         count  = (count_2 & 0xff) << 16;
3531                         count |= (count_1 & 0xff) <<  8;
3532                         count |=  count_0 & 0xff;
3533                 } else {
3534                         count = 1 << 24;
3535                 }
3536                 if (count == 0)
3537                         *per = 1;
3538         }
3539         if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0) < 0)
3540                 goto err;
3541         if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3542                 goto err;
3543
3544         return 0;
3545 err:
3546         dprintk(FE_ERROR, 1, "I/O error");
3547         return -1;
3548 }
3549
3550 static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
3551 {
3552         int res = 0;
3553         int min = 0, med;
3554
3555         if ((val >= tab[min].read && val < tab[max].read) ||
3556             (val >= tab[max].read && val < tab[min].read)) {
3557                 while ((max - min) > 1) {
3558                         med = (max + min) / 2;
3559                         if ((val >= tab[min].read && val < tab[med].read) ||
3560                             (val >= tab[med].read && val < tab[min].read))
3561                                 max = med;
3562                         else
3563                                 min = med;
3564                 }
3565                 res = ((val - tab[min].read) *
3566                        (tab[max].real - tab[min].real) /
3567                        (tab[max].read - tab[min].read)) +
3568                         tab[min].real;
3569         } else {
3570                 if (tab[min].read < tab[max].read) {
3571                         if (val < tab[min].read)
3572                                 res = tab[min].real;
3573                         else if (val >= tab[max].read)
3574                                 res = tab[max].real;
3575                 } else {
3576                         if (val >= tab[min].read)
3577                                 res = tab[min].real;
3578                         else if (val < tab[max].read)
3579                                 res = tab[max].real;
3580                 }
3581         }
3582
3583         return res;
3584 }
3585
3586 static int stv090x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
3587 {
3588         struct stv090x_state *state = fe->demodulator_priv;
3589         u32 reg;
3590         s32 agc_0, agc_1, agc;
3591         s32 str;
3592
3593         reg = STV090x_READ_DEMOD(state, AGCIQIN1);
3594         agc_1 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
3595         reg = STV090x_READ_DEMOD(state, AGCIQIN0);
3596         agc_0 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
3597         agc = MAKEWORD16(agc_1, agc_0);
3598
3599         str = stv090x_table_lookup(stv090x_rf_tab,
3600                 ARRAY_SIZE(stv090x_rf_tab) - 1, agc);
3601         if (agc > stv090x_rf_tab[0].read)
3602                 str = 0;
3603         else if (agc < stv090x_rf_tab[ARRAY_SIZE(stv090x_rf_tab) - 1].read)
3604                 str = -100;
3605         *strength = (str + 100) * 0xFFFF / 100;
3606
3607         return 0;
3608 }
3609
3610 static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
3611 {
3612         struct stv090x_state *state = fe->demodulator_priv;
3613         u32 reg_0, reg_1, reg, i;
3614         s32 val_0, val_1, val = 0;
3615         u8 lock_f;
3616         s32 div;
3617         u32 last;
3618
3619         switch (state->delsys) {
3620         case STV090x_DVBS2:
3621                 reg = STV090x_READ_DEMOD(state, DSTATUS);
3622                 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
3623                 if (lock_f) {
3624                         msleep(5);
3625                         for (i = 0; i < 16; i++) {
3626                                 reg_1 = STV090x_READ_DEMOD(state, NNOSPLHT1);
3627                                 val_1 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
3628                                 reg_0 = STV090x_READ_DEMOD(state, NNOSPLHT0);
3629                                 val_0 = STV090x_GETFIELD_Px(reg_0, NOSPLHT_NORMED_FIELD);
3630                                 val  += MAKEWORD16(val_1, val_0);
3631                                 msleep(1);
3632                         }
3633                         val /= 16;
3634                         last = ARRAY_SIZE(stv090x_s2cn_tab) - 1;
3635                         div = stv090x_s2cn_tab[0].read -
3636                               stv090x_s2cn_tab[last].read;
3637                         *cnr = 0xFFFF - ((val * 0xFFFF) / div);
3638                 }
3639                 break;
3640
3641         case STV090x_DVBS1:
3642         case STV090x_DSS:
3643                 reg = STV090x_READ_DEMOD(state, DSTATUS);
3644                 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
3645                 if (lock_f) {
3646                         msleep(5);
3647                         for (i = 0; i < 16; i++) {
3648                                 reg_1 = STV090x_READ_DEMOD(state, NOSDATAT1);
3649                                 val_1 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
3650                                 reg_0 = STV090x_READ_DEMOD(state, NOSDATAT0);
3651                                 val_0 = STV090x_GETFIELD_Px(reg_0, NOSDATAT_UNNORMED_FIELD);
3652                                 val  += MAKEWORD16(val_1, val_0);
3653                                 msleep(1);
3654                         }
3655                         val /= 16;
3656                         last = ARRAY_SIZE(stv090x_s1cn_tab) - 1;
3657                         div = stv090x_s1cn_tab[0].read -
3658                               stv090x_s1cn_tab[last].read;
3659                         *cnr = 0xFFFF - ((val * 0xFFFF) / div);
3660                 }
3661                 break;
3662         default:
3663                 break;
3664         }
3665
3666         return 0;
3667 }
3668
3669 static int stv090x_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
3670 {
3671         struct stv090x_state *state = fe->demodulator_priv;
3672         u32 reg;
3673
3674         reg = STV090x_READ_DEMOD(state, DISTXCTL);
3675         switch (tone) {
3676         case SEC_TONE_ON:
3677                 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
3678                 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3679                 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3680                         goto err;
3681                 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3682                 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3683                         goto err;
3684                 break;
3685
3686         case SEC_TONE_OFF:
3687                 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
3688                 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3689                 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3690                         goto err;
3691                 break;
3692         default:
3693                 return -EINVAL;
3694         }
3695
3696         return 0;
3697 err:
3698         dprintk(FE_ERROR, 1, "I/O error");
3699         return -1;
3700 }
3701
3702
3703 static enum dvbfe_algo stv090x_frontend_algo(struct dvb_frontend *fe)
3704 {
3705         return DVBFE_ALGO_CUSTOM;
3706 }
3707
3708 static int stv090x_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
3709 {
3710         struct stv090x_state *state = fe->demodulator_priv;
3711         u32 reg, idle = 0, fifo_full = 1;
3712         int i;
3713
3714         reg = STV090x_READ_DEMOD(state, DISTXCTL);
3715
3716         STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD,
3717                 (state->config->diseqc_envelope_mode) ? 4 : 2);
3718         STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3719         if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3720                 goto err;
3721         STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3722         if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3723                 goto err;
3724
3725         STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3726         if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3727                 goto err;
3728
3729         for (i = 0; i < cmd->msg_len; i++) {
3730
3731                 while (fifo_full) {
3732                         reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3733                         fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
3734                 }
3735
3736                 if (STV090x_WRITE_DEMOD(state, DISTXDATA, cmd->msg[i]) < 0)
3737                         goto err;
3738         }
3739         reg = STV090x_READ_DEMOD(state, DISTXCTL);
3740         STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
3741         if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3742                 goto err;
3743
3744         i = 0;
3745
3746         while ((!idle) && (i < 10)) {
3747                 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3748                 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3749                 msleep(10);
3750                 i++;
3751         }
3752
3753         return 0;
3754 err:
3755         dprintk(FE_ERROR, 1, "I/O error");
3756         return -1;
3757 }
3758
3759 static int stv090x_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
3760 {
3761         struct stv090x_state *state = fe->demodulator_priv;
3762         u32 reg, idle = 0, fifo_full = 1;
3763         u8 mode, value;
3764         int i;
3765
3766         reg = STV090x_READ_DEMOD(state, DISTXCTL);
3767
3768         if (burst == SEC_MINI_A) {
3769                 mode = (state->config->diseqc_envelope_mode) ? 5 : 3;
3770                 value = 0x00;
3771         } else {
3772                 mode = (state->config->diseqc_envelope_mode) ? 4 : 2;
3773                 value = 0xFF;
3774         }
3775
3776         STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, mode);
3777         STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3778         if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3779                 goto err;
3780         STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3781         if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3782                 goto err;
3783
3784         STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3785         if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3786                 goto err;
3787
3788         while (fifo_full) {
3789                 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3790                 fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
3791         }
3792
3793         if (STV090x_WRITE_DEMOD(state, DISTXDATA, value) < 0)
3794                 goto err;
3795
3796         reg = STV090x_READ_DEMOD(state, DISTXCTL);
3797         STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
3798         if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3799                 goto err;
3800
3801         i = 0;
3802
3803         while ((!idle) && (i < 10)) {
3804                 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3805                 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3806                 msleep(10);
3807                 i++;
3808         }
3809
3810         return 0;
3811 err:
3812         dprintk(FE_ERROR, 1, "I/O error");
3813         return -1;
3814 }
3815
3816 static int stv090x_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
3817 {
3818         struct stv090x_state *state = fe->demodulator_priv;
3819         u32 reg = 0, i = 0, rx_end = 0;
3820
3821         while ((rx_end != 1) && (i < 10)) {
3822                 msleep(10);
3823                 i++;
3824                 reg = STV090x_READ_DEMOD(state, DISRX_ST0);
3825                 rx_end = STV090x_GETFIELD_Px(reg, RX_END_FIELD);
3826         }
3827
3828         if (rx_end) {
3829                 reply->msg_len = STV090x_GETFIELD_Px(reg, FIFO_BYTENBR_FIELD);
3830                 for (i = 0; i < reply->msg_len; i++)
3831                         reply->msg[i] = STV090x_READ_DEMOD(state, DISRXDATA);
3832         }
3833
3834         return 0;
3835 }
3836
3837 static int stv090x_sleep(struct dvb_frontend *fe)
3838 {
3839         struct stv090x_state *state = fe->demodulator_priv;
3840         u32 reg;
3841
3842         dprintk(FE_DEBUG, 1, "Set %s to sleep",
3843                 state->device == STV0900 ? "STV0900" : "STV0903");
3844
3845         reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3846         STV090x_SETFIELD(reg, STANDBY_FIELD, 0x01);
3847         if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
3848                 goto err;
3849
3850         reg = stv090x_read_reg(state, STV090x_TSTTNR1);
3851         STV090x_SETFIELD(reg, ADC1_PON_FIELD, 0);
3852         if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
3853                 goto err;
3854
3855         return 0;
3856 err:
3857         dprintk(FE_ERROR, 1, "I/O error");
3858         return -1;
3859 }
3860
3861 static int stv090x_wakeup(struct dvb_frontend *fe)
3862 {
3863         struct stv090x_state *state = fe->demodulator_priv;
3864         u32 reg;
3865
3866         dprintk(FE_DEBUG, 1, "Wake %s from standby",
3867                 state->device == STV0900 ? "STV0900" : "STV0903");
3868
3869         reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3870         STV090x_SETFIELD(reg, STANDBY_FIELD, 0x00);
3871         if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
3872                 goto err;
3873
3874         reg = stv090x_read_reg(state, STV090x_TSTTNR1);
3875         STV090x_SETFIELD(reg, ADC1_PON_FIELD, 1);
3876         if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
3877                 goto err;
3878
3879         return 0;
3880 err:
3881         dprintk(FE_ERROR, 1, "I/O error");
3882         return -1;
3883 }
3884
3885 static void stv090x_release(struct dvb_frontend *fe)
3886 {
3887         struct stv090x_state *state = fe->demodulator_priv;
3888
3889         state->internal->num_used--;
3890         if (state->internal->num_used <= 0) {
3891
3892                 dprintk(FE_ERROR, 1, "Actually removing");
3893
3894                 remove_dev(state->internal);
3895                 kfree(state->internal);
3896         }
3897
3898         kfree(state);
3899 }
3900
3901 static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc_mode)
3902 {
3903         u32 reg = 0;
3904
3905         reg = stv090x_read_reg(state, STV090x_GENCFG);
3906
3907         switch (ldpc_mode) {
3908         case STV090x_DUAL:
3909         default:
3910                 if ((state->demod_mode != STV090x_DUAL) || (STV090x_GETFIELD(reg, DDEMOD_FIELD) != 1)) {
3911                         /* set LDPC to dual mode */
3912                         if (stv090x_write_reg(state, STV090x_GENCFG, 0x1d) < 0)
3913                                 goto err;
3914
3915                         state->demod_mode = STV090x_DUAL;
3916
3917                         reg = stv090x_read_reg(state, STV090x_TSTRES0);
3918                         STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
3919                         if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3920                                 goto err;
3921                         STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
3922                         if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3923                                 goto err;
3924
3925                         if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
3926                                 goto err;
3927                         if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
3928                                 goto err;
3929                         if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
3930                                 goto err;
3931                         if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
3932                                 goto err;
3933                         if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
3934                                 goto err;
3935                         if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
3936                                 goto err;
3937                         if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
3938                                 goto err;
3939
3940                         if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
3941                                 goto err;
3942                         if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
3943                                 goto err;
3944                         if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
3945                                 goto err;
3946                         if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
3947                                 goto err;
3948                         if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
3949                                 goto err;
3950                         if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
3951                                 goto err;
3952                         if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
3953                                 goto err;
3954
3955                         if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
3956                                 goto err;
3957                         if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
3958                                 goto err;
3959                 }
3960                 break;
3961
3962         case STV090x_SINGLE:
3963                 if (stv090x_stop_modcod(state) < 0)
3964                         goto err;
3965                 if (stv090x_activate_modcod_single(state) < 0)
3966                         goto err;
3967
3968                 if (state->demod == STV090x_DEMODULATOR_1) {
3969                         if (stv090x_write_reg(state, STV090x_GENCFG, 0x06) < 0) /* path 2 */
3970                                 goto err;
3971                 } else {
3972                         if (stv090x_write_reg(state, STV090x_GENCFG, 0x04) < 0) /* path 1 */
3973                                 goto err;
3974                 }
3975
3976                 reg = stv090x_read_reg(state, STV090x_TSTRES0);
3977                 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
3978                 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3979                         goto err;
3980                 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
3981                 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3982                         goto err;
3983
3984                 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
3985                 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x01);
3986                 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
3987                         goto err;
3988                 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x00);
3989                 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
3990                         goto err;
3991                 break;
3992         }
3993
3994         return 0;
3995 err:
3996         dprintk(FE_ERROR, 1, "I/O error");
3997         return -1;
3998 }
3999
4000 /* return (Hz), clk in Hz*/
4001 static u32 stv090x_get_mclk(struct stv090x_state *state)
4002 {
4003         const struct stv090x_config *config = state->config;
4004         u32 div, reg;
4005         u8 ratio;
4006
4007         div = stv090x_read_reg(state, STV090x_NCOARSE);
4008         reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
4009         ratio = STV090x_GETFIELD(reg, SELX1RATIO_FIELD) ? 4 : 6;
4010
4011         return (div + 1) * config->xtal / ratio; /* kHz */
4012 }
4013
4014 static int stv090x_set_mclk(struct stv090x_state *state, u32 mclk, u32 clk)
4015 {
4016         const struct stv090x_config *config = state->config;
4017         u32 reg, div, clk_sel;
4018
4019         reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
4020         clk_sel = ((STV090x_GETFIELD(reg, SELX1RATIO_FIELD) == 1) ? 4 : 6);
4021
4022         div = ((clk_sel * mclk) / config->xtal) - 1;
4023
4024         reg = stv090x_read_reg(state, STV090x_NCOARSE);
4025         STV090x_SETFIELD(reg, M_DIV_FIELD, div);
4026         if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0)
4027                 goto err;
4028
4029         state->internal->mclk = stv090x_get_mclk(state);
4030
4031         /*Set the DiseqC frequency to 22KHz */
4032         div = state->internal->mclk / 704000;
4033         if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0)
4034                 goto err;
4035         if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
4036                 goto err;
4037
4038         return 0;
4039 err:
4040         dprintk(FE_ERROR, 1, "I/O error");
4041         return -1;
4042 }
4043
4044 static int stv090x_set_tspath(struct stv090x_state *state)
4045 {
4046         u32 reg;
4047
4048         if (state->internal->dev_ver >= 0x20) {
4049                 switch (state->config->ts1_mode) {
4050                 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4051                 case STV090x_TSMODE_DVBCI:
4052                         switch (state->config->ts2_mode) {
4053                         case STV090x_TSMODE_SERIAL_PUNCTURED:
4054                         case STV090x_TSMODE_SERIAL_CONTINUOUS:
4055                         default:
4056                                 stv090x_write_reg(state, STV090x_TSGENERAL, 0x00);
4057                                 break;
4058
4059                         case STV090x_TSMODE_PARALLEL_PUNCTURED:
4060                         case STV090x_TSMODE_DVBCI:
4061                                 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x06) < 0) /* Mux'd stream mode */
4062                                         goto err;
4063                                 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4064                                 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4065                                 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4066                                         goto err;
4067                                 reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
4068                                 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4069                                 if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
4070                                         goto err;
4071                                 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
4072                                         goto err;
4073                                 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
4074                                         goto err;
4075                                 break;
4076                         }
4077                         break;
4078
4079                 case STV090x_TSMODE_SERIAL_PUNCTURED:
4080                 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4081                 default:
4082                         switch (state->config->ts2_mode) {
4083                         case STV090x_TSMODE_SERIAL_PUNCTURED:
4084                         case STV090x_TSMODE_SERIAL_CONTINUOUS:
4085                         default:
4086                                 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
4087                                         goto err;
4088                                 break;
4089
4090                         case STV090x_TSMODE_PARALLEL_PUNCTURED:
4091                         case STV090x_TSMODE_DVBCI:
4092                                 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0a) < 0)
4093                                         goto err;
4094                                 break;
4095                         }
4096                         break;
4097                 }
4098         } else {
4099                 switch (state->config->ts1_mode) {
4100                 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4101                 case STV090x_TSMODE_DVBCI:
4102                         switch (state->config->ts2_mode) {
4103                         case STV090x_TSMODE_SERIAL_PUNCTURED:
4104                         case STV090x_TSMODE_SERIAL_CONTINUOUS:
4105                         default:
4106                                 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x10);
4107                                 break;
4108
4109                         case STV090x_TSMODE_PARALLEL_PUNCTURED:
4110                         case STV090x_TSMODE_DVBCI:
4111                                 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x16);
4112                                 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4113                                 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4114                                 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4115                                         goto err;
4116                                 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4117                                 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 0);
4118                                 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4119                                         goto err;
4120                                 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
4121                                         goto err;
4122                                 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
4123                                         goto err;
4124                                 break;
4125                         }
4126                         break;
4127
4128                 case STV090x_TSMODE_SERIAL_PUNCTURED:
4129                 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4130                 default:
4131                         switch (state->config->ts2_mode) {
4132                         case STV090x_TSMODE_SERIAL_PUNCTURED:
4133                         case STV090x_TSMODE_SERIAL_CONTINUOUS:
4134                         default:
4135                                 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
4136                                 break;
4137
4138                         case STV090x_TSMODE_PARALLEL_PUNCTURED:
4139                         case STV090x_TSMODE_DVBCI:
4140                                 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x12);
4141                                 break;
4142                         }
4143                         break;
4144                 }
4145         }
4146
4147         switch (state->config->ts1_mode) {
4148         case STV090x_TSMODE_PARALLEL_PUNCTURED:
4149                 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4150                 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4151                 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4152                 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4153                         goto err;
4154                 break;
4155
4156         case STV090x_TSMODE_DVBCI:
4157                 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4158                 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4159                 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4160                 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4161                         goto err;
4162                 break;
4163
4164         case STV090x_TSMODE_SERIAL_PUNCTURED:
4165                 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4166                 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4167                 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4168                 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4169                         goto err;
4170                 break;
4171
4172         case STV090x_TSMODE_SERIAL_CONTINUOUS:
4173                 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4174                 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4175                 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4176                 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4177                         goto err;
4178                 break;
4179
4180         default:
4181                 break;
4182         }
4183
4184         switch (state->config->ts2_mode) {
4185         case STV090x_TSMODE_PARALLEL_PUNCTURED:
4186                 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4187                 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4188                 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4189                 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4190                         goto err;
4191                 break;
4192
4193         case STV090x_TSMODE_DVBCI:
4194                 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4195                 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4196                 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4197                 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4198                         goto err;
4199                 break;
4200
4201         case STV090x_TSMODE_SERIAL_PUNCTURED:
4202                 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4203                 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4204                 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4205                 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4206                         goto err;
4207                 break;
4208
4209         case STV090x_TSMODE_SERIAL_CONTINUOUS:
4210                 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4211                 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4212                 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4213                 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4214                         goto err;
4215                 break;
4216
4217         default:
4218                 break;
4219         }
4220
4221         if (state->config->ts1_clk > 0) {
4222                 u32 speed;
4223
4224                 switch (state->config->ts1_mode) {
4225                 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4226                 case STV090x_TSMODE_DVBCI:
4227                 default:
4228                         speed = state->internal->mclk /
4229                                 (state->config->ts1_clk / 4);
4230                         if (speed < 0x08)
4231                                 speed = 0x08;
4232                         if (speed > 0xFF)
4233                                 speed = 0xFF;
4234                         break;
4235                 case STV090x_TSMODE_SERIAL_PUNCTURED:
4236                 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4237                         speed = state->internal->mclk /
4238                                 (state->config->ts1_clk / 32);
4239                         if (speed < 0x20)
4240                                 speed = 0x20;
4241                         if (speed > 0xFF)
4242                                 speed = 0xFF;
4243                         break;
4244                 }
4245                 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4246                 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4247                 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4248                         goto err;
4249                 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, speed) < 0)
4250                         goto err;
4251         }
4252
4253         if (state->config->ts2_clk > 0) {
4254                 u32 speed;
4255
4256                 switch (state->config->ts2_mode) {
4257                 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4258                 case STV090x_TSMODE_DVBCI:
4259                 default:
4260                         speed = state->internal->mclk /
4261                                 (state->config->ts2_clk / 4);
4262                         if (speed < 0x08)
4263                                 speed = 0x08;
4264                         if (speed > 0xFF)
4265                                 speed = 0xFF;
4266                         break;
4267                 case STV090x_TSMODE_SERIAL_PUNCTURED:
4268                 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4269                         speed = state->internal->mclk /
4270                                 (state->config->ts2_clk / 32);
4271                         if (speed < 0x20)
4272                                 speed = 0x20;
4273                         if (speed > 0xFF)
4274                                 speed = 0xFF;
4275                         break;
4276                 }
4277                 reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
4278                 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4279                 if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
4280                         goto err;
4281                 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, speed) < 0)
4282                         goto err;
4283         }
4284
4285         reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4286         STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
4287         if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4288                 goto err;
4289         STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4290         if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4291                 goto err;
4292
4293         reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4294         STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
4295         if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4296                 goto err;
4297         STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4298         if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4299                 goto err;
4300
4301         return 0;
4302 err:
4303         dprintk(FE_ERROR, 1, "I/O error");
4304         return -1;
4305 }
4306
4307 static int stv090x_init(struct dvb_frontend *fe)
4308 {
4309         struct stv090x_state *state = fe->demodulator_priv;
4310         const struct stv090x_config *config = state->config;
4311         u32 reg;
4312
4313         if (state->internal->mclk == 0) {
4314                 stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
4315                 msleep(5);
4316                 if (stv090x_write_reg(state, STV090x_SYNTCTRL,
4317                                       0x20 | config->clk_mode) < 0)
4318                         goto err;
4319                 stv090x_get_mclk(state);
4320         }
4321
4322         if (stv090x_wakeup(fe) < 0) {
4323                 dprintk(FE_ERROR, 1, "Error waking device");
4324                 goto err;
4325         }
4326
4327         if (stv090x_ldpc_mode(state, state->demod_mode) < 0)
4328                 goto err;
4329
4330         reg = STV090x_READ_DEMOD(state, TNRCFG2);
4331         STV090x_SETFIELD_Px(reg, TUN_IQSWAP_FIELD, state->inversion);
4332         if (STV090x_WRITE_DEMOD(state, TNRCFG2, reg) < 0)
4333                 goto err;
4334         reg = STV090x_READ_DEMOD(state, DEMOD);
4335         STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
4336         if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
4337                 goto err;
4338
4339         if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
4340                 goto err;
4341
4342         if (config->tuner_set_mode) {
4343                 if (config->tuner_set_mode(fe, TUNER_WAKE) < 0)
4344                         goto err_gateoff;
4345         }
4346
4347         if (config->tuner_init) {
4348                 if (config->tuner_init(fe) < 0)
4349                         goto err_gateoff;
4350         }
4351
4352         if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
4353                 goto err;
4354
4355         if (stv090x_set_tspath(state) < 0)
4356                 goto err;
4357
4358         return 0;
4359
4360 err_gateoff:
4361         stv090x_i2c_gate_ctrl(fe, 0);
4362 err:
4363         dprintk(FE_ERROR, 1, "I/O error");
4364         return -1;
4365 }
4366
4367 static int stv090x_setup(struct dvb_frontend *fe)
4368 {
4369         struct stv090x_state *state = fe->demodulator_priv;
4370         const struct stv090x_config *config = state->config;
4371         const struct stv090x_reg *stv090x_initval = NULL;
4372         const struct stv090x_reg *stv090x_cut20_val = NULL;
4373         unsigned long t1_size = 0, t2_size = 0;
4374         u32 reg = 0;
4375
4376         int i;
4377
4378         if (state->device == STV0900) {
4379                 dprintk(FE_DEBUG, 1, "Initializing STV0900");
4380                 stv090x_initval = stv0900_initval;
4381                 t1_size = ARRAY_SIZE(stv0900_initval);
4382                 stv090x_cut20_val = stv0900_cut20_val;
4383                 t2_size = ARRAY_SIZE(stv0900_cut20_val);
4384         } else if (state->device == STV0903) {
4385                 dprintk(FE_DEBUG, 1, "Initializing STV0903");
4386                 stv090x_initval = stv0903_initval;
4387                 t1_size = ARRAY_SIZE(stv0903_initval);
4388                 stv090x_cut20_val = stv0903_cut20_val;
4389                 t2_size = ARRAY_SIZE(stv0903_cut20_val);
4390         }
4391
4392         /* STV090x init */
4393
4394         /* Stop Demod */
4395         if (stv090x_write_reg(state, STV090x_P1_DMDISTATE, 0x5c) < 0)
4396                 goto err;
4397         if (stv090x_write_reg(state, STV090x_P2_DMDISTATE, 0x5c) < 0)
4398                 goto err;
4399
4400         msleep(5);
4401
4402         /* Set No Tuner Mode */
4403         if (stv090x_write_reg(state, STV090x_P1_TNRCFG, 0x6c) < 0)
4404                 goto err;
4405         if (stv090x_write_reg(state, STV090x_P2_TNRCFG, 0x6c) < 0)
4406                 goto err;
4407
4408         /* I2C repeater OFF */
4409         STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level);
4410         if (stv090x_write_reg(state, STV090x_P1_I2CRPT, reg) < 0)
4411                 goto err;
4412         if (stv090x_write_reg(state, STV090x_P2_I2CRPT, reg) < 0)
4413                 goto err;
4414
4415         if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
4416                 goto err;
4417         msleep(5);
4418         if (stv090x_write_reg(state, STV090x_I2CCFG, 0x08) < 0) /* 1/41 oversampling */
4419                 goto err;
4420         if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0) /* enable PLL */
4421                 goto err;
4422         msleep(5);
4423
4424         /* write initval */
4425         dprintk(FE_DEBUG, 1, "Setting up initial values");
4426         for (i = 0; i < t1_size; i++) {
4427                 if (stv090x_write_reg(state, stv090x_initval[i].addr, stv090x_initval[i].data) < 0)
4428                         goto err;
4429         }
4430
4431         state->internal->dev_ver = stv090x_read_reg(state, STV090x_MID);
4432         if (state->internal->dev_ver >= 0x20) {
4433                 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
4434                         goto err;
4435
4436                 /* write cut20_val*/
4437                 dprintk(FE_DEBUG, 1, "Setting up Cut 2.0 initial values");
4438                 for (i = 0; i < t2_size; i++) {
4439                         if (stv090x_write_reg(state, stv090x_cut20_val[i].addr, stv090x_cut20_val[i].data) < 0)
4440                                 goto err;
4441                 }
4442
4443         } else if (state->internal->dev_ver < 0x20) {
4444                 dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!",
4445                         state->internal->dev_ver);
4446
4447                 goto err;
4448         } else if (state->internal->dev_ver > 0x30) {
4449                 /* we shouldn't bail out from here */
4450                 dprintk(FE_ERROR, 1, "INFO: Cut: 0x%02x probably incomplete support!",
4451                         state->internal->dev_ver);
4452         }
4453
4454         /* ADC1 range */
4455         reg = stv090x_read_reg(state, STV090x_TSTTNR1);
4456         STV090x_SETFIELD(reg, ADC1_INMODE_FIELD,
4457                 (config->adc1_range == STV090x_ADC_1Vpp) ? 0 : 1);
4458         if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
4459                 goto err;
4460
4461         /* ADC2 range */
4462         reg = stv090x_read_reg(state, STV090x_TSTTNR3);
4463         STV090x_SETFIELD(reg, ADC2_INMODE_FIELD,
4464                 (config->adc2_range == STV090x_ADC_1Vpp) ? 0 : 1);
4465         if (stv090x_write_reg(state, STV090x_TSTTNR3, reg) < 0)
4466                 goto err;
4467
4468         if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
4469                 goto err;
4470         if (stv090x_write_reg(state, STV090x_TSTRES0, 0x00) < 0)
4471                 goto err;
4472
4473         /* workaround for stuck DiSEqC output */
4474         if (config->diseqc_envelope_mode)
4475                 stv090x_send_diseqc_burst(fe, SEC_MINI_A);
4476
4477         return 0;
4478 err:
4479         dprintk(FE_ERROR, 1, "I/O error");
4480         return -1;
4481 }
4482
4483 static struct dvb_frontend_ops stv090x_ops = {
4484
4485         .info = {
4486                 .name                   = "STV090x Multistandard",
4487                 .type                   = FE_QPSK,
4488                 .frequency_min          = 950000,
4489                 .frequency_max          = 2150000,
4490                 .frequency_stepsize     = 0,
4491                 .frequency_tolerance    = 0,
4492                 .symbol_rate_min        = 1000000,
4493                 .symbol_rate_max        = 45000000,
4494                 .caps                   = FE_CAN_INVERSION_AUTO |
4495                                           FE_CAN_FEC_AUTO       |
4496                                           FE_CAN_QPSK           |
4497                                           FE_CAN_2G_MODULATION
4498         },
4499
4500         .release                        = stv090x_release,
4501         .init                           = stv090x_init,
4502
4503         .sleep                          = stv090x_sleep,
4504         .get_frontend_algo              = stv090x_frontend_algo,
4505
4506         .i2c_gate_ctrl                  = stv090x_i2c_gate_ctrl,
4507
4508         .diseqc_send_master_cmd         = stv090x_send_diseqc_msg,
4509         .diseqc_send_burst              = stv090x_send_diseqc_burst,
4510         .diseqc_recv_slave_reply        = stv090x_recv_slave_reply,
4511         .set_tone                       = stv090x_set_tone,
4512
4513         .search                         = stv090x_search,
4514         .read_status                    = stv090x_read_status,
4515         .read_ber                       = stv090x_read_per,
4516         .read_signal_strength           = stv090x_read_signal_strength,
4517         .read_snr                       = stv090x_read_cnr
4518 };
4519
4520
4521 struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
4522                                     struct i2c_adapter *i2c,
4523                                     enum stv090x_demodulator demod)
4524 {
4525         struct stv090x_state *state = NULL;
4526         struct stv090x_dev *temp_int;
4527
4528         state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL);
4529         if (state == NULL)
4530                 goto error;
4531
4532         state->verbose                          = &verbose;
4533         state->config                           = config;
4534         state->i2c                              = i2c;
4535         state->frontend.ops                     = stv090x_ops;
4536         state->frontend.demodulator_priv        = state;
4537         state->demod                            = demod;
4538         state->demod_mode                       = config->demod_mode; /* Single or Dual mode */
4539         state->device                           = config->device;
4540         state->rolloff                          = STV090x_RO_35; /* default */
4541
4542         temp_int = find_dev(state->i2c,
4543                                 state->config->address);
4544
4545         if ((temp_int != NULL) && (state->demod_mode == STV090x_DUAL)) {
4546                 state->internal = temp_int->internal;
4547                 state->internal->num_used++;
4548                 dprintk(FE_INFO, 1, "Found Internal Structure!");
4549                 dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
4550                         state->device == STV0900 ? "STV0900" : "STV0903",
4551                         demod,
4552                         state->internal->dev_ver);
4553                 return &state->frontend;
4554         } else {
4555                 state->internal = kmalloc(sizeof(struct stv090x_internal),
4556                                           GFP_KERNEL);
4557                 temp_int = append_internal(state->internal);
4558                 state->internal->num_used = 1;
4559                 state->internal->mclk = 0;
4560                 state->internal->dev_ver = 0;
4561                 state->internal->i2c_adap = state->i2c;
4562                 state->internal->i2c_addr = state->config->address;
4563                 dprintk(FE_INFO, 1, "Create New Internal Structure!");
4564         }
4565
4566         mutex_init(&state->internal->demod_lock);
4567         mutex_init(&state->internal->tuner_lock);
4568
4569         if (stv090x_sleep(&state->frontend) < 0) {
4570                 dprintk(FE_ERROR, 1, "Error putting device to sleep");
4571                 goto error;
4572         }
4573
4574         if (stv090x_setup(&state->frontend) < 0) {
4575                 dprintk(FE_ERROR, 1, "Error setting up device");
4576                 goto error;
4577         }
4578         if (stv090x_wakeup(&state->frontend) < 0) {
4579                 dprintk(FE_ERROR, 1, "Error waking device");
4580                 goto error;
4581         }
4582
4583         dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
4584                state->device == STV0900 ? "STV0900" : "STV0903",
4585                demod,
4586                state->internal->dev_ver);
4587
4588         return &state->frontend;
4589
4590 error:
4591         kfree(state);
4592         return NULL;
4593 }
4594 EXPORT_SYMBOL(stv090x_attach);
4595 MODULE_PARM_DESC(verbose, "Set Verbosity level");
4596 MODULE_AUTHOR("Manu Abraham");
4597 MODULE_DESCRIPTION("STV090x Multi-Std Broadcast frontend");
4598 MODULE_LICENSE("GPL");