stv090x.c 124 KB

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