stb0899_algo.c 49 KB

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