stv090x.c 137 KB

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