stv090x.c 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047
  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 1.x Long Frame Tracking CR loop */
  485. static struct stv090x_long_frame_crloop stv090x_s2_crl[] = {
  486. /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
  487. { STV090x_QPSK_12, 0x1c, 0x0d, 0x1b, 0x2c, 0x3a, 0x1c, 0x2a, 0x3b, 0x2a, 0x1b },
  488. { STV090x_QPSK_35, 0x2c, 0x0d, 0x2b, 0x2c, 0x3a, 0x0c, 0x3a, 0x2b, 0x2a, 0x0b },
  489. { STV090x_QPSK_23, 0x2c, 0x0d, 0x2b, 0x2c, 0x0b, 0x0c, 0x3a, 0x1b, 0x2a, 0x3a },
  490. { STV090x_QPSK_34, 0x3c, 0x0d, 0x3b, 0x1c, 0x0b, 0x3b, 0x3a, 0x0b, 0x2a, 0x3a },
  491. { STV090x_QPSK_45, 0x3c, 0x0d, 0x3b, 0x1c, 0x0b, 0x3b, 0x3a, 0x0b, 0x2a, 0x3a },
  492. { STV090x_QPSK_56, 0x0d, 0x0d, 0x3b, 0x1c, 0x0b, 0x3b, 0x3a, 0x0b, 0x2a, 0x3a },
  493. { STV090x_QPSK_89, 0x0d, 0x0d, 0x3b, 0x1c, 0x1b, 0x3b, 0x3a, 0x0b, 0x2a, 0x3a },
  494. { STV090x_QPSK_910, 0x1d, 0x0d, 0x3b, 0x1c, 0x1b, 0x3b, 0x3a, 0x0b, 0x2a, 0x3a },
  495. { STV090x_8PSK_35, 0x29, 0x3b, 0x09, 0x2b, 0x38, 0x0b, 0x18, 0x1a, 0x08, 0x0a },
  496. { STV090x_8PSK_23, 0x0a, 0x3b, 0x29, 0x2b, 0x19, 0x0b, 0x38, 0x1a, 0x18, 0x0a },
  497. { STV090x_8PSK_34, 0x3a, 0x3b, 0x2a, 0x2b, 0x39, 0x0b, 0x19, 0x1a, 0x38, 0x0a },
  498. { STV090x_8PSK_56, 0x1b, 0x3b, 0x0b, 0x2b, 0x1a, 0x0b, 0x39, 0x1a, 0x19, 0x0a },
  499. { STV090x_8PSK_89, 0x3b, 0x3b, 0x0b, 0x2b, 0x2a, 0x0b, 0x39, 0x1a, 0x29, 0x39 },
  500. { STV090x_8PSK_910, 0x3b, 0x3b, 0x0b, 0x2b, 0x2a, 0x0b, 0x39, 0x1a, 0x29, 0x39 }
  501. };
  502. /* Cut 2.0 Long Frame Tracking CR loop */
  503. static struct stv090x_long_frame_crloop stv090x_s2_crl_cut20[] = {
  504. /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
  505. { STV090x_QPSK_12, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x1e },
  506. { STV090x_QPSK_35, 0x2f, 0x3f, 0x2e, 0x2f, 0x3d, 0x0f, 0x0e, 0x2e, 0x3d, 0x0e },
  507. { STV090x_QPSK_23, 0x2f, 0x3f, 0x2e, 0x2f, 0x0e, 0x0f, 0x0e, 0x1e, 0x3d, 0x3d },
  508. { STV090x_QPSK_34, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
  509. { STV090x_QPSK_45, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
  510. { STV090x_QPSK_56, 0x3f, 0x3f, 0x3e, 0x1f, 0x0e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
  511. { STV090x_QPSK_89, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
  512. { STV090x_QPSK_910, 0x3f, 0x3f, 0x3e, 0x1f, 0x1e, 0x3e, 0x0e, 0x1e, 0x3d, 0x3d },
  513. { STV090x_8PSK_35, 0x3c, 0x3e, 0x1c, 0x2e, 0x0c, 0x1e, 0x2b, 0x2d, 0x1b, 0x1d },
  514. { STV090x_8PSK_23, 0x1d, 0x3e, 0x3c, 0x2e, 0x2c, 0x1e, 0x0c, 0x2d, 0x2b, 0x1d },
  515. { STV090x_8PSK_34, 0x0e, 0x3e, 0x3d, 0x2e, 0x0d, 0x1e, 0x2c, 0x2d, 0x0c, 0x1d },
  516. { STV090x_8PSK_56, 0x2e, 0x3e, 0x1e, 0x2e, 0x2d, 0x1e, 0x3c, 0x2d, 0x2c, 0x1d },
  517. { STV090x_8PSK_89, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x0d, 0x2d, 0x3c, 0x1d },
  518. { STV090x_8PSK_910, 0x3e, 0x3e, 0x1e, 0x2e, 0x3d, 0x1e, 0x1d, 0x2d, 0x0d, 0x1d }
  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. static struct stv090x_long_frame_crloop stv090x_s2_lowqpsk_crl_cut20[] = {
  536. /* MODCOD 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon 20MPoff 30MPon 30MPoff */
  537. { STV090x_QPSK_14, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x2d, 0x1f, 0x3d, 0x3e },
  538. { STV090x_QPSK_13, 0x0f, 0x3f, 0x0e, 0x3f, 0x2d, 0x2f, 0x3d, 0x0f, 0x3d, 0x2e },
  539. { STV090x_QPSK_25, 0x1f, 0x3f, 0x1e, 0x3f, 0x3d, 0x1f, 0x3d, 0x3e, 0x3d, 0x2e }
  540. };
  541. /* Cut 1.2 & 2.0 Short Frame Tracking CR Loop */
  542. static struct stv090x_short_frame_crloop stv090x_s2_short_crl[] = {
  543. /* MODCOD 2M_cut1.2 2M_cut2.0 5M_cut1.2 5M_cut2.0 10M_cut1.2 10M_cut2.0 20M_cut1.2 20M_cut2.0 30M_cut1.2 30M_cut2.0 */
  544. { STV090x_QPSK, 0x3c, 0x2f, 0x2b, 0x2e, 0x0b, 0x0e, 0x3a, 0x0e, 0x2a, 0x3d },
  545. { STV090x_8PSK, 0x0b, 0x3e, 0x2a, 0x0e, 0x0a, 0x2d, 0x19, 0x0d, 0x09, 0x3c },
  546. { STV090x_16APSK, 0x1b, 0x1e, 0x1b, 0x1e, 0x1b, 0x1e, 0x3a, 0x3d, 0x2a, 0x2d },
  547. { STV090x_32APSK, 0x1b, 0x1e, 0x1b, 0x1e, 0x1b, 0x1e, 0x3a, 0x3d, 0x2a, 0x2d }
  548. };
  549. static inline s32 comp2(s32 __x, s32 __width)
  550. {
  551. if (__width == 32)
  552. return __x;
  553. else
  554. return (__x >= (1 << (__width - 1))) ? (__x - (1 << __width)) : __x;
  555. }
  556. static int stv090x_read_reg(struct stv090x_state *state, unsigned int reg)
  557. {
  558. const struct stv090x_config *config = state->config;
  559. int ret;
  560. u8 b0[] = { reg >> 8, reg & 0xff };
  561. u8 buf;
  562. struct i2c_msg msg[] = {
  563. { .addr = config->address, .flags = 0, .buf = b0, .len = 2 },
  564. { .addr = config->address, .flags = I2C_M_RD, .buf = &buf, .len = 1 }
  565. };
  566. ret = i2c_transfer(state->i2c, msg, 2);
  567. if (ret != 2) {
  568. if (ret != -ERESTARTSYS)
  569. dprintk(FE_ERROR, 1,
  570. "Read error, Reg=[0x%02x], Status=%d",
  571. reg, ret);
  572. return ret < 0 ? ret : -EREMOTEIO;
  573. }
  574. if (unlikely(*state->verbose >= FE_DEBUGREG))
  575. dprintk(FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
  576. reg, buf);
  577. return (unsigned int) buf;
  578. }
  579. static int stv090x_write_regs(struct stv090x_state *state, unsigned int reg, u8 *data, u32 count)
  580. {
  581. const struct stv090x_config *config = state->config;
  582. int ret;
  583. u8 buf[2 + count];
  584. struct i2c_msg i2c_msg = { .addr = config->address, .flags = 0, .buf = buf, .len = 2 + count };
  585. buf[0] = reg >> 8;
  586. buf[1] = reg & 0xff;
  587. memcpy(&buf[2], data, count);
  588. if (unlikely(*state->verbose >= FE_DEBUGREG)) {
  589. int i;
  590. printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
  591. for (i = 0; i < count; i++)
  592. printk(" %02x", data[i]);
  593. printk("\n");
  594. }
  595. ret = i2c_transfer(state->i2c, &i2c_msg, 1);
  596. if (ret != 1) {
  597. if (ret != -ERESTARTSYS)
  598. dprintk(FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
  599. reg, data[0], count, ret);
  600. return ret < 0 ? ret : -EREMOTEIO;
  601. }
  602. return 0;
  603. }
  604. static int stv090x_write_reg(struct stv090x_state *state, unsigned int reg, u8 data)
  605. {
  606. return stv090x_write_regs(state, reg, &data, 1);
  607. }
  608. static int stv090x_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  609. {
  610. struct stv090x_state *state = fe->demodulator_priv;
  611. u32 reg;
  612. reg = STV090x_READ_DEMOD(state, I2CRPT);
  613. if (enable) {
  614. dprintk(FE_DEBUG, 1, "Enable Gate");
  615. STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 1);
  616. if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0)
  617. goto err;
  618. } else {
  619. dprintk(FE_DEBUG, 1, "Disable Gate");
  620. STV090x_SETFIELD_Px(reg, I2CT_ON_FIELD, 0);
  621. if ((STV090x_WRITE_DEMOD(state, I2CRPT, reg)) < 0)
  622. goto err;
  623. }
  624. return 0;
  625. err:
  626. dprintk(FE_ERROR, 1, "I/O error");
  627. return -1;
  628. }
  629. static void stv090x_get_lock_tmg(struct stv090x_state *state)
  630. {
  631. switch (state->algo) {
  632. case STV090x_BLIND_SEARCH:
  633. dprintk(FE_DEBUG, 1, "Blind Search");
  634. if (state->srate <= 1500000) { /*10Msps< SR <=15Msps*/
  635. state->DemodTimeout = 1500;
  636. state->FecTimeout = 400;
  637. } else if (state->srate <= 5000000) { /*10Msps< SR <=15Msps*/
  638. state->DemodTimeout = 1000;
  639. state->FecTimeout = 300;
  640. } else { /*SR >20Msps*/
  641. state->DemodTimeout = 700;
  642. state->FecTimeout = 100;
  643. }
  644. break;
  645. case STV090x_COLD_SEARCH:
  646. case STV090x_WARM_SEARCH:
  647. default:
  648. dprintk(FE_DEBUG, 1, "Normal Search");
  649. if (state->srate <= 1000000) { /*SR <=1Msps*/
  650. state->DemodTimeout = 4500;
  651. state->FecTimeout = 1700;
  652. } else if (state->srate <= 2000000) { /*1Msps < SR <= 2Msps */
  653. state->DemodTimeout = 2500;
  654. state->FecTimeout = 1100;
  655. } else if (state->srate <= 5000000) { /*2Msps < SR <= 5Msps */
  656. state->DemodTimeout = 1000;
  657. state->FecTimeout = 550;
  658. } else if (state->srate <= 10000000) { /*5Msps < SR <= 10Msps */
  659. state->DemodTimeout = 700;
  660. state->FecTimeout = 250;
  661. } else if (state->srate <= 20000000) { /*10Msps < SR <= 20Msps */
  662. state->DemodTimeout = 400;
  663. state->FecTimeout = 130;
  664. } else { /*SR >20Msps*/
  665. state->DemodTimeout = 300;
  666. state->FecTimeout = 100;
  667. }
  668. break;
  669. }
  670. if (state->algo == STV090x_WARM_SEARCH)
  671. state->DemodTimeout /= 2;
  672. }
  673. static int stv090x_set_srate(struct stv090x_state *state, u32 srate)
  674. {
  675. u32 sym;
  676. if (srate > 60000000) {
  677. sym = (srate << 4); /* SR * 2^16 / master_clk */
  678. sym /= (state->mclk >> 12);
  679. } else if (srate > 6000000) {
  680. sym = (srate << 6);
  681. sym /= (state->mclk >> 10);
  682. } else {
  683. sym = (srate << 9);
  684. sym /= (state->mclk >> 7);
  685. }
  686. if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0x7f) < 0) /* MSB */
  687. goto err;
  688. if (STV090x_WRITE_DEMOD(state, SFRINIT0, (sym & 0xff)) < 0) /* LSB */
  689. goto err;
  690. return 0;
  691. err:
  692. dprintk(FE_ERROR, 1, "I/O error");
  693. return -1;
  694. }
  695. static int stv090x_set_max_srate(struct stv090x_state *state, u32 clk, u32 srate)
  696. {
  697. u32 sym;
  698. srate = 105 * (srate / 100);
  699. if (srate > 60000000) {
  700. sym = (srate << 4); /* SR * 2^16 / master_clk */
  701. sym /= (state->mclk >> 12);
  702. } else if (srate > 6000000) {
  703. sym = (srate << 6);
  704. sym /= (state->mclk >> 10);
  705. } else {
  706. sym = (srate << 9);
  707. sym /= (state->mclk >> 7);
  708. }
  709. if (sym < 0x7fff) {
  710. if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) /* MSB */
  711. goto err;
  712. if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) /* LSB */
  713. goto err;
  714. } else {
  715. if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x7f) < 0) /* MSB */
  716. goto err;
  717. if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xff) < 0) /* LSB */
  718. goto err;
  719. }
  720. return 0;
  721. err:
  722. dprintk(FE_ERROR, 1, "I/O error");
  723. return -1;
  724. }
  725. static int stv090x_set_min_srate(struct stv090x_state *state, u32 clk, u32 srate)
  726. {
  727. u32 sym;
  728. srate = 95 * (srate / 100);
  729. if (srate > 60000000) {
  730. sym = (srate << 4); /* SR * 2^16 / master_clk */
  731. sym /= (state->mclk >> 12);
  732. } else if (srate > 6000000) {
  733. sym = (srate << 6);
  734. sym /= (state->mclk >> 10);
  735. } else {
  736. sym = (srate << 9);
  737. sym /= (state->mclk >> 7);
  738. }
  739. if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0xff)) < 0) /* MSB */
  740. goto err;
  741. if (STV090x_WRITE_DEMOD(state, SFRLOW0, (sym & 0xff)) < 0) /* LSB */
  742. goto err;
  743. return 0;
  744. err:
  745. dprintk(FE_ERROR, 1, "I/O error");
  746. return -1;
  747. }
  748. static u32 stv090x_car_width(u32 srate, u32 rolloff)
  749. {
  750. return srate + (srate * rolloff) / 100;
  751. }
  752. static int stv090x_set_vit_thacq(struct stv090x_state *state)
  753. {
  754. if (STV090x_WRITE_DEMOD(state, VTH12, 0x96) < 0)
  755. goto err;
  756. if (STV090x_WRITE_DEMOD(state, VTH23, 0x64) < 0)
  757. goto err;
  758. if (STV090x_WRITE_DEMOD(state, VTH34, 0x36) < 0)
  759. goto err;
  760. if (STV090x_WRITE_DEMOD(state, VTH56, 0x23) < 0)
  761. goto err;
  762. if (STV090x_WRITE_DEMOD(state, VTH67, 0x1e) < 0)
  763. goto err;
  764. if (STV090x_WRITE_DEMOD(state, VTH78, 0x19) < 0)
  765. goto err;
  766. return 0;
  767. err:
  768. dprintk(FE_ERROR, 1, "I/O error");
  769. return -1;
  770. }
  771. static int stv090x_set_vit_thtracq(struct stv090x_state *state)
  772. {
  773. if (STV090x_WRITE_DEMOD(state, VTH12, 0xd0) < 0)
  774. goto err;
  775. if (STV090x_WRITE_DEMOD(state, VTH23, 0x7d) < 0)
  776. goto err;
  777. if (STV090x_WRITE_DEMOD(state, VTH34, 0x53) < 0)
  778. goto err;
  779. if (STV090x_WRITE_DEMOD(state, VTH56, 0x2f) < 0)
  780. goto err;
  781. if (STV090x_WRITE_DEMOD(state, VTH67, 0x24) < 0)
  782. goto err;
  783. if (STV090x_WRITE_DEMOD(state, VTH78, 0x1f) < 0)
  784. goto err;
  785. return 0;
  786. err:
  787. dprintk(FE_ERROR, 1, "I/O error");
  788. return -1;
  789. }
  790. static int stv090x_set_viterbi(struct stv090x_state *state)
  791. {
  792. switch (state->search_mode) {
  793. case STV090x_SEARCH_AUTO:
  794. if (STV090x_WRITE_DEMOD(state, FECM, 0x10) < 0) /* DVB-S and DVB-S2 */
  795. goto err;
  796. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x3f) < 0) /* all puncture rate */
  797. goto err;
  798. break;
  799. case STV090x_SEARCH_DVBS1:
  800. if (STV090x_WRITE_DEMOD(state, FECM, 0x00) < 0) /* disable DSS */
  801. goto err;
  802. switch (state->fec) {
  803. case STV090x_PR12:
  804. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
  805. goto err;
  806. break;
  807. case STV090x_PR23:
  808. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
  809. goto err;
  810. break;
  811. case STV090x_PR34:
  812. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x04) < 0)
  813. goto err;
  814. break;
  815. case STV090x_PR56:
  816. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x08) < 0)
  817. goto err;
  818. break;
  819. case STV090x_PR78:
  820. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x20) < 0)
  821. goto err;
  822. break;
  823. default:
  824. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x2f) < 0) /* all */
  825. goto err;
  826. break;
  827. }
  828. break;
  829. case STV090x_SEARCH_DSS:
  830. if (STV090x_WRITE_DEMOD(state, FECM, 0x80) < 0)
  831. goto err;
  832. switch (state->fec) {
  833. case STV090x_PR12:
  834. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x01) < 0)
  835. goto err;
  836. break;
  837. case STV090x_PR23:
  838. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x02) < 0)
  839. goto err;
  840. break;
  841. case STV090x_PR67:
  842. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x10) < 0)
  843. goto err;
  844. break;
  845. default:
  846. if (STV090x_WRITE_DEMOD(state, PRVIT, 0x13) < 0) /* 1/2, 2/3, 6/7 */
  847. goto err;
  848. break;
  849. }
  850. break;
  851. default:
  852. break;
  853. }
  854. return 0;
  855. err:
  856. dprintk(FE_ERROR, 1, "I/O error");
  857. return -1;
  858. }
  859. static int stv090x_stop_modcod(struct stv090x_state *state)
  860. {
  861. if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
  862. goto err;
  863. if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
  864. goto err;
  865. if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
  866. goto err;
  867. if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
  868. goto err;
  869. if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
  870. goto err;
  871. if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
  872. goto err;
  873. if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
  874. goto err;
  875. if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xff) < 0)
  876. goto err;
  877. if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xff) < 0)
  878. goto err;
  879. if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xff) < 0)
  880. goto err;
  881. if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xff) < 0)
  882. goto err;
  883. if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xff) < 0)
  884. goto err;
  885. if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xff) < 0)
  886. goto err;
  887. if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xff) < 0)
  888. goto err;
  889. if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
  890. goto err;
  891. if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xff) < 0)
  892. goto err;
  893. return 0;
  894. err:
  895. dprintk(FE_ERROR, 1, "I/O error");
  896. return -1;
  897. }
  898. static int stv090x_activate_modcod(struct stv090x_state *state)
  899. {
  900. u32 matype, modcod, f_mod, index;
  901. if (state->dev_ver <= 0x11) {
  902. msleep(5);
  903. modcod = STV090x_READ_DEMOD(state, PLHMODCOD);
  904. matype = modcod & 0x03;
  905. modcod = (modcod & 0x7f) >> 2;
  906. index = STV090x_ADDR_OFFST(state, MODCODLSTF) - (modcod / 2);
  907. switch (matype) {
  908. default:
  909. case 0:
  910. f_mod = 14;
  911. break;
  912. case 1:
  913. f_mod = 13;
  914. break;
  915. case 2:
  916. f_mod = 11;
  917. break;
  918. case 3:
  919. f_mod = 7;
  920. break;
  921. }
  922. if (matype <= 1) {
  923. if (modcod % 2) {
  924. if (stv090x_write_reg(state, index, 0xf0 | f_mod) < 0)
  925. goto err;
  926. } else {
  927. if (stv090x_write_reg(state, index, (f_mod << 4) | 0x0f) < 0)
  928. goto err;
  929. }
  930. }
  931. } else if (state->dev_ver >= 0x12) {
  932. if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
  933. goto err;
  934. if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xfc) < 0)
  935. goto err;
  936. if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xcc) < 0)
  937. goto err;
  938. if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xcc) < 0)
  939. goto err;
  940. if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xcc) < 0)
  941. goto err;
  942. if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xcc) < 0)
  943. goto err;
  944. if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xcc) < 0)
  945. goto err;
  946. if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
  947. goto err;
  948. if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
  949. goto err;
  950. if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
  951. goto err;
  952. if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
  953. goto err;
  954. if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
  955. goto err;
  956. if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
  957. goto err;
  958. if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
  959. goto err;
  960. if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xcc) < 0)
  961. goto err;
  962. if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
  963. goto err;
  964. }
  965. return 0;
  966. err:
  967. dprintk(FE_ERROR, 1, "I/O error");
  968. return -1;
  969. }
  970. static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
  971. {
  972. u32 reg;
  973. switch (state->demod) {
  974. case STV090x_DEMODULATOR_0:
  975. mutex_lock(&demod_lock);
  976. reg = stv090x_read_reg(state, STV090x_STOPCLK2);
  977. STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, enable);
  978. if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
  979. goto err;
  980. mutex_unlock(&demod_lock);
  981. break;
  982. case STV090x_DEMODULATOR_1:
  983. mutex_lock(&demod_lock);
  984. reg = stv090x_read_reg(state, STV090x_STOPCLK2);
  985. STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, enable);
  986. if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
  987. goto err;
  988. mutex_unlock(&demod_lock);
  989. break;
  990. default:
  991. dprintk(FE_ERROR, 1, "Wrong demodulator!");
  992. break;
  993. }
  994. return 0;
  995. err:
  996. mutex_unlock(&demod_lock);
  997. dprintk(FE_ERROR, 1, "I/O error");
  998. return -1;
  999. }
  1000. static int stv090x_delivery_search(struct stv090x_state *state)
  1001. {
  1002. u32 reg;
  1003. switch (state->search_mode) {
  1004. case STV090x_SEARCH_DVBS1:
  1005. case STV090x_SEARCH_DSS:
  1006. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1007. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  1008. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
  1009. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1010. goto err;
  1011. /* Activate Viterbi decoder in legacy search, do not use FRESVIT1, might impact VITERBI2 */
  1012. if (stv090x_vitclk_ctl(state, 0) < 0)
  1013. goto err;
  1014. if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
  1015. goto err;
  1016. if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
  1017. goto err;
  1018. if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x22) < 0) /* disable DVB-S2 */
  1019. goto err;
  1020. stv090x_set_vit_thacq(state);
  1021. stv090x_set_viterbi(state);
  1022. break;
  1023. case STV090x_SEARCH_DVBS2:
  1024. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1025. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
  1026. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
  1027. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1028. goto err;
  1029. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  1030. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
  1031. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1032. goto err;
  1033. if (stv090x_vitclk_ctl(state, 1) < 0)
  1034. goto err;
  1035. if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0) /* stop DVB-S CR loop */
  1036. goto err;
  1037. if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
  1038. goto err;
  1039. if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
  1040. goto err;
  1041. if (state->demod_mode != STV090x_SINGLE) {
  1042. if (state->dev_ver <= 0x11) /* 900 in dual TS mode */
  1043. stv090x_stop_modcod(state);
  1044. else
  1045. stv090x_activate_modcod(state);
  1046. }
  1047. break;
  1048. case STV090x_SEARCH_AUTO:
  1049. default:
  1050. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1051. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
  1052. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
  1053. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1054. goto err;
  1055. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  1056. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
  1057. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1058. goto err;
  1059. if (stv090x_vitclk_ctl(state, 1) < 0)
  1060. goto err;
  1061. if (STV090x_WRITE_DEMOD(state, ACLC, 0x1a) < 0)
  1062. goto err;
  1063. if (STV090x_WRITE_DEMOD(state, ACLC, 0x09) < 0)
  1064. goto err;
  1065. if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
  1066. goto err;
  1067. if (state->demod_mode != STV090x_SINGLE) {
  1068. if (state->dev_ver <= 0x11) /* 900 in dual TS mode */
  1069. stv090x_stop_modcod(state);
  1070. else
  1071. stv090x_activate_modcod(state);
  1072. }
  1073. stv090x_set_vit_thacq(state);
  1074. stv090x_set_viterbi(state);
  1075. break;
  1076. }
  1077. return 0;
  1078. err:
  1079. dprintk(FE_ERROR, 1, "I/O error");
  1080. return -1;
  1081. }
  1082. static int stv090x_start_search(struct stv090x_state *state)
  1083. {
  1084. u32 reg;
  1085. reg = STV090x_READ_DEMOD(state, DMDISTATE);
  1086. STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f);
  1087. if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
  1088. goto err;
  1089. if (state->dev_ver == 0x10) {
  1090. if (STV090x_WRITE_DEMOD(state, CORRELEXP, 0xaa) < 0)
  1091. goto err;
  1092. }
  1093. if (state->dev_ver < 0x20) {
  1094. if (STV090x_WRITE_DEMOD(state, CARHDR, 0x55) < 0)
  1095. goto err;
  1096. }
  1097. if (state->srate <= 5000000) {
  1098. if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0)
  1099. goto err;
  1100. if (STV090x_WRITE_DEMOD(state, CFRUP1, 0x0f) < 0)
  1101. goto err;
  1102. if (STV090x_WRITE_DEMOD(state, CFRUP1, 0xff) < 0)
  1103. goto err;
  1104. if (STV090x_WRITE_DEMOD(state, CFRLOW1, 0xf0) < 0)
  1105. goto err;
  1106. if (STV090x_WRITE_DEMOD(state, CFRLOW0, 0x00) < 0)
  1107. goto err;
  1108. /*enlarge the timing bandwith for Low SR*/
  1109. if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0)
  1110. goto err;
  1111. } else {
  1112. /* If the symbol rate is >5 Msps
  1113. Set The carrier search up and low to auto mode */
  1114. if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
  1115. goto err;
  1116. /*reduce the timing bandwith for high SR*/
  1117. if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
  1118. goto err;
  1119. }
  1120. if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0) < 0)
  1121. goto err;
  1122. if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0) < 0)
  1123. goto err;
  1124. if (state->dev_ver >= 0x20) {
  1125. if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
  1126. goto err;
  1127. if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
  1128. goto err;
  1129. if ((state->search_mode == STV090x_DVBS1) ||
  1130. (state->search_mode == STV090x_DSS) ||
  1131. (state->search_mode == STV090x_SEARCH_AUTO)) {
  1132. if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
  1133. goto err;
  1134. if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0)
  1135. goto err;
  1136. }
  1137. }
  1138. if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00) < 0)
  1139. goto err;
  1140. if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xe0) < 0)
  1141. goto err;
  1142. if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xc0) < 0)
  1143. goto err;
  1144. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1145. STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0);
  1146. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
  1147. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1148. goto err;
  1149. reg = STV090x_READ_DEMOD(state, DMDCFG2);
  1150. STV090x_SETFIELD_Px(reg, S1S2_SEQUENTIAL_FIELD, 0x0);
  1151. if (STV090x_WRITE_DEMOD(state, DMDCFG2, reg) < 0)
  1152. goto err;
  1153. if (state->dev_ver >= 0x20) { /*Frequency offset detector setting*/
  1154. if (state->srate < 10000000) {
  1155. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4c) < 0)
  1156. goto err;
  1157. } else {
  1158. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x4b) < 0)
  1159. goto err;
  1160. }
  1161. } else {
  1162. if (state->srate < 10000000) {
  1163. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
  1164. goto err;
  1165. } else {
  1166. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
  1167. goto err;
  1168. }
  1169. }
  1170. switch (state->algo) {
  1171. case STV090x_WARM_SEARCH:/*The symbol rate and the exact carrier Frequency are known */
  1172. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  1173. goto err;
  1174. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
  1175. goto err;
  1176. break;
  1177. case STV090x_COLD_SEARCH:/*The symbol rate is known*/
  1178. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  1179. goto err;
  1180. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
  1181. goto err;
  1182. break;
  1183. default:
  1184. break;
  1185. }
  1186. return 0;
  1187. err:
  1188. dprintk(FE_ERROR, 1, "I/O error");
  1189. return -1;
  1190. }
  1191. static int stv090x_get_agc2_min_level(struct stv090x_state *state)
  1192. {
  1193. u32 agc2_min = 0, agc2 = 0, freq_init, freq_step, reg;
  1194. s32 i, j, steps, dir;
  1195. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
  1196. goto err;
  1197. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1198. STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 1);
  1199. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 1);
  1200. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1201. goto err;
  1202. if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0) /* SR = 65 Msps Max */
  1203. goto err;
  1204. if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
  1205. goto err;
  1206. if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0) /* SR= 400 ksps Min */
  1207. goto err;
  1208. if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
  1209. goto err;
  1210. if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0) /* stop acq @ coarse carrier state */
  1211. goto err;
  1212. stv090x_set_srate(state, 1000000);
  1213. steps = -1 + state->search_range / 1000000;
  1214. steps /= 2;
  1215. steps = (2 * steps) + 1;
  1216. if (steps < 0)
  1217. steps = 1;
  1218. dir = 1;
  1219. freq_step = (1000000 * 256) / (state->mclk / 256);
  1220. freq_init = 0;
  1221. for (i = 0; i < steps; i++) {
  1222. if (dir > 0)
  1223. freq_init = freq_init + (freq_step * i);
  1224. else
  1225. freq_init = freq_init - (freq_step * i);
  1226. dir = -1;
  1227. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod RESET */
  1228. goto err;
  1229. if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_init >> 8) & 0xff) < 0)
  1230. goto err;
  1231. if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_init & 0xff) < 0)
  1232. goto err;
  1233. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x58) < 0) /* Demod RESET */
  1234. goto err;
  1235. msleep(10);
  1236. for (j = 0; j < 10; j++) {
  1237. agc2 += STV090x_READ_DEMOD(state, AGC2I1) << 8;
  1238. agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
  1239. }
  1240. agc2 /= 10;
  1241. agc2_min = 0xffff;
  1242. if (agc2 < 0xffff)
  1243. agc2_min = agc2;
  1244. }
  1245. return agc2_min;
  1246. err:
  1247. dprintk(FE_ERROR, 1, "I/O error");
  1248. return -1;
  1249. }
  1250. static u32 stv090x_get_srate(struct stv090x_state *state, u32 clk)
  1251. {
  1252. u8 r3, r2, r1, r0;
  1253. s32 srate, int_1, int_2, tmp_1, tmp_2;
  1254. r3 = STV090x_READ_DEMOD(state, SFR3);
  1255. r2 = STV090x_READ_DEMOD(state, SFR2);
  1256. r1 = STV090x_READ_DEMOD(state, SFR1);
  1257. r0 = STV090x_READ_DEMOD(state, SFR0);
  1258. srate = ((r3 << 24) | (r2 << 16) | (r1 << 8) | r0);
  1259. int_1 = clk >> 16;
  1260. int_2 = srate >> 16;
  1261. tmp_1 = clk % 0x10000;
  1262. tmp_2 = srate % 0x10000;
  1263. srate = (int_1 * int_2) +
  1264. ((int_1 * tmp_2) >> 16) +
  1265. ((int_2 * tmp_1) >> 16);
  1266. return srate;
  1267. }
  1268. static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
  1269. {
  1270. struct dvb_frontend *fe = &state->frontend;
  1271. int tmg_lock = 0, i;
  1272. s32 tmg_cpt = 0, dir = 1, steps, cur_step = 0, freq;
  1273. u32 srate_coarse = 0, agc2 = 0, car_step = 1200, reg;
  1274. reg = STV090x_READ_DEMOD(state, DMDISTATE);
  1275. STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x1f); /* Demod RESET */
  1276. if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
  1277. goto err;
  1278. if (STV090x_WRITE_DEMOD(state, TMGCFG, 0x12) < 0)
  1279. goto err;
  1280. if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0xf0) < 0)
  1281. goto err;
  1282. if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0xe0) < 0)
  1283. goto err;
  1284. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1285. STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 1);
  1286. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 1);
  1287. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1288. goto err;
  1289. if (STV090x_WRITE_DEMOD(state, SFRUP1, 0x83) < 0)
  1290. goto err;
  1291. if (STV090x_WRITE_DEMOD(state, SFRUP0, 0xc0) < 0)
  1292. goto err;
  1293. if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x82) < 0)
  1294. goto err;
  1295. if (STV090x_WRITE_DEMOD(state, SFRLOW0, 0xa0) < 0)
  1296. goto err;
  1297. if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x00) < 0)
  1298. goto err;
  1299. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x60) < 0)
  1300. goto err;
  1301. if (state->dev_ver >= 0x20) {
  1302. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0)
  1303. goto err;
  1304. if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
  1305. goto err;
  1306. } else {
  1307. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
  1308. goto err;
  1309. if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x73) < 0)
  1310. goto err;
  1311. }
  1312. if (state->srate <= 2000000)
  1313. car_step = 1000;
  1314. else if (state->srate <= 5000000)
  1315. car_step = 2000;
  1316. else if (state->srate <= 12000000)
  1317. car_step = 3000;
  1318. else
  1319. car_step = 5000;
  1320. steps = -1 + ((state->search_range / 1000) / car_step);
  1321. steps /= 2;
  1322. steps = (2 * steps) + 1;
  1323. if (steps < 0)
  1324. steps = 1;
  1325. else if (steps > 10) {
  1326. steps = 11;
  1327. car_step = (state->search_range / 1000) / 10;
  1328. }
  1329. cur_step = 0;
  1330. dir = 1;
  1331. freq = state->frequency;
  1332. while ((!tmg_lock) && (cur_step < steps)) {
  1333. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5f) < 0) /* Demod RESET */
  1334. goto err;
  1335. reg = STV090x_READ_DEMOD(state, DMDISTATE);
  1336. STV090x_SETFIELD_Px(reg, I2C_DEMOD_MODE_FIELD, 0x00); /* trigger acquisition */
  1337. if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
  1338. goto err;
  1339. msleep(50);
  1340. for (i = 0; i < 10; i++) {
  1341. reg = STV090x_READ_DEMOD(state, DSTATUS);
  1342. if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
  1343. tmg_cpt++;
  1344. agc2 += STV090x_READ_DEMOD(state, AGC2I1) << 8;
  1345. agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
  1346. }
  1347. agc2 /= 10;
  1348. srate_coarse = stv090x_get_srate(state, state->mclk);
  1349. cur_step++;
  1350. dir *= -1;
  1351. if ((tmg_cpt >= 5) && (agc2 < 0x1f00) && (srate_coarse < 55000000) && (srate_coarse > 850000))
  1352. tmg_lock = 1;
  1353. else if (cur_step < steps) {
  1354. if (dir > 0)
  1355. freq += cur_step * car_step;
  1356. else
  1357. freq -= cur_step * car_step;
  1358. /* Setup tuner */
  1359. stv090x_i2c_gate_ctrl(fe, 1);
  1360. if (state->config->tuner_set_frequency)
  1361. state->config->tuner_set_frequency(fe, state->frequency);
  1362. if (state->config->tuner_set_bandwidth)
  1363. state->config->tuner_set_bandwidth(fe, state->tuner_bw);
  1364. stv090x_i2c_gate_ctrl(fe, 0);
  1365. msleep(50);
  1366. stv090x_i2c_gate_ctrl(fe, 1);
  1367. if (state->config->tuner_get_status)
  1368. state->config->tuner_get_status(fe, &reg);
  1369. if (reg)
  1370. dprintk(FE_DEBUG, 1, "Tuner phase locked");
  1371. else
  1372. dprintk(FE_DEBUG, 1, "Tuner unlocked");
  1373. stv090x_i2c_gate_ctrl(fe, 0);
  1374. }
  1375. }
  1376. if (!tmg_lock)
  1377. srate_coarse = 0;
  1378. else
  1379. srate_coarse = stv090x_get_srate(state, state->mclk);
  1380. return srate_coarse;
  1381. err:
  1382. dprintk(FE_ERROR, 1, "I/O error");
  1383. return -1;
  1384. }
  1385. static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
  1386. {
  1387. u32 srate_coarse, freq_coarse, sym, reg;
  1388. srate_coarse = stv090x_get_srate(state, state->mclk);
  1389. freq_coarse = STV090x_READ_DEMOD(state, CFR2) << 8;
  1390. freq_coarse |= STV090x_READ_DEMOD(state, CFR1);
  1391. sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
  1392. if (sym < state->srate)
  1393. srate_coarse = 0;
  1394. else {
  1395. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0) /* Demod RESET */
  1396. goto err;
  1397. if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0x01) < 0)
  1398. goto err;
  1399. if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
  1400. goto err;
  1401. if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
  1402. goto err;
  1403. if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
  1404. goto err;
  1405. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1406. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
  1407. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1408. goto err;
  1409. if (state->dev_ver >= 0x20) {
  1410. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
  1411. goto err;
  1412. } else {
  1413. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
  1414. goto err;
  1415. }
  1416. if (srate_coarse > 3000000) {
  1417. sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
  1418. sym = (sym / 1000) * 65536;
  1419. sym /= (state->mclk / 1000);
  1420. if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
  1421. goto err;
  1422. if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
  1423. goto err;
  1424. sym = 10 * (srate_coarse / 13); /* SFRLOW = SFR - 30% */
  1425. sym = (sym / 1000) * 65536;
  1426. sym /= (state->mclk / 1000);
  1427. if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
  1428. goto err;
  1429. if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
  1430. goto err;
  1431. sym = (srate_coarse / 1000) * 65536;
  1432. sym /= (state->mclk / 1000);
  1433. if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
  1434. goto err;
  1435. if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
  1436. goto err;
  1437. } else {
  1438. sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
  1439. sym = (sym / 100) * 65536;
  1440. sym /= (state->mclk / 100);
  1441. if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
  1442. goto err;
  1443. if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
  1444. goto err;
  1445. sym = 10 * (srate_coarse / 14); /* SFRLOW = SFR - 30% */
  1446. sym = (sym / 100) * 65536;
  1447. sym /= (state->mclk / 100);
  1448. if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
  1449. goto err;
  1450. if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
  1451. goto err;
  1452. sym = (srate_coarse / 100) * 65536;
  1453. sym /= (state->mclk / 100);
  1454. if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
  1455. goto err;
  1456. if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
  1457. goto err;
  1458. }
  1459. if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
  1460. goto err;
  1461. if (STV090x_WRITE_DEMOD(state, CFRINIT1, (freq_coarse >> 8) & 0xff) < 0)
  1462. goto err;
  1463. if (STV090x_WRITE_DEMOD(state, CFRINIT0, freq_coarse & 0xff) < 0)
  1464. goto err;
  1465. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0) /* trigger acquisition */
  1466. goto err;
  1467. }
  1468. return srate_coarse;
  1469. err:
  1470. dprintk(FE_ERROR, 1, "I/O error");
  1471. return -1;
  1472. }
  1473. static int stv090x_get_dmdlock(struct stv090x_state *state, s32 timeout)
  1474. {
  1475. s32 timer = 0, lock = 0;
  1476. u32 reg;
  1477. u8 stat;
  1478. while ((timer < timeout) && (!lock)) {
  1479. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  1480. stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
  1481. switch (stat) {
  1482. case 0: /* searching */
  1483. case 1: /* first PLH detected */
  1484. default:
  1485. dprintk(FE_DEBUG, 1, "Demodulator searching ..");
  1486. lock = 0;
  1487. break;
  1488. case 2: /* DVB-S2 mode */
  1489. case 3: /* DVB-S1/legacy mode */
  1490. reg = STV090x_READ_DEMOD(state, DSTATUS);
  1491. lock = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
  1492. break;
  1493. }
  1494. if (!lock)
  1495. msleep(10);
  1496. else
  1497. dprintk(FE_DEBUG, 1, "Demodulator acquired LOCK");
  1498. timer += 10;
  1499. }
  1500. return lock;
  1501. }
  1502. static int stv090x_blind_search(struct stv090x_state *state)
  1503. {
  1504. u32 agc2, reg, srate_coarse;
  1505. s32 timeout_dmd = 500, cpt_fail, agc2_ovflw, i;
  1506. u8 k_ref, k_max, k_min;
  1507. int coarse_fail, lock;
  1508. if (state->dev_ver < 0x20) {
  1509. k_max = 233;
  1510. k_min = 143;
  1511. } else {
  1512. k_max = 120;
  1513. k_min = 30;
  1514. }
  1515. agc2 = stv090x_get_agc2_min_level(state);
  1516. if (agc2 > STV090x_SEARCH_AGC2_TH) {
  1517. lock = 0;
  1518. } else {
  1519. if (state->dev_ver == 0x10) {
  1520. if (STV090x_WRITE_DEMOD(state, CORRELEXP, 0xaa) < 0)
  1521. goto err;
  1522. }
  1523. if (state->dev_ver < 0x20) {
  1524. if (STV090x_WRITE_DEMOD(state, CARHDR, 0x55) < 0)
  1525. goto err;
  1526. }
  1527. if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
  1528. goto err;
  1529. if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
  1530. goto err;
  1531. if (state->dev_ver >= 0x20) {
  1532. if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
  1533. goto err;
  1534. if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
  1535. goto err;
  1536. if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x82) < 0)
  1537. goto err;
  1538. if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x00) < 0) /* set viterbi hysteresis */
  1539. goto err;
  1540. }
  1541. k_ref = k_max;
  1542. do {
  1543. if (STV090x_WRITE_DEMOD(state, KREFTMG, k_ref) < 0)
  1544. goto err;
  1545. if (stv090x_srate_srch_coarse(state) != 0) {
  1546. srate_coarse = stv090x_srate_srch_fine(state);
  1547. if (srate_coarse != 0) {
  1548. stv090x_get_lock_tmg(state);
  1549. lock = stv090x_get_dmdlock(state, timeout_dmd);
  1550. } else {
  1551. lock = 0;
  1552. }
  1553. } else {
  1554. cpt_fail = 0;
  1555. agc2_ovflw = 0;
  1556. for (i = 0; i < 10; i++) {
  1557. agc2 = STV090x_READ_DEMOD(state, AGC2I1) << 8;
  1558. agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
  1559. if (agc2 >= 0xff00)
  1560. agc2_ovflw++;
  1561. reg = STV090x_READ_DEMOD(state, DSTATUS2);
  1562. if ((STV090x_GETFIELD_Px(reg, CFR_OVERFLOW_FIELD) == 0x01) &&
  1563. (STV090x_GETFIELD_Px(reg, DEMOD_DELOCK_FIELD) == 0x01))
  1564. cpt_fail++;
  1565. }
  1566. if ((cpt_fail > 7) || (agc2_ovflw > 7))
  1567. coarse_fail = 1;
  1568. lock = 0;
  1569. }
  1570. k_ref -= 30;
  1571. } while ((k_ref >= k_min) && (!lock) && (!coarse_fail));
  1572. }
  1573. return lock;
  1574. err:
  1575. dprintk(FE_ERROR, 1, "I/O error");
  1576. return -1;
  1577. }
  1578. static int stv090x_chk_tmg(struct stv090x_state *state)
  1579. {
  1580. u32 reg;
  1581. s32 tmg_cpt, i;
  1582. u8 freq, tmg_thh, tmg_thl;
  1583. int tmg_lock;
  1584. freq = STV090x_READ_DEMOD(state, CARFREQ);
  1585. tmg_thh = STV090x_READ_DEMOD(state, TMGTHRISE);
  1586. tmg_thl = STV090x_READ_DEMOD(state, TMGTHFALL);
  1587. if (STV090x_WRITE_DEMOD(state, TMGTHRISE, 0x20) < 0)
  1588. goto err;
  1589. if (STV090x_WRITE_DEMOD(state, TMGTHFALL, 0x00) < 0)
  1590. goto err;
  1591. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1592. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00); /* stop carrier offset search */
  1593. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1594. goto err;
  1595. if (STV090x_WRITE_DEMOD(state, RTC, 0x80) < 0)
  1596. goto err;
  1597. if (STV090x_WRITE_DEMOD(state, RTCS2, 0x40) < 0)
  1598. goto err;
  1599. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x00) < 0)
  1600. goto err;
  1601. if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0) /* set car ofset to 0 */
  1602. goto err;
  1603. if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
  1604. goto err;
  1605. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x65) < 0)
  1606. goto err;
  1607. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* trigger acquisition */
  1608. goto err;
  1609. msleep(10);
  1610. for (i = 0; i < 10; i++) {
  1611. reg = STV090x_READ_DEMOD(state, DSTATUS);
  1612. if (STV090x_GETFIELD_Px(reg, TMGLOCK_QUALITY_FIELD) >= 2)
  1613. tmg_cpt++;
  1614. msleep(1);
  1615. }
  1616. if (tmg_cpt >= 3)
  1617. tmg_lock = 1;
  1618. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
  1619. goto err;
  1620. if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0) /* DVB-S1 timing */
  1621. goto err;
  1622. if (STV090x_WRITE_DEMOD(state, RTCS2, 0x68) < 0) /* DVB-S2 timing */
  1623. goto err;
  1624. if (STV090x_WRITE_DEMOD(state, CARFREQ, freq) < 0)
  1625. goto err;
  1626. if (STV090x_WRITE_DEMOD(state, TMGTHRISE, tmg_thh) < 0)
  1627. goto err;
  1628. if (STV090x_WRITE_DEMOD(state, TMGTHFALL, tmg_thl) < 0)
  1629. goto err;
  1630. return tmg_lock;
  1631. err:
  1632. dprintk(FE_ERROR, 1, "I/O error");
  1633. return -1;
  1634. }
  1635. static int stv090x_get_coldlock(struct stv090x_state *state, s32 timeout_dmd)
  1636. {
  1637. struct dvb_frontend *fe = &state->frontend;
  1638. u32 reg;
  1639. s32 car_step, steps, cur_step, dir, freq, timeout_lock;
  1640. int lock = 0;
  1641. if (state->srate >= 10000000)
  1642. timeout_lock = timeout_dmd / 3;
  1643. else
  1644. timeout_lock = timeout_dmd / 2;
  1645. lock = stv090x_get_dmdlock(state, timeout_lock); /* cold start wait */
  1646. if (!lock) {
  1647. if (state->srate >= 10000000) {
  1648. if (stv090x_chk_tmg(state)) {
  1649. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  1650. goto err;
  1651. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
  1652. goto err;
  1653. lock = stv090x_get_dmdlock(state, timeout_dmd);
  1654. } else {
  1655. lock = 0;
  1656. }
  1657. } else {
  1658. if (state->srate <= 4000000)
  1659. car_step = 1000;
  1660. else if (state->srate <= 7000000)
  1661. car_step = 2000;
  1662. else if (state->srate <= 10000000)
  1663. car_step = 3000;
  1664. else
  1665. car_step = 5000;
  1666. steps = (state->search_range / 1000) / car_step;
  1667. steps /= 2;
  1668. steps = 2 * (steps + 1);
  1669. if (steps < 0)
  1670. steps = 2;
  1671. else if (steps > 12)
  1672. steps = 12;
  1673. cur_step = 1;
  1674. dir = 1;
  1675. if (!lock) {
  1676. freq = state->frequency;
  1677. state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + state->srate;
  1678. while ((cur_step <= steps) && (!lock)) {
  1679. if (dir > 0)
  1680. freq += cur_step * car_step;
  1681. else
  1682. freq -= cur_step * car_step;
  1683. /* Setup tuner */
  1684. stv090x_i2c_gate_ctrl(fe, 1);
  1685. if (state->config->tuner_set_frequency)
  1686. state->config->tuner_set_frequency(fe, state->frequency);
  1687. if (state->config->tuner_set_bandwidth)
  1688. state->config->tuner_set_bandwidth(fe, state->tuner_bw);
  1689. stv090x_i2c_gate_ctrl(fe, 0);
  1690. msleep(50);
  1691. stv090x_i2c_gate_ctrl(fe, 1);
  1692. if (state->config->tuner_get_status)
  1693. state->config->tuner_get_status(fe, &reg);
  1694. if (reg)
  1695. dprintk(FE_DEBUG, 1, "Tuner phase locked");
  1696. else
  1697. dprintk(FE_DEBUG, 1, "Tuner unlocked");
  1698. stv090x_i2c_gate_ctrl(fe, 0);
  1699. STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c);
  1700. if (state->delsys == STV090x_DVBS2) {
  1701. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  1702. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 0);
  1703. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
  1704. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1705. goto err;
  1706. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  1707. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
  1708. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  1709. goto err;
  1710. }
  1711. if (STV090x_WRITE_DEMOD(state, CFRINIT1, 0x00) < 0)
  1712. goto err;
  1713. if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0x00) < 0)
  1714. goto err;
  1715. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  1716. goto err;
  1717. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x15) < 0)
  1718. goto err;
  1719. lock = stv090x_get_dmdlock(state, (timeout_dmd / 3));
  1720. dir *= -1;
  1721. cur_step++;
  1722. }
  1723. }
  1724. }
  1725. }
  1726. return lock;
  1727. err:
  1728. dprintk(FE_ERROR, 1, "I/O error");
  1729. return -1;
  1730. }
  1731. static int stv090x_get_loop_params(struct stv090x_state *state, s32 *freq_inc, s32 *timeout_sw, s32 *steps)
  1732. {
  1733. s32 timeout, inc, steps_max, srate, car_max;
  1734. srate = state->srate;
  1735. car_max = state->search_range / 1000;
  1736. car_max = 65536 * (car_max / 2);
  1737. car_max /= (state->mclk / 1000);
  1738. if (car_max > 0x4000)
  1739. car_max = 0x4000 ; /* maxcarrier should be<= +-1/4 Mclk */
  1740. inc = srate;
  1741. inc /= state->mclk / 1000;
  1742. inc *= 256;
  1743. inc *= 256;
  1744. inc /= 1000;
  1745. switch (state->search_mode) {
  1746. case STV090x_SEARCH_DVBS1:
  1747. case STV090x_SEARCH_DSS:
  1748. inc *= 3; /* freq step = 3% of srate */
  1749. timeout = 20;
  1750. break;
  1751. case STV090x_SEARCH_DVBS2:
  1752. inc *= 4;
  1753. timeout = 25;
  1754. break;
  1755. case STV090x_SEARCH_AUTO:
  1756. default:
  1757. inc *= 3;
  1758. timeout = 25;
  1759. break;
  1760. }
  1761. inc /= 100;
  1762. if ((inc > car_max) || (inc < 0))
  1763. inc = car_max / 2; /* increment <= 1/8 Mclk */
  1764. timeout *= 27500; /* 27.5 Msps reference */
  1765. if (srate > 0)
  1766. timeout /= (srate / 1000);
  1767. if ((timeout > 100) || (timeout < 0))
  1768. timeout = 100;
  1769. steps_max = (car_max / inc) + 1; /* min steps = 3 */
  1770. if ((steps_max > 100) || (steps_max < 0)) {
  1771. steps_max = 100; /* max steps <= 100 */
  1772. inc = car_max / steps_max;
  1773. }
  1774. *freq_inc = inc;
  1775. *timeout_sw = timeout;
  1776. *steps = steps_max;
  1777. return 0;
  1778. }
  1779. static int stv090x_chk_signal(struct stv090x_state *state)
  1780. {
  1781. s32 offst_car, agc2, car_max;
  1782. int no_signal;
  1783. offst_car = STV090x_READ_DEMOD(state, CFR2) << 8;
  1784. offst_car |= STV090x_READ_DEMOD(state, CFR1);
  1785. agc2 = STV090x_READ_DEMOD(state, AGC2I1) << 8;
  1786. agc2 |= STV090x_READ_DEMOD(state, AGC2I0);
  1787. car_max = state->search_range / 1000;
  1788. car_max += (car_max / 10); /* 10% margin */
  1789. car_max = (65536 * car_max / 2);
  1790. car_max /= state->mclk / 1000;
  1791. if (car_max > 0x4000)
  1792. car_max = 0x4000;
  1793. if ((agc2 > 0x2000) || (offst_car > 2 * car_max) || (offst_car < -2 * car_max)) {
  1794. no_signal = 1;
  1795. dprintk(FE_DEBUG, 1, "No Signal");
  1796. } else {
  1797. no_signal = 0;
  1798. dprintk(FE_DEBUG, 1, "Found Signal");
  1799. }
  1800. return no_signal;
  1801. }
  1802. static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 timeout, int zigzag, s32 steps_max)
  1803. {
  1804. int no_signal, lock = 0;
  1805. s32 cpt_step, offst_freq, car_max;
  1806. u32 reg;
  1807. car_max = state->search_range / 1000;
  1808. car_max += (car_max / 10);
  1809. car_max = (65536 * car_max / 2);
  1810. car_max /= (state->mclk / 1000);
  1811. if (car_max > 0x4000)
  1812. car_max = 0x4000;
  1813. if (zigzag)
  1814. offst_freq = 0;
  1815. else
  1816. offst_freq = -car_max + inc;
  1817. cpt_step = 0;
  1818. do {
  1819. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0)
  1820. goto err;
  1821. if (STV090x_WRITE_DEMOD(state, CFRINIT1, ((offst_freq / 256) & 0xff)) < 0)
  1822. goto err;
  1823. if (STV090x_WRITE_DEMOD(state, CFRINIT0, offst_freq & 0xff) < 0)
  1824. goto err;
  1825. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
  1826. goto err;
  1827. reg = STV090x_READ_DEMOD(state, PDELCTRL1);
  1828. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x1); /* stop DVB-S2 packet delin */
  1829. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  1830. goto err;
  1831. if (state->dev_ver == 0x12) {
  1832. reg = STV090x_READ_DEMOD(state, TSCFGH);
  1833. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x1);
  1834. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  1835. goto err;
  1836. }
  1837. if (zigzag) {
  1838. if (offst_freq >= 0)
  1839. offst_freq = -offst_freq - 2 * inc;
  1840. else
  1841. offst_freq = -offst_freq;
  1842. } else {
  1843. offst_freq += 2 * inc;
  1844. }
  1845. lock = stv090x_get_dmdlock(state, timeout);
  1846. no_signal = stv090x_chk_signal(state);
  1847. } while ((!lock) &&
  1848. (!no_signal) &&
  1849. ((offst_freq - inc) < car_max) &&
  1850. ((offst_freq + inc) > -car_max) &&
  1851. (cpt_step < steps_max));
  1852. reg = STV090x_READ_DEMOD(state, PDELCTRL1);
  1853. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0);
  1854. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  1855. goto err;
  1856. return lock;
  1857. err:
  1858. dprintk(FE_ERROR, 1, "I/O error");
  1859. return -1;
  1860. }
  1861. static int stv090x_sw_algo(struct stv090x_state *state)
  1862. {
  1863. int no_signal, zigzag, lock = 0;
  1864. u32 reg;
  1865. s32 dvbs2_fly_wheel;
  1866. s32 inc, timeout_step, trials, steps_max;
  1867. stv090x_get_loop_params(state, &inc, &timeout_step, &steps_max); /* get params */
  1868. switch (state->search_mode) {
  1869. case STV090x_SEARCH_DVBS1:
  1870. case STV090x_SEARCH_DSS:
  1871. /* accelerate the frequency detector */
  1872. if (state->dev_ver >= 0x20) {
  1873. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3B) < 0)
  1874. goto err;
  1875. } else {
  1876. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
  1877. goto err;
  1878. }
  1879. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x49) < 0)
  1880. goto err;
  1881. zigzag = 0;
  1882. break;
  1883. case STV090x_SEARCH_DVBS2:
  1884. if (state->dev_ver >= 0x20) {
  1885. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
  1886. goto err;
  1887. } else {
  1888. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x68) < 0)
  1889. goto err;
  1890. }
  1891. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
  1892. goto err;
  1893. zigzag = 1;
  1894. break;
  1895. case STV090x_SEARCH_AUTO:
  1896. default:
  1897. /* accelerate the frequency detector */
  1898. if (state->dev_ver >= 0x20) {
  1899. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3b) < 0)
  1900. goto err;
  1901. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
  1902. goto err;
  1903. } else {
  1904. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xef) < 0)
  1905. goto err;
  1906. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x68) < 0)
  1907. goto err;
  1908. }
  1909. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x69) < 0)
  1910. goto err;
  1911. zigzag = 0;
  1912. break;
  1913. }
  1914. trials = 0;
  1915. do {
  1916. lock = stv090x_search_car_loop(state, inc, timeout_step, zigzag, steps_max);
  1917. no_signal = stv090x_chk_signal(state);
  1918. trials++;
  1919. /*run the SW search 2 times maximum*/
  1920. if (lock || no_signal || (trials == 2)) {
  1921. /*Check if the demod is not losing lock in DVBS2*/
  1922. if (state->dev_ver >= 0x20) {
  1923. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
  1924. goto err;
  1925. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
  1926. goto err;
  1927. } else {
  1928. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0xed) < 0)
  1929. goto err;
  1930. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x88) < 0)
  1931. goto err;
  1932. }
  1933. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  1934. if ((lock) && (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == STV090x_DVBS2)) {
  1935. /*Check if the demod is not losing lock in DVBS2*/
  1936. msleep(timeout_step);
  1937. reg = STV090x_READ_DEMOD(state, DMDFLYW);
  1938. dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
  1939. if (dvbs2_fly_wheel < 0xd) { /*if correct frames is decrementing */
  1940. msleep(timeout_step);
  1941. reg = STV090x_READ_DEMOD(state, DMDFLYW);
  1942. dvbs2_fly_wheel = STV090x_GETFIELD_Px(reg, FLYWHEEL_CPT_FIELD);
  1943. }
  1944. if (dvbs2_fly_wheel < 0xd) {
  1945. /*FALSE lock, The demod is loosing lock */
  1946. lock = 0;
  1947. if (trials < 2) {
  1948. if (state->dev_ver >= 0x20) {
  1949. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
  1950. goto err;
  1951. } else {
  1952. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x68) < 0)
  1953. goto err;
  1954. }
  1955. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, 0x89) < 0)
  1956. goto err;
  1957. }
  1958. }
  1959. }
  1960. }
  1961. } while ((!lock) && (trials < 2) && (!no_signal));
  1962. return lock;
  1963. err:
  1964. dprintk(FE_ERROR, 1, "I/O error");
  1965. return -1;
  1966. }
  1967. static enum stv090x_delsys stv090x_get_std(struct stv090x_state *state)
  1968. {
  1969. u32 reg;
  1970. enum stv090x_delsys delsys;
  1971. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  1972. if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 2)
  1973. delsys = STV090x_DVBS2;
  1974. else if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 3) {
  1975. reg = STV090x_READ_DEMOD(state, FECM);
  1976. if (STV090x_GETFIELD_Px(reg, DSS_DVB_FIELD) == 1)
  1977. delsys = STV090x_DSS;
  1978. else
  1979. delsys = STV090x_DVBS1;
  1980. } else {
  1981. delsys = STV090x_ERROR;
  1982. }
  1983. return delsys;
  1984. }
  1985. /* in Hz */
  1986. static s32 stv090x_get_car_freq(struct stv090x_state *state, u32 mclk)
  1987. {
  1988. s32 derot, int_1, int_2, tmp_1, tmp_2;
  1989. u32 pow2;
  1990. derot = STV090x_READ_DEMOD(state, CFR2) << 16;
  1991. derot |= STV090x_READ_DEMOD(state, CFR1) << 8;
  1992. derot |= STV090x_READ_DEMOD(state, CFR0);
  1993. derot = comp2(derot, 24);
  1994. pow2 = 1 << 12;
  1995. int_1 = state->mclk / pow2;
  1996. int_2 = derot / pow2;
  1997. tmp_1 = state->mclk % pow2;
  1998. tmp_2 = derot % pow2;
  1999. derot = (int_1 * int_2) +
  2000. ((int_1 * tmp_2) / pow2) +
  2001. ((int_1 * tmp_1) / pow2);
  2002. return derot;
  2003. }
  2004. static int stv090x_get_viterbi(struct stv090x_state *state)
  2005. {
  2006. u32 reg, rate;
  2007. reg = STV090x_READ_DEMOD(state, VITCURPUN);
  2008. rate = STV090x_GETFIELD_Px(reg, VIT_CURPUN_FIELD);
  2009. switch (rate) {
  2010. case 13:
  2011. state->fec = STV090x_PR12;
  2012. break;
  2013. case 18:
  2014. state->fec = STV090x_PR23;
  2015. break;
  2016. case 21:
  2017. state->fec = STV090x_PR34;
  2018. break;
  2019. case 24:
  2020. state->fec = STV090x_PR56;
  2021. break;
  2022. case 25:
  2023. state->fec = STV090x_PR67;
  2024. break;
  2025. case 26:
  2026. state->fec = STV090x_PR78;
  2027. break;
  2028. default:
  2029. state->fec = STV090x_PRERR;
  2030. break;
  2031. }
  2032. return 0;
  2033. }
  2034. static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *state)
  2035. {
  2036. struct dvb_frontend *fe = &state->frontend;
  2037. u8 tmg;
  2038. u32 reg;
  2039. s32 i = 0, offst_freq;
  2040. msleep(5);
  2041. if (state->algo == STV090x_BLIND_SEARCH) {
  2042. tmg = STV090x_READ_DEMOD(state, TMGREG2);
  2043. STV090x_WRITE_DEMOD(state, SFRSTEP, 0x5c);
  2044. while ((i <= 50) && (!tmg) && (tmg != 0xff)) {
  2045. tmg = STV090x_READ_DEMOD(state, TMGREG2);
  2046. msleep(5);
  2047. i += 5;
  2048. }
  2049. }
  2050. state->delsys = stv090x_get_std(state);
  2051. stv090x_i2c_gate_ctrl(fe, 1);
  2052. if (state->config->tuner_get_frequency)
  2053. state->config->tuner_get_frequency(fe, &state->frequency);
  2054. stv090x_i2c_gate_ctrl(fe, 0);
  2055. offst_freq = stv090x_get_car_freq(state, state->mclk) / 1000;
  2056. state->frequency += offst_freq;
  2057. stv090x_get_viterbi(state);
  2058. reg = STV090x_READ_DEMOD(state, DMDMODCOD);
  2059. state->modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
  2060. state->pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
  2061. state->frame_len = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) >> 1;
  2062. reg = STV090x_READ_DEMOD(state, TMGOBS);
  2063. state->rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
  2064. reg = STV090x_READ_DEMOD(state, FECM);
  2065. state->inversion = STV090x_GETFIELD_Px(reg, IQINV_FIELD);
  2066. if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000)) {
  2067. stv090x_i2c_gate_ctrl(fe, 1);
  2068. if (state->config->tuner_get_frequency)
  2069. state->config->tuner_get_frequency(fe, &state->frequency);
  2070. stv090x_i2c_gate_ctrl(fe, 0);
  2071. if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
  2072. return STV090x_RANGEOK;
  2073. else if (abs(offst_freq) <= (stv090x_car_width(state->srate, state->rolloff) / 2000))
  2074. return STV090x_RANGEOK;
  2075. else
  2076. return STV090x_OUTOFRANGE; /* Out of Range */
  2077. } else {
  2078. if (abs(offst_freq) <= ((state->search_range / 2000) + 500))
  2079. return STV090x_RANGEOK;
  2080. else
  2081. return STV090x_OUTOFRANGE;
  2082. }
  2083. return STV090x_OUTOFRANGE;
  2084. }
  2085. static u32 stv090x_get_tmgoffst(struct stv090x_state *state, u32 srate)
  2086. {
  2087. s32 offst_tmg;
  2088. offst_tmg = STV090x_READ_DEMOD(state, TMGREG2) << 16;
  2089. offst_tmg |= STV090x_READ_DEMOD(state, TMGREG1) << 8;
  2090. offst_tmg |= STV090x_READ_DEMOD(state, TMGREG0);
  2091. offst_tmg = comp2(offst_tmg, 24); /* 2's complement */
  2092. if (!offst_tmg)
  2093. offst_tmg = 1;
  2094. offst_tmg = ((s32) srate * 10) / ((s32) 0x1000000 / offst_tmg);
  2095. offst_tmg /= 320;
  2096. return offst_tmg;
  2097. }
  2098. static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_modcod modcod, s32 pilots)
  2099. {
  2100. u8 aclc = 0x29;
  2101. s32 i;
  2102. struct stv090x_long_frame_crloop *car_loop;
  2103. if (state->dev_ver <= 0x12)
  2104. car_loop = stv090x_s2_crl;
  2105. else if (state->dev_ver == 0x20)
  2106. car_loop = stv090x_s2_crl_cut20;
  2107. else
  2108. car_loop = stv090x_s2_crl;
  2109. if (modcod < STV090x_QPSK_12) {
  2110. i = 0;
  2111. while ((i < 3) && (modcod != stv090x_s2_lowqpsk_crl_cut20[i].modcod))
  2112. i++;
  2113. if (i >= 3)
  2114. i = 2;
  2115. } else {
  2116. i = 0;
  2117. while ((i < 14) && (modcod != car_loop[i].modcod))
  2118. i++;
  2119. if (i >= 14) {
  2120. i = 0;
  2121. while ((i < 11) && (modcod != stv090x_s2_lowqpsk_crl_cut20[i].modcod))
  2122. i++;
  2123. if (i >= 11)
  2124. i = 10;
  2125. }
  2126. }
  2127. if (modcod <= STV090x_QPSK_25) {
  2128. if (pilots) {
  2129. if (state->srate <= 3000000)
  2130. aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_on_2;
  2131. else if (state->srate <= 7000000)
  2132. aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_on_5;
  2133. else if (state->srate <= 15000000)
  2134. aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_on_10;
  2135. else if (state->srate <= 25000000)
  2136. aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_on_20;
  2137. else
  2138. aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_on_30;
  2139. } else {
  2140. if (state->srate <= 3000000)
  2141. aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_off_2;
  2142. else if (state->srate <= 7000000)
  2143. aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_off_5;
  2144. else if (state->srate <= 15000000)
  2145. aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_off_10;
  2146. else if (state->srate <= 25000000)
  2147. aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_off_20;
  2148. else
  2149. aclc = stv090x_s2_lowqpsk_crl_cut20[i].crl_pilots_off_30;
  2150. }
  2151. } else if (modcod <= STV090x_8PSK_910) {
  2152. if (pilots) {
  2153. if (state->srate <= 3000000)
  2154. aclc = car_loop[i].crl_pilots_on_2;
  2155. else if (state->srate <= 7000000)
  2156. aclc = car_loop[i].crl_pilots_on_5;
  2157. else if (state->srate <= 15000000)
  2158. aclc = car_loop[i].crl_pilots_on_10;
  2159. else if (state->srate <= 25000000)
  2160. aclc = car_loop[i].crl_pilots_on_20;
  2161. else
  2162. aclc = car_loop[i].crl_pilots_on_30;
  2163. } else {
  2164. if (state->srate <= 3000000)
  2165. aclc = car_loop[i].crl_pilots_off_2;
  2166. else if (state->srate <= 7000000)
  2167. aclc = car_loop[i].crl_pilots_off_5;
  2168. else if (state->srate <= 15000000)
  2169. aclc = car_loop[i].crl_pilots_off_10;
  2170. else if (state->srate <= 25000000)
  2171. aclc = car_loop[i].crl_pilots_off_20;
  2172. else
  2173. aclc = car_loop[i].crl_pilots_off_30;
  2174. }
  2175. } else { /* 16APSK and 32APSK */
  2176. if (state->srate <= 3000000)
  2177. aclc = stv090x_s2_apsk_crl_cut20[i].crl_pilots_on_2;
  2178. else if (state->srate <= 7000000)
  2179. aclc = stv090x_s2_apsk_crl_cut20[i].crl_pilots_on_5;
  2180. else if (state->srate <= 15000000)
  2181. aclc = stv090x_s2_apsk_crl_cut20[i].crl_pilots_on_10;
  2182. else if (state->srate <= 25000000)
  2183. aclc = stv090x_s2_apsk_crl_cut20[i].crl_pilots_on_20;
  2184. else
  2185. aclc = stv090x_s2_apsk_crl_cut20[i].crl_pilots_on_30;
  2186. }
  2187. return aclc;
  2188. }
  2189. static u8 stv090x_optimize_carloop_short(struct stv090x_state *state)
  2190. {
  2191. s32 index = 0;
  2192. u8 aclc = 0x0b;
  2193. switch (state->modulation) {
  2194. case STV090x_QPSK:
  2195. default:
  2196. index = 0;
  2197. break;
  2198. case STV090x_8PSK:
  2199. index = 1;
  2200. break;
  2201. case STV090x_16APSK:
  2202. index = 2;
  2203. break;
  2204. case STV090x_32APSK:
  2205. index = 3;
  2206. break;
  2207. }
  2208. switch (state->dev_ver) {
  2209. case 0x20:
  2210. if (state->srate <= 3000000)
  2211. aclc = stv090x_s2_short_crl[index].crl_cut20_2;
  2212. else if (state->srate <= 7000000)
  2213. aclc = stv090x_s2_short_crl[index].crl_cut20_5;
  2214. else if (state->srate <= 15000000)
  2215. aclc = stv090x_s2_short_crl[index].crl_cut20_10;
  2216. else if (state->srate <= 25000000)
  2217. aclc = stv090x_s2_short_crl[index].crl_cut20_20;
  2218. else
  2219. aclc = stv090x_s2_short_crl[index].crl_cut20_30;
  2220. break;
  2221. case 0x12:
  2222. default:
  2223. if (state->srate <= 3000000)
  2224. aclc = stv090x_s2_short_crl[index].crl_cut12_2;
  2225. else if (state->srate <= 7000000)
  2226. aclc = stv090x_s2_short_crl[index].crl_cut12_5;
  2227. else if (state->srate <= 15000000)
  2228. aclc = stv090x_s2_short_crl[index].crl_cut12_10;
  2229. else if (state->srate <= 25000000)
  2230. aclc = stv090x_s2_short_crl[index].crl_cut12_20;
  2231. else
  2232. aclc = stv090x_s2_short_crl[index].crl_cut12_30;
  2233. break;
  2234. }
  2235. return aclc;
  2236. }
  2237. static int stv090x_optimize_track(struct stv090x_state *state)
  2238. {
  2239. struct dvb_frontend *fe = &state->frontend;
  2240. enum stv090x_rolloff rolloff;
  2241. enum stv090x_modcod modcod;
  2242. s32 srate, pilots, aclc, f_1, f_0, i = 0, blind_tune = 0;
  2243. u32 reg;
  2244. srate = stv090x_get_srate(state, state->mclk);
  2245. srate += stv090x_get_tmgoffst(state, srate);
  2246. switch (state->delsys) {
  2247. case STV090x_DVBS1:
  2248. case STV090x_DSS:
  2249. if (state->algo == STV090x_SEARCH_AUTO) {
  2250. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  2251. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  2252. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
  2253. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  2254. goto err;
  2255. }
  2256. reg = STV090x_READ_DEMOD(state, DEMOD);
  2257. STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
  2258. STV090x_SETFIELD_Px(reg, MANUAL_ROLLOFF_FIELD, 0x01);
  2259. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  2260. goto err;
  2261. if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
  2262. goto err;
  2263. break;
  2264. case STV090x_DVBS2:
  2265. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  2266. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  2267. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 0);
  2268. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  2269. goto err;
  2270. if (STV090x_WRITE_DEMOD(state, ACLC, 0) < 0)
  2271. goto err;
  2272. if (STV090x_WRITE_DEMOD(state, BCLC, 0) < 0)
  2273. goto err;
  2274. if (state->frame_len == STV090x_LONG_FRAME) {
  2275. reg = STV090x_READ_DEMOD(state, DMDMODCOD);
  2276. modcod = STV090x_GETFIELD_Px(reg, DEMOD_MODCOD_FIELD);
  2277. pilots = STV090x_GETFIELD_Px(reg, DEMOD_TYPE_FIELD) & 0x01;
  2278. aclc = stv090x_optimize_carloop(state, modcod, pilots);
  2279. if (modcod <= STV090x_QPSK_910) {
  2280. STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc);
  2281. } else if (modcod <= STV090x_8PSK_910) {
  2282. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2283. goto err;
  2284. if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
  2285. goto err;
  2286. }
  2287. if ((state->demod_mode == STV090x_SINGLE) && (modcod > STV090x_8PSK_910)) {
  2288. if (modcod <= STV090x_16APSK_910) {
  2289. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2290. goto err;
  2291. if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
  2292. goto err;
  2293. } else {
  2294. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2295. goto err;
  2296. if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
  2297. goto err;
  2298. }
  2299. }
  2300. } else {
  2301. /*Carrier loop setting for short frame*/
  2302. aclc = stv090x_optimize_carloop_short(state);
  2303. if (state->modulation == STV090x_QPSK) {
  2304. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, aclc) < 0)
  2305. goto err;
  2306. } else if (state->modulation == STV090x_8PSK) {
  2307. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2308. goto err;
  2309. if (STV090x_WRITE_DEMOD(state, ACLC2S28, aclc) < 0)
  2310. goto err;
  2311. } else if (state->modulation == STV090x_16APSK) {
  2312. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2313. goto err;
  2314. if (STV090x_WRITE_DEMOD(state, ACLC2S216A, aclc) < 0)
  2315. goto err;
  2316. } else if (state->modulation == STV090x_32APSK) {
  2317. if (STV090x_WRITE_DEMOD(state, ACLC2S2Q, 0x2a) < 0)
  2318. goto err;
  2319. if (STV090x_WRITE_DEMOD(state, ACLC2S232A, aclc) < 0)
  2320. goto err;
  2321. }
  2322. }
  2323. if (state->dev_ver <= 0x11) {
  2324. if (state->demod_mode != STV090x_SINGLE)
  2325. stv090x_activate_modcod(state); /* link to LDPC after demod LOCK */
  2326. }
  2327. STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67); /* PER */
  2328. break;
  2329. case STV090x_UNKNOWN:
  2330. default:
  2331. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  2332. STV090x_SETFIELD_Px(reg, DVBS1_ENABLE_FIELD, 1);
  2333. STV090x_SETFIELD_Px(reg, DVBS2_ENABLE_FIELD, 1);
  2334. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  2335. goto err;
  2336. break;
  2337. }
  2338. f_1 = STV090x_READ_DEMOD(state, CFR2);
  2339. f_0 = STV090x_READ_DEMOD(state, CFR1);
  2340. reg = STV090x_READ_DEMOD(state, TMGOBS);
  2341. rolloff = STV090x_GETFIELD_Px(reg, ROLLOFF_STATUS_FIELD);
  2342. if (state->algo == STV090x_BLIND_SEARCH) {
  2343. STV090x_WRITE_DEMOD(state, SFRSTEP, 0x00);
  2344. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  2345. STV090x_SETFIELD_Px(reg, SCAN_ENABLE_FIELD, 0x00);
  2346. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0x00);
  2347. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  2348. goto err;
  2349. if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0x01) < 0)
  2350. goto err;
  2351. stv090x_set_srate(state, srate);
  2352. stv090x_set_max_srate(state, state->mclk, srate);
  2353. stv090x_set_min_srate(state, state->mclk, srate);
  2354. blind_tune = 1;
  2355. }
  2356. if (state->dev_ver >= 0x20) {
  2357. if ((state->search_mode == STV090x_SEARCH_DVBS1) ||
  2358. (state->search_mode == STV090x_SEARCH_DSS) ||
  2359. (state->search_mode == STV090x_SEARCH_AUTO)) {
  2360. if (STV090x_WRITE_DEMOD(state, VAVSRVIT, 0x0a) < 0)
  2361. goto err;
  2362. if (STV090x_WRITE_DEMOD(state, VITSCALE, 0x00) < 0)
  2363. goto err;
  2364. }
  2365. }
  2366. if (state->dev_ver < 0x20) {
  2367. if (STV090x_WRITE_DEMOD(state, CARHDR, 0x08) < 0)
  2368. goto err;
  2369. }
  2370. if (state->dev_ver == 0x10) {
  2371. if (STV090x_WRITE_DEMOD(state, CORRELEXP, 0x0a) < 0)
  2372. goto err;
  2373. }
  2374. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
  2375. goto err;
  2376. if ((state->dev_ver >= 0x20) || (blind_tune == 1) || (state->srate < 10000000)) {
  2377. if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
  2378. goto err;
  2379. if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
  2380. goto err;
  2381. state->tuner_bw = stv090x_car_width(srate, state->rolloff) + 10000000;
  2382. if ((state->dev_ver >= 0x20) || (blind_tune == 1)) {
  2383. if (state->algo != STV090x_WARM_SEARCH) {
  2384. stv090x_i2c_gate_ctrl(fe, 1);
  2385. if (state->config->tuner_set_bandwidth)
  2386. state->config->tuner_set_bandwidth(fe, state->tuner_bw);
  2387. stv090x_i2c_gate_ctrl(fe, 0);
  2388. }
  2389. }
  2390. if ((state->algo == STV090x_BLIND_SEARCH) || (state->srate < 10000000))
  2391. msleep(50); /* blind search: wait 50ms for SR stabilization */
  2392. else
  2393. msleep(5);
  2394. stv090x_get_lock_tmg(state);
  2395. if (!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) {
  2396. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  2397. goto err;
  2398. if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
  2399. goto err;
  2400. if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
  2401. goto err;
  2402. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
  2403. goto err;
  2404. i = 0;
  2405. while ((!(stv090x_get_dmdlock(state, (state->DemodTimeout / 2)))) && (i <= 2)) {
  2406. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1f) < 0)
  2407. goto err;
  2408. if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
  2409. goto err;
  2410. if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_0) < 0)
  2411. goto err;
  2412. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0)
  2413. goto err;
  2414. i++;
  2415. }
  2416. }
  2417. }
  2418. if (state->dev_ver >= 0x20) {
  2419. if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
  2420. goto err;
  2421. }
  2422. if ((state->delsys == STV090x_DVBS1) || (state->delsys == STV090x_DSS))
  2423. stv090x_set_vit_thtracq(state);
  2424. return 0;
  2425. err:
  2426. dprintk(FE_ERROR, 1, "I/O error");
  2427. return -1;
  2428. }
  2429. static int stv090x_get_feclock(struct stv090x_state *state, s32 timeout)
  2430. {
  2431. s32 timer = 0, lock = 0, stat;
  2432. u32 reg;
  2433. while ((timer < timeout) && (!lock)) {
  2434. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  2435. stat = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
  2436. switch (stat) {
  2437. case 0: /* searching */
  2438. case 1: /* first PLH detected */
  2439. default:
  2440. lock = 0;
  2441. break;
  2442. case 2: /* DVB-S2 mode */
  2443. reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
  2444. lock = STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD);
  2445. break;
  2446. case 3: /* DVB-S1/legacy mode */
  2447. reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
  2448. lock = STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD);
  2449. break;
  2450. }
  2451. if (!lock) {
  2452. msleep(10);
  2453. timer += 10;
  2454. }
  2455. }
  2456. return lock;
  2457. }
  2458. static int stv090x_get_lock(struct stv090x_state *state, s32 timeout_dmd, s32 timeout_fec)
  2459. {
  2460. u32 reg;
  2461. s32 timer = 0;
  2462. int lock;
  2463. lock = stv090x_get_dmdlock(state, timeout_dmd);
  2464. if (lock)
  2465. lock = stv090x_get_feclock(state, timeout_fec);
  2466. if (lock) {
  2467. lock = 0;
  2468. while ((timer < timeout_fec) && (!lock)) {
  2469. reg = STV090x_READ_DEMOD(state, TSSTATUS);
  2470. lock = STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD);
  2471. msleep(1);
  2472. timer++;
  2473. }
  2474. }
  2475. return lock;
  2476. }
  2477. static int stv090x_set_s2rolloff(struct stv090x_state *state)
  2478. {
  2479. s32 rolloff;
  2480. u32 reg;
  2481. if (state->dev_ver == 0x10) {
  2482. reg = STV090x_READ_DEMOD(state, DEMOD);
  2483. STV090x_SETFIELD_Px(reg, MANUAL_ROLLOFF_FIELD, 0x01);
  2484. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  2485. goto err;
  2486. rolloff = STV090x_READ_DEMOD(state, MATSTR1) & 0x03;
  2487. reg = STV090x_READ_DEMOD(state, DEMOD);
  2488. STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, reg);
  2489. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  2490. goto err;
  2491. } else {
  2492. reg = STV090x_READ_DEMOD(state, DEMOD);
  2493. STV090x_SETFIELD_Px(reg, MANUAL_ROLLOFF_FIELD, 0x00);
  2494. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  2495. goto err;
  2496. }
  2497. return 0;
  2498. err:
  2499. dprintk(FE_ERROR, 1, "I/O error");
  2500. return -1;
  2501. }
  2502. static enum stv090x_signal_state stv090x_acq_fixs1(struct stv090x_state *state)
  2503. {
  2504. s32 srate, f_1, f_2;
  2505. enum stv090x_signal_state signal_state = STV090x_NODATA;
  2506. u32 reg;
  2507. int lock;
  2508. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  2509. if (STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD) == 3) { /* DVB-S mode */
  2510. srate = stv090x_get_srate(state, state->mclk);
  2511. srate += stv090x_get_tmgoffst(state, state->srate);
  2512. if (state->algo == STV090x_BLIND_SEARCH)
  2513. stv090x_set_srate(state, state->srate);
  2514. stv090x_get_lock_tmg(state);
  2515. f_1 = STV090x_READ_DEMOD(state, CFR2);
  2516. f_2 = STV090x_READ_DEMOD(state, CFR1);
  2517. reg = STV090x_READ_DEMOD(state, DMDCFGMD);
  2518. STV090x_SETFIELD_Px(reg, CFR_AUTOSCAN_FIELD, 0);
  2519. if (STV090x_WRITE_DEMOD(state, DMDCFGMD, reg) < 0)
  2520. goto err;
  2521. reg = STV090x_READ_DEMOD(state, DEMOD);
  2522. STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, STV090x_IQ_SWAP);
  2523. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  2524. goto err;
  2525. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0) /* stop demod */
  2526. goto err;
  2527. if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
  2528. goto err;
  2529. if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_2) < 0)
  2530. goto err;
  2531. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* warm start trigger */
  2532. goto err;
  2533. if (stv090x_get_lock(state, state->DemodTimeout, state->FecTimeout)) {
  2534. lock = 1;
  2535. stv090x_get_sig_params(state);
  2536. stv090x_optimize_track(state);
  2537. } else {
  2538. reg = STV090x_READ_DEMOD(state, DEMOD);
  2539. STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, STV090x_IQ_NORMAL);
  2540. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  2541. goto err;
  2542. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x1c) < 0)
  2543. goto err;
  2544. if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
  2545. goto err;
  2546. if (STV090x_WRITE_DEMOD(state, CFRINIT0, f_2) < 0)
  2547. goto err;
  2548. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x18) < 0) /* warm start trigger */
  2549. goto err;
  2550. if (stv090x_get_lock(state, state->DemodTimeout, state->FecTimeout)) {
  2551. lock = 1;
  2552. signal_state = stv090x_get_sig_params(state);
  2553. stv090x_optimize_track(state);
  2554. }
  2555. }
  2556. } else {
  2557. lock = 0;
  2558. }
  2559. return signal_state;
  2560. err:
  2561. dprintk(FE_ERROR, 1, "I/O error");
  2562. return -1;
  2563. }
  2564. static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
  2565. {
  2566. struct dvb_frontend *fe = &state->frontend;
  2567. enum stv090x_signal_state signal_state = STV090x_NOCARRIER;
  2568. u32 reg;
  2569. s32 timeout_dmd = 500, timeout_fec = 50;
  2570. int lock = 0, low_sr, no_signal = 0;
  2571. reg = STV090x_READ_DEMOD(state, TSCFGH);
  2572. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* Stop path 1 stream merger */
  2573. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2574. goto err;
  2575. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod stop */
  2576. goto err;
  2577. if (state->dev_ver >= 0x20) {
  2578. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0) /* cut 2.0 */
  2579. goto err;
  2580. } else {
  2581. if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x88) < 0) /* cut 1.x */
  2582. goto err;
  2583. }
  2584. stv090x_get_lock_tmg(state);
  2585. if (state->algo == STV090x_BLIND_SEARCH) {
  2586. state->tuner_bw = 2 * 36000000; /* wide bw for unknown srate */
  2587. if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0x00) < 0) /* wider srate scan */
  2588. goto err;
  2589. stv090x_set_srate(state, 1000000); /* inital srate = 1Msps */
  2590. } else {
  2591. /* known srate */
  2592. if (STV090x_WRITE_DEMOD(state, DMDTOM, 0x20) < 0)
  2593. goto err;
  2594. if (STV090x_WRITE_DEMOD(state, TMGCFG, 0xd2) < 0)
  2595. goto err;
  2596. if (state->srate >= 10000000) {
  2597. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0) /* High SR */
  2598. goto err;
  2599. } else {
  2600. if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x60) < 0) /* Low SR */
  2601. goto err;
  2602. }
  2603. if (state->dev_ver >= 0x20) {
  2604. if (STV090x_WRITE_DEMOD(state, KREFTMG, 0x5a) < 0)
  2605. goto err;
  2606. if (state->algo == STV090x_COLD_SEARCH)
  2607. state->tuner_bw = (15 * (stv090x_car_width(state->srate, state->rolloff) + 1000000)) / 10;
  2608. else if (state->algo == STV090x_WARM_SEARCH)
  2609. state->tuner_bw = stv090x_car_width(state->srate, state->rolloff) + 10000000;
  2610. } else {
  2611. if (STV090x_WRITE_DEMOD(state, KREFTMG, 0xc1) < 0)
  2612. goto err;
  2613. state->tuner_bw = (15 * (stv090x_car_width(state->srate, state->rolloff) + 10000000)) / 10;
  2614. }
  2615. if (STV090x_WRITE_DEMOD(state, TMGCFG2, 0x01) < 0) /* narrow srate scan */
  2616. goto err;
  2617. stv090x_set_srate(state, state->srate);
  2618. stv090x_set_max_srate(state, state->mclk, state->srate);
  2619. stv090x_set_min_srate(state, state->mclk, state->srate);
  2620. if (state->srate >= 10000000)
  2621. low_sr = 1;
  2622. }
  2623. /* Setup tuner */
  2624. stv090x_i2c_gate_ctrl(fe, 1);
  2625. if (state->config->tuner_set_bbgain)
  2626. state->config->tuner_set_bbgain(fe, 10); /* 10dB */
  2627. if (state->config->tuner_set_frequency)
  2628. state->config->tuner_set_frequency(fe, state->frequency);
  2629. if (state->config->tuner_set_bandwidth)
  2630. state->config->tuner_set_bandwidth(fe, state->tuner_bw);
  2631. stv090x_i2c_gate_ctrl(fe, 0);
  2632. msleep(50);
  2633. stv090x_i2c_gate_ctrl(fe, 1);
  2634. if (state->config->tuner_get_status)
  2635. state->config->tuner_get_status(fe, &reg);
  2636. if (reg)
  2637. dprintk(FE_DEBUG, 1, "Tuner phase locked");
  2638. else
  2639. dprintk(FE_DEBUG, 1, "Tuner unlocked");
  2640. stv090x_i2c_gate_ctrl(fe, 0);
  2641. reg = STV090x_READ_DEMOD(state, DEMOD);
  2642. STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, state->inversion);
  2643. STV090x_SETFIELD_Px(reg, MANUAL_ROLLOFF_FIELD, 1);
  2644. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  2645. goto err;
  2646. stv090x_delivery_search(state);
  2647. if (state->algo == STV090x_BLIND_SEARCH)
  2648. stv090x_start_search(state);
  2649. if (state->dev_ver == 0x12) {
  2650. reg = STV090x_READ_DEMOD(state, TSCFGH);
  2651. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
  2652. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2653. goto err;
  2654. msleep(3);
  2655. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
  2656. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2657. goto err;
  2658. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
  2659. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2660. goto err;
  2661. }
  2662. if (state->algo == STV090x_BLIND_SEARCH)
  2663. lock = stv090x_blind_search(state);
  2664. else if (state->algo == STV090x_COLD_SEARCH)
  2665. lock = stv090x_get_coldlock(state, timeout_dmd);
  2666. else if (state->algo == STV090x_WARM_SEARCH)
  2667. lock = stv090x_get_dmdlock(state, timeout_dmd);
  2668. if ((!lock) && (state->algo == STV090x_COLD_SEARCH)) {
  2669. if (!low_sr) {
  2670. if (stv090x_chk_tmg(state))
  2671. lock = stv090x_sw_algo(state);
  2672. }
  2673. }
  2674. if (lock)
  2675. signal_state = stv090x_get_sig_params(state);
  2676. if ((lock) && (signal_state == STV090x_RANGEOK)) { /* signal within Range */
  2677. stv090x_optimize_track(state);
  2678. if (state->dev_ver <= 0x11) { /*workaround for dual DVBS1 cut 1.1 and 1.0 only*/
  2679. if (stv090x_get_std(state) == STV090x_DVBS1) {
  2680. msleep(20);
  2681. reg = STV090x_READ_DEMOD(state, TSCFGH);
  2682. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
  2683. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2684. goto err;
  2685. } else {
  2686. reg = STV090x_READ_DEMOD(state, TSCFGH);
  2687. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
  2688. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2689. goto err;
  2690. msleep(3);
  2691. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
  2692. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2693. goto err;
  2694. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
  2695. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2696. goto err;
  2697. }
  2698. } else if (state->dev_ver == 0x20) { /*cut 2.0 :release TS reset after demod lock and TrackingOptimization*/
  2699. reg = STV090x_READ_DEMOD(state, TSCFGH);
  2700. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
  2701. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2702. goto err;
  2703. msleep(3);
  2704. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 1); /* merger reset */
  2705. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2706. goto err;
  2707. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0); /* release merger reset */
  2708. if (STV090x_WRITE_DEMOD(state, TSCFGH, reg) < 0)
  2709. goto err;
  2710. }
  2711. if (stv090x_get_lock(state, timeout_fec, timeout_fec)) {
  2712. lock = 1;
  2713. if (state->delsys == STV090x_DVBS2) {
  2714. stv090x_set_s2rolloff(state);
  2715. if (STV090x_WRITE_DEMOD(state, PDELCTRL2, 0x40) < 0)
  2716. goto err;
  2717. if (STV090x_WRITE_DEMOD(state, PDELCTRL2, 0x00) < 0) /* RESET counter */
  2718. goto err;
  2719. if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x67) < 0) /* PER */
  2720. goto err;
  2721. } else {
  2722. if (STV090x_WRITE_DEMOD(state, ERRCTRL1, 0x75) < 0)
  2723. goto err;
  2724. }
  2725. if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0x00) < 0)
  2726. goto err;
  2727. if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
  2728. goto err;
  2729. } else {
  2730. lock = 0;
  2731. signal_state = STV090x_NODATA;
  2732. no_signal = stv090x_chk_signal(state);
  2733. }
  2734. }
  2735. if ((signal_state == STV090x_NODATA) && (!no_signal)) {
  2736. if (state->dev_ver <= 0x11) {
  2737. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  2738. if (((STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD)) == STV090x_DVBS2) && (state->inversion == INVERSION_AUTO))
  2739. signal_state = stv090x_acq_fixs1(state);
  2740. }
  2741. }
  2742. return signal_state;
  2743. err:
  2744. dprintk(FE_ERROR, 1, "I/O error");
  2745. return -1;
  2746. }
  2747. static enum dvbfe_search stv090x_search(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
  2748. {
  2749. struct stv090x_state *state = fe->demodulator_priv;
  2750. struct dtv_frontend_properties *props = &fe->dtv_property_cache;
  2751. state->delsys = props->delivery_system;
  2752. state->frequency = p->frequency;
  2753. state->srate = p->u.qpsk.symbol_rate;
  2754. if (!stv090x_algo(state)) {
  2755. dprintk(FE_DEBUG, 1, "Search success!");
  2756. return DVBFE_ALGO_SEARCH_SUCCESS;
  2757. } else {
  2758. dprintk(FE_DEBUG, 1, "Search failed!");
  2759. return DVBFE_ALGO_SEARCH_FAILED;
  2760. }
  2761. return DVBFE_ALGO_SEARCH_ERROR;
  2762. }
  2763. /* FIXME! */
  2764. static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
  2765. {
  2766. struct stv090x_state *state = fe->demodulator_priv;
  2767. u32 reg;
  2768. u8 search_state;
  2769. int locked = 0;
  2770. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  2771. search_state = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
  2772. switch (search_state) {
  2773. case 0: /* searching */
  2774. case 1: /* first PLH detected */
  2775. default:
  2776. dprintk(FE_DEBUG, 1, "Status: Unlocked (Searching ..)");
  2777. locked = 0;
  2778. break;
  2779. case 2: /* DVB-S2 mode */
  2780. dprintk(FE_DEBUG, 1, "Delivery system: DVB-S2");
  2781. reg = STV090x_READ_DEMOD(state, DSTATUS);
  2782. if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
  2783. reg = STV090x_READ_DEMOD(state, TSSTATUS);
  2784. if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
  2785. locked = 1;
  2786. *status = FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  2787. }
  2788. }
  2789. break;
  2790. case 3: /* DVB-S1/legacy mode */
  2791. dprintk(FE_DEBUG, 1, "Delivery system: DVB-S");
  2792. reg = STV090x_READ_DEMOD(state, DSTATUS);
  2793. if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
  2794. reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
  2795. if (STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD)) {
  2796. reg = STV090x_READ_DEMOD(state, TSSTATUS);
  2797. if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
  2798. locked = 1;
  2799. *status = FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  2800. }
  2801. }
  2802. }
  2803. break;
  2804. }
  2805. return locked;
  2806. }
  2807. static int stv090x_read_per(struct dvb_frontend *fe, u32 *per)
  2808. {
  2809. struct stv090x_state *state = fe->demodulator_priv;
  2810. s32 count_4, count_3, count_2, count_1, count_0, count;
  2811. u32 reg, h, m, l;
  2812. enum fe_status status;
  2813. if (!stv090x_read_status(fe, &status)) {
  2814. *per = 1 << 23; /* Max PER */
  2815. } else {
  2816. /* Counter 2 */
  2817. reg = STV090x_READ_DEMOD(state, ERRCNT22);
  2818. h = STV090x_GETFIELD_Px(reg, ERR_CNT2_FIELD);
  2819. reg = STV090x_READ_DEMOD(state, ERRCNT21);
  2820. m = STV090x_GETFIELD_Px(reg, ERR_CNT21_FIELD);
  2821. reg = STV090x_READ_DEMOD(state, ERRCNT20);
  2822. l = STV090x_GETFIELD_Px(reg, ERR_CNT20_FIELD);
  2823. *per = ((h << 16) | (m << 8) | l);
  2824. count_4 = STV090x_READ_DEMOD(state, FBERCPT4);
  2825. count_3 = STV090x_READ_DEMOD(state, FBERCPT3);
  2826. count_2 = STV090x_READ_DEMOD(state, FBERCPT2);
  2827. count_1 = STV090x_READ_DEMOD(state, FBERCPT1);
  2828. count_0 = STV090x_READ_DEMOD(state, FBERCPT0);
  2829. if ((!count_4) && (!count_3)) {
  2830. count = (count_2 & 0xff) << 16;
  2831. count |= (count_1 & 0xff) << 8;
  2832. count |= count_0 & 0xff;
  2833. } else {
  2834. count = 1 << 24;
  2835. }
  2836. if (count == 0)
  2837. *per = 1;
  2838. }
  2839. if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0) < 0)
  2840. goto err;
  2841. if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
  2842. goto err;
  2843. return 0;
  2844. err:
  2845. dprintk(FE_ERROR, 1, "I/O error");
  2846. return -1;
  2847. }
  2848. static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
  2849. {
  2850. int res = 0;
  2851. int min = 0, med;
  2852. if (val < tab[min].read)
  2853. res = tab[min].real;
  2854. else if (val >= tab[max].read)
  2855. res = tab[max].real;
  2856. else {
  2857. while ((max - min) > 1) {
  2858. med = (max + min) / 2;
  2859. if (val >= tab[min].read && val < tab[med].read)
  2860. max = med;
  2861. else
  2862. min = med;
  2863. }
  2864. res = ((val - tab[min].read) *
  2865. (tab[max].real - tab[min].real) /
  2866. (tab[max].read - tab[min].read)) +
  2867. tab[min].real;
  2868. }
  2869. return res;
  2870. }
  2871. static int stv090x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  2872. {
  2873. struct stv090x_state *state = fe->demodulator_priv;
  2874. u32 reg;
  2875. s32 agc;
  2876. reg = STV090x_READ_DEMOD(state, AGCIQIN1);
  2877. agc = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
  2878. *strength = stv090x_table_lookup(stv090x_rf_tab, ARRAY_SIZE(stv090x_rf_tab) - 1, agc);
  2879. if (agc > stv090x_rf_tab[0].read)
  2880. *strength = 5;
  2881. else if (agc < stv090x_rf_tab[ARRAY_SIZE(stv090x_rf_tab) - 1].read)
  2882. *strength = -100;
  2883. return 0;
  2884. }
  2885. static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
  2886. {
  2887. struct stv090x_state *state = fe->demodulator_priv;
  2888. u32 reg_0, reg_1, reg, i;
  2889. s32 val_0, val_1, val = 0;
  2890. u8 lock_f;
  2891. switch (state->delsys) {
  2892. case STV090x_DVBS2:
  2893. reg = STV090x_READ_DEMOD(state, DSTATUS);
  2894. lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
  2895. if (lock_f) {
  2896. msleep(5);
  2897. for (i = 0; i < 16; i++) {
  2898. reg_1 = STV090x_READ_DEMOD(state, NNOSPLHT1);
  2899. val_1 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
  2900. reg_0 = STV090x_READ_DEMOD(state, NNOSPLHT0);
  2901. val_0 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
  2902. val += MAKEWORD16(val_1, val_0);
  2903. msleep(1);
  2904. }
  2905. val /= 16;
  2906. *cnr = stv090x_table_lookup(stv090x_s2cn_tab, ARRAY_SIZE(stv090x_s2cn_tab) - 1, val);
  2907. if (val < stv090x_s2cn_tab[ARRAY_SIZE(stv090x_s2cn_tab) - 1].read)
  2908. *cnr = 1000;
  2909. }
  2910. break;
  2911. case STV090x_DVBS1:
  2912. case STV090x_DSS:
  2913. reg = STV090x_READ_DEMOD(state, DSTATUS);
  2914. lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
  2915. if (lock_f) {
  2916. msleep(5);
  2917. for (i = 0; i < 16; i++) {
  2918. reg_1 = STV090x_READ_DEMOD(state, NOSDATAT1);
  2919. val_1 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
  2920. reg_0 = STV090x_READ_DEMOD(state, NOSDATAT0);
  2921. val_0 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
  2922. val += MAKEWORD16(val_1, val_0);
  2923. msleep(1);
  2924. }
  2925. val /= 16;
  2926. *cnr = stv090x_table_lookup(stv090x_s1cn_tab, ARRAY_SIZE(stv090x_s1cn_tab) - 1, val);
  2927. if (val < stv090x_s2cn_tab[ARRAY_SIZE(stv090x_s1cn_tab) - 1].read)
  2928. *cnr = 1000;
  2929. }
  2930. break;
  2931. default:
  2932. break;
  2933. }
  2934. return 0;
  2935. }
  2936. static int stv090x_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  2937. {
  2938. struct stv090x_state *state = fe->demodulator_priv;
  2939. u32 reg;
  2940. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  2941. switch (tone) {
  2942. case SEC_TONE_ON:
  2943. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
  2944. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  2945. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  2946. goto err;
  2947. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  2948. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  2949. goto err;
  2950. break;
  2951. case SEC_TONE_OFF:
  2952. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
  2953. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  2954. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  2955. goto err;
  2956. break;
  2957. default:
  2958. return -EINVAL;
  2959. }
  2960. return 0;
  2961. err:
  2962. dprintk(FE_ERROR, 1, "I/O error");
  2963. return -1;
  2964. }
  2965. static enum dvbfe_algo stv090x_frontend_algo(struct dvb_frontend *fe)
  2966. {
  2967. return DVBFE_ALGO_CUSTOM;
  2968. }
  2969. static int stv090x_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
  2970. {
  2971. struct stv090x_state *state = fe->demodulator_priv;
  2972. u32 reg, idle = 0, fifo_full = 1;
  2973. int i;
  2974. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  2975. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 2);
  2976. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  2977. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  2978. goto err;
  2979. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  2980. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  2981. goto err;
  2982. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
  2983. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  2984. goto err;
  2985. for (i = 0; i < cmd->msg_len; i++) {
  2986. while (fifo_full) {
  2987. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  2988. fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
  2989. }
  2990. if (STV090x_WRITE_DEMOD(state, DISTXDATA, cmd->msg[i]) < 0)
  2991. goto err;
  2992. }
  2993. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  2994. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
  2995. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  2996. goto err;
  2997. i = 0;
  2998. while ((!idle) && (i < 10)) {
  2999. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3000. idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
  3001. msleep(10);
  3002. i++;
  3003. }
  3004. return 0;
  3005. err:
  3006. dprintk(FE_ERROR, 1, "I/O error");
  3007. return -1;
  3008. }
  3009. static int stv090x_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
  3010. {
  3011. struct stv090x_state *state = fe->demodulator_priv;
  3012. u32 reg, idle = 0, fifo_full = 1;
  3013. u8 mode, value;
  3014. int i;
  3015. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3016. if (burst == SEC_MINI_A) {
  3017. mode = 3;
  3018. value = 0x00;
  3019. } else {
  3020. mode = 2;
  3021. value = 0xFF;
  3022. }
  3023. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, mode);
  3024. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3025. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3026. goto err;
  3027. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  3028. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3029. goto err;
  3030. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
  3031. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3032. goto err;
  3033. while (fifo_full) {
  3034. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3035. fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
  3036. }
  3037. if (STV090x_WRITE_DEMOD(state, DISTXDATA, value) < 0)
  3038. goto err;
  3039. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3040. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
  3041. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3042. goto err;
  3043. i = 0;
  3044. while ((!idle) && (i < 10)) {
  3045. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3046. idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
  3047. msleep(10);
  3048. i++;
  3049. }
  3050. return 0;
  3051. err:
  3052. dprintk(FE_ERROR, 1, "I/O error");
  3053. return -1;
  3054. }
  3055. static int stv090x_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
  3056. {
  3057. struct stv090x_state *state = fe->demodulator_priv;
  3058. u32 reg = 0, i = 0, rx_end = 0;
  3059. while ((rx_end != 1) && (i < 10)) {
  3060. msleep(10);
  3061. i++;
  3062. reg = STV090x_READ_DEMOD(state, DISRX_ST0);
  3063. rx_end = STV090x_GETFIELD_Px(reg, RX_END_FIELD);
  3064. }
  3065. if (rx_end) {
  3066. reply->msg_len = STV090x_GETFIELD_Px(reg, FIFO_BYTENBR_FIELD);
  3067. for (i = 0; i < reply->msg_len; i++)
  3068. reply->msg[i] = STV090x_READ_DEMOD(state, DISRXDATA);
  3069. }
  3070. return 0;
  3071. }
  3072. static int stv090x_sleep(struct dvb_frontend *fe)
  3073. {
  3074. struct stv090x_state *state = fe->demodulator_priv;
  3075. u32 reg;
  3076. dprintk(FE_DEBUG, 1, "Set %s to sleep",
  3077. state->device == STV0900 ? "STV0900" : "STV0903");
  3078. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3079. STV090x_SETFIELD(reg, STANDBY_FIELD, 0x01);
  3080. if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
  3081. goto err;
  3082. reg = stv090x_read_reg(state, STV090x_TSTTNR1);
  3083. STV090x_SETFIELD(reg, ADC1_PON_FIELD, 0);
  3084. if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
  3085. goto err;
  3086. return 0;
  3087. err:
  3088. dprintk(FE_ERROR, 1, "I/O error");
  3089. return -1;
  3090. }
  3091. static int stv090x_wakeup(struct dvb_frontend *fe)
  3092. {
  3093. struct stv090x_state *state = fe->demodulator_priv;
  3094. u32 reg;
  3095. dprintk(FE_DEBUG, 1, "Wake %s from standby",
  3096. state->device == STV0900 ? "STV0900" : "STV0903");
  3097. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3098. STV090x_SETFIELD(reg, STANDBY_FIELD, 0x00);
  3099. if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
  3100. goto err;
  3101. reg = stv090x_read_reg(state, STV090x_TSTTNR1);
  3102. STV090x_SETFIELD(reg, ADC1_PON_FIELD, 1);
  3103. if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
  3104. goto err;
  3105. return 0;
  3106. err:
  3107. dprintk(FE_ERROR, 1, "I/O error");
  3108. return -1;
  3109. }
  3110. static void stv090x_release(struct dvb_frontend *fe)
  3111. {
  3112. struct stv090x_state *state = fe->demodulator_priv;
  3113. kfree(state);
  3114. }
  3115. static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc_mode)
  3116. {
  3117. u32 reg;
  3118. switch (ldpc_mode) {
  3119. case STV090x_DUAL:
  3120. default:
  3121. reg = stv090x_read_reg(state, STV090x_GENCFG);
  3122. if ((state->demod_mode != STV090x_DUAL) || (STV090x_GETFIELD(reg, DDEMOD_FIELD) != 1)) {
  3123. /* follow LDPC default state */
  3124. if (stv090x_write_reg(state, STV090x_GENCFG, reg) < 0)
  3125. goto err;
  3126. state->demod_mode = STV090x_DUAL;
  3127. reg = stv090x_read_reg(state, STV090x_TSTRES0);
  3128. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
  3129. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3130. goto err;
  3131. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
  3132. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3133. goto err;
  3134. }
  3135. break;
  3136. case STV090x_SINGLE:
  3137. if (state->demod == STV090x_DEMODULATOR_1) {
  3138. if (stv090x_write_reg(state, STV090x_GENCFG, 0x06) < 0) /* path 2 */
  3139. goto err;
  3140. } else {
  3141. if (stv090x_write_reg(state, STV090x_GENCFG, 0x04) < 0) /* path 1 */
  3142. goto err;
  3143. }
  3144. reg = stv090x_read_reg(state, STV090x_TSTRES0);
  3145. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
  3146. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3147. goto err;
  3148. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
  3149. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3150. goto err;
  3151. reg = STV090x_READ_DEMOD(state, PDELCTRL1);
  3152. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x01);
  3153. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  3154. goto err;
  3155. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x00);
  3156. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  3157. goto err;
  3158. break;
  3159. }
  3160. return 0;
  3161. err:
  3162. dprintk(FE_ERROR, 1, "I/O error");
  3163. return -1;
  3164. }
  3165. /* return (Hz), clk in Hz*/
  3166. static u32 stv090x_get_mclk(struct stv090x_state *state)
  3167. {
  3168. const struct stv090x_config *config = state->config;
  3169. u32 div, reg;
  3170. u8 ratio;
  3171. div = stv090x_read_reg(state, STV090x_NCOARSE);
  3172. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3173. ratio = STV090x_GETFIELD(reg, SELX1RATIO_FIELD) ? 4 : 6;
  3174. return (div + 1) * config->xtal / ratio; /* kHz */
  3175. }
  3176. static int stv090x_set_mclk(struct stv090x_state *state, u32 mclk, u32 clk)
  3177. {
  3178. const struct stv090x_config *config = state->config;
  3179. u32 reg, div, clk_sel;
  3180. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3181. clk_sel = ((STV090x_GETFIELD(reg, SELX1RATIO_FIELD) == 1) ? 4 : 6);
  3182. div = ((clk_sel * mclk) / config->xtal) - 1;
  3183. reg = stv090x_read_reg(state, STV090x_NCOARSE);
  3184. STV090x_SETFIELD(reg, M_DIV_FIELD, div);
  3185. if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0)
  3186. goto err;
  3187. state->mclk = stv090x_get_mclk(state);
  3188. /*Set the DiseqC frequency to 22KHz */
  3189. div = state->mclk / 704000;
  3190. if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0)
  3191. goto err;
  3192. if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
  3193. goto err;
  3194. return 0;
  3195. err:
  3196. dprintk(FE_ERROR, 1, "I/O error");
  3197. return -1;
  3198. }
  3199. static int stv090x_set_tspath(struct stv090x_state *state)
  3200. {
  3201. u32 reg;
  3202. if (state->dev_ver >= 0x20) {
  3203. switch (state->config->ts1_mode) {
  3204. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3205. case STV090x_TSMODE_DVBCI:
  3206. switch (state->config->ts2_mode) {
  3207. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3208. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3209. default:
  3210. stv090x_write_reg(state, STV090x_TSGENERAL, 0x00);
  3211. break;
  3212. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3213. case STV090x_TSMODE_DVBCI:
  3214. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x06) < 0) /* Mux'd stream mode */
  3215. goto err;
  3216. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3217. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3218. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3219. goto err;
  3220. reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
  3221. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3222. if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
  3223. goto err;
  3224. if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
  3225. goto err;
  3226. if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
  3227. goto err;
  3228. break;
  3229. }
  3230. break;
  3231. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3232. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3233. default:
  3234. switch (state->config->ts2_mode) {
  3235. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3236. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3237. default:
  3238. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
  3239. goto err;
  3240. break;
  3241. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3242. case STV090x_TSMODE_DVBCI:
  3243. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0a) < 0)
  3244. goto err;
  3245. break;
  3246. }
  3247. break;
  3248. }
  3249. } else {
  3250. switch (state->config->ts1_mode) {
  3251. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3252. case STV090x_TSMODE_DVBCI:
  3253. switch (state->config->ts2_mode) {
  3254. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3255. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3256. default:
  3257. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x10);
  3258. break;
  3259. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3260. case STV090x_TSMODE_DVBCI:
  3261. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x16);
  3262. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3263. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3264. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3265. goto err;
  3266. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3267. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 0);
  3268. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3269. goto err;
  3270. if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
  3271. goto err;
  3272. if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
  3273. goto err;
  3274. break;
  3275. }
  3276. break;
  3277. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3278. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3279. default:
  3280. switch (state->config->ts2_mode) {
  3281. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3282. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3283. default:
  3284. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
  3285. break;
  3286. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3287. case STV090x_TSMODE_DVBCI:
  3288. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x12);
  3289. break;
  3290. }
  3291. break;
  3292. }
  3293. }
  3294. switch (state->config->ts1_mode) {
  3295. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3296. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3297. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3298. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3299. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3300. goto err;
  3301. break;
  3302. case STV090x_TSMODE_DVBCI:
  3303. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3304. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3305. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3306. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3307. goto err;
  3308. break;
  3309. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3310. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3311. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3312. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3313. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3314. goto err;
  3315. break;
  3316. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3317. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3318. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3319. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3320. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3321. goto err;
  3322. break;
  3323. default:
  3324. break;
  3325. }
  3326. switch (state->config->ts2_mode) {
  3327. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3328. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3329. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3330. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3331. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3332. goto err;
  3333. break;
  3334. case STV090x_TSMODE_DVBCI:
  3335. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3336. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3337. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3338. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3339. goto err;
  3340. break;
  3341. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3342. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3343. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3344. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x00);
  3345. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3346. goto err;
  3347. break;
  3348. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3349. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3350. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3351. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3352. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3353. goto err;
  3354. break;
  3355. default:
  3356. break;
  3357. }
  3358. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3359. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
  3360. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3361. goto err;
  3362. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
  3363. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3364. goto err;
  3365. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3366. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
  3367. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3368. goto err;
  3369. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
  3370. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3371. goto err;
  3372. return 0;
  3373. err:
  3374. dprintk(FE_ERROR, 1, "I/O error");
  3375. return -1;
  3376. }
  3377. static int stv090x_init(struct dvb_frontend *fe)
  3378. {
  3379. struct stv090x_state *state = fe->demodulator_priv;
  3380. const struct stv090x_config *config = state->config;
  3381. u32 reg;
  3382. stv090x_ldpc_mode(state, state->demod_mode);
  3383. reg = STV090x_READ_DEMOD(state, TNRCFG2);
  3384. STV090x_SETFIELD_Px(reg, TUN_IQSWAP_FIELD, state->inversion);
  3385. if (STV090x_WRITE_DEMOD(state, TNRCFG2, reg) < 0)
  3386. goto err;
  3387. reg = STV090x_READ_DEMOD(state, DEMOD);
  3388. STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
  3389. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  3390. goto err;
  3391. stv090x_i2c_gate_ctrl(fe, 1);
  3392. if (config->tuner_init)
  3393. config->tuner_init(fe);
  3394. stv090x_i2c_gate_ctrl(fe, 0);
  3395. stv090x_set_tspath(state);
  3396. return 0;
  3397. err:
  3398. dprintk(FE_ERROR, 1, "I/O error");
  3399. return -1;
  3400. }
  3401. static int stv090x_setup(struct dvb_frontend *fe)
  3402. {
  3403. struct stv090x_state *state = fe->demodulator_priv;
  3404. const struct stv090x_config *config = state->config;
  3405. const struct stv090x_reg *stv090x_initval = NULL;
  3406. const struct stv090x_reg *stv090x_cut20_val = NULL;
  3407. unsigned long t1_size = 0, t2_size = 0;
  3408. u32 reg = 0;
  3409. int i;
  3410. if (state->device == STV0900) {
  3411. dprintk(FE_DEBUG, 1, "Initializing STV0900");
  3412. stv090x_initval = stv0900_initval;
  3413. t1_size = ARRAY_SIZE(stv0900_initval);
  3414. stv090x_cut20_val = stv0900_cut20_val;
  3415. t2_size = ARRAY_SIZE(stv0900_cut20_val);
  3416. } else if (state->device == STV0903) {
  3417. dprintk(FE_DEBUG, 1, "Initializing STV0903");
  3418. stv090x_initval = stv0903_initval;
  3419. t1_size = ARRAY_SIZE(stv0903_initval);
  3420. stv090x_cut20_val = stv0903_cut20_val;
  3421. t2_size = ARRAY_SIZE(stv0903_cut20_val);
  3422. }
  3423. /* STV090x init */
  3424. if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Stop Demod */
  3425. goto err;
  3426. msleep(5);
  3427. if (STV090x_WRITE_DEMOD(state, TNRCFG, 0x6c) < 0) /* check register ! (No Tuner Mode) */
  3428. goto err;
  3429. STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level);
  3430. if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0) /* repeater OFF */
  3431. goto err;
  3432. if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
  3433. goto err;
  3434. msleep(5);
  3435. if (stv090x_write_reg(state, STV090x_I2CCFG, 0x08) < 0) /* 1/41 oversampling */
  3436. goto err;
  3437. if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0) /* enable PLL */
  3438. goto err;
  3439. msleep(5);
  3440. /* write initval */
  3441. for (i = 0; i < t1_size; i++) {
  3442. dprintk(FE_DEBUG, 1, "Setting up initial values");
  3443. if (stv090x_write_reg(state, stv090x_initval[i].addr, stv090x_initval[i].data) < 0)
  3444. goto err;
  3445. }
  3446. if (state->dev_ver >= 0x20) {
  3447. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
  3448. goto err;
  3449. /* write cut20_val*/
  3450. dprintk(FE_DEBUG, 1, "Setting up Cut 2.0 initial values");
  3451. for (i = 0; i < t2_size; i++) {
  3452. if (stv090x_write_reg(state, stv090x_cut20_val[i].addr, stv090x_cut20_val[i].data) < 0)
  3453. goto err;
  3454. }
  3455. }
  3456. if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
  3457. goto err;
  3458. if (stv090x_write_reg(state, STV090x_TSTRES0, 0x00) < 0)
  3459. goto err;
  3460. stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
  3461. msleep(5);
  3462. if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0)
  3463. goto err;
  3464. stv090x_get_mclk(state);
  3465. return 0;
  3466. err:
  3467. dprintk(FE_ERROR, 1, "I/O error");
  3468. return -1;
  3469. }
  3470. static struct dvb_frontend_ops stv090x_ops = {
  3471. .info = {
  3472. .name = "STV090x Multistandard",
  3473. },
  3474. .release = stv090x_release,
  3475. .init = stv090x_init,
  3476. .sleep = stv090x_sleep,
  3477. .get_frontend_algo = stv090x_frontend_algo,
  3478. .i2c_gate_ctrl = stv090x_i2c_gate_ctrl,
  3479. .diseqc_send_master_cmd = stv090x_send_diseqc_msg,
  3480. .diseqc_send_burst = stv090x_send_diseqc_burst,
  3481. .diseqc_recv_slave_reply = stv090x_recv_slave_reply,
  3482. .set_tone = stv090x_set_tone,
  3483. .search = stv090x_search,
  3484. .read_status = stv090x_read_status,
  3485. .read_ber = stv090x_read_per,
  3486. .read_signal_strength = stv090x_read_signal_strength,
  3487. .read_snr = stv090x_read_cnr
  3488. };
  3489. struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
  3490. struct i2c_adapter *i2c,
  3491. enum stv090x_demodulator demod)
  3492. {
  3493. struct stv090x_state *state = NULL;
  3494. state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL);
  3495. if (state == NULL)
  3496. goto error;
  3497. state->verbose = &verbose;
  3498. state->config = config;
  3499. state->i2c = i2c;
  3500. state->frontend.ops = stv090x_ops;
  3501. state->frontend.demodulator_priv = state;
  3502. state->demod = demod;
  3503. state->demod_mode = config->demod_mode; /* Single or Dual mode */
  3504. state->device = config->device;
  3505. state->rolloff = 35; /* default */
  3506. if (state->demod == STV090x_DEMODULATOR_0)
  3507. mutex_init(&demod_lock);
  3508. if (stv090x_sleep(&state->frontend) < 0) {
  3509. dprintk(FE_ERROR, 1, "Error putting device to sleep");
  3510. goto error;
  3511. }
  3512. if (stv090x_setup(&state->frontend) < 0) {
  3513. dprintk(FE_ERROR, 1, "Error setting up device");
  3514. goto error;
  3515. }
  3516. if (stv090x_wakeup(&state->frontend) < 0) {
  3517. dprintk(FE_ERROR, 1, "Error waking device");
  3518. goto error;
  3519. }
  3520. state->dev_ver = stv090x_read_reg(state, STV090x_MID);
  3521. dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x\n",
  3522. state->device == STV0900 ? "STV0900" : "STV0903",
  3523. demod,
  3524. state->dev_ver);
  3525. return &state->frontend;
  3526. error:
  3527. kfree(state);
  3528. return NULL;
  3529. }
  3530. EXPORT_SYMBOL(stv090x_attach);
  3531. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  3532. MODULE_AUTHOR("Manu Abraham");
  3533. MODULE_DESCRIPTION("STV090x Multi-Std Broadcast frontend");
  3534. MODULE_LICENSE("GPL");