stb0899_algo.c 49 KB

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