stb0899_algo.c 50 KB

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