blob: f1432cb29262f5134c547382eff2407240a5613b [file] [log] [blame]
Manu Abraham8bd135b2007-07-03 09:53:42 -03001/*
2 STB0899 Multistandard Frontend driver
3 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
4
5 Copyright (C) ST Microelectronics
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include "stb0899_drv.h"
23#include "stb0899_priv.h"
24#include "stb0899_reg.h"
25
Reinhard Nissl27713c82008-01-21 16:43:18 -030026inline u32 stb0899_do_div(u64 n, u32 d)
Manu Abraham8bd135b2007-07-03 09:53:42 -030027{
Reinhard Nissl27713c82008-01-21 16:43:18 -030028 /* wrap do_div() for ease of use */
Manu Abraham8bd135b2007-07-03 09:53:42 -030029
Reinhard Nissl27713c82008-01-21 16:43:18 -030030 do_div(n, d);
31 return n;
Manu Abraham8bd135b2007-07-03 09:53:42 -030032}
33
34/*
35 * stb0899_calc_srate
36 * Compute symbol rate
37 */
38static u32 stb0899_calc_srate(u32 master_clk, u8 *sfr)
39{
Reinhard Nissl27713c82008-01-21 16:43:18 -030040 u64 tmp;
Manu Abraham8bd135b2007-07-03 09:53:42 -030041
Reinhard Nissl27713c82008-01-21 16:43:18 -030042 /* srate = (SFR * master_clk) >> 20 */
Manu Abraham8bd135b2007-07-03 09:53:42 -030043
Reinhard Nissl27713c82008-01-21 16:43:18 -030044 /* sfr is of size 20 bit, stored with an offset of 4 bit */
45 tmp = (((u32)sfr[0]) << 16) | (((u32)sfr[1]) << 8) | sfr[2];
46 tmp &= ~0xf;
47 tmp *= master_clk;
48 tmp >>= 24;
Manu Abraham8bd135b2007-07-03 09:53:42 -030049
50 return tmp;
51}
52
53/*
54 * stb0899_get_srate
55 * Get the current symbol rate
56 */
57u32 stb0899_get_srate(struct stb0899_state *state)
58{
59 struct stb0899_internal *internal = &state->internal;
Reinhard Nissl27713c82008-01-21 16:43:18 -030060 u8 sfr[3];
Manu Abraham8bd135b2007-07-03 09:53:42 -030061
62 stb0899_read_regs(state, STB0899_SFRH, sfr, 3);
63
64 return stb0899_calc_srate(internal->master_clk, sfr);
65}
66
67/*
68 * stb0899_set_srate
69 * Set symbol frequency
70 * MasterClock: master clock frequency (hz)
71 * SymbolRate: symbol rate (bauds)
72 * return symbol frequency
73 */
74static u32 stb0899_set_srate(struct stb0899_state *state, u32 master_clk, u32 srate)
75{
76 u32 tmp, tmp_up, srate_up;
77 u8 sfr_up[3], sfr[3];
78
Manu Abrahamba8862a2007-10-31 19:22:02 -030079// srate_up = srate;
Manu Abraham8bd135b2007-07-03 09:53:42 -030080 dprintk(state->verbose, FE_DEBUG, 1, "-->");
81 /*
82 * in order to have the maximum precision, the symbol rate entered into
83 * the chip is computed as the closest value of the "true value".
84 * In this purpose, the symbol rate value is rounded (1 is added on the bit
85 * below the LSB )
86 */
Manu Abrahamba8862a2007-10-31 19:22:02 -030087// srate_up += (srate_up * 3) / 100;
Manu Abraham8bd135b2007-07-03 09:53:42 -030088
Reinhard Nissl27713c82008-01-21 16:43:18 -030089 /*
90 * srate = (SFR * master_clk) >> 20
91 * <=>
92 * SFR = srate << 20 / master_clk
93 *
94 * rounded:
95 * SFR = (srate << 21 + master_clk) / (2 * master_clk)
96 *
97 * stored as 20 bit number with an offset of 4 bit:
98 * sfr = SFR << 4;
99 */
100// tmp_up = stb0899_do_div((((u64)srate_up) << 21) + master_clk, 2 * master_clk);
101// tmp_up <<= 4;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300102
Reinhard Nissl27713c82008-01-21 16:43:18 -0300103 tmp = stb0899_do_div((((u64)srate) << 21) + master_clk, 2 * master_clk);
104 tmp <<= 4;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300105
Reinhard Nissl27713c82008-01-21 16:43:18 -0300106// sfr_up[0] = tmp_up >> 16;
107// sfr_up[1] = tmp_up >> 8;
108// sfr_up[2] = tmp_up;
109
110 sfr[0] = tmp >> 16;
111 sfr[1] = tmp >> 8;
112 sfr[2] = tmp;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300113
Manu Abrahamba8862a2007-10-31 19:22:02 -0300114// stb0899_write_regs(state, STB0899_SFRUPH, sfr_up, 3);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300115 stb0899_write_regs(state, STB0899_SFRH, sfr, 3);
116
117 return srate;
118}
119
120/*
121 * stb0899_calc_loop_time
122 * Compute the amount of time needed by the timing loop to lock
123 * SymbolRate: Symbol rate
124 * return: timing loop time constant (ms)
125 */
126static long stb0899_calc_loop_time(long srate)
127{
128 if (srate > 0)
129 return (100000 / (srate / 1000));
130 else
131 return 0;
132}
133
134/*
135 * stb0899_calc_derot_time
136 * Compute the amount of time needed by the derotator to lock
137 * SymbolRate: Symbol rate
138 * return: derotator time constant (ms)
139 */
140static long stb0899_calc_derot_time(long srate)
141{
142 if (srate > 0)
143 return (100000 / (srate / 1000));
144 else
145 return 0;
146}
147
148/*
149 * stb0899_carr_width
150 * Compute the width of the carrier
151 * return: width of carrier (kHz or Mhz)
152 */
153long stb0899_carr_width(struct stb0899_state *state)
154{
155 struct stb0899_internal *internal = &state->internal;
156
157 return (internal->srate + (internal->srate * internal->rolloff) / 100);
158}
159
160/*
161 * stb0899_first_subrange
162 * Compute the first subrange of the search
163 */
164static void stb0899_first_subrange(struct stb0899_state *state)
165{
166 struct stb0899_internal *internal = &state->internal;
167 struct stb0899_params *params = &state->params;
168 struct stb0899_config *config = state->config;
169
170 int range = 0;
171 u32 bandwidth = 0;
172
173 if (config->tuner_get_bandwidth) {
174 config->tuner_get_bandwidth(&state->frontend, &bandwidth);
175 range = bandwidth - stb0899_carr_width(state) / 2;
176 }
177
178 if (range > 0)
179 internal->sub_range = MIN(internal->srch_range, range);
180 else
181 internal->sub_range = 0;
182
183 internal->freq = params->freq;
184 internal->tuner_offst = 0L;
185 internal->sub_dir = 1;
186}
187
188/*
189 * stb0899_check_tmg
190 * check for timing lock
191 * internal.Ttiming: time to wait for loop lock
192 */
193static enum stb0899_status stb0899_check_tmg(struct stb0899_state *state)
194{
195 struct stb0899_internal *internal = &state->internal;
Arvo Jarveeadf29b2007-11-09 17:24:45 -0300196 int lock;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300197 u8 reg;
Arvo Jarveeadf29b2007-11-09 17:24:45 -0300198 s8 timing;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300199
200 msleep(internal->t_timing);
201
202 reg = stb0899_read_reg(state, STB0899_RTF);
203 STB0899_SETFIELD_VAL(RTF_TIMING_LOOP_FREQ, reg, 0xf2);
204 stb0899_write_reg(state, STB0899_RTF, reg);
205 reg = stb0899_read_reg(state, STB0899_TLIR);
206 lock = STB0899_GETFIELD(TLIR_TMG_LOCK_IND, reg);
207 timing = stb0899_read_reg(state, STB0899_RTF);
208
209 if (lock >= 42) {
Arvo Jarveeadf29b2007-11-09 17:24:45 -0300210 if ((lock > 48) && (ABS(timing) >= 110)) {
Manu Abraham8bd135b2007-07-03 09:53:42 -0300211 internal->status = ANALOGCARRIER;
212 dprintk(state->verbose, FE_DEBUG, 1, "-->ANALOG Carrier !");
213 } else {
214 internal->status = TIMINGOK;
215 dprintk(state->verbose, FE_DEBUG, 1, "------->TIMING OK !");
216 }
217 } else {
218 internal->status = NOTIMING;
219 dprintk(state->verbose, FE_DEBUG, 1, "-->NO TIMING !");
220 }
221 return internal->status;
222}
223
224/*
225 * stb0899_search_tmg
226 * perform a fs/2 zig-zag to find timing
227 */
228static enum stb0899_status stb0899_search_tmg(struct stb0899_state *state)
229{
230 struct stb0899_internal *internal = &state->internal;
231 struct stb0899_params *params = &state->params;
232
233 short int derot_step, derot_freq = 0, derot_limit, next_loop = 3;
234 int index = 0;
235 u8 cfr[2];
236
237 internal->status = NOTIMING;
238
239 /* timing loop computation & symbol rate optimisation */
240 derot_limit = (internal->sub_range / 2L) / internal->mclk;
241 derot_step = (params->srate / 2L) / internal->mclk;
242
243 while ((stb0899_check_tmg(state) != TIMINGOK) && next_loop) {
244 index++;
245 derot_freq += index * internal->direction * derot_step; /* next derot zig zag position */
246
247 if (ABS(derot_freq) > derot_limit)
248 next_loop--;
249
250 if (next_loop) {
251 STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(state->config->inversion * derot_freq));
252 STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(state->config->inversion * derot_freq));
253 stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */
254 }
255 internal->direction = -internal->direction; /* Change zigzag direction */
256 }
257
258 if (internal->status == TIMINGOK) {
259 stb0899_read_regs(state, STB0899_CFRM, cfr, 2); /* get derotator frequency */
260 internal->derot_freq = state->config->inversion * MAKEWORD16(cfr[0], cfr[1]);
261 dprintk(state->verbose, FE_DEBUG, 1, "------->TIMING OK ! Derot Freq = %d", internal->derot_freq);
262 }
263
264 return internal->status;
265}
266
267/*
268 * stb0899_check_carrier
269 * Check for carrier found
270 */
271static enum stb0899_status stb0899_check_carrier(struct stb0899_state *state)
272{
273 struct stb0899_internal *internal = &state->internal;
274 u8 reg;
275
276 msleep(internal->t_derot); /* wait for derotator ok */
277
278 reg = stb0899_read_reg(state, STB0899_CFD);
279 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
Manu Abraham57ad94a2007-07-02 09:51:54 -0300280 stb0899_write_reg(state, STB0899_CFD, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300281
282 reg = stb0899_read_reg(state, STB0899_DSTATUS);
283 dprintk(state->verbose, FE_DEBUG, 1, "--------------------> STB0899_DSTATUS=[0x%02x]", reg);
284 if (STB0899_GETFIELD(CARRIER_FOUND, reg)) {
285 internal->status = CARRIEROK;
286 dprintk(state->verbose, FE_DEBUG, 1, "-------------> CARRIEROK !");
287 } else {
288 internal->status = NOCARRIER;
289 dprintk(state->verbose, FE_DEBUG, 1, "-------------> NOCARRIER !");
290 }
291
292 return internal->status;
293}
294
295/*
296 * stb0899_search_carrier
297 * Search for a QPSK carrier with the derotator
298 */
299static enum stb0899_status stb0899_search_carrier(struct stb0899_state *state)
300{
301 struct stb0899_internal *internal = &state->internal;
302
303 short int derot_freq = 0, last_derot_freq = 0, derot_limit, next_loop = 3;
304 int index = 0;
305 u8 cfr[2];
306 u8 reg;
307
308 internal->status = NOCARRIER;
309 derot_limit = (internal->sub_range / 2L) / internal->mclk;
310 derot_freq = internal->derot_freq;
311
312 reg = stb0899_read_reg(state, STB0899_CFD);
313 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
Manu Abraham57ad94a2007-07-02 09:51:54 -0300314 stb0899_write_reg(state, STB0899_CFD, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300315
316 do {
317 dprintk(state->verbose, FE_DEBUG, 1, "Derot Freq=%d, mclk=%d", derot_freq, internal->mclk);
318 if (stb0899_check_carrier(state) == NOCARRIER) {
319 index++;
320 last_derot_freq = derot_freq;
321 derot_freq += index * internal->direction * internal->derot_step; /* next zig zag derotator position */
322
323 if(ABS(derot_freq) > derot_limit)
324 next_loop--;
325
326 if (next_loop) {
327 reg = stb0899_read_reg(state, STB0899_CFD);
328 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
Manu Abraham57ad94a2007-07-02 09:51:54 -0300329 stb0899_write_reg(state, STB0899_CFD, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300330
331 STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(state->config->inversion * derot_freq));
332 STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(state->config->inversion * derot_freq));
333 stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */
334 }
335 }
336
337 internal->direction = -internal->direction; /* Change zigzag direction */
338 } while ((internal->status != CARRIEROK) && next_loop);
339
340 if (internal->status == CARRIEROK) {
341 stb0899_read_regs(state, STB0899_CFRM, cfr, 2); /* get derotator frequency */
342 internal->derot_freq = state->config->inversion * MAKEWORD16(cfr[0], cfr[1]);
343 dprintk(state->verbose, FE_DEBUG, 1, "----> CARRIER OK !, Derot Freq=%d", internal->derot_freq);
344 } else {
345 internal->derot_freq = last_derot_freq;
346 }
347
348 return internal->status;
349}
350
351/*
352 * stb0899_check_data
353 * Check for data found
354 */
355static enum stb0899_status stb0899_check_data(struct stb0899_state *state)
356{
357 struct stb0899_internal *internal = &state->internal;
358 struct stb0899_params *params = &state->params;
359
360 int lock = 0, index = 0, dataTime = 500, loop;
361 u8 reg;
362
363 internal->status = NODATA;
364
365 /* RESET FEC */
366 reg = stb0899_read_reg(state, STB0899_TSTRES);
367 STB0899_SETFIELD_VAL(FRESACS, reg, 1);
368 stb0899_write_reg(state, STB0899_TSTRES, reg);
369 msleep(1);
370 reg = stb0899_read_reg(state, STB0899_TSTRES);
371 STB0899_SETFIELD_VAL(FRESACS, reg, 0);
372 stb0899_write_reg(state, STB0899_TSTRES, reg);
373
374 if (params->srate <= 2000000)
375 dataTime = 2000;
376 else if (params->srate <= 5000000)
377 dataTime = 1500;
378 else if (params->srate <= 15000000)
379 dataTime = 1000;
380 else
381 dataTime = 500;
382
383 stb0899_write_reg(state, STB0899_DSTATUS2, 0x00); /* force search loop */
384 while (1) {
385 /* WARNING! VIT LOCKED has to be tested before VIT_END_LOOOP */
386 reg = stb0899_read_reg(state, STB0899_VSTATUS);
387 lock = STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg);
388 loop = STB0899_GETFIELD(VSTATUS_END_LOOPVIT, reg);
389
390 if (lock || loop || (index > dataTime))
391 break;
392 index++;
393 }
394
395 if (lock) { /* DATA LOCK indicator */
396 internal->status = DATAOK;
397 dprintk(state->verbose, FE_DEBUG, 1, "-----------------> DATA OK !");
398 }
399
400 return internal->status;
401}
402
403/*
404 * stb0899_search_data
405 * Search for a QPSK carrier with the derotator
406 */
407static enum stb0899_status stb0899_search_data(struct stb0899_state *state)
408{
409 short int derot_freq, derot_step, derot_limit, next_loop = 3;
410 u8 cfr[2];
411 u8 reg;
412 int index = 1;
413
414 struct stb0899_internal *internal = &state->internal;
415 struct stb0899_params *params = &state->params;
416
417 derot_step = (params->srate / 4L) / internal->mclk;
418 derot_limit = (internal->sub_range / 2L) / internal->mclk;
419 derot_freq = internal->derot_freq;
420
421 do {
422 if ((internal->status != CARRIEROK) || (stb0899_check_data(state) != DATAOK)) {
423
424 derot_freq += index * internal->direction * derot_step; /* next zig zag derotator position */
425 if (ABS(derot_freq) > derot_limit)
426 next_loop--;
427
428 if (next_loop) {
429 dprintk(state->verbose, FE_DEBUG, 1, "Derot freq=%d, mclk=%d", derot_freq, internal->mclk);
430 reg = stb0899_read_reg(state, STB0899_CFD);
431 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
Manu Abraham57ad94a2007-07-02 09:51:54 -0300432 stb0899_write_reg(state, STB0899_CFD, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300433
434 STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(state->config->inversion * derot_freq));
435 STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(state->config->inversion * derot_freq));
436 stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */
437
438 stb0899_check_carrier(state);
439 index++;
440 }
441 }
442 internal->direction = -internal->direction; /* change zig zag direction */
443 } while ((internal->status != DATAOK) && next_loop);
444
445 if (internal->status == DATAOK) {
446 stb0899_read_regs(state, STB0899_CFRM, cfr, 2); /* get derotator frequency */
447 internal->derot_freq = state->config->inversion * MAKEWORD16(cfr[0], cfr[1]);
448 dprintk(state->verbose, FE_DEBUG, 1, "------> DATAOK ! Derot Freq=%d", internal->derot_freq);
449 }
450
451 return internal->status;
452}
453
454/*
455 * stb0899_check_range
456 * check if the found frequency is in the correct range
457 */
458static enum stb0899_status stb0899_check_range(struct stb0899_state *state)
459{
460 struct stb0899_internal *internal = &state->internal;
461 struct stb0899_params *params = &state->params;
462
463 int range_offst, tp_freq;
464
465 range_offst = internal->srch_range / 2000;
466 tp_freq = internal->freq + (internal->derot_freq * internal->mclk) / 1000;
467
468 if ((tp_freq >= params->freq - range_offst) && (tp_freq <= params->freq + range_offst)) {
469 internal->status = RANGEOK;
470 dprintk(state->verbose, FE_DEBUG, 1, "----> RANGEOK !");
471 } else {
472 internal->status = OUTOFRANGE;
473 dprintk(state->verbose, FE_DEBUG, 1, "----> OUT OF RANGE !");
474 }
475
476 return internal->status;
477}
478
479/*
480 * NextSubRange
481 * Compute the next subrange of the search
482 */
483static void next_sub_range(struct stb0899_state *state)
484{
485 struct stb0899_internal *internal = &state->internal;
486 struct stb0899_params *params = &state->params;
487
488 long old_sub_range;
489
490 if (internal->sub_dir > 0) {
491 old_sub_range = internal->sub_range;
492 internal->sub_range = MIN((internal->srch_range / 2) -
493 (internal->tuner_offst + internal->sub_range / 2),
494 internal->sub_range);
495
496 if (internal->sub_range < 0)
497 internal->sub_range = 0;
498
499 internal->tuner_offst += (old_sub_range + internal->sub_range) / 2;
500 }
501
502 internal->freq = params->freq + (internal->sub_dir * internal->tuner_offst) / 1000;
503 internal->sub_dir = -internal->sub_dir;
504}
505
506/*
507 * stb0899_dvbs_algo
508 * Search for a signal, timing, carrier and data for a
509 * given frequency in a given range
510 */
511enum stb0899_status stb0899_dvbs_algo(struct stb0899_state *state)
512{
513 struct stb0899_params *params = &state->params;
514 struct stb0899_internal *internal = &state->internal;
515 struct stb0899_config *config = state->config;
516
517 u8 bclc, reg;
Marko Schluessler7d8f1e52007-10-23 19:56:18 -0300518 u8 cfr[2];
Manu Abraham8bd135b2007-07-03 09:53:42 -0300519 u8 eq_const[10];
520 s32 clnI = 3;
521 u32 bandwidth = 0;
522
523 /* BETA values rated @ 99MHz */
524 s32 betaTab[5][4] = {
525 /* 5 10 20 30MBps */
526 { 37, 34, 32, 31 }, /* QPSK 1/2 */
527 { 37, 35, 33, 31 }, /* QPSK 2/3 */
528 { 37, 35, 33, 31 }, /* QPSK 3/4 */
529 { 37, 36, 33, 32 }, /* QPSK 5/6 */
530 { 37, 36, 33, 32 } /* QPSK 7/8 */
531 };
532
533 internal->direction = 1;
534
535 stb0899_set_srate(state, internal->master_clk, params->srate);
536 /* Carrier loop optimization versus symbol rate for acquisition*/
537 if (params->srate <= 5000000) {
538 stb0899_write_reg(state, STB0899_ACLC, 0x89);
539 bclc = stb0899_read_reg(state, STB0899_BCLC);
540 STB0899_SETFIELD_VAL(BETA, bclc, 0x1c);
541 stb0899_write_reg(state, STB0899_BCLC, bclc);
542 clnI = 0;
543 } else if (params->srate <= 15000000) {
544 stb0899_write_reg(state, STB0899_ACLC, 0xc9);
545 bclc = stb0899_read_reg(state, STB0899_BCLC);
546 STB0899_SETFIELD_VAL(BETA, bclc, 0x22);
547 stb0899_write_reg(state, STB0899_BCLC, bclc);
548 clnI = 1;
549 } else if(params->srate <= 25000000) {
550 stb0899_write_reg(state, STB0899_ACLC, 0x89);
551 bclc = stb0899_read_reg(state, STB0899_BCLC);
552 STB0899_SETFIELD_VAL(BETA, bclc, 0x27);
553 stb0899_write_reg(state, STB0899_BCLC, bclc);
554 clnI = 2;
555 } else {
556 stb0899_write_reg(state, STB0899_ACLC, 0xc8);
557 bclc = stb0899_read_reg(state, STB0899_BCLC);
558 STB0899_SETFIELD_VAL(BETA, bclc, 0x29);
559 stb0899_write_reg(state, STB0899_BCLC, bclc);
560 clnI = 3;
561 }
562
563 dprintk(state->verbose, FE_DEBUG, 1, "Set the timing loop to acquisition");
564 /* Set the timing loop to acquisition */
565 stb0899_write_reg(state, STB0899_RTC, 0x46);
566 stb0899_write_reg(state, STB0899_CFD, 0xee);
567
568 /* !! WARNING !!
569 * Do not read any status variables while acquisition,
570 * If any needed, read before the acquisition starts
571 * querying status while acquiring causes the
572 * acquisition to go bad and hence no locks.
573 */
574 dprintk(state->verbose, FE_DEBUG, 1, "Derot Percent=%d Srate=%d mclk=%d",
575 internal->derot_percent, params->srate, internal->mclk);
576
577 /* Initial calculations */
578 internal->derot_step = internal->derot_percent * (params->srate / 1000L) / internal->mclk; /* DerotStep/1000 * Fsymbol */
579 internal->t_timing = stb0899_calc_loop_time(params->srate);
580 internal->t_derot = stb0899_calc_derot_time(params->srate);
581 internal->t_data = 500;
582
583 dprintk(state->verbose, FE_DEBUG, 1, "RESET stream merger");
584 /* RESET Stream merger */
585 reg = stb0899_read_reg(state, STB0899_TSTRES);
586 STB0899_SETFIELD_VAL(FRESRS, reg, 1);
587 stb0899_write_reg(state, STB0899_TSTRES, reg);
588
589 /*
590 * Set KDIVIDER to an intermediate value between
591 * 1/2 and 7/8 for acquisition
592 */
593 reg = stb0899_read_reg(state, STB0899_DEMAPVIT);
594 STB0899_SETFIELD_VAL(DEMAPVIT_KDIVIDER, reg, 60);
595 stb0899_write_reg(state, STB0899_DEMAPVIT, reg);
596
597 stb0899_write_reg(state, STB0899_EQON, 0x01); /* Equalizer OFF while acquiring */
598 stb0899_write_reg(state, STB0899_VITSYNC, 0x19);
599
600 stb0899_first_subrange(state);
601 do {
602 /* Initialisations */
603 cfr[0] = cfr[1] = 0;
604 stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* RESET derotator frequency */
605
606 reg = stb0899_read_reg(state, STB0899_RTF);
607 STB0899_SETFIELD_VAL(RTF_TIMING_LOOP_FREQ, reg, 0);
608 stb0899_write_reg(state, STB0899_RTF, reg);
609 reg = stb0899_read_reg(state, STB0899_CFD);
610 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
Manu Abraham57ad94a2007-07-02 09:51:54 -0300611 stb0899_write_reg(state, STB0899_CFD, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300612
613 internal->derot_freq = 0;
614 internal->status = NOAGC1;
615
616 /* Move tuner to frequency */
617 dprintk(state->verbose, FE_DEBUG, 1, "Tuner set frequency");
618 if (state->config->tuner_set_frequency)
619 state->config->tuner_set_frequency(&state->frontend, internal->freq);
620
Manu Abraham8bd135b2007-07-03 09:53:42 -0300621 if (state->config->tuner_get_frequency)
622 state->config->tuner_get_frequency(&state->frontend, &internal->freq);
623
624 msleep(internal->t_agc1 + internal->t_agc2 + internal->t_timing); /* AGC1, AGC2 and timing loop */
625 dprintk(state->verbose, FE_DEBUG, 1, "current derot freq=%d", internal->derot_freq);
626 internal->status = AGC1OK;
627
628 /* There is signal in the band */
629 if (config->tuner_get_bandwidth)
630 config->tuner_get_bandwidth(&state->frontend, &bandwidth);
631 if (params->srate <= bandwidth / 2)
632 stb0899_search_tmg(state); /* For low rates (SCPC) */
633 else
634 stb0899_check_tmg(state); /* For high rates (MCPC) */
635
636 if (internal->status == TIMINGOK) {
637 dprintk(state->verbose, FE_DEBUG, 1,
638 "TIMING OK ! Derot freq=%d, mclk=%d",
639 internal->derot_freq, internal->mclk);
640
641 if (stb0899_search_carrier(state) == CARRIEROK) { /* Search for carrier */
642 dprintk(state->verbose, FE_DEBUG, 1,
643 "CARRIER OK ! Derot freq=%d, mclk=%d",
644 internal->derot_freq, internal->mclk);
645
646 if (stb0899_search_data(state) == DATAOK) { /* Check for data */
647 dprintk(state->verbose, FE_DEBUG, 1,
648 "DATA OK ! Derot freq=%d, mclk=%d",
649 internal->derot_freq, internal->mclk);
650
651 if (stb0899_check_range(state) == RANGEOK) {
652 dprintk(state->verbose, FE_DEBUG, 1,
653 "RANGE OK ! derot freq=%d, mclk=%d",
654 internal->derot_freq, internal->mclk);
655
656 internal->freq = params->freq + ((internal->derot_freq * internal->mclk) / 1000);
657 reg = stb0899_read_reg(state, STB0899_PLPARM);
658 internal->fecrate = STB0899_GETFIELD(VITCURPUN, reg);
659 dprintk(state->verbose, FE_DEBUG, 1,
660 "freq=%d, internal resultant freq=%d",
661 params->freq, internal->freq);
662
663 dprintk(state->verbose, FE_DEBUG, 1,
664 "internal puncture rate=%d",
665 internal->fecrate);
666 }
667 }
668 }
669 }
670 if (internal->status != RANGEOK)
671 next_sub_range(state);
672
673 } while (internal->sub_range && internal->status != RANGEOK);
674
675 /* Set the timing loop to tracking */
676 stb0899_write_reg(state, STB0899_RTC, 0x33);
677 stb0899_write_reg(state, STB0899_CFD, 0xf7);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300678 /* if locked and range ok, set Kdiv */
679 if (internal->status == RANGEOK) {
680 dprintk(state->verbose, FE_DEBUG, 1, "Locked & Range OK !");
681 stb0899_write_reg(state, STB0899_EQON, 0x41); /* Equalizer OFF while acquiring */
682 stb0899_write_reg(state, STB0899_VITSYNC, 0x39); /* SN to b'11 for acquisition */
683
684 /*
685 * Carrier loop optimization versus
686 * symbol Rate/Puncture Rate for Tracking
687 */
Arvo Jarveb655b6c2007-10-30 09:16:17 -0300688 reg = stb0899_read_reg(state, STB0899_BCLC);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300689 switch (internal->fecrate) {
690 case STB0899_FEC_1_2: /* 13 */
Arvo Jarveb655b6c2007-10-30 09:16:17 -0300691 stb0899_write_reg(state, STB0899_DEMAPVIT, 0x1a);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300692 STB0899_SETFIELD_VAL(BETA, reg, betaTab[0][clnI]);
693 stb0899_write_reg(state, STB0899_BCLC, reg);
694 break;
695 case STB0899_FEC_2_3: /* 18 */
Arvo Jarveb655b6c2007-10-30 09:16:17 -0300696 stb0899_write_reg(state, STB0899_DEMAPVIT, 44);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300697 STB0899_SETFIELD_VAL(BETA, reg, betaTab[1][clnI]);
698 stb0899_write_reg(state, STB0899_BCLC, reg);
699 break;
700 case STB0899_FEC_3_4: /* 21 */
Arvo Jarveb655b6c2007-10-30 09:16:17 -0300701 stb0899_write_reg(state, STB0899_DEMAPVIT, 60);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300702 STB0899_SETFIELD_VAL(BETA, reg, betaTab[2][clnI]);
703 stb0899_write_reg(state, STB0899_BCLC, reg);
704 break;
705 case STB0899_FEC_5_6: /* 24 */
Arvo Jarveb655b6c2007-10-30 09:16:17 -0300706 stb0899_write_reg(state, STB0899_DEMAPVIT, 75);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300707 STB0899_SETFIELD_VAL(BETA, reg, betaTab[3][clnI]);
708 stb0899_write_reg(state, STB0899_BCLC, reg);
709 break;
710 case STB0899_FEC_6_7: /* 25 */
Arvo Jarveb655b6c2007-10-30 09:16:17 -0300711 stb0899_write_reg(state, STB0899_DEMAPVIT, 88);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300712 stb0899_write_reg(state, STB0899_ACLC, 0x88);
713 stb0899_write_reg(state, STB0899_BCLC, 0x9a);
714 break;
715 case STB0899_FEC_7_8: /* 26 */
Arvo Jarveb655b6c2007-10-30 09:16:17 -0300716 stb0899_write_reg(state, STB0899_DEMAPVIT, 94);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300717 STB0899_SETFIELD_VAL(BETA, reg, betaTab[4][clnI]);
718 stb0899_write_reg(state, STB0899_BCLC, reg);
719 break;
720 default:
721 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported Puncture Rate");
722 break;
723 }
724 /* release stream merger RESET */
725 reg = stb0899_read_reg(state, STB0899_TSTRES);
726 STB0899_SETFIELD_VAL(FRESRS, reg, 0);
727 stb0899_write_reg(state, STB0899_TSTRES, reg);
728
729 /* disable carrier detector */
730 reg = stb0899_read_reg(state, STB0899_CFD);
731 STB0899_SETFIELD_VAL(CFD_ON, reg, 0);
Manu Abraham57ad94a2007-07-02 09:51:54 -0300732 stb0899_write_reg(state, STB0899_CFD, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -0300733
734 stb0899_read_regs(state, STB0899_EQUAI1, eq_const, 10);
735 }
736
737 return internal->status;
738}
739
740/*
741 * stb0899_dvbs2_config_uwp
742 * Configure UWP state machine
743 */
744static void stb0899_dvbs2_config_uwp(struct stb0899_state *state)
745{
746 struct stb0899_internal *internal = &state->internal;
747 struct stb0899_config *config = state->config;
748 u32 uwp1, uwp2, uwp3, reg;
749
750 uwp1 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
751 uwp2 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL2);
752 uwp3 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL3);
753
754 STB0899_SETFIELD_VAL(UWP_ESN0_AVE, uwp1, config->esno_ave);
755 STB0899_SETFIELD_VAL(UWP_ESN0_QUANT, uwp1, config->esno_quant);
756 STB0899_SETFIELD_VAL(UWP_TH_SOF, uwp1, config->uwp_threshold_sof);
757
758 STB0899_SETFIELD_VAL(FE_COARSE_TRK, uwp2, internal->av_frame_coarse);
759 STB0899_SETFIELD_VAL(FE_FINE_TRK, uwp2, internal->av_frame_fine);
760 STB0899_SETFIELD_VAL(UWP_MISS_TH, uwp2, config->miss_threshold);
761
762 STB0899_SETFIELD_VAL(UWP_TH_ACQ, uwp3, config->uwp_threshold_acq);
763 STB0899_SETFIELD_VAL(UWP_TH_TRACK, uwp3, config->uwp_threshold_track);
764
765 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL1, STB0899_OFF0_UWP_CNTRL1, uwp1);
766 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL2, STB0899_OFF0_UWP_CNTRL2, uwp2);
767 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL3, STB0899_OFF0_UWP_CNTRL3, uwp3);
768
769 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, SOF_SRCH_TO);
770 STB0899_SETFIELD_VAL(SOF_SEARCH_TIMEOUT, reg, config->sof_search_timeout);
771 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_SOF_SRCH_TO, STB0899_OFF0_SOF_SRCH_TO, reg);
772}
773
774/*
775 * stb0899_dvbs2_config_csm_auto
776 * Set CSM to AUTO mode
777 */
778static void stb0899_dvbs2_config_csm_auto(struct stb0899_state *state)
779{
780 u32 reg;
781
782 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
783 STB0899_SETFIELD_VAL(CSM_AUTO_PARAM, reg, 1);
784 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, reg);
785}
786
787long Log2Int(int number)
788{
789 int i;
790
791 i = 0;
792 while ((1 << i) <= ABS(number))
793 i++;
794
795 if (number == 0)
796 i = 1;
797
798 return i - 1;
799}
800
801/*
802 * stb0899_dvbs2_calc_srate
803 * compute BTR_NOM_FREQ for the symbol rate
804 */
805static u32 stb0899_dvbs2_calc_srate(struct stb0899_state *state)
806{
807 struct stb0899_internal *internal = &state->internal;
808 struct stb0899_config *config = state->config;
809
810 u32 dec_ratio, dec_rate, decim, remain, intval, btr_nom_freq;
811 u32 master_clk, srate;
812
813 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
814 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
815 dec_rate = Log2Int(dec_ratio);
816 decim = 1 << dec_rate;
817 master_clk = internal->master_clk / 1000;
818 srate = internal->srate / 1000;
819
820 if (decim <= 4) {
821 intval = (decim * (1 << (config->btr_nco_bits - 1))) / master_clk;
822 remain = (decim * (1 << (config->btr_nco_bits - 1))) % master_clk;
823 } else {
824 intval = (1 << (config->btr_nco_bits - 1)) / (master_clk / 100) * decim / 100;
825 remain = (decim * (1 << (config->btr_nco_bits - 1))) % master_clk;
826 }
827 btr_nom_freq = (intval * srate) + ((remain * srate) / master_clk);
828
829 return btr_nom_freq;
830}
831
832/*
833 * stb0899_dvbs2_calc_dev
834 * compute the correction to be applied to symbol rate
835 */
836static u32 stb0899_dvbs2_calc_dev(struct stb0899_state *state)
837{
838 struct stb0899_internal *internal = &state->internal;
839 u32 dec_ratio, correction, master_clk, srate;
840
841 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
842 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
843
844 master_clk = internal->master_clk / 1000; /* for integer Caculation*/
845 srate = internal->srate / 1000; /* for integer Caculation*/
846 correction = (512 * master_clk) / (2 * dec_ratio * srate);
847
848 return correction;
849}
850
851/*
852 * stb0899_dvbs2_set_srate
853 * Set DVBS2 symbol rate
854 */
855static void stb0899_dvbs2_set_srate(struct stb0899_state *state)
856{
857 struct stb0899_internal *internal = &state->internal;
858
859 u32 dec_ratio, dec_rate, win_sel, decim, f_sym, btr_nom_freq;
860 u32 correction, freq_adj, band_lim, decim_cntrl, reg;
861 u8 anti_alias;
862
863 /*set decimation to 1*/
864 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
865 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
866 dec_rate = Log2Int(dec_ratio);
867
868 win_sel = 0;
869 if (dec_rate >= 5)
870 win_sel = dec_rate - 4;
871
872 decim = (1 << dec_rate);
873 /* (FSamp/Fsymbol *100) for integer Caculation */
874 f_sym = internal->master_clk / ((decim * internal->srate) / 1000);
875
876 if (f_sym <= 2250) /* don't band limit signal going into btr block*/
877 band_lim = 1;
878 else
879 band_lim = 0; /* band limit signal going into btr block*/
880
881 decim_cntrl = ((win_sel << 3) & 0x18) + ((band_lim << 5) & 0x20) + (dec_rate & 0x7);
882 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DECIM_CNTRL, STB0899_OFF0_DECIM_CNTRL, decim_cntrl);
883
884 if (f_sym <= 3450)
885 anti_alias = 0;
886 else if (f_sym <= 4250)
887 anti_alias = 1;
888 else
889 anti_alias = 2;
890
891 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ANTI_ALIAS_SEL, STB0899_OFF0_ANTI_ALIAS_SEL, anti_alias);
892 btr_nom_freq = stb0899_dvbs2_calc_srate(state);
893 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_NOM_FREQ, STB0899_OFF0_BTR_NOM_FREQ, btr_nom_freq);
894
895 correction = stb0899_dvbs2_calc_dev(state);
896 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_CNTRL);
897 STB0899_SETFIELD_VAL(BTR_FREQ_CORR, reg, correction);
898 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_CNTRL, STB0899_OFF0_BTR_CNTRL, reg);
899
900 /* scale UWP+CSM frequency to sample rate*/
901 freq_adj = internal->srate / (internal->master_clk / 4096);
902 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_FREQ_ADJ_SCALE, STB0899_OFF0_FREQ_ADJ_SCALE, freq_adj);
903}
904
905/*
906 * stb0899_dvbs2_set_btr_loopbw
907 * set bit timing loop bandwidth as a percentage of the symbol rate
908 */
909static void stb0899_dvbs2_set_btr_loopbw(struct stb0899_state *state)
910{
911 struct stb0899_internal *internal = &state->internal;
912 struct stb0899_config *config = state->config;
913
914 u32 sym_peak = 23, zeta = 707, loopbw_percent = 60;
915 s32 dec_ratio, dec_rate, k_btr1_rshft, k_btr1, k_btr0_rshft;
916 s32 k_btr0, k_btr2_rshft, k_direct_shift, k_indirect_shift;
917 u32 decim, K, wn, k_direct, k_indirect;
918 u32 reg;
919
920 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
921 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
922 dec_rate = Log2Int(dec_ratio);
923 decim = (1 << dec_rate);
924
925 sym_peak *= 576000;
926 K = (1 << config->btr_nco_bits) / (internal->master_clk / 1000);
927 K *= (internal->srate / 1000000) * decim; /*k=k 10^-8*/
Manu Abraham8bd135b2007-07-03 09:53:42 -0300928
929 if (K != 0) {
Manu Abrahamb797c202007-02-24 09:14:39 -0300930 K = sym_peak / K;
Manu Abraham8bd135b2007-07-03 09:53:42 -0300931 wn = (4 * zeta * zeta) + 1000000;
932 wn = (2 * (loopbw_percent * 1000) * 40 * zeta) /wn; /*wn =wn 10^-8*/
933
934 k_indirect = (wn * wn) / K;
935 k_indirect = k_indirect; /*kindirect = kindirect 10^-6*/
936 k_direct = (2 * wn * zeta) / K; /*kDirect = kDirect 10^-2*/
937 k_direct *= 100;
938
939 k_direct_shift = Log2Int(k_direct) - Log2Int(10000) - 2;
940 k_btr1_rshft = (-1 * k_direct_shift) + config->btr_gain_shift_offset;
941 k_btr1 = k_direct / (1 << k_direct_shift);
942 k_btr1 /= 10000;
943
944 k_indirect_shift = Log2Int(k_indirect + 15) - 20 /*- 2*/;
945 k_btr0_rshft = (-1 * k_indirect_shift) + config->btr_gain_shift_offset;
946 k_btr0 = k_indirect * (1 << (-k_indirect_shift));
947 k_btr0 /= 1000000;
948
949 k_btr2_rshft = 0;
950 if (k_btr0_rshft > 15) {
951 k_btr2_rshft = k_btr0_rshft - 15;
952 k_btr0_rshft = 15;
953 }
954 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_LOOP_GAIN);
955 STB0899_SETFIELD_VAL(KBTR0_RSHFT, reg, k_btr0_rshft);
956 STB0899_SETFIELD_VAL(KBTR0, reg, k_btr0);
957 STB0899_SETFIELD_VAL(KBTR1_RSHFT, reg, k_btr1_rshft);
958 STB0899_SETFIELD_VAL(KBTR1, reg, k_btr1);
959 STB0899_SETFIELD_VAL(KBTR2_RSHFT, reg, k_btr2_rshft);
960 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_LOOP_GAIN, STB0899_OFF0_BTR_LOOP_GAIN, reg);
961 } else
962 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_LOOP_GAIN, STB0899_OFF0_BTR_LOOP_GAIN, 0xc4c4f);
963}
964
965/*
966 * stb0899_dvbs2_set_carr_freq
967 * set nominal frequency for carrier search
968 */
969static void stb0899_dvbs2_set_carr_freq(struct stb0899_state *state, s32 carr_freq, u32 master_clk)
970{
971 struct stb0899_config *config = state->config;
972 s32 crl_nom_freq;
973 u32 reg;
974
975 crl_nom_freq = (1 << config->crl_nco_bits) / master_clk;
976 crl_nom_freq *= carr_freq;
977 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
978 STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, crl_nom_freq);
979 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
980}
981
982/*
983 * stb0899_dvbs2_init_calc
984 * Initialize DVBS2 UWP, CSM, carrier and timing loops
985 */
986static void stb0899_dvbs2_init_calc(struct stb0899_state *state)
987{
988 struct stb0899_internal *internal = &state->internal;
989 s32 steps, step_size;
990 u32 range, reg;
991
992 /* config uwp and csm */
993 stb0899_dvbs2_config_uwp(state);
994 stb0899_dvbs2_config_csm_auto(state);
995
996 /* initialize BTR */
997 stb0899_dvbs2_set_srate(state);
998 stb0899_dvbs2_set_btr_loopbw(state);
999
1000 if (internal->srate / 1000000 >= 15)
1001 step_size = (1 << 17) / 5;
1002 else if (internal->srate / 1000000 >= 10)
1003 step_size = (1 << 17) / 7;
1004 else if (internal->srate / 1000000 >= 5)
1005 step_size = (1 << 17) / 10;
1006 else
1007 step_size = (1 << 17) / 4;
1008
1009 range = internal->srch_range / 1000000;
1010 steps = (10 * range * (1 << 17)) / (step_size * (internal->srate / 1000000));
1011 steps = (steps + 6) / 10;
1012 steps = (steps == 0) ? 1 : steps;
1013 if (steps % 2 == 0)
1014 stb0899_dvbs2_set_carr_freq(state, internal->center_freq -
1015 (internal->step_size * (internal->srate / 20000000)),
1016 (internal->master_clk) / 1000000);
1017 else
1018 stb0899_dvbs2_set_carr_freq(state, internal->center_freq, (internal->master_clk) / 1000000);
1019
1020 /*Set Carrier Search params (zigzag, num steps and freq step size*/
1021 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, ACQ_CNTRL2);
1022 STB0899_SETFIELD_VAL(ZIGZAG, reg, 1);
1023 STB0899_SETFIELD_VAL(NUM_STEPS, reg, steps);
1024 STB0899_SETFIELD_VAL(FREQ_STEPSIZE, reg, step_size);
1025 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ACQ_CNTRL2, STB0899_OFF0_ACQ_CNTRL2, reg);
1026}
1027
1028/*
1029 * stb0899_dvbs2_btr_init
1030 * initialize the timing loop
1031 */
1032static void stb0899_dvbs2_btr_init(struct stb0899_state *state)
1033{
1034 u32 reg;
1035
1036 /* set enable BTR loopback */
1037 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_CNTRL);
1038 STB0899_SETFIELD_VAL(INTRP_PHS_SENSE, reg, 1);
1039 STB0899_SETFIELD_VAL(BTR_ERR_ENA, reg, 1);
1040 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_CNTRL, STB0899_OFF0_BTR_CNTRL, reg);
1041
1042 /* fix btr freq accum at 0 */
1043 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_FREQ_INIT, STB0899_OFF0_BTR_FREQ_INIT, 0x10000000);
1044 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_FREQ_INIT, STB0899_OFF0_BTR_FREQ_INIT, 0x00000000);
1045
1046 /* fix btr freq accum at 0 */
1047 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_PHS_INIT, STB0899_OFF0_BTR_PHS_INIT, 0x10000000);
1048 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_PHS_INIT, STB0899_OFF0_BTR_PHS_INIT, 0x00000000);
1049}
1050
1051/*
1052 * stb0899_dvbs2_reacquire
1053 * trigger a DVB-S2 acquisition
1054 */
1055static void stb0899_dvbs2_reacquire(struct stb0899_state *state)
1056{
1057 u32 reg = 0;
1058
1059 /* demod soft reset */
1060 STB0899_SETFIELD_VAL(DVBS2_RESET, reg, 1);
1061 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_RESET_CNTRL, STB0899_OFF0_RESET_CNTRL, reg);
1062
1063 /*Reset Timing Loop */
1064 stb0899_dvbs2_btr_init(state);
1065
1066 /* reset Carrier loop */
1067 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_FREQ_INIT, STB0899_OFF0_CRL_FREQ_INIT, (1 << 30));
1068 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_FREQ_INIT, STB0899_OFF0_CRL_FREQ_INIT, 0);
1069 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_LOOP_GAIN, STB0899_OFF0_CRL_LOOP_GAIN, 0);
1070 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_PHS_INIT, STB0899_OFF0_CRL_PHS_INIT, (1 << 30));
1071 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_PHS_INIT, STB0899_OFF0_CRL_PHS_INIT, 0);
1072
1073 /*release demod soft reset */
1074 reg = 0;
1075 STB0899_SETFIELD_VAL(DVBS2_RESET, reg, 0);
1076 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_RESET_CNTRL, STB0899_OFF0_RESET_CNTRL, reg);
1077
1078 /* start acquisition process */
1079 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ACQUIRE_TRIG, STB0899_OFF0_ACQUIRE_TRIG, 1);
1080 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_LOCK_LOST, STB0899_OFF0_LOCK_LOST, 0);
1081
1082 /* equalizer Init */
1083 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQUALIZER_INIT, STB0899_OFF0_EQUALIZER_INIT, 1);
1084
1085 /*Start equilizer */
1086 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQUALIZER_INIT, STB0899_OFF0_EQUALIZER_INIT, 0);
1087
1088 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
1089 STB0899_SETFIELD_VAL(EQ_SHIFT, reg, 0);
1090 STB0899_SETFIELD_VAL(EQ_DISABLE_UPDATE, reg, 0);
1091 STB0899_SETFIELD_VAL(EQ_DELAY, reg, 0x05);
1092 STB0899_SETFIELD_VAL(EQ_ADAPT_MODE, reg, 0x01);
1093 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
1094
1095 /* RESET Packet delineator */
1096 stb0899_write_reg(state, STB0899_PDELCTRL, 0x4a);
1097}
1098
1099/*
1100 * stb0899_dvbs2_get_dmd_status
1101 * get DVB-S2 Demod LOCK status
1102 */
1103static enum stb0899_status stb0899_dvbs2_get_dmd_status(struct stb0899_state *state, int timeout)
1104{
1105 int time = -10, lock = 0, uwp, csm;
1106 u32 reg;
1107
1108 do {
1109 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS);
1110 dprintk(state->verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg);
1111 if (STB0899_GETFIELD(IF_AGC_LOCK, reg))
1112 dprintk(state->verbose, FE_DEBUG, 1, "------------->IF AGC LOCKED !");
1113 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1114 dprintk(state->verbose, FE_DEBUG, 1, "----------->DMD STAT2=[0x%02x]", reg);
1115 uwp = STB0899_GETFIELD(UWP_LOCK, reg);
1116 csm = STB0899_GETFIELD(CSM_LOCK, reg);
1117 if (uwp && csm)
1118 lock = 1;
1119
1120 time += 10;
1121 msleep(10);
1122
1123 } while ((!lock) && (time <= timeout));
1124
1125 if (lock) {
1126 dprintk(state->verbose, FE_DEBUG, 1, "----------------> DVB-S2 LOCK !");
1127 return DVBS2_DEMOD_LOCK;
1128 } else {
1129 return DVBS2_DEMOD_NOLOCK;
1130 }
1131}
1132
1133/*
1134 * stb0899_dvbs2_get_data_lock
1135 * get FEC status
1136 */
1137static int stb0899_dvbs2_get_data_lock(struct stb0899_state *state, int timeout)
1138{
1139 int time = 0, lock = 0;
1140 u8 reg;
1141
1142 while ((!lock) && (time < timeout)) {
1143 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1144 dprintk(state->verbose, FE_DEBUG, 1, "---------> CFGPDELSTATUS=[0x%02x]", reg);
1145 lock = STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg);
1146 time++;
1147 }
1148
1149 return lock;
1150}
1151
1152/*
1153 * stb0899_dvbs2_get_fec_status
1154 * get DVB-S2 FEC LOCK status
1155 */
1156static enum stb0899_status stb0899_dvbs2_get_fec_status(struct stb0899_state *state, int timeout)
1157{
1158 int time = 0, Locked;
1159
1160 do {
1161 Locked = stb0899_dvbs2_get_data_lock(state, 1);
1162 time++;
1163 msleep(1);
1164
1165 } while ((!Locked) && (time < timeout));
1166
1167 if (Locked) {
1168 dprintk(state->verbose, FE_DEBUG, 1, "---------->DVB-S2 FEC LOCK !");
1169 return DVBS2_FEC_LOCK;
1170 } else {
1171 return DVBS2_FEC_NOLOCK;
1172 }
1173}
1174
1175
1176/*
1177 * stb0899_dvbs2_init_csm
1178 * set parameters for manual mode
1179 */
1180static void stb0899_dvbs2_init_csm(struct stb0899_state *state, int pilots, enum stb0899_modcod modcod)
1181{
1182 struct stb0899_internal *internal = &state->internal;
1183
1184 s32 dvt_tbl = 1, two_pass = 0, agc_gain = 6, agc_shift = 0, loop_shift = 0, phs_diff_thr = 0x80;
1185 s32 gamma_acq, gamma_rho_acq, gamma_trk, gamma_rho_trk, lock_count_thr;
1186 u32 csm1, csm2, csm3, csm4;
1187
1188 if (((internal->master_clk / internal->srate) <= 4) && (modcod <= 11) && (pilots == 1)) {
1189 switch (modcod) {
1190 case STB0899_QPSK_12:
1191 gamma_acq = 25;
1192 gamma_rho_acq = 2700;
1193 gamma_trk = 12;
1194 gamma_rho_trk = 180;
1195 lock_count_thr = 8;
1196 break;
1197 case STB0899_QPSK_35:
1198 gamma_acq = 38;
1199 gamma_rho_acq = 7182;
1200 gamma_trk = 14;
1201 gamma_rho_trk = 308;
1202 lock_count_thr = 8;
1203 break;
1204 case STB0899_QPSK_23:
1205 gamma_acq = 42;
1206 gamma_rho_acq = 9408;
1207 gamma_trk = 17;
1208 gamma_rho_trk = 476;
1209 lock_count_thr = 8;
1210 break;
1211 case STB0899_QPSK_34:
1212 gamma_acq = 53;
1213 gamma_rho_acq = 16642;
1214 gamma_trk = 19;
1215 gamma_rho_trk = 646;
1216 lock_count_thr = 8;
1217 break;
1218 case STB0899_QPSK_45:
1219 gamma_acq = 53;
1220 gamma_rho_acq = 17119;
1221 gamma_trk = 22;
1222 gamma_rho_trk = 880;
1223 lock_count_thr = 8;
1224 break;
1225 case STB0899_QPSK_56:
1226 gamma_acq = 55;
1227 gamma_rho_acq = 19250;
1228 gamma_trk = 23;
1229 gamma_rho_trk = 989;
1230 lock_count_thr = 8;
1231 break;
1232 case STB0899_QPSK_89:
1233 gamma_acq = 60;
1234 gamma_rho_acq = 24240;
1235 gamma_trk = 24;
1236 gamma_rho_trk = 1176;
1237 lock_count_thr = 8;
1238 break;
1239 case STB0899_QPSK_910:
1240 gamma_acq = 66;
1241 gamma_rho_acq = 29634;
1242 gamma_trk = 24;
1243 gamma_rho_trk = 1176;
1244 lock_count_thr = 8;
1245 break;
1246 default:
1247 gamma_acq = 66;
1248 gamma_rho_acq = 29634;
1249 gamma_trk = 24;
1250 gamma_rho_trk = 1176;
1251 lock_count_thr = 8;
1252 break;
1253 }
1254
1255 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1256 STB0899_SETFIELD_VAL(CSM_AUTO_PARAM, csm1, 0);
1257 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1258
1259 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1260 csm2 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL2);
1261 csm3 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL3);
1262 csm4 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL4);
1263
1264 STB0899_SETFIELD_VAL(CSM_DVT_TABLE, csm1, dvt_tbl);
1265 STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, two_pass);
1266 STB0899_SETFIELD_VAL(CSM_AGC_GAIN, csm1, agc_gain);
1267 STB0899_SETFIELD_VAL(CSM_AGC_SHIFT, csm1, agc_shift);
1268 STB0899_SETFIELD_VAL(FE_LOOP_SHIFT, csm1, loop_shift);
1269 STB0899_SETFIELD_VAL(CSM_GAMMA_ACQ, csm2, gamma_acq);
1270 STB0899_SETFIELD_VAL(CSM_GAMMA_RHOACQ, csm2, gamma_rho_acq);
1271 STB0899_SETFIELD_VAL(CSM_GAMMA_TRACK, csm3, gamma_trk);
1272 STB0899_SETFIELD_VAL(CSM_GAMMA_RHOTRACK, csm3, gamma_rho_trk);
1273 STB0899_SETFIELD_VAL(CSM_LOCKCOUNT_THRESH, csm4, lock_count_thr);
1274 STB0899_SETFIELD_VAL(CSM_PHASEDIFF_THRESH, csm4, phs_diff_thr);
1275
1276 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1277 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL2, STB0899_OFF0_CSM_CNTRL2, csm2);
1278 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL3, STB0899_OFF0_CSM_CNTRL3, csm3);
1279 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL4, STB0899_OFF0_CSM_CNTRL4, csm4);
1280 }
1281}
1282
1283/*
1284 * stb0899_dvbs2_get_srate
1285 * get DVB-S2 Symbol Rate
1286 */
1287static u32 stb0899_dvbs2_get_srate(struct stb0899_state *state)
1288{
1289 struct stb0899_internal *internal = &state->internal;
1290 struct stb0899_config *config = state->config;
1291
1292 u32 bTrNomFreq, srate, decimRate, intval1, intval2, reg;
1293 int div1, div2, rem1, rem2;
1294
1295 div1 = config->btr_nco_bits / 2;
1296 div2 = config->btr_nco_bits - div1 - 1;
1297
1298 bTrNomFreq = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_NOM_FREQ);
1299
1300 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DECIM_CNTRL);
1301 decimRate = STB0899_GETFIELD(DECIM_RATE, reg);
1302 decimRate = (1 << decimRate);
1303
1304 intval1 = internal->master_clk / (1 << div1);
1305 intval2 = bTrNomFreq / (1 << div2);
1306
1307 rem1 = internal->master_clk % (1 << div1);
1308 rem2 = bTrNomFreq % (1 << div2);
1309 /* only for integer calculation */
1310 srate = (intval1 * intval2) + ((intval1 * rem2) / (1 << div2)) + ((intval2 * rem1) / (1 << div1));
1311 srate /= decimRate; /*symbrate = (btrnomfreq_register_val*MasterClock)/2^(27+decim_rate_field) */
1312
1313 return srate;
1314}
1315
1316/*
1317 * stb0899_dvbs2_algo
1318 * Search for signal, timing, carrier and data for a given
1319 * frequency in a given range
1320 */
1321enum stb0899_status stb0899_dvbs2_algo(struct stb0899_state *state)
1322{
1323 struct stb0899_internal *internal = &state->internal;
1324 enum stb0899_modcod modcod;
1325
1326 s32 offsetfreq, searchTime, FecLockTime, pilots, iqSpectrum;
1327 int i = 0;
1328 u32 reg, csm1;
1329
1330 if (internal->srate <= 2000000) {
1331 searchTime = 5000; /* 5000 ms max time to lock UWP and CSM, SYMB <= 2Mbs */
1332 FecLockTime = 350; /* 350 ms max time to lock FEC, SYMB <= 2Mbs */
1333 } else if (internal->srate <= 5000000) {
1334 searchTime = 2500; /* 2500 ms max time to lock UWP and CSM, 2Mbs < SYMB <= 5Mbs */
1335 FecLockTime = 170; /* 170 ms max time to lock FEC, 2Mbs< SYMB <= 5Mbs */
1336 } else if (internal->srate <= 10000000) {
1337 searchTime = 1500; /* 1500 ms max time to lock UWP and CSM, 5Mbs <SYMB <= 10Mbs */
1338 FecLockTime = 80; /* 80 ms max time to lock FEC, 5Mbs< SYMB <= 10Mbs */
1339 } else if (internal->srate <= 15000000) {
1340 searchTime = 500; /* 500 ms max time to lock UWP and CSM, 10Mbs <SYMB <= 15Mbs */
1341 FecLockTime = 50; /* 50 ms max time to lock FEC, 10Mbs< SYMB <= 15Mbs */
1342 } else if (internal->srate <= 20000000) {
1343 searchTime = 300; /* 300 ms max time to lock UWP and CSM, 15Mbs < SYMB <= 20Mbs */
1344 FecLockTime = 30; /* 50 ms max time to lock FEC, 15Mbs< SYMB <= 20Mbs */
1345 } else if (internal->srate <= 25000000) {
1346 searchTime = 250; /* 250 ms max time to lock UWP and CSM, 20 Mbs < SYMB <= 25Mbs */
1347 FecLockTime = 25; /* 25 ms max time to lock FEC, 20Mbs< SYMB <= 25Mbs */
1348 } else {
1349 searchTime = 150; /* 150 ms max time to lock UWP and CSM, SYMB > 25Mbs */
1350 FecLockTime = 20; /* 20 ms max time to lock FEC, 20Mbs< SYMB <= 25Mbs */
1351 }
1352
1353 /* Maintain Stream Merger in reset during acquisition */
1354 reg = stb0899_read_reg(state, STB0899_TSTRES);
1355 STB0899_SETFIELD_VAL(FRESRS, reg, 1);
1356 stb0899_write_reg(state, STB0899_TSTRES, reg);
1357
1358 /* Move tuner to frequency */
1359 if (state->config->tuner_set_frequency)
1360 state->config->tuner_set_frequency(&state->frontend, internal->freq);
1361 if (state->config->tuner_get_frequency)
1362 state->config->tuner_get_frequency(&state->frontend, &internal->freq);
1363
1364 /* Set IF AGC to acquisition */
1365 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
1366 STB0899_SETFIELD_VAL(IF_LOOP_GAIN, reg, 4);
1367 STB0899_SETFIELD_VAL(IF_AGC_REF, reg, 32);
1368 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
1369
1370 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL2);
1371 STB0899_SETFIELD_VAL(IF_AGC_DUMP_PER, reg, 0);
1372 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL2, STB0899_OFF0_IF_AGC_CNTRL2, reg);
1373
1374 /* Initialisation */
1375 stb0899_dvbs2_init_calc(state);
1376
1377 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
1378 switch (internal->inversion) {
1379 case IQ_SWAP_OFF:
1380 STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, 0);
1381 break;
1382 case IQ_SWAP_ON:
1383 STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, 1);
1384 break;
1385 case IQ_SWAP_AUTO: /* use last successful search first */
1386 STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, 1);
1387 break;
1388 }
1389 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DMD_CNTRL2, STB0899_OFF0_DMD_CNTRL2, reg);
1390 stb0899_dvbs2_reacquire(state);
1391
1392 /* Wait for demod lock (UWP and CSM) */
1393 internal->status = stb0899_dvbs2_get_dmd_status(state, searchTime);
1394
1395 if (internal->status == DVBS2_DEMOD_LOCK) {
1396 dprintk(state->verbose, FE_DEBUG, 1, "------------> DVB-S2 DEMOD LOCK !");
1397 i = 0;
1398 /* Demod Locked, check FEC status */
1399 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1400
1401 /*If false lock (UWP and CSM Locked but no FEC) try 3 time max*/
1402 while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
1403 /* Read the frequency offset*/
1404 offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
1405
1406 /* Set the Nominal frequency to the found frequency offset for the next reacquire*/
1407 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
1408 STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, offsetfreq);
Marko Schluessler08bcdbe2007-09-21 18:40:14 -03001409 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001410 stb0899_dvbs2_reacquire(state);
1411 internal->status = stb0899_dvbs2_get_fec_status(state, searchTime);
1412 i++;
1413 }
1414 }
1415
1416 if (internal->status != DVBS2_FEC_LOCK) {
1417 if (internal->inversion == IQ_SWAP_AUTO) {
1418 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
1419 iqSpectrum = STB0899_GETFIELD(SPECTRUM_INVERT, reg);
1420 /* IQ Spectrum Inversion */
1421 STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, !iqSpectrum);
1422 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DMD_CNTRL2, STB0899_OFF0_DMD_CNTRL2, reg);
1423 /* start acquistion process */
1424 stb0899_dvbs2_reacquire(state);
1425
1426 /* Wait for demod lock (UWP and CSM) */
1427 internal->status = stb0899_dvbs2_get_dmd_status(state, searchTime);
1428 if (internal->status == DVBS2_DEMOD_LOCK) {
1429 i = 0;
1430 /* Demod Locked, check FEC */
1431 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1432 /*try thrice for false locks, (UWP and CSM Locked but no FEC) */
1433 while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
1434 /* Read the frequency offset*/
1435 offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
1436
1437 /* Set the Nominal frequency to the found frequency offset for the next reacquire*/
1438 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
1439 STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, offsetfreq);
Marko Schluessler08bcdbe2007-09-21 18:40:14 -03001440 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
Manu Abraham8bd135b2007-07-03 09:53:42 -03001441
1442 stb0899_dvbs2_reacquire(state);
1443 internal->status = stb0899_dvbs2_get_fec_status(state, searchTime);
1444 i++;
1445 }
1446 }
1447/*
1448 if (pParams->DVBS2State == FE_DVBS2_FEC_LOCKED)
1449 pParams->IQLocked = !iqSpectrum;
1450*/
1451 }
1452 }
1453 if (internal->status == DVBS2_FEC_LOCK) {
1454 dprintk(state->verbose, FE_DEBUG, 1, "----------------> DVB-S2 FEC Lock !");
1455 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1456 modcod = STB0899_GETFIELD(UWP_DECODE_MOD, reg) >> 2;
1457 pilots = STB0899_GETFIELD(UWP_DECODE_MOD, reg) & 0x01;
1458
1459 if ((((10 * internal->master_clk) / (internal->srate / 10)) <= 410) &&
1460 (INRANGE(STB0899_QPSK_23, modcod, STB0899_QPSK_910)) &&
1461 (pilots == 1)) {
1462
1463 stb0899_dvbs2_init_csm(state, pilots, modcod);
1464 /* Wait for UWP,CSM and data LOCK 20ms max */
1465 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1466
1467 i = 0;
1468 while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
1469 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1470 STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, 1);
1471 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1472 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1473 STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, 0);
1474 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1475
1476 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1477 i++;
1478 }
1479 }
1480
1481 if ((((10 * internal->master_clk) / (internal->srate / 10)) <= 410) &&
1482 (INRANGE(STB0899_QPSK_12, modcod, STB0899_QPSK_35)) &&
1483 (pilots == 1)) {
1484
1485 /* Equalizer Disable update */
1486 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
1487 STB0899_SETFIELD_VAL(EQ_DISABLE_UPDATE, reg, 1);
1488 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
1489 }
1490
1491 /* slow down the Equalizer once locked */
1492 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
1493 STB0899_SETFIELD_VAL(EQ_SHIFT, reg, 0x02);
1494 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
1495
1496 /* Store signal parameters */
1497 offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
1498
1499 offsetfreq = offsetfreq / ((1 << 30) / 1000);
1500 offsetfreq *= (internal->master_clk / 1000000);
1501 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
1502 if (STB0899_GETFIELD(SPECTRUM_INVERT, reg))
1503 offsetfreq *= -1;
1504
1505 internal->freq = internal->freq - offsetfreq;
1506 internal->srate = stb0899_dvbs2_get_srate(state);
1507
1508 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1509 internal->modcod = STB0899_GETFIELD(UWP_DECODE_MOD, reg) >> 2;
1510 internal->pilots = STB0899_GETFIELD(UWP_DECODE_MOD, reg) & 0x01;
1511 internal->frame_length = (STB0899_GETFIELD(UWP_DECODE_MOD, reg) >> 1) & 0x01;
1512
1513 /* Set IF AGC to tracking */
1514 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
1515 STB0899_SETFIELD_VAL(IF_LOOP_GAIN, reg, 3);
1516
1517 /* if QPSK 1/2,QPSK 3/5 or QPSK 2/3 set IF AGC reference to 16 otherwise 32*/
1518 if (INRANGE(STB0899_QPSK_12, internal->modcod, STB0899_QPSK_23))
1519 STB0899_SETFIELD_VAL(IF_AGC_REF, reg, 16);
1520
1521 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
1522
1523 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL2);
1524 STB0899_SETFIELD_VAL(IF_AGC_DUMP_PER, reg, 7);
1525 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL2, STB0899_OFF0_IF_AGC_CNTRL2, reg);
1526 }
1527
1528 /* Release Stream Merger Reset */
1529 reg = stb0899_read_reg(state, STB0899_TSTRES);
1530 STB0899_SETFIELD_VAL(FRESRS, reg, 0);
1531 stb0899_write_reg(state, STB0899_TSTRES, reg);
1532
1533 return internal->status;
1534}