stb0899_algo.c 50 KB

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