stv090x.c 137 KB

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