stb0899_algo.c 49 KB

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