blob: e903334a8323c672503d635d7aa3e4d8778e98cc [file] [log] [blame]
Manu Abrahame415c682009-04-06 15:45:20 -03001/*
2 STV0900/0903 Multistandard Broadcast Frontend driver
3 Copyright (C) Manu Abraham <abraham.manu@gmail.com>
4
5 Copyright (C) ST Microelectronics
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/string.h>
26#include <linux/mutex.h>
27
28#include <linux/dvb/frontend.h>
29#include "dvb_frontend.h"
30
31#include "stv6110x.h" /* for demodulator internal modes */
32
33#include "stv090x_reg.h"
34#include "stv090x.h"
35#include "stv090x_priv.h"
36
37static unsigned int verbose;
38module_param(verbose, int, 0644);
39
Andreas Regel97f7a2a2010-01-05 19:19:43 -030040/* internal params node */
41struct stv090x_dev {
42 /* pointer for internal params, one for each pair of demods */
43 struct stv090x_internal *internal;
44 struct stv090x_dev *next_dev;
45};
46
47/* first internal params */
48static struct stv090x_dev *stv090x_first_dev;
49
50/* find chip by i2c adapter and i2c address */
51static struct stv090x_dev *find_dev(struct i2c_adapter *i2c_adap,
52 u8 i2c_addr)
53{
54 struct stv090x_dev *temp_dev = stv090x_first_dev;
55
56 /*
57 Search of the last stv0900 chip or
58 find it by i2c adapter and i2c address */
59 while ((temp_dev != NULL) &&
60 ((temp_dev->internal->i2c_adap != i2c_adap) ||
61 (temp_dev->internal->i2c_addr != i2c_addr))) {
62
63 temp_dev = temp_dev->next_dev;
64 }
65
66 return temp_dev;
67}
68
69/* deallocating chip */
70static void remove_dev(struct stv090x_internal *internal)
71{
72 struct stv090x_dev *prev_dev = stv090x_first_dev;
73 struct stv090x_dev *del_dev = find_dev(internal->i2c_adap,
74 internal->i2c_addr);
75
76 if (del_dev != NULL) {
77 if (del_dev == stv090x_first_dev) {
78 stv090x_first_dev = del_dev->next_dev;
79 } else {
80 while (prev_dev->next_dev != del_dev)
81 prev_dev = prev_dev->next_dev;
82
83 prev_dev->next_dev = del_dev->next_dev;
84 }
85
86 kfree(del_dev);
87 }
88}
89
90/* allocating new chip */
91static struct stv090x_dev *append_internal(struct stv090x_internal *internal)
92{
93 struct stv090x_dev *new_dev;
94 struct stv090x_dev *temp_dev;
95
96 new_dev = kmalloc(sizeof(struct stv090x_dev), GFP_KERNEL);
97 if (new_dev != NULL) {
98 new_dev->internal = internal;
99 new_dev->next_dev = NULL;
100
101 /* append to list */
102 if (stv090x_first_dev == NULL) {
103 stv090x_first_dev = new_dev;
104 } else {
105 temp_dev = stv090x_first_dev;
106 while (temp_dev->next_dev != NULL)
107 temp_dev = temp_dev->next_dev;
108
109 temp_dev->next_dev = new_dev;
110 }
111 }
112
113 return new_dev;
114}
115
Manu Abrahame415c682009-04-06 15:45:20 -0300116
117/* DVBS1 and DSS C/N Lookup table */
118static const struct stv090x_tab stv090x_s1cn_tab[] = {
119 { 0, 8917 }, /* 0.0dB */
120 { 5, 8801 }, /* 0.5dB */
121 { 10, 8667 }, /* 1.0dB */
122 { 15, 8522 }, /* 1.5dB */
123 { 20, 8355 }, /* 2.0dB */
124 { 25, 8175 }, /* 2.5dB */
125 { 30, 7979 }, /* 3.0dB */
126 { 35, 7763 }, /* 3.5dB */
127 { 40, 7530 }, /* 4.0dB */
128 { 45, 7282 }, /* 4.5dB */
129 { 50, 7026 }, /* 5.0dB */
130 { 55, 6781 }, /* 5.5dB */
131 { 60, 6514 }, /* 6.0dB */
132 { 65, 6241 }, /* 6.5dB */
133 { 70, 5965 }, /* 7.0dB */
134 { 75, 5690 }, /* 7.5dB */
135 { 80, 5424 }, /* 8.0dB */
136 { 85, 5161 }, /* 8.5dB */
137 { 90, 4902 }, /* 9.0dB */
138 { 95, 4654 }, /* 9.5dB */
139 { 100, 4417 }, /* 10.0dB */
140 { 105, 4186 }, /* 10.5dB */
141 { 110, 3968 }, /* 11.0dB */
142 { 115, 3757 }, /* 11.5dB */
143 { 120, 3558 }, /* 12.0dB */
144 { 125, 3366 }, /* 12.5dB */
145 { 130, 3185 }, /* 13.0dB */
146 { 135, 3012 }, /* 13.5dB */
147 { 140, 2850 }, /* 14.0dB */
148 { 145, 2698 }, /* 14.5dB */
149 { 150, 2550 }, /* 15.0dB */
150 { 160, 2283 }, /* 16.0dB */
151 { 170, 2042 }, /* 17.0dB */
152 { 180, 1827 }, /* 18.0dB */
153 { 190, 1636 }, /* 19.0dB */
154 { 200, 1466 }, /* 20.0dB */
155 { 210, 1315 }, /* 21.0dB */
156 { 220, 1181 }, /* 22.0dB */
157 { 230, 1064 }, /* 23.0dB */
158 { 240, 960 }, /* 24.0dB */
159 { 250, 869 }, /* 25.0dB */
160 { 260, 792 }, /* 26.0dB */
161 { 270, 724 }, /* 27.0dB */
162 { 280, 665 }, /* 28.0dB */
163 { 290, 616 }, /* 29.0dB */
164 { 300, 573 }, /* 30.0dB */
165 { 310, 537 }, /* 31.0dB */
166 { 320, 507 }, /* 32.0dB */
167 { 330, 483 }, /* 33.0dB */
168 { 400, 398 }, /* 40.0dB */
169 { 450, 381 }, /* 45.0dB */
170 { 500, 377 } /* 50.0dB */
171};
172
173/* DVBS2 C/N Lookup table */
174static const struct stv090x_tab stv090x_s2cn_tab[] = {
175 { -30, 13348 }, /* -3.0dB */
176 { -20, 12640 }, /* -2d.0B */
177 { -10, 11883 }, /* -1.0dB */
178 { 0, 11101 }, /* -0.0dB */
179 { 5, 10718 }, /* 0.5dB */
180 { 10, 10339 }, /* 1.0dB */
181 { 15, 9947 }, /* 1.5dB */
182 { 20, 9552 }, /* 2.0dB */
183 { 25, 9183 }, /* 2.5dB */
184 { 30, 8799 }, /* 3.0dB */
185 { 35, 8422 }, /* 3.5dB */
186 { 40, 8062 }, /* 4.0dB */
187 { 45, 7707 }, /* 4.5dB */
188 { 50, 7353 }, /* 5.0dB */
189 { 55, 7025 }, /* 5.5dB */
190 { 60, 6684 }, /* 6.0dB */
191 { 65, 6331 }, /* 6.5dB */
192 { 70, 6036 }, /* 7.0dB */
193 { 75, 5727 }, /* 7.5dB */
194 { 80, 5437 }, /* 8.0dB */
195 { 85, 5164 }, /* 8.5dB */
196 { 90, 4902 }, /* 9.0dB */
197 { 95, 4653 }, /* 9.5dB */
198 { 100, 4408 }, /* 10.0dB */
199 { 105, 4187 }, /* 10.5dB */
200 { 110, 3961 }, /* 11.0dB */
201 { 115, 3751 }, /* 11.5dB */
202 { 120, 3558 }, /* 12.0dB */
203 { 125, 3368 }, /* 12.5dB */
204 { 130, 3191 }, /* 13.0dB */
205 { 135, 3017 }, /* 13.5dB */
206 { 140, 2862 }, /* 14.0dB */
207 { 145, 2710 }, /* 14.5dB */
208 { 150, 2565 }, /* 15.0dB */
209 { 160, 2300 }, /* 16.0dB */
210 { 170, 2058 }, /* 17.0dB */
211 { 180, 1849 }, /* 18.0dB */
212 { 190, 1663 }, /* 19.0dB */
213 { 200, 1495 }, /* 20.0dB */
214 { 210, 1349 }, /* 21.0dB */
215 { 220, 1222 }, /* 22.0dB */
216 { 230, 1110 }, /* 23.0dB */
217 { 240, 1011 }, /* 24.0dB */
218 { 250, 925 }, /* 25.0dB */
219 { 260, 853 }, /* 26.0dB */
220 { 270, 789 }, /* 27.0dB */
221 { 280, 734 }, /* 28.0dB */
222 { 290, 690 }, /* 29.0dB */
223 { 300, 650 }, /* 30.0dB */
224 { 310, 619 }, /* 31.0dB */
225 { 320, 593 }, /* 32.0dB */
226 { 330, 571 }, /* 33.0dB */
227 { 400, 498 }, /* 40.0dB */
228 { 450, 484 }, /* 45.0dB */
229 { 500, 481 } /* 50.0dB */
230};
231
232/* RF level C/N lookup table */
233static const struct stv090x_tab stv090x_rf_tab[] = {
234 { -5, 0xcaa1 }, /* -5dBm */
235 { -10, 0xc229 }, /* -10dBm */
236 { -15, 0xbb08 }, /* -15dBm */
237 { -20, 0xb4bc }, /* -20dBm */
238 { -25, 0xad5a }, /* -25dBm */
239 { -30, 0xa298 }, /* -30dBm */
240 { -35, 0x98a8 }, /* -35dBm */
241 { -40, 0x8389 }, /* -40dBm */
242 { -45, 0x59be }, /* -45dBm */
243 { -50, 0x3a14 }, /* -50dBm */
244 { -55, 0x2d11 }, /* -55dBm */
245 { -60, 0x210d }, /* -60dBm */
246 { -65, 0xa14f }, /* -65dBm */
247 { -70, 0x07aa } /* -70dBm */
248};
249
250
251static struct stv090x_reg stv0900_initval[] = {
252
253 { STV090x_OUTCFG, 0x00 },
Manu Abraham56571502009-04-07 16:08:26 -0300254 { STV090x_MODECFG, 0xff },
Manu Abrahame415c682009-04-06 15:45:20 -0300255 { STV090x_AGCRF1CFG, 0x11 },
256 { STV090x_AGCRF2CFG, 0x13 },
Manu Abraham56571502009-04-07 16:08:26 -0300257 { STV090x_TSGENERAL1X, 0x14 },
Manu Abrahame415c682009-04-06 15:45:20 -0300258 { STV090x_TSTTNR2, 0x21 },
259 { STV090x_TSTTNR4, 0x21 },
260 { STV090x_P2_DISTXCTL, 0x22 },
261 { STV090x_P2_F22TX, 0xc0 },
262 { STV090x_P2_F22RX, 0xc0 },
263 { STV090x_P2_DISRXCTL, 0x00 },
264 { STV090x_P2_DMDCFGMD, 0xF9 },
265 { STV090x_P2_DEMOD, 0x08 },
266 { STV090x_P2_DMDCFG3, 0xc4 },
267 { STV090x_P2_CARFREQ, 0xed },
268 { STV090x_P2_LDT, 0xd0 },
269 { STV090x_P2_LDT2, 0xb8 },
270 { STV090x_P2_TMGCFG, 0xd2 },
271 { STV090x_P2_TMGTHRISE, 0x20 },
272 { STV090x_P1_TMGCFG, 0xd2 },
273
274 { STV090x_P2_TMGTHFALL, 0x00 },
275 { STV090x_P2_FECSPY, 0x88 },
276 { STV090x_P2_FSPYDATA, 0x3a },
277 { STV090x_P2_FBERCPT4, 0x00 },
278 { STV090x_P2_FSPYBER, 0x10 },
279 { STV090x_P2_ERRCTRL1, 0x35 },
280 { STV090x_P2_ERRCTRL2, 0xc1 },
281 { STV090x_P2_CFRICFG, 0xf8 },
282 { STV090x_P2_NOSCFG, 0x1c },
Manu Abraham56571502009-04-07 16:08:26 -0300283 { STV090x_P2_DMDTOM, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300284 { STV090x_P2_CORRELMANT, 0x70 },
285 { STV090x_P2_CORRELABS, 0x88 },
Manu Abraham56571502009-04-07 16:08:26 -0300286 { STV090x_P2_AGC2O, 0x5b },
Manu Abrahame415c682009-04-06 15:45:20 -0300287 { STV090x_P2_AGC2REF, 0x38 },
288 { STV090x_P2_CARCFG, 0xe4 },
289 { STV090x_P2_ACLC, 0x1A },
290 { STV090x_P2_BCLC, 0x09 },
291 { STV090x_P2_CARHDR, 0x08 },
292 { STV090x_P2_KREFTMG, 0xc1 },
293 { STV090x_P2_SFRUPRATIO, 0xf0 },
294 { STV090x_P2_SFRLOWRATIO, 0x70 },
295 { STV090x_P2_SFRSTEP, 0x58 },
296 { STV090x_P2_TMGCFG2, 0x01 },
297 { STV090x_P2_CAR2CFG, 0x26 },
298 { STV090x_P2_BCLC2S2Q, 0x86 },
299 { STV090x_P2_BCLC2S28, 0x86 },
300 { STV090x_P2_SMAPCOEF7, 0x77 },
301 { STV090x_P2_SMAPCOEF6, 0x85 },
302 { STV090x_P2_SMAPCOEF5, 0x77 },
303 { STV090x_P2_TSCFGL, 0x20 },
304 { STV090x_P2_DMDCFG2, 0x3b },
305 { STV090x_P2_MODCODLST0, 0xff },
306 { STV090x_P2_MODCODLST1, 0xff },
307 { STV090x_P2_MODCODLST2, 0xff },
308 { STV090x_P2_MODCODLST3, 0xff },
309 { STV090x_P2_MODCODLST4, 0xff },
310 { STV090x_P2_MODCODLST5, 0xff },
311 { STV090x_P2_MODCODLST6, 0xff },
312 { STV090x_P2_MODCODLST7, 0xcc },
313 { STV090x_P2_MODCODLST8, 0xcc },
314 { STV090x_P2_MODCODLST9, 0xcc },
315 { STV090x_P2_MODCODLSTA, 0xcc },
316 { STV090x_P2_MODCODLSTB, 0xcc },
317 { STV090x_P2_MODCODLSTC, 0xcc },
318 { STV090x_P2_MODCODLSTD, 0xcc },
319 { STV090x_P2_MODCODLSTE, 0xcc },
320 { STV090x_P2_MODCODLSTF, 0xcf },
321 { STV090x_P1_DISTXCTL, 0x22 },
322 { STV090x_P1_F22TX, 0xc0 },
323 { STV090x_P1_F22RX, 0xc0 },
324 { STV090x_P1_DISRXCTL, 0x00 },
325 { STV090x_P1_DMDCFGMD, 0xf9 },
326 { STV090x_P1_DEMOD, 0x08 },
327 { STV090x_P1_DMDCFG3, 0xc4 },
Manu Abraham56571502009-04-07 16:08:26 -0300328 { STV090x_P1_DMDTOM, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300329 { STV090x_P1_CARFREQ, 0xed },
330 { STV090x_P1_LDT, 0xd0 },
331 { STV090x_P1_LDT2, 0xb8 },
332 { STV090x_P1_TMGCFG, 0xd2 },
333 { STV090x_P1_TMGTHRISE, 0x20 },
334 { STV090x_P1_TMGTHFALL, 0x00 },
335 { STV090x_P1_SFRUPRATIO, 0xf0 },
336 { STV090x_P1_SFRLOWRATIO, 0x70 },
337 { STV090x_P1_TSCFGL, 0x20 },
338 { STV090x_P1_FECSPY, 0x88 },
339 { STV090x_P1_FSPYDATA, 0x3a },
340 { STV090x_P1_FBERCPT4, 0x00 },
341 { STV090x_P1_FSPYBER, 0x10 },
342 { STV090x_P1_ERRCTRL1, 0x35 },
343 { STV090x_P1_ERRCTRL2, 0xc1 },
344 { STV090x_P1_CFRICFG, 0xf8 },
345 { STV090x_P1_NOSCFG, 0x1c },
346 { STV090x_P1_CORRELMANT, 0x70 },
347 { STV090x_P1_CORRELABS, 0x88 },
Manu Abraham56571502009-04-07 16:08:26 -0300348 { STV090x_P1_AGC2O, 0x5b },
Manu Abrahame415c682009-04-06 15:45:20 -0300349 { STV090x_P1_AGC2REF, 0x38 },
350 { STV090x_P1_CARCFG, 0xe4 },
351 { STV090x_P1_ACLC, 0x1A },
352 { STV090x_P1_BCLC, 0x09 },
353 { STV090x_P1_CARHDR, 0x08 },
354 { STV090x_P1_KREFTMG, 0xc1 },
355 { STV090x_P1_SFRSTEP, 0x58 },
356 { STV090x_P1_TMGCFG2, 0x01 },
357 { STV090x_P1_CAR2CFG, 0x26 },
358 { STV090x_P1_BCLC2S2Q, 0x86 },
359 { STV090x_P1_BCLC2S28, 0x86 },
360 { STV090x_P1_SMAPCOEF7, 0x77 },
361 { STV090x_P1_SMAPCOEF6, 0x85 },
362 { STV090x_P1_SMAPCOEF5, 0x77 },
363 { STV090x_P1_DMDCFG2, 0x3b },
364 { STV090x_P1_MODCODLST0, 0xff },
365 { STV090x_P1_MODCODLST1, 0xff },
366 { STV090x_P1_MODCODLST2, 0xff },
367 { STV090x_P1_MODCODLST3, 0xff },
368 { STV090x_P1_MODCODLST4, 0xff },
369 { STV090x_P1_MODCODLST5, 0xff },
370 { STV090x_P1_MODCODLST6, 0xff },
371 { STV090x_P1_MODCODLST7, 0xcc },
372 { STV090x_P1_MODCODLST8, 0xcc },
373 { STV090x_P1_MODCODLST9, 0xcc },
374 { STV090x_P1_MODCODLSTA, 0xcc },
375 { STV090x_P1_MODCODLSTB, 0xcc },
376 { STV090x_P1_MODCODLSTC, 0xcc },
377 { STV090x_P1_MODCODLSTD, 0xcc },
378 { STV090x_P1_MODCODLSTE, 0xcc },
379 { STV090x_P1_MODCODLSTF, 0xcf },
380 { STV090x_GENCFG, 0x1d },
381 { STV090x_NBITER_NF4, 0x37 },
382 { STV090x_NBITER_NF5, 0x29 },
383 { STV090x_NBITER_NF6, 0x37 },
384 { STV090x_NBITER_NF7, 0x33 },
385 { STV090x_NBITER_NF8, 0x31 },
386 { STV090x_NBITER_NF9, 0x2f },
387 { STV090x_NBITER_NF10, 0x39 },
388 { STV090x_NBITER_NF11, 0x3a },
389 { STV090x_NBITER_NF12, 0x29 },
390 { STV090x_NBITER_NF13, 0x37 },
391 { STV090x_NBITER_NF14, 0x33 },
392 { STV090x_NBITER_NF15, 0x2f },
393 { STV090x_NBITER_NF16, 0x39 },
394 { STV090x_NBITER_NF17, 0x3a },
395 { STV090x_NBITERNOERR, 0x04 },
396 { STV090x_GAINLLR_NF4, 0x0C },
397 { STV090x_GAINLLR_NF5, 0x0F },
398 { STV090x_GAINLLR_NF6, 0x11 },
399 { STV090x_GAINLLR_NF7, 0x14 },
400 { STV090x_GAINLLR_NF8, 0x17 },
401 { STV090x_GAINLLR_NF9, 0x19 },
402 { STV090x_GAINLLR_NF10, 0x20 },
403 { STV090x_GAINLLR_NF11, 0x21 },
404 { STV090x_GAINLLR_NF12, 0x0D },
405 { STV090x_GAINLLR_NF13, 0x0F },
406 { STV090x_GAINLLR_NF14, 0x13 },
407 { STV090x_GAINLLR_NF15, 0x1A },
408 { STV090x_GAINLLR_NF16, 0x1F },
409 { STV090x_GAINLLR_NF17, 0x21 },
Manu Abraham56571502009-04-07 16:08:26 -0300410 { STV090x_RCCFGH, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300411 { STV090x_P1_FECM, 0x01 }, /* disable DSS modes */
412 { STV090x_P2_FECM, 0x01 }, /* disable DSS modes */
413 { STV090x_P1_PRVIT, 0x2F }, /* disable PR 6/7 */
414 { STV090x_P2_PRVIT, 0x2F }, /* disable PR 6/7 */
415};
416
417static struct stv090x_reg stv0903_initval[] = {
418 { STV090x_OUTCFG, 0x00 },
419 { STV090x_AGCRF1CFG, 0x11 },
420 { STV090x_STOPCLK1, 0x48 },
421 { STV090x_STOPCLK2, 0x14 },
422 { STV090x_TSTTNR1, 0x27 },
423 { STV090x_TSTTNR2, 0x21 },
424 { STV090x_P1_DISTXCTL, 0x22 },
425 { STV090x_P1_F22TX, 0xc0 },
426 { STV090x_P1_F22RX, 0xc0 },
427 { STV090x_P1_DISRXCTL, 0x00 },
428 { STV090x_P1_DMDCFGMD, 0xF9 },
429 { STV090x_P1_DEMOD, 0x08 },
430 { STV090x_P1_DMDCFG3, 0xc4 },
431 { STV090x_P1_CARFREQ, 0xed },
432 { STV090x_P1_TNRCFG2, 0x82 },
433 { STV090x_P1_LDT, 0xd0 },
434 { STV090x_P1_LDT2, 0xb8 },
435 { STV090x_P1_TMGCFG, 0xd2 },
436 { STV090x_P1_TMGTHRISE, 0x20 },
437 { STV090x_P1_TMGTHFALL, 0x00 },
438 { STV090x_P1_SFRUPRATIO, 0xf0 },
439 { STV090x_P1_SFRLOWRATIO, 0x70 },
440 { STV090x_P1_TSCFGL, 0x20 },
441 { STV090x_P1_FECSPY, 0x88 },
442 { STV090x_P1_FSPYDATA, 0x3a },
443 { STV090x_P1_FBERCPT4, 0x00 },
444 { STV090x_P1_FSPYBER, 0x10 },
445 { STV090x_P1_ERRCTRL1, 0x35 },
446 { STV090x_P1_ERRCTRL2, 0xc1 },
447 { STV090x_P1_CFRICFG, 0xf8 },
448 { STV090x_P1_NOSCFG, 0x1c },
Manu Abraham56571502009-04-07 16:08:26 -0300449 { STV090x_P1_DMDTOM, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300450 { STV090x_P1_CORRELMANT, 0x70 },
451 { STV090x_P1_CORRELABS, 0x88 },
Manu Abraham56571502009-04-07 16:08:26 -0300452 { STV090x_P1_AGC2O, 0x5b },
453 { STV090x_P1_AGC2REF, 0x38 },
Manu Abrahame415c682009-04-06 15:45:20 -0300454 { STV090x_P1_CARCFG, 0xe4 },
455 { STV090x_P1_ACLC, 0x1A },
Manu Abraham56571502009-04-07 16:08:26 -0300456 { STV090x_P1_BCLC, 0x09 },
Manu Abrahame415c682009-04-06 15:45:20 -0300457 { STV090x_P1_CARHDR, 0x08 },
458 { STV090x_P1_KREFTMG, 0xc1 },
459 { STV090x_P1_SFRSTEP, 0x58 },
460 { STV090x_P1_TMGCFG2, 0x01 },
461 { STV090x_P1_CAR2CFG, 0x26 },
462 { STV090x_P1_BCLC2S2Q, 0x86 },
463 { STV090x_P1_BCLC2S28, 0x86 },
464 { STV090x_P1_SMAPCOEF7, 0x77 },
465 { STV090x_P1_SMAPCOEF6, 0x85 },
466 { STV090x_P1_SMAPCOEF5, 0x77 },
467 { STV090x_P1_DMDCFG2, 0x3b },
468 { STV090x_P1_MODCODLST0, 0xff },
469 { STV090x_P1_MODCODLST1, 0xff },
470 { STV090x_P1_MODCODLST2, 0xff },
471 { STV090x_P1_MODCODLST3, 0xff },
472 { STV090x_P1_MODCODLST4, 0xff },
473 { STV090x_P1_MODCODLST5, 0xff },
474 { STV090x_P1_MODCODLST6, 0xff },
475 { STV090x_P1_MODCODLST7, 0xcc },
476 { STV090x_P1_MODCODLST8, 0xcc },
477 { STV090x_P1_MODCODLST9, 0xcc },
478 { STV090x_P1_MODCODLSTA, 0xcc },
479 { STV090x_P1_MODCODLSTB, 0xcc },
480 { STV090x_P1_MODCODLSTC, 0xcc },
481 { STV090x_P1_MODCODLSTD, 0xcc },
482 { STV090x_P1_MODCODLSTE, 0xcc },
483 { STV090x_P1_MODCODLSTF, 0xcf },
484 { STV090x_GENCFG, 0x1c },
485 { STV090x_NBITER_NF4, 0x37 },
486 { STV090x_NBITER_NF5, 0x29 },
487 { STV090x_NBITER_NF6, 0x37 },
488 { STV090x_NBITER_NF7, 0x33 },
489 { STV090x_NBITER_NF8, 0x31 },
490 { STV090x_NBITER_NF9, 0x2f },
491 { STV090x_NBITER_NF10, 0x39 },
492 { STV090x_NBITER_NF11, 0x3a },
493 { STV090x_NBITER_NF12, 0x29 },
494 { STV090x_NBITER_NF13, 0x37 },
495 { STV090x_NBITER_NF14, 0x33 },
496 { STV090x_NBITER_NF15, 0x2f },
497 { STV090x_NBITER_NF16, 0x39 },
498 { STV090x_NBITER_NF17, 0x3a },
499 { STV090x_NBITERNOERR, 0x04 },
500 { STV090x_GAINLLR_NF4, 0x0C },
501 { STV090x_GAINLLR_NF5, 0x0F },
502 { STV090x_GAINLLR_NF6, 0x11 },
503 { STV090x_GAINLLR_NF7, 0x14 },
504 { STV090x_GAINLLR_NF8, 0x17 },
505 { STV090x_GAINLLR_NF9, 0x19 },
506 { STV090x_GAINLLR_NF10, 0x20 },
507 { STV090x_GAINLLR_NF11, 0x21 },
508 { STV090x_GAINLLR_NF12, 0x0D },
509 { STV090x_GAINLLR_NF13, 0x0F },
510 { STV090x_GAINLLR_NF14, 0x13 },
511 { STV090x_GAINLLR_NF15, 0x1A },
512 { STV090x_GAINLLR_NF16, 0x1F },
513 { STV090x_GAINLLR_NF17, 0x21 },
Manu Abraham56571502009-04-07 16:08:26 -0300514 { STV090x_RCCFGH, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300515 { STV090x_P1_FECM, 0x01 }, /*disable the DSS mode */
516 { STV090x_P1_PRVIT, 0x2f } /*disable puncture rate 6/7*/
517};
518
519static struct stv090x_reg stv0900_cut20_val[] = {
520
521 { STV090x_P2_DMDCFG3, 0xe8 },
Manu Abraham56571502009-04-07 16:08:26 -0300522 { STV090x_P2_DMDCFG4, 0x10 },
Manu Abrahame415c682009-04-06 15:45:20 -0300523 { STV090x_P2_CARFREQ, 0x38 },
524 { STV090x_P2_CARHDR, 0x20 },
525 { STV090x_P2_KREFTMG, 0x5a },
526 { STV090x_P2_SMAPCOEF7, 0x06 },
527 { STV090x_P2_SMAPCOEF6, 0x00 },
528 { STV090x_P2_SMAPCOEF5, 0x04 },
529 { STV090x_P2_NOSCFG, 0x0c },
530 { STV090x_P1_DMDCFG3, 0xe8 },
Manu Abraham56571502009-04-07 16:08:26 -0300531 { STV090x_P1_DMDCFG4, 0x10 },
Manu Abrahame415c682009-04-06 15:45:20 -0300532 { STV090x_P1_CARFREQ, 0x38 },
533 { STV090x_P1_CARHDR, 0x20 },
534 { STV090x_P1_KREFTMG, 0x5a },
535 { STV090x_P1_SMAPCOEF7, 0x06 },
536 { STV090x_P1_SMAPCOEF6, 0x00 },
537 { STV090x_P1_SMAPCOEF5, 0x04 },
538 { STV090x_P1_NOSCFG, 0x0c },
539 { STV090x_GAINLLR_NF4, 0x21 },
540 { STV090x_GAINLLR_NF5, 0x21 },
541 { STV090x_GAINLLR_NF6, 0x20 },
542 { STV090x_GAINLLR_NF7, 0x1F },
543 { STV090x_GAINLLR_NF8, 0x1E },
544 { STV090x_GAINLLR_NF9, 0x1E },
545 { STV090x_GAINLLR_NF10, 0x1D },
546 { STV090x_GAINLLR_NF11, 0x1B },
547 { STV090x_GAINLLR_NF12, 0x20 },
548 { STV090x_GAINLLR_NF13, 0x20 },
549 { STV090x_GAINLLR_NF14, 0x20 },
550 { STV090x_GAINLLR_NF15, 0x20 },
551 { STV090x_GAINLLR_NF16, 0x20 },
552 { STV090x_GAINLLR_NF17, 0x21 },
553};
554
555static struct stv090x_reg stv0903_cut20_val[] = {
556 { STV090x_P1_DMDCFG3, 0xe8 },
Manu Abraham56571502009-04-07 16:08:26 -0300557 { STV090x_P1_DMDCFG4, 0x10 },
Manu Abrahame415c682009-04-06 15:45:20 -0300558 { STV090x_P1_CARFREQ, 0x38 },
559 { STV090x_P1_CARHDR, 0x20 },
560 { STV090x_P1_KREFTMG, 0x5a },
561 { STV090x_P1_SMAPCOEF7, 0x06 },
562 { STV090x_P1_SMAPCOEF6, 0x00 },
563 { STV090x_P1_SMAPCOEF5, 0x04 },
564 { STV090x_P1_NOSCFG, 0x0c },
565 { STV090x_GAINLLR_NF4, 0x21 },
566 { STV090x_GAINLLR_NF5, 0x21 },
567 { STV090x_GAINLLR_NF6, 0x20 },
568 { STV090x_GAINLLR_NF7, 0x1F },
569 { STV090x_GAINLLR_NF8, 0x1E },
570 { STV090x_GAINLLR_NF9, 0x1E },
571 { STV090x_GAINLLR_NF10, 0x1D },
572 { STV090x_GAINLLR_NF11, 0x1B },
573 { STV090x_GAINLLR_NF12, 0x20 },
574 { STV090x_GAINLLR_NF13, 0x20 },
575 { STV090x_GAINLLR_NF14, 0x20 },
576 { STV090x_GAINLLR_NF15, 0x20 },
577 { STV090x_GAINLLR_NF16, 0x20 },
578 { STV090x_GAINLLR_NF17, 0x21 }
579};
580
Manu Abrahame415c682009-04-06 15:45:20 -0300581/* Cut 2.0 Long Frame Tracking CR loop */
582static struct stv090x_long_frame_crloop stv090x_s2_crl_cut20[] = {
583 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
584 { STV090x_QPSK_12, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x1e },
585 { STV090x_QPSK_35, 0x2f, 0x3f, 0x2e, 0x2f, 0x3d, 0x0f, 0x0e, 0x2e, 0x3d, 0x0e },
586 { STV090x_QPSK_23, 0x2f, 0x3f, 0x2e, 0x2f, 0x0e, 0x0f, 0x0e, 0x1e, 0x3d, 0x3d },
587 { STV090x_QPSK_34, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
588 { STV090x_QPSK_45, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
589 { STV090x_QPSK_56, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
590 { STV090x_QPSK_89, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
591 { STV090x_QPSK_910, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
592 { STV090x_8PSK_35, 0x3c, 0x3e, 0x1c, 0x2e, 0x0c, 0x1e, 0x2b, 0x2d, 0x1b, 0x1d },
593 { STV090x_8PSK_23, 0x1d, 0x3e, 0x3c, 0x2e, 0x2c, 0x1e, 0x0c, 0x2d, 0x2b, 0x1d },
594 { STV090x_8PSK_34, 0x0e, 0x3e, 0x3d, 0x2e, 0x0d, 0x1e, 0x2c, 0x2d, 0x0c, 0x1d },
595 { STV090x_8PSK_56, 0x2e, 0x3e, 0x1e, 0x2e, 0x2d, 0x1e, 0x3c, 0x2d, 0x2c, 0x1d },
596 { STV090x_8PSK_89, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x0d, 0x2d, 0x3c, 0x1d },
597 { STV090x_8PSK_910, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x1d, 0x2d, 0x0d, 0x1d }
598};
599
Manu Abraham27d40322009-05-02 18:26:58 -0300600/* Cut 3.0 Long Frame Tracking CR loop */
601static struct stv090x_long_frame_crloop stv090x_s2_crl_cut30[] = {
602 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
603 { STV090x_QPSK_12, 0x3c, 0x2c, 0x0c, 0x2c, 0x1b, 0x2c, 0x1b, 0x1c, 0x0b, 0x3b },
604 { STV090x_QPSK_35, 0x0d, 0x0d, 0x0c, 0x0d, 0x1b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
605 { STV090x_QPSK_23, 0x1d, 0x0d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
606 { STV090x_QPSK_34, 0x1d, 0x1d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
607 { STV090x_QPSK_45, 0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
608 { STV090x_QPSK_56, 0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
609 { STV090x_QPSK_89, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
610 { STV090x_QPSK_910, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
611 { STV090x_8PSK_35, 0x39, 0x29, 0x39, 0x19, 0x19, 0x19, 0x19, 0x19, 0x09, 0x19 },
612 { STV090x_8PSK_23, 0x2a, 0x39, 0x1a, 0x0a, 0x39, 0x0a, 0x29, 0x39, 0x29, 0x0a },
613 { STV090x_8PSK_34, 0x2b, 0x3a, 0x1b, 0x1b, 0x3a, 0x1b, 0x1a, 0x0b, 0x1a, 0x3a },
614 { STV090x_8PSK_56, 0x0c, 0x1b, 0x3b, 0x3b, 0x1b, 0x3b, 0x3a, 0x3b, 0x3a, 0x1b },
615 { STV090x_8PSK_89, 0x0d, 0x3c, 0x2c, 0x2c, 0x2b, 0x0c, 0x0b, 0x3b, 0x0b, 0x1b },
616 { STV090x_8PSK_910, 0x0d, 0x0d, 0x2c, 0x3c, 0x3b, 0x1c, 0x0b, 0x3b, 0x0b, 0x1b }
617};
Manu Abrahame415c682009-04-06 15:45:20 -0300618
619/* Cut 2.0 Long Frame Tracking CR Loop */
620static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut20[] = {
621 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
622 { STV090x_16APSK_23, 0x0c, 0x0c, 0x0c, 0x0c, 0x1d, 0x0c, 0x3c, 0x0c, 0x2c, 0x0c },
623 { STV090x_16APSK_34, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0c, 0x2d, 0x0c, 0x1d, 0x0c },
624 { STV090x_16APSK_45, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
625 { STV090x_16APSK_56, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
626 { STV090x_16APSK_89, 0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
627 { STV090x_16APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
628 { STV090x_32APSK_34, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
629 { STV090x_32APSK_45, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
630 { STV090x_32APSK_56, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
631 { STV090x_32APSK_89, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
632 { STV090x_32APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c }
633};
634
Manu Abraham27d40322009-05-02 18:26:58 -0300635/* Cut 3.0 Long Frame Tracking CR Loop */
636static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut30[] = {
637 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
638 { STV090x_16APSK_23, 0x0a, 0x0a, 0x0a, 0x0a, 0x1a, 0x0a, 0x3a, 0x0a, 0x2a, 0x0a },
639 { STV090x_16APSK_34, 0x0a, 0x0a, 0x0a, 0x0a, 0x0b, 0x0a, 0x3b, 0x0a, 0x1b, 0x0a },
640 { STV090x_16APSK_45, 0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
641 { STV090x_16APSK_56, 0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
642 { STV090x_16APSK_89, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
643 { STV090x_16APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
644 { STV090x_32APSK_34, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
645 { STV090x_32APSK_45, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
646 { STV090x_32APSK_56, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
647 { STV090x_32APSK_89, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
648 { STV090x_32APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a }
649};
Manu Abrahame415c682009-04-06 15:45:20 -0300650
651static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut20[] = {
652 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
653 { STV090x_QPSK_14, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x2d, 0x1f, 0x3d, 0x3e },
654 { STV090x_QPSK_13, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x3d, 0x0f, 0x3d, 0x2e },
655 { STV090x_QPSK_25, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x2e }
656};
657
Manu Abraham27d40322009-05-02 18:26:58 -0300658static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut30[] = {
659 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
660 { STV090x_QPSK_14, 0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x2a, 0x1c, 0x3a, 0x3b },
661 { STV090x_QPSK_13, 0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x3a, 0x0c, 0x3a, 0x2b },
662 { STV090x_QPSK_25, 0x1c, 0x3c, 0x1b, 0x3c, 0x3a, 0x1c, 0x3a, 0x3b, 0x3a, 0x2b }
Manu Abrahame415c682009-04-06 15:45:20 -0300663};
664
Manu Abraham27d40322009-05-02 18:26:58 -0300665/* Cut 2.0 Short Frame Tracking CR Loop */
666static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut20[] = {
667 /* MODCOD 2M 5M 10M 20M 30M */
668 { STV090x_QPSK, 0x2f, 0x2e, 0x0e, 0x0e, 0x3d },
669 { STV090x_8PSK, 0x3e, 0x0e, 0x2d, 0x0d, 0x3c },
670 { STV090x_16APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d },
671 { STV090x_32APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d }
672};
673
674/* Cut 3.0 Short Frame Tracking CR Loop */
675static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut30[] = {
676 /* MODCOD 2M 5M 10M 20M 30M */
677 { STV090x_QPSK, 0x2C, 0x2B, 0x0B, 0x0B, 0x3A },
678 { STV090x_8PSK, 0x3B, 0x0B, 0x2A, 0x0A, 0x39 },
679 { STV090x_16APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A },
680 { STV090x_32APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A }
681};
Manu Abrahame415c682009-04-06 15:45:20 -0300682
683static inline s32 comp2(s32 __x, s32 __width)
684{
685 if (__width == 32)
686 return __x;
687 else
688 return (__x >= (1 << (__width - 1))) ? (__x - (1 << __width)) : __x;
689}
690
691static int stv090x_read_reg(struct stv090x_state *state, unsigned int reg)
692{
693 const struct stv090x_config *config = state->config;
694 int ret;
695
696 u8 b0[] = { reg >> 8, reg & 0xff };
697 u8 buf;
698
699 struct i2c_msg msg[] = {
700 { .addr = config->address, .flags = 0, .buf = b0, .len = 2 },
701 { .addr = config->address, .flags = I2C_M_RD, .buf = &buf, .len = 1 }
702 };
703
704 ret = i2c_transfer(state->i2c, msg, 2);
705 if (ret != 2) {
706 if (ret != -ERESTARTSYS)
707 dprintk(FE_ERROR, 1,
708 "Read error, Reg=[0x%02x], Status=%d",
709 reg, ret);
710
711 return ret < 0 ? ret : -EREMOTEIO;
712 }
713 if (unlikely(*state->verbose >= FE_DEBUGREG))
714 dprintk(FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
715 reg, buf);
716
717 return (unsigned int) buf;
718}
719
720static int stv090x_write_regs(struct stv090x_state *state, unsigned int reg, u8 *data, u32 count)
721{
722 const struct stv090x_config *config = state->config;
723 int ret;
724 u8 buf[2 + count];
725 struct i2c_msg i2c_msg = { .addr = config->address, .flags = 0, .buf = buf, .len = 2 + count };
726
727 buf[0] = reg >> 8;
728 buf[1] = reg & 0xff;
729 memcpy(&buf[2], data, count);
730
731 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
732 int i;
733
734 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
735 for (i = 0; i < count; i++)
736 printk(" %02x", data[i]);
737 printk("\n");
738 }
739
740 ret = i2c_transfer(state->i2c, &i2c_msg, 1);
741 if (ret != 1) {
742 if (ret != -ERESTARTSYS)
743 dprintk(FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
744 reg, data[0], count, ret);
745 return ret < 0 ? ret : -EREMOTEIO;
746 }
747
748 return 0;
749}
750
751static int stv090x_write_reg(struct stv090x_state *state, unsigned int reg, u8 data)
752{
753 return stv090x_write_regs(state, reg, &data, 1);
754}
755
756static int stv090x_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
757{
758 struct stv090x_state *state = fe->demodulator_priv;
759 u32 reg;
760
Andreas Regel96506a52010-01-05 19:20:21 -0300761 if (enable)
762 mutex_lock(&state->internal->tuner_lock);
763
Manu Abrahame415c682009-04-06 15:45:20 -0300764 reg = STV090x_READ_DEMOD(state, I2CRPT);
Manu Abrahame415c682009-04-06 15:45:20 -0300765 if (enable) {
Manu Abraham017eb0382009-04-07 05:19:54 -0300766 dprintk(FE_DEBUG, 1, "Enable Gate");
Manu Abrahame415c682009-04-06 15:45:20 -0300767 STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 1);
768 if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0)
769 goto err;
770
771 } else {
Manu Abraham017eb0382009-04-07 05:19:54 -0300772 dprintk(FE_DEBUG, 1, "Disable Gate");
Manu Abrahame415c682009-04-06 15:45:20 -0300773 STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 0);
774 if ((STV090x_WRITE_DEMOD(state, I2CRPT, reg)) < 0)
775 goto err;
776 }
Andreas Regel96506a52010-01-05 19:20:21 -0300777
778 if (!enable)
779 mutex_unlock(&state->internal->tuner_lock);
780
Manu Abrahame415c682009-04-06 15:45:20 -0300781 return 0;
782err:
783 dprintk(FE_ERROR, 1, "I/O error");
Andreas Regel96506a52010-01-05 19:20:21 -0300784 mutex_unlock(&state->internal->tuner_lock);
Manu Abrahame415c682009-04-06 15:45:20 -0300785 return -1;
786}
787
788static void stv090x_get_lock_tmg(struct stv090x_state *state)
789{
790 switch (state->algo) {
791 case STV090x_BLIND_SEARCH:
792 dprintk(FE_DEBUG, 1, "Blind Search");
793 if (state->srate <= 1500000) { /*10Msps< SR <=15Msps*/
794 state->DemodTimeout = 1500;
795 state->FecTimeout = 400;
796 } else if (state->srate <= 5000000) { /*10Msps< SR <=15Msps*/
797 state->DemodTimeout = 1000;
798 state->FecTimeout = 300;
799 } else { /*SR >20Msps*/
800 state->DemodTimeout = 700;
801 state->FecTimeout = 100;
802 }
803 break;
804
805 case STV090x_COLD_SEARCH:
806 case STV090x_WARM_SEARCH:
807 default:
808 dprintk(FE_DEBUG, 1, "Normal Search");
809 if (state->srate <= 1000000) { /*SR <=1Msps*/
810 state->DemodTimeout = 4500;
811 state->FecTimeout = 1700;
812 } else if (state->srate <= 2000000) { /*1Msps < SR <= 2Msps */
813 state->DemodTimeout = 2500;
814 state->FecTimeout = 1100;
815 } else if (state->srate <= 5000000) { /*2Msps < SR <= 5Msps */
816 state->DemodTimeout = 1000;
817 state->FecTimeout = 550;
818 } else if (state->srate <= 10000000) { /*5Msps < SR <= 10Msps */
819 state->DemodTimeout = 700;
820 state->FecTimeout = 250;
821 } else if (state->srate <= 20000000) { /*10Msps < SR <= 20Msps */
822 state->DemodTimeout = 400;
823 state->FecTimeout = 130;
824 } else { /*SR >20Msps*/
825 state->DemodTimeout = 300;
826 state->FecTimeout = 100;
827 }
828 break;
829 }
830
831 if (state->algo == STV090x_WARM_SEARCH)
832 state->DemodTimeout /= 2;
833}
834
835static int stv090x_set_srate(struct stv090x_state *state, u32 srate)
836{
837 u32 sym;
838
Manu Abraham15bb3662009-04-08 20:14:00 -0300839 if (srate > 60000000) {
840 sym = (srate << 4); /* SR * 2^16 / master_clk */
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300841 sym /= (state->internal->mclk >> 12);
Manu Abraham15bb3662009-04-08 20:14:00 -0300842 } else if (srate > 6000000) {
843 sym = (srate << 6);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300844 sym /= (state->internal->mclk >> 10);
Manu Abrahame415c682009-04-06 15:45:20 -0300845 } else {
Manu Abraham15bb3662009-04-08 20:14:00 -0300846 sym = (srate << 9);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300847 sym /= (state->internal->mclk >> 7);
Manu Abrahame415c682009-04-06 15:45:20 -0300848 }
849
Manu Abraham15bb3662009-04-08 20:14:00 -0300850 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0x7f) < 0) /* MSB */
Manu Abrahame415c682009-04-06 15:45:20 -0300851 goto err;
852 if (STV090x_WRITE_DEMOD(state, SFRINIT0, (sym & 0xff)) < 0) /* LSB */
853 goto err;
Manu Abraham15bb3662009-04-08 20:14:00 -0300854
Manu Abrahame415c682009-04-06 15:45:20 -0300855 return 0;
856err:
857 dprintk(FE_ERROR, 1, "I/O error");
858 return -1;
859}
860
861static int stv090x_set_max_srate(struct stv090x_state *state, u32 clk, u32 srate)
862{
863 u32 sym;
864
865 srate = 105 * (srate / 100);
Manu Abraham15bb3662009-04-08 20:14:00 -0300866 if (srate > 60000000) {
867 sym = (srate << 4); /* SR * 2^16 / master_clk */
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300868 sym /= (state->internal->mclk >> 12);
Manu Abraham15bb3662009-04-08 20:14:00 -0300869 } else if (srate > 6000000) {
870 sym = (srate << 6);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300871 sym /= (state->internal->mclk >> 10);
Manu Abrahame415c682009-04-06 15:45:20 -0300872 } else {
Manu Abraham15bb3662009-04-08 20:14:00 -0300873 sym = (srate << 9);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300874 sym /= (state->internal->mclk >> 7);
Manu Abrahame415c682009-04-06 15:45:20 -0300875 }
Manu Abraham15bb3662009-04-08 20:14:00 -0300876
877 if (sym < 0x7fff) {
878 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) /* MSB */
879 goto err;
880 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) /* LSB */
881 goto err;
882 } else {
883 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x7f) < 0) /* MSB */
884 goto err;
885 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xff) < 0) /* LSB */
886 goto err;
887 }
888
Manu Abrahame415c682009-04-06 15:45:20 -0300889 return 0;
890err:
891 dprintk(FE_ERROR, 1, "I/O error");
892 return -1;
893}
894
895static int stv090x_set_min_srate(struct stv090x_state *state, u32 clk, u32 srate)
896{
897 u32 sym;
898
899 srate = 95 * (srate / 100);
Manu Abraham15bb3662009-04-08 20:14:00 -0300900 if (srate > 60000000) {
901 sym = (srate << 4); /* SR * 2^16 / master_clk */
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300902 sym /= (state->internal->mclk >> 12);
Manu Abraham15bb3662009-04-08 20:14:00 -0300903 } else if (srate > 6000000) {
904 sym = (srate << 6);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300905 sym /= (state->internal->mclk >> 10);
Manu Abrahame415c682009-04-06 15:45:20 -0300906 } else {
Manu Abraham15bb3662009-04-08 20:14:00 -0300907 sym = (srate << 9);
Andreas Regel97f7a2a2010-01-05 19:19:43 -0300908 sym /= (state->internal->mclk >> 7);
Manu Abrahame415c682009-04-06 15:45:20 -0300909 }
Manu Abraham15bb3662009-04-08 20:14:00 -0300910
Andreas Regelb671a8d2009-11-13 18:15:27 -0300911 if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0x7f)) < 0) /* MSB */
Manu Abrahame415c682009-04-06 15:45:20 -0300912 goto err;
913 if (STV090x_WRITE_DEMOD(state, SFRLOW0, (sym & 0xff)) < 0) /* LSB */
914 goto err;
915 return 0;
916err:
917 dprintk(FE_ERROR, 1, "I/O error");
918 return -1;
919}
920
Andreas Regel4e58a682009-04-16 08:40:36 -0300921static u32 stv090x_car_width(u32 srate, enum stv090x_rolloff rolloff)
Manu Abrahame415c682009-04-06 15:45:20 -0300922{
Andreas Regel4e58a682009-04-16 08:40:36 -0300923 u32 ro;
924
925 switch (rolloff) {
926 case STV090x_RO_20:
927 ro = 20;
928 break;
929 case STV090x_RO_25:
930 ro = 25;
931 break;
932 case STV090x_RO_35:
933 default:
934 ro = 35;
935 break;
936 }
937
938 return srate + (srate * ro) / 100;
Manu Abrahame415c682009-04-06 15:45:20 -0300939}
940
941static int stv090x_set_vit_thacq(struct stv090x_state *state)
942{
943 if (STV090x_WRITE_DEMOD(state, VTH12, 0x96) < 0)
944 goto err;
945 if (STV090x_WRITE_DEMOD(state, VTH23, 0x64) < 0)
946 goto err;
947 if (STV090x_WRITE_DEMOD(state, VTH34, 0x36) < 0)
948 goto err;
949 if (STV090x_WRITE_DEMOD(state, VTH56, 0x23) < 0)
950 goto err;
951 if (STV090x_WRITE_DEMOD(state, VTH67, 0x1e) < 0)
952 goto err;
953 if (STV090x_WRITE_DEMOD(state, VTH78, 0x19) < 0)
954 goto err;
955 return 0;
956err:
957 dprintk(FE_ERROR, 1, "I/O error");
958 return -1;
959}
960
961static int stv090x_set_vit_thtracq(struct stv090x_state *state)
962{
963 if (STV090x_WRITE_DEMOD(state, VTH12, 0xd0) < 0)
964 goto err;
965 if (STV090x_WRITE_DEMOD(state, VTH23, 0x7d) < 0)
966 goto err;
967 if (STV090x_WRITE_DEMOD(state, VTH34, 0x53) < 0)
968 goto err;
969 if (STV090x_WRITE_DEMOD(state, VTH56, 0x2f) < 0)
970 goto err;
971 if (STV090x_WRITE_DEMOD(state, VTH67, 0x24) < 0)
972 goto err;
973 if (STV090x_WRITE_DEMOD(state, VTH78, 0x1f) < 0)
974 goto err;
975 return 0;
976err:
977 dprintk(FE_ERROR, 1, "I/O error");
978 return -1;
979}
980
981static int stv090x_set_viterbi(struct stv090x_state *state)
982{
983 switch (state->search_mode) {
984 case STV090x_SEARCH_AUTO:
985 if (STV090x_WRITE_DEMOD(state, FECM, 0x10) < 0) /* DVB-S and DVB-S2 */
986 goto err;
987 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x3f) < 0) /* all puncture rate */
988 goto err;
989 break;
990 case STV090x_SEARCH_DVBS1:
991 if (STV090x_WRITE_DEMOD(state, FECM, 0x00) < 0) /* disable DSS */
992 goto err;
993 switch (state->fec) {
994 case STV090x_PR12:
995 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
996 goto err;
997 break;
998
999 case STV090x_PR23:
1000 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
1001 goto err;
1002 break;
1003
1004 case STV090x_PR34:
1005 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x04) < 0)
1006 goto err;
1007 break;
1008
1009 case STV090x_PR56:
1010 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x08) < 0)
1011 goto err;
1012 break;
1013
1014 case STV090x_PR78:
1015 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x20) < 0)
1016 goto err;
1017 break;
1018
1019 default:
1020 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x2f) < 0) /* all */
1021 goto err;
1022 break;
1023 }
1024 break;
1025 case STV090x_SEARCH_DSS:
1026 if (STV090x_WRITE_DEMOD(state, FECM, 0x80) < 0)
1027 goto err;
1028 switch (state->fec) {
1029 case STV090x_PR12:
1030 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
1031 goto err;
1032 break;
1033
1034 case STV090x_PR23:
1035 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
1036 goto err;
1037 break;
1038
1039 case STV090x_PR67:
1040 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x10) < 0)
1041 goto err;
1042 break;
1043
1044 default:
1045 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x13) < 0) /* 1/2, 2/3, 6/7 */
1046 goto err;
1047 break;
1048 }
1049 break;
1050 default:
1051 break;
1052 }
1053 return 0;
1054err:
1055 dprintk(FE_ERROR, 1, "I/O error");
1056 return -1;
1057}
1058
1059static int stv090x_stop_modcod(struct stv090x_state *state)
1060{
1061 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1062 goto err;
1063 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
1064 goto err;
1065 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
1066 goto err;
1067 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
1068 goto err;
1069 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
1070 goto err;
1071 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
1072 goto err;
1073 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
1074 goto err;
1075 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xff) < 0)
1076 goto err;
1077 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xff) < 0)
1078 goto err;
1079 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xff) < 0)
1080 goto err;
1081 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xff) < 0)
1082 goto err;
1083 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xff) < 0)
1084 goto err;
1085 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xff) < 0)
1086 goto err;
1087 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xff) < 0)
1088 goto err;
1089 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
1090 goto err;
1091 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xff) < 0)
1092 goto err;
1093 return 0;
1094err:
1095 dprintk(FE_ERROR, 1, "I/O error");
1096 return -1;
1097}
1098
1099static int stv090x_activate_modcod(struct stv090x_state *state)
1100{
Manu Abraham27d40322009-05-02 18:26:58 -03001101 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1102 goto err;
1103 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xfc) < 0)
1104 goto err;
1105 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xcc) < 0)
1106 goto err;
1107 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xcc) < 0)
1108 goto err;
1109 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xcc) < 0)
1110 goto err;
1111 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xcc) < 0)
1112 goto err;
1113 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xcc) < 0)
1114 goto err;
1115 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
1116 goto err;
1117 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
1118 goto err;
1119 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
1120 goto err;
1121 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
1122 goto err;
1123 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
1124 goto err;
1125 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
1126 goto err;
1127 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
1128 goto err;
1129 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xcc) < 0)
1130 goto err;
1131 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
1132 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001133
Manu Abrahame415c682009-04-06 15:45:20 -03001134 return 0;
1135err:
1136 dprintk(FE_ERROR, 1, "I/O error");
1137 return -1;
1138}
1139
Manu Abraham27d40322009-05-02 18:26:58 -03001140static int stv090x_activate_modcod_single(struct stv090x_state *state)
1141{
1142
1143 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1144 goto err;
1145 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xf0) < 0)
1146 goto err;
1147 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0x00) < 0)
1148 goto err;
1149 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0x00) < 0)
1150 goto err;
1151 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0x00) < 0)
1152 goto err;
1153 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0x00) < 0)
1154 goto err;
1155 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0x00) < 0)
1156 goto err;
1157 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0x00) < 0)
1158 goto err;
1159 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0x00) < 0)
1160 goto err;
1161 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0x00) < 0)
1162 goto err;
1163 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0x00) < 0)
1164 goto err;
1165 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0x00) < 0)
1166 goto err;
1167 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0x00) < 0)
1168 goto err;
1169 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0x00) < 0)
1170 goto err;
1171 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0x00) < 0)
1172 goto err;
1173 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0x0f) < 0)
1174 goto err;
1175
1176 return 0;
1177
1178err:
1179 dprintk(FE_ERROR, 1, "I/O error");
1180 return -1;
1181}
1182
Manu Abrahame415c682009-04-06 15:45:20 -03001183static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
1184{
1185 u32 reg;
1186
1187 switch (state->demod) {
1188 case STV090x_DEMODULATOR_0:
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001189 mutex_lock(&state->internal->demod_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03001190 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1191 STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, enable);
1192 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1193 goto err;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001194 mutex_unlock(&state->internal->demod_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03001195 break;
1196
1197 case STV090x_DEMODULATOR_1:
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001198 mutex_lock(&state->internal->demod_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03001199 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1200 STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, enable);
1201 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1202 goto err;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001203 mutex_unlock(&state->internal->demod_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03001204 break;
1205
1206 default:
1207 dprintk(FE_ERROR, 1, "Wrong demodulator!");
1208 break;
1209 }
1210 return 0;
1211err:
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001212 mutex_unlock(&state->internal->demod_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03001213 dprintk(FE_ERROR, 1, "I/O error");
1214 return -1;
1215}
1216
Manu Abraham27d40322009-05-02 18:26:58 -03001217static int stv090x_dvbs_track_crl(struct stv090x_state *state)
1218{
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001219 if (state->internal->dev_ver >= 0x30) {
Manu Abraham27d40322009-05-02 18:26:58 -03001220 /* Set ACLC BCLC optimised value vs SR */
1221 if (state->srate >= 15000000) {
1222 if (STV090x_WRITE_DEMOD(state, ACLC, 0x2b) < 0)
1223 goto err;
1224 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1a) < 0)
1225 goto err;
1226 } else if ((state->srate >= 7000000) && (15000000 > state->srate)) {
1227 if (STV090x_WRITE_DEMOD(state, ACLC, 0x0c) < 0)
1228 goto err;
1229 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1b) < 0)
1230 goto err;
1231 } else if (state->srate < 7000000) {
1232 if (STV090x_WRITE_DEMOD(state, ACLC, 0x2c) < 0)
1233 goto err;
1234 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1c) < 0)
1235 goto err;
1236 }
1237
1238 } else {
1239 /* Cut 2.0 */
1240 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
1241 goto err;
1242 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1243 goto err;
1244 }
1245 return 0;
1246err:
1247 dprintk(FE_ERROR, 1, "I/O error");
1248 return -1;
1249}
1250
Manu Abrahame415c682009-04-06 15:45:20 -03001251static int stv090x_delivery_search(struct stv090x_state *state)
1252{
1253 u32 reg;
1254
1255 switch (state->search_mode) {
1256 case STV090x_SEARCH_DVBS1:
1257 case STV090x_SEARCH_DSS:
1258 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1259 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1260 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1261 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1262 goto err;
1263
Manu Abraham27d40322009-05-02 18:26:58 -03001264 /* Activate Viterbi decoder in legacy search,
1265 * do not use FRESVIT1, might impact VITERBI2
1266 */
Manu Abrahame415c682009-04-06 15:45:20 -03001267 if (stv090x_vitclk_ctl(state, 0) < 0)
1268 goto err;
1269
Manu Abraham27d40322009-05-02 18:26:58 -03001270 if (stv090x_dvbs_track_crl(state) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001271 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001272
Manu Abrahame415c682009-04-06 15:45:20 -03001273 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x22) < 0) /* disable DVB-S2 */
1274 goto err;
1275
Manu Abraham27d40322009-05-02 18:26:58 -03001276 if (stv090x_set_vit_thacq(state) < 0)
1277 goto err;
1278 if (stv090x_set_viterbi(state) < 0)
1279 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001280 break;
1281
1282 case STV090x_SEARCH_DVBS2:
1283 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1284 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
1285 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1286 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1287 goto err;
1288 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1289 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
1290 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1291 goto err;
1292
1293 if (stv090x_vitclk_ctl(state, 1) < 0)
1294 goto err;
1295
1296 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0) /* stop DVB-S CR loop */
1297 goto err;
1298 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1299 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001300
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001301 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001302 /* enable S2 carrier loop */
1303 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1304 goto err;
1305 } else {
1306 /* > Cut 3: Stop carrier 3 */
1307 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1308 goto err;
1309 }
Manu Abrahame415c682009-04-06 15:45:20 -03001310
1311 if (state->demod_mode != STV090x_SINGLE) {
Manu Abraham27d40322009-05-02 18:26:58 -03001312 /* Cut 2: enable link during search */
1313 if (stv090x_activate_modcod(state) < 0)
1314 goto err;
1315 } else {
1316 /* Single demodulator
1317 * Authorize SHORT and LONG frames,
1318 * QPSK, 8PSK, 16APSK and 32APSK
1319 */
1320 if (stv090x_activate_modcod_single(state) < 0)
1321 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001322 }
Manu Abraham27d40322009-05-02 18:26:58 -03001323
Andreas Regel7b035da2009-11-13 18:20:54 -03001324 if (stv090x_set_vit_thtracq(state) < 0)
1325 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001326 break;
1327
1328 case STV090x_SEARCH_AUTO:
1329 default:
Manu Abraham27d40322009-05-02 18:26:58 -03001330 /* enable DVB-S2 and DVB-S2 in Auto MODE */
Manu Abrahame415c682009-04-06 15:45:20 -03001331 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
Andreas Regelb79c6df2010-01-05 19:18:52 -03001332 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
1333 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1334 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1335 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001336 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1337 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
1338 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1339 goto err;
1340
Andreas Regel4e58a682009-04-16 08:40:36 -03001341 if (stv090x_vitclk_ctl(state, 0) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001342 goto err;
1343
Manu Abraham27d40322009-05-02 18:26:58 -03001344 if (stv090x_dvbs_track_crl(state) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001345 goto err;
1346
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001347 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001348 /* enable S2 carrier loop */
1349 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1350 goto err;
1351 } else {
1352 /* > Cut 3: Stop carrier 3 */
1353 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1354 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001355 }
Manu Abraham27d40322009-05-02 18:26:58 -03001356
1357 if (state->demod_mode != STV090x_SINGLE) {
1358 /* Cut 2: enable link during search */
1359 if (stv090x_activate_modcod(state) < 0)
1360 goto err;
1361 } else {
1362 /* Single demodulator
1363 * Authorize SHORT and LONG frames,
1364 * QPSK, 8PSK, 16APSK and 32APSK
1365 */
1366 if (stv090x_activate_modcod_single(state) < 0)
1367 goto err;
1368 }
1369
Andreas Regel7b035da2009-11-13 18:20:54 -03001370 if (stv090x_set_vit_thacq(state) < 0)
1371 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001372
1373 if (stv090x_set_viterbi(state) < 0)
1374 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001375 break;
1376 }
1377 return 0;
1378err:
1379 dprintk(FE_ERROR, 1, "I/O error");
1380 return -1;
1381}
1382
1383static int stv090x_start_search(struct stv090x_state *state)
1384{
Manu Abraham27d40322009-05-02 18:26:58 -03001385 u32 reg, freq_abs;
1386 s16 freq;
Manu Abrahame415c682009-04-06 15:45:20 -03001387
Manu Abraham27d40322009-05-02 18:26:58 -03001388 /* Reset demodulator */
Manu Abrahame415c682009-04-06 15:45:20 -03001389 reg = STV090x_READ_DEMOD(state, DMDISTATE);
1390 STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f);
1391 if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
1392 goto err;
1393
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001394 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001395 if (state->srate <= 5000000) {
1396 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0)
1397 goto err;
1398 if (STV090x_WRITE_DEMOD(state, CFRUP1, 0x0f) < 0)
1399 goto err;
Andreas Regelb671a8d2009-11-13 18:15:27 -03001400 if (STV090x_WRITE_DEMOD(state, CFRUP0, 0xff) < 0)
Manu Abraham27d40322009-05-02 18:26:58 -03001401 goto err;
1402 if (STV090x_WRITE_DEMOD(state, CFRLOW1, 0xf0) < 0)
1403 goto err;
1404 if (STV090x_WRITE_DEMOD(state, CFRLOW0, 0x00) < 0)
1405 goto err;
1406
1407 /*enlarge the timing bandwith for Low SR*/
1408 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0)
1409 goto err;
1410 } else {
1411 /* If the symbol rate is >5 Msps
1412 Set The carrier search up and low to auto mode */
1413 if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
1414 goto err;
1415 /*reduce the timing bandwith for high SR*/
1416 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
1417 goto err;
1418 }
1419 } else {
1420 /* >= Cut 3 */
1421 if (state->srate <= 5000000) {
1422 /* enlarge the timing bandwith for Low SR */
1423 STV090x_WRITE_DEMOD(state, RTCS2, 0x68);
1424 } else {
1425 /* reduce timing bandwith for high SR */
1426 STV090x_WRITE_DEMOD(state, RTCS2, 0x44);
1427 }
1428
1429 /* Set CFR min and max to manual mode */
1430 STV090x_WRITE_DEMOD(state, CARCFG, 0x46);
1431
1432 if (state->algo == STV090x_WARM_SEARCH) {
1433 /* WARM Start
1434 * CFR min = -1MHz,
1435 * CFR max = +1MHz
1436 */
1437 freq_abs = 1000 << 16;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001438 freq_abs /= (state->internal->mclk / 1000);
Manu Abraham27d40322009-05-02 18:26:58 -03001439 freq = (s16) freq_abs;
1440 } else {
1441 /* COLD Start
1442 * CFR min =- (SearchRange / 2 + 600KHz)
1443 * CFR max = +(SearchRange / 2 + 600KHz)
1444 * (600KHz for the tuner step size)
1445 */
1446 freq_abs = (state->search_range / 2000) + 600;
1447 freq_abs = freq_abs << 16;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001448 freq_abs /= (state->internal->mclk / 1000);
Manu Abraham27d40322009-05-02 18:26:58 -03001449 freq = (s16) freq_abs;
1450 }
1451
1452 if (STV090x_WRITE_DEMOD(state, CFRUP1, MSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001453 goto err;
Andreas Regelb671a8d2009-11-13 18:15:27 -03001454 if (STV090x_WRITE_DEMOD(state, CFRUP0, LSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001455 goto err;
1456
Manu Abraham27d40322009-05-02 18:26:58 -03001457 freq *= -1;
1458
1459 if (STV090x_WRITE_DEMOD(state, CFRLOW1, MSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001460 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001461 if (STV090x_WRITE_DEMOD(state, CFRLOW0, LSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001462 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001463
Manu Abrahame415c682009-04-06 15:45:20 -03001464 }
Manu Abraham27d40322009-05-02 18:26:58 -03001465
Manu Abrahame415c682009-04-06 15:45:20 -03001466 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0) < 0)
1467 goto err;
1468 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0) < 0)
1469 goto err;
1470
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001471 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03001472 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1473 goto err;
1474 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
1475 goto err;
1476
1477 if ((state->search_mode == STV090x_DVBS1) ||
1478 (state->search_mode == STV090x_DSS) ||
1479 (state->search_mode == STV090x_SEARCH_AUTO)) {
1480
1481 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
1482 goto err;
1483 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0)
1484 goto err;
1485 }
1486 }
1487
1488 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00) < 0)
1489 goto err;
1490 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xe0) < 0)
1491 goto err;
1492 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xc0) < 0)
1493 goto err;
1494
1495 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1496 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
1497 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
1498 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1499 goto err;
1500 reg = STV090x_READ_DEMOD(state, DMDCFG2);
1501 STV090x_SETFIELD_Px(reg, S1S2_SEQUENTIAL_FIELD, 0x0);
1502 if (STV090x_WRITE_DEMOD(state, DMDCFG2, reg) < 0)
1503 goto err;
1504
Andreas Regel7b035da2009-11-13 18:20:54 -03001505 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0)
1506 goto err;
1507
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001508 if (state->internal->dev_ver >= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001509 /*Frequency offset detector setting*/
1510 if (state->srate < 2000000) {
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001511 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001512 /* Cut 2 */
1513 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x39) < 0)
1514 goto err;
1515 } else {
Andreas Regel7b035da2009-11-13 18:20:54 -03001516 /* Cut 3 */
Manu Abraham27d40322009-05-02 18:26:58 -03001517 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x89) < 0)
1518 goto err;
1519 }
1520 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x40) < 0)
1521 goto err;
Andreas Regela4978a82009-11-13 18:17:45 -03001522 } else if (state->srate < 10000000) {
Manu Abrahame415c682009-04-06 15:45:20 -03001523 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4c) < 0)
1524 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001525 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1526 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001527 } else {
1528 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4b) < 0)
1529 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001530 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1531 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001532 }
1533 } else {
1534 if (state->srate < 10000000) {
1535 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
1536 goto err;
1537 } else {
1538 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
1539 goto err;
1540 }
1541 }
1542
1543 switch (state->algo) {
Manu Abraham27d40322009-05-02 18:26:58 -03001544 case STV090x_WARM_SEARCH:
1545 /* The symbol rate and the exact
1546 * carrier Frequency are known
1547 */
Manu Abrahame415c682009-04-06 15:45:20 -03001548 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
1549 goto err;
1550 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
1551 goto err;
1552 break;
1553
Manu Abraham27d40322009-05-02 18:26:58 -03001554 case STV090x_COLD_SEARCH:
1555 /* The symbol rate is known */
Manu Abrahame415c682009-04-06 15:45:20 -03001556 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
1557 goto err;
1558 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
1559 goto err;
1560 break;
1561
1562 default:
1563 break;
1564 }
1565 return 0;
1566err:
1567 dprintk(FE_ERROR, 1, "I/O error");
1568 return -1;
1569}
1570
1571static int stv090x_get_agc2_min_level(struct stv090x_state *state)
1572{
Andreas Regelb4a42482009-11-13 18:16:44 -03001573 u32 agc2_min = 0xffff, agc2 = 0, freq_init, freq_step, reg;
Manu Abrahame415c682009-04-06 15:45:20 -03001574 s32 i, j, steps, dir;
1575
1576 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1577 goto err;
1578 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
Andreas Regel7b035da2009-11-13 18:20:54 -03001579 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
1580 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
Manu Abrahame415c682009-04-06 15:45:20 -03001581 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1582 goto err;
1583
1584 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0) /* SR = 65 Msps Max */
1585 goto err;
1586 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1587 goto err;
1588 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0) /* SR= 400 ksps Min */
1589 goto err;
1590 if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1591 goto err;
1592 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0) /* stop acq @ coarse carrier state */
1593 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001594 if (stv090x_set_srate(state, 1000000) < 0)
1595 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001596
Andreas Regel7b035da2009-11-13 18:20:54 -03001597 steps = state->search_range / 1000000;
1598 if (steps <= 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001599 steps = 1;
1600
1601 dir = 1;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001602 freq_step = (1000000 * 256) / (state->internal->mclk / 256);
Manu Abrahame415c682009-04-06 15:45:20 -03001603 freq_init = 0;
1604
1605 for (i = 0; i < steps; i++) {
1606 if (dir > 0)
1607 freq_init = freq_init + (freq_step * i);
1608 else
1609 freq_init = freq_init - (freq_step * i);
1610
Andreas Regelb671a8d2009-11-13 18:15:27 -03001611 dir *= -1;
Manu Abrahame415c682009-04-06 15:45:20 -03001612
1613 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod RESET */
1614 goto err;
1615 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_init >> 8) & 0xff) < 0)
1616 goto err;
1617 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_init & 0xff) < 0)
1618 goto err;
1619 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x58) < 0) /* Demod RESET */
1620 goto err;
1621 msleep(10);
Andreas Regelb4a42482009-11-13 18:16:44 -03001622
1623 agc2 = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03001624 for (j = 0; j < 10; j++) {
Andreas Regelb4a42482009-11-13 18:16:44 -03001625 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1626 STV090x_READ_DEMOD(state, AGC2I0);
Manu Abrahame415c682009-04-06 15:45:20 -03001627 }
1628 agc2 /= 10;
Andreas Regelb4a42482009-11-13 18:16:44 -03001629 if (agc2 < agc2_min)
Manu Abrahame415c682009-04-06 15:45:20 -03001630 agc2_min = agc2;
1631 }
1632
1633 return agc2_min;
1634err:
1635 dprintk(FE_ERROR, 1, "I/O error");
1636 return -1;
1637}
1638
1639static u32 stv090x_get_srate(struct stv090x_state *state, u32 clk)
1640{
1641 u8 r3, r2, r1, r0;
1642 s32 srate, int_1, int_2, tmp_1, tmp_2;
Manu Abrahame415c682009-04-06 15:45:20 -03001643
1644 r3 = STV090x_READ_DEMOD(state, SFR3);
1645 r2 = STV090x_READ_DEMOD(state, SFR2);
1646 r1 = STV090x_READ_DEMOD(state, SFR1);
1647 r0 = STV090x_READ_DEMOD(state, SFR0);
1648
1649 srate = ((r3 << 24) | (r2 << 16) | (r1 << 8) | r0);
1650
Manu Abrahamf430fff2009-04-08 20:18:50 -03001651 int_1 = clk >> 16;
1652 int_2 = srate >> 16;
Manu Abrahame415c682009-04-06 15:45:20 -03001653
Manu Abrahamf430fff2009-04-08 20:18:50 -03001654 tmp_1 = clk % 0x10000;
1655 tmp_2 = srate % 0x10000;
Manu Abrahame415c682009-04-06 15:45:20 -03001656
1657 srate = (int_1 * int_2) +
Manu Abrahamf430fff2009-04-08 20:18:50 -03001658 ((int_1 * tmp_2) >> 16) +
1659 ((int_2 * tmp_1) >> 16);
Manu Abrahame415c682009-04-06 15:45:20 -03001660
1661 return srate;
1662}
1663
1664static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
1665{
1666 struct dvb_frontend *fe = &state->frontend;
1667
1668 int tmg_lock = 0, i;
1669 s32 tmg_cpt = 0, dir = 1, steps, cur_step = 0, freq;
1670 u32 srate_coarse = 0, agc2 = 0, car_step = 1200, reg;
Andreas Regelb4a42482009-11-13 18:16:44 -03001671 u32 agc2th;
1672
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001673 if (state->internal->dev_ver >= 0x30)
Andreas Regelb4a42482009-11-13 18:16:44 -03001674 agc2th = 0x2e00;
1675 else
1676 agc2th = 0x1f00;
Manu Abrahame415c682009-04-06 15:45:20 -03001677
1678 reg = STV090x_READ_DEMOD(state, DMDISTATE);
1679 STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f); /* Demod RESET */
1680 if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
1681 goto err;
1682 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0x12) < 0)
1683 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001684 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0)
1685 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001686 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xf0) < 0)
1687 goto err;
1688 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xe0) < 0)
1689 goto err;
1690 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1691 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 1);
Andreas Regel7b035da2009-11-13 18:20:54 -03001692 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
Manu Abrahame415c682009-04-06 15:45:20 -03001693 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1694 goto err;
1695
1696 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0)
1697 goto err;
1698 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1699 goto err;
1700 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0)
1701 goto err;
1702 if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1703 goto err;
1704 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0)
1705 goto err;
Andreas Regelb4a42482009-11-13 18:16:44 -03001706 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x50) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001707 goto err;
1708
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001709 if (state->internal->dev_ver >= 0x30) {
Manu Abraham27d40322009-05-02 18:26:58 -03001710 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x99) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001711 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001712 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x98) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001713 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001714
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001715 } else if (state->internal->dev_ver >= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001716 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001717 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001718 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001719 goto err;
1720 }
1721
1722 if (state->srate <= 2000000)
1723 car_step = 1000;
1724 else if (state->srate <= 5000000)
1725 car_step = 2000;
1726 else if (state->srate <= 12000000)
1727 car_step = 3000;
1728 else
1729 car_step = 5000;
1730
1731 steps = -1 + ((state->search_range / 1000) / car_step);
1732 steps /= 2;
1733 steps = (2 * steps) + 1;
1734 if (steps < 0)
1735 steps = 1;
1736 else if (steps > 10) {
1737 steps = 11;
1738 car_step = (state->search_range / 1000) / 10;
1739 }
1740 cur_step = 0;
1741 dir = 1;
1742 freq = state->frequency;
1743
1744 while ((!tmg_lock) && (cur_step < steps)) {
1745 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5f) < 0) /* Demod RESET */
1746 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001747 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
1748 goto err;
1749 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
1750 goto err;
1751 if (STV090x_WRITE_DEMOD(state, SFRINIT1, 0x00) < 0)
1752 goto err;
1753 if (STV090x_WRITE_DEMOD(state, SFRINIT0, 0x00) < 0)
1754 goto err;
1755 /* trigger acquisition */
1756 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x40) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001757 goto err;
1758 msleep(50);
1759 for (i = 0; i < 10; i++) {
1760 reg = STV090x_READ_DEMOD(state, DSTATUS);
1761 if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
1762 tmg_cpt++;
Andreas Regelb4a42482009-11-13 18:16:44 -03001763 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1764 STV090x_READ_DEMOD(state, AGC2I0);
Manu Abrahame415c682009-04-06 15:45:20 -03001765 }
1766 agc2 /= 10;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001767 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
Manu Abrahame415c682009-04-06 15:45:20 -03001768 cur_step++;
1769 dir *= -1;
Andreas Regelb4a42482009-11-13 18:16:44 -03001770 if ((tmg_cpt >= 5) && (agc2 < agc2th) &&
1771 (srate_coarse < 50000000) && (srate_coarse > 850000))
Manu Abrahame415c682009-04-06 15:45:20 -03001772 tmg_lock = 1;
1773 else if (cur_step < steps) {
1774 if (dir > 0)
1775 freq += cur_step * car_step;
1776 else
1777 freq -= cur_step * car_step;
1778
1779 /* Setup tuner */
Manu Abraham27d40322009-05-02 18:26:58 -03001780 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
1781 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001782
Manu Abraham27d40322009-05-02 18:26:58 -03001783 if (state->config->tuner_set_frequency) {
Andreas Regela4978a82009-11-13 18:17:45 -03001784 if (state->config->tuner_set_frequency(fe, freq) < 0)
Manu Abraham27d40322009-05-02 18:26:58 -03001785 goto err;
1786 }
Manu Abrahame415c682009-04-06 15:45:20 -03001787
Manu Abraham27d40322009-05-02 18:26:58 -03001788 if (state->config->tuner_set_bandwidth) {
1789 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
1790 goto err;
1791 }
Manu Abrahame415c682009-04-06 15:45:20 -03001792
Manu Abraham27d40322009-05-02 18:26:58 -03001793 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
1794 goto err;
1795
Manu Abrahame415c682009-04-06 15:45:20 -03001796 msleep(50);
Manu Abrahame415c682009-04-06 15:45:20 -03001797
Manu Abraham27d40322009-05-02 18:26:58 -03001798 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
1799 goto err;
1800
1801 if (state->config->tuner_get_status) {
1802 if (state->config->tuner_get_status(fe, &reg) < 0)
1803 goto err;
1804 }
Manu Abrahame415c682009-04-06 15:45:20 -03001805
1806 if (reg)
1807 dprintk(FE_DEBUG, 1, "Tuner phase locked");
1808 else
1809 dprintk(FE_DEBUG, 1, "Tuner unlocked");
1810
Manu Abraham27d40322009-05-02 18:26:58 -03001811 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
1812 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001813
1814 }
1815 }
1816 if (!tmg_lock)
1817 srate_coarse = 0;
1818 else
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001819 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
Manu Abrahame415c682009-04-06 15:45:20 -03001820
1821 return srate_coarse;
1822err:
1823 dprintk(FE_ERROR, 1, "I/O error");
1824 return -1;
1825}
1826
1827static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
1828{
1829 u32 srate_coarse, freq_coarse, sym, reg;
1830
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001831 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
Manu Abrahame415c682009-04-06 15:45:20 -03001832 freq_coarse = STV090x_READ_DEMOD(state, CFR2) << 8;
1833 freq_coarse |= STV090x_READ_DEMOD(state, CFR1);
1834 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1835
1836 if (sym < state->srate)
1837 srate_coarse = 0;
1838 else {
1839 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0) /* Demod RESET */
1840 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001841 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001842 goto err;
1843 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
1844 goto err;
1845 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
1846 goto err;
1847 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
1848 goto err;
1849 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1850 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
1851 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1852 goto err;
1853
Andreas Regelb4a42482009-11-13 18:16:44 -03001854 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1855 goto err;
1856
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001857 if (state->internal->dev_ver >= 0x30) {
Manu Abraham27d40322009-05-02 18:26:58 -03001858 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x79) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001859 goto err;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001860 } else if (state->internal->dev_ver >= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001861 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001862 goto err;
1863 }
1864
1865 if (srate_coarse > 3000000) {
1866 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1867 sym = (sym / 1000) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001868 sym /= (state->internal->mclk / 1000);
Manu Abrahame415c682009-04-06 15:45:20 -03001869 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1870 goto err;
1871 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1872 goto err;
1873 sym = 10 * (srate_coarse / 13); /* SFRLOW = SFR - 30% */
1874 sym = (sym / 1000) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001875 sym /= (state->internal->mclk / 1000);
Manu Abrahame415c682009-04-06 15:45:20 -03001876 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1877 goto err;
1878 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1879 goto err;
1880 sym = (srate_coarse / 1000) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001881 sym /= (state->internal->mclk / 1000);
Manu Abrahame415c682009-04-06 15:45:20 -03001882 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1883 goto err;
1884 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
1885 goto err;
1886 } else {
1887 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1888 sym = (sym / 100) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001889 sym /= (state->internal->mclk / 100);
Manu Abrahame415c682009-04-06 15:45:20 -03001890 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1891 goto err;
1892 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1893 goto err;
1894 sym = 10 * (srate_coarse / 14); /* SFRLOW = SFR - 30% */
1895 sym = (sym / 100) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001896 sym /= (state->internal->mclk / 100);
Manu Abrahame415c682009-04-06 15:45:20 -03001897 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1898 goto err;
1899 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1900 goto err;
1901 sym = (srate_coarse / 100) * 65536;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001902 sym /= (state->internal->mclk / 100);
Manu Abrahame415c682009-04-06 15:45:20 -03001903 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1904 goto err;
1905 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
1906 goto err;
1907 }
1908 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
1909 goto err;
1910 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_coarse >> 8) & 0xff) < 0)
1911 goto err;
1912 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_coarse & 0xff) < 0)
1913 goto err;
1914 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0) /* trigger acquisition */
1915 goto err;
1916 }
1917
1918 return srate_coarse;
1919
1920err:
1921 dprintk(FE_ERROR, 1, "I/O error");
1922 return -1;
1923}
1924
1925static int stv090x_get_dmdlock(struct stv090x_state *state, s32 timeout)
1926{
1927 s32 timer = 0, lock = 0;
1928 u32 reg;
1929 u8 stat;
1930
1931 while ((timer < timeout) && (!lock)) {
1932 reg = STV090x_READ_DEMOD(state, DMDSTATE);
1933 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
1934
1935 switch (stat) {
1936 case 0: /* searching */
1937 case 1: /* first PLH detected */
1938 default:
1939 dprintk(FE_DEBUG, 1, "Demodulator searching ..");
1940 lock = 0;
1941 break;
1942 case 2: /* DVB-S2 mode */
1943 case 3: /* DVB-S1/legacy mode */
1944 reg = STV090x_READ_DEMOD(state, DSTATUS);
1945 lock = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
1946 break;
1947 }
1948
1949 if (!lock)
1950 msleep(10);
1951 else
1952 dprintk(FE_DEBUG, 1, "Demodulator acquired LOCK");
1953
1954 timer += 10;
1955 }
1956 return lock;
1957}
1958
1959static int stv090x_blind_search(struct stv090x_state *state)
1960{
1961 u32 agc2, reg, srate_coarse;
Andreas Regela4978a82009-11-13 18:17:45 -03001962 s32 cpt_fail, agc2_ovflw, i;
Manu Abrahame415c682009-04-06 15:45:20 -03001963 u8 k_ref, k_max, k_min;
1964 int coarse_fail, lock;
1965
Andreas Regel7b035da2009-11-13 18:20:54 -03001966 k_max = 110;
1967 k_min = 10;
Manu Abrahame415c682009-04-06 15:45:20 -03001968
1969 agc2 = stv090x_get_agc2_min_level(state);
1970
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001971 if (agc2 > STV090x_SEARCH_AGC2_TH(state->internal->dev_ver)) {
Manu Abrahame415c682009-04-06 15:45:20 -03001972 lock = 0;
1973 } else {
Manu Abraham27d40322009-05-02 18:26:58 -03001974
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001975 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03001976 if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001977 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001978 } else {
1979 /* > Cut 3 */
1980 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x06) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001981 goto err;
1982 }
1983
Manu Abrahame415c682009-04-06 15:45:20 -03001984 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
1985 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001986
Andreas Regel97f7a2a2010-01-05 19:19:43 -03001987 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03001988 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1989 goto err;
1990 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
1991 goto err;
1992 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
1993 goto err;
1994 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0) /* set viterbi hysteresis */
1995 goto err;
1996 }
1997
1998 k_ref = k_max;
1999 do {
2000 if (STV090x_WRITE_DEMOD(state, KREFTMG, k_ref) < 0)
2001 goto err;
2002 if (stv090x_srate_srch_coarse(state) != 0) {
2003 srate_coarse = stv090x_srate_srch_fine(state);
2004 if (srate_coarse != 0) {
2005 stv090x_get_lock_tmg(state);
Andreas Regela4978a82009-11-13 18:17:45 -03002006 lock = stv090x_get_dmdlock(state,
2007 state->DemodTimeout);
Manu Abrahame415c682009-04-06 15:45:20 -03002008 } else {
2009 lock = 0;
2010 }
2011 } else {
2012 cpt_fail = 0;
2013 agc2_ovflw = 0;
2014 for (i = 0; i < 10; i++) {
Andreas Regelb4a42482009-11-13 18:16:44 -03002015 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
2016 STV090x_READ_DEMOD(state, AGC2I0);
Manu Abrahame415c682009-04-06 15:45:20 -03002017 if (agc2 >= 0xff00)
2018 agc2_ovflw++;
2019 reg = STV090x_READ_DEMOD(state, DSTATUS2);
2020 if ((STV090x_GETFIELD_Px(reg, CFR_OVERFLOW_FIELD) == 0x01) &&
2021 (STV090x_GETFIELD_Px(reg, DEMOD_DELOCK_FIELD) == 0x01))
2022
2023 cpt_fail++;
2024 }
2025 if ((cpt_fail > 7) || (agc2_ovflw > 7))
2026 coarse_fail = 1;
2027
2028 lock = 0;
2029 }
Andreas Regel7b035da2009-11-13 18:20:54 -03002030 k_ref -= 20;
Manu Abrahame415c682009-04-06 15:45:20 -03002031 } while ((k_ref >= k_min) && (!lock) && (!coarse_fail));
2032 }
2033
2034 return lock;
2035
2036err:
2037 dprintk(FE_ERROR, 1, "I/O error");
2038 return -1;
2039}
2040
2041static int stv090x_chk_tmg(struct stv090x_state *state)
2042{
2043 u32 reg;
Manu Abraham27d40322009-05-02 18:26:58 -03002044 s32 tmg_cpt = 0, i;
Manu Abrahame415c682009-04-06 15:45:20 -03002045 u8 freq, tmg_thh, tmg_thl;
2046 int tmg_lock;
2047
2048 freq = STV090x_READ_DEMOD(state, CARFREQ);
2049 tmg_thh = STV090x_READ_DEMOD(state, TMGTHRISE);
2050 tmg_thl = STV090x_READ_DEMOD(state, TMGTHFALL);
2051 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
2052 goto err;
2053 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
2054 goto err;
2055
2056 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2057 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00); /* stop carrier offset search */
2058 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2059 goto err;
2060 if (STV090x_WRITE_DEMOD(state, RTC, 0x80) < 0)
2061 goto err;
2062
2063 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x40) < 0)
2064 goto err;
2065 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x00) < 0)
2066 goto err;
2067
2068 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0) /* set car ofset to 0 */
2069 goto err;
2070 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
2071 goto err;
2072 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x65) < 0)
2073 goto err;
2074
2075 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* trigger acquisition */
2076 goto err;
2077 msleep(10);
2078
2079 for (i = 0; i < 10; i++) {
2080 reg = STV090x_READ_DEMOD(state, DSTATUS);
2081 if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
2082 tmg_cpt++;
2083 msleep(1);
2084 }
2085 if (tmg_cpt >= 3)
2086 tmg_lock = 1;
2087
2088 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
2089 goto err;
2090 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0) /* DVB-S1 timing */
2091 goto err;
2092 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0) /* DVB-S2 timing */
2093 goto err;
2094
2095 if (STV090x_WRITE_DEMOD(state, CARFREQ, freq) < 0)
2096 goto err;
2097 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, tmg_thh) < 0)
2098 goto err;
2099 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, tmg_thl) < 0)
2100 goto err;
2101
2102 return tmg_lock;
2103
2104err:
2105 dprintk(FE_ERROR, 1, "I/O error");
2106 return -1;
2107}
2108
2109static int stv090x_get_coldlock(struct stv090x_state *state, s32 timeout_dmd)
2110{
2111 struct dvb_frontend *fe = &state->frontend;
2112
2113 u32 reg;
2114 s32 car_step, steps, cur_step, dir, freq, timeout_lock;
2115 int lock = 0;
2116
2117 if (state->srate >= 10000000)
2118 timeout_lock = timeout_dmd / 3;
2119 else
2120 timeout_lock = timeout_dmd / 2;
2121
2122 lock = stv090x_get_dmdlock(state, timeout_lock); /* cold start wait */
2123 if (!lock) {
2124 if (state->srate >= 10000000) {
2125 if (stv090x_chk_tmg(state)) {
2126 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2127 goto err;
2128 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2129 goto err;
2130 lock = stv090x_get_dmdlock(state, timeout_dmd);
2131 } else {
2132 lock = 0;
2133 }
2134 } else {
2135 if (state->srate <= 4000000)
2136 car_step = 1000;
2137 else if (state->srate <= 7000000)
2138 car_step = 2000;
2139 else if (state->srate <= 10000000)
2140 car_step = 3000;
2141 else
2142 car_step = 5000;
2143
2144 steps = (state->search_range / 1000) / car_step;
2145 steps /= 2;
2146 steps = 2 * (steps + 1);
2147 if (steps < 0)
2148 steps = 2;
2149 else if (steps > 12)
2150 steps = 12;
2151
2152 cur_step = 1;
2153 dir = 1;
2154
2155 if (!lock) {
2156 freq = state->frequency;
2157 state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + state->srate;
2158 while ((cur_step <= steps) && (!lock)) {
2159 if (dir > 0)
2160 freq += cur_step * car_step;
2161 else
2162 freq -= cur_step * car_step;
2163
2164 /* Setup tuner */
Manu Abraham27d40322009-05-02 18:26:58 -03002165 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2166 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002167
Manu Abraham27d40322009-05-02 18:26:58 -03002168 if (state->config->tuner_set_frequency) {
Andreas Regela4978a82009-11-13 18:17:45 -03002169 if (state->config->tuner_set_frequency(fe, freq) < 0)
Manu Abraham27d40322009-05-02 18:26:58 -03002170 goto err;
2171 }
Manu Abrahame415c682009-04-06 15:45:20 -03002172
Manu Abraham27d40322009-05-02 18:26:58 -03002173 if (state->config->tuner_set_bandwidth) {
2174 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
2175 goto err;
2176 }
Manu Abrahame415c682009-04-06 15:45:20 -03002177
Manu Abraham27d40322009-05-02 18:26:58 -03002178 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2179 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002180
2181 msleep(50);
2182
Manu Abraham27d40322009-05-02 18:26:58 -03002183 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2184 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002185
Manu Abraham27d40322009-05-02 18:26:58 -03002186 if (state->config->tuner_get_status) {
2187 if (state->config->tuner_get_status(fe, &reg) < 0)
2188 goto err;
2189 }
Manu Abrahame415c682009-04-06 15:45:20 -03002190
2191 if (reg)
2192 dprintk(FE_DEBUG, 1, "Tuner phase locked");
2193 else
2194 dprintk(FE_DEBUG, 1, "Tuner unlocked");
2195
Manu Abraham27d40322009-05-02 18:26:58 -03002196 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2197 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002198
2199 STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c);
Manu Abrahame415c682009-04-06 15:45:20 -03002200 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
2201 goto err;
2202 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
2203 goto err;
2204 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2205 goto err;
2206 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2207 goto err;
2208 lock = stv090x_get_dmdlock(state, (timeout_dmd / 3));
2209
2210 dir *= -1;
2211 cur_step++;
2212 }
2213 }
2214 }
2215 }
2216
2217 return lock;
2218
2219err:
2220 dprintk(FE_ERROR, 1, "I/O error");
2221 return -1;
2222}
2223
2224static int stv090x_get_loop_params(struct stv090x_state *state, s32 *freq_inc, s32 *timeout_sw, s32 *steps)
2225{
2226 s32 timeout, inc, steps_max, srate, car_max;
2227
2228 srate = state->srate;
2229 car_max = state->search_range / 1000;
Andreas Regel2f5914b2009-04-23 14:56:41 -03002230 car_max += car_max / 10;
Manu Abrahame415c682009-04-06 15:45:20 -03002231 car_max = 65536 * (car_max / 2);
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002232 car_max /= (state->internal->mclk / 1000);
Manu Abrahame415c682009-04-06 15:45:20 -03002233
2234 if (car_max > 0x4000)
2235 car_max = 0x4000 ; /* maxcarrier should be<= +-1/4 Mclk */
2236
2237 inc = srate;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002238 inc /= state->internal->mclk / 1000;
Manu Abrahame415c682009-04-06 15:45:20 -03002239 inc *= 256;
2240 inc *= 256;
2241 inc /= 1000;
2242
Andreas Regel72982f72009-04-08 17:28:41 -03002243 switch (state->search_mode) {
Manu Abrahame415c682009-04-06 15:45:20 -03002244 case STV090x_SEARCH_DVBS1:
2245 case STV090x_SEARCH_DSS:
2246 inc *= 3; /* freq step = 3% of srate */
2247 timeout = 20;
2248 break;
2249
2250 case STV090x_SEARCH_DVBS2:
2251 inc *= 4;
2252 timeout = 25;
2253 break;
2254
2255 case STV090x_SEARCH_AUTO:
2256 default:
2257 inc *= 3;
2258 timeout = 25;
2259 break;
2260 }
2261 inc /= 100;
2262 if ((inc > car_max) || (inc < 0))
2263 inc = car_max / 2; /* increment <= 1/8 Mclk */
2264
2265 timeout *= 27500; /* 27.5 Msps reference */
2266 if (srate > 0)
2267 timeout /= (srate / 1000);
2268
2269 if ((timeout > 100) || (timeout < 0))
2270 timeout = 100;
2271
2272 steps_max = (car_max / inc) + 1; /* min steps = 3 */
2273 if ((steps_max > 100) || (steps_max < 0)) {
2274 steps_max = 100; /* max steps <= 100 */
2275 inc = car_max / steps_max;
2276 }
2277 *freq_inc = inc;
2278 *timeout_sw = timeout;
2279 *steps = steps_max;
2280
2281 return 0;
2282}
2283
2284static int stv090x_chk_signal(struct stv090x_state *state)
2285{
2286 s32 offst_car, agc2, car_max;
2287 int no_signal;
2288
2289 offst_car = STV090x_READ_DEMOD(state, CFR2) << 8;
2290 offst_car |= STV090x_READ_DEMOD(state, CFR1);
Andreas Regel2f5914b2009-04-23 14:56:41 -03002291 offst_car = comp2(offst_car, 16);
Manu Abrahame415c682009-04-06 15:45:20 -03002292
2293 agc2 = STV090x_READ_DEMOD(state, AGC2I1) << 8;
2294 agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
2295 car_max = state->search_range / 1000;
2296
2297 car_max += (car_max / 10); /* 10% margin */
2298 car_max = (65536 * car_max / 2);
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002299 car_max /= state->internal->mclk / 1000;
Manu Abrahame415c682009-04-06 15:45:20 -03002300
2301 if (car_max > 0x4000)
2302 car_max = 0x4000;
2303
2304 if ((agc2 > 0x2000) || (offst_car > 2 * car_max) || (offst_car < -2 * car_max)) {
2305 no_signal = 1;
2306 dprintk(FE_DEBUG, 1, "No Signal");
2307 } else {
2308 no_signal = 0;
2309 dprintk(FE_DEBUG, 1, "Found Signal");
2310 }
2311
2312 return no_signal;
2313}
2314
2315static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 timeout, int zigzag, s32 steps_max)
2316{
2317 int no_signal, lock = 0;
Manu Abraham27d40322009-05-02 18:26:58 -03002318 s32 cpt_step = 0, offst_freq, car_max;
Manu Abrahame415c682009-04-06 15:45:20 -03002319 u32 reg;
2320
2321 car_max = state->search_range / 1000;
2322 car_max += (car_max / 10);
2323 car_max = (65536 * car_max / 2);
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002324 car_max /= (state->internal->mclk / 1000);
Manu Abrahame415c682009-04-06 15:45:20 -03002325 if (car_max > 0x4000)
2326 car_max = 0x4000;
2327
2328 if (zigzag)
2329 offst_freq = 0;
2330 else
2331 offst_freq = -car_max + inc;
2332
Manu Abrahame415c682009-04-06 15:45:20 -03002333 do {
2334 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0)
2335 goto err;
2336 if (STV090x_WRITE_DEMOD(state, CFRINIT1, ((offst_freq / 256) & 0xff)) < 0)
2337 goto err;
2338 if (STV090x_WRITE_DEMOD(state, CFRINIT0, offst_freq & 0xff) < 0)
2339 goto err;
2340 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
2341 goto err;
2342
2343 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
2344 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x1); /* stop DVB-S2 packet delin */
2345 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
2346 goto err;
2347
Manu Abrahame415c682009-04-06 15:45:20 -03002348 if (zigzag) {
2349 if (offst_freq >= 0)
2350 offst_freq = -offst_freq - 2 * inc;
2351 else
2352 offst_freq = -offst_freq;
2353 } else {
2354 offst_freq += 2 * inc;
2355 }
2356
Andreas Regel2f5914b2009-04-23 14:56:41 -03002357 cpt_step++;
2358
Manu Abrahame415c682009-04-06 15:45:20 -03002359 lock = stv090x_get_dmdlock(state, timeout);
2360 no_signal = stv090x_chk_signal(state);
2361
2362 } while ((!lock) &&
2363 (!no_signal) &&
2364 ((offst_freq - inc) < car_max) &&
2365 ((offst_freq + inc) > -car_max) &&
2366 (cpt_step < steps_max));
2367
2368 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
2369 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0);
2370 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
2371 goto err;
2372
2373 return lock;
2374err:
2375 dprintk(FE_ERROR, 1, "I/O error");
2376 return -1;
2377}
2378
2379static int stv090x_sw_algo(struct stv090x_state *state)
2380{
2381 int no_signal, zigzag, lock = 0;
2382 u32 reg;
2383
2384 s32 dvbs2_fly_wheel;
2385 s32 inc, timeout_step, trials, steps_max;
2386
Manu Abraham27d40322009-05-02 18:26:58 -03002387 /* get params */
2388 stv090x_get_loop_params(state, &inc, &timeout_step, &steps_max);
Manu Abrahame415c682009-04-06 15:45:20 -03002389
Andreas Regel72982f72009-04-08 17:28:41 -03002390 switch (state->search_mode) {
Manu Abrahame415c682009-04-06 15:45:20 -03002391 case STV090x_SEARCH_DVBS1:
2392 case STV090x_SEARCH_DSS:
2393 /* accelerate the frequency detector */
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002394 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002395 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3B) < 0)
2396 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002397 }
Manu Abraham27d40322009-05-02 18:26:58 -03002398
Manu Abrahame415c682009-04-06 15:45:20 -03002399 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x49) < 0)
2400 goto err;
2401 zigzag = 0;
2402 break;
2403
2404 case STV090x_SEARCH_DVBS2:
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002405 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002406 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2407 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002408 }
Manu Abraham27d40322009-05-02 18:26:58 -03002409
Manu Abrahame415c682009-04-06 15:45:20 -03002410 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2411 goto err;
2412 zigzag = 1;
2413 break;
2414
2415 case STV090x_SEARCH_AUTO:
2416 default:
2417 /* accelerate the frequency detector */
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002418 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002419 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3b) < 0)
2420 goto err;
2421 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2422 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002423 }
Manu Abraham27d40322009-05-02 18:26:58 -03002424
Andreas Regel2f5914b2009-04-23 14:56:41 -03002425 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0xc9) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03002426 goto err;
2427 zigzag = 0;
2428 break;
2429 }
2430
2431 trials = 0;
2432 do {
2433 lock = stv090x_search_car_loop(state, inc, timeout_step, zigzag, steps_max);
2434 no_signal = stv090x_chk_signal(state);
2435 trials++;
2436
2437 /*run the SW search 2 times maximum*/
2438 if (lock || no_signal || (trials == 2)) {
2439 /*Check if the demod is not losing lock in DVBS2*/
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002440 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002441 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
2442 goto err;
2443 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
2444 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002445 }
2446
2447 reg = STV090x_READ_DEMOD(state, DMDSTATE);
2448 if ((lock) && (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == STV090x_DVBS2)) {
2449 /*Check if the demod is not losing lock in DVBS2*/
2450 msleep(timeout_step);
2451 reg = STV090x_READ_DEMOD(state, DMDFLYW);
2452 dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
2453 if (dvbs2_fly_wheel < 0xd) { /*if correct frames is decrementing */
2454 msleep(timeout_step);
2455 reg = STV090x_READ_DEMOD(state, DMDFLYW);
2456 dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
2457 }
2458 if (dvbs2_fly_wheel < 0xd) {
2459 /*FALSE lock, The demod is loosing lock */
2460 lock = 0;
2461 if (trials < 2) {
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002462 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002463 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2464 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002465 }
Manu Abraham27d40322009-05-02 18:26:58 -03002466
Manu Abrahame415c682009-04-06 15:45:20 -03002467 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2468 goto err;
2469 }
2470 }
2471 }
2472 }
2473 } while ((!lock) && (trials < 2) && (!no_signal));
2474
2475 return lock;
2476err:
2477 dprintk(FE_ERROR, 1, "I/O error");
2478 return -1;
2479}
2480
2481static enum stv090x_delsys stv090x_get_std(struct stv090x_state *state)
2482{
2483 u32 reg;
2484 enum stv090x_delsys delsys;
2485
2486 reg = STV090x_READ_DEMOD(state, DMDSTATE);
2487 if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 2)
2488 delsys = STV090x_DVBS2;
2489 else if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 3) {
2490 reg = STV090x_READ_DEMOD(state, FECM);
2491 if (STV090x_GETFIELD_Px(reg, DSS_DVB_FIELD) == 1)
2492 delsys = STV090x_DSS;
2493 else
2494 delsys = STV090x_DVBS1;
2495 } else {
2496 delsys = STV090x_ERROR;
2497 }
2498
2499 return delsys;
2500}
2501
2502/* in Hz */
2503static s32 stv090x_get_car_freq(struct stv090x_state *state, u32 mclk)
2504{
2505 s32 derot, int_1, int_2, tmp_1, tmp_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002506
2507 derot = STV090x_READ_DEMOD(state, CFR2) << 16;
2508 derot |= STV090x_READ_DEMOD(state, CFR1) << 8;
2509 derot |= STV090x_READ_DEMOD(state, CFR0);
2510
2511 derot = comp2(derot, 24);
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002512 int_1 = mclk >> 12;
Manu Abrahamda4b9052009-04-08 20:30:29 -03002513 int_2 = derot >> 12;
Manu Abrahame415c682009-04-06 15:45:20 -03002514
Manu Abrahamda4b9052009-04-08 20:30:29 -03002515 /* carrier_frequency = MasterClock * Reg / 2^24 */
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002516 tmp_1 = mclk % 0x1000;
Manu Abrahamda4b9052009-04-08 20:30:29 -03002517 tmp_2 = derot % 0x1000;
Manu Abrahame415c682009-04-06 15:45:20 -03002518
2519 derot = (int_1 * int_2) +
Manu Abrahamda4b9052009-04-08 20:30:29 -03002520 ((int_1 * tmp_2) >> 12) +
Andreas Regelb671a8d2009-11-13 18:15:27 -03002521 ((int_2 * tmp_1) >> 12);
Manu Abrahame415c682009-04-06 15:45:20 -03002522
2523 return derot;
2524}
2525
2526static int stv090x_get_viterbi(struct stv090x_state *state)
2527{
2528 u32 reg, rate;
2529
2530 reg = STV090x_READ_DEMOD(state, VITCURPUN);
2531 rate = STV090x_GETFIELD_Px(reg, VIT_CURPUN_FIELD);
2532
2533 switch (rate) {
2534 case 13:
2535 state->fec = STV090x_PR12;
2536 break;
2537
2538 case 18:
2539 state->fec = STV090x_PR23;
2540 break;
2541
2542 case 21:
2543 state->fec = STV090x_PR34;
2544 break;
2545
2546 case 24:
2547 state->fec = STV090x_PR56;
2548 break;
2549
2550 case 25:
2551 state->fec = STV090x_PR67;
2552 break;
2553
2554 case 26:
2555 state->fec = STV090x_PR78;
2556 break;
2557
2558 default:
2559 state->fec = STV090x_PRERR;
2560 break;
2561 }
2562
2563 return 0;
2564}
2565
2566static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *state)
2567{
2568 struct dvb_frontend *fe = &state->frontend;
2569
2570 u8 tmg;
2571 u32 reg;
2572 s32 i = 0, offst_freq;
2573
2574 msleep(5);
2575
2576 if (state->algo == STV090x_BLIND_SEARCH) {
2577 tmg = STV090x_READ_DEMOD(state, TMGREG2);
2578 STV090x_WRITE_DEMOD(state, SFRSTEP, 0x5c);
Andreas Regel2f5914b2009-04-23 14:56:41 -03002579 while ((i <= 50) && (tmg != 0) && (tmg != 0xff)) {
Manu Abrahame415c682009-04-06 15:45:20 -03002580 tmg = STV090x_READ_DEMOD(state, TMGREG2);
2581 msleep(5);
2582 i += 5;
2583 }
2584 }
2585 state->delsys = stv090x_get_std(state);
2586
Manu Abraham27d40322009-05-02 18:26:58 -03002587 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2588 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002589
Manu Abraham27d40322009-05-02 18:26:58 -03002590 if (state->config->tuner_get_frequency) {
2591 if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
2592 goto err;
2593 }
Manu Abrahame415c682009-04-06 15:45:20 -03002594
Manu Abraham27d40322009-05-02 18:26:58 -03002595 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2596 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002597
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002598 offst_freq = stv090x_get_car_freq(state, state->internal->mclk) / 1000;
Manu Abrahame415c682009-04-06 15:45:20 -03002599 state->frequency += offst_freq;
Manu Abraham27d40322009-05-02 18:26:58 -03002600
2601 if (stv090x_get_viterbi(state) < 0)
2602 goto err;
2603
Manu Abrahame415c682009-04-06 15:45:20 -03002604 reg = STV090x_READ_DEMOD(state, DMDMODCOD);
2605 state->modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
2606 state->pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
2607 state->frame_len = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) >> 1;
2608 reg = STV090x_READ_DEMOD(state, TMGOBS);
2609 state->rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
2610 reg = STV090x_READ_DEMOD(state, FECM);
2611 state->inversion = STV090x_GETFIELD_Px(reg, IQINV_FIELD);
2612
2613 if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000)) {
2614
Manu Abraham27d40322009-05-02 18:26:58 -03002615 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2616 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002617
Manu Abraham27d40322009-05-02 18:26:58 -03002618 if (state->config->tuner_get_frequency) {
2619 if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
2620 goto err;
2621 }
Manu Abrahame415c682009-04-06 15:45:20 -03002622
Manu Abraham27d40322009-05-02 18:26:58 -03002623 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2624 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002625
2626 if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
Andreas Regel2f5914b2009-04-23 14:56:41 -03002627 return STV090x_RANGEOK;
Manu Abrahame415c682009-04-06 15:45:20 -03002628 else if (abs(offst_freq) <= (stv090x_car_width(state->srate, state->rolloff) / 2000))
2629 return STV090x_RANGEOK;
2630 else
2631 return STV090x_OUTOFRANGE; /* Out of Range */
2632 } else {
2633 if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
2634 return STV090x_RANGEOK;
2635 else
2636 return STV090x_OUTOFRANGE;
2637 }
2638
2639 return STV090x_OUTOFRANGE;
Manu Abraham27d40322009-05-02 18:26:58 -03002640err:
2641 dprintk(FE_ERROR, 1, "I/O error");
2642 return -1;
Manu Abrahame415c682009-04-06 15:45:20 -03002643}
2644
2645static u32 stv090x_get_tmgoffst(struct stv090x_state *state, u32 srate)
2646{
2647 s32 offst_tmg;
Manu Abrahame415c682009-04-06 15:45:20 -03002648
2649 offst_tmg = STV090x_READ_DEMOD(state, TMGREG2) << 16;
2650 offst_tmg |= STV090x_READ_DEMOD(state, TMGREG1) << 8;
2651 offst_tmg |= STV090x_READ_DEMOD(state, TMGREG0);
2652
Manu Abrahame415c682009-04-06 15:45:20 -03002653 offst_tmg = comp2(offst_tmg, 24); /* 2's complement */
2654 if (!offst_tmg)
2655 offst_tmg = 1;
2656
Manu Abraham5f99fef2009-04-08 20:24:53 -03002657 offst_tmg = ((s32) srate * 10) / ((s32) 0x1000000 / offst_tmg);
Manu Abrahame415c682009-04-06 15:45:20 -03002658 offst_tmg /= 320;
2659
2660 return offst_tmg;
2661}
2662
2663static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_modcod modcod, s32 pilots)
2664{
2665 u8 aclc = 0x29;
2666 s32 i;
Manu Abraham27d40322009-05-02 18:26:58 -03002667 struct stv090x_long_frame_crloop *car_loop, *car_loop_qpsk_low, *car_loop_apsk_low;
Manu Abrahame415c682009-04-06 15:45:20 -03002668
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002669 if (state->internal->dev_ver == 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03002670 car_loop = stv090x_s2_crl_cut20;
2671 car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut20;
2672 car_loop_apsk_low = stv090x_s2_apsk_crl_cut20;
2673 } else {
2674 /* >= Cut 3 */
2675 car_loop = stv090x_s2_crl_cut30;
2676 car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut30;
2677 car_loop_apsk_low = stv090x_s2_apsk_crl_cut30;
2678 }
Manu Abrahame415c682009-04-06 15:45:20 -03002679
2680 if (modcod < STV090x_QPSK_12) {
2681 i = 0;
Manu Abraham27d40322009-05-02 18:26:58 -03002682 while ((i < 3) && (modcod != car_loop_qpsk_low[i].modcod))
Manu Abrahame415c682009-04-06 15:45:20 -03002683 i++;
2684
2685 if (i >= 3)
2686 i = 2;
2687
2688 } else {
2689 i = 0;
2690 while ((i < 14) && (modcod != car_loop[i].modcod))
2691 i++;
2692
2693 if (i >= 14) {
2694 i = 0;
Manu Abraham27d40322009-05-02 18:26:58 -03002695 while ((i < 11) && (modcod != car_loop_apsk_low[i].modcod))
Manu Abrahame415c682009-04-06 15:45:20 -03002696 i++;
2697
2698 if (i >= 11)
2699 i = 10;
2700 }
2701 }
2702
2703 if (modcod <= STV090x_QPSK_25) {
2704 if (pilots) {
2705 if (state->srate <= 3000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002706 aclc = car_loop_qpsk_low[i].crl_pilots_on_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002707 else if (state->srate <= 7000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002708 aclc = car_loop_qpsk_low[i].crl_pilots_on_5;
Manu Abrahame415c682009-04-06 15:45:20 -03002709 else if (state->srate <= 15000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002710 aclc = car_loop_qpsk_low[i].crl_pilots_on_10;
Manu Abrahame415c682009-04-06 15:45:20 -03002711 else if (state->srate <= 25000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002712 aclc = car_loop_qpsk_low[i].crl_pilots_on_20;
Manu Abrahame415c682009-04-06 15:45:20 -03002713 else
Manu Abraham27d40322009-05-02 18:26:58 -03002714 aclc = car_loop_qpsk_low[i].crl_pilots_on_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002715 } else {
2716 if (state->srate <= 3000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002717 aclc = car_loop_qpsk_low[i].crl_pilots_off_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002718 else if (state->srate <= 7000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002719 aclc = car_loop_qpsk_low[i].crl_pilots_off_5;
Manu Abrahame415c682009-04-06 15:45:20 -03002720 else if (state->srate <= 15000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002721 aclc = car_loop_qpsk_low[i].crl_pilots_off_10;
Manu Abrahame415c682009-04-06 15:45:20 -03002722 else if (state->srate <= 25000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002723 aclc = car_loop_qpsk_low[i].crl_pilots_off_20;
Manu Abrahame415c682009-04-06 15:45:20 -03002724 else
Manu Abraham27d40322009-05-02 18:26:58 -03002725 aclc = car_loop_qpsk_low[i].crl_pilots_off_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002726 }
2727
2728 } else if (modcod <= STV090x_8PSK_910) {
2729 if (pilots) {
2730 if (state->srate <= 3000000)
2731 aclc = car_loop[i].crl_pilots_on_2;
2732 else if (state->srate <= 7000000)
2733 aclc = car_loop[i].crl_pilots_on_5;
2734 else if (state->srate <= 15000000)
2735 aclc = car_loop[i].crl_pilots_on_10;
2736 else if (state->srate <= 25000000)
2737 aclc = car_loop[i].crl_pilots_on_20;
2738 else
2739 aclc = car_loop[i].crl_pilots_on_30;
2740 } else {
2741 if (state->srate <= 3000000)
2742 aclc = car_loop[i].crl_pilots_off_2;
2743 else if (state->srate <= 7000000)
2744 aclc = car_loop[i].crl_pilots_off_5;
2745 else if (state->srate <= 15000000)
2746 aclc = car_loop[i].crl_pilots_off_10;
2747 else if (state->srate <= 25000000)
2748 aclc = car_loop[i].crl_pilots_off_20;
2749 else
2750 aclc = car_loop[i].crl_pilots_off_30;
2751 }
2752 } else { /* 16APSK and 32APSK */
2753 if (state->srate <= 3000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002754 aclc = car_loop_apsk_low[i].crl_pilots_on_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002755 else if (state->srate <= 7000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002756 aclc = car_loop_apsk_low[i].crl_pilots_on_5;
Manu Abrahame415c682009-04-06 15:45:20 -03002757 else if (state->srate <= 15000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002758 aclc = car_loop_apsk_low[i].crl_pilots_on_10;
Manu Abrahame415c682009-04-06 15:45:20 -03002759 else if (state->srate <= 25000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002760 aclc = car_loop_apsk_low[i].crl_pilots_on_20;
Manu Abrahame415c682009-04-06 15:45:20 -03002761 else
Manu Abraham27d40322009-05-02 18:26:58 -03002762 aclc = car_loop_apsk_low[i].crl_pilots_on_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002763 }
2764
2765 return aclc;
2766}
2767
2768static u8 stv090x_optimize_carloop_short(struct stv090x_state *state)
2769{
Manu Abraham0a5ded52009-06-17 16:48:17 -03002770 struct stv090x_short_frame_crloop *short_crl = NULL;
Manu Abrahame415c682009-04-06 15:45:20 -03002771 s32 index = 0;
2772 u8 aclc = 0x0b;
2773
2774 switch (state->modulation) {
2775 case STV090x_QPSK:
2776 default:
2777 index = 0;
2778 break;
2779 case STV090x_8PSK:
2780 index = 1;
2781 break;
2782 case STV090x_16APSK:
2783 index = 2;
2784 break;
2785 case STV090x_32APSK:
2786 index = 3;
2787 break;
2788 }
2789
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002790 if (state->internal->dev_ver >= 0x30) {
Manu Abrahameebf8d82009-06-18 04:50:53 -03002791 /* Cut 3.0 and up */
Manu Abraham27d40322009-05-02 18:26:58 -03002792 short_crl = stv090x_s2_short_crl_cut30;
Manu Abrahameebf8d82009-06-18 04:50:53 -03002793 } else {
2794 /* Cut 2.0 and up: we don't support cuts older than 2.0 */
2795 short_crl = stv090x_s2_short_crl_cut20;
2796 }
Manu Abrahame415c682009-04-06 15:45:20 -03002797
Manu Abraham27d40322009-05-02 18:26:58 -03002798 if (state->srate <= 3000000)
2799 aclc = short_crl[index].crl_2;
2800 else if (state->srate <= 7000000)
2801 aclc = short_crl[index].crl_5;
2802 else if (state->srate <= 15000000)
2803 aclc = short_crl[index].crl_10;
2804 else if (state->srate <= 25000000)
2805 aclc = short_crl[index].crl_20;
2806 else
2807 aclc = short_crl[index].crl_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002808
2809 return aclc;
2810}
2811
2812static int stv090x_optimize_track(struct stv090x_state *state)
2813{
2814 struct dvb_frontend *fe = &state->frontend;
2815
2816 enum stv090x_rolloff rolloff;
2817 enum stv090x_modcod modcod;
2818
2819 s32 srate, pilots, aclc, f_1, f_0, i = 0, blind_tune = 0;
2820 u32 reg;
2821
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002822 srate = stv090x_get_srate(state, state->internal->mclk);
Manu Abrahame415c682009-04-06 15:45:20 -03002823 srate += stv090x_get_tmgoffst(state, srate);
2824
2825 switch (state->delsys) {
2826 case STV090x_DVBS1:
2827 case STV090x_DSS:
Andreas Regelb671a8d2009-11-13 18:15:27 -03002828 if (state->search_mode == STV090x_SEARCH_AUTO) {
Manu Abrahame415c682009-04-06 15:45:20 -03002829 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2830 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
2831 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
2832 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2833 goto err;
2834 }
2835 reg = STV090x_READ_DEMOD(state, DEMOD);
2836 STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
Manu Abraham27d40322009-05-02 18:26:58 -03002837 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x01);
Manu Abrahame415c682009-04-06 15:45:20 -03002838 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
2839 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03002840
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002841 if (state->internal->dev_ver >= 0x30) {
Manu Abraham27d40322009-05-02 18:26:58 -03002842 if (stv090x_get_viterbi(state) < 0)
2843 goto err;
2844
2845 if (state->fec == STV090x_PR12) {
2846 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x98) < 0)
2847 goto err;
2848 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2849 goto err;
2850 } else {
2851 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x18) < 0)
2852 goto err;
2853 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2854 goto err;
2855 }
2856 }
2857
Manu Abrahame415c682009-04-06 15:45:20 -03002858 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
2859 goto err;
2860 break;
2861
2862 case STV090x_DVBS2:
2863 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
Andreas Regel2f5914b2009-04-23 14:56:41 -03002864 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
2865 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
Manu Abrahame415c682009-04-06 15:45:20 -03002866 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2867 goto err;
2868 if (STV090x_WRITE_DEMOD(state, ACLC, 0) < 0)
2869 goto err;
2870 if (STV090x_WRITE_DEMOD(state, BCLC, 0) < 0)
2871 goto err;
2872 if (state->frame_len == STV090x_LONG_FRAME) {
2873 reg = STV090x_READ_DEMOD(state, DMDMODCOD);
2874 modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
2875 pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
2876 aclc = stv090x_optimize_carloop(state, modcod, pilots);
2877 if (modcod <= STV090x_QPSK_910) {
2878 STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc);
2879 } else if (modcod <= STV090x_8PSK_910) {
2880 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2881 goto err;
2882 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
2883 goto err;
2884 }
2885 if ((state->demod_mode == STV090x_SINGLE) && (modcod > STV090x_8PSK_910)) {
2886 if (modcod <= STV090x_16APSK_910) {
2887 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2888 goto err;
2889 if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2890 goto err;
2891 } else {
2892 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2893 goto err;
2894 if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
2895 goto err;
2896 }
2897 }
2898 } else {
2899 /*Carrier loop setting for short frame*/
2900 aclc = stv090x_optimize_carloop_short(state);
2901 if (state->modulation == STV090x_QPSK) {
2902 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc) < 0)
2903 goto err;
2904 } else if (state->modulation == STV090x_8PSK) {
2905 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2906 goto err;
2907 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
2908 goto err;
2909 } else if (state->modulation == STV090x_16APSK) {
2910 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2911 goto err;
2912 if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2913 goto err;
2914 } else if (state->modulation == STV090x_32APSK) {
2915 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2916 goto err;
2917 if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
2918 goto err;
2919 }
2920 }
Manu Abraham27d40322009-05-02 18:26:58 -03002921
Manu Abrahame415c682009-04-06 15:45:20 -03002922 STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67); /* PER */
2923 break;
2924
2925 case STV090x_UNKNOWN:
2926 default:
2927 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2928 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
2929 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
2930 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2931 goto err;
2932 break;
2933 }
2934
2935 f_1 = STV090x_READ_DEMOD(state, CFR2);
2936 f_0 = STV090x_READ_DEMOD(state, CFR1);
2937 reg = STV090x_READ_DEMOD(state, TMGOBS);
2938 rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
2939
2940 if (state->algo == STV090x_BLIND_SEARCH) {
2941 STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00);
2942 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2943 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0x00);
2944 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
2945 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2946 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03002947 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03002948 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03002949
2950 if (stv090x_set_srate(state, srate) < 0)
2951 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002952 blind_tune = 1;
Andreas Regel7b035da2009-11-13 18:20:54 -03002953
2954 if (stv090x_dvbs_track_crl(state) < 0)
2955 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002956 }
2957
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002958 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03002959 if ((state->search_mode == STV090x_SEARCH_DVBS1) ||
2960 (state->search_mode == STV090x_SEARCH_DSS) ||
2961 (state->search_mode == STV090x_SEARCH_AUTO)) {
2962
2963 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x0a) < 0)
2964 goto err;
2965 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x00) < 0)
2966 goto err;
2967 }
2968 }
2969
Manu Abrahame415c682009-04-06 15:45:20 -03002970 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
2971 goto err;
2972
Manu Abraham27d40322009-05-02 18:26:58 -03002973 /* AUTO tracking MODE */
2974 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x80) < 0)
2975 goto err;
2976 /* AUTO tracking MODE */
2977 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x80) < 0)
2978 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002979
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002980 if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1) ||
2981 (state->srate < 10000000)) {
Manu Abraham27d40322009-05-02 18:26:58 -03002982 /* update initial carrier freq with the found freq offset */
Manu Abrahame415c682009-04-06 15:45:20 -03002983 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
2984 goto err;
2985 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
2986 goto err;
2987 state->tuner_bw = stv090x_car_width(srate, state->rolloff) + 10000000;
2988
Andreas Regel97f7a2a2010-01-05 19:19:43 -03002989 if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1)) {
Manu Abrahame415c682009-04-06 15:45:20 -03002990
2991 if (state->algo != STV090x_WARM_SEARCH) {
2992
Manu Abraham27d40322009-05-02 18:26:58 -03002993 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2994 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002995
Manu Abraham27d40322009-05-02 18:26:58 -03002996 if (state->config->tuner_set_bandwidth) {
2997 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
2998 goto err;
2999 }
Manu Abrahame415c682009-04-06 15:45:20 -03003000
Manu Abraham27d40322009-05-02 18:26:58 -03003001 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
3002 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003003
3004 }
3005 }
3006 if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000))
3007 msleep(50); /* blind search: wait 50ms for SR stabilization */
3008 else
3009 msleep(5);
3010
3011 stv090x_get_lock_tmg(state);
3012
3013 if (!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) {
3014 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
3015 goto err;
3016 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
3017 goto err;
3018 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
3019 goto err;
3020 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
3021 goto err;
3022
3023 i = 0;
3024
3025 while ((!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) && (i <= 2)) {
3026
3027 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
3028 goto err;
3029 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
3030 goto err;
3031 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
3032 goto err;
3033 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
3034 goto err;
3035 i++;
3036 }
3037 }
3038
3039 }
3040
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003041 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03003042 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
3043 goto err;
3044 }
Manu Abraham27d40322009-05-02 18:26:58 -03003045
Manu Abrahame415c682009-04-06 15:45:20 -03003046 if ((state->delsys == STV090x_DVBS1) || (state->delsys == STV090x_DSS))
3047 stv090x_set_vit_thtracq(state);
3048
3049 return 0;
3050err:
3051 dprintk(FE_ERROR, 1, "I/O error");
3052 return -1;
3053}
3054
3055static int stv090x_get_feclock(struct stv090x_state *state, s32 timeout)
3056{
3057 s32 timer = 0, lock = 0, stat;
3058 u32 reg;
3059
3060 while ((timer < timeout) && (!lock)) {
3061 reg = STV090x_READ_DEMOD(state, DMDSTATE);
3062 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
3063
3064 switch (stat) {
3065 case 0: /* searching */
3066 case 1: /* first PLH detected */
3067 default:
3068 lock = 0;
3069 break;
3070
3071 case 2: /* DVB-S2 mode */
3072 reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
3073 lock = STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD);
3074 break;
3075
3076 case 3: /* DVB-S1/legacy mode */
3077 reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
3078 lock = STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD);
3079 break;
3080 }
3081 if (!lock) {
3082 msleep(10);
3083 timer += 10;
3084 }
3085 }
3086 return lock;
3087}
3088
3089static int stv090x_get_lock(struct stv090x_state *state, s32 timeout_dmd, s32 timeout_fec)
3090{
3091 u32 reg;
3092 s32 timer = 0;
3093 int lock;
3094
3095 lock = stv090x_get_dmdlock(state, timeout_dmd);
3096 if (lock)
3097 lock = stv090x_get_feclock(state, timeout_fec);
3098
3099 if (lock) {
3100 lock = 0;
3101
3102 while ((timer < timeout_fec) && (!lock)) {
3103 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3104 lock = STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD);
3105 msleep(1);
3106 timer++;
3107 }
3108 }
3109
3110 return lock;
3111}
3112
3113static int stv090x_set_s2rolloff(struct stv090x_state *state)
3114{
Manu Abrahame415c682009-04-06 15:45:20 -03003115 u32 reg;
3116
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003117 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03003118 /* rolloff to auto mode if DVBS2 */
Manu Abrahame415c682009-04-06 15:45:20 -03003119 reg = STV090x_READ_DEMOD(state, DEMOD);
Manu Abraham27d40322009-05-02 18:26:58 -03003120 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x00);
Manu Abrahame415c682009-04-06 15:45:20 -03003121 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3122 goto err;
3123 } else {
Manu Abraham27d40322009-05-02 18:26:58 -03003124 /* DVB-S2 rolloff to auto mode if DVBS2 */
Manu Abrahame415c682009-04-06 15:45:20 -03003125 reg = STV090x_READ_DEMOD(state, DEMOD);
Manu Abraham27d40322009-05-02 18:26:58 -03003126 STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 0x00);
Manu Abrahame415c682009-04-06 15:45:20 -03003127 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3128 goto err;
3129 }
3130 return 0;
3131err:
3132 dprintk(FE_ERROR, 1, "I/O error");
3133 return -1;
3134}
3135
Manu Abrahame415c682009-04-06 15:45:20 -03003136
3137static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
3138{
3139 struct dvb_frontend *fe = &state->frontend;
3140 enum stv090x_signal_state signal_state = STV090x_NOCARRIER;
3141 u32 reg;
Andreas Regela4978a82009-11-13 18:17:45 -03003142 s32 agc1_power, power_iq = 0, i;
Andreas Regel4e58a682009-04-16 08:40:36 -03003143 int lock = 0, low_sr = 0, no_signal = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003144
3145 reg = STV090x_READ_DEMOD(state, TSCFGH);
3146 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* Stop path 1 stream merger */
3147 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3148 goto err;
3149
3150 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod stop */
3151 goto err;
3152
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003153 if (state->internal->dev_ver >= 0x20) {
Andreas Regel7b035da2009-11-13 18:20:54 -03003154 if (state->srate > 5000000) {
3155 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
3156 goto err;
3157 } else {
3158 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x82) < 0)
3159 goto err;
3160 }
Manu Abrahame415c682009-04-06 15:45:20 -03003161 }
3162
3163 stv090x_get_lock_tmg(state);
3164
3165 if (state->algo == STV090x_BLIND_SEARCH) {
3166 state->tuner_bw = 2 * 36000000; /* wide bw for unknown srate */
Manu Abraham27d40322009-05-02 18:26:58 -03003167 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0) /* wider srate scan */
Manu Abrahame415c682009-04-06 15:45:20 -03003168 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003169 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
3170 goto err;
3171 if (stv090x_set_srate(state, 1000000) < 0) /* inital srate = 1Msps */
3172 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003173 } else {
3174 /* known srate */
3175 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
3176 goto err;
3177 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
3178 goto err;
3179
Manu Abraham27d40322009-05-02 18:26:58 -03003180 if (state->srate < 2000000) {
3181 /* SR < 2MSPS */
3182 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x63) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003183 goto err;
3184 } else {
Manu Abraham27d40322009-05-02 18:26:58 -03003185 /* SR >= 2Msps */
3186 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003187 goto err;
3188 }
3189
Manu Abraham27d40322009-05-02 18:26:58 -03003190 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
3191 goto err;
3192
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003193 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03003194 if (STV090x_WRITE_DEMOD(state, KREFTMG, 0x5a) < 0)
3195 goto err;
3196 if (state->algo == STV090x_COLD_SEARCH)
Andreas Regel4e58a682009-04-16 08:40:36 -03003197 state->tuner_bw = (15 * (stv090x_car_width(state->srate, state->rolloff) + 10000000)) / 10;
Manu Abrahame415c682009-04-06 15:45:20 -03003198 else if (state->algo == STV090x_WARM_SEARCH)
3199 state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + 10000000;
Manu Abrahame415c682009-04-06 15:45:20 -03003200 }
Manu Abraham27d40322009-05-02 18:26:58 -03003201
3202 /* if cold start or warm (Symbolrate is known)
3203 * use a Narrow symbol rate scan range
3204 */
3205 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0) /* narrow srate scan */
Manu Abrahame415c682009-04-06 15:45:20 -03003206 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003207
3208 if (stv090x_set_srate(state, state->srate) < 0)
3209 goto err;
3210
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003211 if (stv090x_set_max_srate(state, state->internal->mclk,
3212 state->srate) < 0)
Manu Abraham27d40322009-05-02 18:26:58 -03003213 goto err;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003214 if (stv090x_set_min_srate(state, state->internal->mclk,
3215 state->srate) < 0)
Manu Abraham27d40322009-05-02 18:26:58 -03003216 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003217
3218 if (state->srate >= 10000000)
Andreas Regel4e58a682009-04-16 08:40:36 -03003219 low_sr = 0;
3220 else
Manu Abrahame415c682009-04-06 15:45:20 -03003221 low_sr = 1;
3222 }
3223
3224 /* Setup tuner */
Manu Abraham27d40322009-05-02 18:26:58 -03003225 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
3226 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003227
Manu Abraham27d40322009-05-02 18:26:58 -03003228 if (state->config->tuner_set_bbgain) {
3229 if (state->config->tuner_set_bbgain(fe, 10) < 0) /* 10dB */
3230 goto err;
3231 }
Manu Abrahame415c682009-04-06 15:45:20 -03003232
Manu Abraham27d40322009-05-02 18:26:58 -03003233 if (state->config->tuner_set_frequency) {
3234 if (state->config->tuner_set_frequency(fe, state->frequency) < 0)
3235 goto err;
3236 }
Manu Abrahame415c682009-04-06 15:45:20 -03003237
Manu Abraham27d40322009-05-02 18:26:58 -03003238 if (state->config->tuner_set_bandwidth) {
3239 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
3240 goto err;
3241 }
Manu Abrahame415c682009-04-06 15:45:20 -03003242
Manu Abraham27d40322009-05-02 18:26:58 -03003243 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
3244 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003245
3246 msleep(50);
3247
Manu Abraham27d40322009-05-02 18:26:58 -03003248 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
3249 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003250
Manu Abraham27d40322009-05-02 18:26:58 -03003251 if (state->config->tuner_get_status) {
3252 if (state->config->tuner_get_status(fe, &reg) < 0)
3253 goto err;
3254 }
Manu Abrahame415c682009-04-06 15:45:20 -03003255
3256 if (reg)
3257 dprintk(FE_DEBUG, 1, "Tuner phase locked");
3258 else
3259 dprintk(FE_DEBUG, 1, "Tuner unlocked");
3260
Manu Abraham27d40322009-05-02 18:26:58 -03003261 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003262 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003263
Manu Abraham27d40322009-05-02 18:26:58 -03003264 msleep(10);
3265 agc1_power = MAKEWORD16(STV090x_READ_DEMOD(state, AGCIQIN1),
3266 STV090x_READ_DEMOD(state, AGCIQIN0));
3267
3268 if (agc1_power == 0) {
3269 /* If AGC1 integrator value is 0
3270 * then read POWERI, POWERQ
3271 */
3272 for (i = 0; i < 5; i++) {
3273 power_iq += (STV090x_READ_DEMOD(state, POWERI) +
3274 STV090x_READ_DEMOD(state, POWERQ)) >> 1;
3275 }
3276 power_iq /= 5;
Manu Abrahame415c682009-04-06 15:45:20 -03003277 }
3278
Manu Abraham27d40322009-05-02 18:26:58 -03003279 if ((agc1_power == 0) && (power_iq < STV090x_IQPOWER_THRESHOLD)) {
3280 dprintk(FE_ERROR, 1, "No Signal: POWER_IQ=0x%02x", power_iq);
3281 lock = 0;
Andreas Regelc4fa6492009-11-13 18:18:53 -03003282 signal_state = STV090x_NOAGC1;
Manu Abraham27d40322009-05-02 18:26:58 -03003283 } else {
3284 reg = STV090x_READ_DEMOD(state, DEMOD);
3285 STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, state->inversion);
3286
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003287 if (state->internal->dev_ver <= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03003288 /* rolloff to auto mode if DVBS2 */
3289 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 1);
3290 } else {
3291 /* DVB-S2 rolloff to auto mode if DVBS2 */
3292 STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 1);
3293 }
3294 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3295 goto err;
3296
3297 if (stv090x_delivery_search(state) < 0)
3298 goto err;
3299
3300 if (state->algo != STV090x_BLIND_SEARCH) {
3301 if (stv090x_start_search(state) < 0)
3302 goto err;
3303 }
3304 }
3305
Andreas Regelc4fa6492009-11-13 18:18:53 -03003306 if (signal_state == STV090x_NOAGC1)
3307 return signal_state;
Manu Abraham27d40322009-05-02 18:26:58 -03003308
Manu Abrahame415c682009-04-06 15:45:20 -03003309 if (state->algo == STV090x_BLIND_SEARCH)
3310 lock = stv090x_blind_search(state);
Manu Abraham27d40322009-05-02 18:26:58 -03003311
Manu Abrahame415c682009-04-06 15:45:20 -03003312 else if (state->algo == STV090x_COLD_SEARCH)
Andreas Regela4978a82009-11-13 18:17:45 -03003313 lock = stv090x_get_coldlock(state, state->DemodTimeout);
Manu Abraham27d40322009-05-02 18:26:58 -03003314
Manu Abrahame415c682009-04-06 15:45:20 -03003315 else if (state->algo == STV090x_WARM_SEARCH)
Andreas Regela4978a82009-11-13 18:17:45 -03003316 lock = stv090x_get_dmdlock(state, state->DemodTimeout);
Manu Abrahame415c682009-04-06 15:45:20 -03003317
3318 if ((!lock) && (state->algo == STV090x_COLD_SEARCH)) {
3319 if (!low_sr) {
3320 if (stv090x_chk_tmg(state))
3321 lock = stv090x_sw_algo(state);
3322 }
3323 }
3324
3325 if (lock)
3326 signal_state = stv090x_get_sig_params(state);
3327
3328 if ((lock) && (signal_state == STV090x_RANGEOK)) { /* signal within Range */
3329 stv090x_optimize_track(state);
Manu Abraham27d40322009-05-02 18:26:58 -03003330
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003331 if (state->internal->dev_ver >= 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03003332 /* >= Cut 2.0 :release TS reset after
3333 * demod lock and optimized Tracking
3334 */
Manu Abrahame415c682009-04-06 15:45:20 -03003335 reg = STV090x_READ_DEMOD(state, TSCFGH);
3336 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
3337 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3338 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003339
Manu Abrahame415c682009-04-06 15:45:20 -03003340 msleep(3);
Manu Abraham27d40322009-05-02 18:26:58 -03003341
Manu Abrahame415c682009-04-06 15:45:20 -03003342 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
3343 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3344 goto err;
3345
3346 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
3347 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3348 goto err;
3349 }
3350
Andreas Regela4978a82009-11-13 18:17:45 -03003351 lock = stv090x_get_lock(state, state->FecTimeout,
3352 state->FecTimeout);
3353 if (lock) {
Manu Abrahame415c682009-04-06 15:45:20 -03003354 if (state->delsys == STV090x_DVBS2) {
3355 stv090x_set_s2rolloff(state);
Manu Abraham27d40322009-05-02 18:26:58 -03003356
3357 reg = STV090x_READ_DEMOD(state, PDELCTRL2);
3358 STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 1);
3359 if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003360 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003361 /* Reset DVBS2 packet delinator error counter */
3362 reg = STV090x_READ_DEMOD(state, PDELCTRL2);
3363 STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 0);
3364 if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003365 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003366
Manu Abrahame415c682009-04-06 15:45:20 -03003367 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67) < 0) /* PER */
3368 goto err;
3369 } else {
3370 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
3371 goto err;
3372 }
Manu Abraham27d40322009-05-02 18:26:58 -03003373 /* Reset the Total packet counter */
Manu Abrahame415c682009-04-06 15:45:20 -03003374 if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0x00) < 0)
3375 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003376 /* Reset the packet Error counter2 */
Manu Abrahame415c682009-04-06 15:45:20 -03003377 if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3378 goto err;
3379 } else {
Manu Abrahame415c682009-04-06 15:45:20 -03003380 signal_state = STV090x_NODATA;
3381 no_signal = stv090x_chk_signal(state);
3382 }
3383 }
Manu Abrahame415c682009-04-06 15:45:20 -03003384 return signal_state;
3385
3386err:
3387 dprintk(FE_ERROR, 1, "I/O error");
3388 return -1;
3389}
3390
3391static enum dvbfe_search stv090x_search(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
3392{
3393 struct stv090x_state *state = fe->demodulator_priv;
3394 struct dtv_frontend_properties *props = &fe->dtv_property_cache;
3395
3396 state->delsys = props->delivery_system;
3397 state->frequency = p->frequency;
3398 state->srate = p->u.qpsk.symbol_rate;
Andreas Regel4e58a682009-04-16 08:40:36 -03003399 state->search_mode = STV090x_SEARCH_AUTO;
3400 state->algo = STV090x_COLD_SEARCH;
3401 state->fec = STV090x_PRERR;
Andreas Regelc879d8c2009-11-13 18:11:26 -03003402 if (state->srate > 10000000) {
3403 dprintk(FE_DEBUG, 1, "Search range: 10 MHz");
3404 state->search_range = 10000000;
3405 } else {
3406 dprintk(FE_DEBUG, 1, "Search range: 5 MHz");
3407 state->search_range = 5000000;
3408 }
Manu Abrahame415c682009-04-06 15:45:20 -03003409
Andreas Regel2f5914b2009-04-23 14:56:41 -03003410 if (stv090x_algo(state) == STV090x_RANGEOK) {
Manu Abrahame415c682009-04-06 15:45:20 -03003411 dprintk(FE_DEBUG, 1, "Search success!");
3412 return DVBFE_ALGO_SEARCH_SUCCESS;
3413 } else {
3414 dprintk(FE_DEBUG, 1, "Search failed!");
3415 return DVBFE_ALGO_SEARCH_FAILED;
3416 }
3417
3418 return DVBFE_ALGO_SEARCH_ERROR;
3419}
3420
Manu Abrahame415c682009-04-06 15:45:20 -03003421static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
3422{
3423 struct stv090x_state *state = fe->demodulator_priv;
3424 u32 reg;
3425 u8 search_state;
Manu Abrahame415c682009-04-06 15:45:20 -03003426
3427 reg = STV090x_READ_DEMOD(state, DMDSTATE);
3428 search_state = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
3429
3430 switch (search_state) {
3431 case 0: /* searching */
3432 case 1: /* first PLH detected */
3433 default:
3434 dprintk(FE_DEBUG, 1, "Status: Unlocked (Searching ..)");
Andreas Regel9629c5b2009-04-23 14:58:36 -03003435 *status = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003436 break;
3437
3438 case 2: /* DVB-S2 mode */
3439 dprintk(FE_DEBUG, 1, "Delivery system: DVB-S2");
3440 reg = STV090x_READ_DEMOD(state, DSTATUS);
3441 if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
Andreas Regel1d436172009-11-13 18:19:54 -03003442 reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
3443 if (STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD)) {
3444 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3445 if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
3446 *status = FE_HAS_CARRIER |
3447 FE_HAS_VITERBI |
3448 FE_HAS_SYNC |
3449 FE_HAS_LOCK;
3450 }
Manu Abrahame415c682009-04-06 15:45:20 -03003451 }
3452 }
3453 break;
3454
3455 case 3: /* DVB-S1/legacy mode */
3456 dprintk(FE_DEBUG, 1, "Delivery system: DVB-S");
3457 reg = STV090x_READ_DEMOD(state, DSTATUS);
3458 if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
3459 reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
3460 if (STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD)) {
3461 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3462 if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
Manu Abrahame415c682009-04-06 15:45:20 -03003463 *status = FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
3464 }
3465 }
3466 }
3467 break;
3468 }
3469
Andreas Regel9629c5b2009-04-23 14:58:36 -03003470 return 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003471}
3472
3473static int stv090x_read_per(struct dvb_frontend *fe, u32 *per)
3474{
3475 struct stv090x_state *state = fe->demodulator_priv;
3476
3477 s32 count_4, count_3, count_2, count_1, count_0, count;
3478 u32 reg, h, m, l;
3479 enum fe_status status;
3480
Andreas Regel9629c5b2009-04-23 14:58:36 -03003481 stv090x_read_status(fe, &status);
3482 if (!(status & FE_HAS_LOCK)) {
Manu Abrahame415c682009-04-06 15:45:20 -03003483 *per = 1 << 23; /* Max PER */
3484 } else {
3485 /* Counter 2 */
3486 reg = STV090x_READ_DEMOD(state, ERRCNT22);
3487 h = STV090x_GETFIELD_Px(reg, ERR_CNT2_FIELD);
3488
3489 reg = STV090x_READ_DEMOD(state, ERRCNT21);
3490 m = STV090x_GETFIELD_Px(reg, ERR_CNT21_FIELD);
3491
3492 reg = STV090x_READ_DEMOD(state, ERRCNT20);
3493 l = STV090x_GETFIELD_Px(reg, ERR_CNT20_FIELD);
3494
3495 *per = ((h << 16) | (m << 8) | l);
3496
3497 count_4 = STV090x_READ_DEMOD(state, FBERCPT4);
3498 count_3 = STV090x_READ_DEMOD(state, FBERCPT3);
3499 count_2 = STV090x_READ_DEMOD(state, FBERCPT2);
3500 count_1 = STV090x_READ_DEMOD(state, FBERCPT1);
3501 count_0 = STV090x_READ_DEMOD(state, FBERCPT0);
3502
3503 if ((!count_4) && (!count_3)) {
3504 count = (count_2 & 0xff) << 16;
3505 count |= (count_1 & 0xff) << 8;
3506 count |= count_0 & 0xff;
3507 } else {
3508 count = 1 << 24;
3509 }
3510 if (count == 0)
3511 *per = 1;
3512 }
3513 if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0) < 0)
3514 goto err;
3515 if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3516 goto err;
3517
3518 return 0;
3519err:
3520 dprintk(FE_ERROR, 1, "I/O error");
3521 return -1;
3522}
3523
3524static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
3525{
3526 int res = 0;
3527 int min = 0, med;
3528
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003529 if ((val >= tab[min].read && val < tab[max].read) ||
3530 (val >= tab[max].read && val < tab[min].read)) {
Manu Abrahame415c682009-04-06 15:45:20 -03003531 while ((max - min) > 1) {
3532 med = (max + min) / 2;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003533 if ((val >= tab[min].read && val < tab[med].read) ||
3534 (val >= tab[med].read && val < tab[min].read))
Manu Abrahame415c682009-04-06 15:45:20 -03003535 max = med;
3536 else
3537 min = med;
3538 }
3539 res = ((val - tab[min].read) *
3540 (tab[max].real - tab[min].real) /
3541 (tab[max].read - tab[min].read)) +
3542 tab[min].real;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003543 } else {
3544 if (tab[min].read < tab[max].read) {
3545 if (val < tab[min].read)
3546 res = tab[min].real;
3547 else if (val >= tab[max].read)
3548 res = tab[max].real;
3549 } else {
3550 if (val >= tab[min].read)
3551 res = tab[min].real;
3552 else if (val < tab[max].read)
3553 res = tab[max].real;
3554 }
Manu Abrahame415c682009-04-06 15:45:20 -03003555 }
3556
3557 return res;
3558}
3559
3560static int stv090x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
3561{
3562 struct stv090x_state *state = fe->demodulator_priv;
3563 u32 reg;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003564 s32 agc_0, agc_1, agc;
3565 s32 str;
Manu Abrahame415c682009-04-06 15:45:20 -03003566
3567 reg = STV090x_READ_DEMOD(state, AGCIQIN1);
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003568 agc_1 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
3569 reg = STV090x_READ_DEMOD(state, AGCIQIN0);
3570 agc_0 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
3571 agc = MAKEWORD16(agc_1, agc_0);
Manu Abrahame415c682009-04-06 15:45:20 -03003572
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003573 str = stv090x_table_lookup(stv090x_rf_tab,
3574 ARRAY_SIZE(stv090x_rf_tab) - 1, agc);
Manu Abrahame415c682009-04-06 15:45:20 -03003575 if (agc > stv090x_rf_tab[0].read)
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003576 str = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003577 else if (agc < stv090x_rf_tab[ARRAY_SIZE(stv090x_rf_tab) - 1].read)
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003578 str = -100;
3579 *strength = (str + 100) * 0xFFFF / 100;
Manu Abrahame415c682009-04-06 15:45:20 -03003580
3581 return 0;
3582}
3583
3584static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
3585{
3586 struct stv090x_state *state = fe->demodulator_priv;
3587 u32 reg_0, reg_1, reg, i;
3588 s32 val_0, val_1, val = 0;
3589 u8 lock_f;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003590 s32 div;
3591 u32 last;
Manu Abrahame415c682009-04-06 15:45:20 -03003592
3593 switch (state->delsys) {
3594 case STV090x_DVBS2:
3595 reg = STV090x_READ_DEMOD(state, DSTATUS);
3596 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
3597 if (lock_f) {
3598 msleep(5);
3599 for (i = 0; i < 16; i++) {
3600 reg_1 = STV090x_READ_DEMOD(state, NNOSPLHT1);
3601 val_1 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
3602 reg_0 = STV090x_READ_DEMOD(state, NNOSPLHT0);
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003603 val_0 = STV090x_GETFIELD_Px(reg_0, NOSPLHT_NORMED_FIELD);
Manu Abrahame415c682009-04-06 15:45:20 -03003604 val += MAKEWORD16(val_1, val_0);
3605 msleep(1);
3606 }
3607 val /= 16;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003608 last = ARRAY_SIZE(stv090x_s2cn_tab) - 1;
3609 div = stv090x_s2cn_tab[0].read -
3610 stv090x_s2cn_tab[last].read;
3611 *cnr = 0xFFFF - ((val * 0xFFFF) / div);
Manu Abrahame415c682009-04-06 15:45:20 -03003612 }
3613 break;
3614
3615 case STV090x_DVBS1:
3616 case STV090x_DSS:
3617 reg = STV090x_READ_DEMOD(state, DSTATUS);
3618 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
3619 if (lock_f) {
3620 msleep(5);
3621 for (i = 0; i < 16; i++) {
3622 reg_1 = STV090x_READ_DEMOD(state, NOSDATAT1);
3623 val_1 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
3624 reg_0 = STV090x_READ_DEMOD(state, NOSDATAT0);
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003625 val_0 = STV090x_GETFIELD_Px(reg_0, NOSDATAT_UNNORMED_FIELD);
Manu Abrahame415c682009-04-06 15:45:20 -03003626 val += MAKEWORD16(val_1, val_0);
3627 msleep(1);
3628 }
3629 val /= 16;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003630 last = ARRAY_SIZE(stv090x_s1cn_tab) - 1;
3631 div = stv090x_s1cn_tab[0].read -
3632 stv090x_s1cn_tab[last].read;
3633 *cnr = 0xFFFF - ((val * 0xFFFF) / div);
Manu Abrahame415c682009-04-06 15:45:20 -03003634 }
3635 break;
3636 default:
3637 break;
3638 }
3639
3640 return 0;
3641}
3642
3643static int stv090x_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
3644{
3645 struct stv090x_state *state = fe->demodulator_priv;
3646 u32 reg;
3647
3648 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3649 switch (tone) {
3650 case SEC_TONE_ON:
3651 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
3652 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3653 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3654 goto err;
3655 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3656 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3657 goto err;
3658 break;
3659
3660 case SEC_TONE_OFF:
3661 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
3662 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3663 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3664 goto err;
3665 break;
3666 default:
3667 return -EINVAL;
3668 }
3669
3670 return 0;
3671err:
3672 dprintk(FE_ERROR, 1, "I/O error");
3673 return -1;
3674}
3675
3676
3677static enum dvbfe_algo stv090x_frontend_algo(struct dvb_frontend *fe)
3678{
3679 return DVBFE_ALGO_CUSTOM;
3680}
3681
3682static int stv090x_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
3683{
3684 struct stv090x_state *state = fe->demodulator_priv;
3685 u32 reg, idle = 0, fifo_full = 1;
3686 int i;
3687
3688 reg = STV090x_READ_DEMOD(state, DISTXCTL);
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003689
Oliver Endriss30e8ca22009-12-13 09:02:39 -03003690 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD,
3691 (state->config->diseqc_envelope_mode) ? 4 : 2);
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003692 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3693 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3694 goto err;
3695 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3696 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3697 goto err;
3698
Manu Abrahame415c682009-04-06 15:45:20 -03003699 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3700 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3701 goto err;
3702
3703 for (i = 0; i < cmd->msg_len; i++) {
3704
3705 while (fifo_full) {
3706 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3707 fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
3708 }
3709
3710 if (STV090x_WRITE_DEMOD(state, DISTXDATA, cmd->msg[i]) < 0)
3711 goto err;
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003712 }
3713 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3714 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
3715 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3716 goto err;
3717
3718 i = 0;
3719
3720 while ((!idle) && (i < 10)) {
3721 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3722 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3723 msleep(10);
Manu Abrahame415c682009-04-06 15:45:20 -03003724 i++;
3725 }
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003726
3727 return 0;
3728err:
3729 dprintk(FE_ERROR, 1, "I/O error");
3730 return -1;
3731}
3732
3733static int stv090x_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
3734{
3735 struct stv090x_state *state = fe->demodulator_priv;
3736 u32 reg, idle = 0, fifo_full = 1;
3737 u8 mode, value;
3738 int i;
3739
3740 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3741
3742 if (burst == SEC_MINI_A) {
Oliver Endriss30e8ca22009-12-13 09:02:39 -03003743 mode = (state->config->diseqc_envelope_mode) ? 5 : 3;
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003744 value = 0x00;
3745 } else {
Oliver Endriss30e8ca22009-12-13 09:02:39 -03003746 mode = (state->config->diseqc_envelope_mode) ? 4 : 2;
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003747 value = 0xFF;
3748 }
3749
3750 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, mode);
3751 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3752 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3753 goto err;
3754 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3755 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3756 goto err;
3757
3758 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3759 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3760 goto err;
3761
3762 while (fifo_full) {
3763 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3764 fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
3765 }
3766
3767 if (STV090x_WRITE_DEMOD(state, DISTXDATA, value) < 0)
3768 goto err;
3769
Manu Abrahame415c682009-04-06 15:45:20 -03003770 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3771 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
3772 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3773 goto err;
3774
3775 i = 0;
3776
3777 while ((!idle) && (i < 10)) {
3778 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3779 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3780 msleep(10);
3781 i++;
3782 }
3783
3784 return 0;
3785err:
3786 dprintk(FE_ERROR, 1, "I/O error");
3787 return -1;
3788}
3789
3790static int stv090x_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
3791{
3792 struct stv090x_state *state = fe->demodulator_priv;
3793 u32 reg = 0, i = 0, rx_end = 0;
3794
3795 while ((rx_end != 1) && (i < 10)) {
3796 msleep(10);
3797 i++;
3798 reg = STV090x_READ_DEMOD(state, DISRX_ST0);
3799 rx_end = STV090x_GETFIELD_Px(reg, RX_END_FIELD);
3800 }
3801
3802 if (rx_end) {
3803 reply->msg_len = STV090x_GETFIELD_Px(reg, FIFO_BYTENBR_FIELD);
3804 for (i = 0; i < reply->msg_len; i++)
3805 reply->msg[i] = STV090x_READ_DEMOD(state, DISRXDATA);
3806 }
3807
3808 return 0;
3809}
3810
3811static int stv090x_sleep(struct dvb_frontend *fe)
3812{
3813 struct stv090x_state *state = fe->demodulator_priv;
3814 u32 reg;
3815
3816 dprintk(FE_DEBUG, 1, "Set %s to sleep",
3817 state->device == STV0900 ? "STV0900" : "STV0903");
3818
3819 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3820 STV090x_SETFIELD(reg, STANDBY_FIELD, 0x01);
3821 if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
3822 goto err;
3823
Manu Abraham26b03bc2009-04-08 18:27:10 -03003824 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
3825 STV090x_SETFIELD(reg, ADC1_PON_FIELD, 0);
3826 if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
3827 goto err;
3828
Manu Abrahame415c682009-04-06 15:45:20 -03003829 return 0;
3830err:
3831 dprintk(FE_ERROR, 1, "I/O error");
3832 return -1;
3833}
3834
3835static int stv090x_wakeup(struct dvb_frontend *fe)
3836{
3837 struct stv090x_state *state = fe->demodulator_priv;
3838 u32 reg;
3839
3840 dprintk(FE_DEBUG, 1, "Wake %s from standby",
3841 state->device == STV0900 ? "STV0900" : "STV0903");
3842
3843 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3844 STV090x_SETFIELD(reg, STANDBY_FIELD, 0x00);
3845 if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
3846 goto err;
3847
Manu Abraham26b03bc2009-04-08 18:27:10 -03003848 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
3849 STV090x_SETFIELD(reg, ADC1_PON_FIELD, 1);
3850 if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
3851 goto err;
3852
Manu Abrahame415c682009-04-06 15:45:20 -03003853 return 0;
3854err:
3855 dprintk(FE_ERROR, 1, "I/O error");
3856 return -1;
3857}
3858
3859static void stv090x_release(struct dvb_frontend *fe)
3860{
3861 struct stv090x_state *state = fe->demodulator_priv;
3862
Andreas Regel97f7a2a2010-01-05 19:19:43 -03003863 state->internal->num_used--;
3864 if (state->internal->num_used <= 0) {
3865
3866 dprintk(FE_ERROR, 1, "Actually removing");
3867
3868 remove_dev(state->internal);
3869 kfree(state->internal);
3870 }
3871
Manu Abrahame415c682009-04-06 15:45:20 -03003872 kfree(state);
3873}
3874
3875static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc_mode)
3876{
Manu Abraham27d40322009-05-02 18:26:58 -03003877 u32 reg = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003878
Andreas Regela4978a82009-11-13 18:17:45 -03003879 reg = stv090x_read_reg(state, STV090x_GENCFG);
3880
Manu Abrahame415c682009-04-06 15:45:20 -03003881 switch (ldpc_mode) {
3882 case STV090x_DUAL:
3883 default:
Manu Abrahame415c682009-04-06 15:45:20 -03003884 if ((state->demod_mode != STV090x_DUAL) || (STV090x_GETFIELD(reg, DDEMOD_FIELD) != 1)) {
Manu Abraham27d40322009-05-02 18:26:58 -03003885 /* set LDPC to dual mode */
3886 if (stv090x_write_reg(state, STV090x_GENCFG, 0x1d) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003887 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003888
Manu Abrahame415c682009-04-06 15:45:20 -03003889 state->demod_mode = STV090x_DUAL;
Manu Abraham27d40322009-05-02 18:26:58 -03003890
Manu Abrahame415c682009-04-06 15:45:20 -03003891 reg = stv090x_read_reg(state, STV090x_TSTRES0);
3892 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
3893 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3894 goto err;
3895 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
3896 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3897 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003898
3899 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
3900 goto err;
3901 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
3902 goto err;
3903 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
3904 goto err;
3905 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
3906 goto err;
3907 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
3908 goto err;
3909 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
3910 goto err;
3911 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
3912 goto err;
3913
3914 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
3915 goto err;
3916 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
3917 goto err;
3918 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
3919 goto err;
3920 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
3921 goto err;
3922 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
3923 goto err;
3924 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
3925 goto err;
3926 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
3927 goto err;
3928
3929 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
3930 goto err;
3931 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
3932 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003933 }
3934 break;
3935
3936 case STV090x_SINGLE:
Manu Abraham27d40322009-05-02 18:26:58 -03003937 if (stv090x_stop_modcod(state) < 0)
3938 goto err;
3939 if (stv090x_activate_modcod_single(state) < 0)
3940 goto err;
3941
Manu Abrahame415c682009-04-06 15:45:20 -03003942 if (state->demod == STV090x_DEMODULATOR_1) {
3943 if (stv090x_write_reg(state, STV090x_GENCFG, 0x06) < 0) /* path 2 */
3944 goto err;
3945 } else {
3946 if (stv090x_write_reg(state, STV090x_GENCFG, 0x04) < 0) /* path 1 */
3947 goto err;
3948 }
3949
3950 reg = stv090x_read_reg(state, STV090x_TSTRES0);
3951 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
3952 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3953 goto err;
3954 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
3955 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3956 goto err;
3957
3958 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
3959 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x01);
3960 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
3961 goto err;
3962 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x00);
3963 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
3964 goto err;
3965 break;
3966 }
3967
3968 return 0;
3969err:
3970 dprintk(FE_ERROR, 1, "I/O error");
3971 return -1;
3972}
3973
3974/* return (Hz), clk in Hz*/
3975static u32 stv090x_get_mclk(struct stv090x_state *state)
3976{
3977 const struct stv090x_config *config = state->config;
3978 u32 div, reg;
3979 u8 ratio;
3980
3981 div = stv090x_read_reg(state, STV090x_NCOARSE);
3982 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3983 ratio = STV090x_GETFIELD(reg, SELX1RATIO_FIELD) ? 4 : 6;
3984
3985 return (div + 1) * config->xtal / ratio; /* kHz */
3986}
3987
3988static int stv090x_set_mclk(struct stv090x_state *state, u32 mclk, u32 clk)
3989{
3990 const struct stv090x_config *config = state->config;
3991 u32 reg, div, clk_sel;
3992
3993 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3994 clk_sel = ((STV090x_GETFIELD(reg, SELX1RATIO_FIELD) == 1) ? 4 : 6);
3995
3996 div = ((clk_sel * mclk) / config->xtal) - 1;
3997
3998 reg = stv090x_read_reg(state, STV090x_NCOARSE);
3999 STV090x_SETFIELD(reg, M_DIV_FIELD, div);
4000 if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0)
4001 goto err;
4002
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004003 state->internal->mclk = stv090x_get_mclk(state);
Manu Abrahame415c682009-04-06 15:45:20 -03004004
Manu Abraham94a80912009-04-08 19:45:43 -03004005 /*Set the DiseqC frequency to 22KHz */
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004006 div = state->internal->mclk / 704000;
Manu Abraham94a80912009-04-08 19:45:43 -03004007 if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0)
4008 goto err;
4009 if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
4010 goto err;
4011
Manu Abrahame415c682009-04-06 15:45:20 -03004012 return 0;
4013err:
4014 dprintk(FE_ERROR, 1, "I/O error");
4015 return -1;
4016}
4017
4018static int stv090x_set_tspath(struct stv090x_state *state)
4019{
4020 u32 reg;
4021
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004022 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03004023 switch (state->config->ts1_mode) {
4024 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4025 case STV090x_TSMODE_DVBCI:
4026 switch (state->config->ts2_mode) {
4027 case STV090x_TSMODE_SERIAL_PUNCTURED:
4028 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4029 default:
4030 stv090x_write_reg(state, STV090x_TSGENERAL, 0x00);
4031 break;
4032
4033 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4034 case STV090x_TSMODE_DVBCI:
4035 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x06) < 0) /* Mux'd stream mode */
4036 goto err;
4037 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4038 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4039 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4040 goto err;
4041 reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
4042 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4043 if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
4044 goto err;
4045 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
4046 goto err;
4047 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
4048 goto err;
4049 break;
4050 }
4051 break;
4052
4053 case STV090x_TSMODE_SERIAL_PUNCTURED:
4054 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4055 default:
4056 switch (state->config->ts2_mode) {
4057 case STV090x_TSMODE_SERIAL_PUNCTURED:
4058 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4059 default:
4060 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
4061 goto err;
4062 break;
4063
4064 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4065 case STV090x_TSMODE_DVBCI:
4066 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0a) < 0)
4067 goto err;
4068 break;
4069 }
4070 break;
4071 }
4072 } else {
4073 switch (state->config->ts1_mode) {
4074 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4075 case STV090x_TSMODE_DVBCI:
4076 switch (state->config->ts2_mode) {
4077 case STV090x_TSMODE_SERIAL_PUNCTURED:
4078 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4079 default:
Manu Abraham56571502009-04-07 16:08:26 -03004080 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x10);
Manu Abrahame415c682009-04-06 15:45:20 -03004081 break;
4082
4083 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4084 case STV090x_TSMODE_DVBCI:
Manu Abraham56571502009-04-07 16:08:26 -03004085 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x16);
Manu Abrahame415c682009-04-06 15:45:20 -03004086 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4087 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4088 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4089 goto err;
4090 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4091 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 0);
4092 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4093 goto err;
4094 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
4095 goto err;
4096 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
4097 goto err;
4098 break;
4099 }
4100 break;
4101
4102 case STV090x_TSMODE_SERIAL_PUNCTURED:
4103 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4104 default:
4105 switch (state->config->ts2_mode) {
4106 case STV090x_TSMODE_SERIAL_PUNCTURED:
4107 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4108 default:
Manu Abraham56571502009-04-07 16:08:26 -03004109 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
Manu Abrahame415c682009-04-06 15:45:20 -03004110 break;
4111
4112 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4113 case STV090x_TSMODE_DVBCI:
Manu Abraham56571502009-04-07 16:08:26 -03004114 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x12);
Manu Abrahame415c682009-04-06 15:45:20 -03004115 break;
4116 }
4117 break;
4118 }
4119 }
4120
4121 switch (state->config->ts1_mode) {
4122 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4123 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4124 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4125 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4126 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4127 goto err;
4128 break;
4129
4130 case STV090x_TSMODE_DVBCI:
4131 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4132 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4133 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4134 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4135 goto err;
4136 break;
4137
4138 case STV090x_TSMODE_SERIAL_PUNCTURED:
4139 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4140 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4141 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4142 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4143 goto err;
4144 break;
4145
4146 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4147 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4148 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4149 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4150 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4151 goto err;
4152 break;
4153
4154 default:
4155 break;
4156 }
4157
4158 switch (state->config->ts2_mode) {
4159 case STV090x_TSMODE_PARALLEL_PUNCTURED:
Andreas Regel64104dc2009-04-16 08:43:41 -03004160 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004161 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4162 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
Andreas Regel64104dc2009-04-16 08:43:41 -03004163 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004164 goto err;
4165 break;
4166
4167 case STV090x_TSMODE_DVBCI:
Andreas Regel64104dc2009-04-16 08:43:41 -03004168 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004169 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4170 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
Andreas Regel64104dc2009-04-16 08:43:41 -03004171 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004172 goto err;
4173 break;
4174
4175 case STV090x_TSMODE_SERIAL_PUNCTURED:
Andreas Regel64104dc2009-04-16 08:43:41 -03004176 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004177 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4178 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
Andreas Regel64104dc2009-04-16 08:43:41 -03004179 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004180 goto err;
4181 break;
4182
4183 case STV090x_TSMODE_SERIAL_CONTINUOUS:
Andreas Regel64104dc2009-04-16 08:43:41 -03004184 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004185 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4186 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
Andreas Regel64104dc2009-04-16 08:43:41 -03004187 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004188 goto err;
4189 break;
4190
4191 default:
4192 break;
4193 }
4194 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4195 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
4196 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4197 goto err;
4198 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4199 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4200 goto err;
4201
4202 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4203 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
4204 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4205 goto err;
4206 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4207 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4208 goto err;
4209
4210 return 0;
4211err:
4212 dprintk(FE_ERROR, 1, "I/O error");
4213 return -1;
4214}
4215
4216static int stv090x_init(struct dvb_frontend *fe)
4217{
4218 struct stv090x_state *state = fe->demodulator_priv;
4219 const struct stv090x_config *config = state->config;
4220 u32 reg;
4221
Andreas Regelcbc320d2009-04-23 14:59:03 -03004222 if (stv090x_wakeup(fe) < 0) {
4223 dprintk(FE_ERROR, 1, "Error waking device");
4224 goto err;
4225 }
4226
Manu Abraham27d40322009-05-02 18:26:58 -03004227 if (stv090x_ldpc_mode(state, state->demod_mode) < 0)
4228 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03004229
4230 reg = STV090x_READ_DEMOD(state, TNRCFG2);
4231 STV090x_SETFIELD_Px(reg, TUN_IQSWAP_FIELD, state->inversion);
4232 if (STV090x_WRITE_DEMOD(state, TNRCFG2, reg) < 0)
4233 goto err;
4234 reg = STV090x_READ_DEMOD(state, DEMOD);
4235 STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
4236 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
4237 goto err;
4238
Manu Abraham27d40322009-05-02 18:26:58 -03004239 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
4240 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03004241
Manu Abraham27d40322009-05-02 18:26:58 -03004242 if (config->tuner_set_mode) {
4243 if (config->tuner_set_mode(fe, TUNER_WAKE) < 0)
4244 goto err;
4245 }
Manu Abrahame415c682009-04-06 15:45:20 -03004246
Manu Abraham27d40322009-05-02 18:26:58 -03004247 if (config->tuner_init) {
4248 if (config->tuner_init(fe) < 0)
4249 goto err;
4250 }
Manu Abrahame415c682009-04-06 15:45:20 -03004251
Manu Abraham27d40322009-05-02 18:26:58 -03004252 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
4253 goto err;
4254
4255 if (stv090x_set_tspath(state) < 0)
4256 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03004257
4258 return 0;
4259err:
4260 dprintk(FE_ERROR, 1, "I/O error");
4261 return -1;
4262}
4263
4264static int stv090x_setup(struct dvb_frontend *fe)
4265{
4266 struct stv090x_state *state = fe->demodulator_priv;
4267 const struct stv090x_config *config = state->config;
4268 const struct stv090x_reg *stv090x_initval = NULL;
4269 const struct stv090x_reg *stv090x_cut20_val = NULL;
4270 unsigned long t1_size = 0, t2_size = 0;
Manu Abraham017eb0382009-04-07 05:19:54 -03004271 u32 reg = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03004272
4273 int i;
4274
4275 if (state->device == STV0900) {
4276 dprintk(FE_DEBUG, 1, "Initializing STV0900");
4277 stv090x_initval = stv0900_initval;
4278 t1_size = ARRAY_SIZE(stv0900_initval);
4279 stv090x_cut20_val = stv0900_cut20_val;
4280 t2_size = ARRAY_SIZE(stv0900_cut20_val);
4281 } else if (state->device == STV0903) {
4282 dprintk(FE_DEBUG, 1, "Initializing STV0903");
4283 stv090x_initval = stv0903_initval;
4284 t1_size = ARRAY_SIZE(stv0903_initval);
4285 stv090x_cut20_val = stv0903_cut20_val;
4286 t2_size = ARRAY_SIZE(stv0903_cut20_val);
4287 }
4288
4289 /* STV090x init */
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004290
4291 /* Stop Demod */
4292 if (stv090x_write_reg(state, STV090x_P1_DMDISTATE, 0x5c) < 0)
4293 goto err;
4294 if (stv090x_write_reg(state, STV090x_P2_DMDISTATE, 0x5c) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004295 goto err;
4296
4297 msleep(5);
4298
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004299 /* Set No Tuner Mode */
4300 if (stv090x_write_reg(state, STV090x_P1_TNRCFG, 0x6c) < 0)
4301 goto err;
4302 if (stv090x_write_reg(state, STV090x_P2_TNRCFG, 0x6c) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004303 goto err;
4304
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004305 /* I2C repeater OFF */
Manu Abraham017eb0382009-04-07 05:19:54 -03004306 STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level);
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004307 if (stv090x_write_reg(state, STV090x_P1_I2CRPT, reg) < 0)
4308 goto err;
4309 if (stv090x_write_reg(state, STV090x_P2_I2CRPT, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004310 goto err;
4311
4312 if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
4313 goto err;
4314 msleep(5);
4315 if (stv090x_write_reg(state, STV090x_I2CCFG, 0x08) < 0) /* 1/41 oversampling */
4316 goto err;
4317 if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0) /* enable PLL */
4318 goto err;
4319 msleep(5);
4320
4321 /* write initval */
Andreas Regel2f5914b2009-04-23 14:56:41 -03004322 dprintk(FE_DEBUG, 1, "Setting up initial values");
Manu Abrahame415c682009-04-06 15:45:20 -03004323 for (i = 0; i < t1_size; i++) {
Manu Abrahame415c682009-04-06 15:45:20 -03004324 if (stv090x_write_reg(state, stv090x_initval[i].addr, stv090x_initval[i].data) < 0)
4325 goto err;
4326 }
4327
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004328 state->internal->dev_ver = stv090x_read_reg(state, STV090x_MID);
4329 if (state->internal->dev_ver >= 0x20) {
Manu Abrahame415c682009-04-06 15:45:20 -03004330 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
4331 goto err;
4332
4333 /* write cut20_val*/
4334 dprintk(FE_DEBUG, 1, "Setting up Cut 2.0 initial values");
4335 for (i = 0; i < t2_size; i++) {
4336 if (stv090x_write_reg(state, stv090x_cut20_val[i].addr, stv090x_cut20_val[i].data) < 0)
4337 goto err;
4338 }
Manu Abraham27d40322009-05-02 18:26:58 -03004339
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004340 } else if (state->internal->dev_ver < 0x20) {
Manu Abraham27d40322009-05-02 18:26:58 -03004341 dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!",
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004342 state->internal->dev_ver);
Manu Abraham27d40322009-05-02 18:26:58 -03004343
4344 goto err;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004345 } else if (state->internal->dev_ver > 0x30) {
Manu Abraham27d40322009-05-02 18:26:58 -03004346 /* we shouldn't bail out from here */
4347 dprintk(FE_ERROR, 1, "INFO: Cut: 0x%02x probably incomplete support!",
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004348 state->internal->dev_ver);
Manu Abrahame415c682009-04-06 15:45:20 -03004349 }
4350
4351 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
4352 goto err;
4353 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x00) < 0)
4354 goto err;
4355
4356 stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
4357 msleep(5);
4358 if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0)
4359 goto err;
4360 stv090x_get_mclk(state);
4361
4362 return 0;
4363err:
4364 dprintk(FE_ERROR, 1, "I/O error");
4365 return -1;
4366}
4367
4368static struct dvb_frontend_ops stv090x_ops = {
4369
4370 .info = {
4371 .name = "STV090x Multistandard",
Andreas Regel7fc08bb2009-04-23 15:00:40 -03004372 .type = FE_QPSK,
4373 .frequency_min = 950000,
4374 .frequency_max = 2150000,
4375 .frequency_stepsize = 0,
4376 .frequency_tolerance = 0,
4377 .symbol_rate_min = 1000000,
4378 .symbol_rate_max = 45000000,
4379 .caps = FE_CAN_INVERSION_AUTO |
4380 FE_CAN_FEC_AUTO |
4381 FE_CAN_QPSK |
4382 FE_CAN_2G_MODULATION
Manu Abrahame415c682009-04-06 15:45:20 -03004383 },
4384
4385 .release = stv090x_release,
4386 .init = stv090x_init,
4387
4388 .sleep = stv090x_sleep,
4389 .get_frontend_algo = stv090x_frontend_algo,
4390
4391 .i2c_gate_ctrl = stv090x_i2c_gate_ctrl,
4392
4393 .diseqc_send_master_cmd = stv090x_send_diseqc_msg,
Andreas Regelf9ed95d2009-04-08 17:27:51 -03004394 .diseqc_send_burst = stv090x_send_diseqc_burst,
Manu Abrahame415c682009-04-06 15:45:20 -03004395 .diseqc_recv_slave_reply = stv090x_recv_slave_reply,
4396 .set_tone = stv090x_set_tone,
4397
4398 .search = stv090x_search,
4399 .read_status = stv090x_read_status,
4400 .read_ber = stv090x_read_per,
4401 .read_signal_strength = stv090x_read_signal_strength,
4402 .read_snr = stv090x_read_cnr
4403};
4404
4405
4406struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
4407 struct i2c_adapter *i2c,
4408 enum stv090x_demodulator demod)
4409{
4410 struct stv090x_state *state = NULL;
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004411 struct stv090x_dev *temp_int;
Manu Abrahame415c682009-04-06 15:45:20 -03004412
4413 state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL);
4414 if (state == NULL)
4415 goto error;
4416
4417 state->verbose = &verbose;
4418 state->config = config;
4419 state->i2c = i2c;
4420 state->frontend.ops = stv090x_ops;
4421 state->frontend.demodulator_priv = state;
Manu Abraham56571502009-04-07 16:08:26 -03004422 state->demod = demod;
Manu Abrahame415c682009-04-06 15:45:20 -03004423 state->demod_mode = config->demod_mode; /* Single or Dual mode */
4424 state->device = config->device;
Andreas Regel4e58a682009-04-16 08:40:36 -03004425 state->rolloff = STV090x_RO_35; /* default */
Manu Abrahame415c682009-04-06 15:45:20 -03004426
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004427 temp_int = find_dev(state->i2c,
4428 state->config->address);
4429
4430 if ((temp_int != NULL) && (state->demod_mode == STV090x_DUAL)) {
4431 state->internal = temp_int->internal;
4432 state->internal->num_used++;
4433 dprintk(FE_INFO, 1, "Found Internal Structure!");
4434 dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
4435 state->device == STV0900 ? "STV0900" : "STV0903",
4436 demod,
4437 state->internal->dev_ver);
4438 return &state->frontend;
4439 } else {
4440 state->internal = kmalloc(sizeof(struct stv090x_internal),
4441 GFP_KERNEL);
4442 temp_int = append_internal(state->internal);
4443 state->internal->num_used = 1;
4444 state->internal->i2c_adap = state->i2c;
4445 state->internal->i2c_addr = state->config->address;
4446 dprintk(FE_INFO, 1, "Create New Internal Structure!");
4447 }
4448
4449 mutex_init(&state->internal->demod_lock);
Andreas Regel96506a52010-01-05 19:20:21 -03004450 mutex_init(&state->internal->tuner_lock);
Manu Abrahame415c682009-04-06 15:45:20 -03004451
4452 if (stv090x_sleep(&state->frontend) < 0) {
4453 dprintk(FE_ERROR, 1, "Error putting device to sleep");
4454 goto error;
4455 }
4456
4457 if (stv090x_setup(&state->frontend) < 0) {
4458 dprintk(FE_ERROR, 1, "Error setting up device");
4459 goto error;
4460 }
4461 if (stv090x_wakeup(&state->frontend) < 0) {
4462 dprintk(FE_ERROR, 1, "Error waking device");
4463 goto error;
4464 }
Manu Abrahame415c682009-04-06 15:45:20 -03004465
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004466 dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
Manu Abrahame415c682009-04-06 15:45:20 -03004467 state->device == STV0900 ? "STV0900" : "STV0903",
4468 demod,
Andreas Regel97f7a2a2010-01-05 19:19:43 -03004469 state->internal->dev_ver);
Manu Abrahame415c682009-04-06 15:45:20 -03004470
4471 return &state->frontend;
4472
4473error:
4474 kfree(state);
4475 return NULL;
4476}
4477EXPORT_SYMBOL(stv090x_attach);
4478MODULE_PARM_DESC(verbose, "Set Verbosity level");
4479MODULE_AUTHOR("Manu Abraham");
4480MODULE_DESCRIPTION("STV090x Multi-Std Broadcast frontend");
4481MODULE_LICENSE("GPL");