stv090x.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328
  1. /*
  2. STV0900/0903 Multistandard Broadcast Frontend driver
  3. Copyright (C) Manu Abraham <abraham.manu@gmail.com>
  4. Copyright (C) ST Microelectronics
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/string.h>
  21. #include <linux/mutex.h>
  22. #include <linux/dvb/frontend.h>
  23. #include "dvb_frontend.h"
  24. #include "stv6110x.h" /* for demodulator internal modes */
  25. #include "stv090x_reg.h"
  26. #include "stv090x.h"
  27. #include "stv090x_priv.h"
  28. static unsigned int verbose;
  29. module_param(verbose, int, 0644);
  30. struct mutex demod_lock;
  31. /* DVBS1 and DSS C/N Lookup table */
  32. static const struct stv090x_tab stv090x_s1cn_tab[] = {
  33. { 0, 8917 }, /* 0.0dB */
  34. { 5, 8801 }, /* 0.5dB */
  35. { 10, 8667 }, /* 1.0dB */
  36. { 15, 8522 }, /* 1.5dB */
  37. { 20, 8355 }, /* 2.0dB */
  38. { 25, 8175 }, /* 2.5dB */
  39. { 30, 7979 }, /* 3.0dB */
  40. { 35, 7763 }, /* 3.5dB */
  41. { 40, 7530 }, /* 4.0dB */
  42. { 45, 7282 }, /* 4.5dB */
  43. { 50, 7026 }, /* 5.0dB */
  44. { 55, 6781 }, /* 5.5dB */
  45. { 60, 6514 }, /* 6.0dB */
  46. { 65, 6241 }, /* 6.5dB */
  47. { 70, 5965 }, /* 7.0dB */
  48. { 75, 5690 }, /* 7.5dB */
  49. { 80, 5424 }, /* 8.0dB */
  50. { 85, 5161 }, /* 8.5dB */
  51. { 90, 4902 }, /* 9.0dB */
  52. { 95, 4654 }, /* 9.5dB */
  53. { 100, 4417 }, /* 10.0dB */
  54. { 105, 4186 }, /* 10.5dB */
  55. { 110, 3968 }, /* 11.0dB */
  56. { 115, 3757 }, /* 11.5dB */
  57. { 120, 3558 }, /* 12.0dB */
  58. { 125, 3366 }, /* 12.5dB */
  59. { 130, 3185 }, /* 13.0dB */
  60. { 135, 3012 }, /* 13.5dB */
  61. { 140, 2850 }, /* 14.0dB */
  62. { 145, 2698 }, /* 14.5dB */
  63. { 150, 2550 }, /* 15.0dB */
  64. { 160, 2283 }, /* 16.0dB */
  65. { 170, 2042 }, /* 17.0dB */
  66. { 180, 1827 }, /* 18.0dB */
  67. { 190, 1636 }, /* 19.0dB */
  68. { 200, 1466 }, /* 20.0dB */
  69. { 210, 1315 }, /* 21.0dB */
  70. { 220, 1181 }, /* 22.0dB */
  71. { 230, 1064 }, /* 23.0dB */
  72. { 240, 960 }, /* 24.0dB */
  73. { 250, 869 }, /* 25.0dB */
  74. { 260, 792 }, /* 26.0dB */
  75. { 270, 724 }, /* 27.0dB */
  76. { 280, 665 }, /* 28.0dB */
  77. { 290, 616 }, /* 29.0dB */
  78. { 300, 573 }, /* 30.0dB */
  79. { 310, 537 }, /* 31.0dB */
  80. { 320, 507 }, /* 32.0dB */
  81. { 330, 483 }, /* 33.0dB */
  82. { 400, 398 }, /* 40.0dB */
  83. { 450, 381 }, /* 45.0dB */
  84. { 500, 377 } /* 50.0dB */
  85. };
  86. /* DVBS2 C/N Lookup table */
  87. static const struct stv090x_tab stv090x_s2cn_tab[] = {
  88. { -30, 13348 }, /* -3.0dB */
  89. { -20, 12640 }, /* -2d.0B */
  90. { -10, 11883 }, /* -1.0dB */
  91. { 0, 11101 }, /* -0.0dB */
  92. { 5, 10718 }, /* 0.5dB */
  93. { 10, 10339 }, /* 1.0dB */
  94. { 15, 9947 }, /* 1.5dB */
  95. { 20, 9552 }, /* 2.0dB */
  96. { 25, 9183 }, /* 2.5dB */
  97. { 30, 8799 }, /* 3.0dB */
  98. { 35, 8422 }, /* 3.5dB */
  99. { 40, 8062 }, /* 4.0dB */
  100. { 45, 7707 }, /* 4.5dB */
  101. { 50, 7353 }, /* 5.0dB */
  102. { 55, 7025 }, /* 5.5dB */
  103. { 60, 6684 }, /* 6.0dB */
  104. { 65, 6331 }, /* 6.5dB */
  105. { 70, 6036 }, /* 7.0dB */
  106. { 75, 5727 }, /* 7.5dB */
  107. { 80, 5437 }, /* 8.0dB */
  108. { 85, 5164 }, /* 8.5dB */
  109. { 90, 4902 }, /* 9.0dB */
  110. { 95, 4653 }, /* 9.5dB */
  111. { 100, 4408 }, /* 10.0dB */
  112. { 105, 4187 }, /* 10.5dB */
  113. { 110, 3961 }, /* 11.0dB */
  114. { 115, 3751 }, /* 11.5dB */
  115. { 120, 3558 }, /* 12.0dB */
  116. { 125, 3368 }, /* 12.5dB */
  117. { 130, 3191 }, /* 13.0dB */
  118. { 135, 3017 }, /* 13.5dB */
  119. { 140, 2862 }, /* 14.0dB */
  120. { 145, 2710 }, /* 14.5dB */
  121. { 150, 2565 }, /* 15.0dB */
  122. { 160, 2300 }, /* 16.0dB */
  123. { 170, 2058 }, /* 17.0dB */
  124. { 180, 1849 }, /* 18.0dB */
  125. { 190, 1663 }, /* 19.0dB */
  126. { 200, 1495 }, /* 20.0dB */
  127. { 210, 1349 }, /* 21.0dB */
  128. { 220, 1222 }, /* 22.0dB */
  129. { 230, 1110 }, /* 23.0dB */
  130. { 240, 1011 }, /* 24.0dB */
  131. { 250, 925 }, /* 25.0dB */
  132. { 260, 853 }, /* 26.0dB */
  133. { 270, 789 }, /* 27.0dB */
  134. { 280, 734 }, /* 28.0dB */
  135. { 290, 690 }, /* 29.0dB */
  136. { 300, 650 }, /* 30.0dB */
  137. { 310, 619 }, /* 31.0dB */
  138. { 320, 593 }, /* 32.0dB */
  139. { 330, 571 }, /* 33.0dB */
  140. { 400, 498 }, /* 40.0dB */
  141. { 450, 484 }, /* 45.0dB */
  142. { 500, 481 } /* 50.0dB */
  143. };
  144. /* RF level C/N lookup table */
  145. static const struct stv090x_tab stv090x_rf_tab[] = {
  146. { -5, 0xcaa1 }, /* -5dBm */
  147. { -10, 0xc229 }, /* -10dBm */
  148. { -15, 0xbb08 }, /* -15dBm */
  149. { -20, 0xb4bc }, /* -20dBm */
  150. { -25, 0xad5a }, /* -25dBm */
  151. { -30, 0xa298 }, /* -30dBm */
  152. { -35, 0x98a8 }, /* -35dBm */
  153. { -40, 0x8389 }, /* -40dBm */
  154. { -45, 0x59be }, /* -45dBm */
  155. { -50, 0x3a14 }, /* -50dBm */
  156. { -55, 0x2d11 }, /* -55dBm */
  157. { -60, 0x210d }, /* -60dBm */
  158. { -65, 0xa14f }, /* -65dBm */
  159. { -70, 0x07aa } /* -70dBm */
  160. };
  161. static struct stv090x_reg stv0900_initval[] = {
  162. { STV090x_OUTCFG, 0x00 },
  163. { STV090x_MODECFG, 0xff },
  164. { STV090x_AGCRF1CFG, 0x11 },
  165. { STV090x_AGCRF2CFG, 0x13 },
  166. { STV090x_TSGENERAL1X, 0x14 },
  167. { STV090x_TSTTNR2, 0x21 },
  168. { STV090x_TSTTNR4, 0x21 },
  169. { STV090x_P2_DISTXCTL, 0x22 },
  170. { STV090x_P2_F22TX, 0xc0 },
  171. { STV090x_P2_F22RX, 0xc0 },
  172. { STV090x_P2_DISRXCTL, 0x00 },
  173. { STV090x_P2_DMDCFGMD, 0xF9 },
  174. { STV090x_P2_DEMOD, 0x08 },
  175. { STV090x_P2_DMDCFG3, 0xc4 },
  176. { STV090x_P2_CARFREQ, 0xed },
  177. { STV090x_P2_LDT, 0xd0 },
  178. { STV090x_P2_LDT2, 0xb8 },
  179. { STV090x_P2_TMGCFG, 0xd2 },
  180. { STV090x_P2_TMGTHRISE, 0x20 },
  181. { STV090x_P1_TMGCFG, 0xd2 },
  182. { STV090x_P2_TMGTHFALL, 0x00 },
  183. { STV090x_P2_FECSPY, 0x88 },
  184. { STV090x_P2_FSPYDATA, 0x3a },
  185. { STV090x_P2_FBERCPT4, 0x00 },
  186. { STV090x_P2_FSPYBER, 0x10 },
  187. { STV090x_P2_ERRCTRL1, 0x35 },
  188. { STV090x_P2_ERRCTRL2, 0xc1 },
  189. { STV090x_P2_CFRICFG, 0xf8 },
  190. { STV090x_P2_NOSCFG, 0x1c },
  191. { STV090x_P2_DMDTOM, 0x20 },
  192. { STV090x_P2_CORRELMANT, 0x70 },
  193. { STV090x_P2_CORRELABS, 0x88 },
  194. { STV090x_P2_AGC2O, 0x5b },
  195. { STV090x_P2_AGC2REF, 0x38 },
  196. { STV090x_P2_CARCFG, 0xe4 },
  197. { STV090x_P2_ACLC, 0x1A },
  198. { STV090x_P2_BCLC, 0x09 },
  199. { STV090x_P2_CARHDR, 0x08 },
  200. { STV090x_P2_KREFTMG, 0xc1 },
  201. { STV090x_P2_SFRUPRATIO, 0xf0 },
  202. { STV090x_P2_SFRLOWRATIO, 0x70 },
  203. { STV090x_P2_SFRSTEP, 0x58 },
  204. { STV090x_P2_TMGCFG2, 0x01 },
  205. { STV090x_P2_CAR2CFG, 0x26 },
  206. { STV090x_P2_BCLC2S2Q, 0x86 },
  207. { STV090x_P2_BCLC2S28, 0x86 },
  208. { STV090x_P2_SMAPCOEF7, 0x77 },
  209. { STV090x_P2_SMAPCOEF6, 0x85 },
  210. { STV090x_P2_SMAPCOEF5, 0x77 },
  211. { STV090x_P2_TSCFGL, 0x20 },
  212. { STV090x_P2_DMDCFG2, 0x3b },
  213. { STV090x_P2_MODCODLST0, 0xff },
  214. { STV090x_P2_MODCODLST1, 0xff },
  215. { STV090x_P2_MODCODLST2, 0xff },
  216. { STV090x_P2_MODCODLST3, 0xff },
  217. { STV090x_P2_MODCODLST4, 0xff },
  218. { STV090x_P2_MODCODLST5, 0xff },
  219. { STV090x_P2_MODCODLST6, 0xff },
  220. { STV090x_P2_MODCODLST7, 0xcc },
  221. { STV090x_P2_MODCODLST8, 0xcc },
  222. { STV090x_P2_MODCODLST9, 0xcc },
  223. { STV090x_P2_MODCODLSTA, 0xcc },
  224. { STV090x_P2_MODCODLSTB, 0xcc },
  225. { STV090x_P2_MODCODLSTC, 0xcc },
  226. { STV090x_P2_MODCODLSTD, 0xcc },
  227. { STV090x_P2_MODCODLSTE, 0xcc },
  228. { STV090x_P2_MODCODLSTF, 0xcf },
  229. { STV090x_P1_DISTXCTL, 0x22 },
  230. { STV090x_P1_F22TX, 0xc0 },
  231. { STV090x_P1_F22RX, 0xc0 },
  232. { STV090x_P1_DISRXCTL, 0x00 },
  233. { STV090x_P1_DMDCFGMD, 0xf9 },
  234. { STV090x_P1_DEMOD, 0x08 },
  235. { STV090x_P1_DMDCFG3, 0xc4 },
  236. { STV090x_P1_DMDTOM, 0x20 },
  237. { STV090x_P1_CARFREQ, 0xed },
  238. { STV090x_P1_LDT, 0xd0 },
  239. { STV090x_P1_LDT2, 0xb8 },
  240. { STV090x_P1_TMGCFG, 0xd2 },
  241. { STV090x_P1_TMGTHRISE, 0x20 },
  242. { STV090x_P1_TMGTHFALL, 0x00 },
  243. { STV090x_P1_SFRUPRATIO, 0xf0 },
  244. { STV090x_P1_SFRLOWRATIO, 0x70 },
  245. { STV090x_P1_TSCFGL, 0x20 },
  246. { STV090x_P1_FECSPY, 0x88 },
  247. { STV090x_P1_FSPYDATA, 0x3a },
  248. { STV090x_P1_FBERCPT4, 0x00 },
  249. { STV090x_P1_FSPYBER, 0x10 },
  250. { STV090x_P1_ERRCTRL1, 0x35 },
  251. { STV090x_P1_ERRCTRL2, 0xc1 },
  252. { STV090x_P1_CFRICFG, 0xf8 },
  253. { STV090x_P1_NOSCFG, 0x1c },
  254. { STV090x_P1_CORRELMANT, 0x70 },
  255. { STV090x_P1_CORRELABS, 0x88 },
  256. { STV090x_P1_AGC2O, 0x5b },
  257. { STV090x_P1_AGC2REF, 0x38 },
  258. { STV090x_P1_CARCFG, 0xe4 },
  259. { STV090x_P1_ACLC, 0x1A },
  260. { STV090x_P1_BCLC, 0x09 },
  261. { STV090x_P1_CARHDR, 0x08 },
  262. { STV090x_P1_KREFTMG, 0xc1 },
  263. { STV090x_P1_SFRSTEP, 0x58 },
  264. { STV090x_P1_TMGCFG2, 0x01 },
  265. { STV090x_P1_CAR2CFG, 0x26 },
  266. { STV090x_P1_BCLC2S2Q, 0x86 },
  267. { STV090x_P1_BCLC2S28, 0x86 },
  268. { STV090x_P1_SMAPCOEF7, 0x77 },
  269. { STV090x_P1_SMAPCOEF6, 0x85 },
  270. { STV090x_P1_SMAPCOEF5, 0x77 },
  271. { STV090x_P1_DMDCFG2, 0x3b },
  272. { STV090x_P1_MODCODLST0, 0xff },
  273. { STV090x_P1_MODCODLST1, 0xff },
  274. { STV090x_P1_MODCODLST2, 0xff },
  275. { STV090x_P1_MODCODLST3, 0xff },
  276. { STV090x_P1_MODCODLST4, 0xff },
  277. { STV090x_P1_MODCODLST5, 0xff },
  278. { STV090x_P1_MODCODLST6, 0xff },
  279. { STV090x_P1_MODCODLST7, 0xcc },
  280. { STV090x_P1_MODCODLST8, 0xcc },
  281. { STV090x_P1_MODCODLST9, 0xcc },
  282. { STV090x_P1_MODCODLSTA, 0xcc },
  283. { STV090x_P1_MODCODLSTB, 0xcc },
  284. { STV090x_P1_MODCODLSTC, 0xcc },
  285. { STV090x_P1_MODCODLSTD, 0xcc },
  286. { STV090x_P1_MODCODLSTE, 0xcc },
  287. { STV090x_P1_MODCODLSTF, 0xcf },
  288. { STV090x_GENCFG, 0x1d },
  289. { STV090x_NBITER_NF4, 0x37 },
  290. { STV090x_NBITER_NF5, 0x29 },
  291. { STV090x_NBITER_NF6, 0x37 },
  292. { STV090x_NBITER_NF7, 0x33 },
  293. { STV090x_NBITER_NF8, 0x31 },
  294. { STV090x_NBITER_NF9, 0x2f },
  295. { STV090x_NBITER_NF10, 0x39 },
  296. { STV090x_NBITER_NF11, 0x3a },
  297. { STV090x_NBITER_NF12, 0x29 },
  298. { STV090x_NBITER_NF13, 0x37 },
  299. { STV090x_NBITER_NF14, 0x33 },
  300. { STV090x_NBITER_NF15, 0x2f },
  301. { STV090x_NBITER_NF16, 0x39 },
  302. { STV090x_NBITER_NF17, 0x3a },
  303. { STV090x_NBITERNOERR, 0x04 },
  304. { STV090x_GAINLLR_NF4, 0x0C },
  305. { STV090x_GAINLLR_NF5, 0x0F },
  306. { STV090x_GAINLLR_NF6, 0x11 },
  307. { STV090x_GAINLLR_NF7, 0x14 },
  308. { STV090x_GAINLLR_NF8, 0x17 },
  309. { STV090x_GAINLLR_NF9, 0x19 },
  310. { STV090x_GAINLLR_NF10, 0x20 },
  311. { STV090x_GAINLLR_NF11, 0x21 },
  312. { STV090x_GAINLLR_NF12, 0x0D },
  313. { STV090x_GAINLLR_NF13, 0x0F },
  314. { STV090x_GAINLLR_NF14, 0x13 },
  315. { STV090x_GAINLLR_NF15, 0x1A },
  316. { STV090x_GAINLLR_NF16, 0x1F },
  317. { STV090x_GAINLLR_NF17, 0x21 },
  318. { STV090x_RCCFGH, 0x20 },
  319. { STV090x_P1_FECM, 0x01 }, /* disable DSS modes */
  320. { STV090x_P2_FECM, 0x01 }, /* disable DSS modes */
  321. { STV090x_P1_PRVIT, 0x2F }, /* disable PR 6/7 */
  322. { STV090x_P2_PRVIT, 0x2F }, /* disable PR 6/7 */
  323. };
  324. static struct stv090x_reg stv0903_initval[] = {
  325. { STV090x_OUTCFG, 0x00 },
  326. { STV090x_AGCRF1CFG, 0x11 },
  327. { STV090x_STOPCLK1, 0x48 },
  328. { STV090x_STOPCLK2, 0x14 },
  329. { STV090x_TSTTNR1, 0x27 },
  330. { STV090x_TSTTNR2, 0x21 },
  331. { STV090x_P1_DISTXCTL, 0x22 },
  332. { STV090x_P1_F22TX, 0xc0 },
  333. { STV090x_P1_F22RX, 0xc0 },
  334. { STV090x_P1_DISRXCTL, 0x00 },
  335. { STV090x_P1_DMDCFGMD, 0xF9 },
  336. { STV090x_P1_DEMOD, 0x08 },
  337. { STV090x_P1_DMDCFG3, 0xc4 },
  338. { STV090x_P1_CARFREQ, 0xed },
  339. { STV090x_P1_TNRCFG2, 0x82 },
  340. { STV090x_P1_LDT, 0xd0 },
  341. { STV090x_P1_LDT2, 0xb8 },
  342. { STV090x_P1_TMGCFG, 0xd2 },
  343. { STV090x_P1_TMGTHRISE, 0x20 },
  344. { STV090x_P1_TMGTHFALL, 0x00 },
  345. { STV090x_P1_SFRUPRATIO, 0xf0 },
  346. { STV090x_P1_SFRLOWRATIO, 0x70 },
  347. { STV090x_P1_TSCFGL, 0x20 },
  348. { STV090x_P1_FECSPY, 0x88 },
  349. { STV090x_P1_FSPYDATA, 0x3a },
  350. { STV090x_P1_FBERCPT4, 0x00 },
  351. { STV090x_P1_FSPYBER, 0x10 },
  352. { STV090x_P1_ERRCTRL1, 0x35 },
  353. { STV090x_P1_ERRCTRL2, 0xc1 },
  354. { STV090x_P1_CFRICFG, 0xf8 },
  355. { STV090x_P1_NOSCFG, 0x1c },
  356. { STV090x_P1_DMDTOM, 0x20 },
  357. { STV090x_P1_CORRELMANT, 0x70 },
  358. { STV090x_P1_CORRELABS, 0x88 },
  359. { STV090x_P1_AGC2O, 0x5b },
  360. { STV090x_P1_AGC2REF, 0x38 },
  361. { STV090x_P1_CARCFG, 0xe4 },
  362. { STV090x_P1_ACLC, 0x1A },
  363. { STV090x_P1_BCLC, 0x09 },
  364. { STV090x_P1_CARHDR, 0x08 },
  365. { STV090x_P1_KREFTMG, 0xc1 },
  366. { STV090x_P1_SFRSTEP, 0x58 },
  367. { STV090x_P1_TMGCFG2, 0x01 },
  368. { STV090x_P1_CAR2CFG, 0x26 },
  369. { STV090x_P1_BCLC2S2Q, 0x86 },
  370. { STV090x_P1_BCLC2S28, 0x86 },
  371. { STV090x_P1_SMAPCOEF7, 0x77 },
  372. { STV090x_P1_SMAPCOEF6, 0x85 },
  373. { STV090x_P1_SMAPCOEF5, 0x77 },
  374. { STV090x_P1_DMDCFG2, 0x3b },
  375. { STV090x_P1_MODCODLST0, 0xff },
  376. { STV090x_P1_MODCODLST1, 0xff },
  377. { STV090x_P1_MODCODLST2, 0xff },
  378. { STV090x_P1_MODCODLST3, 0xff },
  379. { STV090x_P1_MODCODLST4, 0xff },
  380. { STV090x_P1_MODCODLST5, 0xff },
  381. { STV090x_P1_MODCODLST6, 0xff },
  382. { STV090x_P1_MODCODLST7, 0xcc },
  383. { STV090x_P1_MODCODLST8, 0xcc },
  384. { STV090x_P1_MODCODLST9, 0xcc },
  385. { STV090x_P1_MODCODLSTA, 0xcc },
  386. { STV090x_P1_MODCODLSTB, 0xcc },
  387. { STV090x_P1_MODCODLSTC, 0xcc },
  388. { STV090x_P1_MODCODLSTD, 0xcc },
  389. { STV090x_P1_MODCODLSTE, 0xcc },
  390. { STV090x_P1_MODCODLSTF, 0xcf },
  391. { STV090x_GENCFG, 0x1c },
  392. { STV090x_NBITER_NF4, 0x37 },
  393. { STV090x_NBITER_NF5, 0x29 },
  394. { STV090x_NBITER_NF6, 0x37 },
  395. { STV090x_NBITER_NF7, 0x33 },
  396. { STV090x_NBITER_NF8, 0x31 },
  397. { STV090x_NBITER_NF9, 0x2f },
  398. { STV090x_NBITER_NF10, 0x39 },
  399. { STV090x_NBITER_NF11, 0x3a },
  400. { STV090x_NBITER_NF12, 0x29 },
  401. { STV090x_NBITER_NF13, 0x37 },
  402. { STV090x_NBITER_NF14, 0x33 },
  403. { STV090x_NBITER_NF15, 0x2f },
  404. { STV090x_NBITER_NF16, 0x39 },
  405. { STV090x_NBITER_NF17, 0x3a },
  406. { STV090x_NBITERNOERR, 0x04 },
  407. { STV090x_GAINLLR_NF4, 0x0C },
  408. { STV090x_GAINLLR_NF5, 0x0F },
  409. { STV090x_GAINLLR_NF6, 0x11 },
  410. { STV090x_GAINLLR_NF7, 0x14 },
  411. { STV090x_GAINLLR_NF8, 0x17 },
  412. { STV090x_GAINLLR_NF9, 0x19 },
  413. { STV090x_GAINLLR_NF10, 0x20 },
  414. { STV090x_GAINLLR_NF11, 0x21 },
  415. { STV090x_GAINLLR_NF12, 0x0D },
  416. { STV090x_GAINLLR_NF13, 0x0F },
  417. { STV090x_GAINLLR_NF14, 0x13 },
  418. { STV090x_GAINLLR_NF15, 0x1A },
  419. { STV090x_GAINLLR_NF16, 0x1F },
  420. { STV090x_GAINLLR_NF17, 0x21 },
  421. { STV090x_RCCFGH, 0x20 },
  422. { STV090x_P1_FECM, 0x01 }, /*disable the DSS mode */
  423. { STV090x_P1_PRVIT, 0x2f } /*disable puncture rate 6/7*/
  424. };
  425. static struct stv090x_reg stv0900_cut20_val[] = {
  426. { STV090x_P2_DMDCFG3, 0xe8 },
  427. { STV090x_P2_DMDCFG4, 0x10 },
  428. { STV090x_P2_CARFREQ, 0x38 },
  429. { STV090x_P2_CARHDR, 0x20 },
  430. { STV090x_P2_KREFTMG, 0x5a },
  431. { STV090x_P2_SMAPCOEF7, 0x06 },
  432. { STV090x_P2_SMAPCOEF6, 0x00 },
  433. { STV090x_P2_SMAPCOEF5, 0x04 },
  434. { STV090x_P2_NOSCFG, 0x0c },
  435. { STV090x_P1_DMDCFG3, 0xe8 },
  436. { STV090x_P1_DMDCFG4, 0x10 },
  437. { STV090x_P1_CARFREQ, 0x38 },
  438. { STV090x_P1_CARHDR, 0x20 },
  439. { STV090x_P1_KREFTMG, 0x5a },
  440. { STV090x_P1_SMAPCOEF7, 0x06 },
  441. { STV090x_P1_SMAPCOEF6, 0x00 },
  442. { STV090x_P1_SMAPCOEF5, 0x04 },
  443. { STV090x_P1_NOSCFG, 0x0c },
  444. { STV090x_GAINLLR_NF4, 0x21 },
  445. { STV090x_GAINLLR_NF5, 0x21 },
  446. { STV090x_GAINLLR_NF6, 0x20 },
  447. { STV090x_GAINLLR_NF7, 0x1F },
  448. { STV090x_GAINLLR_NF8, 0x1E },
  449. { STV090x_GAINLLR_NF9, 0x1E },
  450. { STV090x_GAINLLR_NF10, 0x1D },
  451. { STV090x_GAINLLR_NF11, 0x1B },
  452. { STV090x_GAINLLR_NF12, 0x20 },
  453. { STV090x_GAINLLR_NF13, 0x20 },
  454. { STV090x_GAINLLR_NF14, 0x20 },
  455. { STV090x_GAINLLR_NF15, 0x20 },
  456. { STV090x_GAINLLR_NF16, 0x20 },
  457. { STV090x_GAINLLR_NF17, 0x21 },
  458. };
  459. static struct stv090x_reg stv0903_cut20_val[] = {
  460. { STV090x_P1_DMDCFG3, 0xe8 },
  461. { STV090x_P1_DMDCFG4, 0x10 },
  462. { STV090x_P1_CARFREQ, 0x38 },
  463. { STV090x_P1_CARHDR, 0x20 },
  464. { STV090x_P1_KREFTMG, 0x5a },
  465. { STV090x_P1_SMAPCOEF7, 0x06 },
  466. { STV090x_P1_SMAPCOEF6, 0x00 },
  467. { STV090x_P1_SMAPCOEF5, 0x04 },
  468. { STV090x_P1_NOSCFG, 0x0c },
  469. { STV090x_GAINLLR_NF4, 0x21 },
  470. { STV090x_GAINLLR_NF5, 0x21 },
  471. { STV090x_GAINLLR_NF6, 0x20 },
  472. { STV090x_GAINLLR_NF7, 0x1F },
  473. { STV090x_GAINLLR_NF8, 0x1E },
  474. { STV090x_GAINLLR_NF9, 0x1E },
  475. { STV090x_GAINLLR_NF10, 0x1D },
  476. { STV090x_GAINLLR_NF11, 0x1B },
  477. { STV090x_GAINLLR_NF12, 0x20 },
  478. { STV090x_GAINLLR_NF13, 0x20 },
  479. { STV090x_GAINLLR_NF14, 0x20 },
  480. { STV090x_GAINLLR_NF15, 0x20 },
  481. { STV090x_GAINLLR_NF16, 0x20 },
  482. { STV090x_GAINLLR_NF17, 0x21 }
  483. };
  484. /* Cut 2.0 Long Frame Tracking CR loop */
  485. static struct stv090x_long_frame_crloop stv090x_s2_crl_cut20[] = {
  486. /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
  487. { STV090x_QPSK_12, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x1e },
  488. { STV090x_QPSK_35, 0x2f, 0x3f, 0x2e, 0x2f, 0x3d, 0x0f, 0x0e, 0x2e, 0x3d, 0x0e },
  489. { STV090x_QPSK_23, 0x2f, 0x3f, 0x2e, 0x2f, 0x0e, 0x0f, 0x0e, 0x1e, 0x3d, 0x3d },
  490. { STV090x_QPSK_34, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
  491. { STV090x_QPSK_45, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
  492. { STV090x_QPSK_56, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
  493. { STV090x_QPSK_89, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
  494. { STV090x_QPSK_910, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
  495. { STV090x_8PSK_35, 0x3c, 0x3e, 0x1c, 0x2e, 0x0c, 0x1e, 0x2b, 0x2d, 0x1b, 0x1d },
  496. { STV090x_8PSK_23, 0x1d, 0x3e, 0x3c, 0x2e, 0x2c, 0x1e, 0x0c, 0x2d, 0x2b, 0x1d },
  497. { STV090x_8PSK_34, 0x0e, 0x3e, 0x3d, 0x2e, 0x0d, 0x1e, 0x2c, 0x2d, 0x0c, 0x1d },
  498. { STV090x_8PSK_56, 0x2e, 0x3e, 0x1e, 0x2e, 0x2d, 0x1e, 0x3c, 0x2d, 0x2c, 0x1d },
  499. { STV090x_8PSK_89, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x0d, 0x2d, 0x3c, 0x1d },
  500. { STV090x_8PSK_910, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x1d, 0x2d, 0x0d, 0x1d }
  501. };
  502. /* Cut 3.0 Long Frame Tracking CR loop */
  503. static struct stv090x_long_frame_crloop stv090x_s2_crl_cut30[] = {
  504. /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
  505. { STV090x_QPSK_12, 0x3c, 0x2c, 0x0c, 0x2c, 0x1b, 0x2c, 0x1b, 0x1c, 0x0b, 0x3b },
  506. { STV090x_QPSK_35, 0x0d, 0x0d, 0x0c, 0x0d, 0x1b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
  507. { STV090x_QPSK_23, 0x1d, 0x0d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
  508. { STV090x_QPSK_34, 0x1d, 0x1d, 0x0c, 0x1d, 0x2b, 0x3c, 0x1b, 0x1c, 0x0b, 0x3b },
  509. { STV090x_QPSK_45, 0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
  510. { STV090x_QPSK_56, 0x2d, 0x1d, 0x1c, 0x1d, 0x2b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
  511. { STV090x_QPSK_89, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
  512. { STV090x_QPSK_910, 0x3d, 0x2d, 0x1c, 0x1d, 0x3b, 0x3c, 0x2b, 0x0c, 0x1b, 0x3b },
  513. { STV090x_8PSK_35, 0x39, 0x29, 0x39, 0x19, 0x19, 0x19, 0x19, 0x19, 0x09, 0x19 },
  514. { STV090x_8PSK_23, 0x2a, 0x39, 0x1a, 0x0a, 0x39, 0x0a, 0x29, 0x39, 0x29, 0x0a },
  515. { STV090x_8PSK_34, 0x2b, 0x3a, 0x1b, 0x1b, 0x3a, 0x1b, 0x1a, 0x0b, 0x1a, 0x3a },
  516. { STV090x_8PSK_56, 0x0c, 0x1b, 0x3b, 0x3b, 0x1b, 0x3b, 0x3a, 0x3b, 0x3a, 0x1b },
  517. { STV090x_8PSK_89, 0x0d, 0x3c, 0x2c, 0x2c, 0x2b, 0x0c, 0x0b, 0x3b, 0x0b, 0x1b },
  518. { STV090x_8PSK_910, 0x0d, 0x0d, 0x2c, 0x3c, 0x3b, 0x1c, 0x0b, 0x3b, 0x0b, 0x1b }
  519. };
  520. /* Cut 2.0 Long Frame Tracking CR Loop */
  521. static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut20[] = {
  522. /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
  523. { STV090x_16APSK_23, 0x0c, 0x0c, 0x0c, 0x0c, 0x1d, 0x0c, 0x3c, 0x0c, 0x2c, 0x0c },
  524. { STV090x_16APSK_34, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0c, 0x2d, 0x0c, 0x1d, 0x0c },
  525. { STV090x_16APSK_45, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
  526. { STV090x_16APSK_56, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0c, 0x3d, 0x0c, 0x2d, 0x0c },
  527. { STV090x_16APSK_89, 0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
  528. { STV090x_16APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x2e, 0x0c, 0x0e, 0x0c, 0x3d, 0x0c },
  529. { STV090x_32APSK_34, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
  530. { STV090x_32APSK_45, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
  531. { STV090x_32APSK_56, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
  532. { STV090x_32APSK_89, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
  533. { STV090x_32APSK_910, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c }
  534. };
  535. /* Cut 3.0 Long Frame Tracking CR Loop */
  536. static struct stv090x_long_frame_crloop stv090x_s2_apsk_crl_cut30[] = {
  537. /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
  538. { STV090x_16APSK_23, 0x0a, 0x0a, 0x0a, 0x0a, 0x1a, 0x0a, 0x3a, 0x0a, 0x2a, 0x0a },
  539. { STV090x_16APSK_34, 0x0a, 0x0a, 0x0a, 0x0a, 0x0b, 0x0a, 0x3b, 0x0a, 0x1b, 0x0a },
  540. { STV090x_16APSK_45, 0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
  541. { STV090x_16APSK_56, 0x0a, 0x0a, 0x0a, 0x0a, 0x1b, 0x0a, 0x3b, 0x0a, 0x2b, 0x0a },
  542. { STV090x_16APSK_89, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
  543. { STV090x_16APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x2b, 0x0a, 0x0c, 0x0a, 0x3b, 0x0a },
  544. { STV090x_32APSK_34, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
  545. { STV090x_32APSK_45, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
  546. { STV090x_32APSK_56, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
  547. { STV090x_32APSK_89, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a },
  548. { STV090x_32APSK_910, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a }
  549. };
  550. static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut20[] = {
  551. /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
  552. { STV090x_QPSK_14, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x2d, 0x1f, 0x3d, 0x3e },
  553. { STV090x_QPSK_13, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x3d, 0x0f, 0x3d, 0x2e },
  554. { STV090x_QPSK_25, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x2e }
  555. };
  556. static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut30[] = {
  557. /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
  558. { STV090x_QPSK_14, 0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x2a, 0x1c, 0x3a, 0x3b },
  559. { STV090x_QPSK_13, 0x0c, 0x3c, 0x0b, 0x3c, 0x2a, 0x2c, 0x3a, 0x0c, 0x3a, 0x2b },
  560. { STV090x_QPSK_25, 0x1c, 0x3c, 0x1b, 0x3c, 0x3a, 0x1c, 0x3a, 0x3b, 0x3a, 0x2b }
  561. };
  562. /* Cut 2.0 Short Frame Tracking CR Loop */
  563. static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut20[] = {
  564. /* MODCOD 2M 5M 10M 20M 30M */
  565. { STV090x_QPSK, 0x2f, 0x2e, 0x0e, 0x0e, 0x3d },
  566. { STV090x_8PSK, 0x3e, 0x0e, 0x2d, 0x0d, 0x3c },
  567. { STV090x_16APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d },
  568. { STV090x_32APSK, 0x1e, 0x1e, 0x1e, 0x3d, 0x2d }
  569. };
  570. /* Cut 3.0 Short Frame Tracking CR Loop */
  571. static struct stv090x_short_frame_crloop stv090x_s2_short_crl_cut30[] = {
  572. /* MODCOD 2M 5M 10M 20M 30M */
  573. { STV090x_QPSK, 0x2C, 0x2B, 0x0B, 0x0B, 0x3A },
  574. { STV090x_8PSK, 0x3B, 0x0B, 0x2A, 0x0A, 0x39 },
  575. { STV090x_16APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A },
  576. { STV090x_32APSK, 0x1B, 0x1B, 0x1B, 0x3A, 0x2A }
  577. };
  578. static inline s32 comp2(s32 __x, s32 __width)
  579. {
  580. if (__width == 32)
  581. return __x;
  582. else
  583. return (__x >= (1 << (__width - 1))) ? (__x - (1 << __width)) : __x;
  584. }
  585. static int stv090x_read_reg(struct stv090x_state *state, unsigned int reg)
  586. {
  587. const struct stv090x_config *config = state->config;
  588. int ret;
  589. u8 b0[] = { reg >> 8, reg & 0xff };
  590. u8 buf;
  591. struct i2c_msg msg[] = {
  592. { .addr = config->address, .flags = 0, .buf = b0, .len = 2 },
  593. { .addr = config->address, .flags = I2C_M_RD, .buf = &buf, .len = 1 }
  594. };
  595. ret = i2c_transfer(state->i2c, msg, 2);
  596. if (ret != 2) {
  597. if (ret != -ERESTARTSYS)
  598. dprintk(FE_ERROR, 1,
  599. "Read error, Reg=[0x%02x], Status=%d",
  600. reg, ret);
  601. return ret < 0 ? ret : -EREMOTEIO;
  602. }
  603. if (unlikely(*state->verbose >= FE_DEBUGREG))
  604. dprintk(FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
  605. reg, buf);
  606. return (unsigned int) buf;
  607. }
  608. static int stv090x_write_regs(struct stv090x_state *state, unsigned int reg, u8 *data, u32 count)
  609. {
  610. const struct stv090x_config *config = state->config;
  611. int ret;
  612. u8 buf[2 + count];
  613. struct i2c_msg i2c_msg = { .addr = config->address, .flags = 0, .buf = buf, .len = 2 + count };
  614. buf[0] = reg >> 8;
  615. buf[1] = reg & 0xff;
  616. memcpy(&buf[2], data, count);
  617. if (unlikely(*state->verbose >= FE_DEBUGREG)) {
  618. int i;
  619. printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
  620. for (i = 0; i < count; i++)
  621. printk(" %02x", data[i]);
  622. printk("\n");
  623. }
  624. ret = i2c_transfer(state->i2c, &i2c_msg, 1);
  625. if (ret != 1) {
  626. if (ret != -ERESTARTSYS)
  627. dprintk(FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
  628. reg, data[0], count, ret);
  629. return ret < 0 ? ret : -EREMOTEIO;
  630. }
  631. return 0;
  632. }
  633. static int stv090x_write_reg(struct stv090x_state *state, unsigned int reg, u8 data)
  634. {
  635. return stv090x_write_regs(state, reg, &data, 1);
  636. }
  637. static int stv090x_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  638. {
  639. struct stv090x_state *state = fe->demodulator_priv;
  640. u32 reg;
  641. reg = STV090x_READ_DEMOD(state, I2CRPT);
  642. if (enable) {
  643. dprintk(FE_DEBUG, 1, "Enable Gate");
  644. STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 1);
  645. if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0)
  646. goto err;
  647. } else {
  648. dprintk(FE_DEBUG, 1, "Disable Gate");
  649. STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 0);
  650. if ((STV090x_WRITE_DEMOD(state, I2CRPT, reg)) < 0)
  651. goto err;
  652. }
  653. return 0;
  654. err:
  655. dprintk(FE_ERROR, 1, "I/O error");
  656. return -1;
  657. }
  658. static void stv090x_get_lock_tmg(struct stv090x_state *state)
  659. {
  660. switch (state->algo) {
  661. case STV090x_BLIND_SEARCH:
  662. dprintk(FE_DEBUG, 1, "Blind Search");
  663. if (state->srate <= 1500000) { /*10Msps< SR <=15Msps*/
  664. state->DemodTimeout = 1500;
  665. state->FecTimeout = 400;
  666. } else if (state->srate <= 5000000) { /*10Msps< SR <=15Msps*/
  667. state->DemodTimeout = 1000;
  668. state->FecTimeout = 300;
  669. } else { /*SR >20Msps*/
  670. state->DemodTimeout = 700;
  671. state->FecTimeout = 100;
  672. }
  673. break;
  674. case STV090x_COLD_SEARCH:
  675. case STV090x_WARM_SEARCH:
  676. default:
  677. dprintk(FE_DEBUG, 1, "Normal Search");
  678. if (state->srate <= 1000000) { /*SR <=1Msps*/
  679. state->DemodTimeout = 4500;
  680. state->FecTimeout = 1700;
  681. } else if (state->srate <= 2000000) { /*1Msps < SR <= 2Msps */
  682. state->DemodTimeout = 2500;
  683. state->FecTimeout = 1100;
  684. } else if (state->srate <= 5000000) { /*2Msps < SR <= 5Msps */
  685. state->DemodTimeout = 1000;
  686. state->FecTimeout = 550;
  687. } else if (state->srate <= 10000000) { /*5Msps < SR <= 10Msps */
  688. state->DemodTimeout = 700;
  689. state->FecTimeout = 250;
  690. } else if (state->srate <= 20000000) { /*10Msps < SR <= 20Msps */
  691. state->DemodTimeout = 400;
  692. state->FecTimeout = 130;
  693. } else { /*SR >20Msps*/
  694. state->DemodTimeout = 300;
  695. state->FecTimeout = 100;
  696. }
  697. break;
  698. }
  699. if (state->algo == STV090x_WARM_SEARCH)
  700. state->DemodTimeout /= 2;
  701. }
  702. static int stv090x_set_srate(struct stv090x_state *state, u32 srate)
  703. {
  704. u32 sym;
  705. if (srate > 60000000) {
  706. sym = (srate << 4); /* SR * 2^16 / master_clk */
  707. sym /= (state->mclk >> 12);
  708. } else if (srate > 6000000) {
  709. sym = (srate << 6);
  710. sym /= (state->mclk >> 10);
  711. } else {
  712. sym = (srate << 9);
  713. sym /= (state->mclk >> 7);
  714. }
  715. if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0x7f) < 0) /* MSB */
  716. goto err;
  717. if (STV090x_WRITE_DEMOD(state, SFRINIT0, (sym & 0xff)) < 0) /* LSB */
  718. goto err;
  719. return 0;
  720. err:
  721. dprintk(FE_ERROR, 1, "I/O error");
  722. return -1;
  723. }
  724. static int stv090x_set_max_srate(struct stv090x_state *state, u32 clk, u32 srate)
  725. {
  726. u32 sym;
  727. srate = 105 * (srate / 100);
  728. if (srate > 60000000) {
  729. sym = (srate << 4); /* SR * 2^16 / master_clk */
  730. sym /= (state->mclk >> 12);
  731. } else if (srate > 6000000) {
  732. sym = (srate << 6);
  733. sym /= (state->mclk >> 10);
  734. } else {
  735. sym = (srate << 9);
  736. sym /= (state->mclk >> 7);
  737. }
  738. if (sym < 0x7fff) {
  739. if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) /* MSB */
  740. goto err;
  741. if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) /* LSB */
  742. goto err;
  743. } else {
  744. if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x7f) < 0) /* MSB */
  745. goto err;
  746. if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xff) < 0) /* LSB */
  747. goto err;
  748. }
  749. return 0;
  750. err:
  751. dprintk(FE_ERROR, 1, "I/O error");
  752. return -1;
  753. }
  754. static int stv090x_set_min_srate(struct stv090x_state *state, u32 clk, u32 srate)
  755. {
  756. u32 sym;
  757. srate = 95 * (srate / 100);
  758. if (srate > 60000000) {
  759. sym = (srate << 4); /* SR * 2^16 / master_clk */
  760. sym /= (state->mclk >> 12);
  761. } else if (srate > 6000000) {
  762. sym = (srate << 6);
  763. sym /= (state->mclk >> 10);
  764. } else {
  765. sym = (srate << 9);
  766. sym /= (state->mclk >> 7);
  767. }
  768. if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0x7f)) < 0) /* MSB */
  769. goto err;
  770. if (STV090x_WRITE_DEMOD(state, SFRLOW0, (sym & 0xff)) < 0) /* LSB */
  771. goto err;
  772. return 0;
  773. err:
  774. dprintk(FE_ERROR, 1, "I/O error");
  775. return -1;
  776. }
  777. static u32 stv090x_car_width(u32 srate, enum stv090x_rolloff rolloff)
  778. {
  779. u32 ro;
  780. switch (rolloff) {
  781. case STV090x_RO_20:
  782. ro = 20;
  783. break;
  784. case STV090x_RO_25:
  785. ro = 25;
  786. break;
  787. case STV090x_RO_35:
  788. default:
  789. ro = 35;
  790. break;
  791. }
  792. return srate + (srate * ro) / 100;
  793. }
  794. static int stv090x_set_vit_thacq(struct stv090x_state *state)
  795. {
  796. if (STV090x_WRITE_DEMOD(state, VTH12, 0x96) < 0)
  797. goto err;
  798. if (STV090x_WRITE_DEMOD(state, VTH23, 0x64) < 0)
  799. goto err;
  800. if (STV090x_WRITE_DEMOD(state, VTH34, 0x36) < 0)
  801. goto err;
  802. if (STV090x_WRITE_DEMOD(state, VTH56, 0x23) < 0)
  803. goto err;
  804. if (STV090x_WRITE_DEMOD(state, VTH67, 0x1e) < 0)
  805. goto err;
  806. if (STV090x_WRITE_DEMOD(state, VTH78, 0x19) < 0)
  807. goto err;
  808. return 0;
  809. err:
  810. dprintk(FE_ERROR, 1, "I/O error");
  811. return -1;
  812. }
  813. static int stv090x_set_vit_thtracq(struct stv090x_state *state)
  814. {
  815. if (STV090x_WRITE_DEMOD(state, VTH12, 0xd0) < 0)
  816. goto err;
  817. if (STV090x_WRITE_DEMOD(state, VTH23, 0x7d) < 0)
  818. goto err;
  819. if (STV090x_WRITE_DEMOD(state, VTH34, 0x53) < 0)
  820. goto err;
  821. if (STV090x_WRITE_DEMOD(state, VTH56, 0x2f) < 0)
  822. goto err;
  823. if (STV090x_WRITE_DEMOD(state, VTH67, 0x24) < 0)
  824. goto err;
  825. if (STV090x_WRITE_DEMOD(state, VTH78, 0x1f) < 0)
  826. goto err;
  827. return 0;
  828. err:
  829. dprintk(FE_ERROR, 1, "I/O error");
  830. return -1;
  831. }
  832. static int stv090x_set_viterbi(struct stv090x_state *state)
  833. {
  834. switch (state->search_mode) {
  835. case STV090x_SEARCH_AUTO:
  836. if (STV090x_WRITE_DEMOD(state, FECM, 0x10) < 0) /* DVB-S and DVB-S2 */
  837. goto err;
  838. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x3f) < 0) /* all puncture rate */
  839. goto err;
  840. break;
  841. case STV090x_SEARCH_DVBS1:
  842. if (STV090x_WRITE_DEMOD(state, FECM, 0x00) < 0) /* disable DSS */
  843. goto err;
  844. switch (state->fec) {
  845. case STV090x_PR12:
  846. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
  847. goto err;
  848. break;
  849. case STV090x_PR23:
  850. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
  851. goto err;
  852. break;
  853. case STV090x_PR34:
  854. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x04) < 0)
  855. goto err;
  856. break;
  857. case STV090x_PR56:
  858. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x08) < 0)
  859. goto err;
  860. break;
  861. case STV090x_PR78:
  862. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x20) < 0)
  863. goto err;
  864. break;
  865. default:
  866. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x2f) < 0) /* all */
  867. goto err;
  868. break;
  869. }
  870. break;
  871. case STV090x_SEARCH_DSS:
  872. if (STV090x_WRITE_DEMOD(state, FECM, 0x80) < 0)
  873. goto err;
  874. switch (state->fec) {
  875. case STV090x_PR12:
  876. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
  877. goto err;
  878. break;
  879. case STV090x_PR23:
  880. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
  881. goto err;
  882. break;
  883. case STV090x_PR67:
  884. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x10) < 0)
  885. goto err;
  886. break;
  887. default:
  888. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x13) < 0) /* 1/2, 2/3, 6/7 */
  889. goto err;
  890. break;
  891. }
  892. break;
  893. default:
  894. break;
  895. }
  896. return 0;
  897. err:
  898. dprintk(FE_ERROR, 1, "I/O error");
  899. return -1;
  900. }
  901. static int stv090x_stop_modcod(struct stv090x_state *state)
  902. {
  903. if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
  904. goto err;
  905. if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
  906. goto err;
  907. if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
  908. goto err;
  909. if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
  910. goto err;
  911. if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
  912. goto err;
  913. if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
  914. goto err;
  915. if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
  916. goto err;
  917. if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xff) < 0)
  918. goto err;
  919. if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xff) < 0)
  920. goto err;
  921. if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xff) < 0)
  922. goto err;
  923. if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xff) < 0)
  924. goto err;
  925. if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xff) < 0)
  926. goto err;
  927. if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xff) < 0)
  928. goto err;
  929. if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xff) < 0)
  930. goto err;
  931. if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
  932. goto err;
  933. if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xff) < 0)
  934. goto err;
  935. return 0;
  936. err:
  937. dprintk(FE_ERROR, 1, "I/O error");
  938. return -1;
  939. }
  940. static int stv090x_activate_modcod(struct stv090x_state *state)
  941. {
  942. if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
  943. goto err;
  944. if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xfc) < 0)
  945. goto err;
  946. if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xcc) < 0)
  947. goto err;
  948. if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xcc) < 0)
  949. goto err;
  950. if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xcc) < 0)
  951. goto err;
  952. if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xcc) < 0)
  953. goto err;
  954. if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xcc) < 0)
  955. goto err;
  956. if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
  957. goto err;
  958. if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
  959. goto err;
  960. if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
  961. goto err;
  962. if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
  963. goto err;
  964. if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
  965. goto err;
  966. if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
  967. goto err;
  968. if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
  969. goto err;
  970. if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xcc) < 0)
  971. goto err;
  972. if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
  973. goto err;
  974. return 0;
  975. err:
  976. dprintk(FE_ERROR, 1, "I/O error");
  977. return -1;
  978. }
  979. static int stv090x_activate_modcod_single(struct stv090x_state *state)
  980. {
  981. if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
  982. goto err;
  983. if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xf0) < 0)
  984. goto err;
  985. if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0x00) < 0)
  986. goto err;
  987. if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0x00) < 0)
  988. goto err;
  989. if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0x00) < 0)
  990. goto err;
  991. if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0x00) < 0)
  992. goto err;
  993. if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0x00) < 0)
  994. goto err;
  995. if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0x00) < 0)
  996. goto err;
  997. if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0x00) < 0)
  998. goto err;
  999. if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0x00) < 0)
  1000. goto err;
  1001. if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0x00) < 0)
  1002. goto err;
  1003. if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0x00) < 0)
  1004. goto err;
  1005. if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0x00) < 0)
  1006. goto err;
  1007. if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0x00) < 0)
  1008. goto err;
  1009. if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0x00) < 0)
  1010. goto err;
  1011. if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0x0f) < 0)
  1012. goto err;
  1013. return 0;
  1014. err:
  1015. dprintk(FE_ERROR, 1, "I/O error");
  1016. return -1;
  1017. }
  1018. static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
  1019. {
  1020. u32 reg;
  1021. switch (state->demod) {
  1022. case STV090x_DEMODULATOR_0:
  1023. mutex_lock(&demod_lock);
  1024. reg = stv090x_read_reg(state, STV090x_STOPCLK2);
  1025. STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, enable);
  1026. if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
  1027. goto err;
  1028. mutex_unlock(&demod_lock);
  1029. break;
  1030. case STV090x_DEMODULATOR_1:
  1031. mutex_lock(&demod_lock);
  1032. reg = stv090x_read_reg(state, STV090x_STOPCLK2);
  1033. STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, enable);
  1034. if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
  1035. goto err;
  1036. mutex_unlock(&demod_lock);
  1037. break;
  1038. default:
  1039. dprintk(FE_ERROR, 1, "Wrong demodulator!");
  1040. break;
  1041. }
  1042. return 0;
  1043. err:
  1044. mutex_unlock(&demod_lock);
  1045. dprintk(FE_ERROR, 1, "I/O error");
  1046. return -1;
  1047. }
  1048. static int stv090x_dvbs_track_crl(struct stv090x_state *state)
  1049. {
  1050. if (state->dev_ver >= 0x30) {
  1051. /* Set ACLC BCLC optimised value vs SR */
  1052. if (state->srate >= 15000000) {
  1053. if (STV090x_WRITE_DEMOD(state, ACLC, 0x2b) < 0)
  1054. goto err;
  1055. if (STV090x_WRITE_DEMOD(state, BCLC, 0x1a) < 0)
  1056. goto err;
  1057. } else if ((state->srate >= 7000000) && (15000000 > state->srate)) {
  1058. if (STV090x_WRITE_DEMOD(state, ACLC, 0x0c) < 0)
  1059. goto err;
  1060. if (STV090x_WRITE_DEMOD(state, BCLC, 0x1b) < 0)
  1061. goto err;
  1062. } else if (state->srate < 7000000) {
  1063. if (STV090x_WRITE_DEMOD(state, ACLC, 0x2c) < 0)
  1064. goto err;
  1065. if (STV090x_WRITE_DEMOD(state, BCLC, 0x1c) < 0)
  1066. goto err;
  1067. }
  1068. } else {
  1069. /* Cut 2.0 */
  1070. if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
  1071. goto err;
  1072. if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
  1073. goto err;
  1074. }
  1075. return 0;
  1076. err:
  1077. dprintk(FE_ERROR, 1, "I/O error");
  1078. return -1;
  1079. }
  1080. static int stv090x_delivery_search(struct stv090x_state *state)
  1081. {
  1082. u32 reg;
  1083. switch (state->search_mode) {
  1084. case STV090x_SEARCH_DVBS1:
  1085. case STV090x_SEARCH_DSS:
  1086. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1087. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  1088. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
  1089. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1090. goto err;
  1091. /* Activate Viterbi decoder in legacy search,
  1092. * do not use FRESVIT1, might impact VITERBI2
  1093. */
  1094. if (stv090x_vitclk_ctl(state, 0) < 0)
  1095. goto err;
  1096. if (stv090x_dvbs_track_crl(state) < 0)
  1097. goto err;
  1098. if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x22) < 0) /* disable DVB-S2 */
  1099. goto err;
  1100. if (stv090x_set_vit_thacq(state) < 0)
  1101. goto err;
  1102. if (stv090x_set_viterbi(state) < 0)
  1103. goto err;
  1104. break;
  1105. case STV090x_SEARCH_DVBS2:
  1106. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1107. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
  1108. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
  1109. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1110. goto err;
  1111. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  1112. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
  1113. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1114. goto err;
  1115. if (stv090x_vitclk_ctl(state, 1) < 0)
  1116. goto err;
  1117. if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0) /* stop DVB-S CR loop */
  1118. goto err;
  1119. if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
  1120. goto err;
  1121. if (state->dev_ver <= 0x20) {
  1122. /* enable S2 carrier loop */
  1123. if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
  1124. goto err;
  1125. } else {
  1126. /* > Cut 3: Stop carrier 3 */
  1127. if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
  1128. goto err;
  1129. }
  1130. if (state->demod_mode != STV090x_SINGLE) {
  1131. /* Cut 2: enable link during search */
  1132. if (stv090x_activate_modcod(state) < 0)
  1133. goto err;
  1134. } else {
  1135. /* Single demodulator
  1136. * Authorize SHORT and LONG frames,
  1137. * QPSK, 8PSK, 16APSK and 32APSK
  1138. */
  1139. if (stv090x_activate_modcod_single(state) < 0)
  1140. goto err;
  1141. }
  1142. break;
  1143. case STV090x_SEARCH_AUTO:
  1144. default:
  1145. /* enable DVB-S2 and DVB-S2 in Auto MODE */
  1146. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1147. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  1148. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
  1149. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1150. goto err;
  1151. if (stv090x_vitclk_ctl(state, 0) < 0)
  1152. goto err;
  1153. if (stv090x_dvbs_track_crl(state) < 0)
  1154. goto err;
  1155. if (state->dev_ver <= 0x20) {
  1156. /* enable S2 carrier loop */
  1157. if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
  1158. goto err;
  1159. } else {
  1160. /* > Cut 3: Stop carrier 3 */
  1161. if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x66) < 0)
  1162. goto err;
  1163. }
  1164. if (state->demod_mode != STV090x_SINGLE) {
  1165. /* Cut 2: enable link during search */
  1166. if (stv090x_activate_modcod(state) < 0)
  1167. goto err;
  1168. } else {
  1169. /* Single demodulator
  1170. * Authorize SHORT and LONG frames,
  1171. * QPSK, 8PSK, 16APSK and 32APSK
  1172. */
  1173. if (stv090x_activate_modcod_single(state) < 0)
  1174. goto err;
  1175. }
  1176. if (state->srate >= 2000000) {
  1177. /* Srate >= 2MSPS, Viterbi threshold to acquire */
  1178. if (stv090x_set_vit_thacq(state) < 0)
  1179. goto err;
  1180. } else {
  1181. /* Srate < 2MSPS, Reset Viterbi thresholdto track
  1182. * and then re-acquire
  1183. */
  1184. if (stv090x_set_vit_thtracq(state) < 0)
  1185. goto err;
  1186. }
  1187. if (stv090x_set_viterbi(state) < 0)
  1188. goto err;
  1189. break;
  1190. }
  1191. return 0;
  1192. err:
  1193. dprintk(FE_ERROR, 1, "I/O error");
  1194. return -1;
  1195. }
  1196. static int stv090x_start_search(struct stv090x_state *state)
  1197. {
  1198. u32 reg, freq_abs;
  1199. s16 freq;
  1200. /* Reset demodulator */
  1201. reg = STV090x_READ_DEMOD(state, DMDISTATE);
  1202. STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f);
  1203. if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
  1204. goto err;
  1205. if (state->dev_ver <= 0x20) {
  1206. if (state->srate <= 5000000) {
  1207. if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0)
  1208. goto err;
  1209. if (STV090x_WRITE_DEMOD(state, CFRUP1, 0x0f) < 0)
  1210. goto err;
  1211. if (STV090x_WRITE_DEMOD(state, CFRUP0, 0xff) < 0)
  1212. goto err;
  1213. if (STV090x_WRITE_DEMOD(state, CFRLOW1, 0xf0) < 0)
  1214. goto err;
  1215. if (STV090x_WRITE_DEMOD(state, CFRLOW0, 0x00) < 0)
  1216. goto err;
  1217. /*enlarge the timing bandwith for Low SR*/
  1218. if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0)
  1219. goto err;
  1220. } else {
  1221. /* If the symbol rate is >5 Msps
  1222. Set The carrier search up and low to auto mode */
  1223. if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
  1224. goto err;
  1225. /*reduce the timing bandwith for high SR*/
  1226. if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
  1227. goto err;
  1228. }
  1229. } else {
  1230. /* >= Cut 3 */
  1231. if (state->srate <= 5000000) {
  1232. /* enlarge the timing bandwith for Low SR */
  1233. STV090x_WRITE_DEMOD(state, RTCS2, 0x68);
  1234. } else {
  1235. /* reduce timing bandwith for high SR */
  1236. STV090x_WRITE_DEMOD(state, RTCS2, 0x44);
  1237. }
  1238. /* Set CFR min and max to manual mode */
  1239. STV090x_WRITE_DEMOD(state, CARCFG, 0x46);
  1240. if (state->algo == STV090x_WARM_SEARCH) {
  1241. /* WARM Start
  1242. * CFR min = -1MHz,
  1243. * CFR max = +1MHz
  1244. */
  1245. freq_abs = 1000 << 16;
  1246. freq_abs /= (state->mclk / 1000);
  1247. freq = (s16) freq_abs;
  1248. } else {
  1249. /* COLD Start
  1250. * CFR min =- (SearchRange / 2 + 600KHz)
  1251. * CFR max = +(SearchRange / 2 + 600KHz)
  1252. * (600KHz for the tuner step size)
  1253. */
  1254. freq_abs = (state->search_range / 2000) + 600;
  1255. freq_abs = freq_abs << 16;
  1256. freq_abs /= (state->mclk / 1000);
  1257. freq = (s16) freq_abs;
  1258. }
  1259. if (STV090x_WRITE_DEMOD(state, CFRUP1, MSB(freq)) < 0)
  1260. goto err;
  1261. if (STV090x_WRITE_DEMOD(state, CFRUP0, LSB(freq)) < 0)
  1262. goto err;
  1263. freq *= -1;
  1264. if (STV090x_WRITE_DEMOD(state, CFRLOW1, MSB(freq)) < 0)
  1265. goto err;
  1266. if (STV090x_WRITE_DEMOD(state, CFRLOW0, LSB(freq)) < 0)
  1267. goto err;
  1268. }
  1269. if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0) < 0)
  1270. goto err;
  1271. if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0) < 0)
  1272. goto err;
  1273. if (state->dev_ver >= 0x20) {
  1274. if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
  1275. goto err;
  1276. if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
  1277. goto err;
  1278. if ((state->search_mode == STV090x_DVBS1) ||
  1279. (state->search_mode == STV090x_DSS) ||
  1280. (state->search_mode == STV090x_SEARCH_AUTO)) {
  1281. if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
  1282. goto err;
  1283. if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0)
  1284. goto err;
  1285. }
  1286. }
  1287. if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00) < 0)
  1288. goto err;
  1289. if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xe0) < 0)
  1290. goto err;
  1291. if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xc0) < 0)
  1292. goto err;
  1293. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1294. STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
  1295. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
  1296. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1297. goto err;
  1298. reg = STV090x_READ_DEMOD(state, DMDCFG2);
  1299. STV090x_SETFIELD_Px(reg, S1S2_SEQUENTIAL_FIELD, 0x0);
  1300. if (STV090x_WRITE_DEMOD(state, DMDCFG2, reg) < 0)
  1301. goto err;
  1302. if (state->dev_ver >= 0x20) {
  1303. /*Frequency offset detector setting*/
  1304. if (state->srate < 2000000) {
  1305. if (state->dev_ver <= 0x20) {
  1306. /* Cut 2 */
  1307. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x39) < 0)
  1308. goto err;
  1309. } else {
  1310. /* Cut 2 */
  1311. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x89) < 0)
  1312. goto err;
  1313. }
  1314. if (STV090x_WRITE_DEMOD(state, CARHDR, 0x40) < 0)
  1315. goto err;
  1316. }
  1317. if (state->srate < 10000000) {
  1318. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4c) < 0)
  1319. goto err;
  1320. } else {
  1321. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4b) < 0)
  1322. goto err;
  1323. }
  1324. } else {
  1325. if (state->srate < 10000000) {
  1326. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
  1327. goto err;
  1328. } else {
  1329. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
  1330. goto err;
  1331. }
  1332. }
  1333. switch (state->algo) {
  1334. case STV090x_WARM_SEARCH:
  1335. /* The symbol rate and the exact
  1336. * carrier Frequency are known
  1337. */
  1338. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  1339. goto err;
  1340. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
  1341. goto err;
  1342. break;
  1343. case STV090x_COLD_SEARCH:
  1344. /* The symbol rate is known */
  1345. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  1346. goto err;
  1347. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
  1348. goto err;
  1349. break;
  1350. default:
  1351. break;
  1352. }
  1353. return 0;
  1354. err:
  1355. dprintk(FE_ERROR, 1, "I/O error");
  1356. return -1;
  1357. }
  1358. static int stv090x_get_agc2_min_level(struct stv090x_state *state)
  1359. {
  1360. u32 agc2_min = 0, agc2 = 0, freq_init, freq_step, reg;
  1361. s32 i, j, steps, dir;
  1362. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
  1363. goto err;
  1364. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1365. STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 1);
  1366. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 1);
  1367. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1368. goto err;
  1369. if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0) /* SR = 65 Msps Max */
  1370. goto err;
  1371. if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
  1372. goto err;
  1373. if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0) /* SR= 400 ksps Min */
  1374. goto err;
  1375. if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
  1376. goto err;
  1377. if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0) /* stop acq @ coarse carrier state */
  1378. goto err;
  1379. if (stv090x_set_srate(state, 1000000) < 0)
  1380. goto err;
  1381. steps = -1 + state->search_range / 1000000;
  1382. steps /= 2;
  1383. steps = (2 * steps) + 1;
  1384. if (steps < 0)
  1385. steps = 1;
  1386. dir = 1;
  1387. freq_step = (1000000 * 256) / (state->mclk / 256);
  1388. freq_init = 0;
  1389. for (i = 0; i < steps; i++) {
  1390. if (dir > 0)
  1391. freq_init = freq_init + (freq_step * i);
  1392. else
  1393. freq_init = freq_init - (freq_step * i);
  1394. dir *= -1;
  1395. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod RESET */
  1396. goto err;
  1397. if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_init >> 8) & 0xff) < 0)
  1398. goto err;
  1399. if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_init & 0xff) < 0)
  1400. goto err;
  1401. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x58) < 0) /* Demod RESET */
  1402. goto err;
  1403. msleep(10);
  1404. for (j = 0; j < 10; j++) {
  1405. agc2 += STV090x_READ_DEMOD(state, AGC2I1) << 8;
  1406. agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
  1407. }
  1408. agc2 /= 10;
  1409. agc2_min = 0xffff;
  1410. if (agc2 < 0xffff)
  1411. agc2_min = agc2;
  1412. }
  1413. return agc2_min;
  1414. err:
  1415. dprintk(FE_ERROR, 1, "I/O error");
  1416. return -1;
  1417. }
  1418. static u32 stv090x_get_srate(struct stv090x_state *state, u32 clk)
  1419. {
  1420. u8 r3, r2, r1, r0;
  1421. s32 srate, int_1, int_2, tmp_1, tmp_2;
  1422. r3 = STV090x_READ_DEMOD(state, SFR3);
  1423. r2 = STV090x_READ_DEMOD(state, SFR2);
  1424. r1 = STV090x_READ_DEMOD(state, SFR1);
  1425. r0 = STV090x_READ_DEMOD(state, SFR0);
  1426. srate = ((r3 << 24) | (r2 << 16) | (r1 << 8) | r0);
  1427. int_1 = clk >> 16;
  1428. int_2 = srate >> 16;
  1429. tmp_1 = clk % 0x10000;
  1430. tmp_2 = srate % 0x10000;
  1431. srate = (int_1 * int_2) +
  1432. ((int_1 * tmp_2) >> 16) +
  1433. ((int_2 * tmp_1) >> 16);
  1434. return srate;
  1435. }
  1436. static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
  1437. {
  1438. struct dvb_frontend *fe = &state->frontend;
  1439. int tmg_lock = 0, i;
  1440. s32 tmg_cpt = 0, dir = 1, steps, cur_step = 0, freq;
  1441. u32 srate_coarse = 0, agc2 = 0, car_step = 1200, reg;
  1442. reg = STV090x_READ_DEMOD(state, DMDISTATE);
  1443. STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f); /* Demod RESET */
  1444. if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
  1445. goto err;
  1446. if (STV090x_WRITE_DEMOD(state, TMGCFG, 0x12) < 0)
  1447. goto err;
  1448. if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xf0) < 0)
  1449. goto err;
  1450. if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xe0) < 0)
  1451. goto err;
  1452. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1453. STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 1);
  1454. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 1);
  1455. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1456. goto err;
  1457. if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0)
  1458. goto err;
  1459. if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
  1460. goto err;
  1461. if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0)
  1462. goto err;
  1463. if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
  1464. goto err;
  1465. if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0)
  1466. goto err;
  1467. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x60) < 0)
  1468. goto err;
  1469. if (state->dev_ver >= 0x30) {
  1470. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x99) < 0)
  1471. goto err;
  1472. if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
  1473. goto err;
  1474. } else if (state->dev_ver >= 0x20) {
  1475. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0)
  1476. goto err;
  1477. if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
  1478. goto err;
  1479. }
  1480. if (state->srate <= 2000000)
  1481. car_step = 1000;
  1482. else if (state->srate <= 5000000)
  1483. car_step = 2000;
  1484. else if (state->srate <= 12000000)
  1485. car_step = 3000;
  1486. else
  1487. car_step = 5000;
  1488. steps = -1 + ((state->search_range / 1000) / car_step);
  1489. steps /= 2;
  1490. steps = (2 * steps) + 1;
  1491. if (steps < 0)
  1492. steps = 1;
  1493. else if (steps > 10) {
  1494. steps = 11;
  1495. car_step = (state->search_range / 1000) / 10;
  1496. }
  1497. cur_step = 0;
  1498. dir = 1;
  1499. freq = state->frequency;
  1500. while ((!tmg_lock) && (cur_step < steps)) {
  1501. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5f) < 0) /* Demod RESET */
  1502. goto err;
  1503. reg = STV090x_READ_DEMOD(state, DMDISTATE);
  1504. STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x00); /* trigger acquisition */
  1505. if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
  1506. goto err;
  1507. msleep(50);
  1508. for (i = 0; i < 10; i++) {
  1509. reg = STV090x_READ_DEMOD(state, DSTATUS);
  1510. if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
  1511. tmg_cpt++;
  1512. agc2 += STV090x_READ_DEMOD(state, AGC2I1) << 8;
  1513. agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
  1514. }
  1515. agc2 /= 10;
  1516. srate_coarse = stv090x_get_srate(state, state->mclk);
  1517. cur_step++;
  1518. dir *= -1;
  1519. if ((tmg_cpt >= 5) && (agc2 < 0x1f00) && (srate_coarse < 55000000) && (srate_coarse > 850000))
  1520. tmg_lock = 1;
  1521. else if (cur_step < steps) {
  1522. if (dir > 0)
  1523. freq += cur_step * car_step;
  1524. else
  1525. freq -= cur_step * car_step;
  1526. /* Setup tuner */
  1527. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  1528. goto err;
  1529. if (state->config->tuner_set_frequency) {
  1530. if (state->config->tuner_set_frequency(fe, state->frequency) < 0)
  1531. goto err;
  1532. }
  1533. if (state->config->tuner_set_bandwidth) {
  1534. if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
  1535. goto err;
  1536. }
  1537. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  1538. goto err;
  1539. msleep(50);
  1540. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  1541. goto err;
  1542. if (state->config->tuner_get_status) {
  1543. if (state->config->tuner_get_status(fe, &reg) < 0)
  1544. goto err;
  1545. }
  1546. if (reg)
  1547. dprintk(FE_DEBUG, 1, "Tuner phase locked");
  1548. else
  1549. dprintk(FE_DEBUG, 1, "Tuner unlocked");
  1550. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  1551. goto err;
  1552. }
  1553. }
  1554. if (!tmg_lock)
  1555. srate_coarse = 0;
  1556. else
  1557. srate_coarse = stv090x_get_srate(state, state->mclk);
  1558. return srate_coarse;
  1559. err:
  1560. dprintk(FE_ERROR, 1, "I/O error");
  1561. return -1;
  1562. }
  1563. static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
  1564. {
  1565. u32 srate_coarse, freq_coarse, sym, reg;
  1566. srate_coarse = stv090x_get_srate(state, state->mclk);
  1567. freq_coarse = STV090x_READ_DEMOD(state, CFR2) << 8;
  1568. freq_coarse |= STV090x_READ_DEMOD(state, CFR1);
  1569. sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
  1570. if (sym < state->srate)
  1571. srate_coarse = 0;
  1572. else {
  1573. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0) /* Demod RESET */
  1574. goto err;
  1575. if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0x01) < 0)
  1576. goto err;
  1577. if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
  1578. goto err;
  1579. if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
  1580. goto err;
  1581. if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
  1582. goto err;
  1583. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1584. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
  1585. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1586. goto err;
  1587. if (state->dev_ver >= 0x30) {
  1588. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x79) < 0)
  1589. goto err;
  1590. } else if (state->dev_ver >= 0x20) {
  1591. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
  1592. goto err;
  1593. }
  1594. if (srate_coarse > 3000000) {
  1595. sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
  1596. sym = (sym / 1000) * 65536;
  1597. sym /= (state->mclk / 1000);
  1598. if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
  1599. goto err;
  1600. if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
  1601. goto err;
  1602. sym = 10 * (srate_coarse / 13); /* SFRLOW = SFR - 30% */
  1603. sym = (sym / 1000) * 65536;
  1604. sym /= (state->mclk / 1000);
  1605. if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
  1606. goto err;
  1607. if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
  1608. goto err;
  1609. sym = (srate_coarse / 1000) * 65536;
  1610. sym /= (state->mclk / 1000);
  1611. if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
  1612. goto err;
  1613. if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
  1614. goto err;
  1615. } else {
  1616. sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
  1617. sym = (sym / 100) * 65536;
  1618. sym /= (state->mclk / 100);
  1619. if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
  1620. goto err;
  1621. if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
  1622. goto err;
  1623. sym = 10 * (srate_coarse / 14); /* SFRLOW = SFR - 30% */
  1624. sym = (sym / 100) * 65536;
  1625. sym /= (state->mclk / 100);
  1626. if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
  1627. goto err;
  1628. if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
  1629. goto err;
  1630. sym = (srate_coarse / 100) * 65536;
  1631. sym /= (state->mclk / 100);
  1632. if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
  1633. goto err;
  1634. if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
  1635. goto err;
  1636. }
  1637. if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
  1638. goto err;
  1639. if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_coarse >> 8) & 0xff) < 0)
  1640. goto err;
  1641. if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_coarse & 0xff) < 0)
  1642. goto err;
  1643. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0) /* trigger acquisition */
  1644. goto err;
  1645. }
  1646. return srate_coarse;
  1647. err:
  1648. dprintk(FE_ERROR, 1, "I/O error");
  1649. return -1;
  1650. }
  1651. static int stv090x_get_dmdlock(struct stv090x_state *state, s32 timeout)
  1652. {
  1653. s32 timer = 0, lock = 0;
  1654. u32 reg;
  1655. u8 stat;
  1656. while ((timer < timeout) && (!lock)) {
  1657. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  1658. stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
  1659. switch (stat) {
  1660. case 0: /* searching */
  1661. case 1: /* first PLH detected */
  1662. default:
  1663. dprintk(FE_DEBUG, 1, "Demodulator searching ..");
  1664. lock = 0;
  1665. break;
  1666. case 2: /* DVB-S2 mode */
  1667. case 3: /* DVB-S1/legacy mode */
  1668. reg = STV090x_READ_DEMOD(state, DSTATUS);
  1669. lock = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
  1670. break;
  1671. }
  1672. if (!lock)
  1673. msleep(10);
  1674. else
  1675. dprintk(FE_DEBUG, 1, "Demodulator acquired LOCK");
  1676. timer += 10;
  1677. }
  1678. return lock;
  1679. }
  1680. static int stv090x_blind_search(struct stv090x_state *state)
  1681. {
  1682. u32 agc2, reg, srate_coarse;
  1683. s32 timeout_dmd = 500, cpt_fail, agc2_ovflw, i;
  1684. u8 k_ref, k_max, k_min;
  1685. int coarse_fail, lock;
  1686. k_max = 120;
  1687. k_min = 30;
  1688. agc2 = stv090x_get_agc2_min_level(state);
  1689. if (agc2 > STV090x_SEARCH_AGC2_TH(state->dev_ver)) {
  1690. lock = 0;
  1691. } else {
  1692. if (state->dev_ver <= 0x20) {
  1693. if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
  1694. goto err;
  1695. } else {
  1696. /* > Cut 3 */
  1697. if (STV090x_WRITE_DEMOD(state, CARCFG, 0x06) < 0)
  1698. goto err;
  1699. }
  1700. if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
  1701. goto err;
  1702. if (state->dev_ver >= 0x20) {
  1703. if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
  1704. goto err;
  1705. if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
  1706. goto err;
  1707. if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
  1708. goto err;
  1709. if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0) /* set viterbi hysteresis */
  1710. goto err;
  1711. }
  1712. k_ref = k_max;
  1713. do {
  1714. if (STV090x_WRITE_DEMOD(state, KREFTMG, k_ref) < 0)
  1715. goto err;
  1716. if (stv090x_srate_srch_coarse(state) != 0) {
  1717. srate_coarse = stv090x_srate_srch_fine(state);
  1718. if (srate_coarse != 0) {
  1719. stv090x_get_lock_tmg(state);
  1720. lock = stv090x_get_dmdlock(state, timeout_dmd);
  1721. } else {
  1722. lock = 0;
  1723. }
  1724. } else {
  1725. cpt_fail = 0;
  1726. agc2_ovflw = 0;
  1727. for (i = 0; i < 10; i++) {
  1728. agc2 = STV090x_READ_DEMOD(state, AGC2I1) << 8;
  1729. agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
  1730. if (agc2 >= 0xff00)
  1731. agc2_ovflw++;
  1732. reg = STV090x_READ_DEMOD(state, DSTATUS2);
  1733. if ((STV090x_GETFIELD_Px(reg, CFR_OVERFLOW_FIELD) == 0x01) &&
  1734. (STV090x_GETFIELD_Px(reg, DEMOD_DELOCK_FIELD) == 0x01))
  1735. cpt_fail++;
  1736. }
  1737. if ((cpt_fail > 7) || (agc2_ovflw > 7))
  1738. coarse_fail = 1;
  1739. lock = 0;
  1740. }
  1741. k_ref -= 30;
  1742. } while ((k_ref >= k_min) && (!lock) && (!coarse_fail));
  1743. }
  1744. return lock;
  1745. err:
  1746. dprintk(FE_ERROR, 1, "I/O error");
  1747. return -1;
  1748. }
  1749. static int stv090x_chk_tmg(struct stv090x_state *state)
  1750. {
  1751. u32 reg;
  1752. s32 tmg_cpt = 0, i;
  1753. u8 freq, tmg_thh, tmg_thl;
  1754. int tmg_lock;
  1755. freq = STV090x_READ_DEMOD(state, CARFREQ);
  1756. tmg_thh = STV090x_READ_DEMOD(state, TMGTHRISE);
  1757. tmg_thl = STV090x_READ_DEMOD(state, TMGTHFALL);
  1758. if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
  1759. goto err;
  1760. if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
  1761. goto err;
  1762. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1763. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00); /* stop carrier offset search */
  1764. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1765. goto err;
  1766. if (STV090x_WRITE_DEMOD(state, RTC, 0x80) < 0)
  1767. goto err;
  1768. if (STV090x_WRITE_DEMOD(state, RTCS2, 0x40) < 0)
  1769. goto err;
  1770. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x00) < 0)
  1771. goto err;
  1772. if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0) /* set car ofset to 0 */
  1773. goto err;
  1774. if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
  1775. goto err;
  1776. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x65) < 0)
  1777. goto err;
  1778. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* trigger acquisition */
  1779. goto err;
  1780. msleep(10);
  1781. for (i = 0; i < 10; i++) {
  1782. reg = STV090x_READ_DEMOD(state, DSTATUS);
  1783. if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
  1784. tmg_cpt++;
  1785. msleep(1);
  1786. }
  1787. if (tmg_cpt >= 3)
  1788. tmg_lock = 1;
  1789. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
  1790. goto err;
  1791. if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0) /* DVB-S1 timing */
  1792. goto err;
  1793. if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0) /* DVB-S2 timing */
  1794. goto err;
  1795. if (STV090x_WRITE_DEMOD(state, CARFREQ, freq) < 0)
  1796. goto err;
  1797. if (STV090x_WRITE_DEMOD(state, TMGTHRISE, tmg_thh) < 0)
  1798. goto err;
  1799. if (STV090x_WRITE_DEMOD(state, TMGTHFALL, tmg_thl) < 0)
  1800. goto err;
  1801. return tmg_lock;
  1802. err:
  1803. dprintk(FE_ERROR, 1, "I/O error");
  1804. return -1;
  1805. }
  1806. static int stv090x_get_coldlock(struct stv090x_state *state, s32 timeout_dmd)
  1807. {
  1808. struct dvb_frontend *fe = &state->frontend;
  1809. u32 reg;
  1810. s32 car_step, steps, cur_step, dir, freq, timeout_lock;
  1811. int lock = 0;
  1812. if (state->srate >= 10000000)
  1813. timeout_lock = timeout_dmd / 3;
  1814. else
  1815. timeout_lock = timeout_dmd / 2;
  1816. lock = stv090x_get_dmdlock(state, timeout_lock); /* cold start wait */
  1817. if (!lock) {
  1818. if (state->srate >= 10000000) {
  1819. if (stv090x_chk_tmg(state)) {
  1820. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  1821. goto err;
  1822. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
  1823. goto err;
  1824. lock = stv090x_get_dmdlock(state, timeout_dmd);
  1825. } else {
  1826. lock = 0;
  1827. }
  1828. } else {
  1829. if (state->srate <= 4000000)
  1830. car_step = 1000;
  1831. else if (state->srate <= 7000000)
  1832. car_step = 2000;
  1833. else if (state->srate <= 10000000)
  1834. car_step = 3000;
  1835. else
  1836. car_step = 5000;
  1837. steps = (state->search_range / 1000) / car_step;
  1838. steps /= 2;
  1839. steps = 2 * (steps + 1);
  1840. if (steps < 0)
  1841. steps = 2;
  1842. else if (steps > 12)
  1843. steps = 12;
  1844. cur_step = 1;
  1845. dir = 1;
  1846. if (!lock) {
  1847. freq = state->frequency;
  1848. state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + state->srate;
  1849. while ((cur_step <= steps) && (!lock)) {
  1850. if (dir > 0)
  1851. freq += cur_step * car_step;
  1852. else
  1853. freq -= cur_step * car_step;
  1854. /* Setup tuner */
  1855. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  1856. goto err;
  1857. if (state->config->tuner_set_frequency) {
  1858. if (state->config->tuner_set_frequency(fe, state->frequency) < 0)
  1859. goto err;
  1860. }
  1861. if (state->config->tuner_set_bandwidth) {
  1862. if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
  1863. goto err;
  1864. }
  1865. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  1866. goto err;
  1867. msleep(50);
  1868. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  1869. goto err;
  1870. if (state->config->tuner_get_status) {
  1871. if (state->config->tuner_get_status(fe, &reg) < 0)
  1872. goto err;
  1873. }
  1874. if (reg)
  1875. dprintk(FE_DEBUG, 1, "Tuner phase locked");
  1876. else
  1877. dprintk(FE_DEBUG, 1, "Tuner unlocked");
  1878. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  1879. goto err;
  1880. STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c);
  1881. if (state->delsys == STV090x_DVBS2) {
  1882. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1883. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
  1884. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
  1885. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1886. goto err;
  1887. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  1888. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
  1889. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1890. goto err;
  1891. }
  1892. if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
  1893. goto err;
  1894. if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
  1895. goto err;
  1896. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  1897. goto err;
  1898. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
  1899. goto err;
  1900. lock = stv090x_get_dmdlock(state, (timeout_dmd / 3));
  1901. dir *= -1;
  1902. cur_step++;
  1903. }
  1904. }
  1905. }
  1906. }
  1907. return lock;
  1908. err:
  1909. dprintk(FE_ERROR, 1, "I/O error");
  1910. return -1;
  1911. }
  1912. static int stv090x_get_loop_params(struct stv090x_state *state, s32 *freq_inc, s32 *timeout_sw, s32 *steps)
  1913. {
  1914. s32 timeout, inc, steps_max, srate, car_max;
  1915. srate = state->srate;
  1916. car_max = state->search_range / 1000;
  1917. car_max += car_max / 10;
  1918. car_max = 65536 * (car_max / 2);
  1919. car_max /= (state->mclk / 1000);
  1920. if (car_max > 0x4000)
  1921. car_max = 0x4000 ; /* maxcarrier should be<= +-1/4 Mclk */
  1922. inc = srate;
  1923. inc /= state->mclk / 1000;
  1924. inc *= 256;
  1925. inc *= 256;
  1926. inc /= 1000;
  1927. switch (state->search_mode) {
  1928. case STV090x_SEARCH_DVBS1:
  1929. case STV090x_SEARCH_DSS:
  1930. inc *= 3; /* freq step = 3% of srate */
  1931. timeout = 20;
  1932. break;
  1933. case STV090x_SEARCH_DVBS2:
  1934. inc *= 4;
  1935. timeout = 25;
  1936. break;
  1937. case STV090x_SEARCH_AUTO:
  1938. default:
  1939. inc *= 3;
  1940. timeout = 25;
  1941. break;
  1942. }
  1943. inc /= 100;
  1944. if ((inc > car_max) || (inc < 0))
  1945. inc = car_max / 2; /* increment <= 1/8 Mclk */
  1946. timeout *= 27500; /* 27.5 Msps reference */
  1947. if (srate > 0)
  1948. timeout /= (srate / 1000);
  1949. if ((timeout > 100) || (timeout < 0))
  1950. timeout = 100;
  1951. steps_max = (car_max / inc) + 1; /* min steps = 3 */
  1952. if ((steps_max > 100) || (steps_max < 0)) {
  1953. steps_max = 100; /* max steps <= 100 */
  1954. inc = car_max / steps_max;
  1955. }
  1956. *freq_inc = inc;
  1957. *timeout_sw = timeout;
  1958. *steps = steps_max;
  1959. return 0;
  1960. }
  1961. static int stv090x_chk_signal(struct stv090x_state *state)
  1962. {
  1963. s32 offst_car, agc2, car_max;
  1964. int no_signal;
  1965. offst_car = STV090x_READ_DEMOD(state, CFR2) << 8;
  1966. offst_car |= STV090x_READ_DEMOD(state, CFR1);
  1967. offst_car = comp2(offst_car, 16);
  1968. agc2 = STV090x_READ_DEMOD(state, AGC2I1) << 8;
  1969. agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
  1970. car_max = state->search_range / 1000;
  1971. car_max += (car_max / 10); /* 10% margin */
  1972. car_max = (65536 * car_max / 2);
  1973. car_max /= state->mclk / 1000;
  1974. if (car_max > 0x4000)
  1975. car_max = 0x4000;
  1976. if ((agc2 > 0x2000) || (offst_car > 2 * car_max) || (offst_car < -2 * car_max)) {
  1977. no_signal = 1;
  1978. dprintk(FE_DEBUG, 1, "No Signal");
  1979. } else {
  1980. no_signal = 0;
  1981. dprintk(FE_DEBUG, 1, "Found Signal");
  1982. }
  1983. return no_signal;
  1984. }
  1985. static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 timeout, int zigzag, s32 steps_max)
  1986. {
  1987. int no_signal, lock = 0;
  1988. s32 cpt_step = 0, offst_freq, car_max;
  1989. u32 reg;
  1990. car_max = state->search_range / 1000;
  1991. car_max += (car_max / 10);
  1992. car_max = (65536 * car_max / 2);
  1993. car_max /= (state->mclk / 1000);
  1994. if (car_max > 0x4000)
  1995. car_max = 0x4000;
  1996. if (zigzag)
  1997. offst_freq = 0;
  1998. else
  1999. offst_freq = -car_max + inc;
  2000. do {
  2001. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0)
  2002. goto err;
  2003. if (STV090x_WRITE_DEMOD(state, CFRINIT1, ((offst_freq / 256) & 0xff)) < 0)
  2004. goto err;
  2005. if (STV090x_WRITE_DEMOD(state, CFRINIT0, offst_freq & 0xff) < 0)
  2006. goto err;
  2007. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
  2008. goto err;
  2009. reg = STV090x_READ_DEMOD(state, PDELCTRL1);
  2010. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x1); /* stop DVB-S2 packet delin */
  2011. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  2012. goto err;
  2013. if (zigzag) {
  2014. if (offst_freq >= 0)
  2015. offst_freq = -offst_freq - 2 * inc;
  2016. else
  2017. offst_freq = -offst_freq;
  2018. } else {
  2019. offst_freq += 2 * inc;
  2020. }
  2021. cpt_step++;
  2022. lock = stv090x_get_dmdlock(state, timeout);
  2023. no_signal = stv090x_chk_signal(state);
  2024. } while ((!lock) &&
  2025. (!no_signal) &&
  2026. ((offst_freq - inc) < car_max) &&
  2027. ((offst_freq + inc) > -car_max) &&
  2028. (cpt_step < steps_max));
  2029. reg = STV090x_READ_DEMOD(state, PDELCTRL1);
  2030. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0);
  2031. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  2032. goto err;
  2033. return lock;
  2034. err:
  2035. dprintk(FE_ERROR, 1, "I/O error");
  2036. return -1;
  2037. }
  2038. static int stv090x_sw_algo(struct stv090x_state *state)
  2039. {
  2040. int no_signal, zigzag, lock = 0;
  2041. u32 reg;
  2042. s32 dvbs2_fly_wheel;
  2043. s32 inc, timeout_step, trials, steps_max;
  2044. /* get params */
  2045. stv090x_get_loop_params(state, &inc, &timeout_step, &steps_max);
  2046. switch (state->search_mode) {
  2047. case STV090x_SEARCH_DVBS1:
  2048. case STV090x_SEARCH_DSS:
  2049. /* accelerate the frequency detector */
  2050. if (state->dev_ver >= 0x20) {
  2051. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3B) < 0)
  2052. goto err;
  2053. }
  2054. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x49) < 0)
  2055. goto err;
  2056. zigzag = 0;
  2057. break;
  2058. case STV090x_SEARCH_DVBS2:
  2059. if (state->dev_ver >= 0x20) {
  2060. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
  2061. goto err;
  2062. }
  2063. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
  2064. goto err;
  2065. zigzag = 1;
  2066. break;
  2067. case STV090x_SEARCH_AUTO:
  2068. default:
  2069. /* accelerate the frequency detector */
  2070. if (state->dev_ver >= 0x20) {
  2071. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3b) < 0)
  2072. goto err;
  2073. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
  2074. goto err;
  2075. }
  2076. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0xc9) < 0)
  2077. goto err;
  2078. zigzag = 0;
  2079. break;
  2080. }
  2081. trials = 0;
  2082. do {
  2083. lock = stv090x_search_car_loop(state, inc, timeout_step, zigzag, steps_max);
  2084. no_signal = stv090x_chk_signal(state);
  2085. trials++;
  2086. /*run the SW search 2 times maximum*/
  2087. if (lock || no_signal || (trials == 2)) {
  2088. /*Check if the demod is not losing lock in DVBS2*/
  2089. if (state->dev_ver >= 0x20) {
  2090. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
  2091. goto err;
  2092. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
  2093. goto err;
  2094. }
  2095. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  2096. if ((lock) && (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == STV090x_DVBS2)) {
  2097. /*Check if the demod is not losing lock in DVBS2*/
  2098. msleep(timeout_step);
  2099. reg = STV090x_READ_DEMOD(state, DMDFLYW);
  2100. dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
  2101. if (dvbs2_fly_wheel < 0xd) { /*if correct frames is decrementing */
  2102. msleep(timeout_step);
  2103. reg = STV090x_READ_DEMOD(state, DMDFLYW);
  2104. dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
  2105. }
  2106. if (dvbs2_fly_wheel < 0xd) {
  2107. /*FALSE lock, The demod is loosing lock */
  2108. lock = 0;
  2109. if (trials < 2) {
  2110. if (state->dev_ver >= 0x20) {
  2111. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
  2112. goto err;
  2113. }
  2114. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
  2115. goto err;
  2116. }
  2117. }
  2118. }
  2119. }
  2120. } while ((!lock) && (trials < 2) && (!no_signal));
  2121. return lock;
  2122. err:
  2123. dprintk(FE_ERROR, 1, "I/O error");
  2124. return -1;
  2125. }
  2126. static enum stv090x_delsys stv090x_get_std(struct stv090x_state *state)
  2127. {
  2128. u32 reg;
  2129. enum stv090x_delsys delsys;
  2130. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  2131. if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 2)
  2132. delsys = STV090x_DVBS2;
  2133. else if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 3) {
  2134. reg = STV090x_READ_DEMOD(state, FECM);
  2135. if (STV090x_GETFIELD_Px(reg, DSS_DVB_FIELD) == 1)
  2136. delsys = STV090x_DSS;
  2137. else
  2138. delsys = STV090x_DVBS1;
  2139. } else {
  2140. delsys = STV090x_ERROR;
  2141. }
  2142. return delsys;
  2143. }
  2144. /* in Hz */
  2145. static s32 stv090x_get_car_freq(struct stv090x_state *state, u32 mclk)
  2146. {
  2147. s32 derot, int_1, int_2, tmp_1, tmp_2;
  2148. derot = STV090x_READ_DEMOD(state, CFR2) << 16;
  2149. derot |= STV090x_READ_DEMOD(state, CFR1) << 8;
  2150. derot |= STV090x_READ_DEMOD(state, CFR0);
  2151. derot = comp2(derot, 24);
  2152. int_1 = state->mclk >> 12;
  2153. int_2 = derot >> 12;
  2154. /* carrier_frequency = MasterClock * Reg / 2^24 */
  2155. tmp_1 = state->mclk % 0x1000;
  2156. tmp_2 = derot % 0x1000;
  2157. derot = (int_1 * int_2) +
  2158. ((int_1 * tmp_2) >> 12) +
  2159. ((int_2 * tmp_1) >> 12);
  2160. return derot;
  2161. }
  2162. static int stv090x_get_viterbi(struct stv090x_state *state)
  2163. {
  2164. u32 reg, rate;
  2165. reg = STV090x_READ_DEMOD(state, VITCURPUN);
  2166. rate = STV090x_GETFIELD_Px(reg, VIT_CURPUN_FIELD);
  2167. switch (rate) {
  2168. case 13:
  2169. state->fec = STV090x_PR12;
  2170. break;
  2171. case 18:
  2172. state->fec = STV090x_PR23;
  2173. break;
  2174. case 21:
  2175. state->fec = STV090x_PR34;
  2176. break;
  2177. case 24:
  2178. state->fec = STV090x_PR56;
  2179. break;
  2180. case 25:
  2181. state->fec = STV090x_PR67;
  2182. break;
  2183. case 26:
  2184. state->fec = STV090x_PR78;
  2185. break;
  2186. default:
  2187. state->fec = STV090x_PRERR;
  2188. break;
  2189. }
  2190. return 0;
  2191. }
  2192. static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *state)
  2193. {
  2194. struct dvb_frontend *fe = &state->frontend;
  2195. u8 tmg;
  2196. u32 reg;
  2197. s32 i = 0, offst_freq;
  2198. msleep(5);
  2199. if (state->algo == STV090x_BLIND_SEARCH) {
  2200. tmg = STV090x_READ_DEMOD(state, TMGREG2);
  2201. STV090x_WRITE_DEMOD(state, SFRSTEP, 0x5c);
  2202. while ((i <= 50) && (tmg != 0) && (tmg != 0xff)) {
  2203. tmg = STV090x_READ_DEMOD(state, TMGREG2);
  2204. msleep(5);
  2205. i += 5;
  2206. }
  2207. }
  2208. state->delsys = stv090x_get_std(state);
  2209. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  2210. goto err;
  2211. if (state->config->tuner_get_frequency) {
  2212. if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
  2213. goto err;
  2214. }
  2215. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  2216. goto err;
  2217. offst_freq = stv090x_get_car_freq(state, state->mclk) / 1000;
  2218. state->frequency += offst_freq;
  2219. if (stv090x_get_viterbi(state) < 0)
  2220. goto err;
  2221. reg = STV090x_READ_DEMOD(state, DMDMODCOD);
  2222. state->modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
  2223. state->pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
  2224. state->frame_len = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) >> 1;
  2225. reg = STV090x_READ_DEMOD(state, TMGOBS);
  2226. state->rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
  2227. reg = STV090x_READ_DEMOD(state, FECM);
  2228. state->inversion = STV090x_GETFIELD_Px(reg, IQINV_FIELD);
  2229. if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000)) {
  2230. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  2231. goto err;
  2232. if (state->config->tuner_get_frequency) {
  2233. if (state->config->tuner_get_frequency(fe, &state->frequency) < 0)
  2234. goto err;
  2235. }
  2236. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  2237. goto err;
  2238. if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
  2239. return STV090x_RANGEOK;
  2240. else if (abs(offst_freq) <= (stv090x_car_width(state->srate, state->rolloff) / 2000))
  2241. return STV090x_RANGEOK;
  2242. else
  2243. return STV090x_OUTOFRANGE; /* Out of Range */
  2244. } else {
  2245. if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
  2246. return STV090x_RANGEOK;
  2247. else
  2248. return STV090x_OUTOFRANGE;
  2249. }
  2250. return STV090x_OUTOFRANGE;
  2251. err:
  2252. dprintk(FE_ERROR, 1, "I/O error");
  2253. return -1;
  2254. }
  2255. static u32 stv090x_get_tmgoffst(struct stv090x_state *state, u32 srate)
  2256. {
  2257. s32 offst_tmg;
  2258. offst_tmg = STV090x_READ_DEMOD(state, TMGREG2) << 16;
  2259. offst_tmg |= STV090x_READ_DEMOD(state, TMGREG1) << 8;
  2260. offst_tmg |= STV090x_READ_DEMOD(state, TMGREG0);
  2261. offst_tmg = comp2(offst_tmg, 24); /* 2's complement */
  2262. if (!offst_tmg)
  2263. offst_tmg = 1;
  2264. offst_tmg = ((s32) srate * 10) / ((s32) 0x1000000 / offst_tmg);
  2265. offst_tmg /= 320;
  2266. return offst_tmg;
  2267. }
  2268. static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_modcod modcod, s32 pilots)
  2269. {
  2270. u8 aclc = 0x29;
  2271. s32 i;
  2272. struct stv090x_long_frame_crloop *car_loop, *car_loop_qpsk_low, *car_loop_apsk_low;
  2273. if (state->dev_ver == 0x20) {
  2274. car_loop = stv090x_s2_crl_cut20;
  2275. car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut20;
  2276. car_loop_apsk_low = stv090x_s2_apsk_crl_cut20;
  2277. } else {
  2278. /* >= Cut 3 */
  2279. car_loop = stv090x_s2_crl_cut30;
  2280. car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut30;
  2281. car_loop_apsk_low = stv090x_s2_apsk_crl_cut30;
  2282. }
  2283. if (modcod < STV090x_QPSK_12) {
  2284. i = 0;
  2285. while ((i < 3) && (modcod != car_loop_qpsk_low[i].modcod))
  2286. i++;
  2287. if (i >= 3)
  2288. i = 2;
  2289. } else {
  2290. i = 0;
  2291. while ((i < 14) && (modcod != car_loop[i].modcod))
  2292. i++;
  2293. if (i >= 14) {
  2294. i = 0;
  2295. while ((i < 11) && (modcod != car_loop_apsk_low[i].modcod))
  2296. i++;
  2297. if (i >= 11)
  2298. i = 10;
  2299. }
  2300. }
  2301. if (modcod <= STV090x_QPSK_25) {
  2302. if (pilots) {
  2303. if (state->srate <= 3000000)
  2304. aclc = car_loop_qpsk_low[i].crl_pilots_on_2;
  2305. else if (state->srate <= 7000000)
  2306. aclc = car_loop_qpsk_low[i].crl_pilots_on_5;
  2307. else if (state->srate <= 15000000)
  2308. aclc = car_loop_qpsk_low[i].crl_pilots_on_10;
  2309. else if (state->srate <= 25000000)
  2310. aclc = car_loop_qpsk_low[i].crl_pilots_on_20;
  2311. else
  2312. aclc = car_loop_qpsk_low[i].crl_pilots_on_30;
  2313. } else {
  2314. if (state->srate <= 3000000)
  2315. aclc = car_loop_qpsk_low[i].crl_pilots_off_2;
  2316. else if (state->srate <= 7000000)
  2317. aclc = car_loop_qpsk_low[i].crl_pilots_off_5;
  2318. else if (state->srate <= 15000000)
  2319. aclc = car_loop_qpsk_low[i].crl_pilots_off_10;
  2320. else if (state->srate <= 25000000)
  2321. aclc = car_loop_qpsk_low[i].crl_pilots_off_20;
  2322. else
  2323. aclc = car_loop_qpsk_low[i].crl_pilots_off_30;
  2324. }
  2325. } else if (modcod <= STV090x_8PSK_910) {
  2326. if (pilots) {
  2327. if (state->srate <= 3000000)
  2328. aclc = car_loop[i].crl_pilots_on_2;
  2329. else if (state->srate <= 7000000)
  2330. aclc = car_loop[i].crl_pilots_on_5;
  2331. else if (state->srate <= 15000000)
  2332. aclc = car_loop[i].crl_pilots_on_10;
  2333. else if (state->srate <= 25000000)
  2334. aclc = car_loop[i].crl_pilots_on_20;
  2335. else
  2336. aclc = car_loop[i].crl_pilots_on_30;
  2337. } else {
  2338. if (state->srate <= 3000000)
  2339. aclc = car_loop[i].crl_pilots_off_2;
  2340. else if (state->srate <= 7000000)
  2341. aclc = car_loop[i].crl_pilots_off_5;
  2342. else if (state->srate <= 15000000)
  2343. aclc = car_loop[i].crl_pilots_off_10;
  2344. else if (state->srate <= 25000000)
  2345. aclc = car_loop[i].crl_pilots_off_20;
  2346. else
  2347. aclc = car_loop[i].crl_pilots_off_30;
  2348. }
  2349. } else { /* 16APSK and 32APSK */
  2350. if (state->srate <= 3000000)
  2351. aclc = car_loop_apsk_low[i].crl_pilots_on_2;
  2352. else if (state->srate <= 7000000)
  2353. aclc = car_loop_apsk_low[i].crl_pilots_on_5;
  2354. else if (state->srate <= 15000000)
  2355. aclc = car_loop_apsk_low[i].crl_pilots_on_10;
  2356. else if (state->srate <= 25000000)
  2357. aclc = car_loop_apsk_low[i].crl_pilots_on_20;
  2358. else
  2359. aclc = car_loop_apsk_low[i].crl_pilots_on_30;
  2360. }
  2361. return aclc;
  2362. }
  2363. static u8 stv090x_optimize_carloop_short(struct stv090x_state *state)
  2364. {
  2365. struct stv090x_short_frame_crloop *short_crl = NULL;
  2366. s32 index = 0;
  2367. u8 aclc = 0x0b;
  2368. switch (state->modulation) {
  2369. case STV090x_QPSK:
  2370. default:
  2371. index = 0;
  2372. break;
  2373. case STV090x_8PSK:
  2374. index = 1;
  2375. break;
  2376. case STV090x_16APSK:
  2377. index = 2;
  2378. break;
  2379. case STV090x_32APSK:
  2380. index = 3;
  2381. break;
  2382. }
  2383. if (state->dev_ver >= 0x30) {
  2384. /* Cut 3.0 and up */
  2385. short_crl = stv090x_s2_short_crl_cut30;
  2386. } else {
  2387. /* Cut 2.0 and up: we don't support cuts older than 2.0 */
  2388. short_crl = stv090x_s2_short_crl_cut20;
  2389. }
  2390. if (state->srate <= 3000000)
  2391. aclc = short_crl[index].crl_2;
  2392. else if (state->srate <= 7000000)
  2393. aclc = short_crl[index].crl_5;
  2394. else if (state->srate <= 15000000)
  2395. aclc = short_crl[index].crl_10;
  2396. else if (state->srate <= 25000000)
  2397. aclc = short_crl[index].crl_20;
  2398. else
  2399. aclc = short_crl[index].crl_30;
  2400. return aclc;
  2401. }
  2402. static int stv090x_optimize_track(struct stv090x_state *state)
  2403. {
  2404. struct dvb_frontend *fe = &state->frontend;
  2405. enum stv090x_rolloff rolloff;
  2406. enum stv090x_modcod modcod;
  2407. s32 srate, pilots, aclc, f_1, f_0, i = 0, blind_tune = 0;
  2408. u32 reg;
  2409. srate = stv090x_get_srate(state, state->mclk);
  2410. srate += stv090x_get_tmgoffst(state, srate);
  2411. switch (state->delsys) {
  2412. case STV090x_DVBS1:
  2413. case STV090x_DSS:
  2414. if (state->search_mode == STV090x_SEARCH_AUTO) {
  2415. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  2416. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  2417. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
  2418. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  2419. goto err;
  2420. }
  2421. reg = STV090x_READ_DEMOD(state, DEMOD);
  2422. STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
  2423. STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x01);
  2424. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  2425. goto err;
  2426. if (state->dev_ver >= 0x30) {
  2427. if (stv090x_get_viterbi(state) < 0)
  2428. goto err;
  2429. if (state->fec == STV090x_PR12) {
  2430. if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x98) < 0)
  2431. goto err;
  2432. if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
  2433. goto err;
  2434. } else {
  2435. if (STV090x_WRITE_DEMOD(state, GAUSSR0, 0x18) < 0)
  2436. goto err;
  2437. if (STV090x_WRITE_DEMOD(state, CCIR0, 0x18) < 0)
  2438. goto err;
  2439. }
  2440. }
  2441. if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
  2442. goto err;
  2443. break;
  2444. case STV090x_DVBS2:
  2445. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  2446. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
  2447. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
  2448. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  2449. goto err;
  2450. if (STV090x_WRITE_DEMOD(state, ACLC, 0) < 0)
  2451. goto err;
  2452. if (STV090x_WRITE_DEMOD(state, BCLC, 0) < 0)
  2453. goto err;
  2454. if (state->frame_len == STV090x_LONG_FRAME) {
  2455. reg = STV090x_READ_DEMOD(state, DMDMODCOD);
  2456. modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
  2457. pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
  2458. aclc = stv090x_optimize_carloop(state, modcod, pilots);
  2459. if (modcod <= STV090x_QPSK_910) {
  2460. STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc);
  2461. } else if (modcod <= STV090x_8PSK_910) {
  2462. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2463. goto err;
  2464. if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
  2465. goto err;
  2466. }
  2467. if ((state->demod_mode == STV090x_SINGLE) && (modcod > STV090x_8PSK_910)) {
  2468. if (modcod <= STV090x_16APSK_910) {
  2469. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2470. goto err;
  2471. if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
  2472. goto err;
  2473. } else {
  2474. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2475. goto err;
  2476. if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
  2477. goto err;
  2478. }
  2479. }
  2480. } else {
  2481. /*Carrier loop setting for short frame*/
  2482. aclc = stv090x_optimize_carloop_short(state);
  2483. if (state->modulation == STV090x_QPSK) {
  2484. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc) < 0)
  2485. goto err;
  2486. } else if (state->modulation == STV090x_8PSK) {
  2487. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2488. goto err;
  2489. if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
  2490. goto err;
  2491. } else if (state->modulation == STV090x_16APSK) {
  2492. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2493. goto err;
  2494. if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
  2495. goto err;
  2496. } else if (state->modulation == STV090x_32APSK) {
  2497. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2498. goto err;
  2499. if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
  2500. goto err;
  2501. }
  2502. }
  2503. STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67); /* PER */
  2504. break;
  2505. case STV090x_UNKNOWN:
  2506. default:
  2507. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  2508. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  2509. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
  2510. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  2511. goto err;
  2512. break;
  2513. }
  2514. f_1 = STV090x_READ_DEMOD(state, CFR2);
  2515. f_0 = STV090x_READ_DEMOD(state, CFR1);
  2516. reg = STV090x_READ_DEMOD(state, TMGOBS);
  2517. rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
  2518. if (state->algo == STV090x_BLIND_SEARCH) {
  2519. STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00);
  2520. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  2521. STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0x00);
  2522. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
  2523. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  2524. goto err;
  2525. if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0)
  2526. goto err;
  2527. if (stv090x_set_srate(state, srate) < 0)
  2528. goto err;
  2529. blind_tune = 1;
  2530. }
  2531. if (state->dev_ver >= 0x20) {
  2532. if ((state->search_mode == STV090x_SEARCH_DVBS1) ||
  2533. (state->search_mode == STV090x_SEARCH_DSS) ||
  2534. (state->search_mode == STV090x_SEARCH_AUTO)) {
  2535. if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x0a) < 0)
  2536. goto err;
  2537. if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x00) < 0)
  2538. goto err;
  2539. }
  2540. }
  2541. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
  2542. goto err;
  2543. /* AUTO tracking MODE */
  2544. if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x80) < 0)
  2545. goto err;
  2546. /* AUTO tracking MODE */
  2547. if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x80) < 0)
  2548. goto err;
  2549. if ((state->dev_ver >= 0x20) || (blind_tune == 1) || (state->srate < 10000000)) {
  2550. /* update initial carrier freq with the found freq offset */
  2551. if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
  2552. goto err;
  2553. if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
  2554. goto err;
  2555. state->tuner_bw = stv090x_car_width(srate, state->rolloff) + 10000000;
  2556. if ((state->dev_ver >= 0x20) || (blind_tune == 1)) {
  2557. if (state->algo != STV090x_WARM_SEARCH) {
  2558. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  2559. goto err;
  2560. if (state->config->tuner_set_bandwidth) {
  2561. if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
  2562. goto err;
  2563. }
  2564. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  2565. goto err;
  2566. }
  2567. }
  2568. if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000))
  2569. msleep(50); /* blind search: wait 50ms for SR stabilization */
  2570. else
  2571. msleep(5);
  2572. stv090x_get_lock_tmg(state);
  2573. if (!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) {
  2574. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  2575. goto err;
  2576. if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
  2577. goto err;
  2578. if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
  2579. goto err;
  2580. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
  2581. goto err;
  2582. i = 0;
  2583. while ((!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) && (i <= 2)) {
  2584. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  2585. goto err;
  2586. if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
  2587. goto err;
  2588. if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
  2589. goto err;
  2590. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
  2591. goto err;
  2592. i++;
  2593. }
  2594. }
  2595. }
  2596. if (state->dev_ver >= 0x20) {
  2597. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
  2598. goto err;
  2599. }
  2600. if ((state->delsys == STV090x_DVBS1) || (state->delsys == STV090x_DSS))
  2601. stv090x_set_vit_thtracq(state);
  2602. return 0;
  2603. err:
  2604. dprintk(FE_ERROR, 1, "I/O error");
  2605. return -1;
  2606. }
  2607. static int stv090x_get_feclock(struct stv090x_state *state, s32 timeout)
  2608. {
  2609. s32 timer = 0, lock = 0, stat;
  2610. u32 reg;
  2611. while ((timer < timeout) && (!lock)) {
  2612. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  2613. stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
  2614. switch (stat) {
  2615. case 0: /* searching */
  2616. case 1: /* first PLH detected */
  2617. default:
  2618. lock = 0;
  2619. break;
  2620. case 2: /* DVB-S2 mode */
  2621. reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
  2622. lock = STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD);
  2623. break;
  2624. case 3: /* DVB-S1/legacy mode */
  2625. reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
  2626. lock = STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD);
  2627. break;
  2628. }
  2629. if (!lock) {
  2630. msleep(10);
  2631. timer += 10;
  2632. }
  2633. }
  2634. return lock;
  2635. }
  2636. static int stv090x_get_lock(struct stv090x_state *state, s32 timeout_dmd, s32 timeout_fec)
  2637. {
  2638. u32 reg;
  2639. s32 timer = 0;
  2640. int lock;
  2641. lock = stv090x_get_dmdlock(state, timeout_dmd);
  2642. if (lock)
  2643. lock = stv090x_get_feclock(state, timeout_fec);
  2644. if (lock) {
  2645. lock = 0;
  2646. while ((timer < timeout_fec) && (!lock)) {
  2647. reg = STV090x_READ_DEMOD(state, TSSTATUS);
  2648. lock = STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD);
  2649. msleep(1);
  2650. timer++;
  2651. }
  2652. }
  2653. return lock;
  2654. }
  2655. static int stv090x_set_s2rolloff(struct stv090x_state *state)
  2656. {
  2657. u32 reg;
  2658. if (state->dev_ver <= 0x20) {
  2659. /* rolloff to auto mode if DVBS2 */
  2660. reg = STV090x_READ_DEMOD(state, DEMOD);
  2661. STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x00);
  2662. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  2663. goto err;
  2664. } else {
  2665. /* DVB-S2 rolloff to auto mode if DVBS2 */
  2666. reg = STV090x_READ_DEMOD(state, DEMOD);
  2667. STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 0x00);
  2668. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  2669. goto err;
  2670. }
  2671. return 0;
  2672. err:
  2673. dprintk(FE_ERROR, 1, "I/O error");
  2674. return -1;
  2675. }
  2676. static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
  2677. {
  2678. struct dvb_frontend *fe = &state->frontend;
  2679. enum stv090x_signal_state signal_state = STV090x_NOCARRIER;
  2680. u32 reg;
  2681. s32 timeout_dmd = 500, timeout_fec = 50, agc1_power, power_iq = 0, i;
  2682. int lock = 0, low_sr = 0, no_signal = 0;
  2683. reg = STV090x_READ_DEMOD(state, TSCFGH);
  2684. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* Stop path 1 stream merger */
  2685. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2686. goto err;
  2687. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod stop */
  2688. goto err;
  2689. if (state->dev_ver >= 0x20) {
  2690. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0) /* cut 2.0 */
  2691. goto err;
  2692. }
  2693. stv090x_get_lock_tmg(state);
  2694. if (state->algo == STV090x_BLIND_SEARCH) {
  2695. state->tuner_bw = 2 * 36000000; /* wide bw for unknown srate */
  2696. if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc0) < 0) /* wider srate scan */
  2697. goto err;
  2698. if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
  2699. goto err;
  2700. if (stv090x_set_srate(state, 1000000) < 0) /* inital srate = 1Msps */
  2701. goto err;
  2702. } else {
  2703. /* known srate */
  2704. if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
  2705. goto err;
  2706. if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
  2707. goto err;
  2708. if (state->srate < 2000000) {
  2709. /* SR < 2MSPS */
  2710. if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x63) < 0)
  2711. goto err;
  2712. } else {
  2713. /* SR >= 2Msps */
  2714. if (STV090x_WRITE_DEMOD(state, CORRELMANT, 0x70) < 0)
  2715. goto err;
  2716. }
  2717. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
  2718. goto err;
  2719. if (state->dev_ver >= 0x20) {
  2720. if (STV090x_WRITE_DEMOD(state, KREFTMG, 0x5a) < 0)
  2721. goto err;
  2722. if (state->algo == STV090x_COLD_SEARCH)
  2723. state->tuner_bw = (15 * (stv090x_car_width(state->srate, state->rolloff) + 10000000)) / 10;
  2724. else if (state->algo == STV090x_WARM_SEARCH)
  2725. state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + 10000000;
  2726. }
  2727. /* if cold start or warm (Symbolrate is known)
  2728. * use a Narrow symbol rate scan range
  2729. */
  2730. if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0xc1) < 0) /* narrow srate scan */
  2731. goto err;
  2732. if (stv090x_set_srate(state, state->srate) < 0)
  2733. goto err;
  2734. if (stv090x_set_max_srate(state, state->mclk, state->srate) < 0)
  2735. goto err;
  2736. if (stv090x_set_min_srate(state, state->mclk, state->srate) < 0)
  2737. goto err;
  2738. if (state->srate >= 10000000)
  2739. low_sr = 0;
  2740. else
  2741. low_sr = 1;
  2742. }
  2743. /* Setup tuner */
  2744. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  2745. goto err;
  2746. if (state->config->tuner_set_bbgain) {
  2747. if (state->config->tuner_set_bbgain(fe, 10) < 0) /* 10dB */
  2748. goto err;
  2749. }
  2750. if (state->config->tuner_set_frequency) {
  2751. if (state->config->tuner_set_frequency(fe, state->frequency) < 0)
  2752. goto err;
  2753. }
  2754. if (state->config->tuner_set_bandwidth) {
  2755. if (state->config->tuner_set_bandwidth(fe, state->tuner_bw) < 0)
  2756. goto err;
  2757. }
  2758. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  2759. goto err;
  2760. msleep(50);
  2761. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  2762. goto err;
  2763. if (state->config->tuner_get_status) {
  2764. if (state->config->tuner_get_status(fe, &reg) < 0)
  2765. goto err;
  2766. }
  2767. if (reg)
  2768. dprintk(FE_DEBUG, 1, "Tuner phase locked");
  2769. else
  2770. dprintk(FE_DEBUG, 1, "Tuner unlocked");
  2771. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  2772. goto err;
  2773. msleep(10);
  2774. agc1_power = MAKEWORD16(STV090x_READ_DEMOD(state, AGCIQIN1),
  2775. STV090x_READ_DEMOD(state, AGCIQIN0));
  2776. if (agc1_power == 0) {
  2777. /* If AGC1 integrator value is 0
  2778. * then read POWERI, POWERQ
  2779. */
  2780. for (i = 0; i < 5; i++) {
  2781. power_iq += (STV090x_READ_DEMOD(state, POWERI) +
  2782. STV090x_READ_DEMOD(state, POWERQ)) >> 1;
  2783. }
  2784. power_iq /= 5;
  2785. }
  2786. if ((agc1_power == 0) && (power_iq < STV090x_IQPOWER_THRESHOLD)) {
  2787. dprintk(FE_ERROR, 1, "No Signal: POWER_IQ=0x%02x", power_iq);
  2788. lock = 0;
  2789. } else {
  2790. reg = STV090x_READ_DEMOD(state, DEMOD);
  2791. STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, state->inversion);
  2792. if (state->dev_ver <= 0x20) {
  2793. /* rolloff to auto mode if DVBS2 */
  2794. STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 1);
  2795. } else {
  2796. /* DVB-S2 rolloff to auto mode if DVBS2 */
  2797. STV090x_SETFIELD_Px(reg, MANUAL_S2ROLLOFF_FIELD, 1);
  2798. }
  2799. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  2800. goto err;
  2801. if (stv090x_delivery_search(state) < 0)
  2802. goto err;
  2803. if (state->algo != STV090x_BLIND_SEARCH) {
  2804. if (stv090x_start_search(state) < 0)
  2805. goto err;
  2806. }
  2807. }
  2808. /* need to check for AGC1 state */
  2809. if (state->algo == STV090x_BLIND_SEARCH)
  2810. lock = stv090x_blind_search(state);
  2811. else if (state->algo == STV090x_COLD_SEARCH)
  2812. lock = stv090x_get_coldlock(state, timeout_dmd);
  2813. else if (state->algo == STV090x_WARM_SEARCH)
  2814. lock = stv090x_get_dmdlock(state, timeout_dmd);
  2815. if ((!lock) && (state->algo == STV090x_COLD_SEARCH)) {
  2816. if (!low_sr) {
  2817. if (stv090x_chk_tmg(state))
  2818. lock = stv090x_sw_algo(state);
  2819. }
  2820. }
  2821. if (lock)
  2822. signal_state = stv090x_get_sig_params(state);
  2823. if ((lock) && (signal_state == STV090x_RANGEOK)) { /* signal within Range */
  2824. stv090x_optimize_track(state);
  2825. if (state->dev_ver >= 0x20) {
  2826. /* >= Cut 2.0 :release TS reset after
  2827. * demod lock and optimized Tracking
  2828. */
  2829. reg = STV090x_READ_DEMOD(state, TSCFGH);
  2830. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
  2831. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2832. goto err;
  2833. msleep(3);
  2834. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
  2835. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2836. goto err;
  2837. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
  2838. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2839. goto err;
  2840. }
  2841. if (stv090x_get_lock(state, timeout_fec, timeout_fec)) {
  2842. lock = 1;
  2843. if (state->delsys == STV090x_DVBS2) {
  2844. stv090x_set_s2rolloff(state);
  2845. reg = STV090x_READ_DEMOD(state, PDELCTRL2);
  2846. STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 1);
  2847. if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
  2848. goto err;
  2849. /* Reset DVBS2 packet delinator error counter */
  2850. reg = STV090x_READ_DEMOD(state, PDELCTRL2);
  2851. STV090x_SETFIELD_Px(reg, RESET_UPKO_COUNT, 0);
  2852. if (STV090x_WRITE_DEMOD(state, PDELCTRL2, reg) < 0)
  2853. goto err;
  2854. if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67) < 0) /* PER */
  2855. goto err;
  2856. } else {
  2857. if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
  2858. goto err;
  2859. }
  2860. /* Reset the Total packet counter */
  2861. if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0x00) < 0)
  2862. goto err;
  2863. /* Reset the packet Error counter2 */
  2864. if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
  2865. goto err;
  2866. } else {
  2867. lock = 0;
  2868. signal_state = STV090x_NODATA;
  2869. no_signal = stv090x_chk_signal(state);
  2870. }
  2871. }
  2872. return signal_state;
  2873. err:
  2874. dprintk(FE_ERROR, 1, "I/O error");
  2875. return -1;
  2876. }
  2877. static enum dvbfe_search stv090x_search(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
  2878. {
  2879. struct stv090x_state *state = fe->demodulator_priv;
  2880. struct dtv_frontend_properties *props = &fe->dtv_property_cache;
  2881. state->delsys = props->delivery_system;
  2882. state->frequency = p->frequency;
  2883. state->srate = p->u.qpsk.symbol_rate;
  2884. state->search_mode = STV090x_SEARCH_AUTO;
  2885. state->algo = STV090x_COLD_SEARCH;
  2886. state->fec = STV090x_PRERR;
  2887. if (state->srate > 10000000) {
  2888. dprintk(FE_DEBUG, 1, "Search range: 10 MHz");
  2889. state->search_range = 10000000;
  2890. } else {
  2891. dprintk(FE_DEBUG, 1, "Search range: 5 MHz");
  2892. state->search_range = 5000000;
  2893. }
  2894. if (stv090x_algo(state) == STV090x_RANGEOK) {
  2895. dprintk(FE_DEBUG, 1, "Search success!");
  2896. return DVBFE_ALGO_SEARCH_SUCCESS;
  2897. } else {
  2898. dprintk(FE_DEBUG, 1, "Search failed!");
  2899. return DVBFE_ALGO_SEARCH_FAILED;
  2900. }
  2901. return DVBFE_ALGO_SEARCH_ERROR;
  2902. }
  2903. /* FIXME! */
  2904. static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
  2905. {
  2906. struct stv090x_state *state = fe->demodulator_priv;
  2907. u32 reg;
  2908. u8 search_state;
  2909. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  2910. search_state = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
  2911. switch (search_state) {
  2912. case 0: /* searching */
  2913. case 1: /* first PLH detected */
  2914. default:
  2915. dprintk(FE_DEBUG, 1, "Status: Unlocked (Searching ..)");
  2916. *status = 0;
  2917. break;
  2918. case 2: /* DVB-S2 mode */
  2919. dprintk(FE_DEBUG, 1, "Delivery system: DVB-S2");
  2920. reg = STV090x_READ_DEMOD(state, DSTATUS);
  2921. if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
  2922. reg = STV090x_READ_DEMOD(state, TSSTATUS);
  2923. if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
  2924. *status = FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  2925. }
  2926. }
  2927. break;
  2928. case 3: /* DVB-S1/legacy mode */
  2929. dprintk(FE_DEBUG, 1, "Delivery system: DVB-S");
  2930. reg = STV090x_READ_DEMOD(state, DSTATUS);
  2931. if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
  2932. reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
  2933. if (STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD)) {
  2934. reg = STV090x_READ_DEMOD(state, TSSTATUS);
  2935. if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
  2936. *status = FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  2937. }
  2938. }
  2939. }
  2940. break;
  2941. }
  2942. return 0;
  2943. }
  2944. static int stv090x_read_per(struct dvb_frontend *fe, u32 *per)
  2945. {
  2946. struct stv090x_state *state = fe->demodulator_priv;
  2947. s32 count_4, count_3, count_2, count_1, count_0, count;
  2948. u32 reg, h, m, l;
  2949. enum fe_status status;
  2950. stv090x_read_status(fe, &status);
  2951. if (!(status & FE_HAS_LOCK)) {
  2952. *per = 1 << 23; /* Max PER */
  2953. } else {
  2954. /* Counter 2 */
  2955. reg = STV090x_READ_DEMOD(state, ERRCNT22);
  2956. h = STV090x_GETFIELD_Px(reg, ERR_CNT2_FIELD);
  2957. reg = STV090x_READ_DEMOD(state, ERRCNT21);
  2958. m = STV090x_GETFIELD_Px(reg, ERR_CNT21_FIELD);
  2959. reg = STV090x_READ_DEMOD(state, ERRCNT20);
  2960. l = STV090x_GETFIELD_Px(reg, ERR_CNT20_FIELD);
  2961. *per = ((h << 16) | (m << 8) | l);
  2962. count_4 = STV090x_READ_DEMOD(state, FBERCPT4);
  2963. count_3 = STV090x_READ_DEMOD(state, FBERCPT3);
  2964. count_2 = STV090x_READ_DEMOD(state, FBERCPT2);
  2965. count_1 = STV090x_READ_DEMOD(state, FBERCPT1);
  2966. count_0 = STV090x_READ_DEMOD(state, FBERCPT0);
  2967. if ((!count_4) && (!count_3)) {
  2968. count = (count_2 & 0xff) << 16;
  2969. count |= (count_1 & 0xff) << 8;
  2970. count |= count_0 & 0xff;
  2971. } else {
  2972. count = 1 << 24;
  2973. }
  2974. if (count == 0)
  2975. *per = 1;
  2976. }
  2977. if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0) < 0)
  2978. goto err;
  2979. if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
  2980. goto err;
  2981. return 0;
  2982. err:
  2983. dprintk(FE_ERROR, 1, "I/O error");
  2984. return -1;
  2985. }
  2986. static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
  2987. {
  2988. int res = 0;
  2989. int min = 0, med;
  2990. if ((val >= tab[min].read && val < tab[max].read) ||
  2991. (val >= tab[max].read && val < tab[min].read)) {
  2992. while ((max - min) > 1) {
  2993. med = (max + min) / 2;
  2994. if ((val >= tab[min].read && val < tab[med].read) ||
  2995. (val >= tab[med].read && val < tab[min].read))
  2996. max = med;
  2997. else
  2998. min = med;
  2999. }
  3000. res = ((val - tab[min].read) *
  3001. (tab[max].real - tab[min].real) /
  3002. (tab[max].read - tab[min].read)) +
  3003. tab[min].real;
  3004. } else {
  3005. if (tab[min].read < tab[max].read) {
  3006. if (val < tab[min].read)
  3007. res = tab[min].real;
  3008. else if (val >= tab[max].read)
  3009. res = tab[max].real;
  3010. } else {
  3011. if (val >= tab[min].read)
  3012. res = tab[min].real;
  3013. else if (val < tab[max].read)
  3014. res = tab[max].real;
  3015. }
  3016. }
  3017. return res;
  3018. }
  3019. static int stv090x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  3020. {
  3021. struct stv090x_state *state = fe->demodulator_priv;
  3022. u32 reg;
  3023. s32 agc_0, agc_1, agc;
  3024. s32 str;
  3025. reg = STV090x_READ_DEMOD(state, AGCIQIN1);
  3026. agc_1 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
  3027. reg = STV090x_READ_DEMOD(state, AGCIQIN0);
  3028. agc_0 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
  3029. agc = MAKEWORD16(agc_1, agc_0);
  3030. str = stv090x_table_lookup(stv090x_rf_tab,
  3031. ARRAY_SIZE(stv090x_rf_tab) - 1, agc);
  3032. if (agc > stv090x_rf_tab[0].read)
  3033. str = 0;
  3034. else if (agc < stv090x_rf_tab[ARRAY_SIZE(stv090x_rf_tab) - 1].read)
  3035. str = -100;
  3036. *strength = (str + 100) * 0xFFFF / 100;
  3037. return 0;
  3038. }
  3039. static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
  3040. {
  3041. struct stv090x_state *state = fe->demodulator_priv;
  3042. u32 reg_0, reg_1, reg, i;
  3043. s32 val_0, val_1, val = 0;
  3044. u8 lock_f;
  3045. s32 div;
  3046. u32 last;
  3047. switch (state->delsys) {
  3048. case STV090x_DVBS2:
  3049. reg = STV090x_READ_DEMOD(state, DSTATUS);
  3050. lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
  3051. if (lock_f) {
  3052. msleep(5);
  3053. for (i = 0; i < 16; i++) {
  3054. reg_1 = STV090x_READ_DEMOD(state, NNOSPLHT1);
  3055. val_1 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
  3056. reg_0 = STV090x_READ_DEMOD(state, NNOSPLHT0);
  3057. val_0 = STV090x_GETFIELD_Px(reg_0, NOSPLHT_NORMED_FIELD);
  3058. val += MAKEWORD16(val_1, val_0);
  3059. msleep(1);
  3060. }
  3061. val /= 16;
  3062. last = ARRAY_SIZE(stv090x_s2cn_tab) - 1;
  3063. div = stv090x_s2cn_tab[0].read -
  3064. stv090x_s2cn_tab[last].read;
  3065. *cnr = 0xFFFF - ((val * 0xFFFF) / div);
  3066. }
  3067. break;
  3068. case STV090x_DVBS1:
  3069. case STV090x_DSS:
  3070. reg = STV090x_READ_DEMOD(state, DSTATUS);
  3071. lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
  3072. if (lock_f) {
  3073. msleep(5);
  3074. for (i = 0; i < 16; i++) {
  3075. reg_1 = STV090x_READ_DEMOD(state, NOSDATAT1);
  3076. val_1 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
  3077. reg_0 = STV090x_READ_DEMOD(state, NOSDATAT0);
  3078. val_0 = STV090x_GETFIELD_Px(reg_0, NOSDATAT_UNNORMED_FIELD);
  3079. val += MAKEWORD16(val_1, val_0);
  3080. msleep(1);
  3081. }
  3082. val /= 16;
  3083. last = ARRAY_SIZE(stv090x_s1cn_tab) - 1;
  3084. div = stv090x_s1cn_tab[0].read -
  3085. stv090x_s1cn_tab[last].read;
  3086. *cnr = 0xFFFF - ((val * 0xFFFF) / div);
  3087. }
  3088. break;
  3089. default:
  3090. break;
  3091. }
  3092. return 0;
  3093. }
  3094. static int stv090x_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  3095. {
  3096. struct stv090x_state *state = fe->demodulator_priv;
  3097. u32 reg;
  3098. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3099. switch (tone) {
  3100. case SEC_TONE_ON:
  3101. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
  3102. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3103. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3104. goto err;
  3105. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  3106. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3107. goto err;
  3108. break;
  3109. case SEC_TONE_OFF:
  3110. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
  3111. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3112. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3113. goto err;
  3114. break;
  3115. default:
  3116. return -EINVAL;
  3117. }
  3118. return 0;
  3119. err:
  3120. dprintk(FE_ERROR, 1, "I/O error");
  3121. return -1;
  3122. }
  3123. static enum dvbfe_algo stv090x_frontend_algo(struct dvb_frontend *fe)
  3124. {
  3125. return DVBFE_ALGO_CUSTOM;
  3126. }
  3127. static int stv090x_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
  3128. {
  3129. struct stv090x_state *state = fe->demodulator_priv;
  3130. u32 reg, idle = 0, fifo_full = 1;
  3131. int i;
  3132. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3133. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 2);
  3134. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3135. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3136. goto err;
  3137. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  3138. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3139. goto err;
  3140. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
  3141. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3142. goto err;
  3143. for (i = 0; i < cmd->msg_len; i++) {
  3144. while (fifo_full) {
  3145. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3146. fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
  3147. }
  3148. if (STV090x_WRITE_DEMOD(state, DISTXDATA, cmd->msg[i]) < 0)
  3149. goto err;
  3150. }
  3151. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3152. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
  3153. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3154. goto err;
  3155. i = 0;
  3156. while ((!idle) && (i < 10)) {
  3157. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3158. idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
  3159. msleep(10);
  3160. i++;
  3161. }
  3162. return 0;
  3163. err:
  3164. dprintk(FE_ERROR, 1, "I/O error");
  3165. return -1;
  3166. }
  3167. static int stv090x_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
  3168. {
  3169. struct stv090x_state *state = fe->demodulator_priv;
  3170. u32 reg, idle = 0, fifo_full = 1;
  3171. u8 mode, value;
  3172. int i;
  3173. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3174. if (burst == SEC_MINI_A) {
  3175. mode = 3;
  3176. value = 0x00;
  3177. } else {
  3178. mode = 2;
  3179. value = 0xFF;
  3180. }
  3181. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, mode);
  3182. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3183. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3184. goto err;
  3185. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  3186. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3187. goto err;
  3188. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
  3189. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3190. goto err;
  3191. while (fifo_full) {
  3192. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3193. fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
  3194. }
  3195. if (STV090x_WRITE_DEMOD(state, DISTXDATA, value) < 0)
  3196. goto err;
  3197. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3198. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
  3199. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3200. goto err;
  3201. i = 0;
  3202. while ((!idle) && (i < 10)) {
  3203. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3204. idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
  3205. msleep(10);
  3206. i++;
  3207. }
  3208. return 0;
  3209. err:
  3210. dprintk(FE_ERROR, 1, "I/O error");
  3211. return -1;
  3212. }
  3213. static int stv090x_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
  3214. {
  3215. struct stv090x_state *state = fe->demodulator_priv;
  3216. u32 reg = 0, i = 0, rx_end = 0;
  3217. while ((rx_end != 1) && (i < 10)) {
  3218. msleep(10);
  3219. i++;
  3220. reg = STV090x_READ_DEMOD(state, DISRX_ST0);
  3221. rx_end = STV090x_GETFIELD_Px(reg, RX_END_FIELD);
  3222. }
  3223. if (rx_end) {
  3224. reply->msg_len = STV090x_GETFIELD_Px(reg, FIFO_BYTENBR_FIELD);
  3225. for (i = 0; i < reply->msg_len; i++)
  3226. reply->msg[i] = STV090x_READ_DEMOD(state, DISRXDATA);
  3227. }
  3228. return 0;
  3229. }
  3230. static int stv090x_sleep(struct dvb_frontend *fe)
  3231. {
  3232. struct stv090x_state *state = fe->demodulator_priv;
  3233. u32 reg;
  3234. dprintk(FE_DEBUG, 1, "Set %s to sleep",
  3235. state->device == STV0900 ? "STV0900" : "STV0903");
  3236. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3237. STV090x_SETFIELD(reg, STANDBY_FIELD, 0x01);
  3238. if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
  3239. goto err;
  3240. reg = stv090x_read_reg(state, STV090x_TSTTNR1);
  3241. STV090x_SETFIELD(reg, ADC1_PON_FIELD, 0);
  3242. if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
  3243. goto err;
  3244. return 0;
  3245. err:
  3246. dprintk(FE_ERROR, 1, "I/O error");
  3247. return -1;
  3248. }
  3249. static int stv090x_wakeup(struct dvb_frontend *fe)
  3250. {
  3251. struct stv090x_state *state = fe->demodulator_priv;
  3252. u32 reg;
  3253. dprintk(FE_DEBUG, 1, "Wake %s from standby",
  3254. state->device == STV0900 ? "STV0900" : "STV0903");
  3255. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3256. STV090x_SETFIELD(reg, STANDBY_FIELD, 0x00);
  3257. if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
  3258. goto err;
  3259. reg = stv090x_read_reg(state, STV090x_TSTTNR1);
  3260. STV090x_SETFIELD(reg, ADC1_PON_FIELD, 1);
  3261. if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
  3262. goto err;
  3263. return 0;
  3264. err:
  3265. dprintk(FE_ERROR, 1, "I/O error");
  3266. return -1;
  3267. }
  3268. static void stv090x_release(struct dvb_frontend *fe)
  3269. {
  3270. struct stv090x_state *state = fe->demodulator_priv;
  3271. kfree(state);
  3272. }
  3273. static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc_mode)
  3274. {
  3275. u32 reg = 0;
  3276. switch (ldpc_mode) {
  3277. case STV090x_DUAL:
  3278. default:
  3279. if ((state->demod_mode != STV090x_DUAL) || (STV090x_GETFIELD(reg, DDEMOD_FIELD) != 1)) {
  3280. /* set LDPC to dual mode */
  3281. if (stv090x_write_reg(state, STV090x_GENCFG, 0x1d) < 0)
  3282. goto err;
  3283. state->demod_mode = STV090x_DUAL;
  3284. reg = stv090x_read_reg(state, STV090x_TSTRES0);
  3285. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
  3286. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3287. goto err;
  3288. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
  3289. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3290. goto err;
  3291. if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
  3292. goto err;
  3293. if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
  3294. goto err;
  3295. if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
  3296. goto err;
  3297. if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
  3298. goto err;
  3299. if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
  3300. goto err;
  3301. if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
  3302. goto err;
  3303. if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
  3304. goto err;
  3305. if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
  3306. goto err;
  3307. if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
  3308. goto err;
  3309. if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
  3310. goto err;
  3311. if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
  3312. goto err;
  3313. if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
  3314. goto err;
  3315. if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
  3316. goto err;
  3317. if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
  3318. goto err;
  3319. if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
  3320. goto err;
  3321. if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
  3322. goto err;
  3323. }
  3324. break;
  3325. case STV090x_SINGLE:
  3326. if (stv090x_stop_modcod(state) < 0)
  3327. goto err;
  3328. if (stv090x_activate_modcod_single(state) < 0)
  3329. goto err;
  3330. if (state->demod == STV090x_DEMODULATOR_1) {
  3331. if (stv090x_write_reg(state, STV090x_GENCFG, 0x06) < 0) /* path 2 */
  3332. goto err;
  3333. } else {
  3334. if (stv090x_write_reg(state, STV090x_GENCFG, 0x04) < 0) /* path 1 */
  3335. goto err;
  3336. }
  3337. reg = stv090x_read_reg(state, STV090x_TSTRES0);
  3338. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
  3339. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3340. goto err;
  3341. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
  3342. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3343. goto err;
  3344. reg = STV090x_READ_DEMOD(state, PDELCTRL1);
  3345. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x01);
  3346. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  3347. goto err;
  3348. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x00);
  3349. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  3350. goto err;
  3351. break;
  3352. }
  3353. return 0;
  3354. err:
  3355. dprintk(FE_ERROR, 1, "I/O error");
  3356. return -1;
  3357. }
  3358. /* return (Hz), clk in Hz*/
  3359. static u32 stv090x_get_mclk(struct stv090x_state *state)
  3360. {
  3361. const struct stv090x_config *config = state->config;
  3362. u32 div, reg;
  3363. u8 ratio;
  3364. div = stv090x_read_reg(state, STV090x_NCOARSE);
  3365. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3366. ratio = STV090x_GETFIELD(reg, SELX1RATIO_FIELD) ? 4 : 6;
  3367. return (div + 1) * config->xtal / ratio; /* kHz */
  3368. }
  3369. static int stv090x_set_mclk(struct stv090x_state *state, u32 mclk, u32 clk)
  3370. {
  3371. const struct stv090x_config *config = state->config;
  3372. u32 reg, div, clk_sel;
  3373. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3374. clk_sel = ((STV090x_GETFIELD(reg, SELX1RATIO_FIELD) == 1) ? 4 : 6);
  3375. div = ((clk_sel * mclk) / config->xtal) - 1;
  3376. reg = stv090x_read_reg(state, STV090x_NCOARSE);
  3377. STV090x_SETFIELD(reg, M_DIV_FIELD, div);
  3378. if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0)
  3379. goto err;
  3380. state->mclk = stv090x_get_mclk(state);
  3381. /*Set the DiseqC frequency to 22KHz */
  3382. div = state->mclk / 704000;
  3383. if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0)
  3384. goto err;
  3385. if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
  3386. goto err;
  3387. return 0;
  3388. err:
  3389. dprintk(FE_ERROR, 1, "I/O error");
  3390. return -1;
  3391. }
  3392. static int stv090x_set_tspath(struct stv090x_state *state)
  3393. {
  3394. u32 reg;
  3395. if (state->dev_ver >= 0x20) {
  3396. switch (state->config->ts1_mode) {
  3397. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3398. case STV090x_TSMODE_DVBCI:
  3399. switch (state->config->ts2_mode) {
  3400. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3401. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3402. default:
  3403. stv090x_write_reg(state, STV090x_TSGENERAL, 0x00);
  3404. break;
  3405. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3406. case STV090x_TSMODE_DVBCI:
  3407. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x06) < 0) /* Mux'd stream mode */
  3408. goto err;
  3409. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3410. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3411. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3412. goto err;
  3413. reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
  3414. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3415. if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
  3416. goto err;
  3417. if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
  3418. goto err;
  3419. if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
  3420. goto err;
  3421. break;
  3422. }
  3423. break;
  3424. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3425. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3426. default:
  3427. switch (state->config->ts2_mode) {
  3428. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3429. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3430. default:
  3431. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
  3432. goto err;
  3433. break;
  3434. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3435. case STV090x_TSMODE_DVBCI:
  3436. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0a) < 0)
  3437. goto err;
  3438. break;
  3439. }
  3440. break;
  3441. }
  3442. } else {
  3443. switch (state->config->ts1_mode) {
  3444. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3445. case STV090x_TSMODE_DVBCI:
  3446. switch (state->config->ts2_mode) {
  3447. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3448. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3449. default:
  3450. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x10);
  3451. break;
  3452. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3453. case STV090x_TSMODE_DVBCI:
  3454. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x16);
  3455. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3456. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3457. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3458. goto err;
  3459. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3460. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 0);
  3461. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3462. goto err;
  3463. if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
  3464. goto err;
  3465. if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
  3466. goto err;
  3467. break;
  3468. }
  3469. break;
  3470. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3471. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3472. default:
  3473. switch (state->config->ts2_mode) {
  3474. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3475. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3476. default:
  3477. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
  3478. break;
  3479. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3480. case STV090x_TSMODE_DVBCI:
  3481. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x12);
  3482. break;
  3483. }
  3484. break;
  3485. }
  3486. }
  3487. switch (state->config->ts1_mode) {
  3488. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3489. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3490. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3491. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3492. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3493. goto err;
  3494. break;
  3495. case STV090x_TSMODE_DVBCI:
  3496. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3497. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3498. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3499. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3500. goto err;
  3501. break;
  3502. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3503. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3504. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3505. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3506. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3507. goto err;
  3508. break;
  3509. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3510. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3511. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3512. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3513. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3514. goto err;
  3515. break;
  3516. default:
  3517. break;
  3518. }
  3519. switch (state->config->ts2_mode) {
  3520. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3521. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3522. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3523. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3524. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3525. goto err;
  3526. break;
  3527. case STV090x_TSMODE_DVBCI:
  3528. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3529. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3530. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3531. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3532. goto err;
  3533. break;
  3534. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3535. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3536. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3537. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3538. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3539. goto err;
  3540. break;
  3541. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3542. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3543. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3544. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3545. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3546. goto err;
  3547. break;
  3548. default:
  3549. break;
  3550. }
  3551. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3552. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
  3553. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3554. goto err;
  3555. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
  3556. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3557. goto err;
  3558. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3559. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
  3560. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3561. goto err;
  3562. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
  3563. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3564. goto err;
  3565. return 0;
  3566. err:
  3567. dprintk(FE_ERROR, 1, "I/O error");
  3568. return -1;
  3569. }
  3570. static int stv090x_init(struct dvb_frontend *fe)
  3571. {
  3572. struct stv090x_state *state = fe->demodulator_priv;
  3573. const struct stv090x_config *config = state->config;
  3574. u32 reg;
  3575. if (stv090x_wakeup(fe) < 0) {
  3576. dprintk(FE_ERROR, 1, "Error waking device");
  3577. goto err;
  3578. }
  3579. if (stv090x_ldpc_mode(state, state->demod_mode) < 0)
  3580. goto err;
  3581. reg = STV090x_READ_DEMOD(state, TNRCFG2);
  3582. STV090x_SETFIELD_Px(reg, TUN_IQSWAP_FIELD, state->inversion);
  3583. if (STV090x_WRITE_DEMOD(state, TNRCFG2, reg) < 0)
  3584. goto err;
  3585. reg = STV090x_READ_DEMOD(state, DEMOD);
  3586. STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
  3587. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  3588. goto err;
  3589. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  3590. goto err;
  3591. if (config->tuner_set_mode) {
  3592. if (config->tuner_set_mode(fe, TUNER_WAKE) < 0)
  3593. goto err;
  3594. }
  3595. if (config->tuner_init) {
  3596. if (config->tuner_init(fe) < 0)
  3597. goto err;
  3598. }
  3599. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  3600. goto err;
  3601. if (stv090x_set_tspath(state) < 0)
  3602. goto err;
  3603. return 0;
  3604. err:
  3605. dprintk(FE_ERROR, 1, "I/O error");
  3606. return -1;
  3607. }
  3608. static int stv090x_setup(struct dvb_frontend *fe)
  3609. {
  3610. struct stv090x_state *state = fe->demodulator_priv;
  3611. const struct stv090x_config *config = state->config;
  3612. const struct stv090x_reg *stv090x_initval = NULL;
  3613. const struct stv090x_reg *stv090x_cut20_val = NULL;
  3614. unsigned long t1_size = 0, t2_size = 0;
  3615. u32 reg = 0;
  3616. int i;
  3617. if (state->device == STV0900) {
  3618. dprintk(FE_DEBUG, 1, "Initializing STV0900");
  3619. stv090x_initval = stv0900_initval;
  3620. t1_size = ARRAY_SIZE(stv0900_initval);
  3621. stv090x_cut20_val = stv0900_cut20_val;
  3622. t2_size = ARRAY_SIZE(stv0900_cut20_val);
  3623. } else if (state->device == STV0903) {
  3624. dprintk(FE_DEBUG, 1, "Initializing STV0903");
  3625. stv090x_initval = stv0903_initval;
  3626. t1_size = ARRAY_SIZE(stv0903_initval);
  3627. stv090x_cut20_val = stv0903_cut20_val;
  3628. t2_size = ARRAY_SIZE(stv0903_cut20_val);
  3629. }
  3630. /* STV090x init */
  3631. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Stop Demod */
  3632. goto err;
  3633. msleep(5);
  3634. if (STV090x_WRITE_DEMOD(state, TNRCFG, 0x6c) < 0) /* check register ! (No Tuner Mode) */
  3635. goto err;
  3636. STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level);
  3637. if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0) /* repeater OFF */
  3638. goto err;
  3639. if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
  3640. goto err;
  3641. msleep(5);
  3642. if (stv090x_write_reg(state, STV090x_I2CCFG, 0x08) < 0) /* 1/41 oversampling */
  3643. goto err;
  3644. if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0) /* enable PLL */
  3645. goto err;
  3646. msleep(5);
  3647. /* write initval */
  3648. dprintk(FE_DEBUG, 1, "Setting up initial values");
  3649. for (i = 0; i < t1_size; i++) {
  3650. if (stv090x_write_reg(state, stv090x_initval[i].addr, stv090x_initval[i].data) < 0)
  3651. goto err;
  3652. }
  3653. state->dev_ver = stv090x_read_reg(state, STV090x_MID);
  3654. if (state->dev_ver >= 0x20) {
  3655. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
  3656. goto err;
  3657. /* write cut20_val*/
  3658. dprintk(FE_DEBUG, 1, "Setting up Cut 2.0 initial values");
  3659. for (i = 0; i < t2_size; i++) {
  3660. if (stv090x_write_reg(state, stv090x_cut20_val[i].addr, stv090x_cut20_val[i].data) < 0)
  3661. goto err;
  3662. }
  3663. } else if (state->dev_ver < 0x20) {
  3664. dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!",
  3665. state->dev_ver);
  3666. goto err;
  3667. } else if (state->dev_ver > 0x30) {
  3668. /* we shouldn't bail out from here */
  3669. dprintk(FE_ERROR, 1, "INFO: Cut: 0x%02x probably incomplete support!",
  3670. state->dev_ver);
  3671. }
  3672. if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
  3673. goto err;
  3674. if (stv090x_write_reg(state, STV090x_TSTRES0, 0x00) < 0)
  3675. goto err;
  3676. stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
  3677. msleep(5);
  3678. if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0)
  3679. goto err;
  3680. stv090x_get_mclk(state);
  3681. return 0;
  3682. err:
  3683. dprintk(FE_ERROR, 1, "I/O error");
  3684. return -1;
  3685. }
  3686. static struct dvb_frontend_ops stv090x_ops = {
  3687. .info = {
  3688. .name = "STV090x Multistandard",
  3689. .type = FE_QPSK,
  3690. .frequency_min = 950000,
  3691. .frequency_max = 2150000,
  3692. .frequency_stepsize = 0,
  3693. .frequency_tolerance = 0,
  3694. .symbol_rate_min = 1000000,
  3695. .symbol_rate_max = 45000000,
  3696. .caps = FE_CAN_INVERSION_AUTO |
  3697. FE_CAN_FEC_AUTO |
  3698. FE_CAN_QPSK |
  3699. FE_CAN_2G_MODULATION
  3700. },
  3701. .release = stv090x_release,
  3702. .init = stv090x_init,
  3703. .sleep = stv090x_sleep,
  3704. .get_frontend_algo = stv090x_frontend_algo,
  3705. .i2c_gate_ctrl = stv090x_i2c_gate_ctrl,
  3706. .diseqc_send_master_cmd = stv090x_send_diseqc_msg,
  3707. .diseqc_send_burst = stv090x_send_diseqc_burst,
  3708. .diseqc_recv_slave_reply = stv090x_recv_slave_reply,
  3709. .set_tone = stv090x_set_tone,
  3710. .search = stv090x_search,
  3711. .read_status = stv090x_read_status,
  3712. .read_ber = stv090x_read_per,
  3713. .read_signal_strength = stv090x_read_signal_strength,
  3714. .read_snr = stv090x_read_cnr
  3715. };
  3716. struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
  3717. struct i2c_adapter *i2c,
  3718. enum stv090x_demodulator demod)
  3719. {
  3720. struct stv090x_state *state = NULL;
  3721. state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL);
  3722. if (state == NULL)
  3723. goto error;
  3724. state->verbose = &verbose;
  3725. state->config = config;
  3726. state->i2c = i2c;
  3727. state->frontend.ops = stv090x_ops;
  3728. state->frontend.demodulator_priv = state;
  3729. state->demod = demod;
  3730. state->demod_mode = config->demod_mode; /* Single or Dual mode */
  3731. state->device = config->device;
  3732. state->rolloff = STV090x_RO_35; /* default */
  3733. if (state->demod == STV090x_DEMODULATOR_0)
  3734. mutex_init(&demod_lock);
  3735. if (stv090x_sleep(&state->frontend) < 0) {
  3736. dprintk(FE_ERROR, 1, "Error putting device to sleep");
  3737. goto error;
  3738. }
  3739. if (stv090x_setup(&state->frontend) < 0) {
  3740. dprintk(FE_ERROR, 1, "Error setting up device");
  3741. goto error;
  3742. }
  3743. if (stv090x_wakeup(&state->frontend) < 0) {
  3744. dprintk(FE_ERROR, 1, "Error waking device");
  3745. goto error;
  3746. }
  3747. dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x\n",
  3748. state->device == STV0900 ? "STV0900" : "STV0903",
  3749. demod,
  3750. state->dev_ver);
  3751. return &state->frontend;
  3752. error:
  3753. kfree(state);
  3754. return NULL;
  3755. }
  3756. EXPORT_SYMBOL(stv090x_attach);
  3757. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  3758. MODULE_AUTHOR("Manu Abraham");
  3759. MODULE_DESCRIPTION("STV090x Multi-Std Broadcast frontend");
  3760. MODULE_LICENSE("GPL");