blob: 1573466a5c740aa544b4a68e863722f1791f1892 [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
40struct mutex demod_lock;
41
42/* DVBS1 and DSS C/N Lookup table */
43static const struct stv090x_tab stv090x_s1cn_tab[] = {
44 { 0, 8917 }, /* 0.0dB */
45 { 5, 8801 }, /* 0.5dB */
46 { 10, 8667 }, /* 1.0dB */
47 { 15, 8522 }, /* 1.5dB */
48 { 20, 8355 }, /* 2.0dB */
49 { 25, 8175 }, /* 2.5dB */
50 { 30, 7979 }, /* 3.0dB */
51 { 35, 7763 }, /* 3.5dB */
52 { 40, 7530 }, /* 4.0dB */
53 { 45, 7282 }, /* 4.5dB */
54 { 50, 7026 }, /* 5.0dB */
55 { 55, 6781 }, /* 5.5dB */
56 { 60, 6514 }, /* 6.0dB */
57 { 65, 6241 }, /* 6.5dB */
58 { 70, 5965 }, /* 7.0dB */
59 { 75, 5690 }, /* 7.5dB */
60 { 80, 5424 }, /* 8.0dB */
61 { 85, 5161 }, /* 8.5dB */
62 { 90, 4902 }, /* 9.0dB */
63 { 95, 4654 }, /* 9.5dB */
64 { 100, 4417 }, /* 10.0dB */
65 { 105, 4186 }, /* 10.5dB */
66 { 110, 3968 }, /* 11.0dB */
67 { 115, 3757 }, /* 11.5dB */
68 { 120, 3558 }, /* 12.0dB */
69 { 125, 3366 }, /* 12.5dB */
70 { 130, 3185 }, /* 13.0dB */
71 { 135, 3012 }, /* 13.5dB */
72 { 140, 2850 }, /* 14.0dB */
73 { 145, 2698 }, /* 14.5dB */
74 { 150, 2550 }, /* 15.0dB */
75 { 160, 2283 }, /* 16.0dB */
76 { 170, 2042 }, /* 17.0dB */
77 { 180, 1827 }, /* 18.0dB */
78 { 190, 1636 }, /* 19.0dB */
79 { 200, 1466 }, /* 20.0dB */
80 { 210, 1315 }, /* 21.0dB */
81 { 220, 1181 }, /* 22.0dB */
82 { 230, 1064 }, /* 23.0dB */
83 { 240, 960 }, /* 24.0dB */
84 { 250, 869 }, /* 25.0dB */
85 { 260, 792 }, /* 26.0dB */
86 { 270, 724 }, /* 27.0dB */
87 { 280, 665 }, /* 28.0dB */
88 { 290, 616 }, /* 29.0dB */
89 { 300, 573 }, /* 30.0dB */
90 { 310, 537 }, /* 31.0dB */
91 { 320, 507 }, /* 32.0dB */
92 { 330, 483 }, /* 33.0dB */
93 { 400, 398 }, /* 40.0dB */
94 { 450, 381 }, /* 45.0dB */
95 { 500, 377 } /* 50.0dB */
96};
97
98/* DVBS2 C/N Lookup table */
99static const struct stv090x_tab stv090x_s2cn_tab[] = {
100 { -30, 13348 }, /* -3.0dB */
101 { -20, 12640 }, /* -2d.0B */
102 { -10, 11883 }, /* -1.0dB */
103 { 0, 11101 }, /* -0.0dB */
104 { 5, 10718 }, /* 0.5dB */
105 { 10, 10339 }, /* 1.0dB */
106 { 15, 9947 }, /* 1.5dB */
107 { 20, 9552 }, /* 2.0dB */
108 { 25, 9183 }, /* 2.5dB */
109 { 30, 8799 }, /* 3.0dB */
110 { 35, 8422 }, /* 3.5dB */
111 { 40, 8062 }, /* 4.0dB */
112 { 45, 7707 }, /* 4.5dB */
113 { 50, 7353 }, /* 5.0dB */
114 { 55, 7025 }, /* 5.5dB */
115 { 60, 6684 }, /* 6.0dB */
116 { 65, 6331 }, /* 6.5dB */
117 { 70, 6036 }, /* 7.0dB */
118 { 75, 5727 }, /* 7.5dB */
119 { 80, 5437 }, /* 8.0dB */
120 { 85, 5164 }, /* 8.5dB */
121 { 90, 4902 }, /* 9.0dB */
122 { 95, 4653 }, /* 9.5dB */
123 { 100, 4408 }, /* 10.0dB */
124 { 105, 4187 }, /* 10.5dB */
125 { 110, 3961 }, /* 11.0dB */
126 { 115, 3751 }, /* 11.5dB */
127 { 120, 3558 }, /* 12.0dB */
128 { 125, 3368 }, /* 12.5dB */
129 { 130, 3191 }, /* 13.0dB */
130 { 135, 3017 }, /* 13.5dB */
131 { 140, 2862 }, /* 14.0dB */
132 { 145, 2710 }, /* 14.5dB */
133 { 150, 2565 }, /* 15.0dB */
134 { 160, 2300 }, /* 16.0dB */
135 { 170, 2058 }, /* 17.0dB */
136 { 180, 1849 }, /* 18.0dB */
137 { 190, 1663 }, /* 19.0dB */
138 { 200, 1495 }, /* 20.0dB */
139 { 210, 1349 }, /* 21.0dB */
140 { 220, 1222 }, /* 22.0dB */
141 { 230, 1110 }, /* 23.0dB */
142 { 240, 1011 }, /* 24.0dB */
143 { 250, 925 }, /* 25.0dB */
144 { 260, 853 }, /* 26.0dB */
145 { 270, 789 }, /* 27.0dB */
146 { 280, 734 }, /* 28.0dB */
147 { 290, 690 }, /* 29.0dB */
148 { 300, 650 }, /* 30.0dB */
149 { 310, 619 }, /* 31.0dB */
150 { 320, 593 }, /* 32.0dB */
151 { 330, 571 }, /* 33.0dB */
152 { 400, 498 }, /* 40.0dB */
153 { 450, 484 }, /* 45.0dB */
154 { 500, 481 } /* 50.0dB */
155};
156
157/* RF level C/N lookup table */
158static const struct stv090x_tab stv090x_rf_tab[] = {
159 { -5, 0xcaa1 }, /* -5dBm */
160 { -10, 0xc229 }, /* -10dBm */
161 { -15, 0xbb08 }, /* -15dBm */
162 { -20, 0xb4bc }, /* -20dBm */
163 { -25, 0xad5a }, /* -25dBm */
164 { -30, 0xa298 }, /* -30dBm */
165 { -35, 0x98a8 }, /* -35dBm */
166 { -40, 0x8389 }, /* -40dBm */
167 { -45, 0x59be }, /* -45dBm */
168 { -50, 0x3a14 }, /* -50dBm */
169 { -55, 0x2d11 }, /* -55dBm */
170 { -60, 0x210d }, /* -60dBm */
171 { -65, 0xa14f }, /* -65dBm */
172 { -70, 0x07aa } /* -70dBm */
173};
174
175
176static struct stv090x_reg stv0900_initval[] = {
177
178 { STV090x_OUTCFG, 0x00 },
Manu Abraham56571502009-04-07 16:08:26 -0300179 { STV090x_MODECFG, 0xff },
Manu Abrahame415c682009-04-06 15:45:20 -0300180 { STV090x_AGCRF1CFG, 0x11 },
181 { STV090x_AGCRF2CFG, 0x13 },
Manu Abraham56571502009-04-07 16:08:26 -0300182 { STV090x_TSGENERAL1X, 0x14 },
Manu Abrahame415c682009-04-06 15:45:20 -0300183 { STV090x_TSTTNR2, 0x21 },
184 { STV090x_TSTTNR4, 0x21 },
185 { STV090x_P2_DISTXCTL, 0x22 },
186 { STV090x_P2_F22TX, 0xc0 },
187 { STV090x_P2_F22RX, 0xc0 },
188 { STV090x_P2_DISRXCTL, 0x00 },
189 { STV090x_P2_DMDCFGMD, 0xF9 },
190 { STV090x_P2_DEMOD, 0x08 },
191 { STV090x_P2_DMDCFG3, 0xc4 },
192 { STV090x_P2_CARFREQ, 0xed },
193 { STV090x_P2_LDT, 0xd0 },
194 { STV090x_P2_LDT2, 0xb8 },
195 { STV090x_P2_TMGCFG, 0xd2 },
196 { STV090x_P2_TMGTHRISE, 0x20 },
197 { STV090x_P1_TMGCFG, 0xd2 },
198
199 { STV090x_P2_TMGTHFALL, 0x00 },
200 { STV090x_P2_FECSPY, 0x88 },
201 { STV090x_P2_FSPYDATA, 0x3a },
202 { STV090x_P2_FBERCPT4, 0x00 },
203 { STV090x_P2_FSPYBER, 0x10 },
204 { STV090x_P2_ERRCTRL1, 0x35 },
205 { STV090x_P2_ERRCTRL2, 0xc1 },
206 { STV090x_P2_CFRICFG, 0xf8 },
207 { STV090x_P2_NOSCFG, 0x1c },
Manu Abraham56571502009-04-07 16:08:26 -0300208 { STV090x_P2_DMDTOM, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300209 { STV090x_P2_CORRELMANT, 0x70 },
210 { STV090x_P2_CORRELABS, 0x88 },
Manu Abraham56571502009-04-07 16:08:26 -0300211 { STV090x_P2_AGC2O, 0x5b },
Manu Abrahame415c682009-04-06 15:45:20 -0300212 { STV090x_P2_AGC2REF, 0x38 },
213 { STV090x_P2_CARCFG, 0xe4 },
214 { STV090x_P2_ACLC, 0x1A },
215 { STV090x_P2_BCLC, 0x09 },
216 { STV090x_P2_CARHDR, 0x08 },
217 { STV090x_P2_KREFTMG, 0xc1 },
218 { STV090x_P2_SFRUPRATIO, 0xf0 },
219 { STV090x_P2_SFRLOWRATIO, 0x70 },
220 { STV090x_P2_SFRSTEP, 0x58 },
221 { STV090x_P2_TMGCFG2, 0x01 },
222 { STV090x_P2_CAR2CFG, 0x26 },
223 { STV090x_P2_BCLC2S2Q, 0x86 },
224 { STV090x_P2_BCLC2S28, 0x86 },
225 { STV090x_P2_SMAPCOEF7, 0x77 },
226 { STV090x_P2_SMAPCOEF6, 0x85 },
227 { STV090x_P2_SMAPCOEF5, 0x77 },
228 { STV090x_P2_TSCFGL, 0x20 },
229 { STV090x_P2_DMDCFG2, 0x3b },
230 { STV090x_P2_MODCODLST0, 0xff },
231 { STV090x_P2_MODCODLST1, 0xff },
232 { STV090x_P2_MODCODLST2, 0xff },
233 { STV090x_P2_MODCODLST3, 0xff },
234 { STV090x_P2_MODCODLST4, 0xff },
235 { STV090x_P2_MODCODLST5, 0xff },
236 { STV090x_P2_MODCODLST6, 0xff },
237 { STV090x_P2_MODCODLST7, 0xcc },
238 { STV090x_P2_MODCODLST8, 0xcc },
239 { STV090x_P2_MODCODLST9, 0xcc },
240 { STV090x_P2_MODCODLSTA, 0xcc },
241 { STV090x_P2_MODCODLSTB, 0xcc },
242 { STV090x_P2_MODCODLSTC, 0xcc },
243 { STV090x_P2_MODCODLSTD, 0xcc },
244 { STV090x_P2_MODCODLSTE, 0xcc },
245 { STV090x_P2_MODCODLSTF, 0xcf },
246 { STV090x_P1_DISTXCTL, 0x22 },
247 { STV090x_P1_F22TX, 0xc0 },
248 { STV090x_P1_F22RX, 0xc0 },
249 { STV090x_P1_DISRXCTL, 0x00 },
250 { STV090x_P1_DMDCFGMD, 0xf9 },
251 { STV090x_P1_DEMOD, 0x08 },
252 { STV090x_P1_DMDCFG3, 0xc4 },
Manu Abraham56571502009-04-07 16:08:26 -0300253 { STV090x_P1_DMDTOM, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300254 { STV090x_P1_CARFREQ, 0xed },
255 { STV090x_P1_LDT, 0xd0 },
256 { STV090x_P1_LDT2, 0xb8 },
257 { STV090x_P1_TMGCFG, 0xd2 },
258 { STV090x_P1_TMGTHRISE, 0x20 },
259 { STV090x_P1_TMGTHFALL, 0x00 },
260 { STV090x_P1_SFRUPRATIO, 0xf0 },
261 { STV090x_P1_SFRLOWRATIO, 0x70 },
262 { STV090x_P1_TSCFGL, 0x20 },
263 { STV090x_P1_FECSPY, 0x88 },
264 { STV090x_P1_FSPYDATA, 0x3a },
265 { STV090x_P1_FBERCPT4, 0x00 },
266 { STV090x_P1_FSPYBER, 0x10 },
267 { STV090x_P1_ERRCTRL1, 0x35 },
268 { STV090x_P1_ERRCTRL2, 0xc1 },
269 { STV090x_P1_CFRICFG, 0xf8 },
270 { STV090x_P1_NOSCFG, 0x1c },
271 { STV090x_P1_CORRELMANT, 0x70 },
272 { STV090x_P1_CORRELABS, 0x88 },
Manu Abraham56571502009-04-07 16:08:26 -0300273 { STV090x_P1_AGC2O, 0x5b },
Manu Abrahame415c682009-04-06 15:45:20 -0300274 { STV090x_P1_AGC2REF, 0x38 },
275 { STV090x_P1_CARCFG, 0xe4 },
276 { STV090x_P1_ACLC, 0x1A },
277 { STV090x_P1_BCLC, 0x09 },
278 { STV090x_P1_CARHDR, 0x08 },
279 { STV090x_P1_KREFTMG, 0xc1 },
280 { STV090x_P1_SFRSTEP, 0x58 },
281 { STV090x_P1_TMGCFG2, 0x01 },
282 { STV090x_P1_CAR2CFG, 0x26 },
283 { STV090x_P1_BCLC2S2Q, 0x86 },
284 { STV090x_P1_BCLC2S28, 0x86 },
285 { STV090x_P1_SMAPCOEF7, 0x77 },
286 { STV090x_P1_SMAPCOEF6, 0x85 },
287 { STV090x_P1_SMAPCOEF5, 0x77 },
288 { STV090x_P1_DMDCFG2, 0x3b },
289 { STV090x_P1_MODCODLST0, 0xff },
290 { STV090x_P1_MODCODLST1, 0xff },
291 { STV090x_P1_MODCODLST2, 0xff },
292 { STV090x_P1_MODCODLST3, 0xff },
293 { STV090x_P1_MODCODLST4, 0xff },
294 { STV090x_P1_MODCODLST5, 0xff },
295 { STV090x_P1_MODCODLST6, 0xff },
296 { STV090x_P1_MODCODLST7, 0xcc },
297 { STV090x_P1_MODCODLST8, 0xcc },
298 { STV090x_P1_MODCODLST9, 0xcc },
299 { STV090x_P1_MODCODLSTA, 0xcc },
300 { STV090x_P1_MODCODLSTB, 0xcc },
301 { STV090x_P1_MODCODLSTC, 0xcc },
302 { STV090x_P1_MODCODLSTD, 0xcc },
303 { STV090x_P1_MODCODLSTE, 0xcc },
304 { STV090x_P1_MODCODLSTF, 0xcf },
305 { STV090x_GENCFG, 0x1d },
306 { STV090x_NBITER_NF4, 0x37 },
307 { STV090x_NBITER_NF5, 0x29 },
308 { STV090x_NBITER_NF6, 0x37 },
309 { STV090x_NBITER_NF7, 0x33 },
310 { STV090x_NBITER_NF8, 0x31 },
311 { STV090x_NBITER_NF9, 0x2f },
312 { STV090x_NBITER_NF10, 0x39 },
313 { STV090x_NBITER_NF11, 0x3a },
314 { STV090x_NBITER_NF12, 0x29 },
315 { STV090x_NBITER_NF13, 0x37 },
316 { STV090x_NBITER_NF14, 0x33 },
317 { STV090x_NBITER_NF15, 0x2f },
318 { STV090x_NBITER_NF16, 0x39 },
319 { STV090x_NBITER_NF17, 0x3a },
320 { STV090x_NBITERNOERR, 0x04 },
321 { STV090x_GAINLLR_NF4, 0x0C },
322 { STV090x_GAINLLR_NF5, 0x0F },
323 { STV090x_GAINLLR_NF6, 0x11 },
324 { STV090x_GAINLLR_NF7, 0x14 },
325 { STV090x_GAINLLR_NF8, 0x17 },
326 { STV090x_GAINLLR_NF9, 0x19 },
327 { STV090x_GAINLLR_NF10, 0x20 },
328 { STV090x_GAINLLR_NF11, 0x21 },
329 { STV090x_GAINLLR_NF12, 0x0D },
330 { STV090x_GAINLLR_NF13, 0x0F },
331 { STV090x_GAINLLR_NF14, 0x13 },
332 { STV090x_GAINLLR_NF15, 0x1A },
333 { STV090x_GAINLLR_NF16, 0x1F },
334 { STV090x_GAINLLR_NF17, 0x21 },
Manu Abraham56571502009-04-07 16:08:26 -0300335 { STV090x_RCCFGH, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300336 { STV090x_P1_FECM, 0x01 }, /* disable DSS modes */
337 { STV090x_P2_FECM, 0x01 }, /* disable DSS modes */
338 { STV090x_P1_PRVIT, 0x2F }, /* disable PR 6/7 */
339 { STV090x_P2_PRVIT, 0x2F }, /* disable PR 6/7 */
340};
341
342static struct stv090x_reg stv0903_initval[] = {
343 { STV090x_OUTCFG, 0x00 },
344 { STV090x_AGCRF1CFG, 0x11 },
345 { STV090x_STOPCLK1, 0x48 },
346 { STV090x_STOPCLK2, 0x14 },
347 { STV090x_TSTTNR1, 0x27 },
348 { STV090x_TSTTNR2, 0x21 },
349 { STV090x_P1_DISTXCTL, 0x22 },
350 { STV090x_P1_F22TX, 0xc0 },
351 { STV090x_P1_F22RX, 0xc0 },
352 { STV090x_P1_DISRXCTL, 0x00 },
353 { STV090x_P1_DMDCFGMD, 0xF9 },
354 { STV090x_P1_DEMOD, 0x08 },
355 { STV090x_P1_DMDCFG3, 0xc4 },
356 { STV090x_P1_CARFREQ, 0xed },
357 { STV090x_P1_TNRCFG2, 0x82 },
358 { STV090x_P1_LDT, 0xd0 },
359 { STV090x_P1_LDT2, 0xb8 },
360 { STV090x_P1_TMGCFG, 0xd2 },
361 { STV090x_P1_TMGTHRISE, 0x20 },
362 { STV090x_P1_TMGTHFALL, 0x00 },
363 { STV090x_P1_SFRUPRATIO, 0xf0 },
364 { STV090x_P1_SFRLOWRATIO, 0x70 },
365 { STV090x_P1_TSCFGL, 0x20 },
366 { STV090x_P1_FECSPY, 0x88 },
367 { STV090x_P1_FSPYDATA, 0x3a },
368 { STV090x_P1_FBERCPT4, 0x00 },
369 { STV090x_P1_FSPYBER, 0x10 },
370 { STV090x_P1_ERRCTRL1, 0x35 },
371 { STV090x_P1_ERRCTRL2, 0xc1 },
372 { STV090x_P1_CFRICFG, 0xf8 },
373 { STV090x_P1_NOSCFG, 0x1c },
Manu Abraham56571502009-04-07 16:08:26 -0300374 { STV090x_P1_DMDTOM, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300375 { STV090x_P1_CORRELMANT, 0x70 },
376 { STV090x_P1_CORRELABS, 0x88 },
Manu Abraham56571502009-04-07 16:08:26 -0300377 { STV090x_P1_AGC2O, 0x5b },
378 { STV090x_P1_AGC2REF, 0x38 },
Manu Abrahame415c682009-04-06 15:45:20 -0300379 { STV090x_P1_CARCFG, 0xe4 },
380 { STV090x_P1_ACLC, 0x1A },
Manu Abraham56571502009-04-07 16:08:26 -0300381 { STV090x_P1_BCLC, 0x09 },
Manu Abrahame415c682009-04-06 15:45:20 -0300382 { STV090x_P1_CARHDR, 0x08 },
383 { STV090x_P1_KREFTMG, 0xc1 },
384 { STV090x_P1_SFRSTEP, 0x58 },
385 { STV090x_P1_TMGCFG2, 0x01 },
386 { STV090x_P1_CAR2CFG, 0x26 },
387 { STV090x_P1_BCLC2S2Q, 0x86 },
388 { STV090x_P1_BCLC2S28, 0x86 },
389 { STV090x_P1_SMAPCOEF7, 0x77 },
390 { STV090x_P1_SMAPCOEF6, 0x85 },
391 { STV090x_P1_SMAPCOEF5, 0x77 },
392 { STV090x_P1_DMDCFG2, 0x3b },
393 { STV090x_P1_MODCODLST0, 0xff },
394 { STV090x_P1_MODCODLST1, 0xff },
395 { STV090x_P1_MODCODLST2, 0xff },
396 { STV090x_P1_MODCODLST3, 0xff },
397 { STV090x_P1_MODCODLST4, 0xff },
398 { STV090x_P1_MODCODLST5, 0xff },
399 { STV090x_P1_MODCODLST6, 0xff },
400 { STV090x_P1_MODCODLST7, 0xcc },
401 { STV090x_P1_MODCODLST8, 0xcc },
402 { STV090x_P1_MODCODLST9, 0xcc },
403 { STV090x_P1_MODCODLSTA, 0xcc },
404 { STV090x_P1_MODCODLSTB, 0xcc },
405 { STV090x_P1_MODCODLSTC, 0xcc },
406 { STV090x_P1_MODCODLSTD, 0xcc },
407 { STV090x_P1_MODCODLSTE, 0xcc },
408 { STV090x_P1_MODCODLSTF, 0xcf },
409 { STV090x_GENCFG, 0x1c },
410 { STV090x_NBITER_NF4, 0x37 },
411 { STV090x_NBITER_NF5, 0x29 },
412 { STV090x_NBITER_NF6, 0x37 },
413 { STV090x_NBITER_NF7, 0x33 },
414 { STV090x_NBITER_NF8, 0x31 },
415 { STV090x_NBITER_NF9, 0x2f },
416 { STV090x_NBITER_NF10, 0x39 },
417 { STV090x_NBITER_NF11, 0x3a },
418 { STV090x_NBITER_NF12, 0x29 },
419 { STV090x_NBITER_NF13, 0x37 },
420 { STV090x_NBITER_NF14, 0x33 },
421 { STV090x_NBITER_NF15, 0x2f },
422 { STV090x_NBITER_NF16, 0x39 },
423 { STV090x_NBITER_NF17, 0x3a },
424 { STV090x_NBITERNOERR, 0x04 },
425 { STV090x_GAINLLR_NF4, 0x0C },
426 { STV090x_GAINLLR_NF5, 0x0F },
427 { STV090x_GAINLLR_NF6, 0x11 },
428 { STV090x_GAINLLR_NF7, 0x14 },
429 { STV090x_GAINLLR_NF8, 0x17 },
430 { STV090x_GAINLLR_NF9, 0x19 },
431 { STV090x_GAINLLR_NF10, 0x20 },
432 { STV090x_GAINLLR_NF11, 0x21 },
433 { STV090x_GAINLLR_NF12, 0x0D },
434 { STV090x_GAINLLR_NF13, 0x0F },
435 { STV090x_GAINLLR_NF14, 0x13 },
436 { STV090x_GAINLLR_NF15, 0x1A },
437 { STV090x_GAINLLR_NF16, 0x1F },
438 { STV090x_GAINLLR_NF17, 0x21 },
Manu Abraham56571502009-04-07 16:08:26 -0300439 { STV090x_RCCFGH, 0x20 },
Manu Abrahame415c682009-04-06 15:45:20 -0300440 { STV090x_P1_FECM, 0x01 }, /*disable the DSS mode */
441 { STV090x_P1_PRVIT, 0x2f } /*disable puncture rate 6/7*/
442};
443
444static struct stv090x_reg stv0900_cut20_val[] = {
445
446 { STV090x_P2_DMDCFG3, 0xe8 },
Manu Abraham56571502009-04-07 16:08:26 -0300447 { STV090x_P2_DMDCFG4, 0x10 },
Manu Abrahame415c682009-04-06 15:45:20 -0300448 { STV090x_P2_CARFREQ, 0x38 },
449 { STV090x_P2_CARHDR, 0x20 },
450 { STV090x_P2_KREFTMG, 0x5a },
451 { STV090x_P2_SMAPCOEF7, 0x06 },
452 { STV090x_P2_SMAPCOEF6, 0x00 },
453 { STV090x_P2_SMAPCOEF5, 0x04 },
454 { STV090x_P2_NOSCFG, 0x0c },
455 { STV090x_P1_DMDCFG3, 0xe8 },
Manu Abraham56571502009-04-07 16:08:26 -0300456 { STV090x_P1_DMDCFG4, 0x10 },
Manu Abrahame415c682009-04-06 15:45:20 -0300457 { STV090x_P1_CARFREQ, 0x38 },
458 { STV090x_P1_CARHDR, 0x20 },
459 { STV090x_P1_KREFTMG, 0x5a },
460 { STV090x_P1_SMAPCOEF7, 0x06 },
461 { STV090x_P1_SMAPCOEF6, 0x00 },
462 { STV090x_P1_SMAPCOEF5, 0x04 },
463 { STV090x_P1_NOSCFG, 0x0c },
464 { STV090x_GAINLLR_NF4, 0x21 },
465 { STV090x_GAINLLR_NF5, 0x21 },
466 { STV090x_GAINLLR_NF6, 0x20 },
467 { STV090x_GAINLLR_NF7, 0x1F },
468 { STV090x_GAINLLR_NF8, 0x1E },
469 { STV090x_GAINLLR_NF9, 0x1E },
470 { STV090x_GAINLLR_NF10, 0x1D },
471 { STV090x_GAINLLR_NF11, 0x1B },
472 { STV090x_GAINLLR_NF12, 0x20 },
473 { STV090x_GAINLLR_NF13, 0x20 },
474 { STV090x_GAINLLR_NF14, 0x20 },
475 { STV090x_GAINLLR_NF15, 0x20 },
476 { STV090x_GAINLLR_NF16, 0x20 },
477 { STV090x_GAINLLR_NF17, 0x21 },
478};
479
480static struct stv090x_reg stv0903_cut20_val[] = {
481 { STV090x_P1_DMDCFG3, 0xe8 },
Manu Abraham56571502009-04-07 16:08:26 -0300482 { STV090x_P1_DMDCFG4, 0x10 },
Manu Abrahame415c682009-04-06 15:45:20 -0300483 { STV090x_P1_CARFREQ, 0x38 },
484 { STV090x_P1_CARHDR, 0x20 },
485 { STV090x_P1_KREFTMG, 0x5a },
486 { STV090x_P1_SMAPCOEF7, 0x06 },
487 { STV090x_P1_SMAPCOEF6, 0x00 },
488 { STV090x_P1_SMAPCOEF5, 0x04 },
489 { STV090x_P1_NOSCFG, 0x0c },
490 { STV090x_GAINLLR_NF4, 0x21 },
491 { STV090x_GAINLLR_NF5, 0x21 },
492 { STV090x_GAINLLR_NF6, 0x20 },
493 { STV090x_GAINLLR_NF7, 0x1F },
494 { STV090x_GAINLLR_NF8, 0x1E },
495 { STV090x_GAINLLR_NF9, 0x1E },
496 { STV090x_GAINLLR_NF10, 0x1D },
497 { STV090x_GAINLLR_NF11, 0x1B },
498 { STV090x_GAINLLR_NF12, 0x20 },
499 { STV090x_GAINLLR_NF13, 0x20 },
500 { STV090x_GAINLLR_NF14, 0x20 },
501 { STV090x_GAINLLR_NF15, 0x20 },
502 { STV090x_GAINLLR_NF16, 0x20 },
503 { STV090x_GAINLLR_NF17, 0x21 }
504};
505
Manu Abrahame415c682009-04-06 15:45:20 -0300506/* Cut 2.0 Long Frame Tracking CR loop */
507static struct stv090x_long_frame_crloop stv090x_s2_crl_cut20[] = {
508 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
509 { STV090x_QPSK_12, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x1e },
510 { STV090x_QPSK_35, 0x2f, 0x3f, 0x2e, 0x2f, 0x3d, 0x0f, 0x0e, 0x2e, 0x3d, 0x0e },
511 { STV090x_QPSK_23, 0x2f, 0x3f, 0x2e, 0x2f, 0x0e, 0x0f, 0x0e, 0x1e, 0x3d, 0x3d },
512 { STV090x_QPSK_34, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
513 { STV090x_QPSK_45, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
514 { STV090x_QPSK_56, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
515 { STV090x_QPSK_89, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
516 { STV090x_QPSK_910, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
517 { STV090x_8PSK_35, 0x3c, 0x3e, 0x1c, 0x2e, 0x0c, 0x1e, 0x2b, 0x2d, 0x1b, 0x1d },
518 { STV090x_8PSK_23, 0x1d, 0x3e, 0x3c, 0x2e, 0x2c, 0x1e, 0x0c, 0x2d, 0x2b, 0x1d },
519 { STV090x_8PSK_34, 0x0e, 0x3e, 0x3d, 0x2e, 0x0d, 0x1e, 0x2c, 0x2d, 0x0c, 0x1d },
520 { STV090x_8PSK_56, 0x2e, 0x3e, 0x1e, 0x2e, 0x2d, 0x1e, 0x3c, 0x2d, 0x2c, 0x1d },
521 { STV090x_8PSK_89, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x0d, 0x2d, 0x3c, 0x1d },
522 { STV090x_8PSK_910, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x1d, 0x2d, 0x0d, 0x1d }
523};
524
Manu Abraham27d40322009-05-02 18:26:58 -0300525/* Cut 3.0 Long Frame Tracking CR loop */
526static struct stv090x_long_frame_crloop stv090x_s2_crl_cut30[] = {
527 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
528 { STV090x_QPSK_12, 0x3c, 0x2c, 0x0c, 0x2c, 0x1b, 0x2c, 0x1b, 0x1c, 0x0b, 0x3b },
529 { STV090x_QPSK_35, 0x0d, 0x0d, 0x0c, 0x0d, 0x1b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
530 { STV090x_QPSK_23, 0x1d, 0x0d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
531 { STV090x_QPSK_34, 0x1d, 0x1d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
532 { STV090x_QPSK_45, 0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
533 { STV090x_QPSK_56, 0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
534 { STV090x_QPSK_89, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
535 { STV090x_QPSK_910, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
536 { STV090x_8PSK_35, 0x39, 0x29, 0x39, 0x19, 0x19, 0x19, 0x19, 0x19, 0x09, 0x19 },
537 { STV090x_8PSK_23, 0x2a, 0x39, 0x1a, 0x0a, 0x39, 0x0a, 0x29, 0x39, 0x29, 0x0a },
538 { STV090x_8PSK_34, 0x2b, 0x3a, 0x1b, 0x1b, 0x3a, 0x1b, 0x1a, 0x0b, 0x1a, 0x3a },
539 { STV090x_8PSK_56, 0x0c, 0x1b, 0x3b, 0x3b, 0x1b, 0x3b, 0x3a, 0x3b, 0x3a, 0x1b },
540 { STV090x_8PSK_89, 0x0d, 0x3c, 0x2c, 0x2c, 0x2b, 0x0c, 0x0b, 0x3b, 0x0b, 0x1b },
541 { STV090x_8PSK_910, 0x0d, 0x0d, 0x2c, 0x3c, 0x3b, 0x1c, 0x0b, 0x3b, 0x0b, 0x1b }
542};
Manu Abrahame415c682009-04-06 15:45:20 -0300543
544/* Cut 2.0 Long Frame Tracking CR Loop */
545static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut20[] = {
546 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
547 { STV090x_16APSK_23, 0x0c, 0x0c, 0x0c, 0x0c, 0x1d, 0x0c, 0x3c, 0x0c, 0x2c, 0x0c },
548 { STV090x_16APSK_34, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0c, 0x2d, 0x0c, 0x1d, 0x0c },
549 { STV090x_16APSK_45, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
550 { STV090x_16APSK_56, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
551 { STV090x_16APSK_89, 0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
552 { STV090x_16APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
553 { STV090x_32APSK_34, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
554 { STV090x_32APSK_45, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
555 { STV090x_32APSK_56, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
556 { STV090x_32APSK_89, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
557 { STV090x_32APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c }
558};
559
Manu Abraham27d40322009-05-02 18:26:58 -0300560/* Cut 3.0 Long Frame Tracking CR Loop */
561static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut30[] = {
562 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
563 { STV090x_16APSK_23, 0x0a, 0x0a, 0x0a, 0x0a, 0x1a, 0x0a, 0x3a, 0x0a, 0x2a, 0x0a },
564 { STV090x_16APSK_34, 0x0a, 0x0a, 0x0a, 0x0a, 0x0b, 0x0a, 0x3b, 0x0a, 0x1b, 0x0a },
565 { STV090x_16APSK_45, 0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
566 { STV090x_16APSK_56, 0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
567 { STV090x_16APSK_89, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
568 { STV090x_16APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
569 { STV090x_32APSK_34, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
570 { STV090x_32APSK_45, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
571 { STV090x_32APSK_56, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
572 { STV090x_32APSK_89, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
573 { STV090x_32APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a }
574};
Manu Abrahame415c682009-04-06 15:45:20 -0300575
576static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut20[] = {
577 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
578 { STV090x_QPSK_14, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x2d, 0x1f, 0x3d, 0x3e },
579 { STV090x_QPSK_13, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x3d, 0x0f, 0x3d, 0x2e },
580 { STV090x_QPSK_25, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x2e }
581};
582
Manu Abraham27d40322009-05-02 18:26:58 -0300583static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut30[] = {
584 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
585 { STV090x_QPSK_14, 0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x2a, 0x1c, 0x3a, 0x3b },
586 { STV090x_QPSK_13, 0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x3a, 0x0c, 0x3a, 0x2b },
587 { STV090x_QPSK_25, 0x1c, 0x3c, 0x1b, 0x3c, 0x3a, 0x1c, 0x3a, 0x3b, 0x3a, 0x2b }
Manu Abrahame415c682009-04-06 15:45:20 -0300588};
589
Manu Abraham27d40322009-05-02 18:26:58 -0300590/* Cut 2.0 Short Frame Tracking CR Loop */
591static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut20[] = {
592 /* MODCOD 2M 5M 10M 20M 30M */
593 { STV090x_QPSK, 0x2f, 0x2e, 0x0e, 0x0e, 0x3d },
594 { STV090x_8PSK, 0x3e, 0x0e, 0x2d, 0x0d, 0x3c },
595 { STV090x_16APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d },
596 { STV090x_32APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d }
597};
598
599/* Cut 3.0 Short Frame Tracking CR Loop */
600static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut30[] = {
601 /* MODCOD 2M 5M 10M 20M 30M */
602 { STV090x_QPSK, 0x2C, 0x2B, 0x0B, 0x0B, 0x3A },
603 { STV090x_8PSK, 0x3B, 0x0B, 0x2A, 0x0A, 0x39 },
604 { STV090x_16APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A },
605 { STV090x_32APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A }
606};
Manu Abrahame415c682009-04-06 15:45:20 -0300607
608static inline s32 comp2(s32 __x, s32 __width)
609{
610 if (__width == 32)
611 return __x;
612 else
613 return (__x >= (1 << (__width - 1))) ? (__x - (1 << __width)) : __x;
614}
615
616static int stv090x_read_reg(struct stv090x_state *state, unsigned int reg)
617{
618 const struct stv090x_config *config = state->config;
619 int ret;
620
621 u8 b0[] = { reg >> 8, reg & 0xff };
622 u8 buf;
623
624 struct i2c_msg msg[] = {
625 { .addr = config->address, .flags = 0, .buf = b0, .len = 2 },
626 { .addr = config->address, .flags = I2C_M_RD, .buf = &buf, .len = 1 }
627 };
628
629 ret = i2c_transfer(state->i2c, msg, 2);
630 if (ret != 2) {
631 if (ret != -ERESTARTSYS)
632 dprintk(FE_ERROR, 1,
633 "Read error, Reg=[0x%02x], Status=%d",
634 reg, ret);
635
636 return ret < 0 ? ret : -EREMOTEIO;
637 }
638 if (unlikely(*state->verbose >= FE_DEBUGREG))
639 dprintk(FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
640 reg, buf);
641
642 return (unsigned int) buf;
643}
644
645static int stv090x_write_regs(struct stv090x_state *state, unsigned int reg, u8 *data, u32 count)
646{
647 const struct stv090x_config *config = state->config;
648 int ret;
649 u8 buf[2 + count];
650 struct i2c_msg i2c_msg = { .addr = config->address, .flags = 0, .buf = buf, .len = 2 + count };
651
652 buf[0] = reg >> 8;
653 buf[1] = reg & 0xff;
654 memcpy(&buf[2], data, count);
655
656 if (unlikely(*state->verbose >= FE_DEBUGREG)) {
657 int i;
658
659 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
660 for (i = 0; i < count; i++)
661 printk(" %02x", data[i]);
662 printk("\n");
663 }
664
665 ret = i2c_transfer(state->i2c, &i2c_msg, 1);
666 if (ret != 1) {
667 if (ret != -ERESTARTSYS)
668 dprintk(FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
669 reg, data[0], count, ret);
670 return ret < 0 ? ret : -EREMOTEIO;
671 }
672
673 return 0;
674}
675
676static int stv090x_write_reg(struct stv090x_state *state, unsigned int reg, u8 data)
677{
678 return stv090x_write_regs(state, reg, &data, 1);
679}
680
681static int stv090x_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
682{
683 struct stv090x_state *state = fe->demodulator_priv;
684 u32 reg;
685
686 reg = STV090x_READ_DEMOD(state, I2CRPT);
Manu Abrahame415c682009-04-06 15:45:20 -0300687 if (enable) {
Manu Abraham017eb0382009-04-07 05:19:54 -0300688 dprintk(FE_DEBUG, 1, "Enable Gate");
Manu Abrahame415c682009-04-06 15:45:20 -0300689 STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 1);
690 if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0)
691 goto err;
692
693 } else {
Manu Abraham017eb0382009-04-07 05:19:54 -0300694 dprintk(FE_DEBUG, 1, "Disable Gate");
Manu Abrahame415c682009-04-06 15:45:20 -0300695 STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 0);
696 if ((STV090x_WRITE_DEMOD(state, I2CRPT, reg)) < 0)
697 goto err;
698 }
699 return 0;
700err:
701 dprintk(FE_ERROR, 1, "I/O error");
702 return -1;
703}
704
705static void stv090x_get_lock_tmg(struct stv090x_state *state)
706{
707 switch (state->algo) {
708 case STV090x_BLIND_SEARCH:
709 dprintk(FE_DEBUG, 1, "Blind Search");
710 if (state->srate <= 1500000) { /*10Msps< SR <=15Msps*/
711 state->DemodTimeout = 1500;
712 state->FecTimeout = 400;
713 } else if (state->srate <= 5000000) { /*10Msps< SR <=15Msps*/
714 state->DemodTimeout = 1000;
715 state->FecTimeout = 300;
716 } else { /*SR >20Msps*/
717 state->DemodTimeout = 700;
718 state->FecTimeout = 100;
719 }
720 break;
721
722 case STV090x_COLD_SEARCH:
723 case STV090x_WARM_SEARCH:
724 default:
725 dprintk(FE_DEBUG, 1, "Normal Search");
726 if (state->srate <= 1000000) { /*SR <=1Msps*/
727 state->DemodTimeout = 4500;
728 state->FecTimeout = 1700;
729 } else if (state->srate <= 2000000) { /*1Msps < SR <= 2Msps */
730 state->DemodTimeout = 2500;
731 state->FecTimeout = 1100;
732 } else if (state->srate <= 5000000) { /*2Msps < SR <= 5Msps */
733 state->DemodTimeout = 1000;
734 state->FecTimeout = 550;
735 } else if (state->srate <= 10000000) { /*5Msps < SR <= 10Msps */
736 state->DemodTimeout = 700;
737 state->FecTimeout = 250;
738 } else if (state->srate <= 20000000) { /*10Msps < SR <= 20Msps */
739 state->DemodTimeout = 400;
740 state->FecTimeout = 130;
741 } else { /*SR >20Msps*/
742 state->DemodTimeout = 300;
743 state->FecTimeout = 100;
744 }
745 break;
746 }
747
748 if (state->algo == STV090x_WARM_SEARCH)
749 state->DemodTimeout /= 2;
750}
751
752static int stv090x_set_srate(struct stv090x_state *state, u32 srate)
753{
754 u32 sym;
755
Manu Abraham15bb3662009-04-08 20:14:00 -0300756 if (srate > 60000000) {
757 sym = (srate << 4); /* SR * 2^16 / master_clk */
758 sym /= (state->mclk >> 12);
759 } else if (srate > 6000000) {
760 sym = (srate << 6);
761 sym /= (state->mclk >> 10);
Manu Abrahame415c682009-04-06 15:45:20 -0300762 } else {
Manu Abraham15bb3662009-04-08 20:14:00 -0300763 sym = (srate << 9);
764 sym /= (state->mclk >> 7);
Manu Abrahame415c682009-04-06 15:45:20 -0300765 }
766
Manu Abraham15bb3662009-04-08 20:14:00 -0300767 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0x7f) < 0) /* MSB */
Manu Abrahame415c682009-04-06 15:45:20 -0300768 goto err;
769 if (STV090x_WRITE_DEMOD(state, SFRINIT0, (sym & 0xff)) < 0) /* LSB */
770 goto err;
Manu Abraham15bb3662009-04-08 20:14:00 -0300771
Manu Abrahame415c682009-04-06 15:45:20 -0300772 return 0;
773err:
774 dprintk(FE_ERROR, 1, "I/O error");
775 return -1;
776}
777
778static int stv090x_set_max_srate(struct stv090x_state *state, u32 clk, u32 srate)
779{
780 u32 sym;
781
782 srate = 105 * (srate / 100);
Manu Abraham15bb3662009-04-08 20:14:00 -0300783 if (srate > 60000000) {
784 sym = (srate << 4); /* SR * 2^16 / master_clk */
785 sym /= (state->mclk >> 12);
786 } else if (srate > 6000000) {
787 sym = (srate << 6);
788 sym /= (state->mclk >> 10);
Manu Abrahame415c682009-04-06 15:45:20 -0300789 } else {
Manu Abraham15bb3662009-04-08 20:14:00 -0300790 sym = (srate << 9);
791 sym /= (state->mclk >> 7);
Manu Abrahame415c682009-04-06 15:45:20 -0300792 }
Manu Abraham15bb3662009-04-08 20:14:00 -0300793
794 if (sym < 0x7fff) {
795 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) /* MSB */
796 goto err;
797 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) /* LSB */
798 goto err;
799 } else {
800 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x7f) < 0) /* MSB */
801 goto err;
802 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xff) < 0) /* LSB */
803 goto err;
804 }
805
Manu Abrahame415c682009-04-06 15:45:20 -0300806 return 0;
807err:
808 dprintk(FE_ERROR, 1, "I/O error");
809 return -1;
810}
811
812static int stv090x_set_min_srate(struct stv090x_state *state, u32 clk, u32 srate)
813{
814 u32 sym;
815
816 srate = 95 * (srate / 100);
Manu Abraham15bb3662009-04-08 20:14:00 -0300817 if (srate > 60000000) {
818 sym = (srate << 4); /* SR * 2^16 / master_clk */
819 sym /= (state->mclk >> 12);
820 } else if (srate > 6000000) {
821 sym = (srate << 6);
822 sym /= (state->mclk >> 10);
Manu Abrahame415c682009-04-06 15:45:20 -0300823 } else {
Manu Abraham15bb3662009-04-08 20:14:00 -0300824 sym = (srate << 9);
825 sym /= (state->mclk >> 7);
Manu Abrahame415c682009-04-06 15:45:20 -0300826 }
Manu Abraham15bb3662009-04-08 20:14:00 -0300827
Andreas Regelb671a8d2009-11-13 18:15:27 -0300828 if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0x7f)) < 0) /* MSB */
Manu Abrahame415c682009-04-06 15:45:20 -0300829 goto err;
830 if (STV090x_WRITE_DEMOD(state, SFRLOW0, (sym & 0xff)) < 0) /* LSB */
831 goto err;
832 return 0;
833err:
834 dprintk(FE_ERROR, 1, "I/O error");
835 return -1;
836}
837
Andreas Regel4e58a682009-04-16 08:40:36 -0300838static u32 stv090x_car_width(u32 srate, enum stv090x_rolloff rolloff)
Manu Abrahame415c682009-04-06 15:45:20 -0300839{
Andreas Regel4e58a682009-04-16 08:40:36 -0300840 u32 ro;
841
842 switch (rolloff) {
843 case STV090x_RO_20:
844 ro = 20;
845 break;
846 case STV090x_RO_25:
847 ro = 25;
848 break;
849 case STV090x_RO_35:
850 default:
851 ro = 35;
852 break;
853 }
854
855 return srate + (srate * ro) / 100;
Manu Abrahame415c682009-04-06 15:45:20 -0300856}
857
858static int stv090x_set_vit_thacq(struct stv090x_state *state)
859{
860 if (STV090x_WRITE_DEMOD(state, VTH12, 0x96) < 0)
861 goto err;
862 if (STV090x_WRITE_DEMOD(state, VTH23, 0x64) < 0)
863 goto err;
864 if (STV090x_WRITE_DEMOD(state, VTH34, 0x36) < 0)
865 goto err;
866 if (STV090x_WRITE_DEMOD(state, VTH56, 0x23) < 0)
867 goto err;
868 if (STV090x_WRITE_DEMOD(state, VTH67, 0x1e) < 0)
869 goto err;
870 if (STV090x_WRITE_DEMOD(state, VTH78, 0x19) < 0)
871 goto err;
872 return 0;
873err:
874 dprintk(FE_ERROR, 1, "I/O error");
875 return -1;
876}
877
878static int stv090x_set_vit_thtracq(struct stv090x_state *state)
879{
880 if (STV090x_WRITE_DEMOD(state, VTH12, 0xd0) < 0)
881 goto err;
882 if (STV090x_WRITE_DEMOD(state, VTH23, 0x7d) < 0)
883 goto err;
884 if (STV090x_WRITE_DEMOD(state, VTH34, 0x53) < 0)
885 goto err;
886 if (STV090x_WRITE_DEMOD(state, VTH56, 0x2f) < 0)
887 goto err;
888 if (STV090x_WRITE_DEMOD(state, VTH67, 0x24) < 0)
889 goto err;
890 if (STV090x_WRITE_DEMOD(state, VTH78, 0x1f) < 0)
891 goto err;
892 return 0;
893err:
894 dprintk(FE_ERROR, 1, "I/O error");
895 return -1;
896}
897
898static int stv090x_set_viterbi(struct stv090x_state *state)
899{
900 switch (state->search_mode) {
901 case STV090x_SEARCH_AUTO:
902 if (STV090x_WRITE_DEMOD(state, FECM, 0x10) < 0) /* DVB-S and DVB-S2 */
903 goto err;
904 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x3f) < 0) /* all puncture rate */
905 goto err;
906 break;
907 case STV090x_SEARCH_DVBS1:
908 if (STV090x_WRITE_DEMOD(state, FECM, 0x00) < 0) /* disable DSS */
909 goto err;
910 switch (state->fec) {
911 case STV090x_PR12:
912 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
913 goto err;
914 break;
915
916 case STV090x_PR23:
917 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
918 goto err;
919 break;
920
921 case STV090x_PR34:
922 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x04) < 0)
923 goto err;
924 break;
925
926 case STV090x_PR56:
927 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x08) < 0)
928 goto err;
929 break;
930
931 case STV090x_PR78:
932 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x20) < 0)
933 goto err;
934 break;
935
936 default:
937 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x2f) < 0) /* all */
938 goto err;
939 break;
940 }
941 break;
942 case STV090x_SEARCH_DSS:
943 if (STV090x_WRITE_DEMOD(state, FECM, 0x80) < 0)
944 goto err;
945 switch (state->fec) {
946 case STV090x_PR12:
947 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
948 goto err;
949 break;
950
951 case STV090x_PR23:
952 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
953 goto err;
954 break;
955
956 case STV090x_PR67:
957 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x10) < 0)
958 goto err;
959 break;
960
961 default:
962 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x13) < 0) /* 1/2, 2/3, 6/7 */
963 goto err;
964 break;
965 }
966 break;
967 default:
968 break;
969 }
970 return 0;
971err:
972 dprintk(FE_ERROR, 1, "I/O error");
973 return -1;
974}
975
976static int stv090x_stop_modcod(struct stv090x_state *state)
977{
978 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
979 goto err;
980 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
981 goto err;
982 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
983 goto err;
984 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
985 goto err;
986 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
987 goto err;
988 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
989 goto err;
990 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
991 goto err;
992 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xff) < 0)
993 goto err;
994 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xff) < 0)
995 goto err;
996 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xff) < 0)
997 goto err;
998 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xff) < 0)
999 goto err;
1000 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xff) < 0)
1001 goto err;
1002 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xff) < 0)
1003 goto err;
1004 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xff) < 0)
1005 goto err;
1006 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
1007 goto err;
1008 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xff) < 0)
1009 goto err;
1010 return 0;
1011err:
1012 dprintk(FE_ERROR, 1, "I/O error");
1013 return -1;
1014}
1015
1016static int stv090x_activate_modcod(struct stv090x_state *state)
1017{
Manu Abraham27d40322009-05-02 18:26:58 -03001018 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1019 goto err;
1020 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xfc) < 0)
1021 goto err;
1022 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xcc) < 0)
1023 goto err;
1024 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xcc) < 0)
1025 goto err;
1026 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xcc) < 0)
1027 goto err;
1028 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xcc) < 0)
1029 goto err;
1030 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xcc) < 0)
1031 goto err;
1032 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
1033 goto err;
1034 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
1035 goto err;
1036 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
1037 goto err;
1038 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
1039 goto err;
1040 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
1041 goto err;
1042 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
1043 goto err;
1044 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
1045 goto err;
1046 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xcc) < 0)
1047 goto err;
1048 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
1049 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001050
Manu Abrahame415c682009-04-06 15:45:20 -03001051 return 0;
1052err:
1053 dprintk(FE_ERROR, 1, "I/O error");
1054 return -1;
1055}
1056
Manu Abraham27d40322009-05-02 18:26:58 -03001057static int stv090x_activate_modcod_single(struct stv090x_state *state)
1058{
1059
1060 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1061 goto err;
1062 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xf0) < 0)
1063 goto err;
1064 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0x00) < 0)
1065 goto err;
1066 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0x00) < 0)
1067 goto err;
1068 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0x00) < 0)
1069 goto err;
1070 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0x00) < 0)
1071 goto err;
1072 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0x00) < 0)
1073 goto err;
1074 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0x00) < 0)
1075 goto err;
1076 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0x00) < 0)
1077 goto err;
1078 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0x00) < 0)
1079 goto err;
1080 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0x00) < 0)
1081 goto err;
1082 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0x00) < 0)
1083 goto err;
1084 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0x00) < 0)
1085 goto err;
1086 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0x00) < 0)
1087 goto err;
1088 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0x00) < 0)
1089 goto err;
1090 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0x0f) < 0)
1091 goto err;
1092
1093 return 0;
1094
1095err:
1096 dprintk(FE_ERROR, 1, "I/O error");
1097 return -1;
1098}
1099
Manu Abrahame415c682009-04-06 15:45:20 -03001100static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
1101{
1102 u32 reg;
1103
1104 switch (state->demod) {
1105 case STV090x_DEMODULATOR_0:
1106 mutex_lock(&demod_lock);
1107 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1108 STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, enable);
1109 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1110 goto err;
1111 mutex_unlock(&demod_lock);
1112 break;
1113
1114 case STV090x_DEMODULATOR_1:
1115 mutex_lock(&demod_lock);
1116 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1117 STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, enable);
1118 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1119 goto err;
1120 mutex_unlock(&demod_lock);
1121 break;
1122
1123 default:
1124 dprintk(FE_ERROR, 1, "Wrong demodulator!");
1125 break;
1126 }
1127 return 0;
1128err:
1129 mutex_unlock(&demod_lock);
1130 dprintk(FE_ERROR, 1, "I/O error");
1131 return -1;
1132}
1133
Manu Abraham27d40322009-05-02 18:26:58 -03001134static int stv090x_dvbs_track_crl(struct stv090x_state *state)
1135{
1136 if (state->dev_ver >= 0x30) {
1137 /* Set ACLC BCLC optimised value vs SR */
1138 if (state->srate >= 15000000) {
1139 if (STV090x_WRITE_DEMOD(state, ACLC, 0x2b) < 0)
1140 goto err;
1141 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1a) < 0)
1142 goto err;
1143 } else if ((state->srate >= 7000000) && (15000000 > state->srate)) {
1144 if (STV090x_WRITE_DEMOD(state, ACLC, 0x0c) < 0)
1145 goto err;
1146 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1b) < 0)
1147 goto err;
1148 } else if (state->srate < 7000000) {
1149 if (STV090x_WRITE_DEMOD(state, ACLC, 0x2c) < 0)
1150 goto err;
1151 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1c) < 0)
1152 goto err;
1153 }
1154
1155 } else {
1156 /* Cut 2.0 */
1157 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
1158 goto err;
1159 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1160 goto err;
1161 }
1162 return 0;
1163err:
1164 dprintk(FE_ERROR, 1, "I/O error");
1165 return -1;
1166}
1167
Manu Abrahame415c682009-04-06 15:45:20 -03001168static int stv090x_delivery_search(struct stv090x_state *state)
1169{
1170 u32 reg;
1171
1172 switch (state->search_mode) {
1173 case STV090x_SEARCH_DVBS1:
1174 case STV090x_SEARCH_DSS:
1175 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1176 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1177 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1178 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1179 goto err;
1180
Manu Abraham27d40322009-05-02 18:26:58 -03001181 /* Activate Viterbi decoder in legacy search,
1182 * do not use FRESVIT1, might impact VITERBI2
1183 */
Manu Abrahame415c682009-04-06 15:45:20 -03001184 if (stv090x_vitclk_ctl(state, 0) < 0)
1185 goto err;
1186
Manu Abraham27d40322009-05-02 18:26:58 -03001187 if (stv090x_dvbs_track_crl(state) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001188 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001189
Manu Abrahame415c682009-04-06 15:45:20 -03001190 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x22) < 0) /* disable DVB-S2 */
1191 goto err;
1192
Manu Abraham27d40322009-05-02 18:26:58 -03001193 if (stv090x_set_vit_thacq(state) < 0)
1194 goto err;
1195 if (stv090x_set_viterbi(state) < 0)
1196 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001197 break;
1198
1199 case STV090x_SEARCH_DVBS2:
1200 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1201 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
1202 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1203 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1204 goto err;
1205 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1206 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
1207 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1208 goto err;
1209
1210 if (stv090x_vitclk_ctl(state, 1) < 0)
1211 goto err;
1212
1213 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0) /* stop DVB-S CR loop */
1214 goto err;
1215 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1216 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001217
1218 if (state->dev_ver <= 0x20) {
1219 /* enable S2 carrier loop */
1220 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1221 goto err;
1222 } else {
1223 /* > Cut 3: Stop carrier 3 */
1224 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1225 goto err;
1226 }
Manu Abrahame415c682009-04-06 15:45:20 -03001227
1228 if (state->demod_mode != STV090x_SINGLE) {
Manu Abraham27d40322009-05-02 18:26:58 -03001229 /* Cut 2: enable link during search */
1230 if (stv090x_activate_modcod(state) < 0)
1231 goto err;
1232 } else {
1233 /* Single demodulator
1234 * Authorize SHORT and LONG frames,
1235 * QPSK, 8PSK, 16APSK and 32APSK
1236 */
1237 if (stv090x_activate_modcod_single(state) < 0)
1238 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001239 }
Manu Abraham27d40322009-05-02 18:26:58 -03001240
Andreas Regel7b035da2009-11-13 18:20:54 -03001241 if (stv090x_set_vit_thtracq(state) < 0)
1242 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001243 break;
1244
1245 case STV090x_SEARCH_AUTO:
1246 default:
Manu Abraham27d40322009-05-02 18:26:58 -03001247 /* enable DVB-S2 and DVB-S2 in Auto MODE */
Manu Abrahame415c682009-04-06 15:45:20 -03001248 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
Manu Abrahame415c682009-04-06 15:45:20 -03001249 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1250 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
1251 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1252 goto err;
1253
Andreas Regel4e58a682009-04-16 08:40:36 -03001254 if (stv090x_vitclk_ctl(state, 0) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001255 goto err;
1256
Manu Abraham27d40322009-05-02 18:26:58 -03001257 if (stv090x_dvbs_track_crl(state) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001258 goto err;
1259
Manu Abraham27d40322009-05-02 18:26:58 -03001260 if (state->dev_ver <= 0x20) {
1261 /* enable S2 carrier loop */
1262 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1263 goto err;
1264 } else {
1265 /* > Cut 3: Stop carrier 3 */
1266 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1267 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001268 }
Manu Abraham27d40322009-05-02 18:26:58 -03001269
1270 if (state->demod_mode != STV090x_SINGLE) {
1271 /* Cut 2: enable link during search */
1272 if (stv090x_activate_modcod(state) < 0)
1273 goto err;
1274 } else {
1275 /* Single demodulator
1276 * Authorize SHORT and LONG frames,
1277 * QPSK, 8PSK, 16APSK and 32APSK
1278 */
1279 if (stv090x_activate_modcod_single(state) < 0)
1280 goto err;
1281 }
1282
Andreas Regel7b035da2009-11-13 18:20:54 -03001283 if (stv090x_set_vit_thacq(state) < 0)
1284 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001285
1286 if (stv090x_set_viterbi(state) < 0)
1287 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001288 break;
1289 }
1290 return 0;
1291err:
1292 dprintk(FE_ERROR, 1, "I/O error");
1293 return -1;
1294}
1295
1296static int stv090x_start_search(struct stv090x_state *state)
1297{
Manu Abraham27d40322009-05-02 18:26:58 -03001298 u32 reg, freq_abs;
1299 s16 freq;
Manu Abrahame415c682009-04-06 15:45:20 -03001300
Manu Abraham27d40322009-05-02 18:26:58 -03001301 /* Reset demodulator */
Manu Abrahame415c682009-04-06 15:45:20 -03001302 reg = STV090x_READ_DEMOD(state, DMDISTATE);
1303 STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f);
1304 if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
1305 goto err;
1306
Manu Abraham27d40322009-05-02 18:26:58 -03001307 if (state->dev_ver <= 0x20) {
1308 if (state->srate <= 5000000) {
1309 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0)
1310 goto err;
1311 if (STV090x_WRITE_DEMOD(state, CFRUP1, 0x0f) < 0)
1312 goto err;
Andreas Regelb671a8d2009-11-13 18:15:27 -03001313 if (STV090x_WRITE_DEMOD(state, CFRUP0, 0xff) < 0)
Manu Abraham27d40322009-05-02 18:26:58 -03001314 goto err;
1315 if (STV090x_WRITE_DEMOD(state, CFRLOW1, 0xf0) < 0)
1316 goto err;
1317 if (STV090x_WRITE_DEMOD(state, CFRLOW0, 0x00) < 0)
1318 goto err;
1319
1320 /*enlarge the timing bandwith for Low SR*/
1321 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0)
1322 goto err;
1323 } else {
1324 /* If the symbol rate is >5 Msps
1325 Set The carrier search up and low to auto mode */
1326 if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
1327 goto err;
1328 /*reduce the timing bandwith for high SR*/
1329 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
1330 goto err;
1331 }
1332 } else {
1333 /* >= Cut 3 */
1334 if (state->srate <= 5000000) {
1335 /* enlarge the timing bandwith for Low SR */
1336 STV090x_WRITE_DEMOD(state, RTCS2, 0x68);
1337 } else {
1338 /* reduce timing bandwith for high SR */
1339 STV090x_WRITE_DEMOD(state, RTCS2, 0x44);
1340 }
1341
1342 /* Set CFR min and max to manual mode */
1343 STV090x_WRITE_DEMOD(state, CARCFG, 0x46);
1344
1345 if (state->algo == STV090x_WARM_SEARCH) {
1346 /* WARM Start
1347 * CFR min = -1MHz,
1348 * CFR max = +1MHz
1349 */
1350 freq_abs = 1000 << 16;
1351 freq_abs /= (state->mclk / 1000);
1352 freq = (s16) freq_abs;
1353 } else {
1354 /* COLD Start
1355 * CFR min =- (SearchRange / 2 + 600KHz)
1356 * CFR max = +(SearchRange / 2 + 600KHz)
1357 * (600KHz for the tuner step size)
1358 */
1359 freq_abs = (state->search_range / 2000) + 600;
1360 freq_abs = freq_abs << 16;
1361 freq_abs /= (state->mclk / 1000);
1362 freq = (s16) freq_abs;
1363 }
1364
1365 if (STV090x_WRITE_DEMOD(state, CFRUP1, MSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001366 goto err;
Andreas Regelb671a8d2009-11-13 18:15:27 -03001367 if (STV090x_WRITE_DEMOD(state, CFRUP0, LSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001368 goto err;
1369
Manu Abraham27d40322009-05-02 18:26:58 -03001370 freq *= -1;
1371
1372 if (STV090x_WRITE_DEMOD(state, CFRLOW1, MSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001373 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001374 if (STV090x_WRITE_DEMOD(state, CFRLOW0, LSB(freq)) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001375 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001376
Manu Abrahame415c682009-04-06 15:45:20 -03001377 }
Manu Abraham27d40322009-05-02 18:26:58 -03001378
Manu Abrahame415c682009-04-06 15:45:20 -03001379 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0) < 0)
1380 goto err;
1381 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0) < 0)
1382 goto err;
1383
1384 if (state->dev_ver >= 0x20) {
1385 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1386 goto err;
1387 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
1388 goto err;
1389
1390 if ((state->search_mode == STV090x_DVBS1) ||
1391 (state->search_mode == STV090x_DSS) ||
1392 (state->search_mode == STV090x_SEARCH_AUTO)) {
1393
1394 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
1395 goto err;
1396 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0)
1397 goto err;
1398 }
1399 }
1400
1401 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00) < 0)
1402 goto err;
1403 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xe0) < 0)
1404 goto err;
1405 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xc0) < 0)
1406 goto err;
1407
1408 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1409 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
1410 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
1411 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1412 goto err;
1413 reg = STV090x_READ_DEMOD(state, DMDCFG2);
1414 STV090x_SETFIELD_Px(reg, S1S2_SEQUENTIAL_FIELD, 0x0);
1415 if (STV090x_WRITE_DEMOD(state, DMDCFG2, reg) < 0)
1416 goto err;
1417
Andreas Regel7b035da2009-11-13 18:20:54 -03001418 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0)
1419 goto err;
1420
Manu Abraham27d40322009-05-02 18:26:58 -03001421 if (state->dev_ver >= 0x20) {
1422 /*Frequency offset detector setting*/
1423 if (state->srate < 2000000) {
1424 if (state->dev_ver <= 0x20) {
1425 /* Cut 2 */
1426 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x39) < 0)
1427 goto err;
1428 } else {
Andreas Regel7b035da2009-11-13 18:20:54 -03001429 /* Cut 3 */
Manu Abraham27d40322009-05-02 18:26:58 -03001430 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x89) < 0)
1431 goto err;
1432 }
1433 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x40) < 0)
1434 goto err;
Andreas Regela4978a82009-11-13 18:17:45 -03001435 } else if (state->srate < 10000000) {
Manu Abrahame415c682009-04-06 15:45:20 -03001436 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4c) < 0)
1437 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001438 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1439 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001440 } else {
1441 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4b) < 0)
1442 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001443 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1444 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001445 }
1446 } else {
1447 if (state->srate < 10000000) {
1448 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
1449 goto err;
1450 } else {
1451 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
1452 goto err;
1453 }
1454 }
1455
1456 switch (state->algo) {
Manu Abraham27d40322009-05-02 18:26:58 -03001457 case STV090x_WARM_SEARCH:
1458 /* The symbol rate and the exact
1459 * carrier Frequency are known
1460 */
Manu Abrahame415c682009-04-06 15:45:20 -03001461 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
1462 goto err;
1463 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
1464 goto err;
1465 break;
1466
Manu Abraham27d40322009-05-02 18:26:58 -03001467 case STV090x_COLD_SEARCH:
1468 /* The symbol rate is known */
Manu Abrahame415c682009-04-06 15:45:20 -03001469 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
1470 goto err;
1471 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
1472 goto err;
1473 break;
1474
1475 default:
1476 break;
1477 }
1478 return 0;
1479err:
1480 dprintk(FE_ERROR, 1, "I/O error");
1481 return -1;
1482}
1483
1484static int stv090x_get_agc2_min_level(struct stv090x_state *state)
1485{
Andreas Regelb4a42482009-11-13 18:16:44 -03001486 u32 agc2_min = 0xffff, agc2 = 0, freq_init, freq_step, reg;
Manu Abrahame415c682009-04-06 15:45:20 -03001487 s32 i, j, steps, dir;
1488
1489 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1490 goto err;
1491 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
Andreas Regel7b035da2009-11-13 18:20:54 -03001492 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
1493 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
Manu Abrahame415c682009-04-06 15:45:20 -03001494 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1495 goto err;
1496
1497 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0) /* SR = 65 Msps Max */
1498 goto err;
1499 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1500 goto err;
1501 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0) /* SR= 400 ksps Min */
1502 goto err;
1503 if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1504 goto err;
1505 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0) /* stop acq @ coarse carrier state */
1506 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001507 if (stv090x_set_srate(state, 1000000) < 0)
1508 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001509
Andreas Regel7b035da2009-11-13 18:20:54 -03001510 steps = state->search_range / 1000000;
1511 if (steps <= 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001512 steps = 1;
1513
1514 dir = 1;
1515 freq_step = (1000000 * 256) / (state->mclk / 256);
1516 freq_init = 0;
1517
1518 for (i = 0; i < steps; i++) {
1519 if (dir > 0)
1520 freq_init = freq_init + (freq_step * i);
1521 else
1522 freq_init = freq_init - (freq_step * i);
1523
Andreas Regelb671a8d2009-11-13 18:15:27 -03001524 dir *= -1;
Manu Abrahame415c682009-04-06 15:45:20 -03001525
1526 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod RESET */
1527 goto err;
1528 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_init >> 8) & 0xff) < 0)
1529 goto err;
1530 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_init & 0xff) < 0)
1531 goto err;
1532 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x58) < 0) /* Demod RESET */
1533 goto err;
1534 msleep(10);
Andreas Regelb4a42482009-11-13 18:16:44 -03001535
1536 agc2 = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03001537 for (j = 0; j < 10; j++) {
Andreas Regelb4a42482009-11-13 18:16:44 -03001538 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1539 STV090x_READ_DEMOD(state, AGC2I0);
Manu Abrahame415c682009-04-06 15:45:20 -03001540 }
1541 agc2 /= 10;
Andreas Regelb4a42482009-11-13 18:16:44 -03001542 if (agc2 < agc2_min)
Manu Abrahame415c682009-04-06 15:45:20 -03001543 agc2_min = agc2;
1544 }
1545
1546 return agc2_min;
1547err:
1548 dprintk(FE_ERROR, 1, "I/O error");
1549 return -1;
1550}
1551
1552static u32 stv090x_get_srate(struct stv090x_state *state, u32 clk)
1553{
1554 u8 r3, r2, r1, r0;
1555 s32 srate, int_1, int_2, tmp_1, tmp_2;
Manu Abrahame415c682009-04-06 15:45:20 -03001556
1557 r3 = STV090x_READ_DEMOD(state, SFR3);
1558 r2 = STV090x_READ_DEMOD(state, SFR2);
1559 r1 = STV090x_READ_DEMOD(state, SFR1);
1560 r0 = STV090x_READ_DEMOD(state, SFR0);
1561
1562 srate = ((r3 << 24) | (r2 << 16) | (r1 << 8) | r0);
1563
Manu Abrahamf430fff2009-04-08 20:18:50 -03001564 int_1 = clk >> 16;
1565 int_2 = srate >> 16;
Manu Abrahame415c682009-04-06 15:45:20 -03001566
Manu Abrahamf430fff2009-04-08 20:18:50 -03001567 tmp_1 = clk % 0x10000;
1568 tmp_2 = srate % 0x10000;
Manu Abrahame415c682009-04-06 15:45:20 -03001569
1570 srate = (int_1 * int_2) +
Manu Abrahamf430fff2009-04-08 20:18:50 -03001571 ((int_1 * tmp_2) >> 16) +
1572 ((int_2 * tmp_1) >> 16);
Manu Abrahame415c682009-04-06 15:45:20 -03001573
1574 return srate;
1575}
1576
1577static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
1578{
1579 struct dvb_frontend *fe = &state->frontend;
1580
1581 int tmg_lock = 0, i;
1582 s32 tmg_cpt = 0, dir = 1, steps, cur_step = 0, freq;
1583 u32 srate_coarse = 0, agc2 = 0, car_step = 1200, reg;
Andreas Regelb4a42482009-11-13 18:16:44 -03001584 u32 agc2th;
1585
1586 if (state->dev_ver >= 0x30)
1587 agc2th = 0x2e00;
1588 else
1589 agc2th = 0x1f00;
Manu Abrahame415c682009-04-06 15:45:20 -03001590
1591 reg = STV090x_READ_DEMOD(state, DMDISTATE);
1592 STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f); /* Demod RESET */
1593 if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
1594 goto err;
1595 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0x12) < 0)
1596 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001597 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0)
1598 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001599 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xf0) < 0)
1600 goto err;
1601 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xe0) < 0)
1602 goto err;
1603 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1604 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 1);
Andreas Regel7b035da2009-11-13 18:20:54 -03001605 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
Manu Abrahame415c682009-04-06 15:45:20 -03001606 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1607 goto err;
1608
1609 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0)
1610 goto err;
1611 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1612 goto err;
1613 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0)
1614 goto err;
1615 if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1616 goto err;
1617 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0)
1618 goto err;
Andreas Regelb4a42482009-11-13 18:16:44 -03001619 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x50) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001620 goto err;
1621
Manu Abraham27d40322009-05-02 18:26:58 -03001622 if (state->dev_ver >= 0x30) {
1623 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x99) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001624 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001625 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x98) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001626 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001627
1628 } else if (state->dev_ver >= 0x20) {
1629 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001630 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001631 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001632 goto err;
1633 }
1634
1635 if (state->srate <= 2000000)
1636 car_step = 1000;
1637 else if (state->srate <= 5000000)
1638 car_step = 2000;
1639 else if (state->srate <= 12000000)
1640 car_step = 3000;
1641 else
1642 car_step = 5000;
1643
1644 steps = -1 + ((state->search_range / 1000) / car_step);
1645 steps /= 2;
1646 steps = (2 * steps) + 1;
1647 if (steps < 0)
1648 steps = 1;
1649 else if (steps > 10) {
1650 steps = 11;
1651 car_step = (state->search_range / 1000) / 10;
1652 }
1653 cur_step = 0;
1654 dir = 1;
1655 freq = state->frequency;
1656
1657 while ((!tmg_lock) && (cur_step < steps)) {
1658 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5f) < 0) /* Demod RESET */
1659 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001660 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
1661 goto err;
1662 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
1663 goto err;
1664 if (STV090x_WRITE_DEMOD(state, SFRINIT1, 0x00) < 0)
1665 goto err;
1666 if (STV090x_WRITE_DEMOD(state, SFRINIT0, 0x00) < 0)
1667 goto err;
1668 /* trigger acquisition */
1669 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x40) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001670 goto err;
1671 msleep(50);
1672 for (i = 0; i < 10; i++) {
1673 reg = STV090x_READ_DEMOD(state, DSTATUS);
1674 if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
1675 tmg_cpt++;
Andreas Regelb4a42482009-11-13 18:16:44 -03001676 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1677 STV090x_READ_DEMOD(state, AGC2I0);
Manu Abrahame415c682009-04-06 15:45:20 -03001678 }
1679 agc2 /= 10;
1680 srate_coarse = stv090x_get_srate(state, state->mclk);
1681 cur_step++;
1682 dir *= -1;
Andreas Regelb4a42482009-11-13 18:16:44 -03001683 if ((tmg_cpt >= 5) && (agc2 < agc2th) &&
1684 (srate_coarse < 50000000) && (srate_coarse > 850000))
Manu Abrahame415c682009-04-06 15:45:20 -03001685 tmg_lock = 1;
1686 else if (cur_step < steps) {
1687 if (dir > 0)
1688 freq += cur_step * car_step;
1689 else
1690 freq -= cur_step * car_step;
1691
1692 /* Setup tuner */
Manu Abraham27d40322009-05-02 18:26:58 -03001693 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
1694 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001695
Manu Abraham27d40322009-05-02 18:26:58 -03001696 if (state->config->tuner_set_frequency) {
Andreas Regela4978a82009-11-13 18:17:45 -03001697 if (state->config->tuner_set_frequency(fe, freq) < 0)
Manu Abraham27d40322009-05-02 18:26:58 -03001698 goto err;
1699 }
Manu Abrahame415c682009-04-06 15:45:20 -03001700
Manu Abraham27d40322009-05-02 18:26:58 -03001701 if (state->config->tuner_set_bandwidth) {
1702 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
1703 goto err;
1704 }
Manu Abrahame415c682009-04-06 15:45:20 -03001705
Manu Abraham27d40322009-05-02 18:26:58 -03001706 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
1707 goto err;
1708
Manu Abrahame415c682009-04-06 15:45:20 -03001709 msleep(50);
Manu Abrahame415c682009-04-06 15:45:20 -03001710
Manu Abraham27d40322009-05-02 18:26:58 -03001711 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
1712 goto err;
1713
1714 if (state->config->tuner_get_status) {
1715 if (state->config->tuner_get_status(fe, &reg) < 0)
1716 goto err;
1717 }
Manu Abrahame415c682009-04-06 15:45:20 -03001718
1719 if (reg)
1720 dprintk(FE_DEBUG, 1, "Tuner phase locked");
1721 else
1722 dprintk(FE_DEBUG, 1, "Tuner unlocked");
1723
Manu Abraham27d40322009-05-02 18:26:58 -03001724 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
1725 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03001726
1727 }
1728 }
1729 if (!tmg_lock)
1730 srate_coarse = 0;
1731 else
1732 srate_coarse = stv090x_get_srate(state, state->mclk);
1733
1734 return srate_coarse;
1735err:
1736 dprintk(FE_ERROR, 1, "I/O error");
1737 return -1;
1738}
1739
1740static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
1741{
1742 u32 srate_coarse, freq_coarse, sym, reg;
1743
1744 srate_coarse = stv090x_get_srate(state, state->mclk);
1745 freq_coarse = STV090x_READ_DEMOD(state, CFR2) << 8;
1746 freq_coarse |= STV090x_READ_DEMOD(state, CFR1);
1747 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1748
1749 if (sym < state->srate)
1750 srate_coarse = 0;
1751 else {
1752 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0) /* Demod RESET */
1753 goto err;
Andreas Regel7b035da2009-11-13 18:20:54 -03001754 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001755 goto err;
1756 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
1757 goto err;
1758 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
1759 goto err;
1760 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
1761 goto err;
1762 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1763 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
1764 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1765 goto err;
1766
Andreas Regelb4a42482009-11-13 18:16:44 -03001767 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1768 goto err;
1769
Manu Abraham27d40322009-05-02 18:26:58 -03001770 if (state->dev_ver >= 0x30) {
1771 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x79) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001772 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001773 } else if (state->dev_ver >= 0x20) {
1774 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001775 goto err;
1776 }
1777
1778 if (srate_coarse > 3000000) {
1779 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1780 sym = (sym / 1000) * 65536;
1781 sym /= (state->mclk / 1000);
1782 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1783 goto err;
1784 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1785 goto err;
1786 sym = 10 * (srate_coarse / 13); /* SFRLOW = SFR - 30% */
1787 sym = (sym / 1000) * 65536;
1788 sym /= (state->mclk / 1000);
1789 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1790 goto err;
1791 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1792 goto err;
1793 sym = (srate_coarse / 1000) * 65536;
1794 sym /= (state->mclk / 1000);
1795 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1796 goto err;
1797 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
1798 goto err;
1799 } else {
1800 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1801 sym = (sym / 100) * 65536;
1802 sym /= (state->mclk / 100);
1803 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1804 goto err;
1805 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1806 goto err;
1807 sym = 10 * (srate_coarse / 14); /* SFRLOW = SFR - 30% */
1808 sym = (sym / 100) * 65536;
1809 sym /= (state->mclk / 100);
1810 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1811 goto err;
1812 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1813 goto err;
1814 sym = (srate_coarse / 100) * 65536;
1815 sym /= (state->mclk / 100);
1816 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1817 goto err;
1818 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
1819 goto err;
1820 }
1821 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
1822 goto err;
1823 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_coarse >> 8) & 0xff) < 0)
1824 goto err;
1825 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_coarse & 0xff) < 0)
1826 goto err;
1827 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0) /* trigger acquisition */
1828 goto err;
1829 }
1830
1831 return srate_coarse;
1832
1833err:
1834 dprintk(FE_ERROR, 1, "I/O error");
1835 return -1;
1836}
1837
1838static int stv090x_get_dmdlock(struct stv090x_state *state, s32 timeout)
1839{
1840 s32 timer = 0, lock = 0;
1841 u32 reg;
1842 u8 stat;
1843
1844 while ((timer < timeout) && (!lock)) {
1845 reg = STV090x_READ_DEMOD(state, DMDSTATE);
1846 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
1847
1848 switch (stat) {
1849 case 0: /* searching */
1850 case 1: /* first PLH detected */
1851 default:
1852 dprintk(FE_DEBUG, 1, "Demodulator searching ..");
1853 lock = 0;
1854 break;
1855 case 2: /* DVB-S2 mode */
1856 case 3: /* DVB-S1/legacy mode */
1857 reg = STV090x_READ_DEMOD(state, DSTATUS);
1858 lock = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
1859 break;
1860 }
1861
1862 if (!lock)
1863 msleep(10);
1864 else
1865 dprintk(FE_DEBUG, 1, "Demodulator acquired LOCK");
1866
1867 timer += 10;
1868 }
1869 return lock;
1870}
1871
1872static int stv090x_blind_search(struct stv090x_state *state)
1873{
1874 u32 agc2, reg, srate_coarse;
Andreas Regela4978a82009-11-13 18:17:45 -03001875 s32 cpt_fail, agc2_ovflw, i;
Manu Abrahame415c682009-04-06 15:45:20 -03001876 u8 k_ref, k_max, k_min;
1877 int coarse_fail, lock;
1878
Andreas Regel7b035da2009-11-13 18:20:54 -03001879 k_max = 110;
1880 k_min = 10;
Manu Abrahame415c682009-04-06 15:45:20 -03001881
1882 agc2 = stv090x_get_agc2_min_level(state);
1883
Manu Abraham27d40322009-05-02 18:26:58 -03001884 if (agc2 > STV090x_SEARCH_AGC2_TH(state->dev_ver)) {
Manu Abrahame415c682009-04-06 15:45:20 -03001885 lock = 0;
1886 } else {
Manu Abraham27d40322009-05-02 18:26:58 -03001887
1888 if (state->dev_ver <= 0x20) {
1889 if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001890 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001891 } else {
1892 /* > Cut 3 */
1893 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x06) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03001894 goto err;
1895 }
1896
Manu Abrahame415c682009-04-06 15:45:20 -03001897 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
1898 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03001899
Manu Abrahame415c682009-04-06 15:45:20 -03001900 if (state->dev_ver >= 0x20) {
1901 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1902 goto err;
1903 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
1904 goto err;
1905 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
1906 goto err;
1907 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0) /* set viterbi hysteresis */
1908 goto err;
1909 }
1910
1911 k_ref = k_max;
1912 do {
1913 if (STV090x_WRITE_DEMOD(state, KREFTMG, k_ref) < 0)
1914 goto err;
1915 if (stv090x_srate_srch_coarse(state) != 0) {
1916 srate_coarse = stv090x_srate_srch_fine(state);
1917 if (srate_coarse != 0) {
1918 stv090x_get_lock_tmg(state);
Andreas Regela4978a82009-11-13 18:17:45 -03001919 lock = stv090x_get_dmdlock(state,
1920 state->DemodTimeout);
Manu Abrahame415c682009-04-06 15:45:20 -03001921 } else {
1922 lock = 0;
1923 }
1924 } else {
1925 cpt_fail = 0;
1926 agc2_ovflw = 0;
1927 for (i = 0; i < 10; i++) {
Andreas Regelb4a42482009-11-13 18:16:44 -03001928 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1929 STV090x_READ_DEMOD(state, AGC2I0);
Manu Abrahame415c682009-04-06 15:45:20 -03001930 if (agc2 >= 0xff00)
1931 agc2_ovflw++;
1932 reg = STV090x_READ_DEMOD(state, DSTATUS2);
1933 if ((STV090x_GETFIELD_Px(reg, CFR_OVERFLOW_FIELD) == 0x01) &&
1934 (STV090x_GETFIELD_Px(reg, DEMOD_DELOCK_FIELD) == 0x01))
1935
1936 cpt_fail++;
1937 }
1938 if ((cpt_fail > 7) || (agc2_ovflw > 7))
1939 coarse_fail = 1;
1940
1941 lock = 0;
1942 }
Andreas Regel7b035da2009-11-13 18:20:54 -03001943 k_ref -= 20;
Manu Abrahame415c682009-04-06 15:45:20 -03001944 } while ((k_ref >= k_min) && (!lock) && (!coarse_fail));
1945 }
1946
1947 return lock;
1948
1949err:
1950 dprintk(FE_ERROR, 1, "I/O error");
1951 return -1;
1952}
1953
1954static int stv090x_chk_tmg(struct stv090x_state *state)
1955{
1956 u32 reg;
Manu Abraham27d40322009-05-02 18:26:58 -03001957 s32 tmg_cpt = 0, i;
Manu Abrahame415c682009-04-06 15:45:20 -03001958 u8 freq, tmg_thh, tmg_thl;
1959 int tmg_lock;
1960
1961 freq = STV090x_READ_DEMOD(state, CARFREQ);
1962 tmg_thh = STV090x_READ_DEMOD(state, TMGTHRISE);
1963 tmg_thl = STV090x_READ_DEMOD(state, TMGTHFALL);
1964 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
1965 goto err;
1966 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
1967 goto err;
1968
1969 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1970 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00); /* stop carrier offset search */
1971 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1972 goto err;
1973 if (STV090x_WRITE_DEMOD(state, RTC, 0x80) < 0)
1974 goto err;
1975
1976 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x40) < 0)
1977 goto err;
1978 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x00) < 0)
1979 goto err;
1980
1981 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0) /* set car ofset to 0 */
1982 goto err;
1983 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
1984 goto err;
1985 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x65) < 0)
1986 goto err;
1987
1988 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* trigger acquisition */
1989 goto err;
1990 msleep(10);
1991
1992 for (i = 0; i < 10; i++) {
1993 reg = STV090x_READ_DEMOD(state, DSTATUS);
1994 if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
1995 tmg_cpt++;
1996 msleep(1);
1997 }
1998 if (tmg_cpt >= 3)
1999 tmg_lock = 1;
2000
2001 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
2002 goto err;
2003 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0) /* DVB-S1 timing */
2004 goto err;
2005 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0) /* DVB-S2 timing */
2006 goto err;
2007
2008 if (STV090x_WRITE_DEMOD(state, CARFREQ, freq) < 0)
2009 goto err;
2010 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, tmg_thh) < 0)
2011 goto err;
2012 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, tmg_thl) < 0)
2013 goto err;
2014
2015 return tmg_lock;
2016
2017err:
2018 dprintk(FE_ERROR, 1, "I/O error");
2019 return -1;
2020}
2021
2022static int stv090x_get_coldlock(struct stv090x_state *state, s32 timeout_dmd)
2023{
2024 struct dvb_frontend *fe = &state->frontend;
2025
2026 u32 reg;
2027 s32 car_step, steps, cur_step, dir, freq, timeout_lock;
2028 int lock = 0;
2029
2030 if (state->srate >= 10000000)
2031 timeout_lock = timeout_dmd / 3;
2032 else
2033 timeout_lock = timeout_dmd / 2;
2034
2035 lock = stv090x_get_dmdlock(state, timeout_lock); /* cold start wait */
2036 if (!lock) {
2037 if (state->srate >= 10000000) {
2038 if (stv090x_chk_tmg(state)) {
2039 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2040 goto err;
2041 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2042 goto err;
2043 lock = stv090x_get_dmdlock(state, timeout_dmd);
2044 } else {
2045 lock = 0;
2046 }
2047 } else {
2048 if (state->srate <= 4000000)
2049 car_step = 1000;
2050 else if (state->srate <= 7000000)
2051 car_step = 2000;
2052 else if (state->srate <= 10000000)
2053 car_step = 3000;
2054 else
2055 car_step = 5000;
2056
2057 steps = (state->search_range / 1000) / car_step;
2058 steps /= 2;
2059 steps = 2 * (steps + 1);
2060 if (steps < 0)
2061 steps = 2;
2062 else if (steps > 12)
2063 steps = 12;
2064
2065 cur_step = 1;
2066 dir = 1;
2067
2068 if (!lock) {
2069 freq = state->frequency;
2070 state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + state->srate;
2071 while ((cur_step <= steps) && (!lock)) {
2072 if (dir > 0)
2073 freq += cur_step * car_step;
2074 else
2075 freq -= cur_step * car_step;
2076
2077 /* Setup tuner */
Manu Abraham27d40322009-05-02 18:26:58 -03002078 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2079 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002080
Manu Abraham27d40322009-05-02 18:26:58 -03002081 if (state->config->tuner_set_frequency) {
Andreas Regela4978a82009-11-13 18:17:45 -03002082 if (state->config->tuner_set_frequency(fe, freq) < 0)
Manu Abraham27d40322009-05-02 18:26:58 -03002083 goto err;
2084 }
Manu Abrahame415c682009-04-06 15:45:20 -03002085
Manu Abraham27d40322009-05-02 18:26:58 -03002086 if (state->config->tuner_set_bandwidth) {
2087 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
2088 goto err;
2089 }
Manu Abrahame415c682009-04-06 15:45:20 -03002090
Manu Abraham27d40322009-05-02 18:26:58 -03002091 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2092 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002093
2094 msleep(50);
2095
Manu Abraham27d40322009-05-02 18:26:58 -03002096 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2097 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002098
Manu Abraham27d40322009-05-02 18:26:58 -03002099 if (state->config->tuner_get_status) {
2100 if (state->config->tuner_get_status(fe, &reg) < 0)
2101 goto err;
2102 }
Manu Abrahame415c682009-04-06 15:45:20 -03002103
2104 if (reg)
2105 dprintk(FE_DEBUG, 1, "Tuner phase locked");
2106 else
2107 dprintk(FE_DEBUG, 1, "Tuner unlocked");
2108
Manu Abraham27d40322009-05-02 18:26:58 -03002109 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2110 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002111
2112 STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c);
Manu Abrahame415c682009-04-06 15:45:20 -03002113 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
2114 goto err;
2115 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
2116 goto err;
2117 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2118 goto err;
2119 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2120 goto err;
2121 lock = stv090x_get_dmdlock(state, (timeout_dmd / 3));
2122
2123 dir *= -1;
2124 cur_step++;
2125 }
2126 }
2127 }
2128 }
2129
2130 return lock;
2131
2132err:
2133 dprintk(FE_ERROR, 1, "I/O error");
2134 return -1;
2135}
2136
2137static int stv090x_get_loop_params(struct stv090x_state *state, s32 *freq_inc, s32 *timeout_sw, s32 *steps)
2138{
2139 s32 timeout, inc, steps_max, srate, car_max;
2140
2141 srate = state->srate;
2142 car_max = state->search_range / 1000;
Andreas Regel2f5914b2009-04-23 14:56:41 -03002143 car_max += car_max / 10;
Manu Abrahame415c682009-04-06 15:45:20 -03002144 car_max = 65536 * (car_max / 2);
2145 car_max /= (state->mclk / 1000);
2146
2147 if (car_max > 0x4000)
2148 car_max = 0x4000 ; /* maxcarrier should be<= +-1/4 Mclk */
2149
2150 inc = srate;
2151 inc /= state->mclk / 1000;
2152 inc *= 256;
2153 inc *= 256;
2154 inc /= 1000;
2155
Andreas Regel72982f72009-04-08 17:28:41 -03002156 switch (state->search_mode) {
Manu Abrahame415c682009-04-06 15:45:20 -03002157 case STV090x_SEARCH_DVBS1:
2158 case STV090x_SEARCH_DSS:
2159 inc *= 3; /* freq step = 3% of srate */
2160 timeout = 20;
2161 break;
2162
2163 case STV090x_SEARCH_DVBS2:
2164 inc *= 4;
2165 timeout = 25;
2166 break;
2167
2168 case STV090x_SEARCH_AUTO:
2169 default:
2170 inc *= 3;
2171 timeout = 25;
2172 break;
2173 }
2174 inc /= 100;
2175 if ((inc > car_max) || (inc < 0))
2176 inc = car_max / 2; /* increment <= 1/8 Mclk */
2177
2178 timeout *= 27500; /* 27.5 Msps reference */
2179 if (srate > 0)
2180 timeout /= (srate / 1000);
2181
2182 if ((timeout > 100) || (timeout < 0))
2183 timeout = 100;
2184
2185 steps_max = (car_max / inc) + 1; /* min steps = 3 */
2186 if ((steps_max > 100) || (steps_max < 0)) {
2187 steps_max = 100; /* max steps <= 100 */
2188 inc = car_max / steps_max;
2189 }
2190 *freq_inc = inc;
2191 *timeout_sw = timeout;
2192 *steps = steps_max;
2193
2194 return 0;
2195}
2196
2197static int stv090x_chk_signal(struct stv090x_state *state)
2198{
2199 s32 offst_car, agc2, car_max;
2200 int no_signal;
2201
2202 offst_car = STV090x_READ_DEMOD(state, CFR2) << 8;
2203 offst_car |= STV090x_READ_DEMOD(state, CFR1);
Andreas Regel2f5914b2009-04-23 14:56:41 -03002204 offst_car = comp2(offst_car, 16);
Manu Abrahame415c682009-04-06 15:45:20 -03002205
2206 agc2 = STV090x_READ_DEMOD(state, AGC2I1) << 8;
2207 agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
2208 car_max = state->search_range / 1000;
2209
2210 car_max += (car_max / 10); /* 10% margin */
2211 car_max = (65536 * car_max / 2);
2212 car_max /= state->mclk / 1000;
2213
2214 if (car_max > 0x4000)
2215 car_max = 0x4000;
2216
2217 if ((agc2 > 0x2000) || (offst_car > 2 * car_max) || (offst_car < -2 * car_max)) {
2218 no_signal = 1;
2219 dprintk(FE_DEBUG, 1, "No Signal");
2220 } else {
2221 no_signal = 0;
2222 dprintk(FE_DEBUG, 1, "Found Signal");
2223 }
2224
2225 return no_signal;
2226}
2227
2228static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 timeout, int zigzag, s32 steps_max)
2229{
2230 int no_signal, lock = 0;
Manu Abraham27d40322009-05-02 18:26:58 -03002231 s32 cpt_step = 0, offst_freq, car_max;
Manu Abrahame415c682009-04-06 15:45:20 -03002232 u32 reg;
2233
2234 car_max = state->search_range / 1000;
2235 car_max += (car_max / 10);
2236 car_max = (65536 * car_max / 2);
2237 car_max /= (state->mclk / 1000);
2238 if (car_max > 0x4000)
2239 car_max = 0x4000;
2240
2241 if (zigzag)
2242 offst_freq = 0;
2243 else
2244 offst_freq = -car_max + inc;
2245
Manu Abrahame415c682009-04-06 15:45:20 -03002246 do {
2247 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0)
2248 goto err;
2249 if (STV090x_WRITE_DEMOD(state, CFRINIT1, ((offst_freq / 256) & 0xff)) < 0)
2250 goto err;
2251 if (STV090x_WRITE_DEMOD(state, CFRINIT0, offst_freq & 0xff) < 0)
2252 goto err;
2253 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
2254 goto err;
2255
2256 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
2257 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x1); /* stop DVB-S2 packet delin */
2258 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
2259 goto err;
2260
Manu Abrahame415c682009-04-06 15:45:20 -03002261 if (zigzag) {
2262 if (offst_freq >= 0)
2263 offst_freq = -offst_freq - 2 * inc;
2264 else
2265 offst_freq = -offst_freq;
2266 } else {
2267 offst_freq += 2 * inc;
2268 }
2269
Andreas Regel2f5914b2009-04-23 14:56:41 -03002270 cpt_step++;
2271
Manu Abrahame415c682009-04-06 15:45:20 -03002272 lock = stv090x_get_dmdlock(state, timeout);
2273 no_signal = stv090x_chk_signal(state);
2274
2275 } while ((!lock) &&
2276 (!no_signal) &&
2277 ((offst_freq - inc) < car_max) &&
2278 ((offst_freq + inc) > -car_max) &&
2279 (cpt_step < steps_max));
2280
2281 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
2282 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0);
2283 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
2284 goto err;
2285
2286 return lock;
2287err:
2288 dprintk(FE_ERROR, 1, "I/O error");
2289 return -1;
2290}
2291
2292static int stv090x_sw_algo(struct stv090x_state *state)
2293{
2294 int no_signal, zigzag, lock = 0;
2295 u32 reg;
2296
2297 s32 dvbs2_fly_wheel;
2298 s32 inc, timeout_step, trials, steps_max;
2299
Manu Abraham27d40322009-05-02 18:26:58 -03002300 /* get params */
2301 stv090x_get_loop_params(state, &inc, &timeout_step, &steps_max);
Manu Abrahame415c682009-04-06 15:45:20 -03002302
Andreas Regel72982f72009-04-08 17:28:41 -03002303 switch (state->search_mode) {
Manu Abrahame415c682009-04-06 15:45:20 -03002304 case STV090x_SEARCH_DVBS1:
2305 case STV090x_SEARCH_DSS:
2306 /* accelerate the frequency detector */
2307 if (state->dev_ver >= 0x20) {
2308 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3B) < 0)
2309 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002310 }
Manu Abraham27d40322009-05-02 18:26:58 -03002311
Manu Abrahame415c682009-04-06 15:45:20 -03002312 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x49) < 0)
2313 goto err;
2314 zigzag = 0;
2315 break;
2316
2317 case STV090x_SEARCH_DVBS2:
2318 if (state->dev_ver >= 0x20) {
2319 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2320 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002321 }
Manu Abraham27d40322009-05-02 18:26:58 -03002322
Manu Abrahame415c682009-04-06 15:45:20 -03002323 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2324 goto err;
2325 zigzag = 1;
2326 break;
2327
2328 case STV090x_SEARCH_AUTO:
2329 default:
2330 /* accelerate the frequency detector */
2331 if (state->dev_ver >= 0x20) {
2332 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3b) < 0)
2333 goto err;
2334 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2335 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002336 }
Manu Abraham27d40322009-05-02 18:26:58 -03002337
Andreas Regel2f5914b2009-04-23 14:56:41 -03002338 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0xc9) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03002339 goto err;
2340 zigzag = 0;
2341 break;
2342 }
2343
2344 trials = 0;
2345 do {
2346 lock = stv090x_search_car_loop(state, inc, timeout_step, zigzag, steps_max);
2347 no_signal = stv090x_chk_signal(state);
2348 trials++;
2349
2350 /*run the SW search 2 times maximum*/
2351 if (lock || no_signal || (trials == 2)) {
2352 /*Check if the demod is not losing lock in DVBS2*/
2353 if (state->dev_ver >= 0x20) {
2354 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
2355 goto err;
2356 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
2357 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002358 }
2359
2360 reg = STV090x_READ_DEMOD(state, DMDSTATE);
2361 if ((lock) && (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == STV090x_DVBS2)) {
2362 /*Check if the demod is not losing lock in DVBS2*/
2363 msleep(timeout_step);
2364 reg = STV090x_READ_DEMOD(state, DMDFLYW);
2365 dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
2366 if (dvbs2_fly_wheel < 0xd) { /*if correct frames is decrementing */
2367 msleep(timeout_step);
2368 reg = STV090x_READ_DEMOD(state, DMDFLYW);
2369 dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
2370 }
2371 if (dvbs2_fly_wheel < 0xd) {
2372 /*FALSE lock, The demod is loosing lock */
2373 lock = 0;
2374 if (trials < 2) {
2375 if (state->dev_ver >= 0x20) {
2376 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2377 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002378 }
Manu Abraham27d40322009-05-02 18:26:58 -03002379
Manu Abrahame415c682009-04-06 15:45:20 -03002380 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2381 goto err;
2382 }
2383 }
2384 }
2385 }
2386 } while ((!lock) && (trials < 2) && (!no_signal));
2387
2388 return lock;
2389err:
2390 dprintk(FE_ERROR, 1, "I/O error");
2391 return -1;
2392}
2393
2394static enum stv090x_delsys stv090x_get_std(struct stv090x_state *state)
2395{
2396 u32 reg;
2397 enum stv090x_delsys delsys;
2398
2399 reg = STV090x_READ_DEMOD(state, DMDSTATE);
2400 if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 2)
2401 delsys = STV090x_DVBS2;
2402 else if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 3) {
2403 reg = STV090x_READ_DEMOD(state, FECM);
2404 if (STV090x_GETFIELD_Px(reg, DSS_DVB_FIELD) == 1)
2405 delsys = STV090x_DSS;
2406 else
2407 delsys = STV090x_DVBS1;
2408 } else {
2409 delsys = STV090x_ERROR;
2410 }
2411
2412 return delsys;
2413}
2414
2415/* in Hz */
2416static s32 stv090x_get_car_freq(struct stv090x_state *state, u32 mclk)
2417{
2418 s32 derot, int_1, int_2, tmp_1, tmp_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002419
2420 derot = STV090x_READ_DEMOD(state, CFR2) << 16;
2421 derot |= STV090x_READ_DEMOD(state, CFR1) << 8;
2422 derot |= STV090x_READ_DEMOD(state, CFR0);
2423
2424 derot = comp2(derot, 24);
Manu Abrahamda4b9052009-04-08 20:30:29 -03002425 int_1 = state->mclk >> 12;
2426 int_2 = derot >> 12;
Manu Abrahame415c682009-04-06 15:45:20 -03002427
Manu Abrahamda4b9052009-04-08 20:30:29 -03002428 /* carrier_frequency = MasterClock * Reg / 2^24 */
2429 tmp_1 = state->mclk % 0x1000;
2430 tmp_2 = derot % 0x1000;
Manu Abrahame415c682009-04-06 15:45:20 -03002431
2432 derot = (int_1 * int_2) +
Manu Abrahamda4b9052009-04-08 20:30:29 -03002433 ((int_1 * tmp_2) >> 12) +
Andreas Regelb671a8d2009-11-13 18:15:27 -03002434 ((int_2 * tmp_1) >> 12);
Manu Abrahame415c682009-04-06 15:45:20 -03002435
2436 return derot;
2437}
2438
2439static int stv090x_get_viterbi(struct stv090x_state *state)
2440{
2441 u32 reg, rate;
2442
2443 reg = STV090x_READ_DEMOD(state, VITCURPUN);
2444 rate = STV090x_GETFIELD_Px(reg, VIT_CURPUN_FIELD);
2445
2446 switch (rate) {
2447 case 13:
2448 state->fec = STV090x_PR12;
2449 break;
2450
2451 case 18:
2452 state->fec = STV090x_PR23;
2453 break;
2454
2455 case 21:
2456 state->fec = STV090x_PR34;
2457 break;
2458
2459 case 24:
2460 state->fec = STV090x_PR56;
2461 break;
2462
2463 case 25:
2464 state->fec = STV090x_PR67;
2465 break;
2466
2467 case 26:
2468 state->fec = STV090x_PR78;
2469 break;
2470
2471 default:
2472 state->fec = STV090x_PRERR;
2473 break;
2474 }
2475
2476 return 0;
2477}
2478
2479static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *state)
2480{
2481 struct dvb_frontend *fe = &state->frontend;
2482
2483 u8 tmg;
2484 u32 reg;
2485 s32 i = 0, offst_freq;
2486
2487 msleep(5);
2488
2489 if (state->algo == STV090x_BLIND_SEARCH) {
2490 tmg = STV090x_READ_DEMOD(state, TMGREG2);
2491 STV090x_WRITE_DEMOD(state, SFRSTEP, 0x5c);
Andreas Regel2f5914b2009-04-23 14:56:41 -03002492 while ((i <= 50) && (tmg != 0) && (tmg != 0xff)) {
Manu Abrahame415c682009-04-06 15:45:20 -03002493 tmg = STV090x_READ_DEMOD(state, TMGREG2);
2494 msleep(5);
2495 i += 5;
2496 }
2497 }
2498 state->delsys = stv090x_get_std(state);
2499
Manu Abraham27d40322009-05-02 18:26:58 -03002500 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2501 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002502
Manu Abraham27d40322009-05-02 18:26:58 -03002503 if (state->config->tuner_get_frequency) {
2504 if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
2505 goto err;
2506 }
Manu Abrahame415c682009-04-06 15:45:20 -03002507
Manu Abraham27d40322009-05-02 18:26:58 -03002508 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2509 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002510
2511 offst_freq = stv090x_get_car_freq(state, state->mclk) / 1000;
2512 state->frequency += offst_freq;
Manu Abraham27d40322009-05-02 18:26:58 -03002513
2514 if (stv090x_get_viterbi(state) < 0)
2515 goto err;
2516
Manu Abrahame415c682009-04-06 15:45:20 -03002517 reg = STV090x_READ_DEMOD(state, DMDMODCOD);
2518 state->modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
2519 state->pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
2520 state->frame_len = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) >> 1;
2521 reg = STV090x_READ_DEMOD(state, TMGOBS);
2522 state->rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
2523 reg = STV090x_READ_DEMOD(state, FECM);
2524 state->inversion = STV090x_GETFIELD_Px(reg, IQINV_FIELD);
2525
2526 if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000)) {
2527
Manu Abraham27d40322009-05-02 18:26:58 -03002528 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2529 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002530
Manu Abraham27d40322009-05-02 18:26:58 -03002531 if (state->config->tuner_get_frequency) {
2532 if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
2533 goto err;
2534 }
Manu Abrahame415c682009-04-06 15:45:20 -03002535
Manu Abraham27d40322009-05-02 18:26:58 -03002536 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2537 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002538
2539 if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
Andreas Regel2f5914b2009-04-23 14:56:41 -03002540 return STV090x_RANGEOK;
Manu Abrahame415c682009-04-06 15:45:20 -03002541 else if (abs(offst_freq) <= (stv090x_car_width(state->srate, state->rolloff) / 2000))
2542 return STV090x_RANGEOK;
2543 else
2544 return STV090x_OUTOFRANGE; /* Out of Range */
2545 } else {
2546 if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
2547 return STV090x_RANGEOK;
2548 else
2549 return STV090x_OUTOFRANGE;
2550 }
2551
2552 return STV090x_OUTOFRANGE;
Manu Abraham27d40322009-05-02 18:26:58 -03002553err:
2554 dprintk(FE_ERROR, 1, "I/O error");
2555 return -1;
Manu Abrahame415c682009-04-06 15:45:20 -03002556}
2557
2558static u32 stv090x_get_tmgoffst(struct stv090x_state *state, u32 srate)
2559{
2560 s32 offst_tmg;
Manu Abrahame415c682009-04-06 15:45:20 -03002561
2562 offst_tmg = STV090x_READ_DEMOD(state, TMGREG2) << 16;
2563 offst_tmg |= STV090x_READ_DEMOD(state, TMGREG1) << 8;
2564 offst_tmg |= STV090x_READ_DEMOD(state, TMGREG0);
2565
Manu Abrahame415c682009-04-06 15:45:20 -03002566 offst_tmg = comp2(offst_tmg, 24); /* 2's complement */
2567 if (!offst_tmg)
2568 offst_tmg = 1;
2569
Manu Abraham5f99feff2009-04-08 20:24:53 -03002570 offst_tmg = ((s32) srate * 10) / ((s32) 0x1000000 / offst_tmg);
Manu Abrahame415c682009-04-06 15:45:20 -03002571 offst_tmg /= 320;
2572
2573 return offst_tmg;
2574}
2575
2576static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_modcod modcod, s32 pilots)
2577{
2578 u8 aclc = 0x29;
2579 s32 i;
Manu Abraham27d40322009-05-02 18:26:58 -03002580 struct stv090x_long_frame_crloop *car_loop, *car_loop_qpsk_low, *car_loop_apsk_low;
Manu Abrahame415c682009-04-06 15:45:20 -03002581
Manu Abraham27d40322009-05-02 18:26:58 -03002582 if (state->dev_ver == 0x20) {
2583 car_loop = stv090x_s2_crl_cut20;
2584 car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut20;
2585 car_loop_apsk_low = stv090x_s2_apsk_crl_cut20;
2586 } else {
2587 /* >= Cut 3 */
2588 car_loop = stv090x_s2_crl_cut30;
2589 car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut30;
2590 car_loop_apsk_low = stv090x_s2_apsk_crl_cut30;
2591 }
Manu Abrahame415c682009-04-06 15:45:20 -03002592
2593 if (modcod < STV090x_QPSK_12) {
2594 i = 0;
Manu Abraham27d40322009-05-02 18:26:58 -03002595 while ((i < 3) && (modcod != car_loop_qpsk_low[i].modcod))
Manu Abrahame415c682009-04-06 15:45:20 -03002596 i++;
2597
2598 if (i >= 3)
2599 i = 2;
2600
2601 } else {
2602 i = 0;
2603 while ((i < 14) && (modcod != car_loop[i].modcod))
2604 i++;
2605
2606 if (i >= 14) {
2607 i = 0;
Manu Abraham27d40322009-05-02 18:26:58 -03002608 while ((i < 11) && (modcod != car_loop_apsk_low[i].modcod))
Manu Abrahame415c682009-04-06 15:45:20 -03002609 i++;
2610
2611 if (i >= 11)
2612 i = 10;
2613 }
2614 }
2615
2616 if (modcod <= STV090x_QPSK_25) {
2617 if (pilots) {
2618 if (state->srate <= 3000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002619 aclc = car_loop_qpsk_low[i].crl_pilots_on_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002620 else if (state->srate <= 7000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002621 aclc = car_loop_qpsk_low[i].crl_pilots_on_5;
Manu Abrahame415c682009-04-06 15:45:20 -03002622 else if (state->srate <= 15000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002623 aclc = car_loop_qpsk_low[i].crl_pilots_on_10;
Manu Abrahame415c682009-04-06 15:45:20 -03002624 else if (state->srate <= 25000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002625 aclc = car_loop_qpsk_low[i].crl_pilots_on_20;
Manu Abrahame415c682009-04-06 15:45:20 -03002626 else
Manu Abraham27d40322009-05-02 18:26:58 -03002627 aclc = car_loop_qpsk_low[i].crl_pilots_on_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002628 } else {
2629 if (state->srate <= 3000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002630 aclc = car_loop_qpsk_low[i].crl_pilots_off_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002631 else if (state->srate <= 7000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002632 aclc = car_loop_qpsk_low[i].crl_pilots_off_5;
Manu Abrahame415c682009-04-06 15:45:20 -03002633 else if (state->srate <= 15000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002634 aclc = car_loop_qpsk_low[i].crl_pilots_off_10;
Manu Abrahame415c682009-04-06 15:45:20 -03002635 else if (state->srate <= 25000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002636 aclc = car_loop_qpsk_low[i].crl_pilots_off_20;
Manu Abrahame415c682009-04-06 15:45:20 -03002637 else
Manu Abraham27d40322009-05-02 18:26:58 -03002638 aclc = car_loop_qpsk_low[i].crl_pilots_off_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002639 }
2640
2641 } else if (modcod <= STV090x_8PSK_910) {
2642 if (pilots) {
2643 if (state->srate <= 3000000)
2644 aclc = car_loop[i].crl_pilots_on_2;
2645 else if (state->srate <= 7000000)
2646 aclc = car_loop[i].crl_pilots_on_5;
2647 else if (state->srate <= 15000000)
2648 aclc = car_loop[i].crl_pilots_on_10;
2649 else if (state->srate <= 25000000)
2650 aclc = car_loop[i].crl_pilots_on_20;
2651 else
2652 aclc = car_loop[i].crl_pilots_on_30;
2653 } else {
2654 if (state->srate <= 3000000)
2655 aclc = car_loop[i].crl_pilots_off_2;
2656 else if (state->srate <= 7000000)
2657 aclc = car_loop[i].crl_pilots_off_5;
2658 else if (state->srate <= 15000000)
2659 aclc = car_loop[i].crl_pilots_off_10;
2660 else if (state->srate <= 25000000)
2661 aclc = car_loop[i].crl_pilots_off_20;
2662 else
2663 aclc = car_loop[i].crl_pilots_off_30;
2664 }
2665 } else { /* 16APSK and 32APSK */
2666 if (state->srate <= 3000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002667 aclc = car_loop_apsk_low[i].crl_pilots_on_2;
Manu Abrahame415c682009-04-06 15:45:20 -03002668 else if (state->srate <= 7000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002669 aclc = car_loop_apsk_low[i].crl_pilots_on_5;
Manu Abrahame415c682009-04-06 15:45:20 -03002670 else if (state->srate <= 15000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002671 aclc = car_loop_apsk_low[i].crl_pilots_on_10;
Manu Abrahame415c682009-04-06 15:45:20 -03002672 else if (state->srate <= 25000000)
Manu Abraham27d40322009-05-02 18:26:58 -03002673 aclc = car_loop_apsk_low[i].crl_pilots_on_20;
Manu Abrahame415c682009-04-06 15:45:20 -03002674 else
Manu Abraham27d40322009-05-02 18:26:58 -03002675 aclc = car_loop_apsk_low[i].crl_pilots_on_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002676 }
2677
2678 return aclc;
2679}
2680
2681static u8 stv090x_optimize_carloop_short(struct stv090x_state *state)
2682{
Manu Abraham0a5ded52009-06-17 16:48:17 -03002683 struct stv090x_short_frame_crloop *short_crl = NULL;
Manu Abrahame415c682009-04-06 15:45:20 -03002684 s32 index = 0;
2685 u8 aclc = 0x0b;
2686
2687 switch (state->modulation) {
2688 case STV090x_QPSK:
2689 default:
2690 index = 0;
2691 break;
2692 case STV090x_8PSK:
2693 index = 1;
2694 break;
2695 case STV090x_16APSK:
2696 index = 2;
2697 break;
2698 case STV090x_32APSK:
2699 index = 3;
2700 break;
2701 }
2702
Manu Abrahameebf8d82009-06-18 04:50:53 -03002703 if (state->dev_ver >= 0x30) {
2704 /* Cut 3.0 and up */
Manu Abraham27d40322009-05-02 18:26:58 -03002705 short_crl = stv090x_s2_short_crl_cut30;
Manu Abrahameebf8d82009-06-18 04:50:53 -03002706 } else {
2707 /* Cut 2.0 and up: we don't support cuts older than 2.0 */
2708 short_crl = stv090x_s2_short_crl_cut20;
2709 }
Manu Abrahame415c682009-04-06 15:45:20 -03002710
Manu Abraham27d40322009-05-02 18:26:58 -03002711 if (state->srate <= 3000000)
2712 aclc = short_crl[index].crl_2;
2713 else if (state->srate <= 7000000)
2714 aclc = short_crl[index].crl_5;
2715 else if (state->srate <= 15000000)
2716 aclc = short_crl[index].crl_10;
2717 else if (state->srate <= 25000000)
2718 aclc = short_crl[index].crl_20;
2719 else
2720 aclc = short_crl[index].crl_30;
Manu Abrahame415c682009-04-06 15:45:20 -03002721
2722 return aclc;
2723}
2724
2725static int stv090x_optimize_track(struct stv090x_state *state)
2726{
2727 struct dvb_frontend *fe = &state->frontend;
2728
2729 enum stv090x_rolloff rolloff;
2730 enum stv090x_modcod modcod;
2731
2732 s32 srate, pilots, aclc, f_1, f_0, i = 0, blind_tune = 0;
2733 u32 reg;
2734
2735 srate = stv090x_get_srate(state, state->mclk);
2736 srate += stv090x_get_tmgoffst(state, srate);
2737
2738 switch (state->delsys) {
2739 case STV090x_DVBS1:
2740 case STV090x_DSS:
Andreas Regelb671a8d2009-11-13 18:15:27 -03002741 if (state->search_mode == STV090x_SEARCH_AUTO) {
Manu Abrahame415c682009-04-06 15:45:20 -03002742 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2743 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
2744 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
2745 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2746 goto err;
2747 }
2748 reg = STV090x_READ_DEMOD(state, DEMOD);
2749 STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
Manu Abraham27d40322009-05-02 18:26:58 -03002750 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x01);
Manu Abrahame415c682009-04-06 15:45:20 -03002751 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
2752 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03002753
2754 if (state->dev_ver >= 0x30) {
2755 if (stv090x_get_viterbi(state) < 0)
2756 goto err;
2757
2758 if (state->fec == STV090x_PR12) {
2759 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x98) < 0)
2760 goto err;
2761 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2762 goto err;
2763 } else {
2764 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x18) < 0)
2765 goto err;
2766 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2767 goto err;
2768 }
2769 }
2770
Manu Abrahame415c682009-04-06 15:45:20 -03002771 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
2772 goto err;
2773 break;
2774
2775 case STV090x_DVBS2:
2776 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
Andreas Regel2f5914b2009-04-23 14:56:41 -03002777 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
2778 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
Manu Abrahame415c682009-04-06 15:45:20 -03002779 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2780 goto err;
2781 if (STV090x_WRITE_DEMOD(state, ACLC, 0) < 0)
2782 goto err;
2783 if (STV090x_WRITE_DEMOD(state, BCLC, 0) < 0)
2784 goto err;
2785 if (state->frame_len == STV090x_LONG_FRAME) {
2786 reg = STV090x_READ_DEMOD(state, DMDMODCOD);
2787 modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
2788 pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
2789 aclc = stv090x_optimize_carloop(state, modcod, pilots);
2790 if (modcod <= STV090x_QPSK_910) {
2791 STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc);
2792 } else if (modcod <= STV090x_8PSK_910) {
2793 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2794 goto err;
2795 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
2796 goto err;
2797 }
2798 if ((state->demod_mode == STV090x_SINGLE) && (modcod > STV090x_8PSK_910)) {
2799 if (modcod <= STV090x_16APSK_910) {
2800 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2801 goto err;
2802 if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2803 goto err;
2804 } else {
2805 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2806 goto err;
2807 if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
2808 goto err;
2809 }
2810 }
2811 } else {
2812 /*Carrier loop setting for short frame*/
2813 aclc = stv090x_optimize_carloop_short(state);
2814 if (state->modulation == STV090x_QPSK) {
2815 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc) < 0)
2816 goto err;
2817 } else if (state->modulation == STV090x_8PSK) {
2818 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2819 goto err;
2820 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
2821 goto err;
2822 } else if (state->modulation == STV090x_16APSK) {
2823 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2824 goto err;
2825 if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2826 goto err;
2827 } else if (state->modulation == STV090x_32APSK) {
2828 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2829 goto err;
2830 if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
2831 goto err;
2832 }
2833 }
Manu Abraham27d40322009-05-02 18:26:58 -03002834
Manu Abrahame415c682009-04-06 15:45:20 -03002835 STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67); /* PER */
2836 break;
2837
2838 case STV090x_UNKNOWN:
2839 default:
2840 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2841 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
2842 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
2843 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2844 goto err;
2845 break;
2846 }
2847
2848 f_1 = STV090x_READ_DEMOD(state, CFR2);
2849 f_0 = STV090x_READ_DEMOD(state, CFR1);
2850 reg = STV090x_READ_DEMOD(state, TMGOBS);
2851 rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
2852
2853 if (state->algo == STV090x_BLIND_SEARCH) {
2854 STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00);
2855 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2856 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0x00);
2857 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
2858 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2859 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03002860 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03002861 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03002862
2863 if (stv090x_set_srate(state, srate) < 0)
2864 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002865 blind_tune = 1;
Andreas Regel7b035da2009-11-13 18:20:54 -03002866
2867 if (stv090x_dvbs_track_crl(state) < 0)
2868 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002869 }
2870
2871 if (state->dev_ver >= 0x20) {
2872 if ((state->search_mode == STV090x_SEARCH_DVBS1) ||
2873 (state->search_mode == STV090x_SEARCH_DSS) ||
2874 (state->search_mode == STV090x_SEARCH_AUTO)) {
2875
2876 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x0a) < 0)
2877 goto err;
2878 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x00) < 0)
2879 goto err;
2880 }
2881 }
2882
Manu Abrahame415c682009-04-06 15:45:20 -03002883 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
2884 goto err;
2885
Manu Abraham27d40322009-05-02 18:26:58 -03002886 /* AUTO tracking MODE */
2887 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x80) < 0)
2888 goto err;
2889 /* AUTO tracking MODE */
2890 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x80) < 0)
2891 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002892
Manu Abraham27d40322009-05-02 18:26:58 -03002893 if ((state->dev_ver >= 0x20) || (blind_tune == 1) || (state->srate < 10000000)) {
2894 /* update initial carrier freq with the found freq offset */
Manu Abrahame415c682009-04-06 15:45:20 -03002895 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
2896 goto err;
2897 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
2898 goto err;
2899 state->tuner_bw = stv090x_car_width(srate, state->rolloff) + 10000000;
2900
2901 if ((state->dev_ver >= 0x20) || (blind_tune == 1)) {
2902
2903 if (state->algo != STV090x_WARM_SEARCH) {
2904
Manu Abraham27d40322009-05-02 18:26:58 -03002905 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
2906 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002907
Manu Abraham27d40322009-05-02 18:26:58 -03002908 if (state->config->tuner_set_bandwidth) {
2909 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
2910 goto err;
2911 }
Manu Abrahame415c682009-04-06 15:45:20 -03002912
Manu Abraham27d40322009-05-02 18:26:58 -03002913 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2914 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03002915
2916 }
2917 }
2918 if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000))
2919 msleep(50); /* blind search: wait 50ms for SR stabilization */
2920 else
2921 msleep(5);
2922
2923 stv090x_get_lock_tmg(state);
2924
2925 if (!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) {
2926 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2927 goto err;
2928 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
2929 goto err;
2930 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
2931 goto err;
2932 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
2933 goto err;
2934
2935 i = 0;
2936
2937 while ((!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) && (i <= 2)) {
2938
2939 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2940 goto err;
2941 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
2942 goto err;
2943 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
2944 goto err;
2945 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
2946 goto err;
2947 i++;
2948 }
2949 }
2950
2951 }
2952
2953 if (state->dev_ver >= 0x20) {
2954 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
2955 goto err;
2956 }
Manu Abraham27d40322009-05-02 18:26:58 -03002957
Manu Abrahame415c682009-04-06 15:45:20 -03002958 if ((state->delsys == STV090x_DVBS1) || (state->delsys == STV090x_DSS))
2959 stv090x_set_vit_thtracq(state);
2960
2961 return 0;
2962err:
2963 dprintk(FE_ERROR, 1, "I/O error");
2964 return -1;
2965}
2966
2967static int stv090x_get_feclock(struct stv090x_state *state, s32 timeout)
2968{
2969 s32 timer = 0, lock = 0, stat;
2970 u32 reg;
2971
2972 while ((timer < timeout) && (!lock)) {
2973 reg = STV090x_READ_DEMOD(state, DMDSTATE);
2974 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
2975
2976 switch (stat) {
2977 case 0: /* searching */
2978 case 1: /* first PLH detected */
2979 default:
2980 lock = 0;
2981 break;
2982
2983 case 2: /* DVB-S2 mode */
2984 reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
2985 lock = STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD);
2986 break;
2987
2988 case 3: /* DVB-S1/legacy mode */
2989 reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
2990 lock = STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD);
2991 break;
2992 }
2993 if (!lock) {
2994 msleep(10);
2995 timer += 10;
2996 }
2997 }
2998 return lock;
2999}
3000
3001static int stv090x_get_lock(struct stv090x_state *state, s32 timeout_dmd, s32 timeout_fec)
3002{
3003 u32 reg;
3004 s32 timer = 0;
3005 int lock;
3006
3007 lock = stv090x_get_dmdlock(state, timeout_dmd);
3008 if (lock)
3009 lock = stv090x_get_feclock(state, timeout_fec);
3010
3011 if (lock) {
3012 lock = 0;
3013
3014 while ((timer < timeout_fec) && (!lock)) {
3015 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3016 lock = STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD);
3017 msleep(1);
3018 timer++;
3019 }
3020 }
3021
3022 return lock;
3023}
3024
3025static int stv090x_set_s2rolloff(struct stv090x_state *state)
3026{
Manu Abrahame415c682009-04-06 15:45:20 -03003027 u32 reg;
3028
Manu Abraham27d40322009-05-02 18:26:58 -03003029 if (state->dev_ver <= 0x20) {
3030 /* rolloff to auto mode if DVBS2 */
Manu Abrahame415c682009-04-06 15:45:20 -03003031 reg = STV090x_READ_DEMOD(state, DEMOD);
Manu Abraham27d40322009-05-02 18:26:58 -03003032 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x00);
Manu Abrahame415c682009-04-06 15:45:20 -03003033 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3034 goto err;
3035 } else {
Manu Abraham27d40322009-05-02 18:26:58 -03003036 /* DVB-S2 rolloff to auto mode if DVBS2 */
Manu Abrahame415c682009-04-06 15:45:20 -03003037 reg = STV090x_READ_DEMOD(state, DEMOD);
Manu Abraham27d40322009-05-02 18:26:58 -03003038 STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 0x00);
Manu Abrahame415c682009-04-06 15:45:20 -03003039 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3040 goto err;
3041 }
3042 return 0;
3043err:
3044 dprintk(FE_ERROR, 1, "I/O error");
3045 return -1;
3046}
3047
Manu Abrahame415c682009-04-06 15:45:20 -03003048
3049static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
3050{
3051 struct dvb_frontend *fe = &state->frontend;
3052 enum stv090x_signal_state signal_state = STV090x_NOCARRIER;
3053 u32 reg;
Andreas Regela4978a82009-11-13 18:17:45 -03003054 s32 agc1_power, power_iq = 0, i;
Andreas Regel4e58a682009-04-16 08:40:36 -03003055 int lock = 0, low_sr = 0, no_signal = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003056
3057 reg = STV090x_READ_DEMOD(state, TSCFGH);
3058 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* Stop path 1 stream merger */
3059 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3060 goto err;
3061
3062 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod stop */
3063 goto err;
3064
3065 if (state->dev_ver >= 0x20) {
Andreas Regel7b035da2009-11-13 18:20:54 -03003066 if (state->srate > 5000000) {
3067 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
3068 goto err;
3069 } else {
3070 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x82) < 0)
3071 goto err;
3072 }
Manu Abrahame415c682009-04-06 15:45:20 -03003073 }
3074
3075 stv090x_get_lock_tmg(state);
3076
3077 if (state->algo == STV090x_BLIND_SEARCH) {
3078 state->tuner_bw = 2 * 36000000; /* wide bw for unknown srate */
Manu Abraham27d40322009-05-02 18:26:58 -03003079 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0) /* wider srate scan */
Manu Abrahame415c682009-04-06 15:45:20 -03003080 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003081 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
3082 goto err;
3083 if (stv090x_set_srate(state, 1000000) < 0) /* inital srate = 1Msps */
3084 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003085 } else {
3086 /* known srate */
3087 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
3088 goto err;
3089 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
3090 goto err;
3091
Manu Abraham27d40322009-05-02 18:26:58 -03003092 if (state->srate < 2000000) {
3093 /* SR < 2MSPS */
3094 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x63) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003095 goto err;
3096 } else {
Manu Abraham27d40322009-05-02 18:26:58 -03003097 /* SR >= 2Msps */
3098 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003099 goto err;
3100 }
3101
Manu Abraham27d40322009-05-02 18:26:58 -03003102 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
3103 goto err;
3104
Manu Abrahame415c682009-04-06 15:45:20 -03003105 if (state->dev_ver >= 0x20) {
3106 if (STV090x_WRITE_DEMOD(state, KREFTMG, 0x5a) < 0)
3107 goto err;
3108 if (state->algo == STV090x_COLD_SEARCH)
Andreas Regel4e58a682009-04-16 08:40:36 -03003109 state->tuner_bw = (15 * (stv090x_car_width(state->srate, state->rolloff) + 10000000)) / 10;
Manu Abrahame415c682009-04-06 15:45:20 -03003110 else if (state->algo == STV090x_WARM_SEARCH)
3111 state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + 10000000;
Manu Abrahame415c682009-04-06 15:45:20 -03003112 }
Manu Abraham27d40322009-05-02 18:26:58 -03003113
3114 /* if cold start or warm (Symbolrate is known)
3115 * use a Narrow symbol rate scan range
3116 */
3117 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0) /* narrow srate scan */
Manu Abrahame415c682009-04-06 15:45:20 -03003118 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003119
3120 if (stv090x_set_srate(state, state->srate) < 0)
3121 goto err;
3122
3123 if (stv090x_set_max_srate(state, state->mclk, state->srate) < 0)
3124 goto err;
3125 if (stv090x_set_min_srate(state, state->mclk, state->srate) < 0)
3126 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003127
3128 if (state->srate >= 10000000)
Andreas Regel4e58a682009-04-16 08:40:36 -03003129 low_sr = 0;
3130 else
Manu Abrahame415c682009-04-06 15:45:20 -03003131 low_sr = 1;
3132 }
3133
3134 /* Setup tuner */
Manu Abraham27d40322009-05-02 18:26:58 -03003135 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
3136 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003137
Manu Abraham27d40322009-05-02 18:26:58 -03003138 if (state->config->tuner_set_bbgain) {
3139 if (state->config->tuner_set_bbgain(fe, 10) < 0) /* 10dB */
3140 goto err;
3141 }
Manu Abrahame415c682009-04-06 15:45:20 -03003142
Manu Abraham27d40322009-05-02 18:26:58 -03003143 if (state->config->tuner_set_frequency) {
3144 if (state->config->tuner_set_frequency(fe, state->frequency) < 0)
3145 goto err;
3146 }
Manu Abrahame415c682009-04-06 15:45:20 -03003147
Manu Abraham27d40322009-05-02 18:26:58 -03003148 if (state->config->tuner_set_bandwidth) {
3149 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
3150 goto err;
3151 }
Manu Abrahame415c682009-04-06 15:45:20 -03003152
Manu Abraham27d40322009-05-02 18:26:58 -03003153 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
3154 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003155
3156 msleep(50);
3157
Manu Abraham27d40322009-05-02 18:26:58 -03003158 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
3159 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003160
Manu Abraham27d40322009-05-02 18:26:58 -03003161 if (state->config->tuner_get_status) {
3162 if (state->config->tuner_get_status(fe, &reg) < 0)
3163 goto err;
3164 }
Manu Abrahame415c682009-04-06 15:45:20 -03003165
3166 if (reg)
3167 dprintk(FE_DEBUG, 1, "Tuner phase locked");
3168 else
3169 dprintk(FE_DEBUG, 1, "Tuner unlocked");
3170
Manu Abraham27d40322009-05-02 18:26:58 -03003171 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003172 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003173
Manu Abraham27d40322009-05-02 18:26:58 -03003174 msleep(10);
3175 agc1_power = MAKEWORD16(STV090x_READ_DEMOD(state, AGCIQIN1),
3176 STV090x_READ_DEMOD(state, AGCIQIN0));
3177
3178 if (agc1_power == 0) {
3179 /* If AGC1 integrator value is 0
3180 * then read POWERI, POWERQ
3181 */
3182 for (i = 0; i < 5; i++) {
3183 power_iq += (STV090x_READ_DEMOD(state, POWERI) +
3184 STV090x_READ_DEMOD(state, POWERQ)) >> 1;
3185 }
3186 power_iq /= 5;
Manu Abrahame415c682009-04-06 15:45:20 -03003187 }
3188
Manu Abraham27d40322009-05-02 18:26:58 -03003189 if ((agc1_power == 0) && (power_iq < STV090x_IQPOWER_THRESHOLD)) {
3190 dprintk(FE_ERROR, 1, "No Signal: POWER_IQ=0x%02x", power_iq);
3191 lock = 0;
Andreas Regelc4fa6492009-11-13 18:18:53 -03003192 signal_state = STV090x_NOAGC1;
Manu Abraham27d40322009-05-02 18:26:58 -03003193 } else {
3194 reg = STV090x_READ_DEMOD(state, DEMOD);
3195 STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, state->inversion);
3196
3197 if (state->dev_ver <= 0x20) {
3198 /* rolloff to auto mode if DVBS2 */
3199 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 1);
3200 } else {
3201 /* DVB-S2 rolloff to auto mode if DVBS2 */
3202 STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 1);
3203 }
3204 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3205 goto err;
3206
3207 if (stv090x_delivery_search(state) < 0)
3208 goto err;
3209
3210 if (state->algo != STV090x_BLIND_SEARCH) {
3211 if (stv090x_start_search(state) < 0)
3212 goto err;
3213 }
3214 }
3215
Andreas Regelc4fa6492009-11-13 18:18:53 -03003216 if (signal_state == STV090x_NOAGC1)
3217 return signal_state;
Manu Abraham27d40322009-05-02 18:26:58 -03003218
Manu Abrahame415c682009-04-06 15:45:20 -03003219 if (state->algo == STV090x_BLIND_SEARCH)
3220 lock = stv090x_blind_search(state);
Manu Abraham27d40322009-05-02 18:26:58 -03003221
Manu Abrahame415c682009-04-06 15:45:20 -03003222 else if (state->algo == STV090x_COLD_SEARCH)
Andreas Regela4978a82009-11-13 18:17:45 -03003223 lock = stv090x_get_coldlock(state, state->DemodTimeout);
Manu Abraham27d40322009-05-02 18:26:58 -03003224
Manu Abrahame415c682009-04-06 15:45:20 -03003225 else if (state->algo == STV090x_WARM_SEARCH)
Andreas Regela4978a82009-11-13 18:17:45 -03003226 lock = stv090x_get_dmdlock(state, state->DemodTimeout);
Manu Abrahame415c682009-04-06 15:45:20 -03003227
3228 if ((!lock) && (state->algo == STV090x_COLD_SEARCH)) {
3229 if (!low_sr) {
3230 if (stv090x_chk_tmg(state))
3231 lock = stv090x_sw_algo(state);
3232 }
3233 }
3234
3235 if (lock)
3236 signal_state = stv090x_get_sig_params(state);
3237
3238 if ((lock) && (signal_state == STV090x_RANGEOK)) { /* signal within Range */
3239 stv090x_optimize_track(state);
Manu Abraham27d40322009-05-02 18:26:58 -03003240
3241 if (state->dev_ver >= 0x20) {
3242 /* >= Cut 2.0 :release TS reset after
3243 * demod lock and optimized Tracking
3244 */
Manu Abrahame415c682009-04-06 15:45:20 -03003245 reg = STV090x_READ_DEMOD(state, TSCFGH);
3246 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
3247 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3248 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003249
Manu Abrahame415c682009-04-06 15:45:20 -03003250 msleep(3);
Manu Abraham27d40322009-05-02 18:26:58 -03003251
Manu Abrahame415c682009-04-06 15:45:20 -03003252 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
3253 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3254 goto err;
3255
3256 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
3257 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3258 goto err;
3259 }
3260
Andreas Regela4978a82009-11-13 18:17:45 -03003261 lock = stv090x_get_lock(state, state->FecTimeout,
3262 state->FecTimeout);
3263 if (lock) {
Manu Abrahame415c682009-04-06 15:45:20 -03003264 if (state->delsys == STV090x_DVBS2) {
3265 stv090x_set_s2rolloff(state);
Manu Abraham27d40322009-05-02 18:26:58 -03003266
3267 reg = STV090x_READ_DEMOD(state, PDELCTRL2);
3268 STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 1);
3269 if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003270 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003271 /* Reset DVBS2 packet delinator error counter */
3272 reg = STV090x_READ_DEMOD(state, PDELCTRL2);
3273 STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 0);
3274 if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003275 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003276
Manu Abrahame415c682009-04-06 15:45:20 -03003277 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67) < 0) /* PER */
3278 goto err;
3279 } else {
3280 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
3281 goto err;
3282 }
Manu Abraham27d40322009-05-02 18:26:58 -03003283 /* Reset the Total packet counter */
Manu Abrahame415c682009-04-06 15:45:20 -03003284 if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0x00) < 0)
3285 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003286 /* Reset the packet Error counter2 */
Manu Abrahame415c682009-04-06 15:45:20 -03003287 if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3288 goto err;
3289 } else {
Manu Abrahame415c682009-04-06 15:45:20 -03003290 signal_state = STV090x_NODATA;
3291 no_signal = stv090x_chk_signal(state);
3292 }
3293 }
Manu Abrahame415c682009-04-06 15:45:20 -03003294 return signal_state;
3295
3296err:
3297 dprintk(FE_ERROR, 1, "I/O error");
3298 return -1;
3299}
3300
3301static enum dvbfe_search stv090x_search(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
3302{
3303 struct stv090x_state *state = fe->demodulator_priv;
3304 struct dtv_frontend_properties *props = &fe->dtv_property_cache;
3305
3306 state->delsys = props->delivery_system;
3307 state->frequency = p->frequency;
3308 state->srate = p->u.qpsk.symbol_rate;
Andreas Regel4e58a682009-04-16 08:40:36 -03003309 state->search_mode = STV090x_SEARCH_AUTO;
3310 state->algo = STV090x_COLD_SEARCH;
3311 state->fec = STV090x_PRERR;
Andreas Regelc879d8c2009-11-13 18:11:26 -03003312 if (state->srate > 10000000) {
3313 dprintk(FE_DEBUG, 1, "Search range: 10 MHz");
3314 state->search_range = 10000000;
3315 } else {
3316 dprintk(FE_DEBUG, 1, "Search range: 5 MHz");
3317 state->search_range = 5000000;
3318 }
Manu Abrahame415c682009-04-06 15:45:20 -03003319
Andreas Regel2f5914b2009-04-23 14:56:41 -03003320 if (stv090x_algo(state) == STV090x_RANGEOK) {
Manu Abrahame415c682009-04-06 15:45:20 -03003321 dprintk(FE_DEBUG, 1, "Search success!");
3322 return DVBFE_ALGO_SEARCH_SUCCESS;
3323 } else {
3324 dprintk(FE_DEBUG, 1, "Search failed!");
3325 return DVBFE_ALGO_SEARCH_FAILED;
3326 }
3327
3328 return DVBFE_ALGO_SEARCH_ERROR;
3329}
3330
Manu Abrahame415c682009-04-06 15:45:20 -03003331static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
3332{
3333 struct stv090x_state *state = fe->demodulator_priv;
3334 u32 reg;
3335 u8 search_state;
Manu Abrahame415c682009-04-06 15:45:20 -03003336
3337 reg = STV090x_READ_DEMOD(state, DMDSTATE);
3338 search_state = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
3339
3340 switch (search_state) {
3341 case 0: /* searching */
3342 case 1: /* first PLH detected */
3343 default:
3344 dprintk(FE_DEBUG, 1, "Status: Unlocked (Searching ..)");
Andreas Regel9629c5b2009-04-23 14:58:36 -03003345 *status = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003346 break;
3347
3348 case 2: /* DVB-S2 mode */
3349 dprintk(FE_DEBUG, 1, "Delivery system: DVB-S2");
3350 reg = STV090x_READ_DEMOD(state, DSTATUS);
3351 if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
Andreas Regel1d436172009-11-13 18:19:54 -03003352 reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
3353 if (STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD)) {
3354 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3355 if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
3356 *status = FE_HAS_CARRIER |
3357 FE_HAS_VITERBI |
3358 FE_HAS_SYNC |
3359 FE_HAS_LOCK;
3360 }
Manu Abrahame415c682009-04-06 15:45:20 -03003361 }
3362 }
3363 break;
3364
3365 case 3: /* DVB-S1/legacy mode */
3366 dprintk(FE_DEBUG, 1, "Delivery system: DVB-S");
3367 reg = STV090x_READ_DEMOD(state, DSTATUS);
3368 if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
3369 reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
3370 if (STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD)) {
3371 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3372 if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
Manu Abrahame415c682009-04-06 15:45:20 -03003373 *status = FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
3374 }
3375 }
3376 }
3377 break;
3378 }
3379
Andreas Regel9629c5b2009-04-23 14:58:36 -03003380 return 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003381}
3382
3383static int stv090x_read_per(struct dvb_frontend *fe, u32 *per)
3384{
3385 struct stv090x_state *state = fe->demodulator_priv;
3386
3387 s32 count_4, count_3, count_2, count_1, count_0, count;
3388 u32 reg, h, m, l;
3389 enum fe_status status;
3390
Andreas Regel9629c5b2009-04-23 14:58:36 -03003391 stv090x_read_status(fe, &status);
3392 if (!(status & FE_HAS_LOCK)) {
Manu Abrahame415c682009-04-06 15:45:20 -03003393 *per = 1 << 23; /* Max PER */
3394 } else {
3395 /* Counter 2 */
3396 reg = STV090x_READ_DEMOD(state, ERRCNT22);
3397 h = STV090x_GETFIELD_Px(reg, ERR_CNT2_FIELD);
3398
3399 reg = STV090x_READ_DEMOD(state, ERRCNT21);
3400 m = STV090x_GETFIELD_Px(reg, ERR_CNT21_FIELD);
3401
3402 reg = STV090x_READ_DEMOD(state, ERRCNT20);
3403 l = STV090x_GETFIELD_Px(reg, ERR_CNT20_FIELD);
3404
3405 *per = ((h << 16) | (m << 8) | l);
3406
3407 count_4 = STV090x_READ_DEMOD(state, FBERCPT4);
3408 count_3 = STV090x_READ_DEMOD(state, FBERCPT3);
3409 count_2 = STV090x_READ_DEMOD(state, FBERCPT2);
3410 count_1 = STV090x_READ_DEMOD(state, FBERCPT1);
3411 count_0 = STV090x_READ_DEMOD(state, FBERCPT0);
3412
3413 if ((!count_4) && (!count_3)) {
3414 count = (count_2 & 0xff) << 16;
3415 count |= (count_1 & 0xff) << 8;
3416 count |= count_0 & 0xff;
3417 } else {
3418 count = 1 << 24;
3419 }
3420 if (count == 0)
3421 *per = 1;
3422 }
3423 if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0) < 0)
3424 goto err;
3425 if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3426 goto err;
3427
3428 return 0;
3429err:
3430 dprintk(FE_ERROR, 1, "I/O error");
3431 return -1;
3432}
3433
3434static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
3435{
3436 int res = 0;
3437 int min = 0, med;
3438
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003439 if ((val >= tab[min].read && val < tab[max].read) ||
3440 (val >= tab[max].read && val < tab[min].read)) {
Manu Abrahame415c682009-04-06 15:45:20 -03003441 while ((max - min) > 1) {
3442 med = (max + min) / 2;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003443 if ((val >= tab[min].read && val < tab[med].read) ||
3444 (val >= tab[med].read && val < tab[min].read))
Manu Abrahame415c682009-04-06 15:45:20 -03003445 max = med;
3446 else
3447 min = med;
3448 }
3449 res = ((val - tab[min].read) *
3450 (tab[max].real - tab[min].real) /
3451 (tab[max].read - tab[min].read)) +
3452 tab[min].real;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003453 } else {
3454 if (tab[min].read < tab[max].read) {
3455 if (val < tab[min].read)
3456 res = tab[min].real;
3457 else if (val >= tab[max].read)
3458 res = tab[max].real;
3459 } else {
3460 if (val >= tab[min].read)
3461 res = tab[min].real;
3462 else if (val < tab[max].read)
3463 res = tab[max].real;
3464 }
Manu Abrahame415c682009-04-06 15:45:20 -03003465 }
3466
3467 return res;
3468}
3469
3470static int stv090x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
3471{
3472 struct stv090x_state *state = fe->demodulator_priv;
3473 u32 reg;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003474 s32 agc_0, agc_1, agc;
3475 s32 str;
Manu Abrahame415c682009-04-06 15:45:20 -03003476
3477 reg = STV090x_READ_DEMOD(state, AGCIQIN1);
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003478 agc_1 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
3479 reg = STV090x_READ_DEMOD(state, AGCIQIN0);
3480 agc_0 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
3481 agc = MAKEWORD16(agc_1, agc_0);
Manu Abrahame415c682009-04-06 15:45:20 -03003482
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003483 str = stv090x_table_lookup(stv090x_rf_tab,
3484 ARRAY_SIZE(stv090x_rf_tab) - 1, agc);
Manu Abrahame415c682009-04-06 15:45:20 -03003485 if (agc > stv090x_rf_tab[0].read)
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003486 str = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003487 else if (agc < stv090x_rf_tab[ARRAY_SIZE(stv090x_rf_tab) - 1].read)
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003488 str = -100;
3489 *strength = (str + 100) * 0xFFFF / 100;
Manu Abrahame415c682009-04-06 15:45:20 -03003490
3491 return 0;
3492}
3493
3494static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
3495{
3496 struct stv090x_state *state = fe->demodulator_priv;
3497 u32 reg_0, reg_1, reg, i;
3498 s32 val_0, val_1, val = 0;
3499 u8 lock_f;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003500 s32 div;
3501 u32 last;
Manu Abrahame415c682009-04-06 15:45:20 -03003502
3503 switch (state->delsys) {
3504 case STV090x_DVBS2:
3505 reg = STV090x_READ_DEMOD(state, DSTATUS);
3506 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
3507 if (lock_f) {
3508 msleep(5);
3509 for (i = 0; i < 16; i++) {
3510 reg_1 = STV090x_READ_DEMOD(state, NNOSPLHT1);
3511 val_1 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
3512 reg_0 = STV090x_READ_DEMOD(state, NNOSPLHT0);
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003513 val_0 = STV090x_GETFIELD_Px(reg_0, NOSPLHT_NORMED_FIELD);
Manu Abrahame415c682009-04-06 15:45:20 -03003514 val += MAKEWORD16(val_1, val_0);
3515 msleep(1);
3516 }
3517 val /= 16;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003518 last = ARRAY_SIZE(stv090x_s2cn_tab) - 1;
3519 div = stv090x_s2cn_tab[0].read -
3520 stv090x_s2cn_tab[last].read;
3521 *cnr = 0xFFFF - ((val * 0xFFFF) / div);
Manu Abrahame415c682009-04-06 15:45:20 -03003522 }
3523 break;
3524
3525 case STV090x_DVBS1:
3526 case STV090x_DSS:
3527 reg = STV090x_READ_DEMOD(state, DSTATUS);
3528 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
3529 if (lock_f) {
3530 msleep(5);
3531 for (i = 0; i < 16; i++) {
3532 reg_1 = STV090x_READ_DEMOD(state, NOSDATAT1);
3533 val_1 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
3534 reg_0 = STV090x_READ_DEMOD(state, NOSDATAT0);
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003535 val_0 = STV090x_GETFIELD_Px(reg_0, NOSDATAT_UNNORMED_FIELD);
Manu Abrahame415c682009-04-06 15:45:20 -03003536 val += MAKEWORD16(val_1, val_0);
3537 msleep(1);
3538 }
3539 val /= 16;
Andreas Regeldbeb7db2009-11-13 18:14:00 -03003540 last = ARRAY_SIZE(stv090x_s1cn_tab) - 1;
3541 div = stv090x_s1cn_tab[0].read -
3542 stv090x_s1cn_tab[last].read;
3543 *cnr = 0xFFFF - ((val * 0xFFFF) / div);
Manu Abrahame415c682009-04-06 15:45:20 -03003544 }
3545 break;
3546 default:
3547 break;
3548 }
3549
3550 return 0;
3551}
3552
3553static int stv090x_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
3554{
3555 struct stv090x_state *state = fe->demodulator_priv;
3556 u32 reg;
3557
3558 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3559 switch (tone) {
3560 case SEC_TONE_ON:
3561 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
3562 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3563 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3564 goto err;
3565 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3566 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3567 goto err;
3568 break;
3569
3570 case SEC_TONE_OFF:
3571 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
3572 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3573 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3574 goto err;
3575 break;
3576 default:
3577 return -EINVAL;
3578 }
3579
3580 return 0;
3581err:
3582 dprintk(FE_ERROR, 1, "I/O error");
3583 return -1;
3584}
3585
3586
3587static enum dvbfe_algo stv090x_frontend_algo(struct dvb_frontend *fe)
3588{
3589 return DVBFE_ALGO_CUSTOM;
3590}
3591
3592static int stv090x_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
3593{
3594 struct stv090x_state *state = fe->demodulator_priv;
3595 u32 reg, idle = 0, fifo_full = 1;
3596 int i;
3597
3598 reg = STV090x_READ_DEMOD(state, DISTXCTL);
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003599
Oliver Endriss30e8ca22009-12-13 09:02:39 -03003600 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD,
3601 (state->config->diseqc_envelope_mode) ? 4 : 2);
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003602 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3603 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3604 goto err;
3605 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3606 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3607 goto err;
3608
Manu Abrahame415c682009-04-06 15:45:20 -03003609 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3610 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3611 goto err;
3612
3613 for (i = 0; i < cmd->msg_len; i++) {
3614
3615 while (fifo_full) {
3616 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3617 fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
3618 }
3619
3620 if (STV090x_WRITE_DEMOD(state, DISTXDATA, cmd->msg[i]) < 0)
3621 goto err;
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003622 }
3623 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3624 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
3625 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3626 goto err;
3627
3628 i = 0;
3629
3630 while ((!idle) && (i < 10)) {
3631 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3632 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3633 msleep(10);
Manu Abrahame415c682009-04-06 15:45:20 -03003634 i++;
3635 }
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003636
3637 return 0;
3638err:
3639 dprintk(FE_ERROR, 1, "I/O error");
3640 return -1;
3641}
3642
3643static int stv090x_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
3644{
3645 struct stv090x_state *state = fe->demodulator_priv;
3646 u32 reg, idle = 0, fifo_full = 1;
3647 u8 mode, value;
3648 int i;
3649
3650 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3651
3652 if (burst == SEC_MINI_A) {
Oliver Endriss30e8ca22009-12-13 09:02:39 -03003653 mode = (state->config->diseqc_envelope_mode) ? 5 : 3;
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003654 value = 0x00;
3655 } else {
Oliver Endriss30e8ca22009-12-13 09:02:39 -03003656 mode = (state->config->diseqc_envelope_mode) ? 4 : 2;
Andreas Regelf9ed95d2009-04-08 17:27:51 -03003657 value = 0xFF;
3658 }
3659
3660 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, mode);
3661 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3662 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3663 goto err;
3664 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3665 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3666 goto err;
3667
3668 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3669 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3670 goto err;
3671
3672 while (fifo_full) {
3673 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3674 fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
3675 }
3676
3677 if (STV090x_WRITE_DEMOD(state, DISTXDATA, value) < 0)
3678 goto err;
3679
Manu Abrahame415c682009-04-06 15:45:20 -03003680 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3681 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
3682 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3683 goto err;
3684
3685 i = 0;
3686
3687 while ((!idle) && (i < 10)) {
3688 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3689 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3690 msleep(10);
3691 i++;
3692 }
3693
3694 return 0;
3695err:
3696 dprintk(FE_ERROR, 1, "I/O error");
3697 return -1;
3698}
3699
3700static int stv090x_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
3701{
3702 struct stv090x_state *state = fe->demodulator_priv;
3703 u32 reg = 0, i = 0, rx_end = 0;
3704
3705 while ((rx_end != 1) && (i < 10)) {
3706 msleep(10);
3707 i++;
3708 reg = STV090x_READ_DEMOD(state, DISRX_ST0);
3709 rx_end = STV090x_GETFIELD_Px(reg, RX_END_FIELD);
3710 }
3711
3712 if (rx_end) {
3713 reply->msg_len = STV090x_GETFIELD_Px(reg, FIFO_BYTENBR_FIELD);
3714 for (i = 0; i < reply->msg_len; i++)
3715 reply->msg[i] = STV090x_READ_DEMOD(state, DISRXDATA);
3716 }
3717
3718 return 0;
3719}
3720
3721static int stv090x_sleep(struct dvb_frontend *fe)
3722{
3723 struct stv090x_state *state = fe->demodulator_priv;
3724 u32 reg;
3725
3726 dprintk(FE_DEBUG, 1, "Set %s to sleep",
3727 state->device == STV0900 ? "STV0900" : "STV0903");
3728
3729 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3730 STV090x_SETFIELD(reg, STANDBY_FIELD, 0x01);
3731 if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
3732 goto err;
3733
Manu Abraham26b03bc2009-04-08 18:27:10 -03003734 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
3735 STV090x_SETFIELD(reg, ADC1_PON_FIELD, 0);
3736 if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
3737 goto err;
3738
Manu Abrahame415c682009-04-06 15:45:20 -03003739 return 0;
3740err:
3741 dprintk(FE_ERROR, 1, "I/O error");
3742 return -1;
3743}
3744
3745static int stv090x_wakeup(struct dvb_frontend *fe)
3746{
3747 struct stv090x_state *state = fe->demodulator_priv;
3748 u32 reg;
3749
3750 dprintk(FE_DEBUG, 1, "Wake %s from standby",
3751 state->device == STV0900 ? "STV0900" : "STV0903");
3752
3753 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3754 STV090x_SETFIELD(reg, STANDBY_FIELD, 0x00);
3755 if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
3756 goto err;
3757
Manu Abraham26b03bc2009-04-08 18:27:10 -03003758 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
3759 STV090x_SETFIELD(reg, ADC1_PON_FIELD, 1);
3760 if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
3761 goto err;
3762
Manu Abrahame415c682009-04-06 15:45:20 -03003763 return 0;
3764err:
3765 dprintk(FE_ERROR, 1, "I/O error");
3766 return -1;
3767}
3768
3769static void stv090x_release(struct dvb_frontend *fe)
3770{
3771 struct stv090x_state *state = fe->demodulator_priv;
3772
3773 kfree(state);
3774}
3775
3776static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc_mode)
3777{
Manu Abraham27d40322009-05-02 18:26:58 -03003778 u32 reg = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03003779
Andreas Regela4978a82009-11-13 18:17:45 -03003780 reg = stv090x_read_reg(state, STV090x_GENCFG);
3781
Manu Abrahame415c682009-04-06 15:45:20 -03003782 switch (ldpc_mode) {
3783 case STV090x_DUAL:
3784 default:
Manu Abrahame415c682009-04-06 15:45:20 -03003785 if ((state->demod_mode != STV090x_DUAL) || (STV090x_GETFIELD(reg, DDEMOD_FIELD) != 1)) {
Manu Abraham27d40322009-05-02 18:26:58 -03003786 /* set LDPC to dual mode */
3787 if (stv090x_write_reg(state, STV090x_GENCFG, 0x1d) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03003788 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003789
Manu Abrahame415c682009-04-06 15:45:20 -03003790 state->demod_mode = STV090x_DUAL;
Manu Abraham27d40322009-05-02 18:26:58 -03003791
Manu Abrahame415c682009-04-06 15:45:20 -03003792 reg = stv090x_read_reg(state, STV090x_TSTRES0);
3793 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
3794 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3795 goto err;
3796 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
3797 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3798 goto err;
Manu Abraham27d40322009-05-02 18:26:58 -03003799
3800 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
3801 goto err;
3802 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
3803 goto err;
3804 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
3805 goto err;
3806 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
3807 goto err;
3808 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
3809 goto err;
3810 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
3811 goto err;
3812 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
3813 goto err;
3814
3815 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
3816 goto err;
3817 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
3818 goto err;
3819 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
3820 goto err;
3821 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
3822 goto err;
3823 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
3824 goto err;
3825 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
3826 goto err;
3827 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
3828 goto err;
3829
3830 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
3831 goto err;
3832 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
3833 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03003834 }
3835 break;
3836
3837 case STV090x_SINGLE:
Manu Abraham27d40322009-05-02 18:26:58 -03003838 if (stv090x_stop_modcod(state) < 0)
3839 goto err;
3840 if (stv090x_activate_modcod_single(state) < 0)
3841 goto err;
3842
Manu Abrahame415c682009-04-06 15:45:20 -03003843 if (state->demod == STV090x_DEMODULATOR_1) {
3844 if (stv090x_write_reg(state, STV090x_GENCFG, 0x06) < 0) /* path 2 */
3845 goto err;
3846 } else {
3847 if (stv090x_write_reg(state, STV090x_GENCFG, 0x04) < 0) /* path 1 */
3848 goto err;
3849 }
3850
3851 reg = stv090x_read_reg(state, STV090x_TSTRES0);
3852 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
3853 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3854 goto err;
3855 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
3856 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
3857 goto err;
3858
3859 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
3860 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x01);
3861 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
3862 goto err;
3863 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x00);
3864 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
3865 goto err;
3866 break;
3867 }
3868
3869 return 0;
3870err:
3871 dprintk(FE_ERROR, 1, "I/O error");
3872 return -1;
3873}
3874
3875/* return (Hz), clk in Hz*/
3876static u32 stv090x_get_mclk(struct stv090x_state *state)
3877{
3878 const struct stv090x_config *config = state->config;
3879 u32 div, reg;
3880 u8 ratio;
3881
3882 div = stv090x_read_reg(state, STV090x_NCOARSE);
3883 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3884 ratio = STV090x_GETFIELD(reg, SELX1RATIO_FIELD) ? 4 : 6;
3885
3886 return (div + 1) * config->xtal / ratio; /* kHz */
3887}
3888
3889static int stv090x_set_mclk(struct stv090x_state *state, u32 mclk, u32 clk)
3890{
3891 const struct stv090x_config *config = state->config;
3892 u32 reg, div, clk_sel;
3893
3894 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
3895 clk_sel = ((STV090x_GETFIELD(reg, SELX1RATIO_FIELD) == 1) ? 4 : 6);
3896
3897 div = ((clk_sel * mclk) / config->xtal) - 1;
3898
3899 reg = stv090x_read_reg(state, STV090x_NCOARSE);
3900 STV090x_SETFIELD(reg, M_DIV_FIELD, div);
3901 if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0)
3902 goto err;
3903
3904 state->mclk = stv090x_get_mclk(state);
3905
Manu Abraham94a80912009-04-08 19:45:43 -03003906 /*Set the DiseqC frequency to 22KHz */
3907 div = state->mclk / 704000;
3908 if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0)
3909 goto err;
3910 if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
3911 goto err;
3912
Manu Abrahame415c682009-04-06 15:45:20 -03003913 return 0;
3914err:
3915 dprintk(FE_ERROR, 1, "I/O error");
3916 return -1;
3917}
3918
3919static int stv090x_set_tspath(struct stv090x_state *state)
3920{
3921 u32 reg;
3922
3923 if (state->dev_ver >= 0x20) {
3924 switch (state->config->ts1_mode) {
3925 case STV090x_TSMODE_PARALLEL_PUNCTURED:
3926 case STV090x_TSMODE_DVBCI:
3927 switch (state->config->ts2_mode) {
3928 case STV090x_TSMODE_SERIAL_PUNCTURED:
3929 case STV090x_TSMODE_SERIAL_CONTINUOUS:
3930 default:
3931 stv090x_write_reg(state, STV090x_TSGENERAL, 0x00);
3932 break;
3933
3934 case STV090x_TSMODE_PARALLEL_PUNCTURED:
3935 case STV090x_TSMODE_DVBCI:
3936 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x06) < 0) /* Mux'd stream mode */
3937 goto err;
3938 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
3939 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
3940 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
3941 goto err;
3942 reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
3943 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
3944 if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
3945 goto err;
3946 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
3947 goto err;
3948 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
3949 goto err;
3950 break;
3951 }
3952 break;
3953
3954 case STV090x_TSMODE_SERIAL_PUNCTURED:
3955 case STV090x_TSMODE_SERIAL_CONTINUOUS:
3956 default:
3957 switch (state->config->ts2_mode) {
3958 case STV090x_TSMODE_SERIAL_PUNCTURED:
3959 case STV090x_TSMODE_SERIAL_CONTINUOUS:
3960 default:
3961 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
3962 goto err;
3963 break;
3964
3965 case STV090x_TSMODE_PARALLEL_PUNCTURED:
3966 case STV090x_TSMODE_DVBCI:
3967 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0a) < 0)
3968 goto err;
3969 break;
3970 }
3971 break;
3972 }
3973 } else {
3974 switch (state->config->ts1_mode) {
3975 case STV090x_TSMODE_PARALLEL_PUNCTURED:
3976 case STV090x_TSMODE_DVBCI:
3977 switch (state->config->ts2_mode) {
3978 case STV090x_TSMODE_SERIAL_PUNCTURED:
3979 case STV090x_TSMODE_SERIAL_CONTINUOUS:
3980 default:
Manu Abraham56571502009-04-07 16:08:26 -03003981 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x10);
Manu Abrahame415c682009-04-06 15:45:20 -03003982 break;
3983
3984 case STV090x_TSMODE_PARALLEL_PUNCTURED:
3985 case STV090x_TSMODE_DVBCI:
Manu Abraham56571502009-04-07 16:08:26 -03003986 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x16);
Manu Abrahame415c682009-04-06 15:45:20 -03003987 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
3988 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
3989 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
3990 goto err;
3991 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
3992 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 0);
3993 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
3994 goto err;
3995 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
3996 goto err;
3997 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
3998 goto err;
3999 break;
4000 }
4001 break;
4002
4003 case STV090x_TSMODE_SERIAL_PUNCTURED:
4004 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4005 default:
4006 switch (state->config->ts2_mode) {
4007 case STV090x_TSMODE_SERIAL_PUNCTURED:
4008 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4009 default:
Manu Abraham56571502009-04-07 16:08:26 -03004010 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
Manu Abrahame415c682009-04-06 15:45:20 -03004011 break;
4012
4013 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4014 case STV090x_TSMODE_DVBCI:
Manu Abraham56571502009-04-07 16:08:26 -03004015 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x12);
Manu Abrahame415c682009-04-06 15:45:20 -03004016 break;
4017 }
4018 break;
4019 }
4020 }
4021
4022 switch (state->config->ts1_mode) {
4023 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4024 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4025 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4026 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4027 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4028 goto err;
4029 break;
4030
4031 case STV090x_TSMODE_DVBCI:
4032 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4033 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4034 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4035 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4036 goto err;
4037 break;
4038
4039 case STV090x_TSMODE_SERIAL_PUNCTURED:
4040 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4041 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4042 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4043 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4044 goto err;
4045 break;
4046
4047 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4048 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4049 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4050 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4051 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4052 goto err;
4053 break;
4054
4055 default:
4056 break;
4057 }
4058
4059 switch (state->config->ts2_mode) {
4060 case STV090x_TSMODE_PARALLEL_PUNCTURED:
Andreas Regel64104dc2009-04-16 08:43:41 -03004061 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004062 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4063 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
Andreas Regel64104dc2009-04-16 08:43:41 -03004064 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004065 goto err;
4066 break;
4067
4068 case STV090x_TSMODE_DVBCI:
Andreas Regel64104dc2009-04-16 08:43:41 -03004069 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004070 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4071 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
Andreas Regel64104dc2009-04-16 08:43:41 -03004072 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004073 goto err;
4074 break;
4075
4076 case STV090x_TSMODE_SERIAL_PUNCTURED:
Andreas Regel64104dc2009-04-16 08:43:41 -03004077 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004078 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4079 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
Andreas Regel64104dc2009-04-16 08:43:41 -03004080 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004081 goto err;
4082 break;
4083
4084 case STV090x_TSMODE_SERIAL_CONTINUOUS:
Andreas Regel64104dc2009-04-16 08:43:41 -03004085 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
Manu Abrahame415c682009-04-06 15:45:20 -03004086 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4087 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
Andreas Regel64104dc2009-04-16 08:43:41 -03004088 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
Manu Abrahame415c682009-04-06 15:45:20 -03004089 goto err;
4090 break;
4091
4092 default:
4093 break;
4094 }
4095 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4096 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
4097 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4098 goto err;
4099 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4100 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4101 goto err;
4102
4103 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4104 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
4105 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4106 goto err;
4107 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4108 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4109 goto err;
4110
4111 return 0;
4112err:
4113 dprintk(FE_ERROR, 1, "I/O error");
4114 return -1;
4115}
4116
4117static int stv090x_init(struct dvb_frontend *fe)
4118{
4119 struct stv090x_state *state = fe->demodulator_priv;
4120 const struct stv090x_config *config = state->config;
4121 u32 reg;
4122
Andreas Regelcbc320d2009-04-23 14:59:03 -03004123 if (stv090x_wakeup(fe) < 0) {
4124 dprintk(FE_ERROR, 1, "Error waking device");
4125 goto err;
4126 }
4127
Manu Abraham27d40322009-05-02 18:26:58 -03004128 if (stv090x_ldpc_mode(state, state->demod_mode) < 0)
4129 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03004130
4131 reg = STV090x_READ_DEMOD(state, TNRCFG2);
4132 STV090x_SETFIELD_Px(reg, TUN_IQSWAP_FIELD, state->inversion);
4133 if (STV090x_WRITE_DEMOD(state, TNRCFG2, reg) < 0)
4134 goto err;
4135 reg = STV090x_READ_DEMOD(state, DEMOD);
4136 STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
4137 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
4138 goto err;
4139
Manu Abraham27d40322009-05-02 18:26:58 -03004140 if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
4141 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03004142
Manu Abraham27d40322009-05-02 18:26:58 -03004143 if (config->tuner_set_mode) {
4144 if (config->tuner_set_mode(fe, TUNER_WAKE) < 0)
4145 goto err;
4146 }
Manu Abrahame415c682009-04-06 15:45:20 -03004147
Manu Abraham27d40322009-05-02 18:26:58 -03004148 if (config->tuner_init) {
4149 if (config->tuner_init(fe) < 0)
4150 goto err;
4151 }
Manu Abrahame415c682009-04-06 15:45:20 -03004152
Manu Abraham27d40322009-05-02 18:26:58 -03004153 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
4154 goto err;
4155
4156 if (stv090x_set_tspath(state) < 0)
4157 goto err;
Manu Abrahame415c682009-04-06 15:45:20 -03004158
4159 return 0;
4160err:
4161 dprintk(FE_ERROR, 1, "I/O error");
4162 return -1;
4163}
4164
4165static int stv090x_setup(struct dvb_frontend *fe)
4166{
4167 struct stv090x_state *state = fe->demodulator_priv;
4168 const struct stv090x_config *config = state->config;
4169 const struct stv090x_reg *stv090x_initval = NULL;
4170 const struct stv090x_reg *stv090x_cut20_val = NULL;
4171 unsigned long t1_size = 0, t2_size = 0;
Manu Abraham017eb0382009-04-07 05:19:54 -03004172 u32 reg = 0;
Manu Abrahame415c682009-04-06 15:45:20 -03004173
4174 int i;
4175
4176 if (state->device == STV0900) {
4177 dprintk(FE_DEBUG, 1, "Initializing STV0900");
4178 stv090x_initval = stv0900_initval;
4179 t1_size = ARRAY_SIZE(stv0900_initval);
4180 stv090x_cut20_val = stv0900_cut20_val;
4181 t2_size = ARRAY_SIZE(stv0900_cut20_val);
4182 } else if (state->device == STV0903) {
4183 dprintk(FE_DEBUG, 1, "Initializing STV0903");
4184 stv090x_initval = stv0903_initval;
4185 t1_size = ARRAY_SIZE(stv0903_initval);
4186 stv090x_cut20_val = stv0903_cut20_val;
4187 t2_size = ARRAY_SIZE(stv0903_cut20_val);
4188 }
4189
4190 /* STV090x init */
4191 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Stop Demod */
4192 goto err;
4193
4194 msleep(5);
4195
4196 if (STV090x_WRITE_DEMOD(state, TNRCFG, 0x6c) < 0) /* check register ! (No Tuner Mode) */
4197 goto err;
4198
Manu Abraham017eb0382009-04-07 05:19:54 -03004199 STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level);
4200 if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0) /* repeater OFF */
Manu Abrahame415c682009-04-06 15:45:20 -03004201 goto err;
4202
4203 if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
4204 goto err;
4205 msleep(5);
4206 if (stv090x_write_reg(state, STV090x_I2CCFG, 0x08) < 0) /* 1/41 oversampling */
4207 goto err;
4208 if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0) /* enable PLL */
4209 goto err;
4210 msleep(5);
4211
4212 /* write initval */
Andreas Regel2f5914b2009-04-23 14:56:41 -03004213 dprintk(FE_DEBUG, 1, "Setting up initial values");
Manu Abrahame415c682009-04-06 15:45:20 -03004214 for (i = 0; i < t1_size; i++) {
Manu Abrahame415c682009-04-06 15:45:20 -03004215 if (stv090x_write_reg(state, stv090x_initval[i].addr, stv090x_initval[i].data) < 0)
4216 goto err;
4217 }
4218
Andreas Regel2f5914b2009-04-23 14:56:41 -03004219 state->dev_ver = stv090x_read_reg(state, STV090x_MID);
Manu Abrahame415c682009-04-06 15:45:20 -03004220 if (state->dev_ver >= 0x20) {
4221 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
4222 goto err;
4223
4224 /* write cut20_val*/
4225 dprintk(FE_DEBUG, 1, "Setting up Cut 2.0 initial values");
4226 for (i = 0; i < t2_size; i++) {
4227 if (stv090x_write_reg(state, stv090x_cut20_val[i].addr, stv090x_cut20_val[i].data) < 0)
4228 goto err;
4229 }
Manu Abraham27d40322009-05-02 18:26:58 -03004230
4231 } else if (state->dev_ver < 0x20) {
4232 dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!",
4233 state->dev_ver);
4234
4235 goto err;
4236 } else if (state->dev_ver > 0x30) {
4237 /* we shouldn't bail out from here */
4238 dprintk(FE_ERROR, 1, "INFO: Cut: 0x%02x probably incomplete support!",
4239 state->dev_ver);
Manu Abrahame415c682009-04-06 15:45:20 -03004240 }
4241
4242 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
4243 goto err;
4244 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x00) < 0)
4245 goto err;
4246
4247 stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
4248 msleep(5);
4249 if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0)
4250 goto err;
4251 stv090x_get_mclk(state);
4252
4253 return 0;
4254err:
4255 dprintk(FE_ERROR, 1, "I/O error");
4256 return -1;
4257}
4258
4259static struct dvb_frontend_ops stv090x_ops = {
4260
4261 .info = {
4262 .name = "STV090x Multistandard",
Andreas Regel7fc08bb2009-04-23 15:00:40 -03004263 .type = FE_QPSK,
4264 .frequency_min = 950000,
4265 .frequency_max = 2150000,
4266 .frequency_stepsize = 0,
4267 .frequency_tolerance = 0,
4268 .symbol_rate_min = 1000000,
4269 .symbol_rate_max = 45000000,
4270 .caps = FE_CAN_INVERSION_AUTO |
4271 FE_CAN_FEC_AUTO |
4272 FE_CAN_QPSK |
4273 FE_CAN_2G_MODULATION
Manu Abrahame415c682009-04-06 15:45:20 -03004274 },
4275
4276 .release = stv090x_release,
4277 .init = stv090x_init,
4278
4279 .sleep = stv090x_sleep,
4280 .get_frontend_algo = stv090x_frontend_algo,
4281
4282 .i2c_gate_ctrl = stv090x_i2c_gate_ctrl,
4283
4284 .diseqc_send_master_cmd = stv090x_send_diseqc_msg,
Andreas Regelf9ed95d2009-04-08 17:27:51 -03004285 .diseqc_send_burst = stv090x_send_diseqc_burst,
Manu Abrahame415c682009-04-06 15:45:20 -03004286 .diseqc_recv_slave_reply = stv090x_recv_slave_reply,
4287 .set_tone = stv090x_set_tone,
4288
4289 .search = stv090x_search,
4290 .read_status = stv090x_read_status,
4291 .read_ber = stv090x_read_per,
4292 .read_signal_strength = stv090x_read_signal_strength,
4293 .read_snr = stv090x_read_cnr
4294};
4295
4296
4297struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
4298 struct i2c_adapter *i2c,
4299 enum stv090x_demodulator demod)
4300{
4301 struct stv090x_state *state = NULL;
4302
4303 state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL);
4304 if (state == NULL)
4305 goto error;
4306
4307 state->verbose = &verbose;
4308 state->config = config;
4309 state->i2c = i2c;
4310 state->frontend.ops = stv090x_ops;
4311 state->frontend.demodulator_priv = state;
Manu Abraham56571502009-04-07 16:08:26 -03004312 state->demod = demod;
Manu Abrahame415c682009-04-06 15:45:20 -03004313 state->demod_mode = config->demod_mode; /* Single or Dual mode */
4314 state->device = config->device;
Andreas Regel4e58a682009-04-16 08:40:36 -03004315 state->rolloff = STV090x_RO_35; /* default */
Manu Abrahame415c682009-04-06 15:45:20 -03004316
4317 if (state->demod == STV090x_DEMODULATOR_0)
4318 mutex_init(&demod_lock);
4319
4320 if (stv090x_sleep(&state->frontend) < 0) {
4321 dprintk(FE_ERROR, 1, "Error putting device to sleep");
4322 goto error;
4323 }
4324
4325 if (stv090x_setup(&state->frontend) < 0) {
4326 dprintk(FE_ERROR, 1, "Error setting up device");
4327 goto error;
4328 }
4329 if (stv090x_wakeup(&state->frontend) < 0) {
4330 dprintk(FE_ERROR, 1, "Error waking device");
4331 goto error;
4332 }
Manu Abrahame415c682009-04-06 15:45:20 -03004333
4334 dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x\n",
4335 state->device == STV0900 ? "STV0900" : "STV0903",
4336 demod,
4337 state->dev_ver);
4338
4339 return &state->frontend;
4340
4341error:
4342 kfree(state);
4343 return NULL;
4344}
4345EXPORT_SYMBOL(stv090x_attach);
4346MODULE_PARM_DESC(verbose, "Set Verbosity level");
4347MODULE_AUTHOR("Manu Abraham");
4348MODULE_DESCRIPTION("STV090x Multi-Std Broadcast frontend");
4349MODULE_LICENSE("GPL");