stv090x.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302
  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) & 0xff)) < 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, CFRUP1, 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, CFRUP1, 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_1 * 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->algo == 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. state->search_range = 2000000;
  2888. if (stv090x_algo(state) == STV090x_RANGEOK) {
  2889. dprintk(FE_DEBUG, 1, "Search success!");
  2890. return DVBFE_ALGO_SEARCH_SUCCESS;
  2891. } else {
  2892. dprintk(FE_DEBUG, 1, "Search failed!");
  2893. return DVBFE_ALGO_SEARCH_FAILED;
  2894. }
  2895. return DVBFE_ALGO_SEARCH_ERROR;
  2896. }
  2897. /* FIXME! */
  2898. static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
  2899. {
  2900. struct stv090x_state *state = fe->demodulator_priv;
  2901. u32 reg;
  2902. u8 search_state;
  2903. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  2904. search_state = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
  2905. switch (search_state) {
  2906. case 0: /* searching */
  2907. case 1: /* first PLH detected */
  2908. default:
  2909. dprintk(FE_DEBUG, 1, "Status: Unlocked (Searching ..)");
  2910. *status = 0;
  2911. break;
  2912. case 2: /* DVB-S2 mode */
  2913. dprintk(FE_DEBUG, 1, "Delivery system: DVB-S2");
  2914. reg = STV090x_READ_DEMOD(state, DSTATUS);
  2915. if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
  2916. reg = STV090x_READ_DEMOD(state, TSSTATUS);
  2917. if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
  2918. *status = FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  2919. }
  2920. }
  2921. break;
  2922. case 3: /* DVB-S1/legacy mode */
  2923. dprintk(FE_DEBUG, 1, "Delivery system: DVB-S");
  2924. reg = STV090x_READ_DEMOD(state, DSTATUS);
  2925. if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
  2926. reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
  2927. if (STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD)) {
  2928. reg = STV090x_READ_DEMOD(state, TSSTATUS);
  2929. if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
  2930. *status = FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  2931. }
  2932. }
  2933. }
  2934. break;
  2935. }
  2936. return 0;
  2937. }
  2938. static int stv090x_read_per(struct dvb_frontend *fe, u32 *per)
  2939. {
  2940. struct stv090x_state *state = fe->demodulator_priv;
  2941. s32 count_4, count_3, count_2, count_1, count_0, count;
  2942. u32 reg, h, m, l;
  2943. enum fe_status status;
  2944. stv090x_read_status(fe, &status);
  2945. if (!(status & FE_HAS_LOCK)) {
  2946. *per = 1 << 23; /* Max PER */
  2947. } else {
  2948. /* Counter 2 */
  2949. reg = STV090x_READ_DEMOD(state, ERRCNT22);
  2950. h = STV090x_GETFIELD_Px(reg, ERR_CNT2_FIELD);
  2951. reg = STV090x_READ_DEMOD(state, ERRCNT21);
  2952. m = STV090x_GETFIELD_Px(reg, ERR_CNT21_FIELD);
  2953. reg = STV090x_READ_DEMOD(state, ERRCNT20);
  2954. l = STV090x_GETFIELD_Px(reg, ERR_CNT20_FIELD);
  2955. *per = ((h << 16) | (m << 8) | l);
  2956. count_4 = STV090x_READ_DEMOD(state, FBERCPT4);
  2957. count_3 = STV090x_READ_DEMOD(state, FBERCPT3);
  2958. count_2 = STV090x_READ_DEMOD(state, FBERCPT2);
  2959. count_1 = STV090x_READ_DEMOD(state, FBERCPT1);
  2960. count_0 = STV090x_READ_DEMOD(state, FBERCPT0);
  2961. if ((!count_4) && (!count_3)) {
  2962. count = (count_2 & 0xff) << 16;
  2963. count |= (count_1 & 0xff) << 8;
  2964. count |= count_0 & 0xff;
  2965. } else {
  2966. count = 1 << 24;
  2967. }
  2968. if (count == 0)
  2969. *per = 1;
  2970. }
  2971. if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0) < 0)
  2972. goto err;
  2973. if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
  2974. goto err;
  2975. return 0;
  2976. err:
  2977. dprintk(FE_ERROR, 1, "I/O error");
  2978. return -1;
  2979. }
  2980. static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
  2981. {
  2982. int res = 0;
  2983. int min = 0, med;
  2984. if (val < tab[min].read)
  2985. res = tab[min].real;
  2986. else if (val >= tab[max].read)
  2987. res = tab[max].real;
  2988. else {
  2989. while ((max - min) > 1) {
  2990. med = (max + min) / 2;
  2991. if (val >= tab[min].read && val < tab[med].read)
  2992. max = med;
  2993. else
  2994. min = med;
  2995. }
  2996. res = ((val - tab[min].read) *
  2997. (tab[max].real - tab[min].real) /
  2998. (tab[max].read - tab[min].read)) +
  2999. tab[min].real;
  3000. }
  3001. return res;
  3002. }
  3003. static int stv090x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  3004. {
  3005. struct stv090x_state *state = fe->demodulator_priv;
  3006. u32 reg;
  3007. s32 agc;
  3008. reg = STV090x_READ_DEMOD(state, AGCIQIN1);
  3009. agc = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
  3010. *strength = stv090x_table_lookup(stv090x_rf_tab, ARRAY_SIZE(stv090x_rf_tab) - 1, agc);
  3011. if (agc > stv090x_rf_tab[0].read)
  3012. *strength = 5;
  3013. else if (agc < stv090x_rf_tab[ARRAY_SIZE(stv090x_rf_tab) - 1].read)
  3014. *strength = -100;
  3015. return 0;
  3016. }
  3017. static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
  3018. {
  3019. struct stv090x_state *state = fe->demodulator_priv;
  3020. u32 reg_0, reg_1, reg, i;
  3021. s32 val_0, val_1, val = 0;
  3022. u8 lock_f;
  3023. switch (state->delsys) {
  3024. case STV090x_DVBS2:
  3025. reg = STV090x_READ_DEMOD(state, DSTATUS);
  3026. lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
  3027. if (lock_f) {
  3028. msleep(5);
  3029. for (i = 0; i < 16; i++) {
  3030. reg_1 = STV090x_READ_DEMOD(state, NNOSPLHT1);
  3031. val_1 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
  3032. reg_0 = STV090x_READ_DEMOD(state, NNOSPLHT0);
  3033. val_0 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
  3034. val += MAKEWORD16(val_1, val_0);
  3035. msleep(1);
  3036. }
  3037. val /= 16;
  3038. *cnr = stv090x_table_lookup(stv090x_s2cn_tab, ARRAY_SIZE(stv090x_s2cn_tab) - 1, val);
  3039. if (val < stv090x_s2cn_tab[ARRAY_SIZE(stv090x_s2cn_tab) - 1].read)
  3040. *cnr = 1000;
  3041. }
  3042. break;
  3043. case STV090x_DVBS1:
  3044. case STV090x_DSS:
  3045. reg = STV090x_READ_DEMOD(state, DSTATUS);
  3046. lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
  3047. if (lock_f) {
  3048. msleep(5);
  3049. for (i = 0; i < 16; i++) {
  3050. reg_1 = STV090x_READ_DEMOD(state, NOSDATAT1);
  3051. val_1 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
  3052. reg_0 = STV090x_READ_DEMOD(state, NOSDATAT0);
  3053. val_0 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
  3054. val += MAKEWORD16(val_1, val_0);
  3055. msleep(1);
  3056. }
  3057. val /= 16;
  3058. *cnr = stv090x_table_lookup(stv090x_s1cn_tab, ARRAY_SIZE(stv090x_s1cn_tab) - 1, val);
  3059. if (val < stv090x_s2cn_tab[ARRAY_SIZE(stv090x_s1cn_tab) - 1].read)
  3060. *cnr = 1000;
  3061. }
  3062. break;
  3063. default:
  3064. break;
  3065. }
  3066. return 0;
  3067. }
  3068. static int stv090x_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  3069. {
  3070. struct stv090x_state *state = fe->demodulator_priv;
  3071. u32 reg;
  3072. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3073. switch (tone) {
  3074. case SEC_TONE_ON:
  3075. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
  3076. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3077. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3078. goto err;
  3079. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  3080. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3081. goto err;
  3082. break;
  3083. case SEC_TONE_OFF:
  3084. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
  3085. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3086. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3087. goto err;
  3088. break;
  3089. default:
  3090. return -EINVAL;
  3091. }
  3092. return 0;
  3093. err:
  3094. dprintk(FE_ERROR, 1, "I/O error");
  3095. return -1;
  3096. }
  3097. static enum dvbfe_algo stv090x_frontend_algo(struct dvb_frontend *fe)
  3098. {
  3099. return DVBFE_ALGO_CUSTOM;
  3100. }
  3101. static int stv090x_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
  3102. {
  3103. struct stv090x_state *state = fe->demodulator_priv;
  3104. u32 reg, idle = 0, fifo_full = 1;
  3105. int i;
  3106. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3107. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 2);
  3108. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3109. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3110. goto err;
  3111. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  3112. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3113. goto err;
  3114. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
  3115. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3116. goto err;
  3117. for (i = 0; i < cmd->msg_len; i++) {
  3118. while (fifo_full) {
  3119. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3120. fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
  3121. }
  3122. if (STV090x_WRITE_DEMOD(state, DISTXDATA, cmd->msg[i]) < 0)
  3123. goto err;
  3124. }
  3125. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3126. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
  3127. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3128. goto err;
  3129. i = 0;
  3130. while ((!idle) && (i < 10)) {
  3131. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3132. idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
  3133. msleep(10);
  3134. i++;
  3135. }
  3136. return 0;
  3137. err:
  3138. dprintk(FE_ERROR, 1, "I/O error");
  3139. return -1;
  3140. }
  3141. static int stv090x_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
  3142. {
  3143. struct stv090x_state *state = fe->demodulator_priv;
  3144. u32 reg, idle = 0, fifo_full = 1;
  3145. u8 mode, value;
  3146. int i;
  3147. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3148. if (burst == SEC_MINI_A) {
  3149. mode = 3;
  3150. value = 0x00;
  3151. } else {
  3152. mode = 2;
  3153. value = 0xFF;
  3154. }
  3155. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, mode);
  3156. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3157. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3158. goto err;
  3159. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  3160. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3161. goto err;
  3162. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
  3163. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3164. goto err;
  3165. while (fifo_full) {
  3166. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3167. fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
  3168. }
  3169. if (STV090x_WRITE_DEMOD(state, DISTXDATA, value) < 0)
  3170. goto err;
  3171. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3172. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
  3173. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3174. goto err;
  3175. i = 0;
  3176. while ((!idle) && (i < 10)) {
  3177. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3178. idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
  3179. msleep(10);
  3180. i++;
  3181. }
  3182. return 0;
  3183. err:
  3184. dprintk(FE_ERROR, 1, "I/O error");
  3185. return -1;
  3186. }
  3187. static int stv090x_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
  3188. {
  3189. struct stv090x_state *state = fe->demodulator_priv;
  3190. u32 reg = 0, i = 0, rx_end = 0;
  3191. while ((rx_end != 1) && (i < 10)) {
  3192. msleep(10);
  3193. i++;
  3194. reg = STV090x_READ_DEMOD(state, DISRX_ST0);
  3195. rx_end = STV090x_GETFIELD_Px(reg, RX_END_FIELD);
  3196. }
  3197. if (rx_end) {
  3198. reply->msg_len = STV090x_GETFIELD_Px(reg, FIFO_BYTENBR_FIELD);
  3199. for (i = 0; i < reply->msg_len; i++)
  3200. reply->msg[i] = STV090x_READ_DEMOD(state, DISRXDATA);
  3201. }
  3202. return 0;
  3203. }
  3204. static int stv090x_sleep(struct dvb_frontend *fe)
  3205. {
  3206. struct stv090x_state *state = fe->demodulator_priv;
  3207. u32 reg;
  3208. dprintk(FE_DEBUG, 1, "Set %s to sleep",
  3209. state->device == STV0900 ? "STV0900" : "STV0903");
  3210. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3211. STV090x_SETFIELD(reg, STANDBY_FIELD, 0x01);
  3212. if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
  3213. goto err;
  3214. reg = stv090x_read_reg(state, STV090x_TSTTNR1);
  3215. STV090x_SETFIELD(reg, ADC1_PON_FIELD, 0);
  3216. if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
  3217. goto err;
  3218. return 0;
  3219. err:
  3220. dprintk(FE_ERROR, 1, "I/O error");
  3221. return -1;
  3222. }
  3223. static int stv090x_wakeup(struct dvb_frontend *fe)
  3224. {
  3225. struct stv090x_state *state = fe->demodulator_priv;
  3226. u32 reg;
  3227. dprintk(FE_DEBUG, 1, "Wake %s from standby",
  3228. state->device == STV0900 ? "STV0900" : "STV0903");
  3229. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3230. STV090x_SETFIELD(reg, STANDBY_FIELD, 0x00);
  3231. if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
  3232. goto err;
  3233. reg = stv090x_read_reg(state, STV090x_TSTTNR1);
  3234. STV090x_SETFIELD(reg, ADC1_PON_FIELD, 1);
  3235. if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
  3236. goto err;
  3237. return 0;
  3238. err:
  3239. dprintk(FE_ERROR, 1, "I/O error");
  3240. return -1;
  3241. }
  3242. static void stv090x_release(struct dvb_frontend *fe)
  3243. {
  3244. struct stv090x_state *state = fe->demodulator_priv;
  3245. kfree(state);
  3246. }
  3247. static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc_mode)
  3248. {
  3249. u32 reg = 0;
  3250. switch (ldpc_mode) {
  3251. case STV090x_DUAL:
  3252. default:
  3253. if ((state->demod_mode != STV090x_DUAL) || (STV090x_GETFIELD(reg, DDEMOD_FIELD) != 1)) {
  3254. /* set LDPC to dual mode */
  3255. if (stv090x_write_reg(state, STV090x_GENCFG, 0x1d) < 0)
  3256. goto err;
  3257. state->demod_mode = STV090x_DUAL;
  3258. reg = stv090x_read_reg(state, STV090x_TSTRES0);
  3259. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
  3260. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3261. goto err;
  3262. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
  3263. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3264. goto err;
  3265. if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
  3266. goto err;
  3267. if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
  3268. goto err;
  3269. if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
  3270. goto err;
  3271. if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
  3272. goto err;
  3273. if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
  3274. goto err;
  3275. if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
  3276. goto err;
  3277. if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
  3278. goto err;
  3279. if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
  3280. goto err;
  3281. if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
  3282. goto err;
  3283. if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
  3284. goto err;
  3285. if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
  3286. goto err;
  3287. if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
  3288. goto err;
  3289. if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
  3290. goto err;
  3291. if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
  3292. goto err;
  3293. if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
  3294. goto err;
  3295. if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
  3296. goto err;
  3297. }
  3298. break;
  3299. case STV090x_SINGLE:
  3300. if (stv090x_stop_modcod(state) < 0)
  3301. goto err;
  3302. if (stv090x_activate_modcod_single(state) < 0)
  3303. goto err;
  3304. if (state->demod == STV090x_DEMODULATOR_1) {
  3305. if (stv090x_write_reg(state, STV090x_GENCFG, 0x06) < 0) /* path 2 */
  3306. goto err;
  3307. } else {
  3308. if (stv090x_write_reg(state, STV090x_GENCFG, 0x04) < 0) /* path 1 */
  3309. goto err;
  3310. }
  3311. reg = stv090x_read_reg(state, STV090x_TSTRES0);
  3312. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
  3313. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3314. goto err;
  3315. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
  3316. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3317. goto err;
  3318. reg = STV090x_READ_DEMOD(state, PDELCTRL1);
  3319. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x01);
  3320. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  3321. goto err;
  3322. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x00);
  3323. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  3324. goto err;
  3325. break;
  3326. }
  3327. return 0;
  3328. err:
  3329. dprintk(FE_ERROR, 1, "I/O error");
  3330. return -1;
  3331. }
  3332. /* return (Hz), clk in Hz*/
  3333. static u32 stv090x_get_mclk(struct stv090x_state *state)
  3334. {
  3335. const struct stv090x_config *config = state->config;
  3336. u32 div, reg;
  3337. u8 ratio;
  3338. div = stv090x_read_reg(state, STV090x_NCOARSE);
  3339. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3340. ratio = STV090x_GETFIELD(reg, SELX1RATIO_FIELD) ? 4 : 6;
  3341. return (div + 1) * config->xtal / ratio; /* kHz */
  3342. }
  3343. static int stv090x_set_mclk(struct stv090x_state *state, u32 mclk, u32 clk)
  3344. {
  3345. const struct stv090x_config *config = state->config;
  3346. u32 reg, div, clk_sel;
  3347. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3348. clk_sel = ((STV090x_GETFIELD(reg, SELX1RATIO_FIELD) == 1) ? 4 : 6);
  3349. div = ((clk_sel * mclk) / config->xtal) - 1;
  3350. reg = stv090x_read_reg(state, STV090x_NCOARSE);
  3351. STV090x_SETFIELD(reg, M_DIV_FIELD, div);
  3352. if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0)
  3353. goto err;
  3354. state->mclk = stv090x_get_mclk(state);
  3355. /*Set the DiseqC frequency to 22KHz */
  3356. div = state->mclk / 704000;
  3357. if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0)
  3358. goto err;
  3359. if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
  3360. goto err;
  3361. return 0;
  3362. err:
  3363. dprintk(FE_ERROR, 1, "I/O error");
  3364. return -1;
  3365. }
  3366. static int stv090x_set_tspath(struct stv090x_state *state)
  3367. {
  3368. u32 reg;
  3369. if (state->dev_ver >= 0x20) {
  3370. switch (state->config->ts1_mode) {
  3371. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3372. case STV090x_TSMODE_DVBCI:
  3373. switch (state->config->ts2_mode) {
  3374. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3375. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3376. default:
  3377. stv090x_write_reg(state, STV090x_TSGENERAL, 0x00);
  3378. break;
  3379. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3380. case STV090x_TSMODE_DVBCI:
  3381. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x06) < 0) /* Mux'd stream mode */
  3382. goto err;
  3383. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3384. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3385. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3386. goto err;
  3387. reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
  3388. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3389. if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
  3390. goto err;
  3391. if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
  3392. goto err;
  3393. if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
  3394. goto err;
  3395. break;
  3396. }
  3397. break;
  3398. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3399. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3400. default:
  3401. switch (state->config->ts2_mode) {
  3402. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3403. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3404. default:
  3405. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
  3406. goto err;
  3407. break;
  3408. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3409. case STV090x_TSMODE_DVBCI:
  3410. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0a) < 0)
  3411. goto err;
  3412. break;
  3413. }
  3414. break;
  3415. }
  3416. } else {
  3417. switch (state->config->ts1_mode) {
  3418. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3419. case STV090x_TSMODE_DVBCI:
  3420. switch (state->config->ts2_mode) {
  3421. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3422. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3423. default:
  3424. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x10);
  3425. break;
  3426. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3427. case STV090x_TSMODE_DVBCI:
  3428. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x16);
  3429. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3430. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3431. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3432. goto err;
  3433. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3434. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 0);
  3435. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3436. goto err;
  3437. if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
  3438. goto err;
  3439. if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
  3440. goto err;
  3441. break;
  3442. }
  3443. break;
  3444. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3445. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3446. default:
  3447. switch (state->config->ts2_mode) {
  3448. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3449. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3450. default:
  3451. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
  3452. break;
  3453. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3454. case STV090x_TSMODE_DVBCI:
  3455. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x12);
  3456. break;
  3457. }
  3458. break;
  3459. }
  3460. }
  3461. switch (state->config->ts1_mode) {
  3462. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3463. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3464. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3465. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3466. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3467. goto err;
  3468. break;
  3469. case STV090x_TSMODE_DVBCI:
  3470. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3471. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3472. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3473. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3474. goto err;
  3475. break;
  3476. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3477. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3478. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3479. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3480. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3481. goto err;
  3482. break;
  3483. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3484. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3485. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3486. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3487. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3488. goto err;
  3489. break;
  3490. default:
  3491. break;
  3492. }
  3493. switch (state->config->ts2_mode) {
  3494. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3495. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3496. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3497. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3498. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3499. goto err;
  3500. break;
  3501. case STV090x_TSMODE_DVBCI:
  3502. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3503. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3504. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3505. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3506. goto err;
  3507. break;
  3508. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3509. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3510. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3511. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3512. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3513. goto err;
  3514. break;
  3515. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3516. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3517. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3518. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3519. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3520. goto err;
  3521. break;
  3522. default:
  3523. break;
  3524. }
  3525. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3526. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
  3527. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3528. goto err;
  3529. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
  3530. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3531. goto err;
  3532. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3533. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
  3534. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3535. goto err;
  3536. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
  3537. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3538. goto err;
  3539. return 0;
  3540. err:
  3541. dprintk(FE_ERROR, 1, "I/O error");
  3542. return -1;
  3543. }
  3544. static int stv090x_init(struct dvb_frontend *fe)
  3545. {
  3546. struct stv090x_state *state = fe->demodulator_priv;
  3547. const struct stv090x_config *config = state->config;
  3548. u32 reg;
  3549. if (stv090x_wakeup(fe) < 0) {
  3550. dprintk(FE_ERROR, 1, "Error waking device");
  3551. goto err;
  3552. }
  3553. if (stv090x_ldpc_mode(state, state->demod_mode) < 0)
  3554. goto err;
  3555. reg = STV090x_READ_DEMOD(state, TNRCFG2);
  3556. STV090x_SETFIELD_Px(reg, TUN_IQSWAP_FIELD, state->inversion);
  3557. if (STV090x_WRITE_DEMOD(state, TNRCFG2, reg) < 0)
  3558. goto err;
  3559. reg = STV090x_READ_DEMOD(state, DEMOD);
  3560. STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
  3561. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  3562. goto err;
  3563. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  3564. goto err;
  3565. if (config->tuner_set_mode) {
  3566. if (config->tuner_set_mode(fe, TUNER_WAKE) < 0)
  3567. goto err;
  3568. }
  3569. if (config->tuner_init) {
  3570. if (config->tuner_init(fe) < 0)
  3571. goto err;
  3572. }
  3573. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  3574. goto err;
  3575. if (stv090x_set_tspath(state) < 0)
  3576. goto err;
  3577. return 0;
  3578. err:
  3579. dprintk(FE_ERROR, 1, "I/O error");
  3580. return -1;
  3581. }
  3582. static int stv090x_setup(struct dvb_frontend *fe)
  3583. {
  3584. struct stv090x_state *state = fe->demodulator_priv;
  3585. const struct stv090x_config *config = state->config;
  3586. const struct stv090x_reg *stv090x_initval = NULL;
  3587. const struct stv090x_reg *stv090x_cut20_val = NULL;
  3588. unsigned long t1_size = 0, t2_size = 0;
  3589. u32 reg = 0;
  3590. int i;
  3591. if (state->device == STV0900) {
  3592. dprintk(FE_DEBUG, 1, "Initializing STV0900");
  3593. stv090x_initval = stv0900_initval;
  3594. t1_size = ARRAY_SIZE(stv0900_initval);
  3595. stv090x_cut20_val = stv0900_cut20_val;
  3596. t2_size = ARRAY_SIZE(stv0900_cut20_val);
  3597. } else if (state->device == STV0903) {
  3598. dprintk(FE_DEBUG, 1, "Initializing STV0903");
  3599. stv090x_initval = stv0903_initval;
  3600. t1_size = ARRAY_SIZE(stv0903_initval);
  3601. stv090x_cut20_val = stv0903_cut20_val;
  3602. t2_size = ARRAY_SIZE(stv0903_cut20_val);
  3603. }
  3604. /* STV090x init */
  3605. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Stop Demod */
  3606. goto err;
  3607. msleep(5);
  3608. if (STV090x_WRITE_DEMOD(state, TNRCFG, 0x6c) < 0) /* check register ! (No Tuner Mode) */
  3609. goto err;
  3610. STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level);
  3611. if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0) /* repeater OFF */
  3612. goto err;
  3613. if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
  3614. goto err;
  3615. msleep(5);
  3616. if (stv090x_write_reg(state, STV090x_I2CCFG, 0x08) < 0) /* 1/41 oversampling */
  3617. goto err;
  3618. if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0) /* enable PLL */
  3619. goto err;
  3620. msleep(5);
  3621. /* write initval */
  3622. dprintk(FE_DEBUG, 1, "Setting up initial values");
  3623. for (i = 0; i < t1_size; i++) {
  3624. if (stv090x_write_reg(state, stv090x_initval[i].addr, stv090x_initval[i].data) < 0)
  3625. goto err;
  3626. }
  3627. state->dev_ver = stv090x_read_reg(state, STV090x_MID);
  3628. if (state->dev_ver >= 0x20) {
  3629. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
  3630. goto err;
  3631. /* write cut20_val*/
  3632. dprintk(FE_DEBUG, 1, "Setting up Cut 2.0 initial values");
  3633. for (i = 0; i < t2_size; i++) {
  3634. if (stv090x_write_reg(state, stv090x_cut20_val[i].addr, stv090x_cut20_val[i].data) < 0)
  3635. goto err;
  3636. }
  3637. } else if (state->dev_ver < 0x20) {
  3638. dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!",
  3639. state->dev_ver);
  3640. goto err;
  3641. } else if (state->dev_ver > 0x30) {
  3642. /* we shouldn't bail out from here */
  3643. dprintk(FE_ERROR, 1, "INFO: Cut: 0x%02x probably incomplete support!",
  3644. state->dev_ver);
  3645. }
  3646. if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
  3647. goto err;
  3648. if (stv090x_write_reg(state, STV090x_TSTRES0, 0x00) < 0)
  3649. goto err;
  3650. stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
  3651. msleep(5);
  3652. if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0)
  3653. goto err;
  3654. stv090x_get_mclk(state);
  3655. return 0;
  3656. err:
  3657. dprintk(FE_ERROR, 1, "I/O error");
  3658. return -1;
  3659. }
  3660. static struct dvb_frontend_ops stv090x_ops = {
  3661. .info = {
  3662. .name = "STV090x Multistandard",
  3663. .type = FE_QPSK,
  3664. .frequency_min = 950000,
  3665. .frequency_max = 2150000,
  3666. .frequency_stepsize = 0,
  3667. .frequency_tolerance = 0,
  3668. .symbol_rate_min = 1000000,
  3669. .symbol_rate_max = 45000000,
  3670. .caps = FE_CAN_INVERSION_AUTO |
  3671. FE_CAN_FEC_AUTO |
  3672. FE_CAN_QPSK |
  3673. FE_CAN_2G_MODULATION
  3674. },
  3675. .release = stv090x_release,
  3676. .init = stv090x_init,
  3677. .sleep = stv090x_sleep,
  3678. .get_frontend_algo = stv090x_frontend_algo,
  3679. .i2c_gate_ctrl = stv090x_i2c_gate_ctrl,
  3680. .diseqc_send_master_cmd = stv090x_send_diseqc_msg,
  3681. .diseqc_send_burst = stv090x_send_diseqc_burst,
  3682. .diseqc_recv_slave_reply = stv090x_recv_slave_reply,
  3683. .set_tone = stv090x_set_tone,
  3684. .search = stv090x_search,
  3685. .read_status = stv090x_read_status,
  3686. .read_ber = stv090x_read_per,
  3687. .read_signal_strength = stv090x_read_signal_strength,
  3688. .read_snr = stv090x_read_cnr
  3689. };
  3690. struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
  3691. struct i2c_adapter *i2c,
  3692. enum stv090x_demodulator demod)
  3693. {
  3694. struct stv090x_state *state = NULL;
  3695. state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL);
  3696. if (state == NULL)
  3697. goto error;
  3698. state->verbose = &verbose;
  3699. state->config = config;
  3700. state->i2c = i2c;
  3701. state->frontend.ops = stv090x_ops;
  3702. state->frontend.demodulator_priv = state;
  3703. state->demod = demod;
  3704. state->demod_mode = config->demod_mode; /* Single or Dual mode */
  3705. state->device = config->device;
  3706. state->rolloff = STV090x_RO_35; /* default */
  3707. if (state->demod == STV090x_DEMODULATOR_0)
  3708. mutex_init(&demod_lock);
  3709. if (stv090x_sleep(&state->frontend) < 0) {
  3710. dprintk(FE_ERROR, 1, "Error putting device to sleep");
  3711. goto error;
  3712. }
  3713. if (stv090x_setup(&state->frontend) < 0) {
  3714. dprintk(FE_ERROR, 1, "Error setting up device");
  3715. goto error;
  3716. }
  3717. if (stv090x_wakeup(&state->frontend) < 0) {
  3718. dprintk(FE_ERROR, 1, "Error waking device");
  3719. goto error;
  3720. }
  3721. dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x\n",
  3722. state->device == STV0900 ? "STV0900" : "STV0903",
  3723. demod,
  3724. state->dev_ver);
  3725. return &state->frontend;
  3726. error:
  3727. kfree(state);
  3728. return NULL;
  3729. }
  3730. EXPORT_SYMBOL(stv090x_attach);
  3731. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  3732. MODULE_AUTHOR("Manu Abraham");
  3733. MODULE_DESCRIPTION("STV090x Multi-Std Broadcast frontend");
  3734. MODULE_LICENSE("GPL");