--- /media/storage/Download/dvb/mantis-303b1d29d735/linux/drivers/media/dvb/frontends/stb0899_algo.c 2008-09-21 18:41:00.000000000 +0300 +++ stb0899_algo.c 2008-10-09 19:39:57.000000000 +0200 @@ -203,8 +203,6 @@ static enum stb0899_status stb0899_search_tmg(struct stb0899_state *state) { struct stb0899_internal *internal = &state->internal; - struct stb0899_params *params = &state->params; - short int derot_step, derot_freq = 0, derot_limit, next_loop = 3; int index = 0; u8 cfr[2]; @@ -213,12 +211,18 @@ /* timing loop computation & symbol rate optimisation */ derot_limit = (internal->sub_range / 2L) / internal->mclk; - derot_step = (params->srate / 2L) / internal->mclk; + derot_step = internal->derot_step * 4; + + dprintk(state->verbose, FE_DEBUG, 1, "limit = %d, step = %d, mclk = %d", + derot_limit, derot_step, internal->mclk); while ((stb0899_check_tmg(state) != TIMINGOK) && next_loop) { - index++; + derot_freq += index * internal->direction * derot_step; /* next derot zig zag position */ + dprintk(state->verbose, FE_DEBUG, 1, "index = %d, derot_freq = %d, limit = %d, direction = %d, step = %d", + index, derot_freq, derot_limit, internal->direction, derot_step); + if (ABS(derot_freq) > derot_limit) next_loop--; @@ -227,6 +231,8 @@ STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(state->config->inversion * derot_freq)); stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */ } + + index++; internal->direction = -internal->direction; /* Change zigzag direction */ } @@ -275,14 +281,21 @@ { struct stb0899_internal *internal = &state->internal; - short int derot_freq = 0, last_derot_freq = 0, derot_limit, next_loop = 3; + short int derot_freq = 0, last_derot_freq = 0, derot_limit, derot_step, next_loop = 3; int index = 0; + int base_freq; u8 cfr[2]; u8 reg; internal->status = NOCARRIER; derot_limit = (internal->sub_range / 2L) / internal->mclk; derot_freq = internal->derot_freq; + derot_step = internal->derot_step * 2; + last_derot_freq = internal->derot_freq; + base_freq = internal->derot_freq; + + dprintk(state->verbose, FE_DEBUG, 1, "freq = %d, limit = %d, step = %d, mclk = %d", + derot_freq, derot_limit, derot_step, internal->mclk); reg = stb0899_read_reg(state, STB0899_CFD); STB0899_SETFIELD_VAL(CFD_ON, reg, 1); @@ -291,11 +304,16 @@ do { dprintk(state->verbose, FE_DEBUG, 1, "Derot Freq=%d, mclk=%d", derot_freq, internal->mclk); if (stb0899_check_carrier(state) == NOCARRIER) { - index++; + last_derot_freq = derot_freq; - derot_freq += index * internal->direction * internal->derot_step; /* next zig zag derotator position */ + derot_freq += index * internal->direction * derot_step; /* next zig zag derotator position */ - if(ABS(derot_freq) > derot_limit) + + dprintk(state->verbose, FE_DEBUG, 1, "index = %d, derot_freq = %d, limit = %d, step = %d", + index, derot_freq, derot_limit, derot_step); + + // Alex: should limit based on initial base freq + if(derot_freq > base_freq + derot_limit || derot_freq < base_freq - derot_limit) next_loop--; if (next_loop) { @@ -307,9 +325,10 @@ STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(state->config->inversion * derot_freq)); stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */ } - } + index++; internal->direction = -internal->direction; /* Change zigzag direction */ + } } while ((internal->status != CARRIEROK) && next_loop); if (internal->status == CARRIEROK) { @@ -335,16 +354,22 @@ int lock = 0, index = 0, dataTime = 500, loop; u8 reg; + // Alex: added sleep 5 mSec + msleep(5); + internal->status = NODATA; /* RESET FEC */ reg = stb0899_read_reg(state, STB0899_TSTRES); STB0899_SETFIELD_VAL(FRESACS, reg, 1); stb0899_write_reg(state, STB0899_TSTRES, reg); - msleep(1); + // Alex: changed from 1 to 5 mSec + msleep(5); reg = stb0899_read_reg(state, STB0899_TSTRES); STB0899_SETFIELD_VAL(FRESACS, reg, 0); stb0899_write_reg(state, STB0899_TSTRES, reg); + // Alex: added 5 mSec + msleep(5); if (params->srate <= 2000000) dataTime = 2000; @@ -357,6 +382,9 @@ stb0899_write_reg(state, STB0899_DSTATUS2, 0x00); /* force search loop */ while (1) { + // Alex: added 1 mSec + msleep(1); + /* WARNING! VIT LOCKED has to be tested before VIT_END_LOOOP */ reg = stb0899_read_reg(state, STB0899_VSTATUS); lock = STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg); @@ -384,20 +412,29 @@ short int derot_freq, derot_step, derot_limit, next_loop = 3; u8 cfr[2]; u8 reg; - int index = 1; + int index = 0; + int base_freq; struct stb0899_internal *internal = &state->internal; - struct stb0899_params *params = &state->params; - derot_step = (params->srate / 4L) / internal->mclk; + derot_step = internal->derot_step; derot_limit = (internal->sub_range / 2L) / internal->mclk; derot_freq = internal->derot_freq; + base_freq = internal->derot_freq; + + dprintk(state->verbose, FE_DEBUG, 1, "limit = %d, step = %d, mclk = %d", + derot_limit, derot_step, internal->mclk); do { if ((internal->status != CARRIEROK) || (stb0899_check_data(state) != DATAOK)) { derot_freq += index * internal->direction * derot_step; /* next zig zag derotator position */ - if (ABS(derot_freq) > derot_limit) + + dprintk(state->verbose, FE_DEBUG, 1, "index = %d, derot_freq = %d, limit = %d, direction = %d, step = %d", + index, derot_freq, derot_limit, internal->direction, derot_step); + + // Alex: should limit based on initial base freq + if(derot_freq > base_freq + derot_limit || derot_freq < base_freq - derot_limit) next_loop--; if (next_loop) { @@ -411,9 +448,10 @@ stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */ stb0899_check_carrier(state); - index++; } } + + index++; internal->direction = -internal->direction; /* change zig zag direction */ } while ((internal->status != DATAOK) && next_loop); @@ -551,6 +589,10 @@ /* Initial calculations */ internal->derot_step = internal->derot_percent * (params->srate / 1000L) / internal->mclk; /* DerotStep/1000 * Fsymbol */ + + dprintk(state->verbose, FE_DEBUG, 1, "Derot step=%d", + internal->derot_step); + internal->t_derot = stb0899_calc_derot_time(params->srate); internal->t_data = 500; @@ -748,6 +790,17 @@ reg = STB0899_READ_S2REG(STB0899_S2DEMOD, SOF_SRCH_TO); STB0899_SETFIELD_VAL(SOF_SEARCH_TIMEOUT, reg, config->sof_search_timeout); stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_SOF_SRCH_TO, STB0899_OFF0_SOF_SRCH_TO, reg); + + dprintk(state->verbose, FE_DEBUG, 1, "esno_ave = %d", config->esno_ave); + dprintk(state->verbose, FE_DEBUG, 1, "esno_quant = %d", config->esno_quant); + dprintk(state->verbose, FE_DEBUG, 1, "uwp_threshold_sof = %d", config->uwp_threshold_sof); + dprintk(state->verbose, FE_DEBUG, 1, "av_frame_coarse = %d", internal->av_frame_coarse); + dprintk(state->verbose, FE_DEBUG, 1, "av_frame_fine = %d", internal->av_frame_fine); + dprintk(state->verbose, FE_DEBUG, 1, "miss_threshold = %d", config->miss_threshold); + dprintk(state->verbose, FE_DEBUG, 1, "uwp_threshold_acq = %d", config->uwp_threshold_acq); + dprintk(state->verbose, FE_DEBUG, 1, "uwp_threshold_track = %d", config->uwp_threshold_track); + dprintk(state->verbose, FE_DEBUG, 1, "sof_search_timeout = %d", config->sof_search_timeout); + } /* @@ -844,6 +897,8 @@ dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio; dec_rate = Log2Int(dec_ratio); + dprintk(state->verbose, FE_DEBUG, 1, "dec_ratio %d, dec_rate %d", dec_ratio, dec_rate); + win_sel = 0; if (dec_rate >= 5) win_sel = dec_rate - 4; @@ -857,6 +912,8 @@ else band_lim = 0; /* band limit signal going into btr block*/ + dprintk(state->verbose, FE_DEBUG, 1, "decim %d, f_sym %d, master_clk %d, srate %d, band_lim %d", decim, f_sym, internal->master_clk, internal->srate, band_lim); + decim_cntrl = ((win_sel << 3) & 0x18) + ((band_lim << 5) & 0x20) + (dec_rate & 0x7); stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DECIM_CNTRL, STB0899_OFF0_DECIM_CNTRL, decim_cntrl); @@ -867,6 +924,8 @@ else anti_alias = 2; + dprintk(state->verbose, FE_DEBUG, 1, "anti_alias %d", anti_alias); + stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ANTI_ALIAS_SEL, STB0899_OFF0_ANTI_ALIAS_SEL, anti_alias); btr_nom_freq = stb0899_dvbs2_calc_srate(state); stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_NOM_FREQ, STB0899_OFF0_BTR_NOM_FREQ, btr_nom_freq); @@ -879,6 +938,9 @@ /* scale UWP+CSM frequency to sample rate*/ freq_adj = internal->srate / (internal->master_clk / 4096); stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_FREQ_ADJ_SCALE, STB0899_OFF0_FREQ_ADJ_SCALE, freq_adj); + + dprintk(state->verbose, FE_DEBUG, 1, "btr_nom_freq %d, correction %d, freq_adj %d", btr_nom_freq, correction, freq_adj); + } /* @@ -951,8 +1013,13 @@ s32 crl_nom_freq; u32 reg; + dprintk(state->verbose, FE_DEBUG, 1, "carr_freq = %d, master_clk = %d", carr_freq, master_clk); + crl_nom_freq = (1 << config->crl_nco_bits) / master_clk; crl_nom_freq *= carr_freq; + + dprintk(state->verbose, FE_DEBUG, 1, "crl_nom_freq = %d", crl_nom_freq); + reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ); STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, crl_nom_freq); stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg); @@ -985,7 +1052,12 @@ else step_size = (1 << 17) / 4; - range = internal->srch_range / 1000000; + // Alex: make smaller steps + step_size = 100; + + dprintk(state->verbose, FE_DEBUG, 1, "step size %d", step_size); + + range = 10;//Alex: return to: internal->srch_range / 1000000; steps = (10 * range * (1 << 17)) / (step_size * (internal->srate / 1000000)); steps = (steps + 6) / 10; steps = (steps == 0) ? 1 : steps; @@ -996,6 +1068,8 @@ else stb0899_dvbs2_set_carr_freq(state, internal->center_freq, (internal->master_clk) / 1000000); + dprintk(state->verbose, FE_DEBUG, 1, "range %d, steps %d", range, steps); + /*Set Carrier Search params (zigzag, num steps and freq step size*/ reg = STB0899_READ_S2REG(STB0899_S2DEMOD, ACQ_CNTRL2); STB0899_SETFIELD_VAL(ZIGZAG, reg, 1); @@ -1084,6 +1158,8 @@ int time = -10, lock = 0, uwp, csm; u32 reg; + dprintk(state->verbose, FE_DEBUG, 1, "timeout = %d mSec", timeout); + do { reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS); dprintk(state->verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg); @@ -1102,7 +1177,7 @@ } while ((!lock) && (time <= timeout)); if (lock) { - dprintk(state->verbose, FE_DEBUG, 1, "----------------> DVB-S2 LOCK !"); + dprintk(state->verbose, FE_DEBUG, 1, "----------------> DVB-S2 LOCK ! after %d mSec", time); return DVBS2_DEMOD_LOCK; } else { return DVBS2_DEMOD_NOLOCK; @@ -1136,6 +1211,8 @@ { int time = 0, Locked; + dprintk(state->verbose, FE_DEBUG, 1, "timeout = %d", timeout); + do { Locked = stb0899_dvbs2_get_data_lock(state, 1); time++; @@ -1342,6 +1418,11 @@ FecLockTime = 20; /* 20 ms max time to lock FEC, 20Mbs< SYMB <= 25Mbs */ } + searchTime = 5000; + FecLockTime = 500; + + dprintk(state->verbose, FE_DEBUG, 1, "srate = %d, searchTime = %d, FecLockTime = %d", internal->srate, searchTime, FecLockTime); + /* Maintain Stream Merger in reset during acquisition */ reg = stb0899_read_reg(state, STB0899_TSTRES); STB0899_SETFIELD_VAL(FRESRS, reg, 1); @@ -1401,12 +1482,18 @@ /* Read the frequency offset*/ offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ); + dprintk(state->verbose, FE_DEBUG, 1, "1: i = %d, offsetfreq = %d", i, offsetfreq); + /* Set the Nominal frequency to the found frequency offset for the next reacquire*/ reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ); STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, offsetfreq); stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg); stb0899_dvbs2_reacquire(state); + + msleep(10); + internal->status = stb0899_dvbs2_get_fec_status(state, searchTime); + i++; } } @@ -1412,12 +1499,19 @@ } if (internal->status != DVBS2_FEC_LOCK) { + dprintk(state->verbose, FE_DEBUG, 1, "still no lock, inversion = %d", internal->inversion); + if (internal->inversion == IQ_SWAP_AUTO) { + dprintk(state->verbose, FE_DEBUG, 1, "inversion = IQ_SWAP_AUTO"); + reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2); iqSpectrum = STB0899_GETFIELD(SPECTRUM_INVERT, reg); /* IQ Spectrum Inversion */ STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, !iqSpectrum); stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DMD_CNTRL2, STB0899_OFF0_DMD_CNTRL2, reg); + + msleep(10); + /* start acquistion process */ stb0899_dvbs2_reacquire(state); @@ -1432,13 +1527,19 @@ /* Read the frequency offset*/ offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ); + dprintk(state->verbose, FE_DEBUG, 1, "2: i = %d, offsetfreq = %d", i, offsetfreq); + /* Set the Nominal frequency to the found frequency offset for the next reacquire*/ reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ); STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, offsetfreq); stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg); + msleep(10); + stb0899_dvbs2_reacquire(state); + internal->status = stb0899_dvbs2_get_fec_status(state, searchTime); + i++; } } @@ -1464,13 +1565,21 @@ i = 0; while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) { + + dprintk(state->verbose, FE_DEBUG, 1, "3: i = %d", i); + csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1); STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, 1); stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1); + + msleep(10); + csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1); STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, 0); stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1); + msleep(10); + internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime); i++; } @@ -1480,6 +1589,8 @@ (INRANGE(STB0899_QPSK_12, modcod, STB0899_QPSK_35)) && (pilots == 1)) { + dprintk(state->verbose, FE_DEBUG, 1, "Equalizer disable update"); + /* Equalizer Disable update */ reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL); STB0899_SETFIELD_VAL(EQ_DISABLE_UPDATE, reg, 1); @@ -1491,18 +1602,26 @@ STB0899_SETFIELD_VAL(EQ_SHIFT, reg, 0x02); stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg); + msleep(10); + /* Store signal parameters */ offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ); + dprintk(state->verbose, FE_DEBUG, 1, "offsetfreq from reg = %d", offsetfreq); + offsetfreq = offsetfreq / ((1 << 30) / 1000); offsetfreq *= (internal->master_clk / 1000000); reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2); if (STB0899_GETFIELD(SPECTRUM_INVERT, reg)) offsetfreq *= -1; + dprintk(state->verbose, FE_DEBUG, 1, "offsetfreq after calc = %d", offsetfreq); + internal->freq = internal->freq - offsetfreq; internal->srate = stb0899_dvbs2_get_srate(state); + dprintk(state->verbose, FE_DEBUG, 1, "freq = %d, srate = %d", internal->freq, internal->srate); + reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2); internal->modcod = STB0899_GETFIELD(UWP_DECODE_MOD, reg) >> 2; internal->pilots = STB0899_GETFIELD(UWP_DECODE_MOD, reg) & 0x01;