drxk_hard.c 177 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drxk_hard: DRX-K DVB-C/T demodulator driver
  4. *
  5. * Copyright (C) 2010-2011 Digital Devices GmbH
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/firmware.h>
  14. #include <linux/i2c.h>
  15. #include <linux/hardirq.h>
  16. #include <asm/div64.h>
  17. #include <media/dvb_frontend.h>
  18. #include "drxk.h"
  19. #include "drxk_hard.h"
  20. #include <media/dvb_math.h>
  21. static int power_down_dvbt(struct drxk_state *state, bool set_power_mode);
  22. static int power_down_qam(struct drxk_state *state);
  23. static int set_dvbt_standard(struct drxk_state *state,
  24. enum operation_mode o_mode);
  25. static int set_qam_standard(struct drxk_state *state,
  26. enum operation_mode o_mode);
  27. static int set_qam(struct drxk_state *state, u16 intermediate_freqk_hz,
  28. s32 tuner_freq_offset);
  29. static int set_dvbt_standard(struct drxk_state *state,
  30. enum operation_mode o_mode);
  31. static int dvbt_start(struct drxk_state *state);
  32. static int set_dvbt(struct drxk_state *state, u16 intermediate_freqk_hz,
  33. s32 tuner_freq_offset);
  34. static int get_qam_lock_status(struct drxk_state *state, u32 *p_lock_status);
  35. static int get_dvbt_lock_status(struct drxk_state *state, u32 *p_lock_status);
  36. static int switch_antenna_to_qam(struct drxk_state *state);
  37. static int switch_antenna_to_dvbt(struct drxk_state *state);
  38. static bool is_dvbt(struct drxk_state *state)
  39. {
  40. return state->m_operation_mode == OM_DVBT;
  41. }
  42. static bool is_qam(struct drxk_state *state)
  43. {
  44. return state->m_operation_mode == OM_QAM_ITU_A ||
  45. state->m_operation_mode == OM_QAM_ITU_B ||
  46. state->m_operation_mode == OM_QAM_ITU_C;
  47. }
  48. #define NOA1ROM 0
  49. #define DRXDAP_FASI_SHORT_FORMAT(addr) (((addr) & 0xFC30FF80) == 0)
  50. #define DRXDAP_FASI_LONG_FORMAT(addr) (((addr) & 0xFC30FF80) != 0)
  51. #define DEFAULT_MER_83 165
  52. #define DEFAULT_MER_93 250
  53. #ifndef DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH
  54. #define DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH (0x02)
  55. #endif
  56. #ifndef DRXK_MPEG_PARALLEL_OUTPUT_PIN_DRIVE_STRENGTH
  57. #define DRXK_MPEG_PARALLEL_OUTPUT_PIN_DRIVE_STRENGTH (0x03)
  58. #endif
  59. #define DEFAULT_DRXK_MPEG_LOCK_TIMEOUT 700
  60. #define DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT 500
  61. #ifndef DRXK_KI_RAGC_ATV
  62. #define DRXK_KI_RAGC_ATV 4
  63. #endif
  64. #ifndef DRXK_KI_IAGC_ATV
  65. #define DRXK_KI_IAGC_ATV 6
  66. #endif
  67. #ifndef DRXK_KI_DAGC_ATV
  68. #define DRXK_KI_DAGC_ATV 7
  69. #endif
  70. #ifndef DRXK_KI_RAGC_QAM
  71. #define DRXK_KI_RAGC_QAM 3
  72. #endif
  73. #ifndef DRXK_KI_IAGC_QAM
  74. #define DRXK_KI_IAGC_QAM 4
  75. #endif
  76. #ifndef DRXK_KI_DAGC_QAM
  77. #define DRXK_KI_DAGC_QAM 7
  78. #endif
  79. #ifndef DRXK_KI_RAGC_DVBT
  80. #define DRXK_KI_RAGC_DVBT (IsA1WithPatchCode(state) ? 3 : 2)
  81. #endif
  82. #ifndef DRXK_KI_IAGC_DVBT
  83. #define DRXK_KI_IAGC_DVBT (IsA1WithPatchCode(state) ? 4 : 2)
  84. #endif
  85. #ifndef DRXK_KI_DAGC_DVBT
  86. #define DRXK_KI_DAGC_DVBT (IsA1WithPatchCode(state) ? 10 : 7)
  87. #endif
  88. #ifndef DRXK_AGC_DAC_OFFSET
  89. #define DRXK_AGC_DAC_OFFSET (0x800)
  90. #endif
  91. #ifndef DRXK_BANDWIDTH_8MHZ_IN_HZ
  92. #define DRXK_BANDWIDTH_8MHZ_IN_HZ (0x8B8249L)
  93. #endif
  94. #ifndef DRXK_BANDWIDTH_7MHZ_IN_HZ
  95. #define DRXK_BANDWIDTH_7MHZ_IN_HZ (0x7A1200L)
  96. #endif
  97. #ifndef DRXK_BANDWIDTH_6MHZ_IN_HZ
  98. #define DRXK_BANDWIDTH_6MHZ_IN_HZ (0x68A1B6L)
  99. #endif
  100. #ifndef DRXK_QAM_SYMBOLRATE_MAX
  101. #define DRXK_QAM_SYMBOLRATE_MAX (7233000)
  102. #endif
  103. #define DRXK_BL_ROM_OFFSET_TAPS_DVBT 56
  104. #define DRXK_BL_ROM_OFFSET_TAPS_ITU_A 64
  105. #define DRXK_BL_ROM_OFFSET_TAPS_ITU_C 0x5FE0
  106. #define DRXK_BL_ROM_OFFSET_TAPS_BG 24
  107. #define DRXK_BL_ROM_OFFSET_TAPS_DKILLP 32
  108. #define DRXK_BL_ROM_OFFSET_TAPS_NTSC 40
  109. #define DRXK_BL_ROM_OFFSET_TAPS_FM 48
  110. #define DRXK_BL_ROM_OFFSET_UCODE 0
  111. #define DRXK_BLC_TIMEOUT 100
  112. #define DRXK_BLCC_NR_ELEMENTS_TAPS 2
  113. #define DRXK_BLCC_NR_ELEMENTS_UCODE 6
  114. #define DRXK_BLDC_NR_ELEMENTS_TAPS 28
  115. #ifndef DRXK_OFDM_NE_NOTCH_WIDTH
  116. #define DRXK_OFDM_NE_NOTCH_WIDTH (4)
  117. #endif
  118. #define DRXK_QAM_SL_SIG_POWER_QAM16 (40960)
  119. #define DRXK_QAM_SL_SIG_POWER_QAM32 (20480)
  120. #define DRXK_QAM_SL_SIG_POWER_QAM64 (43008)
  121. #define DRXK_QAM_SL_SIG_POWER_QAM128 (20992)
  122. #define DRXK_QAM_SL_SIG_POWER_QAM256 (43520)
  123. static unsigned int debug;
  124. module_param(debug, int, 0644);
  125. MODULE_PARM_DESC(debug, "enable debug messages");
  126. #define dprintk(level, fmt, arg...) do { \
  127. if (debug >= level) \
  128. printk(KERN_DEBUG KBUILD_MODNAME ": %s " fmt, __func__, ##arg); \
  129. } while (0)
  130. static inline u32 MulDiv32(u32 a, u32 b, u32 c)
  131. {
  132. u64 tmp64;
  133. tmp64 = (u64) a * (u64) b;
  134. do_div(tmp64, c);
  135. return (u32) tmp64;
  136. }
  137. static inline u32 Frac28a(u32 a, u32 c)
  138. {
  139. int i = 0;
  140. u32 Q1 = 0;
  141. u32 R0 = 0;
  142. R0 = (a % c) << 4; /* 32-28 == 4 shifts possible at max */
  143. Q1 = a / c; /*
  144. * integer part, only the 4 least significant
  145. * bits will be visible in the result
  146. */
  147. /* division using radix 16, 7 nibbles in the result */
  148. for (i = 0; i < 7; i++) {
  149. Q1 = (Q1 << 4) | (R0 / c);
  150. R0 = (R0 % c) << 4;
  151. }
  152. /* rounding */
  153. if ((R0 >> 3) >= c)
  154. Q1++;
  155. return Q1;
  156. }
  157. static inline u32 log10times100(u32 value)
  158. {
  159. return (100L * intlog10(value)) >> 24;
  160. }
  161. /***************************************************************************/
  162. /* I2C **********************************************************************/
  163. /***************************************************************************/
  164. static int drxk_i2c_lock(struct drxk_state *state)
  165. {
  166. i2c_lock_bus(state->i2c, I2C_LOCK_SEGMENT);
  167. state->drxk_i2c_exclusive_lock = true;
  168. return 0;
  169. }
  170. static void drxk_i2c_unlock(struct drxk_state *state)
  171. {
  172. if (!state->drxk_i2c_exclusive_lock)
  173. return;
  174. i2c_unlock_bus(state->i2c, I2C_LOCK_SEGMENT);
  175. state->drxk_i2c_exclusive_lock = false;
  176. }
  177. static int drxk_i2c_transfer(struct drxk_state *state, struct i2c_msg *msgs,
  178. unsigned len)
  179. {
  180. if (state->drxk_i2c_exclusive_lock)
  181. return __i2c_transfer(state->i2c, msgs, len);
  182. else
  183. return i2c_transfer(state->i2c, msgs, len);
  184. }
  185. static int i2c_read1(struct drxk_state *state, u8 adr, u8 *val)
  186. {
  187. struct i2c_msg msgs[1] = { {.addr = adr, .flags = I2C_M_RD,
  188. .buf = val, .len = 1}
  189. };
  190. return drxk_i2c_transfer(state, msgs, 1);
  191. }
  192. static int i2c_write(struct drxk_state *state, u8 adr, u8 *data, int len)
  193. {
  194. int status;
  195. struct i2c_msg msg = {
  196. .addr = adr, .flags = 0, .buf = data, .len = len };
  197. dprintk(3, ":");
  198. if (debug > 2) {
  199. int i;
  200. for (i = 0; i < len; i++)
  201. pr_cont(" %02x", data[i]);
  202. pr_cont("\n");
  203. }
  204. status = drxk_i2c_transfer(state, &msg, 1);
  205. if (status >= 0 && status != 1)
  206. status = -EIO;
  207. if (status < 0)
  208. pr_err("i2c write error at addr 0x%02x\n", adr);
  209. return status;
  210. }
  211. static int i2c_read(struct drxk_state *state,
  212. u8 adr, u8 *msg, int len, u8 *answ, int alen)
  213. {
  214. int status;
  215. struct i2c_msg msgs[2] = {
  216. {.addr = adr, .flags = 0,
  217. .buf = msg, .len = len},
  218. {.addr = adr, .flags = I2C_M_RD,
  219. .buf = answ, .len = alen}
  220. };
  221. status = drxk_i2c_transfer(state, msgs, 2);
  222. if (status != 2) {
  223. if (debug > 2)
  224. pr_cont(": ERROR!\n");
  225. if (status >= 0)
  226. status = -EIO;
  227. pr_err("i2c read error at addr 0x%02x\n", adr);
  228. return status;
  229. }
  230. if (debug > 2) {
  231. int i;
  232. dprintk(2, ": read from");
  233. for (i = 0; i < len; i++)
  234. pr_cont(" %02x", msg[i]);
  235. pr_cont(", value = ");
  236. for (i = 0; i < alen; i++)
  237. pr_cont(" %02x", answ[i]);
  238. pr_cont("\n");
  239. }
  240. return 0;
  241. }
  242. static int read16_flags(struct drxk_state *state, u32 reg, u16 *data, u8 flags)
  243. {
  244. int status;
  245. u8 adr = state->demod_address, mm1[4], mm2[2], len;
  246. if (state->single_master)
  247. flags |= 0xC0;
  248. if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
  249. mm1[0] = (((reg << 1) & 0xFF) | 0x01);
  250. mm1[1] = ((reg >> 16) & 0xFF);
  251. mm1[2] = ((reg >> 24) & 0xFF) | flags;
  252. mm1[3] = ((reg >> 7) & 0xFF);
  253. len = 4;
  254. } else {
  255. mm1[0] = ((reg << 1) & 0xFF);
  256. mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
  257. len = 2;
  258. }
  259. dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
  260. status = i2c_read(state, adr, mm1, len, mm2, 2);
  261. if (status < 0)
  262. return status;
  263. if (data)
  264. *data = mm2[0] | (mm2[1] << 8);
  265. return 0;
  266. }
  267. static int read16(struct drxk_state *state, u32 reg, u16 *data)
  268. {
  269. return read16_flags(state, reg, data, 0);
  270. }
  271. static int read32_flags(struct drxk_state *state, u32 reg, u32 *data, u8 flags)
  272. {
  273. int status;
  274. u8 adr = state->demod_address, mm1[4], mm2[4], len;
  275. if (state->single_master)
  276. flags |= 0xC0;
  277. if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
  278. mm1[0] = (((reg << 1) & 0xFF) | 0x01);
  279. mm1[1] = ((reg >> 16) & 0xFF);
  280. mm1[2] = ((reg >> 24) & 0xFF) | flags;
  281. mm1[3] = ((reg >> 7) & 0xFF);
  282. len = 4;
  283. } else {
  284. mm1[0] = ((reg << 1) & 0xFF);
  285. mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
  286. len = 2;
  287. }
  288. dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
  289. status = i2c_read(state, adr, mm1, len, mm2, 4);
  290. if (status < 0)
  291. return status;
  292. if (data)
  293. *data = mm2[0] | (mm2[1] << 8) |
  294. (mm2[2] << 16) | (mm2[3] << 24);
  295. return 0;
  296. }
  297. static int read32(struct drxk_state *state, u32 reg, u32 *data)
  298. {
  299. return read32_flags(state, reg, data, 0);
  300. }
  301. static int write16_flags(struct drxk_state *state, u32 reg, u16 data, u8 flags)
  302. {
  303. u8 adr = state->demod_address, mm[6], len;
  304. if (state->single_master)
  305. flags |= 0xC0;
  306. if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
  307. mm[0] = (((reg << 1) & 0xFF) | 0x01);
  308. mm[1] = ((reg >> 16) & 0xFF);
  309. mm[2] = ((reg >> 24) & 0xFF) | flags;
  310. mm[3] = ((reg >> 7) & 0xFF);
  311. len = 4;
  312. } else {
  313. mm[0] = ((reg << 1) & 0xFF);
  314. mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
  315. len = 2;
  316. }
  317. mm[len] = data & 0xff;
  318. mm[len + 1] = (data >> 8) & 0xff;
  319. dprintk(2, "(0x%08x, 0x%04x, 0x%02x)\n", reg, data, flags);
  320. return i2c_write(state, adr, mm, len + 2);
  321. }
  322. static int write16(struct drxk_state *state, u32 reg, u16 data)
  323. {
  324. return write16_flags(state, reg, data, 0);
  325. }
  326. static int write32_flags(struct drxk_state *state, u32 reg, u32 data, u8 flags)
  327. {
  328. u8 adr = state->demod_address, mm[8], len;
  329. if (state->single_master)
  330. flags |= 0xC0;
  331. if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
  332. mm[0] = (((reg << 1) & 0xFF) | 0x01);
  333. mm[1] = ((reg >> 16) & 0xFF);
  334. mm[2] = ((reg >> 24) & 0xFF) | flags;
  335. mm[3] = ((reg >> 7) & 0xFF);
  336. len = 4;
  337. } else {
  338. mm[0] = ((reg << 1) & 0xFF);
  339. mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
  340. len = 2;
  341. }
  342. mm[len] = data & 0xff;
  343. mm[len + 1] = (data >> 8) & 0xff;
  344. mm[len + 2] = (data >> 16) & 0xff;
  345. mm[len + 3] = (data >> 24) & 0xff;
  346. dprintk(2, "(0x%08x, 0x%08x, 0x%02x)\n", reg, data, flags);
  347. return i2c_write(state, adr, mm, len + 4);
  348. }
  349. static int write32(struct drxk_state *state, u32 reg, u32 data)
  350. {
  351. return write32_flags(state, reg, data, 0);
  352. }
  353. static int write_block(struct drxk_state *state, u32 address,
  354. const int block_size, const u8 p_block[])
  355. {
  356. int status = 0, blk_size = block_size;
  357. u8 flags = 0;
  358. if (state->single_master)
  359. flags |= 0xC0;
  360. while (blk_size > 0) {
  361. int chunk = blk_size > state->m_chunk_size ?
  362. state->m_chunk_size : blk_size;
  363. u8 *adr_buf = &state->chunk[0];
  364. u32 adr_length = 0;
  365. if (DRXDAP_FASI_LONG_FORMAT(address) || (flags != 0)) {
  366. adr_buf[0] = (((address << 1) & 0xFF) | 0x01);
  367. adr_buf[1] = ((address >> 16) & 0xFF);
  368. adr_buf[2] = ((address >> 24) & 0xFF);
  369. adr_buf[3] = ((address >> 7) & 0xFF);
  370. adr_buf[2] |= flags;
  371. adr_length = 4;
  372. if (chunk == state->m_chunk_size)
  373. chunk -= 2;
  374. } else {
  375. adr_buf[0] = ((address << 1) & 0xFF);
  376. adr_buf[1] = (((address >> 16) & 0x0F) |
  377. ((address >> 18) & 0xF0));
  378. adr_length = 2;
  379. }
  380. memcpy(&state->chunk[adr_length], p_block, chunk);
  381. dprintk(2, "(0x%08x, 0x%02x)\n", address, flags);
  382. if (debug > 1) {
  383. int i;
  384. if (p_block)
  385. for (i = 0; i < chunk; i++)
  386. pr_cont(" %02x", p_block[i]);
  387. pr_cont("\n");
  388. }
  389. status = i2c_write(state, state->demod_address,
  390. &state->chunk[0], chunk + adr_length);
  391. if (status < 0) {
  392. pr_err("%s: i2c write error at addr 0x%02x\n",
  393. __func__, address);
  394. break;
  395. }
  396. p_block += chunk;
  397. address += (chunk >> 1);
  398. blk_size -= chunk;
  399. }
  400. return status;
  401. }
  402. #ifndef DRXK_MAX_RETRIES_POWERUP
  403. #define DRXK_MAX_RETRIES_POWERUP 20
  404. #endif
  405. static int power_up_device(struct drxk_state *state)
  406. {
  407. int status;
  408. u8 data = 0;
  409. u16 retry_count = 0;
  410. dprintk(1, "\n");
  411. status = i2c_read1(state, state->demod_address, &data);
  412. if (status < 0) {
  413. do {
  414. data = 0;
  415. status = i2c_write(state, state->demod_address,
  416. &data, 1);
  417. usleep_range(10000, 11000);
  418. retry_count++;
  419. if (status < 0)
  420. continue;
  421. status = i2c_read1(state, state->demod_address,
  422. &data);
  423. } while (status < 0 &&
  424. (retry_count < DRXK_MAX_RETRIES_POWERUP));
  425. if (status < 0 && retry_count >= DRXK_MAX_RETRIES_POWERUP)
  426. goto error;
  427. }
  428. /* Make sure all clk domains are active */
  429. status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_NONE);
  430. if (status < 0)
  431. goto error;
  432. status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
  433. if (status < 0)
  434. goto error;
  435. /* Enable pll lock tests */
  436. status = write16(state, SIO_CC_PLL_LOCK__A, 1);
  437. if (status < 0)
  438. goto error;
  439. state->m_current_power_mode = DRX_POWER_UP;
  440. error:
  441. if (status < 0)
  442. pr_err("Error %d on %s\n", status, __func__);
  443. return status;
  444. }
  445. static int init_state(struct drxk_state *state)
  446. {
  447. /*
  448. * FIXME: most (all?) of the values below should be moved into
  449. * struct drxk_config, as they are probably board-specific
  450. */
  451. u32 ul_vsb_if_agc_mode = DRXK_AGC_CTRL_AUTO;
  452. u32 ul_vsb_if_agc_output_level = 0;
  453. u32 ul_vsb_if_agc_min_level = 0;
  454. u32 ul_vsb_if_agc_max_level = 0x7FFF;
  455. u32 ul_vsb_if_agc_speed = 3;
  456. u32 ul_vsb_rf_agc_mode = DRXK_AGC_CTRL_AUTO;
  457. u32 ul_vsb_rf_agc_output_level = 0;
  458. u32 ul_vsb_rf_agc_min_level = 0;
  459. u32 ul_vsb_rf_agc_max_level = 0x7FFF;
  460. u32 ul_vsb_rf_agc_speed = 3;
  461. u32 ul_vsb_rf_agc_top = 9500;
  462. u32 ul_vsb_rf_agc_cut_off_current = 4000;
  463. u32 ul_atv_if_agc_mode = DRXK_AGC_CTRL_AUTO;
  464. u32 ul_atv_if_agc_output_level = 0;
  465. u32 ul_atv_if_agc_min_level = 0;
  466. u32 ul_atv_if_agc_max_level = 0;
  467. u32 ul_atv_if_agc_speed = 3;
  468. u32 ul_atv_rf_agc_mode = DRXK_AGC_CTRL_OFF;
  469. u32 ul_atv_rf_agc_output_level = 0;
  470. u32 ul_atv_rf_agc_min_level = 0;
  471. u32 ul_atv_rf_agc_max_level = 0;
  472. u32 ul_atv_rf_agc_top = 9500;
  473. u32 ul_atv_rf_agc_cut_off_current = 4000;
  474. u32 ul_atv_rf_agc_speed = 3;
  475. u32 ulQual83 = DEFAULT_MER_83;
  476. u32 ulQual93 = DEFAULT_MER_93;
  477. u32 ul_mpeg_lock_time_out = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
  478. u32 ul_demod_lock_time_out = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
  479. /* io_pad_cfg register (8 bit reg.) MSB bit is 1 (default value) */
  480. /* io_pad_cfg_mode output mode is drive always */
  481. /* io_pad_cfg_drive is set to power 2 (23 mA) */
  482. u32 ul_gpio_cfg = 0x0113;
  483. u32 ul_invert_ts_clock = 0;
  484. u32 ul_ts_data_strength = DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH;
  485. u32 ul_dvbt_bitrate = 50000000;
  486. u32 ul_dvbc_bitrate = DRXK_QAM_SYMBOLRATE_MAX * 8;
  487. u32 ul_insert_rs_byte = 0;
  488. u32 ul_rf_mirror = 1;
  489. u32 ul_power_down = 0;
  490. dprintk(1, "\n");
  491. state->m_has_lna = false;
  492. state->m_has_dvbt = false;
  493. state->m_has_dvbc = false;
  494. state->m_has_atv = false;
  495. state->m_has_oob = false;
  496. state->m_has_audio = false;
  497. if (!state->m_chunk_size)
  498. state->m_chunk_size = 124;
  499. state->m_osc_clock_freq = 0;
  500. state->m_smart_ant_inverted = false;
  501. state->m_b_p_down_open_bridge = false;
  502. /* real system clock frequency in kHz */
  503. state->m_sys_clock_freq = 151875;
  504. /* Timing div, 250ns/Psys */
  505. /* Timing div, = (delay (nano seconds) * sysclk (kHz))/ 1000 */
  506. state->m_hi_cfg_timing_div = ((state->m_sys_clock_freq / 1000) *
  507. HI_I2C_DELAY) / 1000;
  508. /* Clipping */
  509. if (state->m_hi_cfg_timing_div > SIO_HI_RA_RAM_PAR_2_CFG_DIV__M)
  510. state->m_hi_cfg_timing_div = SIO_HI_RA_RAM_PAR_2_CFG_DIV__M;
  511. state->m_hi_cfg_wake_up_key = (state->demod_address << 1);
  512. /* port/bridge/power down ctrl */
  513. state->m_hi_cfg_ctrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
  514. state->m_b_power_down = (ul_power_down != 0);
  515. state->m_drxk_a3_patch_code = false;
  516. /* Init AGC and PGA parameters */
  517. /* VSB IF */
  518. state->m_vsb_if_agc_cfg.ctrl_mode = ul_vsb_if_agc_mode;
  519. state->m_vsb_if_agc_cfg.output_level = ul_vsb_if_agc_output_level;
  520. state->m_vsb_if_agc_cfg.min_output_level = ul_vsb_if_agc_min_level;
  521. state->m_vsb_if_agc_cfg.max_output_level = ul_vsb_if_agc_max_level;
  522. state->m_vsb_if_agc_cfg.speed = ul_vsb_if_agc_speed;
  523. state->m_vsb_pga_cfg = 140;
  524. /* VSB RF */
  525. state->m_vsb_rf_agc_cfg.ctrl_mode = ul_vsb_rf_agc_mode;
  526. state->m_vsb_rf_agc_cfg.output_level = ul_vsb_rf_agc_output_level;
  527. state->m_vsb_rf_agc_cfg.min_output_level = ul_vsb_rf_agc_min_level;
  528. state->m_vsb_rf_agc_cfg.max_output_level = ul_vsb_rf_agc_max_level;
  529. state->m_vsb_rf_agc_cfg.speed = ul_vsb_rf_agc_speed;
  530. state->m_vsb_rf_agc_cfg.top = ul_vsb_rf_agc_top;
  531. state->m_vsb_rf_agc_cfg.cut_off_current = ul_vsb_rf_agc_cut_off_current;
  532. state->m_vsb_pre_saw_cfg.reference = 0x07;
  533. state->m_vsb_pre_saw_cfg.use_pre_saw = true;
  534. state->m_Quality83percent = DEFAULT_MER_83;
  535. state->m_Quality93percent = DEFAULT_MER_93;
  536. if (ulQual93 <= 500 && ulQual83 < ulQual93) {
  537. state->m_Quality83percent = ulQual83;
  538. state->m_Quality93percent = ulQual93;
  539. }
  540. /* ATV IF */
  541. state->m_atv_if_agc_cfg.ctrl_mode = ul_atv_if_agc_mode;
  542. state->m_atv_if_agc_cfg.output_level = ul_atv_if_agc_output_level;
  543. state->m_atv_if_agc_cfg.min_output_level = ul_atv_if_agc_min_level;
  544. state->m_atv_if_agc_cfg.max_output_level = ul_atv_if_agc_max_level;
  545. state->m_atv_if_agc_cfg.speed = ul_atv_if_agc_speed;
  546. /* ATV RF */
  547. state->m_atv_rf_agc_cfg.ctrl_mode = ul_atv_rf_agc_mode;
  548. state->m_atv_rf_agc_cfg.output_level = ul_atv_rf_agc_output_level;
  549. state->m_atv_rf_agc_cfg.min_output_level = ul_atv_rf_agc_min_level;
  550. state->m_atv_rf_agc_cfg.max_output_level = ul_atv_rf_agc_max_level;
  551. state->m_atv_rf_agc_cfg.speed = ul_atv_rf_agc_speed;
  552. state->m_atv_rf_agc_cfg.top = ul_atv_rf_agc_top;
  553. state->m_atv_rf_agc_cfg.cut_off_current = ul_atv_rf_agc_cut_off_current;
  554. state->m_atv_pre_saw_cfg.reference = 0x04;
  555. state->m_atv_pre_saw_cfg.use_pre_saw = true;
  556. /* DVBT RF */
  557. state->m_dvbt_rf_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_OFF;
  558. state->m_dvbt_rf_agc_cfg.output_level = 0;
  559. state->m_dvbt_rf_agc_cfg.min_output_level = 0;
  560. state->m_dvbt_rf_agc_cfg.max_output_level = 0xFFFF;
  561. state->m_dvbt_rf_agc_cfg.top = 0x2100;
  562. state->m_dvbt_rf_agc_cfg.cut_off_current = 4000;
  563. state->m_dvbt_rf_agc_cfg.speed = 1;
  564. /* DVBT IF */
  565. state->m_dvbt_if_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_AUTO;
  566. state->m_dvbt_if_agc_cfg.output_level = 0;
  567. state->m_dvbt_if_agc_cfg.min_output_level = 0;
  568. state->m_dvbt_if_agc_cfg.max_output_level = 9000;
  569. state->m_dvbt_if_agc_cfg.top = 13424;
  570. state->m_dvbt_if_agc_cfg.cut_off_current = 0;
  571. state->m_dvbt_if_agc_cfg.speed = 3;
  572. state->m_dvbt_if_agc_cfg.fast_clip_ctrl_delay = 30;
  573. state->m_dvbt_if_agc_cfg.ingain_tgt_max = 30000;
  574. /* state->m_dvbtPgaCfg = 140; */
  575. state->m_dvbt_pre_saw_cfg.reference = 4;
  576. state->m_dvbt_pre_saw_cfg.use_pre_saw = false;
  577. /* QAM RF */
  578. state->m_qam_rf_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_OFF;
  579. state->m_qam_rf_agc_cfg.output_level = 0;
  580. state->m_qam_rf_agc_cfg.min_output_level = 6023;
  581. state->m_qam_rf_agc_cfg.max_output_level = 27000;
  582. state->m_qam_rf_agc_cfg.top = 0x2380;
  583. state->m_qam_rf_agc_cfg.cut_off_current = 4000;
  584. state->m_qam_rf_agc_cfg.speed = 3;
  585. /* QAM IF */
  586. state->m_qam_if_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_AUTO;
  587. state->m_qam_if_agc_cfg.output_level = 0;
  588. state->m_qam_if_agc_cfg.min_output_level = 0;
  589. state->m_qam_if_agc_cfg.max_output_level = 9000;
  590. state->m_qam_if_agc_cfg.top = 0x0511;
  591. state->m_qam_if_agc_cfg.cut_off_current = 0;
  592. state->m_qam_if_agc_cfg.speed = 3;
  593. state->m_qam_if_agc_cfg.ingain_tgt_max = 5119;
  594. state->m_qam_if_agc_cfg.fast_clip_ctrl_delay = 50;
  595. state->m_qam_pga_cfg = 140;
  596. state->m_qam_pre_saw_cfg.reference = 4;
  597. state->m_qam_pre_saw_cfg.use_pre_saw = false;
  598. state->m_operation_mode = OM_NONE;
  599. state->m_drxk_state = DRXK_UNINITIALIZED;
  600. /* MPEG output configuration */
  601. state->m_enable_mpeg_output = true; /* If TRUE; enable MPEG output */
  602. state->m_insert_rs_byte = false; /* If TRUE; insert RS byte */
  603. state->m_invert_data = false; /* If TRUE; invert DATA signals */
  604. state->m_invert_err = false; /* If TRUE; invert ERR signal */
  605. state->m_invert_str = false; /* If TRUE; invert STR signals */
  606. state->m_invert_val = false; /* If TRUE; invert VAL signals */
  607. state->m_invert_clk = (ul_invert_ts_clock != 0); /* If TRUE; invert CLK signals */
  608. /* If TRUE; static MPEG clockrate will be used;
  609. otherwise clockrate will adapt to the bitrate of the TS */
  610. state->m_dvbt_bitrate = ul_dvbt_bitrate;
  611. state->m_dvbc_bitrate = ul_dvbc_bitrate;
  612. state->m_ts_data_strength = (ul_ts_data_strength & 0x07);
  613. /* Maximum bitrate in b/s in case static clockrate is selected */
  614. state->m_mpeg_ts_static_bitrate = 19392658;
  615. state->m_disable_te_ihandling = false;
  616. if (ul_insert_rs_byte)
  617. state->m_insert_rs_byte = true;
  618. state->m_mpeg_lock_time_out = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
  619. if (ul_mpeg_lock_time_out < 10000)
  620. state->m_mpeg_lock_time_out = ul_mpeg_lock_time_out;
  621. state->m_demod_lock_time_out = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
  622. if (ul_demod_lock_time_out < 10000)
  623. state->m_demod_lock_time_out = ul_demod_lock_time_out;
  624. /* QAM defaults */
  625. state->m_constellation = DRX_CONSTELLATION_AUTO;
  626. state->m_qam_interleave_mode = DRXK_QAM_I12_J17;
  627. state->m_fec_rs_plen = 204 * 8; /* fecRsPlen annex A */
  628. state->m_fec_rs_prescale = 1;
  629. state->m_sqi_speed = DRXK_DVBT_SQI_SPEED_MEDIUM;
  630. state->m_agcfast_clip_ctrl_delay = 0;
  631. state->m_gpio_cfg = ul_gpio_cfg;
  632. state->m_b_power_down = false;
  633. state->m_current_power_mode = DRX_POWER_DOWN;
  634. state->m_rfmirror = (ul_rf_mirror == 0);
  635. state->m_if_agc_pol = false;
  636. return 0;
  637. }
  638. static int drxx_open(struct drxk_state *state)
  639. {
  640. int status = 0;
  641. u32 jtag = 0;
  642. u16 bid = 0;
  643. u16 key = 0;
  644. dprintk(1, "\n");
  645. /* stop lock indicator process */
  646. status = write16(state, SCU_RAM_GPIO__A,
  647. SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
  648. if (status < 0)
  649. goto error;
  650. /* Check device id */
  651. status = read16(state, SIO_TOP_COMM_KEY__A, &key);
  652. if (status < 0)
  653. goto error;
  654. status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
  655. if (status < 0)
  656. goto error;
  657. status = read32(state, SIO_TOP_JTAGID_LO__A, &jtag);
  658. if (status < 0)
  659. goto error;
  660. status = read16(state, SIO_PDR_UIO_IN_HI__A, &bid);
  661. if (status < 0)
  662. goto error;
  663. status = write16(state, SIO_TOP_COMM_KEY__A, key);
  664. error:
  665. if (status < 0)
  666. pr_err("Error %d on %s\n", status, __func__);
  667. return status;
  668. }
  669. static int get_device_capabilities(struct drxk_state *state)
  670. {
  671. u16 sio_pdr_ohw_cfg = 0;
  672. u32 sio_top_jtagid_lo = 0;
  673. int status;
  674. const char *spin = "";
  675. dprintk(1, "\n");
  676. /* driver 0.9.0 */
  677. /* stop lock indicator process */
  678. status = write16(state, SCU_RAM_GPIO__A,
  679. SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
  680. if (status < 0)
  681. goto error;
  682. status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
  683. if (status < 0)
  684. goto error;
  685. status = read16(state, SIO_PDR_OHW_CFG__A, &sio_pdr_ohw_cfg);
  686. if (status < 0)
  687. goto error;
  688. status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
  689. if (status < 0)
  690. goto error;
  691. switch ((sio_pdr_ohw_cfg & SIO_PDR_OHW_CFG_FREF_SEL__M)) {
  692. case 0:
  693. /* ignore (bypass ?) */
  694. break;
  695. case 1:
  696. /* 27 MHz */
  697. state->m_osc_clock_freq = 27000;
  698. break;
  699. case 2:
  700. /* 20.25 MHz */
  701. state->m_osc_clock_freq = 20250;
  702. break;
  703. case 3:
  704. /* 4 MHz */
  705. state->m_osc_clock_freq = 20250;
  706. break;
  707. default:
  708. pr_err("Clock Frequency is unknown\n");
  709. return -EINVAL;
  710. }
  711. /*
  712. Determine device capabilities
  713. Based on pinning v14
  714. */
  715. status = read32(state, SIO_TOP_JTAGID_LO__A, &sio_top_jtagid_lo);
  716. if (status < 0)
  717. goto error;
  718. pr_info("status = 0x%08x\n", sio_top_jtagid_lo);
  719. /* driver 0.9.0 */
  720. switch ((sio_top_jtagid_lo >> 29) & 0xF) {
  721. case 0:
  722. state->m_device_spin = DRXK_SPIN_A1;
  723. spin = "A1";
  724. break;
  725. case 2:
  726. state->m_device_spin = DRXK_SPIN_A2;
  727. spin = "A2";
  728. break;
  729. case 3:
  730. state->m_device_spin = DRXK_SPIN_A3;
  731. spin = "A3";
  732. break;
  733. default:
  734. state->m_device_spin = DRXK_SPIN_UNKNOWN;
  735. status = -EINVAL;
  736. pr_err("Spin %d unknown\n", (sio_top_jtagid_lo >> 29) & 0xF);
  737. goto error2;
  738. }
  739. switch ((sio_top_jtagid_lo >> 12) & 0xFF) {
  740. case 0x13:
  741. /* typeId = DRX3913K_TYPE_ID */
  742. state->m_has_lna = false;
  743. state->m_has_oob = false;
  744. state->m_has_atv = false;
  745. state->m_has_audio = false;
  746. state->m_has_dvbt = true;
  747. state->m_has_dvbc = true;
  748. state->m_has_sawsw = true;
  749. state->m_has_gpio2 = false;
  750. state->m_has_gpio1 = false;
  751. state->m_has_irqn = false;
  752. break;
  753. case 0x15:
  754. /* typeId = DRX3915K_TYPE_ID */
  755. state->m_has_lna = false;
  756. state->m_has_oob = false;
  757. state->m_has_atv = true;
  758. state->m_has_audio = false;
  759. state->m_has_dvbt = true;
  760. state->m_has_dvbc = false;
  761. state->m_has_sawsw = true;
  762. state->m_has_gpio2 = true;
  763. state->m_has_gpio1 = true;
  764. state->m_has_irqn = false;
  765. break;
  766. case 0x16:
  767. /* typeId = DRX3916K_TYPE_ID */
  768. state->m_has_lna = false;
  769. state->m_has_oob = false;
  770. state->m_has_atv = true;
  771. state->m_has_audio = false;
  772. state->m_has_dvbt = true;
  773. state->m_has_dvbc = false;
  774. state->m_has_sawsw = true;
  775. state->m_has_gpio2 = true;
  776. state->m_has_gpio1 = true;
  777. state->m_has_irqn = false;
  778. break;
  779. case 0x18:
  780. /* typeId = DRX3918K_TYPE_ID */
  781. state->m_has_lna = false;
  782. state->m_has_oob = false;
  783. state->m_has_atv = true;
  784. state->m_has_audio = true;
  785. state->m_has_dvbt = true;
  786. state->m_has_dvbc = false;
  787. state->m_has_sawsw = true;
  788. state->m_has_gpio2 = true;
  789. state->m_has_gpio1 = true;
  790. state->m_has_irqn = false;
  791. break;
  792. case 0x21:
  793. /* typeId = DRX3921K_TYPE_ID */
  794. state->m_has_lna = false;
  795. state->m_has_oob = false;
  796. state->m_has_atv = true;
  797. state->m_has_audio = true;
  798. state->m_has_dvbt = true;
  799. state->m_has_dvbc = true;
  800. state->m_has_sawsw = true;
  801. state->m_has_gpio2 = true;
  802. state->m_has_gpio1 = true;
  803. state->m_has_irqn = false;
  804. break;
  805. case 0x23:
  806. /* typeId = DRX3923K_TYPE_ID */
  807. state->m_has_lna = false;
  808. state->m_has_oob = false;
  809. state->m_has_atv = true;
  810. state->m_has_audio = true;
  811. state->m_has_dvbt = true;
  812. state->m_has_dvbc = true;
  813. state->m_has_sawsw = true;
  814. state->m_has_gpio2 = true;
  815. state->m_has_gpio1 = true;
  816. state->m_has_irqn = false;
  817. break;
  818. case 0x25:
  819. /* typeId = DRX3925K_TYPE_ID */
  820. state->m_has_lna = false;
  821. state->m_has_oob = false;
  822. state->m_has_atv = true;
  823. state->m_has_audio = true;
  824. state->m_has_dvbt = true;
  825. state->m_has_dvbc = true;
  826. state->m_has_sawsw = true;
  827. state->m_has_gpio2 = true;
  828. state->m_has_gpio1 = true;
  829. state->m_has_irqn = false;
  830. break;
  831. case 0x26:
  832. /* typeId = DRX3926K_TYPE_ID */
  833. state->m_has_lna = false;
  834. state->m_has_oob = false;
  835. state->m_has_atv = true;
  836. state->m_has_audio = false;
  837. state->m_has_dvbt = true;
  838. state->m_has_dvbc = true;
  839. state->m_has_sawsw = true;
  840. state->m_has_gpio2 = true;
  841. state->m_has_gpio1 = true;
  842. state->m_has_irqn = false;
  843. break;
  844. default:
  845. pr_err("DeviceID 0x%02x not supported\n",
  846. ((sio_top_jtagid_lo >> 12) & 0xFF));
  847. status = -EINVAL;
  848. goto error2;
  849. }
  850. pr_info("detected a drx-39%02xk, spin %s, xtal %d.%03d MHz\n",
  851. ((sio_top_jtagid_lo >> 12) & 0xFF), spin,
  852. state->m_osc_clock_freq / 1000,
  853. state->m_osc_clock_freq % 1000);
  854. error:
  855. if (status < 0)
  856. pr_err("Error %d on %s\n", status, __func__);
  857. error2:
  858. return status;
  859. }
  860. static int hi_command(struct drxk_state *state, u16 cmd, u16 *p_result)
  861. {
  862. int status;
  863. bool powerdown_cmd;
  864. dprintk(1, "\n");
  865. /* Write command */
  866. status = write16(state, SIO_HI_RA_RAM_CMD__A, cmd);
  867. if (status < 0)
  868. goto error;
  869. if (cmd == SIO_HI_RA_RAM_CMD_RESET)
  870. usleep_range(1000, 2000);
  871. powerdown_cmd =
  872. (bool) ((cmd == SIO_HI_RA_RAM_CMD_CONFIG) &&
  873. ((state->m_hi_cfg_ctrl) &
  874. SIO_HI_RA_RAM_PAR_5_CFG_SLEEP__M) ==
  875. SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ);
  876. if (!powerdown_cmd) {
  877. /* Wait until command rdy */
  878. u32 retry_count = 0;
  879. u16 wait_cmd;
  880. do {
  881. usleep_range(1000, 2000);
  882. retry_count += 1;
  883. status = read16(state, SIO_HI_RA_RAM_CMD__A,
  884. &wait_cmd);
  885. } while ((status < 0) && (retry_count < DRXK_MAX_RETRIES)
  886. && (wait_cmd != 0));
  887. if (status < 0)
  888. goto error;
  889. status = read16(state, SIO_HI_RA_RAM_RES__A, p_result);
  890. }
  891. error:
  892. if (status < 0)
  893. pr_err("Error %d on %s\n", status, __func__);
  894. return status;
  895. }
  896. static int hi_cfg_command(struct drxk_state *state)
  897. {
  898. int status;
  899. dprintk(1, "\n");
  900. mutex_lock(&state->mutex);
  901. status = write16(state, SIO_HI_RA_RAM_PAR_6__A,
  902. state->m_hi_cfg_timeout);
  903. if (status < 0)
  904. goto error;
  905. status = write16(state, SIO_HI_RA_RAM_PAR_5__A,
  906. state->m_hi_cfg_ctrl);
  907. if (status < 0)
  908. goto error;
  909. status = write16(state, SIO_HI_RA_RAM_PAR_4__A,
  910. state->m_hi_cfg_wake_up_key);
  911. if (status < 0)
  912. goto error;
  913. status = write16(state, SIO_HI_RA_RAM_PAR_3__A,
  914. state->m_hi_cfg_bridge_delay);
  915. if (status < 0)
  916. goto error;
  917. status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
  918. state->m_hi_cfg_timing_div);
  919. if (status < 0)
  920. goto error;
  921. status = write16(state, SIO_HI_RA_RAM_PAR_1__A,
  922. SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
  923. if (status < 0)
  924. goto error;
  925. status = hi_command(state, SIO_HI_RA_RAM_CMD_CONFIG, NULL);
  926. if (status < 0)
  927. goto error;
  928. state->m_hi_cfg_ctrl &= ~SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
  929. error:
  930. mutex_unlock(&state->mutex);
  931. if (status < 0)
  932. pr_err("Error %d on %s\n", status, __func__);
  933. return status;
  934. }
  935. static int init_hi(struct drxk_state *state)
  936. {
  937. dprintk(1, "\n");
  938. state->m_hi_cfg_wake_up_key = (state->demod_address << 1);
  939. state->m_hi_cfg_timeout = 0x96FF;
  940. /* port/bridge/power down ctrl */
  941. state->m_hi_cfg_ctrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
  942. return hi_cfg_command(state);
  943. }
  944. static int mpegts_configure_pins(struct drxk_state *state, bool mpeg_enable)
  945. {
  946. int status = -1;
  947. u16 sio_pdr_mclk_cfg = 0;
  948. u16 sio_pdr_mdx_cfg = 0;
  949. u16 err_cfg = 0;
  950. dprintk(1, ": mpeg %s, %s mode\n",
  951. mpeg_enable ? "enable" : "disable",
  952. state->m_enable_parallel ? "parallel" : "serial");
  953. /* stop lock indicator process */
  954. status = write16(state, SCU_RAM_GPIO__A,
  955. SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
  956. if (status < 0)
  957. goto error;
  958. /* MPEG TS pad configuration */
  959. status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
  960. if (status < 0)
  961. goto error;
  962. if (!mpeg_enable) {
  963. /* Set MPEG TS pads to inputmode */
  964. status = write16(state, SIO_PDR_MSTRT_CFG__A, 0x0000);
  965. if (status < 0)
  966. goto error;
  967. status = write16(state, SIO_PDR_MERR_CFG__A, 0x0000);
  968. if (status < 0)
  969. goto error;
  970. status = write16(state, SIO_PDR_MCLK_CFG__A, 0x0000);
  971. if (status < 0)
  972. goto error;
  973. status = write16(state, SIO_PDR_MVAL_CFG__A, 0x0000);
  974. if (status < 0)
  975. goto error;
  976. status = write16(state, SIO_PDR_MD0_CFG__A, 0x0000);
  977. if (status < 0)
  978. goto error;
  979. status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
  980. if (status < 0)
  981. goto error;
  982. status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
  983. if (status < 0)
  984. goto error;
  985. status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
  986. if (status < 0)
  987. goto error;
  988. status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
  989. if (status < 0)
  990. goto error;
  991. status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
  992. if (status < 0)
  993. goto error;
  994. status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
  995. if (status < 0)
  996. goto error;
  997. status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
  998. if (status < 0)
  999. goto error;
  1000. } else {
  1001. /* Enable MPEG output */
  1002. sio_pdr_mdx_cfg =
  1003. ((state->m_ts_data_strength <<
  1004. SIO_PDR_MD0_CFG_DRIVE__B) | 0x0003);
  1005. sio_pdr_mclk_cfg = ((state->m_ts_clockk_strength <<
  1006. SIO_PDR_MCLK_CFG_DRIVE__B) |
  1007. 0x0003);
  1008. status = write16(state, SIO_PDR_MSTRT_CFG__A, sio_pdr_mdx_cfg);
  1009. if (status < 0)
  1010. goto error;
  1011. if (state->enable_merr_cfg)
  1012. err_cfg = sio_pdr_mdx_cfg;
  1013. status = write16(state, SIO_PDR_MERR_CFG__A, err_cfg);
  1014. if (status < 0)
  1015. goto error;
  1016. status = write16(state, SIO_PDR_MVAL_CFG__A, err_cfg);
  1017. if (status < 0)
  1018. goto error;
  1019. if (state->m_enable_parallel) {
  1020. /* parallel -> enable MD1 to MD7 */
  1021. status = write16(state, SIO_PDR_MD1_CFG__A,
  1022. sio_pdr_mdx_cfg);
  1023. if (status < 0)
  1024. goto error;
  1025. status = write16(state, SIO_PDR_MD2_CFG__A,
  1026. sio_pdr_mdx_cfg);
  1027. if (status < 0)
  1028. goto error;
  1029. status = write16(state, SIO_PDR_MD3_CFG__A,
  1030. sio_pdr_mdx_cfg);
  1031. if (status < 0)
  1032. goto error;
  1033. status = write16(state, SIO_PDR_MD4_CFG__A,
  1034. sio_pdr_mdx_cfg);
  1035. if (status < 0)
  1036. goto error;
  1037. status = write16(state, SIO_PDR_MD5_CFG__A,
  1038. sio_pdr_mdx_cfg);
  1039. if (status < 0)
  1040. goto error;
  1041. status = write16(state, SIO_PDR_MD6_CFG__A,
  1042. sio_pdr_mdx_cfg);
  1043. if (status < 0)
  1044. goto error;
  1045. status = write16(state, SIO_PDR_MD7_CFG__A,
  1046. sio_pdr_mdx_cfg);
  1047. if (status < 0)
  1048. goto error;
  1049. } else {
  1050. sio_pdr_mdx_cfg = ((state->m_ts_data_strength <<
  1051. SIO_PDR_MD0_CFG_DRIVE__B)
  1052. | 0x0003);
  1053. /* serial -> disable MD1 to MD7 */
  1054. status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
  1055. if (status < 0)
  1056. goto error;
  1057. status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
  1058. if (status < 0)
  1059. goto error;
  1060. status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
  1061. if (status < 0)
  1062. goto error;
  1063. status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
  1064. if (status < 0)
  1065. goto error;
  1066. status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
  1067. if (status < 0)
  1068. goto error;
  1069. status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
  1070. if (status < 0)
  1071. goto error;
  1072. status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
  1073. if (status < 0)
  1074. goto error;
  1075. }
  1076. status = write16(state, SIO_PDR_MCLK_CFG__A, sio_pdr_mclk_cfg);
  1077. if (status < 0)
  1078. goto error;
  1079. status = write16(state, SIO_PDR_MD0_CFG__A, sio_pdr_mdx_cfg);
  1080. if (status < 0)
  1081. goto error;
  1082. }
  1083. /* Enable MB output over MPEG pads and ctl input */
  1084. status = write16(state, SIO_PDR_MON_CFG__A, 0x0000);
  1085. if (status < 0)
  1086. goto error;
  1087. /* Write nomagic word to enable pdr reg write */
  1088. status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
  1089. error:
  1090. if (status < 0)
  1091. pr_err("Error %d on %s\n", status, __func__);
  1092. return status;
  1093. }
  1094. static int mpegts_disable(struct drxk_state *state)
  1095. {
  1096. dprintk(1, "\n");
  1097. return mpegts_configure_pins(state, false);
  1098. }
  1099. static int bl_chain_cmd(struct drxk_state *state,
  1100. u16 rom_offset, u16 nr_of_elements, u32 time_out)
  1101. {
  1102. u16 bl_status = 0;
  1103. int status;
  1104. unsigned long end;
  1105. dprintk(1, "\n");
  1106. mutex_lock(&state->mutex);
  1107. status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_CHAIN);
  1108. if (status < 0)
  1109. goto error;
  1110. status = write16(state, SIO_BL_CHAIN_ADDR__A, rom_offset);
  1111. if (status < 0)
  1112. goto error;
  1113. status = write16(state, SIO_BL_CHAIN_LEN__A, nr_of_elements);
  1114. if (status < 0)
  1115. goto error;
  1116. status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
  1117. if (status < 0)
  1118. goto error;
  1119. end = jiffies + msecs_to_jiffies(time_out);
  1120. do {
  1121. usleep_range(1000, 2000);
  1122. status = read16(state, SIO_BL_STATUS__A, &bl_status);
  1123. if (status < 0)
  1124. goto error;
  1125. } while ((bl_status == 0x1) &&
  1126. ((time_is_after_jiffies(end))));
  1127. if (bl_status == 0x1) {
  1128. pr_err("SIO not ready\n");
  1129. status = -EINVAL;
  1130. goto error2;
  1131. }
  1132. error:
  1133. if (status < 0)
  1134. pr_err("Error %d on %s\n", status, __func__);
  1135. error2:
  1136. mutex_unlock(&state->mutex);
  1137. return status;
  1138. }
  1139. static int download_microcode(struct drxk_state *state,
  1140. const u8 p_mc_image[], u32 length)
  1141. {
  1142. const u8 *p_src = p_mc_image;
  1143. u32 address;
  1144. u16 n_blocks;
  1145. u16 block_size;
  1146. u32 offset = 0;
  1147. u32 i;
  1148. int status = 0;
  1149. dprintk(1, "\n");
  1150. /* down the drain (we don't care about MAGIC_WORD) */
  1151. #if 0
  1152. /* For future reference */
  1153. drain = (p_src[0] << 8) | p_src[1];
  1154. #endif
  1155. p_src += sizeof(u16);
  1156. offset += sizeof(u16);
  1157. n_blocks = (p_src[0] << 8) | p_src[1];
  1158. p_src += sizeof(u16);
  1159. offset += sizeof(u16);
  1160. for (i = 0; i < n_blocks; i += 1) {
  1161. address = (p_src[0] << 24) | (p_src[1] << 16) |
  1162. (p_src[2] << 8) | p_src[3];
  1163. p_src += sizeof(u32);
  1164. offset += sizeof(u32);
  1165. block_size = ((p_src[0] << 8) | p_src[1]) * sizeof(u16);
  1166. p_src += sizeof(u16);
  1167. offset += sizeof(u16);
  1168. #if 0
  1169. /* For future reference */
  1170. flags = (p_src[0] << 8) | p_src[1];
  1171. #endif
  1172. p_src += sizeof(u16);
  1173. offset += sizeof(u16);
  1174. #if 0
  1175. /* For future reference */
  1176. block_crc = (p_src[0] << 8) | p_src[1];
  1177. #endif
  1178. p_src += sizeof(u16);
  1179. offset += sizeof(u16);
  1180. if (offset + block_size > length) {
  1181. pr_err("Firmware is corrupted.\n");
  1182. return -EINVAL;
  1183. }
  1184. status = write_block(state, address, block_size, p_src);
  1185. if (status < 0) {
  1186. pr_err("Error %d while loading firmware\n", status);
  1187. break;
  1188. }
  1189. p_src += block_size;
  1190. offset += block_size;
  1191. }
  1192. return status;
  1193. }
  1194. static int dvbt_enable_ofdm_token_ring(struct drxk_state *state, bool enable)
  1195. {
  1196. int status;
  1197. u16 data = 0;
  1198. u16 desired_ctrl = SIO_OFDM_SH_OFDM_RING_ENABLE_ON;
  1199. u16 desired_status = SIO_OFDM_SH_OFDM_RING_STATUS_ENABLED;
  1200. unsigned long end;
  1201. dprintk(1, "\n");
  1202. if (!enable) {
  1203. desired_ctrl = SIO_OFDM_SH_OFDM_RING_ENABLE_OFF;
  1204. desired_status = SIO_OFDM_SH_OFDM_RING_STATUS_DOWN;
  1205. }
  1206. status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
  1207. if (status >= 0 && data == desired_status) {
  1208. /* tokenring already has correct status */
  1209. return status;
  1210. }
  1211. /* Disable/enable dvbt tokenring bridge */
  1212. status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, desired_ctrl);
  1213. end = jiffies + msecs_to_jiffies(DRXK_OFDM_TR_SHUTDOWN_TIMEOUT);
  1214. do {
  1215. status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
  1216. if ((status >= 0 && data == desired_status)
  1217. || time_is_after_jiffies(end))
  1218. break;
  1219. usleep_range(1000, 2000);
  1220. } while (1);
  1221. if (data != desired_status) {
  1222. pr_err("SIO not ready\n");
  1223. return -EINVAL;
  1224. }
  1225. return status;
  1226. }
  1227. static int mpegts_stop(struct drxk_state *state)
  1228. {
  1229. int status = 0;
  1230. u16 fec_oc_snc_mode = 0;
  1231. u16 fec_oc_ipr_mode = 0;
  1232. dprintk(1, "\n");
  1233. /* Graceful shutdown (byte boundaries) */
  1234. status = read16(state, FEC_OC_SNC_MODE__A, &fec_oc_snc_mode);
  1235. if (status < 0)
  1236. goto error;
  1237. fec_oc_snc_mode |= FEC_OC_SNC_MODE_SHUTDOWN__M;
  1238. status = write16(state, FEC_OC_SNC_MODE__A, fec_oc_snc_mode);
  1239. if (status < 0)
  1240. goto error;
  1241. /* Suppress MCLK during absence of data */
  1242. status = read16(state, FEC_OC_IPR_MODE__A, &fec_oc_ipr_mode);
  1243. if (status < 0)
  1244. goto error;
  1245. fec_oc_ipr_mode |= FEC_OC_IPR_MODE_MCLK_DIS_DAT_ABS__M;
  1246. status = write16(state, FEC_OC_IPR_MODE__A, fec_oc_ipr_mode);
  1247. error:
  1248. if (status < 0)
  1249. pr_err("Error %d on %s\n", status, __func__);
  1250. return status;
  1251. }
  1252. static int scu_command(struct drxk_state *state,
  1253. u16 cmd, u8 parameter_len,
  1254. u16 *parameter, u8 result_len, u16 *result)
  1255. {
  1256. #if (SCU_RAM_PARAM_0__A - SCU_RAM_PARAM_15__A) != 15
  1257. #error DRXK register mapping no longer compatible with this routine!
  1258. #endif
  1259. u16 cur_cmd = 0;
  1260. int status = -EINVAL;
  1261. unsigned long end;
  1262. u8 buffer[34];
  1263. int cnt = 0, ii;
  1264. const char *p;
  1265. char errname[30];
  1266. dprintk(1, "\n");
  1267. if ((cmd == 0) || ((parameter_len > 0) && (parameter == NULL)) ||
  1268. ((result_len > 0) && (result == NULL))) {
  1269. pr_err("Error %d on %s\n", status, __func__);
  1270. return status;
  1271. }
  1272. mutex_lock(&state->mutex);
  1273. /* assume that the command register is ready
  1274. since it is checked afterwards */
  1275. if (parameter) {
  1276. for (ii = parameter_len - 1; ii >= 0; ii -= 1) {
  1277. buffer[cnt++] = (parameter[ii] & 0xFF);
  1278. buffer[cnt++] = ((parameter[ii] >> 8) & 0xFF);
  1279. }
  1280. }
  1281. buffer[cnt++] = (cmd & 0xFF);
  1282. buffer[cnt++] = ((cmd >> 8) & 0xFF);
  1283. write_block(state, SCU_RAM_PARAM_0__A -
  1284. (parameter_len - 1), cnt, buffer);
  1285. /* Wait until SCU has processed command */
  1286. end = jiffies + msecs_to_jiffies(DRXK_MAX_WAITTIME);
  1287. do {
  1288. usleep_range(1000, 2000);
  1289. status = read16(state, SCU_RAM_COMMAND__A, &cur_cmd);
  1290. if (status < 0)
  1291. goto error;
  1292. } while (!(cur_cmd == DRX_SCU_READY) && (time_is_after_jiffies(end)));
  1293. if (cur_cmd != DRX_SCU_READY) {
  1294. pr_err("SCU not ready\n");
  1295. status = -EIO;
  1296. goto error2;
  1297. }
  1298. /* read results */
  1299. if ((result_len > 0) && (result != NULL)) {
  1300. s16 err;
  1301. int ii;
  1302. for (ii = result_len - 1; ii >= 0; ii -= 1) {
  1303. status = read16(state, SCU_RAM_PARAM_0__A - ii,
  1304. &result[ii]);
  1305. if (status < 0)
  1306. goto error;
  1307. }
  1308. /* Check if an error was reported by SCU */
  1309. err = (s16)result[0];
  1310. if (err >= 0)
  1311. goto error;
  1312. /* check for the known error codes */
  1313. switch (err) {
  1314. case SCU_RESULT_UNKCMD:
  1315. p = "SCU_RESULT_UNKCMD";
  1316. break;
  1317. case SCU_RESULT_UNKSTD:
  1318. p = "SCU_RESULT_UNKSTD";
  1319. break;
  1320. case SCU_RESULT_SIZE:
  1321. p = "SCU_RESULT_SIZE";
  1322. break;
  1323. case SCU_RESULT_INVPAR:
  1324. p = "SCU_RESULT_INVPAR";
  1325. break;
  1326. default: /* Other negative values are errors */
  1327. sprintf(errname, "ERROR: %d\n", err);
  1328. p = errname;
  1329. }
  1330. pr_err("%s while sending cmd 0x%04x with params:", p, cmd);
  1331. print_hex_dump_bytes("drxk: ", DUMP_PREFIX_NONE, buffer, cnt);
  1332. status = -EINVAL;
  1333. goto error2;
  1334. }
  1335. error:
  1336. if (status < 0)
  1337. pr_err("Error %d on %s\n", status, __func__);
  1338. error2:
  1339. mutex_unlock(&state->mutex);
  1340. return status;
  1341. }
  1342. static int set_iqm_af(struct drxk_state *state, bool active)
  1343. {
  1344. u16 data = 0;
  1345. int status;
  1346. dprintk(1, "\n");
  1347. /* Configure IQM */
  1348. status = read16(state, IQM_AF_STDBY__A, &data);
  1349. if (status < 0)
  1350. goto error;
  1351. if (!active) {
  1352. data |= (IQM_AF_STDBY_STDBY_ADC_STANDBY
  1353. | IQM_AF_STDBY_STDBY_AMP_STANDBY
  1354. | IQM_AF_STDBY_STDBY_PD_STANDBY
  1355. | IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY
  1356. | IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY);
  1357. } else {
  1358. data &= ((~IQM_AF_STDBY_STDBY_ADC_STANDBY)
  1359. & (~IQM_AF_STDBY_STDBY_AMP_STANDBY)
  1360. & (~IQM_AF_STDBY_STDBY_PD_STANDBY)
  1361. & (~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY)
  1362. & (~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY)
  1363. );
  1364. }
  1365. status = write16(state, IQM_AF_STDBY__A, data);
  1366. error:
  1367. if (status < 0)
  1368. pr_err("Error %d on %s\n", status, __func__);
  1369. return status;
  1370. }
  1371. static int ctrl_power_mode(struct drxk_state *state, enum drx_power_mode *mode)
  1372. {
  1373. int status = 0;
  1374. u16 sio_cc_pwd_mode = 0;
  1375. dprintk(1, "\n");
  1376. /* Check arguments */
  1377. if (mode == NULL)
  1378. return -EINVAL;
  1379. switch (*mode) {
  1380. case DRX_POWER_UP:
  1381. sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_NONE;
  1382. break;
  1383. case DRXK_POWER_DOWN_OFDM:
  1384. sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_OFDM;
  1385. break;
  1386. case DRXK_POWER_DOWN_CORE:
  1387. sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_CLOCK;
  1388. break;
  1389. case DRXK_POWER_DOWN_PLL:
  1390. sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_PLL;
  1391. break;
  1392. case DRX_POWER_DOWN:
  1393. sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_OSC;
  1394. break;
  1395. default:
  1396. /* Unknow sleep mode */
  1397. return -EINVAL;
  1398. }
  1399. /* If already in requested power mode, do nothing */
  1400. if (state->m_current_power_mode == *mode)
  1401. return 0;
  1402. /* For next steps make sure to start from DRX_POWER_UP mode */
  1403. if (state->m_current_power_mode != DRX_POWER_UP) {
  1404. status = power_up_device(state);
  1405. if (status < 0)
  1406. goto error;
  1407. status = dvbt_enable_ofdm_token_ring(state, true);
  1408. if (status < 0)
  1409. goto error;
  1410. }
  1411. if (*mode == DRX_POWER_UP) {
  1412. /* Restore analog & pin configuration */
  1413. } else {
  1414. /* Power down to requested mode */
  1415. /* Backup some register settings */
  1416. /* Set pins with possible pull-ups connected
  1417. to them in input mode */
  1418. /* Analog power down */
  1419. /* ADC power down */
  1420. /* Power down device */
  1421. /* stop all comm_exec */
  1422. /* Stop and power down previous standard */
  1423. switch (state->m_operation_mode) {
  1424. case OM_DVBT:
  1425. status = mpegts_stop(state);
  1426. if (status < 0)
  1427. goto error;
  1428. status = power_down_dvbt(state, false);
  1429. if (status < 0)
  1430. goto error;
  1431. break;
  1432. case OM_QAM_ITU_A:
  1433. case OM_QAM_ITU_C:
  1434. status = mpegts_stop(state);
  1435. if (status < 0)
  1436. goto error;
  1437. status = power_down_qam(state);
  1438. if (status < 0)
  1439. goto error;
  1440. break;
  1441. default:
  1442. break;
  1443. }
  1444. status = dvbt_enable_ofdm_token_ring(state, false);
  1445. if (status < 0)
  1446. goto error;
  1447. status = write16(state, SIO_CC_PWD_MODE__A, sio_cc_pwd_mode);
  1448. if (status < 0)
  1449. goto error;
  1450. status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
  1451. if (status < 0)
  1452. goto error;
  1453. if (*mode != DRXK_POWER_DOWN_OFDM) {
  1454. state->m_hi_cfg_ctrl |=
  1455. SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
  1456. status = hi_cfg_command(state);
  1457. if (status < 0)
  1458. goto error;
  1459. }
  1460. }
  1461. state->m_current_power_mode = *mode;
  1462. error:
  1463. if (status < 0)
  1464. pr_err("Error %d on %s\n", status, __func__);
  1465. return status;
  1466. }
  1467. static int power_down_dvbt(struct drxk_state *state, bool set_power_mode)
  1468. {
  1469. enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
  1470. u16 cmd_result = 0;
  1471. u16 data = 0;
  1472. int status;
  1473. dprintk(1, "\n");
  1474. status = read16(state, SCU_COMM_EXEC__A, &data);
  1475. if (status < 0)
  1476. goto error;
  1477. if (data == SCU_COMM_EXEC_ACTIVE) {
  1478. /* Send OFDM stop command */
  1479. status = scu_command(state,
  1480. SCU_RAM_COMMAND_STANDARD_OFDM
  1481. | SCU_RAM_COMMAND_CMD_DEMOD_STOP,
  1482. 0, NULL, 1, &cmd_result);
  1483. if (status < 0)
  1484. goto error;
  1485. /* Send OFDM reset command */
  1486. status = scu_command(state,
  1487. SCU_RAM_COMMAND_STANDARD_OFDM
  1488. | SCU_RAM_COMMAND_CMD_DEMOD_RESET,
  1489. 0, NULL, 1, &cmd_result);
  1490. if (status < 0)
  1491. goto error;
  1492. }
  1493. /* Reset datapath for OFDM, processors first */
  1494. status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
  1495. if (status < 0)
  1496. goto error;
  1497. status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
  1498. if (status < 0)
  1499. goto error;
  1500. status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
  1501. if (status < 0)
  1502. goto error;
  1503. /* powerdown AFE */
  1504. status = set_iqm_af(state, false);
  1505. if (status < 0)
  1506. goto error;
  1507. /* powerdown to OFDM mode */
  1508. if (set_power_mode) {
  1509. status = ctrl_power_mode(state, &power_mode);
  1510. if (status < 0)
  1511. goto error;
  1512. }
  1513. error:
  1514. if (status < 0)
  1515. pr_err("Error %d on %s\n", status, __func__);
  1516. return status;
  1517. }
  1518. static int setoperation_mode(struct drxk_state *state,
  1519. enum operation_mode o_mode)
  1520. {
  1521. int status = 0;
  1522. dprintk(1, "\n");
  1523. /*
  1524. Stop and power down previous standard
  1525. TODO investigate total power down instead of partial
  1526. power down depending on "previous" standard.
  1527. */
  1528. /* disable HW lock indicator */
  1529. status = write16(state, SCU_RAM_GPIO__A,
  1530. SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
  1531. if (status < 0)
  1532. goto error;
  1533. /* Device is already at the required mode */
  1534. if (state->m_operation_mode == o_mode)
  1535. return 0;
  1536. switch (state->m_operation_mode) {
  1537. /* OM_NONE was added for start up */
  1538. case OM_NONE:
  1539. break;
  1540. case OM_DVBT:
  1541. status = mpegts_stop(state);
  1542. if (status < 0)
  1543. goto error;
  1544. status = power_down_dvbt(state, true);
  1545. if (status < 0)
  1546. goto error;
  1547. state->m_operation_mode = OM_NONE;
  1548. break;
  1549. case OM_QAM_ITU_A: /* fallthrough */
  1550. case OM_QAM_ITU_C:
  1551. status = mpegts_stop(state);
  1552. if (status < 0)
  1553. goto error;
  1554. status = power_down_qam(state);
  1555. if (status < 0)
  1556. goto error;
  1557. state->m_operation_mode = OM_NONE;
  1558. break;
  1559. case OM_QAM_ITU_B:
  1560. default:
  1561. status = -EINVAL;
  1562. goto error;
  1563. }
  1564. /*
  1565. Power up new standard
  1566. */
  1567. switch (o_mode) {
  1568. case OM_DVBT:
  1569. dprintk(1, ": DVB-T\n");
  1570. state->m_operation_mode = o_mode;
  1571. status = set_dvbt_standard(state, o_mode);
  1572. if (status < 0)
  1573. goto error;
  1574. break;
  1575. case OM_QAM_ITU_A: /* fallthrough */
  1576. case OM_QAM_ITU_C:
  1577. dprintk(1, ": DVB-C Annex %c\n",
  1578. (state->m_operation_mode == OM_QAM_ITU_A) ? 'A' : 'C');
  1579. state->m_operation_mode = o_mode;
  1580. status = set_qam_standard(state, o_mode);
  1581. if (status < 0)
  1582. goto error;
  1583. break;
  1584. case OM_QAM_ITU_B:
  1585. default:
  1586. status = -EINVAL;
  1587. }
  1588. error:
  1589. if (status < 0)
  1590. pr_err("Error %d on %s\n", status, __func__);
  1591. return status;
  1592. }
  1593. static int start(struct drxk_state *state, s32 offset_freq,
  1594. s32 intermediate_frequency)
  1595. {
  1596. int status = -EINVAL;
  1597. u16 i_freqk_hz;
  1598. s32 offsetk_hz = offset_freq / 1000;
  1599. dprintk(1, "\n");
  1600. if (state->m_drxk_state != DRXK_STOPPED &&
  1601. state->m_drxk_state != DRXK_DTV_STARTED)
  1602. goto error;
  1603. state->m_b_mirror_freq_spect = (state->props.inversion == INVERSION_ON);
  1604. if (intermediate_frequency < 0) {
  1605. state->m_b_mirror_freq_spect = !state->m_b_mirror_freq_spect;
  1606. intermediate_frequency = -intermediate_frequency;
  1607. }
  1608. switch (state->m_operation_mode) {
  1609. case OM_QAM_ITU_A:
  1610. case OM_QAM_ITU_C:
  1611. i_freqk_hz = (intermediate_frequency / 1000);
  1612. status = set_qam(state, i_freqk_hz, offsetk_hz);
  1613. if (status < 0)
  1614. goto error;
  1615. state->m_drxk_state = DRXK_DTV_STARTED;
  1616. break;
  1617. case OM_DVBT:
  1618. i_freqk_hz = (intermediate_frequency / 1000);
  1619. status = mpegts_stop(state);
  1620. if (status < 0)
  1621. goto error;
  1622. status = set_dvbt(state, i_freqk_hz, offsetk_hz);
  1623. if (status < 0)
  1624. goto error;
  1625. status = dvbt_start(state);
  1626. if (status < 0)
  1627. goto error;
  1628. state->m_drxk_state = DRXK_DTV_STARTED;
  1629. break;
  1630. default:
  1631. break;
  1632. }
  1633. error:
  1634. if (status < 0)
  1635. pr_err("Error %d on %s\n", status, __func__);
  1636. return status;
  1637. }
  1638. static int shut_down(struct drxk_state *state)
  1639. {
  1640. dprintk(1, "\n");
  1641. mpegts_stop(state);
  1642. return 0;
  1643. }
  1644. static int get_lock_status(struct drxk_state *state, u32 *p_lock_status)
  1645. {
  1646. int status = -EINVAL;
  1647. dprintk(1, "\n");
  1648. if (p_lock_status == NULL)
  1649. goto error;
  1650. *p_lock_status = NOT_LOCKED;
  1651. /* define the SCU command code */
  1652. switch (state->m_operation_mode) {
  1653. case OM_QAM_ITU_A:
  1654. case OM_QAM_ITU_B:
  1655. case OM_QAM_ITU_C:
  1656. status = get_qam_lock_status(state, p_lock_status);
  1657. break;
  1658. case OM_DVBT:
  1659. status = get_dvbt_lock_status(state, p_lock_status);
  1660. break;
  1661. default:
  1662. pr_debug("Unsupported operation mode %d in %s\n",
  1663. state->m_operation_mode, __func__);
  1664. return 0;
  1665. }
  1666. error:
  1667. if (status < 0)
  1668. pr_err("Error %d on %s\n", status, __func__);
  1669. return status;
  1670. }
  1671. static int mpegts_start(struct drxk_state *state)
  1672. {
  1673. int status;
  1674. u16 fec_oc_snc_mode = 0;
  1675. /* Allow OC to sync again */
  1676. status = read16(state, FEC_OC_SNC_MODE__A, &fec_oc_snc_mode);
  1677. if (status < 0)
  1678. goto error;
  1679. fec_oc_snc_mode &= ~FEC_OC_SNC_MODE_SHUTDOWN__M;
  1680. status = write16(state, FEC_OC_SNC_MODE__A, fec_oc_snc_mode);
  1681. if (status < 0)
  1682. goto error;
  1683. status = write16(state, FEC_OC_SNC_UNLOCK__A, 1);
  1684. error:
  1685. if (status < 0)
  1686. pr_err("Error %d on %s\n", status, __func__);
  1687. return status;
  1688. }
  1689. static int mpegts_dto_init(struct drxk_state *state)
  1690. {
  1691. int status;
  1692. dprintk(1, "\n");
  1693. /* Rate integration settings */
  1694. status = write16(state, FEC_OC_RCN_CTL_STEP_LO__A, 0x0000);
  1695. if (status < 0)
  1696. goto error;
  1697. status = write16(state, FEC_OC_RCN_CTL_STEP_HI__A, 0x000C);
  1698. if (status < 0)
  1699. goto error;
  1700. status = write16(state, FEC_OC_RCN_GAIN__A, 0x000A);
  1701. if (status < 0)
  1702. goto error;
  1703. status = write16(state, FEC_OC_AVR_PARM_A__A, 0x0008);
  1704. if (status < 0)
  1705. goto error;
  1706. status = write16(state, FEC_OC_AVR_PARM_B__A, 0x0006);
  1707. if (status < 0)
  1708. goto error;
  1709. status = write16(state, FEC_OC_TMD_HI_MARGIN__A, 0x0680);
  1710. if (status < 0)
  1711. goto error;
  1712. status = write16(state, FEC_OC_TMD_LO_MARGIN__A, 0x0080);
  1713. if (status < 0)
  1714. goto error;
  1715. status = write16(state, FEC_OC_TMD_COUNT__A, 0x03F4);
  1716. if (status < 0)
  1717. goto error;
  1718. /* Additional configuration */
  1719. status = write16(state, FEC_OC_OCR_INVERT__A, 0);
  1720. if (status < 0)
  1721. goto error;
  1722. status = write16(state, FEC_OC_SNC_LWM__A, 2);
  1723. if (status < 0)
  1724. goto error;
  1725. status = write16(state, FEC_OC_SNC_HWM__A, 12);
  1726. error:
  1727. if (status < 0)
  1728. pr_err("Error %d on %s\n", status, __func__);
  1729. return status;
  1730. }
  1731. static int mpegts_dto_setup(struct drxk_state *state,
  1732. enum operation_mode o_mode)
  1733. {
  1734. int status;
  1735. u16 fec_oc_reg_mode = 0; /* FEC_OC_MODE register value */
  1736. u16 fec_oc_reg_ipr_mode = 0; /* FEC_OC_IPR_MODE register value */
  1737. u16 fec_oc_dto_mode = 0; /* FEC_OC_IPR_INVERT register value */
  1738. u16 fec_oc_fct_mode = 0; /* FEC_OC_IPR_INVERT register value */
  1739. u16 fec_oc_dto_period = 2; /* FEC_OC_IPR_INVERT register value */
  1740. u16 fec_oc_dto_burst_len = 188; /* FEC_OC_IPR_INVERT register value */
  1741. u32 fec_oc_rcn_ctl_rate = 0; /* FEC_OC_IPR_INVERT register value */
  1742. u16 fec_oc_tmd_mode = 0;
  1743. u16 fec_oc_tmd_int_upd_rate = 0;
  1744. u32 max_bit_rate = 0;
  1745. bool static_clk = false;
  1746. dprintk(1, "\n");
  1747. /* Check insertion of the Reed-Solomon parity bytes */
  1748. status = read16(state, FEC_OC_MODE__A, &fec_oc_reg_mode);
  1749. if (status < 0)
  1750. goto error;
  1751. status = read16(state, FEC_OC_IPR_MODE__A, &fec_oc_reg_ipr_mode);
  1752. if (status < 0)
  1753. goto error;
  1754. fec_oc_reg_mode &= (~FEC_OC_MODE_PARITY__M);
  1755. fec_oc_reg_ipr_mode &= (~FEC_OC_IPR_MODE_MVAL_DIS_PAR__M);
  1756. if (state->m_insert_rs_byte) {
  1757. /* enable parity symbol forward */
  1758. fec_oc_reg_mode |= FEC_OC_MODE_PARITY__M;
  1759. /* MVAL disable during parity bytes */
  1760. fec_oc_reg_ipr_mode |= FEC_OC_IPR_MODE_MVAL_DIS_PAR__M;
  1761. /* TS burst length to 204 */
  1762. fec_oc_dto_burst_len = 204;
  1763. }
  1764. /* Check serial or parallel output */
  1765. fec_oc_reg_ipr_mode &= (~(FEC_OC_IPR_MODE_SERIAL__M));
  1766. if (!state->m_enable_parallel) {
  1767. /* MPEG data output is serial -> set ipr_mode[0] */
  1768. fec_oc_reg_ipr_mode |= FEC_OC_IPR_MODE_SERIAL__M;
  1769. }
  1770. switch (o_mode) {
  1771. case OM_DVBT:
  1772. max_bit_rate = state->m_dvbt_bitrate;
  1773. fec_oc_tmd_mode = 3;
  1774. fec_oc_rcn_ctl_rate = 0xC00000;
  1775. static_clk = state->m_dvbt_static_clk;
  1776. break;
  1777. case OM_QAM_ITU_A: /* fallthrough */
  1778. case OM_QAM_ITU_C:
  1779. fec_oc_tmd_mode = 0x0004;
  1780. fec_oc_rcn_ctl_rate = 0xD2B4EE; /* good for >63 Mb/s */
  1781. max_bit_rate = state->m_dvbc_bitrate;
  1782. static_clk = state->m_dvbc_static_clk;
  1783. break;
  1784. default:
  1785. status = -EINVAL;
  1786. } /* switch (standard) */
  1787. if (status < 0)
  1788. goto error;
  1789. /* Configure DTO's */
  1790. if (static_clk) {
  1791. u32 bit_rate = 0;
  1792. /* Rational DTO for MCLK source (static MCLK rate),
  1793. Dynamic DTO for optimal grouping
  1794. (avoid intra-packet gaps),
  1795. DTO offset enable to sync TS burst with MSTRT */
  1796. fec_oc_dto_mode = (FEC_OC_DTO_MODE_DYNAMIC__M |
  1797. FEC_OC_DTO_MODE_OFFSET_ENABLE__M);
  1798. fec_oc_fct_mode = (FEC_OC_FCT_MODE_RAT_ENA__M |
  1799. FEC_OC_FCT_MODE_VIRT_ENA__M);
  1800. /* Check user defined bitrate */
  1801. bit_rate = max_bit_rate;
  1802. if (bit_rate > 75900000UL) { /* max is 75.9 Mb/s */
  1803. bit_rate = 75900000UL;
  1804. }
  1805. /* Rational DTO period:
  1806. dto_period = (Fsys / bitrate) - 2
  1807. result should be floored,
  1808. to make sure >= requested bitrate
  1809. */
  1810. fec_oc_dto_period = (u16) (((state->m_sys_clock_freq)
  1811. * 1000) / bit_rate);
  1812. if (fec_oc_dto_period <= 2)
  1813. fec_oc_dto_period = 0;
  1814. else
  1815. fec_oc_dto_period -= 2;
  1816. fec_oc_tmd_int_upd_rate = 8;
  1817. } else {
  1818. /* (commonAttr->static_clk == false) => dynamic mode */
  1819. fec_oc_dto_mode = FEC_OC_DTO_MODE_DYNAMIC__M;
  1820. fec_oc_fct_mode = FEC_OC_FCT_MODE__PRE;
  1821. fec_oc_tmd_int_upd_rate = 5;
  1822. }
  1823. /* Write appropriate registers with requested configuration */
  1824. status = write16(state, FEC_OC_DTO_BURST_LEN__A, fec_oc_dto_burst_len);
  1825. if (status < 0)
  1826. goto error;
  1827. status = write16(state, FEC_OC_DTO_PERIOD__A, fec_oc_dto_period);
  1828. if (status < 0)
  1829. goto error;
  1830. status = write16(state, FEC_OC_DTO_MODE__A, fec_oc_dto_mode);
  1831. if (status < 0)
  1832. goto error;
  1833. status = write16(state, FEC_OC_FCT_MODE__A, fec_oc_fct_mode);
  1834. if (status < 0)
  1835. goto error;
  1836. status = write16(state, FEC_OC_MODE__A, fec_oc_reg_mode);
  1837. if (status < 0)
  1838. goto error;
  1839. status = write16(state, FEC_OC_IPR_MODE__A, fec_oc_reg_ipr_mode);
  1840. if (status < 0)
  1841. goto error;
  1842. /* Rate integration settings */
  1843. status = write32(state, FEC_OC_RCN_CTL_RATE_LO__A, fec_oc_rcn_ctl_rate);
  1844. if (status < 0)
  1845. goto error;
  1846. status = write16(state, FEC_OC_TMD_INT_UPD_RATE__A,
  1847. fec_oc_tmd_int_upd_rate);
  1848. if (status < 0)
  1849. goto error;
  1850. status = write16(state, FEC_OC_TMD_MODE__A, fec_oc_tmd_mode);
  1851. error:
  1852. if (status < 0)
  1853. pr_err("Error %d on %s\n", status, __func__);
  1854. return status;
  1855. }
  1856. static int mpegts_configure_polarity(struct drxk_state *state)
  1857. {
  1858. u16 fec_oc_reg_ipr_invert = 0;
  1859. /* Data mask for the output data byte */
  1860. u16 invert_data_mask =
  1861. FEC_OC_IPR_INVERT_MD7__M | FEC_OC_IPR_INVERT_MD6__M |
  1862. FEC_OC_IPR_INVERT_MD5__M | FEC_OC_IPR_INVERT_MD4__M |
  1863. FEC_OC_IPR_INVERT_MD3__M | FEC_OC_IPR_INVERT_MD2__M |
  1864. FEC_OC_IPR_INVERT_MD1__M | FEC_OC_IPR_INVERT_MD0__M;
  1865. dprintk(1, "\n");
  1866. /* Control selective inversion of output bits */
  1867. fec_oc_reg_ipr_invert &= (~(invert_data_mask));
  1868. if (state->m_invert_data)
  1869. fec_oc_reg_ipr_invert |= invert_data_mask;
  1870. fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MERR__M));
  1871. if (state->m_invert_err)
  1872. fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MERR__M;
  1873. fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MSTRT__M));
  1874. if (state->m_invert_str)
  1875. fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MSTRT__M;
  1876. fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MVAL__M));
  1877. if (state->m_invert_val)
  1878. fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MVAL__M;
  1879. fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MCLK__M));
  1880. if (state->m_invert_clk)
  1881. fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MCLK__M;
  1882. return write16(state, FEC_OC_IPR_INVERT__A, fec_oc_reg_ipr_invert);
  1883. }
  1884. #define SCU_RAM_AGC_KI_INV_RF_POL__M 0x4000
  1885. static int set_agc_rf(struct drxk_state *state,
  1886. struct s_cfg_agc *p_agc_cfg, bool is_dtv)
  1887. {
  1888. int status = -EINVAL;
  1889. u16 data = 0;
  1890. struct s_cfg_agc *p_if_agc_settings;
  1891. dprintk(1, "\n");
  1892. if (p_agc_cfg == NULL)
  1893. goto error;
  1894. switch (p_agc_cfg->ctrl_mode) {
  1895. case DRXK_AGC_CTRL_AUTO:
  1896. /* Enable RF AGC DAC */
  1897. status = read16(state, IQM_AF_STDBY__A, &data);
  1898. if (status < 0)
  1899. goto error;
  1900. data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
  1901. status = write16(state, IQM_AF_STDBY__A, data);
  1902. if (status < 0)
  1903. goto error;
  1904. status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
  1905. if (status < 0)
  1906. goto error;
  1907. /* Enable SCU RF AGC loop */
  1908. data &= ~SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
  1909. /* Polarity */
  1910. if (state->m_rf_agc_pol)
  1911. data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
  1912. else
  1913. data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
  1914. status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
  1915. if (status < 0)
  1916. goto error;
  1917. /* Set speed (using complementary reduction value) */
  1918. status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
  1919. if (status < 0)
  1920. goto error;
  1921. data &= ~SCU_RAM_AGC_KI_RED_RAGC_RED__M;
  1922. data |= (~(p_agc_cfg->speed <<
  1923. SCU_RAM_AGC_KI_RED_RAGC_RED__B)
  1924. & SCU_RAM_AGC_KI_RED_RAGC_RED__M);
  1925. status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
  1926. if (status < 0)
  1927. goto error;
  1928. if (is_dvbt(state))
  1929. p_if_agc_settings = &state->m_dvbt_if_agc_cfg;
  1930. else if (is_qam(state))
  1931. p_if_agc_settings = &state->m_qam_if_agc_cfg;
  1932. else
  1933. p_if_agc_settings = &state->m_atv_if_agc_cfg;
  1934. if (p_if_agc_settings == NULL) {
  1935. status = -EINVAL;
  1936. goto error;
  1937. }
  1938. /* Set TOP, only if IF-AGC is in AUTO mode */
  1939. if (p_if_agc_settings->ctrl_mode == DRXK_AGC_CTRL_AUTO) {
  1940. status = write16(state,
  1941. SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
  1942. p_agc_cfg->top);
  1943. if (status < 0)
  1944. goto error;
  1945. }
  1946. /* Cut-Off current */
  1947. status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A,
  1948. p_agc_cfg->cut_off_current);
  1949. if (status < 0)
  1950. goto error;
  1951. /* Max. output level */
  1952. status = write16(state, SCU_RAM_AGC_RF_MAX__A,
  1953. p_agc_cfg->max_output_level);
  1954. if (status < 0)
  1955. goto error;
  1956. break;
  1957. case DRXK_AGC_CTRL_USER:
  1958. /* Enable RF AGC DAC */
  1959. status = read16(state, IQM_AF_STDBY__A, &data);
  1960. if (status < 0)
  1961. goto error;
  1962. data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
  1963. status = write16(state, IQM_AF_STDBY__A, data);
  1964. if (status < 0)
  1965. goto error;
  1966. /* Disable SCU RF AGC loop */
  1967. status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
  1968. if (status < 0)
  1969. goto error;
  1970. data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
  1971. if (state->m_rf_agc_pol)
  1972. data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
  1973. else
  1974. data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
  1975. status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
  1976. if (status < 0)
  1977. goto error;
  1978. /* SCU c.o.c. to 0, enabling full control range */
  1979. status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, 0);
  1980. if (status < 0)
  1981. goto error;
  1982. /* Write value to output pin */
  1983. status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A,
  1984. p_agc_cfg->output_level);
  1985. if (status < 0)
  1986. goto error;
  1987. break;
  1988. case DRXK_AGC_CTRL_OFF:
  1989. /* Disable RF AGC DAC */
  1990. status = read16(state, IQM_AF_STDBY__A, &data);
  1991. if (status < 0)
  1992. goto error;
  1993. data |= IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
  1994. status = write16(state, IQM_AF_STDBY__A, data);
  1995. if (status < 0)
  1996. goto error;
  1997. /* Disable SCU RF AGC loop */
  1998. status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
  1999. if (status < 0)
  2000. goto error;
  2001. data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
  2002. status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
  2003. if (status < 0)
  2004. goto error;
  2005. break;
  2006. default:
  2007. status = -EINVAL;
  2008. }
  2009. error:
  2010. if (status < 0)
  2011. pr_err("Error %d on %s\n", status, __func__);
  2012. return status;
  2013. }
  2014. #define SCU_RAM_AGC_KI_INV_IF_POL__M 0x2000
  2015. static int set_agc_if(struct drxk_state *state,
  2016. struct s_cfg_agc *p_agc_cfg, bool is_dtv)
  2017. {
  2018. u16 data = 0;
  2019. int status = 0;
  2020. struct s_cfg_agc *p_rf_agc_settings;
  2021. dprintk(1, "\n");
  2022. switch (p_agc_cfg->ctrl_mode) {
  2023. case DRXK_AGC_CTRL_AUTO:
  2024. /* Enable IF AGC DAC */
  2025. status = read16(state, IQM_AF_STDBY__A, &data);
  2026. if (status < 0)
  2027. goto error;
  2028. data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
  2029. status = write16(state, IQM_AF_STDBY__A, data);
  2030. if (status < 0)
  2031. goto error;
  2032. status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
  2033. if (status < 0)
  2034. goto error;
  2035. /* Enable SCU IF AGC loop */
  2036. data &= ~SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
  2037. /* Polarity */
  2038. if (state->m_if_agc_pol)
  2039. data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
  2040. else
  2041. data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
  2042. status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
  2043. if (status < 0)
  2044. goto error;
  2045. /* Set speed (using complementary reduction value) */
  2046. status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
  2047. if (status < 0)
  2048. goto error;
  2049. data &= ~SCU_RAM_AGC_KI_RED_IAGC_RED__M;
  2050. data |= (~(p_agc_cfg->speed <<
  2051. SCU_RAM_AGC_KI_RED_IAGC_RED__B)
  2052. & SCU_RAM_AGC_KI_RED_IAGC_RED__M);
  2053. status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
  2054. if (status < 0)
  2055. goto error;
  2056. if (is_qam(state))
  2057. p_rf_agc_settings = &state->m_qam_rf_agc_cfg;
  2058. else
  2059. p_rf_agc_settings = &state->m_atv_rf_agc_cfg;
  2060. if (p_rf_agc_settings == NULL)
  2061. return -1;
  2062. /* Restore TOP */
  2063. status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
  2064. p_rf_agc_settings->top);
  2065. if (status < 0)
  2066. goto error;
  2067. break;
  2068. case DRXK_AGC_CTRL_USER:
  2069. /* Enable IF AGC DAC */
  2070. status = read16(state, IQM_AF_STDBY__A, &data);
  2071. if (status < 0)
  2072. goto error;
  2073. data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
  2074. status = write16(state, IQM_AF_STDBY__A, data);
  2075. if (status < 0)
  2076. goto error;
  2077. status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
  2078. if (status < 0)
  2079. goto error;
  2080. /* Disable SCU IF AGC loop */
  2081. data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
  2082. /* Polarity */
  2083. if (state->m_if_agc_pol)
  2084. data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
  2085. else
  2086. data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
  2087. status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
  2088. if (status < 0)
  2089. goto error;
  2090. /* Write value to output pin */
  2091. status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
  2092. p_agc_cfg->output_level);
  2093. if (status < 0)
  2094. goto error;
  2095. break;
  2096. case DRXK_AGC_CTRL_OFF:
  2097. /* Disable If AGC DAC */
  2098. status = read16(state, IQM_AF_STDBY__A, &data);
  2099. if (status < 0)
  2100. goto error;
  2101. data |= IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
  2102. status = write16(state, IQM_AF_STDBY__A, data);
  2103. if (status < 0)
  2104. goto error;
  2105. /* Disable SCU IF AGC loop */
  2106. status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
  2107. if (status < 0)
  2108. goto error;
  2109. data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
  2110. status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
  2111. if (status < 0)
  2112. goto error;
  2113. break;
  2114. } /* switch (agcSettingsIf->ctrl_mode) */
  2115. /* always set the top to support
  2116. configurations without if-loop */
  2117. status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, p_agc_cfg->top);
  2118. error:
  2119. if (status < 0)
  2120. pr_err("Error %d on %s\n", status, __func__);
  2121. return status;
  2122. }
  2123. static int get_qam_signal_to_noise(struct drxk_state *state,
  2124. s32 *p_signal_to_noise)
  2125. {
  2126. int status = 0;
  2127. u16 qam_sl_err_power = 0; /* accum. error between
  2128. raw and sliced symbols */
  2129. u32 qam_sl_sig_power = 0; /* used for MER, depends of
  2130. QAM modulation */
  2131. u32 qam_sl_mer = 0; /* QAM MER */
  2132. dprintk(1, "\n");
  2133. /* MER calculation */
  2134. /* get the register value needed for MER */
  2135. status = read16(state, QAM_SL_ERR_POWER__A, &qam_sl_err_power);
  2136. if (status < 0) {
  2137. pr_err("Error %d on %s\n", status, __func__);
  2138. return -EINVAL;
  2139. }
  2140. switch (state->props.modulation) {
  2141. case QAM_16:
  2142. qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM16 << 2;
  2143. break;
  2144. case QAM_32:
  2145. qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM32 << 2;
  2146. break;
  2147. case QAM_64:
  2148. qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM64 << 2;
  2149. break;
  2150. case QAM_128:
  2151. qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM128 << 2;
  2152. break;
  2153. default:
  2154. case QAM_256:
  2155. qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM256 << 2;
  2156. break;
  2157. }
  2158. if (qam_sl_err_power > 0) {
  2159. qam_sl_mer = log10times100(qam_sl_sig_power) -
  2160. log10times100((u32) qam_sl_err_power);
  2161. }
  2162. *p_signal_to_noise = qam_sl_mer;
  2163. return status;
  2164. }
  2165. static int get_dvbt_signal_to_noise(struct drxk_state *state,
  2166. s32 *p_signal_to_noise)
  2167. {
  2168. int status;
  2169. u16 reg_data = 0;
  2170. u32 eq_reg_td_sqr_err_i = 0;
  2171. u32 eq_reg_td_sqr_err_q = 0;
  2172. u16 eq_reg_td_sqr_err_exp = 0;
  2173. u16 eq_reg_td_tps_pwr_ofs = 0;
  2174. u16 eq_reg_td_req_smb_cnt = 0;
  2175. u32 tps_cnt = 0;
  2176. u32 sqr_err_iq = 0;
  2177. u32 a = 0;
  2178. u32 b = 0;
  2179. u32 c = 0;
  2180. u32 i_mer = 0;
  2181. u16 transmission_params = 0;
  2182. dprintk(1, "\n");
  2183. status = read16(state, OFDM_EQ_TOP_TD_TPS_PWR_OFS__A,
  2184. &eq_reg_td_tps_pwr_ofs);
  2185. if (status < 0)
  2186. goto error;
  2187. status = read16(state, OFDM_EQ_TOP_TD_REQ_SMB_CNT__A,
  2188. &eq_reg_td_req_smb_cnt);
  2189. if (status < 0)
  2190. goto error;
  2191. status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_EXP__A,
  2192. &eq_reg_td_sqr_err_exp);
  2193. if (status < 0)
  2194. goto error;
  2195. status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_I__A,
  2196. &reg_data);
  2197. if (status < 0)
  2198. goto error;
  2199. /* Extend SQR_ERR_I operational range */
  2200. eq_reg_td_sqr_err_i = (u32) reg_data;
  2201. if ((eq_reg_td_sqr_err_exp > 11) &&
  2202. (eq_reg_td_sqr_err_i < 0x00000FFFUL)) {
  2203. eq_reg_td_sqr_err_i += 0x00010000UL;
  2204. }
  2205. status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_Q__A, &reg_data);
  2206. if (status < 0)
  2207. goto error;
  2208. /* Extend SQR_ERR_Q operational range */
  2209. eq_reg_td_sqr_err_q = (u32) reg_data;
  2210. if ((eq_reg_td_sqr_err_exp > 11) &&
  2211. (eq_reg_td_sqr_err_q < 0x00000FFFUL))
  2212. eq_reg_td_sqr_err_q += 0x00010000UL;
  2213. status = read16(state, OFDM_SC_RA_RAM_OP_PARAM__A,
  2214. &transmission_params);
  2215. if (status < 0)
  2216. goto error;
  2217. /* Check input data for MER */
  2218. /* MER calculation (in 0.1 dB) without math.h */
  2219. if ((eq_reg_td_tps_pwr_ofs == 0) || (eq_reg_td_req_smb_cnt == 0))
  2220. i_mer = 0;
  2221. else if ((eq_reg_td_sqr_err_i + eq_reg_td_sqr_err_q) == 0) {
  2222. /* No error at all, this must be the HW reset value
  2223. * Apparently no first measurement yet
  2224. * Set MER to 0.0 */
  2225. i_mer = 0;
  2226. } else {
  2227. sqr_err_iq = (eq_reg_td_sqr_err_i + eq_reg_td_sqr_err_q) <<
  2228. eq_reg_td_sqr_err_exp;
  2229. if ((transmission_params &
  2230. OFDM_SC_RA_RAM_OP_PARAM_MODE__M)
  2231. == OFDM_SC_RA_RAM_OP_PARAM_MODE_2K)
  2232. tps_cnt = 17;
  2233. else
  2234. tps_cnt = 68;
  2235. /* IMER = 100 * log10 (x)
  2236. where x = (eq_reg_td_tps_pwr_ofs^2 *
  2237. eq_reg_td_req_smb_cnt * tps_cnt)/sqr_err_iq
  2238. => IMER = a + b -c
  2239. where a = 100 * log10 (eq_reg_td_tps_pwr_ofs^2)
  2240. b = 100 * log10 (eq_reg_td_req_smb_cnt * tps_cnt)
  2241. c = 100 * log10 (sqr_err_iq)
  2242. */
  2243. /* log(x) x = 9bits * 9bits->18 bits */
  2244. a = log10times100(eq_reg_td_tps_pwr_ofs *
  2245. eq_reg_td_tps_pwr_ofs);
  2246. /* log(x) x = 16bits * 7bits->23 bits */
  2247. b = log10times100(eq_reg_td_req_smb_cnt * tps_cnt);
  2248. /* log(x) x = (16bits + 16bits) << 15 ->32 bits */
  2249. c = log10times100(sqr_err_iq);
  2250. i_mer = a + b - c;
  2251. }
  2252. *p_signal_to_noise = i_mer;
  2253. error:
  2254. if (status < 0)
  2255. pr_err("Error %d on %s\n", status, __func__);
  2256. return status;
  2257. }
  2258. static int get_signal_to_noise(struct drxk_state *state, s32 *p_signal_to_noise)
  2259. {
  2260. dprintk(1, "\n");
  2261. *p_signal_to_noise = 0;
  2262. switch (state->m_operation_mode) {
  2263. case OM_DVBT:
  2264. return get_dvbt_signal_to_noise(state, p_signal_to_noise);
  2265. case OM_QAM_ITU_A:
  2266. case OM_QAM_ITU_C:
  2267. return get_qam_signal_to_noise(state, p_signal_to_noise);
  2268. default:
  2269. break;
  2270. }
  2271. return 0;
  2272. }
  2273. #if 0
  2274. static int get_dvbt_quality(struct drxk_state *state, s32 *p_quality)
  2275. {
  2276. /* SNR Values for quasi errorfree reception rom Nordig 2.2 */
  2277. int status = 0;
  2278. dprintk(1, "\n");
  2279. static s32 QE_SN[] = {
  2280. 51, /* QPSK 1/2 */
  2281. 69, /* QPSK 2/3 */
  2282. 79, /* QPSK 3/4 */
  2283. 89, /* QPSK 5/6 */
  2284. 97, /* QPSK 7/8 */
  2285. 108, /* 16-QAM 1/2 */
  2286. 131, /* 16-QAM 2/3 */
  2287. 146, /* 16-QAM 3/4 */
  2288. 156, /* 16-QAM 5/6 */
  2289. 160, /* 16-QAM 7/8 */
  2290. 165, /* 64-QAM 1/2 */
  2291. 187, /* 64-QAM 2/3 */
  2292. 202, /* 64-QAM 3/4 */
  2293. 216, /* 64-QAM 5/6 */
  2294. 225, /* 64-QAM 7/8 */
  2295. };
  2296. *p_quality = 0;
  2297. do {
  2298. s32 signal_to_noise = 0;
  2299. u16 constellation = 0;
  2300. u16 code_rate = 0;
  2301. u32 signal_to_noise_rel;
  2302. u32 ber_quality;
  2303. status = get_dvbt_signal_to_noise(state, &signal_to_noise);
  2304. if (status < 0)
  2305. break;
  2306. status = read16(state, OFDM_EQ_TOP_TD_TPS_CONST__A,
  2307. &constellation);
  2308. if (status < 0)
  2309. break;
  2310. constellation &= OFDM_EQ_TOP_TD_TPS_CONST__M;
  2311. status = read16(state, OFDM_EQ_TOP_TD_TPS_CODE_HP__A,
  2312. &code_rate);
  2313. if (status < 0)
  2314. break;
  2315. code_rate &= OFDM_EQ_TOP_TD_TPS_CODE_HP__M;
  2316. if (constellation > OFDM_EQ_TOP_TD_TPS_CONST_64QAM ||
  2317. code_rate > OFDM_EQ_TOP_TD_TPS_CODE_LP_7_8)
  2318. break;
  2319. signal_to_noise_rel = signal_to_noise -
  2320. QE_SN[constellation * 5 + code_rate];
  2321. ber_quality = 100;
  2322. if (signal_to_noise_rel < -70)
  2323. *p_quality = 0;
  2324. else if (signal_to_noise_rel < 30)
  2325. *p_quality = ((signal_to_noise_rel + 70) *
  2326. ber_quality) / 100;
  2327. else
  2328. *p_quality = ber_quality;
  2329. } while (0);
  2330. return 0;
  2331. };
  2332. static int get_dvbc_quality(struct drxk_state *state, s32 *p_quality)
  2333. {
  2334. int status = 0;
  2335. *p_quality = 0;
  2336. dprintk(1, "\n");
  2337. do {
  2338. u32 signal_to_noise = 0;
  2339. u32 ber_quality = 100;
  2340. u32 signal_to_noise_rel = 0;
  2341. status = get_qam_signal_to_noise(state, &signal_to_noise);
  2342. if (status < 0)
  2343. break;
  2344. switch (state->props.modulation) {
  2345. case QAM_16:
  2346. signal_to_noise_rel = signal_to_noise - 200;
  2347. break;
  2348. case QAM_32:
  2349. signal_to_noise_rel = signal_to_noise - 230;
  2350. break; /* Not in NorDig */
  2351. case QAM_64:
  2352. signal_to_noise_rel = signal_to_noise - 260;
  2353. break;
  2354. case QAM_128:
  2355. signal_to_noise_rel = signal_to_noise - 290;
  2356. break;
  2357. default:
  2358. case QAM_256:
  2359. signal_to_noise_rel = signal_to_noise - 320;
  2360. break;
  2361. }
  2362. if (signal_to_noise_rel < -70)
  2363. *p_quality = 0;
  2364. else if (signal_to_noise_rel < 30)
  2365. *p_quality = ((signal_to_noise_rel + 70) *
  2366. ber_quality) / 100;
  2367. else
  2368. *p_quality = ber_quality;
  2369. } while (0);
  2370. return status;
  2371. }
  2372. static int get_quality(struct drxk_state *state, s32 *p_quality)
  2373. {
  2374. dprintk(1, "\n");
  2375. switch (state->m_operation_mode) {
  2376. case OM_DVBT:
  2377. return get_dvbt_quality(state, p_quality);
  2378. case OM_QAM_ITU_A:
  2379. return get_dvbc_quality(state, p_quality);
  2380. default:
  2381. break;
  2382. }
  2383. return 0;
  2384. }
  2385. #endif
  2386. /* Free data ram in SIO HI */
  2387. #define SIO_HI_RA_RAM_USR_BEGIN__A 0x420040
  2388. #define SIO_HI_RA_RAM_USR_END__A 0x420060
  2389. #define DRXK_HI_ATOMIC_BUF_START (SIO_HI_RA_RAM_USR_BEGIN__A)
  2390. #define DRXK_HI_ATOMIC_BUF_END (SIO_HI_RA_RAM_USR_BEGIN__A + 7)
  2391. #define DRXK_HI_ATOMIC_READ SIO_HI_RA_RAM_PAR_3_ACP_RW_READ
  2392. #define DRXK_HI_ATOMIC_WRITE SIO_HI_RA_RAM_PAR_3_ACP_RW_WRITE
  2393. #define DRXDAP_FASI_ADDR2BLOCK(addr) (((addr) >> 22) & 0x3F)
  2394. #define DRXDAP_FASI_ADDR2BANK(addr) (((addr) >> 16) & 0x3F)
  2395. #define DRXDAP_FASI_ADDR2OFFSET(addr) ((addr) & 0x7FFF)
  2396. static int ConfigureI2CBridge(struct drxk_state *state, bool b_enable_bridge)
  2397. {
  2398. int status = -EINVAL;
  2399. dprintk(1, "\n");
  2400. if (state->m_drxk_state == DRXK_UNINITIALIZED)
  2401. return 0;
  2402. if (state->m_drxk_state == DRXK_POWERED_DOWN)
  2403. goto error;
  2404. if (state->no_i2c_bridge)
  2405. return 0;
  2406. status = write16(state, SIO_HI_RA_RAM_PAR_1__A,
  2407. SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
  2408. if (status < 0)
  2409. goto error;
  2410. if (b_enable_bridge) {
  2411. status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
  2412. SIO_HI_RA_RAM_PAR_2_BRD_CFG_CLOSED);
  2413. if (status < 0)
  2414. goto error;
  2415. } else {
  2416. status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
  2417. SIO_HI_RA_RAM_PAR_2_BRD_CFG_OPEN);
  2418. if (status < 0)
  2419. goto error;
  2420. }
  2421. status = hi_command(state, SIO_HI_RA_RAM_CMD_BRDCTRL, NULL);
  2422. error:
  2423. if (status < 0)
  2424. pr_err("Error %d on %s\n", status, __func__);
  2425. return status;
  2426. }
  2427. static int set_pre_saw(struct drxk_state *state,
  2428. struct s_cfg_pre_saw *p_pre_saw_cfg)
  2429. {
  2430. int status = -EINVAL;
  2431. dprintk(1, "\n");
  2432. if ((p_pre_saw_cfg == NULL)
  2433. || (p_pre_saw_cfg->reference > IQM_AF_PDREF__M))
  2434. goto error;
  2435. status = write16(state, IQM_AF_PDREF__A, p_pre_saw_cfg->reference);
  2436. error:
  2437. if (status < 0)
  2438. pr_err("Error %d on %s\n", status, __func__);
  2439. return status;
  2440. }
  2441. static int bl_direct_cmd(struct drxk_state *state, u32 target_addr,
  2442. u16 rom_offset, u16 nr_of_elements, u32 time_out)
  2443. {
  2444. u16 bl_status = 0;
  2445. u16 offset = (u16) ((target_addr >> 0) & 0x00FFFF);
  2446. u16 blockbank = (u16) ((target_addr >> 16) & 0x000FFF);
  2447. int status;
  2448. unsigned long end;
  2449. dprintk(1, "\n");
  2450. mutex_lock(&state->mutex);
  2451. status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_DIRECT);
  2452. if (status < 0)
  2453. goto error;
  2454. status = write16(state, SIO_BL_TGT_HDR__A, blockbank);
  2455. if (status < 0)
  2456. goto error;
  2457. status = write16(state, SIO_BL_TGT_ADDR__A, offset);
  2458. if (status < 0)
  2459. goto error;
  2460. status = write16(state, SIO_BL_SRC_ADDR__A, rom_offset);
  2461. if (status < 0)
  2462. goto error;
  2463. status = write16(state, SIO_BL_SRC_LEN__A, nr_of_elements);
  2464. if (status < 0)
  2465. goto error;
  2466. status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
  2467. if (status < 0)
  2468. goto error;
  2469. end = jiffies + msecs_to_jiffies(time_out);
  2470. do {
  2471. status = read16(state, SIO_BL_STATUS__A, &bl_status);
  2472. if (status < 0)
  2473. goto error;
  2474. } while ((bl_status == 0x1) && time_is_after_jiffies(end));
  2475. if (bl_status == 0x1) {
  2476. pr_err("SIO not ready\n");
  2477. status = -EINVAL;
  2478. goto error2;
  2479. }
  2480. error:
  2481. if (status < 0)
  2482. pr_err("Error %d on %s\n", status, __func__);
  2483. error2:
  2484. mutex_unlock(&state->mutex);
  2485. return status;
  2486. }
  2487. static int adc_sync_measurement(struct drxk_state *state, u16 *count)
  2488. {
  2489. u16 data = 0;
  2490. int status;
  2491. dprintk(1, "\n");
  2492. /* start measurement */
  2493. status = write16(state, IQM_AF_COMM_EXEC__A, IQM_AF_COMM_EXEC_ACTIVE);
  2494. if (status < 0)
  2495. goto error;
  2496. status = write16(state, IQM_AF_START_LOCK__A, 1);
  2497. if (status < 0)
  2498. goto error;
  2499. *count = 0;
  2500. status = read16(state, IQM_AF_PHASE0__A, &data);
  2501. if (status < 0)
  2502. goto error;
  2503. if (data == 127)
  2504. *count = *count + 1;
  2505. status = read16(state, IQM_AF_PHASE1__A, &data);
  2506. if (status < 0)
  2507. goto error;
  2508. if (data == 127)
  2509. *count = *count + 1;
  2510. status = read16(state, IQM_AF_PHASE2__A, &data);
  2511. if (status < 0)
  2512. goto error;
  2513. if (data == 127)
  2514. *count = *count + 1;
  2515. error:
  2516. if (status < 0)
  2517. pr_err("Error %d on %s\n", status, __func__);
  2518. return status;
  2519. }
  2520. static int adc_synchronization(struct drxk_state *state)
  2521. {
  2522. u16 count = 0;
  2523. int status;
  2524. dprintk(1, "\n");
  2525. status = adc_sync_measurement(state, &count);
  2526. if (status < 0)
  2527. goto error;
  2528. if (count == 1) {
  2529. /* Try sampling on a different edge */
  2530. u16 clk_neg = 0;
  2531. status = read16(state, IQM_AF_CLKNEG__A, &clk_neg);
  2532. if (status < 0)
  2533. goto error;
  2534. if ((clk_neg & IQM_AF_CLKNEG_CLKNEGDATA__M) ==
  2535. IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS) {
  2536. clk_neg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
  2537. clk_neg |=
  2538. IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_NEG;
  2539. } else {
  2540. clk_neg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
  2541. clk_neg |=
  2542. IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS;
  2543. }
  2544. status = write16(state, IQM_AF_CLKNEG__A, clk_neg);
  2545. if (status < 0)
  2546. goto error;
  2547. status = adc_sync_measurement(state, &count);
  2548. if (status < 0)
  2549. goto error;
  2550. }
  2551. if (count < 2)
  2552. status = -EINVAL;
  2553. error:
  2554. if (status < 0)
  2555. pr_err("Error %d on %s\n", status, __func__);
  2556. return status;
  2557. }
  2558. static int set_frequency_shifter(struct drxk_state *state,
  2559. u16 intermediate_freqk_hz,
  2560. s32 tuner_freq_offset, bool is_dtv)
  2561. {
  2562. bool select_pos_image = false;
  2563. u32 rf_freq_residual = tuner_freq_offset;
  2564. u32 fm_frequency_shift = 0;
  2565. bool tuner_mirror = !state->m_b_mirror_freq_spect;
  2566. u32 adc_freq;
  2567. bool adc_flip;
  2568. int status;
  2569. u32 if_freq_actual;
  2570. u32 sampling_frequency = (u32) (state->m_sys_clock_freq / 3);
  2571. u32 frequency_shift;
  2572. bool image_to_select;
  2573. dprintk(1, "\n");
  2574. /*
  2575. Program frequency shifter
  2576. No need to account for mirroring on RF
  2577. */
  2578. if (is_dtv) {
  2579. if ((state->m_operation_mode == OM_QAM_ITU_A) ||
  2580. (state->m_operation_mode == OM_QAM_ITU_C) ||
  2581. (state->m_operation_mode == OM_DVBT))
  2582. select_pos_image = true;
  2583. else
  2584. select_pos_image = false;
  2585. }
  2586. if (tuner_mirror)
  2587. /* tuner doesn't mirror */
  2588. if_freq_actual = intermediate_freqk_hz +
  2589. rf_freq_residual + fm_frequency_shift;
  2590. else
  2591. /* tuner mirrors */
  2592. if_freq_actual = intermediate_freqk_hz -
  2593. rf_freq_residual - fm_frequency_shift;
  2594. if (if_freq_actual > sampling_frequency / 2) {
  2595. /* adc mirrors */
  2596. adc_freq = sampling_frequency - if_freq_actual;
  2597. adc_flip = true;
  2598. } else {
  2599. /* adc doesn't mirror */
  2600. adc_freq = if_freq_actual;
  2601. adc_flip = false;
  2602. }
  2603. frequency_shift = adc_freq;
  2604. image_to_select = state->m_rfmirror ^ tuner_mirror ^
  2605. adc_flip ^ select_pos_image;
  2606. state->m_iqm_fs_rate_ofs =
  2607. Frac28a((frequency_shift), sampling_frequency);
  2608. if (image_to_select)
  2609. state->m_iqm_fs_rate_ofs = ~state->m_iqm_fs_rate_ofs + 1;
  2610. /* Program frequency shifter with tuner offset compensation */
  2611. /* frequency_shift += tuner_freq_offset; TODO */
  2612. status = write32(state, IQM_FS_RATE_OFS_LO__A,
  2613. state->m_iqm_fs_rate_ofs);
  2614. if (status < 0)
  2615. pr_err("Error %d on %s\n", status, __func__);
  2616. return status;
  2617. }
  2618. static int init_agc(struct drxk_state *state, bool is_dtv)
  2619. {
  2620. u16 ingain_tgt = 0;
  2621. u16 ingain_tgt_min = 0;
  2622. u16 ingain_tgt_max = 0;
  2623. u16 clp_cyclen = 0;
  2624. u16 clp_sum_min = 0;
  2625. u16 clp_dir_to = 0;
  2626. u16 sns_sum_min = 0;
  2627. u16 sns_sum_max = 0;
  2628. u16 clp_sum_max = 0;
  2629. u16 sns_dir_to = 0;
  2630. u16 ki_innergain_min = 0;
  2631. u16 if_iaccu_hi_tgt = 0;
  2632. u16 if_iaccu_hi_tgt_min = 0;
  2633. u16 if_iaccu_hi_tgt_max = 0;
  2634. u16 data = 0;
  2635. u16 fast_clp_ctrl_delay = 0;
  2636. u16 clp_ctrl_mode = 0;
  2637. int status = 0;
  2638. dprintk(1, "\n");
  2639. /* Common settings */
  2640. sns_sum_max = 1023;
  2641. if_iaccu_hi_tgt_min = 2047;
  2642. clp_cyclen = 500;
  2643. clp_sum_max = 1023;
  2644. /* AGCInit() not available for DVBT; init done in microcode */
  2645. if (!is_qam(state)) {
  2646. pr_err("%s: mode %d is not DVB-C\n",
  2647. __func__, state->m_operation_mode);
  2648. return -EINVAL;
  2649. }
  2650. /* FIXME: Analog TV AGC require different settings */
  2651. /* Standard specific settings */
  2652. clp_sum_min = 8;
  2653. clp_dir_to = (u16) -9;
  2654. clp_ctrl_mode = 0;
  2655. sns_sum_min = 8;
  2656. sns_dir_to = (u16) -9;
  2657. ki_innergain_min = (u16) -1030;
  2658. if_iaccu_hi_tgt_max = 0x2380;
  2659. if_iaccu_hi_tgt = 0x2380;
  2660. ingain_tgt_min = 0x0511;
  2661. ingain_tgt = 0x0511;
  2662. ingain_tgt_max = 5119;
  2663. fast_clp_ctrl_delay = state->m_qam_if_agc_cfg.fast_clip_ctrl_delay;
  2664. status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A,
  2665. fast_clp_ctrl_delay);
  2666. if (status < 0)
  2667. goto error;
  2668. status = write16(state, SCU_RAM_AGC_CLP_CTRL_MODE__A, clp_ctrl_mode);
  2669. if (status < 0)
  2670. goto error;
  2671. status = write16(state, SCU_RAM_AGC_INGAIN_TGT__A, ingain_tgt);
  2672. if (status < 0)
  2673. goto error;
  2674. status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, ingain_tgt_min);
  2675. if (status < 0)
  2676. goto error;
  2677. status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, ingain_tgt_max);
  2678. if (status < 0)
  2679. goto error;
  2680. status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MIN__A,
  2681. if_iaccu_hi_tgt_min);
  2682. if (status < 0)
  2683. goto error;
  2684. status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
  2685. if_iaccu_hi_tgt_max);
  2686. if (status < 0)
  2687. goto error;
  2688. status = write16(state, SCU_RAM_AGC_IF_IACCU_HI__A, 0);
  2689. if (status < 0)
  2690. goto error;
  2691. status = write16(state, SCU_RAM_AGC_IF_IACCU_LO__A, 0);
  2692. if (status < 0)
  2693. goto error;
  2694. status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, 0);
  2695. if (status < 0)
  2696. goto error;
  2697. status = write16(state, SCU_RAM_AGC_RF_IACCU_LO__A, 0);
  2698. if (status < 0)
  2699. goto error;
  2700. status = write16(state, SCU_RAM_AGC_CLP_SUM_MAX__A, clp_sum_max);
  2701. if (status < 0)
  2702. goto error;
  2703. status = write16(state, SCU_RAM_AGC_SNS_SUM_MAX__A, sns_sum_max);
  2704. if (status < 0)
  2705. goto error;
  2706. status = write16(state, SCU_RAM_AGC_KI_INNERGAIN_MIN__A,
  2707. ki_innergain_min);
  2708. if (status < 0)
  2709. goto error;
  2710. status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT__A,
  2711. if_iaccu_hi_tgt);
  2712. if (status < 0)
  2713. goto error;
  2714. status = write16(state, SCU_RAM_AGC_CLP_CYCLEN__A, clp_cyclen);
  2715. if (status < 0)
  2716. goto error;
  2717. status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MAX__A, 1023);
  2718. if (status < 0)
  2719. goto error;
  2720. status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MIN__A, (u16) -1023);
  2721. if (status < 0)
  2722. goto error;
  2723. status = write16(state, SCU_RAM_AGC_FAST_SNS_CTRL_DELAY__A, 50);
  2724. if (status < 0)
  2725. goto error;
  2726. status = write16(state, SCU_RAM_AGC_KI_MAXMINGAIN_TH__A, 20);
  2727. if (status < 0)
  2728. goto error;
  2729. status = write16(state, SCU_RAM_AGC_CLP_SUM_MIN__A, clp_sum_min);
  2730. if (status < 0)
  2731. goto error;
  2732. status = write16(state, SCU_RAM_AGC_SNS_SUM_MIN__A, sns_sum_min);
  2733. if (status < 0)
  2734. goto error;
  2735. status = write16(state, SCU_RAM_AGC_CLP_DIR_TO__A, clp_dir_to);
  2736. if (status < 0)
  2737. goto error;
  2738. status = write16(state, SCU_RAM_AGC_SNS_DIR_TO__A, sns_dir_to);
  2739. if (status < 0)
  2740. goto error;
  2741. status = write16(state, SCU_RAM_AGC_KI_MINGAIN__A, 0x7fff);
  2742. if (status < 0)
  2743. goto error;
  2744. status = write16(state, SCU_RAM_AGC_KI_MAXGAIN__A, 0x0);
  2745. if (status < 0)
  2746. goto error;
  2747. status = write16(state, SCU_RAM_AGC_KI_MIN__A, 0x0117);
  2748. if (status < 0)
  2749. goto error;
  2750. status = write16(state, SCU_RAM_AGC_KI_MAX__A, 0x0657);
  2751. if (status < 0)
  2752. goto error;
  2753. status = write16(state, SCU_RAM_AGC_CLP_SUM__A, 0);
  2754. if (status < 0)
  2755. goto error;
  2756. status = write16(state, SCU_RAM_AGC_CLP_CYCCNT__A, 0);
  2757. if (status < 0)
  2758. goto error;
  2759. status = write16(state, SCU_RAM_AGC_CLP_DIR_WD__A, 0);
  2760. if (status < 0)
  2761. goto error;
  2762. status = write16(state, SCU_RAM_AGC_CLP_DIR_STP__A, 1);
  2763. if (status < 0)
  2764. goto error;
  2765. status = write16(state, SCU_RAM_AGC_SNS_SUM__A, 0);
  2766. if (status < 0)
  2767. goto error;
  2768. status = write16(state, SCU_RAM_AGC_SNS_CYCCNT__A, 0);
  2769. if (status < 0)
  2770. goto error;
  2771. status = write16(state, SCU_RAM_AGC_SNS_DIR_WD__A, 0);
  2772. if (status < 0)
  2773. goto error;
  2774. status = write16(state, SCU_RAM_AGC_SNS_DIR_STP__A, 1);
  2775. if (status < 0)
  2776. goto error;
  2777. status = write16(state, SCU_RAM_AGC_SNS_CYCLEN__A, 500);
  2778. if (status < 0)
  2779. goto error;
  2780. status = write16(state, SCU_RAM_AGC_KI_CYCLEN__A, 500);
  2781. if (status < 0)
  2782. goto error;
  2783. /* Initialize inner-loop KI gain factors */
  2784. status = read16(state, SCU_RAM_AGC_KI__A, &data);
  2785. if (status < 0)
  2786. goto error;
  2787. data = 0x0657;
  2788. data &= ~SCU_RAM_AGC_KI_RF__M;
  2789. data |= (DRXK_KI_RAGC_QAM << SCU_RAM_AGC_KI_RF__B);
  2790. data &= ~SCU_RAM_AGC_KI_IF__M;
  2791. data |= (DRXK_KI_IAGC_QAM << SCU_RAM_AGC_KI_IF__B);
  2792. status = write16(state, SCU_RAM_AGC_KI__A, data);
  2793. error:
  2794. if (status < 0)
  2795. pr_err("Error %d on %s\n", status, __func__);
  2796. return status;
  2797. }
  2798. static int dvbtqam_get_acc_pkt_err(struct drxk_state *state, u16 *packet_err)
  2799. {
  2800. int status;
  2801. dprintk(1, "\n");
  2802. if (packet_err == NULL)
  2803. status = write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
  2804. else
  2805. status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A,
  2806. packet_err);
  2807. if (status < 0)
  2808. pr_err("Error %d on %s\n", status, __func__);
  2809. return status;
  2810. }
  2811. static int dvbt_sc_command(struct drxk_state *state,
  2812. u16 cmd, u16 subcmd,
  2813. u16 param0, u16 param1, u16 param2,
  2814. u16 param3, u16 param4)
  2815. {
  2816. u16 cur_cmd = 0;
  2817. u16 err_code = 0;
  2818. u16 retry_cnt = 0;
  2819. u16 sc_exec = 0;
  2820. int status;
  2821. dprintk(1, "\n");
  2822. status = read16(state, OFDM_SC_COMM_EXEC__A, &sc_exec);
  2823. if (sc_exec != 1) {
  2824. /* SC is not running */
  2825. status = -EINVAL;
  2826. }
  2827. if (status < 0)
  2828. goto error;
  2829. /* Wait until sc is ready to receive command */
  2830. retry_cnt = 0;
  2831. do {
  2832. usleep_range(1000, 2000);
  2833. status = read16(state, OFDM_SC_RA_RAM_CMD__A, &cur_cmd);
  2834. retry_cnt++;
  2835. } while ((cur_cmd != 0) && (retry_cnt < DRXK_MAX_RETRIES));
  2836. if (retry_cnt >= DRXK_MAX_RETRIES && (status < 0))
  2837. goto error;
  2838. /* Write sub-command */
  2839. switch (cmd) {
  2840. /* All commands using sub-cmd */
  2841. case OFDM_SC_RA_RAM_CMD_PROC_START:
  2842. case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
  2843. case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
  2844. status = write16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, subcmd);
  2845. if (status < 0)
  2846. goto error;
  2847. break;
  2848. default:
  2849. /* Do nothing */
  2850. break;
  2851. }
  2852. /* Write needed parameters and the command */
  2853. status = 0;
  2854. switch (cmd) {
  2855. /* All commands using 5 parameters */
  2856. /* All commands using 4 parameters */
  2857. /* All commands using 3 parameters */
  2858. /* All commands using 2 parameters */
  2859. case OFDM_SC_RA_RAM_CMD_PROC_START:
  2860. case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
  2861. case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
  2862. status |= write16(state, OFDM_SC_RA_RAM_PARAM1__A, param1);
  2863. /* fall through - All commands using 1 parameters */
  2864. case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
  2865. case OFDM_SC_RA_RAM_CMD_USER_IO:
  2866. status |= write16(state, OFDM_SC_RA_RAM_PARAM0__A, param0);
  2867. /* fall through - All commands using 0 parameters */
  2868. case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
  2869. case OFDM_SC_RA_RAM_CMD_NULL:
  2870. /* Write command */
  2871. status |= write16(state, OFDM_SC_RA_RAM_CMD__A, cmd);
  2872. break;
  2873. default:
  2874. /* Unknown command */
  2875. status = -EINVAL;
  2876. }
  2877. if (status < 0)
  2878. goto error;
  2879. /* Wait until sc is ready processing command */
  2880. retry_cnt = 0;
  2881. do {
  2882. usleep_range(1000, 2000);
  2883. status = read16(state, OFDM_SC_RA_RAM_CMD__A, &cur_cmd);
  2884. retry_cnt++;
  2885. } while ((cur_cmd != 0) && (retry_cnt < DRXK_MAX_RETRIES));
  2886. if (retry_cnt >= DRXK_MAX_RETRIES && (status < 0))
  2887. goto error;
  2888. /* Check for illegal cmd */
  2889. status = read16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, &err_code);
  2890. if (err_code == 0xFFFF) {
  2891. /* illegal command */
  2892. status = -EINVAL;
  2893. }
  2894. if (status < 0)
  2895. goto error;
  2896. /* Retrieve results parameters from SC */
  2897. switch (cmd) {
  2898. /* All commands yielding 5 results */
  2899. /* All commands yielding 4 results */
  2900. /* All commands yielding 3 results */
  2901. /* All commands yielding 2 results */
  2902. /* All commands yielding 1 result */
  2903. case OFDM_SC_RA_RAM_CMD_USER_IO:
  2904. case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
  2905. status = read16(state, OFDM_SC_RA_RAM_PARAM0__A, &(param0));
  2906. /* All commands yielding 0 results */
  2907. case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
  2908. case OFDM_SC_RA_RAM_CMD_SET_TIMER:
  2909. case OFDM_SC_RA_RAM_CMD_PROC_START:
  2910. case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
  2911. case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
  2912. case OFDM_SC_RA_RAM_CMD_NULL:
  2913. break;
  2914. default:
  2915. /* Unknown command */
  2916. status = -EINVAL;
  2917. break;
  2918. } /* switch (cmd->cmd) */
  2919. error:
  2920. if (status < 0)
  2921. pr_err("Error %d on %s\n", status, __func__);
  2922. return status;
  2923. }
  2924. static int power_up_dvbt(struct drxk_state *state)
  2925. {
  2926. enum drx_power_mode power_mode = DRX_POWER_UP;
  2927. int status;
  2928. dprintk(1, "\n");
  2929. status = ctrl_power_mode(state, &power_mode);
  2930. if (status < 0)
  2931. pr_err("Error %d on %s\n", status, __func__);
  2932. return status;
  2933. }
  2934. static int dvbt_ctrl_set_inc_enable(struct drxk_state *state, bool *enabled)
  2935. {
  2936. int status;
  2937. dprintk(1, "\n");
  2938. if (*enabled)
  2939. status = write16(state, IQM_CF_BYPASSDET__A, 0);
  2940. else
  2941. status = write16(state, IQM_CF_BYPASSDET__A, 1);
  2942. if (status < 0)
  2943. pr_err("Error %d on %s\n", status, __func__);
  2944. return status;
  2945. }
  2946. #define DEFAULT_FR_THRES_8K 4000
  2947. static int dvbt_ctrl_set_fr_enable(struct drxk_state *state, bool *enabled)
  2948. {
  2949. int status;
  2950. dprintk(1, "\n");
  2951. if (*enabled) {
  2952. /* write mask to 1 */
  2953. status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A,
  2954. DEFAULT_FR_THRES_8K);
  2955. } else {
  2956. /* write mask to 0 */
  2957. status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A, 0);
  2958. }
  2959. if (status < 0)
  2960. pr_err("Error %d on %s\n", status, __func__);
  2961. return status;
  2962. }
  2963. static int dvbt_ctrl_set_echo_threshold(struct drxk_state *state,
  2964. struct drxk_cfg_dvbt_echo_thres_t *echo_thres)
  2965. {
  2966. u16 data = 0;
  2967. int status;
  2968. dprintk(1, "\n");
  2969. status = read16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, &data);
  2970. if (status < 0)
  2971. goto error;
  2972. switch (echo_thres->fft_mode) {
  2973. case DRX_FFTMODE_2K:
  2974. data &= ~OFDM_SC_RA_RAM_ECHO_THRES_2K__M;
  2975. data |= ((echo_thres->threshold <<
  2976. OFDM_SC_RA_RAM_ECHO_THRES_2K__B)
  2977. & (OFDM_SC_RA_RAM_ECHO_THRES_2K__M));
  2978. break;
  2979. case DRX_FFTMODE_8K:
  2980. data &= ~OFDM_SC_RA_RAM_ECHO_THRES_8K__M;
  2981. data |= ((echo_thres->threshold <<
  2982. OFDM_SC_RA_RAM_ECHO_THRES_8K__B)
  2983. & (OFDM_SC_RA_RAM_ECHO_THRES_8K__M));
  2984. break;
  2985. default:
  2986. return -EINVAL;
  2987. }
  2988. status = write16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, data);
  2989. error:
  2990. if (status < 0)
  2991. pr_err("Error %d on %s\n", status, __func__);
  2992. return status;
  2993. }
  2994. static int dvbt_ctrl_set_sqi_speed(struct drxk_state *state,
  2995. enum drxk_cfg_dvbt_sqi_speed *speed)
  2996. {
  2997. int status = -EINVAL;
  2998. dprintk(1, "\n");
  2999. switch (*speed) {
  3000. case DRXK_DVBT_SQI_SPEED_FAST:
  3001. case DRXK_DVBT_SQI_SPEED_MEDIUM:
  3002. case DRXK_DVBT_SQI_SPEED_SLOW:
  3003. break;
  3004. default:
  3005. goto error;
  3006. }
  3007. status = write16(state, SCU_RAM_FEC_PRE_RS_BER_FILTER_SH__A,
  3008. (u16) *speed);
  3009. error:
  3010. if (status < 0)
  3011. pr_err("Error %d on %s\n", status, __func__);
  3012. return status;
  3013. }
  3014. /*============================================================================*/
  3015. /*
  3016. * \brief Activate DVBT specific presets
  3017. * \param demod instance of demodulator.
  3018. * \return DRXStatus_t.
  3019. *
  3020. * Called in DVBTSetStandard
  3021. *
  3022. */
  3023. static int dvbt_activate_presets(struct drxk_state *state)
  3024. {
  3025. int status;
  3026. bool setincenable = false;
  3027. bool setfrenable = true;
  3028. struct drxk_cfg_dvbt_echo_thres_t echo_thres2k = { 0, DRX_FFTMODE_2K };
  3029. struct drxk_cfg_dvbt_echo_thres_t echo_thres8k = { 0, DRX_FFTMODE_8K };
  3030. dprintk(1, "\n");
  3031. status = dvbt_ctrl_set_inc_enable(state, &setincenable);
  3032. if (status < 0)
  3033. goto error;
  3034. status = dvbt_ctrl_set_fr_enable(state, &setfrenable);
  3035. if (status < 0)
  3036. goto error;
  3037. status = dvbt_ctrl_set_echo_threshold(state, &echo_thres2k);
  3038. if (status < 0)
  3039. goto error;
  3040. status = dvbt_ctrl_set_echo_threshold(state, &echo_thres8k);
  3041. if (status < 0)
  3042. goto error;
  3043. status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A,
  3044. state->m_dvbt_if_agc_cfg.ingain_tgt_max);
  3045. error:
  3046. if (status < 0)
  3047. pr_err("Error %d on %s\n", status, __func__);
  3048. return status;
  3049. }
  3050. /*============================================================================*/
  3051. /*
  3052. * \brief Initialize channelswitch-independent settings for DVBT.
  3053. * \param demod instance of demodulator.
  3054. * \return DRXStatus_t.
  3055. *
  3056. * For ROM code channel filter taps are loaded from the bootloader. For microcode
  3057. * the DVB-T taps from the drxk_filters.h are used.
  3058. */
  3059. static int set_dvbt_standard(struct drxk_state *state,
  3060. enum operation_mode o_mode)
  3061. {
  3062. u16 cmd_result = 0;
  3063. u16 data = 0;
  3064. int status;
  3065. dprintk(1, "\n");
  3066. power_up_dvbt(state);
  3067. /* added antenna switch */
  3068. switch_antenna_to_dvbt(state);
  3069. /* send OFDM reset command */
  3070. status = scu_command(state,
  3071. SCU_RAM_COMMAND_STANDARD_OFDM
  3072. | SCU_RAM_COMMAND_CMD_DEMOD_RESET,
  3073. 0, NULL, 1, &cmd_result);
  3074. if (status < 0)
  3075. goto error;
  3076. /* send OFDM setenv command */
  3077. status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
  3078. | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV,
  3079. 0, NULL, 1, &cmd_result);
  3080. if (status < 0)
  3081. goto error;
  3082. /* reset datapath for OFDM, processors first */
  3083. status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
  3084. if (status < 0)
  3085. goto error;
  3086. status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
  3087. if (status < 0)
  3088. goto error;
  3089. status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
  3090. if (status < 0)
  3091. goto error;
  3092. /* IQM setup */
  3093. /* synchronize on ofdstate->m_festart */
  3094. status = write16(state, IQM_AF_UPD_SEL__A, 1);
  3095. if (status < 0)
  3096. goto error;
  3097. /* window size for clipping ADC detection */
  3098. status = write16(state, IQM_AF_CLP_LEN__A, 0);
  3099. if (status < 0)
  3100. goto error;
  3101. /* window size for for sense pre-SAW detection */
  3102. status = write16(state, IQM_AF_SNS_LEN__A, 0);
  3103. if (status < 0)
  3104. goto error;
  3105. /* sense threshold for sense pre-SAW detection */
  3106. status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
  3107. if (status < 0)
  3108. goto error;
  3109. status = set_iqm_af(state, true);
  3110. if (status < 0)
  3111. goto error;
  3112. status = write16(state, IQM_AF_AGC_RF__A, 0);
  3113. if (status < 0)
  3114. goto error;
  3115. /* Impulse noise cruncher setup */
  3116. status = write16(state, IQM_AF_INC_LCT__A, 0); /* crunch in IQM_CF */
  3117. if (status < 0)
  3118. goto error;
  3119. status = write16(state, IQM_CF_DET_LCT__A, 0); /* detect in IQM_CF */
  3120. if (status < 0)
  3121. goto error;
  3122. status = write16(state, IQM_CF_WND_LEN__A, 3); /* peak detector window length */
  3123. if (status < 0)
  3124. goto error;
  3125. status = write16(state, IQM_RC_STRETCH__A, 16);
  3126. if (status < 0)
  3127. goto error;
  3128. status = write16(state, IQM_CF_OUT_ENA__A, 0x4); /* enable output 2 */
  3129. if (status < 0)
  3130. goto error;
  3131. status = write16(state, IQM_CF_DS_ENA__A, 0x4); /* decimate output 2 */
  3132. if (status < 0)
  3133. goto error;
  3134. status = write16(state, IQM_CF_SCALE__A, 1600);
  3135. if (status < 0)
  3136. goto error;
  3137. status = write16(state, IQM_CF_SCALE_SH__A, 0);
  3138. if (status < 0)
  3139. goto error;
  3140. /* virtual clipping threshold for clipping ADC detection */
  3141. status = write16(state, IQM_AF_CLP_TH__A, 448);
  3142. if (status < 0)
  3143. goto error;
  3144. status = write16(state, IQM_CF_DATATH__A, 495); /* crunching threshold */
  3145. if (status < 0)
  3146. goto error;
  3147. status = bl_chain_cmd(state, DRXK_BL_ROM_OFFSET_TAPS_DVBT,
  3148. DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
  3149. if (status < 0)
  3150. goto error;
  3151. status = write16(state, IQM_CF_PKDTH__A, 2); /* peak detector threshold */
  3152. if (status < 0)
  3153. goto error;
  3154. status = write16(state, IQM_CF_POW_MEAS_LEN__A, 2);
  3155. if (status < 0)
  3156. goto error;
  3157. /* enable power measurement interrupt */
  3158. status = write16(state, IQM_CF_COMM_INT_MSK__A, 1);
  3159. if (status < 0)
  3160. goto error;
  3161. status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
  3162. if (status < 0)
  3163. goto error;
  3164. /* IQM will not be reset from here, sync ADC and update/init AGC */
  3165. status = adc_synchronization(state);
  3166. if (status < 0)
  3167. goto error;
  3168. status = set_pre_saw(state, &state->m_dvbt_pre_saw_cfg);
  3169. if (status < 0)
  3170. goto error;
  3171. /* Halt SCU to enable safe non-atomic accesses */
  3172. status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
  3173. if (status < 0)
  3174. goto error;
  3175. status = set_agc_rf(state, &state->m_dvbt_rf_agc_cfg, true);
  3176. if (status < 0)
  3177. goto error;
  3178. status = set_agc_if(state, &state->m_dvbt_if_agc_cfg, true);
  3179. if (status < 0)
  3180. goto error;
  3181. /* Set Noise Estimation notch width and enable DC fix */
  3182. status = read16(state, OFDM_SC_RA_RAM_CONFIG__A, &data);
  3183. if (status < 0)
  3184. goto error;
  3185. data |= OFDM_SC_RA_RAM_CONFIG_NE_FIX_ENABLE__M;
  3186. status = write16(state, OFDM_SC_RA_RAM_CONFIG__A, data);
  3187. if (status < 0)
  3188. goto error;
  3189. /* Activate SCU to enable SCU commands */
  3190. status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
  3191. if (status < 0)
  3192. goto error;
  3193. if (!state->m_drxk_a3_rom_code) {
  3194. /* AGCInit() is not done for DVBT, so set agcfast_clip_ctrl_delay */
  3195. status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A,
  3196. state->m_dvbt_if_agc_cfg.fast_clip_ctrl_delay);
  3197. if (status < 0)
  3198. goto error;
  3199. }
  3200. /* OFDM_SC setup */
  3201. #ifdef COMPILE_FOR_NONRT
  3202. status = write16(state, OFDM_SC_RA_RAM_BE_OPT_DELAY__A, 1);
  3203. if (status < 0)
  3204. goto error;
  3205. status = write16(state, OFDM_SC_RA_RAM_BE_OPT_INIT_DELAY__A, 2);
  3206. if (status < 0)
  3207. goto error;
  3208. #endif
  3209. /* FEC setup */
  3210. status = write16(state, FEC_DI_INPUT_CTL__A, 1); /* OFDM input */
  3211. if (status < 0)
  3212. goto error;
  3213. #ifdef COMPILE_FOR_NONRT
  3214. status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x400);
  3215. if (status < 0)
  3216. goto error;
  3217. #else
  3218. status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x1000);
  3219. if (status < 0)
  3220. goto error;
  3221. #endif
  3222. status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, 0x0001);
  3223. if (status < 0)
  3224. goto error;
  3225. /* Setup MPEG bus */
  3226. status = mpegts_dto_setup(state, OM_DVBT);
  3227. if (status < 0)
  3228. goto error;
  3229. /* Set DVBT Presets */
  3230. status = dvbt_activate_presets(state);
  3231. if (status < 0)
  3232. goto error;
  3233. error:
  3234. if (status < 0)
  3235. pr_err("Error %d on %s\n", status, __func__);
  3236. return status;
  3237. }
  3238. /*============================================================================*/
  3239. /*
  3240. * \brief start dvbt demodulating for channel.
  3241. * \param demod instance of demodulator.
  3242. * \return DRXStatus_t.
  3243. */
  3244. static int dvbt_start(struct drxk_state *state)
  3245. {
  3246. u16 param1;
  3247. int status;
  3248. /* drxk_ofdm_sc_cmd_t scCmd; */
  3249. dprintk(1, "\n");
  3250. /* start correct processes to get in lock */
  3251. /* DRXK: OFDM_SC_RA_RAM_PROC_LOCKTRACK is no longer in mapfile! */
  3252. param1 = OFDM_SC_RA_RAM_LOCKTRACK_MIN;
  3253. status = dvbt_sc_command(state, OFDM_SC_RA_RAM_CMD_PROC_START, 0,
  3254. OFDM_SC_RA_RAM_SW_EVENT_RUN_NMASK__M, param1,
  3255. 0, 0, 0);
  3256. if (status < 0)
  3257. goto error;
  3258. /* start FEC OC */
  3259. status = mpegts_start(state);
  3260. if (status < 0)
  3261. goto error;
  3262. status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
  3263. if (status < 0)
  3264. goto error;
  3265. error:
  3266. if (status < 0)
  3267. pr_err("Error %d on %s\n", status, __func__);
  3268. return status;
  3269. }
  3270. /*============================================================================*/
  3271. /*
  3272. * \brief Set up dvbt demodulator for channel.
  3273. * \param demod instance of demodulator.
  3274. * \return DRXStatus_t.
  3275. * // original DVBTSetChannel()
  3276. */
  3277. static int set_dvbt(struct drxk_state *state, u16 intermediate_freqk_hz,
  3278. s32 tuner_freq_offset)
  3279. {
  3280. u16 cmd_result = 0;
  3281. u16 transmission_params = 0;
  3282. u16 operation_mode = 0;
  3283. u32 iqm_rc_rate_ofs = 0;
  3284. u32 bandwidth = 0;
  3285. u16 param1;
  3286. int status;
  3287. dprintk(1, "IF =%d, TFO = %d\n",
  3288. intermediate_freqk_hz, tuner_freq_offset);
  3289. status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
  3290. | SCU_RAM_COMMAND_CMD_DEMOD_STOP,
  3291. 0, NULL, 1, &cmd_result);
  3292. if (status < 0)
  3293. goto error;
  3294. /* Halt SCU to enable safe non-atomic accesses */
  3295. status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
  3296. if (status < 0)
  3297. goto error;
  3298. /* Stop processors */
  3299. status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
  3300. if (status < 0)
  3301. goto error;
  3302. status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
  3303. if (status < 0)
  3304. goto error;
  3305. /* Mandatory fix, always stop CP, required to set spl offset back to
  3306. hardware default (is set to 0 by ucode during pilot detection */
  3307. status = write16(state, OFDM_CP_COMM_EXEC__A, OFDM_CP_COMM_EXEC_STOP);
  3308. if (status < 0)
  3309. goto error;
  3310. /*== Write channel settings to device ================================*/
  3311. /* mode */
  3312. switch (state->props.transmission_mode) {
  3313. case TRANSMISSION_MODE_AUTO:
  3314. default:
  3315. operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_MODE__M;
  3316. /* fall through - try first guess DRX_FFTMODE_8K */
  3317. case TRANSMISSION_MODE_8K:
  3318. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_MODE_8K;
  3319. break;
  3320. case TRANSMISSION_MODE_2K:
  3321. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_MODE_2K;
  3322. break;
  3323. }
  3324. /* guard */
  3325. switch (state->props.guard_interval) {
  3326. default:
  3327. case GUARD_INTERVAL_AUTO:
  3328. operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_GUARD__M;
  3329. /* fall through - try first guess DRX_GUARD_1DIV4 */
  3330. case GUARD_INTERVAL_1_4:
  3331. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_4;
  3332. break;
  3333. case GUARD_INTERVAL_1_32:
  3334. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_32;
  3335. break;
  3336. case GUARD_INTERVAL_1_16:
  3337. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_16;
  3338. break;
  3339. case GUARD_INTERVAL_1_8:
  3340. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_8;
  3341. break;
  3342. }
  3343. /* hierarchy */
  3344. switch (state->props.hierarchy) {
  3345. case HIERARCHY_AUTO:
  3346. case HIERARCHY_NONE:
  3347. default:
  3348. operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_HIER__M;
  3349. /* try first guess SC_RA_RAM_OP_PARAM_HIER_NO */
  3350. /* transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_NO; */
  3351. /* fall through */
  3352. case HIERARCHY_1:
  3353. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A1;
  3354. break;
  3355. case HIERARCHY_2:
  3356. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A2;
  3357. break;
  3358. case HIERARCHY_4:
  3359. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A4;
  3360. break;
  3361. }
  3362. /* modulation */
  3363. switch (state->props.modulation) {
  3364. case QAM_AUTO:
  3365. default:
  3366. operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_CONST__M;
  3367. /* fall through - try first guess DRX_CONSTELLATION_QAM64 */
  3368. case QAM_64:
  3369. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM64;
  3370. break;
  3371. case QPSK:
  3372. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QPSK;
  3373. break;
  3374. case QAM_16:
  3375. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM16;
  3376. break;
  3377. }
  3378. #if 0
  3379. /* No hierarchical channels support in BDA */
  3380. /* Priority (only for hierarchical channels) */
  3381. switch (channel->priority) {
  3382. case DRX_PRIORITY_LOW:
  3383. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_LO;
  3384. WR16(dev_addr, OFDM_EC_SB_PRIOR__A,
  3385. OFDM_EC_SB_PRIOR_LO);
  3386. break;
  3387. case DRX_PRIORITY_HIGH:
  3388. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
  3389. WR16(dev_addr, OFDM_EC_SB_PRIOR__A,
  3390. OFDM_EC_SB_PRIOR_HI));
  3391. break;
  3392. case DRX_PRIORITY_UNKNOWN: /* fall through */
  3393. default:
  3394. status = -EINVAL;
  3395. goto error;
  3396. }
  3397. #else
  3398. /* Set Priority high */
  3399. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
  3400. status = write16(state, OFDM_EC_SB_PRIOR__A, OFDM_EC_SB_PRIOR_HI);
  3401. if (status < 0)
  3402. goto error;
  3403. #endif
  3404. /* coderate */
  3405. switch (state->props.code_rate_HP) {
  3406. case FEC_AUTO:
  3407. default:
  3408. operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_RATE__M;
  3409. /* fall through - try first guess DRX_CODERATE_2DIV3 */
  3410. case FEC_2_3:
  3411. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_2_3;
  3412. break;
  3413. case FEC_1_2:
  3414. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_1_2;
  3415. break;
  3416. case FEC_3_4:
  3417. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_3_4;
  3418. break;
  3419. case FEC_5_6:
  3420. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_5_6;
  3421. break;
  3422. case FEC_7_8:
  3423. transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_7_8;
  3424. break;
  3425. }
  3426. /*
  3427. * SAW filter selection: normally not necessary, but if wanted
  3428. * the application can select a SAW filter via the driver by
  3429. * using UIOs
  3430. */
  3431. /* First determine real bandwidth (Hz) */
  3432. /* Also set delay for impulse noise cruncher */
  3433. /*
  3434. * Also set parameters for EC_OC fix, note EC_OC_REG_TMD_HIL_MAR is
  3435. * changed by SC for fix for some 8K,1/8 guard but is restored by
  3436. * InitEC and ResetEC functions
  3437. */
  3438. switch (state->props.bandwidth_hz) {
  3439. case 0:
  3440. state->props.bandwidth_hz = 8000000;
  3441. /* fall through */
  3442. case 8000000:
  3443. bandwidth = DRXK_BANDWIDTH_8MHZ_IN_HZ;
  3444. status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
  3445. 3052);
  3446. if (status < 0)
  3447. goto error;
  3448. /* cochannel protection for PAL 8 MHz */
  3449. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
  3450. 7);
  3451. if (status < 0)
  3452. goto error;
  3453. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
  3454. 7);
  3455. if (status < 0)
  3456. goto error;
  3457. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
  3458. 7);
  3459. if (status < 0)
  3460. goto error;
  3461. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
  3462. 1);
  3463. if (status < 0)
  3464. goto error;
  3465. break;
  3466. case 7000000:
  3467. bandwidth = DRXK_BANDWIDTH_7MHZ_IN_HZ;
  3468. status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
  3469. 3491);
  3470. if (status < 0)
  3471. goto error;
  3472. /* cochannel protection for PAL 7 MHz */
  3473. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
  3474. 8);
  3475. if (status < 0)
  3476. goto error;
  3477. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
  3478. 8);
  3479. if (status < 0)
  3480. goto error;
  3481. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
  3482. 4);
  3483. if (status < 0)
  3484. goto error;
  3485. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
  3486. 1);
  3487. if (status < 0)
  3488. goto error;
  3489. break;
  3490. case 6000000:
  3491. bandwidth = DRXK_BANDWIDTH_6MHZ_IN_HZ;
  3492. status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
  3493. 4073);
  3494. if (status < 0)
  3495. goto error;
  3496. /* cochannel protection for NTSC 6 MHz */
  3497. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
  3498. 19);
  3499. if (status < 0)
  3500. goto error;
  3501. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
  3502. 19);
  3503. if (status < 0)
  3504. goto error;
  3505. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
  3506. 14);
  3507. if (status < 0)
  3508. goto error;
  3509. status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
  3510. 1);
  3511. if (status < 0)
  3512. goto error;
  3513. break;
  3514. default:
  3515. status = -EINVAL;
  3516. goto error;
  3517. }
  3518. if (iqm_rc_rate_ofs == 0) {
  3519. /* Now compute IQM_RC_RATE_OFS
  3520. (((SysFreq/BandWidth)/2)/2) -1) * 2^23)
  3521. =>
  3522. ((SysFreq / BandWidth) * (2^21)) - (2^23)
  3523. */
  3524. /* (SysFreq / BandWidth) * (2^28) */
  3525. /*
  3526. * assert (MAX(sysClk)/MIN(bandwidth) < 16)
  3527. * => assert(MAX(sysClk) < 16*MIN(bandwidth))
  3528. * => assert(109714272 > 48000000) = true
  3529. * so Frac 28 can be used
  3530. */
  3531. iqm_rc_rate_ofs = Frac28a((u32)
  3532. ((state->m_sys_clock_freq *
  3533. 1000) / 3), bandwidth);
  3534. /* (SysFreq / BandWidth) * (2^21), rounding before truncating */
  3535. if ((iqm_rc_rate_ofs & 0x7fL) >= 0x40)
  3536. iqm_rc_rate_ofs += 0x80L;
  3537. iqm_rc_rate_ofs = iqm_rc_rate_ofs >> 7;
  3538. /* ((SysFreq / BandWidth) * (2^21)) - (2^23) */
  3539. iqm_rc_rate_ofs = iqm_rc_rate_ofs - (1 << 23);
  3540. }
  3541. iqm_rc_rate_ofs &=
  3542. ((((u32) IQM_RC_RATE_OFS_HI__M) <<
  3543. IQM_RC_RATE_OFS_LO__W) | IQM_RC_RATE_OFS_LO__M);
  3544. status = write32(state, IQM_RC_RATE_OFS_LO__A, iqm_rc_rate_ofs);
  3545. if (status < 0)
  3546. goto error;
  3547. /* Bandwidth setting done */
  3548. #if 0
  3549. status = dvbt_set_frequency_shift(demod, channel, tuner_offset);
  3550. if (status < 0)
  3551. goto error;
  3552. #endif
  3553. status = set_frequency_shifter(state, intermediate_freqk_hz,
  3554. tuner_freq_offset, true);
  3555. if (status < 0)
  3556. goto error;
  3557. /*== start SC, write channel settings to SC ==========================*/
  3558. /* Activate SCU to enable SCU commands */
  3559. status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
  3560. if (status < 0)
  3561. goto error;
  3562. /* Enable SC after setting all other parameters */
  3563. status = write16(state, OFDM_SC_COMM_STATE__A, 0);
  3564. if (status < 0)
  3565. goto error;
  3566. status = write16(state, OFDM_SC_COMM_EXEC__A, 1);
  3567. if (status < 0)
  3568. goto error;
  3569. status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
  3570. | SCU_RAM_COMMAND_CMD_DEMOD_START,
  3571. 0, NULL, 1, &cmd_result);
  3572. if (status < 0)
  3573. goto error;
  3574. /* Write SC parameter registers, set all AUTO flags in operation mode */
  3575. param1 = (OFDM_SC_RA_RAM_OP_AUTO_MODE__M |
  3576. OFDM_SC_RA_RAM_OP_AUTO_GUARD__M |
  3577. OFDM_SC_RA_RAM_OP_AUTO_CONST__M |
  3578. OFDM_SC_RA_RAM_OP_AUTO_HIER__M |
  3579. OFDM_SC_RA_RAM_OP_AUTO_RATE__M);
  3580. status = dvbt_sc_command(state, OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM,
  3581. 0, transmission_params, param1, 0, 0, 0);
  3582. if (status < 0)
  3583. goto error;
  3584. if (!state->m_drxk_a3_rom_code)
  3585. status = dvbt_ctrl_set_sqi_speed(state, &state->m_sqi_speed);
  3586. error:
  3587. if (status < 0)
  3588. pr_err("Error %d on %s\n", status, __func__);
  3589. return status;
  3590. }
  3591. /*============================================================================*/
  3592. /*
  3593. * \brief Retrieve lock status .
  3594. * \param demod Pointer to demodulator instance.
  3595. * \param lockStat Pointer to lock status structure.
  3596. * \return DRXStatus_t.
  3597. *
  3598. */
  3599. static int get_dvbt_lock_status(struct drxk_state *state, u32 *p_lock_status)
  3600. {
  3601. int status;
  3602. const u16 mpeg_lock_mask = (OFDM_SC_RA_RAM_LOCK_MPEG__M |
  3603. OFDM_SC_RA_RAM_LOCK_FEC__M);
  3604. const u16 fec_lock_mask = (OFDM_SC_RA_RAM_LOCK_FEC__M);
  3605. const u16 demod_lock_mask = OFDM_SC_RA_RAM_LOCK_DEMOD__M;
  3606. u16 sc_ra_ram_lock = 0;
  3607. u16 sc_comm_exec = 0;
  3608. dprintk(1, "\n");
  3609. *p_lock_status = NOT_LOCKED;
  3610. /* driver 0.9.0 */
  3611. /* Check if SC is running */
  3612. status = read16(state, OFDM_SC_COMM_EXEC__A, &sc_comm_exec);
  3613. if (status < 0)
  3614. goto end;
  3615. if (sc_comm_exec == OFDM_SC_COMM_EXEC_STOP)
  3616. goto end;
  3617. status = read16(state, OFDM_SC_RA_RAM_LOCK__A, &sc_ra_ram_lock);
  3618. if (status < 0)
  3619. goto end;
  3620. if ((sc_ra_ram_lock & mpeg_lock_mask) == mpeg_lock_mask)
  3621. *p_lock_status = MPEG_LOCK;
  3622. else if ((sc_ra_ram_lock & fec_lock_mask) == fec_lock_mask)
  3623. *p_lock_status = FEC_LOCK;
  3624. else if ((sc_ra_ram_lock & demod_lock_mask) == demod_lock_mask)
  3625. *p_lock_status = DEMOD_LOCK;
  3626. else if (sc_ra_ram_lock & OFDM_SC_RA_RAM_LOCK_NODVBT__M)
  3627. *p_lock_status = NEVER_LOCK;
  3628. end:
  3629. if (status < 0)
  3630. pr_err("Error %d on %s\n", status, __func__);
  3631. return status;
  3632. }
  3633. static int power_up_qam(struct drxk_state *state)
  3634. {
  3635. enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
  3636. int status;
  3637. dprintk(1, "\n");
  3638. status = ctrl_power_mode(state, &power_mode);
  3639. if (status < 0)
  3640. pr_err("Error %d on %s\n", status, __func__);
  3641. return status;
  3642. }
  3643. /* Power Down QAM */
  3644. static int power_down_qam(struct drxk_state *state)
  3645. {
  3646. u16 data = 0;
  3647. u16 cmd_result;
  3648. int status = 0;
  3649. dprintk(1, "\n");
  3650. status = read16(state, SCU_COMM_EXEC__A, &data);
  3651. if (status < 0)
  3652. goto error;
  3653. if (data == SCU_COMM_EXEC_ACTIVE) {
  3654. /*
  3655. STOP demodulator
  3656. QAM and HW blocks
  3657. */
  3658. /* stop all comstate->m_exec */
  3659. status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
  3660. if (status < 0)
  3661. goto error;
  3662. status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
  3663. | SCU_RAM_COMMAND_CMD_DEMOD_STOP,
  3664. 0, NULL, 1, &cmd_result);
  3665. if (status < 0)
  3666. goto error;
  3667. }
  3668. /* powerdown AFE */
  3669. status = set_iqm_af(state, false);
  3670. error:
  3671. if (status < 0)
  3672. pr_err("Error %d on %s\n", status, __func__);
  3673. return status;
  3674. }
  3675. /*============================================================================*/
  3676. /*
  3677. * \brief Setup of the QAM Measurement intervals for signal quality
  3678. * \param demod instance of demod.
  3679. * \param modulation current modulation.
  3680. * \return DRXStatus_t.
  3681. *
  3682. * NOTE:
  3683. * Take into account that for certain settings the errorcounters can overflow.
  3684. * The implementation does not check this.
  3685. *
  3686. */
  3687. static int set_qam_measurement(struct drxk_state *state,
  3688. enum e_drxk_constellation modulation,
  3689. u32 symbol_rate)
  3690. {
  3691. u32 fec_bits_desired = 0; /* BER accounting period */
  3692. u32 fec_rs_period_total = 0; /* Total period */
  3693. u16 fec_rs_prescale = 0; /* ReedSolomon Measurement Prescale */
  3694. u16 fec_rs_period = 0; /* Value for corresponding I2C register */
  3695. int status = 0;
  3696. dprintk(1, "\n");
  3697. fec_rs_prescale = 1;
  3698. /* fec_bits_desired = symbol_rate [kHz] *
  3699. FrameLenght [ms] *
  3700. (modulation + 1) *
  3701. SyncLoss (== 1) *
  3702. ViterbiLoss (==1)
  3703. */
  3704. switch (modulation) {
  3705. case DRX_CONSTELLATION_QAM16:
  3706. fec_bits_desired = 4 * symbol_rate;
  3707. break;
  3708. case DRX_CONSTELLATION_QAM32:
  3709. fec_bits_desired = 5 * symbol_rate;
  3710. break;
  3711. case DRX_CONSTELLATION_QAM64:
  3712. fec_bits_desired = 6 * symbol_rate;
  3713. break;
  3714. case DRX_CONSTELLATION_QAM128:
  3715. fec_bits_desired = 7 * symbol_rate;
  3716. break;
  3717. case DRX_CONSTELLATION_QAM256:
  3718. fec_bits_desired = 8 * symbol_rate;
  3719. break;
  3720. default:
  3721. status = -EINVAL;
  3722. }
  3723. if (status < 0)
  3724. goto error;
  3725. fec_bits_desired /= 1000; /* symbol_rate [Hz] -> symbol_rate [kHz] */
  3726. fec_bits_desired *= 500; /* meas. period [ms] */
  3727. /* Annex A/C: bits/RsPeriod = 204 * 8 = 1632 */
  3728. /* fec_rs_period_total = fec_bits_desired / 1632 */
  3729. fec_rs_period_total = (fec_bits_desired / 1632UL) + 1; /* roughly ceil */
  3730. /* fec_rs_period_total = fec_rs_prescale * fec_rs_period */
  3731. fec_rs_prescale = 1 + (u16) (fec_rs_period_total >> 16);
  3732. if (fec_rs_prescale == 0) {
  3733. /* Divide by zero (though impossible) */
  3734. status = -EINVAL;
  3735. if (status < 0)
  3736. goto error;
  3737. }
  3738. fec_rs_period =
  3739. ((u16) fec_rs_period_total +
  3740. (fec_rs_prescale >> 1)) / fec_rs_prescale;
  3741. /* write corresponding registers */
  3742. status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, fec_rs_period);
  3743. if (status < 0)
  3744. goto error;
  3745. status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A,
  3746. fec_rs_prescale);
  3747. if (status < 0)
  3748. goto error;
  3749. status = write16(state, FEC_OC_SNC_FAIL_PERIOD__A, fec_rs_period);
  3750. error:
  3751. if (status < 0)
  3752. pr_err("Error %d on %s\n", status, __func__);
  3753. return status;
  3754. }
  3755. static int set_qam16(struct drxk_state *state)
  3756. {
  3757. int status = 0;
  3758. dprintk(1, "\n");
  3759. /* QAM Equalizer Setup */
  3760. /* Equalizer */
  3761. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13517);
  3762. if (status < 0)
  3763. goto error;
  3764. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 13517);
  3765. if (status < 0)
  3766. goto error;
  3767. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 13517);
  3768. if (status < 0)
  3769. goto error;
  3770. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13517);
  3771. if (status < 0)
  3772. goto error;
  3773. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13517);
  3774. if (status < 0)
  3775. goto error;
  3776. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 13517);
  3777. if (status < 0)
  3778. goto error;
  3779. /* Decision Feedback Equalizer */
  3780. status = write16(state, QAM_DQ_QUAL_FUN0__A, 2);
  3781. if (status < 0)
  3782. goto error;
  3783. status = write16(state, QAM_DQ_QUAL_FUN1__A, 2);
  3784. if (status < 0)
  3785. goto error;
  3786. status = write16(state, QAM_DQ_QUAL_FUN2__A, 2);
  3787. if (status < 0)
  3788. goto error;
  3789. status = write16(state, QAM_DQ_QUAL_FUN3__A, 2);
  3790. if (status < 0)
  3791. goto error;
  3792. status = write16(state, QAM_DQ_QUAL_FUN4__A, 2);
  3793. if (status < 0)
  3794. goto error;
  3795. status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
  3796. if (status < 0)
  3797. goto error;
  3798. status = write16(state, QAM_SY_SYNC_HWM__A, 5);
  3799. if (status < 0)
  3800. goto error;
  3801. status = write16(state, QAM_SY_SYNC_AWM__A, 4);
  3802. if (status < 0)
  3803. goto error;
  3804. status = write16(state, QAM_SY_SYNC_LWM__A, 3);
  3805. if (status < 0)
  3806. goto error;
  3807. /* QAM Slicer Settings */
  3808. status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
  3809. DRXK_QAM_SL_SIG_POWER_QAM16);
  3810. if (status < 0)
  3811. goto error;
  3812. /* QAM Loop Controller Coeficients */
  3813. status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
  3814. if (status < 0)
  3815. goto error;
  3816. status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
  3817. if (status < 0)
  3818. goto error;
  3819. status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
  3820. if (status < 0)
  3821. goto error;
  3822. status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
  3823. if (status < 0)
  3824. goto error;
  3825. status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
  3826. if (status < 0)
  3827. goto error;
  3828. status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
  3829. if (status < 0)
  3830. goto error;
  3831. status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
  3832. if (status < 0)
  3833. goto error;
  3834. status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
  3835. if (status < 0)
  3836. goto error;
  3837. status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
  3838. if (status < 0)
  3839. goto error;
  3840. status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
  3841. if (status < 0)
  3842. goto error;
  3843. status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
  3844. if (status < 0)
  3845. goto error;
  3846. status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
  3847. if (status < 0)
  3848. goto error;
  3849. status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
  3850. if (status < 0)
  3851. goto error;
  3852. status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
  3853. if (status < 0)
  3854. goto error;
  3855. status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
  3856. if (status < 0)
  3857. goto error;
  3858. status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
  3859. if (status < 0)
  3860. goto error;
  3861. status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 32);
  3862. if (status < 0)
  3863. goto error;
  3864. status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
  3865. if (status < 0)
  3866. goto error;
  3867. status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
  3868. if (status < 0)
  3869. goto error;
  3870. status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
  3871. if (status < 0)
  3872. goto error;
  3873. /* QAM State Machine (FSM) Thresholds */
  3874. status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 140);
  3875. if (status < 0)
  3876. goto error;
  3877. status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
  3878. if (status < 0)
  3879. goto error;
  3880. status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 95);
  3881. if (status < 0)
  3882. goto error;
  3883. status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 120);
  3884. if (status < 0)
  3885. goto error;
  3886. status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 230);
  3887. if (status < 0)
  3888. goto error;
  3889. status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 105);
  3890. if (status < 0)
  3891. goto error;
  3892. status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
  3893. if (status < 0)
  3894. goto error;
  3895. status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
  3896. if (status < 0)
  3897. goto error;
  3898. status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 24);
  3899. if (status < 0)
  3900. goto error;
  3901. /* QAM FSM Tracking Parameters */
  3902. status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 16);
  3903. if (status < 0)
  3904. goto error;
  3905. status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 220);
  3906. if (status < 0)
  3907. goto error;
  3908. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 25);
  3909. if (status < 0)
  3910. goto error;
  3911. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 6);
  3912. if (status < 0)
  3913. goto error;
  3914. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -24);
  3915. if (status < 0)
  3916. goto error;
  3917. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -65);
  3918. if (status < 0)
  3919. goto error;
  3920. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -127);
  3921. if (status < 0)
  3922. goto error;
  3923. error:
  3924. if (status < 0)
  3925. pr_err("Error %d on %s\n", status, __func__);
  3926. return status;
  3927. }
  3928. /*============================================================================*/
  3929. /*
  3930. * \brief QAM32 specific setup
  3931. * \param demod instance of demod.
  3932. * \return DRXStatus_t.
  3933. */
  3934. static int set_qam32(struct drxk_state *state)
  3935. {
  3936. int status = 0;
  3937. dprintk(1, "\n");
  3938. /* QAM Equalizer Setup */
  3939. /* Equalizer */
  3940. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6707);
  3941. if (status < 0)
  3942. goto error;
  3943. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6707);
  3944. if (status < 0)
  3945. goto error;
  3946. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6707);
  3947. if (status < 0)
  3948. goto error;
  3949. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6707);
  3950. if (status < 0)
  3951. goto error;
  3952. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6707);
  3953. if (status < 0)
  3954. goto error;
  3955. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 6707);
  3956. if (status < 0)
  3957. goto error;
  3958. /* Decision Feedback Equalizer */
  3959. status = write16(state, QAM_DQ_QUAL_FUN0__A, 3);
  3960. if (status < 0)
  3961. goto error;
  3962. status = write16(state, QAM_DQ_QUAL_FUN1__A, 3);
  3963. if (status < 0)
  3964. goto error;
  3965. status = write16(state, QAM_DQ_QUAL_FUN2__A, 3);
  3966. if (status < 0)
  3967. goto error;
  3968. status = write16(state, QAM_DQ_QUAL_FUN3__A, 3);
  3969. if (status < 0)
  3970. goto error;
  3971. status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
  3972. if (status < 0)
  3973. goto error;
  3974. status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
  3975. if (status < 0)
  3976. goto error;
  3977. status = write16(state, QAM_SY_SYNC_HWM__A, 6);
  3978. if (status < 0)
  3979. goto error;
  3980. status = write16(state, QAM_SY_SYNC_AWM__A, 5);
  3981. if (status < 0)
  3982. goto error;
  3983. status = write16(state, QAM_SY_SYNC_LWM__A, 3);
  3984. if (status < 0)
  3985. goto error;
  3986. /* QAM Slicer Settings */
  3987. status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
  3988. DRXK_QAM_SL_SIG_POWER_QAM32);
  3989. if (status < 0)
  3990. goto error;
  3991. /* QAM Loop Controller Coeficients */
  3992. status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
  3993. if (status < 0)
  3994. goto error;
  3995. status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
  3996. if (status < 0)
  3997. goto error;
  3998. status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
  3999. if (status < 0)
  4000. goto error;
  4001. status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
  4002. if (status < 0)
  4003. goto error;
  4004. status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
  4005. if (status < 0)
  4006. goto error;
  4007. status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
  4008. if (status < 0)
  4009. goto error;
  4010. status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
  4011. if (status < 0)
  4012. goto error;
  4013. status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
  4014. if (status < 0)
  4015. goto error;
  4016. status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
  4017. if (status < 0)
  4018. goto error;
  4019. status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
  4020. if (status < 0)
  4021. goto error;
  4022. status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
  4023. if (status < 0)
  4024. goto error;
  4025. status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
  4026. if (status < 0)
  4027. goto error;
  4028. status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
  4029. if (status < 0)
  4030. goto error;
  4031. status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
  4032. if (status < 0)
  4033. goto error;
  4034. status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
  4035. if (status < 0)
  4036. goto error;
  4037. status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
  4038. if (status < 0)
  4039. goto error;
  4040. status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 16);
  4041. if (status < 0)
  4042. goto error;
  4043. status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
  4044. if (status < 0)
  4045. goto error;
  4046. status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
  4047. if (status < 0)
  4048. goto error;
  4049. status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
  4050. if (status < 0)
  4051. goto error;
  4052. /* QAM State Machine (FSM) Thresholds */
  4053. status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 90);
  4054. if (status < 0)
  4055. goto error;
  4056. status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
  4057. if (status < 0)
  4058. goto error;
  4059. status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
  4060. if (status < 0)
  4061. goto error;
  4062. status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
  4063. if (status < 0)
  4064. goto error;
  4065. status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 170);
  4066. if (status < 0)
  4067. goto error;
  4068. status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
  4069. if (status < 0)
  4070. goto error;
  4071. status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
  4072. if (status < 0)
  4073. goto error;
  4074. status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
  4075. if (status < 0)
  4076. goto error;
  4077. status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 10);
  4078. if (status < 0)
  4079. goto error;
  4080. /* QAM FSM Tracking Parameters */
  4081. status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
  4082. if (status < 0)
  4083. goto error;
  4084. status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 140);
  4085. if (status < 0)
  4086. goto error;
  4087. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) -8);
  4088. if (status < 0)
  4089. goto error;
  4090. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) -16);
  4091. if (status < 0)
  4092. goto error;
  4093. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -26);
  4094. if (status < 0)
  4095. goto error;
  4096. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -56);
  4097. if (status < 0)
  4098. goto error;
  4099. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -86);
  4100. error:
  4101. if (status < 0)
  4102. pr_err("Error %d on %s\n", status, __func__);
  4103. return status;
  4104. }
  4105. /*============================================================================*/
  4106. /*
  4107. * \brief QAM64 specific setup
  4108. * \param demod instance of demod.
  4109. * \return DRXStatus_t.
  4110. */
  4111. static int set_qam64(struct drxk_state *state)
  4112. {
  4113. int status = 0;
  4114. dprintk(1, "\n");
  4115. /* QAM Equalizer Setup */
  4116. /* Equalizer */
  4117. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13336);
  4118. if (status < 0)
  4119. goto error;
  4120. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12618);
  4121. if (status < 0)
  4122. goto error;
  4123. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 11988);
  4124. if (status < 0)
  4125. goto error;
  4126. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13809);
  4127. if (status < 0)
  4128. goto error;
  4129. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13809);
  4130. if (status < 0)
  4131. goto error;
  4132. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15609);
  4133. if (status < 0)
  4134. goto error;
  4135. /* Decision Feedback Equalizer */
  4136. status = write16(state, QAM_DQ_QUAL_FUN0__A, 4);
  4137. if (status < 0)
  4138. goto error;
  4139. status = write16(state, QAM_DQ_QUAL_FUN1__A, 4);
  4140. if (status < 0)
  4141. goto error;
  4142. status = write16(state, QAM_DQ_QUAL_FUN2__A, 4);
  4143. if (status < 0)
  4144. goto error;
  4145. status = write16(state, QAM_DQ_QUAL_FUN3__A, 4);
  4146. if (status < 0)
  4147. goto error;
  4148. status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
  4149. if (status < 0)
  4150. goto error;
  4151. status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
  4152. if (status < 0)
  4153. goto error;
  4154. status = write16(state, QAM_SY_SYNC_HWM__A, 5);
  4155. if (status < 0)
  4156. goto error;
  4157. status = write16(state, QAM_SY_SYNC_AWM__A, 4);
  4158. if (status < 0)
  4159. goto error;
  4160. status = write16(state, QAM_SY_SYNC_LWM__A, 3);
  4161. if (status < 0)
  4162. goto error;
  4163. /* QAM Slicer Settings */
  4164. status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
  4165. DRXK_QAM_SL_SIG_POWER_QAM64);
  4166. if (status < 0)
  4167. goto error;
  4168. /* QAM Loop Controller Coeficients */
  4169. status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
  4170. if (status < 0)
  4171. goto error;
  4172. status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
  4173. if (status < 0)
  4174. goto error;
  4175. status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
  4176. if (status < 0)
  4177. goto error;
  4178. status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
  4179. if (status < 0)
  4180. goto error;
  4181. status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
  4182. if (status < 0)
  4183. goto error;
  4184. status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
  4185. if (status < 0)
  4186. goto error;
  4187. status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
  4188. if (status < 0)
  4189. goto error;
  4190. status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
  4191. if (status < 0)
  4192. goto error;
  4193. status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
  4194. if (status < 0)
  4195. goto error;
  4196. status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 30);
  4197. if (status < 0)
  4198. goto error;
  4199. status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 100);
  4200. if (status < 0)
  4201. goto error;
  4202. status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
  4203. if (status < 0)
  4204. goto error;
  4205. status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 30);
  4206. if (status < 0)
  4207. goto error;
  4208. status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
  4209. if (status < 0)
  4210. goto error;
  4211. status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
  4212. if (status < 0)
  4213. goto error;
  4214. status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
  4215. if (status < 0)
  4216. goto error;
  4217. status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
  4218. if (status < 0)
  4219. goto error;
  4220. status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
  4221. if (status < 0)
  4222. goto error;
  4223. status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
  4224. if (status < 0)
  4225. goto error;
  4226. status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
  4227. if (status < 0)
  4228. goto error;
  4229. /* QAM State Machine (FSM) Thresholds */
  4230. status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 100);
  4231. if (status < 0)
  4232. goto error;
  4233. status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
  4234. if (status < 0)
  4235. goto error;
  4236. status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
  4237. if (status < 0)
  4238. goto error;
  4239. status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 110);
  4240. if (status < 0)
  4241. goto error;
  4242. status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 200);
  4243. if (status < 0)
  4244. goto error;
  4245. status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 95);
  4246. if (status < 0)
  4247. goto error;
  4248. status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
  4249. if (status < 0)
  4250. goto error;
  4251. status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
  4252. if (status < 0)
  4253. goto error;
  4254. status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 15);
  4255. if (status < 0)
  4256. goto error;
  4257. /* QAM FSM Tracking Parameters */
  4258. status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
  4259. if (status < 0)
  4260. goto error;
  4261. status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 141);
  4262. if (status < 0)
  4263. goto error;
  4264. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 7);
  4265. if (status < 0)
  4266. goto error;
  4267. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 0);
  4268. if (status < 0)
  4269. goto error;
  4270. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -15);
  4271. if (status < 0)
  4272. goto error;
  4273. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -45);
  4274. if (status < 0)
  4275. goto error;
  4276. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -80);
  4277. error:
  4278. if (status < 0)
  4279. pr_err("Error %d on %s\n", status, __func__);
  4280. return status;
  4281. }
  4282. /*============================================================================*/
  4283. /*
  4284. * \brief QAM128 specific setup
  4285. * \param demod: instance of demod.
  4286. * \return DRXStatus_t.
  4287. */
  4288. static int set_qam128(struct drxk_state *state)
  4289. {
  4290. int status = 0;
  4291. dprintk(1, "\n");
  4292. /* QAM Equalizer Setup */
  4293. /* Equalizer */
  4294. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6564);
  4295. if (status < 0)
  4296. goto error;
  4297. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6598);
  4298. if (status < 0)
  4299. goto error;
  4300. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6394);
  4301. if (status < 0)
  4302. goto error;
  4303. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6409);
  4304. if (status < 0)
  4305. goto error;
  4306. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6656);
  4307. if (status < 0)
  4308. goto error;
  4309. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 7238);
  4310. if (status < 0)
  4311. goto error;
  4312. /* Decision Feedback Equalizer */
  4313. status = write16(state, QAM_DQ_QUAL_FUN0__A, 6);
  4314. if (status < 0)
  4315. goto error;
  4316. status = write16(state, QAM_DQ_QUAL_FUN1__A, 6);
  4317. if (status < 0)
  4318. goto error;
  4319. status = write16(state, QAM_DQ_QUAL_FUN2__A, 6);
  4320. if (status < 0)
  4321. goto error;
  4322. status = write16(state, QAM_DQ_QUAL_FUN3__A, 6);
  4323. if (status < 0)
  4324. goto error;
  4325. status = write16(state, QAM_DQ_QUAL_FUN4__A, 5);
  4326. if (status < 0)
  4327. goto error;
  4328. status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
  4329. if (status < 0)
  4330. goto error;
  4331. status = write16(state, QAM_SY_SYNC_HWM__A, 6);
  4332. if (status < 0)
  4333. goto error;
  4334. status = write16(state, QAM_SY_SYNC_AWM__A, 5);
  4335. if (status < 0)
  4336. goto error;
  4337. status = write16(state, QAM_SY_SYNC_LWM__A, 3);
  4338. if (status < 0)
  4339. goto error;
  4340. /* QAM Slicer Settings */
  4341. status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
  4342. DRXK_QAM_SL_SIG_POWER_QAM128);
  4343. if (status < 0)
  4344. goto error;
  4345. /* QAM Loop Controller Coeficients */
  4346. status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
  4347. if (status < 0)
  4348. goto error;
  4349. status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
  4350. if (status < 0)
  4351. goto error;
  4352. status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
  4353. if (status < 0)
  4354. goto error;
  4355. status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
  4356. if (status < 0)
  4357. goto error;
  4358. status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
  4359. if (status < 0)
  4360. goto error;
  4361. status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
  4362. if (status < 0)
  4363. goto error;
  4364. status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
  4365. if (status < 0)
  4366. goto error;
  4367. status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
  4368. if (status < 0)
  4369. goto error;
  4370. status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
  4371. if (status < 0)
  4372. goto error;
  4373. status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 40);
  4374. if (status < 0)
  4375. goto error;
  4376. status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 120);
  4377. if (status < 0)
  4378. goto error;
  4379. status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
  4380. if (status < 0)
  4381. goto error;
  4382. status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 40);
  4383. if (status < 0)
  4384. goto error;
  4385. status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 60);
  4386. if (status < 0)
  4387. goto error;
  4388. status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
  4389. if (status < 0)
  4390. goto error;
  4391. status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
  4392. if (status < 0)
  4393. goto error;
  4394. status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 64);
  4395. if (status < 0)
  4396. goto error;
  4397. status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
  4398. if (status < 0)
  4399. goto error;
  4400. status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
  4401. if (status < 0)
  4402. goto error;
  4403. status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
  4404. if (status < 0)
  4405. goto error;
  4406. /* QAM State Machine (FSM) Thresholds */
  4407. status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
  4408. if (status < 0)
  4409. goto error;
  4410. status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
  4411. if (status < 0)
  4412. goto error;
  4413. status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
  4414. if (status < 0)
  4415. goto error;
  4416. status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
  4417. if (status < 0)
  4418. goto error;
  4419. status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 140);
  4420. if (status < 0)
  4421. goto error;
  4422. status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
  4423. if (status < 0)
  4424. goto error;
  4425. status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
  4426. if (status < 0)
  4427. goto error;
  4428. status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 5);
  4429. if (status < 0)
  4430. goto error;
  4431. status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
  4432. if (status < 0)
  4433. goto error;
  4434. /* QAM FSM Tracking Parameters */
  4435. status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
  4436. if (status < 0)
  4437. goto error;
  4438. status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 65);
  4439. if (status < 0)
  4440. goto error;
  4441. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 5);
  4442. if (status < 0)
  4443. goto error;
  4444. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 3);
  4445. if (status < 0)
  4446. goto error;
  4447. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -1);
  4448. if (status < 0)
  4449. goto error;
  4450. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -12);
  4451. if (status < 0)
  4452. goto error;
  4453. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -23);
  4454. error:
  4455. if (status < 0)
  4456. pr_err("Error %d on %s\n", status, __func__);
  4457. return status;
  4458. }
  4459. /*============================================================================*/
  4460. /*
  4461. * \brief QAM256 specific setup
  4462. * \param demod: instance of demod.
  4463. * \return DRXStatus_t.
  4464. */
  4465. static int set_qam256(struct drxk_state *state)
  4466. {
  4467. int status = 0;
  4468. dprintk(1, "\n");
  4469. /* QAM Equalizer Setup */
  4470. /* Equalizer */
  4471. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 11502);
  4472. if (status < 0)
  4473. goto error;
  4474. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12084);
  4475. if (status < 0)
  4476. goto error;
  4477. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 12543);
  4478. if (status < 0)
  4479. goto error;
  4480. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 12931);
  4481. if (status < 0)
  4482. goto error;
  4483. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13629);
  4484. if (status < 0)
  4485. goto error;
  4486. status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15385);
  4487. if (status < 0)
  4488. goto error;
  4489. /* Decision Feedback Equalizer */
  4490. status = write16(state, QAM_DQ_QUAL_FUN0__A, 8);
  4491. if (status < 0)
  4492. goto error;
  4493. status = write16(state, QAM_DQ_QUAL_FUN1__A, 8);
  4494. if (status < 0)
  4495. goto error;
  4496. status = write16(state, QAM_DQ_QUAL_FUN2__A, 8);
  4497. if (status < 0)
  4498. goto error;
  4499. status = write16(state, QAM_DQ_QUAL_FUN3__A, 8);
  4500. if (status < 0)
  4501. goto error;
  4502. status = write16(state, QAM_DQ_QUAL_FUN4__A, 6);
  4503. if (status < 0)
  4504. goto error;
  4505. status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
  4506. if (status < 0)
  4507. goto error;
  4508. status = write16(state, QAM_SY_SYNC_HWM__A, 5);
  4509. if (status < 0)
  4510. goto error;
  4511. status = write16(state, QAM_SY_SYNC_AWM__A, 4);
  4512. if (status < 0)
  4513. goto error;
  4514. status = write16(state, QAM_SY_SYNC_LWM__A, 3);
  4515. if (status < 0)
  4516. goto error;
  4517. /* QAM Slicer Settings */
  4518. status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
  4519. DRXK_QAM_SL_SIG_POWER_QAM256);
  4520. if (status < 0)
  4521. goto error;
  4522. /* QAM Loop Controller Coeficients */
  4523. status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
  4524. if (status < 0)
  4525. goto error;
  4526. status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
  4527. if (status < 0)
  4528. goto error;
  4529. status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
  4530. if (status < 0)
  4531. goto error;
  4532. status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
  4533. if (status < 0)
  4534. goto error;
  4535. status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
  4536. if (status < 0)
  4537. goto error;
  4538. status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
  4539. if (status < 0)
  4540. goto error;
  4541. status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
  4542. if (status < 0)
  4543. goto error;
  4544. status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
  4545. if (status < 0)
  4546. goto error;
  4547. status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
  4548. if (status < 0)
  4549. goto error;
  4550. status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 50);
  4551. if (status < 0)
  4552. goto error;
  4553. status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 250);
  4554. if (status < 0)
  4555. goto error;
  4556. status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
  4557. if (status < 0)
  4558. goto error;
  4559. status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 50);
  4560. if (status < 0)
  4561. goto error;
  4562. status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 125);
  4563. if (status < 0)
  4564. goto error;
  4565. status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
  4566. if (status < 0)
  4567. goto error;
  4568. status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
  4569. if (status < 0)
  4570. goto error;
  4571. status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
  4572. if (status < 0)
  4573. goto error;
  4574. status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
  4575. if (status < 0)
  4576. goto error;
  4577. status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
  4578. if (status < 0)
  4579. goto error;
  4580. status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
  4581. if (status < 0)
  4582. goto error;
  4583. /* QAM State Machine (FSM) Thresholds */
  4584. status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
  4585. if (status < 0)
  4586. goto error;
  4587. status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
  4588. if (status < 0)
  4589. goto error;
  4590. status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
  4591. if (status < 0)
  4592. goto error;
  4593. status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
  4594. if (status < 0)
  4595. goto error;
  4596. status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 150);
  4597. if (status < 0)
  4598. goto error;
  4599. status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 110);
  4600. if (status < 0)
  4601. goto error;
  4602. status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
  4603. if (status < 0)
  4604. goto error;
  4605. status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
  4606. if (status < 0)
  4607. goto error;
  4608. status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
  4609. if (status < 0)
  4610. goto error;
  4611. /* QAM FSM Tracking Parameters */
  4612. status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
  4613. if (status < 0)
  4614. goto error;
  4615. status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 74);
  4616. if (status < 0)
  4617. goto error;
  4618. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 18);
  4619. if (status < 0)
  4620. goto error;
  4621. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 13);
  4622. if (status < 0)
  4623. goto error;
  4624. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) 7);
  4625. if (status < 0)
  4626. goto error;
  4627. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) 0);
  4628. if (status < 0)
  4629. goto error;
  4630. status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -8);
  4631. error:
  4632. if (status < 0)
  4633. pr_err("Error %d on %s\n", status, __func__);
  4634. return status;
  4635. }
  4636. /*============================================================================*/
  4637. /*
  4638. * \brief Reset QAM block.
  4639. * \param demod: instance of demod.
  4640. * \param channel: pointer to channel data.
  4641. * \return DRXStatus_t.
  4642. */
  4643. static int qam_reset_qam(struct drxk_state *state)
  4644. {
  4645. int status;
  4646. u16 cmd_result;
  4647. dprintk(1, "\n");
  4648. /* Stop QAM comstate->m_exec */
  4649. status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
  4650. if (status < 0)
  4651. goto error;
  4652. status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
  4653. | SCU_RAM_COMMAND_CMD_DEMOD_RESET,
  4654. 0, NULL, 1, &cmd_result);
  4655. error:
  4656. if (status < 0)
  4657. pr_err("Error %d on %s\n", status, __func__);
  4658. return status;
  4659. }
  4660. /*============================================================================*/
  4661. /*
  4662. * \brief Set QAM symbolrate.
  4663. * \param demod: instance of demod.
  4664. * \param channel: pointer to channel data.
  4665. * \return DRXStatus_t.
  4666. */
  4667. static int qam_set_symbolrate(struct drxk_state *state)
  4668. {
  4669. u32 adc_frequency = 0;
  4670. u32 symb_freq = 0;
  4671. u32 iqm_rc_rate = 0;
  4672. u16 ratesel = 0;
  4673. u32 lc_symb_rate = 0;
  4674. int status;
  4675. dprintk(1, "\n");
  4676. /* Select & calculate correct IQM rate */
  4677. adc_frequency = (state->m_sys_clock_freq * 1000) / 3;
  4678. ratesel = 0;
  4679. if (state->props.symbol_rate <= 1188750)
  4680. ratesel = 3;
  4681. else if (state->props.symbol_rate <= 2377500)
  4682. ratesel = 2;
  4683. else if (state->props.symbol_rate <= 4755000)
  4684. ratesel = 1;
  4685. status = write16(state, IQM_FD_RATESEL__A, ratesel);
  4686. if (status < 0)
  4687. goto error;
  4688. /*
  4689. IqmRcRate = ((Fadc / (symbolrate * (4<<ratesel))) - 1) * (1<<23)
  4690. */
  4691. symb_freq = state->props.symbol_rate * (1 << ratesel);
  4692. if (symb_freq == 0) {
  4693. /* Divide by zero */
  4694. status = -EINVAL;
  4695. goto error;
  4696. }
  4697. iqm_rc_rate = (adc_frequency / symb_freq) * (1 << 21) +
  4698. (Frac28a((adc_frequency % symb_freq), symb_freq) >> 7) -
  4699. (1 << 23);
  4700. status = write32(state, IQM_RC_RATE_OFS_LO__A, iqm_rc_rate);
  4701. if (status < 0)
  4702. goto error;
  4703. state->m_iqm_rc_rate = iqm_rc_rate;
  4704. /*
  4705. LcSymbFreq = round (.125 * symbolrate / adc_freq * (1<<15))
  4706. */
  4707. symb_freq = state->props.symbol_rate;
  4708. if (adc_frequency == 0) {
  4709. /* Divide by zero */
  4710. status = -EINVAL;
  4711. goto error;
  4712. }
  4713. lc_symb_rate = (symb_freq / adc_frequency) * (1 << 12) +
  4714. (Frac28a((symb_freq % adc_frequency), adc_frequency) >>
  4715. 16);
  4716. if (lc_symb_rate > 511)
  4717. lc_symb_rate = 511;
  4718. status = write16(state, QAM_LC_SYMBOL_FREQ__A, (u16) lc_symb_rate);
  4719. error:
  4720. if (status < 0)
  4721. pr_err("Error %d on %s\n", status, __func__);
  4722. return status;
  4723. }
  4724. /*============================================================================*/
  4725. /*
  4726. * \brief Get QAM lock status.
  4727. * \param demod: instance of demod.
  4728. * \param channel: pointer to channel data.
  4729. * \return DRXStatus_t.
  4730. */
  4731. static int get_qam_lock_status(struct drxk_state *state, u32 *p_lock_status)
  4732. {
  4733. int status;
  4734. u16 result[2] = { 0, 0 };
  4735. dprintk(1, "\n");
  4736. *p_lock_status = NOT_LOCKED;
  4737. status = scu_command(state,
  4738. SCU_RAM_COMMAND_STANDARD_QAM |
  4739. SCU_RAM_COMMAND_CMD_DEMOD_GET_LOCK, 0, NULL, 2,
  4740. result);
  4741. if (status < 0)
  4742. pr_err("Error %d on %s\n", status, __func__);
  4743. if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_DEMOD_LOCKED) {
  4744. /* 0x0000 NOT LOCKED */
  4745. } else if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_LOCKED) {
  4746. /* 0x4000 DEMOD LOCKED */
  4747. *p_lock_status = DEMOD_LOCK;
  4748. } else if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_NEVER_LOCK) {
  4749. /* 0x8000 DEMOD + FEC LOCKED (system lock) */
  4750. *p_lock_status = MPEG_LOCK;
  4751. } else {
  4752. /* 0xC000 NEVER LOCKED */
  4753. /* (system will never be able to lock to the signal) */
  4754. /*
  4755. * TODO: check this, intermediate & standard specific lock
  4756. * states are not taken into account here
  4757. */
  4758. *p_lock_status = NEVER_LOCK;
  4759. }
  4760. return status;
  4761. }
  4762. #define QAM_MIRROR__M 0x03
  4763. #define QAM_MIRROR_NORMAL 0x00
  4764. #define QAM_MIRRORED 0x01
  4765. #define QAM_MIRROR_AUTO_ON 0x02
  4766. #define QAM_LOCKRANGE__M 0x10
  4767. #define QAM_LOCKRANGE_NORMAL 0x10
  4768. static int qam_demodulator_command(struct drxk_state *state,
  4769. int number_of_parameters)
  4770. {
  4771. int status;
  4772. u16 cmd_result;
  4773. u16 set_param_parameters[4] = { 0, 0, 0, 0 };
  4774. set_param_parameters[0] = state->m_constellation; /* modulation */
  4775. set_param_parameters[1] = DRXK_QAM_I12_J17; /* interleave mode */
  4776. if (number_of_parameters == 2) {
  4777. u16 set_env_parameters[1] = { 0 };
  4778. if (state->m_operation_mode == OM_QAM_ITU_C)
  4779. set_env_parameters[0] = QAM_TOP_ANNEX_C;
  4780. else
  4781. set_env_parameters[0] = QAM_TOP_ANNEX_A;
  4782. status = scu_command(state,
  4783. SCU_RAM_COMMAND_STANDARD_QAM
  4784. | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV,
  4785. 1, set_env_parameters, 1, &cmd_result);
  4786. if (status < 0)
  4787. goto error;
  4788. status = scu_command(state,
  4789. SCU_RAM_COMMAND_STANDARD_QAM
  4790. | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
  4791. number_of_parameters, set_param_parameters,
  4792. 1, &cmd_result);
  4793. } else if (number_of_parameters == 4) {
  4794. if (state->m_operation_mode == OM_QAM_ITU_C)
  4795. set_param_parameters[2] = QAM_TOP_ANNEX_C;
  4796. else
  4797. set_param_parameters[2] = QAM_TOP_ANNEX_A;
  4798. set_param_parameters[3] |= (QAM_MIRROR_AUTO_ON);
  4799. /* Env parameters */
  4800. /* check for LOCKRANGE Extended */
  4801. /* set_param_parameters[3] |= QAM_LOCKRANGE_NORMAL; */
  4802. status = scu_command(state,
  4803. SCU_RAM_COMMAND_STANDARD_QAM
  4804. | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
  4805. number_of_parameters, set_param_parameters,
  4806. 1, &cmd_result);
  4807. } else {
  4808. pr_warn("Unknown QAM demodulator parameter count %d\n",
  4809. number_of_parameters);
  4810. status = -EINVAL;
  4811. }
  4812. error:
  4813. if (status < 0)
  4814. pr_warn("Warning %d on %s\n", status, __func__);
  4815. return status;
  4816. }
  4817. static int set_qam(struct drxk_state *state, u16 intermediate_freqk_hz,
  4818. s32 tuner_freq_offset)
  4819. {
  4820. int status;
  4821. u16 cmd_result;
  4822. int qam_demod_param_count = state->qam_demod_parameter_count;
  4823. dprintk(1, "\n");
  4824. /*
  4825. * STEP 1: reset demodulator
  4826. * resets FEC DI and FEC RS
  4827. * resets QAM block
  4828. * resets SCU variables
  4829. */
  4830. status = write16(state, FEC_DI_COMM_EXEC__A, FEC_DI_COMM_EXEC_STOP);
  4831. if (status < 0)
  4832. goto error;
  4833. status = write16(state, FEC_RS_COMM_EXEC__A, FEC_RS_COMM_EXEC_STOP);
  4834. if (status < 0)
  4835. goto error;
  4836. status = qam_reset_qam(state);
  4837. if (status < 0)
  4838. goto error;
  4839. /*
  4840. * STEP 2: configure demodulator
  4841. * -set params; resets IQM,QAM,FEC HW; initializes some
  4842. * SCU variables
  4843. */
  4844. status = qam_set_symbolrate(state);
  4845. if (status < 0)
  4846. goto error;
  4847. /* Set params */
  4848. switch (state->props.modulation) {
  4849. case QAM_256:
  4850. state->m_constellation = DRX_CONSTELLATION_QAM256;
  4851. break;
  4852. case QAM_AUTO:
  4853. case QAM_64:
  4854. state->m_constellation = DRX_CONSTELLATION_QAM64;
  4855. break;
  4856. case QAM_16:
  4857. state->m_constellation = DRX_CONSTELLATION_QAM16;
  4858. break;
  4859. case QAM_32:
  4860. state->m_constellation = DRX_CONSTELLATION_QAM32;
  4861. break;
  4862. case QAM_128:
  4863. state->m_constellation = DRX_CONSTELLATION_QAM128;
  4864. break;
  4865. default:
  4866. status = -EINVAL;
  4867. break;
  4868. }
  4869. if (status < 0)
  4870. goto error;
  4871. /* Use the 4-parameter if it's requested or we're probing for
  4872. * the correct command. */
  4873. if (state->qam_demod_parameter_count == 4
  4874. || !state->qam_demod_parameter_count) {
  4875. qam_demod_param_count = 4;
  4876. status = qam_demodulator_command(state, qam_demod_param_count);
  4877. }
  4878. /* Use the 2-parameter command if it was requested or if we're
  4879. * probing for the correct command and the 4-parameter command
  4880. * failed. */
  4881. if (state->qam_demod_parameter_count == 2
  4882. || (!state->qam_demod_parameter_count && status < 0)) {
  4883. qam_demod_param_count = 2;
  4884. status = qam_demodulator_command(state, qam_demod_param_count);
  4885. }
  4886. if (status < 0) {
  4887. dprintk(1, "Could not set demodulator parameters.\n");
  4888. dprintk(1,
  4889. "Make sure qam_demod_parameter_count (%d) is correct for your firmware (%s).\n",
  4890. state->qam_demod_parameter_count,
  4891. state->microcode_name);
  4892. goto error;
  4893. } else if (!state->qam_demod_parameter_count) {
  4894. dprintk(1,
  4895. "Auto-probing the QAM command parameters was successful - using %d parameters.\n",
  4896. qam_demod_param_count);
  4897. /*
  4898. * One of our commands was successful. We don't need to
  4899. * auto-probe anymore, now that we got the correct command.
  4900. */
  4901. state->qam_demod_parameter_count = qam_demod_param_count;
  4902. }
  4903. /*
  4904. * STEP 3: enable the system in a mode where the ADC provides valid
  4905. * signal setup modulation independent registers
  4906. */
  4907. #if 0
  4908. status = set_frequency(channel, tuner_freq_offset));
  4909. if (status < 0)
  4910. goto error;
  4911. #endif
  4912. status = set_frequency_shifter(state, intermediate_freqk_hz,
  4913. tuner_freq_offset, true);
  4914. if (status < 0)
  4915. goto error;
  4916. /* Setup BER measurement */
  4917. status = set_qam_measurement(state, state->m_constellation,
  4918. state->props.symbol_rate);
  4919. if (status < 0)
  4920. goto error;
  4921. /* Reset default values */
  4922. status = write16(state, IQM_CF_SCALE_SH__A, IQM_CF_SCALE_SH__PRE);
  4923. if (status < 0)
  4924. goto error;
  4925. status = write16(state, QAM_SY_TIMEOUT__A, QAM_SY_TIMEOUT__PRE);
  4926. if (status < 0)
  4927. goto error;
  4928. /* Reset default LC values */
  4929. status = write16(state, QAM_LC_RATE_LIMIT__A, 3);
  4930. if (status < 0)
  4931. goto error;
  4932. status = write16(state, QAM_LC_LPF_FACTORP__A, 4);
  4933. if (status < 0)
  4934. goto error;
  4935. status = write16(state, QAM_LC_LPF_FACTORI__A, 4);
  4936. if (status < 0)
  4937. goto error;
  4938. status = write16(state, QAM_LC_MODE__A, 7);
  4939. if (status < 0)
  4940. goto error;
  4941. status = write16(state, QAM_LC_QUAL_TAB0__A, 1);
  4942. if (status < 0)
  4943. goto error;
  4944. status = write16(state, QAM_LC_QUAL_TAB1__A, 1);
  4945. if (status < 0)
  4946. goto error;
  4947. status = write16(state, QAM_LC_QUAL_TAB2__A, 1);
  4948. if (status < 0)
  4949. goto error;
  4950. status = write16(state, QAM_LC_QUAL_TAB3__A, 1);
  4951. if (status < 0)
  4952. goto error;
  4953. status = write16(state, QAM_LC_QUAL_TAB4__A, 2);
  4954. if (status < 0)
  4955. goto error;
  4956. status = write16(state, QAM_LC_QUAL_TAB5__A, 2);
  4957. if (status < 0)
  4958. goto error;
  4959. status = write16(state, QAM_LC_QUAL_TAB6__A, 2);
  4960. if (status < 0)
  4961. goto error;
  4962. status = write16(state, QAM_LC_QUAL_TAB8__A, 2);
  4963. if (status < 0)
  4964. goto error;
  4965. status = write16(state, QAM_LC_QUAL_TAB9__A, 2);
  4966. if (status < 0)
  4967. goto error;
  4968. status = write16(state, QAM_LC_QUAL_TAB10__A, 2);
  4969. if (status < 0)
  4970. goto error;
  4971. status = write16(state, QAM_LC_QUAL_TAB12__A, 2);
  4972. if (status < 0)
  4973. goto error;
  4974. status = write16(state, QAM_LC_QUAL_TAB15__A, 3);
  4975. if (status < 0)
  4976. goto error;
  4977. status = write16(state, QAM_LC_QUAL_TAB16__A, 3);
  4978. if (status < 0)
  4979. goto error;
  4980. status = write16(state, QAM_LC_QUAL_TAB20__A, 4);
  4981. if (status < 0)
  4982. goto error;
  4983. status = write16(state, QAM_LC_QUAL_TAB25__A, 4);
  4984. if (status < 0)
  4985. goto error;
  4986. /* Mirroring, QAM-block starting point not inverted */
  4987. status = write16(state, QAM_SY_SP_INV__A,
  4988. QAM_SY_SP_INV_SPECTRUM_INV_DIS);
  4989. if (status < 0)
  4990. goto error;
  4991. /* Halt SCU to enable safe non-atomic accesses */
  4992. status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
  4993. if (status < 0)
  4994. goto error;
  4995. /* STEP 4: modulation specific setup */
  4996. switch (state->props.modulation) {
  4997. case QAM_16:
  4998. status = set_qam16(state);
  4999. break;
  5000. case QAM_32:
  5001. status = set_qam32(state);
  5002. break;
  5003. case QAM_AUTO:
  5004. case QAM_64:
  5005. status = set_qam64(state);
  5006. break;
  5007. case QAM_128:
  5008. status = set_qam128(state);
  5009. break;
  5010. case QAM_256:
  5011. status = set_qam256(state);
  5012. break;
  5013. default:
  5014. status = -EINVAL;
  5015. break;
  5016. }
  5017. if (status < 0)
  5018. goto error;
  5019. /* Activate SCU to enable SCU commands */
  5020. status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
  5021. if (status < 0)
  5022. goto error;
  5023. /* Re-configure MPEG output, requires knowledge of channel bitrate */
  5024. /* extAttr->currentChannel.modulation = channel->modulation; */
  5025. /* extAttr->currentChannel.symbolrate = channel->symbolrate; */
  5026. status = mpegts_dto_setup(state, state->m_operation_mode);
  5027. if (status < 0)
  5028. goto error;
  5029. /* start processes */
  5030. status = mpegts_start(state);
  5031. if (status < 0)
  5032. goto error;
  5033. status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
  5034. if (status < 0)
  5035. goto error;
  5036. status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_ACTIVE);
  5037. if (status < 0)
  5038. goto error;
  5039. status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
  5040. if (status < 0)
  5041. goto error;
  5042. /* STEP 5: start QAM demodulator (starts FEC, QAM and IQM HW) */
  5043. status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
  5044. | SCU_RAM_COMMAND_CMD_DEMOD_START,
  5045. 0, NULL, 1, &cmd_result);
  5046. if (status < 0)
  5047. goto error;
  5048. /* update global DRXK data container */
  5049. /*? extAttr->qamInterleaveMode = DRXK_QAM_I12_J17; */
  5050. error:
  5051. if (status < 0)
  5052. pr_err("Error %d on %s\n", status, __func__);
  5053. return status;
  5054. }
  5055. static int set_qam_standard(struct drxk_state *state,
  5056. enum operation_mode o_mode)
  5057. {
  5058. int status;
  5059. #ifdef DRXK_QAM_TAPS
  5060. #define DRXK_QAMA_TAPS_SELECT
  5061. #include "drxk_filters.h"
  5062. #undef DRXK_QAMA_TAPS_SELECT
  5063. #endif
  5064. dprintk(1, "\n");
  5065. /* added antenna switch */
  5066. switch_antenna_to_qam(state);
  5067. /* Ensure correct power-up mode */
  5068. status = power_up_qam(state);
  5069. if (status < 0)
  5070. goto error;
  5071. /* Reset QAM block */
  5072. status = qam_reset_qam(state);
  5073. if (status < 0)
  5074. goto error;
  5075. /* Setup IQM */
  5076. status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
  5077. if (status < 0)
  5078. goto error;
  5079. status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
  5080. if (status < 0)
  5081. goto error;
  5082. /* Upload IQM Channel Filter settings by
  5083. boot loader from ROM table */
  5084. switch (o_mode) {
  5085. case OM_QAM_ITU_A:
  5086. status = bl_chain_cmd(state, DRXK_BL_ROM_OFFSET_TAPS_ITU_A,
  5087. DRXK_BLCC_NR_ELEMENTS_TAPS,
  5088. DRXK_BLC_TIMEOUT);
  5089. break;
  5090. case OM_QAM_ITU_C:
  5091. status = bl_direct_cmd(state, IQM_CF_TAP_RE0__A,
  5092. DRXK_BL_ROM_OFFSET_TAPS_ITU_C,
  5093. DRXK_BLDC_NR_ELEMENTS_TAPS,
  5094. DRXK_BLC_TIMEOUT);
  5095. if (status < 0)
  5096. goto error;
  5097. status = bl_direct_cmd(state,
  5098. IQM_CF_TAP_IM0__A,
  5099. DRXK_BL_ROM_OFFSET_TAPS_ITU_C,
  5100. DRXK_BLDC_NR_ELEMENTS_TAPS,
  5101. DRXK_BLC_TIMEOUT);
  5102. break;
  5103. default:
  5104. status = -EINVAL;
  5105. }
  5106. if (status < 0)
  5107. goto error;
  5108. status = write16(state, IQM_CF_OUT_ENA__A, 1 << IQM_CF_OUT_ENA_QAM__B);
  5109. if (status < 0)
  5110. goto error;
  5111. status = write16(state, IQM_CF_SYMMETRIC__A, 0);
  5112. if (status < 0)
  5113. goto error;
  5114. status = write16(state, IQM_CF_MIDTAP__A,
  5115. ((1 << IQM_CF_MIDTAP_RE__B) | (1 << IQM_CF_MIDTAP_IM__B)));
  5116. if (status < 0)
  5117. goto error;
  5118. status = write16(state, IQM_RC_STRETCH__A, 21);
  5119. if (status < 0)
  5120. goto error;
  5121. status = write16(state, IQM_AF_CLP_LEN__A, 0);
  5122. if (status < 0)
  5123. goto error;
  5124. status = write16(state, IQM_AF_CLP_TH__A, 448);
  5125. if (status < 0)
  5126. goto error;
  5127. status = write16(state, IQM_AF_SNS_LEN__A, 0);
  5128. if (status < 0)
  5129. goto error;
  5130. status = write16(state, IQM_CF_POW_MEAS_LEN__A, 0);
  5131. if (status < 0)
  5132. goto error;
  5133. status = write16(state, IQM_FS_ADJ_SEL__A, 1);
  5134. if (status < 0)
  5135. goto error;
  5136. status = write16(state, IQM_RC_ADJ_SEL__A, 1);
  5137. if (status < 0)
  5138. goto error;
  5139. status = write16(state, IQM_CF_ADJ_SEL__A, 1);
  5140. if (status < 0)
  5141. goto error;
  5142. status = write16(state, IQM_AF_UPD_SEL__A, 0);
  5143. if (status < 0)
  5144. goto error;
  5145. /* IQM Impulse Noise Processing Unit */
  5146. status = write16(state, IQM_CF_CLP_VAL__A, 500);
  5147. if (status < 0)
  5148. goto error;
  5149. status = write16(state, IQM_CF_DATATH__A, 1000);
  5150. if (status < 0)
  5151. goto error;
  5152. status = write16(state, IQM_CF_BYPASSDET__A, 1);
  5153. if (status < 0)
  5154. goto error;
  5155. status = write16(state, IQM_CF_DET_LCT__A, 0);
  5156. if (status < 0)
  5157. goto error;
  5158. status = write16(state, IQM_CF_WND_LEN__A, 1);
  5159. if (status < 0)
  5160. goto error;
  5161. status = write16(state, IQM_CF_PKDTH__A, 1);
  5162. if (status < 0)
  5163. goto error;
  5164. status = write16(state, IQM_AF_INC_BYPASS__A, 1);
  5165. if (status < 0)
  5166. goto error;
  5167. /* turn on IQMAF. Must be done before setAgc**() */
  5168. status = set_iqm_af(state, true);
  5169. if (status < 0)
  5170. goto error;
  5171. status = write16(state, IQM_AF_START_LOCK__A, 0x01);
  5172. if (status < 0)
  5173. goto error;
  5174. /* IQM will not be reset from here, sync ADC and update/init AGC */
  5175. status = adc_synchronization(state);
  5176. if (status < 0)
  5177. goto error;
  5178. /* Set the FSM step period */
  5179. status = write16(state, SCU_RAM_QAM_FSM_STEP_PERIOD__A, 2000);
  5180. if (status < 0)
  5181. goto error;
  5182. /* Halt SCU to enable safe non-atomic accesses */
  5183. status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
  5184. if (status < 0)
  5185. goto error;
  5186. /* No more resets of the IQM, current standard correctly set =>
  5187. now AGCs can be configured. */
  5188. status = init_agc(state, true);
  5189. if (status < 0)
  5190. goto error;
  5191. status = set_pre_saw(state, &(state->m_qam_pre_saw_cfg));
  5192. if (status < 0)
  5193. goto error;
  5194. /* Configure AGC's */
  5195. status = set_agc_rf(state, &(state->m_qam_rf_agc_cfg), true);
  5196. if (status < 0)
  5197. goto error;
  5198. status = set_agc_if(state, &(state->m_qam_if_agc_cfg), true);
  5199. if (status < 0)
  5200. goto error;
  5201. /* Activate SCU to enable SCU commands */
  5202. status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
  5203. error:
  5204. if (status < 0)
  5205. pr_err("Error %d on %s\n", status, __func__);
  5206. return status;
  5207. }
  5208. static int write_gpio(struct drxk_state *state)
  5209. {
  5210. int status;
  5211. u16 value = 0;
  5212. dprintk(1, "\n");
  5213. /* stop lock indicator process */
  5214. status = write16(state, SCU_RAM_GPIO__A,
  5215. SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
  5216. if (status < 0)
  5217. goto error;
  5218. /* Write magic word to enable pdr reg write */
  5219. status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
  5220. if (status < 0)
  5221. goto error;
  5222. if (state->m_has_sawsw) {
  5223. if (state->uio_mask & 0x0001) { /* UIO-1 */
  5224. /* write to io pad configuration register - output mode */
  5225. status = write16(state, SIO_PDR_SMA_TX_CFG__A,
  5226. state->m_gpio_cfg);
  5227. if (status < 0)
  5228. goto error;
  5229. /* use corresponding bit in io data output registar */
  5230. status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
  5231. if (status < 0)
  5232. goto error;
  5233. if ((state->m_gpio & 0x0001) == 0)
  5234. value &= 0x7FFF; /* write zero to 15th bit - 1st UIO */
  5235. else
  5236. value |= 0x8000; /* write one to 15th bit - 1st UIO */
  5237. /* write back to io data output register */
  5238. status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
  5239. if (status < 0)
  5240. goto error;
  5241. }
  5242. if (state->uio_mask & 0x0002) { /* UIO-2 */
  5243. /* write to io pad configuration register - output mode */
  5244. status = write16(state, SIO_PDR_SMA_RX_CFG__A,
  5245. state->m_gpio_cfg);
  5246. if (status < 0)
  5247. goto error;
  5248. /* use corresponding bit in io data output registar */
  5249. status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
  5250. if (status < 0)
  5251. goto error;
  5252. if ((state->m_gpio & 0x0002) == 0)
  5253. value &= 0xBFFF; /* write zero to 14th bit - 2st UIO */
  5254. else
  5255. value |= 0x4000; /* write one to 14th bit - 2st UIO */
  5256. /* write back to io data output register */
  5257. status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
  5258. if (status < 0)
  5259. goto error;
  5260. }
  5261. if (state->uio_mask & 0x0004) { /* UIO-3 */
  5262. /* write to io pad configuration register - output mode */
  5263. status = write16(state, SIO_PDR_GPIO_CFG__A,
  5264. state->m_gpio_cfg);
  5265. if (status < 0)
  5266. goto error;
  5267. /* use corresponding bit in io data output registar */
  5268. status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
  5269. if (status < 0)
  5270. goto error;
  5271. if ((state->m_gpio & 0x0004) == 0)
  5272. value &= 0xFFFB; /* write zero to 2nd bit - 3rd UIO */
  5273. else
  5274. value |= 0x0004; /* write one to 2nd bit - 3rd UIO */
  5275. /* write back to io data output register */
  5276. status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
  5277. if (status < 0)
  5278. goto error;
  5279. }
  5280. }
  5281. /* Write magic word to disable pdr reg write */
  5282. status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
  5283. error:
  5284. if (status < 0)
  5285. pr_err("Error %d on %s\n", status, __func__);
  5286. return status;
  5287. }
  5288. static int switch_antenna_to_qam(struct drxk_state *state)
  5289. {
  5290. int status = 0;
  5291. bool gpio_state;
  5292. dprintk(1, "\n");
  5293. if (!state->antenna_gpio)
  5294. return 0;
  5295. gpio_state = state->m_gpio & state->antenna_gpio;
  5296. if (state->antenna_dvbt ^ gpio_state) {
  5297. /* Antenna is on DVB-T mode. Switch */
  5298. if (state->antenna_dvbt)
  5299. state->m_gpio &= ~state->antenna_gpio;
  5300. else
  5301. state->m_gpio |= state->antenna_gpio;
  5302. status = write_gpio(state);
  5303. }
  5304. if (status < 0)
  5305. pr_err("Error %d on %s\n", status, __func__);
  5306. return status;
  5307. }
  5308. static int switch_antenna_to_dvbt(struct drxk_state *state)
  5309. {
  5310. int status = 0;
  5311. bool gpio_state;
  5312. dprintk(1, "\n");
  5313. if (!state->antenna_gpio)
  5314. return 0;
  5315. gpio_state = state->m_gpio & state->antenna_gpio;
  5316. if (!(state->antenna_dvbt ^ gpio_state)) {
  5317. /* Antenna is on DVB-C mode. Switch */
  5318. if (state->antenna_dvbt)
  5319. state->m_gpio |= state->antenna_gpio;
  5320. else
  5321. state->m_gpio &= ~state->antenna_gpio;
  5322. status = write_gpio(state);
  5323. }
  5324. if (status < 0)
  5325. pr_err("Error %d on %s\n", status, __func__);
  5326. return status;
  5327. }
  5328. static int power_down_device(struct drxk_state *state)
  5329. {
  5330. /* Power down to requested mode */
  5331. /* Backup some register settings */
  5332. /* Set pins with possible pull-ups connected to them in input mode */
  5333. /* Analog power down */
  5334. /* ADC power down */
  5335. /* Power down device */
  5336. int status;
  5337. dprintk(1, "\n");
  5338. if (state->m_b_p_down_open_bridge) {
  5339. /* Open I2C bridge before power down of DRXK */
  5340. status = ConfigureI2CBridge(state, true);
  5341. if (status < 0)
  5342. goto error;
  5343. }
  5344. /* driver 0.9.0 */
  5345. status = dvbt_enable_ofdm_token_ring(state, false);
  5346. if (status < 0)
  5347. goto error;
  5348. status = write16(state, SIO_CC_PWD_MODE__A,
  5349. SIO_CC_PWD_MODE_LEVEL_CLOCK);
  5350. if (status < 0)
  5351. goto error;
  5352. status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
  5353. if (status < 0)
  5354. goto error;
  5355. state->m_hi_cfg_ctrl |= SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
  5356. status = hi_cfg_command(state);
  5357. error:
  5358. if (status < 0)
  5359. pr_err("Error %d on %s\n", status, __func__);
  5360. return status;
  5361. }
  5362. static int init_drxk(struct drxk_state *state)
  5363. {
  5364. int status = 0, n = 0;
  5365. enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
  5366. u16 driver_version;
  5367. dprintk(1, "\n");
  5368. if (state->m_drxk_state == DRXK_UNINITIALIZED) {
  5369. drxk_i2c_lock(state);
  5370. status = power_up_device(state);
  5371. if (status < 0)
  5372. goto error;
  5373. status = drxx_open(state);
  5374. if (status < 0)
  5375. goto error;
  5376. /* Soft reset of OFDM-, sys- and osc-clockdomain */
  5377. status = write16(state, SIO_CC_SOFT_RST__A,
  5378. SIO_CC_SOFT_RST_OFDM__M
  5379. | SIO_CC_SOFT_RST_SYS__M
  5380. | SIO_CC_SOFT_RST_OSC__M);
  5381. if (status < 0)
  5382. goto error;
  5383. status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
  5384. if (status < 0)
  5385. goto error;
  5386. /*
  5387. * TODO is this needed? If yes, how much delay in
  5388. * worst case scenario
  5389. */
  5390. usleep_range(1000, 2000);
  5391. state->m_drxk_a3_patch_code = true;
  5392. status = get_device_capabilities(state);
  5393. if (status < 0)
  5394. goto error;
  5395. /* Bridge delay, uses oscilator clock */
  5396. /* Delay = (delay (nano seconds) * oscclk (kHz))/ 1000 */
  5397. /* SDA brdige delay */
  5398. state->m_hi_cfg_bridge_delay =
  5399. (u16) ((state->m_osc_clock_freq / 1000) *
  5400. HI_I2C_BRIDGE_DELAY) / 1000;
  5401. /* Clipping */
  5402. if (state->m_hi_cfg_bridge_delay >
  5403. SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M) {
  5404. state->m_hi_cfg_bridge_delay =
  5405. SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M;
  5406. }
  5407. /* SCL bridge delay, same as SDA for now */
  5408. state->m_hi_cfg_bridge_delay +=
  5409. state->m_hi_cfg_bridge_delay <<
  5410. SIO_HI_RA_RAM_PAR_3_CFG_DBL_SCL__B;
  5411. status = init_hi(state);
  5412. if (status < 0)
  5413. goto error;
  5414. /* disable various processes */
  5415. #if NOA1ROM
  5416. if (!(state->m_DRXK_A1_ROM_CODE)
  5417. && !(state->m_DRXK_A2_ROM_CODE))
  5418. #endif
  5419. {
  5420. status = write16(state, SCU_RAM_GPIO__A,
  5421. SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
  5422. if (status < 0)
  5423. goto error;
  5424. }
  5425. /* disable MPEG port */
  5426. status = mpegts_disable(state);
  5427. if (status < 0)
  5428. goto error;
  5429. /* Stop AUD and SCU */
  5430. status = write16(state, AUD_COMM_EXEC__A, AUD_COMM_EXEC_STOP);
  5431. if (status < 0)
  5432. goto error;
  5433. status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_STOP);
  5434. if (status < 0)
  5435. goto error;
  5436. /* enable token-ring bus through OFDM block for possible ucode upload */
  5437. status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A,
  5438. SIO_OFDM_SH_OFDM_RING_ENABLE_ON);
  5439. if (status < 0)
  5440. goto error;
  5441. /* include boot loader section */
  5442. status = write16(state, SIO_BL_COMM_EXEC__A,
  5443. SIO_BL_COMM_EXEC_ACTIVE);
  5444. if (status < 0)
  5445. goto error;
  5446. status = bl_chain_cmd(state, 0, 6, 100);
  5447. if (status < 0)
  5448. goto error;
  5449. if (state->fw) {
  5450. status = download_microcode(state, state->fw->data,
  5451. state->fw->size);
  5452. if (status < 0)
  5453. goto error;
  5454. }
  5455. /* disable token-ring bus through OFDM block for possible ucode upload */
  5456. status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A,
  5457. SIO_OFDM_SH_OFDM_RING_ENABLE_OFF);
  5458. if (status < 0)
  5459. goto error;
  5460. /* Run SCU for a little while to initialize microcode version numbers */
  5461. status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
  5462. if (status < 0)
  5463. goto error;
  5464. status = drxx_open(state);
  5465. if (status < 0)
  5466. goto error;
  5467. /* added for test */
  5468. msleep(30);
  5469. power_mode = DRXK_POWER_DOWN_OFDM;
  5470. status = ctrl_power_mode(state, &power_mode);
  5471. if (status < 0)
  5472. goto error;
  5473. /* Stamp driver version number in SCU data RAM in BCD code
  5474. Done to enable field application engineers to retrieve drxdriver version
  5475. via I2C from SCU RAM.
  5476. Not using SCU command interface for SCU register access since no
  5477. microcode may be present.
  5478. */
  5479. driver_version =
  5480. (((DRXK_VERSION_MAJOR / 100) % 10) << 12) +
  5481. (((DRXK_VERSION_MAJOR / 10) % 10) << 8) +
  5482. ((DRXK_VERSION_MAJOR % 10) << 4) +
  5483. (DRXK_VERSION_MINOR % 10);
  5484. status = write16(state, SCU_RAM_DRIVER_VER_HI__A,
  5485. driver_version);
  5486. if (status < 0)
  5487. goto error;
  5488. driver_version =
  5489. (((DRXK_VERSION_PATCH / 1000) % 10) << 12) +
  5490. (((DRXK_VERSION_PATCH / 100) % 10) << 8) +
  5491. (((DRXK_VERSION_PATCH / 10) % 10) << 4) +
  5492. (DRXK_VERSION_PATCH % 10);
  5493. status = write16(state, SCU_RAM_DRIVER_VER_LO__A,
  5494. driver_version);
  5495. if (status < 0)
  5496. goto error;
  5497. pr_info("DRXK driver version %d.%d.%d\n",
  5498. DRXK_VERSION_MAJOR, DRXK_VERSION_MINOR,
  5499. DRXK_VERSION_PATCH);
  5500. /*
  5501. * Dirty fix of default values for ROM/PATCH microcode
  5502. * Dirty because this fix makes it impossible to setup
  5503. * suitable values before calling DRX_Open. This solution
  5504. * requires changes to RF AGC speed to be done via the CTRL
  5505. * function after calling DRX_Open
  5506. */
  5507. /* m_dvbt_rf_agc_cfg.speed = 3; */
  5508. /* Reset driver debug flags to 0 */
  5509. status = write16(state, SCU_RAM_DRIVER_DEBUG__A, 0);
  5510. if (status < 0)
  5511. goto error;
  5512. /* driver 0.9.0 */
  5513. /* Setup FEC OC:
  5514. NOTE: No more full FEC resets allowed afterwards!! */
  5515. status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_STOP);
  5516. if (status < 0)
  5517. goto error;
  5518. /* MPEGTS functions are still the same */
  5519. status = mpegts_dto_init(state);
  5520. if (status < 0)
  5521. goto error;
  5522. status = mpegts_stop(state);
  5523. if (status < 0)
  5524. goto error;
  5525. status = mpegts_configure_polarity(state);
  5526. if (status < 0)
  5527. goto error;
  5528. status = mpegts_configure_pins(state, state->m_enable_mpeg_output);
  5529. if (status < 0)
  5530. goto error;
  5531. /* added: configure GPIO */
  5532. status = write_gpio(state);
  5533. if (status < 0)
  5534. goto error;
  5535. state->m_drxk_state = DRXK_STOPPED;
  5536. if (state->m_b_power_down) {
  5537. status = power_down_device(state);
  5538. if (status < 0)
  5539. goto error;
  5540. state->m_drxk_state = DRXK_POWERED_DOWN;
  5541. } else
  5542. state->m_drxk_state = DRXK_STOPPED;
  5543. /* Initialize the supported delivery systems */
  5544. n = 0;
  5545. if (state->m_has_dvbc) {
  5546. state->frontend.ops.delsys[n++] = SYS_DVBC_ANNEX_A;
  5547. state->frontend.ops.delsys[n++] = SYS_DVBC_ANNEX_C;
  5548. strlcat(state->frontend.ops.info.name, " DVB-C",
  5549. sizeof(state->frontend.ops.info.name));
  5550. }
  5551. if (state->m_has_dvbt) {
  5552. state->frontend.ops.delsys[n++] = SYS_DVBT;
  5553. strlcat(state->frontend.ops.info.name, " DVB-T",
  5554. sizeof(state->frontend.ops.info.name));
  5555. }
  5556. drxk_i2c_unlock(state);
  5557. }
  5558. error:
  5559. if (status < 0) {
  5560. state->m_drxk_state = DRXK_NO_DEV;
  5561. drxk_i2c_unlock(state);
  5562. pr_err("Error %d on %s\n", status, __func__);
  5563. }
  5564. return status;
  5565. }
  5566. static void load_firmware_cb(const struct firmware *fw,
  5567. void *context)
  5568. {
  5569. struct drxk_state *state = context;
  5570. dprintk(1, ": %s\n", fw ? "firmware loaded" : "firmware not loaded");
  5571. if (!fw) {
  5572. pr_err("Could not load firmware file %s.\n",
  5573. state->microcode_name);
  5574. pr_info("Copy %s to your hotplug directory!\n",
  5575. state->microcode_name);
  5576. state->microcode_name = NULL;
  5577. /*
  5578. * As firmware is now load asynchronous, it is not possible
  5579. * anymore to fail at frontend attach. We might silently
  5580. * return here, and hope that the driver won't crash.
  5581. * We might also change all DVB callbacks to return -ENODEV
  5582. * if the device is not initialized.
  5583. * As the DRX-K devices have their own internal firmware,
  5584. * let's just hope that it will match a firmware revision
  5585. * compatible with this driver and proceed.
  5586. */
  5587. }
  5588. state->fw = fw;
  5589. init_drxk(state);
  5590. }
  5591. static void drxk_release(struct dvb_frontend *fe)
  5592. {
  5593. struct drxk_state *state = fe->demodulator_priv;
  5594. dprintk(1, "\n");
  5595. release_firmware(state->fw);
  5596. kfree(state);
  5597. }
  5598. static int drxk_sleep(struct dvb_frontend *fe)
  5599. {
  5600. struct drxk_state *state = fe->demodulator_priv;
  5601. dprintk(1, "\n");
  5602. if (state->m_drxk_state == DRXK_NO_DEV)
  5603. return -ENODEV;
  5604. if (state->m_drxk_state == DRXK_UNINITIALIZED)
  5605. return 0;
  5606. shut_down(state);
  5607. return 0;
  5608. }
  5609. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  5610. {
  5611. struct drxk_state *state = fe->demodulator_priv;
  5612. dprintk(1, ": %s\n", enable ? "enable" : "disable");
  5613. if (state->m_drxk_state == DRXK_NO_DEV)
  5614. return -ENODEV;
  5615. return ConfigureI2CBridge(state, enable ? true : false);
  5616. }
  5617. static int drxk_set_parameters(struct dvb_frontend *fe)
  5618. {
  5619. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  5620. u32 delsys = p->delivery_system, old_delsys;
  5621. struct drxk_state *state = fe->demodulator_priv;
  5622. u32 IF;
  5623. dprintk(1, "\n");
  5624. if (state->m_drxk_state == DRXK_NO_DEV)
  5625. return -ENODEV;
  5626. if (state->m_drxk_state == DRXK_UNINITIALIZED)
  5627. return -EAGAIN;
  5628. if (!fe->ops.tuner_ops.get_if_frequency) {
  5629. pr_err("Error: get_if_frequency() not defined at tuner. Can't work without it!\n");
  5630. return -EINVAL;
  5631. }
  5632. if (fe->ops.i2c_gate_ctrl)
  5633. fe->ops.i2c_gate_ctrl(fe, 1);
  5634. if (fe->ops.tuner_ops.set_params)
  5635. fe->ops.tuner_ops.set_params(fe);
  5636. if (fe->ops.i2c_gate_ctrl)
  5637. fe->ops.i2c_gate_ctrl(fe, 0);
  5638. old_delsys = state->props.delivery_system;
  5639. state->props = *p;
  5640. if (old_delsys != delsys) {
  5641. shut_down(state);
  5642. switch (delsys) {
  5643. case SYS_DVBC_ANNEX_A:
  5644. case SYS_DVBC_ANNEX_C:
  5645. if (!state->m_has_dvbc)
  5646. return -EINVAL;
  5647. state->m_itut_annex_c = (delsys == SYS_DVBC_ANNEX_C) ?
  5648. true : false;
  5649. if (state->m_itut_annex_c)
  5650. setoperation_mode(state, OM_QAM_ITU_C);
  5651. else
  5652. setoperation_mode(state, OM_QAM_ITU_A);
  5653. break;
  5654. case SYS_DVBT:
  5655. if (!state->m_has_dvbt)
  5656. return -EINVAL;
  5657. setoperation_mode(state, OM_DVBT);
  5658. break;
  5659. default:
  5660. return -EINVAL;
  5661. }
  5662. }
  5663. fe->ops.tuner_ops.get_if_frequency(fe, &IF);
  5664. start(state, 0, IF);
  5665. /* After set_frontend, stats aren't available */
  5666. p->strength.stat[0].scale = FE_SCALE_RELATIVE;
  5667. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5668. p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5669. p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5670. p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5671. p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5672. p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5673. p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5674. /* printk(KERN_DEBUG "drxk: %s IF=%d done\n", __func__, IF); */
  5675. return 0;
  5676. }
  5677. static int get_strength(struct drxk_state *state, u64 *strength)
  5678. {
  5679. int status;
  5680. struct s_cfg_agc rf_agc, if_agc;
  5681. u32 total_gain = 0;
  5682. u32 atten = 0;
  5683. u32 agc_range = 0;
  5684. u16 scu_lvl = 0;
  5685. u16 scu_coc = 0;
  5686. /* FIXME: those are part of the tuner presets */
  5687. u16 tuner_rf_gain = 50; /* Default value on az6007 driver */
  5688. u16 tuner_if_gain = 40; /* Default value on az6007 driver */
  5689. *strength = 0;
  5690. if (is_dvbt(state)) {
  5691. rf_agc = state->m_dvbt_rf_agc_cfg;
  5692. if_agc = state->m_dvbt_if_agc_cfg;
  5693. } else if (is_qam(state)) {
  5694. rf_agc = state->m_qam_rf_agc_cfg;
  5695. if_agc = state->m_qam_if_agc_cfg;
  5696. } else {
  5697. rf_agc = state->m_atv_rf_agc_cfg;
  5698. if_agc = state->m_atv_if_agc_cfg;
  5699. }
  5700. if (rf_agc.ctrl_mode == DRXK_AGC_CTRL_AUTO) {
  5701. /* SCU output_level */
  5702. status = read16(state, SCU_RAM_AGC_RF_IACCU_HI__A, &scu_lvl);
  5703. if (status < 0)
  5704. return status;
  5705. /* SCU c.o.c. */
  5706. status = read16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, &scu_coc);
  5707. if (status < 0)
  5708. return status;
  5709. if (((u32) scu_lvl + (u32) scu_coc) < 0xffff)
  5710. rf_agc.output_level = scu_lvl + scu_coc;
  5711. else
  5712. rf_agc.output_level = 0xffff;
  5713. /* Take RF gain into account */
  5714. total_gain += tuner_rf_gain;
  5715. /* clip output value */
  5716. if (rf_agc.output_level < rf_agc.min_output_level)
  5717. rf_agc.output_level = rf_agc.min_output_level;
  5718. if (rf_agc.output_level > rf_agc.max_output_level)
  5719. rf_agc.output_level = rf_agc.max_output_level;
  5720. agc_range = (u32) (rf_agc.max_output_level - rf_agc.min_output_level);
  5721. if (agc_range > 0) {
  5722. atten += 100UL *
  5723. ((u32)(tuner_rf_gain)) *
  5724. ((u32)(rf_agc.output_level - rf_agc.min_output_level))
  5725. / agc_range;
  5726. }
  5727. }
  5728. if (if_agc.ctrl_mode == DRXK_AGC_CTRL_AUTO) {
  5729. status = read16(state, SCU_RAM_AGC_IF_IACCU_HI__A,
  5730. &if_agc.output_level);
  5731. if (status < 0)
  5732. return status;
  5733. status = read16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A,
  5734. &if_agc.top);
  5735. if (status < 0)
  5736. return status;
  5737. /* Take IF gain into account */
  5738. total_gain += (u32) tuner_if_gain;
  5739. /* clip output value */
  5740. if (if_agc.output_level < if_agc.min_output_level)
  5741. if_agc.output_level = if_agc.min_output_level;
  5742. if (if_agc.output_level > if_agc.max_output_level)
  5743. if_agc.output_level = if_agc.max_output_level;
  5744. agc_range = (u32)(if_agc.max_output_level - if_agc.min_output_level);
  5745. if (agc_range > 0) {
  5746. atten += 100UL *
  5747. ((u32)(tuner_if_gain)) *
  5748. ((u32)(if_agc.output_level - if_agc.min_output_level))
  5749. / agc_range;
  5750. }
  5751. }
  5752. /*
  5753. * Convert to 0..65535 scale.
  5754. * If it can't be measured (AGC is disabled), just show 100%.
  5755. */
  5756. if (total_gain > 0)
  5757. *strength = (65535UL * atten / total_gain / 100);
  5758. else
  5759. *strength = 65535;
  5760. return 0;
  5761. }
  5762. static int drxk_get_stats(struct dvb_frontend *fe)
  5763. {
  5764. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  5765. struct drxk_state *state = fe->demodulator_priv;
  5766. int status;
  5767. u32 stat;
  5768. u16 reg16;
  5769. u32 post_bit_count;
  5770. u32 post_bit_err_count;
  5771. u32 post_bit_error_scale;
  5772. u32 pre_bit_err_count;
  5773. u32 pre_bit_count;
  5774. u32 pkt_count;
  5775. u32 pkt_error_count;
  5776. s32 cnr;
  5777. if (state->m_drxk_state == DRXK_NO_DEV)
  5778. return -ENODEV;
  5779. if (state->m_drxk_state == DRXK_UNINITIALIZED)
  5780. return -EAGAIN;
  5781. /* get status */
  5782. state->fe_status = 0;
  5783. get_lock_status(state, &stat);
  5784. if (stat == MPEG_LOCK)
  5785. state->fe_status |= 0x1f;
  5786. if (stat == FEC_LOCK)
  5787. state->fe_status |= 0x0f;
  5788. if (stat == DEMOD_LOCK)
  5789. state->fe_status |= 0x07;
  5790. /*
  5791. * Estimate signal strength from AGC
  5792. */
  5793. get_strength(state, &c->strength.stat[0].uvalue);
  5794. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  5795. if (stat >= DEMOD_LOCK) {
  5796. get_signal_to_noise(state, &cnr);
  5797. c->cnr.stat[0].svalue = cnr * 100;
  5798. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  5799. } else {
  5800. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5801. }
  5802. if (stat < FEC_LOCK) {
  5803. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5804. c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5805. c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5806. c->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5807. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5808. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  5809. return 0;
  5810. }
  5811. /* Get post BER */
  5812. /* BER measurement is valid if at least FEC lock is achieved */
  5813. /*
  5814. * OFDM_EC_VD_REQ_SMB_CNT__A and/or OFDM_EC_VD_REQ_BIT_CNT can be
  5815. * written to set nr of symbols or bits over which to measure
  5816. * EC_VD_REG_ERR_BIT_CNT__A . See CtrlSetCfg().
  5817. */
  5818. /* Read registers for post/preViterbi BER calculation */
  5819. status = read16(state, OFDM_EC_VD_ERR_BIT_CNT__A, &reg16);
  5820. if (status < 0)
  5821. goto error;
  5822. pre_bit_err_count = reg16;
  5823. status = read16(state, OFDM_EC_VD_IN_BIT_CNT__A , &reg16);
  5824. if (status < 0)
  5825. goto error;
  5826. pre_bit_count = reg16;
  5827. /* Number of bit-errors */
  5828. status = read16(state, FEC_RS_NR_BIT_ERRORS__A, &reg16);
  5829. if (status < 0)
  5830. goto error;
  5831. post_bit_err_count = reg16;
  5832. status = read16(state, FEC_RS_MEASUREMENT_PRESCALE__A, &reg16);
  5833. if (status < 0)
  5834. goto error;
  5835. post_bit_error_scale = reg16;
  5836. status = read16(state, FEC_RS_MEASUREMENT_PERIOD__A, &reg16);
  5837. if (status < 0)
  5838. goto error;
  5839. pkt_count = reg16;
  5840. status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, &reg16);
  5841. if (status < 0)
  5842. goto error;
  5843. pkt_error_count = reg16;
  5844. write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
  5845. post_bit_err_count *= post_bit_error_scale;
  5846. post_bit_count = pkt_count * 204 * 8;
  5847. /* Store the results */
  5848. c->block_error.stat[0].scale = FE_SCALE_COUNTER;
  5849. c->block_error.stat[0].uvalue += pkt_error_count;
  5850. c->block_count.stat[0].scale = FE_SCALE_COUNTER;
  5851. c->block_count.stat[0].uvalue += pkt_count;
  5852. c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  5853. c->pre_bit_error.stat[0].uvalue += pre_bit_err_count;
  5854. c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  5855. c->pre_bit_count.stat[0].uvalue += pre_bit_count;
  5856. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  5857. c->post_bit_error.stat[0].uvalue += post_bit_err_count;
  5858. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  5859. c->post_bit_count.stat[0].uvalue += post_bit_count;
  5860. error:
  5861. return status;
  5862. }
  5863. static int drxk_read_status(struct dvb_frontend *fe, enum fe_status *status)
  5864. {
  5865. struct drxk_state *state = fe->demodulator_priv;
  5866. int rc;
  5867. dprintk(1, "\n");
  5868. rc = drxk_get_stats(fe);
  5869. if (rc < 0)
  5870. return rc;
  5871. *status = state->fe_status;
  5872. return 0;
  5873. }
  5874. static int drxk_read_signal_strength(struct dvb_frontend *fe,
  5875. u16 *strength)
  5876. {
  5877. struct drxk_state *state = fe->demodulator_priv;
  5878. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  5879. dprintk(1, "\n");
  5880. if (state->m_drxk_state == DRXK_NO_DEV)
  5881. return -ENODEV;
  5882. if (state->m_drxk_state == DRXK_UNINITIALIZED)
  5883. return -EAGAIN;
  5884. *strength = c->strength.stat[0].uvalue;
  5885. return 0;
  5886. }
  5887. static int drxk_read_snr(struct dvb_frontend *fe, u16 *snr)
  5888. {
  5889. struct drxk_state *state = fe->demodulator_priv;
  5890. s32 snr2;
  5891. dprintk(1, "\n");
  5892. if (state->m_drxk_state == DRXK_NO_DEV)
  5893. return -ENODEV;
  5894. if (state->m_drxk_state == DRXK_UNINITIALIZED)
  5895. return -EAGAIN;
  5896. get_signal_to_noise(state, &snr2);
  5897. /* No negative SNR, clip to zero */
  5898. if (snr2 < 0)
  5899. snr2 = 0;
  5900. *snr = snr2 & 0xffff;
  5901. return 0;
  5902. }
  5903. static int drxk_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  5904. {
  5905. struct drxk_state *state = fe->demodulator_priv;
  5906. u16 err;
  5907. dprintk(1, "\n");
  5908. if (state->m_drxk_state == DRXK_NO_DEV)
  5909. return -ENODEV;
  5910. if (state->m_drxk_state == DRXK_UNINITIALIZED)
  5911. return -EAGAIN;
  5912. dvbtqam_get_acc_pkt_err(state, &err);
  5913. *ucblocks = (u32) err;
  5914. return 0;
  5915. }
  5916. static int drxk_get_tune_settings(struct dvb_frontend *fe,
  5917. struct dvb_frontend_tune_settings *sets)
  5918. {
  5919. struct drxk_state *state = fe->demodulator_priv;
  5920. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  5921. dprintk(1, "\n");
  5922. if (state->m_drxk_state == DRXK_NO_DEV)
  5923. return -ENODEV;
  5924. if (state->m_drxk_state == DRXK_UNINITIALIZED)
  5925. return -EAGAIN;
  5926. switch (p->delivery_system) {
  5927. case SYS_DVBC_ANNEX_A:
  5928. case SYS_DVBC_ANNEX_C:
  5929. case SYS_DVBT:
  5930. sets->min_delay_ms = 3000;
  5931. sets->max_drift = 0;
  5932. sets->step_size = 0;
  5933. return 0;
  5934. default:
  5935. return -EINVAL;
  5936. }
  5937. }
  5938. static const struct dvb_frontend_ops drxk_ops = {
  5939. /* .delsys will be filled dynamically */
  5940. .info = {
  5941. .name = "DRXK",
  5942. .frequency_min_hz = 47 * MHz,
  5943. .frequency_max_hz = 865 * MHz,
  5944. /* For DVB-C */
  5945. .symbol_rate_min = 870000,
  5946. .symbol_rate_max = 11700000,
  5947. /* For DVB-T */
  5948. .frequency_stepsize_hz = 166667,
  5949. .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
  5950. FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO |
  5951. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  5952. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_MUTE_TS |
  5953. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER |
  5954. FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO
  5955. },
  5956. .release = drxk_release,
  5957. .sleep = drxk_sleep,
  5958. .i2c_gate_ctrl = drxk_gate_ctrl,
  5959. .set_frontend = drxk_set_parameters,
  5960. .get_tune_settings = drxk_get_tune_settings,
  5961. .read_status = drxk_read_status,
  5962. .read_signal_strength = drxk_read_signal_strength,
  5963. .read_snr = drxk_read_snr,
  5964. .read_ucblocks = drxk_read_ucblocks,
  5965. };
  5966. struct dvb_frontend *drxk_attach(const struct drxk_config *config,
  5967. struct i2c_adapter *i2c)
  5968. {
  5969. struct dtv_frontend_properties *p;
  5970. struct drxk_state *state = NULL;
  5971. u8 adr = config->adr;
  5972. int status;
  5973. dprintk(1, "\n");
  5974. state = kzalloc(sizeof(struct drxk_state), GFP_KERNEL);
  5975. if (!state)
  5976. return NULL;
  5977. state->i2c = i2c;
  5978. state->demod_address = adr;
  5979. state->single_master = config->single_master;
  5980. state->microcode_name = config->microcode_name;
  5981. state->qam_demod_parameter_count = config->qam_demod_parameter_count;
  5982. state->no_i2c_bridge = config->no_i2c_bridge;
  5983. state->antenna_gpio = config->antenna_gpio;
  5984. state->antenna_dvbt = config->antenna_dvbt;
  5985. state->m_chunk_size = config->chunk_size;
  5986. state->enable_merr_cfg = config->enable_merr_cfg;
  5987. if (config->dynamic_clk) {
  5988. state->m_dvbt_static_clk = false;
  5989. state->m_dvbc_static_clk = false;
  5990. } else {
  5991. state->m_dvbt_static_clk = true;
  5992. state->m_dvbc_static_clk = true;
  5993. }
  5994. if (config->mpeg_out_clk_strength)
  5995. state->m_ts_clockk_strength = config->mpeg_out_clk_strength & 0x07;
  5996. else
  5997. state->m_ts_clockk_strength = 0x06;
  5998. if (config->parallel_ts)
  5999. state->m_enable_parallel = true;
  6000. else
  6001. state->m_enable_parallel = false;
  6002. /* NOTE: as more UIO bits will be used, add them to the mask */
  6003. state->uio_mask = config->antenna_gpio;
  6004. /* Default gpio to DVB-C */
  6005. if (!state->antenna_dvbt && state->antenna_gpio)
  6006. state->m_gpio |= state->antenna_gpio;
  6007. else
  6008. state->m_gpio &= ~state->antenna_gpio;
  6009. mutex_init(&state->mutex);
  6010. memcpy(&state->frontend.ops, &drxk_ops, sizeof(drxk_ops));
  6011. state->frontend.demodulator_priv = state;
  6012. init_state(state);
  6013. /* Load firmware and initialize DRX-K */
  6014. if (state->microcode_name) {
  6015. const struct firmware *fw = NULL;
  6016. status = request_firmware(&fw, state->microcode_name,
  6017. state->i2c->dev.parent);
  6018. if (status < 0)
  6019. fw = NULL;
  6020. load_firmware_cb(fw, state);
  6021. } else if (init_drxk(state) < 0)
  6022. goto error;
  6023. /* Initialize stats */
  6024. p = &state->frontend.dtv_property_cache;
  6025. p->strength.len = 1;
  6026. p->cnr.len = 1;
  6027. p->block_error.len = 1;
  6028. p->block_count.len = 1;
  6029. p->pre_bit_error.len = 1;
  6030. p->pre_bit_count.len = 1;
  6031. p->post_bit_error.len = 1;
  6032. p->post_bit_count.len = 1;
  6033. p->strength.stat[0].scale = FE_SCALE_RELATIVE;
  6034. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  6035. p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  6036. p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  6037. p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  6038. p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  6039. p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  6040. p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  6041. pr_info("frontend initialized.\n");
  6042. return &state->frontend;
  6043. error:
  6044. pr_err("not found\n");
  6045. kfree(state);
  6046. return NULL;
  6047. }
  6048. EXPORT_SYMBOL(drxk_attach);
  6049. MODULE_DESCRIPTION("DRX-K driver");
  6050. MODULE_AUTHOR("Ralph Metzler");
  6051. MODULE_LICENSE("GPL");