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"
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 */
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,
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;
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,
80 if (del_dev != NULL) {
81 if (del_dev == stv090x_first_dev) {
82 stv090x_first_dev = del_dev->next_dev;
84 while (prev_dev->next_dev != del_dev)
85 prev_dev = prev_dev->next_dev;
87 prev_dev->next_dev = del_dev->next_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;
106 if (stv090x_first_dev == NULL) {
107 stv090x_first_dev = new_dev;
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;
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)
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;
700 u8 b0[] = { reg >> 8, reg & 0xff };
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);
710 if (ret != -ERESTARTSYS)
712 "Read error, Reg=[0x%02x], Status=%d",
715 return ret < 0 ? ret : -EREMOTEIO;
717 if (unlikely(*state->verbose >= FE_DEBUGREG))
718 dprintk(FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
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;
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)) {
733 "%s: i2c wr reg=%04x: len=%d is too big!\n",
734 KBUILD_MODNAME, reg, count);
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);
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;
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)
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.
775 if (state->config->tuner_i2c_lock)
776 state->config->tuner_i2c_lock(&state->frontend, 1);
778 mutex_lock(&state->internal->tuner_lock);
781 reg = STV090x_READ_DEMOD(state, I2CRPT);
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)
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)
796 if (state->config->tuner_i2c_lock)
797 state->config->tuner_i2c_lock(&state->frontend, 0);
799 mutex_unlock(&state->internal->tuner_lock);
804 dprintk(FE_ERROR, 1, "I/O error");
805 if (state->config->tuner_i2c_lock)
806 state->config->tuner_i2c_lock(&state->frontend, 0);
808 mutex_unlock(&state->internal->tuner_lock);
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;
829 case STV090x_COLD_SEARCH:
830 case STV090x_WARM_SEARCH:
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;
855 if (state->algo == STV090x_WARM_SEARCH)
856 state->DemodTimeout /= 2;
859 static int stv090x_set_srate(struct stv090x_state *state, u32 srate)
863 if (srate > 60000000) {
864 sym = (srate << 4); /* SR * 2^16 / master_clk */
865 sym /= (state->internal->mclk >> 12);
866 } else if (srate > 6000000) {
868 sym /= (state->internal->mclk >> 10);
871 sym /= (state->internal->mclk >> 7);
874 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0x7f) < 0) /* MSB */
876 if (STV090x_WRITE_DEMOD(state, SFRINIT0, (sym & 0xff)) < 0) /* LSB */
881 dprintk(FE_ERROR, 1, "I/O error");
885 static int stv090x_set_max_srate(struct stv090x_state *state, u32 clk, u32 srate)
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) {
895 sym /= (state->internal->mclk >> 10);
898 sym /= (state->internal->mclk >> 7);
902 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) /* MSB */
904 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) /* LSB */
907 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x7f) < 0) /* MSB */
909 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xff) < 0) /* LSB */
915 dprintk(FE_ERROR, 1, "I/O error");
919 static int stv090x_set_min_srate(struct stv090x_state *state, u32 clk, u32 srate)
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) {
929 sym /= (state->internal->mclk >> 10);
932 sym /= (state->internal->mclk >> 7);
935 if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0x7f)) < 0) /* MSB */
937 if (STV090x_WRITE_DEMOD(state, SFRLOW0, (sym & 0xff)) < 0) /* LSB */
941 dprintk(FE_ERROR, 1, "I/O error");
945 static u32 stv090x_car_width(u32 srate, enum stv090x_rolloff rolloff)
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)
969 if (STV090x_WRITE_DEMOD(state, VTH23, 0x64) < 0)
971 if (STV090x_WRITE_DEMOD(state, VTH34, 0x36) < 0)
973 if (STV090x_WRITE_DEMOD(state, VTH56, 0x23) < 0)
975 if (STV090x_WRITE_DEMOD(state, VTH67, 0x1e) < 0)
977 if (STV090x_WRITE_DEMOD(state, VTH78, 0x19) < 0)
981 dprintk(FE_ERROR, 1, "I/O error");
985 static int stv090x_set_vit_thtracq(struct stv090x_state *state)
987 if (STV090x_WRITE_DEMOD(state, VTH12, 0xd0) < 0)
989 if (STV090x_WRITE_DEMOD(state, VTH23, 0x7d) < 0)
991 if (STV090x_WRITE_DEMOD(state, VTH34, 0x53) < 0)
993 if (STV090x_WRITE_DEMOD(state, VTH56, 0x2f) < 0)
995 if (STV090x_WRITE_DEMOD(state, VTH67, 0x24) < 0)
997 if (STV090x_WRITE_DEMOD(state, VTH78, 0x1f) < 0)
1001 dprintk(FE_ERROR, 1, "I/O error");
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 */
1011 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x3f) < 0) /* all puncture rate */
1014 case STV090x_SEARCH_DVBS1:
1015 if (STV090x_WRITE_DEMOD(state, FECM, 0x00) < 0) /* disable DSS */
1017 switch (state->fec) {
1019 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
1024 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
1029 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x04) < 0)
1034 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x08) < 0)
1039 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x20) < 0)
1044 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x2f) < 0) /* all */
1049 case STV090x_SEARCH_DSS:
1050 if (STV090x_WRITE_DEMOD(state, FECM, 0x80) < 0)
1052 switch (state->fec) {
1054 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
1059 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
1064 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x10) < 0)
1069 if (STV090x_WRITE_DEMOD(state, PRVIT, 0x13) < 0) /* 1/2, 2/3, 6/7 */
1079 dprintk(FE_ERROR, 1, "I/O error");
1083 static int stv090x_stop_modcod(struct stv090x_state *state)
1085 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1087 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
1089 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
1091 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
1093 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
1095 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
1097 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
1099 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xff) < 0)
1101 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xff) < 0)
1103 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xff) < 0)
1105 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xff) < 0)
1107 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xff) < 0)
1109 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xff) < 0)
1111 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xff) < 0)
1113 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
1115 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xff) < 0)
1119 dprintk(FE_ERROR, 1, "I/O error");
1123 static int stv090x_activate_modcod(struct stv090x_state *state)
1125 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1127 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xfc) < 0)
1129 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xcc) < 0)
1131 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xcc) < 0)
1133 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xcc) < 0)
1135 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xcc) < 0)
1137 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xcc) < 0)
1139 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
1141 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
1143 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
1145 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
1147 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
1149 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
1151 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
1153 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xcc) < 0)
1155 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
1160 dprintk(FE_ERROR, 1, "I/O error");
1164 static int stv090x_activate_modcod_single(struct stv090x_state *state)
1167 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
1169 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xf0) < 0)
1171 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0x00) < 0)
1173 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0x00) < 0)
1175 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0x00) < 0)
1177 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0x00) < 0)
1179 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0x00) < 0)
1181 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0x00) < 0)
1183 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0x00) < 0)
1185 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0x00) < 0)
1187 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0x00) < 0)
1189 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0x00) < 0)
1191 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0x00) < 0)
1193 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0x00) < 0)
1195 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0x00) < 0)
1197 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0x0f) < 0)
1203 dprintk(FE_ERROR, 1, "I/O error");
1207 static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
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)
1218 mutex_unlock(&state->internal->demod_lock);
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)
1227 mutex_unlock(&state->internal->demod_lock);
1231 dprintk(FE_ERROR, 1, "Wrong demodulator!");
1236 mutex_unlock(&state->internal->demod_lock);
1237 dprintk(FE_ERROR, 1, "I/O error");
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)
1248 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1a) < 0)
1250 } else if ((state->srate >= 7000000) && (15000000 > state->srate)) {
1251 if (STV090x_WRITE_DEMOD(state, ACLC, 0x0c) < 0)
1253 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1b) < 0)
1255 } else if (state->srate < 7000000) {
1256 if (STV090x_WRITE_DEMOD(state, ACLC, 0x2c) < 0)
1258 if (STV090x_WRITE_DEMOD(state, BCLC, 0x1c) < 0)
1264 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
1266 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1271 dprintk(FE_ERROR, 1, "I/O error");
1275 static int stv090x_delivery_search(struct stv090x_state *state)
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)
1288 /* Activate Viterbi decoder in legacy search,
1289 * do not use FRESVIT1, might impact VITERBI2
1291 if (stv090x_vitclk_ctl(state, 0) < 0)
1294 if (stv090x_dvbs_track_crl(state) < 0)
1297 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x22) < 0) /* disable DVB-S2 */
1300 if (stv090x_set_vit_thacq(state) < 0)
1302 if (stv090x_set_viterbi(state) < 0)
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)
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)
1317 if (stv090x_vitclk_ctl(state, 1) < 0)
1320 if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0) /* stop DVB-S CR loop */
1322 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1325 if (state->internal->dev_ver <= 0x20) {
1326 /* enable S2 carrier loop */
1327 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1330 /* > Cut 3: Stop carrier 3 */
1331 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1335 if (state->demod_mode != STV090x_SINGLE) {
1336 /* Cut 2: enable link during search */
1337 if (stv090x_activate_modcod(state) < 0)
1340 /* Single demodulator
1341 * Authorize SHORT and LONG frames,
1342 * QPSK, 8PSK, 16APSK and 32APSK
1344 if (stv090x_activate_modcod_single(state) < 0)
1348 if (stv090x_set_vit_thtracq(state) < 0)
1352 case STV090x_SEARCH_AUTO:
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)
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)
1365 if (stv090x_vitclk_ctl(state, 0) < 0)
1368 if (stv090x_dvbs_track_crl(state) < 0)
1371 if (state->internal->dev_ver <= 0x20) {
1372 /* enable S2 carrier loop */
1373 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1376 /* > Cut 3: Stop carrier 3 */
1377 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
1381 if (state->demod_mode != STV090x_SINGLE) {
1382 /* Cut 2: enable link during search */
1383 if (stv090x_activate_modcod(state) < 0)
1386 /* Single demodulator
1387 * Authorize SHORT and LONG frames,
1388 * QPSK, 8PSK, 16APSK and 32APSK
1390 if (stv090x_activate_modcod_single(state) < 0)
1394 if (stv090x_set_vit_thacq(state) < 0)
1397 if (stv090x_set_viterbi(state) < 0)
1403 dprintk(FE_ERROR, 1, "I/O error");
1407 static int stv090x_start_search(struct stv090x_state *state)
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)
1418 if (state->internal->dev_ver <= 0x20) {
1419 if (state->srate <= 5000000) {
1420 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0)
1422 if (STV090x_WRITE_DEMOD(state, CFRUP1, 0x0f) < 0)
1424 if (STV090x_WRITE_DEMOD(state, CFRUP0, 0xff) < 0)
1426 if (STV090x_WRITE_DEMOD(state, CFRLOW1, 0xf0) < 0)
1428 if (STV090x_WRITE_DEMOD(state, CFRLOW0, 0x00) < 0)
1431 /*enlarge the timing bandwidth for Low SR*/
1432 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0)
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)
1439 /*reduce the timing bandwidth for high SR*/
1440 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
1445 if (state->srate <= 5000000) {
1446 /* enlarge the timing bandwidth for Low SR */
1447 STV090x_WRITE_DEMOD(state, RTCS2, 0x68);
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) {
1461 freq_abs = 1000 << 16;
1462 freq_abs /= (state->internal->mclk / 1000);
1463 freq = (s16) freq_abs;
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)
1478 if (STV090x_WRITE_DEMOD(state, CFRUP0, LSB(freq)) < 0)
1483 if (STV090x_WRITE_DEMOD(state, CFRLOW1, MSB(freq)) < 0)
1485 if (STV090x_WRITE_DEMOD(state, CFRLOW0, LSB(freq)) < 0)
1490 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0) < 0)
1492 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0) < 0)
1495 if (state->internal->dev_ver >= 0x20) {
1496 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1498 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
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)
1507 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0)
1512 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00) < 0)
1514 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xe0) < 0)
1516 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xc0) < 0)
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)
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)
1529 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0)
1532 if (state->internal->dev_ver >= 0x20) {
1533 /*Frequency offset detector setting*/
1534 if (state->srate < 2000000) {
1535 if (state->internal->dev_ver <= 0x20) {
1537 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x39) < 0)
1541 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x89) < 0)
1544 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x40) < 0)
1546 } else if (state->srate < 10000000) {
1547 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4c) < 0)
1549 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1552 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4b) < 0)
1554 if (STV090x_WRITE_DEMOD(state, CARHDR, 0x20) < 0)
1558 if (state->srate < 10000000) {
1559 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
1562 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
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)
1574 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
1578 case STV090x_COLD_SEARCH:
1579 /* The symbol rate is known */
1580 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
1582 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
1591 dprintk(FE_ERROR, 1, "I/O error");
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)
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)
1608 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0) /* SR = 65 Msps Max */
1610 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1612 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0) /* SR= 400 ksps Min */
1614 if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1616 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0) /* stop acq @ coarse carrier state */
1618 if (stv090x_set_srate(state, 1000000) < 0)
1621 steps = state->search_range / 1000000;
1626 freq_step = (1000000 * 256) / (state->internal->mclk / 256);
1629 for (i = 0; i < steps; i++) {
1631 freq_init = freq_init + (freq_step * i);
1633 freq_init = freq_init - (freq_step * i);
1637 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod RESET */
1639 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_init >> 8) & 0xff) < 0)
1641 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_init & 0xff) < 0)
1643 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x58) < 0) /* Demod RESET */
1648 for (j = 0; j < 10; j++) {
1649 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1650 STV090x_READ_DEMOD(state, AGC2I0);
1653 if (agc2 < agc2_min)
1659 dprintk(FE_ERROR, 1, "I/O error");
1663 static u32 stv090x_get_srate(struct stv090x_state *state, u32 clk)
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);
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);
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;
1697 if (state->internal->dev_ver >= 0x30)
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)
1706 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0x12) < 0)
1708 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0)
1710 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xf0) < 0)
1712 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xe0) < 0)
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)
1720 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0)
1722 if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
1724 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0)
1726 if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
1728 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0)
1730 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x50) < 0)
1733 if (state->internal->dev_ver >= 0x30) {
1734 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x99) < 0)
1736 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x98) < 0)
1739 } else if (state->internal->dev_ver >= 0x20) {
1740 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0)
1742 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
1746 if (state->srate <= 2000000)
1748 else if (state->srate <= 5000000)
1750 else if (state->srate <= 12000000)
1755 steps = -1 + ((state->search_range / 1000) / car_step);
1757 steps = (2 * steps) + 1;
1760 else if (steps > 10) {
1762 car_step = (state->search_range / 1000) / 10;
1766 freq = state->frequency;
1768 while ((!tmg_lock) && (cur_step < steps)) {
1769 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5f) < 0) /* Demod RESET */
1771 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
1773 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
1775 if (STV090x_WRITE_DEMOD(state, SFRINIT1, 0x00) < 0)
1777 if (STV090x_WRITE_DEMOD(state, SFRINIT0, 0x00) < 0)
1779 /* trigger acquisition */
1780 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x40) < 0)
1783 for (i = 0; i < 10; i++) {
1784 reg = STV090x_READ_DEMOD(state, DSTATUS);
1785 if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
1787 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
1788 STV090x_READ_DEMOD(state, AGC2I0);
1791 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
1794 if ((tmg_cpt >= 5) && (agc2 < agc2th) &&
1795 (srate_coarse < 50000000) && (srate_coarse > 850000))
1797 else if (cur_step < steps) {
1799 freq += cur_step * car_step;
1801 freq -= cur_step * car_step;
1804 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
1807 if (state->config->tuner_set_frequency) {
1808 if (state->config->tuner_set_frequency(fe, freq) < 0)
1812 if (state->config->tuner_set_bandwidth) {
1813 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
1817 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
1822 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
1825 if (state->config->tuner_get_status) {
1826 if (state->config->tuner_get_status(fe, ®) < 0)
1831 dprintk(FE_DEBUG, 1, "Tuner phase locked");
1833 dprintk(FE_DEBUG, 1, "Tuner unlocked");
1835 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
1843 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
1845 return srate_coarse;
1848 stv090x_i2c_gate_ctrl(state, 0);
1850 dprintk(FE_ERROR, 1, "I/O error");
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)
1866 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0) /* Demod RESET */
1868 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
1870 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
1872 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
1874 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
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)
1881 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1884 if (state->internal->dev_ver >= 0x30) {
1885 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x79) < 0)
1887 } else if (state->internal->dev_ver >= 0x20) {
1888 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
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)
1898 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
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)
1905 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1907 sym = (srate_coarse / 1000) * 65536;
1908 sym /= (state->internal->mclk / 1000);
1909 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1911 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
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)
1919 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
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)
1926 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1928 sym = (srate_coarse / 100) * 65536;
1929 sym /= (state->internal->mclk / 100);
1930 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1932 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
1935 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
1937 if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_coarse >> 8) & 0xff) < 0)
1939 if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_coarse & 0xff) < 0)
1941 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0) /* trigger acquisition */
1945 return srate_coarse;
1948 dprintk(FE_ERROR, 1, "I/O error");
1952 static int stv090x_get_dmdlock(struct stv090x_state *state, s32 timeout)
1954 s32 timer = 0, lock = 0;
1958 while ((timer < timeout) && (!lock)) {
1959 reg = STV090x_READ_DEMOD(state, DMDSTATE);
1960 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
1963 case 0: /* searching */
1964 case 1: /* first PLH detected */
1966 dprintk(FE_DEBUG, 1, "Demodulator searching ..");
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);
1979 dprintk(FE_DEBUG, 1, "Demodulator acquired 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;
1997 agc2 = stv090x_get_agc2_min_level(state);
1999 if (agc2 > STV090x_SEARCH_AGC2_TH(state->internal->dev_ver)) {
2003 if (state->internal->dev_ver <= 0x20) {
2004 if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
2008 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x06) < 0)
2012 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
2015 if (state->internal->dev_ver >= 0x20) {
2016 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
2018 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
2020 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
2022 if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0) /* set viterbi hysteresis */
2028 if (STV090x_WRITE_DEMOD(state, KREFTMG, k_ref) < 0)
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);
2042 for (i = 0; i < 10; i++) {
2043 agc2 += (STV090x_READ_DEMOD(state, AGC2I1) << 8) |
2044 STV090x_READ_DEMOD(state, AGC2I0);
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))
2053 if ((cpt_fail > 7) || (agc2_ovflw > 7))
2059 } while ((k_ref >= k_min) && (!lock) && (!coarse_fail));
2065 dprintk(FE_ERROR, 1, "I/O error");
2069 static int stv090x_chk_tmg(struct stv090x_state *state)
2073 u8 freq, tmg_thh, tmg_thl;
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)
2081 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
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)
2088 if (STV090x_WRITE_DEMOD(state, RTC, 0x80) < 0)
2091 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x40) < 0)
2093 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x00) < 0)
2096 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0) /* set car ofset to 0 */
2098 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
2100 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x65) < 0)
2103 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* trigger acquisition */
2107 for (i = 0; i < 10; i++) {
2108 reg = STV090x_READ_DEMOD(state, DSTATUS);
2109 if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
2116 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
2118 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0) /* DVB-S1 timing */
2120 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0) /* DVB-S2 timing */
2123 if (STV090x_WRITE_DEMOD(state, CARFREQ, freq) < 0)
2125 if (STV090x_WRITE_DEMOD(state, TMGTHRISE, tmg_thh) < 0)
2127 if (STV090x_WRITE_DEMOD(state, TMGTHFALL, tmg_thl) < 0)
2133 dprintk(FE_ERROR, 1, "I/O error");
2137 static int stv090x_get_coldlock(struct stv090x_state *state, s32 timeout_dmd)
2139 struct dvb_frontend *fe = &state->frontend;
2142 s32 car_step, steps, cur_step, dir, freq, timeout_lock;
2145 if (state->srate >= 10000000)
2146 timeout_lock = timeout_dmd / 3;
2148 timeout_lock = timeout_dmd / 2;
2150 lock = stv090x_get_dmdlock(state, timeout_lock); /* cold start wait */
2154 if (state->srate >= 10000000) {
2155 if (stv090x_chk_tmg(state)) {
2156 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2158 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2160 return stv090x_get_dmdlock(state, timeout_dmd);
2165 if (state->srate <= 4000000)
2167 else if (state->srate <= 7000000)
2169 else if (state->srate <= 10000000)
2174 steps = (state->search_range / 1000) / car_step;
2176 steps = 2 * (steps + 1);
2179 else if (steps > 12)
2185 freq = state->frequency;
2186 state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + state->srate;
2187 while ((cur_step <= steps) && (!lock)) {
2189 freq += cur_step * car_step;
2191 freq -= cur_step * car_step;
2194 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
2197 if (state->config->tuner_set_frequency) {
2198 if (state->config->tuner_set_frequency(fe, freq) < 0)
2202 if (state->config->tuner_set_bandwidth) {
2203 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
2207 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
2212 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
2215 if (state->config->tuner_get_status) {
2216 if (state->config->tuner_get_status(fe, ®) < 0)
2221 dprintk(FE_DEBUG, 1, "Tuner phase locked");
2223 dprintk(FE_DEBUG, 1, "Tuner unlocked");
2225 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
2228 STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c);
2229 if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
2231 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
2233 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
2235 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
2237 lock = stv090x_get_dmdlock(state, (timeout_dmd / 3));
2246 stv090x_i2c_gate_ctrl(state, 0);
2248 dprintk(FE_ERROR, 1, "I/O error");
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 */
2266 inc /= state->internal->mclk / 1000;
2271 switch (state->search_mode) {
2272 case STV090x_SEARCH_DVBS1:
2273 case STV090x_SEARCH_DSS:
2274 inc *= 3; /* freq step = 3% of srate */
2278 case STV090x_SEARCH_DVBS2:
2283 case STV090x_SEARCH_AUTO:
2290 if ((inc > car_max) || (inc < 0))
2291 inc = car_max / 2; /* increment <= 1/8 Mclk */
2293 timeout *= 27500; /* 27.5 Msps reference */
2295 timeout /= (srate / 1000);
2297 if ((timeout > 100) || (timeout < 0))
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;
2306 *timeout_sw = timeout;
2312 static int stv090x_chk_signal(struct stv090x_state *state)
2314 s32 offst_car, agc2, car_max;
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)
2332 if ((agc2 > 0x2000) || (offst_car > 2 * car_max) || (offst_car < -2 * car_max)) {
2334 dprintk(FE_DEBUG, 1, "No Signal");
2337 dprintk(FE_DEBUG, 1, "Found 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;
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)
2359 offst_freq = -car_max + inc;
2362 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0)
2364 if (STV090x_WRITE_DEMOD(state, CFRINIT1, ((offst_freq / 256) & 0xff)) < 0)
2366 if (STV090x_WRITE_DEMOD(state, CFRINIT0, offst_freq & 0xff) < 0)
2368 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
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)
2377 if (offst_freq >= 0)
2378 offst_freq = -offst_freq - 2 * inc;
2380 offst_freq = -offst_freq;
2382 offst_freq += 2 * inc;
2387 lock = stv090x_get_dmdlock(state, timeout);
2388 no_signal = stv090x_chk_signal(state);
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)
2403 dprintk(FE_ERROR, 1, "I/O error");
2407 static int stv090x_sw_algo(struct stv090x_state *state)
2409 int no_signal, zigzag, lock = 0;
2412 s32 dvbs2_fly_wheel;
2413 s32 inc, timeout_step, trials, steps_max;
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)
2427 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x49) < 0)
2432 case STV090x_SEARCH_DVBS2:
2433 if (state->internal->dev_ver >= 0x20) {
2434 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2438 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2443 case STV090x_SEARCH_AUTO:
2445 /* accelerate the frequency detector */
2446 if (state->internal->dev_ver >= 0x20) {
2447 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3b) < 0)
2449 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2453 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0xc9) < 0)
2461 lock = stv090x_search_car_loop(state, inc, timeout_step, zigzag, steps_max);
2462 no_signal = stv090x_chk_signal(state);
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)
2471 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
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 */
2490 if (state->internal->dev_ver >= 0x20) {
2491 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2495 if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
2501 } while ((!lock) && (trials < 2) && (!no_signal));
2505 dprintk(FE_ERROR, 1, "I/O error");
2509 static enum stv090x_delsys stv090x_get_std(struct stv090x_state *state)
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;
2522 delsys = STV090x_DVBS1;
2524 delsys = STV090x_ERROR;
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);
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);
2554 static int stv090x_get_viterbi(struct stv090x_state *state)
2558 reg = STV090x_READ_DEMOD(state, VITCURPUN);
2559 rate = STV090x_GETFIELD_Px(reg, VIT_CURPUN_FIELD);
2563 state->fec = STV090x_PR12;
2567 state->fec = STV090x_PR23;
2571 state->fec = STV090x_PR34;
2575 state->fec = STV090x_PR56;
2579 state->fec = STV090x_PR67;
2583 state->fec = STV090x_PR78;
2587 state->fec = STV090x_PRERR;
2594 static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *state)
2596 struct dvb_frontend *fe = &state->frontend;
2600 s32 i = 0, offst_freq;
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);
2613 state->delsys = stv090x_get_std(state);
2615 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
2618 if (state->config->tuner_get_frequency) {
2619 if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
2623 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
2626 offst_freq = stv090x_get_car_freq(state, state->internal->mclk) / 1000;
2627 state->frequency += offst_freq;
2629 if (stv090x_get_viterbi(state) < 0)
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)
2646 if (state->config->tuner_get_frequency) {
2647 if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
2651 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
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;
2659 if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
2660 return STV090x_RANGEOK;
2663 return STV090x_OUTOFRANGE;
2666 stv090x_i2c_gate_ctrl(state, 0);
2668 dprintk(FE_ERROR, 1, "I/O error");
2672 static u32 stv090x_get_tmgoffst(struct stv090x_state *state, u32 srate)
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 */
2684 offst_tmg = ((s32) srate * 10) / ((s32) 0x1000000 / offst_tmg);
2690 static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_modcod modcod, s32 pilots)
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;
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) {
2709 while ((i < 3) && (modcod != car_loop_qpsk_low[i].modcod))
2717 while ((i < 14) && (modcod != car_loop[i].modcod))
2722 while ((i < 11) && (modcod != car_loop_apsk_low[i].modcod))
2730 if (modcod <= STV090x_QPSK_25) {
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;
2741 aclc = car_loop_qpsk_low[i].crl_pilots_on_30;
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;
2752 aclc = car_loop_qpsk_low[i].crl_pilots_off_30;
2755 } else if (modcod <= STV090x_8PSK_910) {
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;
2766 aclc = car_loop[i].crl_pilots_on_30;
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;
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.
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;
2795 aclc = car_loop_apsk_low[i].crl_pilots_on_30;
2801 static u8 stv090x_optimize_carloop_short(struct stv090x_state *state)
2803 struct stv090x_short_frame_crloop *short_crl = NULL;
2807 switch (state->modulation) {
2815 case STV090x_16APSK:
2818 case STV090x_32APSK:
2823 if (state->internal->dev_ver >= 0x30) {
2824 /* Cut 3.0 and up */
2825 short_crl = stv090x_s2_short_crl_cut30;
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;
2840 aclc = short_crl[index].crl_30;
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;
2854 srate = stv090x_get_srate(state, state->internal->mclk);
2855 srate += stv090x_get_tmgoffst(state, srate);
2857 switch (state->delsys) {
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)
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)
2873 if (state->internal->dev_ver >= 0x30) {
2874 if (stv090x_get_viterbi(state) < 0)
2877 if (state->fec == STV090x_PR12) {
2878 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x98) < 0)
2880 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2883 if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x18) < 0)
2885 if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
2890 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
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)
2900 if (state->internal->dev_ver >= 0x30) {
2901 if (STV090x_WRITE_DEMOD(state, ACLC, 0) < 0)
2903 if (STV090x_WRITE_DEMOD(state, BCLC, 0) < 0)
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)
2916 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
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)
2923 if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2926 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2928 if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
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)
2938 } else if (state->modulation == STV090x_8PSK) {
2939 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2941 if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
2943 } else if (state->modulation == STV090x_16APSK) {
2944 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2946 if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
2948 } else if (state->modulation == STV090x_32APSK) {
2949 if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
2951 if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
2956 STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67); /* PER */
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)
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)
2980 if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
2983 if (stv090x_set_srate(state, srate) < 0)
2987 if (stv090x_dvbs_track_crl(state) < 0)
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)
2998 if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x00) < 0)
3003 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
3006 /* AUTO tracking MODE */
3007 if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x80) < 0)
3009 /* AUTO tracking MODE */
3010 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x80) < 0)
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)
3018 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
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)
3029 if (state->config->tuner_set_bandwidth) {
3030 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
3034 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
3039 if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000))
3040 msleep(50); /* blind search: wait 50ms for SR stabilization */
3044 stv090x_get_lock_tmg(state);
3046 if (!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) {
3047 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
3049 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
3051 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
3053 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
3058 while ((!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) && (i <= 2)) {
3060 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
3062 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
3064 if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
3066 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
3074 if (state->internal->dev_ver >= 0x20) {
3075 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
3079 if ((state->delsys == STV090x_DVBS1) || (state->delsys == STV090x_DSS))
3080 stv090x_set_vit_thtracq(state);
3085 stv090x_i2c_gate_ctrl(state, 0);
3087 dprintk(FE_ERROR, 1, "I/O error");
3091 static int stv090x_get_feclock(struct stv090x_state *state, s32 timeout)
3093 s32 timer = 0, lock = 0, stat;
3096 while ((timer < timeout) && (!lock)) {
3097 reg = STV090x_READ_DEMOD(state, DMDSTATE);
3098 stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
3101 case 0: /* searching */
3102 case 1: /* first PLH detected */
3107 case 2: /* DVB-S2 mode */
3108 reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
3109 lock = STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD);
3112 case 3: /* DVB-S1/legacy mode */
3113 reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
3114 lock = STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD);
3125 static int stv090x_get_lock(struct stv090x_state *state, s32 timeout_dmd, s32 timeout_fec)
3131 lock = stv090x_get_dmdlock(state, timeout_dmd);
3133 lock = stv090x_get_feclock(state, timeout_fec);
3138 while ((timer < timeout_fec) && (!lock)) {
3139 reg = STV090x_READ_DEMOD(state, TSSTATUS);
3140 lock = STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD);
3149 static int stv090x_set_s2rolloff(struct stv090x_state *state)
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)
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)
3168 dprintk(FE_ERROR, 1, "I/O error");
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;
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)
3186 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod stop */
3189 if (state->internal->dev_ver >= 0x20) {
3190 if (state->srate > 5000000) {
3191 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
3194 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x82) < 0)
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 */
3205 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
3207 if (stv090x_set_srate(state, 1000000) < 0) /* initial srate = 1Msps */
3211 if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
3213 if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
3216 if (state->srate < 2000000) {
3218 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x63) < 0)
3222 if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
3226 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
3229 if (state->internal->dev_ver >= 0x20) {
3230 if (STV090x_WRITE_DEMOD(state, KREFTMG, 0x5a) < 0)
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 */
3244 if (stv090x_set_srate(state, state->srate) < 0)
3247 if (stv090x_set_max_srate(state, state->internal->mclk,
3250 if (stv090x_set_min_srate(state, state->internal->mclk,
3254 if (state->srate >= 10000000)
3261 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
3264 if (state->config->tuner_set_bbgain) {
3265 reg = state->config->tuner_bbgain;
3267 reg = 10; /* default: 10dB */
3268 if (state->config->tuner_set_bbgain(fe, reg) < 0)
3272 if (state->config->tuner_set_frequency) {
3273 if (state->config->tuner_set_frequency(fe, state->frequency) < 0)
3277 if (state->config->tuner_set_bandwidth) {
3278 if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
3282 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
3287 if (state->config->tuner_get_status) {
3288 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
3290 if (state->config->tuner_get_status(fe, ®) < 0)
3292 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
3296 dprintk(FE_DEBUG, 1, "Tuner phase locked");
3298 dprintk(FE_DEBUG, 1, "Tuner unlocked");
3299 return STV090x_NOCARRIER;
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;
3318 if ((agc1_power == 0) && (power_iq < STV090x_IQPOWER_THRESHOLD)) {
3319 dprintk(FE_ERROR, 1, "No Signal: POWER_IQ=0x%02x", power_iq);
3321 signal_state = STV090x_NOAGC1;
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);
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)
3336 if (stv090x_delivery_search(state) < 0)
3339 if (state->algo != STV090x_BLIND_SEARCH) {
3340 if (stv090x_start_search(state) < 0)
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)) {
3359 if (stv090x_chk_tmg(state))
3360 lock = stv090x_sw_algo(state);
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)
3381 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
3382 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3385 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
3386 if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
3390 lock = stv090x_get_lock(state, state->FecTimeout,
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)
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)
3406 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67) < 0) /* PER */
3409 if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
3412 /* Reset the Total packet counter */
3413 if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0x00) < 0)
3415 /* Reset the packet Error counter2 */
3416 if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3419 signal_state = STV090x_NODATA;
3420 stv090x_chk_signal(state);
3423 return signal_state;
3426 stv090x_i2c_gate_ctrl(state, 0);
3428 dprintk(FE_ERROR, 1, "I/O error");
3432 static int stv090x_set_mis(struct stv090x_state *state, int mis)
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)
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)
3448 if (STV090x_WRITE_DEMOD(state, ISIENTRY, mis) < 0)
3450 if (STV090x_WRITE_DEMOD(state, ISIBITENA, 0xff) < 0)
3455 dprintk(FE_ERROR, 1, "I/O error");
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) {
3469 state->delsys = STV090x_DSS;
3472 state->delsys = STV090x_DVBS1;
3475 state->delsys = STV090x_DVBS2;
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;
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;
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;
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 */
3526 dprintk(FE_DEBUG, 1, "Status: Unlocked (Searching ..)");
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;
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;
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;
3565 enum fe_status status;
3567 stv090x_read_status(fe, &status);
3568 if (!(status & FE_HAS_LOCK)) {
3569 *per = 1 << 23; /* Max PER */
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;
3599 if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0) < 0)
3601 if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
3606 dprintk(FE_ERROR, 1, "I/O error");
3610 static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
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))
3625 res = ((val - tab[min].read) *
3626 (tab[max].real - tab[min].real) /
3627 (tab[max].read - tab[min].read)) +
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;
3636 if (val >= tab[min].read)
3637 res = tab[min].real;
3638 else if (val < tab[max].read)
3639 res = tab[max].real;
3646 static int stv090x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
3648 struct stv090x_state *state = fe->demodulator_priv;
3650 s32 agc_0, agc_1, agc;
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)
3663 else if (agc < stv090x_rf_tab[ARRAY_SIZE(stv090x_rf_tab) - 1].read)
3665 *strength = (str + 100) * 0xFFFF / 100;
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;
3679 switch (state->delsys) {
3681 reg = STV090x_READ_DEMOD(state, DSTATUS);
3682 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
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);
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);
3700 *cnr = val * 0xFFFF / div;
3706 reg = STV090x_READ_DEMOD(state, DSTATUS);
3707 lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
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);
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;
3733 static int stv090x_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
3735 struct stv090x_state *state = fe->demodulator_priv;
3738 reg = STV090x_READ_DEMOD(state, DISTXCTL);
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)
3745 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3746 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
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)
3762 dprintk(FE_ERROR, 1, "I/O error");
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;
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)
3785 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3786 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3789 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3790 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3793 for (i = 0; i < cmd->msg_len; i++) {
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)
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)
3810 while ((!idle) && (i < 10)) {
3811 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3812 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3819 dprintk(FE_ERROR, 1, "I/O error");
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;
3831 reg = STV090x_READ_DEMOD(state, DISTXCTL);
3833 if (burst == SEC_MINI_A) {
3834 mode = (state->config->diseqc_envelope_mode) ? 5 : 3;
3837 mode = (state->config->diseqc_envelope_mode) ? 4 : 2;
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)
3845 STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
3846 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
3849 STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
3850 if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
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)
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)
3868 while ((!idle) && (i < 10)) {
3869 reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
3870 idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
3877 dprintk(FE_ERROR, 1, "I/O error");
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)) {
3889 reg = STV090x_READ_DEMOD(state, DISRX_ST0);
3890 rx_end = STV090x_GETFIELD_Px(reg, RX_END_FIELD);
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);
3902 static int stv090x_sleep(struct dvb_frontend *fe)
3904 struct stv090x_state *state = fe->demodulator_priv;
3906 u8 full_standby = 0;
3908 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
3911 if (state->config->tuner_sleep) {
3912 if (state->config->tuner_sleep(fe) < 0)
3916 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
3919 dprintk(FE_DEBUG, 1, "Set %s(%d) to sleep",
3920 state->device == STV0900 ? "STV0900" : "STV0903",
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)
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)
3938 /* check whether path 2 is already sleeping, that is when
3940 reg = stv090x_read_reg(state, STV090x_TSTTNR3);
3941 if (STV090x_GETFIELD(reg, ADC2_PON_FIELD) == 0)
3945 reg = stv090x_read_reg(state, STV090x_STOPCLK1);
3946 /* packet delineator 1 clock */
3947 STV090x_SETFIELD(reg, STOP_CLKPKDT1_FIELD, 1);
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 */
3953 STV090x_SETFIELD(reg, STOP_CLKFEC_FIELD, 1);
3954 if (stv090x_write_reg(state, STV090x_STOPCLK1, reg) < 0)
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 */
3964 STV090x_SETFIELD(reg, STOP_CLKTS_FIELD, 1);
3965 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
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)
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)
3981 /* check whether path 1 is already sleeping, that is when
3983 reg = stv090x_read_reg(state, STV090x_TSTTNR1);
3984 if (STV090x_GETFIELD(reg, ADC1_PON_FIELD) == 0)
3988 reg = stv090x_read_reg(state, STV090x_STOPCLK1);
3989 /* packet delineator 2 clock */
3990 STV090x_SETFIELD(reg, STOP_CLKPKDT2_FIELD, 1);
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 */
3996 STV090x_SETFIELD(reg, STOP_CLKFEC_FIELD, 1);
3997 if (stv090x_write_reg(state, STV090x_STOPCLK1, reg) < 0)
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 */
4007 STV090x_SETFIELD(reg, STOP_CLKTS_FIELD, 1);
4008 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
4013 dprintk(FE_ERROR, 1, "Wrong demodulator!");
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)
4025 mutex_unlock(&state->internal->demod_lock);
4029 stv090x_i2c_gate_ctrl(state, 0);
4032 mutex_unlock(&state->internal->demod_lock);
4034 dprintk(FE_ERROR, 1, "I/O error");
4038 static int stv090x_wakeup(struct dvb_frontend *fe)
4040 struct stv090x_state *state = fe->demodulator_priv;
4043 dprintk(FE_DEBUG, 1, "Wake %s(%d) from standby",
4044 state->device == STV0900 ? "STV0900" : "STV0903",
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)
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)
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)
4068 /* activate clocks */
4069 reg = stv090x_read_reg(state, STV090x_STOPCLK1);
4070 /* packet delineator 1 clock */
4071 STV090x_SETFIELD(reg, STOP_CLKPKDT1_FIELD, 0);
4073 STV090x_SETFIELD(reg, STOP_CLKADCI1_FIELD, 0);
4075 STV090x_SETFIELD(reg, STOP_CLKFEC_FIELD, 0);
4076 if (stv090x_write_reg(state, STV090x_STOPCLK1, reg) < 0)
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);
4084 STV090x_SETFIELD(reg, STOP_CLKTS_FIELD, 0);
4085 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
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)
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)
4101 /* activate clocks */
4102 reg = stv090x_read_reg(state, STV090x_STOPCLK1);
4103 /* packet delineator 2 clock */
4104 STV090x_SETFIELD(reg, STOP_CLKPKDT2_FIELD, 0);
4106 STV090x_SETFIELD(reg, STOP_CLKADCI2_FIELD, 0);
4108 STV090x_SETFIELD(reg, STOP_CLKFEC_FIELD, 0);
4109 if (stv090x_write_reg(state, STV090x_STOPCLK1, reg) < 0)
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);
4117 STV090x_SETFIELD(reg, STOP_CLKTS_FIELD, 0);
4118 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
4123 dprintk(FE_ERROR, 1, "Wrong demodulator!");
4127 mutex_unlock(&state->internal->demod_lock);
4130 mutex_unlock(&state->internal->demod_lock);
4131 dprintk(FE_ERROR, 1, "I/O error");
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);
4151 static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc_mode)
4155 reg = stv090x_read_reg(state, STV090x_GENCFG);
4157 switch (ldpc_mode) {
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)
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)
4171 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
4172 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
4175 if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
4177 if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
4179 if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
4181 if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
4183 if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
4185 if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
4187 if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
4190 if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
4192 if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
4194 if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
4196 if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
4198 if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
4200 if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
4202 if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
4205 if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
4207 if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
4212 case STV090x_SINGLE:
4213 if (stv090x_stop_modcod(state) < 0)
4215 if (stv090x_activate_modcod_single(state) < 0)
4218 if (state->demod == STV090x_DEMODULATOR_1) {
4219 if (stv090x_write_reg(state, STV090x_GENCFG, 0x06) < 0) /* path 2 */
4222 if (stv090x_write_reg(state, STV090x_GENCFG, 0x04) < 0) /* path 1 */
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)
4230 STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
4231 if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
4234 reg = STV090x_READ_DEMOD(state, PDELCTRL1);
4235 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x01);
4236 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
4238 STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x00);
4239 if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
4246 dprintk(FE_ERROR, 1, "I/O error");
4250 /* return (Hz), clk in Hz*/
4251 static u32 stv090x_get_mclk(struct stv090x_state *state)
4253 const struct stv090x_config *config = state->config;
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)
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)
4285 if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
4290 dprintk(FE_ERROR, 1, "I/O error");
4294 static int stv0900_set_tspath(struct stv090x_state *state)
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:
4306 stv090x_write_reg(state, STV090x_TSGENERAL, 0x00);
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 */
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)
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)
4321 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
4323 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
4329 case STV090x_TSMODE_SERIAL_PUNCTURED:
4330 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4332 switch (state->config->ts2_mode) {
4333 case STV090x_TSMODE_SERIAL_PUNCTURED:
4334 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4336 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
4340 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4341 case STV090x_TSMODE_DVBCI:
4342 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0a) < 0)
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:
4356 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x10);
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)
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)
4370 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
4372 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
4378 case STV090x_TSMODE_SERIAL_PUNCTURED:
4379 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4381 switch (state->config->ts2_mode) {
4382 case STV090x_TSMODE_SERIAL_PUNCTURED:
4383 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4385 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
4388 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4389 case STV090x_TSMODE_DVBCI:
4390 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x12);
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)
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)
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)
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)
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)
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)
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)
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)
4479 if (state->config->ts1_clk > 0) {
4482 switch (state->config->ts1_mode) {
4483 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4484 case STV090x_TSMODE_DVBCI:
4486 speed = state->internal->mclk /
4487 (state->config->ts1_clk / 4);
4493 case STV090x_TSMODE_SERIAL_PUNCTURED:
4494 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4495 speed = state->internal->mclk /
4496 (state->config->ts1_clk / 32);
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)
4507 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, speed) < 0)
4511 if (state->config->ts2_clk > 0) {
4514 switch (state->config->ts2_mode) {
4515 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4516 case STV090x_TSMODE_DVBCI:
4518 speed = state->internal->mclk /
4519 (state->config->ts2_clk / 4);
4525 case STV090x_TSMODE_SERIAL_PUNCTURED:
4526 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4527 speed = state->internal->mclk /
4528 (state->config->ts2_clk / 32);
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)
4539 if (stv090x_write_reg(state, STV090x_P2_TSSPEED, speed) < 0)
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)
4547 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4548 if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
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)
4555 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4556 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4561 dprintk(FE_ERROR, 1, "I/O error");
4565 static int stv0903_set_tspath(struct stv090x_state *state)
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);
4576 case STV090x_TSMODE_SERIAL_PUNCTURED:
4577 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4579 stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c);
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);
4589 case STV090x_TSMODE_SERIAL_PUNCTURED:
4590 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4592 stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
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)
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)
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)
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)
4634 if (state->config->ts1_clk > 0) {
4637 switch (state->config->ts1_mode) {
4638 case STV090x_TSMODE_PARALLEL_PUNCTURED:
4639 case STV090x_TSMODE_DVBCI:
4641 speed = state->internal->mclk /
4642 (state->config->ts1_clk / 4);
4648 case STV090x_TSMODE_SERIAL_PUNCTURED:
4649 case STV090x_TSMODE_SERIAL_CONTINUOUS:
4650 speed = state->internal->mclk /
4651 (state->config->ts1_clk / 32);
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)
4662 if (stv090x_write_reg(state, STV090x_P1_TSSPEED, speed) < 0)
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)
4670 STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
4671 if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
4676 dprintk(FE_ERROR, 1, "I/O error");
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;
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
4690 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
4693 if (config->tuner_init) {
4694 if (config->tuner_init(fe) < 0)
4698 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
4701 stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
4703 if (stv090x_write_reg(state, STV090x_SYNTCTRL,
4704 0x20 | config->clk_mode) < 0)
4706 stv090x_get_mclk(state);
4709 if (stv090x_wakeup(fe) < 0) {
4710 dprintk(FE_ERROR, 1, "Error waking device");
4714 if (stv090x_ldpc_mode(state, state->demod_mode) < 0)
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)
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)
4726 if (stv090x_i2c_gate_ctrl(state, 1) < 0)
4729 if (config->tuner_set_mode) {
4730 if (config->tuner_set_mode(fe, TUNER_WAKE) < 0)
4734 if (config->tuner_init) {
4735 if (config->tuner_init(fe) < 0)
4739 if (stv090x_i2c_gate_ctrl(state, 0) < 0)
4742 if (state->device == STV0900) {
4743 if (stv0900_set_tspath(state) < 0)
4746 if (stv0903_set_tspath(state) < 0)
4753 stv090x_i2c_gate_ctrl(state, 0);
4755 dprintk(FE_ERROR, 1, "I/O error");
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;
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);
4787 if (stv090x_write_reg(state, STV090x_P1_DMDISTATE, 0x5c) < 0)
4789 if (state->device == STV0900)
4790 if (stv090x_write_reg(state, STV090x_P2_DMDISTATE, 0x5c) < 0)
4795 /* Set No Tuner Mode */
4796 if (stv090x_write_reg(state, STV090x_P1_TNRCFG, 0x6c) < 0)
4798 if (state->device == STV0900)
4799 if (stv090x_write_reg(state, STV090x_P2_TNRCFG, 0x6c) < 0)
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)
4806 if (state->device == STV0900)
4807 if (stv090x_write_reg(state, STV090x_P2_I2CRPT, reg) < 0)
4810 if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
4813 if (stv090x_write_reg(state, STV090x_I2CCFG, 0x08) < 0) /* 1/41 oversampling */
4815 if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0) /* enable PLL */
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)
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)
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)
4838 } else if (state->internal->dev_ver < 0x20) {
4839 dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!",
4840 state->internal->dev_ver);
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);
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)
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)
4863 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
4865 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x00) < 0)
4870 dprintk(FE_ERROR, 1, "I/O error");
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;
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 },
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 |
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);
4933 state->verbose = &verbose;
4934 state->config = config;
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!");
4951 state->internal = kmalloc(sizeof(struct stv090x_internal),
4953 if (!state->internal)
4955 temp_int = append_internal(state->internal);
4957 kfree(state->internal);
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");
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",
4988 state->internal->dev_ver);
4990 return &state->frontend;
4993 remove_dev(state->internal);
4994 kfree(state->internal);
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");