stv090x.c 122 KB

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