stb0899_algo.c 49 KB

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