stv090x.c 132 KB

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