stb0899_algo.c 49 KB

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