2 STB0899 Multistandard Frontend driver
3 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
5 Copyright (C) ST Microelectronics
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "stb0899_drv.h"
23 #include "stb0899_priv.h"
24 #include "stb0899_reg.h"
26 inline u32 stb0899_do_div(u64 n, u32 d)
28 /* wrap do_div() for ease of use */
38 static u32 stb0899_calc_srate(u32 master_clk, u8 *sfr)
42 /* srate = (SFR * master_clk) >> 20 */
44 /* 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];
55 * Get the current symbol rate
57 u32 stb0899_get_srate(struct stb0899_state *state)
59 struct stb0899_internal *internal = &state->internal;
62 stb0899_read_regs(state, STB0899_SFRH, sfr, 3);
64 return stb0899_calc_srate(internal->master_clk, sfr);
69 * Set symbol frequency
70 * MasterClock: master clock frequency (hz)
71 * SymbolRate: symbol rate (bauds)
72 * return symbol frequency
74 static u32 stb0899_set_srate(struct stb0899_state *state, u32 master_clk, u32 srate)
79 dprintk(state->verbose, FE_DEBUG, 1, "-->");
81 * in order to have the maximum precision, the symbol rate entered into
82 * the chip is computed as the closest value of the "true value".
83 * In this purpose, the symbol rate value is rounded (1 is added on the bit
86 * srate = (SFR * master_clk) >> 20
88 * SFR = srate << 20 / master_clk
91 * SFR = (srate << 21 + master_clk) / (2 * master_clk)
93 * stored as 20 bit number with an offset of 4 bit:
97 tmp = stb0899_do_div((((u64)srate) << 21) + master_clk, 2 * master_clk);
104 stb0899_write_regs(state, STB0899_SFRH, sfr, 3);
110 * stb0899_calc_derot_time
111 * Compute the amount of time needed by the derotator to lock
112 * SymbolRate: Symbol rate
113 * return: derotator time constant (ms)
115 static long stb0899_calc_derot_time(long srate)
118 return (100000 / (srate / 1000));
125 * Compute the width of the carrier
126 * return: width of carrier (kHz or Mhz)
128 long stb0899_carr_width(struct stb0899_state *state)
130 struct stb0899_internal *internal = &state->internal;
132 return (internal->srate + (internal->srate * internal->rolloff) / 100);
136 * stb0899_first_subrange
137 * Compute the first subrange of the search
139 static void stb0899_first_subrange(struct stb0899_state *state)
141 struct stb0899_internal *internal = &state->internal;
142 struct stb0899_params *params = &state->params;
143 struct stb0899_config *config = state->config;
148 if (config->tuner_get_bandwidth) {
149 config->tuner_get_bandwidth(&state->frontend, &bandwidth);
150 range = bandwidth - stb0899_carr_width(state) / 2;
154 internal->sub_range = MIN(internal->srch_range, range);
156 internal->sub_range = 0;
158 internal->freq = params->freq;
159 internal->tuner_offst = 0L;
160 internal->sub_dir = 1;
165 * check for timing lock
166 * internal.Ttiming: time to wait for loop lock
168 static enum stb0899_status stb0899_check_tmg(struct stb0899_state *state)
170 struct stb0899_internal *internal = &state->internal;
175 msleep(internal->t_derot);
177 stb0899_write_reg(state, STB0899_RTF, 0xf2);
178 reg = stb0899_read_reg(state, STB0899_TLIR);
179 lock = STB0899_GETFIELD(TLIR_TMG_LOCK_IND, reg);
180 timing = stb0899_read_reg(state, STB0899_RTF);
183 if ((lock > 48) && (ABS(timing) >= 110)) {
184 internal->status = ANALOGCARRIER;
185 dprintk(state->verbose, FE_DEBUG, 1, "-->ANALOG Carrier !");
187 internal->status = TIMINGOK;
188 dprintk(state->verbose, FE_DEBUG, 1, "------->TIMING OK !");
191 internal->status = NOTIMING;
192 dprintk(state->verbose, FE_DEBUG, 1, "-->NO TIMING !");
194 return internal->status;
199 * perform a fs/2 zig-zag to find timing
201 static enum stb0899_status stb0899_search_tmg(struct stb0899_state *state)
203 struct stb0899_internal *internal = &state->internal;
204 struct stb0899_params *params = &state->params;
206 short int derot_step, derot_freq = 0, derot_limit, next_loop = 3;
210 internal->status = NOTIMING;
212 /* timing loop computation & symbol rate optimisation */
213 derot_limit = (internal->sub_range / 2L) / internal->mclk;
214 derot_step = (params->srate / 2L) / internal->mclk;
216 while ((stb0899_check_tmg(state) != TIMINGOK) && next_loop) {
218 derot_freq += index * internal->direction * derot_step; /* next derot zig zag position */
220 if (ABS(derot_freq) > derot_limit)
224 STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(state->config->inversion * derot_freq));
225 STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(state->config->inversion * derot_freq));
226 stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */
228 internal->direction = -internal->direction; /* Change zigzag direction */
231 if (internal->status == TIMINGOK) {
232 stb0899_read_regs(state, STB0899_CFRM, cfr, 2); /* get derotator frequency */
233 internal->derot_freq = state->config->inversion * MAKEWORD16(cfr[0], cfr[1]);
234 dprintk(state->verbose, FE_DEBUG, 1, "------->TIMING OK ! Derot Freq = %d", internal->derot_freq);
237 return internal->status;
241 * stb0899_check_carrier
242 * Check for carrier found
244 static enum stb0899_status stb0899_check_carrier(struct stb0899_state *state)
246 struct stb0899_internal *internal = &state->internal;
249 msleep(internal->t_derot); /* wait for derotator ok */
251 reg = stb0899_read_reg(state, STB0899_CFD);
252 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
253 stb0899_write_reg(state, STB0899_CFD, reg);
255 reg = stb0899_read_reg(state, STB0899_DSTATUS);
256 dprintk(state->verbose, FE_DEBUG, 1, "--------------------> STB0899_DSTATUS=[0x%02x]", reg);
257 if (STB0899_GETFIELD(CARRIER_FOUND, reg)) {
258 internal->status = CARRIEROK;
259 dprintk(state->verbose, FE_DEBUG, 1, "-------------> CARRIEROK !");
261 internal->status = NOCARRIER;
262 dprintk(state->verbose, FE_DEBUG, 1, "-------------> NOCARRIER !");
265 return internal->status;
269 * stb0899_search_carrier
270 * Search for a QPSK carrier with the derotator
272 static enum stb0899_status stb0899_search_carrier(struct stb0899_state *state)
274 struct stb0899_internal *internal = &state->internal;
276 short int derot_freq = 0, last_derot_freq = 0, derot_limit, next_loop = 3;
281 internal->status = NOCARRIER;
282 derot_limit = (internal->sub_range / 2L) / internal->mclk;
283 derot_freq = internal->derot_freq;
285 reg = stb0899_read_reg(state, STB0899_CFD);
286 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
287 stb0899_write_reg(state, STB0899_CFD, reg);
290 dprintk(state->verbose, FE_DEBUG, 1, "Derot Freq=%d, mclk=%d", derot_freq, internal->mclk);
291 if (stb0899_check_carrier(state) == NOCARRIER) {
293 last_derot_freq = derot_freq;
294 derot_freq += index * internal->direction * internal->derot_step; /* next zig zag derotator position */
296 if(ABS(derot_freq) > derot_limit)
300 reg = stb0899_read_reg(state, STB0899_CFD);
301 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
302 stb0899_write_reg(state, STB0899_CFD, reg);
304 STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(state->config->inversion * derot_freq));
305 STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(state->config->inversion * derot_freq));
306 stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */
310 internal->direction = -internal->direction; /* Change zigzag direction */
311 } while ((internal->status != CARRIEROK) && next_loop);
313 if (internal->status == CARRIEROK) {
314 stb0899_read_regs(state, STB0899_CFRM, cfr, 2); /* get derotator frequency */
315 internal->derot_freq = state->config->inversion * MAKEWORD16(cfr[0], cfr[1]);
316 dprintk(state->verbose, FE_DEBUG, 1, "----> CARRIER OK !, Derot Freq=%d", internal->derot_freq);
318 internal->derot_freq = last_derot_freq;
321 return internal->status;
326 * Check for data found
328 static enum stb0899_status stb0899_check_data(struct stb0899_state *state)
330 struct stb0899_internal *internal = &state->internal;
331 struct stb0899_params *params = &state->params;
333 int lock = 0, index = 0, dataTime = 500, loop;
336 internal->status = NODATA;
339 reg = stb0899_read_reg(state, STB0899_TSTRES);
340 STB0899_SETFIELD_VAL(FRESACS, reg, 1);
341 stb0899_write_reg(state, STB0899_TSTRES, reg);
343 reg = stb0899_read_reg(state, STB0899_TSTRES);
344 STB0899_SETFIELD_VAL(FRESACS, reg, 0);
345 stb0899_write_reg(state, STB0899_TSTRES, reg);
347 if (params->srate <= 2000000)
349 else if (params->srate <= 5000000)
351 else if (params->srate <= 15000000)
356 stb0899_write_reg(state, STB0899_DSTATUS2, 0x00); /* force search loop */
358 /* WARNING! VIT LOCKED has to be tested before VIT_END_LOOOP */
359 reg = stb0899_read_reg(state, STB0899_VSTATUS);
360 lock = STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg);
361 loop = STB0899_GETFIELD(VSTATUS_END_LOOPVIT, reg);
363 if (lock || loop || (index > dataTime))
368 if (lock) { /* DATA LOCK indicator */
369 internal->status = DATAOK;
370 dprintk(state->verbose, FE_DEBUG, 1, "-----------------> DATA OK !");
373 return internal->status;
377 * stb0899_search_data
378 * Search for a QPSK carrier with the derotator
380 static enum stb0899_status stb0899_search_data(struct stb0899_state *state)
382 short int derot_freq, derot_step, derot_limit, next_loop = 3;
387 struct stb0899_internal *internal = &state->internal;
388 struct stb0899_params *params = &state->params;
390 derot_step = (params->srate / 4L) / internal->mclk;
391 derot_limit = (internal->sub_range / 2L) / internal->mclk;
392 derot_freq = internal->derot_freq;
395 if ((internal->status != CARRIEROK) || (stb0899_check_data(state) != DATAOK)) {
397 derot_freq += index * internal->direction * derot_step; /* next zig zag derotator position */
398 if (ABS(derot_freq) > derot_limit)
402 dprintk(state->verbose, FE_DEBUG, 1, "Derot freq=%d, mclk=%d", derot_freq, internal->mclk);
403 reg = stb0899_read_reg(state, STB0899_CFD);
404 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
405 stb0899_write_reg(state, STB0899_CFD, reg);
407 STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(state->config->inversion * derot_freq));
408 STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(state->config->inversion * derot_freq));
409 stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */
411 stb0899_check_carrier(state);
415 internal->direction = -internal->direction; /* change zig zag direction */
416 } while ((internal->status != DATAOK) && next_loop);
418 if (internal->status == DATAOK) {
419 stb0899_read_regs(state, STB0899_CFRM, cfr, 2); /* get derotator frequency */
420 internal->derot_freq = state->config->inversion * MAKEWORD16(cfr[0], cfr[1]);
421 dprintk(state->verbose, FE_DEBUG, 1, "------> DATAOK ! Derot Freq=%d", internal->derot_freq);
424 return internal->status;
428 * stb0899_check_range
429 * check if the found frequency is in the correct range
431 static enum stb0899_status stb0899_check_range(struct stb0899_state *state)
433 struct stb0899_internal *internal = &state->internal;
434 struct stb0899_params *params = &state->params;
436 int range_offst, tp_freq;
438 range_offst = internal->srch_range / 2000;
439 tp_freq = internal->freq + (internal->derot_freq * internal->mclk) / 1000;
441 if ((tp_freq >= params->freq - range_offst) && (tp_freq <= params->freq + range_offst)) {
442 internal->status = RANGEOK;
443 dprintk(state->verbose, FE_DEBUG, 1, "----> RANGEOK !");
445 internal->status = OUTOFRANGE;
446 dprintk(state->verbose, FE_DEBUG, 1, "----> OUT OF RANGE !");
449 return internal->status;
454 * Compute the next subrange of the search
456 static void next_sub_range(struct stb0899_state *state)
458 struct stb0899_internal *internal = &state->internal;
459 struct stb0899_params *params = &state->params;
463 if (internal->sub_dir > 0) {
464 old_sub_range = internal->sub_range;
465 internal->sub_range = MIN((internal->srch_range / 2) -
466 (internal->tuner_offst + internal->sub_range / 2),
467 internal->sub_range);
469 if (internal->sub_range < 0)
470 internal->sub_range = 0;
472 internal->tuner_offst += (old_sub_range + internal->sub_range) / 2;
475 internal->freq = params->freq + (internal->sub_dir * internal->tuner_offst) / 1000;
476 internal->sub_dir = -internal->sub_dir;
481 * Search for a signal, timing, carrier and data for a
482 * given frequency in a given range
484 enum stb0899_status stb0899_dvbs_algo(struct stb0899_state *state)
486 struct stb0899_params *params = &state->params;
487 struct stb0899_internal *internal = &state->internal;
488 struct stb0899_config *config = state->config;
496 /* BETA values rated @ 99MHz */
497 s32 betaTab[5][4] = {
499 { 37, 34, 32, 31 }, /* QPSK 1/2 */
500 { 37, 35, 33, 31 }, /* QPSK 2/3 */
501 { 37, 35, 33, 31 }, /* QPSK 3/4 */
502 { 37, 36, 33, 32 }, /* QPSK 5/6 */
503 { 37, 36, 33, 32 } /* QPSK 7/8 */
506 internal->direction = 1;
508 stb0899_set_srate(state, internal->master_clk, params->srate);
509 /* Carrier loop optimization versus symbol rate for acquisition*/
510 if (params->srate <= 5000000) {
511 stb0899_write_reg(state, STB0899_ACLC, 0x89);
512 bclc = stb0899_read_reg(state, STB0899_BCLC);
513 STB0899_SETFIELD_VAL(BETA, bclc, 0x1c);
514 stb0899_write_reg(state, STB0899_BCLC, bclc);
516 } else if (params->srate <= 15000000) {
517 stb0899_write_reg(state, STB0899_ACLC, 0xc9);
518 bclc = stb0899_read_reg(state, STB0899_BCLC);
519 STB0899_SETFIELD_VAL(BETA, bclc, 0x22);
520 stb0899_write_reg(state, STB0899_BCLC, bclc);
522 } else if(params->srate <= 25000000) {
523 stb0899_write_reg(state, STB0899_ACLC, 0x89);
524 bclc = stb0899_read_reg(state, STB0899_BCLC);
525 STB0899_SETFIELD_VAL(BETA, bclc, 0x27);
526 stb0899_write_reg(state, STB0899_BCLC, bclc);
529 stb0899_write_reg(state, STB0899_ACLC, 0xc8);
530 bclc = stb0899_read_reg(state, STB0899_BCLC);
531 STB0899_SETFIELD_VAL(BETA, bclc, 0x29);
532 stb0899_write_reg(state, STB0899_BCLC, bclc);
536 dprintk(state->verbose, FE_DEBUG, 1, "Set the timing loop to acquisition");
537 /* Set the timing loop to acquisition */
538 stb0899_write_reg(state, STB0899_RTC, 0x46);
539 stb0899_write_reg(state, STB0899_CFD, 0xee);
542 * Do not read any status variables while acquisition,
543 * If any needed, read before the acquisition starts
544 * querying status while acquiring causes the
545 * acquisition to go bad and hence no locks.
547 dprintk(state->verbose, FE_DEBUG, 1, "Derot Percent=%d Srate=%d mclk=%d",
548 internal->derot_percent, params->srate, internal->mclk);
550 /* Initial calculations */
551 internal->derot_step = internal->derot_percent * (params->srate / 1000L) / internal->mclk; /* DerotStep/1000 * Fsymbol */
552 internal->t_derot = stb0899_calc_derot_time(params->srate);
553 internal->t_data = 500;
555 dprintk(state->verbose, FE_DEBUG, 1, "RESET stream merger");
556 /* RESET Stream merger */
557 reg = stb0899_read_reg(state, STB0899_TSTRES);
558 STB0899_SETFIELD_VAL(FRESRS, reg, 1);
559 stb0899_write_reg(state, STB0899_TSTRES, reg);
562 * Set KDIVIDER to an intermediate value between
563 * 1/2 and 7/8 for acquisition
565 reg = stb0899_read_reg(state, STB0899_DEMAPVIT);
566 STB0899_SETFIELD_VAL(DEMAPVIT_KDIVIDER, reg, 60);
567 stb0899_write_reg(state, STB0899_DEMAPVIT, reg);
569 stb0899_write_reg(state, STB0899_EQON, 0x01); /* Equalizer OFF while acquiring */
570 stb0899_write_reg(state, STB0899_VITSYNC, 0x19);
572 stb0899_first_subrange(state);
574 /* Initialisations */
576 stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* RESET derotator frequency */
578 stb0899_write_reg(state, STB0899_RTF, 0);
579 reg = stb0899_read_reg(state, STB0899_CFD);
580 STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
581 stb0899_write_reg(state, STB0899_CFD, reg);
583 internal->derot_freq = 0;
584 internal->status = NOAGC1;
586 /* enable tuner I/O */
587 stb0899_i2c_gate_ctrl(&state->frontend, 1);
589 /* Move tuner to frequency */
590 dprintk(state->verbose, FE_DEBUG, 1, "Tuner set frequency");
591 if (state->config->tuner_set_frequency)
592 state->config->tuner_set_frequency(&state->frontend, internal->freq);
594 if (state->config->tuner_get_frequency)
595 state->config->tuner_get_frequency(&state->frontend, &internal->freq);
597 msleep(internal->t_agc1 + internal->t_agc2 + internal->t_derot); /* AGC1, AGC2 and timing loop */
598 dprintk(state->verbose, FE_DEBUG, 1, "current derot freq=%d", internal->derot_freq);
599 internal->status = AGC1OK;
601 /* There is signal in the band */
602 if (config->tuner_get_bandwidth)
603 config->tuner_get_bandwidth(&state->frontend, &bandwidth);
605 /* disable tuner I/O */
606 stb0899_i2c_gate_ctrl(&state->frontend, 0);
608 if (params->srate <= bandwidth / 2)
609 stb0899_search_tmg(state); /* For low rates (SCPC) */
611 stb0899_check_tmg(state); /* For high rates (MCPC) */
613 if (internal->status == TIMINGOK) {
614 dprintk(state->verbose, FE_DEBUG, 1,
615 "TIMING OK ! Derot freq=%d, mclk=%d",
616 internal->derot_freq, internal->mclk);
618 if (stb0899_search_carrier(state) == CARRIEROK) { /* Search for carrier */
619 dprintk(state->verbose, FE_DEBUG, 1,
620 "CARRIER OK ! Derot freq=%d, mclk=%d",
621 internal->derot_freq, internal->mclk);
623 if (stb0899_search_data(state) == DATAOK) { /* Check for data */
624 dprintk(state->verbose, FE_DEBUG, 1,
625 "DATA OK ! Derot freq=%d, mclk=%d",
626 internal->derot_freq, internal->mclk);
628 if (stb0899_check_range(state) == RANGEOK) {
629 dprintk(state->verbose, FE_DEBUG, 1,
630 "RANGE OK ! derot freq=%d, mclk=%d",
631 internal->derot_freq, internal->mclk);
633 internal->freq = params->freq + ((internal->derot_freq * internal->mclk) / 1000);
634 reg = stb0899_read_reg(state, STB0899_PLPARM);
635 internal->fecrate = STB0899_GETFIELD(VITCURPUN, reg);
636 dprintk(state->verbose, FE_DEBUG, 1,
637 "freq=%d, internal resultant freq=%d",
638 params->freq, internal->freq);
640 dprintk(state->verbose, FE_DEBUG, 1,
641 "internal puncture rate=%d",
647 if (internal->status != RANGEOK)
648 next_sub_range(state);
650 } while (internal->sub_range && internal->status != RANGEOK);
652 /* Set the timing loop to tracking */
653 stb0899_write_reg(state, STB0899_RTC, 0x33);
654 stb0899_write_reg(state, STB0899_CFD, 0xf7);
655 /* if locked and range ok, set Kdiv */
656 if (internal->status == RANGEOK) {
657 dprintk(state->verbose, FE_DEBUG, 1, "Locked & Range OK !");
658 stb0899_write_reg(state, STB0899_EQON, 0x41); /* Equalizer OFF while acquiring */
659 stb0899_write_reg(state, STB0899_VITSYNC, 0x39); /* SN to b'11 for acquisition */
662 * Carrier loop optimization versus
663 * symbol Rate/Puncture Rate for Tracking
665 reg = stb0899_read_reg(state, STB0899_BCLC);
666 switch (internal->fecrate) {
667 case STB0899_FEC_1_2: /* 13 */
668 stb0899_write_reg(state, STB0899_DEMAPVIT, 0x1a);
669 STB0899_SETFIELD_VAL(BETA, reg, betaTab[0][clnI]);
670 stb0899_write_reg(state, STB0899_BCLC, reg);
672 case STB0899_FEC_2_3: /* 18 */
673 stb0899_write_reg(state, STB0899_DEMAPVIT, 44);
674 STB0899_SETFIELD_VAL(BETA, reg, betaTab[1][clnI]);
675 stb0899_write_reg(state, STB0899_BCLC, reg);
677 case STB0899_FEC_3_4: /* 21 */
678 stb0899_write_reg(state, STB0899_DEMAPVIT, 60);
679 STB0899_SETFIELD_VAL(BETA, reg, betaTab[2][clnI]);
680 stb0899_write_reg(state, STB0899_BCLC, reg);
682 case STB0899_FEC_5_6: /* 24 */
683 stb0899_write_reg(state, STB0899_DEMAPVIT, 75);
684 STB0899_SETFIELD_VAL(BETA, reg, betaTab[3][clnI]);
685 stb0899_write_reg(state, STB0899_BCLC, reg);
687 case STB0899_FEC_6_7: /* 25 */
688 stb0899_write_reg(state, STB0899_DEMAPVIT, 88);
689 stb0899_write_reg(state, STB0899_ACLC, 0x88);
690 stb0899_write_reg(state, STB0899_BCLC, 0x9a);
692 case STB0899_FEC_7_8: /* 26 */
693 stb0899_write_reg(state, STB0899_DEMAPVIT, 94);
694 STB0899_SETFIELD_VAL(BETA, reg, betaTab[4][clnI]);
695 stb0899_write_reg(state, STB0899_BCLC, reg);
698 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported Puncture Rate");
701 /* release stream merger RESET */
702 reg = stb0899_read_reg(state, STB0899_TSTRES);
703 STB0899_SETFIELD_VAL(FRESRS, reg, 0);
704 stb0899_write_reg(state, STB0899_TSTRES, reg);
706 /* disable carrier detector */
707 reg = stb0899_read_reg(state, STB0899_CFD);
708 STB0899_SETFIELD_VAL(CFD_ON, reg, 0);
709 stb0899_write_reg(state, STB0899_CFD, reg);
711 stb0899_read_regs(state, STB0899_EQUAI1, eq_const, 10);
714 return internal->status;
718 * stb0899_dvbs2_config_uwp
719 * Configure UWP state machine
721 static void stb0899_dvbs2_config_uwp(struct stb0899_state *state)
723 struct stb0899_internal *internal = &state->internal;
724 struct stb0899_config *config = state->config;
725 u32 uwp1, uwp2, uwp3, reg;
727 uwp1 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
728 uwp2 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL2);
729 uwp3 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL3);
731 STB0899_SETFIELD_VAL(UWP_ESN0_AVE, uwp1, config->esno_ave);
732 STB0899_SETFIELD_VAL(UWP_ESN0_QUANT, uwp1, config->esno_quant);
733 STB0899_SETFIELD_VAL(UWP_TH_SOF, uwp1, config->uwp_threshold_sof);
735 STB0899_SETFIELD_VAL(FE_COARSE_TRK, uwp2, internal->av_frame_coarse);
736 STB0899_SETFIELD_VAL(FE_FINE_TRK, uwp2, internal->av_frame_fine);
737 STB0899_SETFIELD_VAL(UWP_MISS_TH, uwp2, config->miss_threshold);
739 STB0899_SETFIELD_VAL(UWP_TH_ACQ, uwp3, config->uwp_threshold_acq);
740 STB0899_SETFIELD_VAL(UWP_TH_TRACK, uwp3, config->uwp_threshold_track);
742 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL1, STB0899_OFF0_UWP_CNTRL1, uwp1);
743 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL2, STB0899_OFF0_UWP_CNTRL2, uwp2);
744 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL3, STB0899_OFF0_UWP_CNTRL3, uwp3);
746 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, SOF_SRCH_TO);
747 STB0899_SETFIELD_VAL(SOF_SEARCH_TIMEOUT, reg, config->sof_search_timeout);
748 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_SOF_SRCH_TO, STB0899_OFF0_SOF_SRCH_TO, reg);
752 * stb0899_dvbs2_config_csm_auto
753 * Set CSM to AUTO mode
755 static void stb0899_dvbs2_config_csm_auto(struct stb0899_state *state)
759 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
760 STB0899_SETFIELD_VAL(CSM_AUTO_PARAM, reg, 1);
761 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, reg);
764 long Log2Int(int number)
769 while ((1 << i) <= ABS(number))
779 * stb0899_dvbs2_calc_srate
780 * compute BTR_NOM_FREQ for the symbol rate
782 static u32 stb0899_dvbs2_calc_srate(struct stb0899_state *state)
784 struct stb0899_internal *internal = &state->internal;
785 struct stb0899_config *config = state->config;
787 u32 dec_ratio, dec_rate, decim, remain, intval, btr_nom_freq;
788 u32 master_clk, srate;
790 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
791 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
792 dec_rate = Log2Int(dec_ratio);
793 decim = 1 << dec_rate;
794 master_clk = internal->master_clk / 1000;
795 srate = internal->srate / 1000;
798 intval = (decim * (1 << (config->btr_nco_bits - 1))) / master_clk;
799 remain = (decim * (1 << (config->btr_nco_bits - 1))) % master_clk;
801 intval = (1 << (config->btr_nco_bits - 1)) / (master_clk / 100) * decim / 100;
802 remain = (decim * (1 << (config->btr_nco_bits - 1))) % master_clk;
804 btr_nom_freq = (intval * srate) + ((remain * srate) / master_clk);
810 * stb0899_dvbs2_calc_dev
811 * compute the correction to be applied to symbol rate
813 static u32 stb0899_dvbs2_calc_dev(struct stb0899_state *state)
815 struct stb0899_internal *internal = &state->internal;
816 u32 dec_ratio, correction, master_clk, srate;
818 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
819 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
821 master_clk = internal->master_clk / 1000; /* for integer Caculation*/
822 srate = internal->srate / 1000; /* for integer Caculation*/
823 correction = (512 * master_clk) / (2 * dec_ratio * srate);
829 * stb0899_dvbs2_set_srate
830 * Set DVBS2 symbol rate
832 static void stb0899_dvbs2_set_srate(struct stb0899_state *state)
834 struct stb0899_internal *internal = &state->internal;
836 u32 dec_ratio, dec_rate, win_sel, decim, f_sym, btr_nom_freq;
837 u32 correction, freq_adj, band_lim, decim_cntrl, reg;
840 /*set decimation to 1*/
841 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
842 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
843 dec_rate = Log2Int(dec_ratio);
847 win_sel = dec_rate - 4;
849 decim = (1 << dec_rate);
850 /* (FSamp/Fsymbol *100) for integer Caculation */
851 f_sym = internal->master_clk / ((decim * internal->srate) / 1000);
853 if (f_sym <= 2250) /* don't band limit signal going into btr block*/
856 band_lim = 0; /* band limit signal going into btr block*/
858 decim_cntrl = ((win_sel << 3) & 0x18) + ((band_lim << 5) & 0x20) + (dec_rate & 0x7);
859 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DECIM_CNTRL, STB0899_OFF0_DECIM_CNTRL, decim_cntrl);
863 else if (f_sym <= 4250)
868 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ANTI_ALIAS_SEL, STB0899_OFF0_ANTI_ALIAS_SEL, anti_alias);
869 btr_nom_freq = stb0899_dvbs2_calc_srate(state);
870 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_NOM_FREQ, STB0899_OFF0_BTR_NOM_FREQ, btr_nom_freq);
872 correction = stb0899_dvbs2_calc_dev(state);
873 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_CNTRL);
874 STB0899_SETFIELD_VAL(BTR_FREQ_CORR, reg, correction);
875 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_CNTRL, STB0899_OFF0_BTR_CNTRL, reg);
877 /* scale UWP+CSM frequency to sample rate*/
878 freq_adj = internal->srate / (internal->master_clk / 4096);
879 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_FREQ_ADJ_SCALE, STB0899_OFF0_FREQ_ADJ_SCALE, freq_adj);
883 * stb0899_dvbs2_set_btr_loopbw
884 * set bit timing loop bandwidth as a percentage of the symbol rate
886 static void stb0899_dvbs2_set_btr_loopbw(struct stb0899_state *state)
888 struct stb0899_internal *internal = &state->internal;
889 struct stb0899_config *config = state->config;
891 u32 sym_peak = 23, zeta = 707, loopbw_percent = 60;
892 s32 dec_ratio, dec_rate, k_btr1_rshft, k_btr1, k_btr0_rshft;
893 s32 k_btr0, k_btr2_rshft, k_direct_shift, k_indirect_shift;
894 u32 decim, K, wn, k_direct, k_indirect;
897 dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
898 dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
899 dec_rate = Log2Int(dec_ratio);
900 decim = (1 << dec_rate);
903 K = (1 << config->btr_nco_bits) / (internal->master_clk / 1000);
904 K *= (internal->srate / 1000000) * decim; /*k=k 10^-8*/
908 wn = (4 * zeta * zeta) + 1000000;
909 wn = (2 * (loopbw_percent * 1000) * 40 * zeta) /wn; /*wn =wn 10^-8*/
911 k_indirect = (wn * wn) / K;
912 k_indirect = k_indirect; /*kindirect = kindirect 10^-6*/
913 k_direct = (2 * wn * zeta) / K; /*kDirect = kDirect 10^-2*/
916 k_direct_shift = Log2Int(k_direct) - Log2Int(10000) - 2;
917 k_btr1_rshft = (-1 * k_direct_shift) + config->btr_gain_shift_offset;
918 k_btr1 = k_direct / (1 << k_direct_shift);
921 k_indirect_shift = Log2Int(k_indirect + 15) - 20 /*- 2*/;
922 k_btr0_rshft = (-1 * k_indirect_shift) + config->btr_gain_shift_offset;
923 k_btr0 = k_indirect * (1 << (-k_indirect_shift));
927 if (k_btr0_rshft > 15) {
928 k_btr2_rshft = k_btr0_rshft - 15;
931 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_LOOP_GAIN);
932 STB0899_SETFIELD_VAL(KBTR0_RSHFT, reg, k_btr0_rshft);
933 STB0899_SETFIELD_VAL(KBTR0, reg, k_btr0);
934 STB0899_SETFIELD_VAL(KBTR1_RSHFT, reg, k_btr1_rshft);
935 STB0899_SETFIELD_VAL(KBTR1, reg, k_btr1);
936 STB0899_SETFIELD_VAL(KBTR2_RSHFT, reg, k_btr2_rshft);
937 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_LOOP_GAIN, STB0899_OFF0_BTR_LOOP_GAIN, reg);
939 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_LOOP_GAIN, STB0899_OFF0_BTR_LOOP_GAIN, 0xc4c4f);
943 * stb0899_dvbs2_set_carr_freq
944 * set nominal frequency for carrier search
946 static void stb0899_dvbs2_set_carr_freq(struct stb0899_state *state, s32 carr_freq, u32 master_clk)
948 struct stb0899_config *config = state->config;
952 crl_nom_freq = (1 << config->crl_nco_bits) / master_clk;
953 crl_nom_freq *= carr_freq;
954 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
955 STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, crl_nom_freq);
956 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
960 * stb0899_dvbs2_init_calc
961 * Initialize DVBS2 UWP, CSM, carrier and timing loops
963 static void stb0899_dvbs2_init_calc(struct stb0899_state *state)
965 struct stb0899_internal *internal = &state->internal;
966 s32 steps, step_size;
969 /* config uwp and csm */
970 stb0899_dvbs2_config_uwp(state);
971 stb0899_dvbs2_config_csm_auto(state);
974 stb0899_dvbs2_set_srate(state);
975 stb0899_dvbs2_set_btr_loopbw(state);
977 if (internal->srate / 1000000 >= 15)
978 step_size = (1 << 17) / 5;
979 else if (internal->srate / 1000000 >= 10)
980 step_size = (1 << 17) / 7;
981 else if (internal->srate / 1000000 >= 5)
982 step_size = (1 << 17) / 10;
984 step_size = (1 << 17) / 4;
986 range = internal->srch_range / 1000000;
987 steps = (10 * range * (1 << 17)) / (step_size * (internal->srate / 1000000));
988 steps = (steps + 6) / 10;
989 steps = (steps == 0) ? 1 : steps;
991 stb0899_dvbs2_set_carr_freq(state, internal->center_freq -
992 (internal->step_size * (internal->srate / 20000000)),
993 (internal->master_clk) / 1000000);
995 stb0899_dvbs2_set_carr_freq(state, internal->center_freq, (internal->master_clk) / 1000000);
997 /*Set Carrier Search params (zigzag, num steps and freq step size*/
998 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, ACQ_CNTRL2);
999 STB0899_SETFIELD_VAL(ZIGZAG, reg, 1);
1000 STB0899_SETFIELD_VAL(NUM_STEPS, reg, steps);
1001 STB0899_SETFIELD_VAL(FREQ_STEPSIZE, reg, step_size);
1002 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ACQ_CNTRL2, STB0899_OFF0_ACQ_CNTRL2, reg);
1006 * stb0899_dvbs2_btr_init
1007 * initialize the timing loop
1009 static void stb0899_dvbs2_btr_init(struct stb0899_state *state)
1013 /* set enable BTR loopback */
1014 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_CNTRL);
1015 STB0899_SETFIELD_VAL(INTRP_PHS_SENSE, reg, 1);
1016 STB0899_SETFIELD_VAL(BTR_ERR_ENA, reg, 1);
1017 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_CNTRL, STB0899_OFF0_BTR_CNTRL, reg);
1019 /* fix btr freq accum at 0 */
1020 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_FREQ_INIT, STB0899_OFF0_BTR_FREQ_INIT, 0x10000000);
1021 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_FREQ_INIT, STB0899_OFF0_BTR_FREQ_INIT, 0x00000000);
1023 /* fix btr freq accum at 0 */
1024 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_PHS_INIT, STB0899_OFF0_BTR_PHS_INIT, 0x10000000);
1025 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_PHS_INIT, STB0899_OFF0_BTR_PHS_INIT, 0x00000000);
1029 * stb0899_dvbs2_reacquire
1030 * trigger a DVB-S2 acquisition
1032 static void stb0899_dvbs2_reacquire(struct stb0899_state *state)
1036 /* demod soft reset */
1037 STB0899_SETFIELD_VAL(DVBS2_RESET, reg, 1);
1038 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_RESET_CNTRL, STB0899_OFF0_RESET_CNTRL, reg);
1040 /*Reset Timing Loop */
1041 stb0899_dvbs2_btr_init(state);
1043 /* reset Carrier loop */
1044 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_FREQ_INIT, STB0899_OFF0_CRL_FREQ_INIT, (1 << 30));
1045 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_FREQ_INIT, STB0899_OFF0_CRL_FREQ_INIT, 0);
1046 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_LOOP_GAIN, STB0899_OFF0_CRL_LOOP_GAIN, 0);
1047 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_PHS_INIT, STB0899_OFF0_CRL_PHS_INIT, (1 << 30));
1048 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_PHS_INIT, STB0899_OFF0_CRL_PHS_INIT, 0);
1050 /*release demod soft reset */
1052 STB0899_SETFIELD_VAL(DVBS2_RESET, reg, 0);
1053 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_RESET_CNTRL, STB0899_OFF0_RESET_CNTRL, reg);
1055 /* start acquisition process */
1056 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ACQUIRE_TRIG, STB0899_OFF0_ACQUIRE_TRIG, 1);
1057 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_LOCK_LOST, STB0899_OFF0_LOCK_LOST, 0);
1059 /* equalizer Init */
1060 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQUALIZER_INIT, STB0899_OFF0_EQUALIZER_INIT, 1);
1062 /*Start equilizer */
1063 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQUALIZER_INIT, STB0899_OFF0_EQUALIZER_INIT, 0);
1065 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
1066 STB0899_SETFIELD_VAL(EQ_SHIFT, reg, 0);
1067 STB0899_SETFIELD_VAL(EQ_DISABLE_UPDATE, reg, 0);
1068 STB0899_SETFIELD_VAL(EQ_DELAY, reg, 0x05);
1069 STB0899_SETFIELD_VAL(EQ_ADAPT_MODE, reg, 0x01);
1070 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
1072 /* RESET Packet delineator */
1073 stb0899_write_reg(state, STB0899_PDELCTRL, 0x4a);
1077 * stb0899_dvbs2_get_dmd_status
1078 * get DVB-S2 Demod LOCK status
1080 static enum stb0899_status stb0899_dvbs2_get_dmd_status(struct stb0899_state *state, int timeout)
1082 int time = -10, lock = 0, uwp, csm;
1086 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS);
1087 dprintk(state->verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg);
1088 if (STB0899_GETFIELD(IF_AGC_LOCK, reg))
1089 dprintk(state->verbose, FE_DEBUG, 1, "------------->IF AGC LOCKED !");
1090 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1091 dprintk(state->verbose, FE_DEBUG, 1, "----------->DMD STAT2=[0x%02x]", reg);
1092 uwp = STB0899_GETFIELD(UWP_LOCK, reg);
1093 csm = STB0899_GETFIELD(CSM_LOCK, reg);
1100 } while ((!lock) && (time <= timeout));
1103 dprintk(state->verbose, FE_DEBUG, 1, "----------------> DVB-S2 LOCK !");
1104 return DVBS2_DEMOD_LOCK;
1106 return DVBS2_DEMOD_NOLOCK;
1111 * stb0899_dvbs2_get_data_lock
1114 static int stb0899_dvbs2_get_data_lock(struct stb0899_state *state, int timeout)
1116 int time = 0, lock = 0;
1119 while ((!lock) && (time < timeout)) {
1120 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1121 dprintk(state->verbose, FE_DEBUG, 1, "---------> CFGPDELSTATUS=[0x%02x]", reg);
1122 lock = STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg);
1130 * stb0899_dvbs2_get_fec_status
1131 * get DVB-S2 FEC LOCK status
1133 static enum stb0899_status stb0899_dvbs2_get_fec_status(struct stb0899_state *state, int timeout)
1135 int time = 0, Locked;
1138 Locked = stb0899_dvbs2_get_data_lock(state, 1);
1142 } while ((!Locked) && (time < timeout));
1145 dprintk(state->verbose, FE_DEBUG, 1, "---------->DVB-S2 FEC LOCK !");
1146 return DVBS2_FEC_LOCK;
1148 return DVBS2_FEC_NOLOCK;
1154 * stb0899_dvbs2_init_csm
1155 * set parameters for manual mode
1157 static void stb0899_dvbs2_init_csm(struct stb0899_state *state, int pilots, enum stb0899_modcod modcod)
1159 struct stb0899_internal *internal = &state->internal;
1161 s32 dvt_tbl = 1, two_pass = 0, agc_gain = 6, agc_shift = 0, loop_shift = 0, phs_diff_thr = 0x80;
1162 s32 gamma_acq, gamma_rho_acq, gamma_trk, gamma_rho_trk, lock_count_thr;
1163 u32 csm1, csm2, csm3, csm4;
1165 if (((internal->master_clk / internal->srate) <= 4) && (modcod <= 11) && (pilots == 1)) {
1167 case STB0899_QPSK_12:
1169 gamma_rho_acq = 2700;
1171 gamma_rho_trk = 180;
1174 case STB0899_QPSK_35:
1176 gamma_rho_acq = 7182;
1178 gamma_rho_trk = 308;
1181 case STB0899_QPSK_23:
1183 gamma_rho_acq = 9408;
1185 gamma_rho_trk = 476;
1188 case STB0899_QPSK_34:
1190 gamma_rho_acq = 16642;
1192 gamma_rho_trk = 646;
1195 case STB0899_QPSK_45:
1197 gamma_rho_acq = 17119;
1199 gamma_rho_trk = 880;
1202 case STB0899_QPSK_56:
1204 gamma_rho_acq = 19250;
1206 gamma_rho_trk = 989;
1209 case STB0899_QPSK_89:
1211 gamma_rho_acq = 24240;
1213 gamma_rho_trk = 1176;
1216 case STB0899_QPSK_910:
1218 gamma_rho_acq = 29634;
1220 gamma_rho_trk = 1176;
1225 gamma_rho_acq = 29634;
1227 gamma_rho_trk = 1176;
1232 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1233 STB0899_SETFIELD_VAL(CSM_AUTO_PARAM, csm1, 0);
1234 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1236 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1237 csm2 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL2);
1238 csm3 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL3);
1239 csm4 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL4);
1241 STB0899_SETFIELD_VAL(CSM_DVT_TABLE, csm1, dvt_tbl);
1242 STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, two_pass);
1243 STB0899_SETFIELD_VAL(CSM_AGC_GAIN, csm1, agc_gain);
1244 STB0899_SETFIELD_VAL(CSM_AGC_SHIFT, csm1, agc_shift);
1245 STB0899_SETFIELD_VAL(FE_LOOP_SHIFT, csm1, loop_shift);
1246 STB0899_SETFIELD_VAL(CSM_GAMMA_ACQ, csm2, gamma_acq);
1247 STB0899_SETFIELD_VAL(CSM_GAMMA_RHOACQ, csm2, gamma_rho_acq);
1248 STB0899_SETFIELD_VAL(CSM_GAMMA_TRACK, csm3, gamma_trk);
1249 STB0899_SETFIELD_VAL(CSM_GAMMA_RHOTRACK, csm3, gamma_rho_trk);
1250 STB0899_SETFIELD_VAL(CSM_LOCKCOUNT_THRESH, csm4, lock_count_thr);
1251 STB0899_SETFIELD_VAL(CSM_PHASEDIFF_THRESH, csm4, phs_diff_thr);
1253 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1254 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL2, STB0899_OFF0_CSM_CNTRL2, csm2);
1255 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL3, STB0899_OFF0_CSM_CNTRL3, csm3);
1256 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL4, STB0899_OFF0_CSM_CNTRL4, csm4);
1261 * stb0899_dvbs2_get_srate
1262 * get DVB-S2 Symbol Rate
1264 static u32 stb0899_dvbs2_get_srate(struct stb0899_state *state)
1266 struct stb0899_internal *internal = &state->internal;
1267 struct stb0899_config *config = state->config;
1269 u32 bTrNomFreq, srate, decimRate, intval1, intval2, reg;
1270 int div1, div2, rem1, rem2;
1272 div1 = config->btr_nco_bits / 2;
1273 div2 = config->btr_nco_bits - div1 - 1;
1275 bTrNomFreq = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_NOM_FREQ);
1277 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DECIM_CNTRL);
1278 decimRate = STB0899_GETFIELD(DECIM_RATE, reg);
1279 decimRate = (1 << decimRate);
1281 intval1 = internal->master_clk / (1 << div1);
1282 intval2 = bTrNomFreq / (1 << div2);
1284 rem1 = internal->master_clk % (1 << div1);
1285 rem2 = bTrNomFreq % (1 << div2);
1286 /* only for integer calculation */
1287 srate = (intval1 * intval2) + ((intval1 * rem2) / (1 << div2)) + ((intval2 * rem1) / (1 << div1));
1288 srate /= decimRate; /*symbrate = (btrnomfreq_register_val*MasterClock)/2^(27+decim_rate_field) */
1294 * stb0899_dvbs2_algo
1295 * Search for signal, timing, carrier and data for a given
1296 * frequency in a given range
1298 enum stb0899_status stb0899_dvbs2_algo(struct stb0899_state *state)
1300 struct stb0899_internal *internal = &state->internal;
1301 enum stb0899_modcod modcod;
1303 s32 offsetfreq, searchTime, FecLockTime, pilots, iqSpectrum;
1307 if (internal->srate <= 2000000) {
1308 searchTime = 5000; /* 5000 ms max time to lock UWP and CSM, SYMB <= 2Mbs */
1309 FecLockTime = 350; /* 350 ms max time to lock FEC, SYMB <= 2Mbs */
1310 } else if (internal->srate <= 5000000) {
1311 searchTime = 2500; /* 2500 ms max time to lock UWP and CSM, 2Mbs < SYMB <= 5Mbs */
1312 FecLockTime = 170; /* 170 ms max time to lock FEC, 2Mbs< SYMB <= 5Mbs */
1313 } else if (internal->srate <= 10000000) {
1314 searchTime = 1500; /* 1500 ms max time to lock UWP and CSM, 5Mbs <SYMB <= 10Mbs */
1315 FecLockTime = 80; /* 80 ms max time to lock FEC, 5Mbs< SYMB <= 10Mbs */
1316 } else if (internal->srate <= 15000000) {
1317 searchTime = 500; /* 500 ms max time to lock UWP and CSM, 10Mbs <SYMB <= 15Mbs */
1318 FecLockTime = 50; /* 50 ms max time to lock FEC, 10Mbs< SYMB <= 15Mbs */
1319 } else if (internal->srate <= 20000000) {
1320 searchTime = 300; /* 300 ms max time to lock UWP and CSM, 15Mbs < SYMB <= 20Mbs */
1321 FecLockTime = 30; /* 50 ms max time to lock FEC, 15Mbs< SYMB <= 20Mbs */
1322 } else if (internal->srate <= 25000000) {
1323 searchTime = 250; /* 250 ms max time to lock UWP and CSM, 20 Mbs < SYMB <= 25Mbs */
1324 FecLockTime = 25; /* 25 ms max time to lock FEC, 20Mbs< SYMB <= 25Mbs */
1326 searchTime = 150; /* 150 ms max time to lock UWP and CSM, SYMB > 25Mbs */
1327 FecLockTime = 20; /* 20 ms max time to lock FEC, 20Mbs< SYMB <= 25Mbs */
1330 /* Maintain Stream Merger in reset during acquisition */
1331 reg = stb0899_read_reg(state, STB0899_TSTRES);
1332 STB0899_SETFIELD_VAL(FRESRS, reg, 1);
1333 stb0899_write_reg(state, STB0899_TSTRES, reg);
1335 /* enable tuner I/O */
1336 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1338 /* Move tuner to frequency */
1339 if (state->config->tuner_set_frequency)
1340 state->config->tuner_set_frequency(&state->frontend, internal->freq);
1341 if (state->config->tuner_get_frequency)
1342 state->config->tuner_get_frequency(&state->frontend, &internal->freq);
1344 /* disable tuner I/O */
1345 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1347 /* Set IF AGC to acquisition */
1348 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
1349 STB0899_SETFIELD_VAL(IF_LOOP_GAIN, reg, 4);
1350 STB0899_SETFIELD_VAL(IF_AGC_REF, reg, 32);
1351 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
1353 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL2);
1354 STB0899_SETFIELD_VAL(IF_AGC_DUMP_PER, reg, 0);
1355 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL2, STB0899_OFF0_IF_AGC_CNTRL2, reg);
1357 /* Initialisation */
1358 stb0899_dvbs2_init_calc(state);
1360 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
1361 switch (internal->inversion) {
1363 STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, 0);
1366 STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, 1);
1368 case IQ_SWAP_AUTO: /* use last successful search first */
1369 STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, 1);
1372 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DMD_CNTRL2, STB0899_OFF0_DMD_CNTRL2, reg);
1373 stb0899_dvbs2_reacquire(state);
1375 /* Wait for demod lock (UWP and CSM) */
1376 internal->status = stb0899_dvbs2_get_dmd_status(state, searchTime);
1378 if (internal->status == DVBS2_DEMOD_LOCK) {
1379 dprintk(state->verbose, FE_DEBUG, 1, "------------> DVB-S2 DEMOD LOCK !");
1381 /* Demod Locked, check FEC status */
1382 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1384 /*If false lock (UWP and CSM Locked but no FEC) try 3 time max*/
1385 while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
1386 /* Read the frequency offset*/
1387 offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
1389 /* Set the Nominal frequency to the found frequency offset for the next reacquire*/
1390 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
1391 STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, offsetfreq);
1392 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
1393 stb0899_dvbs2_reacquire(state);
1394 internal->status = stb0899_dvbs2_get_fec_status(state, searchTime);
1399 if (internal->status != DVBS2_FEC_LOCK) {
1400 if (internal->inversion == IQ_SWAP_AUTO) {
1401 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
1402 iqSpectrum = STB0899_GETFIELD(SPECTRUM_INVERT, reg);
1403 /* IQ Spectrum Inversion */
1404 STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, !iqSpectrum);
1405 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DMD_CNTRL2, STB0899_OFF0_DMD_CNTRL2, reg);
1406 /* start acquistion process */
1407 stb0899_dvbs2_reacquire(state);
1409 /* Wait for demod lock (UWP and CSM) */
1410 internal->status = stb0899_dvbs2_get_dmd_status(state, searchTime);
1411 if (internal->status == DVBS2_DEMOD_LOCK) {
1413 /* Demod Locked, check FEC */
1414 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1415 /*try thrice for false locks, (UWP and CSM Locked but no FEC) */
1416 while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
1417 /* Read the frequency offset*/
1418 offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
1420 /* Set the Nominal frequency to the found frequency offset for the next reacquire*/
1421 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
1422 STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, offsetfreq);
1423 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
1425 stb0899_dvbs2_reacquire(state);
1426 internal->status = stb0899_dvbs2_get_fec_status(state, searchTime);
1431 if (pParams->DVBS2State == FE_DVBS2_FEC_LOCKED)
1432 pParams->IQLocked = !iqSpectrum;
1436 if (internal->status == DVBS2_FEC_LOCK) {
1437 dprintk(state->verbose, FE_DEBUG, 1, "----------------> DVB-S2 FEC Lock !");
1438 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1439 modcod = STB0899_GETFIELD(UWP_DECODE_MOD, reg) >> 2;
1440 pilots = STB0899_GETFIELD(UWP_DECODE_MOD, reg) & 0x01;
1442 if ((((10 * internal->master_clk) / (internal->srate / 10)) <= 410) &&
1443 (INRANGE(STB0899_QPSK_23, modcod, STB0899_QPSK_910)) &&
1446 stb0899_dvbs2_init_csm(state, pilots, modcod);
1447 /* Wait for UWP,CSM and data LOCK 20ms max */
1448 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1451 while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
1452 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1453 STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, 1);
1454 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1455 csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
1456 STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, 0);
1457 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
1459 internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
1464 if ((((10 * internal->master_clk) / (internal->srate / 10)) <= 410) &&
1465 (INRANGE(STB0899_QPSK_12, modcod, STB0899_QPSK_35)) &&
1468 /* Equalizer Disable update */
1469 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
1470 STB0899_SETFIELD_VAL(EQ_DISABLE_UPDATE, reg, 1);
1471 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
1474 /* slow down the Equalizer once locked */
1475 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
1476 STB0899_SETFIELD_VAL(EQ_SHIFT, reg, 0x02);
1477 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
1479 /* Store signal parameters */
1480 offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
1482 offsetfreq = offsetfreq / ((1 << 30) / 1000);
1483 offsetfreq *= (internal->master_clk / 1000000);
1484 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
1485 if (STB0899_GETFIELD(SPECTRUM_INVERT, reg))
1488 internal->freq = internal->freq - offsetfreq;
1489 internal->srate = stb0899_dvbs2_get_srate(state);
1491 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1492 internal->modcod = STB0899_GETFIELD(UWP_DECODE_MOD, reg) >> 2;
1493 internal->pilots = STB0899_GETFIELD(UWP_DECODE_MOD, reg) & 0x01;
1494 internal->frame_length = (STB0899_GETFIELD(UWP_DECODE_MOD, reg) >> 1) & 0x01;
1496 /* Set IF AGC to tracking */
1497 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
1498 STB0899_SETFIELD_VAL(IF_LOOP_GAIN, reg, 3);
1500 /* if QPSK 1/2,QPSK 3/5 or QPSK 2/3 set IF AGC reference to 16 otherwise 32*/
1501 if (INRANGE(STB0899_QPSK_12, internal->modcod, STB0899_QPSK_23))
1502 STB0899_SETFIELD_VAL(IF_AGC_REF, reg, 16);
1504 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
1506 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL2);
1507 STB0899_SETFIELD_VAL(IF_AGC_DUMP_PER, reg, 7);
1508 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL2, STB0899_OFF0_IF_AGC_CNTRL2, reg);
1511 /* Release Stream Merger Reset */
1512 reg = stb0899_read_reg(state, STB0899_TSTRES);
1513 STB0899_SETFIELD_VAL(FRESRS, reg, 0);
1514 stb0899_write_reg(state, STB0899_TSTRES, reg);
1516 return internal->status;