stv090x.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489
  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. if (p->frequency == 0)
  2966. return DVBFE_ALGO_SEARCH_INVALID;
  2967. state->delsys = props->delivery_system;
  2968. state->frequency = p->frequency;
  2969. state->srate = p->u.qpsk.symbol_rate;
  2970. state->search_mode = STV090x_SEARCH_AUTO;
  2971. state->algo = STV090x_COLD_SEARCH;
  2972. state->fec = STV090x_PRERR;
  2973. if (state->srate > 10000000) {
  2974. dprintk(FE_DEBUG, 1, "Search range: 10 MHz");
  2975. state->search_range = 10000000;
  2976. } else {
  2977. dprintk(FE_DEBUG, 1, "Search range: 5 MHz");
  2978. state->search_range = 5000000;
  2979. }
  2980. if (stv090x_algo(state) == STV090x_RANGEOK) {
  2981. dprintk(FE_DEBUG, 1, "Search success!");
  2982. return DVBFE_ALGO_SEARCH_SUCCESS;
  2983. } else {
  2984. dprintk(FE_DEBUG, 1, "Search failed!");
  2985. return DVBFE_ALGO_SEARCH_FAILED;
  2986. }
  2987. return DVBFE_ALGO_SEARCH_ERROR;
  2988. }
  2989. static int stv090x_read_status(struct dvb_frontend *fe, enum fe_status *status)
  2990. {
  2991. struct stv090x_state *state = fe->demodulator_priv;
  2992. u32 reg;
  2993. u8 search_state;
  2994. reg = STV090x_READ_DEMOD(state, DMDSTATE);
  2995. search_state = STV090x_GETFIELD_Px(reg, HEADER_MODE_FIELD);
  2996. switch (search_state) {
  2997. case 0: /* searching */
  2998. case 1: /* first PLH detected */
  2999. default:
  3000. dprintk(FE_DEBUG, 1, "Status: Unlocked (Searching ..)");
  3001. *status = 0;
  3002. break;
  3003. case 2: /* DVB-S2 mode */
  3004. dprintk(FE_DEBUG, 1, "Delivery system: DVB-S2");
  3005. reg = STV090x_READ_DEMOD(state, DSTATUS);
  3006. if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
  3007. reg = STV090x_READ_DEMOD(state, PDELSTATUS1);
  3008. if (STV090x_GETFIELD_Px(reg, PKTDELIN_LOCK_FIELD)) {
  3009. reg = STV090x_READ_DEMOD(state, TSSTATUS);
  3010. if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
  3011. *status = FE_HAS_SIGNAL |
  3012. FE_HAS_CARRIER |
  3013. FE_HAS_VITERBI |
  3014. FE_HAS_SYNC |
  3015. FE_HAS_LOCK;
  3016. }
  3017. }
  3018. }
  3019. break;
  3020. case 3: /* DVB-S1/legacy mode */
  3021. dprintk(FE_DEBUG, 1, "Delivery system: DVB-S");
  3022. reg = STV090x_READ_DEMOD(state, DSTATUS);
  3023. if (STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD)) {
  3024. reg = STV090x_READ_DEMOD(state, VSTATUSVIT);
  3025. if (STV090x_GETFIELD_Px(reg, LOCKEDVIT_FIELD)) {
  3026. reg = STV090x_READ_DEMOD(state, TSSTATUS);
  3027. if (STV090x_GETFIELD_Px(reg, TSFIFO_LINEOK_FIELD)) {
  3028. *status = FE_HAS_SIGNAL |
  3029. FE_HAS_CARRIER |
  3030. FE_HAS_VITERBI |
  3031. FE_HAS_SYNC |
  3032. FE_HAS_LOCK;
  3033. }
  3034. }
  3035. }
  3036. break;
  3037. }
  3038. return 0;
  3039. }
  3040. static int stv090x_read_per(struct dvb_frontend *fe, u32 *per)
  3041. {
  3042. struct stv090x_state *state = fe->demodulator_priv;
  3043. s32 count_4, count_3, count_2, count_1, count_0, count;
  3044. u32 reg, h, m, l;
  3045. enum fe_status status;
  3046. stv090x_read_status(fe, &status);
  3047. if (!(status & FE_HAS_LOCK)) {
  3048. *per = 1 << 23; /* Max PER */
  3049. } else {
  3050. /* Counter 2 */
  3051. reg = STV090x_READ_DEMOD(state, ERRCNT22);
  3052. h = STV090x_GETFIELD_Px(reg, ERR_CNT2_FIELD);
  3053. reg = STV090x_READ_DEMOD(state, ERRCNT21);
  3054. m = STV090x_GETFIELD_Px(reg, ERR_CNT21_FIELD);
  3055. reg = STV090x_READ_DEMOD(state, ERRCNT20);
  3056. l = STV090x_GETFIELD_Px(reg, ERR_CNT20_FIELD);
  3057. *per = ((h << 16) | (m << 8) | l);
  3058. count_4 = STV090x_READ_DEMOD(state, FBERCPT4);
  3059. count_3 = STV090x_READ_DEMOD(state, FBERCPT3);
  3060. count_2 = STV090x_READ_DEMOD(state, FBERCPT2);
  3061. count_1 = STV090x_READ_DEMOD(state, FBERCPT1);
  3062. count_0 = STV090x_READ_DEMOD(state, FBERCPT0);
  3063. if ((!count_4) && (!count_3)) {
  3064. count = (count_2 & 0xff) << 16;
  3065. count |= (count_1 & 0xff) << 8;
  3066. count |= count_0 & 0xff;
  3067. } else {
  3068. count = 1 << 24;
  3069. }
  3070. if (count == 0)
  3071. *per = 1;
  3072. }
  3073. if (STV090x_WRITE_DEMOD(state, FBERCPT4, 0) < 0)
  3074. goto err;
  3075. if (STV090x_WRITE_DEMOD(state, ERRCTRL2, 0xc1) < 0)
  3076. goto err;
  3077. return 0;
  3078. err:
  3079. dprintk(FE_ERROR, 1, "I/O error");
  3080. return -1;
  3081. }
  3082. static int stv090x_table_lookup(const struct stv090x_tab *tab, int max, int val)
  3083. {
  3084. int res = 0;
  3085. int min = 0, med;
  3086. if ((val >= tab[min].read && val < tab[max].read) ||
  3087. (val >= tab[max].read && val < tab[min].read)) {
  3088. while ((max - min) > 1) {
  3089. med = (max + min) / 2;
  3090. if ((val >= tab[min].read && val < tab[med].read) ||
  3091. (val >= tab[med].read && val < tab[min].read))
  3092. max = med;
  3093. else
  3094. min = med;
  3095. }
  3096. res = ((val - tab[min].read) *
  3097. (tab[max].real - tab[min].real) /
  3098. (tab[max].read - tab[min].read)) +
  3099. tab[min].real;
  3100. } else {
  3101. if (tab[min].read < tab[max].read) {
  3102. if (val < tab[min].read)
  3103. res = tab[min].real;
  3104. else if (val >= tab[max].read)
  3105. res = tab[max].real;
  3106. } else {
  3107. if (val >= tab[min].read)
  3108. res = tab[min].real;
  3109. else if (val < tab[max].read)
  3110. res = tab[max].real;
  3111. }
  3112. }
  3113. return res;
  3114. }
  3115. static int stv090x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  3116. {
  3117. struct stv090x_state *state = fe->demodulator_priv;
  3118. u32 reg;
  3119. s32 agc_0, agc_1, agc;
  3120. s32 str;
  3121. reg = STV090x_READ_DEMOD(state, AGCIQIN1);
  3122. agc_1 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
  3123. reg = STV090x_READ_DEMOD(state, AGCIQIN0);
  3124. agc_0 = STV090x_GETFIELD_Px(reg, AGCIQ_VALUE_FIELD);
  3125. agc = MAKEWORD16(agc_1, agc_0);
  3126. str = stv090x_table_lookup(stv090x_rf_tab,
  3127. ARRAY_SIZE(stv090x_rf_tab) - 1, agc);
  3128. if (agc > stv090x_rf_tab[0].read)
  3129. str = 0;
  3130. else if (agc < stv090x_rf_tab[ARRAY_SIZE(stv090x_rf_tab) - 1].read)
  3131. str = -100;
  3132. *strength = (str + 100) * 0xFFFF / 100;
  3133. return 0;
  3134. }
  3135. static int stv090x_read_cnr(struct dvb_frontend *fe, u16 *cnr)
  3136. {
  3137. struct stv090x_state *state = fe->demodulator_priv;
  3138. u32 reg_0, reg_1, reg, i;
  3139. s32 val_0, val_1, val = 0;
  3140. u8 lock_f;
  3141. s32 div;
  3142. u32 last;
  3143. switch (state->delsys) {
  3144. case STV090x_DVBS2:
  3145. reg = STV090x_READ_DEMOD(state, DSTATUS);
  3146. lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
  3147. if (lock_f) {
  3148. msleep(5);
  3149. for (i = 0; i < 16; i++) {
  3150. reg_1 = STV090x_READ_DEMOD(state, NNOSPLHT1);
  3151. val_1 = STV090x_GETFIELD_Px(reg_1, NOSPLHT_NORMED_FIELD);
  3152. reg_0 = STV090x_READ_DEMOD(state, NNOSPLHT0);
  3153. val_0 = STV090x_GETFIELD_Px(reg_0, NOSPLHT_NORMED_FIELD);
  3154. val += MAKEWORD16(val_1, val_0);
  3155. msleep(1);
  3156. }
  3157. val /= 16;
  3158. last = ARRAY_SIZE(stv090x_s2cn_tab) - 1;
  3159. div = stv090x_s2cn_tab[0].read -
  3160. stv090x_s2cn_tab[last].read;
  3161. *cnr = 0xFFFF - ((val * 0xFFFF) / div);
  3162. }
  3163. break;
  3164. case STV090x_DVBS1:
  3165. case STV090x_DSS:
  3166. reg = STV090x_READ_DEMOD(state, DSTATUS);
  3167. lock_f = STV090x_GETFIELD_Px(reg, LOCK_DEFINITIF_FIELD);
  3168. if (lock_f) {
  3169. msleep(5);
  3170. for (i = 0; i < 16; i++) {
  3171. reg_1 = STV090x_READ_DEMOD(state, NOSDATAT1);
  3172. val_1 = STV090x_GETFIELD_Px(reg_1, NOSDATAT_UNNORMED_FIELD);
  3173. reg_0 = STV090x_READ_DEMOD(state, NOSDATAT0);
  3174. val_0 = STV090x_GETFIELD_Px(reg_0, NOSDATAT_UNNORMED_FIELD);
  3175. val += MAKEWORD16(val_1, val_0);
  3176. msleep(1);
  3177. }
  3178. val /= 16;
  3179. last = ARRAY_SIZE(stv090x_s1cn_tab) - 1;
  3180. div = stv090x_s1cn_tab[0].read -
  3181. stv090x_s1cn_tab[last].read;
  3182. *cnr = 0xFFFF - ((val * 0xFFFF) / div);
  3183. }
  3184. break;
  3185. default:
  3186. break;
  3187. }
  3188. return 0;
  3189. }
  3190. static int stv090x_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  3191. {
  3192. struct stv090x_state *state = fe->demodulator_priv;
  3193. u32 reg;
  3194. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3195. switch (tone) {
  3196. case SEC_TONE_ON:
  3197. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
  3198. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3199. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3200. goto err;
  3201. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  3202. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3203. goto err;
  3204. break;
  3205. case SEC_TONE_OFF:
  3206. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, 0);
  3207. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3208. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3209. goto err;
  3210. break;
  3211. default:
  3212. return -EINVAL;
  3213. }
  3214. return 0;
  3215. err:
  3216. dprintk(FE_ERROR, 1, "I/O error");
  3217. return -1;
  3218. }
  3219. static enum dvbfe_algo stv090x_frontend_algo(struct dvb_frontend *fe)
  3220. {
  3221. return DVBFE_ALGO_CUSTOM;
  3222. }
  3223. static int stv090x_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
  3224. {
  3225. struct stv090x_state *state = fe->demodulator_priv;
  3226. u32 reg, idle = 0, fifo_full = 1;
  3227. int i;
  3228. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3229. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD,
  3230. (state->config->diseqc_envelope_mode) ? 4 : 2);
  3231. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3232. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3233. goto err;
  3234. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  3235. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3236. goto err;
  3237. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
  3238. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3239. goto err;
  3240. for (i = 0; i < cmd->msg_len; i++) {
  3241. while (fifo_full) {
  3242. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3243. fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
  3244. }
  3245. if (STV090x_WRITE_DEMOD(state, DISTXDATA, cmd->msg[i]) < 0)
  3246. goto err;
  3247. }
  3248. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3249. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
  3250. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3251. goto err;
  3252. i = 0;
  3253. while ((!idle) && (i < 10)) {
  3254. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3255. idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
  3256. msleep(10);
  3257. i++;
  3258. }
  3259. return 0;
  3260. err:
  3261. dprintk(FE_ERROR, 1, "I/O error");
  3262. return -1;
  3263. }
  3264. static int stv090x_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
  3265. {
  3266. struct stv090x_state *state = fe->demodulator_priv;
  3267. u32 reg, idle = 0, fifo_full = 1;
  3268. u8 mode, value;
  3269. int i;
  3270. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3271. if (burst == SEC_MINI_A) {
  3272. mode = (state->config->diseqc_envelope_mode) ? 5 : 3;
  3273. value = 0x00;
  3274. } else {
  3275. mode = (state->config->diseqc_envelope_mode) ? 4 : 2;
  3276. value = 0xFF;
  3277. }
  3278. STV090x_SETFIELD_Px(reg, DISTX_MODE_FIELD, mode);
  3279. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 1);
  3280. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3281. goto err;
  3282. STV090x_SETFIELD_Px(reg, DISEQC_RESET_FIELD, 0);
  3283. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3284. goto err;
  3285. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 1);
  3286. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3287. goto err;
  3288. while (fifo_full) {
  3289. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3290. fifo_full = STV090x_GETFIELD_Px(reg, FIFO_FULL_FIELD);
  3291. }
  3292. if (STV090x_WRITE_DEMOD(state, DISTXDATA, value) < 0)
  3293. goto err;
  3294. reg = STV090x_READ_DEMOD(state, DISTXCTL);
  3295. STV090x_SETFIELD_Px(reg, DIS_PRECHARGE_FIELD, 0);
  3296. if (STV090x_WRITE_DEMOD(state, DISTXCTL, reg) < 0)
  3297. goto err;
  3298. i = 0;
  3299. while ((!idle) && (i < 10)) {
  3300. reg = STV090x_READ_DEMOD(state, DISTXSTATUS);
  3301. idle = STV090x_GETFIELD_Px(reg, TX_IDLE_FIELD);
  3302. msleep(10);
  3303. i++;
  3304. }
  3305. return 0;
  3306. err:
  3307. dprintk(FE_ERROR, 1, "I/O error");
  3308. return -1;
  3309. }
  3310. static int stv090x_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
  3311. {
  3312. struct stv090x_state *state = fe->demodulator_priv;
  3313. u32 reg = 0, i = 0, rx_end = 0;
  3314. while ((rx_end != 1) && (i < 10)) {
  3315. msleep(10);
  3316. i++;
  3317. reg = STV090x_READ_DEMOD(state, DISRX_ST0);
  3318. rx_end = STV090x_GETFIELD_Px(reg, RX_END_FIELD);
  3319. }
  3320. if (rx_end) {
  3321. reply->msg_len = STV090x_GETFIELD_Px(reg, FIFO_BYTENBR_FIELD);
  3322. for (i = 0; i < reply->msg_len; i++)
  3323. reply->msg[i] = STV090x_READ_DEMOD(state, DISRXDATA);
  3324. }
  3325. return 0;
  3326. }
  3327. static int stv090x_sleep(struct dvb_frontend *fe)
  3328. {
  3329. struct stv090x_state *state = fe->demodulator_priv;
  3330. u32 reg;
  3331. dprintk(FE_DEBUG, 1, "Set %s to sleep",
  3332. state->device == STV0900 ? "STV0900" : "STV0903");
  3333. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3334. STV090x_SETFIELD(reg, STANDBY_FIELD, 0x01);
  3335. if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
  3336. goto err;
  3337. reg = stv090x_read_reg(state, STV090x_TSTTNR1);
  3338. STV090x_SETFIELD(reg, ADC1_PON_FIELD, 0);
  3339. if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
  3340. goto err;
  3341. return 0;
  3342. err:
  3343. dprintk(FE_ERROR, 1, "I/O error");
  3344. return -1;
  3345. }
  3346. static int stv090x_wakeup(struct dvb_frontend *fe)
  3347. {
  3348. struct stv090x_state *state = fe->demodulator_priv;
  3349. u32 reg;
  3350. dprintk(FE_DEBUG, 1, "Wake %s from standby",
  3351. state->device == STV0900 ? "STV0900" : "STV0903");
  3352. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3353. STV090x_SETFIELD(reg, STANDBY_FIELD, 0x00);
  3354. if (stv090x_write_reg(state, STV090x_SYNTCTRL, reg) < 0)
  3355. goto err;
  3356. reg = stv090x_read_reg(state, STV090x_TSTTNR1);
  3357. STV090x_SETFIELD(reg, ADC1_PON_FIELD, 1);
  3358. if (stv090x_write_reg(state, STV090x_TSTTNR1, reg) < 0)
  3359. goto err;
  3360. return 0;
  3361. err:
  3362. dprintk(FE_ERROR, 1, "I/O error");
  3363. return -1;
  3364. }
  3365. static void stv090x_release(struct dvb_frontend *fe)
  3366. {
  3367. struct stv090x_state *state = fe->demodulator_priv;
  3368. state->internal->num_used--;
  3369. if (state->internal->num_used <= 0) {
  3370. dprintk(FE_ERROR, 1, "Actually removing");
  3371. remove_dev(state->internal);
  3372. kfree(state->internal);
  3373. }
  3374. kfree(state);
  3375. }
  3376. static int stv090x_ldpc_mode(struct stv090x_state *state, enum stv090x_mode ldpc_mode)
  3377. {
  3378. u32 reg = 0;
  3379. reg = stv090x_read_reg(state, STV090x_GENCFG);
  3380. switch (ldpc_mode) {
  3381. case STV090x_DUAL:
  3382. default:
  3383. if ((state->demod_mode != STV090x_DUAL) || (STV090x_GETFIELD(reg, DDEMOD_FIELD) != 1)) {
  3384. /* set LDPC to dual mode */
  3385. if (stv090x_write_reg(state, STV090x_GENCFG, 0x1d) < 0)
  3386. goto err;
  3387. state->demod_mode = STV090x_DUAL;
  3388. reg = stv090x_read_reg(state, STV090x_TSTRES0);
  3389. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
  3390. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3391. goto err;
  3392. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
  3393. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3394. goto err;
  3395. if (STV090x_WRITE_DEMOD(state, MODCODLST0, 0xff) < 0)
  3396. goto err;
  3397. if (STV090x_WRITE_DEMOD(state, MODCODLST1, 0xff) < 0)
  3398. goto err;
  3399. if (STV090x_WRITE_DEMOD(state, MODCODLST2, 0xff) < 0)
  3400. goto err;
  3401. if (STV090x_WRITE_DEMOD(state, MODCODLST3, 0xff) < 0)
  3402. goto err;
  3403. if (STV090x_WRITE_DEMOD(state, MODCODLST4, 0xff) < 0)
  3404. goto err;
  3405. if (STV090x_WRITE_DEMOD(state, MODCODLST5, 0xff) < 0)
  3406. goto err;
  3407. if (STV090x_WRITE_DEMOD(state, MODCODLST6, 0xff) < 0)
  3408. goto err;
  3409. if (STV090x_WRITE_DEMOD(state, MODCODLST7, 0xcc) < 0)
  3410. goto err;
  3411. if (STV090x_WRITE_DEMOD(state, MODCODLST8, 0xcc) < 0)
  3412. goto err;
  3413. if (STV090x_WRITE_DEMOD(state, MODCODLST9, 0xcc) < 0)
  3414. goto err;
  3415. if (STV090x_WRITE_DEMOD(state, MODCODLSTA, 0xcc) < 0)
  3416. goto err;
  3417. if (STV090x_WRITE_DEMOD(state, MODCODLSTB, 0xcc) < 0)
  3418. goto err;
  3419. if (STV090x_WRITE_DEMOD(state, MODCODLSTC, 0xcc) < 0)
  3420. goto err;
  3421. if (STV090x_WRITE_DEMOD(state, MODCODLSTD, 0xcc) < 0)
  3422. goto err;
  3423. if (STV090x_WRITE_DEMOD(state, MODCODLSTE, 0xff) < 0)
  3424. goto err;
  3425. if (STV090x_WRITE_DEMOD(state, MODCODLSTF, 0xcf) < 0)
  3426. goto err;
  3427. }
  3428. break;
  3429. case STV090x_SINGLE:
  3430. if (stv090x_stop_modcod(state) < 0)
  3431. goto err;
  3432. if (stv090x_activate_modcod_single(state) < 0)
  3433. goto err;
  3434. if (state->demod == STV090x_DEMODULATOR_1) {
  3435. if (stv090x_write_reg(state, STV090x_GENCFG, 0x06) < 0) /* path 2 */
  3436. goto err;
  3437. } else {
  3438. if (stv090x_write_reg(state, STV090x_GENCFG, 0x04) < 0) /* path 1 */
  3439. goto err;
  3440. }
  3441. reg = stv090x_read_reg(state, STV090x_TSTRES0);
  3442. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x1);
  3443. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3444. goto err;
  3445. STV090x_SETFIELD(reg, FRESFEC_FIELD, 0x0);
  3446. if (stv090x_write_reg(state, STV090x_TSTRES0, reg) < 0)
  3447. goto err;
  3448. reg = STV090x_READ_DEMOD(state, PDELCTRL1);
  3449. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x01);
  3450. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  3451. goto err;
  3452. STV090x_SETFIELD_Px(reg, ALGOSWRST_FIELD, 0x00);
  3453. if (STV090x_WRITE_DEMOD(state, PDELCTRL1, reg) < 0)
  3454. goto err;
  3455. break;
  3456. }
  3457. return 0;
  3458. err:
  3459. dprintk(FE_ERROR, 1, "I/O error");
  3460. return -1;
  3461. }
  3462. /* return (Hz), clk in Hz*/
  3463. static u32 stv090x_get_mclk(struct stv090x_state *state)
  3464. {
  3465. const struct stv090x_config *config = state->config;
  3466. u32 div, reg;
  3467. u8 ratio;
  3468. div = stv090x_read_reg(state, STV090x_NCOARSE);
  3469. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3470. ratio = STV090x_GETFIELD(reg, SELX1RATIO_FIELD) ? 4 : 6;
  3471. return (div + 1) * config->xtal / ratio; /* kHz */
  3472. }
  3473. static int stv090x_set_mclk(struct stv090x_state *state, u32 mclk, u32 clk)
  3474. {
  3475. const struct stv090x_config *config = state->config;
  3476. u32 reg, div, clk_sel;
  3477. reg = stv090x_read_reg(state, STV090x_SYNTCTRL);
  3478. clk_sel = ((STV090x_GETFIELD(reg, SELX1RATIO_FIELD) == 1) ? 4 : 6);
  3479. div = ((clk_sel * mclk) / config->xtal) - 1;
  3480. reg = stv090x_read_reg(state, STV090x_NCOARSE);
  3481. STV090x_SETFIELD(reg, M_DIV_FIELD, div);
  3482. if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0)
  3483. goto err;
  3484. state->internal->mclk = stv090x_get_mclk(state);
  3485. /*Set the DiseqC frequency to 22KHz */
  3486. div = state->internal->mclk / 704000;
  3487. if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0)
  3488. goto err;
  3489. if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
  3490. goto err;
  3491. return 0;
  3492. err:
  3493. dprintk(FE_ERROR, 1, "I/O error");
  3494. return -1;
  3495. }
  3496. static int stv090x_set_tspath(struct stv090x_state *state)
  3497. {
  3498. u32 reg;
  3499. if (state->internal->dev_ver >= 0x20) {
  3500. switch (state->config->ts1_mode) {
  3501. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3502. case STV090x_TSMODE_DVBCI:
  3503. switch (state->config->ts2_mode) {
  3504. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3505. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3506. default:
  3507. stv090x_write_reg(state, STV090x_TSGENERAL, 0x00);
  3508. break;
  3509. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3510. case STV090x_TSMODE_DVBCI:
  3511. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x06) < 0) /* Mux'd stream mode */
  3512. goto err;
  3513. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3514. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3515. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3516. goto err;
  3517. reg = stv090x_read_reg(state, STV090x_P2_TSCFGM);
  3518. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3519. if (stv090x_write_reg(state, STV090x_P2_TSCFGM, reg) < 0)
  3520. goto err;
  3521. if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
  3522. goto err;
  3523. if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
  3524. goto err;
  3525. break;
  3526. }
  3527. break;
  3528. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3529. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3530. default:
  3531. switch (state->config->ts2_mode) {
  3532. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3533. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3534. default:
  3535. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
  3536. goto err;
  3537. break;
  3538. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3539. case STV090x_TSMODE_DVBCI:
  3540. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0a) < 0)
  3541. goto err;
  3542. break;
  3543. }
  3544. break;
  3545. }
  3546. } else {
  3547. switch (state->config->ts1_mode) {
  3548. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3549. case STV090x_TSMODE_DVBCI:
  3550. switch (state->config->ts2_mode) {
  3551. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3552. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3553. default:
  3554. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x10);
  3555. break;
  3556. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3557. case STV090x_TSMODE_DVBCI:
  3558. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x16);
  3559. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3560. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 3);
  3561. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3562. goto err;
  3563. reg = stv090x_read_reg(state, STV090x_P1_TSCFGM);
  3564. STV090x_SETFIELD_Px(reg, TSFIFO_MANSPEED_FIELD, 0);
  3565. if (stv090x_write_reg(state, STV090x_P1_TSCFGM, reg) < 0)
  3566. goto err;
  3567. if (stv090x_write_reg(state, STV090x_P1_TSSPEED, 0x14) < 0)
  3568. goto err;
  3569. if (stv090x_write_reg(state, STV090x_P2_TSSPEED, 0x28) < 0)
  3570. goto err;
  3571. break;
  3572. }
  3573. break;
  3574. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3575. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3576. default:
  3577. switch (state->config->ts2_mode) {
  3578. case STV090x_TSMODE_SERIAL_PUNCTURED:
  3579. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3580. default:
  3581. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x14);
  3582. break;
  3583. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  3584. case STV090x_TSMODE_DVBCI:
  3585. stv090x_write_reg(state, STV090x_TSGENERAL1X, 0x12);
  3586. break;
  3587. }
  3588. break;
  3589. }
  3590. }
  3591. switch (state->config->ts1_mode) {
  3592. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  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, 0x00);
  3596. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3597. goto err;
  3598. break;
  3599. case STV090x_TSMODE_DVBCI:
  3600. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3601. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3602. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3603. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3604. goto err;
  3605. break;
  3606. case STV090x_TSMODE_SERIAL_PUNCTURED:
  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, 0x00);
  3610. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3611. goto err;
  3612. break;
  3613. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3614. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3615. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3616. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3617. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3618. goto err;
  3619. break;
  3620. default:
  3621. break;
  3622. }
  3623. switch (state->config->ts2_mode) {
  3624. case STV090x_TSMODE_PARALLEL_PUNCTURED:
  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, 0x00);
  3628. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3629. goto err;
  3630. break;
  3631. case STV090x_TSMODE_DVBCI:
  3632. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3633. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x00);
  3634. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3635. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3636. goto err;
  3637. break;
  3638. case STV090x_TSMODE_SERIAL_PUNCTURED:
  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, 0x00);
  3642. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3643. goto err;
  3644. break;
  3645. case STV090x_TSMODE_SERIAL_CONTINUOUS:
  3646. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3647. STV090x_SETFIELD_Px(reg, TSFIFO_SERIAL_FIELD, 0x01);
  3648. STV090x_SETFIELD_Px(reg, TSFIFO_DVBCI_FIELD, 0x01);
  3649. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3650. goto err;
  3651. break;
  3652. default:
  3653. break;
  3654. }
  3655. reg = stv090x_read_reg(state, STV090x_P2_TSCFGH);
  3656. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
  3657. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3658. goto err;
  3659. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
  3660. if (stv090x_write_reg(state, STV090x_P2_TSCFGH, reg) < 0)
  3661. goto err;
  3662. reg = stv090x_read_reg(state, STV090x_P1_TSCFGH);
  3663. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x01);
  3664. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3665. goto err;
  3666. STV090x_SETFIELD_Px(reg, RST_HWARE_FIELD, 0x00);
  3667. if (stv090x_write_reg(state, STV090x_P1_TSCFGH, reg) < 0)
  3668. goto err;
  3669. return 0;
  3670. err:
  3671. dprintk(FE_ERROR, 1, "I/O error");
  3672. return -1;
  3673. }
  3674. static int stv090x_init(struct dvb_frontend *fe)
  3675. {
  3676. struct stv090x_state *state = fe->demodulator_priv;
  3677. const struct stv090x_config *config = state->config;
  3678. u32 reg;
  3679. if (stv090x_wakeup(fe) < 0) {
  3680. dprintk(FE_ERROR, 1, "Error waking device");
  3681. goto err;
  3682. }
  3683. if (stv090x_ldpc_mode(state, state->demod_mode) < 0)
  3684. goto err;
  3685. reg = STV090x_READ_DEMOD(state, TNRCFG2);
  3686. STV090x_SETFIELD_Px(reg, TUN_IQSWAP_FIELD, state->inversion);
  3687. if (STV090x_WRITE_DEMOD(state, TNRCFG2, reg) < 0)
  3688. goto err;
  3689. reg = STV090x_READ_DEMOD(state, DEMOD);
  3690. STV090x_SETFIELD_Px(reg, ROLLOFF_CONTROL_FIELD, state->rolloff);
  3691. if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
  3692. goto err;
  3693. if (stv090x_i2c_gate_ctrl(fe, 1) < 0)
  3694. goto err;
  3695. if (config->tuner_set_mode) {
  3696. if (config->tuner_set_mode(fe, TUNER_WAKE) < 0)
  3697. goto err;
  3698. }
  3699. if (config->tuner_init) {
  3700. if (config->tuner_init(fe) < 0)
  3701. goto err;
  3702. }
  3703. if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
  3704. goto err;
  3705. if (stv090x_set_tspath(state) < 0)
  3706. goto err;
  3707. return 0;
  3708. err:
  3709. dprintk(FE_ERROR, 1, "I/O error");
  3710. return -1;
  3711. }
  3712. static int stv090x_setup(struct dvb_frontend *fe)
  3713. {
  3714. struct stv090x_state *state = fe->demodulator_priv;
  3715. const struct stv090x_config *config = state->config;
  3716. const struct stv090x_reg *stv090x_initval = NULL;
  3717. const struct stv090x_reg *stv090x_cut20_val = NULL;
  3718. unsigned long t1_size = 0, t2_size = 0;
  3719. u32 reg = 0;
  3720. int i;
  3721. if (state->device == STV0900) {
  3722. dprintk(FE_DEBUG, 1, "Initializing STV0900");
  3723. stv090x_initval = stv0900_initval;
  3724. t1_size = ARRAY_SIZE(stv0900_initval);
  3725. stv090x_cut20_val = stv0900_cut20_val;
  3726. t2_size = ARRAY_SIZE(stv0900_cut20_val);
  3727. } else if (state->device == STV0903) {
  3728. dprintk(FE_DEBUG, 1, "Initializing STV0903");
  3729. stv090x_initval = stv0903_initval;
  3730. t1_size = ARRAY_SIZE(stv0903_initval);
  3731. stv090x_cut20_val = stv0903_cut20_val;
  3732. t2_size = ARRAY_SIZE(stv0903_cut20_val);
  3733. }
  3734. /* STV090x init */
  3735. /* Stop Demod */
  3736. if (stv090x_write_reg(state, STV090x_P1_DMDISTATE, 0x5c) < 0)
  3737. goto err;
  3738. if (stv090x_write_reg(state, STV090x_P2_DMDISTATE, 0x5c) < 0)
  3739. goto err;
  3740. msleep(5);
  3741. /* Set No Tuner Mode */
  3742. if (stv090x_write_reg(state, STV090x_P1_TNRCFG, 0x6c) < 0)
  3743. goto err;
  3744. if (stv090x_write_reg(state, STV090x_P2_TNRCFG, 0x6c) < 0)
  3745. goto err;
  3746. /* I2C repeater OFF */
  3747. STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level);
  3748. if (stv090x_write_reg(state, STV090x_P1_I2CRPT, reg) < 0)
  3749. goto err;
  3750. if (stv090x_write_reg(state, STV090x_P2_I2CRPT, reg) < 0)
  3751. goto err;
  3752. if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
  3753. goto err;
  3754. msleep(5);
  3755. if (stv090x_write_reg(state, STV090x_I2CCFG, 0x08) < 0) /* 1/41 oversampling */
  3756. goto err;
  3757. if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0) /* enable PLL */
  3758. goto err;
  3759. msleep(5);
  3760. /* write initval */
  3761. dprintk(FE_DEBUG, 1, "Setting up initial values");
  3762. for (i = 0; i < t1_size; i++) {
  3763. if (stv090x_write_reg(state, stv090x_initval[i].addr, stv090x_initval[i].data) < 0)
  3764. goto err;
  3765. }
  3766. state->internal->dev_ver = stv090x_read_reg(state, STV090x_MID);
  3767. if (state->internal->dev_ver >= 0x20) {
  3768. if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
  3769. goto err;
  3770. /* write cut20_val*/
  3771. dprintk(FE_DEBUG, 1, "Setting up Cut 2.0 initial values");
  3772. for (i = 0; i < t2_size; i++) {
  3773. if (stv090x_write_reg(state, stv090x_cut20_val[i].addr, stv090x_cut20_val[i].data) < 0)
  3774. goto err;
  3775. }
  3776. } else if (state->internal->dev_ver < 0x20) {
  3777. dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!",
  3778. state->internal->dev_ver);
  3779. goto err;
  3780. } else if (state->internal->dev_ver > 0x30) {
  3781. /* we shouldn't bail out from here */
  3782. dprintk(FE_ERROR, 1, "INFO: Cut: 0x%02x probably incomplete support!",
  3783. state->internal->dev_ver);
  3784. }
  3785. if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
  3786. goto err;
  3787. if (stv090x_write_reg(state, STV090x_TSTRES0, 0x00) < 0)
  3788. goto err;
  3789. stv090x_set_mclk(state, 135000000, config->xtal); /* 135 Mhz */
  3790. msleep(5);
  3791. if (stv090x_write_reg(state, STV090x_SYNTCTRL, 0x20 | config->clk_mode) < 0)
  3792. goto err;
  3793. stv090x_get_mclk(state);
  3794. return 0;
  3795. err:
  3796. dprintk(FE_ERROR, 1, "I/O error");
  3797. return -1;
  3798. }
  3799. static struct dvb_frontend_ops stv090x_ops = {
  3800. .info = {
  3801. .name = "STV090x Multistandard",
  3802. .type = FE_QPSK,
  3803. .frequency_min = 950000,
  3804. .frequency_max = 2150000,
  3805. .frequency_stepsize = 0,
  3806. .frequency_tolerance = 0,
  3807. .symbol_rate_min = 1000000,
  3808. .symbol_rate_max = 45000000,
  3809. .caps = FE_CAN_INVERSION_AUTO |
  3810. FE_CAN_FEC_AUTO |
  3811. FE_CAN_QPSK |
  3812. FE_CAN_2G_MODULATION
  3813. },
  3814. .release = stv090x_release,
  3815. .init = stv090x_init,
  3816. .sleep = stv090x_sleep,
  3817. .get_frontend_algo = stv090x_frontend_algo,
  3818. .i2c_gate_ctrl = stv090x_i2c_gate_ctrl,
  3819. .diseqc_send_master_cmd = stv090x_send_diseqc_msg,
  3820. .diseqc_send_burst = stv090x_send_diseqc_burst,
  3821. .diseqc_recv_slave_reply = stv090x_recv_slave_reply,
  3822. .set_tone = stv090x_set_tone,
  3823. .search = stv090x_search,
  3824. .read_status = stv090x_read_status,
  3825. .read_ber = stv090x_read_per,
  3826. .read_signal_strength = stv090x_read_signal_strength,
  3827. .read_snr = stv090x_read_cnr
  3828. };
  3829. struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
  3830. struct i2c_adapter *i2c,
  3831. enum stv090x_demodulator demod)
  3832. {
  3833. struct stv090x_state *state = NULL;
  3834. struct stv090x_dev *temp_int;
  3835. state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL);
  3836. if (state == NULL)
  3837. goto error;
  3838. state->verbose = &verbose;
  3839. state->config = config;
  3840. state->i2c = i2c;
  3841. state->frontend.ops = stv090x_ops;
  3842. state->frontend.demodulator_priv = state;
  3843. state->demod = demod;
  3844. state->demod_mode = config->demod_mode; /* Single or Dual mode */
  3845. state->device = config->device;
  3846. state->rolloff = STV090x_RO_35; /* default */
  3847. temp_int = find_dev(state->i2c,
  3848. state->config->address);
  3849. if ((temp_int != NULL) && (state->demod_mode == STV090x_DUAL)) {
  3850. state->internal = temp_int->internal;
  3851. state->internal->num_used++;
  3852. dprintk(FE_INFO, 1, "Found Internal Structure!");
  3853. dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
  3854. state->device == STV0900 ? "STV0900" : "STV0903",
  3855. demod,
  3856. state->internal->dev_ver);
  3857. return &state->frontend;
  3858. } else {
  3859. state->internal = kmalloc(sizeof(struct stv090x_internal),
  3860. GFP_KERNEL);
  3861. temp_int = append_internal(state->internal);
  3862. state->internal->num_used = 1;
  3863. state->internal->i2c_adap = state->i2c;
  3864. state->internal->i2c_addr = state->config->address;
  3865. dprintk(FE_INFO, 1, "Create New Internal Structure!");
  3866. }
  3867. mutex_init(&state->internal->demod_lock);
  3868. mutex_init(&state->internal->tuner_lock);
  3869. if (stv090x_sleep(&state->frontend) < 0) {
  3870. dprintk(FE_ERROR, 1, "Error putting device to sleep");
  3871. goto error;
  3872. }
  3873. if (stv090x_setup(&state->frontend) < 0) {
  3874. dprintk(FE_ERROR, 1, "Error setting up device");
  3875. goto error;
  3876. }
  3877. if (stv090x_wakeup(&state->frontend) < 0) {
  3878. dprintk(FE_ERROR, 1, "Error waking device");
  3879. goto error;
  3880. }
  3881. dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
  3882. state->device == STV0900 ? "STV0900" : "STV0903",
  3883. demod,
  3884. state->internal->dev_ver);
  3885. return &state->frontend;
  3886. error:
  3887. kfree(state);
  3888. return NULL;
  3889. }
  3890. EXPORT_SYMBOL(stv090x_attach);
  3891. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  3892. MODULE_AUTHOR("Manu Abraham");
  3893. MODULE_DESCRIPTION("STV090x Multi-Std Broadcast frontend");
  3894. MODULE_LICENSE("GPL");