1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/bitops.h>
23#include "stb0899_drv.h"
24#include "stb0899_priv.h"
25#include "stb0899_reg.h"
26
27static inline u32 stb0899_do_div(u64 n, u32 d)
28{
29
30
31 do_div(n, d);
32 return n;
33}
34
35#if 0
36
37
38
39
40
41static u32 stb0899_calc_srate(u32 master_clk, u8 *sfr)
42{
43 u64 tmp;
44
45
46
47
48 tmp = (((u32)sfr[0]) << 16) | (((u32)sfr[1]) << 8) | sfr[2];
49 tmp &= ~0xf;
50 tmp *= master_clk;
51 tmp >>= 24;
52
53 return tmp;
54}
55
56
57
58
59
60static u32 stb0899_get_srate(struct stb0899_state *state)
61{
62 struct stb0899_internal *internal = &state->internal;
63 u8 sfr[3];
64
65 stb0899_read_regs(state, STB0899_SFRH, sfr, 3);
66
67 return stb0899_calc_srate(internal->master_clk, sfr);
68}
69#endif
70
71
72
73
74
75
76
77
78static u32 stb0899_set_srate(struct stb0899_state *state, u32 master_clk, u32 srate)
79{
80 u32 tmp;
81 u8 sfr[3];
82
83 dprintk(state->verbose, FE_DEBUG, 1, "-->");
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 tmp = stb0899_do_div((((u64)srate) << 21) + master_clk, 2 * master_clk);
102 tmp <<= 4;
103
104 sfr[0] = tmp >> 16;
105 sfr[1] = tmp >> 8;
106 sfr[2] = tmp;
107
108 stb0899_write_regs(state, STB0899_SFRH, sfr, 3);
109
110 return srate;
111}
112
113
114
115
116
117
118
119static long stb0899_calc_derot_time(long srate)
120{
121 if (srate > 0)
122 return (100000 / (srate / 1000));
123 else
124 return 0;
125}
126
127
128
129
130
131
132long stb0899_carr_width(struct stb0899_state *state)
133{
134 struct stb0899_internal *internal = &state->internal;
135
136 return (internal->srate + (internal->srate * internal->rolloff) / 100);
137}
138
139
140
141
142
143static void stb0899_first_subrange(struct stb0899_state *state)
144{
145 struct stb0899_internal *internal = &state->internal;
146 struct stb0899_params *params = &state->params;
147 struct stb0899_config *config = state->config;
148
149 int range = 0;
150 u32 bandwidth = 0;
151
152 if (config->tuner_get_bandwidth) {
153 stb0899_i2c_gate_ctrl(&state->frontend, 1);
154 config->tuner_get_bandwidth(&state->frontend, &bandwidth);
155 stb0899_i2c_gate_ctrl(&state->frontend, 0);
156 range = bandwidth - stb0899_carr_width(state) / 2;
157 }
158
159 if (range > 0)
160 internal->sub_range = min(internal->srch_range, range);
161 else
162 internal->sub_range = 0;
163
164 internal->freq = params->freq;
165 internal->tuner_offst = 0L;
166 internal->sub_dir = 1;
167}
168
169
170
171
172
173
174static enum stb0899_status stb0899_check_tmg(struct stb0899_state *state)
175{
176 struct stb0899_internal *internal = &state->internal;
177 int lock;
178 u8 reg;
179 s8 timing;
180
181 msleep(internal->t_derot);
182
183 stb0899_write_reg(state, STB0899_RTF, 0xf2);
184 reg = stb0899_read_reg(state, STB0899_TLIR);
185 lock = STB0899_GETFIELD(TLIR_TMG_LOCK_IND, reg);
186 timing = stb0899_read_reg(state, STB0899_RTF);
187
188 if (lock >= 42) {
189 if ((lock > 48) && (abs(timing) >= 110)) {
190 internal->status = ANALOGCARRIER;
191 dprintk(state->verbose, FE_DEBUG, 1, "-->ANALOG Carrier !");
192 } else {
193 internal->status = TIMINGOK;
194 dprintk(state->verbose, FE_DEBUG, 1, "------->TIMING OK !");
195 }
196 } else {
197 internal->status = NOTIMING;
198 dprintk(state->verbose, FE_DEBUG, 1, "-->NO TIMING !");
199 }
200 return internal->status;
201}
202
203
204
205
206
207static enum stb0899_status stb0899_search_tmg(struct stb0899_state *state)
208{
209 struct stb0899_internal *internal = &state->internal;
210 struct stb0899_params *params = &state->params;
211
212 short int derot_step, derot_freq = 0, derot_limit, next_loop = 3;
213 int index = 0;
214 u8 cfr[2];
215
216 internal->status = NOTIMING;
217
218
219 derot_limit = (internal->sub_range / 2L) / internal->mclk;
220 derot_step = (params->srate / 2L) / internal->mclk;
221
222 while ((stb0899_check_tmg(state) != TIMINGOK) && next_loop) {
223 index++;
224 derot_freq += index * internal->direction * derot_step;
225
226 if (abs(derot_freq) > derot_limit)
227 next_loop--;
228
229 if (next_loop) {
230 STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(internal->inversion * derot_freq));
231 STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(internal->inversion * derot_freq));
232 stb0899_write_regs(state, STB0899_CFRM, cfr, 2);
233 }
234 internal->direction = -internal->direction;
235 }
236
237 if (internal->status == TIMINGOK) {
238 stb0899_read_regs(state, STB0899_CFRM, cfr, 2);
239 internal->derot_freq = internal->inversion * MAKEWORD16(cfr[0], cfr[1]);
240 dprintk(state->verbose, FE_DEBUG, 1, "------->TIMING OK ! Derot Freq = %d", internal->derot_freq);
241 }
242
243 return internal->status;
244}
245
246
247
248
249
250static enum stb0899_status stb0899_check_carrier(struct stb0899_state *state)
251{
252 struct stb0899_internal *internal = &state->internal;
253 u8 reg;
254
255 msleep(internal->t_derot);
256
257 reg = stb0899_read_reg(state, STB0899_CFD);
258 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
259 stb0899_write_reg(state, STB0899_CFD, reg);
260
261 reg = stb0899_read_reg(state, STB0899_DSTATUS);
262 dprintk(state->verbose, FE_DEBUG, 1, "--------------------> STB0899_DSTATUS=[0x%02x]", reg);
263 if (STB0899_GETFIELD(CARRIER_FOUND, reg)) {
264 internal->status = CARRIEROK;
265 dprintk(state->verbose, FE_DEBUG, 1, "-------------> CARRIEROK !");
266 } else {
267 internal->status = NOCARRIER;
268 dprintk(state->verbose, FE_DEBUG, 1, "-------------> NOCARRIER !");
269 }
270
271 return internal->status;
272}
273
274
275
276
277
278static enum stb0899_status stb0899_search_carrier(struct stb0899_state *state)
279{
280 struct stb0899_internal *internal = &state->internal;
281
282 short int derot_freq = 0, last_derot_freq = 0, derot_limit, next_loop = 3;
283 int index = 0;
284 u8 cfr[2];
285 u8 reg;
286
287 internal->status = NOCARRIER;
288 derot_limit = (internal->sub_range / 2L) / internal->mclk;
289 derot_freq = internal->derot_freq;
290
291 reg = stb0899_read_reg(state, STB0899_CFD);
292 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
293 stb0899_write_reg(state, STB0899_CFD, reg);
294
295 do {
296 dprintk(state->verbose, FE_DEBUG, 1, "Derot Freq=%d, mclk=%d", derot_freq, internal->mclk);
297 if (stb0899_check_carrier(state) == NOCARRIER) {
298 index++;
299 last_derot_freq = derot_freq;
300 derot_freq += index * internal->direction * internal->derot_step;
301
302 if(abs(derot_freq) > derot_limit)
303 next_loop--;
304
305 if (next_loop) {
306 reg = stb0899_read_reg(state, STB0899_CFD);
307 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
308 stb0899_write_reg(state, STB0899_CFD, reg);
309
310 STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(internal->inversion * derot_freq));
311 STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(internal->inversion * derot_freq));
312 stb0899_write_regs(state, STB0899_CFRM, cfr, 2);
313 }
314 }
315
316 internal->direction = -internal->direction;
317 } while ((internal->status != CARRIEROK) && next_loop);
318
319 if (internal->status == CARRIEROK) {
320 stb0899_read_regs(state, STB0899_CFRM, cfr, 2);
321 internal->derot_freq = internal->inversion * MAKEWORD16(cfr[0], cfr[1]);
322 dprintk(state->verbose, FE_DEBUG, 1, "----> CARRIER OK !, Derot Freq=%d", internal->derot_freq);
323 } else {
324 internal->derot_freq = last_derot_freq;
325 }
326
327 return internal->status;
328}
329
330
331
332
333
334static enum stb0899_status stb0899_check_data(struct stb0899_state *state)
335{
336 struct stb0899_internal *internal = &state->internal;
337 struct stb0899_params *params = &state->params;
338
339 int lock = 0, index = 0, dataTime = 500, loop;
340 u8 reg;
341
342 internal->status = NODATA;
343
344
345 reg = stb0899_read_reg(state, STB0899_TSTRES);
346 STB0899_SETFIELD_VAL(FRESACS, reg, 1);
347 stb0899_write_reg(state, STB0899_TSTRES, reg);
348 msleep(1);
349 reg = stb0899_read_reg(state, STB0899_TSTRES);
350 STB0899_SETFIELD_VAL(FRESACS, reg, 0);
351 stb0899_write_reg(state, STB0899_TSTRES, reg);
352
353 if (params->srate <= 2000000)
354 dataTime = 2000;
355 else if (params->srate <= 5000000)
356 dataTime = 1500;
357 else if (params->srate <= 15000000)
358 dataTime = 1000;
359 else
360 dataTime = 500;
361
362
363 stb0899_read_reg(state, STB0899_VSTATUS);
364
365 stb0899_write_reg(state, STB0899_DSTATUS2, 0x00);
366 while (1) {
367
368 reg = stb0899_read_reg(state, STB0899_VSTATUS);
369 lock = STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg);
370 loop = STB0899_GETFIELD(VSTATUS_END_LOOPVIT, reg);
371
372 if (lock || loop || (index > dataTime))
373 break;
374 index++;
375 }
376
377 if (lock) {
378 internal->status = DATAOK;
379 dprintk(state->verbose, FE_DEBUG, 1, "-----------------> DATA OK !");
380 }
381
382 return internal->status;
383}
384
385
386
387
388
389static enum stb0899_status stb0899_search_data(struct stb0899_state *state)
390{
391 short int derot_freq, derot_step, derot_limit, next_loop = 3;
392 u8 cfr[2];
393 u8 reg;
394 int index = 1;
395
396 struct stb0899_internal *internal = &state->internal;
397 struct stb0899_params *params = &state->params;
398
399 derot_step = (params->srate / 4L) / internal->mclk;
400 derot_limit = (internal->sub_range / 2L) / internal->mclk;
401 derot_freq = internal->derot_freq;
402
403 do {
404 if ((internal->status != CARRIEROK) || (stb0899_check_data(state) != DATAOK)) {
405
406 derot_freq += index * internal->direction * derot_step;
407 if (abs(derot_freq) > derot_limit)
408 next_loop--;
409
410 if (next_loop) {
411 dprintk(state->verbose, FE_DEBUG, 1, "Derot freq=%d, mclk=%d", derot_freq, internal->mclk);
412 reg = stb0899_read_reg(state, STB0899_CFD);
413 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
414 stb0899_write_reg(state, STB0899_CFD, reg);
415
416 STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(internal->inversion * derot_freq));
417 STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(internal->inversion * derot_freq));
418 stb0899_write_regs(state, STB0899_CFRM, cfr, 2);
419
420 stb0899_check_carrier(state);
421 index++;
422 }
423 }
424 internal->direction = -internal->direction;
425 } while ((internal->status != DATAOK) && next_loop);
426
427 if (internal->status == DATAOK) {
428 stb0899_read_regs(state, STB0899_CFRM, cfr, 2);
429
430
431 reg = stb0899_read_reg(state, STB0899_IQSWAP);
432 if (STB0899_GETFIELD(SYM, reg))
433 internal->inversion = IQ_SWAP_ON;
434 else
435 internal->inversion = IQ_SWAP_OFF;
436
437 internal->derot_freq = internal->inversion * MAKEWORD16(cfr[0], cfr[1]);
438 dprintk(state->verbose, FE_DEBUG, 1, "------> DATAOK ! Derot Freq=%d", internal->derot_freq);
439 }
440
441 return internal->status;
442}
443
444
445
446
447
448static enum stb0899_status stb0899_check_range(struct stb0899_state *state)
449{
450 struct stb0899_internal *internal = &state->internal;
451 struct stb0899_params *params = &state->params;
452
453 int range_offst, tp_freq;
454
455 range_offst = internal->srch_range / 2000;
456 tp_freq = internal->freq - (internal->derot_freq * internal->mclk) / 1000;
457
458 if ((tp_freq >= params->freq - range_offst) && (tp_freq <= params->freq + range_offst)) {
459 internal->status = RANGEOK;
460 dprintk(state->verbose, FE_DEBUG, 1, "----> RANGEOK !");
461 } else {
462 internal->status = OUTOFRANGE;
463 dprintk(state->verbose, FE_DEBUG, 1, "----> OUT OF RANGE !");
464 }
465
466 return internal->status;
467}
468
469
470
471
472
473static void next_sub_range(struct stb0899_state *state)
474{
475 struct stb0899_internal *internal = &state->internal;
476 struct stb0899_params *params = &state->params;
477
478 long old_sub_range;
479
480 if (internal->sub_dir > 0) {
481 old_sub_range = internal->sub_range;
482 internal->sub_range = min((internal->srch_range / 2) -
483 (internal->tuner_offst + internal->sub_range / 2),
484 internal->sub_range);
485
486 if (internal->sub_range < 0)
487 internal->sub_range = 0;
488
489 internal->tuner_offst += (old_sub_range + internal->sub_range) / 2;
490 }
491
492 internal->freq = params->freq + (internal->sub_dir * internal->tuner_offst) / 1000;
493 internal->sub_dir = -internal->sub_dir;
494}
495
496
497
498
499
500
501enum stb0899_status stb0899_dvbs_algo(struct stb0899_state *state)
502{
503 struct stb0899_params *params = &state->params;
504 struct stb0899_internal *internal = &state->internal;
505 struct stb0899_config *config = state->config;
506
507 u8 bclc, reg;
508 u8 cfr[2];
509 u8 eq_const[10];
510 s32 clnI = 3;
511 u32 bandwidth = 0;
512
513
514 s32 betaTab[5][4] = {
515
516 { 37, 34, 32, 31 },
517 { 37, 35, 33, 31 },
518 { 37, 35, 33, 31 },
519 { 37, 36, 33, 32 },
520 { 37, 36, 33, 32 }
521 };
522
523 internal->direction = 1;
524
525 stb0899_set_srate(state, internal->master_clk, params->srate);
526
527 if (params->srate <= 5000000) {
528 stb0899_write_reg(state, STB0899_ACLC, 0x89);
529 bclc = stb0899_read_reg(state, STB0899_BCLC);
530 STB0899_SETFIELD_VAL(BETA, bclc, 0x1c);
531 stb0899_write_reg(state, STB0899_BCLC, bclc);
532 clnI = 0;
533 } else if (params->srate <= 15000000) {
534 stb0899_write_reg(state, STB0899_ACLC, 0xc9);
535 bclc = stb0899_read_reg(state, STB0899_BCLC);
536 STB0899_SETFIELD_VAL(BETA, bclc, 0x22);
537 stb0899_write_reg(state, STB0899_BCLC, bclc);
538 clnI = 1;
539 } else if(params->srate <= 25000000) {
540 stb0899_write_reg(state, STB0899_ACLC, 0x89);
541 bclc = stb0899_read_reg(state, STB0899_BCLC);
542 STB0899_SETFIELD_VAL(BETA, bclc, 0x27);
543 stb0899_write_reg(state, STB0899_BCLC, bclc);
544 clnI = 2;
545 } else {
546 stb0899_write_reg(state, STB0899_ACLC, 0xc8);
547 bclc = stb0899_read_reg(state, STB0899_BCLC);
548 STB0899_SETFIELD_VAL(BETA, bclc, 0x29);
549 stb0899_write_reg(state, STB0899_BCLC, bclc);
550 clnI = 3;
551 }
552
553 dprintk(state->verbose, FE_DEBUG, 1, "Set the timing loop to acquisition");
554
555 stb0899_write_reg(state, STB0899_RTC, 0x46);
556 stb0899_write_reg(state, STB0899_CFD, 0xee);
557
558
559
560
561
562
563
564 dprintk(state->verbose, FE_DEBUG, 1, "Derot Percent=%d Srate=%d mclk=%d",
565 internal->derot_percent, params->srate, internal->mclk);
566
567
568 internal->derot_step = internal->derot_percent * (params->srate / 1000L) / internal->mclk;
569 internal->t_derot = stb0899_calc_derot_time(params->srate);
570 internal->t_data = 500;
571
572 dprintk(state->verbose, FE_DEBUG, 1, "RESET stream merger");
573
574 reg = stb0899_read_reg(state, STB0899_TSTRES);
575 STB0899_SETFIELD_VAL(FRESRS, reg, 1);
576 stb0899_write_reg(state, STB0899_TSTRES, reg);
577
578
579
580
581
582 reg = stb0899_read_reg(state, STB0899_DEMAPVIT);
583 STB0899_SETFIELD_VAL(DEMAPVIT_KDIVIDER, reg, 60);
584 stb0899_write_reg(state, STB0899_DEMAPVIT, reg);
585
586 stb0899_write_reg(state, STB0899_EQON, 0x01);
587 stb0899_write_reg(state, STB0899_VITSYNC, 0x19);
588
589 stb0899_first_subrange(state);
590 do {
591
592 cfr[0] = cfr[1] = 0;
593 stb0899_write_regs(state, STB0899_CFRM, cfr, 2);
594
595 stb0899_write_reg(state, STB0899_RTF, 0);
596 reg = stb0899_read_reg(state, STB0899_CFD);
597 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
598 stb0899_write_reg(state, STB0899_CFD, reg);
599
600 internal->derot_freq = 0;
601 internal->status = NOAGC1;
602
603
604 stb0899_i2c_gate_ctrl(&state->frontend, 1);
605
606
607 dprintk(state->verbose, FE_DEBUG, 1, "Tuner set frequency");
608 if (state->config->tuner_set_frequency)
609 state->config->tuner_set_frequency(&state->frontend, internal->freq);
610
611 if (state->config->tuner_get_frequency)
612 state->config->tuner_get_frequency(&state->frontend, &internal->freq);
613
614 msleep(internal->t_agc1 + internal->t_agc2 + internal->t_derot);
615 dprintk(state->verbose, FE_DEBUG, 1, "current derot freq=%d", internal->derot_freq);
616 internal->status = AGC1OK;
617
618
619 if (config->tuner_get_bandwidth)
620 config->tuner_get_bandwidth(&state->frontend, &bandwidth);
621
622
623 stb0899_i2c_gate_ctrl(&state->frontend, 0);
624
625 if (params->srate <= bandwidth / 2)
626 stb0899_search_tmg(state);
627 else
628 stb0899_check_tmg(state);
629
630 if (internal->status == TIMINGOK) {
631 dprintk(state->verbose, FE_DEBUG, 1,
632 "TIMING OK ! Derot freq=%d, mclk=%d",
633 internal->derot_freq, internal->mclk);
634
635 if (stb0899_search_carrier(state) == CARRIEROK) {
636 dprintk(state->verbose, FE_DEBUG, 1,
637 "CARRIER OK ! Derot freq=%d, mclk=%d",
638 internal->derot_freq, internal->mclk);
639
640 if (stb0899_search_data(state) == DATAOK) {
641 dprintk(state->verbose, FE_DEBUG, 1,
642 "DATA OK ! Derot freq=%d, mclk=%d",
643 internal->derot_freq, internal->mclk);
644
645 if (stb0899_check_range(state) == RANGEOK) {
646 dprintk(state->verbose, FE_DEBUG, 1,
647 "RANGE OK ! derot freq=%d, mclk=%d",
648 internal->derot_freq, internal->mclk);
649
650 internal->freq = params->freq - ((internal->derot_freq * internal->mclk) / 1000);
651 reg = stb0899_read_reg(state, STB0899_PLPARM);
652 internal->fecrate = STB0899_GETFIELD(VITCURPUN, reg);
653 dprintk(state->verbose, FE_DEBUG, 1,
654 "freq=%d, internal resultant freq=%d",
655 params->freq, internal->freq);
656
657 dprintk(state->verbose, FE_DEBUG, 1,
658 "internal puncture rate=%d",
659 internal->fecrate);
660 }
661 }
662 }
663 }
664 if (internal->status != RANGEOK)
665 next_sub_range(state);
666
667 } while (internal->sub_range && internal->status != RANGEOK);
668
669
670 stb0899_write_reg(state, STB0899_RTC, 0x33);
671 stb0899_write_reg(state, STB0899_CFD, 0xf7);
672
673 if (internal->status == RANGEOK) {
674 dprintk(state->verbose, FE_DEBUG, 1, "Locked & Range OK !");
675 stb0899_write_reg(state, STB0899_EQON, 0x41);
676 stb0899_write_reg(state, STB0899_VITSYNC, 0x39);
677
678
679
680
681
682 reg = stb0899_read_reg(state, STB0899_BCLC);
683 switch (internal->fecrate) {
684 case STB0899_FEC_1_2:
685 stb0899_write_reg(state, STB0899_DEMAPVIT, 0x1a);
686 STB0899_SETFIELD_VAL(BETA, reg, betaTab[0][clnI]);
687 stb0899_write_reg(state, STB0899_BCLC, reg);
688 break;
689 case STB0899_FEC_2_3:
690 stb0899_write_reg(state, STB0899_DEMAPVIT, 44);
691 STB0899_SETFIELD_VAL(BETA, reg, betaTab[1][clnI]);
692 stb0899_write_reg(state, STB0899_BCLC, reg);
693 break;
694 case STB0899_FEC_3_4:
695 stb0899_write_reg(state, STB0899_DEMAPVIT, 60);
696 STB0899_SETFIELD_VAL(BETA, reg, betaTab[2][clnI]);
697 stb0899_write_reg(state, STB0899_BCLC, reg);
698 break;
699 case STB0899_FEC_5_6:
700 stb0899_write_reg(state, STB0899_DEMAPVIT, 75);
701 STB0899_SETFIELD_VAL(BETA, reg, betaTab[3][clnI]);
702 stb0899_write_reg(state, STB0899_BCLC, reg);
703 break;
704 case STB0899_FEC_6_7:
705 stb0899_write_reg(state, STB0899_DEMAPVIT, 88);
706 stb0899_write_reg(state, STB0899_ACLC, 0x88);
707 stb0899_write_reg(state, STB0899_BCLC, 0x9a);
708 break;
709 case STB0899_FEC_7_8:
710 stb0899_write_reg(state, STB0899_DEMAPVIT, 94);
711 STB0899_SETFIELD_VAL(BETA, reg, betaTab[4][clnI]);
712 stb0899_write_reg(state, STB0899_BCLC, reg);
713 break;
714 default:
715 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported Puncture Rate");
716 break;
717 }
718
719 reg = stb0899_read_reg(state, STB0899_TSTRES);
720 STB0899_SETFIELD_VAL(FRESRS, reg, 0);
721 stb0899_write_reg(state, STB0899_TSTRES, reg);
722
723
724 reg = stb0899_read_reg(state, STB0899_CFD);
725 STB0899_SETFIELD_VAL(CFD_ON, reg, 0);
726 stb0899_write_reg(state, STB0899_CFD, reg);
727
728 stb0899_read_regs(state, STB0899_EQUAI1, eq_const, 10);
729 }
730
731 return internal->status;
732}
733
734
735
736
737
738static void stb0899_dvbs2_config_uwp(struct stb0899_state *state)
739{
740 struct stb0899_internal *internal = &state->internal;
741 struct stb0899_config *config = state->config;
742 u32 uwp1, uwp2, uwp3, reg;
743
744 uwp1 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
745 uwp2 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL2);
746 uwp3 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL3);
747
748 STB0899_SETFIELD_VAL(UWP_ESN0_AVE, uwp1, config->esno_ave);
749 STB0899_SETFIELD_VAL(UWP_ESN0_QUANT, uwp1, config->esno_quant);
750 STB0899_SETFIELD_VAL(UWP_TH_SOF, uwp1, config->uwp_threshold_sof);
751
752 STB0899_SETFIELD_VAL(FE_COARSE_TRK, uwp2, internal->av_frame_coarse);
753 STB0899_SETFIELD_VAL(FE_FINE_TRK, uwp2, internal->av_frame_fine);
754 STB0899_SETFIELD_VAL(UWP_MISS_TH, uwp2, config->miss_threshold);
755
756 STB0899_SETFIELD_VAL(UWP_TH_ACQ, uwp3, config->uwp_threshold_acq);
757 STB0899_SETFIELD_VAL(UWP_TH_TRACK, uwp3, config->uwp_threshold_track);
758
759 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL1, STB0899_OFF0_UWP_CNTRL1, uwp1);
760 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL2, STB0899_OFF0_UWP_CNTRL2, uwp2);
761 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL3, STB0899_OFF0_UWP_CNTRL3, uwp3);
762
763 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, SOF_SRCH_TO);
764 STB0899_SETFIELD_VAL(SOF_SEARCH_TIMEOUT, reg, config->sof_search_timeout);
765 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_SOF_SRCH_TO, STB0899_OFF0_SOF_SRCH_TO, reg);
766}
767
768
769
770
771
772static void stb0899_dvbs2_config_csm_auto(struct stb0899_state *state)
773{
774 u32 reg;
775
776 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
777 STB0899_SETFIELD_VAL(CSM_AUTO_PARAM, reg, 1);
778 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, reg);
779}
780
781static long Log2Int(int number)
782{
783 int i;
784
785 i = 0;
786 while ((1 << i) <= abs(number))
787 i++;
788
789 if (number == 0)
790 i = 1;
791
792 return i - 1;
793}
794
795
796
797
798
799static u32 stb0899_dvbs2_calc_srate(struct stb0899_state *state)
800{
801 struct stb0899_internal *internal = &state->internal;
802 struct stb0899_config *config = state->config;
803
804 u32 dec_ratio, dec_rate, decim, remain, intval, btr_nom_freq;
805 u32 master_clk, srate;
806
807 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
808 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
809 dec_rate = Log2Int(dec_ratio);
810 decim = 1 << dec_rate;
811 master_clk = internal->master_clk / 1000;
812 srate = internal->srate / 1000;
813
814 if (decim <= 4) {
815 intval = (decim * (1 << (config->btr_nco_bits - 1))) / master_clk;
816 remain = (decim * (1 << (config->btr_nco_bits - 1))) % master_clk;
817 } else {
818 intval = (1 << (config->btr_nco_bits - 1)) / (master_clk / 100) * decim / 100;
819 remain = (decim * (1 << (config->btr_nco_bits - 1))) % master_clk;
820 }
821 btr_nom_freq = (intval * srate) + ((remain * srate) / master_clk);
822
823 return btr_nom_freq;
824}
825
826
827
828
829
830static u32 stb0899_dvbs2_calc_dev(struct stb0899_state *state)
831{
832 struct stb0899_internal *internal = &state->internal;
833 u32 dec_ratio, correction, master_clk, srate;
834
835 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
836 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
837
838 master_clk = internal->master_clk / 1000;
839 srate = internal->srate / 1000;
840 correction = (512 * master_clk) / (2 * dec_ratio * srate);
841
842 return correction;
843}
844
845
846
847
848
849static void stb0899_dvbs2_set_srate(struct stb0899_state *state)
850{
851 struct stb0899_internal *internal = &state->internal;
852
853 u32 dec_ratio, dec_rate, win_sel, decim, f_sym, btr_nom_freq;
854 u32 correction, freq_adj, band_lim, decim_cntrl, reg;
855 u8 anti_alias;
856
857
858 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
859 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
860 dec_rate = Log2Int(dec_ratio);
861
862 win_sel = 0;
863 if (dec_rate >= 5)
864 win_sel = dec_rate - 4;
865
866 decim = (1 << dec_rate);
867
868 f_sym = internal->master_clk / ((decim * internal->srate) / 1000);
869
870 if (f_sym <= 2250)
871 band_lim = 1;
872 else
873 band_lim = 0;
874
875 decim_cntrl = ((win_sel << 3) & 0x18) + ((band_lim << 5) & 0x20) + (dec_rate & 0x7);
876 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DECIM_CNTRL, STB0899_OFF0_DECIM_CNTRL, decim_cntrl);
877
878 if (f_sym <= 3450)
879 anti_alias = 0;
880 else if (f_sym <= 4250)
881 anti_alias = 1;
882 else
883 anti_alias = 2;
884
885 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ANTI_ALIAS_SEL, STB0899_OFF0_ANTI_ALIAS_SEL, anti_alias);
886 btr_nom_freq = stb0899_dvbs2_calc_srate(state);
887 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_NOM_FREQ, STB0899_OFF0_BTR_NOM_FREQ, btr_nom_freq);
888
889 correction = stb0899_dvbs2_calc_dev(state);
890 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_CNTRL);
891 STB0899_SETFIELD_VAL(BTR_FREQ_CORR, reg, correction);
892 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_CNTRL, STB0899_OFF0_BTR_CNTRL, reg);
893
894
895 freq_adj = internal->srate / (internal->master_clk / 4096);
896 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_FREQ_ADJ_SCALE, STB0899_OFF0_FREQ_ADJ_SCALE, freq_adj);
897}
898
899
900
901
902
903static void stb0899_dvbs2_set_btr_loopbw(struct stb0899_state *state)
904{
905 struct stb0899_internal *internal = &state->internal;
906 struct stb0899_config *config = state->config;
907
908 u32 sym_peak = 23, zeta = 707, loopbw_percent = 60;
909 s32 dec_ratio, dec_rate, k_btr1_rshft, k_btr1, k_btr0_rshft;
910 s32 k_btr0, k_btr2_rshft, k_direct_shift, k_indirect_shift;
911 u32 decim, K, wn, k_direct, k_indirect;
912 u32 reg;
913
914 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
915 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
916 dec_rate = Log2Int(dec_ratio);
917 decim = (1 << dec_rate);
918
919 sym_peak *= 576000;
920 K = (1 << config->btr_nco_bits) / (internal->master_clk / 1000);
921 K *= (internal->srate / 1000000) * decim;
922
923 if (K != 0) {
924 K = sym_peak / K;
925 wn = (4 * zeta * zeta) + 1000000;
926 wn = (2 * (loopbw_percent * 1000) * 40 * zeta) /wn;
927
928 k_indirect = (wn * wn) / K;
929 k_direct = (2 * wn * zeta) / K;
930 k_direct *= 100;
931
932 k_direct_shift = Log2Int(k_direct) - Log2Int(10000) - 2;
933 k_btr1_rshft = (-1 * k_direct_shift) + config->btr_gain_shift_offset;
934 k_btr1 = k_direct / (1 << k_direct_shift);
935 k_btr1 /= 10000;
936
937 k_indirect_shift = Log2Int(k_indirect + 15) - 20 ;
938 k_btr0_rshft = (-1 * k_indirect_shift) + config->btr_gain_shift_offset;
939 k_btr0 = k_indirect * (1 << (-k_indirect_shift));
940 k_btr0 /= 1000000;
941
942 k_btr2_rshft = 0;
943 if (k_btr0_rshft > 15) {
944 k_btr2_rshft = k_btr0_rshft - 15;
945 k_btr0_rshft = 15;
946 }
947 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_LOOP_GAIN);
948 STB0899_SETFIELD_VAL(KBTR0_RSHFT, reg, k_btr0_rshft);
949 STB0899_SETFIELD_VAL(KBTR0, reg, k_btr0);
950 STB0899_SETFIELD_VAL(KBTR1_RSHFT, reg, k_btr1_rshft);
951 STB0899_SETFIELD_VAL(KBTR1, reg, k_btr1);
952 STB0899_SETFIELD_VAL(KBTR2_RSHFT, reg, k_btr2_rshft);
953 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_LOOP_GAIN, STB0899_OFF0_BTR_LOOP_GAIN, reg);
954 } else
955 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_LOOP_GAIN, STB0899_OFF0_BTR_LOOP_GAIN, 0xc4c4f);
956}
957
958
959
960
961
962static void stb0899_dvbs2_set_carr_freq(struct stb0899_state *state, s32 carr_freq, u32 master_clk)
963{
964 struct stb0899_config *config = state->config;
965 s32 crl_nom_freq;
966 u32 reg;
967
968 crl_nom_freq = (1 << config->crl_nco_bits) / master_clk;
969 crl_nom_freq *= carr_freq;
970 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
971 STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, crl_nom_freq);
972 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
973}
974
975
976
977
978
979static void stb0899_dvbs2_init_calc(struct stb0899_state *state)
980{
981 struct stb0899_internal *internal = &state->internal;
982 s32 steps, step_size;
983 u32 range, reg;
984
985
986 stb0899_dvbs2_config_uwp(state);
987 stb0899_dvbs2_config_csm_auto(state);
988
989
990 stb0899_dvbs2_set_srate(state);
991 stb0899_dvbs2_set_btr_loopbw(state);
992
993 if (internal->srate / 1000000 >= 15)
994 step_size = (1 << 17) / 5;
995 else if (internal->srate / 1000000 >= 10)
996 step_size = (1 << 17) / 7;
997 else if (internal->srate / 1000000 >= 5)
998 step_size = (1 << 17) / 10;
999 else
1000 step_size = (1 << 17) / 4;
1001
1002 range = internal->srch_range / 1000000;
1003 steps = (10 * range * (1 << 17)) / (step_size * (internal->srate / 1000000));
1004 steps = (steps + 6) / 10;
1005 steps = (steps == 0) ? 1 : steps;
1006 if (steps % 2 == 0)
1007 stb0899_dvbs2_set_carr_freq(state, internal->center_freq -
1008 (internal->step_size * (internal->srate / 20000000)),
1009 (internal->master_clk) / 1000000);
1010 else
1011 stb0899_dvbs2_set_carr_freq(state, internal->center_freq, (internal->master_clk) / 1000000);
1012
1013
1014 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, ACQ_CNTRL2);
1015 STB0899_SETFIELD_VAL(ZIGZAG, reg, 1);
1016 STB0899_SETFIELD_VAL(NUM_STEPS, reg, steps);
1017 STB0899_SETFIELD_VAL(FREQ_STEPSIZE, reg, step_size);
1018 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ACQ_CNTRL2, STB0899_OFF0_ACQ_CNTRL2, reg);
1019}
1020
1021
1022
1023
1024
1025static void stb0899_dvbs2_btr_init(struct stb0899_state *state)
1026{
1027 u32 reg;
1028
1029
1030 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_CNTRL);
1031 STB0899_SETFIELD_VAL(INTRP_PHS_SENSE, reg, 1);
1032 STB0899_SETFIELD_VAL(BTR_ERR_ENA, reg, 1);
1033 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_CNTRL, STB0899_OFF0_BTR_CNTRL, reg);
1034
1035
1036 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_FREQ_INIT, STB0899_OFF0_BTR_FREQ_INIT, 0x10000000);
1037 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_FREQ_INIT, STB0899_OFF0_BTR_FREQ_INIT, 0x00000000);
1038
1039
1040 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_PHS_INIT, STB0899_OFF0_BTR_PHS_INIT, 0x10000000);
1041 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_PHS_INIT, STB0899_OFF0_BTR_PHS_INIT, 0x00000000);
1042}
1043
1044
1045
1046
1047
1048static void stb0899_dvbs2_reacquire(struct stb0899_state *state)
1049{
1050 u32 reg = 0;
1051
1052
1053 STB0899_SETFIELD_VAL(DVBS2_RESET, reg, 1);
1054 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_RESET_CNTRL, STB0899_OFF0_RESET_CNTRL, reg);
1055
1056
1057 stb0899_dvbs2_btr_init(state);
1058
1059
1060 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_FREQ_INIT, STB0899_OFF0_CRL_FREQ_INIT, (1 << 30));
1061 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_FREQ_INIT, STB0899_OFF0_CRL_FREQ_INIT, 0);
1062 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_LOOP_GAIN, STB0899_OFF0_CRL_LOOP_GAIN, 0);
1063 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_PHS_INIT, STB0899_OFF0_CRL_PHS_INIT, (1 << 30));
1064 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_PHS_INIT, STB0899_OFF0_CRL_PHS_INIT, 0);
1065
1066
1067 reg = 0;
1068 STB0899_SETFIELD_VAL(DVBS2_RESET, reg, 0);
1069 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_RESET_CNTRL, STB0899_OFF0_RESET_CNTRL, reg);
1070
1071
1072 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ACQUIRE_TRIG, STB0899_OFF0_ACQUIRE_TRIG, 1);
1073 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_LOCK_LOST, STB0899_OFF0_LOCK_LOST, 0);
1074
1075
1076 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQUALIZER_INIT, STB0899_OFF0_EQUALIZER_INIT, 1);
1077
1078
1079 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQUALIZER_INIT, STB0899_OFF0_EQUALIZER_INIT, 0);
1080
1081 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
1082 STB0899_SETFIELD_VAL(EQ_SHIFT, reg, 0);
1083 STB0899_SETFIELD_VAL(EQ_DISABLE_UPDATE, reg, 0);
1084 STB0899_SETFIELD_VAL(EQ_DELAY, reg, 0x05);
1085 STB0899_SETFIELD_VAL(EQ_ADAPT_MODE, reg, 0x01);
1086 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
1087
1088
1089 stb0899_write_reg(state, STB0899_PDELCTRL, 0x4a);
1090}
1091
1092
1093
1094
1095
1096static enum stb0899_status stb0899_dvbs2_get_dmd_status(struct stb0899_state *state, int timeout)
1097{
1098 int time = -10, lock = 0, uwp, csm;
1099 u32 reg;
1100
1101 do {
1102 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS);
1103 dprintk(state->verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg);
1104 if (STB0899_GETFIELD(IF_AGC_LOCK, reg))
1105 dprintk(state->verbose, FE_DEBUG, 1, "------------->IF AGC LOCKED !");
1106 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1107 dprintk(state->verbose, FE_DEBUG, 1, "----------->DMD STAT2=[0x%02x]", reg);
1108 uwp = STB0899_GETFIELD(UWP_LOCK, reg);
1109 csm = STB0899_GETFIELD(CSM_LOCK, reg);
1110 if (uwp && csm)
1111 lock = 1;
1112
1113 time += 10;
1114 msleep(10);
1115
1116 } while ((!lock) && (time <= timeout));
1117
1118 if (lock) {
1119 dprintk(state->verbose, FE_DEBUG, 1, "----------------> DVB-S2 LOCK !");
1120 return DVBS2_DEMOD_LOCK;
1121 } else {
1122 return DVBS2_DEMOD_NOLOCK;
1123 }
1124}
1125
1126
1127
1128
1129
1130static int stb0899_dvbs2_get_data_lock(struct stb0899_state *state, int timeout)
1131{
1132 int time = 0, lock = 0;
1133 u8 reg;
1134
1135 while ((!lock) && (time < timeout)) {
1136 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1137 dprintk(state->verbose, FE_DEBUG, 1, "---------> CFGPDELSTATUS=[0x%02x]", reg);
1138 lock = STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg);
1139 time++;
1140 }
1141
1142 return lock;
1143}
1144
1145
1146
1147
1148
1149static enum stb0899_status stb0899_dvbs2_get_fec_status(struct stb0899_state *state, int timeout)
1150{
1151 int time = 0, Locked;
1152
1153 do {
1154 Locked = stb0899_dvbs2_get_data_lock(state, 1);
1155 time++;
1156 msleep(1);
1157
1158 } while ((!Locked) && (time < timeout));
1159
1160 if (Locked) {
1161 dprintk(state->verbose, FE_DEBUG, 1, "---------->DVB-S2 FEC LOCK !");
1162 return DVBS2_FEC_LOCK;
1163 } else {
1164 return DVBS2_FEC_NOLOCK;
1165 }
1166}
1167
1168
1169
1170
1171
1172
1173static void stb0899_dvbs2_init_csm(struct stb0899_state *state, int pilots, enum stb0899_modcod modcod)
1174{
1175 struct stb0899_internal *internal = &state->internal;
1176
1177 s32 dvt_tbl = 1, two_pass = 0, agc_gain = 6, agc_shift = 0, loop_shift = 0, phs_diff_thr = 0x80;
1178 s32 gamma_acq, gamma_rho_acq, gamma_trk, gamma_rho_trk, lock_count_thr;
1179 u32 csm1, csm2, csm3, csm4;
1180
1181 if (((internal->master_clk / internal->srate) <= 4) && (modcod <= 11) && (pilots == 1)) {
1182 switch (modcod) {
1183 case STB0899_QPSK_12:
1184 gamma_acq = 25;
1185 gamma_rho_acq = 2700;
1186 gamma_trk = 12;
1187 gamma_rho_trk = 180;
1188 lock_count_thr = 8;
1189 break;
1190 case STB0899_QPSK_35:
1191 gamma_acq = 38;
1192 gamma_rho_acq = 7182;
1193 gamma_trk = 14;
1194 gamma_rho_trk = 308;
1195 lock_count_thr = 8;
1196 break;
1197 case STB0899_QPSK_23:
1198 gamma_acq = 42;
1199 gamma_rho_acq = 9408;
1200 gamma_trk = 17;
1201 gamma_rho_trk = 476;
1202 lock_count_thr = 8;
1203 break;
1204 case STB0899_QPSK_34:
1205 gamma_acq = 53;
1206 gamma_rho_acq = 16642;
1207 gamma_trk = 19;
1208 gamma_rho_trk = 646;
1209 lock_count_thr = 8;
1210 break;
1211 case STB0899_QPSK_45:
1212 gamma_acq = 53;
1213 gamma_rho_acq = 17119;
1214 gamma_trk = 22;
1215 gamma_rho_trk = 880;
1216 lock_count_thr = 8;
1217 break;
1218 case STB0899_QPSK_56:
1219 gamma_acq = 55;
1220 gamma_rho_acq = 19250;
1221 gamma_trk = 23;
1222 gamma_rho_trk = 989;
1223 lock_count_thr = 8;
1224 break;
1225 case STB0899_QPSK_89:
1226 gamma_acq = 60;
1227 gamma_rho_acq = 24240;
1228 gamma_trk = 24;
1229 gamma_rho_trk = 1176;
1230 lock_count_thr = 8;
1231 break;
1232 case STB0899_QPSK_910:
1233 gamma_acq = 66;
1234 gamma_rho_acq = 29634;
1235 gamma_trk = 24;
1236 gamma_rho_trk = 1176;
1237 lock_count_thr = 8;
1238 break;
1239 default:
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 }
1247
1248 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1249 STB0899_SETFIELD_VAL(CSM_AUTO_PARAM, csm1, 0);
1250 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1251
1252 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1253 csm2 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL2);
1254 csm3 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL3);
1255 csm4 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL4);
1256
1257 STB0899_SETFIELD_VAL(CSM_DVT_TABLE, csm1, dvt_tbl);
1258 STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, two_pass);
1259 STB0899_SETFIELD_VAL(CSM_AGC_GAIN, csm1, agc_gain);
1260 STB0899_SETFIELD_VAL(CSM_AGC_SHIFT, csm1, agc_shift);
1261 STB0899_SETFIELD_VAL(FE_LOOP_SHIFT, csm1, loop_shift);
1262 STB0899_SETFIELD_VAL(CSM_GAMMA_ACQ, csm2, gamma_acq);
1263 STB0899_SETFIELD_VAL(CSM_GAMMA_RHOACQ, csm2, gamma_rho_acq);
1264 STB0899_SETFIELD_VAL(CSM_GAMMA_TRACK, csm3, gamma_trk);
1265 STB0899_SETFIELD_VAL(CSM_GAMMA_RHOTRACK, csm3, gamma_rho_trk);
1266 STB0899_SETFIELD_VAL(CSM_LOCKCOUNT_THRESH, csm4, lock_count_thr);
1267 STB0899_SETFIELD_VAL(CSM_PHASEDIFF_THRESH, csm4, phs_diff_thr);
1268
1269 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1270 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL2, STB0899_OFF0_CSM_CNTRL2, csm2);
1271 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL3, STB0899_OFF0_CSM_CNTRL3, csm3);
1272 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL4, STB0899_OFF0_CSM_CNTRL4, csm4);
1273 }
1274}
1275
1276
1277
1278
1279
1280static u32 stb0899_dvbs2_get_srate(struct stb0899_state *state)
1281{
1282 struct stb0899_internal *internal = &state->internal;
1283 struct stb0899_config *config = state->config;
1284
1285 u32 bTrNomFreq, srate, decimRate, intval1, intval2, reg;
1286 int div1, div2, rem1, rem2;
1287
1288 div1 = config->btr_nco_bits / 2;
1289 div2 = config->btr_nco_bits - div1 - 1;
1290
1291 bTrNomFreq = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_NOM_FREQ);
1292
1293 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DECIM_CNTRL);
1294 decimRate = STB0899_GETFIELD(DECIM_RATE, reg);
1295 decimRate = (1 << decimRate);
1296
1297 intval1 = internal->master_clk / (1 << div1);
1298 intval2 = bTrNomFreq / (1 << div2);
1299
1300 rem1 = internal->master_clk % (1 << div1);
1301 rem2 = bTrNomFreq % (1 << div2);
1302
1303 srate = (intval1 * intval2) + ((intval1 * rem2) / (1 << div2)) + ((intval2 * rem1) / (1 << div1));
1304 srate /= decimRate;
1305
1306 return srate;
1307}
1308
1309
1310
1311
1312
1313
1314enum stb0899_status stb0899_dvbs2_algo(struct stb0899_state *state)
1315{
1316 struct stb0899_internal *internal = &state->internal;
1317 enum stb0899_modcod modcod;
1318
1319 s32 offsetfreq, searchTime, FecLockTime, pilots, iqSpectrum;
1320 int i = 0;
1321 u32 reg, csm1;
1322
1323 if (internal->srate <= 2000000) {
1324 searchTime = 5000;
1325 FecLockTime = 350;
1326 } else if (internal->srate <= 5000000) {
1327 searchTime = 2500;
1328 FecLockTime = 170;
1329 } else if (internal->srate <= 10000000) {
1330 searchTime = 1500;
1331 FecLockTime = 80;
1332 } else if (internal->srate <= 15000000) {
1333 searchTime = 500;
1334 FecLockTime = 50;
1335 } else if (internal->srate <= 20000000) {
1336 searchTime = 300;
1337 FecLockTime = 30;
1338 } else if (internal->srate <= 25000000) {
1339 searchTime = 250;
1340 FecLockTime = 25;
1341 } else {
1342 searchTime = 150;
1343 FecLockTime = 20;
1344 }
1345
1346
1347 reg = stb0899_read_reg(state, STB0899_TSTRES);
1348 STB0899_SETFIELD_VAL(FRESRS, reg, 1);
1349 stb0899_write_reg(state, STB0899_TSTRES, reg);
1350
1351
1352 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1353
1354
1355 if (state->config->tuner_set_frequency)
1356 state->config->tuner_set_frequency(&state->frontend, internal->freq);
1357 if (state->config->tuner_get_frequency)
1358 state->config->tuner_get_frequency(&state->frontend, &internal->freq);
1359
1360
1361 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1362
1363
1364 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
1365 STB0899_SETFIELD_VAL(IF_LOOP_GAIN, reg, 4);
1366 STB0899_SETFIELD_VAL(IF_AGC_REF, reg, 32);
1367 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
1368
1369 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL2);
1370 STB0899_SETFIELD_VAL(IF_AGC_DUMP_PER, reg, 0);
1371 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL2, STB0899_OFF0_IF_AGC_CNTRL2, reg);
1372
1373
1374 stb0899_dvbs2_init_calc(state);
1375
1376 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
1377 switch (internal->inversion) {
1378 case IQ_SWAP_OFF:
1379 STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, 0);
1380 break;
1381 case IQ_SWAP_ON:
1382 STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, 1);
1383 break;
1384 }
1385 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DMD_CNTRL2, STB0899_OFF0_DMD_CNTRL2, reg);
1386 stb0899_dvbs2_reacquire(state);
1387
1388
1389 internal->status = stb0899_dvbs2_get_dmd_status(state, searchTime);
1390
1391 if (internal->status == DVBS2_DEMOD_LOCK) {
1392 dprintk(state->verbose, FE_DEBUG, 1, "------------> DVB-S2 DEMOD LOCK !");
1393 i = 0;
1394
1395 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1396
1397
1398 while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
1399
1400 offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
1401
1402
1403 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
1404 STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, offsetfreq);
1405 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
1406 stb0899_dvbs2_reacquire(state);
1407 internal->status = stb0899_dvbs2_get_fec_status(state, searchTime);
1408 i++;
1409 }
1410 }
1411
1412 if (internal->status != DVBS2_FEC_LOCK) {
1413 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
1414 iqSpectrum = STB0899_GETFIELD(SPECTRUM_INVERT, reg);
1415
1416 STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, !iqSpectrum);
1417 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DMD_CNTRL2, STB0899_OFF0_DMD_CNTRL2, reg);
1418
1419 stb0899_dvbs2_reacquire(state);
1420
1421
1422 internal->status = stb0899_dvbs2_get_dmd_status(state, searchTime);
1423 if (internal->status == DVBS2_DEMOD_LOCK) {
1424 i = 0;
1425
1426 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1427
1428 while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
1429
1430 offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
1431
1432
1433 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
1434 STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, offsetfreq);
1435 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
1436
1437 stb0899_dvbs2_reacquire(state);
1438 internal->status = stb0899_dvbs2_get_fec_status(state, searchTime);
1439 i++;
1440 }
1441 }
1442
1443
1444
1445
1446 }
1447 if (internal->status == DVBS2_FEC_LOCK) {
1448 dprintk(state->verbose, FE_DEBUG, 1, "----------------> DVB-S2 FEC Lock !");
1449 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1450 modcod = STB0899_GETFIELD(UWP_DECODE_MOD, reg) >> 2;
1451 pilots = STB0899_GETFIELD(UWP_DECODE_MOD, reg) & 0x01;
1452
1453 if ((((10 * internal->master_clk) / (internal->srate / 10)) <= 410) &&
1454 (INRANGE(STB0899_QPSK_23, modcod, STB0899_QPSK_910)) &&
1455 (pilots == 1)) {
1456
1457 stb0899_dvbs2_init_csm(state, pilots, modcod);
1458
1459 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1460
1461 i = 0;
1462 while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
1463 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1464 STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, 1);
1465 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1466 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1467 STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, 0);
1468 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1469
1470 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1471 i++;
1472 }
1473 }
1474
1475 if ((((10 * internal->master_clk) / (internal->srate / 10)) <= 410) &&
1476 (INRANGE(STB0899_QPSK_12, modcod, STB0899_QPSK_35)) &&
1477 (pilots == 1)) {
1478
1479
1480 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
1481 STB0899_SETFIELD_VAL(EQ_DISABLE_UPDATE, reg, 1);
1482 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
1483 }
1484
1485
1486 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
1487 STB0899_SETFIELD_VAL(EQ_SHIFT, reg, 0x02);
1488 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
1489
1490
1491 offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
1492
1493 offsetfreq = sign_extend32(offsetfreq, 29);
1494
1495 offsetfreq = offsetfreq / ((1 << 30) / 1000);
1496 offsetfreq *= (internal->master_clk / 1000000);
1497
1498
1499 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
1500 if (STB0899_GETFIELD(SPECTRUM_INVERT, reg))
1501 internal->inversion = IQ_SWAP_ON;
1502 else
1503 internal->inversion = IQ_SWAP_OFF;
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
1514 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
1515 STB0899_SETFIELD_VAL(IF_LOOP_GAIN, reg, 3);
1516
1517
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
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}
1535