sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / dvb-frontends / stv090x.c
blob7ef469c0c8668ac5cf76a1268e0aac387c19d04c
1 /*
2 STV0900/0903 Multistandard Broadcast Frontend driver
3 Copyright (C) Manu Abraham <abraham.manu@gmail.com>
5 Copyright (C) ST Microelectronics
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.
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.
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.
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/string.h>
26 #include <linux/slab.h>
27 #include <linux/mutex.h>
29 #include <linux/dvb/frontend.h>
30 #include "dvb_frontend.h"
32 #include "stv6110x.h" /* for demodulator internal modes */
34 #include "stv090x_reg.h"
35 #include "stv090x.h"
36 #include "stv090x_priv.h"
38 /* Max transfer size done by I2C transfer functions */
39 #define MAX_XFER_SIZE 64
41 static unsigned int verbose;
42 module_param(verbose, int, 0644);
44 /* internal params node */
45 struct stv090x_dev {
46 /* pointer for internal params, one for each pair of demods */
47 struct stv090x_internal *internal;
48 struct stv090x_dev *next_dev;
51 /* first internal params */
52 static struct stv090x_dev *stv090x_first_dev;
54 /* find chip by i2c adapter and i2c address */
55 static struct stv090x_dev *find_dev(struct i2c_adapter *i2c_adap,
56 u8 i2c_addr)
58 struct stv090x_dev *temp_dev = stv090x_first_dev;
61 Search of the last stv0900 chip or
62 find it by i2c adapter and i2c address */
63 while ((temp_dev != NULL) &&
64 ((temp_dev->internal->i2c_adap != i2c_adap) ||
65 (temp_dev->internal->i2c_addr != i2c_addr))) {
67 temp_dev = temp_dev->next_dev;
70 return temp_dev;
73 /* deallocating chip */
74 static void remove_dev(struct stv090x_internal *internal)
76 struct stv090x_dev *prev_dev = stv090x_first_dev;
77 struct stv090x_dev *del_dev = find_dev(internal->i2c_adap,
78 internal->i2c_addr);
80 if (del_dev != NULL) {
81 if (del_dev == stv090x_first_dev) {
82 stv090x_first_dev = del_dev->next_dev;
83 } else {
84 while (prev_dev->next_dev != del_dev)
85 prev_dev = prev_dev->next_dev;
87 prev_dev->next_dev = del_dev->next_dev;
90 kfree(del_dev);
94 /* allocating new chip */
95 static struct stv090x_dev *append_internal(struct stv090x_internal *internal)
97 struct stv090x_dev *new_dev;
98 struct stv090x_dev *temp_dev;
100 new_dev = kmalloc(sizeof(struct stv090x_dev), GFP_KERNEL);
101 if (new_dev != NULL) {
102 new_dev->internal = internal;
103 new_dev->next_dev = NULL;
105 /* append to list */
106 if (stv090x_first_dev == NULL) {
107 stv090x_first_dev = new_dev;
108 } else {
109 temp_dev = stv090x_first_dev;
110 while (temp_dev->next_dev != NULL)
111 temp_dev = temp_dev->next_dev;
113 temp_dev->next_dev = new_dev;
117 return new_dev;
121 /* DVBS1 and DSS C/N Lookup table */
122 static const struct stv090x_tab stv090x_s1cn_tab[] = {
123 { 0, 8917 }, /* 0.0dB */
124 { 5, 8801 }, /* 0.5dB */
125 { 10, 8667 }, /* 1.0dB */
126 { 15, 8522 }, /* 1.5dB */
127 { 20, 8355 }, /* 2.0dB */
128 { 25, 8175 }, /* 2.5dB */
129 { 30, 7979 }, /* 3.0dB */
130 { 35, 7763 }, /* 3.5dB */
131 { 40, 7530 }, /* 4.0dB */
132 { 45, 7282 }, /* 4.5dB */
133 { 50, 7026 }, /* 5.0dB */
134 { 55, 6781 }, /* 5.5dB */
135 { 60, 6514 }, /* 6.0dB */
136 { 65, 6241 }, /* 6.5dB */
137 { 70, 5965 }, /* 7.0dB */
138 { 75, 5690 }, /* 7.5dB */
139 { 80, 5424 }, /* 8.0dB */
140 { 85, 5161 }, /* 8.5dB */
141 { 90, 4902 }, /* 9.0dB */
142 { 95, 4654 }, /* 9.5dB */
143 { 100, 4417 }, /* 10.0dB */
144 { 105, 4186 }, /* 10.5dB */
145 { 110, 3968 }, /* 11.0dB */
146 { 115, 3757 }, /* 11.5dB */
147 { 120, 3558 }, /* 12.0dB */
148 { 125, 3366 }, /* 12.5dB */
149 { 130, 3185 }, /* 13.0dB */
150 { 135, 3012 }, /* 13.5dB */
151 { 140, 2850 }, /* 14.0dB */
152 { 145, 2698 }, /* 14.5dB */
153 { 150, 2550 }, /* 15.0dB */
154 { 160, 2283 }, /* 16.0dB */
155 { 170, 2042 }, /* 17.0dB */
156 { 180, 1827 }, /* 18.0dB */
157 { 190, 1636 }, /* 19.0dB */
158 { 200, 1466 }, /* 20.0dB */
159 { 210, 1315 }, /* 21.0dB */
160 { 220, 1181 }, /* 22.0dB */
161 { 230, 1064 }, /* 23.0dB */
162 { 240, 960 }, /* 24.0dB */
163 { 250, 869 }, /* 25.0dB */
164 { 260, 792 }, /* 26.0dB */
165 { 270, 724 }, /* 27.0dB */
166 { 280, 665 }, /* 28.0dB */
167 { 290, 616 }, /* 29.0dB */
168 { 300, 573 }, /* 30.0dB */
169 { 310, 537 }, /* 31.0dB */
170 { 320, 507 }, /* 32.0dB */
171 { 330, 483 }, /* 33.0dB */
172 { 400, 398 }, /* 40.0dB */
173 { 450, 381 }, /* 45.0dB */
174 { 500, 377 } /* 50.0dB */
177 /* DVBS2 C/N Lookup table */
178 static const struct stv090x_tab stv090x_s2cn_tab[] = {
179 { -30, 13348 }, /* -3.0dB */
180 { -20, 12640 }, /* -2d.0B */
181 { -10, 11883 }, /* -1.0dB */
182 { 0, 11101 }, /* -0.0dB */
183 { 5, 10718 }, /* 0.5dB */
184 { 10, 10339 }, /* 1.0dB */
185 { 15, 9947 }, /* 1.5dB */
186 { 20, 9552 }, /* 2.0dB */
187 { 25, 9183 }, /* 2.5dB */
188 { 30, 8799 }, /* 3.0dB */
189 { 35, 8422 }, /* 3.5dB */
190 { 40, 8062 }, /* 4.0dB */
191 { 45, 7707 }, /* 4.5dB */
192 { 50, 7353 }, /* 5.0dB */
193 { 55, 7025 }, /* 5.5dB */
194 { 60, 6684 }, /* 6.0dB */
195 { 65, 6331 }, /* 6.5dB */
196 { 70, 6036 }, /* 7.0dB */
197 { 75, 5727 }, /* 7.5dB */
198 { 80, 5437 }, /* 8.0dB */
199 { 85, 5164 }, /* 8.5dB */
200 { 90, 4902 }, /* 9.0dB */
201 { 95, 4653 }, /* 9.5dB */
202 { 100, 4408 }, /* 10.0dB */
203 { 105, 4187 }, /* 10.5dB */
204 { 110, 3961 }, /* 11.0dB */
205 { 115, 3751 }, /* 11.5dB */
206 { 120, 3558 }, /* 12.0dB */
207 { 125, 3368 }, /* 12.5dB */
208 { 130, 3191 }, /* 13.0dB */
209 { 135, 3017 }, /* 13.5dB */
210 { 140, 2862 }, /* 14.0dB */
211 { 145, 2710 }, /* 14.5dB */
212 { 150, 2565 }, /* 15.0dB */
213 { 160, 2300 }, /* 16.0dB */
214 { 170, 2058 }, /* 17.0dB */
215 { 180, 1849 }, /* 18.0dB */
216 { 190, 1663 }, /* 19.0dB */
217 { 200, 1495 }, /* 20.0dB */
218 { 210, 1349 }, /* 21.0dB */
219 { 220, 1222 }, /* 22.0dB */
220 { 230, 1110 }, /* 23.0dB */
221 { 240, 1011 }, /* 24.0dB */
222 { 250, 925 }, /* 25.0dB */
223 { 260, 853 }, /* 26.0dB */
224 { 270, 789 }, /* 27.0dB */
225 { 280, 734 }, /* 28.0dB */
226 { 290, 690 }, /* 29.0dB */
227 { 300, 650 }, /* 30.0dB */
228 { 310, 619 }, /* 31.0dB */
229 { 320, 593 }, /* 32.0dB */
230 { 330, 571 }, /* 33.0dB */
231 { 400, 498 }, /* 40.0dB */
232 { 450, 484 }, /* 45.0dB */
233 { 500, 481 } /* 50.0dB */
236 /* RF level C/N lookup table */
237 static const struct stv090x_tab stv090x_rf_tab[] = {
238 { -5, 0xcaa1 }, /* -5dBm */
239 { -10, 0xc229 }, /* -10dBm */
240 { -15, 0xbb08 }, /* -15dBm */
241 { -20, 0xb4bc }, /* -20dBm */
242 { -25, 0xad5a }, /* -25dBm */
243 { -30, 0xa298 }, /* -30dBm */
244 { -35, 0x98a8 }, /* -35dBm */
245 { -40, 0x8389 }, /* -40dBm */
246 { -45, 0x59be }, /* -45dBm */
247 { -50, 0x3a14 }, /* -50dBm */
248 { -55, 0x2d11 }, /* -55dBm */
249 { -60, 0x210d }, /* -60dBm */
250 { -65, 0xa14f }, /* -65dBm */
251 { -70, 0x07aa } /* -70dBm */
255 static struct stv090x_reg stv0900_initval[] = {
257 { STV090x_OUTCFG, 0x00 },
258 { STV090x_MODECFG, 0xff },
259 { STV090x_AGCRF1CFG, 0x11 },
260 { STV090x_AGCRF2CFG, 0x13 },
261 { STV090x_TSGENERAL1X, 0x14 },
262 { STV090x_TSTTNR2, 0x21 },
263 { STV090x_TSTTNR4, 0x21 },
264 { STV090x_P2_DISTXCTL, 0x22 },
265 { STV090x_P2_F22TX, 0xc0 },
266 { STV090x_P2_F22RX, 0xc0 },
267 { STV090x_P2_DISRXCTL, 0x00 },
268 { STV090x_P2_DMDCFGMD, 0xF9 },
269 { STV090x_P2_DEMOD, 0x08 },
270 { STV090x_P2_DMDCFG3, 0xc4 },
271 { STV090x_P2_CARFREQ, 0xed },
272 { STV090x_P2_LDT, 0xd0 },
273 { STV090x_P2_LDT2, 0xb8 },
274 { STV090x_P2_TMGCFG, 0xd2 },
275 { STV090x_P2_TMGTHRISE, 0x20 },
276 { STV090x_P1_TMGCFG, 0xd2 },
278 { STV090x_P2_TMGTHFALL, 0x00 },
279 { STV090x_P2_FECSPY, 0x88 },
280 { STV090x_P2_FSPYDATA, 0x3a },
281 { STV090x_P2_FBERCPT4, 0x00 },
282 { STV090x_P2_FSPYBER, 0x10 },
283 { STV090x_P2_ERRCTRL1, 0x35 },
284 { STV090x_P2_ERRCTRL2, 0xc1 },
285 { STV090x_P2_CFRICFG, 0xf8 },
286 { STV090x_P2_NOSCFG, 0x1c },
287 { STV090x_P2_DMDTOM, 0x20 },
288 { STV090x_P2_CORRELMANT, 0x70 },
289 { STV090x_P2_CORRELABS, 0x88 },
290 { STV090x_P2_AGC2O, 0x5b },
291 { STV090x_P2_AGC2REF, 0x38 },
292 { STV090x_P2_CARCFG, 0xe4 },
293 { STV090x_P2_ACLC, 0x1A },
294 { STV090x_P2_BCLC, 0x09 },
295 { STV090x_P2_CARHDR, 0x08 },
296 { STV090x_P2_KREFTMG, 0xc1 },
297 { STV090x_P2_SFRUPRATIO, 0xf0 },
298 { STV090x_P2_SFRLOWRATIO, 0x70 },
299 { STV090x_P2_SFRSTEP, 0x58 },
300 { STV090x_P2_TMGCFG2, 0x01 },
301 { STV090x_P2_CAR2CFG, 0x26 },
302 { STV090x_P2_BCLC2S2Q, 0x86 },
303 { STV090x_P2_BCLC2S28, 0x86 },
304 { STV090x_P2_SMAPCOEF7, 0x77 },
305 { STV090x_P2_SMAPCOEF6, 0x85 },
306 { STV090x_P2_SMAPCOEF5, 0x77 },
307 { STV090x_P2_TSCFGL, 0x20 },
308 { STV090x_P2_DMDCFG2, 0x3b },
309 { STV090x_P2_MODCODLST0, 0xff },
310 { STV090x_P2_MODCODLST1, 0xff },
311 { STV090x_P2_MODCODLST2, 0xff },
312 { STV090x_P2_MODCODLST3, 0xff },
313 { STV090x_P2_MODCODLST4, 0xff },
314 { STV090x_P2_MODCODLST5, 0xff },
315 { STV090x_P2_MODCODLST6, 0xff },
316 { STV090x_P2_MODCODLST7, 0xcc },
317 { STV090x_P2_MODCODLST8, 0xcc },
318 { STV090x_P2_MODCODLST9, 0xcc },
319 { STV090x_P2_MODCODLSTA, 0xcc },
320 { STV090x_P2_MODCODLSTB, 0xcc },
321 { STV090x_P2_MODCODLSTC, 0xcc },
322 { STV090x_P2_MODCODLSTD, 0xcc },
323 { STV090x_P2_MODCODLSTE, 0xcc },
324 { STV090x_P2_MODCODLSTF, 0xcf },
325 { STV090x_P1_DISTXCTL, 0x22 },
326 { STV090x_P1_F22TX, 0xc0 },
327 { STV090x_P1_F22RX, 0xc0 },
328 { STV090x_P1_DISRXCTL, 0x00 },
329 { STV090x_P1_DMDCFGMD, 0xf9 },
330 { STV090x_P1_DEMOD, 0x08 },
331 { STV090x_P1_DMDCFG3, 0xc4 },
332 { STV090x_P1_DMDTOM, 0x20 },
333 { STV090x_P1_CARFREQ, 0xed },
334 { STV090x_P1_LDT, 0xd0 },
335 { STV090x_P1_LDT2, 0xb8 },
336 { STV090x_P1_TMGCFG, 0xd2 },
337 { STV090x_P1_TMGTHRISE, 0x20 },
338 { STV090x_P1_TMGTHFALL, 0x00 },
339 { STV090x_P1_SFRUPRATIO, 0xf0 },
340 { STV090x_P1_SFRLOWRATIO, 0x70 },
341 { STV090x_P1_TSCFGL, 0x20 },
342 { STV090x_P1_FECSPY, 0x88 },
343 { STV090x_P1_FSPYDATA, 0x3a },
344 { STV090x_P1_FBERCPT4, 0x00 },
345 { STV090x_P1_FSPYBER, 0x10 },
346 { STV090x_P1_ERRCTRL1, 0x35 },
347 { STV090x_P1_ERRCTRL2, 0xc1 },
348 { STV090x_P1_CFRICFG, 0xf8 },
349 { STV090x_P1_NOSCFG, 0x1c },
350 { STV090x_P1_CORRELMANT, 0x70 },
351 { STV090x_P1_CORRELABS, 0x88 },
352 { STV090x_P1_AGC2O, 0x5b },
353 { STV090x_P1_AGC2REF, 0x38 },
354 { STV090x_P1_CARCFG, 0xe4 },
355 { STV090x_P1_ACLC, 0x1A },
356 { STV090x_P1_BCLC, 0x09 },
357 { STV090x_P1_CARHDR, 0x08 },
358 { STV090x_P1_KREFTMG, 0xc1 },
359 { STV090x_P1_SFRSTEP, 0x58 },
360 { STV090x_P1_TMGCFG2, 0x01 },
361 { STV090x_P1_CAR2CFG, 0x26 },
362 { STV090x_P1_BCLC2S2Q, 0x86 },
363 { STV090x_P1_BCLC2S28, 0x86 },
364 { STV090x_P1_SMAPCOEF7, 0x77 },
365 { STV090x_P1_SMAPCOEF6, 0x85 },
366 { STV090x_P1_SMAPCOEF5, 0x77 },
367 { STV090x_P1_DMDCFG2, 0x3b },
368 { STV090x_P1_MODCODLST0, 0xff },
369 { STV090x_P1_MODCODLST1, 0xff },
370 { STV090x_P1_MODCODLST2, 0xff },
371 { STV090x_P1_MODCODLST3, 0xff },
372 { STV090x_P1_MODCODLST4, 0xff },
373 { STV090x_P1_MODCODLST5, 0xff },
374 { STV090x_P1_MODCODLST6, 0xff },
375 { STV090x_P1_MODCODLST7, 0xcc },
376 { STV090x_P1_MODCODLST8, 0xcc },
377 { STV090x_P1_MODCODLST9, 0xcc },
378 { STV090x_P1_MODCODLSTA, 0xcc },
379 { STV090x_P1_MODCODLSTB, 0xcc },
380 { STV090x_P1_MODCODLSTC, 0xcc },
381 { STV090x_P1_MODCODLSTD, 0xcc },
382 { STV090x_P1_MODCODLSTE, 0xcc },
383 { STV090x_P1_MODCODLSTF, 0xcf },
384 { STV090x_GENCFG, 0x1d },
385 { STV090x_NBITER_NF4, 0x37 },
386 { STV090x_NBITER_NF5, 0x29 },
387 { STV090x_NBITER_NF6, 0x37 },
388 { STV090x_NBITER_NF7, 0x33 },
389 { STV090x_NBITER_NF8, 0x31 },
390 { STV090x_NBITER_NF9, 0x2f },
391 { STV090x_NBITER_NF10, 0x39 },
392 { STV090x_NBITER_NF11, 0x3a },
393 { STV090x_NBITER_NF12, 0x29 },
394 { STV090x_NBITER_NF13, 0x37 },
395 { STV090x_NBITER_NF14, 0x33 },
396 { STV090x_NBITER_NF15, 0x2f },
397 { STV090x_NBITER_NF16, 0x39 },
398 { STV090x_NBITER_NF17, 0x3a },
399 { STV090x_NBITERNOERR, 0x04 },
400 { STV090x_GAINLLR_NF4, 0x0C },
401 { STV090x_GAINLLR_NF5, 0x0F },
402 { STV090x_GAINLLR_NF6, 0x11 },
403 { STV090x_GAINLLR_NF7, 0x14 },
404 { STV090x_GAINLLR_NF8, 0x17 },
405 { STV090x_GAINLLR_NF9, 0x19 },
406 { STV090x_GAINLLR_NF10, 0x20 },
407 { STV090x_GAINLLR_NF11, 0x21 },
408 { STV090x_GAINLLR_NF12, 0x0D },
409 { STV090x_GAINLLR_NF13, 0x0F },
410 { STV090x_GAINLLR_NF14, 0x13 },
411 { STV090x_GAINLLR_NF15, 0x1A },
412 { STV090x_GAINLLR_NF16, 0x1F },
413 { STV090x_GAINLLR_NF17, 0x21 },
414 { STV090x_RCCFGH, 0x20 },
415 { STV090x_P1_FECM, 0x01 }, /* disable DSS modes */
416 { STV090x_P2_FECM, 0x01 }, /* disable DSS modes */
417 { STV090x_P1_PRVIT, 0x2F }, /* disable PR 6/7 */
418 { STV090x_P2_PRVIT, 0x2F }, /* disable PR 6/7 */
421 static struct stv090x_reg stv0903_initval[] = {
422 { STV090x_OUTCFG, 0x00 },
423 { STV090x_AGCRF1CFG, 0x11 },
424 { STV090x_STOPCLK1, 0x48 },
425 { STV090x_STOPCLK2, 0x14 },
426 { STV090x_TSTTNR1, 0x27 },
427 { STV090x_TSTTNR2, 0x21 },
428 { STV090x_P1_DISTXCTL, 0x22 },
429 { STV090x_P1_F22TX, 0xc0 },
430 { STV090x_P1_F22RX, 0xc0 },
431 { STV090x_P1_DISRXCTL, 0x00 },
432 { STV090x_P1_DMDCFGMD, 0xF9 },
433 { STV090x_P1_DEMOD, 0x08 },
434 { STV090x_P1_DMDCFG3, 0xc4 },
435 { STV090x_P1_CARFREQ, 0xed },
436 { STV090x_P1_TNRCFG2, 0x82 },
437 { STV090x_P1_LDT, 0xd0 },
438 { STV090x_P1_LDT2, 0xb8 },
439 { STV090x_P1_TMGCFG, 0xd2 },
440 { STV090x_P1_TMGTHRISE, 0x20 },
441 { STV090x_P1_TMGTHFALL, 0x00 },
442 { STV090x_P1_SFRUPRATIO, 0xf0 },
443 { STV090x_P1_SFRLOWRATIO, 0x70 },
444 { STV090x_P1_TSCFGL, 0x20 },
445 { STV090x_P1_FECSPY, 0x88 },
446 { STV090x_P1_FSPYDATA, 0x3a },
447 { STV090x_P1_FBERCPT4, 0x00 },
448 { STV090x_P1_FSPYBER, 0x10 },
449 { STV090x_P1_ERRCTRL1, 0x35 },
450 { STV090x_P1_ERRCTRL2, 0xc1 },
451 { STV090x_P1_CFRICFG, 0xf8 },
452 { STV090x_P1_NOSCFG, 0x1c },
453 { STV090x_P1_DMDTOM, 0x20 },
454 { STV090x_P1_CORRELMANT, 0x70 },
455 { STV090x_P1_CORRELABS, 0x88 },
456 { STV090x_P1_AGC2O, 0x5b },
457 { STV090x_P1_AGC2REF, 0x38 },
458 { STV090x_P1_CARCFG, 0xe4 },
459 { STV090x_P1_ACLC, 0x1A },
460 { STV090x_P1_BCLC, 0x09 },
461 { STV090x_P1_CARHDR, 0x08 },
462 { STV090x_P1_KREFTMG, 0xc1 },
463 { STV090x_P1_SFRSTEP, 0x58 },
464 { STV090x_P1_TMGCFG2, 0x01 },
465 { STV090x_P1_CAR2CFG, 0x26 },
466 { STV090x_P1_BCLC2S2Q, 0x86 },
467 { STV090x_P1_BCLC2S28, 0x86 },
468 { STV090x_P1_SMAPCOEF7, 0x77 },
469 { STV090x_P1_SMAPCOEF6, 0x85 },
470 { STV090x_P1_SMAPCOEF5, 0x77 },
471 { STV090x_P1_DMDCFG2, 0x3b },
472 { STV090x_P1_MODCODLST0, 0xff },
473 { STV090x_P1_MODCODLST1, 0xff },
474 { STV090x_P1_MODCODLST2, 0xff },
475 { STV090x_P1_MODCODLST3, 0xff },
476 { STV090x_P1_MODCODLST4, 0xff },
477 { STV090x_P1_MODCODLST5, 0xff },
478 { STV090x_P1_MODCODLST6, 0xff },
479 { STV090x_P1_MODCODLST7, 0xcc },
480 { STV090x_P1_MODCODLST8, 0xcc },
481 { STV090x_P1_MODCODLST9, 0xcc },
482 { STV090x_P1_MODCODLSTA, 0xcc },
483 { STV090x_P1_MODCODLSTB, 0xcc },
484 { STV090x_P1_MODCODLSTC, 0xcc },
485 { STV090x_P1_MODCODLSTD, 0xcc },
486 { STV090x_P1_MODCODLSTE, 0xcc },
487 { STV090x_P1_MODCODLSTF, 0xcf },
488 { STV090x_GENCFG, 0x1c },
489 { STV090x_NBITER_NF4, 0x37 },
490 { STV090x_NBITER_NF5, 0x29 },
491 { STV090x_NBITER_NF6, 0x37 },
492 { STV090x_NBITER_NF7, 0x33 },
493 { STV090x_NBITER_NF8, 0x31 },
494 { STV090x_NBITER_NF9, 0x2f },
495 { STV090x_NBITER_NF10, 0x39 },
496 { STV090x_NBITER_NF11, 0x3a },
497 { STV090x_NBITER_NF12, 0x29 },
498 { STV090x_NBITER_NF13, 0x37 },
499 { STV090x_NBITER_NF14, 0x33 },
500 { STV090x_NBITER_NF15, 0x2f },
501 { STV090x_NBITER_NF16, 0x39 },
502 { STV090x_NBITER_NF17, 0x3a },
503 { STV090x_NBITERNOERR, 0x04 },
504 { STV090x_GAINLLR_NF4, 0x0C },
505 { STV090x_GAINLLR_NF5, 0x0F },
506 { STV090x_GAINLLR_NF6, 0x11 },
507 { STV090x_GAINLLR_NF7, 0x14 },
508 { STV090x_GAINLLR_NF8, 0x17 },
509 { STV090x_GAINLLR_NF9, 0x19 },
510 { STV090x_GAINLLR_NF10, 0x20 },
511 { STV090x_GAINLLR_NF11, 0x21 },
512 { STV090x_GAINLLR_NF12, 0x0D },
513 { STV090x_GAINLLR_NF13, 0x0F },
514 { STV090x_GAINLLR_NF14, 0x13 },
515 { STV090x_GAINLLR_NF15, 0x1A },
516 { STV090x_GAINLLR_NF16, 0x1F },
517 { STV090x_GAINLLR_NF17, 0x21 },
518 { STV090x_RCCFGH, 0x20 },
519 { STV090x_P1_FECM, 0x01 }, /*disable the DSS mode */
520 { STV090x_P1_PRVIT, 0x2f } /*disable puncture rate 6/7*/
523 static struct stv090x_reg stv0900_cut20_val[] = {
525 { STV090x_P2_DMDCFG3, 0xe8 },
526 { STV090x_P2_DMDCFG4, 0x10 },
527 { STV090x_P2_CARFREQ, 0x38 },
528 { STV090x_P2_CARHDR, 0x20 },
529 { STV090x_P2_KREFTMG, 0x5a },
530 { STV090x_P2_SMAPCOEF7, 0x06 },
531 { STV090x_P2_SMAPCOEF6, 0x00 },
532 { STV090x_P2_SMAPCOEF5, 0x04 },
533 { STV090x_P2_NOSCFG, 0x0c },
534 { STV090x_P1_DMDCFG3, 0xe8 },
535 { STV090x_P1_DMDCFG4, 0x10 },
536 { STV090x_P1_CARFREQ, 0x38 },
537 { STV090x_P1_CARHDR, 0x20 },
538 { STV090x_P1_KREFTMG, 0x5a },
539 { STV090x_P1_SMAPCOEF7, 0x06 },
540 { STV090x_P1_SMAPCOEF6, 0x00 },
541 { STV090x_P1_SMAPCOEF5, 0x04 },
542 { STV090x_P1_NOSCFG, 0x0c },
543 { STV090x_GAINLLR_NF4, 0x21 },
544 { STV090x_GAINLLR_NF5, 0x21 },
545 { STV090x_GAINLLR_NF6, 0x20 },
546 { STV090x_GAINLLR_NF7, 0x1F },
547 { STV090x_GAINLLR_NF8, 0x1E },
548 { STV090x_GAINLLR_NF9, 0x1E },
549 { STV090x_GAINLLR_NF10, 0x1D },
550 { STV090x_GAINLLR_NF11, 0x1B },
551 { STV090x_GAINLLR_NF12, 0x20 },
552 { STV090x_GAINLLR_NF13, 0x20 },
553 { STV090x_GAINLLR_NF14, 0x20 },
554 { STV090x_GAINLLR_NF15, 0x20 },
555 { STV090x_GAINLLR_NF16, 0x20 },
556 { STV090x_GAINLLR_NF17, 0x21 },
559 static struct stv090x_reg stv0903_cut20_val[] = {
560 { STV090x_P1_DMDCFG3, 0xe8 },
561 { STV090x_P1_DMDCFG4, 0x10 },
562 { STV090x_P1_CARFREQ, 0x38 },
563 { STV090x_P1_CARHDR, 0x20 },
564 { STV090x_P1_KREFTMG, 0x5a },
565 { STV090x_P1_SMAPCOEF7, 0x06 },
566 { STV090x_P1_SMAPCOEF6, 0x00 },
567 { STV090x_P1_SMAPCOEF5, 0x04 },
568 { STV090x_P1_NOSCFG, 0x0c },
569 { STV090x_GAINLLR_NF4, 0x21 },
570 { STV090x_GAINLLR_NF5, 0x21 },
571 { STV090x_GAINLLR_NF6, 0x20 },
572 { STV090x_GAINLLR_NF7, 0x1F },
573 { STV090x_GAINLLR_NF8, 0x1E },
574 { STV090x_GAINLLR_NF9, 0x1E },
575 { STV090x_GAINLLR_NF10, 0x1D },
576 { STV090x_GAINLLR_NF11, 0x1B },
577 { STV090x_GAINLLR_NF12, 0x20 },
578 { STV090x_GAINLLR_NF13, 0x20 },
579 { STV090x_GAINLLR_NF14, 0x20 },
580 { STV090x_GAINLLR_NF15, 0x20 },
581 { STV090x_GAINLLR_NF16, 0x20 },
582 { STV090x_GAINLLR_NF17, 0x21 }
585 /* Cut 2.0 Long Frame Tracking CR loop */
586 static struct stv090x_long_frame_crloop stv090x_s2_crl_cut20[] = {
587 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
588 { STV090x_QPSK_12, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x1e },
589 { STV090x_QPSK_35, 0x2f, 0x3f, 0x2e, 0x2f, 0x3d, 0x0f, 0x0e, 0x2e, 0x3d, 0x0e },
590 { STV090x_QPSK_23, 0x2f, 0x3f, 0x2e, 0x2f, 0x0e, 0x0f, 0x0e, 0x1e, 0x3d, 0x3d },
591 { STV090x_QPSK_34, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
592 { STV090x_QPSK_45, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
593 { STV090x_QPSK_56, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
594 { STV090x_QPSK_89, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
595 { STV090x_QPSK_910, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
596 { STV090x_8PSK_35, 0x3c, 0x3e, 0x1c, 0x2e, 0x0c, 0x1e, 0x2b, 0x2d, 0x1b, 0x1d },
597 { STV090x_8PSK_23, 0x1d, 0x3e, 0x3c, 0x2e, 0x2c, 0x1e, 0x0c, 0x2d, 0x2b, 0x1d },
598 { STV090x_8PSK_34, 0x0e, 0x3e, 0x3d, 0x2e, 0x0d, 0x1e, 0x2c, 0x2d, 0x0c, 0x1d },
599 { STV090x_8PSK_56, 0x2e, 0x3e, 0x1e, 0x2e, 0x2d, 0x1e, 0x3c, 0x2d, 0x2c, 0x1d },
600 { STV090x_8PSK_89, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x0d, 0x2d, 0x3c, 0x1d },
601 { STV090x_8PSK_910, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x1d, 0x2d, 0x0d, 0x1d }
604 /* Cut 3.0 Long Frame Tracking CR loop */
605 static struct stv090x_long_frame_crloop stv090x_s2_crl_cut30[] = {
606 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
607 { STV090x_QPSK_12, 0x3c, 0x2c, 0x0c, 0x2c, 0x1b, 0x2c, 0x1b, 0x1c, 0x0b, 0x3b },
608 { STV090x_QPSK_35, 0x0d, 0x0d, 0x0c, 0x0d, 0x1b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
609 { STV090x_QPSK_23, 0x1d, 0x0d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
610 { STV090x_QPSK_34, 0x1d, 0x1d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
611 { STV090x_QPSK_45, 0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
612 { STV090x_QPSK_56, 0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
613 { STV090x_QPSK_89, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
614 { STV090x_QPSK_910, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
615 { STV090x_8PSK_35, 0x39, 0x29, 0x39, 0x19, 0x19, 0x19, 0x19, 0x19, 0x09, 0x19 },
616 { STV090x_8PSK_23, 0x2a, 0x39, 0x1a, 0x0a, 0x39, 0x0a, 0x29, 0x39, 0x29, 0x0a },
617 { STV090x_8PSK_34, 0x2b, 0x3a, 0x1b, 0x1b, 0x3a, 0x1b, 0x1a, 0x0b, 0x1a, 0x3a },
618 { STV090x_8PSK_56, 0x0c, 0x1b, 0x3b, 0x3b, 0x1b, 0x3b, 0x3a, 0x3b, 0x3a, 0x1b },
619 { STV090x_8PSK_89, 0x0d, 0x3c, 0x2c, 0x2c, 0x2b, 0x0c, 0x0b, 0x3b, 0x0b, 0x1b },
620 { STV090x_8PSK_910, 0x0d, 0x0d, 0x2c, 0x3c, 0x3b, 0x1c, 0x0b, 0x3b, 0x0b, 0x1b }
623 /* Cut 2.0 Long Frame Tracking CR Loop */
624 static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut20[] = {
625 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
626 { STV090x_16APSK_23, 0x0c, 0x0c, 0x0c, 0x0c, 0x1d, 0x0c, 0x3c, 0x0c, 0x2c, 0x0c },
627 { STV090x_16APSK_34, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0c, 0x2d, 0x0c, 0x1d, 0x0c },
628 { STV090x_16APSK_45, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
629 { STV090x_16APSK_56, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
630 { STV090x_16APSK_89, 0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
631 { STV090x_16APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
632 { STV090x_32APSK_34, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
633 { STV090x_32APSK_45, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
634 { STV090x_32APSK_56, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
635 { STV090x_32APSK_89, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
636 { STV090x_32APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c }
639 /* Cut 3.0 Long Frame Tracking CR Loop */
640 static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut30[] = {
641 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
642 { STV090x_16APSK_23, 0x0a, 0x0a, 0x0a, 0x0a, 0x1a, 0x0a, 0x3a, 0x0a, 0x2a, 0x0a },
643 { STV090x_16APSK_34, 0x0a, 0x0a, 0x0a, 0x0a, 0x0b, 0x0a, 0x3b, 0x0a, 0x1b, 0x0a },
644 { STV090x_16APSK_45, 0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
645 { STV090x_16APSK_56, 0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
646 { STV090x_16APSK_89, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
647 { STV090x_16APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
648 { STV090x_32APSK_34, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
649 { STV090x_32APSK_45, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
650 { STV090x_32APSK_56, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
651 { STV090x_32APSK_89, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
652 { STV090x_32APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a }
655 static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut20[] = {
656 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
657 { STV090x_QPSK_14, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x2d, 0x1f, 0x3d, 0x3e },
658 { STV090x_QPSK_13, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x3d, 0x0f, 0x3d, 0x2e },
659 { STV090x_QPSK_25, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x2e }
662 static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut30[] = {
663 /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
664 { STV090x_QPSK_14, 0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x2a, 0x1c, 0x3a, 0x3b },
665 { STV090x_QPSK_13, 0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x3a, 0x0c, 0x3a, 0x2b },
666 { STV090x_QPSK_25, 0x1c, 0x3c, 0x1b, 0x3c, 0x3a, 0x1c, 0x3a, 0x3b, 0x3a, 0x2b }
669 /* Cut 2.0 Short Frame Tracking CR Loop */
670 static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut20[] = {
671 /* MODCOD 2M 5M 10M 20M 30M */
672 { STV090x_QPSK, 0x2f, 0x2e, 0x0e, 0x0e, 0x3d },
673 { STV090x_8PSK, 0x3e, 0x0e, 0x2d, 0x0d, 0x3c },
674 { STV090x_16APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d },
675 { STV090x_32APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d }
678 /* Cut 3.0 Short Frame Tracking CR Loop */
679 static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut30[] = {
680 /* MODCOD 2M 5M 10M 20M 30M */
681 { STV090x_QPSK, 0x2C, 0x2B, 0x0B, 0x0B, 0x3A },
682 { STV090x_8PSK, 0x3B, 0x0B, 0x2A, 0x0A, 0x39 },
683 { STV090x_16APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A },
684 { STV090x_32APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A }
687 static inline s32 comp2(s32 __x, s32 __width)
689 if (__width == 32)
690 return __x;
691 else
692 return (__x >= (1 << (__width - 1))) ? (__x - (1 << __width)) : __x;
695 static int stv090x_read_reg(struct stv090x_state *state, unsigned int reg)
697 const struct stv090x_config *config = state->config;
698 int ret;
700 u8 b0[] = { reg >> 8, reg & 0xff };
701 u8 buf;
703 struct i2c_msg msg[] = {
704 { .addr = config->address, .flags = 0, .buf = b0, .len = 2 },
705 { .addr = config->address, .flags = I2C_M_RD, .buf = &buf, .len = 1 }
708 ret = i2c_transfer(state->i2c, msg, 2);
709 if (ret != 2) {
710 if (ret != -ERESTARTSYS)
711 dprintk(FE_ERROR, 1,
712 "Read error, Reg=[0x%02x], Status=%d",
713 reg, ret);
715 return ret < 0 ? ret : -EREMOTEIO;
717 if (unlikely(*state->verbose >= FE_DEBUGREG))
718 dprintk(FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
719 reg, buf);
721 return (unsigned int) buf;
724 static int stv090x_write_regs(struct stv090x_state *state, unsigned int reg, u8 *data, u32 count)
726 const struct stv090x_config *config = state->config;
727 int ret;
728 u8 buf[MAX_XFER_SIZE];
729 struct i2c_msg i2c_msg = { .addr = config->address, .flags = 0, .buf = buf, .len = 2 + count };
731 if (2 + count > sizeof(buf)) {
732 printk(KERN_WARNING
733 "%s: i2c wr reg=%04x: len=%d is too big!\n",
734 KBUILD_MODNAME, reg, count);
735 return -EINVAL;
738 buf[0] = reg >> 8;
739 buf[1] = reg & 0xff;
740 memcpy(&buf[2], data, count);
742 dprintk(FE_DEBUGREG, 1, "%s [0x%04x]: %*ph",
743 __func__, reg, count, data);
745 ret = i2c_transfer(state->i2c, &i2c_msg, 1);
746 if (ret != 1) {
747 if (ret != -ERESTARTSYS)
748 dprintk(FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
749 reg, data[0], count, ret);
750 return ret < 0 ? ret : -EREMOTEIO;
753 return 0;
756 static int stv090x_write_reg(struct stv090x_state *state, unsigned int reg, u8 data)
758 return stv090x_write_regs(state, reg, &data, 1);
761 static int stv090x_i2c_gate_ctrl(struct stv090x_state *state, int enable)
763 u32 reg;
766 * NOTE! A lock is used as a FSM to control the state in which
767 * access is serialized between two tuners on the same demod.
768 * This has nothing to do with a lock to protect a critical section
769 * which may in some other cases be confused with protecting I/O
770 * access to the demodulator gate.
771 * In case of any error, the lock is unlocked and exit within the
772 * relevant operations themselves.
774 if (enable) {
775 if (state->config->tuner_i2c_lock)
776 state->config->tuner_i2c_lock(&state->frontend, 1);
777 else
778 mutex_lock(&state->internal->tuner_lock);
781 reg = STV090x_READ_DEMOD(state, I2CRPT);
782 if (enable) {
783 dprintk(FE_DEBUG, 1, "Enable Gate");
784 STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 1);
785 if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0)
786 goto err;
788 } else {
789 dprintk(FE_DEBUG, 1, "Disable Gate");
790 STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 0);
791 if ((STV090x_WRITE_DEMOD(state, I2CRPT, reg)) < 0)
792 goto err;
795 if (!enable) {
796 if (state->config->tuner_i2c_lock)
797 state->config->tuner_i2c_lock(&state->frontend, 0);
798 else
799 mutex_unlock(&state->internal->tuner_lock);
802 return 0;
803 err:
804 dprintk(FE_ERROR, 1, "I/O error");
805 if (state->config->tuner_i2c_lock)
806 state->config->tuner_i2c_lock(&state->frontend, 0);
807 else
808 mutex_unlock(&state->internal->tuner_lock);
809 return -1;
812 static void stv090x_get_lock_tmg(struct stv090x_state *state)
814 switch (state->algo) {
815 case STV090x_BLIND_SEARCH:
816 dprintk(FE_DEBUG, 1, "Blind Search");
817 if (state->srate <= 1500000) { /*10Msps< SR <=15Msps*/
818 state->DemodTimeout = 1500;
819 state->FecTimeout = 400;
820 } else if (state->srate <= 5000000) { /*10Msps< SR <=15Msps*/
821 state->DemodTimeout = 1000;
822 state->FecTimeout = 300;
823 } else { /*SR >20Msps*/
824 state->DemodTimeout = 700;
825 state->FecTimeout = 100;
827 break;
829 case STV090x_COLD_SEARCH:
830 case STV090x_WARM_SEARCH:
831 default:
832 dprintk(FE_DEBUG, 1, "Normal Search");
833 if (state->srate <= 1000000) { /*SR <=1Msps*/
834 state->DemodTimeout = 4500;
835 state->FecTimeout = 1700;
836 } else if (state->srate <= 2000000) { /*1Msps < SR <= 2Msps */
837 state->DemodTimeout = 2500;
838 state->FecTimeout = 1100;
839 } else if (state->srate <= 5000000) { /*2Msps < SR <= 5Msps */
840 state->DemodTimeout = 1000;
841 state->FecTimeout = 550;
842 } else if (state->srate <= 10000000) { /*5Msps < SR <= 10Msps */
843 state->DemodTimeout = 700;
844 state->FecTimeout = 250;
845 } else if (state->srate <= 20000000) { /*10Msps < SR <= 20Msps */
846 state->DemodTimeout = 400;
847 state->FecTimeout = 130;
848 } else { /*SR >20Msps*/
849 state->DemodTimeout = 300;
850 state->FecTimeout = 100;
852 break;
855 if (state->algo == STV090x_WARM_SEARCH)
856 state->DemodTimeout /= 2;
859 static int stv090x_set_srate(struct stv090x_state *state, u32 srate)
861 u32 sym;
863 if (srate > 60000000) {
864 sym = (srate << 4); /* SR * 2^16 / master_clk */
865 sym /= (state->internal->mclk >> 12);
866 } else if (srate > 6000000) {
867 sym = (srate << 6);
868 sym /= (state->internal->mclk >> 10);
869 } else {
870 sym = (srate << 9);
871 sym /= (state->internal->mclk >> 7);
874 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0x7f) < 0) /* MSB */
875 goto err;
876 if (STV090x_WRITE_DEMOD(state, SFRINIT0, (sym & 0xff)) < 0) /* LSB */
877 goto err;
879 return 0;
880 err:
881 dprintk(FE_ERROR, 1, "I/O error");
882 return -1;
885 static int stv090x_set_max_srate(struct stv090x_state *state, u32 clk, u32 srate)
887 u32 sym;
889 srate = 105 * (srate / 100);
890 if (srate > 60000000) {
891 sym = (srate << 4); /* SR * 2^16 / master_clk */
892 sym /= (state->internal->mclk >> 12);
893 } else if (srate > 6000000) {
894 sym = (srate << 6);
895 sym /= (state->internal->mclk >> 10);
896 } else {
897 sym = (srate << 9);
898 sym /= (state->internal->mclk >> 7);
901 if (sym < 0x7fff) {
902 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) /* MSB */
903 goto err;
904 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) /* LSB */
905 goto err;
906 } else {
907 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x7f) < 0) /* MSB */
908 goto err;
909 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xff) < 0) /* LSB */
910 goto err;
913 return 0;
914 err:
915 dprintk(FE_ERROR, 1, "I/O error");
916 return -1;
919 static int stv090x_set_min_srate(struct stv090x_state *state, u32 clk, u32 srate)
921 u32 sym;
923 srate = 95 * (srate / 100);
924 if (srate > 60000000) {
925 sym = (srate << 4); /* SR * 2^16 / master_clk */
926 sym /= (state->internal->mclk >> 12);
927 } else if (srate > 6000000) {
928 sym = (srate << 6);
929 sym /= (state->internal->mclk >> 10);
930 } else {
931 sym = (srate << 9);
932 sym /= (state->internal->mclk >> 7);
935 if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0x7f)) < 0) /* MSB */
936 goto err;
937 if (STV090x_WRITE_DEMOD(state, SFRLOW0, (sym & 0xff)) < 0) /* LSB */
938 goto err;
939 return 0;
940 err:
941 dprintk(FE_ERROR, 1, "I/O error");
942 return -1;
945 static u32 stv090x_car_width(u32 srate, enum stv090x_rolloff rolloff)
947 u32 ro;
949 switch (rolloff) {
950 case STV090x_RO_20:
951 ro = 20;
952 break;
953 case STV090x_RO_25:
954 ro = 25;
955 break;
956 case STV090x_RO_35:
957 default:
958 ro = 35;
959 break;
962 return srate + (srate * ro) / 100;
965 static int stv090x_set_vit_thacq(struct stv090x_state *state)
967 if (STV090x_WRITE_DEMOD(state, VTH12, 0x96) < 0)
968 goto err;
969 if (STV090x_WRITE_DEMOD(state, VTH23, 0x64) < 0)
970 goto err;
971 if (STV090x_WRITE_DEMOD(state, VTH34, 0x36) < 0)
972 goto err;
973 if (STV090x_WRITE_DEMOD(state, VTH56, 0x23) < 0)
974 goto err;
975 if (STV090x_WRITE_DEMOD(state, VTH67, 0x1e) < 0)
976 goto err;
977 if (STV090x_WRITE_DEMOD(state, VTH78, 0x19) < 0)
978 goto err;
979 return 0;
980 err:
981 dprintk(FE_ERROR, 1, "I/O error");
982 return -1;
985 static int stv090x_set_vit_thtracq(struct stv090x_state *state)
987 if (STV090x_WRITE_DEMOD(state, VTH12, 0xd0) < 0)
988 goto err;
989 if (STV090x_WRITE_DEMOD(state, VTH23, 0x7d) < 0)
990 goto err;
991 if (STV090x_WRITE_DEMOD(state, VTH34, 0x53) < 0)
992 goto err;
993 if (STV090x_WRITE_DEMOD(state, VTH56, 0x2f) < 0)
994 goto err;
995 if (STV090x_WRITE_DEMOD(state, VTH67, 0x24) < 0)
996 goto err;
997 if (STV090x_WRITE_DEMOD(state, VTH78, 0x1f) < 0)
998 goto err;
999 return 0;
1000 err:
1001 dprintk(FE_ERROR, 1, "I/O error");
1002 return -1;
1005 static int stv090x_set_viterbi(struct stv090x_state *state)
1007 switch (state->search_mode) {
1008 case STV090x_SEARCH_AUTO:
1009 if (STV090x_WRITE_DEMOD(state, FECM, 0x10) < 0) /* DVB-S and DVB-S2 */
1010 goto err;
1011 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x3f) < 0) /* all puncture rate */
1012 goto err;
1013 break;
1014 case STV090x_SEARCH_DVBS1:
1015 if (STV090x_WRITE_DEMOD(state, FECM, 0x00) < 0) /* disable DSS */
1016 goto err;
1017 switch (state->fec) {
1018 case STV090x_PR12:
1019 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
1020 goto err;
1021 break;
1023 case STV090x_PR23:
1024 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
1025 goto err;
1026 break;
1028 case STV090x_PR34:
1029 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x04) < 0)
1030 goto err;
1031 break;
1033 case STV090x_PR56:
1034 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x08) < 0)
1035 goto err;
1036 break;
1038 case STV090x_PR78:
1039 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x20) < 0)
1040 goto err;
1041 break;
1043 default:
1044 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x2f) < 0) /* all */
1045 goto err;
1046 break;
1048 break;
1049 case STV090x_SEARCH_DSS:
1050 if (STV090x_WRITE_DEMOD(state, FECM, 0x80) < 0)
1051 goto err;
1052 switch (state->fec) {
1053 case STV090x_PR12:
1054 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
1055 goto err;
1056 break;
1058 case STV090x_PR23:
1059 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
1060 goto err;
1061 break;
1063 case STV090x_PR67:
1064 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x10) < 0)
1065 goto err;
1066 break;
1068 default:
1069 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x13) < 0) /* 1/2, 2/3, 6/7 */
1070 goto err;
1071 break;
1073 break;
1074 default:
1075 break;
1077 return 0;
1078 err:
1079 dprintk(FE_ERROR, 1, "I/O error");
1080 return -1;
1083 static int stv090x_stop_modcod(struct stv090x_state *state)
1085 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1086 goto err;
1087 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
1088 goto err;
1089 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
1090 goto err;
1091 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
1092 goto err;
1093 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
1094 goto err;
1095 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
1096 goto err;
1097 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
1098 goto err;
1099 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xff) < 0)
1100 goto err;
1101 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xff) < 0)
1102 goto err;
1103 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xff) < 0)
1104 goto err;
1105 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xff) < 0)
1106 goto err;
1107 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xff) < 0)
1108 goto err;
1109 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xff) < 0)
1110 goto err;
1111 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xff) < 0)
1112 goto err;
1113 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
1114 goto err;
1115 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xff) < 0)
1116 goto err;
1117 return 0;
1118 err:
1119 dprintk(FE_ERROR, 1, "I/O error");
1120 return -1;
1123 static int stv090x_activate_modcod(struct stv090x_state *state)
1125 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1126 goto err;
1127 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xfc) < 0)
1128 goto err;
1129 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xcc) < 0)
1130 goto err;
1131 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xcc) < 0)
1132 goto err;
1133 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xcc) < 0)
1134 goto err;
1135 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xcc) < 0)
1136 goto err;
1137 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xcc) < 0)
1138 goto err;
1139 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
1140 goto err;
1141 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
1142 goto err;
1143 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
1144 goto err;
1145 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
1146 goto err;
1147 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
1148 goto err;
1149 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
1150 goto err;
1151 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
1152 goto err;
1153 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xcc) < 0)
1154 goto err;
1155 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
1156 goto err;
1158 return 0;
1159 err:
1160 dprintk(FE_ERROR, 1, "I/O error");
1161 return -1;
1164 static int stv090x_activate_modcod_single(struct stv090x_state *state)
1167 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1168 goto err;
1169 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xf0) < 0)
1170 goto err;
1171 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0x00) < 0)
1172 goto err;
1173 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0x00) < 0)
1174 goto err;
1175 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0x00) < 0)
1176 goto err;
1177 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0x00) < 0)
1178 goto err;
1179 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0x00) < 0)
1180 goto err;
1181 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0x00) < 0)
1182 goto err;
1183 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0x00) < 0)
1184 goto err;
1185 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0x00) < 0)
1186 goto err;
1187 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0x00) < 0)
1188 goto err;
1189 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0x00) < 0)
1190 goto err;
1191 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0x00) < 0)
1192 goto err;
1193 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0x00) < 0)
1194 goto err;
1195 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0x00) < 0)
1196 goto err;
1197 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0x0f) < 0)
1198 goto err;
1200 return 0;
1202 err:
1203 dprintk(FE_ERROR, 1, "I/O error");
1204 return -1;
1207 static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
1209 u32 reg;
1211 switch (state->demod) {
1212 case STV090x_DEMODULATOR_0:
1213 mutex_lock(&state->internal->demod_lock);
1214 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1215 STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, enable);
1216 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1217 goto err;
1218 mutex_unlock(&state->internal->demod_lock);
1219 break;
1221 case STV090x_DEMODULATOR_1:
1222 mutex_lock(&state->internal->demod_lock);
1223 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1224 STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, enable);
1225 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1226 goto err;
1227 mutex_unlock(&state->internal->demod_lock);
1228 break;
1230 default:
1231 dprintk(FE_ERROR, 1, "Wrong demodulator!");
1232 break;
1234 return 0;
1235 err:
1236 mutex_unlock(&state->internal->demod_lock);
1237 dprintk(FE_ERROR, 1, "I/O error");
1238 return -1;
1241 static int stv090x_dvbs_track_crl(struct stv090x_state *state)
1243 if (state->internal->dev_ver >= 0x30) {
1244 /* Set ACLC BCLC optimised value vs SR */
1245 if (state->srate >= 15000000) {
1246 if (STV090x_WRITE_DEMOD(state, ACLC, 0x2b) < 0)
1247 goto err;
1248 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1a) < 0)
1249 goto err;
1250 } else if ((state->srate >= 7000000) && (15000000 > state->srate)) {
1251 if (STV090x_WRITE_DEMOD(state, ACLC, 0x0c) < 0)
1252 goto err;
1253 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1b) < 0)
1254 goto err;
1255 } else if (state->srate < 7000000) {
1256 if (STV090x_WRITE_DEMOD(state, ACLC, 0x2c) < 0)
1257 goto err;
1258 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1c) < 0)
1259 goto err;
1262 } else {
1263 /* Cut 2.0 */
1264 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
1265 goto err;
1266 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1267 goto err;
1269 return 0;
1270 err:
1271 dprintk(FE_ERROR, 1, "I/O error");
1272 return -1;
1275 static int stv090x_delivery_search(struct stv090x_state *state)
1277 u32 reg;
1279 switch (state->search_mode) {
1280 case STV090x_SEARCH_DVBS1:
1281 case STV090x_SEARCH_DSS:
1282 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1283 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1284 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1285 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1286 goto err;
1288 /* Activate Viterbi decoder in legacy search,
1289 * do not use FRESVIT1, might impact VITERBI2
1291 if (stv090x_vitclk_ctl(state, 0) < 0)
1292 goto err;
1294 if (stv090x_dvbs_track_crl(state) < 0)
1295 goto err;
1297 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x22) < 0) /* disable DVB-S2 */
1298 goto err;
1300 if (stv090x_set_vit_thacq(state) < 0)
1301 goto err;
1302 if (stv090x_set_viterbi(state) < 0)
1303 goto err;
1304 break;
1306 case STV090x_SEARCH_DVBS2:
1307 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1308 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
1309 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1310 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1311 goto err;
1312 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1313 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
1314 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1315 goto err;
1317 if (stv090x_vitclk_ctl(state, 1) < 0)
1318 goto err;
1320 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0) /* stop DVB-S CR loop */
1321 goto err;
1322 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1323 goto err;
1325 if (state->internal->dev_ver <= 0x20) {
1326 /* enable S2 carrier loop */
1327 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1328 goto err;
1329 } else {
1330 /* > Cut 3: Stop carrier 3 */
1331 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1332 goto err;
1335 if (state->demod_mode != STV090x_SINGLE) {
1336 /* Cut 2: enable link during search */
1337 if (stv090x_activate_modcod(state) < 0)
1338 goto err;
1339 } else {
1340 /* Single demodulator
1341 * Authorize SHORT and LONG frames,
1342 * QPSK, 8PSK, 16APSK and 32APSK
1344 if (stv090x_activate_modcod_single(state) < 0)
1345 goto err;
1348 if (stv090x_set_vit_thtracq(state) < 0)
1349 goto err;
1350 break;
1352 case STV090x_SEARCH_AUTO:
1353 default:
1354 /* enable DVB-S2 and DVB-S2 in Auto MODE */
1355 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1356 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
1357 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
1358 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1359 goto err;
1360 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
1361 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
1362 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1363 goto err;
1365 if (stv090x_vitclk_ctl(state, 0) < 0)
1366 goto err;
1368 if (stv090x_dvbs_track_crl(state) < 0)
1369 goto err;
1371 if (state->internal->dev_ver <= 0x20) {
1372 /* enable S2 carrier loop */
1373 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1374 goto err;
1375 } else {
1376 /* > Cut 3: Stop carrier 3 */
1377 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1378 goto err;
1381 if (state->demod_mode != STV090x_SINGLE) {
1382 /* Cut 2: enable link during search */
1383 if (stv090x_activate_modcod(state) < 0)
1384 goto err;
1385 } else {
1386 /* Single demodulator
1387 * Authorize SHORT and LONG frames,
1388 * QPSK, 8PSK, 16APSK and 32APSK
1390 if (stv090x_activate_modcod_single(state) < 0)
1391 goto err;
1394 if (stv090x_set_vit_thacq(state) < 0)
1395 goto err;
1397 if (stv090x_set_viterbi(state) < 0)
1398 goto err;
1399 break;
1401 return 0;
1402 err:
1403 dprintk(FE_ERROR, 1, "I/O error");
1404 return -1;
1407 static int stv090x_start_search(struct stv090x_state *state)
1409 u32 reg, freq_abs;
1410 s16 freq;
1412 /* Reset demodulator */
1413 reg = STV090x_READ_DEMOD(state, DMDISTATE);
1414 STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f);
1415 if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
1416 goto err;
1418 if (state->internal->dev_ver <= 0x20) {
1419 if (state->srate <= 5000000) {
1420 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0)
1421 goto err;
1422 if (STV090x_WRITE_DEMOD(state, CFRUP1, 0x0f) < 0)
1423 goto err;
1424 if (STV090x_WRITE_DEMOD(state, CFRUP0, 0xff) < 0)
1425 goto err;
1426 if (STV090x_WRITE_DEMOD(state, CFRLOW1, 0xf0) < 0)
1427 goto err;
1428 if (STV090x_WRITE_DEMOD(state, CFRLOW0, 0x00) < 0)
1429 goto err;
1431 /*enlarge the timing bandwidth for Low SR*/
1432 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0)
1433 goto err;
1434 } else {
1435 /* If the symbol rate is >5 Msps
1436 Set The carrier search up and low to auto mode */
1437 if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
1438 goto err;
1439 /*reduce the timing bandwidth for high SR*/
1440 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
1441 goto err;
1443 } else {
1444 /* >= Cut 3 */
1445 if (state->srate <= 5000000) {
1446 /* enlarge the timing bandwidth for Low SR */
1447 STV090x_WRITE_DEMOD(state, RTCS2, 0x68);
1448 } else {
1449 /* reduce timing bandwidth for high SR */
1450 STV090x_WRITE_DEMOD(state, RTCS2, 0x44);
1453 /* Set CFR min and max to manual mode */
1454 STV090x_WRITE_DEMOD(state, CARCFG, 0x46);
1456 if (state->algo == STV090x_WARM_SEARCH) {
1457 /* WARM Start
1458 * CFR min = -1MHz,
1459 * CFR max = +1MHz
1461 freq_abs = 1000 << 16;
1462 freq_abs /= (state->internal->mclk / 1000);
1463 freq = (s16) freq_abs;
1464 } else {
1465 /* COLD Start
1466 * CFR min =- (SearchRange / 2 + 600KHz)
1467 * CFR max = +(SearchRange / 2 + 600KHz)
1468 * (600KHz for the tuner step size)
1470 freq_abs = (state->search_range / 2000) + 600;
1471 freq_abs = freq_abs << 16;
1472 freq_abs /= (state->internal->mclk / 1000);
1473 freq = (s16) freq_abs;
1476 if (STV090x_WRITE_DEMOD(state, CFRUP1, MSB(freq)) < 0)
1477 goto err;
1478 if (STV090x_WRITE_DEMOD(state, CFRUP0, LSB(freq)) < 0)
1479 goto err;
1481 freq *= -1;
1483 if (STV090x_WRITE_DEMOD(state, CFRLOW1, MSB(freq)) < 0)
1484 goto err;
1485 if (STV090x_WRITE_DEMOD(state, CFRLOW0, LSB(freq)) < 0)
1486 goto err;
1490 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0) < 0)
1491 goto err;
1492 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0) < 0)
1493 goto err;
1495 if (state->internal->dev_ver >= 0x20) {
1496 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1497 goto err;
1498 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
1499 goto err;
1501 if ((state->search_mode == STV090x_SEARCH_DVBS1) ||
1502 (state->search_mode == STV090x_SEARCH_DSS) ||
1503 (state->search_mode == STV090x_SEARCH_AUTO)) {
1505 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
1506 goto err;
1507 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0)
1508 goto err;
1512 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00) < 0)
1513 goto err;
1514 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xe0) < 0)
1515 goto err;
1516 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xc0) < 0)
1517 goto err;
1519 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1520 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
1521 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
1522 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1523 goto err;
1524 reg = STV090x_READ_DEMOD(state, DMDCFG2);
1525 STV090x_SETFIELD_Px(reg, S1S2_SEQUENTIAL_FIELD, 0x0);
1526 if (STV090x_WRITE_DEMOD(state, DMDCFG2, reg) < 0)
1527 goto err;
1529 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0)
1530 goto err;
1532 if (state->internal->dev_ver >= 0x20) {
1533 /*Frequency offset detector setting*/
1534 if (state->srate < 2000000) {
1535 if (state->internal->dev_ver <= 0x20) {
1536 /* Cut 2 */
1537 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x39) < 0)
1538 goto err;
1539 } else {
1540 /* Cut 3 */
1541 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x89) < 0)
1542 goto err;
1544 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x40) < 0)
1545 goto err;
1546 } else if (state->srate < 10000000) {
1547 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4c) < 0)
1548 goto err;
1549 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1550 goto err;
1551 } else {
1552 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4b) < 0)
1553 goto err;
1554 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1555 goto err;
1557 } else {
1558 if (state->srate < 10000000) {
1559 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
1560 goto err;
1561 } else {
1562 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
1563 goto err;
1567 switch (state->algo) {
1568 case STV090x_WARM_SEARCH:
1569 /* The symbol rate and the exact
1570 * carrier Frequency are known
1572 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
1573 goto err;
1574 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
1575 goto err;
1576 break;
1578 case STV090x_COLD_SEARCH:
1579 /* The symbol rate is known */
1580 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
1581 goto err;
1582 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
1583 goto err;
1584 break;
1586 default:
1587 break;
1589 return 0;
1590 err:
1591 dprintk(FE_ERROR, 1, "I/O error");
1592 return -1;
1595 static int stv090x_get_agc2_min_level(struct stv090x_state *state)
1597 u32 agc2_min = 0xffff, agc2 = 0, freq_init, freq_step, reg;
1598 s32 i, j, steps, dir;
1600 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1601 goto err;
1602 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1603 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
1604 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
1605 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1606 goto err;
1608 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0) /* SR = 65 Msps Max */
1609 goto err;
1610 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1611 goto err;
1612 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0) /* SR= 400 ksps Min */
1613 goto err;
1614 if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1615 goto err;
1616 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0) /* stop acq @ coarse carrier state */
1617 goto err;
1618 if (stv090x_set_srate(state, 1000000) < 0)
1619 goto err;
1621 steps = state->search_range / 1000000;
1622 if (steps <= 0)
1623 steps = 1;
1625 dir = 1;
1626 freq_step = (1000000 * 256) / (state->internal->mclk / 256);
1627 freq_init = 0;
1629 for (i = 0; i < steps; i++) {
1630 if (dir > 0)
1631 freq_init = freq_init + (freq_step * i);
1632 else
1633 freq_init = freq_init - (freq_step * i);
1635 dir *= -1;
1637 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod RESET */
1638 goto err;
1639 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_init >> 8) & 0xff) < 0)
1640 goto err;
1641 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_init & 0xff) < 0)
1642 goto err;
1643 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x58) < 0) /* Demod RESET */
1644 goto err;
1645 msleep(10);
1647 agc2 = 0;
1648 for (j = 0; j < 10; j++) {
1649 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1650 STV090x_READ_DEMOD(state, AGC2I0);
1652 agc2 /= 10;
1653 if (agc2 < agc2_min)
1654 agc2_min = agc2;
1657 return agc2_min;
1658 err:
1659 dprintk(FE_ERROR, 1, "I/O error");
1660 return -1;
1663 static u32 stv090x_get_srate(struct stv090x_state *state, u32 clk)
1665 u8 r3, r2, r1, r0;
1666 s32 srate, int_1, int_2, tmp_1, tmp_2;
1668 r3 = STV090x_READ_DEMOD(state, SFR3);
1669 r2 = STV090x_READ_DEMOD(state, SFR2);
1670 r1 = STV090x_READ_DEMOD(state, SFR1);
1671 r0 = STV090x_READ_DEMOD(state, SFR0);
1673 srate = ((r3 << 24) | (r2 << 16) | (r1 << 8) | r0);
1675 int_1 = clk >> 16;
1676 int_2 = srate >> 16;
1678 tmp_1 = clk % 0x10000;
1679 tmp_2 = srate % 0x10000;
1681 srate = (int_1 * int_2) +
1682 ((int_1 * tmp_2) >> 16) +
1683 ((int_2 * tmp_1) >> 16);
1685 return srate;
1688 static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
1690 struct dvb_frontend *fe = &state->frontend;
1692 int tmg_lock = 0, i;
1693 s32 tmg_cpt = 0, dir = 1, steps, cur_step = 0, freq;
1694 u32 srate_coarse = 0, agc2 = 0, car_step = 1200, reg;
1695 u32 agc2th;
1697 if (state->internal->dev_ver >= 0x30)
1698 agc2th = 0x2e00;
1699 else
1700 agc2th = 0x1f00;
1702 reg = STV090x_READ_DEMOD(state, DMDISTATE);
1703 STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f); /* Demod RESET */
1704 if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
1705 goto err;
1706 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0x12) < 0)
1707 goto err;
1708 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0)
1709 goto err;
1710 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xf0) < 0)
1711 goto err;
1712 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xe0) < 0)
1713 goto err;
1714 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1715 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 1);
1716 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
1717 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1718 goto err;
1720 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0)
1721 goto err;
1722 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1723 goto err;
1724 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0)
1725 goto err;
1726 if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1727 goto err;
1728 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0)
1729 goto err;
1730 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x50) < 0)
1731 goto err;
1733 if (state->internal->dev_ver >= 0x30) {
1734 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x99) < 0)
1735 goto err;
1736 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x98) < 0)
1737 goto err;
1739 } else if (state->internal->dev_ver >= 0x20) {
1740 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0)
1741 goto err;
1742 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
1743 goto err;
1746 if (state->srate <= 2000000)
1747 car_step = 1000;
1748 else if (state->srate <= 5000000)
1749 car_step = 2000;
1750 else if (state->srate <= 12000000)
1751 car_step = 3000;
1752 else
1753 car_step = 5000;
1755 steps = -1 + ((state->search_range / 1000) / car_step);
1756 steps /= 2;
1757 steps = (2 * steps) + 1;
1758 if (steps < 0)
1759 steps = 1;
1760 else if (steps > 10) {
1761 steps = 11;
1762 car_step = (state->search_range / 1000) / 10;
1764 cur_step = 0;
1765 dir = 1;
1766 freq = state->frequency;
1768 while ((!tmg_lock) && (cur_step < steps)) {
1769 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5f) < 0) /* Demod RESET */
1770 goto err;
1771 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
1772 goto err;
1773 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
1774 goto err;
1775 if (STV090x_WRITE_DEMOD(state, SFRINIT1, 0x00) < 0)
1776 goto err;
1777 if (STV090x_WRITE_DEMOD(state, SFRINIT0, 0x00) < 0)
1778 goto err;
1779 /* trigger acquisition */
1780 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x40) < 0)
1781 goto err;
1782 msleep(50);
1783 for (i = 0; i < 10; i++) {
1784 reg = STV090x_READ_DEMOD(state, DSTATUS);
1785 if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
1786 tmg_cpt++;
1787 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1788 STV090x_READ_DEMOD(state, AGC2I0);
1790 agc2 /= 10;
1791 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
1792 cur_step++;
1793 dir *= -1;
1794 if ((tmg_cpt >= 5) && (agc2 < agc2th) &&
1795 (srate_coarse < 50000000) && (srate_coarse > 850000))
1796 tmg_lock = 1;
1797 else if (cur_step < steps) {
1798 if (dir > 0)
1799 freq += cur_step * car_step;
1800 else
1801 freq -= cur_step * car_step;
1803 /* Setup tuner */
1804 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
1805 goto err;
1807 if (state->config->tuner_set_frequency) {
1808 if (state->config->tuner_set_frequency(fe, freq) < 0)
1809 goto err_gateoff;
1812 if (state->config->tuner_set_bandwidth) {
1813 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
1814 goto err_gateoff;
1817 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
1818 goto err;
1820 msleep(50);
1822 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
1823 goto err;
1825 if (state->config->tuner_get_status) {
1826 if (state->config->tuner_get_status(fe, &reg) < 0)
1827 goto err_gateoff;
1830 if (reg)
1831 dprintk(FE_DEBUG, 1, "Tuner phase locked");
1832 else
1833 dprintk(FE_DEBUG, 1, "Tuner unlocked");
1835 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
1836 goto err;
1840 if (!tmg_lock)
1841 srate_coarse = 0;
1842 else
1843 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
1845 return srate_coarse;
1847 err_gateoff:
1848 stv090x_i2c_gate_ctrl(state, 0);
1849 err:
1850 dprintk(FE_ERROR, 1, "I/O error");
1851 return -1;
1854 static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
1856 u32 srate_coarse, freq_coarse, sym, reg;
1858 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
1859 freq_coarse = STV090x_READ_DEMOD(state, CFR2) << 8;
1860 freq_coarse |= STV090x_READ_DEMOD(state, CFR1);
1861 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1863 if (sym < state->srate)
1864 srate_coarse = 0;
1865 else {
1866 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0) /* Demod RESET */
1867 goto err;
1868 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
1869 goto err;
1870 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
1871 goto err;
1872 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
1873 goto err;
1874 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
1875 goto err;
1876 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
1877 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
1878 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
1879 goto err;
1881 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1882 goto err;
1884 if (state->internal->dev_ver >= 0x30) {
1885 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x79) < 0)
1886 goto err;
1887 } else if (state->internal->dev_ver >= 0x20) {
1888 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
1889 goto err;
1892 if (srate_coarse > 3000000) {
1893 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1894 sym = (sym / 1000) * 65536;
1895 sym /= (state->internal->mclk / 1000);
1896 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1897 goto err;
1898 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1899 goto err;
1900 sym = 10 * (srate_coarse / 13); /* SFRLOW = SFR - 30% */
1901 sym = (sym / 1000) * 65536;
1902 sym /= (state->internal->mclk / 1000);
1903 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1904 goto err;
1905 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1906 goto err;
1907 sym = (srate_coarse / 1000) * 65536;
1908 sym /= (state->internal->mclk / 1000);
1909 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1910 goto err;
1911 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
1912 goto err;
1913 } else {
1914 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1915 sym = (sym / 100) * 65536;
1916 sym /= (state->internal->mclk / 100);
1917 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1918 goto err;
1919 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1920 goto err;
1921 sym = 10 * (srate_coarse / 14); /* SFRLOW = SFR - 30% */
1922 sym = (sym / 100) * 65536;
1923 sym /= (state->internal->mclk / 100);
1924 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1925 goto err;
1926 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1927 goto err;
1928 sym = (srate_coarse / 100) * 65536;
1929 sym /= (state->internal->mclk / 100);
1930 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1931 goto err;
1932 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
1933 goto err;
1935 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
1936 goto err;
1937 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_coarse >> 8) & 0xff) < 0)
1938 goto err;
1939 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_coarse & 0xff) < 0)
1940 goto err;
1941 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0) /* trigger acquisition */
1942 goto err;
1945 return srate_coarse;
1947 err:
1948 dprintk(FE_ERROR, 1, "I/O error");
1949 return -1;
1952 static int stv090x_get_dmdlock(struct stv090x_state *state, s32 timeout)
1954 s32 timer = 0, lock = 0;
1955 u32 reg;
1956 u8 stat;
1958 while ((timer < timeout) && (!lock)) {
1959 reg = STV090x_READ_DEMOD(state, DMDSTATE);
1960 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
1962 switch (stat) {
1963 case 0: /* searching */
1964 case 1: /* first PLH detected */
1965 default:
1966 dprintk(FE_DEBUG, 1, "Demodulator searching ..");
1967 lock = 0;
1968 break;
1969 case 2: /* DVB-S2 mode */
1970 case 3: /* DVB-S1/legacy mode */
1971 reg = STV090x_READ_DEMOD(state, DSTATUS);
1972 lock = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
1973 break;
1976 if (!lock)
1977 msleep(10);
1978 else
1979 dprintk(FE_DEBUG, 1, "Demodulator acquired LOCK");
1981 timer += 10;
1983 return lock;
1986 static int stv090x_blind_search(struct stv090x_state *state)
1988 u32 agc2, reg, srate_coarse;
1989 s32 cpt_fail, agc2_ovflw, i;
1990 u8 k_ref, k_max, k_min;
1991 int coarse_fail = 0;
1992 int lock;
1994 k_max = 110;
1995 k_min = 10;
1997 agc2 = stv090x_get_agc2_min_level(state);
1999 if (agc2 > STV090x_SEARCH_AGC2_TH(state->internal->dev_ver)) {
2000 lock = 0;
2001 } else {
2003 if (state->internal->dev_ver <= 0x20) {
2004 if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
2005 goto err;
2006 } else {
2007 /* > Cut 3 */
2008 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x06) < 0)
2009 goto err;
2012 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
2013 goto err;
2015 if (state->internal->dev_ver >= 0x20) {
2016 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
2017 goto err;
2018 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
2019 goto err;
2020 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
2021 goto err;
2022 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0) /* set viterbi hysteresis */
2023 goto err;
2026 k_ref = k_max;
2027 do {
2028 if (STV090x_WRITE_DEMOD(state, KREFTMG, k_ref) < 0)
2029 goto err;
2030 if (stv090x_srate_srch_coarse(state) != 0) {
2031 srate_coarse = stv090x_srate_srch_fine(state);
2032 if (srate_coarse != 0) {
2033 stv090x_get_lock_tmg(state);
2034 lock = stv090x_get_dmdlock(state,
2035 state->DemodTimeout);
2036 } else {
2037 lock = 0;
2039 } else {
2040 cpt_fail = 0;
2041 agc2_ovflw = 0;
2042 for (i = 0; i < 10; i++) {
2043 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
2044 STV090x_READ_DEMOD(state, AGC2I0);
2045 if (agc2 >= 0xff00)
2046 agc2_ovflw++;
2047 reg = STV090x_READ_DEMOD(state, DSTATUS2);
2048 if ((STV090x_GETFIELD_Px(reg, CFR_OVERFLOW_FIELD) == 0x01) &&
2049 (STV090x_GETFIELD_Px(reg, DEMOD_DELOCK_FIELD) == 0x01))
2051 cpt_fail++;
2053 if ((cpt_fail > 7) || (agc2_ovflw > 7))
2054 coarse_fail = 1;
2056 lock = 0;
2058 k_ref -= 20;
2059 } while ((k_ref >= k_min) && (!lock) && (!coarse_fail));
2062 return lock;
2064 err:
2065 dprintk(FE_ERROR, 1, "I/O error");
2066 return -1;
2069 static int stv090x_chk_tmg(struct stv090x_state *state)
2071 u32 reg;
2072 s32 tmg_cpt = 0, i;
2073 u8 freq, tmg_thh, tmg_thl;
2074 int tmg_lock = 0;
2076 freq = STV090x_READ_DEMOD(state, CARFREQ);
2077 tmg_thh = STV090x_READ_DEMOD(state, TMGTHRISE);
2078 tmg_thl = STV090x_READ_DEMOD(state, TMGTHFALL);
2079 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
2080 goto err;
2081 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
2082 goto err;
2084 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2085 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00); /* stop carrier offset search */
2086 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2087 goto err;
2088 if (STV090x_WRITE_DEMOD(state, RTC, 0x80) < 0)
2089 goto err;
2091 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x40) < 0)
2092 goto err;
2093 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x00) < 0)
2094 goto err;
2096 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0) /* set car ofset to 0 */
2097 goto err;
2098 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
2099 goto err;
2100 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x65) < 0)
2101 goto err;
2103 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* trigger acquisition */
2104 goto err;
2105 msleep(10);
2107 for (i = 0; i < 10; i++) {
2108 reg = STV090x_READ_DEMOD(state, DSTATUS);
2109 if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
2110 tmg_cpt++;
2111 msleep(1);
2113 if (tmg_cpt >= 3)
2114 tmg_lock = 1;
2116 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
2117 goto err;
2118 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0) /* DVB-S1 timing */
2119 goto err;
2120 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0) /* DVB-S2 timing */
2121 goto err;
2123 if (STV090x_WRITE_DEMOD(state, CARFREQ, freq) < 0)
2124 goto err;
2125 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, tmg_thh) < 0)
2126 goto err;
2127 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, tmg_thl) < 0)
2128 goto err;
2130 return tmg_lock;
2132 err:
2133 dprintk(FE_ERROR, 1, "I/O error");
2134 return -1;
2137 static int stv090x_get_coldlock(struct stv090x_state *state, s32 timeout_dmd)
2139 struct dvb_frontend *fe = &state->frontend;
2141 u32 reg;
2142 s32 car_step, steps, cur_step, dir, freq, timeout_lock;
2143 int lock;
2145 if (state->srate >= 10000000)
2146 timeout_lock = timeout_dmd / 3;
2147 else
2148 timeout_lock = timeout_dmd / 2;
2150 lock = stv090x_get_dmdlock(state, timeout_lock); /* cold start wait */
2151 if (lock)
2152 return lock;
2154 if (state->srate >= 10000000) {
2155 if (stv090x_chk_tmg(state)) {
2156 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2157 goto err;
2158 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2159 goto err;
2160 return stv090x_get_dmdlock(state, timeout_dmd);
2162 return 0;
2165 if (state->srate <= 4000000)
2166 car_step = 1000;
2167 else if (state->srate <= 7000000)
2168 car_step = 2000;
2169 else if (state->srate <= 10000000)
2170 car_step = 3000;
2171 else
2172 car_step = 5000;
2174 steps = (state->search_range / 1000) / car_step;
2175 steps /= 2;
2176 steps = 2 * (steps + 1);
2177 if (steps < 0)
2178 steps = 2;
2179 else if (steps > 12)
2180 steps = 12;
2182 cur_step = 1;
2183 dir = 1;
2185 freq = state->frequency;
2186 state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + state->srate;
2187 while ((cur_step <= steps) && (!lock)) {
2188 if (dir > 0)
2189 freq += cur_step * car_step;
2190 else
2191 freq -= cur_step * car_step;
2193 /* Setup tuner */
2194 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
2195 goto err;
2197 if (state->config->tuner_set_frequency) {
2198 if (state->config->tuner_set_frequency(fe, freq) < 0)
2199 goto err_gateoff;
2202 if (state->config->tuner_set_bandwidth) {
2203 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
2204 goto err_gateoff;
2207 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
2208 goto err;
2210 msleep(50);
2212 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
2213 goto err;
2215 if (state->config->tuner_get_status) {
2216 if (state->config->tuner_get_status(fe, &reg) < 0)
2217 goto err_gateoff;
2220 if (reg)
2221 dprintk(FE_DEBUG, 1, "Tuner phase locked");
2222 else
2223 dprintk(FE_DEBUG, 1, "Tuner unlocked");
2225 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
2226 goto err;
2228 STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c);
2229 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
2230 goto err;
2231 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
2232 goto err;
2233 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2234 goto err;
2235 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2236 goto err;
2237 lock = stv090x_get_dmdlock(state, (timeout_dmd / 3));
2239 dir *= -1;
2240 cur_step++;
2243 return lock;
2245 err_gateoff:
2246 stv090x_i2c_gate_ctrl(state, 0);
2247 err:
2248 dprintk(FE_ERROR, 1, "I/O error");
2249 return -1;
2252 static int stv090x_get_loop_params(struct stv090x_state *state, s32 *freq_inc, s32 *timeout_sw, s32 *steps)
2254 s32 timeout, inc, steps_max, srate, car_max;
2256 srate = state->srate;
2257 car_max = state->search_range / 1000;
2258 car_max += car_max / 10;
2259 car_max = 65536 * (car_max / 2);
2260 car_max /= (state->internal->mclk / 1000);
2262 if (car_max > 0x4000)
2263 car_max = 0x4000 ; /* maxcarrier should be<= +-1/4 Mclk */
2265 inc = srate;
2266 inc /= state->internal->mclk / 1000;
2267 inc *= 256;
2268 inc *= 256;
2269 inc /= 1000;
2271 switch (state->search_mode) {
2272 case STV090x_SEARCH_DVBS1:
2273 case STV090x_SEARCH_DSS:
2274 inc *= 3; /* freq step = 3% of srate */
2275 timeout = 20;
2276 break;
2278 case STV090x_SEARCH_DVBS2:
2279 inc *= 4;
2280 timeout = 25;
2281 break;
2283 case STV090x_SEARCH_AUTO:
2284 default:
2285 inc *= 3;
2286 timeout = 25;
2287 break;
2289 inc /= 100;
2290 if ((inc > car_max) || (inc < 0))
2291 inc = car_max / 2; /* increment <= 1/8 Mclk */
2293 timeout *= 27500; /* 27.5 Msps reference */
2294 if (srate > 0)
2295 timeout /= (srate / 1000);
2297 if ((timeout > 100) || (timeout < 0))
2298 timeout = 100;
2300 steps_max = (car_max / inc) + 1; /* min steps = 3 */
2301 if ((steps_max > 100) || (steps_max < 0)) {
2302 steps_max = 100; /* max steps <= 100 */
2303 inc = car_max / steps_max;
2305 *freq_inc = inc;
2306 *timeout_sw = timeout;
2307 *steps = steps_max;
2309 return 0;
2312 static int stv090x_chk_signal(struct stv090x_state *state)
2314 s32 offst_car, agc2, car_max;
2315 int no_signal;
2317 offst_car = STV090x_READ_DEMOD(state, CFR2) << 8;
2318 offst_car |= STV090x_READ_DEMOD(state, CFR1);
2319 offst_car = comp2(offst_car, 16);
2321 agc2 = STV090x_READ_DEMOD(state, AGC2I1) << 8;
2322 agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
2323 car_max = state->search_range / 1000;
2325 car_max += (car_max / 10); /* 10% margin */
2326 car_max = (65536 * car_max / 2);
2327 car_max /= state->internal->mclk / 1000;
2329 if (car_max > 0x4000)
2330 car_max = 0x4000;
2332 if ((agc2 > 0x2000) || (offst_car > 2 * car_max) || (offst_car < -2 * car_max)) {
2333 no_signal = 1;
2334 dprintk(FE_DEBUG, 1, "No Signal");
2335 } else {
2336 no_signal = 0;
2337 dprintk(FE_DEBUG, 1, "Found Signal");
2340 return no_signal;
2343 static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 timeout, int zigzag, s32 steps_max)
2345 int no_signal, lock = 0;
2346 s32 cpt_step = 0, offst_freq, car_max;
2347 u32 reg;
2349 car_max = state->search_range / 1000;
2350 car_max += (car_max / 10);
2351 car_max = (65536 * car_max / 2);
2352 car_max /= (state->internal->mclk / 1000);
2353 if (car_max > 0x4000)
2354 car_max = 0x4000;
2356 if (zigzag)
2357 offst_freq = 0;
2358 else
2359 offst_freq = -car_max + inc;
2361 do {
2362 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0)
2363 goto err;
2364 if (STV090x_WRITE_DEMOD(state, CFRINIT1, ((offst_freq / 256) & 0xff)) < 0)
2365 goto err;
2366 if (STV090x_WRITE_DEMOD(state, CFRINIT0, offst_freq & 0xff) < 0)
2367 goto err;
2368 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
2369 goto err;
2371 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
2372 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x1); /* stop DVB-S2 packet delin */
2373 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
2374 goto err;
2376 if (zigzag) {
2377 if (offst_freq >= 0)
2378 offst_freq = -offst_freq - 2 * inc;
2379 else
2380 offst_freq = -offst_freq;
2381 } else {
2382 offst_freq += 2 * inc;
2385 cpt_step++;
2387 lock = stv090x_get_dmdlock(state, timeout);
2388 no_signal = stv090x_chk_signal(state);
2390 } while ((!lock) &&
2391 (!no_signal) &&
2392 ((offst_freq - inc) < car_max) &&
2393 ((offst_freq + inc) > -car_max) &&
2394 (cpt_step < steps_max));
2396 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
2397 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0);
2398 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
2399 goto err;
2401 return lock;
2402 err:
2403 dprintk(FE_ERROR, 1, "I/O error");
2404 return -1;
2407 static int stv090x_sw_algo(struct stv090x_state *state)
2409 int no_signal, zigzag, lock = 0;
2410 u32 reg;
2412 s32 dvbs2_fly_wheel;
2413 s32 inc, timeout_step, trials, steps_max;
2415 /* get params */
2416 stv090x_get_loop_params(state, &inc, &timeout_step, &steps_max);
2418 switch (state->search_mode) {
2419 case STV090x_SEARCH_DVBS1:
2420 case STV090x_SEARCH_DSS:
2421 /* accelerate the frequency detector */
2422 if (state->internal->dev_ver >= 0x20) {
2423 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3B) < 0)
2424 goto err;
2427 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x49) < 0)
2428 goto err;
2429 zigzag = 0;
2430 break;
2432 case STV090x_SEARCH_DVBS2:
2433 if (state->internal->dev_ver >= 0x20) {
2434 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2435 goto err;
2438 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2439 goto err;
2440 zigzag = 1;
2441 break;
2443 case STV090x_SEARCH_AUTO:
2444 default:
2445 /* accelerate the frequency detector */
2446 if (state->internal->dev_ver >= 0x20) {
2447 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3b) < 0)
2448 goto err;
2449 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2450 goto err;
2453 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0xc9) < 0)
2454 goto err;
2455 zigzag = 0;
2456 break;
2459 trials = 0;
2460 do {
2461 lock = stv090x_search_car_loop(state, inc, timeout_step, zigzag, steps_max);
2462 no_signal = stv090x_chk_signal(state);
2463 trials++;
2465 /*run the SW search 2 times maximum*/
2466 if (lock || no_signal || (trials == 2)) {
2467 /*Check if the demod is not losing lock in DVBS2*/
2468 if (state->internal->dev_ver >= 0x20) {
2469 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
2470 goto err;
2471 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
2472 goto err;
2475 reg = STV090x_READ_DEMOD(state, DMDSTATE);
2476 if ((lock) && (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == STV090x_DVBS2)) {
2477 /*Check if the demod is not losing lock in DVBS2*/
2478 msleep(timeout_step);
2479 reg = STV090x_READ_DEMOD(state, DMDFLYW);
2480 dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
2481 if (dvbs2_fly_wheel < 0xd) { /*if correct frames is decrementing */
2482 msleep(timeout_step);
2483 reg = STV090x_READ_DEMOD(state, DMDFLYW);
2484 dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
2486 if (dvbs2_fly_wheel < 0xd) {
2487 /*FALSE lock, The demod is losing lock */
2488 lock = 0;
2489 if (trials < 2) {
2490 if (state->internal->dev_ver >= 0x20) {
2491 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2492 goto err;
2495 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2496 goto err;
2501 } while ((!lock) && (trials < 2) && (!no_signal));
2503 return lock;
2504 err:
2505 dprintk(FE_ERROR, 1, "I/O error");
2506 return -1;
2509 static enum stv090x_delsys stv090x_get_std(struct stv090x_state *state)
2511 u32 reg;
2512 enum stv090x_delsys delsys;
2514 reg = STV090x_READ_DEMOD(state, DMDSTATE);
2515 if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 2)
2516 delsys = STV090x_DVBS2;
2517 else if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 3) {
2518 reg = STV090x_READ_DEMOD(state, FECM);
2519 if (STV090x_GETFIELD_Px(reg, DSS_DVB_FIELD) == 1)
2520 delsys = STV090x_DSS;
2521 else
2522 delsys = STV090x_DVBS1;
2523 } else {
2524 delsys = STV090x_ERROR;
2527 return delsys;
2530 /* in Hz */
2531 static s32 stv090x_get_car_freq(struct stv090x_state *state, u32 mclk)
2533 s32 derot, int_1, int_2, tmp_1, tmp_2;
2535 derot = STV090x_READ_DEMOD(state, CFR2) << 16;
2536 derot |= STV090x_READ_DEMOD(state, CFR1) << 8;
2537 derot |= STV090x_READ_DEMOD(state, CFR0);
2539 derot = comp2(derot, 24);
2540 int_1 = mclk >> 12;
2541 int_2 = derot >> 12;
2543 /* carrier_frequency = MasterClock * Reg / 2^24 */
2544 tmp_1 = mclk % 0x1000;
2545 tmp_2 = derot % 0x1000;
2547 derot = (int_1 * int_2) +
2548 ((int_1 * tmp_2) >> 12) +
2549 ((int_2 * tmp_1) >> 12);
2551 return derot;
2554 static int stv090x_get_viterbi(struct stv090x_state *state)
2556 u32 reg, rate;
2558 reg = STV090x_READ_DEMOD(state, VITCURPUN);
2559 rate = STV090x_GETFIELD_Px(reg, VIT_CURPUN_FIELD);
2561 switch (rate) {
2562 case 13:
2563 state->fec = STV090x_PR12;
2564 break;
2566 case 18:
2567 state->fec = STV090x_PR23;
2568 break;
2570 case 21:
2571 state->fec = STV090x_PR34;
2572 break;
2574 case 24:
2575 state->fec = STV090x_PR56;
2576 break;
2578 case 25:
2579 state->fec = STV090x_PR67;
2580 break;
2582 case 26:
2583 state->fec = STV090x_PR78;
2584 break;
2586 default:
2587 state->fec = STV090x_PRERR;
2588 break;
2591 return 0;
2594 static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *state)
2596 struct dvb_frontend *fe = &state->frontend;
2598 u8 tmg;
2599 u32 reg;
2600 s32 i = 0, offst_freq;
2602 msleep(5);
2604 if (state->algo == STV090x_BLIND_SEARCH) {
2605 tmg = STV090x_READ_DEMOD(state, TMGREG2);
2606 STV090x_WRITE_DEMOD(state, SFRSTEP, 0x5c);
2607 while ((i <= 50) && (tmg != 0) && (tmg != 0xff)) {
2608 tmg = STV090x_READ_DEMOD(state, TMGREG2);
2609 msleep(5);
2610 i += 5;
2613 state->delsys = stv090x_get_std(state);
2615 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
2616 goto err;
2618 if (state->config->tuner_get_frequency) {
2619 if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
2620 goto err_gateoff;
2623 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
2624 goto err;
2626 offst_freq = stv090x_get_car_freq(state, state->internal->mclk) / 1000;
2627 state->frequency += offst_freq;
2629 if (stv090x_get_viterbi(state) < 0)
2630 goto err;
2632 reg = STV090x_READ_DEMOD(state, DMDMODCOD);
2633 state->modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
2634 state->pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
2635 state->frame_len = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) >> 1;
2636 reg = STV090x_READ_DEMOD(state, TMGOBS);
2637 state->rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
2638 reg = STV090x_READ_DEMOD(state, FECM);
2639 state->inversion = STV090x_GETFIELD_Px(reg, IQINV_FIELD);
2641 if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000)) {
2643 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
2644 goto err;
2646 if (state->config->tuner_get_frequency) {
2647 if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
2648 goto err_gateoff;
2651 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
2652 goto err;
2654 if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
2655 return STV090x_RANGEOK;
2656 else if (abs(offst_freq) <= (stv090x_car_width(state->srate, state->rolloff) / 2000))
2657 return STV090x_RANGEOK;
2658 } else {
2659 if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
2660 return STV090x_RANGEOK;
2663 return STV090x_OUTOFRANGE;
2665 err_gateoff:
2666 stv090x_i2c_gate_ctrl(state, 0);
2667 err:
2668 dprintk(FE_ERROR, 1, "I/O error");
2669 return -1;
2672 static u32 stv090x_get_tmgoffst(struct stv090x_state *state, u32 srate)
2674 s32 offst_tmg;
2676 offst_tmg = STV090x_READ_DEMOD(state, TMGREG2) << 16;
2677 offst_tmg |= STV090x_READ_DEMOD(state, TMGREG1) << 8;
2678 offst_tmg |= STV090x_READ_DEMOD(state, TMGREG0);
2680 offst_tmg = comp2(offst_tmg, 24); /* 2's complement */
2681 if (!offst_tmg)
2682 offst_tmg = 1;
2684 offst_tmg = ((s32) srate * 10) / ((s32) 0x1000000 / offst_tmg);
2685 offst_tmg /= 320;
2687 return offst_tmg;
2690 static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_modcod modcod, s32 pilots)
2692 u8 aclc = 0x29;
2693 s32 i;
2694 struct stv090x_long_frame_crloop *car_loop, *car_loop_qpsk_low, *car_loop_apsk_low;
2696 if (state->internal->dev_ver == 0x20) {
2697 car_loop = stv090x_s2_crl_cut20;
2698 car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut20;
2699 car_loop_apsk_low = stv090x_s2_apsk_crl_cut20;
2700 } else {
2701 /* >= Cut 3 */
2702 car_loop = stv090x_s2_crl_cut30;
2703 car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut30;
2704 car_loop_apsk_low = stv090x_s2_apsk_crl_cut30;
2707 if (modcod < STV090x_QPSK_12) {
2708 i = 0;
2709 while ((i < 3) && (modcod != car_loop_qpsk_low[i].modcod))
2710 i++;
2712 if (i >= 3)
2713 i = 2;
2715 } else {
2716 i = 0;
2717 while ((i < 14) && (modcod != car_loop[i].modcod))
2718 i++;
2720 if (i >= 14) {
2721 i = 0;
2722 while ((i < 11) && (modcod != car_loop_apsk_low[i].modcod))
2723 i++;
2725 if (i >= 11)
2726 i = 10;
2730 if (modcod <= STV090x_QPSK_25) {
2731 if (pilots) {
2732 if (state->srate <= 3000000)
2733 aclc = car_loop_qpsk_low[i].crl_pilots_on_2;
2734 else if (state->srate <= 7000000)
2735 aclc = car_loop_qpsk_low[i].crl_pilots_on_5;
2736 else if (state->srate <= 15000000)
2737 aclc = car_loop_qpsk_low[i].crl_pilots_on_10;
2738 else if (state->srate <= 25000000)
2739 aclc = car_loop_qpsk_low[i].crl_pilots_on_20;
2740 else
2741 aclc = car_loop_qpsk_low[i].crl_pilots_on_30;
2742 } else {
2743 if (state->srate <= 3000000)
2744 aclc = car_loop_qpsk_low[i].crl_pilots_off_2;
2745 else if (state->srate <= 7000000)
2746 aclc = car_loop_qpsk_low[i].crl_pilots_off_5;
2747 else if (state->srate <= 15000000)
2748 aclc = car_loop_qpsk_low[i].crl_pilots_off_10;
2749 else if (state->srate <= 25000000)
2750 aclc = car_loop_qpsk_low[i].crl_pilots_off_20;
2751 else
2752 aclc = car_loop_qpsk_low[i].crl_pilots_off_30;
2755 } else if (modcod <= STV090x_8PSK_910) {
2756 if (pilots) {
2757 if (state->srate <= 3000000)
2758 aclc = car_loop[i].crl_pilots_on_2;
2759 else if (state->srate <= 7000000)
2760 aclc = car_loop[i].crl_pilots_on_5;
2761 else if (state->srate <= 15000000)
2762 aclc = car_loop[i].crl_pilots_on_10;
2763 else if (state->srate <= 25000000)
2764 aclc = car_loop[i].crl_pilots_on_20;
2765 else
2766 aclc = car_loop[i].crl_pilots_on_30;
2767 } else {
2768 if (state->srate <= 3000000)
2769 aclc = car_loop[i].crl_pilots_off_2;
2770 else if (state->srate <= 7000000)
2771 aclc = car_loop[i].crl_pilots_off_5;
2772 else if (state->srate <= 15000000)
2773 aclc = car_loop[i].crl_pilots_off_10;
2774 else if (state->srate <= 25000000)
2775 aclc = car_loop[i].crl_pilots_off_20;
2776 else
2777 aclc = car_loop[i].crl_pilots_off_30;
2779 } else { /* 16APSK and 32APSK */
2781 * This should never happen in practice, except if
2782 * something is really wrong at the car_loop table.
2784 if (i >= 11)
2785 i = 10;
2786 if (state->srate <= 3000000)
2787 aclc = car_loop_apsk_low[i].crl_pilots_on_2;
2788 else if (state->srate <= 7000000)
2789 aclc = car_loop_apsk_low[i].crl_pilots_on_5;
2790 else if (state->srate <= 15000000)
2791 aclc = car_loop_apsk_low[i].crl_pilots_on_10;
2792 else if (state->srate <= 25000000)
2793 aclc = car_loop_apsk_low[i].crl_pilots_on_20;
2794 else
2795 aclc = car_loop_apsk_low[i].crl_pilots_on_30;
2798 return aclc;
2801 static u8 stv090x_optimize_carloop_short(struct stv090x_state *state)
2803 struct stv090x_short_frame_crloop *short_crl = NULL;
2804 s32 index = 0;
2805 u8 aclc = 0x0b;
2807 switch (state->modulation) {
2808 case STV090x_QPSK:
2809 default:
2810 index = 0;
2811 break;
2812 case STV090x_8PSK:
2813 index = 1;
2814 break;
2815 case STV090x_16APSK:
2816 index = 2;
2817 break;
2818 case STV090x_32APSK:
2819 index = 3;
2820 break;
2823 if (state->internal->dev_ver >= 0x30) {
2824 /* Cut 3.0 and up */
2825 short_crl = stv090x_s2_short_crl_cut30;
2826 } else {
2827 /* Cut 2.0 and up: we don't support cuts older than 2.0 */
2828 short_crl = stv090x_s2_short_crl_cut20;
2831 if (state->srate <= 3000000)
2832 aclc = short_crl[index].crl_2;
2833 else if (state->srate <= 7000000)
2834 aclc = short_crl[index].crl_5;
2835 else if (state->srate <= 15000000)
2836 aclc = short_crl[index].crl_10;
2837 else if (state->srate <= 25000000)
2838 aclc = short_crl[index].crl_20;
2839 else
2840 aclc = short_crl[index].crl_30;
2842 return aclc;
2845 static int stv090x_optimize_track(struct stv090x_state *state)
2847 struct dvb_frontend *fe = &state->frontend;
2849 enum stv090x_modcod modcod;
2851 s32 srate, pilots, aclc, f_1, f_0, i = 0, blind_tune = 0;
2852 u32 reg;
2854 srate = stv090x_get_srate(state, state->internal->mclk);
2855 srate += stv090x_get_tmgoffst(state, srate);
2857 switch (state->delsys) {
2858 case STV090x_DVBS1:
2859 case STV090x_DSS:
2860 if (state->search_mode == STV090x_SEARCH_AUTO) {
2861 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2862 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
2863 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
2864 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2865 goto err;
2867 reg = STV090x_READ_DEMOD(state, DEMOD);
2868 STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
2869 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x01);
2870 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
2871 goto err;
2873 if (state->internal->dev_ver >= 0x30) {
2874 if (stv090x_get_viterbi(state) < 0)
2875 goto err;
2877 if (state->fec == STV090x_PR12) {
2878 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x98) < 0)
2879 goto err;
2880 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2881 goto err;
2882 } else {
2883 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x18) < 0)
2884 goto err;
2885 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2886 goto err;
2890 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
2891 goto err;
2892 break;
2894 case STV090x_DVBS2:
2895 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2896 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
2897 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
2898 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2899 goto err;
2900 if (state->internal->dev_ver >= 0x30) {
2901 if (STV090x_WRITE_DEMOD(state, ACLC, 0) < 0)
2902 goto err;
2903 if (STV090x_WRITE_DEMOD(state, BCLC, 0) < 0)
2904 goto err;
2906 if (state->frame_len == STV090x_LONG_FRAME) {
2907 reg = STV090x_READ_DEMOD(state, DMDMODCOD);
2908 modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
2909 pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
2910 aclc = stv090x_optimize_carloop(state, modcod, pilots);
2911 if (modcod <= STV090x_QPSK_910) {
2912 STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc);
2913 } else if (modcod <= STV090x_8PSK_910) {
2914 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2915 goto err;
2916 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
2917 goto err;
2919 if ((state->demod_mode == STV090x_SINGLE) && (modcod > STV090x_8PSK_910)) {
2920 if (modcod <= STV090x_16APSK_910) {
2921 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2922 goto err;
2923 if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2924 goto err;
2925 } else {
2926 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2927 goto err;
2928 if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
2929 goto err;
2932 } else {
2933 /*Carrier loop setting for short frame*/
2934 aclc = stv090x_optimize_carloop_short(state);
2935 if (state->modulation == STV090x_QPSK) {
2936 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc) < 0)
2937 goto err;
2938 } else if (state->modulation == STV090x_8PSK) {
2939 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2940 goto err;
2941 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
2942 goto err;
2943 } else if (state->modulation == STV090x_16APSK) {
2944 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2945 goto err;
2946 if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2947 goto err;
2948 } else if (state->modulation == STV090x_32APSK) {
2949 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2950 goto err;
2951 if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
2952 goto err;
2956 STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67); /* PER */
2957 break;
2959 case STV090x_ERROR:
2960 default:
2961 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2962 STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
2963 STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
2964 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2965 goto err;
2966 break;
2969 f_1 = STV090x_READ_DEMOD(state, CFR2);
2970 f_0 = STV090x_READ_DEMOD(state, CFR1);
2971 reg = STV090x_READ_DEMOD(state, TMGOBS);
2973 if (state->algo == STV090x_BLIND_SEARCH) {
2974 STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00);
2975 reg = STV090x_READ_DEMOD(state, DMDCFGMD);
2976 STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0x00);
2977 STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
2978 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
2979 goto err;
2980 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
2981 goto err;
2983 if (stv090x_set_srate(state, srate) < 0)
2984 goto err;
2985 blind_tune = 1;
2987 if (stv090x_dvbs_track_crl(state) < 0)
2988 goto err;
2991 if (state->internal->dev_ver >= 0x20) {
2992 if ((state->search_mode == STV090x_SEARCH_DVBS1) ||
2993 (state->search_mode == STV090x_SEARCH_DSS) ||
2994 (state->search_mode == STV090x_SEARCH_AUTO)) {
2996 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x0a) < 0)
2997 goto err;
2998 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x00) < 0)
2999 goto err;
3003 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
3004 goto err;
3006 /* AUTO tracking MODE */
3007 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x80) < 0)
3008 goto err;
3009 /* AUTO tracking MODE */
3010 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x80) < 0)
3011 goto err;
3013 if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1) ||
3014 (state->srate < 10000000)) {
3015 /* update initial carrier freq with the found freq offset */
3016 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
3017 goto err;
3018 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
3019 goto err;
3020 state->tuner_bw = stv090x_car_width(srate, state->rolloff) + 10000000;
3022 if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1)) {
3024 if (state->algo != STV090x_WARM_SEARCH) {
3026 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
3027 goto err;
3029 if (state->config->tuner_set_bandwidth) {
3030 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
3031 goto err_gateoff;
3034 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
3035 goto err;
3039 if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000))
3040 msleep(50); /* blind search: wait 50ms for SR stabilization */
3041 else
3042 msleep(5);
3044 stv090x_get_lock_tmg(state);
3046 if (!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) {
3047 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
3048 goto err;
3049 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
3050 goto err;
3051 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
3052 goto err;
3053 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
3054 goto err;
3056 i = 0;
3058 while ((!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) && (i <= 2)) {
3060 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
3061 goto err;
3062 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
3063 goto err;
3064 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
3065 goto err;
3066 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
3067 goto err;
3068 i++;
3074 if (state->internal->dev_ver >= 0x20) {
3075 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
3076 goto err;
3079 if ((state->delsys == STV090x_DVBS1) || (state->delsys == STV090x_DSS))
3080 stv090x_set_vit_thtracq(state);
3082 return 0;
3084 err_gateoff:
3085 stv090x_i2c_gate_ctrl(state, 0);
3086 err:
3087 dprintk(FE_ERROR, 1, "I/O error");
3088 return -1;
3091 static int stv090x_get_feclock(struct stv090x_state *state, s32 timeout)
3093 s32 timer = 0, lock = 0, stat;
3094 u32 reg;
3096 while ((timer < timeout) && (!lock)) {
3097 reg = STV090x_READ_DEMOD(state, DMDSTATE);
3098 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
3100 switch (stat) {
3101 case 0: /* searching */
3102 case 1: /* first PLH detected */
3103 default:
3104 lock = 0;
3105 break;
3107 case 2: /* DVB-S2 mode */
3108 reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
3109 lock = STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD);
3110 break;
3112 case 3: /* DVB-S1/legacy mode */
3113 reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
3114 lock = STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD);
3115 break;
3117 if (!lock) {
3118 msleep(10);
3119 timer += 10;
3122 return lock;
3125 static int stv090x_get_lock(struct stv090x_state *state, s32 timeout_dmd, s32 timeout_fec)
3127 u32 reg;
3128 s32 timer = 0;
3129 int lock;
3131 lock = stv090x_get_dmdlock(state, timeout_dmd);
3132 if (lock)
3133 lock = stv090x_get_feclock(state, timeout_fec);
3135 if (lock) {
3136 lock = 0;
3138 while ((timer < timeout_fec) && (!lock)) {
3139 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3140 lock = STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD);
3141 msleep(1);
3142 timer++;
3146 return lock;
3149 static int stv090x_set_s2rolloff(struct stv090x_state *state)
3151 u32 reg;
3153 if (state->internal->dev_ver <= 0x20) {
3154 /* rolloff to auto mode if DVBS2 */
3155 reg = STV090x_READ_DEMOD(state, DEMOD);
3156 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x00);
3157 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3158 goto err;
3159 } else {
3160 /* DVB-S2 rolloff to auto mode if DVBS2 */
3161 reg = STV090x_READ_DEMOD(state, DEMOD);
3162 STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 0x00);
3163 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3164 goto err;
3166 return 0;
3167 err:
3168 dprintk(FE_ERROR, 1, "I/O error");
3169 return -1;
3173 static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
3175 struct dvb_frontend *fe = &state->frontend;
3176 enum stv090x_signal_state signal_state = STV090x_NOCARRIER;
3177 u32 reg;
3178 s32 agc1_power, power_iq = 0, i;
3179 int lock = 0, low_sr = 0;
3181 reg = STV090x_READ_DEMOD(state, TSCFGH);
3182 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* Stop path 1 stream merger */
3183 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3184 goto err;
3186 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod stop */
3187 goto err;
3189 if (state->internal->dev_ver >= 0x20) {
3190 if (state->srate > 5000000) {
3191 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
3192 goto err;
3193 } else {
3194 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x82) < 0)
3195 goto err;
3199 stv090x_get_lock_tmg(state);
3201 if (state->algo == STV090x_BLIND_SEARCH) {
3202 state->tuner_bw = 2 * 36000000; /* wide bw for unknown srate */
3203 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0) /* wider srate scan */
3204 goto err;
3205 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
3206 goto err;
3207 if (stv090x_set_srate(state, 1000000) < 0) /* initial srate = 1Msps */
3208 goto err;
3209 } else {
3210 /* known srate */
3211 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
3212 goto err;
3213 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
3214 goto err;
3216 if (state->srate < 2000000) {
3217 /* SR < 2MSPS */
3218 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x63) < 0)
3219 goto err;
3220 } else {
3221 /* SR >= 2Msps */
3222 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
3223 goto err;
3226 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
3227 goto err;
3229 if (state->internal->dev_ver >= 0x20) {
3230 if (STV090x_WRITE_DEMOD(state, KREFTMG, 0x5a) < 0)
3231 goto err;
3232 if (state->algo == STV090x_COLD_SEARCH)
3233 state->tuner_bw = (15 * (stv090x_car_width(state->srate, state->rolloff) + 10000000)) / 10;
3234 else if (state->algo == STV090x_WARM_SEARCH)
3235 state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + 10000000;
3238 /* if cold start or warm (Symbolrate is known)
3239 * use a Narrow symbol rate scan range
3241 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0) /* narrow srate scan */
3242 goto err;
3244 if (stv090x_set_srate(state, state->srate) < 0)
3245 goto err;
3247 if (stv090x_set_max_srate(state, state->internal->mclk,
3248 state->srate) < 0)
3249 goto err;
3250 if (stv090x_set_min_srate(state, state->internal->mclk,
3251 state->srate) < 0)
3252 goto err;
3254 if (state->srate >= 10000000)
3255 low_sr = 0;
3256 else
3257 low_sr = 1;
3260 /* Setup tuner */
3261 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
3262 goto err;
3264 if (state->config->tuner_set_bbgain) {
3265 reg = state->config->tuner_bbgain;
3266 if (reg == 0)
3267 reg = 10; /* default: 10dB */
3268 if (state->config->tuner_set_bbgain(fe, reg) < 0)
3269 goto err_gateoff;
3272 if (state->config->tuner_set_frequency) {
3273 if (state->config->tuner_set_frequency(fe, state->frequency) < 0)
3274 goto err_gateoff;
3277 if (state->config->tuner_set_bandwidth) {
3278 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
3279 goto err_gateoff;
3282 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
3283 goto err;
3285 msleep(50);
3287 if (state->config->tuner_get_status) {
3288 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
3289 goto err;
3290 if (state->config->tuner_get_status(fe, &reg) < 0)
3291 goto err_gateoff;
3292 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
3293 goto err;
3295 if (reg)
3296 dprintk(FE_DEBUG, 1, "Tuner phase locked");
3297 else {
3298 dprintk(FE_DEBUG, 1, "Tuner unlocked");
3299 return STV090x_NOCARRIER;
3303 msleep(10);
3304 agc1_power = MAKEWORD16(STV090x_READ_DEMOD(state, AGCIQIN1),
3305 STV090x_READ_DEMOD(state, AGCIQIN0));
3307 if (agc1_power == 0) {
3308 /* If AGC1 integrator value is 0
3309 * then read POWERI, POWERQ
3311 for (i = 0; i < 5; i++) {
3312 power_iq += (STV090x_READ_DEMOD(state, POWERI) +
3313 STV090x_READ_DEMOD(state, POWERQ)) >> 1;
3315 power_iq /= 5;
3318 if ((agc1_power == 0) && (power_iq < STV090x_IQPOWER_THRESHOLD)) {
3319 dprintk(FE_ERROR, 1, "No Signal: POWER_IQ=0x%02x", power_iq);
3320 lock = 0;
3321 signal_state = STV090x_NOAGC1;
3322 } else {
3323 reg = STV090x_READ_DEMOD(state, DEMOD);
3324 STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, state->inversion);
3326 if (state->internal->dev_ver <= 0x20) {
3327 /* rolloff to auto mode if DVBS2 */
3328 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 1);
3329 } else {
3330 /* DVB-S2 rolloff to auto mode if DVBS2 */
3331 STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 1);
3333 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
3334 goto err;
3336 if (stv090x_delivery_search(state) < 0)
3337 goto err;
3339 if (state->algo != STV090x_BLIND_SEARCH) {
3340 if (stv090x_start_search(state) < 0)
3341 goto err;
3345 if (signal_state == STV090x_NOAGC1)
3346 return signal_state;
3348 if (state->algo == STV090x_BLIND_SEARCH)
3349 lock = stv090x_blind_search(state);
3351 else if (state->algo == STV090x_COLD_SEARCH)
3352 lock = stv090x_get_coldlock(state, state->DemodTimeout);
3354 else if (state->algo == STV090x_WARM_SEARCH)
3355 lock = stv090x_get_dmdlock(state, state->DemodTimeout);
3357 if ((!lock) && (state->algo == STV090x_COLD_SEARCH)) {
3358 if (!low_sr) {
3359 if (stv090x_chk_tmg(state))
3360 lock = stv090x_sw_algo(state);
3364 if (lock)
3365 signal_state = stv090x_get_sig_params(state);
3367 if ((lock) && (signal_state == STV090x_RANGEOK)) { /* signal within Range */
3368 stv090x_optimize_track(state);
3370 if (state->internal->dev_ver >= 0x20) {
3371 /* >= Cut 2.0 :release TS reset after
3372 * demod lock and optimized Tracking
3374 reg = STV090x_READ_DEMOD(state, TSCFGH);
3375 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
3376 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3377 goto err;
3379 msleep(3);
3381 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
3382 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3383 goto err;
3385 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
3386 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3387 goto err;
3390 lock = stv090x_get_lock(state, state->FecTimeout,
3391 state->FecTimeout);
3392 if (lock) {
3393 if (state->delsys == STV090x_DVBS2) {
3394 stv090x_set_s2rolloff(state);
3396 reg = STV090x_READ_DEMOD(state, PDELCTRL2);
3397 STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 1);
3398 if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
3399 goto err;
3400 /* Reset DVBS2 packet delinator error counter */
3401 reg = STV090x_READ_DEMOD(state, PDELCTRL2);
3402 STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 0);
3403 if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
3404 goto err;
3406 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67) < 0) /* PER */
3407 goto err;
3408 } else {
3409 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
3410 goto err;
3412 /* Reset the Total packet counter */
3413 if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0x00) < 0)
3414 goto err;
3415 /* Reset the packet Error counter2 */
3416 if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3417 goto err;
3418 } else {
3419 signal_state = STV090x_NODATA;
3420 stv090x_chk_signal(state);
3423 return signal_state;
3425 err_gateoff:
3426 stv090x_i2c_gate_ctrl(state, 0);
3427 err:
3428 dprintk(FE_ERROR, 1, "I/O error");
3429 return -1;
3432 static int stv090x_set_mis(struct stv090x_state *state, int mis)
3434 u32 reg;
3436 if (mis < 0 || mis > 255) {
3437 dprintk(FE_DEBUG, 1, "Disable MIS filtering");
3438 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
3439 STV090x_SETFIELD_Px(reg, FILTER_EN_FIELD, 0x00);
3440 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
3441 goto err;
3442 } else {
3443 dprintk(FE_DEBUG, 1, "Enable MIS filtering - %d", mis);
3444 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
3445 STV090x_SETFIELD_Px(reg, FILTER_EN_FIELD, 0x01);
3446 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
3447 goto err;
3448 if (STV090x_WRITE_DEMOD(state, ISIENTRY, mis) < 0)
3449 goto err;
3450 if (STV090x_WRITE_DEMOD(state, ISIBITENA, 0xff) < 0)
3451 goto err;
3453 return 0;
3454 err:
3455 dprintk(FE_ERROR, 1, "I/O error");
3456 return -1;
3459 static enum dvbfe_search stv090x_search(struct dvb_frontend *fe)
3461 struct stv090x_state *state = fe->demodulator_priv;
3462 struct dtv_frontend_properties *props = &fe->dtv_property_cache;
3464 if (props->frequency == 0)
3465 return DVBFE_ALGO_SEARCH_INVALID;
3467 switch (props->delivery_system) {
3468 case SYS_DSS:
3469 state->delsys = STV090x_DSS;
3470 break;
3471 case SYS_DVBS:
3472 state->delsys = STV090x_DVBS1;
3473 break;
3474 case SYS_DVBS2:
3475 state->delsys = STV090x_DVBS2;
3476 break;
3477 default:
3478 return DVBFE_ALGO_SEARCH_INVALID;
3481 state->frequency = props->frequency;
3482 state->srate = props->symbol_rate;
3483 state->search_mode = STV090x_SEARCH_AUTO;
3484 state->algo = STV090x_COLD_SEARCH;
3485 state->fec = STV090x_PRERR;
3486 if (state->srate > 10000000) {
3487 dprintk(FE_DEBUG, 1, "Search range: 10 MHz");
3488 state->search_range = 10000000;
3489 } else {
3490 dprintk(FE_DEBUG, 1, "Search range: 5 MHz");
3491 state->search_range = 5000000;
3494 stv090x_set_mis(state, props->stream_id);
3496 if (stv090x_algo(state) == STV090x_RANGEOK) {
3497 dprintk(FE_DEBUG, 1, "Search success!");
3498 return DVBFE_ALGO_SEARCH_SUCCESS;
3499 } else {
3500 dprintk(FE_DEBUG, 1, "Search failed!");
3501 return DVBFE_ALGO_SEARCH_FAILED;
3504 return DVBFE_ALGO_SEARCH_ERROR;
3507 static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
3509 struct stv090x_state *state = fe->demodulator_priv;
3510 u32 reg, dstatus;
3511 u8 search_state;
3513 *status = 0;
3515 dstatus = STV090x_READ_DEMOD(state, DSTATUS);
3516 if (STV090x_GETFIELD_Px(dstatus, CAR_LOCK_FIELD))
3517 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
3519 reg = STV090x_READ_DEMOD(state, DMDSTATE);
3520 search_state = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
3522 switch (search_state) {
3523 case 0: /* searching */
3524 case 1: /* first PLH detected */
3525 default:
3526 dprintk(FE_DEBUG, 1, "Status: Unlocked (Searching ..)");
3527 break;
3529 case 2: /* DVB-S2 mode */
3530 dprintk(FE_DEBUG, 1, "Delivery system: DVB-S2");
3531 if (STV090x_GETFIELD_Px(dstatus, LOCK_DEFINITIF_FIELD)) {
3532 reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
3533 if (STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD)) {
3534 *status |= FE_HAS_VITERBI;
3535 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3536 if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD))
3537 *status |= FE_HAS_SYNC | FE_HAS_LOCK;
3540 break;
3542 case 3: /* DVB-S1/legacy mode */
3543 dprintk(FE_DEBUG, 1, "Delivery system: DVB-S");
3544 if (STV090x_GETFIELD_Px(dstatus, LOCK_DEFINITIF_FIELD)) {
3545 reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
3546 if (STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD)) {
3547 *status |= FE_HAS_VITERBI;
3548 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3549 if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD))
3550 *status |= FE_HAS_SYNC | FE_HAS_LOCK;
3553 break;
3556 return 0;
3559 static int stv090x_read_per(struct dvb_frontend *fe, u32 *per)
3561 struct stv090x_state *state = fe->demodulator_priv;
3563 s32 count_4, count_3, count_2, count_1, count_0, count;
3564 u32 reg, h, m, l;
3565 enum fe_status status;
3567 stv090x_read_status(fe, &status);
3568 if (!(status & FE_HAS_LOCK)) {
3569 *per = 1 << 23; /* Max PER */
3570 } else {
3571 /* Counter 2 */
3572 reg = STV090x_READ_DEMOD(state, ERRCNT22);
3573 h = STV090x_GETFIELD_Px(reg, ERR_CNT2_FIELD);
3575 reg = STV090x_READ_DEMOD(state, ERRCNT21);
3576 m = STV090x_GETFIELD_Px(reg, ERR_CNT21_FIELD);
3578 reg = STV090x_READ_DEMOD(state, ERRCNT20);
3579 l = STV090x_GETFIELD_Px(reg, ERR_CNT20_FIELD);
3581 *per = ((h << 16) | (m << 8) | l);
3583 count_4 = STV090x_READ_DEMOD(state, FBERCPT4);
3584 count_3 = STV090x_READ_DEMOD(state, FBERCPT3);
3585 count_2 = STV090x_READ_DEMOD(state, FBERCPT2);
3586 count_1 = STV090x_READ_DEMOD(state, FBERCPT1);
3587 count_0 = STV090x_READ_DEMOD(state, FBERCPT0);
3589 if ((!count_4) && (!count_3)) {
3590 count = (count_2 & 0xff) << 16;
3591 count |= (count_1 & 0xff) << 8;
3592 count |= count_0 & 0xff;
3593 } else {
3594 count = 1 << 24;
3596 if (count == 0)
3597 *per = 1;
3599 if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0) < 0)
3600 goto err;
3601 if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3602 goto err;
3604 return 0;
3605 err:
3606 dprintk(FE_ERROR, 1, "I/O error");
3607 return -1;
3610 static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
3612 int res = 0;
3613 int min = 0, med;
3615 if ((val >= tab[min].read && val < tab[max].read) ||
3616 (val >= tab[max].read && val < tab[min].read)) {
3617 while ((max - min) > 1) {
3618 med = (max + min) / 2;
3619 if ((val >= tab[min].read && val < tab[med].read) ||
3620 (val >= tab[med].read && val < tab[min].read))
3621 max = med;
3622 else
3623 min = med;
3625 res = ((val - tab[min].read) *
3626 (tab[max].real - tab[min].real) /
3627 (tab[max].read - tab[min].read)) +
3628 tab[min].real;
3629 } else {
3630 if (tab[min].read < tab[max].read) {
3631 if (val < tab[min].read)
3632 res = tab[min].real;
3633 else if (val >= tab[max].read)
3634 res = tab[max].real;
3635 } else {
3636 if (val >= tab[min].read)
3637 res = tab[min].real;
3638 else if (val < tab[max].read)
3639 res = tab[max].real;
3643 return res;
3646 static int stv090x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
3648 struct stv090x_state *state = fe->demodulator_priv;
3649 u32 reg;
3650 s32 agc_0, agc_1, agc;
3651 s32 str;
3653 reg = STV090x_READ_DEMOD(state, AGCIQIN1);
3654 agc_1 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
3655 reg = STV090x_READ_DEMOD(state, AGCIQIN0);
3656 agc_0 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
3657 agc = MAKEWORD16(agc_1, agc_0);
3659 str = stv090x_table_lookup(stv090x_rf_tab,
3660 ARRAY_SIZE(stv090x_rf_tab) - 1, agc);
3661 if (agc > stv090x_rf_tab[0].read)
3662 str = 0;
3663 else if (agc < stv090x_rf_tab[ARRAY_SIZE(stv090x_rf_tab) - 1].read)
3664 str = -100;
3665 *strength = (str + 100) * 0xFFFF / 100;
3667 return 0;
3670 static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
3672 struct stv090x_state *state = fe->demodulator_priv;
3673 u32 reg_0, reg_1, reg, i;
3674 s32 val_0, val_1, val = 0;
3675 u8 lock_f;
3676 s32 div;
3677 u32 last;
3679 switch (state->delsys) {
3680 case STV090x_DVBS2:
3681 reg = STV090x_READ_DEMOD(state, DSTATUS);
3682 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
3683 if (lock_f) {
3684 msleep(5);
3685 for (i = 0; i < 16; i++) {
3686 reg_1 = STV090x_READ_DEMOD(state, NNOSPLHT1);
3687 val_1 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
3688 reg_0 = STV090x_READ_DEMOD(state, NNOSPLHT0);
3689 val_0 = STV090x_GETFIELD_Px(reg_0, NOSPLHT_NORMED_FIELD);
3690 val += MAKEWORD16(val_1, val_0);
3691 msleep(1);
3693 val /= 16;
3694 last = ARRAY_SIZE(stv090x_s2cn_tab) - 1;
3695 div = stv090x_s2cn_tab[last].real -
3696 stv090x_s2cn_tab[3].real;
3697 val = stv090x_table_lookup(stv090x_s2cn_tab, last, val);
3698 if (val < 0)
3699 val = 0;
3700 *cnr = val * 0xFFFF / div;
3702 break;
3704 case STV090x_DVBS1:
3705 case STV090x_DSS:
3706 reg = STV090x_READ_DEMOD(state, DSTATUS);
3707 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
3708 if (lock_f) {
3709 msleep(5);
3710 for (i = 0; i < 16; i++) {
3711 reg_1 = STV090x_READ_DEMOD(state, NOSDATAT1);
3712 val_1 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
3713 reg_0 = STV090x_READ_DEMOD(state, NOSDATAT0);
3714 val_0 = STV090x_GETFIELD_Px(reg_0, NOSDATAT_UNNORMED_FIELD);
3715 val += MAKEWORD16(val_1, val_0);
3716 msleep(1);
3718 val /= 16;
3719 last = ARRAY_SIZE(stv090x_s1cn_tab) - 1;
3720 div = stv090x_s1cn_tab[last].real -
3721 stv090x_s1cn_tab[0].real;
3722 val = stv090x_table_lookup(stv090x_s1cn_tab, last, val);
3723 *cnr = val * 0xFFFF / div;
3725 break;
3726 default:
3727 break;
3730 return 0;
3733 static int stv090x_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
3735 struct stv090x_state *state = fe->demodulator_priv;
3736 u32 reg;
3738 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3739 switch (tone) {
3740 case SEC_TONE_ON:
3741 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
3742 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3743 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3744 goto err;
3745 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3746 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3747 goto err;
3748 break;
3750 case SEC_TONE_OFF:
3751 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
3752 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3753 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3754 goto err;
3755 break;
3756 default:
3757 return -EINVAL;
3760 return 0;
3761 err:
3762 dprintk(FE_ERROR, 1, "I/O error");
3763 return -1;
3767 static enum dvbfe_algo stv090x_frontend_algo(struct dvb_frontend *fe)
3769 return DVBFE_ALGO_CUSTOM;
3772 static int stv090x_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
3774 struct stv090x_state *state = fe->demodulator_priv;
3775 u32 reg, idle = 0, fifo_full = 1;
3776 int i;
3778 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3780 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD,
3781 (state->config->diseqc_envelope_mode) ? 4 : 2);
3782 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3783 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3784 goto err;
3785 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3786 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3787 goto err;
3789 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3790 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3791 goto err;
3793 for (i = 0; i < cmd->msg_len; i++) {
3795 while (fifo_full) {
3796 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3797 fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
3800 if (STV090x_WRITE_DEMOD(state, DISTXDATA, cmd->msg[i]) < 0)
3801 goto err;
3803 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3804 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
3805 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3806 goto err;
3808 i = 0;
3810 while ((!idle) && (i < 10)) {
3811 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3812 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3813 msleep(10);
3814 i++;
3817 return 0;
3818 err:
3819 dprintk(FE_ERROR, 1, "I/O error");
3820 return -1;
3823 static int stv090x_send_diseqc_burst(struct dvb_frontend *fe,
3824 enum fe_sec_mini_cmd burst)
3826 struct stv090x_state *state = fe->demodulator_priv;
3827 u32 reg, idle = 0, fifo_full = 1;
3828 u8 mode, value;
3829 int i;
3831 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3833 if (burst == SEC_MINI_A) {
3834 mode = (state->config->diseqc_envelope_mode) ? 5 : 3;
3835 value = 0x00;
3836 } else {
3837 mode = (state->config->diseqc_envelope_mode) ? 4 : 2;
3838 value = 0xFF;
3841 STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, mode);
3842 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
3843 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3844 goto err;
3845 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3846 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3847 goto err;
3849 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3850 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3851 goto err;
3853 while (fifo_full) {
3854 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3855 fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
3858 if (STV090x_WRITE_DEMOD(state, DISTXDATA, value) < 0)
3859 goto err;
3861 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3862 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
3863 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3864 goto err;
3866 i = 0;
3868 while ((!idle) && (i < 10)) {
3869 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3870 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3871 msleep(10);
3872 i++;
3875 return 0;
3876 err:
3877 dprintk(FE_ERROR, 1, "I/O error");
3878 return -1;
3881 static int stv090x_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
3883 struct stv090x_state *state = fe->demodulator_priv;
3884 u32 reg = 0, i = 0, rx_end = 0;
3886 while ((rx_end != 1) && (i < 10)) {
3887 msleep(10);
3888 i++;
3889 reg = STV090x_READ_DEMOD(state, DISRX_ST0);
3890 rx_end = STV090x_GETFIELD_Px(reg, RX_END_FIELD);
3893 if (rx_end) {
3894 reply->msg_len = STV090x_GETFIELD_Px(reg, FIFO_BYTENBR_FIELD);
3895 for (i = 0; i < reply->msg_len; i++)
3896 reply->msg[i] = STV090x_READ_DEMOD(state, DISRXDATA);
3899 return 0;
3902 static int stv090x_sleep(struct dvb_frontend *fe)
3904 struct stv090x_state *state = fe->demodulator_priv;
3905 u32 reg;
3906 u8 full_standby = 0;
3908 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
3909 goto err;
3911 if (state->config->tuner_sleep) {
3912 if (state->config->tuner_sleep(fe) < 0)
3913 goto err_gateoff;
3916 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
3917 goto err;
3919 dprintk(FE_DEBUG, 1, "Set %s(%d) to sleep",
3920 state->device == STV0900 ? "STV0900" : "STV0903",
3921 state->demod);
3923 mutex_lock(&state->internal->demod_lock);
3925 switch (state->demod) {
3926 case STV090x_DEMODULATOR_0:
3927 /* power off ADC 1 */
3928 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
3929 STV090x_SETFIELD(reg, ADC1_PON_FIELD, 0);
3930 if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
3931 goto err_unlock;
3932 /* power off DiSEqC 1 */
3933 reg = stv090x_read_reg(state, STV090x_TSTTNR2);
3934 STV090x_SETFIELD(reg, DISEQC1_PON_FIELD, 0);
3935 if (stv090x_write_reg(state, STV090x_TSTTNR2, reg) < 0)
3936 goto err_unlock;
3938 /* check whether path 2 is already sleeping, that is when
3939 ADC2 is off */
3940 reg = stv090x_read_reg(state, STV090x_TSTTNR3);
3941 if (STV090x_GETFIELD(reg, ADC2_PON_FIELD) == 0)
3942 full_standby = 1;
3944 /* stop clocks */
3945 reg = stv090x_read_reg(state, STV090x_STOPCLK1);
3946 /* packet delineator 1 clock */
3947 STV090x_SETFIELD(reg, STOP_CLKPKDT1_FIELD, 1);
3948 /* ADC 1 clock */
3949 STV090x_SETFIELD(reg, STOP_CLKADCI1_FIELD, 1);
3950 /* FEC clock is shared between the two paths, only stop it
3951 when full standby is possible */
3952 if (full_standby)
3953 STV090x_SETFIELD(reg, STOP_CLKFEC_FIELD, 1);
3954 if (stv090x_write_reg(state, STV090x_STOPCLK1, reg) < 0)
3955 goto err_unlock;
3956 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
3957 /* sampling 1 clock */
3958 STV090x_SETFIELD(reg, STOP_CLKSAMP1_FIELD, 1);
3959 /* viterbi 1 clock */
3960 STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, 1);
3961 /* TS clock is shared between the two paths, only stop it
3962 when full standby is possible */
3963 if (full_standby)
3964 STV090x_SETFIELD(reg, STOP_CLKTS_FIELD, 1);
3965 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
3966 goto err_unlock;
3967 break;
3969 case STV090x_DEMODULATOR_1:
3970 /* power off ADC 2 */
3971 reg = stv090x_read_reg(state, STV090x_TSTTNR3);
3972 STV090x_SETFIELD(reg, ADC2_PON_FIELD, 0);
3973 if (stv090x_write_reg(state, STV090x_TSTTNR3, reg) < 0)
3974 goto err_unlock;
3975 /* power off DiSEqC 2 */
3976 reg = stv090x_read_reg(state, STV090x_TSTTNR4);
3977 STV090x_SETFIELD(reg, DISEQC2_PON_FIELD, 0);
3978 if (stv090x_write_reg(state, STV090x_TSTTNR4, reg) < 0)
3979 goto err_unlock;
3981 /* check whether path 1 is already sleeping, that is when
3982 ADC1 is off */
3983 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
3984 if (STV090x_GETFIELD(reg, ADC1_PON_FIELD) == 0)
3985 full_standby = 1;
3987 /* stop clocks */
3988 reg = stv090x_read_reg(state, STV090x_STOPCLK1);
3989 /* packet delineator 2 clock */
3990 STV090x_SETFIELD(reg, STOP_CLKPKDT2_FIELD, 1);
3991 /* ADC 2 clock */
3992 STV090x_SETFIELD(reg, STOP_CLKADCI2_FIELD, 1);
3993 /* FEC clock is shared between the two paths, only stop it
3994 when full standby is possible */
3995 if (full_standby)
3996 STV090x_SETFIELD(reg, STOP_CLKFEC_FIELD, 1);
3997 if (stv090x_write_reg(state, STV090x_STOPCLK1, reg) < 0)
3998 goto err_unlock;
3999 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
4000 /* sampling 2 clock */
4001 STV090x_SETFIELD(reg, STOP_CLKSAMP2_FIELD, 1);
4002 /* viterbi 2 clock */
4003 STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, 1);
4004 /* TS clock is shared between the two paths, only stop it
4005 when full standby is possible */
4006 if (full_standby)
4007 STV090x_SETFIELD(reg, STOP_CLKTS_FIELD, 1);
4008 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
4009 goto err_unlock;
4010 break;
4012 default:
4013 dprintk(FE_ERROR, 1, "Wrong demodulator!");
4014 break;
4017 if (full_standby) {
4018 /* general power off */
4019 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
4020 STV090x_SETFIELD(reg, STANDBY_FIELD, 0x01);
4021 if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
4022 goto err_unlock;
4025 mutex_unlock(&state->internal->demod_lock);
4026 return 0;
4028 err_gateoff:
4029 stv090x_i2c_gate_ctrl(state, 0);
4030 goto err;
4031 err_unlock:
4032 mutex_unlock(&state->internal->demod_lock);
4033 err:
4034 dprintk(FE_ERROR, 1, "I/O error");
4035 return -1;
4038 static int stv090x_wakeup(struct dvb_frontend *fe)
4040 struct stv090x_state *state = fe->demodulator_priv;
4041 u32 reg;
4043 dprintk(FE_DEBUG, 1, "Wake %s(%d) from standby",
4044 state->device == STV0900 ? "STV0900" : "STV0903",
4045 state->demod);
4047 mutex_lock(&state->internal->demod_lock);
4049 /* general power on */
4050 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
4051 STV090x_SETFIELD(reg, STANDBY_FIELD, 0x00);
4052 if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
4053 goto err;
4055 switch (state->demod) {
4056 case STV090x_DEMODULATOR_0:
4057 /* power on ADC 1 */
4058 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
4059 STV090x_SETFIELD(reg, ADC1_PON_FIELD, 1);
4060 if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
4061 goto err;
4062 /* power on DiSEqC 1 */
4063 reg = stv090x_read_reg(state, STV090x_TSTTNR2);
4064 STV090x_SETFIELD(reg, DISEQC1_PON_FIELD, 1);
4065 if (stv090x_write_reg(state, STV090x_TSTTNR2, reg) < 0)
4066 goto err;
4068 /* activate clocks */
4069 reg = stv090x_read_reg(state, STV090x_STOPCLK1);
4070 /* packet delineator 1 clock */
4071 STV090x_SETFIELD(reg, STOP_CLKPKDT1_FIELD, 0);
4072 /* ADC 1 clock */
4073 STV090x_SETFIELD(reg, STOP_CLKADCI1_FIELD, 0);
4074 /* FEC clock */
4075 STV090x_SETFIELD(reg, STOP_CLKFEC_FIELD, 0);
4076 if (stv090x_write_reg(state, STV090x_STOPCLK1, reg) < 0)
4077 goto err;
4078 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
4079 /* sampling 1 clock */
4080 STV090x_SETFIELD(reg, STOP_CLKSAMP1_FIELD, 0);
4081 /* viterbi 1 clock */
4082 STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, 0);
4083 /* TS clock */
4084 STV090x_SETFIELD(reg, STOP_CLKTS_FIELD, 0);
4085 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
4086 goto err;
4087 break;
4089 case STV090x_DEMODULATOR_1:
4090 /* power on ADC 2 */
4091 reg = stv090x_read_reg(state, STV090x_TSTTNR3);
4092 STV090x_SETFIELD(reg, ADC2_PON_FIELD, 1);
4093 if (stv090x_write_reg(state, STV090x_TSTTNR3, reg) < 0)
4094 goto err;
4095 /* power on DiSEqC 2 */
4096 reg = stv090x_read_reg(state, STV090x_TSTTNR4);
4097 STV090x_SETFIELD(reg, DISEQC2_PON_FIELD, 1);
4098 if (stv090x_write_reg(state, STV090x_TSTTNR4, reg) < 0)
4099 goto err;
4101 /* activate clocks */
4102 reg = stv090x_read_reg(state, STV090x_STOPCLK1);
4103 /* packet delineator 2 clock */
4104 STV090x_SETFIELD(reg, STOP_CLKPKDT2_FIELD, 0);
4105 /* ADC 2 clock */
4106 STV090x_SETFIELD(reg, STOP_CLKADCI2_FIELD, 0);
4107 /* FEC clock */
4108 STV090x_SETFIELD(reg, STOP_CLKFEC_FIELD, 0);
4109 if (stv090x_write_reg(state, STV090x_STOPCLK1, reg) < 0)
4110 goto err;
4111 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
4112 /* sampling 2 clock */
4113 STV090x_SETFIELD(reg, STOP_CLKSAMP2_FIELD, 0);
4114 /* viterbi 2 clock */
4115 STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, 0);
4116 /* TS clock */
4117 STV090x_SETFIELD(reg, STOP_CLKTS_FIELD, 0);
4118 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
4119 goto err;
4120 break;
4122 default:
4123 dprintk(FE_ERROR, 1, "Wrong demodulator!");
4124 break;
4127 mutex_unlock(&state->internal->demod_lock);
4128 return 0;
4129 err:
4130 mutex_unlock(&state->internal->demod_lock);
4131 dprintk(FE_ERROR, 1, "I/O error");
4132 return -1;
4135 static void stv090x_release(struct dvb_frontend *fe)
4137 struct stv090x_state *state = fe->demodulator_priv;
4139 state->internal->num_used--;
4140 if (state->internal->num_used <= 0) {
4142 dprintk(FE_ERROR, 1, "Actually removing");
4144 remove_dev(state->internal);
4145 kfree(state->internal);
4148 kfree(state);
4151 static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc_mode)
4153 u32 reg = 0;
4155 reg = stv090x_read_reg(state, STV090x_GENCFG);
4157 switch (ldpc_mode) {
4158 case STV090x_DUAL:
4159 default:
4160 if ((state->demod_mode != STV090x_DUAL) || (STV090x_GETFIELD(reg, DDEMOD_FIELD) != 1)) {
4161 /* set LDPC to dual mode */
4162 if (stv090x_write_reg(state, STV090x_GENCFG, 0x1d) < 0)
4163 goto err;
4165 state->demod_mode = STV090x_DUAL;
4167 reg = stv090x_read_reg(state, STV090x_TSTRES0);
4168 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
4169 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
4170 goto err;
4171 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
4172 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
4173 goto err;
4175 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
4176 goto err;
4177 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
4178 goto err;
4179 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
4180 goto err;
4181 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
4182 goto err;
4183 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
4184 goto err;
4185 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
4186 goto err;
4187 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
4188 goto err;
4190 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
4191 goto err;
4192 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
4193 goto err;
4194 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
4195 goto err;
4196 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
4197 goto err;
4198 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
4199 goto err;
4200 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
4201 goto err;
4202 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
4203 goto err;
4205 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
4206 goto err;
4207 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
4208 goto err;
4210 break;
4212 case STV090x_SINGLE:
4213 if (stv090x_stop_modcod(state) < 0)
4214 goto err;
4215 if (stv090x_activate_modcod_single(state) < 0)
4216 goto err;
4218 if (state->demod == STV090x_DEMODULATOR_1) {
4219 if (stv090x_write_reg(state, STV090x_GENCFG, 0x06) < 0) /* path 2 */
4220 goto err;
4221 } else {
4222 if (stv090x_write_reg(state, STV090x_GENCFG, 0x04) < 0) /* path 1 */
4223 goto err;
4226 reg = stv090x_read_reg(state, STV090x_TSTRES0);
4227 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
4228 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
4229 goto err;
4230 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
4231 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
4232 goto err;
4234 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
4235 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x01);
4236 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
4237 goto err;
4238 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x00);
4239 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
4240 goto err;
4241 break;
4244 return 0;
4245 err:
4246 dprintk(FE_ERROR, 1, "I/O error");
4247 return -1;
4250 /* return (Hz), clk in Hz*/
4251 static u32 stv090x_get_mclk(struct stv090x_state *state)
4253 const struct stv090x_config *config = state->config;
4254 u32 div, reg;
4255 u8 ratio;
4257 div = stv090x_read_reg(state, STV090x_NCOARSE);
4258 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
4259 ratio = STV090x_GETFIELD(reg, SELX1RATIO_FIELD) ? 4 : 6;
4261 return (div + 1) * config->xtal / ratio; /* kHz */
4264 static int stv090x_set_mclk(struct stv090x_state *state, u32 mclk, u32 clk)
4266 const struct stv090x_config *config = state->config;
4267 u32 reg, div, clk_sel;
4269 reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
4270 clk_sel = ((STV090x_GETFIELD(reg, SELX1RATIO_FIELD) == 1) ? 4 : 6);
4272 div = ((clk_sel * mclk) / config->xtal) - 1;
4274 reg = stv090x_read_reg(state, STV090x_NCOARSE);
4275 STV090x_SETFIELD(reg, M_DIV_FIELD, div);
4276 if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0)
4277 goto err;
4279 state->internal->mclk = stv090x_get_mclk(state);
4281 /*Set the DiseqC frequency to 22KHz */
4282 div = state->internal->mclk / 704000;
4283 if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0)
4284 goto err;
4285 if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
4286 goto err;
4288 return 0;
4289 err:
4290 dprintk(FE_ERROR, 1, "I/O error");
4291 return -1;
4294 static int stv0900_set_tspath(struct stv090x_state *state)
4296 u32 reg;
4298 if (state->internal->dev_ver >= 0x20) {
4299 switch (state->config->ts1_mode) {
4300 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4301 case STV090x_TSMODE_DVBCI:
4302 switch (state->config->ts2_mode) {
4303 case STV090x_TSMODE_SERIAL_PUNCTURED:
4304 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4305 default:
4306 stv090x_write_reg(state, STV090x_TSGENERAL, 0x00);
4307 break;
4309 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4310 case STV090x_TSMODE_DVBCI:
4311 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x06) < 0) /* Mux'd stream mode */
4312 goto err;
4313 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4314 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4315 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4316 goto err;
4317 reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
4318 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4319 if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
4320 goto err;
4321 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
4322 goto err;
4323 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
4324 goto err;
4325 break;
4327 break;
4329 case STV090x_TSMODE_SERIAL_PUNCTURED:
4330 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4331 default:
4332 switch (state->config->ts2_mode) {
4333 case STV090x_TSMODE_SERIAL_PUNCTURED:
4334 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4335 default:
4336 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
4337 goto err;
4338 break;
4340 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4341 case STV090x_TSMODE_DVBCI:
4342 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0a) < 0)
4343 goto err;
4344 break;
4346 break;
4348 } else {
4349 switch (state->config->ts1_mode) {
4350 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4351 case STV090x_TSMODE_DVBCI:
4352 switch (state->config->ts2_mode) {
4353 case STV090x_TSMODE_SERIAL_PUNCTURED:
4354 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4355 default:
4356 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x10);
4357 break;
4359 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4360 case STV090x_TSMODE_DVBCI:
4361 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x16);
4362 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4363 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4364 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4365 goto err;
4366 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4367 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 0);
4368 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4369 goto err;
4370 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
4371 goto err;
4372 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
4373 goto err;
4374 break;
4376 break;
4378 case STV090x_TSMODE_SERIAL_PUNCTURED:
4379 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4380 default:
4381 switch (state->config->ts2_mode) {
4382 case STV090x_TSMODE_SERIAL_PUNCTURED:
4383 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4384 default:
4385 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
4386 break;
4388 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4389 case STV090x_TSMODE_DVBCI:
4390 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x12);
4391 break;
4393 break;
4397 switch (state->config->ts1_mode) {
4398 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4399 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4400 STV090x_SETFIELD_Px(reg, TSFIFO_TEIUPDATE_FIELD, state->config->ts1_tei);
4401 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4402 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4403 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4404 goto err;
4405 break;
4407 case STV090x_TSMODE_DVBCI:
4408 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4409 STV090x_SETFIELD_Px(reg, TSFIFO_TEIUPDATE_FIELD, state->config->ts1_tei);
4410 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4411 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4412 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4413 goto err;
4414 break;
4416 case STV090x_TSMODE_SERIAL_PUNCTURED:
4417 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4418 STV090x_SETFIELD_Px(reg, TSFIFO_TEIUPDATE_FIELD, state->config->ts1_tei);
4419 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4420 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4421 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4422 goto err;
4423 break;
4425 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4426 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4427 STV090x_SETFIELD_Px(reg, TSFIFO_TEIUPDATE_FIELD, state->config->ts1_tei);
4428 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4429 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4430 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4431 goto err;
4432 break;
4434 default:
4435 break;
4438 switch (state->config->ts2_mode) {
4439 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4440 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4441 STV090x_SETFIELD_Px(reg, TSFIFO_TEIUPDATE_FIELD, state->config->ts2_tei);
4442 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4443 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4444 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4445 goto err;
4446 break;
4448 case STV090x_TSMODE_DVBCI:
4449 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4450 STV090x_SETFIELD_Px(reg, TSFIFO_TEIUPDATE_FIELD, state->config->ts2_tei);
4451 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4452 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4453 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4454 goto err;
4455 break;
4457 case STV090x_TSMODE_SERIAL_PUNCTURED:
4458 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4459 STV090x_SETFIELD_Px(reg, TSFIFO_TEIUPDATE_FIELD, state->config->ts2_tei);
4460 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4461 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4462 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4463 goto err;
4464 break;
4466 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4467 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4468 STV090x_SETFIELD_Px(reg, TSFIFO_TEIUPDATE_FIELD, state->config->ts2_tei);
4469 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4470 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4471 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4472 goto err;
4473 break;
4475 default:
4476 break;
4479 if (state->config->ts1_clk > 0) {
4480 u32 speed;
4482 switch (state->config->ts1_mode) {
4483 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4484 case STV090x_TSMODE_DVBCI:
4485 default:
4486 speed = state->internal->mclk /
4487 (state->config->ts1_clk / 4);
4488 if (speed < 0x08)
4489 speed = 0x08;
4490 if (speed > 0xFF)
4491 speed = 0xFF;
4492 break;
4493 case STV090x_TSMODE_SERIAL_PUNCTURED:
4494 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4495 speed = state->internal->mclk /
4496 (state->config->ts1_clk / 32);
4497 if (speed < 0x20)
4498 speed = 0x20;
4499 if (speed > 0xFF)
4500 speed = 0xFF;
4501 break;
4503 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4504 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4505 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4506 goto err;
4507 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, speed) < 0)
4508 goto err;
4511 if (state->config->ts2_clk > 0) {
4512 u32 speed;
4514 switch (state->config->ts2_mode) {
4515 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4516 case STV090x_TSMODE_DVBCI:
4517 default:
4518 speed = state->internal->mclk /
4519 (state->config->ts2_clk / 4);
4520 if (speed < 0x08)
4521 speed = 0x08;
4522 if (speed > 0xFF)
4523 speed = 0xFF;
4524 break;
4525 case STV090x_TSMODE_SERIAL_PUNCTURED:
4526 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4527 speed = state->internal->mclk /
4528 (state->config->ts2_clk / 32);
4529 if (speed < 0x20)
4530 speed = 0x20;
4531 if (speed > 0xFF)
4532 speed = 0xFF;
4533 break;
4535 reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
4536 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4537 if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
4538 goto err;
4539 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, speed) < 0)
4540 goto err;
4543 reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
4544 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
4545 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4546 goto err;
4547 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4548 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
4549 goto err;
4551 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4552 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
4553 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4554 goto err;
4555 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4556 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4557 goto err;
4559 return 0;
4560 err:
4561 dprintk(FE_ERROR, 1, "I/O error");
4562 return -1;
4565 static int stv0903_set_tspath(struct stv090x_state *state)
4567 u32 reg;
4569 if (state->internal->dev_ver >= 0x20) {
4570 switch (state->config->ts1_mode) {
4571 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4572 case STV090x_TSMODE_DVBCI:
4573 stv090x_write_reg(state, STV090x_TSGENERAL, 0x00);
4574 break;
4576 case STV090x_TSMODE_SERIAL_PUNCTURED:
4577 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4578 default:
4579 stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c);
4580 break;
4582 } else {
4583 switch (state->config->ts1_mode) {
4584 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4585 case STV090x_TSMODE_DVBCI:
4586 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x10);
4587 break;
4589 case STV090x_TSMODE_SERIAL_PUNCTURED:
4590 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4591 default:
4592 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
4593 break;
4597 switch (state->config->ts1_mode) {
4598 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4599 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4600 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4601 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4602 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4603 goto err;
4604 break;
4606 case STV090x_TSMODE_DVBCI:
4607 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4608 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
4609 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4610 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4611 goto err;
4612 break;
4614 case STV090x_TSMODE_SERIAL_PUNCTURED:
4615 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4616 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4617 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
4618 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4619 goto err;
4620 break;
4622 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4623 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4624 STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
4625 STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
4626 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4627 goto err;
4628 break;
4630 default:
4631 break;
4634 if (state->config->ts1_clk > 0) {
4635 u32 speed;
4637 switch (state->config->ts1_mode) {
4638 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4639 case STV090x_TSMODE_DVBCI:
4640 default:
4641 speed = state->internal->mclk /
4642 (state->config->ts1_clk / 4);
4643 if (speed < 0x08)
4644 speed = 0x08;
4645 if (speed > 0xFF)
4646 speed = 0xFF;
4647 break;
4648 case STV090x_TSMODE_SERIAL_PUNCTURED:
4649 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4650 speed = state->internal->mclk /
4651 (state->config->ts1_clk / 32);
4652 if (speed < 0x20)
4653 speed = 0x20;
4654 if (speed > 0xFF)
4655 speed = 0xFF;
4656 break;
4658 reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
4659 STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
4660 if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
4661 goto err;
4662 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, speed) < 0)
4663 goto err;
4666 reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
4667 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
4668 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4669 goto err;
4670 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4671 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4672 goto err;
4674 return 0;
4675 err:
4676 dprintk(FE_ERROR, 1, "I/O error");
4677 return -1;
4680 static int stv090x_init(struct dvb_frontend *fe)
4682 struct stv090x_state *state = fe->demodulator_priv;
4683 const struct stv090x_config *config = state->config;
4684 u32 reg;
4686 if (state->internal->mclk == 0) {
4687 /* call tuner init to configure the tuner's clock output
4688 divider directly before setting up the master clock of
4689 the stv090x. */
4690 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
4691 goto err;
4693 if (config->tuner_init) {
4694 if (config->tuner_init(fe) < 0)
4695 goto err_gateoff;
4698 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
4699 goto err;
4701 stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
4702 msleep(5);
4703 if (stv090x_write_reg(state, STV090x_SYNTCTRL,
4704 0x20 | config->clk_mode) < 0)
4705 goto err;
4706 stv090x_get_mclk(state);
4709 if (stv090x_wakeup(fe) < 0) {
4710 dprintk(FE_ERROR, 1, "Error waking device");
4711 goto err;
4714 if (stv090x_ldpc_mode(state, state->demod_mode) < 0)
4715 goto err;
4717 reg = STV090x_READ_DEMOD(state, TNRCFG2);
4718 STV090x_SETFIELD_Px(reg, TUN_IQSWAP_FIELD, state->inversion);
4719 if (STV090x_WRITE_DEMOD(state, TNRCFG2, reg) < 0)
4720 goto err;
4721 reg = STV090x_READ_DEMOD(state, DEMOD);
4722 STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
4723 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
4724 goto err;
4726 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
4727 goto err;
4729 if (config->tuner_set_mode) {
4730 if (config->tuner_set_mode(fe, TUNER_WAKE) < 0)
4731 goto err_gateoff;
4734 if (config->tuner_init) {
4735 if (config->tuner_init(fe) < 0)
4736 goto err_gateoff;
4739 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
4740 goto err;
4742 if (state->device == STV0900) {
4743 if (stv0900_set_tspath(state) < 0)
4744 goto err;
4745 } else {
4746 if (stv0903_set_tspath(state) < 0)
4747 goto err;
4750 return 0;
4752 err_gateoff:
4753 stv090x_i2c_gate_ctrl(state, 0);
4754 err:
4755 dprintk(FE_ERROR, 1, "I/O error");
4756 return -1;
4759 static int stv090x_setup(struct dvb_frontend *fe)
4761 struct stv090x_state *state = fe->demodulator_priv;
4762 const struct stv090x_config *config = state->config;
4763 const struct stv090x_reg *stv090x_initval = NULL;
4764 const struct stv090x_reg *stv090x_cut20_val = NULL;
4765 unsigned long t1_size = 0, t2_size = 0;
4766 u32 reg = 0;
4768 int i;
4770 if (state->device == STV0900) {
4771 dprintk(FE_DEBUG, 1, "Initializing STV0900");
4772 stv090x_initval = stv0900_initval;
4773 t1_size = ARRAY_SIZE(stv0900_initval);
4774 stv090x_cut20_val = stv0900_cut20_val;
4775 t2_size = ARRAY_SIZE(stv0900_cut20_val);
4776 } else if (state->device == STV0903) {
4777 dprintk(FE_DEBUG, 1, "Initializing STV0903");
4778 stv090x_initval = stv0903_initval;
4779 t1_size = ARRAY_SIZE(stv0903_initval);
4780 stv090x_cut20_val = stv0903_cut20_val;
4781 t2_size = ARRAY_SIZE(stv0903_cut20_val);
4784 /* STV090x init */
4786 /* Stop Demod */
4787 if (stv090x_write_reg(state, STV090x_P1_DMDISTATE, 0x5c) < 0)
4788 goto err;
4789 if (state->device == STV0900)
4790 if (stv090x_write_reg(state, STV090x_P2_DMDISTATE, 0x5c) < 0)
4791 goto err;
4793 msleep(5);
4795 /* Set No Tuner Mode */
4796 if (stv090x_write_reg(state, STV090x_P1_TNRCFG, 0x6c) < 0)
4797 goto err;
4798 if (state->device == STV0900)
4799 if (stv090x_write_reg(state, STV090x_P2_TNRCFG, 0x6c) < 0)
4800 goto err;
4802 /* I2C repeater OFF */
4803 STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level);
4804 if (stv090x_write_reg(state, STV090x_P1_I2CRPT, reg) < 0)
4805 goto err;
4806 if (state->device == STV0900)
4807 if (stv090x_write_reg(state, STV090x_P2_I2CRPT, reg) < 0)
4808 goto err;
4810 if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
4811 goto err;
4812 msleep(5);
4813 if (stv090x_write_reg(state, STV090x_I2CCFG, 0x08) < 0) /* 1/41 oversampling */
4814 goto err;
4815 if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0) /* enable PLL */
4816 goto err;
4817 msleep(5);
4819 /* write initval */
4820 dprintk(FE_DEBUG, 1, "Setting up initial values");
4821 for (i = 0; i < t1_size; i++) {
4822 if (stv090x_write_reg(state, stv090x_initval[i].addr, stv090x_initval[i].data) < 0)
4823 goto err;
4826 state->internal->dev_ver = stv090x_read_reg(state, STV090x_MID);
4827 if (state->internal->dev_ver >= 0x20) {
4828 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
4829 goto err;
4831 /* write cut20_val*/
4832 dprintk(FE_DEBUG, 1, "Setting up Cut 2.0 initial values");
4833 for (i = 0; i < t2_size; i++) {
4834 if (stv090x_write_reg(state, stv090x_cut20_val[i].addr, stv090x_cut20_val[i].data) < 0)
4835 goto err;
4838 } else if (state->internal->dev_ver < 0x20) {
4839 dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!",
4840 state->internal->dev_ver);
4842 goto err;
4843 } else if (state->internal->dev_ver > 0x30) {
4844 /* we shouldn't bail out from here */
4845 dprintk(FE_ERROR, 1, "INFO: Cut: 0x%02x probably incomplete support!",
4846 state->internal->dev_ver);
4849 /* ADC1 range */
4850 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
4851 STV090x_SETFIELD(reg, ADC1_INMODE_FIELD,
4852 (config->adc1_range == STV090x_ADC_1Vpp) ? 0 : 1);
4853 if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
4854 goto err;
4856 /* ADC2 range */
4857 reg = stv090x_read_reg(state, STV090x_TSTTNR3);
4858 STV090x_SETFIELD(reg, ADC2_INMODE_FIELD,
4859 (config->adc2_range == STV090x_ADC_1Vpp) ? 0 : 1);
4860 if (stv090x_write_reg(state, STV090x_TSTTNR3, reg) < 0)
4861 goto err;
4863 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
4864 goto err;
4865 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x00) < 0)
4866 goto err;
4868 return 0;
4869 err:
4870 dprintk(FE_ERROR, 1, "I/O error");
4871 return -1;
4874 static int stv090x_set_gpio(struct dvb_frontend *fe, u8 gpio, u8 dir,
4875 u8 value, u8 xor_value)
4877 struct stv090x_state *state = fe->demodulator_priv;
4878 u8 reg = 0;
4880 STV090x_SETFIELD(reg, GPIOx_OPD_FIELD, dir);
4881 STV090x_SETFIELD(reg, GPIOx_CONFIG_FIELD, value);
4882 STV090x_SETFIELD(reg, GPIOx_XOR_FIELD, xor_value);
4884 return stv090x_write_reg(state, STV090x_GPIOxCFG(gpio), reg);
4887 static const struct dvb_frontend_ops stv090x_ops = {
4888 .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
4889 .info = {
4890 .name = "STV090x Multistandard",
4891 .frequency_min = 950000,
4892 .frequency_max = 2150000,
4893 .frequency_stepsize = 0,
4894 .frequency_tolerance = 0,
4895 .symbol_rate_min = 1000000,
4896 .symbol_rate_max = 45000000,
4897 .caps = FE_CAN_INVERSION_AUTO |
4898 FE_CAN_FEC_AUTO |
4899 FE_CAN_QPSK |
4900 FE_CAN_2G_MODULATION
4903 .release = stv090x_release,
4904 .init = stv090x_init,
4906 .sleep = stv090x_sleep,
4907 .get_frontend_algo = stv090x_frontend_algo,
4909 .diseqc_send_master_cmd = stv090x_send_diseqc_msg,
4910 .diseqc_send_burst = stv090x_send_diseqc_burst,
4911 .diseqc_recv_slave_reply = stv090x_recv_slave_reply,
4912 .set_tone = stv090x_set_tone,
4914 .search = stv090x_search,
4915 .read_status = stv090x_read_status,
4916 .read_ber = stv090x_read_per,
4917 .read_signal_strength = stv090x_read_signal_strength,
4918 .read_snr = stv090x_read_cnr,
4922 struct dvb_frontend *stv090x_attach(struct stv090x_config *config,
4923 struct i2c_adapter *i2c,
4924 enum stv090x_demodulator demod)
4926 struct stv090x_state *state = NULL;
4927 struct stv090x_dev *temp_int;
4929 state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL);
4930 if (state == NULL)
4931 goto error;
4933 state->verbose = &verbose;
4934 state->config = config;
4935 state->i2c = i2c;
4936 state->frontend.ops = stv090x_ops;
4937 state->frontend.demodulator_priv = state;
4938 state->demod = demod;
4939 state->demod_mode = config->demod_mode; /* Single or Dual mode */
4940 state->device = config->device;
4941 state->rolloff = STV090x_RO_35; /* default */
4943 temp_int = find_dev(state->i2c,
4944 state->config->address);
4946 if ((temp_int != NULL) && (state->demod_mode == STV090x_DUAL)) {
4947 state->internal = temp_int->internal;
4948 state->internal->num_used++;
4949 dprintk(FE_INFO, 1, "Found Internal Structure!");
4950 } else {
4951 state->internal = kmalloc(sizeof(struct stv090x_internal),
4952 GFP_KERNEL);
4953 if (!state->internal)
4954 goto error;
4955 temp_int = append_internal(state->internal);
4956 if (!temp_int) {
4957 kfree(state->internal);
4958 goto error;
4960 state->internal->num_used = 1;
4961 state->internal->mclk = 0;
4962 state->internal->dev_ver = 0;
4963 state->internal->i2c_adap = state->i2c;
4964 state->internal->i2c_addr = state->config->address;
4965 dprintk(FE_INFO, 1, "Create New Internal Structure!");
4967 mutex_init(&state->internal->demod_lock);
4968 mutex_init(&state->internal->tuner_lock);
4970 if (stv090x_setup(&state->frontend) < 0) {
4971 dprintk(FE_ERROR, 1, "Error setting up device");
4972 goto err_remove;
4976 if (state->internal->dev_ver >= 0x30)
4977 state->frontend.ops.info.caps |= FE_CAN_MULTISTREAM;
4979 /* workaround for stuck DiSEqC output */
4980 if (config->diseqc_envelope_mode)
4981 stv090x_send_diseqc_burst(&state->frontend, SEC_MINI_A);
4983 config->set_gpio = stv090x_set_gpio;
4985 dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
4986 state->device == STV0900 ? "STV0900" : "STV0903",
4987 demod,
4988 state->internal->dev_ver);
4990 return &state->frontend;
4992 err_remove:
4993 remove_dev(state->internal);
4994 kfree(state->internal);
4995 error:
4996 kfree(state);
4997 return NULL;
4999 EXPORT_SYMBOL(stv090x_attach);
5000 MODULE_PARM_DESC(verbose, "Set Verbosity level");
5001 MODULE_AUTHOR("Manu Abraham");
5002 MODULE_DESCRIPTION("STV090x Multi-Std Broadcast frontend");
5003 MODULE_LICENSE("GPL");