stv090x.c 132 KB

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