q6afe.c 206 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392
  1. /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kthread.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/wait.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/sched.h>
  20. #include <linux/delay.h>
  21. #include <dsp/msm_audio_ion.h>
  22. #include <dsp/apr_audio-v2.h>
  23. #include <dsp/audio_cal_utils.h>
  24. #include <dsp/q6afe-v2.h>
  25. #include <dsp/q6audio-v2.h>
  26. #include <dsp/q6common.h>
  27. #include <ipc/apr_tal.h>
  28. #include "adsp_err.h"
  29. #define WAKELOCK_TIMEOUT 5000
  30. enum {
  31. AFE_COMMON_RX_CAL = 0,
  32. AFE_COMMON_TX_CAL,
  33. AFE_LSM_TX_CAL,
  34. AFE_AANC_CAL,
  35. AFE_FB_SPKR_PROT_CAL,
  36. AFE_HW_DELAY_CAL,
  37. AFE_SIDETONE_CAL,
  38. AFE_SIDETONE_IIR_CAL,
  39. AFE_TOPOLOGY_CAL,
  40. AFE_LSM_TOPOLOGY_CAL,
  41. AFE_CUST_TOPOLOGY_CAL,
  42. AFE_FB_SPKR_PROT_TH_VI_CAL,
  43. AFE_FB_SPKR_PROT_EX_VI_CAL,
  44. MAX_AFE_CAL_TYPES
  45. };
  46. enum fbsp_state {
  47. FBSP_INCORRECT_OP_MODE,
  48. FBSP_INACTIVE,
  49. FBSP_WARMUP,
  50. FBSP_IN_PROGRESS,
  51. FBSP_SUCCESS,
  52. FBSP_FAILED,
  53. MAX_FBSP_STATE
  54. };
  55. static char fbsp_state[MAX_FBSP_STATE][50] = {
  56. [FBSP_INCORRECT_OP_MODE] = "incorrect operation mode",
  57. [FBSP_INACTIVE] = "port not started",
  58. [FBSP_WARMUP] = "waiting for warmup",
  59. [FBSP_IN_PROGRESS] = "in progress state",
  60. [FBSP_SUCCESS] = "success",
  61. [FBSP_FAILED] = "failed"
  62. };
  63. enum {
  64. USE_CALIBRATED_R0TO,
  65. USE_SAFE_R0TO
  66. };
  67. enum {
  68. QUICK_CALIB_DISABLE,
  69. QUICK_CALIB_ENABLE
  70. };
  71. enum {
  72. Q6AFE_MSM_SPKR_PROCESSING = 0,
  73. Q6AFE_MSM_SPKR_CALIBRATION,
  74. Q6AFE_MSM_SPKR_FTM_MODE
  75. };
  76. struct wlock {
  77. struct wakeup_source ws;
  78. };
  79. static struct wlock wl;
  80. struct afe_ctl {
  81. void *apr;
  82. atomic_t state;
  83. atomic_t status;
  84. wait_queue_head_t wait[AFE_MAX_PORTS];
  85. struct task_struct *task;
  86. void (*tx_cb)(uint32_t opcode,
  87. uint32_t token, uint32_t *payload, void *priv);
  88. void (*rx_cb)(uint32_t opcode,
  89. uint32_t token, uint32_t *payload, void *priv);
  90. void *tx_private_data;
  91. void *rx_private_data;
  92. uint32_t mmap_handle;
  93. int topology[AFE_MAX_PORTS];
  94. struct cal_type_data *cal_data[MAX_AFE_CAL_TYPES];
  95. atomic_t mem_map_cal_handles[MAX_AFE_CAL_TYPES];
  96. atomic_t mem_map_cal_index;
  97. u32 afe_cal_mode[AFE_MAX_PORTS];
  98. u16 dtmf_gen_rx_portid;
  99. struct audio_cal_info_spk_prot_cfg prot_cfg;
  100. struct afe_spkr_prot_calib_get_resp calib_data;
  101. struct audio_cal_info_sp_th_vi_ftm_cfg th_ftm_cfg;
  102. struct audio_cal_info_sp_ex_vi_ftm_cfg ex_ftm_cfg;
  103. struct afe_sp_th_vi_get_param_resp th_vi_resp;
  104. struct afe_sp_ex_vi_get_param_resp ex_vi_resp;
  105. struct afe_av_dev_drift_get_param_resp av_dev_drift_resp;
  106. int vi_tx_port;
  107. int vi_rx_port;
  108. uint32_t afe_sample_rates[AFE_MAX_PORTS];
  109. struct aanc_data aanc_info;
  110. struct mutex afe_cmd_lock;
  111. int set_custom_topology;
  112. int dev_acdb_id[AFE_MAX_PORTS];
  113. routing_cb rt_cb;
  114. };
  115. static atomic_t afe_ports_mad_type[SLIMBUS_PORT_LAST - SLIMBUS_0_RX];
  116. static unsigned long afe_configured_cmd;
  117. static struct afe_ctl this_afe;
  118. #define TIMEOUT_MS 1000
  119. #define Q6AFE_MAX_VOLUME 0x3FFF
  120. static int pcm_afe_instance[2];
  121. static int proxy_afe_instance[2];
  122. bool afe_close_done[2] = {true, true};
  123. #define SIZEOF_CFG_CMD(y) \
  124. (sizeof(struct apr_hdr) + sizeof(u16) + (sizeof(struct y)))
  125. static int afe_get_cal_hw_delay(int32_t path,
  126. struct audio_cal_hw_delay_entry *entry);
  127. static int remap_cal_data(struct cal_block_data *cal_block, int cal_index);
  128. int afe_get_topology(int port_id)
  129. {
  130. int topology;
  131. int port_index = afe_get_port_index(port_id);
  132. if ((port_index < 0) || (port_index >= AFE_MAX_PORTS)) {
  133. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  134. topology = -EINVAL;
  135. goto done;
  136. }
  137. topology = this_afe.topology[port_index];
  138. done:
  139. return topology;
  140. }
  141. /**
  142. * afe_set_aanc_info -
  143. * Update AFE AANC info
  144. *
  145. * @q6_aanc_info: AFE AANC info params
  146. *
  147. */
  148. void afe_set_aanc_info(struct aanc_data *q6_aanc_info)
  149. {
  150. this_afe.aanc_info.aanc_active = q6_aanc_info->aanc_active;
  151. this_afe.aanc_info.aanc_rx_port = q6_aanc_info->aanc_rx_port;
  152. this_afe.aanc_info.aanc_tx_port = q6_aanc_info->aanc_tx_port;
  153. pr_debug("%s: aanc active is %d rx port is 0x%x, tx port is 0x%x\n",
  154. __func__,
  155. this_afe.aanc_info.aanc_active,
  156. this_afe.aanc_info.aanc_rx_port,
  157. this_afe.aanc_info.aanc_tx_port);
  158. }
  159. EXPORT_SYMBOL(afe_set_aanc_info);
  160. static void afe_callback_debug_print(struct apr_client_data *data)
  161. {
  162. uint32_t *payload;
  163. payload = data->payload;
  164. if (data->payload_size >= 8)
  165. pr_debug("%s: code = 0x%x PL#0[0x%x], PL#1[0x%x], size = %d\n",
  166. __func__, data->opcode, payload[0], payload[1],
  167. data->payload_size);
  168. else if (data->payload_size >= 4)
  169. pr_debug("%s: code = 0x%x PL#0[0x%x], size = %d\n",
  170. __func__, data->opcode, payload[0],
  171. data->payload_size);
  172. else
  173. pr_debug("%s: code = 0x%x, size = %d\n",
  174. __func__, data->opcode, data->payload_size);
  175. }
  176. static void av_dev_drift_afe_cb_handler(uint32_t opcode, uint32_t *payload,
  177. uint32_t payload_size)
  178. {
  179. u32 param_id;
  180. size_t expected_size =
  181. sizeof(u32) + sizeof(struct afe_param_id_dev_timing_stats);
  182. /* Get param ID depending on command type */
  183. param_id = (opcode == AFE_PORT_CMDRSP_GET_PARAM_V3) ? payload[3] :
  184. payload[2];
  185. if (param_id != AFE_PARAM_ID_DEV_TIMING_STATS) {
  186. pr_err("%s: Unrecognized param ID %d\n", __func__, param_id);
  187. return;
  188. }
  189. switch (opcode) {
  190. case AFE_PORT_CMDRSP_GET_PARAM_V2:
  191. expected_size += sizeof(struct param_hdr_v1);
  192. if (payload_size < expected_size) {
  193. pr_err("%s: Error: received size %d, expected size %zu\n",
  194. __func__, payload_size, expected_size);
  195. return;
  196. }
  197. /* Repack response to add IID */
  198. this_afe.av_dev_drift_resp.status = payload[0];
  199. this_afe.av_dev_drift_resp.pdata.module_id = payload[1];
  200. this_afe.av_dev_drift_resp.pdata.instance_id = INSTANCE_ID_0;
  201. this_afe.av_dev_drift_resp.pdata.param_id = payload[2];
  202. this_afe.av_dev_drift_resp.pdata.param_size = payload[3];
  203. memcpy(&this_afe.av_dev_drift_resp.timing_stats, &payload[4],
  204. sizeof(struct afe_param_id_dev_timing_stats));
  205. break;
  206. case AFE_PORT_CMDRSP_GET_PARAM_V3:
  207. expected_size += sizeof(struct param_hdr_v3);
  208. if (payload_size < expected_size) {
  209. pr_err("%s: Error: received size %d, expected size %zu\n",
  210. __func__, payload_size, expected_size);
  211. return;
  212. }
  213. memcpy(&this_afe.av_dev_drift_resp, payload,
  214. sizeof(this_afe.av_dev_drift_resp));
  215. break;
  216. default:
  217. pr_err("%s: Unrecognized command %d\n", __func__, opcode);
  218. return;
  219. }
  220. if (!this_afe.av_dev_drift_resp.status) {
  221. atomic_set(&this_afe.state, 0);
  222. } else {
  223. pr_debug("%s: av_dev_drift_resp status: %d", __func__,
  224. this_afe.av_dev_drift_resp.status);
  225. atomic_set(&this_afe.state, -1);
  226. }
  227. }
  228. static int32_t sp_make_afe_callback(uint32_t opcode, uint32_t *payload,
  229. uint32_t payload_size)
  230. {
  231. struct param_hdr_v3 param_hdr;
  232. u32 *data_dest = NULL;
  233. u32 *data_start = NULL;
  234. size_t expected_size = sizeof(u32);
  235. memset(&param_hdr, 0, sizeof(param_hdr));
  236. /* Set command specific details */
  237. switch (opcode) {
  238. case AFE_PORT_CMDRSP_GET_PARAM_V2:
  239. expected_size += sizeof(struct param_hdr_v1);
  240. param_hdr.module_id = payload[1];
  241. param_hdr.instance_id = INSTANCE_ID_0;
  242. param_hdr.param_id = payload[2];
  243. param_hdr.param_size = payload[3];
  244. data_start = &payload[4];
  245. break;
  246. case AFE_PORT_CMDRSP_GET_PARAM_V3:
  247. expected_size += sizeof(struct param_hdr_v3);
  248. memcpy(&param_hdr, &payload[1], sizeof(struct param_hdr_v3));
  249. data_start = &payload[5];
  250. break;
  251. default:
  252. pr_err("%s: Unrecognized command %d\n", __func__, opcode);
  253. return -EINVAL;
  254. }
  255. switch (param_hdr.param_id) {
  256. case AFE_PARAM_ID_CALIB_RES_CFG_V2:
  257. expected_size += sizeof(struct asm_calib_res_cfg);
  258. data_dest = (u32 *) &this_afe.calib_data;
  259. break;
  260. case AFE_PARAM_ID_SP_V2_TH_VI_FTM_PARAMS:
  261. expected_size += sizeof(struct afe_sp_th_vi_ftm_params);
  262. data_dest = (u32 *) &this_afe.th_vi_resp;
  263. break;
  264. case AFE_PARAM_ID_SP_V2_EX_VI_FTM_PARAMS:
  265. expected_size += sizeof(struct afe_sp_ex_vi_ftm_params);
  266. data_dest = (u32 *) &this_afe.ex_vi_resp;
  267. break;
  268. default:
  269. pr_err("%s: Unrecognized param ID %d\n", __func__,
  270. param_hdr.param_id);
  271. return -EINVAL;
  272. }
  273. if (payload_size < expected_size) {
  274. pr_err("%s: Error: received size %d, expected size %zu for param %d\n",
  275. __func__, payload_size, expected_size,
  276. param_hdr.param_id);
  277. return -EINVAL;
  278. }
  279. data_dest[0] = payload[0];
  280. memcpy(&data_dest[1], &param_hdr, sizeof(struct param_hdr_v3));
  281. memcpy(&data_dest[5], data_start, param_hdr.param_size);
  282. if (!data_dest[0]) {
  283. atomic_set(&this_afe.state, 0);
  284. } else {
  285. pr_debug("%s: status: %d", __func__, data_dest[0]);
  286. atomic_set(&this_afe.state, -1);
  287. }
  288. return 0;
  289. }
  290. static int32_t afe_callback(struct apr_client_data *data, void *priv)
  291. {
  292. if (!data) {
  293. pr_err("%s: Invalid param data\n", __func__);
  294. return -EINVAL;
  295. }
  296. if (data->opcode == RESET_EVENTS) {
  297. pr_debug("%s: reset event = %d %d apr[%pK]\n",
  298. __func__,
  299. data->reset_event, data->reset_proc, this_afe.apr);
  300. cal_utils_clear_cal_block_q6maps(MAX_AFE_CAL_TYPES,
  301. this_afe.cal_data);
  302. /* Reset the custom topology mode: to resend again to AFE. */
  303. mutex_lock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  304. this_afe.set_custom_topology = 1;
  305. mutex_unlock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  306. rtac_clear_mapping(AFE_RTAC_CAL);
  307. if (this_afe.apr) {
  308. apr_reset(this_afe.apr);
  309. atomic_set(&this_afe.state, 0);
  310. this_afe.apr = NULL;
  311. rtac_set_afe_handle(this_afe.apr);
  312. }
  313. /* send info to user */
  314. if (this_afe.task == NULL)
  315. this_afe.task = current;
  316. pr_debug("%s: task_name = %s pid = %d\n",
  317. __func__,
  318. this_afe.task->comm, this_afe.task->pid);
  319. /*
  320. * Pass reset events to proxy driver, if cb is registered
  321. */
  322. if (this_afe.tx_cb) {
  323. this_afe.tx_cb(data->opcode, data->token,
  324. data->payload,
  325. this_afe.tx_private_data);
  326. this_afe.tx_cb = NULL;
  327. }
  328. if (this_afe.rx_cb) {
  329. this_afe.rx_cb(data->opcode, data->token,
  330. data->payload,
  331. this_afe.rx_private_data);
  332. this_afe.rx_cb = NULL;
  333. }
  334. return 0;
  335. }
  336. afe_callback_debug_print(data);
  337. if (data->opcode == AFE_PORT_CMDRSP_GET_PARAM_V2 ||
  338. data->opcode == AFE_PORT_CMDRSP_GET_PARAM_V3) {
  339. uint32_t *payload = data->payload;
  340. uint32_t param_id;
  341. if (!payload || (data->token >= AFE_MAX_PORTS)) {
  342. pr_err("%s: Error: size %d payload %pK token %d\n",
  343. __func__, data->payload_size,
  344. payload, data->token);
  345. return -EINVAL;
  346. }
  347. param_id = (data->opcode == AFE_PORT_CMDRSP_GET_PARAM_V3) ?
  348. payload[3] :
  349. payload[2];
  350. if (param_id == AFE_PARAM_ID_DEV_TIMING_STATS) {
  351. av_dev_drift_afe_cb_handler(data->opcode, data->payload,
  352. data->payload_size);
  353. } else {
  354. if (rtac_make_afe_callback(data->payload,
  355. data->payload_size))
  356. return 0;
  357. if (sp_make_afe_callback(data->opcode, data->payload,
  358. data->payload_size))
  359. return -EINVAL;
  360. }
  361. wake_up(&this_afe.wait[data->token]);
  362. } else if (data->payload_size) {
  363. uint32_t *payload;
  364. uint16_t port_id = 0;
  365. payload = data->payload;
  366. if (data->opcode == APR_BASIC_RSP_RESULT) {
  367. pr_debug("%s:opcode = 0x%x cmd = 0x%x status = 0x%x token=%d\n",
  368. __func__, data->opcode,
  369. payload[0], payload[1], data->token);
  370. /* payload[1] contains the error status for response */
  371. if (payload[1] != 0) {
  372. atomic_set(&this_afe.status, payload[1]);
  373. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  374. __func__, payload[0], payload[1]);
  375. }
  376. switch (payload[0]) {
  377. case AFE_PORT_CMD_SET_PARAM_V2:
  378. case AFE_PORT_CMD_SET_PARAM_V3:
  379. if (rtac_make_afe_callback(payload,
  380. data->payload_size))
  381. return 0;
  382. case AFE_PORT_CMD_DEVICE_STOP:
  383. case AFE_PORT_CMD_DEVICE_START:
  384. case AFE_PSEUDOPORT_CMD_START:
  385. case AFE_PSEUDOPORT_CMD_STOP:
  386. case AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS:
  387. case AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS:
  388. case AFE_SERVICE_CMD_UNREGISTER_RT_PORT_DRIVER:
  389. case AFE_PORTS_CMD_DTMF_CTL:
  390. case AFE_SVC_CMD_SET_PARAM:
  391. case AFE_SVC_CMD_SET_PARAM_V2:
  392. atomic_set(&this_afe.state, 0);
  393. wake_up(&this_afe.wait[data->token]);
  394. break;
  395. case AFE_SERVICE_CMD_REGISTER_RT_PORT_DRIVER:
  396. break;
  397. case AFE_PORT_DATA_CMD_RT_PROXY_PORT_WRITE_V2:
  398. port_id = RT_PROXY_PORT_001_TX;
  399. break;
  400. case AFE_PORT_DATA_CMD_RT_PROXY_PORT_READ_V2:
  401. port_id = RT_PROXY_PORT_001_RX;
  402. break;
  403. case AFE_CMD_ADD_TOPOLOGIES:
  404. atomic_set(&this_afe.state, 0);
  405. wake_up(&this_afe.wait[data->token]);
  406. pr_debug("%s: AFE_CMD_ADD_TOPOLOGIES cmd 0x%x\n",
  407. __func__, payload[1]);
  408. break;
  409. case AFE_PORT_CMD_GET_PARAM_V2:
  410. case AFE_PORT_CMD_GET_PARAM_V3:
  411. /*
  412. * Should only come here if there is an APR
  413. * error or malformed APR packet. Otherwise
  414. * response will be returned as
  415. * AFE_PORT_CMDRSP_GET_PARAM_V2/3
  416. */
  417. pr_debug("%s: AFE Get Param opcode 0x%x token 0x%x src %d dest %d\n",
  418. __func__, data->opcode, data->token,
  419. data->src_port, data->dest_port);
  420. if (payload[1] != 0) {
  421. pr_err("%s: AFE Get Param failed with error %d\n",
  422. __func__, payload[1]);
  423. if (rtac_make_afe_callback(
  424. payload,
  425. data->payload_size))
  426. return 0;
  427. }
  428. atomic_set(&this_afe.state, payload[1]);
  429. wake_up(&this_afe.wait[data->token]);
  430. break;
  431. default:
  432. pr_err("%s: Unknown cmd 0x%x\n", __func__,
  433. payload[0]);
  434. break;
  435. }
  436. } else if (data->opcode ==
  437. AFE_SERVICE_CMDRSP_SHARED_MEM_MAP_REGIONS) {
  438. pr_debug("%s: mmap_handle: 0x%x, cal index %d\n",
  439. __func__, payload[0],
  440. atomic_read(&this_afe.mem_map_cal_index));
  441. if (atomic_read(&this_afe.mem_map_cal_index) != -1)
  442. atomic_set(&this_afe.mem_map_cal_handles[
  443. atomic_read(
  444. &this_afe.mem_map_cal_index)],
  445. (uint32_t)payload[0]);
  446. else
  447. this_afe.mmap_handle = payload[0];
  448. atomic_set(&this_afe.state, 0);
  449. wake_up(&this_afe.wait[data->token]);
  450. } else if (data->opcode == AFE_EVENT_RT_PROXY_PORT_STATUS) {
  451. port_id = (uint16_t)(0x0000FFFF & payload[0]);
  452. }
  453. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  454. switch (port_id) {
  455. case RT_PROXY_PORT_001_TX: {
  456. if (this_afe.tx_cb) {
  457. this_afe.tx_cb(data->opcode, data->token,
  458. data->payload,
  459. this_afe.tx_private_data);
  460. }
  461. break;
  462. }
  463. case RT_PROXY_PORT_001_RX: {
  464. if (this_afe.rx_cb) {
  465. this_afe.rx_cb(data->opcode, data->token,
  466. data->payload,
  467. this_afe.rx_private_data);
  468. }
  469. break;
  470. }
  471. default:
  472. pr_debug("%s: default case 0x%x\n", __func__, port_id);
  473. break;
  474. }
  475. }
  476. return 0;
  477. }
  478. /**
  479. * afe_get_port_type -
  480. * Retrieve AFE port type whether RX or TX
  481. *
  482. * @port_id: AFE Port ID number
  483. *
  484. * Returns RX/TX type on success or -EINVAL on failure.
  485. */
  486. int afe_get_port_type(u16 port_id)
  487. {
  488. int ret;
  489. switch (port_id) {
  490. case PRIMARY_I2S_RX:
  491. case SECONDARY_I2S_RX:
  492. case MI2S_RX:
  493. case HDMI_RX:
  494. case DISPLAY_PORT_RX:
  495. case AFE_PORT_ID_SPDIF_RX:
  496. case SLIMBUS_0_RX:
  497. case SLIMBUS_1_RX:
  498. case SLIMBUS_2_RX:
  499. case SLIMBUS_3_RX:
  500. case SLIMBUS_4_RX:
  501. case SLIMBUS_5_RX:
  502. case SLIMBUS_6_RX:
  503. case SLIMBUS_7_RX:
  504. case SLIMBUS_8_RX:
  505. case INT_BT_SCO_RX:
  506. case INT_BT_A2DP_RX:
  507. case INT_FM_RX:
  508. case VOICE_PLAYBACK_TX:
  509. case VOICE2_PLAYBACK_TX:
  510. case RT_PROXY_PORT_001_RX:
  511. case AUDIO_PORT_ID_I2S_RX:
  512. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  513. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  514. case AFE_PORT_ID_SECONDARY_MI2S_RX_SD1:
  515. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  516. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  517. case AFE_PORT_ID_QUINARY_MI2S_RX:
  518. case AFE_PORT_ID_PRIMARY_PCM_RX:
  519. case AFE_PORT_ID_SECONDARY_PCM_RX:
  520. case AFE_PORT_ID_TERTIARY_PCM_RX:
  521. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  522. case AFE_PORT_ID_QUINARY_PCM_RX:
  523. case AFE_PORT_ID_PRIMARY_TDM_RX:
  524. case AFE_PORT_ID_PRIMARY_TDM_RX_1:
  525. case AFE_PORT_ID_PRIMARY_TDM_RX_2:
  526. case AFE_PORT_ID_PRIMARY_TDM_RX_3:
  527. case AFE_PORT_ID_PRIMARY_TDM_RX_4:
  528. case AFE_PORT_ID_PRIMARY_TDM_RX_5:
  529. case AFE_PORT_ID_PRIMARY_TDM_RX_6:
  530. case AFE_PORT_ID_PRIMARY_TDM_RX_7:
  531. case AFE_PORT_ID_SECONDARY_TDM_RX:
  532. case AFE_PORT_ID_SECONDARY_TDM_RX_1:
  533. case AFE_PORT_ID_SECONDARY_TDM_RX_2:
  534. case AFE_PORT_ID_SECONDARY_TDM_RX_3:
  535. case AFE_PORT_ID_SECONDARY_TDM_RX_4:
  536. case AFE_PORT_ID_SECONDARY_TDM_RX_5:
  537. case AFE_PORT_ID_SECONDARY_TDM_RX_6:
  538. case AFE_PORT_ID_SECONDARY_TDM_RX_7:
  539. case AFE_PORT_ID_TERTIARY_TDM_RX:
  540. case AFE_PORT_ID_TERTIARY_TDM_RX_1:
  541. case AFE_PORT_ID_TERTIARY_TDM_RX_2:
  542. case AFE_PORT_ID_TERTIARY_TDM_RX_3:
  543. case AFE_PORT_ID_TERTIARY_TDM_RX_4:
  544. case AFE_PORT_ID_TERTIARY_TDM_RX_5:
  545. case AFE_PORT_ID_TERTIARY_TDM_RX_6:
  546. case AFE_PORT_ID_TERTIARY_TDM_RX_7:
  547. case AFE_PORT_ID_QUATERNARY_TDM_RX:
  548. case AFE_PORT_ID_QUATERNARY_TDM_RX_1:
  549. case AFE_PORT_ID_QUATERNARY_TDM_RX_2:
  550. case AFE_PORT_ID_QUATERNARY_TDM_RX_3:
  551. case AFE_PORT_ID_QUATERNARY_TDM_RX_4:
  552. case AFE_PORT_ID_QUATERNARY_TDM_RX_5:
  553. case AFE_PORT_ID_QUATERNARY_TDM_RX_6:
  554. case AFE_PORT_ID_QUATERNARY_TDM_RX_7:
  555. case AFE_PORT_ID_QUINARY_TDM_RX:
  556. case AFE_PORT_ID_QUINARY_TDM_RX_1:
  557. case AFE_PORT_ID_QUINARY_TDM_RX_2:
  558. case AFE_PORT_ID_QUINARY_TDM_RX_3:
  559. case AFE_PORT_ID_QUINARY_TDM_RX_4:
  560. case AFE_PORT_ID_QUINARY_TDM_RX_5:
  561. case AFE_PORT_ID_QUINARY_TDM_RX_6:
  562. case AFE_PORT_ID_QUINARY_TDM_RX_7:
  563. case AFE_PORT_ID_USB_RX:
  564. case AFE_PORT_ID_INT0_MI2S_RX:
  565. case AFE_PORT_ID_INT1_MI2S_RX:
  566. case AFE_PORT_ID_INT2_MI2S_RX:
  567. case AFE_PORT_ID_INT3_MI2S_RX:
  568. case AFE_PORT_ID_INT4_MI2S_RX:
  569. case AFE_PORT_ID_INT5_MI2S_RX:
  570. case AFE_PORT_ID_INT6_MI2S_RX:
  571. ret = MSM_AFE_PORT_TYPE_RX;
  572. break;
  573. case PRIMARY_I2S_TX:
  574. case SECONDARY_I2S_TX:
  575. case MI2S_TX:
  576. case DIGI_MIC_TX:
  577. case VOICE_RECORD_TX:
  578. case SLIMBUS_0_TX:
  579. case SLIMBUS_1_TX:
  580. case SLIMBUS_2_TX:
  581. case SLIMBUS_3_TX:
  582. case SLIMBUS_4_TX:
  583. case SLIMBUS_5_TX:
  584. case SLIMBUS_6_TX:
  585. case SLIMBUS_7_TX:
  586. case SLIMBUS_8_TX:
  587. case INT_FM_TX:
  588. case VOICE_RECORD_RX:
  589. case INT_BT_SCO_TX:
  590. case RT_PROXY_PORT_001_TX:
  591. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  592. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  593. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  594. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  595. case AFE_PORT_ID_QUINARY_MI2S_TX:
  596. case AFE_PORT_ID_SENARY_MI2S_TX:
  597. case AFE_PORT_ID_PRIMARY_PCM_TX:
  598. case AFE_PORT_ID_SECONDARY_PCM_TX:
  599. case AFE_PORT_ID_TERTIARY_PCM_TX:
  600. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  601. case AFE_PORT_ID_QUINARY_PCM_TX:
  602. case AFE_PORT_ID_PRIMARY_TDM_TX:
  603. case AFE_PORT_ID_PRIMARY_TDM_TX_1:
  604. case AFE_PORT_ID_PRIMARY_TDM_TX_2:
  605. case AFE_PORT_ID_PRIMARY_TDM_TX_3:
  606. case AFE_PORT_ID_PRIMARY_TDM_TX_4:
  607. case AFE_PORT_ID_PRIMARY_TDM_TX_5:
  608. case AFE_PORT_ID_PRIMARY_TDM_TX_6:
  609. case AFE_PORT_ID_PRIMARY_TDM_TX_7:
  610. case AFE_PORT_ID_SECONDARY_TDM_TX:
  611. case AFE_PORT_ID_SECONDARY_TDM_TX_1:
  612. case AFE_PORT_ID_SECONDARY_TDM_TX_2:
  613. case AFE_PORT_ID_SECONDARY_TDM_TX_3:
  614. case AFE_PORT_ID_SECONDARY_TDM_TX_4:
  615. case AFE_PORT_ID_SECONDARY_TDM_TX_5:
  616. case AFE_PORT_ID_SECONDARY_TDM_TX_6:
  617. case AFE_PORT_ID_SECONDARY_TDM_TX_7:
  618. case AFE_PORT_ID_TERTIARY_TDM_TX:
  619. case AFE_PORT_ID_TERTIARY_TDM_TX_1:
  620. case AFE_PORT_ID_TERTIARY_TDM_TX_2:
  621. case AFE_PORT_ID_TERTIARY_TDM_TX_3:
  622. case AFE_PORT_ID_TERTIARY_TDM_TX_4:
  623. case AFE_PORT_ID_TERTIARY_TDM_TX_5:
  624. case AFE_PORT_ID_TERTIARY_TDM_TX_6:
  625. case AFE_PORT_ID_TERTIARY_TDM_TX_7:
  626. case AFE_PORT_ID_QUATERNARY_TDM_TX:
  627. case AFE_PORT_ID_QUATERNARY_TDM_TX_1:
  628. case AFE_PORT_ID_QUATERNARY_TDM_TX_2:
  629. case AFE_PORT_ID_QUATERNARY_TDM_TX_3:
  630. case AFE_PORT_ID_QUATERNARY_TDM_TX_4:
  631. case AFE_PORT_ID_QUATERNARY_TDM_TX_5:
  632. case AFE_PORT_ID_QUATERNARY_TDM_TX_6:
  633. case AFE_PORT_ID_QUATERNARY_TDM_TX_7:
  634. case AFE_PORT_ID_QUINARY_TDM_TX:
  635. case AFE_PORT_ID_QUINARY_TDM_TX_1:
  636. case AFE_PORT_ID_QUINARY_TDM_TX_2:
  637. case AFE_PORT_ID_QUINARY_TDM_TX_3:
  638. case AFE_PORT_ID_QUINARY_TDM_TX_4:
  639. case AFE_PORT_ID_QUINARY_TDM_TX_5:
  640. case AFE_PORT_ID_QUINARY_TDM_TX_6:
  641. case AFE_PORT_ID_QUINARY_TDM_TX_7:
  642. case AFE_PORT_ID_USB_TX:
  643. case AFE_PORT_ID_INT0_MI2S_TX:
  644. case AFE_PORT_ID_INT1_MI2S_TX:
  645. case AFE_PORT_ID_INT2_MI2S_TX:
  646. case AFE_PORT_ID_INT3_MI2S_TX:
  647. case AFE_PORT_ID_INT4_MI2S_TX:
  648. case AFE_PORT_ID_INT5_MI2S_TX:
  649. case AFE_PORT_ID_INT6_MI2S_TX:
  650. ret = MSM_AFE_PORT_TYPE_TX;
  651. break;
  652. default:
  653. WARN_ON(1);
  654. pr_err("%s: Invalid port id = 0x%x\n",
  655. __func__, port_id);
  656. ret = -EINVAL;
  657. }
  658. return ret;
  659. }
  660. EXPORT_SYMBOL(afe_get_port_type);
  661. int afe_sizeof_cfg_cmd(u16 port_id)
  662. {
  663. int ret_size;
  664. switch (port_id) {
  665. case PRIMARY_I2S_RX:
  666. case PRIMARY_I2S_TX:
  667. case SECONDARY_I2S_RX:
  668. case SECONDARY_I2S_TX:
  669. case MI2S_RX:
  670. case MI2S_TX:
  671. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  672. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  673. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  674. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  675. case AFE_PORT_ID_QUINARY_MI2S_RX:
  676. case AFE_PORT_ID_QUINARY_MI2S_TX:
  677. ret_size = SIZEOF_CFG_CMD(afe_param_id_i2s_cfg);
  678. break;
  679. case HDMI_RX:
  680. case DISPLAY_PORT_RX:
  681. ret_size =
  682. SIZEOF_CFG_CMD(afe_param_id_hdmi_multi_chan_audio_cfg);
  683. break;
  684. case SLIMBUS_0_RX:
  685. case SLIMBUS_0_TX:
  686. case SLIMBUS_1_RX:
  687. case SLIMBUS_1_TX:
  688. case SLIMBUS_2_RX:
  689. case SLIMBUS_2_TX:
  690. case SLIMBUS_3_RX:
  691. case SLIMBUS_3_TX:
  692. case SLIMBUS_4_RX:
  693. case SLIMBUS_4_TX:
  694. case SLIMBUS_5_RX:
  695. case SLIMBUS_5_TX:
  696. case SLIMBUS_6_RX:
  697. case SLIMBUS_6_TX:
  698. case SLIMBUS_7_RX:
  699. case SLIMBUS_7_TX:
  700. case SLIMBUS_8_RX:
  701. case SLIMBUS_8_TX:
  702. ret_size = SIZEOF_CFG_CMD(afe_param_id_slimbus_cfg);
  703. break;
  704. case VOICE_PLAYBACK_TX:
  705. case VOICE2_PLAYBACK_TX:
  706. case VOICE_RECORD_RX:
  707. case VOICE_RECORD_TX:
  708. ret_size = SIZEOF_CFG_CMD(afe_param_id_pseudo_port_cfg);
  709. break;
  710. case RT_PROXY_PORT_001_RX:
  711. case RT_PROXY_PORT_001_TX:
  712. ret_size = SIZEOF_CFG_CMD(afe_param_id_rt_proxy_port_cfg);
  713. break;
  714. case AFE_PORT_ID_USB_RX:
  715. case AFE_PORT_ID_USB_TX:
  716. ret_size = SIZEOF_CFG_CMD(afe_param_id_usb_audio_cfg);
  717. break;
  718. case AFE_PORT_ID_PRIMARY_PCM_RX:
  719. case AFE_PORT_ID_PRIMARY_PCM_TX:
  720. case AFE_PORT_ID_SECONDARY_PCM_RX:
  721. case AFE_PORT_ID_SECONDARY_PCM_TX:
  722. case AFE_PORT_ID_TERTIARY_PCM_RX:
  723. case AFE_PORT_ID_TERTIARY_PCM_TX:
  724. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  725. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  726. case AFE_PORT_ID_QUINARY_PCM_RX:
  727. case AFE_PORT_ID_QUINARY_PCM_TX:
  728. default:
  729. pr_debug("%s: default case 0x%x\n", __func__, port_id);
  730. ret_size = SIZEOF_CFG_CMD(afe_param_id_pcm_cfg);
  731. break;
  732. }
  733. return ret_size;
  734. }
  735. /**
  736. * afe_q6_interface_prepare -
  737. * wrapper API to check Q6 AFE registered to APR otherwise registers
  738. *
  739. * Returns 0 on success or error on failure.
  740. */
  741. int afe_q6_interface_prepare(void)
  742. {
  743. int ret = 0;
  744. pr_debug("%s:\n", __func__);
  745. if (this_afe.apr == NULL) {
  746. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  747. 0xFFFFFFFF, &this_afe);
  748. if (this_afe.apr == NULL) {
  749. pr_err("%s: Unable to register AFE\n", __func__);
  750. ret = -ENODEV;
  751. }
  752. rtac_set_afe_handle(this_afe.apr);
  753. }
  754. return ret;
  755. }
  756. EXPORT_SYMBOL(afe_q6_interface_prepare);
  757. /*
  758. * afe_apr_send_pkt : returns 0 on success, negative otherwise.
  759. */
  760. static int afe_apr_send_pkt(void *data, wait_queue_head_t *wait)
  761. {
  762. int ret;
  763. if (wait)
  764. atomic_set(&this_afe.state, 1);
  765. atomic_set(&this_afe.status, 0);
  766. ret = apr_send_pkt(this_afe.apr, data);
  767. if (ret > 0) {
  768. if (wait) {
  769. ret = wait_event_timeout(*wait,
  770. (atomic_read(&this_afe.state) == 0),
  771. msecs_to_jiffies(TIMEOUT_MS));
  772. if (!ret) {
  773. ret = -ETIMEDOUT;
  774. } else if (atomic_read(&this_afe.status) > 0) {
  775. pr_err("%s: DSP returned error[%s]\n", __func__,
  776. adsp_err_get_err_str(atomic_read(
  777. &this_afe.status)));
  778. ret = adsp_err_get_lnx_err_code(
  779. atomic_read(&this_afe.status));
  780. } else {
  781. ret = 0;
  782. }
  783. } else {
  784. ret = 0;
  785. }
  786. } else if (ret == 0) {
  787. pr_err("%s: packet not transmitted\n", __func__);
  788. /* apr_send_pkt can return 0 when nothing is transmitted */
  789. ret = -EINVAL;
  790. }
  791. pr_debug("%s: leave %d\n", __func__, ret);
  792. return ret;
  793. }
  794. /* This function shouldn't be called directly. Instead call q6afe_set_params. */
  795. static int q6afe_set_params_v2(u16 port_id, int index,
  796. struct mem_mapping_hdr *mem_hdr,
  797. u8 *packed_param_data, u32 packed_data_size)
  798. {
  799. struct afe_port_cmd_set_param_v2 *set_param = NULL;
  800. uint32_t size = sizeof(struct afe_port_cmd_set_param_v2);
  801. int rc = 0;
  802. if (packed_param_data != NULL)
  803. size += packed_data_size;
  804. set_param = kzalloc(size, GFP_KERNEL);
  805. if (set_param == NULL)
  806. return -ENOMEM;
  807. set_param->apr_hdr.hdr_field =
  808. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  809. APR_PKT_VER);
  810. set_param->apr_hdr.pkt_size = size;
  811. set_param->apr_hdr.src_port = 0;
  812. set_param->apr_hdr.dest_port = 0;
  813. set_param->apr_hdr.token = index;
  814. set_param->apr_hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  815. set_param->port_id = port_id;
  816. if (packed_data_size > U16_MAX) {
  817. pr_err("%s: Invalid data size for set params V2 %d\n", __func__,
  818. packed_data_size);
  819. rc = -EINVAL;
  820. goto done;
  821. }
  822. set_param->payload_size = packed_data_size;
  823. if (mem_hdr != NULL) {
  824. set_param->mem_hdr = *mem_hdr;
  825. } else if (packed_param_data != NULL) {
  826. memcpy(&set_param->param_data, packed_param_data,
  827. packed_data_size);
  828. } else {
  829. pr_err("%s: Both memory header and param data are NULL\n",
  830. __func__);
  831. rc = -EINVAL;
  832. goto done;
  833. }
  834. rc = afe_apr_send_pkt(set_param, &this_afe.wait[index]);
  835. done:
  836. kfree(set_param);
  837. return rc;
  838. }
  839. /* This function shouldn't be called directly. Instead call q6afe_set_params. */
  840. static int q6afe_set_params_v3(u16 port_id, int index,
  841. struct mem_mapping_hdr *mem_hdr,
  842. u8 *packed_param_data, u32 packed_data_size)
  843. {
  844. struct afe_port_cmd_set_param_v3 *set_param = NULL;
  845. uint32_t size = sizeof(struct afe_port_cmd_set_param_v3);
  846. int rc = 0;
  847. if (packed_param_data != NULL)
  848. size += packed_data_size;
  849. set_param = kzalloc(size, GFP_KERNEL);
  850. if (set_param == NULL)
  851. return -ENOMEM;
  852. set_param->apr_hdr.hdr_field =
  853. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  854. APR_PKT_VER);
  855. set_param->apr_hdr.pkt_size = size;
  856. set_param->apr_hdr.src_port = 0;
  857. set_param->apr_hdr.dest_port = 0;
  858. set_param->apr_hdr.token = index;
  859. set_param->apr_hdr.opcode = AFE_PORT_CMD_SET_PARAM_V3;
  860. set_param->port_id = port_id;
  861. set_param->payload_size = packed_data_size;
  862. if (mem_hdr != NULL) {
  863. set_param->mem_hdr = *mem_hdr;
  864. } else if (packed_param_data != NULL) {
  865. memcpy(&set_param->param_data, packed_param_data,
  866. packed_data_size);
  867. } else {
  868. pr_err("%s: Both memory header and param data are NULL\n",
  869. __func__);
  870. rc = -EINVAL;
  871. goto done;
  872. }
  873. rc = afe_apr_send_pkt(set_param, &this_afe.wait[index]);
  874. done:
  875. kfree(set_param);
  876. return rc;
  877. }
  878. static int q6afe_set_params(u16 port_id, int index,
  879. struct mem_mapping_hdr *mem_hdr,
  880. u8 *packed_param_data, u32 packed_data_size)
  881. {
  882. int ret = 0;
  883. ret = afe_q6_interface_prepare();
  884. if (ret != 0) {
  885. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  886. return ret;
  887. }
  888. port_id = q6audio_get_port_id(port_id);
  889. ret = q6audio_validate_port(port_id);
  890. if (ret < 0) {
  891. pr_err("%s: Not a valid port id = 0x%x ret %d\n", __func__,
  892. port_id, ret);
  893. return -EINVAL;
  894. }
  895. if (index < 0 || index >= AFE_MAX_PORTS) {
  896. pr_err("%s: AFE port index[%d] invalid\n", __func__, index);
  897. return -EINVAL;
  898. }
  899. if (q6common_is_instance_id_supported())
  900. return q6afe_set_params_v3(port_id, index, mem_hdr,
  901. packed_param_data, packed_data_size);
  902. else
  903. return q6afe_set_params_v2(port_id, index, mem_hdr,
  904. packed_param_data, packed_data_size);
  905. }
  906. static int q6afe_pack_and_set_param_in_band(u16 port_id, int index,
  907. struct param_hdr_v3 param_hdr,
  908. u8 *param_data)
  909. {
  910. u8 *packed_param_data = NULL;
  911. int packed_data_size = sizeof(union param_hdrs) + param_hdr.param_size;
  912. int ret;
  913. packed_param_data = kzalloc(packed_data_size, GFP_KERNEL);
  914. if (packed_param_data == NULL)
  915. return -ENOMEM;
  916. ret = q6common_pack_pp_params(packed_param_data, &param_hdr, param_data,
  917. &packed_data_size);
  918. if (ret) {
  919. pr_err("%s: Failed to pack param header and data, error %d\n",
  920. __func__, ret);
  921. goto fail_cmd;
  922. }
  923. ret = q6afe_set_params(port_id, index, NULL, packed_param_data,
  924. packed_data_size);
  925. fail_cmd:
  926. kfree(packed_param_data);
  927. return ret;
  928. }
  929. /* This function shouldn't be called directly. Instead call q6afe_get_param. */
  930. static int q6afe_get_params_v2(u16 port_id, int index,
  931. struct mem_mapping_hdr *mem_hdr,
  932. struct param_hdr_v3 *param_hdr)
  933. {
  934. struct afe_port_cmd_get_param_v2 afe_get_param;
  935. u32 param_size = param_hdr->param_size;
  936. memset(&afe_get_param, 0, sizeof(afe_get_param));
  937. afe_get_param.apr_hdr.hdr_field =
  938. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  939. APR_PKT_VER);
  940. afe_get_param.apr_hdr.pkt_size = sizeof(afe_get_param) + param_size;
  941. afe_get_param.apr_hdr.src_port = 0;
  942. afe_get_param.apr_hdr.dest_port = 0;
  943. afe_get_param.apr_hdr.token = index;
  944. afe_get_param.apr_hdr.opcode = AFE_PORT_CMD_GET_PARAM_V2;
  945. afe_get_param.port_id = port_id;
  946. afe_get_param.payload_size = sizeof(struct param_hdr_v1) + param_size;
  947. if (mem_hdr != NULL)
  948. afe_get_param.mem_hdr = *mem_hdr;
  949. /* Set MID and PID in command */
  950. afe_get_param.module_id = param_hdr->module_id;
  951. afe_get_param.param_id = param_hdr->param_id;
  952. /* Set param header in payload */
  953. afe_get_param.param_hdr.module_id = param_hdr->module_id;
  954. afe_get_param.param_hdr.param_id = param_hdr->param_id;
  955. afe_get_param.param_hdr.param_size = param_size;
  956. return afe_apr_send_pkt(&afe_get_param, &this_afe.wait[index]);
  957. }
  958. /* This function shouldn't be called directly. Instead call q6afe_get_param. */
  959. static int q6afe_get_params_v3(u16 port_id, int index,
  960. struct mem_mapping_hdr *mem_hdr,
  961. struct param_hdr_v3 *param_hdr)
  962. {
  963. struct afe_port_cmd_get_param_v3 afe_get_param;
  964. memset(&afe_get_param, 0, sizeof(afe_get_param));
  965. afe_get_param.apr_hdr.hdr_field =
  966. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  967. APR_PKT_VER);
  968. afe_get_param.apr_hdr.pkt_size = sizeof(afe_get_param);
  969. afe_get_param.apr_hdr.src_port = 0;
  970. afe_get_param.apr_hdr.dest_port = 0;
  971. afe_get_param.apr_hdr.token = index;
  972. afe_get_param.apr_hdr.opcode = AFE_PORT_CMD_GET_PARAM_V3;
  973. afe_get_param.port_id = port_id;
  974. if (mem_hdr != NULL)
  975. afe_get_param.mem_hdr = *mem_hdr;
  976. /* Set param header in command, no payload in V3 */
  977. afe_get_param.param_hdr = *param_hdr;
  978. return afe_apr_send_pkt(&afe_get_param, &this_afe.wait[index]);
  979. }
  980. /*
  981. * Calling functions copy param data directly from this_afe. Do not copy data
  982. * back to caller here.
  983. */
  984. static int q6afe_get_params(u16 port_id, struct mem_mapping_hdr *mem_hdr,
  985. struct param_hdr_v3 *param_hdr)
  986. {
  987. int index;
  988. int ret;
  989. ret = afe_q6_interface_prepare();
  990. if (ret != 0) {
  991. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  992. return ret;
  993. }
  994. port_id = q6audio_get_port_id(port_id);
  995. ret = q6audio_validate_port(port_id);
  996. if (ret < 0) {
  997. pr_err("%s: Not a valid port id = 0x%x ret %d\n", __func__,
  998. port_id, ret);
  999. return -EINVAL;
  1000. }
  1001. index = q6audio_get_port_index(port_id);
  1002. if (index < 0 || index >= AFE_MAX_PORTS) {
  1003. pr_err("%s: AFE port index[%d] invalid\n", __func__, index);
  1004. return -EINVAL;
  1005. }
  1006. if (q6common_is_instance_id_supported())
  1007. return q6afe_get_params_v3(port_id, index, NULL, param_hdr);
  1008. else
  1009. return q6afe_get_params_v2(port_id, index, NULL, param_hdr);
  1010. }
  1011. /*
  1012. * This function shouldn't be called directly. Instead call
  1013. * q6afe_svc_set_params.
  1014. */
  1015. static int q6afe_svc_set_params_v1(int index, struct mem_mapping_hdr *mem_hdr,
  1016. u8 *packed_param_data, u32 packed_data_size)
  1017. {
  1018. struct afe_svc_cmd_set_param_v1 *svc_set_param = NULL;
  1019. uint32_t size = sizeof(struct afe_svc_cmd_set_param_v1);
  1020. int rc = 0;
  1021. if (packed_param_data != NULL)
  1022. size += packed_data_size;
  1023. svc_set_param = kzalloc(size, GFP_KERNEL);
  1024. if (svc_set_param == NULL)
  1025. return -ENOMEM;
  1026. svc_set_param->apr_hdr.hdr_field =
  1027. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  1028. APR_PKT_VER);
  1029. svc_set_param->apr_hdr.pkt_size = size;
  1030. svc_set_param->apr_hdr.src_port = 0;
  1031. svc_set_param->apr_hdr.dest_port = 0;
  1032. svc_set_param->apr_hdr.token = index;
  1033. svc_set_param->apr_hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  1034. svc_set_param->payload_size = packed_data_size;
  1035. if (mem_hdr != NULL) {
  1036. /* Out of band case. */
  1037. svc_set_param->mem_hdr = *mem_hdr;
  1038. } else if (packed_param_data != NULL) {
  1039. /* In band case. */
  1040. memcpy(&svc_set_param->param_data, packed_param_data,
  1041. packed_data_size);
  1042. } else {
  1043. pr_err("%s: Both memory header and param data are NULL\n",
  1044. __func__);
  1045. rc = -EINVAL;
  1046. goto done;
  1047. }
  1048. rc = afe_apr_send_pkt(svc_set_param, &this_afe.wait[index]);
  1049. done:
  1050. kfree(svc_set_param);
  1051. return rc;
  1052. }
  1053. /*
  1054. * This function shouldn't be called directly. Instead call
  1055. * q6afe_svc_set_params.
  1056. */
  1057. static int q6afe_svc_set_params_v2(int index, struct mem_mapping_hdr *mem_hdr,
  1058. u8 *packed_param_data, u32 packed_data_size)
  1059. {
  1060. struct afe_svc_cmd_set_param_v2 *svc_set_param = NULL;
  1061. uint16_t size = sizeof(struct afe_svc_cmd_set_param_v2);
  1062. int rc = 0;
  1063. if (packed_param_data != NULL)
  1064. size += packed_data_size;
  1065. svc_set_param = kzalloc(size, GFP_KERNEL);
  1066. if (svc_set_param == NULL)
  1067. return -ENOMEM;
  1068. svc_set_param->apr_hdr.hdr_field =
  1069. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  1070. APR_PKT_VER);
  1071. svc_set_param->apr_hdr.pkt_size = size;
  1072. svc_set_param->apr_hdr.src_port = 0;
  1073. svc_set_param->apr_hdr.dest_port = 0;
  1074. svc_set_param->apr_hdr.token = index;
  1075. svc_set_param->apr_hdr.opcode = AFE_SVC_CMD_SET_PARAM_V2;
  1076. svc_set_param->payload_size = packed_data_size;
  1077. if (mem_hdr != NULL) {
  1078. /* Out of band case. */
  1079. svc_set_param->mem_hdr = *mem_hdr;
  1080. } else if (packed_param_data != NULL) {
  1081. /* In band case. */
  1082. memcpy(&svc_set_param->param_data, packed_param_data,
  1083. packed_data_size);
  1084. } else {
  1085. pr_err("%s: Both memory header and param data are NULL\n",
  1086. __func__);
  1087. rc = -EINVAL;
  1088. goto done;
  1089. }
  1090. rc = afe_apr_send_pkt(svc_set_param, &this_afe.wait[index]);
  1091. done:
  1092. kfree(svc_set_param);
  1093. return rc;
  1094. }
  1095. static int q6afe_svc_set_params(int index, struct mem_mapping_hdr *mem_hdr,
  1096. u8 *packed_param_data, u32 packed_data_size)
  1097. {
  1098. int ret;
  1099. ret = afe_q6_interface_prepare();
  1100. if (ret != 0) {
  1101. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  1102. return ret;
  1103. }
  1104. if (q6common_is_instance_id_supported())
  1105. return q6afe_svc_set_params_v2(index, mem_hdr,
  1106. packed_param_data,
  1107. packed_data_size);
  1108. else
  1109. return q6afe_svc_set_params_v1(index, mem_hdr,
  1110. packed_param_data,
  1111. packed_data_size);
  1112. }
  1113. static int q6afe_svc_pack_and_set_param_in_band(int index,
  1114. struct param_hdr_v3 param_hdr,
  1115. u8 *param_data)
  1116. {
  1117. u8 *packed_param_data = NULL;
  1118. u32 packed_data_size =
  1119. sizeof(struct param_hdr_v3) + param_hdr.param_size;
  1120. int ret = 0;
  1121. packed_param_data = kzalloc(packed_data_size, GFP_KERNEL);
  1122. if (!packed_param_data)
  1123. return -ENOMEM;
  1124. ret = q6common_pack_pp_params(packed_param_data, &param_hdr, param_data,
  1125. &packed_data_size);
  1126. if (ret) {
  1127. pr_err("%s: Failed to pack parameter header and data, error %d\n",
  1128. __func__, ret);
  1129. goto done;
  1130. }
  1131. ret = q6afe_svc_set_params(index, NULL, packed_param_data,
  1132. packed_data_size);
  1133. done:
  1134. kfree(packed_param_data);
  1135. return ret;
  1136. }
  1137. static int afe_send_cal_block(u16 port_id, struct cal_block_data *cal_block)
  1138. {
  1139. struct mem_mapping_hdr mem_hdr;
  1140. int payload_size = 0;
  1141. int result = 0;
  1142. memset(&mem_hdr, 0, sizeof(mem_hdr));
  1143. if (!cal_block) {
  1144. pr_debug("%s: No AFE cal to send!\n", __func__);
  1145. result = -EINVAL;
  1146. goto done;
  1147. }
  1148. if (cal_block->cal_data.size <= 0) {
  1149. pr_debug("%s: AFE cal has invalid size!\n", __func__);
  1150. result = -EINVAL;
  1151. goto done;
  1152. }
  1153. payload_size = cal_block->cal_data.size;
  1154. mem_hdr.data_payload_addr_lsw =
  1155. lower_32_bits(cal_block->cal_data.paddr);
  1156. mem_hdr.data_payload_addr_msw =
  1157. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  1158. mem_hdr.mem_map_handle = cal_block->map_data.q6map_handle;
  1159. pr_debug("%s: AFE cal sent for device port = 0x%x, cal size = %zd, cal addr = 0x%pK\n",
  1160. __func__, port_id,
  1161. cal_block->cal_data.size, &cal_block->cal_data.paddr);
  1162. result = q6afe_set_params(port_id, q6audio_get_port_index(port_id),
  1163. &mem_hdr, NULL, payload_size);
  1164. if (result)
  1165. pr_err("%s: AFE cal for port 0x%x failed %d\n",
  1166. __func__, port_id, result);
  1167. done:
  1168. return result;
  1169. }
  1170. static int afe_send_custom_topology_block(struct cal_block_data *cal_block)
  1171. {
  1172. int result = 0;
  1173. int index = 0;
  1174. struct cmd_set_topologies afe_cal;
  1175. if (!cal_block) {
  1176. pr_err("%s: No AFE SVC cal to send!\n", __func__);
  1177. return -EINVAL;
  1178. }
  1179. if (cal_block->cal_data.size <= 0) {
  1180. pr_err("%s: AFE SVC cal has invalid size: %zd!\n",
  1181. __func__, cal_block->cal_data.size);
  1182. return -EINVAL;
  1183. }
  1184. afe_cal.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1185. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1186. afe_cal.hdr.pkt_size = sizeof(afe_cal);
  1187. afe_cal.hdr.src_port = 0;
  1188. afe_cal.hdr.dest_port = 0;
  1189. afe_cal.hdr.token = index;
  1190. afe_cal.hdr.opcode = AFE_CMD_ADD_TOPOLOGIES;
  1191. afe_cal.payload_size = cal_block->cal_data.size;
  1192. afe_cal.payload_addr_lsw =
  1193. lower_32_bits(cal_block->cal_data.paddr);
  1194. afe_cal.payload_addr_msw =
  1195. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  1196. afe_cal.mem_map_handle = cal_block->map_data.q6map_handle;
  1197. pr_debug("%s:cmd_id:0x%x calsize:%zd memmap_hdl:0x%x caladdr:0x%pK",
  1198. __func__, AFE_CMD_ADD_TOPOLOGIES, cal_block->cal_data.size,
  1199. afe_cal.mem_map_handle, &cal_block->cal_data.paddr);
  1200. result = afe_apr_send_pkt(&afe_cal, &this_afe.wait[index]);
  1201. if (result)
  1202. pr_err("%s: AFE send topology for command 0x%x failed %d\n",
  1203. __func__, AFE_CMD_ADD_TOPOLOGIES, result);
  1204. return result;
  1205. }
  1206. static void afe_send_custom_topology(void)
  1207. {
  1208. struct cal_block_data *cal_block = NULL;
  1209. int cal_index = AFE_CUST_TOPOLOGY_CAL;
  1210. int ret;
  1211. if (this_afe.cal_data[cal_index] == NULL) {
  1212. pr_err("%s: cal_index %d not allocated!\n",
  1213. __func__, cal_index);
  1214. return;
  1215. }
  1216. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  1217. if (!this_afe.set_custom_topology)
  1218. goto unlock;
  1219. this_afe.set_custom_topology = 0;
  1220. cal_block = cal_utils_get_only_cal_block(this_afe.cal_data[cal_index]);
  1221. if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
  1222. pr_err("%s cal_block not found!!\n", __func__);
  1223. goto unlock;
  1224. }
  1225. pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
  1226. ret = remap_cal_data(cal_block, cal_index);
  1227. if (ret) {
  1228. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  1229. __func__, cal_index);
  1230. goto unlock;
  1231. }
  1232. ret = afe_send_custom_topology_block(cal_block);
  1233. if (ret < 0) {
  1234. pr_err("%s: No cal sent for cal_index %d! ret %d\n",
  1235. __func__, cal_index, ret);
  1236. goto unlock;
  1237. }
  1238. pr_debug("%s:sent custom topology for AFE\n", __func__);
  1239. unlock:
  1240. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  1241. }
  1242. static int afe_spk_ramp_dn_cfg(int port)
  1243. {
  1244. struct param_hdr_v3 param_info;
  1245. int ret = -EINVAL;
  1246. memset(&param_info, 0, sizeof(param_info));
  1247. if (afe_get_port_type(port) != MSM_AFE_PORT_TYPE_RX) {
  1248. pr_debug("%s: port doesn't match 0x%x\n", __func__, port);
  1249. return 0;
  1250. }
  1251. if (this_afe.prot_cfg.mode == MSM_SPKR_PROT_DISABLED ||
  1252. (this_afe.vi_rx_port != port)) {
  1253. pr_debug("%s: spkr protection disabled port 0x%x %d 0x%x\n",
  1254. __func__, port, ret, this_afe.vi_rx_port);
  1255. return 0;
  1256. }
  1257. param_info.module_id = AFE_MODULE_FB_SPKR_PROT_V2_RX;
  1258. param_info.instance_id = INSTANCE_ID_0;
  1259. param_info.param_id = AFE_PARAM_ID_FBSP_PTONE_RAMP_CFG;
  1260. param_info.param_size = 0;
  1261. ret = q6afe_pack_and_set_param_in_band(port,
  1262. q6audio_get_port_index(port),
  1263. param_info, NULL);
  1264. if (ret) {
  1265. pr_err("%s: Failed to set speaker ramp duration param, err %d\n",
  1266. __func__, ret);
  1267. goto fail_cmd;
  1268. }
  1269. /* dsp needs atleast 15ms to ramp down pilot tone*/
  1270. usleep_range(15000, 15010);
  1271. ret = 0;
  1272. fail_cmd:
  1273. pr_debug("%s: config.pdata.param_id 0x%x status %d\n", __func__,
  1274. param_info.param_id, ret);
  1275. return ret;
  1276. }
  1277. static int afe_spk_prot_prepare(int src_port, int dst_port, int param_id,
  1278. union afe_spkr_prot_config *prot_config)
  1279. {
  1280. struct param_hdr_v3 param_info;
  1281. int ret = -EINVAL;
  1282. memset(&param_info, 0, sizeof(param_info));
  1283. ret = q6audio_validate_port(src_port);
  1284. if (ret < 0) {
  1285. pr_err("%s: Invalid src port 0x%x ret %d", __func__, src_port,
  1286. ret);
  1287. ret = -EINVAL;
  1288. goto fail_cmd;
  1289. }
  1290. ret = q6audio_validate_port(dst_port);
  1291. if (ret < 0) {
  1292. pr_err("%s: Invalid dst port 0x%x ret %d", __func__,
  1293. dst_port, ret);
  1294. ret = -EINVAL;
  1295. goto fail_cmd;
  1296. }
  1297. switch (param_id) {
  1298. case AFE_PARAM_ID_FBSP_MODE_RX_CFG:
  1299. case AFE_PARAM_ID_SP_RX_LIMITER_TH:
  1300. param_info.module_id = AFE_MODULE_FB_SPKR_PROT_V2_RX;
  1301. break;
  1302. case AFE_PARAM_ID_FEEDBACK_PATH_CFG:
  1303. this_afe.vi_tx_port = src_port;
  1304. this_afe.vi_rx_port = dst_port;
  1305. param_info.module_id = AFE_MODULE_FEEDBACK;
  1306. break;
  1307. /*
  1308. * AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG_V2 is same as
  1309. * AFE_PARAM_ID_SP_V2_TH_VI_MODE_CFG
  1310. */
  1311. case AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG_V2:
  1312. case AFE_PARAM_ID_SP_V2_TH_VI_FTM_CFG:
  1313. param_info.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_TH_VI;
  1314. break;
  1315. case AFE_PARAM_ID_SP_V2_EX_VI_MODE_CFG:
  1316. case AFE_PARAM_ID_SP_V2_EX_VI_FTM_CFG:
  1317. param_info.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_EX_VI;
  1318. break;
  1319. default:
  1320. pr_err("%s: default case 0x%x\n", __func__, param_id);
  1321. goto fail_cmd;
  1322. }
  1323. param_info.instance_id = INSTANCE_ID_0;
  1324. param_info.param_id = param_id;
  1325. param_info.param_size = sizeof(union afe_spkr_prot_config);
  1326. ret = q6afe_pack_and_set_param_in_band(src_port,
  1327. q6audio_get_port_index(src_port),
  1328. param_info, (u8 *) prot_config);
  1329. if (ret)
  1330. pr_err("%s: port = 0x%x param = 0x%x failed %d\n", __func__,
  1331. src_port, param_id, ret);
  1332. fail_cmd:
  1333. pr_debug("%s: config.pdata.param_id 0x%x status %d 0x%x\n", __func__,
  1334. param_info.param_id, ret, src_port);
  1335. return ret;
  1336. }
  1337. static void afe_send_cal_spkr_prot_tx(int port_id)
  1338. {
  1339. union afe_spkr_prot_config afe_spk_config;
  1340. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL ||
  1341. this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL ||
  1342. this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL] == NULL)
  1343. return;
  1344. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  1345. if ((this_afe.prot_cfg.mode != MSM_SPKR_PROT_DISABLED) &&
  1346. (this_afe.vi_tx_port == port_id)) {
  1347. if (this_afe.prot_cfg.mode ==
  1348. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS) {
  1349. afe_spk_config.vi_proc_cfg.operation_mode =
  1350. Q6AFE_MSM_SPKR_CALIBRATION;
  1351. afe_spk_config.vi_proc_cfg.quick_calib_flag =
  1352. this_afe.prot_cfg.quick_calib_flag;
  1353. } else {
  1354. afe_spk_config.vi_proc_cfg.operation_mode =
  1355. Q6AFE_MSM_SPKR_PROCESSING;
  1356. }
  1357. if (this_afe.th_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE)
  1358. afe_spk_config.vi_proc_cfg.operation_mode =
  1359. Q6AFE_MSM_SPKR_FTM_MODE;
  1360. afe_spk_config.vi_proc_cfg.minor_version = 1;
  1361. afe_spk_config.vi_proc_cfg.r0_cali_q24[SP_V2_SPKR_1] =
  1362. (uint32_t) this_afe.prot_cfg.r0[SP_V2_SPKR_1];
  1363. afe_spk_config.vi_proc_cfg.r0_cali_q24[SP_V2_SPKR_2] =
  1364. (uint32_t) this_afe.prot_cfg.r0[SP_V2_SPKR_2];
  1365. afe_spk_config.vi_proc_cfg.t0_cali_q6[SP_V2_SPKR_1] =
  1366. (uint32_t) this_afe.prot_cfg.t0[SP_V2_SPKR_1];
  1367. afe_spk_config.vi_proc_cfg.t0_cali_q6[SP_V2_SPKR_2] =
  1368. (uint32_t) this_afe.prot_cfg.t0[SP_V2_SPKR_2];
  1369. if (this_afe.prot_cfg.mode != MSM_SPKR_PROT_NOT_CALIBRATED) {
  1370. struct asm_spkr_calib_vi_proc_cfg *vi_proc_cfg;
  1371. vi_proc_cfg = &afe_spk_config.vi_proc_cfg;
  1372. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_1] =
  1373. USE_CALIBRATED_R0TO;
  1374. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_2] =
  1375. USE_CALIBRATED_R0TO;
  1376. } else {
  1377. struct asm_spkr_calib_vi_proc_cfg *vi_proc_cfg;
  1378. vi_proc_cfg = &afe_spk_config.vi_proc_cfg;
  1379. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_1] =
  1380. USE_SAFE_R0TO;
  1381. vi_proc_cfg->r0_t0_selection_flag[SP_V2_SPKR_2] =
  1382. USE_SAFE_R0TO;
  1383. }
  1384. if (afe_spk_prot_prepare(port_id, 0,
  1385. AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG_V2,
  1386. &afe_spk_config))
  1387. pr_err("%s: SPKR_CALIB_VI_PROC_CFG failed\n",
  1388. __func__);
  1389. }
  1390. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  1391. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  1392. if ((this_afe.th_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE) &&
  1393. (this_afe.vi_tx_port == port_id)) {
  1394. afe_spk_config.th_vi_ftm_cfg.minor_version = 1;
  1395. afe_spk_config.th_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_1] =
  1396. this_afe.th_ftm_cfg.wait_time[SP_V2_SPKR_1];
  1397. afe_spk_config.th_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_2] =
  1398. this_afe.th_ftm_cfg.wait_time[SP_V2_SPKR_2];
  1399. afe_spk_config.th_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_1] =
  1400. this_afe.th_ftm_cfg.ftm_time[SP_V2_SPKR_1];
  1401. afe_spk_config.th_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_2] =
  1402. this_afe.th_ftm_cfg.ftm_time[SP_V2_SPKR_2];
  1403. if (afe_spk_prot_prepare(port_id, 0,
  1404. AFE_PARAM_ID_SP_V2_TH_VI_FTM_CFG,
  1405. &afe_spk_config))
  1406. pr_err("%s: th vi ftm cfg failed\n", __func__);
  1407. this_afe.th_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  1408. }
  1409. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  1410. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  1411. if ((this_afe.ex_ftm_cfg.mode == MSM_SPKR_PROT_IN_FTM_MODE) &&
  1412. (this_afe.vi_tx_port == port_id)) {
  1413. afe_spk_config.ex_vi_mode_cfg.minor_version = 1;
  1414. afe_spk_config.ex_vi_mode_cfg.operation_mode =
  1415. Q6AFE_MSM_SPKR_FTM_MODE;
  1416. if (afe_spk_prot_prepare(port_id, 0,
  1417. AFE_PARAM_ID_SP_V2_EX_VI_MODE_CFG,
  1418. &afe_spk_config))
  1419. pr_err("%s: ex vi mode cfg failed\n", __func__);
  1420. afe_spk_config.ex_vi_ftm_cfg.minor_version = 1;
  1421. afe_spk_config.ex_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_1] =
  1422. this_afe.ex_ftm_cfg.wait_time[SP_V2_SPKR_1];
  1423. afe_spk_config.ex_vi_ftm_cfg.wait_time_ms[SP_V2_SPKR_2] =
  1424. this_afe.ex_ftm_cfg.wait_time[SP_V2_SPKR_2];
  1425. afe_spk_config.ex_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_1] =
  1426. this_afe.ex_ftm_cfg.ftm_time[SP_V2_SPKR_1];
  1427. afe_spk_config.ex_vi_ftm_cfg.ftm_time_ms[SP_V2_SPKR_2] =
  1428. this_afe.ex_ftm_cfg.ftm_time[SP_V2_SPKR_2];
  1429. if (afe_spk_prot_prepare(port_id, 0,
  1430. AFE_PARAM_ID_SP_V2_EX_VI_FTM_CFG,
  1431. &afe_spk_config))
  1432. pr_err("%s: ex vi ftm cfg failed\n", __func__);
  1433. this_afe.ex_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  1434. }
  1435. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  1436. }
  1437. static void afe_send_cal_spkr_prot_rx(int port_id)
  1438. {
  1439. union afe_spkr_prot_config afe_spk_config;
  1440. union afe_spkr_prot_config afe_spk_limiter_config;
  1441. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL)
  1442. goto done;
  1443. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  1444. if (this_afe.prot_cfg.mode != MSM_SPKR_PROT_DISABLED &&
  1445. (this_afe.vi_rx_port == port_id)) {
  1446. if (this_afe.prot_cfg.mode ==
  1447. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS)
  1448. afe_spk_config.mode_rx_cfg.mode =
  1449. Q6AFE_MSM_SPKR_CALIBRATION;
  1450. else
  1451. afe_spk_config.mode_rx_cfg.mode =
  1452. Q6AFE_MSM_SPKR_PROCESSING;
  1453. afe_spk_config.mode_rx_cfg.minor_version = 1;
  1454. if (afe_spk_prot_prepare(port_id, 0,
  1455. AFE_PARAM_ID_FBSP_MODE_RX_CFG,
  1456. &afe_spk_config))
  1457. pr_err("%s: RX MODE_VI_PROC_CFG failed\n",
  1458. __func__);
  1459. if (afe_spk_config.mode_rx_cfg.mode ==
  1460. Q6AFE_MSM_SPKR_PROCESSING) {
  1461. if (this_afe.prot_cfg.sp_version >=
  1462. AFE_API_VERSION_SUPPORT_SPV3) {
  1463. afe_spk_limiter_config.limiter_th_cfg.
  1464. minor_version = 1;
  1465. afe_spk_limiter_config.limiter_th_cfg.
  1466. lim_thr_per_calib_q27[SP_V2_SPKR_1] =
  1467. this_afe.prot_cfg.limiter_th[SP_V2_SPKR_1];
  1468. afe_spk_limiter_config.limiter_th_cfg.
  1469. lim_thr_per_calib_q27[SP_V2_SPKR_2] =
  1470. this_afe.prot_cfg.limiter_th[SP_V2_SPKR_2];
  1471. if (afe_spk_prot_prepare(port_id, 0,
  1472. AFE_PARAM_ID_SP_RX_LIMITER_TH,
  1473. &afe_spk_limiter_config))
  1474. pr_err("%s: SP_RX_LIMITER_TH failed.\n",
  1475. __func__);
  1476. } else {
  1477. pr_debug("%s: SPv3 failed to apply on AFE API version=%d.\n",
  1478. __func__,
  1479. this_afe.prot_cfg.sp_version);
  1480. }
  1481. }
  1482. }
  1483. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  1484. done:
  1485. return;
  1486. }
  1487. static int afe_send_hw_delay(u16 port_id, u32 rate)
  1488. {
  1489. struct audio_cal_hw_delay_entry delay_entry;
  1490. struct afe_param_id_device_hw_delay_cfg hw_delay;
  1491. struct param_hdr_v3 param_info;
  1492. int ret = -EINVAL;
  1493. pr_debug("%s:\n", __func__);
  1494. memset(&delay_entry, 0, sizeof(delay_entry));
  1495. memset(&param_info, 0, sizeof(param_info));
  1496. delay_entry.sample_rate = rate;
  1497. if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX)
  1498. ret = afe_get_cal_hw_delay(TX_DEVICE, &delay_entry);
  1499. else if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_RX)
  1500. ret = afe_get_cal_hw_delay(RX_DEVICE, &delay_entry);
  1501. /*
  1502. * HW delay is only used for IMS calls to sync audio with video
  1503. * It is only needed for devices & sample rates used for IMS video
  1504. * calls. Values are received from ACDB calbration files
  1505. */
  1506. if (ret != 0) {
  1507. pr_debug("%s: debug: HW delay info not available %d\n",
  1508. __func__, ret);
  1509. goto fail_cmd;
  1510. }
  1511. param_info.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  1512. param_info.instance_id = INSTANCE_ID_0;
  1513. param_info.param_id = AFE_PARAM_ID_DEVICE_HW_DELAY;
  1514. param_info.param_size = sizeof(hw_delay);
  1515. hw_delay.delay_in_us = delay_entry.delay_usec;
  1516. hw_delay.device_hw_delay_minor_version =
  1517. AFE_API_VERSION_DEVICE_HW_DELAY;
  1518. ret = q6afe_pack_and_set_param_in_band(port_id,
  1519. q6audio_get_port_index(port_id),
  1520. param_info, (u8 *) &hw_delay);
  1521. if (ret)
  1522. pr_err("%s: AFE hw delay for port 0x%x failed %d\n",
  1523. __func__, port_id, ret);
  1524. fail_cmd:
  1525. pr_debug("%s: port_id 0x%x rate %u delay_usec %d status %d\n",
  1526. __func__, port_id, rate, delay_entry.delay_usec, ret);
  1527. return ret;
  1528. }
  1529. static struct cal_block_data *afe_find_cal_topo_id_by_port(
  1530. struct cal_type_data *cal_type, u16 port_id)
  1531. {
  1532. struct list_head *ptr, *next;
  1533. struct cal_block_data *cal_block = NULL;
  1534. int32_t path;
  1535. struct audio_cal_info_afe_top *afe_top;
  1536. int afe_port_index = q6audio_get_port_index(port_id);
  1537. if (afe_port_index < 0)
  1538. goto err_exit;
  1539. list_for_each_safe(ptr, next,
  1540. &cal_type->cal_blocks) {
  1541. cal_block = list_entry(ptr,
  1542. struct cal_block_data, list);
  1543. /* Skip cal_block if it is already marked stale */
  1544. if (cal_utils_is_cal_stale(cal_block))
  1545. continue;
  1546. path = ((afe_get_port_type(port_id) ==
  1547. MSM_AFE_PORT_TYPE_TX)?(TX_DEVICE):(RX_DEVICE));
  1548. afe_top =
  1549. (struct audio_cal_info_afe_top *)cal_block->cal_info;
  1550. if (afe_top->path == path) {
  1551. if (this_afe.dev_acdb_id[afe_port_index] > 0) {
  1552. if (afe_top->acdb_id ==
  1553. this_afe.dev_acdb_id[afe_port_index]) {
  1554. pr_debug("%s: top_id:%x acdb_id:%d afe_port_id:%d\n",
  1555. __func__, afe_top->topology,
  1556. afe_top->acdb_id,
  1557. q6audio_get_port_id(port_id));
  1558. return cal_block;
  1559. }
  1560. } else {
  1561. pr_debug("%s: top_id:%x acdb_id:%d afe_port:%d\n",
  1562. __func__, afe_top->topology, afe_top->acdb_id,
  1563. q6audio_get_port_id(port_id));
  1564. return cal_block;
  1565. }
  1566. }
  1567. }
  1568. err_exit:
  1569. return NULL;
  1570. }
  1571. /*
  1572. * Retrieving cal_block will mark cal_block as stale.
  1573. * Hence it cannot be reused or resent unless the flag
  1574. * is reset.
  1575. */
  1576. static int afe_get_cal_topology_id(u16 port_id, u32 *topology_id,
  1577. int cal_type_index)
  1578. {
  1579. int ret = 0;
  1580. struct cal_block_data *cal_block = NULL;
  1581. struct audio_cal_info_afe_top *afe_top_info = NULL;
  1582. if (this_afe.cal_data[cal_type_index] == NULL) {
  1583. pr_err("%s: cal_type %d not initialized\n", __func__,
  1584. cal_type_index);
  1585. return -EINVAL;
  1586. }
  1587. if (topology_id == NULL) {
  1588. pr_err("%s: topology_id is NULL\n", __func__);
  1589. return -EINVAL;
  1590. }
  1591. *topology_id = 0;
  1592. mutex_lock(&this_afe.cal_data[cal_type_index]->lock);
  1593. cal_block = afe_find_cal_topo_id_by_port(
  1594. this_afe.cal_data[cal_type_index], port_id);
  1595. if (cal_block == NULL) {
  1596. pr_err("%s: cal_type %d not initialized for this port %d\n",
  1597. __func__, cal_type_index, port_id);
  1598. ret = -EINVAL;
  1599. goto unlock;
  1600. }
  1601. afe_top_info = ((struct audio_cal_info_afe_top *)
  1602. cal_block->cal_info);
  1603. if (!afe_top_info->topology) {
  1604. pr_err("%s: invalid topology id : [%d, %d]\n",
  1605. __func__, afe_top_info->acdb_id, afe_top_info->topology);
  1606. ret = -EINVAL;
  1607. goto unlock;
  1608. }
  1609. *topology_id = (u32)afe_top_info->topology;
  1610. cal_utils_mark_cal_used(cal_block);
  1611. pr_debug("%s: port_id = %u acdb_id = %d topology_id = %u ret=%d\n",
  1612. __func__, port_id, afe_top_info->acdb_id,
  1613. afe_top_info->topology, ret);
  1614. unlock:
  1615. mutex_unlock(&this_afe.cal_data[cal_type_index]->lock);
  1616. return ret;
  1617. }
  1618. static int afe_send_port_topology_id(u16 port_id)
  1619. {
  1620. struct afe_param_id_set_topology_cfg topology;
  1621. struct param_hdr_v3 param_info;
  1622. u32 topology_id = 0;
  1623. int index = 0;
  1624. int ret = 0;
  1625. memset(&topology, 0, sizeof(topology));
  1626. memset(&param_info, 0, sizeof(param_info));
  1627. index = q6audio_get_port_index(port_id);
  1628. if (index < 0 || index >= AFE_MAX_PORTS) {
  1629. pr_err("%s: AFE port index[%d] invalid!\n",
  1630. __func__, index);
  1631. return -EINVAL;
  1632. }
  1633. ret = afe_get_cal_topology_id(port_id, &topology_id, AFE_TOPOLOGY_CAL);
  1634. if (ret < 0) {
  1635. pr_debug("%s: Check for LSM topology\n", __func__);
  1636. ret = afe_get_cal_topology_id(port_id, &topology_id,
  1637. AFE_LSM_TOPOLOGY_CAL);
  1638. }
  1639. if (ret || !topology_id) {
  1640. pr_debug("%s: AFE port[%d] get_cal_topology[%d] invalid!\n",
  1641. __func__, port_id, topology_id);
  1642. goto done;
  1643. }
  1644. param_info.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  1645. param_info.instance_id = INSTANCE_ID_0;
  1646. param_info.param_id = AFE_PARAM_ID_SET_TOPOLOGY;
  1647. param_info.param_size = sizeof(topology);
  1648. topology.minor_version = AFE_API_VERSION_TOPOLOGY_V1;
  1649. topology.topology_id = topology_id;
  1650. ret = q6afe_pack_and_set_param_in_band(port_id,
  1651. q6audio_get_port_index(port_id),
  1652. param_info, (u8 *) &topology);
  1653. if (ret) {
  1654. pr_err("%s: AFE set topology id enable for port 0x%x failed %d\n",
  1655. __func__, port_id, ret);
  1656. goto done;
  1657. }
  1658. this_afe.topology[index] = topology_id;
  1659. rtac_update_afe_topology(port_id);
  1660. done:
  1661. pr_debug("%s: AFE set topology id 0x%x enable for port 0x%x ret %d\n",
  1662. __func__, topology_id, port_id, ret);
  1663. return ret;
  1664. }
  1665. static int remap_cal_data(struct cal_block_data *cal_block, int cal_index)
  1666. {
  1667. int ret = 0;
  1668. if (cal_block->map_data.dma_buf == NULL) {
  1669. pr_err("%s: No ION allocation for cal index %d!\n",
  1670. __func__, cal_index);
  1671. ret = -EINVAL;
  1672. goto done;
  1673. }
  1674. if ((cal_block->map_data.map_size > 0) &&
  1675. (cal_block->map_data.q6map_handle == 0)) {
  1676. atomic_set(&this_afe.mem_map_cal_index, cal_index);
  1677. ret = afe_cmd_memory_map(cal_block->cal_data.paddr,
  1678. cal_block->map_data.map_size);
  1679. atomic_set(&this_afe.mem_map_cal_index, -1);
  1680. if (ret < 0) {
  1681. pr_err("%s: mmap did not work! size = %zd ret %d\n",
  1682. __func__,
  1683. cal_block->map_data.map_size, ret);
  1684. pr_debug("%s: mmap did not work! addr = 0x%pK, size = %zd\n",
  1685. __func__,
  1686. &cal_block->cal_data.paddr,
  1687. cal_block->map_data.map_size);
  1688. goto done;
  1689. }
  1690. cal_block->map_data.q6map_handle = atomic_read(&this_afe.
  1691. mem_map_cal_handles[cal_index]);
  1692. }
  1693. done:
  1694. return ret;
  1695. }
  1696. static struct cal_block_data *afe_find_cal(int cal_index, int port_id)
  1697. {
  1698. struct list_head *ptr, *next;
  1699. struct cal_block_data *cal_block = NULL;
  1700. struct audio_cal_info_afe *afe_cal_info = NULL;
  1701. int afe_port_index = q6audio_get_port_index(port_id);
  1702. pr_debug("%s: cal_index %d port_id %d port_index %d\n", __func__,
  1703. cal_index, port_id, afe_port_index);
  1704. if (afe_port_index < 0) {
  1705. pr_err("%s: Error getting AFE port index %d\n",
  1706. __func__, afe_port_index);
  1707. goto exit;
  1708. }
  1709. list_for_each_safe(ptr, next,
  1710. &this_afe.cal_data[cal_index]->cal_blocks) {
  1711. cal_block = list_entry(ptr, struct cal_block_data, list);
  1712. afe_cal_info = cal_block->cal_info;
  1713. if ((afe_cal_info->acdb_id ==
  1714. this_afe.dev_acdb_id[afe_port_index]) &&
  1715. (afe_cal_info->sample_rate ==
  1716. this_afe.afe_sample_rates[afe_port_index])) {
  1717. pr_debug("%s: cal block is a match, size is %zd\n",
  1718. __func__, cal_block->cal_data.size);
  1719. goto exit;
  1720. }
  1721. }
  1722. pr_err("%s: no matching cal_block found\n", __func__);
  1723. cal_block = NULL;
  1724. exit:
  1725. return cal_block;
  1726. }
  1727. static int send_afe_cal_type(int cal_index, int port_id)
  1728. {
  1729. struct cal_block_data *cal_block = NULL;
  1730. int ret;
  1731. int afe_port_index = q6audio_get_port_index(port_id);
  1732. pr_debug("%s:\n", __func__);
  1733. if (this_afe.cal_data[cal_index] == NULL) {
  1734. pr_warn("%s: cal_index %d not allocated!\n",
  1735. __func__, cal_index);
  1736. ret = -EINVAL;
  1737. goto done;
  1738. }
  1739. if (afe_port_index < 0) {
  1740. pr_err("%s: Error getting AFE port index %d\n",
  1741. __func__, afe_port_index);
  1742. ret = -EINVAL;
  1743. goto done;
  1744. }
  1745. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  1746. if (((cal_index == AFE_COMMON_RX_CAL) ||
  1747. (cal_index == AFE_COMMON_TX_CAL) ||
  1748. (cal_index == AFE_LSM_TX_CAL)) &&
  1749. (this_afe.dev_acdb_id[afe_port_index] > 0))
  1750. cal_block = afe_find_cal(cal_index, port_id);
  1751. else
  1752. cal_block = cal_utils_get_only_cal_block(
  1753. this_afe.cal_data[cal_index]);
  1754. if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
  1755. pr_err("%s cal_block not found!!\n", __func__);
  1756. ret = -EINVAL;
  1757. goto unlock;
  1758. }
  1759. pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
  1760. ret = remap_cal_data(cal_block, cal_index);
  1761. if (ret) {
  1762. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  1763. __func__, cal_index);
  1764. ret = -EINVAL;
  1765. goto unlock;
  1766. }
  1767. ret = afe_send_cal_block(port_id, cal_block);
  1768. if (ret < 0)
  1769. pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d\n",
  1770. __func__, cal_index, port_id, ret);
  1771. cal_utils_mark_cal_used(cal_block);
  1772. unlock:
  1773. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  1774. done:
  1775. return ret;
  1776. }
  1777. void afe_send_cal(u16 port_id)
  1778. {
  1779. int ret;
  1780. pr_debug("%s: port_id=0x%x\n", __func__, port_id);
  1781. if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) {
  1782. afe_send_cal_spkr_prot_tx(port_id);
  1783. ret = send_afe_cal_type(AFE_COMMON_TX_CAL, port_id);
  1784. if (ret < 0)
  1785. send_afe_cal_type(AFE_LSM_TX_CAL, port_id);
  1786. } else if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_RX) {
  1787. send_afe_cal_type(AFE_COMMON_RX_CAL, port_id);
  1788. afe_send_cal_spkr_prot_rx(port_id);
  1789. }
  1790. }
  1791. int afe_turn_onoff_hw_mad(u16 mad_type, u16 enable)
  1792. {
  1793. struct afe_param_hw_mad_ctrl mad_enable_param;
  1794. struct param_hdr_v3 param_info;
  1795. int ret;
  1796. pr_debug("%s: enter\n", __func__);
  1797. memset(&mad_enable_param, 0, sizeof(mad_enable_param));
  1798. memset(&param_info, 0, sizeof(param_info));
  1799. param_info.module_id = AFE_MODULE_HW_MAD;
  1800. param_info.instance_id = INSTANCE_ID_0;
  1801. param_info.param_id = AFE_PARAM_ID_HW_MAD_CTRL;
  1802. param_info.param_size = sizeof(mad_enable_param);
  1803. mad_enable_param.minor_version = 1;
  1804. mad_enable_param.mad_type = mad_type;
  1805. mad_enable_param.mad_enable = enable;
  1806. ret = q6afe_pack_and_set_param_in_band(SLIMBUS_5_TX, IDX_GLOBAL_CFG,
  1807. param_info,
  1808. (u8 *) &mad_enable_param);
  1809. if (ret)
  1810. pr_err("%s: AFE_PARAM_ID_HW_MAD_CTRL failed %d\n", __func__,
  1811. ret);
  1812. return ret;
  1813. }
  1814. static int afe_send_slimbus_slave_cfg(
  1815. struct afe_param_cdc_slimbus_slave_cfg *sb_slave_cfg)
  1816. {
  1817. struct param_hdr_v3 param_hdr;
  1818. int ret;
  1819. pr_debug("%s: enter\n", __func__);
  1820. memset(&param_hdr, 0, sizeof(param_hdr));
  1821. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  1822. param_hdr.instance_id = INSTANCE_ID_0;
  1823. param_hdr.param_id = AFE_PARAM_ID_CDC_SLIMBUS_SLAVE_CFG;
  1824. param_hdr.param_size = sizeof(struct afe_param_cdc_slimbus_slave_cfg);
  1825. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  1826. (u8 *) sb_slave_cfg);
  1827. if (ret)
  1828. pr_err("%s: AFE_PARAM_ID_CDC_SLIMBUS_SLAVE_CFG failed %d\n",
  1829. __func__, ret);
  1830. pr_debug("%s: leave %d\n", __func__, ret);
  1831. return ret;
  1832. }
  1833. static int afe_send_codec_reg_page_config(
  1834. struct afe_param_cdc_reg_page_cfg *cdc_reg_page_cfg)
  1835. {
  1836. struct param_hdr_v3 param_hdr;
  1837. int ret;
  1838. memset(&param_hdr, 0, sizeof(param_hdr));
  1839. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  1840. param_hdr.instance_id = INSTANCE_ID_0;
  1841. param_hdr.param_id = AFE_PARAM_ID_CDC_REG_PAGE_CFG;
  1842. param_hdr.param_size = sizeof(struct afe_param_cdc_reg_page_cfg);
  1843. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  1844. (u8 *) cdc_reg_page_cfg);
  1845. if (ret)
  1846. pr_err("%s: AFE_PARAM_ID_CDC_REG_PAGE_CFG failed %d\n",
  1847. __func__, ret);
  1848. return ret;
  1849. }
  1850. static int afe_send_codec_reg_config(
  1851. struct afe_param_cdc_reg_cfg_data *cdc_reg_cfg)
  1852. {
  1853. u8 *packed_param_data = NULL;
  1854. u32 packed_data_size = 0;
  1855. u32 single_param_size = 0;
  1856. u32 max_data_size = 0;
  1857. u32 max_single_param = 0;
  1858. struct param_hdr_v3 param_hdr;
  1859. int idx = 0;
  1860. int ret = -EINVAL;
  1861. memset(&param_hdr, 0, sizeof(param_hdr));
  1862. max_single_param = sizeof(struct param_hdr_v3) +
  1863. sizeof(struct afe_param_cdc_reg_cfg);
  1864. max_data_size = APR_MAX_BUF - sizeof(struct afe_svc_cmd_set_param_v2);
  1865. packed_param_data = kzalloc(max_data_size, GFP_KERNEL);
  1866. if (!packed_param_data)
  1867. return -ENOMEM;
  1868. /* param_hdr is the same for all params sent, set once at top */
  1869. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  1870. param_hdr.instance_id = INSTANCE_ID_0;
  1871. param_hdr.param_id = AFE_PARAM_ID_CDC_REG_CFG;
  1872. param_hdr.param_size = sizeof(struct afe_param_cdc_reg_cfg);
  1873. while (idx < cdc_reg_cfg->num_registers) {
  1874. memset(packed_param_data, 0, max_data_size);
  1875. packed_data_size = 0;
  1876. single_param_size = 0;
  1877. while (packed_data_size + max_single_param < max_data_size &&
  1878. idx < cdc_reg_cfg->num_registers) {
  1879. ret = q6common_pack_pp_params(
  1880. packed_param_data + packed_data_size,
  1881. &param_hdr, (u8 *) &cdc_reg_cfg->reg_data[idx],
  1882. &single_param_size);
  1883. if (ret) {
  1884. pr_err("%s: Failed to pack parameters with error %d\n",
  1885. __func__, ret);
  1886. goto done;
  1887. }
  1888. packed_data_size += single_param_size;
  1889. idx++;
  1890. }
  1891. ret = q6afe_svc_set_params(IDX_GLOBAL_CFG, NULL,
  1892. packed_param_data, packed_data_size);
  1893. if (ret) {
  1894. pr_err("%s: AFE_PARAM_ID_CDC_REG_CFG failed %d\n",
  1895. __func__, ret);
  1896. break;
  1897. }
  1898. }
  1899. done:
  1900. kfree(packed_param_data);
  1901. return ret;
  1902. }
  1903. static int afe_init_cdc_reg_config(void)
  1904. {
  1905. struct param_hdr_v3 param_hdr;
  1906. int ret;
  1907. pr_debug("%s: enter\n", __func__);
  1908. memset(&param_hdr, 0, sizeof(param_hdr));
  1909. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  1910. param_hdr.instance_id = INSTANCE_ID_0;
  1911. param_hdr.param_id = AFE_PARAM_ID_CDC_REG_CFG_INIT;
  1912. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  1913. NULL);
  1914. if (ret)
  1915. pr_err("%s: AFE_PARAM_ID_CDC_INIT_REG_CFG failed %d\n",
  1916. __func__, ret);
  1917. return ret;
  1918. }
  1919. static int afe_send_slimbus_slave_port_cfg(
  1920. struct afe_param_slimbus_slave_port_cfg *slim_slave_config, u16 port_id)
  1921. {
  1922. struct param_hdr_v3 param_hdr;
  1923. int ret;
  1924. pr_debug("%s: enter, port_id = 0x%x\n", __func__, port_id);
  1925. memset(&param_hdr, 0, sizeof(param_hdr));
  1926. param_hdr.module_id = AFE_MODULE_HW_MAD;
  1927. param_hdr.instance_id = INSTANCE_ID_0;
  1928. param_hdr.reserved = 0;
  1929. param_hdr.param_id = AFE_PARAM_ID_SLIMBUS_SLAVE_PORT_CFG;
  1930. param_hdr.param_size = sizeof(struct afe_param_slimbus_slave_port_cfg);
  1931. ret = q6afe_pack_and_set_param_in_band(port_id,
  1932. q6audio_get_port_index(port_id),
  1933. param_hdr,
  1934. (u8 *) slim_slave_config);
  1935. if (ret)
  1936. pr_err("%s: AFE_PARAM_ID_SLIMBUS_SLAVE_PORT_CFG failed %d\n",
  1937. __func__, ret);
  1938. pr_debug("%s: leave %d\n", __func__, ret);
  1939. return ret;
  1940. }
  1941. static int afe_aanc_port_cfg(void *apr, uint16_t tx_port, uint16_t rx_port)
  1942. {
  1943. struct afe_param_aanc_port_cfg aanc_port_cfg;
  1944. struct param_hdr_v3 param_hdr;
  1945. int ret = 0;
  1946. pr_debug("%s: tx_port 0x%x, rx_port 0x%x\n",
  1947. __func__, tx_port, rx_port);
  1948. pr_debug("%s: AANC sample rate tx rate: %d rx rate %d\n", __func__,
  1949. this_afe.aanc_info.aanc_tx_port_sample_rate,
  1950. this_afe.aanc_info.aanc_rx_port_sample_rate);
  1951. memset(&aanc_port_cfg, 0, sizeof(aanc_port_cfg));
  1952. memset(&param_hdr, 0, sizeof(param_hdr));
  1953. /*
  1954. * If aanc tx sample rate or rx sample rate is zero, skip aanc
  1955. * configuration as AFE resampler will fail for invalid sample
  1956. * rates.
  1957. */
  1958. if (!this_afe.aanc_info.aanc_tx_port_sample_rate ||
  1959. !this_afe.aanc_info.aanc_rx_port_sample_rate) {
  1960. return -EINVAL;
  1961. }
  1962. param_hdr.module_id = AFE_MODULE_AANC;
  1963. param_hdr.instance_id = INSTANCE_ID_0;
  1964. param_hdr.param_id = AFE_PARAM_ID_AANC_PORT_CONFIG;
  1965. param_hdr.param_size = sizeof(struct afe_param_aanc_port_cfg);
  1966. aanc_port_cfg.aanc_port_cfg_minor_version =
  1967. AFE_API_VERSION_AANC_PORT_CONFIG;
  1968. aanc_port_cfg.tx_port_sample_rate =
  1969. this_afe.aanc_info.aanc_tx_port_sample_rate;
  1970. aanc_port_cfg.tx_port_channel_map[0] = AANC_TX_VOICE_MIC;
  1971. aanc_port_cfg.tx_port_channel_map[1] = AANC_TX_NOISE_MIC;
  1972. aanc_port_cfg.tx_port_channel_map[2] = AANC_TX_ERROR_MIC;
  1973. aanc_port_cfg.tx_port_channel_map[3] = AANC_TX_MIC_UNUSED;
  1974. aanc_port_cfg.tx_port_channel_map[4] = AANC_TX_MIC_UNUSED;
  1975. aanc_port_cfg.tx_port_channel_map[5] = AANC_TX_MIC_UNUSED;
  1976. aanc_port_cfg.tx_port_channel_map[6] = AANC_TX_MIC_UNUSED;
  1977. aanc_port_cfg.tx_port_channel_map[7] = AANC_TX_MIC_UNUSED;
  1978. aanc_port_cfg.tx_port_num_channels = 3;
  1979. aanc_port_cfg.rx_path_ref_port_id = rx_port;
  1980. aanc_port_cfg.ref_port_sample_rate =
  1981. this_afe.aanc_info.aanc_rx_port_sample_rate;
  1982. ret = q6afe_pack_and_set_param_in_band(tx_port,
  1983. q6audio_get_port_index(tx_port),
  1984. param_hdr,
  1985. (u8 *) &aanc_port_cfg);
  1986. if (ret)
  1987. pr_err("%s: AFE AANC port config failed for tx_port 0x%x, rx_port 0x%x ret %d\n",
  1988. __func__, tx_port, rx_port, ret);
  1989. return ret;
  1990. }
  1991. static int afe_aanc_mod_enable(void *apr, uint16_t tx_port, uint16_t enable)
  1992. {
  1993. struct afe_mod_enable_param mod_enable;
  1994. struct param_hdr_v3 param_hdr;
  1995. int ret = 0;
  1996. pr_debug("%s: tx_port 0x%x\n", __func__, tx_port);
  1997. memset(&mod_enable, 0, sizeof(mod_enable));
  1998. memset(&param_hdr, 0, sizeof(param_hdr));
  1999. param_hdr.module_id = AFE_MODULE_AANC;
  2000. param_hdr.instance_id = INSTANCE_ID_0;
  2001. param_hdr.param_id = AFE_PARAM_ID_ENABLE;
  2002. param_hdr.param_size = sizeof(struct afe_mod_enable_param);
  2003. mod_enable.enable = enable;
  2004. mod_enable.reserved = 0;
  2005. ret = q6afe_pack_and_set_param_in_band(tx_port,
  2006. q6audio_get_port_index(tx_port),
  2007. param_hdr, (u8 *) &mod_enable);
  2008. if (ret)
  2009. pr_err("%s: AFE AANC enable failed for tx_port 0x%x ret %d\n",
  2010. __func__, tx_port, ret);
  2011. return ret;
  2012. }
  2013. static int afe_send_bank_selection_clip(
  2014. struct afe_param_id_clip_bank_sel *param)
  2015. {
  2016. struct param_hdr_v3 param_hdr;
  2017. int ret;
  2018. if (!param) {
  2019. pr_err("%s: Invalid params", __func__);
  2020. return -EINVAL;
  2021. }
  2022. memset(&param_hdr, 0, sizeof(param_hdr));
  2023. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  2024. param_hdr.instance_id = INSTANCE_ID_0;
  2025. param_hdr.param_id = AFE_PARAM_ID_CLIP_BANK_SEL_CFG;
  2026. param_hdr.param_size = sizeof(struct afe_param_id_clip_bank_sel);
  2027. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  2028. (u8 *) param);
  2029. if (ret)
  2030. pr_err("%s: AFE_PARAM_ID_CLIP_BANK_SEL_CFG failed %d\n",
  2031. __func__, ret);
  2032. return ret;
  2033. }
  2034. int afe_send_aanc_version(
  2035. struct afe_param_id_cdc_aanc_version *version_cfg)
  2036. {
  2037. struct param_hdr_v3 param_hdr;
  2038. int ret;
  2039. pr_debug("%s: enter\n", __func__);
  2040. memset(&param_hdr, 0, sizeof(param_hdr));
  2041. param_hdr.module_id = AFE_MODULE_CDC_DEV_CFG;
  2042. param_hdr.instance_id = INSTANCE_ID_0;
  2043. param_hdr.param_id = AFE_PARAM_ID_CDC_AANC_VERSION;
  2044. param_hdr.param_size = sizeof(struct afe_param_id_cdc_aanc_version);
  2045. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  2046. (u8 *) version_cfg);
  2047. if (ret)
  2048. pr_err("%s: AFE_PARAM_ID_CDC_AANC_VERSION failed %d\n",
  2049. __func__, ret);
  2050. return ret;
  2051. }
  2052. /**
  2053. * afe_port_set_mad_type -
  2054. * to update mad type
  2055. *
  2056. * @port_id: AFE port id number
  2057. * @mad_type: MAD type enum value
  2058. *
  2059. * Returns 0 on success or error on failure.
  2060. */
  2061. int afe_port_set_mad_type(u16 port_id, enum afe_mad_type mad_type)
  2062. {
  2063. int i;
  2064. if (port_id == AFE_PORT_ID_TERTIARY_MI2S_TX ||
  2065. port_id == AFE_PORT_ID_INT3_MI2S_TX) {
  2066. mad_type = MAD_SW_AUDIO;
  2067. return 0;
  2068. }
  2069. i = port_id - SLIMBUS_0_RX;
  2070. if (i < 0 || i >= ARRAY_SIZE(afe_ports_mad_type)) {
  2071. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  2072. return -EINVAL;
  2073. }
  2074. atomic_set(&afe_ports_mad_type[i], mad_type);
  2075. return 0;
  2076. }
  2077. EXPORT_SYMBOL(afe_port_set_mad_type);
  2078. /**
  2079. * afe_port_get_mad_type -
  2080. * to retrieve mad type
  2081. *
  2082. * @port_id: AFE port id number
  2083. *
  2084. * Returns valid enum value on success or MAD_HW_NONE on failure.
  2085. */
  2086. enum afe_mad_type afe_port_get_mad_type(u16 port_id)
  2087. {
  2088. int i;
  2089. if (port_id == AFE_PORT_ID_TERTIARY_MI2S_TX ||
  2090. port_id == AFE_PORT_ID_INT3_MI2S_TX)
  2091. return MAD_SW_AUDIO;
  2092. i = port_id - SLIMBUS_0_RX;
  2093. if (i < 0 || i >= ARRAY_SIZE(afe_ports_mad_type)) {
  2094. pr_debug("%s: Non Slimbus port_id 0x%x\n", __func__, port_id);
  2095. return MAD_HW_NONE;
  2096. }
  2097. return (enum afe_mad_type) atomic_read(&afe_ports_mad_type[i]);
  2098. }
  2099. EXPORT_SYMBOL(afe_port_get_mad_type);
  2100. /**
  2101. * afe_set_config -
  2102. * to configure AFE session with
  2103. * specified configuration for given config type
  2104. *
  2105. * @config_type: config type
  2106. * @config_data: configuration to pass to AFE session
  2107. * @arg: argument used in specific config types
  2108. *
  2109. * Returns 0 on success or error value on port start failure.
  2110. */
  2111. int afe_set_config(enum afe_config_type config_type, void *config_data, int arg)
  2112. {
  2113. int ret;
  2114. pr_debug("%s: enter config_type %d\n", __func__, config_type);
  2115. ret = afe_q6_interface_prepare();
  2116. if (ret) {
  2117. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  2118. return ret;
  2119. }
  2120. switch (config_type) {
  2121. case AFE_SLIMBUS_SLAVE_CONFIG:
  2122. ret = afe_send_slimbus_slave_cfg(config_data);
  2123. if (!ret)
  2124. ret = afe_init_cdc_reg_config();
  2125. else
  2126. pr_err("%s: Sending slimbus slave config failed %d\n",
  2127. __func__, ret);
  2128. break;
  2129. case AFE_CDC_REGISTERS_CONFIG:
  2130. ret = afe_send_codec_reg_config(config_data);
  2131. break;
  2132. case AFE_SLIMBUS_SLAVE_PORT_CONFIG:
  2133. ret = afe_send_slimbus_slave_port_cfg(config_data, arg);
  2134. break;
  2135. case AFE_AANC_VERSION:
  2136. ret = afe_send_aanc_version(config_data);
  2137. break;
  2138. case AFE_CLIP_BANK_SEL:
  2139. ret = afe_send_bank_selection_clip(config_data);
  2140. break;
  2141. case AFE_CDC_CLIP_REGISTERS_CONFIG:
  2142. ret = afe_send_codec_reg_config(config_data);
  2143. break;
  2144. case AFE_CDC_REGISTER_PAGE_CONFIG:
  2145. ret = afe_send_codec_reg_page_config(config_data);
  2146. break;
  2147. default:
  2148. pr_err("%s: unknown configuration type %d",
  2149. __func__, config_type);
  2150. ret = -EINVAL;
  2151. }
  2152. if (!ret)
  2153. set_bit(config_type, &afe_configured_cmd);
  2154. return ret;
  2155. }
  2156. EXPORT_SYMBOL(afe_set_config);
  2157. /*
  2158. * afe_clear_config - If SSR happens ADSP loses AFE configs, let AFE driver know
  2159. * about the state so client driver can wait until AFE is
  2160. * reconfigured.
  2161. */
  2162. void afe_clear_config(enum afe_config_type config)
  2163. {
  2164. clear_bit(config, &afe_configured_cmd);
  2165. }
  2166. EXPORT_SYMBOL(afe_clear_config);
  2167. bool afe_has_config(enum afe_config_type config)
  2168. {
  2169. return !!test_bit(config, &afe_configured_cmd);
  2170. }
  2171. int afe_send_spdif_clk_cfg(struct afe_param_id_spdif_clk_cfg *cfg,
  2172. u16 port_id)
  2173. {
  2174. struct afe_param_id_spdif_clk_cfg clk_cfg;
  2175. struct param_hdr_v3 param_hdr;
  2176. int ret = 0;
  2177. if (!cfg) {
  2178. pr_err("%s: Error, no configuration data\n", __func__);
  2179. return -EINVAL;
  2180. }
  2181. memset(&clk_cfg, 0, sizeof(clk_cfg));
  2182. memset(&param_hdr, 0, sizeof(param_hdr));
  2183. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2184. param_hdr.instance_id = INSTANCE_ID_0;
  2185. param_hdr.param_id = AFE_PARAM_ID_SPDIF_CLK_CONFIG;
  2186. param_hdr.param_size = sizeof(struct afe_param_id_spdif_clk_cfg);
  2187. pr_debug("%s: Minor version = 0x%x clk val = %d clk root = 0x%x port id = 0x%x\n",
  2188. __func__, clk_cfg.clk_cfg_minor_version, clk_cfg.clk_value,
  2189. clk_cfg.clk_root, q6audio_get_port_id(port_id));
  2190. ret = q6afe_pack_and_set_param_in_band(port_id,
  2191. q6audio_get_port_index(port_id),
  2192. param_hdr, (u8 *) &clk_cfg);
  2193. if (ret < 0)
  2194. pr_err("%s: AFE send clock config for port 0x%x failed ret = %d\n",
  2195. __func__, port_id, ret);
  2196. return ret;
  2197. }
  2198. /**
  2199. * afe_send_spdif_ch_status_cfg -
  2200. * to configure AFE session with
  2201. * specified channel status configuration
  2202. *
  2203. * @ch_status_cfg: channel status configutation
  2204. * @port_id: AFE port id number
  2205. *
  2206. * Returns 0 on success or error value on port start failure.
  2207. */
  2208. int afe_send_spdif_ch_status_cfg(struct afe_param_id_spdif_ch_status_cfg
  2209. *ch_status_cfg, u16 port_id)
  2210. {
  2211. struct param_hdr_v3 param_hdr;
  2212. int ret = 0;
  2213. if (!ch_status_cfg)
  2214. pr_err("%s: Error, no configuration data\n", __func__);
  2215. return -EINVAL;
  2216. memset(&param_hdr, 0, sizeof(param_hdr));
  2217. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2218. param_hdr.instance_id = INSTANCE_ID_0;
  2219. param_hdr.param_id = AFE_PARAM_ID_SPDIF_CLK_CONFIG;
  2220. param_hdr.param_size = sizeof(struct afe_param_id_spdif_ch_status_cfg);
  2221. ret = q6afe_pack_and_set_param_in_band(port_id,
  2222. q6audio_get_port_index(port_id),
  2223. param_hdr, (u8 *) ch_status_cfg);
  2224. if (ret < 0)
  2225. pr_err("%s: AFE send channel status for port 0x%x failed ret = %d\n",
  2226. __func__, port_id, ret);
  2227. return ret;
  2228. }
  2229. EXPORT_SYMBOL(afe_send_spdif_ch_status_cfg);
  2230. static int afe_send_cmd_port_start(u16 port_id)
  2231. {
  2232. struct afe_port_cmd_device_start start;
  2233. int ret, index;
  2234. pr_debug("%s: enter\n", __func__);
  2235. index = q6audio_get_port_index(port_id);
  2236. if (index < 0 || index >= AFE_MAX_PORTS) {
  2237. pr_err("%s: AFE port index[%d] invalid!\n",
  2238. __func__, index);
  2239. return -EINVAL;
  2240. }
  2241. ret = q6audio_validate_port(port_id);
  2242. if (ret < 0) {
  2243. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  2244. return -EINVAL;
  2245. }
  2246. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2247. APR_HDR_LEN(APR_HDR_SIZE),
  2248. APR_PKT_VER);
  2249. start.hdr.pkt_size = sizeof(start);
  2250. start.hdr.src_port = 0;
  2251. start.hdr.dest_port = 0;
  2252. start.hdr.token = index;
  2253. start.hdr.opcode = AFE_PORT_CMD_DEVICE_START;
  2254. start.port_id = q6audio_get_port_id(port_id);
  2255. pr_debug("%s: cmd device start opcode[0x%x] port id[0x%x]\n",
  2256. __func__, start.hdr.opcode, start.port_id);
  2257. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  2258. if (ret) {
  2259. pr_err("%s: AFE enable for port 0x%x failed %d\n", __func__,
  2260. port_id, ret);
  2261. } else if (this_afe.task != current) {
  2262. this_afe.task = current;
  2263. pr_debug("task_name = %s pid = %d\n",
  2264. this_afe.task->comm, this_afe.task->pid);
  2265. }
  2266. return ret;
  2267. }
  2268. static int afe_aanc_start(uint16_t tx_port_id, uint16_t rx_port_id)
  2269. {
  2270. int ret;
  2271. pr_debug("%s: Tx port is 0x%x, Rx port is 0x%x\n",
  2272. __func__, tx_port_id, rx_port_id);
  2273. ret = afe_aanc_port_cfg(this_afe.apr, tx_port_id, rx_port_id);
  2274. if (ret) {
  2275. pr_err("%s: Send AANC Port Config failed %d\n",
  2276. __func__, ret);
  2277. goto fail_cmd;
  2278. }
  2279. send_afe_cal_type(AFE_AANC_CAL, tx_port_id);
  2280. fail_cmd:
  2281. return ret;
  2282. }
  2283. /**
  2284. * afe_spdif_port_start - to configure AFE session with
  2285. * specified port configuration
  2286. *
  2287. * @port_id: AFE port id number
  2288. * @spdif_port: spdif port configutation
  2289. * @rate: sampling rate of port
  2290. *
  2291. * Returns 0 on success or error value on port start failure.
  2292. */
  2293. int afe_spdif_port_start(u16 port_id, struct afe_spdif_port_config *spdif_port,
  2294. u32 rate)
  2295. {
  2296. struct param_hdr_v3 param_hdr;
  2297. uint16_t port_index;
  2298. int ret = 0;
  2299. if (!spdif_port) {
  2300. pr_err("%s: Error, no configuration data\n", __func__);
  2301. ret = -EINVAL;
  2302. return ret;
  2303. }
  2304. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  2305. memset(&param_hdr, 0, sizeof(param_hdr));
  2306. ret = q6audio_validate_port(port_id);
  2307. if (ret < 0) {
  2308. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  2309. return -EINVAL;
  2310. }
  2311. afe_send_cal(port_id);
  2312. afe_send_hw_delay(port_id, rate);
  2313. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2314. param_hdr.instance_id = INSTANCE_ID_0;
  2315. param_hdr.param_id = AFE_PARAM_ID_SPDIF_CONFIG;
  2316. param_hdr.param_size = sizeof(struct afe_spdif_port_config);
  2317. ret = q6afe_pack_and_set_param_in_band(port_id,
  2318. q6audio_get_port_index(port_id),
  2319. param_hdr, (u8 *) spdif_port);
  2320. if (ret) {
  2321. pr_err("%s: AFE enable for port 0x%x failed ret = %d\n",
  2322. __func__, port_id, ret);
  2323. goto fail_cmd;
  2324. }
  2325. port_index = afe_get_port_index(port_id);
  2326. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  2327. this_afe.afe_sample_rates[port_index] = rate;
  2328. } else {
  2329. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  2330. ret = -EINVAL;
  2331. goto fail_cmd;
  2332. }
  2333. ret = afe_send_spdif_ch_status_cfg(&spdif_port->ch_status, port_id);
  2334. if (ret < 0) {
  2335. pr_err("%s: afe send failed %d\n", __func__, ret);
  2336. goto fail_cmd;
  2337. }
  2338. return afe_send_cmd_port_start(port_id);
  2339. fail_cmd:
  2340. return ret;
  2341. }
  2342. EXPORT_SYMBOL(afe_spdif_port_start);
  2343. int afe_send_slot_mapping_cfg(
  2344. struct afe_param_id_slot_mapping_cfg *slot_mapping_cfg,
  2345. u16 port_id)
  2346. {
  2347. struct param_hdr_v3 param_hdr;
  2348. int ret = 0;
  2349. if (!slot_mapping_cfg) {
  2350. pr_err("%s: Error, no configuration data\n", __func__);
  2351. return -EINVAL;
  2352. }
  2353. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  2354. memset(&param_hdr, 0, sizeof(param_hdr));
  2355. param_hdr.module_id = AFE_MODULE_TDM;
  2356. param_hdr.instance_id = INSTANCE_ID_0;
  2357. param_hdr.param_id = AFE_PARAM_ID_PORT_SLOT_MAPPING_CONFIG;
  2358. param_hdr.param_size = sizeof(struct afe_param_id_slot_mapping_cfg);
  2359. ret = q6afe_pack_and_set_param_in_band(port_id,
  2360. q6audio_get_port_index(port_id),
  2361. param_hdr,
  2362. (u8 *) slot_mapping_cfg);
  2363. if (ret < 0)
  2364. pr_err("%s: AFE send slot mapping for port 0x%x failed ret = %d\n",
  2365. __func__, port_id, ret);
  2366. return ret;
  2367. }
  2368. int afe_send_custom_tdm_header_cfg(
  2369. struct afe_param_id_custom_tdm_header_cfg *custom_tdm_header_cfg,
  2370. u16 port_id)
  2371. {
  2372. struct param_hdr_v3 param_hdr;
  2373. int ret = 0;
  2374. if (!custom_tdm_header_cfg) {
  2375. pr_err("%s: Error, no configuration data\n", __func__);
  2376. return -EINVAL;
  2377. }
  2378. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  2379. memset(&param_hdr, 0, sizeof(param_hdr));
  2380. param_hdr.module_id = AFE_MODULE_TDM;
  2381. param_hdr.instance_id = INSTANCE_ID_0;
  2382. param_hdr.param_id = AFE_PARAM_ID_CUSTOM_TDM_HEADER_CONFIG;
  2383. param_hdr.param_size =
  2384. sizeof(struct afe_param_id_custom_tdm_header_cfg);
  2385. ret = q6afe_pack_and_set_param_in_band(port_id,
  2386. q6audio_get_port_index(port_id),
  2387. param_hdr,
  2388. (u8 *) custom_tdm_header_cfg);
  2389. if (ret < 0)
  2390. pr_err("%s: AFE send custom tdm header for port 0x%x failed ret = %d\n",
  2391. __func__, port_id, ret);
  2392. return ret;
  2393. }
  2394. /**
  2395. * afe_tdm_port_start - to configure AFE session with
  2396. * specified port configuration
  2397. *
  2398. * @port_id: AFE port id number
  2399. * @tdm_port: TDM port configutation
  2400. * @rate: sampling rate of port
  2401. * @num_groups: number of TDM groups
  2402. *
  2403. * Returns 0 on success or error value on port start failure.
  2404. */
  2405. int afe_tdm_port_start(u16 port_id, struct afe_tdm_port_config *tdm_port,
  2406. u32 rate, u16 num_groups)
  2407. {
  2408. struct param_hdr_v3 param_hdr;
  2409. int index = 0;
  2410. uint16_t port_index = 0;
  2411. enum afe_mad_type mad_type = MAD_HW_NONE;
  2412. int ret = 0;
  2413. if (!tdm_port) {
  2414. pr_err("%s: Error, no configuration data\n", __func__);
  2415. return -EINVAL;
  2416. }
  2417. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  2418. memset(&param_hdr, 0, sizeof(param_hdr));
  2419. index = q6audio_get_port_index(port_id);
  2420. if (index < 0 || index >= AFE_MAX_PORTS) {
  2421. pr_err("%s: AFE port index[%d] invalid!\n",
  2422. __func__, index);
  2423. return -EINVAL;
  2424. }
  2425. ret = q6audio_validate_port(port_id);
  2426. if (ret < 0) {
  2427. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  2428. return -EINVAL;
  2429. }
  2430. ret = afe_q6_interface_prepare();
  2431. if (ret != 0) {
  2432. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  2433. return ret;
  2434. }
  2435. if ((index >= 0) && (index < AFE_MAX_PORTS)) {
  2436. this_afe.afe_sample_rates[index] = rate;
  2437. if (this_afe.rt_cb)
  2438. this_afe.dev_acdb_id[index] = this_afe.rt_cb(port_id);
  2439. }
  2440. /* Also send the topology id here: */
  2441. port_index = afe_get_port_index(port_id);
  2442. if (!(this_afe.afe_cal_mode[port_index] == AFE_CAL_MODE_NONE)) {
  2443. /* One time call: only for first time */
  2444. afe_send_custom_topology();
  2445. afe_send_port_topology_id(port_id);
  2446. afe_send_cal(port_id);
  2447. afe_send_hw_delay(port_id, rate);
  2448. }
  2449. /* Start SW MAD module */
  2450. mad_type = afe_port_get_mad_type(port_id);
  2451. pr_debug("%s: port_id 0x%x, mad_type %d\n", __func__, port_id,
  2452. mad_type);
  2453. if (mad_type != MAD_HW_NONE && mad_type != MAD_SW_AUDIO) {
  2454. if (!afe_has_config(AFE_CDC_REGISTERS_CONFIG) ||
  2455. !afe_has_config(AFE_SLIMBUS_SLAVE_CONFIG)) {
  2456. pr_err("%s: AFE isn't configured yet for\n"
  2457. "HW MAD try Again\n", __func__);
  2458. ret = -EAGAIN;
  2459. goto fail_cmd;
  2460. }
  2461. ret = afe_turn_onoff_hw_mad(mad_type, true);
  2462. if (ret) {
  2463. pr_err("%s: afe_turn_onoff_hw_mad failed %d\n",
  2464. __func__, ret);
  2465. goto fail_cmd;
  2466. }
  2467. }
  2468. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2469. param_hdr.instance_id = INSTANCE_ID_0;
  2470. param_hdr.param_id = AFE_PARAM_ID_TDM_CONFIG;
  2471. param_hdr.param_size = sizeof(struct afe_param_id_tdm_cfg);
  2472. ret = q6afe_pack_and_set_param_in_band(port_id,
  2473. q6audio_get_port_index(port_id),
  2474. param_hdr,
  2475. (u8 *) &tdm_port->tdm);
  2476. if (ret) {
  2477. pr_err("%s: AFE enable for port 0x%x failed ret = %d\n",
  2478. __func__, port_id, ret);
  2479. goto fail_cmd;
  2480. }
  2481. port_index = afe_get_port_index(port_id);
  2482. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  2483. this_afe.afe_sample_rates[port_index] = rate;
  2484. } else {
  2485. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  2486. ret = -EINVAL;
  2487. goto fail_cmd;
  2488. }
  2489. ret = afe_send_slot_mapping_cfg(&tdm_port->slot_mapping,
  2490. port_id);
  2491. if (ret < 0) {
  2492. pr_err("%s: afe send failed %d\n", __func__, ret);
  2493. goto fail_cmd;
  2494. }
  2495. if (tdm_port->custom_tdm_header.header_type) {
  2496. ret = afe_send_custom_tdm_header_cfg(
  2497. &tdm_port->custom_tdm_header, port_id);
  2498. if (ret < 0) {
  2499. pr_err("%s: afe send failed %d\n", __func__, ret);
  2500. goto fail_cmd;
  2501. }
  2502. }
  2503. ret = afe_send_cmd_port_start(port_id);
  2504. fail_cmd:
  2505. return ret;
  2506. }
  2507. EXPORT_SYMBOL(afe_tdm_port_start);
  2508. /**
  2509. * afe_set_cal_mode -
  2510. * set cal mode for AFE calibration
  2511. *
  2512. * @port_id: AFE port id number
  2513. * @afe_cal_mode: AFE calib mode
  2514. *
  2515. */
  2516. void afe_set_cal_mode(u16 port_id, enum afe_cal_mode afe_cal_mode)
  2517. {
  2518. uint16_t port_index;
  2519. port_index = afe_get_port_index(port_id);
  2520. this_afe.afe_cal_mode[port_index] = afe_cal_mode;
  2521. }
  2522. EXPORT_SYMBOL(afe_set_cal_mode);
  2523. /**
  2524. * afe_set_routing_callback -
  2525. * Update callback function for routing
  2526. *
  2527. * @cb: callback function to update with
  2528. *
  2529. */
  2530. void afe_set_routing_callback(routing_cb cb)
  2531. {
  2532. this_afe.rt_cb = cb;
  2533. }
  2534. EXPORT_SYMBOL(afe_set_routing_callback);
  2535. int afe_port_send_usb_dev_param(u16 port_id, union afe_port_config *afe_config)
  2536. {
  2537. struct afe_param_id_usb_audio_dev_params usb_dev;
  2538. struct afe_param_id_usb_audio_dev_lpcm_fmt lpcm_fmt;
  2539. struct afe_param_id_usb_audio_svc_interval svc_int;
  2540. struct param_hdr_v3 param_hdr;
  2541. int ret = 0, index = 0;
  2542. if (!afe_config) {
  2543. pr_err("%s: Error, no configuration data\n", __func__);
  2544. ret = -EINVAL;
  2545. goto exit;
  2546. }
  2547. index = q6audio_get_port_index(port_id);
  2548. if (index < 0 || index >= AFE_MAX_PORTS) {
  2549. pr_err("%s: AFE port index[%d] invalid!\n",
  2550. __func__, index);
  2551. return -EINVAL;
  2552. }
  2553. memset(&usb_dev, 0, sizeof(usb_dev));
  2554. memset(&lpcm_fmt, 0, sizeof(lpcm_fmt));
  2555. memset(&param_hdr, 0, sizeof(param_hdr));
  2556. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2557. param_hdr.instance_id = INSTANCE_ID_0;
  2558. param_hdr.param_id = AFE_PARAM_ID_USB_AUDIO_DEV_PARAMS;
  2559. param_hdr.param_size = sizeof(usb_dev);
  2560. usb_dev.cfg_minor_version = AFE_API_MINOR_VERSION_USB_AUDIO_CONFIG;
  2561. usb_dev.dev_token = afe_config->usb_audio.dev_token;
  2562. ret = q6afe_pack_and_set_param_in_band(port_id,
  2563. q6audio_get_port_index(port_id),
  2564. param_hdr, (u8 *) &usb_dev);
  2565. if (ret) {
  2566. pr_err("%s: AFE device param cmd failed %d\n",
  2567. __func__, ret);
  2568. goto exit;
  2569. }
  2570. param_hdr.param_id = AFE_PARAM_ID_USB_AUDIO_DEV_LPCM_FMT;
  2571. param_hdr.param_size = sizeof(lpcm_fmt);
  2572. lpcm_fmt.cfg_minor_version = AFE_API_MINOR_VERSION_USB_AUDIO_CONFIG;
  2573. lpcm_fmt.endian = afe_config->usb_audio.endian;
  2574. ret = q6afe_pack_and_set_param_in_band(port_id,
  2575. q6audio_get_port_index(port_id),
  2576. param_hdr, (u8 *) &lpcm_fmt);
  2577. if (ret) {
  2578. pr_err("%s: AFE device param cmd LPCM_FMT failed %d\n",
  2579. __func__, ret);
  2580. goto exit;
  2581. }
  2582. param_hdr.param_id = AFE_PARAM_ID_USB_AUDIO_SVC_INTERVAL;
  2583. param_hdr.param_size = sizeof(svc_int);
  2584. svc_int.cfg_minor_version =
  2585. AFE_API_MINOR_VERSION_USB_AUDIO_CONFIG;
  2586. svc_int.svc_interval = afe_config->usb_audio.service_interval;
  2587. pr_debug("%s: AFE device param cmd sending SVC_INTERVAL %d\n",
  2588. __func__, svc_int.svc_interval);
  2589. ret = q6afe_pack_and_set_param_in_band(port_id,
  2590. q6audio_get_port_index(port_id),
  2591. param_hdr, (u8 *) &svc_int);
  2592. if (ret) {
  2593. pr_err("%s: AFE device param cmd svc_interval failed %d\n",
  2594. __func__, ret);
  2595. ret = -EINVAL;
  2596. goto exit;
  2597. }
  2598. exit:
  2599. return ret;
  2600. }
  2601. static int q6afe_send_dec_config(u16 port_id,
  2602. union afe_port_config afe_config,
  2603. struct afe_dec_config *cfg)
  2604. {
  2605. struct avs_dec_depacketizer_id_param_t dec_depkt_id_param;
  2606. struct afe_enc_dec_imc_info_param_t imc_info_param;
  2607. struct afe_port_media_type_t media_type;
  2608. struct param_hdr_v3 param_hdr;
  2609. int ret;
  2610. memset(&dec_depkt_id_param, 0, sizeof(dec_depkt_id_param));
  2611. memset(&imc_info_param, 0, sizeof(imc_info_param));
  2612. memset(&media_type, 0, sizeof(media_type));
  2613. memset(&param_hdr, 0, sizeof(param_hdr));
  2614. param_hdr.module_id = AFE_MODULE_ID_DECODER;
  2615. param_hdr.instance_id = INSTANCE_ID_0;
  2616. pr_debug("%s: sending AFE_DECODER_PARAM_ID_DEPACKETIZER to DSP payload\n",
  2617. __func__);
  2618. param_hdr.param_id = AFE_DECODER_PARAM_ID_DEPACKETIZER_ID;
  2619. param_hdr.param_size = sizeof(struct avs_dec_depacketizer_id_param_t);
  2620. dec_depkt_id_param.dec_depacketizer_id =
  2621. AFE_MODULE_ID_DEPACKETIZER_COP;
  2622. ret = q6afe_pack_and_set_param_in_band(port_id,
  2623. q6audio_get_port_index(port_id),
  2624. param_hdr,
  2625. (u8 *) &dec_depkt_id_param);
  2626. if (ret) {
  2627. pr_err("%s: AFE_DECODER_PARAM_ID_DEPACKETIZER for port 0x%x failed %d\n",
  2628. __func__, port_id, ret);
  2629. goto exit;
  2630. }
  2631. pr_debug("%s:sending AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION to DSP payload\n",
  2632. __func__);
  2633. param_hdr.param_id = AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION;
  2634. param_hdr.param_size = sizeof(struct afe_enc_dec_imc_info_param_t);
  2635. imc_info_param.imc_info = cfg->abr_dec_cfg.imc_info;
  2636. ret = q6afe_pack_and_set_param_in_band(port_id,
  2637. q6audio_get_port_index(port_id),
  2638. param_hdr,
  2639. (u8 *) &imc_info_param);
  2640. if (ret) {
  2641. pr_err("%s: AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION for port 0x%x failed %d\n",
  2642. __func__, port_id, ret);
  2643. goto exit;
  2644. }
  2645. pr_debug("%s:Sending AFE_API_VERSION_PORT_MEDIA_TYPE to DSP", __func__);
  2646. param_hdr.module_id = AFE_MODULE_PORT;
  2647. param_hdr.param_id = AFE_PARAM_ID_PORT_MEDIA_TYPE;
  2648. param_hdr.param_size = sizeof(struct afe_port_media_type_t);
  2649. media_type.minor_version = AFE_API_VERSION_PORT_MEDIA_TYPE;
  2650. media_type.sample_rate = afe_config.slim_sch.sample_rate;
  2651. media_type.bit_width = afe_config.slim_sch.bit_width;
  2652. media_type.num_channels = afe_config.slim_sch.num_channels;
  2653. media_type.data_format = AFE_PORT_DATA_FORMAT_PCM;
  2654. media_type.reserved = 0;
  2655. ret = q6afe_pack_and_set_param_in_band(port_id,
  2656. q6audio_get_port_index(port_id),
  2657. param_hdr, (u8 *) &media_type);
  2658. if (ret) {
  2659. pr_err("%s: AFE_API_VERSION_PORT_MEDIA_TYPE for port 0x%x failed %d\n",
  2660. __func__, port_id, ret);
  2661. goto exit;
  2662. }
  2663. exit:
  2664. return ret;
  2665. }
  2666. static int q6afe_send_enc_config(u16 port_id,
  2667. union afe_enc_config_data *cfg, u32 format,
  2668. union afe_port_config afe_config,
  2669. u16 afe_in_channels, u16 afe_in_bit_width,
  2670. u32 scrambler_mode)
  2671. {
  2672. u32 enc_fmt;
  2673. struct afe_enc_cfg_blk_param_t enc_blk_param;
  2674. struct afe_param_id_aptx_sync_mode sync_mode_param;
  2675. struct avs_enc_packetizer_id_param_t enc_pkt_id_param;
  2676. struct avs_enc_set_scrambler_param_t enc_set_scrambler_param;
  2677. struct afe_enc_level_to_bitrate_map_param_t map_param;
  2678. struct afe_enc_dec_imc_info_param_t imc_info_param;
  2679. struct afe_port_media_type_t media_type;
  2680. struct param_hdr_v3 param_hdr;
  2681. int ret;
  2682. pr_debug("%s:update DSP for enc format = %d\n", __func__, format);
  2683. memset(&enc_blk_param, 0, sizeof(enc_blk_param));
  2684. memset(&sync_mode_param, 0, sizeof(sync_mode_param));
  2685. memset(&enc_pkt_id_param, 0, sizeof(enc_pkt_id_param));
  2686. memset(&enc_set_scrambler_param, 0, sizeof(enc_set_scrambler_param));
  2687. memset(&map_param, 0, sizeof(map_param));
  2688. memset(&imc_info_param, 0, sizeof(imc_info_param));
  2689. memset(&media_type, 0, sizeof(media_type));
  2690. memset(&param_hdr, 0, sizeof(param_hdr));
  2691. if (format != ASM_MEDIA_FMT_SBC && format != ASM_MEDIA_FMT_AAC_V2 &&
  2692. format != ASM_MEDIA_FMT_APTX && format != ASM_MEDIA_FMT_APTX_HD &&
  2693. format != ASM_MEDIA_FMT_CELT && format != ASM_MEDIA_FMT_LDAC) {
  2694. pr_err("%s:Unsuppported format Ignore AFE config\n", __func__);
  2695. return 0;
  2696. }
  2697. param_hdr.module_id = AFE_MODULE_ID_ENCODER;
  2698. param_hdr.instance_id = INSTANCE_ID_0;
  2699. param_hdr.param_id = AFE_ENCODER_PARAM_ID_ENC_FMT_ID;
  2700. param_hdr.param_size = sizeof(enc_fmt);
  2701. enc_fmt = format;
  2702. pr_debug("%s:sending AFE_ENCODER_PARAM_ID_ENC_FMT_ID payload\n",
  2703. __func__);
  2704. ret = q6afe_pack_and_set_param_in_band(port_id,
  2705. q6audio_get_port_index(port_id),
  2706. param_hdr, (u8 *) &enc_fmt);
  2707. if (ret) {
  2708. pr_err("%s:unable to send AFE_ENCODER_PARAM_ID_ENC_FMT_ID",
  2709. __func__);
  2710. goto exit;
  2711. }
  2712. if (format == ASM_MEDIA_FMT_LDAC) {
  2713. param_hdr.param_size = sizeof(struct afe_enc_cfg_blk_param_t)
  2714. - sizeof(struct afe_abr_enc_cfg_t);
  2715. enc_blk_param.enc_cfg_blk_size =
  2716. sizeof(union afe_enc_config_data)
  2717. - sizeof(struct afe_abr_enc_cfg_t);
  2718. } else {
  2719. param_hdr.param_size = sizeof(struct afe_enc_cfg_blk_param_t);
  2720. enc_blk_param.enc_cfg_blk_size =
  2721. sizeof(union afe_enc_config_data);
  2722. }
  2723. pr_debug("%s:send AFE_ENCODER_PARAM_ID_ENC_CFG_BLK to DSP payloadn",
  2724. __func__);
  2725. param_hdr.param_id = AFE_ENCODER_PARAM_ID_ENC_CFG_BLK;
  2726. enc_blk_param.enc_blk_config = *cfg;
  2727. ret = q6afe_pack_and_set_param_in_band(port_id,
  2728. q6audio_get_port_index(port_id),
  2729. param_hdr,
  2730. (u8 *) &enc_blk_param);
  2731. if (ret) {
  2732. pr_err("%s: AFE_ENCODER_PARAM_ID_ENC_CFG_BLK for port 0x%x failed %d\n",
  2733. __func__, port_id, ret);
  2734. goto exit;
  2735. }
  2736. if (format == ASM_MEDIA_FMT_APTX) {
  2737. pr_debug("%s: sending AFE_PARAM_ID_APTX_SYNC_MODE to DSP",
  2738. __func__);
  2739. param_hdr.param_id = AFE_PARAM_ID_APTX_SYNC_MODE;
  2740. param_hdr.param_size =
  2741. sizeof(struct afe_param_id_aptx_sync_mode);
  2742. sync_mode_param.sync_mode =
  2743. enc_blk_param.enc_blk_config.aptx_config.
  2744. aptx_v2_cfg.sync_mode;
  2745. ret = q6afe_pack_and_set_param_in_band(port_id,
  2746. q6audio_get_port_index(port_id),
  2747. param_hdr,
  2748. (u8 *) &sync_mode_param);
  2749. if (ret) {
  2750. pr_err("%s: AFE_PARAM_ID_APTX_SYNC_MODE for port 0x%x failed %d\n",
  2751. __func__, port_id, ret);
  2752. goto exit;
  2753. }
  2754. }
  2755. pr_debug("%s:sending AFE_ENCODER_PARAM_ID_PACKETIZER to DSP\n",
  2756. __func__);
  2757. param_hdr.param_id = AFE_ENCODER_PARAM_ID_PACKETIZER_ID;
  2758. param_hdr.param_size = sizeof(struct avs_enc_packetizer_id_param_t);
  2759. enc_pkt_id_param.enc_packetizer_id = AFE_MODULE_ID_PACKETIZER_COP;
  2760. ret = q6afe_pack_and_set_param_in_band(port_id,
  2761. q6audio_get_port_index(port_id),
  2762. param_hdr,
  2763. (u8 *) &enc_pkt_id_param);
  2764. if (ret) {
  2765. pr_err("%s: AFE_ENCODER_PARAM_ID_PACKETIZER for port 0x%x failed %d\n",
  2766. __func__, port_id, ret);
  2767. goto exit;
  2768. }
  2769. pr_debug("%s:sending AFE_ENCODER_PARAM_ID_ENABLE_SCRAMBLING mode= %d to DSP payload\n",
  2770. __func__, scrambler_mode);
  2771. param_hdr.param_id = AFE_ENCODER_PARAM_ID_ENABLE_SCRAMBLING;
  2772. param_hdr.param_size = sizeof(struct avs_enc_set_scrambler_param_t);
  2773. enc_set_scrambler_param.enable_scrambler = scrambler_mode;
  2774. ret = q6afe_pack_and_set_param_in_band(port_id,
  2775. q6audio_get_port_index(port_id),
  2776. param_hdr,
  2777. (u8 *) &enc_set_scrambler_param);
  2778. if (ret) {
  2779. pr_err("%s: AFE_ENCODER_PARAM_ID_ENABLE_SCRAMBLING for port 0x%x failed %d\n",
  2780. __func__, port_id, ret);
  2781. goto exit;
  2782. }
  2783. if (format == ASM_MEDIA_FMT_LDAC) {
  2784. pr_debug("%s:sending AFE_ENCODER_PARAM_ID_BIT_RATE_LEVEL_MAP to DSP payload",
  2785. __func__);
  2786. param_hdr.param_id = AFE_ENCODER_PARAM_ID_BIT_RATE_LEVEL_MAP;
  2787. param_hdr.param_size =
  2788. sizeof(struct afe_enc_level_to_bitrate_map_param_t);
  2789. map_param.mapping_table =
  2790. cfg->ldac_config.abr_config.mapping_info;
  2791. ret = q6afe_pack_and_set_param_in_band(port_id,
  2792. q6audio_get_port_index(port_id),
  2793. param_hdr,
  2794. (u8 *) &map_param);
  2795. if (ret) {
  2796. pr_err("%s: AFE_ENCODER_PARAM_ID_BIT_RATE_LEVEL_MAP for port 0x%x failed %d\n",
  2797. __func__, port_id, ret);
  2798. goto exit;
  2799. }
  2800. pr_debug("%s: sending AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION to DSP payload",
  2801. __func__);
  2802. param_hdr.param_id =
  2803. AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION;
  2804. param_hdr.param_size =
  2805. sizeof(struct afe_enc_dec_imc_info_param_t);
  2806. imc_info_param.imc_info =
  2807. cfg->ldac_config.abr_config.imc_info;
  2808. ret = q6afe_pack_and_set_param_in_band(port_id,
  2809. q6audio_get_port_index(port_id),
  2810. param_hdr,
  2811. (u8 *) &imc_info_param);
  2812. if (ret) {
  2813. pr_err("%s: AFE_ENCDEC_PARAM_ID_DEC_TO_ENC_COMMUNICATION for port 0x%x failed %d\n",
  2814. __func__, port_id, ret);
  2815. goto exit;
  2816. }
  2817. }
  2818. pr_debug("%s:Sending AFE_API_VERSION_PORT_MEDIA_TYPE to DSP", __func__);
  2819. param_hdr.module_id = AFE_MODULE_PORT;
  2820. param_hdr.param_id = AFE_PARAM_ID_PORT_MEDIA_TYPE;
  2821. param_hdr.param_size = sizeof(struct afe_port_media_type_t);
  2822. media_type.minor_version = AFE_API_VERSION_PORT_MEDIA_TYPE;
  2823. if (format == ASM_MEDIA_FMT_LDAC)
  2824. media_type.sample_rate =
  2825. cfg->ldac_config.custom_config.sample_rate;
  2826. else
  2827. media_type.sample_rate =
  2828. afe_config.slim_sch.sample_rate;
  2829. if (afe_in_bit_width)
  2830. media_type.bit_width = afe_in_bit_width;
  2831. else
  2832. media_type.bit_width = afe_config.slim_sch.bit_width;
  2833. if (afe_in_channels)
  2834. media_type.num_channels = afe_in_channels;
  2835. else
  2836. media_type.num_channels = afe_config.slim_sch.num_channels;
  2837. media_type.data_format = AFE_PORT_DATA_FORMAT_PCM;
  2838. media_type.reserved = 0;
  2839. ret = q6afe_pack_and_set_param_in_band(port_id,
  2840. q6audio_get_port_index(port_id),
  2841. param_hdr, (u8 *) &media_type);
  2842. if (ret) {
  2843. pr_err("%s: AFE_API_VERSION_PORT_MEDIA_TYPE for port 0x%x failed %d\n",
  2844. __func__, port_id, ret);
  2845. goto exit;
  2846. }
  2847. exit:
  2848. return ret;
  2849. }
  2850. static int __afe_port_start(u16 port_id, union afe_port_config *afe_config,
  2851. u32 rate, u16 afe_in_channels, u16 afe_in_bit_width,
  2852. union afe_enc_config_data *enc_cfg,
  2853. u32 codec_format, u32 scrambler_mode,
  2854. struct afe_dec_config *dec_cfg)
  2855. {
  2856. union afe_port_config port_cfg;
  2857. struct param_hdr_v3 param_hdr;
  2858. int ret = 0;
  2859. int cfg_type;
  2860. int index = 0;
  2861. enum afe_mad_type mad_type;
  2862. uint16_t port_index;
  2863. memset(&param_hdr, 0, sizeof(param_hdr));
  2864. memset(&port_cfg, 0, sizeof(port_cfg));
  2865. if (!afe_config) {
  2866. pr_err("%s: Error, no configuration data\n", __func__);
  2867. ret = -EINVAL;
  2868. return ret;
  2869. }
  2870. if ((port_id == RT_PROXY_DAI_001_RX) ||
  2871. (port_id == RT_PROXY_DAI_002_TX)) {
  2872. pr_debug("%s: before incrementing pcm_afe_instance %d port_id 0x%x\n",
  2873. __func__,
  2874. pcm_afe_instance[port_id & 0x1], port_id);
  2875. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  2876. pcm_afe_instance[port_id & 0x1]++;
  2877. return 0;
  2878. }
  2879. if ((port_id == RT_PROXY_DAI_002_RX) ||
  2880. (port_id == RT_PROXY_DAI_001_TX)) {
  2881. pr_debug("%s: before incrementing proxy_afe_instance %d port_id 0x%x\n",
  2882. __func__,
  2883. proxy_afe_instance[port_id & 0x1], port_id);
  2884. if (!afe_close_done[port_id & 0x1]) {
  2885. /*close pcm dai corresponding to the proxy dai*/
  2886. afe_close(port_id - 0x10);
  2887. pcm_afe_instance[port_id & 0x1]++;
  2888. pr_debug("%s: reconfigure afe port again\n", __func__);
  2889. }
  2890. proxy_afe_instance[port_id & 0x1]++;
  2891. afe_close_done[port_id & 0x1] = false;
  2892. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  2893. }
  2894. pr_debug("%s: port id: 0x%x\n", __func__, port_id);
  2895. index = q6audio_get_port_index(port_id);
  2896. if (index < 0 || index >= AFE_MAX_PORTS) {
  2897. pr_err("%s: AFE port index[%d] invalid!\n",
  2898. __func__, index);
  2899. return -EINVAL;
  2900. }
  2901. ret = q6audio_validate_port(port_id);
  2902. if (ret < 0) {
  2903. pr_err("%s: port id: 0x%x ret %d\n", __func__, port_id, ret);
  2904. return -EINVAL;
  2905. }
  2906. ret = afe_q6_interface_prepare();
  2907. if (ret != 0) {
  2908. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  2909. return ret;
  2910. }
  2911. if ((index >= 0) && (index < AFE_MAX_PORTS)) {
  2912. this_afe.afe_sample_rates[index] = rate;
  2913. if (this_afe.rt_cb)
  2914. this_afe.dev_acdb_id[index] = this_afe.rt_cb(port_id);
  2915. }
  2916. mutex_lock(&this_afe.afe_cmd_lock);
  2917. /* Also send the topology id here: */
  2918. port_index = afe_get_port_index(port_id);
  2919. if (!(this_afe.afe_cal_mode[port_index] == AFE_CAL_MODE_NONE)) {
  2920. /* One time call: only for first time */
  2921. afe_send_custom_topology();
  2922. afe_send_port_topology_id(port_id);
  2923. afe_send_cal(port_id);
  2924. afe_send_hw_delay(port_id, rate);
  2925. }
  2926. /* Start SW MAD module */
  2927. mad_type = afe_port_get_mad_type(port_id);
  2928. pr_debug("%s: port_id 0x%x, mad_type %d\n", __func__, port_id,
  2929. mad_type);
  2930. if (mad_type != MAD_HW_NONE && mad_type != MAD_SW_AUDIO) {
  2931. if (!afe_has_config(AFE_CDC_REGISTERS_CONFIG) ||
  2932. !afe_has_config(AFE_SLIMBUS_SLAVE_CONFIG)) {
  2933. pr_err("%s: AFE isn't configured yet for\n"
  2934. "HW MAD try Again\n", __func__);
  2935. ret = -EAGAIN;
  2936. goto fail_cmd;
  2937. }
  2938. ret = afe_turn_onoff_hw_mad(mad_type, true);
  2939. if (ret) {
  2940. pr_err("%s: afe_turn_onoff_hw_mad failed %d\n",
  2941. __func__, ret);
  2942. goto fail_cmd;
  2943. }
  2944. }
  2945. if ((this_afe.aanc_info.aanc_active) &&
  2946. (this_afe.aanc_info.aanc_tx_port == port_id)) {
  2947. this_afe.aanc_info.aanc_tx_port_sample_rate = rate;
  2948. port_index =
  2949. afe_get_port_index(this_afe.aanc_info.aanc_rx_port);
  2950. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  2951. this_afe.aanc_info.aanc_rx_port_sample_rate =
  2952. this_afe.afe_sample_rates[port_index];
  2953. } else {
  2954. pr_err("%s: Invalid port index %d\n",
  2955. __func__, port_index);
  2956. ret = -EINVAL;
  2957. goto fail_cmd;
  2958. }
  2959. ret = afe_aanc_start(this_afe.aanc_info.aanc_tx_port,
  2960. this_afe.aanc_info.aanc_rx_port);
  2961. pr_debug("%s: afe_aanc_start ret %d\n", __func__, ret);
  2962. }
  2963. if ((port_id == AFE_PORT_ID_USB_RX) ||
  2964. (port_id == AFE_PORT_ID_USB_TX)) {
  2965. ret = afe_port_send_usb_dev_param(port_id, afe_config);
  2966. if (ret) {
  2967. pr_err("%s: AFE device param for port 0x%x failed %d\n",
  2968. __func__, port_id, ret);
  2969. ret = -EINVAL;
  2970. goto fail_cmd;
  2971. }
  2972. }
  2973. switch (port_id) {
  2974. case AFE_PORT_ID_PRIMARY_PCM_RX:
  2975. case AFE_PORT_ID_PRIMARY_PCM_TX:
  2976. case AFE_PORT_ID_SECONDARY_PCM_RX:
  2977. case AFE_PORT_ID_SECONDARY_PCM_TX:
  2978. case AFE_PORT_ID_TERTIARY_PCM_RX:
  2979. case AFE_PORT_ID_TERTIARY_PCM_TX:
  2980. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  2981. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  2982. case AFE_PORT_ID_QUINARY_PCM_RX:
  2983. case AFE_PORT_ID_QUINARY_PCM_TX:
  2984. cfg_type = AFE_PARAM_ID_PCM_CONFIG;
  2985. break;
  2986. case PRIMARY_I2S_RX:
  2987. case PRIMARY_I2S_TX:
  2988. case SECONDARY_I2S_RX:
  2989. case SECONDARY_I2S_TX:
  2990. case MI2S_RX:
  2991. case MI2S_TX:
  2992. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  2993. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  2994. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  2995. case AFE_PORT_ID_SECONDARY_MI2S_RX_SD1:
  2996. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  2997. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  2998. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  2999. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  3000. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  3001. case AFE_PORT_ID_QUINARY_MI2S_RX:
  3002. case AFE_PORT_ID_QUINARY_MI2S_TX:
  3003. case AFE_PORT_ID_SENARY_MI2S_TX:
  3004. case AFE_PORT_ID_INT0_MI2S_RX:
  3005. case AFE_PORT_ID_INT0_MI2S_TX:
  3006. case AFE_PORT_ID_INT1_MI2S_RX:
  3007. case AFE_PORT_ID_INT1_MI2S_TX:
  3008. case AFE_PORT_ID_INT2_MI2S_RX:
  3009. case AFE_PORT_ID_INT2_MI2S_TX:
  3010. case AFE_PORT_ID_INT3_MI2S_RX:
  3011. case AFE_PORT_ID_INT3_MI2S_TX:
  3012. case AFE_PORT_ID_INT4_MI2S_RX:
  3013. case AFE_PORT_ID_INT4_MI2S_TX:
  3014. case AFE_PORT_ID_INT5_MI2S_RX:
  3015. case AFE_PORT_ID_INT5_MI2S_TX:
  3016. case AFE_PORT_ID_INT6_MI2S_RX:
  3017. case AFE_PORT_ID_INT6_MI2S_TX:
  3018. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  3019. break;
  3020. case HDMI_RX:
  3021. case DISPLAY_PORT_RX:
  3022. cfg_type = AFE_PARAM_ID_HDMI_CONFIG;
  3023. break;
  3024. case VOICE_PLAYBACK_TX:
  3025. case VOICE2_PLAYBACK_TX:
  3026. case VOICE_RECORD_RX:
  3027. case VOICE_RECORD_TX:
  3028. cfg_type = AFE_PARAM_ID_PSEUDO_PORT_CONFIG;
  3029. break;
  3030. case SLIMBUS_0_RX:
  3031. case SLIMBUS_0_TX:
  3032. case SLIMBUS_1_RX:
  3033. case SLIMBUS_1_TX:
  3034. case SLIMBUS_2_RX:
  3035. case SLIMBUS_2_TX:
  3036. case SLIMBUS_3_RX:
  3037. case SLIMBUS_3_TX:
  3038. case SLIMBUS_4_RX:
  3039. case SLIMBUS_4_TX:
  3040. case SLIMBUS_5_RX:
  3041. case SLIMBUS_5_TX:
  3042. case SLIMBUS_6_RX:
  3043. case SLIMBUS_6_TX:
  3044. case SLIMBUS_7_RX:
  3045. case SLIMBUS_7_TX:
  3046. case SLIMBUS_8_RX:
  3047. case SLIMBUS_8_TX:
  3048. cfg_type = AFE_PARAM_ID_SLIMBUS_CONFIG;
  3049. break;
  3050. case AFE_PORT_ID_USB_RX:
  3051. case AFE_PORT_ID_USB_TX:
  3052. cfg_type = AFE_PARAM_ID_USB_AUDIO_CONFIG;
  3053. break;
  3054. case RT_PROXY_PORT_001_RX:
  3055. case RT_PROXY_PORT_001_TX:
  3056. cfg_type = AFE_PARAM_ID_RT_PROXY_CONFIG;
  3057. break;
  3058. case INT_BT_SCO_RX:
  3059. case INT_BT_A2DP_RX:
  3060. case INT_BT_SCO_TX:
  3061. case INT_FM_RX:
  3062. case INT_FM_TX:
  3063. cfg_type = AFE_PARAM_ID_INTERNAL_BT_FM_CONFIG;
  3064. break;
  3065. default:
  3066. pr_err("%s: Invalid port id 0x%x\n", __func__, port_id);
  3067. ret = -EINVAL;
  3068. goto fail_cmd;
  3069. }
  3070. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  3071. param_hdr.instance_id = INSTANCE_ID_0;
  3072. param_hdr.param_id = cfg_type;
  3073. param_hdr.param_size = sizeof(union afe_port_config);
  3074. port_cfg = *afe_config;
  3075. if (((enc_cfg != NULL) || (dec_cfg != NULL)) &&
  3076. (codec_format != ASM_MEDIA_FMT_NONE) &&
  3077. (cfg_type == AFE_PARAM_ID_SLIMBUS_CONFIG)) {
  3078. port_cfg.slim_sch.data_format =
  3079. AFE_SB_DATA_FORMAT_GENERIC_COMPRESSED;
  3080. }
  3081. ret = q6afe_pack_and_set_param_in_band(port_id,
  3082. q6audio_get_port_index(port_id),
  3083. param_hdr, (u8 *) &port_cfg);
  3084. if (ret) {
  3085. pr_err("%s: AFE enable for port 0x%x failed %d\n",
  3086. __func__, port_id, ret);
  3087. goto fail_cmd;
  3088. }
  3089. if ((codec_format != ASM_MEDIA_FMT_NONE) &&
  3090. (cfg_type == AFE_PARAM_ID_SLIMBUS_CONFIG)) {
  3091. if (enc_cfg != NULL) {
  3092. pr_debug("%s: Found AFE encoder support for SLIMBUS format = %d\n",
  3093. __func__, codec_format);
  3094. ret = q6afe_send_enc_config(port_id, enc_cfg,
  3095. codec_format, *afe_config,
  3096. afe_in_channels,
  3097. afe_in_bit_width,
  3098. scrambler_mode);
  3099. if (ret) {
  3100. pr_err("%s: AFE encoder config for port 0x%x failed %d\n",
  3101. __func__, port_id, ret);
  3102. goto fail_cmd;
  3103. }
  3104. }
  3105. if (dec_cfg != NULL) {
  3106. pr_debug("%s: Found AFE decoder support for SLIMBUS format = %d\n",
  3107. __func__, codec_format);
  3108. ret = q6afe_send_dec_config(port_id, *afe_config,
  3109. dec_cfg);
  3110. if (ret) {
  3111. pr_err("%s: AFE decoder config for port 0x%x failed %d\n",
  3112. __func__, port_id, ret);
  3113. goto fail_cmd;
  3114. }
  3115. }
  3116. }
  3117. port_index = afe_get_port_index(port_id);
  3118. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  3119. /*
  3120. * If afe_port_start() for tx port called before
  3121. * rx port, then aanc rx sample rate is zero. So,
  3122. * AANC state machine in AFE will not get triggered.
  3123. * Make sure to check whether aanc is active during
  3124. * afe_port_start() for rx port and if aanc rx
  3125. * sample rate is zero, call afe_aanc_start to configure
  3126. * aanc with valid sample rates.
  3127. */
  3128. if (this_afe.aanc_info.aanc_active &&
  3129. !this_afe.aanc_info.aanc_rx_port_sample_rate) {
  3130. this_afe.aanc_info.aanc_rx_port_sample_rate =
  3131. this_afe.afe_sample_rates[port_index];
  3132. ret = afe_aanc_start(this_afe.aanc_info.aanc_tx_port,
  3133. this_afe.aanc_info.aanc_rx_port);
  3134. pr_debug("%s: afe_aanc_start ret %d\n", __func__, ret);
  3135. }
  3136. } else {
  3137. pr_err("%s: Invalid port index %d\n", __func__, port_index);
  3138. ret = -EINVAL;
  3139. goto fail_cmd;
  3140. }
  3141. ret = afe_send_cmd_port_start(port_id);
  3142. fail_cmd:
  3143. mutex_unlock(&this_afe.afe_cmd_lock);
  3144. return ret;
  3145. }
  3146. /**
  3147. * afe_port_start - to configure AFE session with
  3148. * specified port configuration
  3149. *
  3150. * @port_id: AFE port id number
  3151. * @afe_config: port configutation
  3152. * @rate: sampling rate of port
  3153. *
  3154. * Returns 0 on success or error value on port start failure.
  3155. */
  3156. int afe_port_start(u16 port_id, union afe_port_config *afe_config,
  3157. u32 rate)
  3158. {
  3159. return __afe_port_start(port_id, afe_config, rate,
  3160. 0, 0, NULL, ASM_MEDIA_FMT_NONE, 0, NULL);
  3161. }
  3162. EXPORT_SYMBOL(afe_port_start);
  3163. /**
  3164. * afe_port_start_v2 - to configure AFE session with
  3165. * specified port configuration and encoder /decoder params
  3166. *
  3167. * @port_id: AFE port id number
  3168. * @afe_config: port configutation
  3169. * @rate: sampling rate of port
  3170. * @enc_cfg: AFE enc configuration information to setup encoder
  3171. * @afe_in_channels: AFE input channel configuration, this needs
  3172. * update only if input channel is differ from AFE output
  3173. * @dec_cfg: AFE dec configuration information to set up decoder
  3174. *
  3175. * Returns 0 on success or error value on port start failure.
  3176. */
  3177. int afe_port_start_v2(u16 port_id, union afe_port_config *afe_config,
  3178. u32 rate, u16 afe_in_channels, u16 afe_in_bit_width,
  3179. struct afe_enc_config *enc_cfg,
  3180. struct afe_dec_config *dec_cfg)
  3181. {
  3182. int ret = 0;
  3183. if (enc_cfg != NULL)
  3184. ret = __afe_port_start(port_id, afe_config, rate,
  3185. afe_in_channels, afe_in_bit_width,
  3186. &enc_cfg->data, enc_cfg->format,
  3187. enc_cfg->scrambler_mode, dec_cfg);
  3188. else if (dec_cfg != NULL)
  3189. ret = __afe_port_start(port_id, afe_config, rate,
  3190. afe_in_channels, afe_in_bit_width,
  3191. NULL, dec_cfg->format, 0, dec_cfg);
  3192. return ret;
  3193. }
  3194. EXPORT_SYMBOL(afe_port_start_v2);
  3195. int afe_get_port_index(u16 port_id)
  3196. {
  3197. switch (port_id) {
  3198. case PRIMARY_I2S_RX: return IDX_PRIMARY_I2S_RX;
  3199. case PRIMARY_I2S_TX: return IDX_PRIMARY_I2S_TX;
  3200. case AFE_PORT_ID_PRIMARY_PCM_RX:
  3201. return IDX_AFE_PORT_ID_PRIMARY_PCM_RX;
  3202. case AFE_PORT_ID_PRIMARY_PCM_TX:
  3203. return IDX_AFE_PORT_ID_PRIMARY_PCM_TX;
  3204. case AFE_PORT_ID_SECONDARY_PCM_RX:
  3205. return IDX_AFE_PORT_ID_SECONDARY_PCM_RX;
  3206. case AFE_PORT_ID_SECONDARY_PCM_TX:
  3207. return IDX_AFE_PORT_ID_SECONDARY_PCM_TX;
  3208. case AFE_PORT_ID_TERTIARY_PCM_RX:
  3209. return IDX_AFE_PORT_ID_TERTIARY_PCM_RX;
  3210. case AFE_PORT_ID_TERTIARY_PCM_TX:
  3211. return IDX_AFE_PORT_ID_TERTIARY_PCM_TX;
  3212. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  3213. return IDX_AFE_PORT_ID_QUATERNARY_PCM_RX;
  3214. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  3215. return IDX_AFE_PORT_ID_QUATERNARY_PCM_TX;
  3216. case AFE_PORT_ID_QUINARY_PCM_RX:
  3217. return IDX_AFE_PORT_ID_QUINARY_PCM_RX;
  3218. case AFE_PORT_ID_QUINARY_PCM_TX:
  3219. return IDX_AFE_PORT_ID_QUINARY_PCM_TX;
  3220. case SECONDARY_I2S_RX: return IDX_SECONDARY_I2S_RX;
  3221. case SECONDARY_I2S_TX: return IDX_SECONDARY_I2S_TX;
  3222. case MI2S_RX: return IDX_MI2S_RX;
  3223. case MI2S_TX: return IDX_MI2S_TX;
  3224. case HDMI_RX: return IDX_HDMI_RX;
  3225. case DISPLAY_PORT_RX: return IDX_DISPLAY_PORT_RX;
  3226. case AFE_PORT_ID_SPDIF_RX: return IDX_SPDIF_RX;
  3227. case RSVD_2: return IDX_RSVD_2;
  3228. case RSVD_3: return IDX_RSVD_3;
  3229. case DIGI_MIC_TX: return IDX_DIGI_MIC_TX;
  3230. case VOICE_RECORD_RX: return IDX_VOICE_RECORD_RX;
  3231. case VOICE_RECORD_TX: return IDX_VOICE_RECORD_TX;
  3232. case VOICE_PLAYBACK_TX: return IDX_VOICE_PLAYBACK_TX;
  3233. case VOICE2_PLAYBACK_TX: return IDX_VOICE2_PLAYBACK_TX;
  3234. case SLIMBUS_0_RX: return IDX_SLIMBUS_0_RX;
  3235. case SLIMBUS_0_TX: return IDX_SLIMBUS_0_TX;
  3236. case SLIMBUS_1_RX: return IDX_SLIMBUS_1_RX;
  3237. case SLIMBUS_1_TX: return IDX_SLIMBUS_1_TX;
  3238. case SLIMBUS_2_RX: return IDX_SLIMBUS_2_RX;
  3239. case SLIMBUS_2_TX: return IDX_SLIMBUS_2_TX;
  3240. case SLIMBUS_3_RX: return IDX_SLIMBUS_3_RX;
  3241. case SLIMBUS_3_TX: return IDX_SLIMBUS_3_TX;
  3242. case INT_BT_SCO_RX: return IDX_INT_BT_SCO_RX;
  3243. case INT_BT_SCO_TX: return IDX_INT_BT_SCO_TX;
  3244. case INT_BT_A2DP_RX: return IDX_INT_BT_A2DP_RX;
  3245. case INT_FM_RX: return IDX_INT_FM_RX;
  3246. case INT_FM_TX: return IDX_INT_FM_TX;
  3247. case RT_PROXY_PORT_001_RX: return IDX_RT_PROXY_PORT_001_RX;
  3248. case RT_PROXY_PORT_001_TX: return IDX_RT_PROXY_PORT_001_TX;
  3249. case SLIMBUS_4_RX: return IDX_SLIMBUS_4_RX;
  3250. case SLIMBUS_4_TX: return IDX_SLIMBUS_4_TX;
  3251. case SLIMBUS_5_RX: return IDX_SLIMBUS_5_RX;
  3252. case SLIMBUS_5_TX: return IDX_SLIMBUS_5_TX;
  3253. case SLIMBUS_6_RX: return IDX_SLIMBUS_6_RX;
  3254. case SLIMBUS_6_TX: return IDX_SLIMBUS_6_TX;
  3255. case SLIMBUS_7_RX: return IDX_SLIMBUS_7_RX;
  3256. case SLIMBUS_7_TX: return IDX_SLIMBUS_7_TX;
  3257. case SLIMBUS_8_RX: return IDX_SLIMBUS_8_RX;
  3258. case SLIMBUS_8_TX: return IDX_SLIMBUS_8_TX;
  3259. case AFE_PORT_ID_USB_RX: return IDX_AFE_PORT_ID_USB_RX;
  3260. case AFE_PORT_ID_USB_TX: return IDX_AFE_PORT_ID_USB_TX;
  3261. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  3262. return IDX_AFE_PORT_ID_PRIMARY_MI2S_RX;
  3263. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  3264. return IDX_AFE_PORT_ID_PRIMARY_MI2S_TX;
  3265. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  3266. return IDX_AFE_PORT_ID_QUATERNARY_MI2S_RX;
  3267. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  3268. return IDX_AFE_PORT_ID_QUATERNARY_MI2S_TX;
  3269. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  3270. return IDX_AFE_PORT_ID_SECONDARY_MI2S_RX;
  3271. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  3272. return IDX_AFE_PORT_ID_SECONDARY_MI2S_TX;
  3273. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  3274. return IDX_AFE_PORT_ID_TERTIARY_MI2S_RX;
  3275. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  3276. return IDX_AFE_PORT_ID_TERTIARY_MI2S_TX;
  3277. case AFE_PORT_ID_SECONDARY_MI2S_RX_SD1:
  3278. return IDX_AFE_PORT_ID_SECONDARY_MI2S_RX_SD1;
  3279. case AFE_PORT_ID_QUINARY_MI2S_RX:
  3280. return IDX_AFE_PORT_ID_QUINARY_MI2S_RX;
  3281. case AFE_PORT_ID_QUINARY_MI2S_TX:
  3282. return IDX_AFE_PORT_ID_QUINARY_MI2S_TX;
  3283. case AFE_PORT_ID_SENARY_MI2S_TX:
  3284. return IDX_AFE_PORT_ID_SENARY_MI2S_TX;
  3285. case AFE_PORT_ID_PRIMARY_TDM_RX:
  3286. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_0;
  3287. case AFE_PORT_ID_PRIMARY_TDM_TX:
  3288. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_0;
  3289. case AFE_PORT_ID_PRIMARY_TDM_RX_1:
  3290. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_1;
  3291. case AFE_PORT_ID_PRIMARY_TDM_TX_1:
  3292. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_1;
  3293. case AFE_PORT_ID_PRIMARY_TDM_RX_2:
  3294. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_2;
  3295. case AFE_PORT_ID_PRIMARY_TDM_TX_2:
  3296. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_2;
  3297. case AFE_PORT_ID_PRIMARY_TDM_RX_3:
  3298. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_3;
  3299. case AFE_PORT_ID_PRIMARY_TDM_TX_3:
  3300. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_3;
  3301. case AFE_PORT_ID_PRIMARY_TDM_RX_4:
  3302. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_4;
  3303. case AFE_PORT_ID_PRIMARY_TDM_TX_4:
  3304. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_4;
  3305. case AFE_PORT_ID_PRIMARY_TDM_RX_5:
  3306. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_5;
  3307. case AFE_PORT_ID_PRIMARY_TDM_TX_5:
  3308. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_5;
  3309. case AFE_PORT_ID_PRIMARY_TDM_RX_6:
  3310. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_6;
  3311. case AFE_PORT_ID_PRIMARY_TDM_TX_6:
  3312. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_6;
  3313. case AFE_PORT_ID_PRIMARY_TDM_RX_7:
  3314. return IDX_AFE_PORT_ID_PRIMARY_TDM_RX_7;
  3315. case AFE_PORT_ID_PRIMARY_TDM_TX_7:
  3316. return IDX_AFE_PORT_ID_PRIMARY_TDM_TX_7;
  3317. case AFE_PORT_ID_SECONDARY_TDM_RX:
  3318. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_0;
  3319. case AFE_PORT_ID_SECONDARY_TDM_TX:
  3320. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_0;
  3321. case AFE_PORT_ID_SECONDARY_TDM_RX_1:
  3322. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_1;
  3323. case AFE_PORT_ID_SECONDARY_TDM_TX_1:
  3324. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_1;
  3325. case AFE_PORT_ID_SECONDARY_TDM_RX_2:
  3326. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_2;
  3327. case AFE_PORT_ID_SECONDARY_TDM_TX_2:
  3328. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_2;
  3329. case AFE_PORT_ID_SECONDARY_TDM_RX_3:
  3330. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_3;
  3331. case AFE_PORT_ID_SECONDARY_TDM_TX_3:
  3332. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_3;
  3333. case AFE_PORT_ID_SECONDARY_TDM_RX_4:
  3334. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_4;
  3335. case AFE_PORT_ID_SECONDARY_TDM_TX_4:
  3336. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_4;
  3337. case AFE_PORT_ID_SECONDARY_TDM_RX_5:
  3338. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_5;
  3339. case AFE_PORT_ID_SECONDARY_TDM_TX_5:
  3340. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_5;
  3341. case AFE_PORT_ID_SECONDARY_TDM_RX_6:
  3342. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_6;
  3343. case AFE_PORT_ID_SECONDARY_TDM_TX_6:
  3344. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_6;
  3345. case AFE_PORT_ID_SECONDARY_TDM_RX_7:
  3346. return IDX_AFE_PORT_ID_SECONDARY_TDM_RX_7;
  3347. case AFE_PORT_ID_SECONDARY_TDM_TX_7:
  3348. return IDX_AFE_PORT_ID_SECONDARY_TDM_TX_7;
  3349. case AFE_PORT_ID_TERTIARY_TDM_RX:
  3350. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_0;
  3351. case AFE_PORT_ID_TERTIARY_TDM_TX:
  3352. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_0;
  3353. case AFE_PORT_ID_TERTIARY_TDM_RX_1:
  3354. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_1;
  3355. case AFE_PORT_ID_TERTIARY_TDM_TX_1:
  3356. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_1;
  3357. case AFE_PORT_ID_TERTIARY_TDM_RX_2:
  3358. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_2;
  3359. case AFE_PORT_ID_TERTIARY_TDM_TX_2:
  3360. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_2;
  3361. case AFE_PORT_ID_TERTIARY_TDM_RX_3:
  3362. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_3;
  3363. case AFE_PORT_ID_TERTIARY_TDM_TX_3:
  3364. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_3;
  3365. case AFE_PORT_ID_TERTIARY_TDM_RX_4:
  3366. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_4;
  3367. case AFE_PORT_ID_TERTIARY_TDM_TX_4:
  3368. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_4;
  3369. case AFE_PORT_ID_TERTIARY_TDM_RX_5:
  3370. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_5;
  3371. case AFE_PORT_ID_TERTIARY_TDM_TX_5:
  3372. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_5;
  3373. case AFE_PORT_ID_TERTIARY_TDM_RX_6:
  3374. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_6;
  3375. case AFE_PORT_ID_TERTIARY_TDM_TX_6:
  3376. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_6;
  3377. case AFE_PORT_ID_TERTIARY_TDM_RX_7:
  3378. return IDX_AFE_PORT_ID_TERTIARY_TDM_RX_7;
  3379. case AFE_PORT_ID_TERTIARY_TDM_TX_7:
  3380. return IDX_AFE_PORT_ID_TERTIARY_TDM_TX_7;
  3381. case AFE_PORT_ID_QUATERNARY_TDM_RX:
  3382. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_0;
  3383. case AFE_PORT_ID_QUATERNARY_TDM_TX:
  3384. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_0;
  3385. case AFE_PORT_ID_QUATERNARY_TDM_RX_1:
  3386. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_1;
  3387. case AFE_PORT_ID_QUATERNARY_TDM_TX_1:
  3388. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_1;
  3389. case AFE_PORT_ID_QUATERNARY_TDM_RX_2:
  3390. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_2;
  3391. case AFE_PORT_ID_QUATERNARY_TDM_TX_2:
  3392. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_2;
  3393. case AFE_PORT_ID_QUATERNARY_TDM_RX_3:
  3394. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_3;
  3395. case AFE_PORT_ID_QUATERNARY_TDM_TX_3:
  3396. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_3;
  3397. case AFE_PORT_ID_QUATERNARY_TDM_RX_4:
  3398. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_4;
  3399. case AFE_PORT_ID_QUATERNARY_TDM_TX_4:
  3400. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_4;
  3401. case AFE_PORT_ID_QUATERNARY_TDM_RX_5:
  3402. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_5;
  3403. case AFE_PORT_ID_QUATERNARY_TDM_TX_5:
  3404. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_5;
  3405. case AFE_PORT_ID_QUATERNARY_TDM_RX_6:
  3406. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_6;
  3407. case AFE_PORT_ID_QUATERNARY_TDM_TX_6:
  3408. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_6;
  3409. case AFE_PORT_ID_QUATERNARY_TDM_RX_7:
  3410. return IDX_AFE_PORT_ID_QUATERNARY_TDM_RX_7;
  3411. case AFE_PORT_ID_QUATERNARY_TDM_TX_7:
  3412. return IDX_AFE_PORT_ID_QUATERNARY_TDM_TX_7;
  3413. case AFE_PORT_ID_QUINARY_TDM_RX:
  3414. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_0;
  3415. case AFE_PORT_ID_QUINARY_TDM_TX:
  3416. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_0;
  3417. case AFE_PORT_ID_QUINARY_TDM_RX_1:
  3418. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_1;
  3419. case AFE_PORT_ID_QUINARY_TDM_TX_1:
  3420. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_1;
  3421. case AFE_PORT_ID_QUINARY_TDM_RX_2:
  3422. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_2;
  3423. case AFE_PORT_ID_QUINARY_TDM_TX_2:
  3424. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_2;
  3425. case AFE_PORT_ID_QUINARY_TDM_RX_3:
  3426. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_3;
  3427. case AFE_PORT_ID_QUINARY_TDM_TX_3:
  3428. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_3;
  3429. case AFE_PORT_ID_QUINARY_TDM_RX_4:
  3430. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_4;
  3431. case AFE_PORT_ID_QUINARY_TDM_TX_4:
  3432. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_4;
  3433. case AFE_PORT_ID_QUINARY_TDM_RX_5:
  3434. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_5;
  3435. case AFE_PORT_ID_QUINARY_TDM_TX_5:
  3436. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_5;
  3437. case AFE_PORT_ID_QUINARY_TDM_RX_6:
  3438. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_6;
  3439. case AFE_PORT_ID_QUINARY_TDM_TX_6:
  3440. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_6;
  3441. case AFE_PORT_ID_QUINARY_TDM_RX_7:
  3442. return IDX_AFE_PORT_ID_QUINARY_TDM_RX_7;
  3443. case AFE_PORT_ID_QUINARY_TDM_TX_7:
  3444. return IDX_AFE_PORT_ID_QUINARY_TDM_TX_7;
  3445. case AFE_PORT_ID_INT0_MI2S_RX:
  3446. return IDX_AFE_PORT_ID_INT0_MI2S_RX;
  3447. case AFE_PORT_ID_INT0_MI2S_TX:
  3448. return IDX_AFE_PORT_ID_INT0_MI2S_TX;
  3449. case AFE_PORT_ID_INT1_MI2S_RX:
  3450. return IDX_AFE_PORT_ID_INT1_MI2S_RX;
  3451. case AFE_PORT_ID_INT1_MI2S_TX:
  3452. return IDX_AFE_PORT_ID_INT1_MI2S_TX;
  3453. case AFE_PORT_ID_INT2_MI2S_RX:
  3454. return IDX_AFE_PORT_ID_INT2_MI2S_RX;
  3455. case AFE_PORT_ID_INT2_MI2S_TX:
  3456. return IDX_AFE_PORT_ID_INT2_MI2S_TX;
  3457. case AFE_PORT_ID_INT3_MI2S_RX:
  3458. return IDX_AFE_PORT_ID_INT3_MI2S_RX;
  3459. case AFE_PORT_ID_INT3_MI2S_TX:
  3460. return IDX_AFE_PORT_ID_INT3_MI2S_TX;
  3461. case AFE_PORT_ID_INT4_MI2S_RX:
  3462. return IDX_AFE_PORT_ID_INT4_MI2S_RX;
  3463. case AFE_PORT_ID_INT4_MI2S_TX:
  3464. return IDX_AFE_PORT_ID_INT4_MI2S_TX;
  3465. case AFE_PORT_ID_INT5_MI2S_RX:
  3466. return IDX_AFE_PORT_ID_INT5_MI2S_RX;
  3467. case AFE_PORT_ID_INT5_MI2S_TX:
  3468. return IDX_AFE_PORT_ID_INT5_MI2S_TX;
  3469. case AFE_PORT_ID_INT6_MI2S_RX:
  3470. return IDX_AFE_PORT_ID_INT6_MI2S_RX;
  3471. case AFE_PORT_ID_INT6_MI2S_TX:
  3472. return IDX_AFE_PORT_ID_INT6_MI2S_TX;
  3473. default:
  3474. pr_err("%s: port 0x%x\n", __func__, port_id);
  3475. return -EINVAL;
  3476. }
  3477. }
  3478. /**
  3479. * afe_open -
  3480. * command to open AFE port
  3481. *
  3482. * @port_id: AFE port id
  3483. * @afe_config: AFE port config to pass
  3484. * @rate: sample rate
  3485. *
  3486. * Returns 0 on success or error on failure
  3487. */
  3488. int afe_open(u16 port_id,
  3489. union afe_port_config *afe_config, int rate)
  3490. {
  3491. struct afe_port_cmd_device_start start;
  3492. union afe_port_config port_cfg;
  3493. struct param_hdr_v3 param_hdr;
  3494. int ret = 0;
  3495. int cfg_type;
  3496. int index = 0;
  3497. memset(&param_hdr, 0, sizeof(param_hdr));
  3498. memset(&start, 0, sizeof(start));
  3499. memset(&port_cfg, 0, sizeof(port_cfg));
  3500. if (!afe_config) {
  3501. pr_err("%s: Error, no configuration data\n", __func__);
  3502. ret = -EINVAL;
  3503. return ret;
  3504. }
  3505. pr_err("%s: port_id 0x%x rate %d\n", __func__, port_id, rate);
  3506. index = q6audio_get_port_index(port_id);
  3507. if (index < 0 || index >= AFE_MAX_PORTS) {
  3508. pr_err("%s: AFE port index[%d] invalid!\n",
  3509. __func__, index);
  3510. return -EINVAL;
  3511. }
  3512. ret = q6audio_validate_port(port_id);
  3513. if (ret < 0) {
  3514. pr_err("%s: Invalid port 0x%x ret %d", __func__, port_id, ret);
  3515. return -EINVAL;
  3516. }
  3517. if ((port_id == RT_PROXY_DAI_001_RX) ||
  3518. (port_id == RT_PROXY_DAI_002_TX)) {
  3519. pr_err("%s: wrong port 0x%x\n", __func__, port_id);
  3520. return -EINVAL;
  3521. }
  3522. if ((port_id == RT_PROXY_DAI_002_RX) ||
  3523. (port_id == RT_PROXY_DAI_001_TX))
  3524. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  3525. ret = afe_q6_interface_prepare();
  3526. if (ret != 0) {
  3527. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  3528. return -EINVAL;
  3529. }
  3530. if ((index >= 0) && (index < AFE_MAX_PORTS)) {
  3531. this_afe.afe_sample_rates[index] = rate;
  3532. if (this_afe.rt_cb)
  3533. this_afe.dev_acdb_id[index] = this_afe.rt_cb(port_id);
  3534. }
  3535. /* Also send the topology id here: */
  3536. afe_send_custom_topology(); /* One time call: only for first time */
  3537. afe_send_port_topology_id(port_id);
  3538. ret = q6audio_validate_port(port_id);
  3539. if (ret < 0) {
  3540. pr_err("%s: Failed : Invalid Port id = 0x%x ret %d\n",
  3541. __func__, port_id, ret);
  3542. return -EINVAL;
  3543. }
  3544. mutex_lock(&this_afe.afe_cmd_lock);
  3545. switch (port_id) {
  3546. case PRIMARY_I2S_RX:
  3547. case PRIMARY_I2S_TX:
  3548. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  3549. break;
  3550. case AFE_PORT_ID_PRIMARY_PCM_RX:
  3551. case AFE_PORT_ID_PRIMARY_PCM_TX:
  3552. case AFE_PORT_ID_SECONDARY_PCM_RX:
  3553. case AFE_PORT_ID_SECONDARY_PCM_TX:
  3554. case AFE_PORT_ID_TERTIARY_PCM_RX:
  3555. case AFE_PORT_ID_TERTIARY_PCM_TX:
  3556. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  3557. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  3558. case AFE_PORT_ID_QUINARY_PCM_RX:
  3559. case AFE_PORT_ID_QUINARY_PCM_TX:
  3560. cfg_type = AFE_PARAM_ID_PCM_CONFIG;
  3561. break;
  3562. case SECONDARY_I2S_RX:
  3563. case SECONDARY_I2S_TX:
  3564. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  3565. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  3566. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  3567. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  3568. case MI2S_RX:
  3569. case MI2S_TX:
  3570. case AFE_PORT_ID_QUINARY_MI2S_RX:
  3571. case AFE_PORT_ID_QUINARY_MI2S_TX:
  3572. case AFE_PORT_ID_SENARY_MI2S_TX:
  3573. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  3574. break;
  3575. case HDMI_RX:
  3576. case DISPLAY_PORT_RX:
  3577. cfg_type = AFE_PARAM_ID_HDMI_CONFIG;
  3578. break;
  3579. case SLIMBUS_0_RX:
  3580. case SLIMBUS_0_TX:
  3581. case SLIMBUS_1_RX:
  3582. case SLIMBUS_1_TX:
  3583. case SLIMBUS_2_RX:
  3584. case SLIMBUS_2_TX:
  3585. case SLIMBUS_3_RX:
  3586. case SLIMBUS_3_TX:
  3587. case SLIMBUS_4_RX:
  3588. case SLIMBUS_4_TX:
  3589. case SLIMBUS_5_RX:
  3590. case SLIMBUS_6_RX:
  3591. case SLIMBUS_6_TX:
  3592. case SLIMBUS_7_RX:
  3593. case SLIMBUS_7_TX:
  3594. case SLIMBUS_8_RX:
  3595. case SLIMBUS_8_TX:
  3596. cfg_type = AFE_PARAM_ID_SLIMBUS_CONFIG;
  3597. break;
  3598. case AFE_PORT_ID_USB_RX:
  3599. case AFE_PORT_ID_USB_TX:
  3600. cfg_type = AFE_PARAM_ID_USB_AUDIO_CONFIG;
  3601. break;
  3602. default:
  3603. pr_err("%s: Invalid port id 0x%x\n",
  3604. __func__, port_id);
  3605. ret = -EINVAL;
  3606. goto fail_cmd;
  3607. }
  3608. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  3609. param_hdr.instance_id = INSTANCE_ID_0;
  3610. param_hdr.param_id = cfg_type;
  3611. param_hdr.param_size = sizeof(union afe_port_config);
  3612. port_cfg = *afe_config;
  3613. ret = q6afe_pack_and_set_param_in_band(port_id,
  3614. q6audio_get_port_index(port_id),
  3615. param_hdr, (u8 *) &port_cfg);
  3616. if (ret) {
  3617. pr_err("%s: AFE enable for port 0x%x opcode[0x%x]failed %d\n",
  3618. __func__, port_id, cfg_type, ret);
  3619. goto fail_cmd;
  3620. }
  3621. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3622. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3623. start.hdr.pkt_size = sizeof(start);
  3624. start.hdr.src_port = 0;
  3625. start.hdr.dest_port = 0;
  3626. start.hdr.token = index;
  3627. start.hdr.opcode = AFE_PORT_CMD_DEVICE_START;
  3628. start.port_id = q6audio_get_port_id(port_id);
  3629. pr_debug("%s: cmd device start opcode[0x%x] port id[0x%x]\n",
  3630. __func__, start.hdr.opcode, start.port_id);
  3631. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  3632. if (ret) {
  3633. pr_err("%s: AFE enable for port 0x%x failed %d\n", __func__,
  3634. port_id, ret);
  3635. goto fail_cmd;
  3636. }
  3637. fail_cmd:
  3638. mutex_unlock(&this_afe.afe_cmd_lock);
  3639. return ret;
  3640. }
  3641. EXPORT_SYMBOL(afe_open);
  3642. /**
  3643. * afe_loopback -
  3644. * command to set loopback between AFE ports
  3645. *
  3646. * @enable: enable or disable loopback
  3647. * @rx_port: AFE RX port ID
  3648. * @tx_port: AFE TX port ID
  3649. *
  3650. * Returns 0 on success or error on failure
  3651. */
  3652. int afe_loopback(u16 enable, u16 rx_port, u16 tx_port)
  3653. {
  3654. struct afe_loopback_cfg_v1 lb_param;
  3655. struct param_hdr_v3 param_hdr;
  3656. int ret = 0;
  3657. memset(&lb_param, 0, sizeof(lb_param));
  3658. memset(&param_hdr, 0, sizeof(param_hdr));
  3659. if (rx_port == MI2S_RX)
  3660. rx_port = AFE_PORT_ID_PRIMARY_MI2S_RX;
  3661. if (tx_port == MI2S_TX)
  3662. tx_port = AFE_PORT_ID_PRIMARY_MI2S_TX;
  3663. param_hdr.module_id = AFE_MODULE_LOOPBACK;
  3664. param_hdr.instance_id = INSTANCE_ID_0;
  3665. param_hdr.param_id = AFE_PARAM_ID_LOOPBACK_CONFIG;
  3666. param_hdr.param_size = sizeof(struct afe_loopback_cfg_v1);
  3667. lb_param.dst_port_id = rx_port;
  3668. lb_param.routing_mode = LB_MODE_DEFAULT;
  3669. lb_param.enable = (enable ? 1 : 0);
  3670. lb_param.loopback_cfg_minor_version = AFE_API_VERSION_LOOPBACK_CONFIG;
  3671. ret = q6afe_pack_and_set_param_in_band(tx_port,
  3672. q6audio_get_port_index(tx_port),
  3673. param_hdr, (u8 *) &lb_param);
  3674. if (ret)
  3675. pr_err("%s: AFE loopback failed %d\n", __func__, ret);
  3676. return ret;
  3677. }
  3678. EXPORT_SYMBOL(afe_loopback);
  3679. /**
  3680. * afe_loopback_gain -
  3681. * command to set gain for AFE loopback
  3682. *
  3683. * @port_id: AFE port id
  3684. * @volume: gain value to set
  3685. *
  3686. * Returns 0 on success or error on failure
  3687. */
  3688. int afe_loopback_gain(u16 port_id, u16 volume)
  3689. {
  3690. struct afe_loopback_gain_per_path_param set_param;
  3691. struct param_hdr_v3 param_hdr;
  3692. int ret = 0;
  3693. memset(&set_param, 0, sizeof(set_param));
  3694. memset(&param_hdr, 0, sizeof(param_hdr));
  3695. if (this_afe.apr == NULL) {
  3696. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  3697. 0xFFFFFFFF, &this_afe);
  3698. pr_debug("%s: Register AFE\n", __func__);
  3699. if (this_afe.apr == NULL) {
  3700. pr_err("%s: Unable to register AFE\n", __func__);
  3701. ret = -ENODEV;
  3702. return ret;
  3703. }
  3704. rtac_set_afe_handle(this_afe.apr);
  3705. }
  3706. ret = q6audio_validate_port(port_id);
  3707. if (ret < 0) {
  3708. pr_err("%s: Failed : Invalid Port id = 0x%x ret %d\n",
  3709. __func__, port_id, ret);
  3710. ret = -EINVAL;
  3711. goto fail_cmd;
  3712. }
  3713. /* RX ports numbers are even .TX ports numbers are odd. */
  3714. if (port_id % 2 == 0) {
  3715. pr_err("%s: Failed : afe loopback gain only for TX ports. port_id %d\n",
  3716. __func__, port_id);
  3717. ret = -EINVAL;
  3718. goto fail_cmd;
  3719. }
  3720. pr_debug("%s: port 0x%x volume %d\n", __func__, port_id, volume);
  3721. param_hdr.module_id = AFE_MODULE_LOOPBACK;
  3722. param_hdr.instance_id = INSTANCE_ID_0;
  3723. param_hdr.param_id = AFE_PARAM_ID_LOOPBACK_GAIN_PER_PATH;
  3724. param_hdr.param_size = sizeof(struct afe_loopback_gain_per_path_param);
  3725. set_param.rx_port_id = port_id;
  3726. set_param.gain = volume;
  3727. ret = q6afe_pack_and_set_param_in_band(port_id,
  3728. q6audio_get_port_index(port_id),
  3729. param_hdr, (u8 *) &set_param);
  3730. if (ret)
  3731. pr_err("%s: AFE param set failed for port 0x%x ret %d\n",
  3732. __func__, port_id, ret);
  3733. fail_cmd:
  3734. return ret;
  3735. }
  3736. EXPORT_SYMBOL(afe_loopback_gain);
  3737. int afe_pseudo_port_start_nowait(u16 port_id)
  3738. {
  3739. struct afe_pseudoport_start_command start;
  3740. int ret = 0;
  3741. pr_debug("%s: port_id=0x%x\n", __func__, port_id);
  3742. if (this_afe.apr == NULL) {
  3743. pr_err("%s: AFE APR is not registered\n", __func__);
  3744. return -ENODEV;
  3745. }
  3746. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3747. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3748. start.hdr.pkt_size = sizeof(start);
  3749. start.hdr.src_port = 0;
  3750. start.hdr.dest_port = 0;
  3751. start.hdr.token = 0;
  3752. start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
  3753. start.port_id = port_id;
  3754. start.timing = 1;
  3755. ret = afe_apr_send_pkt(&start, NULL);
  3756. if (ret) {
  3757. pr_err("%s: AFE enable for port 0x%x failed %d\n",
  3758. __func__, port_id, ret);
  3759. return ret;
  3760. }
  3761. return 0;
  3762. }
  3763. int afe_start_pseudo_port(u16 port_id)
  3764. {
  3765. int ret = 0;
  3766. struct afe_pseudoport_start_command start;
  3767. int index = 0;
  3768. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  3769. ret = afe_q6_interface_prepare();
  3770. if (ret != 0) {
  3771. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  3772. return ret;
  3773. }
  3774. index = q6audio_get_port_index(port_id);
  3775. if (index < 0 || index >= AFE_MAX_PORTS) {
  3776. pr_err("%s: AFE port index[%d] invalid!\n",
  3777. __func__, index);
  3778. return -EINVAL;
  3779. }
  3780. ret = q6audio_validate_port(port_id);
  3781. if (ret < 0) {
  3782. pr_err("%s: Invalid port 0x%x ret %d",
  3783. __func__, port_id, ret);
  3784. return -EINVAL;
  3785. }
  3786. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3787. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3788. start.hdr.pkt_size = sizeof(start);
  3789. start.hdr.src_port = 0;
  3790. start.hdr.dest_port = 0;
  3791. start.hdr.token = 0;
  3792. start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
  3793. start.port_id = port_id;
  3794. start.timing = 1;
  3795. start.hdr.token = index;
  3796. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  3797. if (ret)
  3798. pr_err("%s: AFE enable for port 0x%x failed %d\n",
  3799. __func__, port_id, ret);
  3800. return ret;
  3801. }
  3802. int afe_pseudo_port_stop_nowait(u16 port_id)
  3803. {
  3804. int ret = 0;
  3805. struct afe_pseudoport_stop_command stop;
  3806. int index = 0;
  3807. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  3808. if (this_afe.apr == NULL) {
  3809. pr_err("%s: AFE is already closed\n", __func__);
  3810. return -EINVAL;
  3811. }
  3812. index = q6audio_get_port_index(port_id);
  3813. if (index < 0 || index >= AFE_MAX_PORTS) {
  3814. pr_err("%s: AFE port index[%d] invalid!\n",
  3815. __func__, index);
  3816. return -EINVAL;
  3817. }
  3818. ret = q6audio_validate_port(port_id);
  3819. if (ret < 0) {
  3820. pr_err("%s: Invalid port 0x%x ret %d",
  3821. __func__, port_id, ret);
  3822. return -EINVAL;
  3823. }
  3824. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3825. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3826. stop.hdr.pkt_size = sizeof(stop);
  3827. stop.hdr.src_port = 0;
  3828. stop.hdr.dest_port = 0;
  3829. stop.hdr.token = 0;
  3830. stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
  3831. stop.port_id = port_id;
  3832. stop.reserved = 0;
  3833. stop.hdr.token = index;
  3834. ret = afe_apr_send_pkt(&stop, NULL);
  3835. if (ret)
  3836. pr_err("%s: AFE close failed %d\n", __func__, ret);
  3837. return ret;
  3838. }
  3839. int afe_port_group_set_param(u16 group_id,
  3840. union afe_port_group_config *afe_group_config)
  3841. {
  3842. struct param_hdr_v3 param_hdr;
  3843. int cfg_type;
  3844. int ret;
  3845. if (!afe_group_config) {
  3846. pr_err("%s: Error, no configuration data\n", __func__);
  3847. return -EINVAL;
  3848. }
  3849. pr_debug("%s: group id: 0x%x\n", __func__, group_id);
  3850. memset(&param_hdr, 0, sizeof(param_hdr));
  3851. ret = afe_q6_interface_prepare();
  3852. if (ret != 0) {
  3853. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  3854. return ret;
  3855. }
  3856. switch (group_id) {
  3857. case AFE_GROUP_DEVICE_ID_PRIMARY_TDM_RX:
  3858. case AFE_GROUP_DEVICE_ID_PRIMARY_TDM_TX:
  3859. case AFE_GROUP_DEVICE_ID_SECONDARY_TDM_RX:
  3860. case AFE_GROUP_DEVICE_ID_SECONDARY_TDM_TX:
  3861. case AFE_GROUP_DEVICE_ID_TERTIARY_TDM_RX:
  3862. case AFE_GROUP_DEVICE_ID_TERTIARY_TDM_TX:
  3863. case AFE_GROUP_DEVICE_ID_QUATERNARY_TDM_RX:
  3864. case AFE_GROUP_DEVICE_ID_QUATERNARY_TDM_TX:
  3865. case AFE_GROUP_DEVICE_ID_QUINARY_TDM_RX:
  3866. case AFE_GROUP_DEVICE_ID_QUINARY_TDM_TX:
  3867. cfg_type = AFE_PARAM_ID_GROUP_DEVICE_TDM_CONFIG;
  3868. break;
  3869. default:
  3870. pr_err("%s: Invalid group id 0x%x\n", __func__, group_id);
  3871. return -EINVAL;
  3872. }
  3873. param_hdr.module_id = AFE_MODULE_GROUP_DEVICE;
  3874. param_hdr.instance_id = INSTANCE_ID_0;
  3875. param_hdr.param_id = cfg_type;
  3876. param_hdr.param_size = sizeof(union afe_port_group_config);
  3877. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  3878. (u8 *) afe_group_config);
  3879. if (ret)
  3880. pr_err("%s: AFE_PARAM_ID_GROUP_DEVICE_CFG failed %d\n",
  3881. __func__, ret);
  3882. return ret;
  3883. }
  3884. /**
  3885. * afe_port_group_enable -
  3886. * command to enable AFE port group
  3887. *
  3888. * @group_id: group ID for AFE port group
  3889. * @afe_group_config: config for AFE group
  3890. * @enable: flag to indicate enable or disable
  3891. *
  3892. * Returns 0 on success or error on failure
  3893. */
  3894. int afe_port_group_enable(u16 group_id,
  3895. union afe_port_group_config *afe_group_config,
  3896. u16 enable)
  3897. {
  3898. struct afe_group_device_enable group_enable;
  3899. struct param_hdr_v3 param_hdr;
  3900. int ret;
  3901. pr_debug("%s: group id: 0x%x enable: %d\n", __func__,
  3902. group_id, enable);
  3903. memset(&group_enable, 0, sizeof(group_enable));
  3904. memset(&param_hdr, 0, sizeof(param_hdr));
  3905. ret = afe_q6_interface_prepare();
  3906. if (ret != 0) {
  3907. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  3908. return ret;
  3909. }
  3910. if (enable) {
  3911. ret = afe_port_group_set_param(group_id, afe_group_config);
  3912. if (ret < 0) {
  3913. pr_err("%s: afe send failed %d\n", __func__, ret);
  3914. return ret;
  3915. }
  3916. }
  3917. param_hdr.module_id = AFE_MODULE_GROUP_DEVICE;
  3918. param_hdr.instance_id = INSTANCE_ID_0;
  3919. param_hdr.param_id = AFE_PARAM_ID_GROUP_DEVICE_ENABLE;
  3920. param_hdr.param_size = sizeof(struct afe_group_device_enable);
  3921. group_enable.group_id = group_id;
  3922. group_enable.enable = enable;
  3923. ret = q6afe_svc_pack_and_set_param_in_band(IDX_GLOBAL_CFG, param_hdr,
  3924. (u8 *) &group_enable);
  3925. if (ret)
  3926. pr_err("%s: AFE_PARAM_ID_GROUP_DEVICE_ENABLE failed %d\n",
  3927. __func__, ret);
  3928. return ret;
  3929. }
  3930. EXPORT_SYMBOL(afe_port_group_enable);
  3931. int afe_stop_pseudo_port(u16 port_id)
  3932. {
  3933. int ret = 0;
  3934. struct afe_pseudoport_stop_command stop;
  3935. int index = 0;
  3936. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  3937. if (this_afe.apr == NULL) {
  3938. pr_err("%s: AFE is already closed\n", __func__);
  3939. return -EINVAL;
  3940. }
  3941. index = q6audio_get_port_index(port_id);
  3942. if (index < 0 || index >= AFE_MAX_PORTS) {
  3943. pr_err("%s: AFE port index[%d] invalid!\n",
  3944. __func__, index);
  3945. return -EINVAL;
  3946. }
  3947. ret = q6audio_validate_port(port_id);
  3948. if (ret < 0) {
  3949. pr_err("%s: Invalid port 0x%x ret %d\n",
  3950. __func__, port_id, ret);
  3951. return -EINVAL;
  3952. }
  3953. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3954. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3955. stop.hdr.pkt_size = sizeof(stop);
  3956. stop.hdr.src_port = 0;
  3957. stop.hdr.dest_port = 0;
  3958. stop.hdr.token = 0;
  3959. stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
  3960. stop.port_id = port_id;
  3961. stop.reserved = 0;
  3962. stop.hdr.token = index;
  3963. ret = afe_apr_send_pkt(&stop, &this_afe.wait[index]);
  3964. if (ret)
  3965. pr_err("%s: AFE close failed %d\n", __func__, ret);
  3966. return ret;
  3967. }
  3968. /**
  3969. * afe_req_mmap_handle -
  3970. * Retrieve AFE memory map handle
  3971. *
  3972. * @ac: AFE audio client
  3973. *
  3974. * Returns memory map handle
  3975. */
  3976. uint32_t afe_req_mmap_handle(struct afe_audio_client *ac)
  3977. {
  3978. return ac->mem_map_handle;
  3979. }
  3980. EXPORT_SYMBOL(afe_req_mmap_handle);
  3981. /**
  3982. * q6afe_audio_client_alloc -
  3983. * Assign new AFE audio client
  3984. *
  3985. * @priv: privata data to hold for audio client
  3986. *
  3987. * Returns ac pointer on success or NULL on failure
  3988. */
  3989. struct afe_audio_client *q6afe_audio_client_alloc(void *priv)
  3990. {
  3991. struct afe_audio_client *ac;
  3992. int lcnt = 0;
  3993. ac = kzalloc(sizeof(struct afe_audio_client), GFP_KERNEL);
  3994. if (!ac)
  3995. return NULL;
  3996. ac->priv = priv;
  3997. init_waitqueue_head(&ac->cmd_wait);
  3998. INIT_LIST_HEAD(&ac->port[0].mem_map_handle);
  3999. INIT_LIST_HEAD(&ac->port[1].mem_map_handle);
  4000. pr_debug("%s: mem_map_handle list init'ed\n", __func__);
  4001. mutex_init(&ac->cmd_lock);
  4002. for (lcnt = 0; lcnt <= OUT; lcnt++) {
  4003. mutex_init(&ac->port[lcnt].lock);
  4004. spin_lock_init(&ac->port[lcnt].dsp_lock);
  4005. }
  4006. atomic_set(&ac->cmd_state, 0);
  4007. return ac;
  4008. }
  4009. EXPORT_SYMBOL(q6afe_audio_client_alloc);
  4010. /**
  4011. * q6afe_audio_client_buf_alloc_contiguous -
  4012. * Allocate contiguous shared buffers
  4013. *
  4014. * @dir: RX or TX direction of AFE port
  4015. * @ac: AFE audio client handle
  4016. * @bufsz: size of each shared buffer
  4017. * @bufcnt: number of buffers
  4018. *
  4019. * Returns 0 on success or error on failure
  4020. */
  4021. int q6afe_audio_client_buf_alloc_contiguous(unsigned int dir,
  4022. struct afe_audio_client *ac,
  4023. unsigned int bufsz,
  4024. unsigned int bufcnt)
  4025. {
  4026. int cnt = 0;
  4027. int rc = 0;
  4028. struct afe_audio_buffer *buf;
  4029. size_t len;
  4030. if (!(ac) || ((dir != IN) && (dir != OUT))) {
  4031. pr_err("%s: ac %pK dir %d\n", __func__, ac, dir);
  4032. return -EINVAL;
  4033. }
  4034. pr_debug("%s: bufsz[%d]bufcnt[%d]\n",
  4035. __func__,
  4036. bufsz, bufcnt);
  4037. if (ac->port[dir].buf) {
  4038. pr_debug("%s: buffer already allocated\n", __func__);
  4039. return 0;
  4040. }
  4041. mutex_lock(&ac->cmd_lock);
  4042. buf = kzalloc(((sizeof(struct afe_audio_buffer))*bufcnt),
  4043. GFP_KERNEL);
  4044. if (!buf) {
  4045. pr_err("%s: null buf\n", __func__);
  4046. mutex_unlock(&ac->cmd_lock);
  4047. goto fail;
  4048. }
  4049. ac->port[dir].buf = buf;
  4050. rc = msm_audio_ion_alloc(&buf[0].dma_buf,
  4051. bufsz * bufcnt,
  4052. &buf[0].phys, &len,
  4053. &buf[0].data);
  4054. if (rc) {
  4055. pr_err("%s: audio ION alloc failed, rc = %d\n",
  4056. __func__, rc);
  4057. mutex_unlock(&ac->cmd_lock);
  4058. goto fail;
  4059. }
  4060. buf[0].used = dir ^ 1;
  4061. buf[0].size = bufsz;
  4062. buf[0].actual_size = bufsz;
  4063. cnt = 1;
  4064. while (cnt < bufcnt) {
  4065. if (bufsz > 0) {
  4066. buf[cnt].data = buf[0].data + (cnt * bufsz);
  4067. buf[cnt].phys = buf[0].phys + (cnt * bufsz);
  4068. if (!buf[cnt].data) {
  4069. pr_err("%s: Buf alloc failed\n",
  4070. __func__);
  4071. mutex_unlock(&ac->cmd_lock);
  4072. goto fail;
  4073. }
  4074. buf[cnt].used = dir ^ 1;
  4075. buf[cnt].size = bufsz;
  4076. buf[cnt].actual_size = bufsz;
  4077. pr_debug("%s: data[%pK]phys[%pK][%pK]\n", __func__,
  4078. buf[cnt].data,
  4079. &buf[cnt].phys,
  4080. &buf[cnt].phys);
  4081. }
  4082. cnt++;
  4083. }
  4084. ac->port[dir].max_buf_cnt = cnt;
  4085. mutex_unlock(&ac->cmd_lock);
  4086. return 0;
  4087. fail:
  4088. pr_err("%s: jump fail\n", __func__);
  4089. q6afe_audio_client_buf_free_contiguous(dir, ac);
  4090. return -EINVAL;
  4091. }
  4092. EXPORT_SYMBOL(q6afe_audio_client_buf_alloc_contiguous);
  4093. /**
  4094. * afe_memory_map -
  4095. * command to map shared buffers to AFE
  4096. *
  4097. * @dma_addr_p: DMA physical address
  4098. * @dma_buf_sz: shared DMA buffer size
  4099. * @ac: AFE audio client handle
  4100. *
  4101. * Returns 0 on success or error on failure
  4102. */
  4103. int afe_memory_map(phys_addr_t dma_addr_p, u32 dma_buf_sz,
  4104. struct afe_audio_client *ac)
  4105. {
  4106. int ret = 0;
  4107. mutex_lock(&this_afe.afe_cmd_lock);
  4108. ac->mem_map_handle = 0;
  4109. ret = afe_cmd_memory_map(dma_addr_p, dma_buf_sz);
  4110. if (ret < 0) {
  4111. pr_err("%s: afe_cmd_memory_map failed %d\n",
  4112. __func__, ret);
  4113. mutex_unlock(&this_afe.afe_cmd_lock);
  4114. return ret;
  4115. }
  4116. ac->mem_map_handle = this_afe.mmap_handle;
  4117. mutex_unlock(&this_afe.afe_cmd_lock);
  4118. return ret;
  4119. }
  4120. EXPORT_SYMBOL(afe_memory_map);
  4121. int afe_cmd_memory_map(phys_addr_t dma_addr_p, u32 dma_buf_sz)
  4122. {
  4123. int ret = 0;
  4124. int cmd_size = 0;
  4125. void *payload = NULL;
  4126. void *mmap_region_cmd = NULL;
  4127. struct afe_service_cmd_shared_mem_map_regions *mregion = NULL;
  4128. struct afe_service_shared_map_region_payload *mregion_pl = NULL;
  4129. int index = 0;
  4130. pr_debug("%s:\n", __func__);
  4131. if (this_afe.apr == NULL) {
  4132. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4133. 0xFFFFFFFF, &this_afe);
  4134. pr_debug("%s: Register AFE\n", __func__);
  4135. if (this_afe.apr == NULL) {
  4136. pr_err("%s: Unable to register AFE\n", __func__);
  4137. ret = -ENODEV;
  4138. return ret;
  4139. }
  4140. rtac_set_afe_handle(this_afe.apr);
  4141. }
  4142. if (dma_buf_sz % SZ_4K != 0) {
  4143. /*
  4144. * The memory allocated by msm_audio_ion_alloc is always 4kB
  4145. * aligned, ADSP expects the size to be 4kB aligned as well
  4146. * so re-adjusts the buffer size before passing to ADSP.
  4147. */
  4148. dma_buf_sz = PAGE_ALIGN(dma_buf_sz);
  4149. }
  4150. cmd_size = sizeof(struct afe_service_cmd_shared_mem_map_regions)
  4151. + sizeof(struct afe_service_shared_map_region_payload);
  4152. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  4153. if (!mmap_region_cmd)
  4154. return -ENOMEM;
  4155. mregion = (struct afe_service_cmd_shared_mem_map_regions *)
  4156. mmap_region_cmd;
  4157. mregion->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4158. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4159. mregion->hdr.pkt_size = cmd_size;
  4160. mregion->hdr.src_port = 0;
  4161. mregion->hdr.dest_port = 0;
  4162. mregion->hdr.token = 0;
  4163. mregion->hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS;
  4164. mregion->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  4165. mregion->num_regions = 1;
  4166. mregion->property_flag = 0x00;
  4167. /* Todo */
  4168. index = mregion->hdr.token = IDX_RSVD_2;
  4169. payload = ((u8 *) mmap_region_cmd +
  4170. sizeof(struct afe_service_cmd_shared_mem_map_regions));
  4171. mregion_pl = (struct afe_service_shared_map_region_payload *)payload;
  4172. mregion_pl->shm_addr_lsw = lower_32_bits(dma_addr_p);
  4173. mregion_pl->shm_addr_msw = msm_audio_populate_upper_32_bits(dma_addr_p);
  4174. mregion_pl->mem_size_bytes = dma_buf_sz;
  4175. pr_debug("%s: dma_addr_p 0x%pK , size %d\n", __func__,
  4176. &dma_addr_p, dma_buf_sz);
  4177. atomic_set(&this_afe.state, 1);
  4178. atomic_set(&this_afe.status, 0);
  4179. this_afe.mmap_handle = 0;
  4180. ret = apr_send_pkt(this_afe.apr, (uint32_t *) mmap_region_cmd);
  4181. if (ret < 0) {
  4182. pr_err("%s: AFE memory map cmd failed %d\n",
  4183. __func__, ret);
  4184. ret = -EINVAL;
  4185. goto fail_cmd;
  4186. }
  4187. ret = wait_event_timeout(this_afe.wait[index],
  4188. (atomic_read(&this_afe.state) == 0),
  4189. msecs_to_jiffies(TIMEOUT_MS));
  4190. if (!ret) {
  4191. pr_err("%s: wait_event timeout\n", __func__);
  4192. ret = -EINVAL;
  4193. goto fail_cmd;
  4194. }
  4195. if (atomic_read(&this_afe.status) > 0) {
  4196. pr_err("%s: config cmd failed [%s]\n",
  4197. __func__, adsp_err_get_err_str(
  4198. atomic_read(&this_afe.status)));
  4199. ret = adsp_err_get_lnx_err_code(
  4200. atomic_read(&this_afe.status));
  4201. goto fail_cmd;
  4202. }
  4203. kfree(mmap_region_cmd);
  4204. return 0;
  4205. fail_cmd:
  4206. kfree(mmap_region_cmd);
  4207. pr_err("%s: fail_cmd\n", __func__);
  4208. return ret;
  4209. }
  4210. int afe_cmd_memory_map_nowait(int port_id, phys_addr_t dma_addr_p,
  4211. u32 dma_buf_sz)
  4212. {
  4213. int ret = 0;
  4214. int cmd_size = 0;
  4215. void *payload = NULL;
  4216. void *mmap_region_cmd = NULL;
  4217. struct afe_service_cmd_shared_mem_map_regions *mregion = NULL;
  4218. struct afe_service_shared_map_region_payload *mregion_pl = NULL;
  4219. int index = 0;
  4220. pr_debug("%s:\n", __func__);
  4221. if (this_afe.apr == NULL) {
  4222. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4223. 0xFFFFFFFF, &this_afe);
  4224. pr_debug("%s: Register AFE\n", __func__);
  4225. if (this_afe.apr == NULL) {
  4226. pr_err("%s: Unable to register AFE\n", __func__);
  4227. ret = -ENODEV;
  4228. return ret;
  4229. }
  4230. rtac_set_afe_handle(this_afe.apr);
  4231. }
  4232. index = q6audio_get_port_index(port_id);
  4233. if (index < 0 || index >= AFE_MAX_PORTS) {
  4234. pr_err("%s: AFE port index[%d] invalid!\n",
  4235. __func__, index);
  4236. return -EINVAL;
  4237. }
  4238. ret = q6audio_validate_port(port_id);
  4239. if (ret < 0) {
  4240. pr_err("%s: Invalid port 0x%x ret %d",
  4241. __func__, port_id, ret);
  4242. return -EINVAL;
  4243. }
  4244. cmd_size = sizeof(struct afe_service_cmd_shared_mem_map_regions)
  4245. + sizeof(struct afe_service_shared_map_region_payload);
  4246. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  4247. if (!mmap_region_cmd)
  4248. return -ENOMEM;
  4249. mregion = (struct afe_service_cmd_shared_mem_map_regions *)
  4250. mmap_region_cmd;
  4251. mregion->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4252. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4253. mregion->hdr.pkt_size = sizeof(mregion);
  4254. mregion->hdr.src_port = 0;
  4255. mregion->hdr.dest_port = 0;
  4256. mregion->hdr.token = 0;
  4257. mregion->hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS;
  4258. mregion->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  4259. mregion->num_regions = 1;
  4260. mregion->property_flag = 0x00;
  4261. payload = ((u8 *) mmap_region_cmd +
  4262. sizeof(struct afe_service_cmd_shared_mem_map_regions));
  4263. mregion_pl = (struct afe_service_shared_map_region_payload *)payload;
  4264. mregion_pl->shm_addr_lsw = lower_32_bits(dma_addr_p);
  4265. mregion_pl->shm_addr_msw = msm_audio_populate_upper_32_bits(dma_addr_p);
  4266. mregion_pl->mem_size_bytes = dma_buf_sz;
  4267. ret = afe_apr_send_pkt(mmap_region_cmd, NULL);
  4268. if (ret)
  4269. pr_err("%s: AFE memory map cmd failed %d\n",
  4270. __func__, ret);
  4271. kfree(mmap_region_cmd);
  4272. return ret;
  4273. }
  4274. /**
  4275. * q6afe_audio_client_buf_free_contiguous -
  4276. * frees the shared contiguous memory
  4277. *
  4278. * @dir: RX or TX direction of port
  4279. * @ac: AFE audio client handle
  4280. *
  4281. */
  4282. int q6afe_audio_client_buf_free_contiguous(unsigned int dir,
  4283. struct afe_audio_client *ac)
  4284. {
  4285. struct afe_audio_port_data *port;
  4286. int cnt = 0;
  4287. mutex_lock(&ac->cmd_lock);
  4288. port = &ac->port[dir];
  4289. if (!port->buf) {
  4290. pr_err("%s: buf is null\n", __func__);
  4291. mutex_unlock(&ac->cmd_lock);
  4292. return 0;
  4293. }
  4294. cnt = port->max_buf_cnt - 1;
  4295. if (port->buf[0].data) {
  4296. pr_debug("%s: data[%pK], phys[%pK], dma_buf[%pK]\n",
  4297. __func__,
  4298. port->buf[0].data,
  4299. &port->buf[0].phys,
  4300. port->buf[0].dma_buf);
  4301. msm_audio_ion_free(port->buf[0].dma_buf);
  4302. port->buf[0].dma_buf = NULL;
  4303. }
  4304. while (cnt >= 0) {
  4305. port->buf[cnt].data = NULL;
  4306. port->buf[cnt].phys = 0;
  4307. cnt--;
  4308. }
  4309. port->max_buf_cnt = 0;
  4310. kfree(port->buf);
  4311. port->buf = NULL;
  4312. mutex_unlock(&ac->cmd_lock);
  4313. return 0;
  4314. }
  4315. EXPORT_SYMBOL(q6afe_audio_client_buf_free_contiguous);
  4316. /**
  4317. * q6afe_audio_client_free -
  4318. * frees the audio client from AFE
  4319. *
  4320. * @ac: AFE audio client handle
  4321. *
  4322. */
  4323. void q6afe_audio_client_free(struct afe_audio_client *ac)
  4324. {
  4325. int loopcnt;
  4326. struct afe_audio_port_data *port;
  4327. if (!ac) {
  4328. pr_err("%s: audio client is NULL\n", __func__);
  4329. return;
  4330. }
  4331. for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
  4332. port = &ac->port[loopcnt];
  4333. if (!port->buf)
  4334. continue;
  4335. pr_debug("%s: loopcnt = %d\n", __func__, loopcnt);
  4336. q6afe_audio_client_buf_free_contiguous(loopcnt, ac);
  4337. }
  4338. kfree(ac);
  4339. }
  4340. EXPORT_SYMBOL(q6afe_audio_client_free);
  4341. /**
  4342. * afe_cmd_memory_unmap -
  4343. * command to unmap memory for AFE shared buffer
  4344. *
  4345. * @mem_map_handle: memory map handle to be unmapped
  4346. *
  4347. * Returns 0 on success or error on failure
  4348. */
  4349. int afe_cmd_memory_unmap(u32 mem_map_handle)
  4350. {
  4351. int ret = 0;
  4352. struct afe_service_cmd_shared_mem_unmap_regions mregion;
  4353. int index = 0;
  4354. pr_debug("%s: handle 0x%x\n", __func__, mem_map_handle);
  4355. if (this_afe.apr == NULL) {
  4356. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4357. 0xFFFFFFFF, &this_afe);
  4358. pr_debug("%s: Register AFE\n", __func__);
  4359. if (this_afe.apr == NULL) {
  4360. pr_err("%s: Unable to register AFE\n", __func__);
  4361. ret = -ENODEV;
  4362. return ret;
  4363. }
  4364. rtac_set_afe_handle(this_afe.apr);
  4365. }
  4366. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4367. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4368. mregion.hdr.pkt_size = sizeof(mregion);
  4369. mregion.hdr.src_port = 0;
  4370. mregion.hdr.dest_port = 0;
  4371. mregion.hdr.token = 0;
  4372. mregion.hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS;
  4373. mregion.mem_map_handle = mem_map_handle;
  4374. /* Todo */
  4375. index = mregion.hdr.token = IDX_RSVD_2;
  4376. atomic_set(&this_afe.status, 0);
  4377. ret = afe_apr_send_pkt(&mregion, &this_afe.wait[index]);
  4378. if (ret)
  4379. pr_err("%s: AFE memory unmap cmd failed %d\n",
  4380. __func__, ret);
  4381. return ret;
  4382. }
  4383. EXPORT_SYMBOL(afe_cmd_memory_unmap);
  4384. int afe_cmd_memory_unmap_nowait(u32 mem_map_handle)
  4385. {
  4386. int ret = 0;
  4387. struct afe_service_cmd_shared_mem_unmap_regions mregion;
  4388. pr_debug("%s: handle 0x%x\n", __func__, mem_map_handle);
  4389. if (this_afe.apr == NULL) {
  4390. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4391. 0xFFFFFFFF, &this_afe);
  4392. pr_debug("%s: Register AFE\n", __func__);
  4393. if (this_afe.apr == NULL) {
  4394. pr_err("%s: Unable to register AFE\n", __func__);
  4395. ret = -ENODEV;
  4396. return ret;
  4397. }
  4398. rtac_set_afe_handle(this_afe.apr);
  4399. }
  4400. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4401. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4402. mregion.hdr.pkt_size = sizeof(mregion);
  4403. mregion.hdr.src_port = 0;
  4404. mregion.hdr.dest_port = 0;
  4405. mregion.hdr.token = 0;
  4406. mregion.hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS;
  4407. mregion.mem_map_handle = mem_map_handle;
  4408. ret = afe_apr_send_pkt(&mregion, NULL);
  4409. if (ret)
  4410. pr_err("%s: AFE memory unmap cmd failed %d\n",
  4411. __func__, ret);
  4412. return ret;
  4413. }
  4414. /**
  4415. * afe_register_get_events -
  4416. * register for events from proxy port
  4417. *
  4418. * @port_id: Port ID to register events
  4419. * @cb: callback function to invoke for events from proxy port
  4420. * @private_data: private data to sent back in callback fn
  4421. *
  4422. * Returns 0 on success or error on failure
  4423. */
  4424. int afe_register_get_events(u16 port_id,
  4425. void (*cb)(uint32_t opcode,
  4426. uint32_t token, uint32_t *payload, void *priv),
  4427. void *private_data)
  4428. {
  4429. int ret = 0;
  4430. struct afe_service_cmd_register_rt_port_driver rtproxy;
  4431. pr_debug("%s: port_id: 0x%x\n", __func__, port_id);
  4432. if (this_afe.apr == NULL) {
  4433. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4434. 0xFFFFFFFF, &this_afe);
  4435. pr_debug("%s: Register AFE\n", __func__);
  4436. if (this_afe.apr == NULL) {
  4437. pr_err("%s: Unable to register AFE\n", __func__);
  4438. ret = -ENODEV;
  4439. return ret;
  4440. }
  4441. rtac_set_afe_handle(this_afe.apr);
  4442. }
  4443. if ((port_id == RT_PROXY_DAI_002_RX) ||
  4444. (port_id == RT_PROXY_DAI_001_TX)) {
  4445. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  4446. } else {
  4447. pr_err("%s: wrong port id 0x%x\n", __func__, port_id);
  4448. return -EINVAL;
  4449. }
  4450. if (port_id == RT_PROXY_PORT_001_TX) {
  4451. this_afe.tx_cb = cb;
  4452. this_afe.tx_private_data = private_data;
  4453. } else if (port_id == RT_PROXY_PORT_001_RX) {
  4454. this_afe.rx_cb = cb;
  4455. this_afe.rx_private_data = private_data;
  4456. }
  4457. rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4458. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4459. rtproxy.hdr.pkt_size = sizeof(rtproxy);
  4460. rtproxy.hdr.src_port = 1;
  4461. rtproxy.hdr.dest_port = 1;
  4462. rtproxy.hdr.opcode = AFE_SERVICE_CMD_REGISTER_RT_PORT_DRIVER;
  4463. rtproxy.port_id = port_id;
  4464. rtproxy.reserved = 0;
  4465. ret = afe_apr_send_pkt(&rtproxy, NULL);
  4466. if (ret)
  4467. pr_err("%s: AFE reg. rtproxy_event failed %d\n",
  4468. __func__, ret);
  4469. return ret;
  4470. }
  4471. EXPORT_SYMBOL(afe_register_get_events);
  4472. /**
  4473. * afe_unregister_get_events -
  4474. * unregister for events from proxy port
  4475. *
  4476. * @port_id: Port ID to unregister events
  4477. *
  4478. * Returns 0 on success or error on failure
  4479. */
  4480. int afe_unregister_get_events(u16 port_id)
  4481. {
  4482. int ret = 0;
  4483. struct afe_service_cmd_unregister_rt_port_driver rtproxy;
  4484. int index = 0;
  4485. pr_debug("%s:\n", __func__);
  4486. if (this_afe.apr == NULL) {
  4487. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4488. 0xFFFFFFFF, &this_afe);
  4489. pr_debug("%s: Register AFE\n", __func__);
  4490. if (this_afe.apr == NULL) {
  4491. pr_err("%s: Unable to register AFE\n", __func__);
  4492. ret = -ENODEV;
  4493. return ret;
  4494. }
  4495. rtac_set_afe_handle(this_afe.apr);
  4496. }
  4497. if ((port_id == RT_PROXY_DAI_002_RX) ||
  4498. (port_id == RT_PROXY_DAI_001_TX)) {
  4499. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  4500. } else {
  4501. pr_err("%s: wrong port id 0x%x\n", __func__, port_id);
  4502. return -EINVAL;
  4503. }
  4504. index = q6audio_get_port_index(port_id);
  4505. if (index < 0 || index >= AFE_MAX_PORTS) {
  4506. pr_err("%s: AFE port index[%d] invalid!\n",
  4507. __func__, index);
  4508. return -EINVAL;
  4509. }
  4510. ret = q6audio_validate_port(port_id);
  4511. if (ret < 0) {
  4512. pr_err("%s: Invalid port 0x%x ret %d", __func__, port_id, ret);
  4513. return -EINVAL;
  4514. }
  4515. rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4516. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4517. rtproxy.hdr.pkt_size = sizeof(rtproxy);
  4518. rtproxy.hdr.src_port = 0;
  4519. rtproxy.hdr.dest_port = 0;
  4520. rtproxy.hdr.token = 0;
  4521. rtproxy.hdr.opcode = AFE_SERVICE_CMD_UNREGISTER_RT_PORT_DRIVER;
  4522. rtproxy.port_id = port_id;
  4523. rtproxy.reserved = 0;
  4524. rtproxy.hdr.token = index;
  4525. if (port_id == RT_PROXY_PORT_001_TX) {
  4526. this_afe.tx_cb = NULL;
  4527. this_afe.tx_private_data = NULL;
  4528. } else if (port_id == RT_PROXY_PORT_001_RX) {
  4529. this_afe.rx_cb = NULL;
  4530. this_afe.rx_private_data = NULL;
  4531. }
  4532. ret = afe_apr_send_pkt(&rtproxy, &this_afe.wait[index]);
  4533. if (ret)
  4534. pr_err("%s: AFE enable Unreg. rtproxy_event failed %d\n",
  4535. __func__, ret);
  4536. return ret;
  4537. }
  4538. EXPORT_SYMBOL(afe_unregister_get_events);
  4539. /**
  4540. * afe_rt_proxy_port_write -
  4541. * command for AFE RT proxy port write
  4542. *
  4543. * @buf_addr_p: Physical buffer address with
  4544. * playback data to proxy port
  4545. * @mem_map_handle: memory map handle of write buffer
  4546. * @bytes: number of bytes to write
  4547. *
  4548. * Returns 0 on success or error on failure
  4549. */
  4550. int afe_rt_proxy_port_write(phys_addr_t buf_addr_p,
  4551. u32 mem_map_handle, int bytes)
  4552. {
  4553. int ret = 0;
  4554. struct afe_port_data_cmd_rt_proxy_port_write_v2 afecmd_wr;
  4555. if (this_afe.apr == NULL) {
  4556. pr_err("%s: register to AFE is not done\n", __func__);
  4557. ret = -ENODEV;
  4558. return ret;
  4559. }
  4560. pr_debug("%s: buf_addr_p = 0x%pK bytes = %d\n", __func__,
  4561. &buf_addr_p, bytes);
  4562. afecmd_wr.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4563. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4564. afecmd_wr.hdr.pkt_size = sizeof(afecmd_wr);
  4565. afecmd_wr.hdr.src_port = 0;
  4566. afecmd_wr.hdr.dest_port = 0;
  4567. afecmd_wr.hdr.token = 0;
  4568. afecmd_wr.hdr.opcode = AFE_PORT_DATA_CMD_RT_PROXY_PORT_WRITE_V2;
  4569. afecmd_wr.port_id = RT_PROXY_PORT_001_TX;
  4570. afecmd_wr.buffer_address_lsw = lower_32_bits(buf_addr_p);
  4571. afecmd_wr.buffer_address_msw =
  4572. msm_audio_populate_upper_32_bits(buf_addr_p);
  4573. afecmd_wr.mem_map_handle = mem_map_handle;
  4574. afecmd_wr.available_bytes = bytes;
  4575. afecmd_wr.reserved = 0;
  4576. ret = afe_apr_send_pkt(&afecmd_wr, NULL);
  4577. if (ret)
  4578. pr_err("%s: AFE rtproxy write to port 0x%x failed %d\n",
  4579. __func__, afecmd_wr.port_id, ret);
  4580. return ret;
  4581. }
  4582. EXPORT_SYMBOL(afe_rt_proxy_port_write);
  4583. /**
  4584. * afe_rt_proxy_port_read -
  4585. * command for AFE RT proxy port read
  4586. *
  4587. * @buf_addr_p: Physical buffer address to fill read data
  4588. * @mem_map_handle: memory map handle for buffer read
  4589. * @bytes: number of bytes to read
  4590. *
  4591. * Returns 0 on success or error on failure
  4592. */
  4593. int afe_rt_proxy_port_read(phys_addr_t buf_addr_p,
  4594. u32 mem_map_handle, int bytes)
  4595. {
  4596. int ret = 0;
  4597. struct afe_port_data_cmd_rt_proxy_port_read_v2 afecmd_rd;
  4598. if (this_afe.apr == NULL) {
  4599. pr_err("%s: register to AFE is not done\n", __func__);
  4600. ret = -ENODEV;
  4601. return ret;
  4602. }
  4603. pr_debug("%s: buf_addr_p = 0x%pK bytes = %d\n", __func__,
  4604. &buf_addr_p, bytes);
  4605. afecmd_rd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4606. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4607. afecmd_rd.hdr.pkt_size = sizeof(afecmd_rd);
  4608. afecmd_rd.hdr.src_port = 0;
  4609. afecmd_rd.hdr.dest_port = 0;
  4610. afecmd_rd.hdr.token = 0;
  4611. afecmd_rd.hdr.opcode = AFE_PORT_DATA_CMD_RT_PROXY_PORT_READ_V2;
  4612. afecmd_rd.port_id = RT_PROXY_PORT_001_RX;
  4613. afecmd_rd.buffer_address_lsw = lower_32_bits(buf_addr_p);
  4614. afecmd_rd.buffer_address_msw =
  4615. msm_audio_populate_upper_32_bits(buf_addr_p);
  4616. afecmd_rd.available_bytes = bytes;
  4617. afecmd_rd.mem_map_handle = mem_map_handle;
  4618. ret = afe_apr_send_pkt(&afecmd_rd, NULL);
  4619. if (ret)
  4620. pr_err("%s: AFE rtproxy read cmd to port 0x%x failed %d\n",
  4621. __func__, afecmd_rd.port_id, ret);
  4622. return ret;
  4623. }
  4624. EXPORT_SYMBOL(afe_rt_proxy_port_read);
  4625. #ifdef CONFIG_DEBUG_FS
  4626. static struct dentry *debugfs_afelb;
  4627. static struct dentry *debugfs_afelb_gain;
  4628. static int afe_debug_open(struct inode *inode, struct file *file)
  4629. {
  4630. file->private_data = inode->i_private;
  4631. pr_info("%s: debug intf %s\n", __func__, (char *) file->private_data);
  4632. return 0;
  4633. }
  4634. static int afe_get_parameters(char *buf, long int *param1, int num_of_par)
  4635. {
  4636. char *token;
  4637. int base, cnt;
  4638. token = strsep(&buf, " ");
  4639. for (cnt = 0; cnt < num_of_par; cnt++) {
  4640. if (token != NULL) {
  4641. if ((token[1] == 'x') || (token[1] == 'X'))
  4642. base = 16;
  4643. else
  4644. base = 10;
  4645. if (kstrtoul(token, base, &param1[cnt]) != 0) {
  4646. pr_err("%s: kstrtoul failed\n",
  4647. __func__);
  4648. return -EINVAL;
  4649. }
  4650. token = strsep(&buf, " ");
  4651. } else {
  4652. pr_err("%s: token NULL\n", __func__);
  4653. return -EINVAL;
  4654. }
  4655. }
  4656. return 0;
  4657. }
  4658. #define AFE_LOOPBACK_ON (1)
  4659. #define AFE_LOOPBACK_OFF (0)
  4660. static ssize_t afe_debug_write(struct file *filp,
  4661. const char __user *ubuf, size_t cnt, loff_t *ppos)
  4662. {
  4663. char *lb_str = filp->private_data;
  4664. char lbuf[32];
  4665. int rc;
  4666. unsigned long param[5];
  4667. if (cnt > sizeof(lbuf) - 1) {
  4668. pr_err("%s: cnt %zd size %zd\n", __func__, cnt, sizeof(lbuf)-1);
  4669. return -EINVAL;
  4670. }
  4671. rc = copy_from_user(lbuf, ubuf, cnt);
  4672. if (rc) {
  4673. pr_err("%s: copy from user failed %d\n", __func__, rc);
  4674. return -EFAULT;
  4675. }
  4676. lbuf[cnt] = '\0';
  4677. if (!strcmp(lb_str, "afe_loopback")) {
  4678. rc = afe_get_parameters(lbuf, param, 3);
  4679. if (!rc) {
  4680. pr_info("%s: %lu %lu %lu\n", lb_str, param[0], param[1],
  4681. param[2]);
  4682. if ((param[0] != AFE_LOOPBACK_ON) && (param[0] !=
  4683. AFE_LOOPBACK_OFF)) {
  4684. pr_err("%s: Error, parameter 0 incorrect\n",
  4685. __func__);
  4686. rc = -EINVAL;
  4687. goto afe_error;
  4688. }
  4689. if ((q6audio_validate_port(param[1]) < 0) ||
  4690. (q6audio_validate_port(param[2])) < 0) {
  4691. pr_err("%s: Error, invalid afe port\n",
  4692. __func__);
  4693. }
  4694. if (this_afe.apr == NULL) {
  4695. pr_err("%s: Error, AFE not opened\n", __func__);
  4696. rc = -EINVAL;
  4697. } else {
  4698. rc = afe_loopback(param[0], param[1], param[2]);
  4699. }
  4700. } else {
  4701. pr_err("%s: Error, invalid parameters\n", __func__);
  4702. rc = -EINVAL;
  4703. }
  4704. } else if (!strcmp(lb_str, "afe_loopback_gain")) {
  4705. rc = afe_get_parameters(lbuf, param, 2);
  4706. if (!rc) {
  4707. pr_info("%s: %s %lu %lu\n",
  4708. __func__, lb_str, param[0], param[1]);
  4709. rc = q6audio_validate_port(param[0]);
  4710. if (rc < 0) {
  4711. pr_err("%s: Error, invalid afe port %d %lu\n",
  4712. __func__, rc, param[0]);
  4713. rc = -EINVAL;
  4714. goto afe_error;
  4715. }
  4716. if (param[1] > 100) {
  4717. pr_err("%s: Error, volume should be 0 to 100 percentage param = %lu\n",
  4718. __func__, param[1]);
  4719. rc = -EINVAL;
  4720. goto afe_error;
  4721. }
  4722. param[1] = (Q6AFE_MAX_VOLUME * param[1]) / 100;
  4723. if (this_afe.apr == NULL) {
  4724. pr_err("%s: Error, AFE not opened\n", __func__);
  4725. rc = -EINVAL;
  4726. } else {
  4727. rc = afe_loopback_gain(param[0], param[1]);
  4728. }
  4729. } else {
  4730. pr_err("%s: Error, invalid parameters\n", __func__);
  4731. rc = -EINVAL;
  4732. }
  4733. }
  4734. afe_error:
  4735. if (rc == 0)
  4736. rc = cnt;
  4737. else
  4738. pr_err("%s: rc = %d\n", __func__, rc);
  4739. return rc;
  4740. }
  4741. static const struct file_operations afe_debug_fops = {
  4742. .open = afe_debug_open,
  4743. .write = afe_debug_write
  4744. };
  4745. static void config_debug_fs_init(void)
  4746. {
  4747. debugfs_afelb = debugfs_create_file("afe_loopback",
  4748. 0664, NULL, (void *) "afe_loopback",
  4749. &afe_debug_fops);
  4750. debugfs_afelb_gain = debugfs_create_file("afe_loopback_gain",
  4751. 0664, NULL, (void *) "afe_loopback_gain",
  4752. &afe_debug_fops);
  4753. }
  4754. static void config_debug_fs_exit(void)
  4755. {
  4756. debugfs_remove(debugfs_afelb);
  4757. debugfs_remove(debugfs_afelb_gain);
  4758. }
  4759. #else
  4760. static void config_debug_fs_init(void)
  4761. {
  4762. }
  4763. static void config_debug_fs_exit(void)
  4764. {
  4765. }
  4766. #endif
  4767. /**
  4768. * afe_set_dtmf_gen_rx_portid -
  4769. * Set port_id for DTMF tone generation
  4770. *
  4771. * @port_id: AFE port id
  4772. * @set: set or reset port id value for dtmf gen
  4773. *
  4774. */
  4775. void afe_set_dtmf_gen_rx_portid(u16 port_id, int set)
  4776. {
  4777. if (set)
  4778. this_afe.dtmf_gen_rx_portid = port_id;
  4779. else if (this_afe.dtmf_gen_rx_portid == port_id)
  4780. this_afe.dtmf_gen_rx_portid = -1;
  4781. }
  4782. EXPORT_SYMBOL(afe_set_dtmf_gen_rx_portid);
  4783. /**
  4784. * afe_dtmf_generate_rx - command to generate AFE DTMF RX
  4785. *
  4786. * @duration_in_ms: Duration in ms for dtmf tone
  4787. * @high_freq: Higher frequency for dtmf
  4788. * @low_freq: lower frequency for dtmf
  4789. * @gain: Gain value for DTMF tone
  4790. *
  4791. * Returns 0 on success, appropriate error code otherwise
  4792. */
  4793. int afe_dtmf_generate_rx(int64_t duration_in_ms,
  4794. uint16_t high_freq,
  4795. uint16_t low_freq, uint16_t gain)
  4796. {
  4797. int ret = 0;
  4798. int index = 0;
  4799. struct afe_dtmf_generation_command cmd_dtmf;
  4800. pr_debug("%s: DTMF AFE Gen\n", __func__);
  4801. if (afe_validate_port(this_afe.dtmf_gen_rx_portid) < 0) {
  4802. pr_err("%s: Failed : Invalid Port id = 0x%x\n",
  4803. __func__, this_afe.dtmf_gen_rx_portid);
  4804. ret = -EINVAL;
  4805. goto fail_cmd;
  4806. }
  4807. if (this_afe.apr == NULL) {
  4808. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  4809. 0xFFFFFFFF, &this_afe);
  4810. pr_debug("%s: Register AFE\n", __func__);
  4811. if (this_afe.apr == NULL) {
  4812. pr_err("%s: Unable to register AFE\n", __func__);
  4813. ret = -ENODEV;
  4814. return ret;
  4815. }
  4816. rtac_set_afe_handle(this_afe.apr);
  4817. }
  4818. pr_debug("%s: dur=%lld: hfreq=%d lfreq=%d gain=%d portid=0x%x\n",
  4819. __func__,
  4820. duration_in_ms, high_freq, low_freq, gain,
  4821. this_afe.dtmf_gen_rx_portid);
  4822. cmd_dtmf.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4823. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4824. cmd_dtmf.hdr.pkt_size = sizeof(cmd_dtmf);
  4825. cmd_dtmf.hdr.src_port = 0;
  4826. cmd_dtmf.hdr.dest_port = 0;
  4827. cmd_dtmf.hdr.token = 0;
  4828. cmd_dtmf.hdr.opcode = AFE_PORTS_CMD_DTMF_CTL;
  4829. cmd_dtmf.duration_in_ms = duration_in_ms;
  4830. cmd_dtmf.high_freq = high_freq;
  4831. cmd_dtmf.low_freq = low_freq;
  4832. cmd_dtmf.gain = gain;
  4833. cmd_dtmf.num_ports = 1;
  4834. cmd_dtmf.port_ids = q6audio_get_port_id(this_afe.dtmf_gen_rx_portid);
  4835. atomic_set(&this_afe.state, 1);
  4836. atomic_set(&this_afe.status, 0);
  4837. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &cmd_dtmf);
  4838. if (ret < 0) {
  4839. pr_err("%s: AFE DTMF failed for num_ports:%d ids:0x%x\n",
  4840. __func__, cmd_dtmf.num_ports, cmd_dtmf.port_ids);
  4841. ret = -EINVAL;
  4842. goto fail_cmd;
  4843. }
  4844. index = q6audio_get_port_index(this_afe.dtmf_gen_rx_portid);
  4845. if (index < 0 || index >= AFE_MAX_PORTS) {
  4846. pr_err("%s: AFE port index[%d] invalid!\n",
  4847. __func__, index);
  4848. ret = -EINVAL;
  4849. goto fail_cmd;
  4850. }
  4851. ret = wait_event_timeout(this_afe.wait[index],
  4852. (atomic_read(&this_afe.state) == 0),
  4853. msecs_to_jiffies(TIMEOUT_MS));
  4854. if (!ret) {
  4855. pr_err("%s: wait_event timeout\n", __func__);
  4856. ret = -EINVAL;
  4857. goto fail_cmd;
  4858. }
  4859. if (atomic_read(&this_afe.status) > 0) {
  4860. pr_err("%s: config cmd failed [%s]\n",
  4861. __func__, adsp_err_get_err_str(
  4862. atomic_read(&this_afe.status)));
  4863. ret = adsp_err_get_lnx_err_code(
  4864. atomic_read(&this_afe.status));
  4865. goto fail_cmd;
  4866. }
  4867. return 0;
  4868. fail_cmd:
  4869. pr_err("%s: failed %d\n", __func__, ret);
  4870. return ret;
  4871. }
  4872. EXPORT_SYMBOL(afe_dtmf_generate_rx);
  4873. static int afe_sidetone_iir(u16 tx_port_id)
  4874. {
  4875. int ret;
  4876. uint16_t size = 0;
  4877. int cal_index = AFE_SIDETONE_IIR_CAL;
  4878. int iir_pregain = 0;
  4879. int iir_num_biquad_stages = 0;
  4880. int iir_enable;
  4881. struct cal_block_data *cal_block;
  4882. int mid;
  4883. struct afe_mod_enable_param enable;
  4884. struct afe_sidetone_iir_filter_config_params filter_data;
  4885. struct param_hdr_v3 param_hdr;
  4886. u8 *packed_param_data = NULL;
  4887. u32 packed_param_size = 0;
  4888. u32 single_param_size = 0;
  4889. struct audio_cal_info_sidetone_iir *st_iir_cal_info = NULL;
  4890. memset(&enable, 0, sizeof(enable));
  4891. memset(&filter_data, 0, sizeof(filter_data));
  4892. memset(&param_hdr, 0, sizeof(param_hdr));
  4893. if (this_afe.cal_data[cal_index] == NULL) {
  4894. pr_err("%s: cal data is NULL\n", __func__);
  4895. ret = -EINVAL;
  4896. goto done;
  4897. }
  4898. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  4899. cal_block = cal_utils_get_only_cal_block(this_afe.cal_data[cal_index]);
  4900. if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
  4901. pr_err("%s: cal_block not found\n ", __func__);
  4902. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  4903. ret = -EINVAL;
  4904. goto done;
  4905. }
  4906. /* Cache data from cal block while inside lock to reduce locked time */
  4907. st_iir_cal_info =
  4908. (struct audio_cal_info_sidetone_iir *) cal_block->cal_info;
  4909. iir_pregain = st_iir_cal_info->pregain;
  4910. iir_enable = st_iir_cal_info->iir_enable;
  4911. iir_num_biquad_stages = st_iir_cal_info->num_biquad_stages;
  4912. mid = st_iir_cal_info->mid;
  4913. /*
  4914. * calculate the actual size of payload based on no of stages
  4915. * enabled in calibration
  4916. */
  4917. size = (MAX_SIDETONE_IIR_DATA_SIZE / MAX_NO_IIR_FILTER_STAGE) *
  4918. iir_num_biquad_stages;
  4919. /*
  4920. * For an odd number of stages, 2 bytes of padding are
  4921. * required at the end of the payload.
  4922. */
  4923. if (iir_num_biquad_stages % 2) {
  4924. pr_debug("%s: adding 2 to size:%d\n", __func__, size);
  4925. size = size + 2;
  4926. }
  4927. memcpy(&filter_data.iir_config, &st_iir_cal_info->iir_config, size);
  4928. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  4929. packed_param_size =
  4930. sizeof(param_hdr) * 2 + sizeof(enable) + sizeof(filter_data);
  4931. packed_param_data = kzalloc(packed_param_size, GFP_KERNEL);
  4932. if (!packed_param_data)
  4933. return -ENOMEM;
  4934. packed_param_size = 0;
  4935. /*
  4936. * Set IIR enable params
  4937. */
  4938. param_hdr.module_id = mid;
  4939. param_hdr.param_id = INSTANCE_ID_0;
  4940. param_hdr.param_id = AFE_PARAM_ID_ENABLE;
  4941. param_hdr.param_size = sizeof(enable);
  4942. enable.enable = iir_enable;
  4943. ret = q6common_pack_pp_params(packed_param_data, &param_hdr,
  4944. (u8 *) &enable, &single_param_size);
  4945. if (ret) {
  4946. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  4947. ret);
  4948. goto done;
  4949. }
  4950. packed_param_size += single_param_size;
  4951. /*
  4952. * Set IIR filter config params
  4953. */
  4954. param_hdr.module_id = mid;
  4955. param_hdr.instance_id = INSTANCE_ID_0;
  4956. param_hdr.param_id = AFE_PARAM_ID_SIDETONE_IIR_FILTER_CONFIG;
  4957. param_hdr.param_size = sizeof(filter_data.num_biquad_stages) +
  4958. sizeof(filter_data.pregain) + size;
  4959. filter_data.num_biquad_stages = iir_num_biquad_stages;
  4960. filter_data.pregain = iir_pregain;
  4961. ret = q6common_pack_pp_params(packed_param_data + packed_param_size,
  4962. &param_hdr, (u8 *) &filter_data,
  4963. &single_param_size);
  4964. if (ret) {
  4965. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  4966. ret);
  4967. goto done;
  4968. }
  4969. packed_param_size += single_param_size;
  4970. pr_debug("%s: tx(0x%x)mid(0x%x)iir_en(%d)stg(%d)gain(0x%x)size(%d)\n",
  4971. __func__, tx_port_id, mid, enable.enable,
  4972. filter_data.num_biquad_stages, filter_data.pregain,
  4973. param_hdr.param_size);
  4974. ret = q6afe_set_params(tx_port_id, q6audio_get_port_index(tx_port_id),
  4975. NULL, packed_param_data, packed_param_size);
  4976. if (ret)
  4977. pr_err("%s: AFE sidetone failed for tx_port(0x%x)\n",
  4978. __func__, tx_port_id);
  4979. done:
  4980. kfree(packed_param_data);
  4981. return ret;
  4982. }
  4983. static int afe_sidetone(u16 tx_port_id, u16 rx_port_id, bool enable)
  4984. {
  4985. int ret;
  4986. int cal_index = AFE_SIDETONE_CAL;
  4987. int sidetone_gain;
  4988. int sidetone_enable;
  4989. struct cal_block_data *cal_block;
  4990. int mid = 0;
  4991. struct afe_loopback_sidetone_gain gain_data;
  4992. struct loopback_cfg_data cfg_data;
  4993. struct param_hdr_v3 param_hdr;
  4994. u8 *packed_param_data = NULL;
  4995. u32 packed_param_size = 0;
  4996. u32 single_param_size = 0;
  4997. struct audio_cal_info_sidetone *st_cal_info = NULL;
  4998. if (this_afe.cal_data[cal_index] == NULL) {
  4999. pr_err("%s: cal data is NULL\n", __func__);
  5000. ret = -EINVAL;
  5001. goto done;
  5002. }
  5003. memset(&gain_data, 0, sizeof(gain_data));
  5004. memset(&cfg_data, 0, sizeof(cfg_data));
  5005. memset(&param_hdr, 0, sizeof(param_hdr));
  5006. packed_param_size =
  5007. sizeof(param_hdr) * 2 + sizeof(gain_data) + sizeof(cfg_data);
  5008. packed_param_data = kzalloc(packed_param_size, GFP_KERNEL);
  5009. if (!packed_param_data)
  5010. return -ENOMEM;
  5011. packed_param_size = 0;
  5012. mutex_lock(&this_afe.cal_data[cal_index]->lock);
  5013. cal_block = cal_utils_get_only_cal_block(this_afe.cal_data[cal_index]);
  5014. if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
  5015. pr_err("%s: cal_block not found\n", __func__);
  5016. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  5017. ret = -EINVAL;
  5018. goto done;
  5019. }
  5020. /* Cache data from cal block while inside lock to reduce locked time */
  5021. st_cal_info = (struct audio_cal_info_sidetone *) cal_block->cal_info;
  5022. sidetone_gain = st_cal_info->gain;
  5023. sidetone_enable = st_cal_info->enable;
  5024. mid = st_cal_info->mid;
  5025. mutex_unlock(&this_afe.cal_data[cal_index]->lock);
  5026. /* Set gain data. */
  5027. param_hdr.module_id = AFE_MODULE_LOOPBACK;
  5028. param_hdr.instance_id = INSTANCE_ID_0;
  5029. param_hdr.param_id = AFE_PARAM_ID_LOOPBACK_GAIN_PER_PATH;
  5030. param_hdr.param_size = sizeof(struct afe_loopback_sidetone_gain);
  5031. gain_data.rx_port_id = rx_port_id;
  5032. gain_data.gain = sidetone_gain;
  5033. ret = q6common_pack_pp_params(packed_param_data, &param_hdr,
  5034. (u8 *) &gain_data, &single_param_size);
  5035. if (ret) {
  5036. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  5037. ret);
  5038. goto done;
  5039. }
  5040. packed_param_size += single_param_size;
  5041. /* Set configuration data. */
  5042. param_hdr.module_id = AFE_MODULE_LOOPBACK;
  5043. param_hdr.instance_id = INSTANCE_ID_0;
  5044. param_hdr.param_id = AFE_PARAM_ID_LOOPBACK_CONFIG;
  5045. param_hdr.param_size = sizeof(struct loopback_cfg_data);
  5046. cfg_data.loopback_cfg_minor_version = AFE_API_VERSION_LOOPBACK_CONFIG;
  5047. cfg_data.dst_port_id = rx_port_id;
  5048. cfg_data.routing_mode = LB_MODE_SIDETONE;
  5049. cfg_data.enable = enable;
  5050. ret = q6common_pack_pp_params(packed_param_data + packed_param_size,
  5051. &param_hdr, (u8 *) &cfg_data,
  5052. &single_param_size);
  5053. if (ret) {
  5054. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  5055. ret);
  5056. goto done;
  5057. }
  5058. packed_param_size += single_param_size;
  5059. pr_debug("%s rx(0x%x) tx(0x%x) enable(%d) mid(0x%x) gain(%d) sidetone_enable(%d)\n",
  5060. __func__, rx_port_id, tx_port_id,
  5061. enable, mid, sidetone_gain, sidetone_enable);
  5062. ret = q6afe_set_params(tx_port_id, q6audio_get_port_index(tx_port_id),
  5063. NULL, packed_param_data, packed_param_size);
  5064. if (ret)
  5065. pr_err("%s: AFE sidetone send failed for tx_port:%d rx_port:%d ret:%d\n",
  5066. __func__, tx_port_id, rx_port_id, ret);
  5067. done:
  5068. kfree(packed_param_data);
  5069. return ret;
  5070. }
  5071. int afe_sidetone_enable(u16 tx_port_id, u16 rx_port_id, bool enable)
  5072. {
  5073. int ret;
  5074. int index;
  5075. index = q6audio_get_port_index(rx_port_id);
  5076. if (index < 0 || index >= AFE_MAX_PORTS) {
  5077. pr_err("%s: AFE port index[%d] invalid!\n",
  5078. __func__, index);
  5079. ret = -EINVAL;
  5080. goto done;
  5081. }
  5082. if (q6audio_validate_port(rx_port_id) < 0) {
  5083. pr_err("%s: Invalid port 0x%x\n",
  5084. __func__, rx_port_id);
  5085. ret = -EINVAL;
  5086. goto done;
  5087. }
  5088. index = q6audio_get_port_index(tx_port_id);
  5089. if (index < 0 || index >= AFE_MAX_PORTS) {
  5090. pr_err("%s: AFE port index[%d] invalid!\n",
  5091. __func__, index);
  5092. ret = -EINVAL;
  5093. goto done;
  5094. }
  5095. if (q6audio_validate_port(tx_port_id) < 0) {
  5096. pr_err("%s: Invalid port 0x%x\n",
  5097. __func__, tx_port_id);
  5098. ret = -EINVAL;
  5099. goto done;
  5100. }
  5101. if (enable) {
  5102. ret = afe_sidetone_iir(tx_port_id);
  5103. if (ret)
  5104. goto done;
  5105. }
  5106. ret = afe_sidetone(tx_port_id, rx_port_id, enable);
  5107. done:
  5108. return ret;
  5109. }
  5110. /**
  5111. * afe_set_display_stream - command to update AFE dp port params
  5112. *
  5113. * @rx_port_id: AFE port id
  5114. * @stream_idx: dp controller stream index
  5115. * @ctl_idx: dp controller index
  5116. *
  5117. * Returns 0 on success, appropriate error code otherwise
  5118. */
  5119. int afe_set_display_stream(u16 rx_port_id, u32 stream_idx, u32 ctl_idx)
  5120. {
  5121. int ret;
  5122. struct param_hdr_v3 param_hdr;
  5123. u32 packed_param_size = 0;
  5124. u8 *packed_param_data = NULL;
  5125. struct afe_display_stream_idx stream_data;
  5126. struct afe_display_ctl_idx ctl_data;
  5127. u32 single_param_size = 0;
  5128. memset(&param_hdr, 0, sizeof(param_hdr));
  5129. memset(&stream_data, 0, sizeof(stream_data));
  5130. memset(&ctl_data, 0, sizeof(ctl_data));
  5131. packed_param_size =
  5132. sizeof(param_hdr) * 2 + sizeof(stream_data) + sizeof(ctl_data);
  5133. packed_param_data = kzalloc(packed_param_size, GFP_KERNEL);
  5134. if (!packed_param_data)
  5135. return -ENOMEM;
  5136. packed_param_size = 0;
  5137. /* Set stream index */
  5138. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5139. param_hdr.instance_id = INSTANCE_ID_0;
  5140. param_hdr.param_id = AFE_PARAM_ID_HDMI_DP_MST_VID_IDX_CFG;
  5141. param_hdr.param_size = sizeof(struct afe_display_stream_idx);
  5142. stream_data.minor_version = 1;
  5143. stream_data.stream_idx = stream_idx;
  5144. ret = q6common_pack_pp_params(packed_param_data, &param_hdr,
  5145. (u8 *) &stream_data, &single_param_size);
  5146. if (ret) {
  5147. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  5148. ret);
  5149. goto done;
  5150. }
  5151. packed_param_size += single_param_size;
  5152. /* Set controller dptx index */
  5153. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5154. param_hdr.instance_id = INSTANCE_ID_0;
  5155. param_hdr.param_id = AFE_PARAM_ID_HDMI_DPTX_IDX_CFG;
  5156. param_hdr.param_size = sizeof(struct afe_display_ctl_idx);
  5157. ctl_data.minor_version = 1;
  5158. ctl_data.ctl_idx = ctl_idx;
  5159. ret = q6common_pack_pp_params(packed_param_data + packed_param_size,
  5160. &param_hdr, (u8 *) &ctl_data,
  5161. &single_param_size);
  5162. if (ret) {
  5163. pr_err("%s: Failed to pack param data, error %d\n", __func__,
  5164. ret);
  5165. goto done;
  5166. }
  5167. packed_param_size += single_param_size;
  5168. pr_debug("%s: rx(0x%x) stream(%d) controller(%d)\n",
  5169. __func__, rx_port_id, stream_idx, ctl_idx);
  5170. ret = q6afe_set_params(rx_port_id, q6audio_get_port_index(rx_port_id),
  5171. NULL, packed_param_data, packed_param_size);
  5172. if (ret)
  5173. pr_err("%s: AFE display stream send failed for rx_port:%d ret:%d\n",
  5174. __func__, rx_port_id, ret);
  5175. done:
  5176. kfree(packed_param_data);
  5177. return ret;
  5178. }
  5179. EXPORT_SYMBOL(afe_set_display_stream);
  5180. int afe_validate_port(u16 port_id)
  5181. {
  5182. int ret;
  5183. switch (port_id) {
  5184. case PRIMARY_I2S_RX:
  5185. case PRIMARY_I2S_TX:
  5186. case AFE_PORT_ID_PRIMARY_PCM_RX:
  5187. case AFE_PORT_ID_PRIMARY_PCM_TX:
  5188. case AFE_PORT_ID_SECONDARY_PCM_RX:
  5189. case AFE_PORT_ID_SECONDARY_PCM_TX:
  5190. case AFE_PORT_ID_TERTIARY_PCM_RX:
  5191. case AFE_PORT_ID_TERTIARY_PCM_TX:
  5192. case AFE_PORT_ID_QUATERNARY_PCM_RX:
  5193. case AFE_PORT_ID_QUATERNARY_PCM_TX:
  5194. case AFE_PORT_ID_QUINARY_PCM_RX:
  5195. case AFE_PORT_ID_QUINARY_PCM_TX:
  5196. case SECONDARY_I2S_RX:
  5197. case SECONDARY_I2S_TX:
  5198. case MI2S_RX:
  5199. case MI2S_TX:
  5200. case HDMI_RX:
  5201. case DISPLAY_PORT_RX:
  5202. case AFE_PORT_ID_SPDIF_RX:
  5203. case RSVD_2:
  5204. case RSVD_3:
  5205. case DIGI_MIC_TX:
  5206. case VOICE_RECORD_RX:
  5207. case VOICE_RECORD_TX:
  5208. case VOICE_PLAYBACK_TX:
  5209. case VOICE2_PLAYBACK_TX:
  5210. case SLIMBUS_0_RX:
  5211. case SLIMBUS_0_TX:
  5212. case SLIMBUS_1_RX:
  5213. case SLIMBUS_1_TX:
  5214. case SLIMBUS_2_RX:
  5215. case SLIMBUS_2_TX:
  5216. case SLIMBUS_3_RX:
  5217. case INT_BT_SCO_RX:
  5218. case INT_BT_SCO_TX:
  5219. case INT_BT_A2DP_RX:
  5220. case INT_FM_RX:
  5221. case INT_FM_TX:
  5222. case RT_PROXY_PORT_001_RX:
  5223. case RT_PROXY_PORT_001_TX:
  5224. case SLIMBUS_4_RX:
  5225. case SLIMBUS_4_TX:
  5226. case SLIMBUS_5_RX:
  5227. case SLIMBUS_6_RX:
  5228. case SLIMBUS_6_TX:
  5229. case SLIMBUS_7_RX:
  5230. case SLIMBUS_7_TX:
  5231. case SLIMBUS_8_RX:
  5232. case SLIMBUS_8_TX:
  5233. case AFE_PORT_ID_USB_RX:
  5234. case AFE_PORT_ID_USB_TX:
  5235. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  5236. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  5237. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  5238. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  5239. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  5240. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  5241. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  5242. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  5243. case AFE_PORT_ID_QUINARY_MI2S_RX:
  5244. case AFE_PORT_ID_QUINARY_MI2S_TX:
  5245. case AFE_PORT_ID_SENARY_MI2S_TX:
  5246. case AFE_PORT_ID_PRIMARY_TDM_RX:
  5247. case AFE_PORT_ID_PRIMARY_TDM_TX:
  5248. case AFE_PORT_ID_PRIMARY_TDM_RX_1:
  5249. case AFE_PORT_ID_PRIMARY_TDM_TX_1:
  5250. case AFE_PORT_ID_PRIMARY_TDM_RX_2:
  5251. case AFE_PORT_ID_PRIMARY_TDM_TX_2:
  5252. case AFE_PORT_ID_PRIMARY_TDM_RX_3:
  5253. case AFE_PORT_ID_PRIMARY_TDM_TX_3:
  5254. case AFE_PORT_ID_PRIMARY_TDM_RX_4:
  5255. case AFE_PORT_ID_PRIMARY_TDM_TX_4:
  5256. case AFE_PORT_ID_PRIMARY_TDM_RX_5:
  5257. case AFE_PORT_ID_PRIMARY_TDM_TX_5:
  5258. case AFE_PORT_ID_PRIMARY_TDM_RX_6:
  5259. case AFE_PORT_ID_PRIMARY_TDM_TX_6:
  5260. case AFE_PORT_ID_PRIMARY_TDM_RX_7:
  5261. case AFE_PORT_ID_PRIMARY_TDM_TX_7:
  5262. case AFE_PORT_ID_SECONDARY_TDM_RX:
  5263. case AFE_PORT_ID_SECONDARY_TDM_TX:
  5264. case AFE_PORT_ID_SECONDARY_TDM_RX_1:
  5265. case AFE_PORT_ID_SECONDARY_TDM_TX_1:
  5266. case AFE_PORT_ID_SECONDARY_TDM_RX_2:
  5267. case AFE_PORT_ID_SECONDARY_TDM_TX_2:
  5268. case AFE_PORT_ID_SECONDARY_TDM_RX_3:
  5269. case AFE_PORT_ID_SECONDARY_TDM_TX_3:
  5270. case AFE_PORT_ID_SECONDARY_TDM_RX_4:
  5271. case AFE_PORT_ID_SECONDARY_TDM_TX_4:
  5272. case AFE_PORT_ID_SECONDARY_TDM_RX_5:
  5273. case AFE_PORT_ID_SECONDARY_TDM_TX_5:
  5274. case AFE_PORT_ID_SECONDARY_TDM_RX_6:
  5275. case AFE_PORT_ID_SECONDARY_TDM_TX_6:
  5276. case AFE_PORT_ID_SECONDARY_TDM_RX_7:
  5277. case AFE_PORT_ID_SECONDARY_TDM_TX_7:
  5278. case AFE_PORT_ID_TERTIARY_TDM_RX:
  5279. case AFE_PORT_ID_TERTIARY_TDM_TX:
  5280. case AFE_PORT_ID_TERTIARY_TDM_RX_1:
  5281. case AFE_PORT_ID_TERTIARY_TDM_TX_1:
  5282. case AFE_PORT_ID_TERTIARY_TDM_RX_2:
  5283. case AFE_PORT_ID_TERTIARY_TDM_TX_2:
  5284. case AFE_PORT_ID_TERTIARY_TDM_RX_3:
  5285. case AFE_PORT_ID_TERTIARY_TDM_TX_3:
  5286. case AFE_PORT_ID_TERTIARY_TDM_RX_4:
  5287. case AFE_PORT_ID_TERTIARY_TDM_TX_4:
  5288. case AFE_PORT_ID_TERTIARY_TDM_RX_5:
  5289. case AFE_PORT_ID_TERTIARY_TDM_TX_5:
  5290. case AFE_PORT_ID_TERTIARY_TDM_RX_6:
  5291. case AFE_PORT_ID_TERTIARY_TDM_TX_6:
  5292. case AFE_PORT_ID_TERTIARY_TDM_RX_7:
  5293. case AFE_PORT_ID_TERTIARY_TDM_TX_7:
  5294. case AFE_PORT_ID_QUATERNARY_TDM_RX:
  5295. case AFE_PORT_ID_QUATERNARY_TDM_TX:
  5296. case AFE_PORT_ID_QUATERNARY_TDM_RX_1:
  5297. case AFE_PORT_ID_QUATERNARY_TDM_TX_1:
  5298. case AFE_PORT_ID_QUATERNARY_TDM_RX_2:
  5299. case AFE_PORT_ID_QUATERNARY_TDM_TX_2:
  5300. case AFE_PORT_ID_QUATERNARY_TDM_RX_3:
  5301. case AFE_PORT_ID_QUATERNARY_TDM_TX_3:
  5302. case AFE_PORT_ID_QUATERNARY_TDM_RX_4:
  5303. case AFE_PORT_ID_QUATERNARY_TDM_TX_4:
  5304. case AFE_PORT_ID_QUATERNARY_TDM_RX_5:
  5305. case AFE_PORT_ID_QUATERNARY_TDM_TX_5:
  5306. case AFE_PORT_ID_QUATERNARY_TDM_RX_6:
  5307. case AFE_PORT_ID_QUATERNARY_TDM_TX_6:
  5308. case AFE_PORT_ID_QUATERNARY_TDM_RX_7:
  5309. case AFE_PORT_ID_QUATERNARY_TDM_TX_7:
  5310. case AFE_PORT_ID_QUINARY_TDM_RX:
  5311. case AFE_PORT_ID_QUINARY_TDM_TX:
  5312. case AFE_PORT_ID_QUINARY_TDM_RX_1:
  5313. case AFE_PORT_ID_QUINARY_TDM_TX_1:
  5314. case AFE_PORT_ID_QUINARY_TDM_RX_2:
  5315. case AFE_PORT_ID_QUINARY_TDM_TX_2:
  5316. case AFE_PORT_ID_QUINARY_TDM_RX_3:
  5317. case AFE_PORT_ID_QUINARY_TDM_TX_3:
  5318. case AFE_PORT_ID_QUINARY_TDM_RX_4:
  5319. case AFE_PORT_ID_QUINARY_TDM_TX_4:
  5320. case AFE_PORT_ID_QUINARY_TDM_RX_5:
  5321. case AFE_PORT_ID_QUINARY_TDM_TX_5:
  5322. case AFE_PORT_ID_QUINARY_TDM_RX_6:
  5323. case AFE_PORT_ID_QUINARY_TDM_TX_6:
  5324. case AFE_PORT_ID_QUINARY_TDM_RX_7:
  5325. case AFE_PORT_ID_QUINARY_TDM_TX_7:
  5326. case AFE_PORT_ID_INT0_MI2S_RX:
  5327. case AFE_PORT_ID_INT1_MI2S_RX:
  5328. case AFE_PORT_ID_INT2_MI2S_RX:
  5329. case AFE_PORT_ID_INT3_MI2S_RX:
  5330. case AFE_PORT_ID_INT4_MI2S_RX:
  5331. case AFE_PORT_ID_INT5_MI2S_RX:
  5332. case AFE_PORT_ID_INT6_MI2S_RX:
  5333. case AFE_PORT_ID_INT0_MI2S_TX:
  5334. case AFE_PORT_ID_INT1_MI2S_TX:
  5335. case AFE_PORT_ID_INT2_MI2S_TX:
  5336. case AFE_PORT_ID_INT3_MI2S_TX:
  5337. case AFE_PORT_ID_INT4_MI2S_TX:
  5338. case AFE_PORT_ID_INT5_MI2S_TX:
  5339. case AFE_PORT_ID_INT6_MI2S_TX:
  5340. {
  5341. ret = 0;
  5342. break;
  5343. }
  5344. default:
  5345. pr_err("%s: default ret 0x%x\n", __func__, port_id);
  5346. ret = -EINVAL;
  5347. }
  5348. return ret;
  5349. }
  5350. int afe_convert_virtual_to_portid(u16 port_id)
  5351. {
  5352. int ret;
  5353. /*
  5354. * if port_id is virtual, convert to physical..
  5355. * if port_id is already physical, return physical
  5356. */
  5357. if (afe_validate_port(port_id) < 0) {
  5358. if (port_id == RT_PROXY_DAI_001_RX ||
  5359. port_id == RT_PROXY_DAI_001_TX ||
  5360. port_id == RT_PROXY_DAI_002_RX ||
  5361. port_id == RT_PROXY_DAI_002_TX) {
  5362. ret = VIRTUAL_ID_TO_PORTID(port_id);
  5363. } else {
  5364. pr_err("%s: wrong port 0x%x\n",
  5365. __func__, port_id);
  5366. ret = -EINVAL;
  5367. }
  5368. } else
  5369. ret = port_id;
  5370. return ret;
  5371. }
  5372. int afe_port_stop_nowait(int port_id)
  5373. {
  5374. struct afe_port_cmd_device_stop stop;
  5375. int ret = 0;
  5376. if (this_afe.apr == NULL) {
  5377. pr_err("%s: AFE is already closed\n", __func__);
  5378. ret = -EINVAL;
  5379. goto fail_cmd;
  5380. }
  5381. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  5382. port_id = q6audio_convert_virtual_to_portid(port_id);
  5383. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5384. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5385. stop.hdr.pkt_size = sizeof(stop);
  5386. stop.hdr.src_port = 0;
  5387. stop.hdr.dest_port = 0;
  5388. stop.hdr.token = 0;
  5389. stop.hdr.opcode = AFE_PORT_CMD_DEVICE_STOP;
  5390. stop.port_id = port_id;
  5391. stop.reserved = 0;
  5392. ret = afe_apr_send_pkt(&stop, NULL);
  5393. if (ret)
  5394. pr_err("%s: AFE close failed %d\n", __func__, ret);
  5395. fail_cmd:
  5396. return ret;
  5397. }
  5398. /**
  5399. * afe_close - command to close AFE port
  5400. *
  5401. * @port_id: AFE port id
  5402. *
  5403. * Returns 0 on success, appropriate error code otherwise
  5404. */
  5405. int afe_close(int port_id)
  5406. {
  5407. struct afe_port_cmd_device_stop stop;
  5408. enum afe_mad_type mad_type;
  5409. int ret = 0;
  5410. int index = 0;
  5411. uint16_t port_index;
  5412. if (this_afe.apr == NULL) {
  5413. pr_err("%s: AFE is already closed\n", __func__);
  5414. if ((port_id == RT_PROXY_DAI_001_RX) ||
  5415. (port_id == RT_PROXY_DAI_002_TX))
  5416. pcm_afe_instance[port_id & 0x1] = 0;
  5417. if ((port_id == RT_PROXY_DAI_002_RX) ||
  5418. (port_id == RT_PROXY_DAI_001_TX))
  5419. proxy_afe_instance[port_id & 0x1] = 0;
  5420. afe_close_done[port_id & 0x1] = true;
  5421. ret = -EINVAL;
  5422. goto fail_cmd;
  5423. }
  5424. pr_debug("%s: port_id = 0x%x\n", __func__, port_id);
  5425. if ((port_id == RT_PROXY_DAI_001_RX) ||
  5426. (port_id == RT_PROXY_DAI_002_TX)) {
  5427. pr_debug("%s: before decrementing pcm_afe_instance %d\n",
  5428. __func__, pcm_afe_instance[port_id & 0x1]);
  5429. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  5430. pcm_afe_instance[port_id & 0x1]--;
  5431. if ((!(pcm_afe_instance[port_id & 0x1] == 0 &&
  5432. proxy_afe_instance[port_id & 0x1] == 0)) ||
  5433. afe_close_done[port_id & 0x1] == true)
  5434. return 0;
  5435. afe_close_done[port_id & 0x1] = true;
  5436. }
  5437. if ((port_id == RT_PROXY_DAI_002_RX) ||
  5438. (port_id == RT_PROXY_DAI_001_TX)) {
  5439. pr_debug("%s: before decrementing proxy_afe_instance %d\n",
  5440. __func__, proxy_afe_instance[port_id & 0x1]);
  5441. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  5442. proxy_afe_instance[port_id & 0x1]--;
  5443. if ((!(pcm_afe_instance[port_id & 0x1] == 0 &&
  5444. proxy_afe_instance[port_id & 0x1] == 0)) ||
  5445. afe_close_done[port_id & 0x1] == true)
  5446. return 0;
  5447. afe_close_done[port_id & 0x1] = true;
  5448. }
  5449. port_id = q6audio_convert_virtual_to_portid(port_id);
  5450. index = q6audio_get_port_index(port_id);
  5451. if (index < 0 || index >= AFE_MAX_PORTS) {
  5452. pr_err("%s: AFE port index[%d] invalid!\n",
  5453. __func__, index);
  5454. return -EINVAL;
  5455. }
  5456. ret = q6audio_validate_port(port_id);
  5457. if (ret < 0) {
  5458. pr_warn("%s: Not a valid port id 0x%x ret %d\n",
  5459. __func__, port_id, ret);
  5460. return -EINVAL;
  5461. }
  5462. mad_type = afe_port_get_mad_type(port_id);
  5463. pr_debug("%s: port_id 0x%x, mad_type %d\n", __func__, port_id,
  5464. mad_type);
  5465. if (mad_type != MAD_HW_NONE && mad_type != MAD_SW_AUDIO) {
  5466. pr_debug("%s: Turn off MAD\n", __func__);
  5467. ret = afe_turn_onoff_hw_mad(mad_type, false);
  5468. if (ret) {
  5469. pr_err("%s: afe_turn_onoff_hw_mad failed %d\n",
  5470. __func__, ret);
  5471. return ret;
  5472. }
  5473. } else {
  5474. pr_debug("%s: Not a MAD port\n", __func__);
  5475. }
  5476. port_index = afe_get_port_index(port_id);
  5477. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  5478. this_afe.afe_sample_rates[port_index] = 0;
  5479. this_afe.topology[port_index] = 0;
  5480. this_afe.dev_acdb_id[port_index] = 0;
  5481. } else {
  5482. pr_err("%s: port %d\n", __func__, port_index);
  5483. ret = -EINVAL;
  5484. goto fail_cmd;
  5485. }
  5486. if ((port_id == this_afe.aanc_info.aanc_tx_port) &&
  5487. (this_afe.aanc_info.aanc_active)) {
  5488. memset(&this_afe.aanc_info, 0x00, sizeof(this_afe.aanc_info));
  5489. ret = afe_aanc_mod_enable(this_afe.apr, port_id, 0);
  5490. if (ret)
  5491. pr_err("%s: AFE mod disable failed %d\n",
  5492. __func__, ret);
  5493. }
  5494. /*
  5495. * even if ramp down configuration failed it is not serious enough to
  5496. * warrant bailaing out.
  5497. */
  5498. if (afe_spk_ramp_dn_cfg(port_id) < 0)
  5499. pr_err("%s: ramp down configuration failed\n", __func__);
  5500. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5501. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5502. stop.hdr.pkt_size = sizeof(stop);
  5503. stop.hdr.src_port = 0;
  5504. stop.hdr.dest_port = 0;
  5505. stop.hdr.token = index;
  5506. stop.hdr.opcode = AFE_PORT_CMD_DEVICE_STOP;
  5507. stop.port_id = q6audio_get_port_id(port_id);
  5508. stop.reserved = 0;
  5509. ret = afe_apr_send_pkt(&stop, &this_afe.wait[index]);
  5510. if (ret)
  5511. pr_err("%s: AFE close failed %d\n", __func__, ret);
  5512. fail_cmd:
  5513. return ret;
  5514. }
  5515. EXPORT_SYMBOL(afe_close);
  5516. int afe_set_digital_codec_core_clock(u16 port_id,
  5517. struct afe_digital_clk_cfg *cfg)
  5518. {
  5519. struct afe_digital_clk_cfg clk_cfg;
  5520. struct param_hdr_v3 param_hdr;
  5521. int ret = 0;
  5522. if (!cfg) {
  5523. pr_err("%s: clock cfg is NULL\n", __func__);
  5524. return -EINVAL;
  5525. }
  5526. memset(&clk_cfg, 0, sizeof(clk_cfg));
  5527. memset(&param_hdr, 0, sizeof(param_hdr));
  5528. /*default rx port is taken to enable the codec digital clock*/
  5529. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5530. param_hdr.instance_id = INSTANCE_ID_0;
  5531. param_hdr.param_id = AFE_PARAM_ID_INTERNAL_DIGIATL_CDC_CLK_CONFIG;
  5532. param_hdr.param_size = sizeof(struct afe_digital_clk_cfg);
  5533. clk_cfg = *cfg;
  5534. pr_debug("%s: Minor version =0x%x clk val = %d\n"
  5535. "clk root = 0x%x resrv = 0x%x\n",
  5536. __func__, cfg->i2s_cfg_minor_version, cfg->clk_val,
  5537. cfg->clk_root, cfg->reserved);
  5538. ret = q6afe_pack_and_set_param_in_band(port_id,
  5539. q6audio_get_port_index(port_id),
  5540. param_hdr, (u8 *) &clk_cfg);
  5541. if (ret < 0)
  5542. pr_err("%s: AFE enable for port 0x%x ret %d\n", __func__,
  5543. port_id, ret);
  5544. return ret;
  5545. }
  5546. /**
  5547. * afe_set_lpass_clock - Enable AFE lpass clock
  5548. *
  5549. * @port_id: AFE port id
  5550. * @cfg: pointer to clk set struct
  5551. *
  5552. * Returns 0 on success, appropriate error code otherwise
  5553. */
  5554. int afe_set_lpass_clock(u16 port_id, struct afe_clk_cfg *cfg)
  5555. {
  5556. struct afe_clk_cfg clk_cfg;
  5557. struct param_hdr_v3 param_hdr;
  5558. int ret = 0;
  5559. if (!cfg) {
  5560. pr_err("%s: clock cfg is NULL\n", __func__);
  5561. return -EINVAL;
  5562. }
  5563. memset(&clk_cfg, 0, sizeof(clk_cfg));
  5564. memset(&param_hdr, 0, sizeof(param_hdr));
  5565. ret = q6audio_is_digital_pcm_interface(port_id);
  5566. if (ret < 0) {
  5567. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  5568. __func__, ret);
  5569. return -EINVAL;
  5570. }
  5571. mutex_lock(&this_afe.afe_cmd_lock);
  5572. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5573. param_hdr.instance_id = INSTANCE_ID_0;
  5574. param_hdr.param_id = AFE_PARAM_ID_LPAIF_CLK_CONFIG;
  5575. param_hdr.param_size = sizeof(clk_cfg);
  5576. clk_cfg = *cfg;
  5577. pr_debug("%s: Minor version =0x%x clk val1 = %d\n"
  5578. "clk val2 = %d, clk src = 0x%x\n"
  5579. "clk root = 0x%x clk mode = 0x%x resrv = 0x%x\n"
  5580. "port id = 0x%x\n",
  5581. __func__, cfg->i2s_cfg_minor_version,
  5582. cfg->clk_val1, cfg->clk_val2, cfg->clk_src,
  5583. cfg->clk_root, cfg->clk_set_mode,
  5584. cfg->reserved, q6audio_get_port_id(port_id));
  5585. ret = q6afe_pack_and_set_param_in_band(port_id,
  5586. q6audio_get_port_index(port_id),
  5587. param_hdr, (u8 *) &clk_cfg);
  5588. if (ret < 0)
  5589. pr_err("%s: AFE enable for port 0x%x ret %d\n",
  5590. __func__, port_id, ret);
  5591. mutex_unlock(&this_afe.afe_cmd_lock);
  5592. return ret;
  5593. }
  5594. EXPORT_SYMBOL(afe_set_lpass_clock);
  5595. /**
  5596. * afe_set_lpass_clk_cfg - Set AFE clk config
  5597. *
  5598. * @index: port index
  5599. * @cfg: pointer to clk set struct
  5600. *
  5601. * Returns 0 on success, appropriate error code otherwise
  5602. */
  5603. int afe_set_lpass_clk_cfg(int index, struct afe_clk_set *cfg)
  5604. {
  5605. struct param_hdr_v3 param_hdr;
  5606. int ret = 0;
  5607. if (!cfg) {
  5608. pr_err("%s: clock cfg is NULL\n", __func__);
  5609. ret = -EINVAL;
  5610. return ret;
  5611. }
  5612. if (index < 0 || index >= AFE_MAX_PORTS) {
  5613. pr_err("%s: index[%d] invalid!\n", __func__, index);
  5614. return -EINVAL;
  5615. }
  5616. memset(&param_hdr, 0, sizeof(param_hdr));
  5617. ret = afe_q6_interface_prepare();
  5618. if (ret != 0) {
  5619. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  5620. return ret;
  5621. }
  5622. mutex_lock(&this_afe.afe_cmd_lock);
  5623. param_hdr.module_id = AFE_MODULE_CLOCK_SET;
  5624. param_hdr.instance_id = INSTANCE_ID_0;
  5625. param_hdr.param_id = AFE_PARAM_ID_CLOCK_SET;
  5626. param_hdr.param_size = sizeof(struct afe_clk_set);
  5627. pr_debug("%s: Minor version =0x%x clk id = %d\n"
  5628. "clk freq (Hz) = %d, clk attri = 0x%x\n"
  5629. "clk root = 0x%x clk enable = 0x%x\n",
  5630. __func__, cfg->clk_set_minor_version,
  5631. cfg->clk_id, cfg->clk_freq_in_hz, cfg->clk_attri,
  5632. cfg->clk_root, cfg->enable);
  5633. ret = q6afe_svc_pack_and_set_param_in_band(index, param_hdr,
  5634. (u8 *) cfg);
  5635. if (ret < 0)
  5636. pr_err("%s: AFE clk cfg failed with ret %d\n",
  5637. __func__, ret);
  5638. mutex_unlock(&this_afe.afe_cmd_lock);
  5639. return ret;
  5640. }
  5641. EXPORT_SYMBOL(afe_set_lpass_clk_cfg);
  5642. /**
  5643. * afe_set_lpass_clock_v2 - Enable AFE lpass clock
  5644. *
  5645. * @port_id: AFE port id
  5646. * @cfg: pointer to clk set struct
  5647. *
  5648. * Returns 0 on success, appropriate error code otherwise
  5649. */
  5650. int afe_set_lpass_clock_v2(u16 port_id, struct afe_clk_set *cfg)
  5651. {
  5652. int index = 0;
  5653. int ret = 0;
  5654. index = q6audio_get_port_index(port_id);
  5655. if (index < 0 || index >= AFE_MAX_PORTS) {
  5656. pr_err("%s: AFE port index[%d] invalid!\n",
  5657. __func__, index);
  5658. return -EINVAL;
  5659. }
  5660. ret = q6audio_is_digital_pcm_interface(port_id);
  5661. if (ret < 0) {
  5662. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  5663. __func__, ret);
  5664. return -EINVAL;
  5665. }
  5666. ret = afe_set_lpass_clk_cfg(index, cfg);
  5667. if (ret)
  5668. pr_err("%s: afe_set_lpass_clk_cfg_v2 failed %d\n",
  5669. __func__, ret);
  5670. return ret;
  5671. }
  5672. EXPORT_SYMBOL(afe_set_lpass_clock_v2);
  5673. int afe_set_lpass_internal_digital_codec_clock(u16 port_id,
  5674. struct afe_digital_clk_cfg *cfg)
  5675. {
  5676. struct afe_digital_clk_cfg clk_cfg;
  5677. struct param_hdr_v3 param_hdr;
  5678. int ret = 0;
  5679. if (!cfg) {
  5680. pr_err("%s: clock cfg is NULL\n", __func__);
  5681. return -EINVAL;
  5682. }
  5683. memset(&clk_cfg, 0, sizeof(clk_cfg));
  5684. memset(&param_hdr, 0, sizeof(param_hdr));
  5685. ret = q6audio_is_digital_pcm_interface(port_id);
  5686. if (ret < 0) {
  5687. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  5688. __func__, ret);
  5689. return -EINVAL;
  5690. }
  5691. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5692. param_hdr.instance_id = INSTANCE_ID_0;
  5693. param_hdr.param_id = AFE_PARAM_ID_INTERNAL_DIGIATL_CDC_CLK_CONFIG;
  5694. param_hdr.param_size = sizeof(clk_cfg);
  5695. clk_cfg = *cfg;
  5696. pr_debug("%s: Minor version =0x%x clk val = %d\n"
  5697. "clk root = 0x%x resrv = 0x%x port id = 0x%x\n",
  5698. __func__, cfg->i2s_cfg_minor_version,
  5699. cfg->clk_val, cfg->clk_root, cfg->reserved,
  5700. q6audio_get_port_id(port_id));
  5701. ret = q6afe_pack_and_set_param_in_band(port_id,
  5702. q6audio_get_port_index(port_id),
  5703. param_hdr, (u8 *) &clk_cfg);
  5704. if (ret < 0)
  5705. pr_err("%s: AFE enable for port 0x0x%x ret %d\n",
  5706. __func__, port_id, ret);
  5707. return ret;
  5708. }
  5709. int afe_enable_lpass_core_shared_clock(u16 port_id, u32 enable)
  5710. {
  5711. struct afe_param_id_lpass_core_shared_clk_cfg clk_cfg;
  5712. struct param_hdr_v3 param_hdr;
  5713. int ret = 0;
  5714. memset(&clk_cfg, 0, sizeof(clk_cfg));
  5715. memset(&param_hdr, 0, sizeof(param_hdr));
  5716. ret = q6audio_is_digital_pcm_interface(port_id);
  5717. if (ret < 0) {
  5718. pr_err("%s: q6audio_is_digital_pcm_interface fail %d\n",
  5719. __func__, ret);
  5720. return -EINVAL;
  5721. }
  5722. mutex_lock(&this_afe.afe_cmd_lock);
  5723. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5724. param_hdr.instance_id = INSTANCE_ID_0;
  5725. param_hdr.param_id = AFE_PARAM_ID_LPASS_CORE_SHARED_CLOCK_CONFIG;
  5726. param_hdr.param_size = sizeof(clk_cfg);
  5727. clk_cfg.lpass_core_shared_clk_cfg_minor_version =
  5728. AFE_API_VERSION_LPASS_CORE_SHARED_CLK_CONFIG;
  5729. clk_cfg.enable = enable;
  5730. pr_debug("%s: port id = %d, enable = %d\n",
  5731. __func__, q6audio_get_port_id(port_id), enable);
  5732. ret = q6afe_pack_and_set_param_in_band(port_id,
  5733. q6audio_get_port_index(port_id),
  5734. param_hdr, (u8 *) &clk_cfg);
  5735. if (ret < 0)
  5736. pr_err("%s: AFE enable for port 0x%x ret %d\n",
  5737. __func__, port_id, ret);
  5738. mutex_unlock(&this_afe.afe_cmd_lock);
  5739. return ret;
  5740. }
  5741. int q6afe_check_osr_clk_freq(u32 freq)
  5742. {
  5743. int ret = 0;
  5744. switch (freq) {
  5745. case Q6AFE_LPASS_OSR_CLK_12_P288_MHZ:
  5746. case Q6AFE_LPASS_OSR_CLK_9_P600_MHZ:
  5747. case Q6AFE_LPASS_OSR_CLK_8_P192_MHZ:
  5748. case Q6AFE_LPASS_OSR_CLK_6_P144_MHZ:
  5749. case Q6AFE_LPASS_OSR_CLK_4_P096_MHZ:
  5750. case Q6AFE_LPASS_OSR_CLK_3_P072_MHZ:
  5751. case Q6AFE_LPASS_OSR_CLK_2_P048_MHZ:
  5752. case Q6AFE_LPASS_OSR_CLK_1_P536_MHZ:
  5753. case Q6AFE_LPASS_OSR_CLK_1_P024_MHZ:
  5754. case Q6AFE_LPASS_OSR_CLK_768_kHZ:
  5755. case Q6AFE_LPASS_OSR_CLK_512_kHZ:
  5756. break;
  5757. default:
  5758. pr_err("%s: default freq 0x%x\n",
  5759. __func__, freq);
  5760. ret = -EINVAL;
  5761. }
  5762. return ret;
  5763. }
  5764. int afe_get_sp_th_vi_ftm_data(struct afe_sp_th_vi_get_param *th_vi)
  5765. {
  5766. struct param_hdr_v3 param_hdr;
  5767. int port = SLIMBUS_4_TX;
  5768. int ret = -EINVAL;
  5769. if (!th_vi) {
  5770. pr_err("%s: Invalid params\n", __func__);
  5771. goto done;
  5772. }
  5773. if (this_afe.vi_tx_port != -1)
  5774. port = this_afe.vi_tx_port;
  5775. memset(&param_hdr, 0, sizeof(param_hdr));
  5776. param_hdr.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_TH_VI;
  5777. param_hdr.instance_id = INSTANCE_ID_0;
  5778. param_hdr.param_id = AFE_PARAM_ID_SP_V2_TH_VI_FTM_PARAMS;
  5779. param_hdr.param_size = sizeof(struct afe_sp_th_vi_ftm_params);
  5780. ret = q6afe_get_params(port, NULL, &param_hdr);
  5781. if (ret) {
  5782. pr_err("%s: Failed to get TH VI FTM data\n", __func__);
  5783. goto done;
  5784. }
  5785. th_vi->pdata = param_hdr;
  5786. memcpy(&th_vi->param, &this_afe.th_vi_resp.param,
  5787. sizeof(this_afe.th_vi_resp.param));
  5788. pr_debug("%s: DC resistance %d %d temp %d %d status %d %d\n",
  5789. __func__, th_vi->param.dc_res_q24[SP_V2_SPKR_1],
  5790. th_vi->param.dc_res_q24[SP_V2_SPKR_2],
  5791. th_vi->param.temp_q22[SP_V2_SPKR_1],
  5792. th_vi->param.temp_q22[SP_V2_SPKR_2],
  5793. th_vi->param.status[SP_V2_SPKR_1],
  5794. th_vi->param.status[SP_V2_SPKR_2]);
  5795. ret = 0;
  5796. done:
  5797. return ret;
  5798. }
  5799. int afe_get_sp_ex_vi_ftm_data(struct afe_sp_ex_vi_get_param *ex_vi)
  5800. {
  5801. struct param_hdr_v3 param_hdr;
  5802. int port = SLIMBUS_4_TX;
  5803. int ret = -EINVAL;
  5804. if (!ex_vi) {
  5805. pr_err("%s: Invalid params\n", __func__);
  5806. goto done;
  5807. }
  5808. if (this_afe.vi_tx_port != -1)
  5809. port = this_afe.vi_tx_port;
  5810. memset(&param_hdr, 0, sizeof(param_hdr));
  5811. param_hdr.module_id = AFE_MODULE_SPEAKER_PROTECTION_V2_EX_VI;
  5812. param_hdr.instance_id = INSTANCE_ID_0;
  5813. param_hdr.param_id = AFE_PARAM_ID_SP_V2_EX_VI_FTM_PARAMS;
  5814. param_hdr.param_size = sizeof(struct afe_sp_ex_vi_ftm_params);
  5815. ret = q6afe_get_params(port, NULL, &param_hdr);
  5816. if (ret < 0) {
  5817. pr_err("%s: get param port 0x%x param id[0x%x]failed %d\n",
  5818. __func__, port, param_hdr.param_id, ret);
  5819. goto done;
  5820. }
  5821. ex_vi->pdata = param_hdr;
  5822. memcpy(&ex_vi->param, &this_afe.ex_vi_resp.param,
  5823. sizeof(this_afe.ex_vi_resp.param));
  5824. pr_debug("%s: freq %d %d resistance %d %d qfactor %d %d state %d %d\n",
  5825. __func__, ex_vi->param.freq_q20[SP_V2_SPKR_1],
  5826. ex_vi->param.freq_q20[SP_V2_SPKR_2],
  5827. ex_vi->param.resis_q24[SP_V2_SPKR_1],
  5828. ex_vi->param.resis_q24[SP_V2_SPKR_2],
  5829. ex_vi->param.qmct_q24[SP_V2_SPKR_1],
  5830. ex_vi->param.qmct_q24[SP_V2_SPKR_2],
  5831. ex_vi->param.status[SP_V2_SPKR_1],
  5832. ex_vi->param.status[SP_V2_SPKR_2]);
  5833. ret = 0;
  5834. done:
  5835. return ret;
  5836. }
  5837. /**
  5838. * afe_get_av_dev_drift -
  5839. * command to retrieve AV drift
  5840. *
  5841. * @timing_stats: timing stats to be updated with AV drift values
  5842. * @port: AFE port ID
  5843. *
  5844. * Returns 0 on success or error on failure
  5845. */
  5846. int afe_get_av_dev_drift(struct afe_param_id_dev_timing_stats *timing_stats,
  5847. u16 port)
  5848. {
  5849. struct param_hdr_v3 param_hdr;
  5850. int ret = -EINVAL;
  5851. if (!timing_stats) {
  5852. pr_err("%s: Invalid params\n", __func__);
  5853. goto exit;
  5854. }
  5855. memset(&param_hdr, 0, sizeof(param_hdr));
  5856. param_hdr.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  5857. param_hdr.instance_id = INSTANCE_ID_0;
  5858. param_hdr.param_id = AFE_PARAM_ID_DEV_TIMING_STATS;
  5859. param_hdr.param_size = sizeof(struct afe_param_id_dev_timing_stats);
  5860. ret = q6afe_get_params(port, NULL, &param_hdr);
  5861. if (ret < 0) {
  5862. pr_err("%s: get param port 0x%x param id[0x%x] failed %d\n",
  5863. __func__, port, param_hdr.param_id, ret);
  5864. goto exit;
  5865. }
  5866. memcpy(timing_stats, &this_afe.av_dev_drift_resp.timing_stats,
  5867. param_hdr.param_size);
  5868. ret = 0;
  5869. exit:
  5870. return ret;
  5871. }
  5872. EXPORT_SYMBOL(afe_get_av_dev_drift);
  5873. int afe_spk_prot_get_calib_data(struct afe_spkr_prot_get_vi_calib *calib_resp)
  5874. {
  5875. struct param_hdr_v3 param_hdr;
  5876. int port = SLIMBUS_4_TX;
  5877. int ret = -EINVAL;
  5878. if (!calib_resp) {
  5879. pr_err("%s: Invalid params\n", __func__);
  5880. goto fail_cmd;
  5881. }
  5882. if (this_afe.vi_tx_port != -1)
  5883. port = this_afe.vi_tx_port;
  5884. memset(&param_hdr, 0, sizeof(param_hdr));
  5885. param_hdr.module_id = AFE_MODULE_FB_SPKR_PROT_VI_PROC_V2;
  5886. param_hdr.instance_id = INSTANCE_ID_0;
  5887. param_hdr.param_id = AFE_PARAM_ID_CALIB_RES_CFG_V2;
  5888. param_hdr.param_size = sizeof(struct afe_spkr_prot_get_vi_calib);
  5889. ret = q6afe_get_params(port, NULL, &param_hdr);
  5890. if (ret < 0) {
  5891. pr_err("%s: get param port 0x%x param id[0x%x]failed %d\n",
  5892. __func__, port, param_hdr.param_id, ret);
  5893. goto fail_cmd;
  5894. }
  5895. memcpy(&calib_resp->res_cfg, &this_afe.calib_data.res_cfg,
  5896. sizeof(this_afe.calib_data.res_cfg));
  5897. pr_info("%s: state %s resistance %d %d\n", __func__,
  5898. fbsp_state[calib_resp->res_cfg.th_vi_ca_state],
  5899. calib_resp->res_cfg.r0_cali_q24[SP_V2_SPKR_1],
  5900. calib_resp->res_cfg.r0_cali_q24[SP_V2_SPKR_2]);
  5901. ret = 0;
  5902. fail_cmd:
  5903. return ret;
  5904. }
  5905. /**
  5906. * afe_spk_prot_feed_back_cfg -
  5907. * command to setup spk protection feedback config
  5908. *
  5909. * @src_port: source port id
  5910. * @dst_port: destination port id
  5911. * @l_ch: left speaker active or not
  5912. * @r_ch: right speaker active or not
  5913. * @enable: flag to enable or disable
  5914. *
  5915. * Returns 0 on success or error on failure
  5916. */
  5917. int afe_spk_prot_feed_back_cfg(int src_port, int dst_port,
  5918. int l_ch, int r_ch, u32 enable)
  5919. {
  5920. int ret = -EINVAL;
  5921. union afe_spkr_prot_config prot_config;
  5922. int index = 0;
  5923. if (!enable) {
  5924. pr_debug("%s: Disable Feedback tx path", __func__);
  5925. this_afe.vi_tx_port = -1;
  5926. this_afe.vi_rx_port = -1;
  5927. return 0;
  5928. }
  5929. if ((q6audio_validate_port(src_port) < 0) ||
  5930. (q6audio_validate_port(dst_port) < 0)) {
  5931. pr_err("%s: invalid ports src 0x%x dst 0x%x",
  5932. __func__, src_port, dst_port);
  5933. goto fail_cmd;
  5934. }
  5935. if (!l_ch && !r_ch) {
  5936. pr_err("%s: error ch values zero\n", __func__);
  5937. goto fail_cmd;
  5938. }
  5939. pr_debug("%s: src_port 0x%x dst_port 0x%x l_ch %d r_ch %d\n",
  5940. __func__, src_port, dst_port, l_ch, r_ch);
  5941. memset(&prot_config, 0, sizeof(prot_config));
  5942. prot_config.feedback_path_cfg.dst_portid =
  5943. q6audio_get_port_id(dst_port);
  5944. if (l_ch) {
  5945. prot_config.feedback_path_cfg.chan_info[index++] = 1;
  5946. prot_config.feedback_path_cfg.chan_info[index++] = 2;
  5947. }
  5948. if (r_ch) {
  5949. prot_config.feedback_path_cfg.chan_info[index++] = 3;
  5950. prot_config.feedback_path_cfg.chan_info[index++] = 4;
  5951. }
  5952. prot_config.feedback_path_cfg.num_channels = index;
  5953. pr_debug("%s no of channels: %d\n", __func__, index);
  5954. prot_config.feedback_path_cfg.minor_version = 1;
  5955. ret = afe_spk_prot_prepare(src_port, dst_port,
  5956. AFE_PARAM_ID_FEEDBACK_PATH_CFG, &prot_config);
  5957. fail_cmd:
  5958. return ret;
  5959. }
  5960. EXPORT_SYMBOL(afe_spk_prot_feed_back_cfg);
  5961. static int get_cal_type_index(int32_t cal_type)
  5962. {
  5963. int ret = -EINVAL;
  5964. switch (cal_type) {
  5965. case AFE_COMMON_RX_CAL_TYPE:
  5966. ret = AFE_COMMON_RX_CAL;
  5967. break;
  5968. case AFE_COMMON_TX_CAL_TYPE:
  5969. ret = AFE_COMMON_TX_CAL;
  5970. break;
  5971. case AFE_LSM_TX_CAL_TYPE:
  5972. ret = AFE_LSM_TX_CAL;
  5973. break;
  5974. case AFE_AANC_CAL_TYPE:
  5975. ret = AFE_AANC_CAL;
  5976. break;
  5977. case AFE_HW_DELAY_CAL_TYPE:
  5978. ret = AFE_HW_DELAY_CAL;
  5979. break;
  5980. case AFE_FB_SPKR_PROT_CAL_TYPE:
  5981. ret = AFE_FB_SPKR_PROT_CAL;
  5982. break;
  5983. case AFE_SIDETONE_CAL_TYPE:
  5984. ret = AFE_SIDETONE_CAL;
  5985. break;
  5986. case AFE_SIDETONE_IIR_CAL_TYPE:
  5987. ret = AFE_SIDETONE_IIR_CAL;
  5988. break;
  5989. case AFE_TOPOLOGY_CAL_TYPE:
  5990. ret = AFE_TOPOLOGY_CAL;
  5991. break;
  5992. case AFE_LSM_TOPOLOGY_CAL_TYPE:
  5993. ret = AFE_LSM_TOPOLOGY_CAL;
  5994. break;
  5995. case AFE_CUST_TOPOLOGY_CAL_TYPE:
  5996. ret = AFE_CUST_TOPOLOGY_CAL;
  5997. break;
  5998. default:
  5999. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  6000. }
  6001. return ret;
  6002. }
  6003. int afe_alloc_cal(int32_t cal_type, size_t data_size,
  6004. void *data)
  6005. {
  6006. int ret = 0;
  6007. int cal_index;
  6008. cal_index = get_cal_type_index(cal_type);
  6009. pr_debug("%s: cal_type = %d cal_index = %d\n",
  6010. __func__, cal_type, cal_index);
  6011. if (cal_index < 0) {
  6012. pr_err("%s: could not get cal index %d!\n",
  6013. __func__, cal_index);
  6014. ret = -EINVAL;
  6015. goto done;
  6016. }
  6017. ret = cal_utils_alloc_cal(data_size, data,
  6018. this_afe.cal_data[cal_index], 0, NULL);
  6019. if (ret < 0) {
  6020. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  6021. __func__, ret, cal_type);
  6022. ret = -EINVAL;
  6023. goto done;
  6024. }
  6025. done:
  6026. return ret;
  6027. }
  6028. static int afe_dealloc_cal(int32_t cal_type, size_t data_size,
  6029. void *data)
  6030. {
  6031. int ret = 0;
  6032. int cal_index;
  6033. pr_debug("%s:\n", __func__);
  6034. cal_index = get_cal_type_index(cal_type);
  6035. if (cal_index < 0) {
  6036. pr_err("%s: could not get cal index %d!\n",
  6037. __func__, cal_index);
  6038. ret = -EINVAL;
  6039. goto done;
  6040. }
  6041. ret = cal_utils_dealloc_cal(data_size, data,
  6042. this_afe.cal_data[cal_index]);
  6043. if (ret < 0) {
  6044. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  6045. __func__, ret, cal_type);
  6046. ret = -EINVAL;
  6047. goto done;
  6048. }
  6049. done:
  6050. return ret;
  6051. }
  6052. static int afe_set_cal(int32_t cal_type, size_t data_size,
  6053. void *data)
  6054. {
  6055. int ret = 0;
  6056. int cal_index;
  6057. pr_debug("%s:\n", __func__);
  6058. cal_index = get_cal_type_index(cal_type);
  6059. if (cal_index < 0) {
  6060. pr_err("%s: could not get cal index %d!\n",
  6061. __func__, cal_index);
  6062. ret = -EINVAL;
  6063. goto done;
  6064. }
  6065. ret = cal_utils_set_cal(data_size, data,
  6066. this_afe.cal_data[cal_index], 0, NULL);
  6067. if (ret < 0) {
  6068. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  6069. __func__, ret, cal_type);
  6070. ret = -EINVAL;
  6071. goto done;
  6072. }
  6073. if (cal_index == AFE_CUST_TOPOLOGY_CAL) {
  6074. mutex_lock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  6075. this_afe.set_custom_topology = 1;
  6076. pr_debug("%s:[AFE_CUSTOM_TOPOLOGY] ret = %d, cal type = %d!\n",
  6077. __func__, ret, cal_type);
  6078. mutex_unlock(&this_afe.cal_data[AFE_CUST_TOPOLOGY_CAL]->lock);
  6079. }
  6080. done:
  6081. return ret;
  6082. }
  6083. static struct cal_block_data *afe_find_hw_delay_by_path(
  6084. struct cal_type_data *cal_type, int path)
  6085. {
  6086. struct list_head *ptr, *next;
  6087. struct cal_block_data *cal_block = NULL;
  6088. pr_debug("%s:\n", __func__);
  6089. list_for_each_safe(ptr, next,
  6090. &cal_type->cal_blocks) {
  6091. cal_block = list_entry(ptr,
  6092. struct cal_block_data, list);
  6093. if (cal_utils_is_cal_stale(cal_block))
  6094. continue;
  6095. if (((struct audio_cal_info_hw_delay *)cal_block->cal_info)
  6096. ->path == path) {
  6097. return cal_block;
  6098. }
  6099. }
  6100. return NULL;
  6101. }
  6102. static int afe_get_cal_hw_delay(int32_t path,
  6103. struct audio_cal_hw_delay_entry *entry)
  6104. {
  6105. int ret = 0;
  6106. int i;
  6107. struct cal_block_data *cal_block = NULL;
  6108. struct audio_cal_hw_delay_data *hw_delay_info = NULL;
  6109. pr_debug("%s:\n", __func__);
  6110. if (this_afe.cal_data[AFE_HW_DELAY_CAL] == NULL) {
  6111. pr_err("%s: AFE_HW_DELAY_CAL not initialized\n", __func__);
  6112. ret = -EINVAL;
  6113. goto done;
  6114. }
  6115. if (entry == NULL) {
  6116. pr_err("%s: entry is NULL\n", __func__);
  6117. ret = -EINVAL;
  6118. goto done;
  6119. }
  6120. if ((path >= MAX_PATH_TYPE) || (path < 0)) {
  6121. pr_err("%s: bad path: %d\n",
  6122. __func__, path);
  6123. ret = -EINVAL;
  6124. goto done;
  6125. }
  6126. mutex_lock(&this_afe.cal_data[AFE_HW_DELAY_CAL]->lock);
  6127. cal_block = afe_find_hw_delay_by_path(
  6128. this_afe.cal_data[AFE_HW_DELAY_CAL], path);
  6129. if (cal_block == NULL)
  6130. goto unlock;
  6131. hw_delay_info = &((struct audio_cal_info_hw_delay *)
  6132. cal_block->cal_info)->data;
  6133. if (hw_delay_info->num_entries > MAX_HW_DELAY_ENTRIES) {
  6134. pr_err("%s: invalid num entries: %d\n",
  6135. __func__, hw_delay_info->num_entries);
  6136. ret = -EINVAL;
  6137. goto unlock;
  6138. }
  6139. for (i = 0; i < hw_delay_info->num_entries; i++) {
  6140. if (hw_delay_info->entry[i].sample_rate ==
  6141. entry->sample_rate) {
  6142. entry->delay_usec = hw_delay_info->entry[i].delay_usec;
  6143. break;
  6144. }
  6145. }
  6146. if (i == hw_delay_info->num_entries) {
  6147. pr_err("%s: Unable to find delay for sample rate %d\n",
  6148. __func__, entry->sample_rate);
  6149. ret = -EFAULT;
  6150. goto unlock;
  6151. }
  6152. cal_utils_mark_cal_used(cal_block);
  6153. pr_debug("%s: Path = %d samplerate = %u usec = %u status %d\n",
  6154. __func__, path, entry->sample_rate, entry->delay_usec, ret);
  6155. unlock:
  6156. mutex_unlock(&this_afe.cal_data[AFE_HW_DELAY_CAL]->lock);
  6157. done:
  6158. return ret;
  6159. }
  6160. static int afe_set_cal_sp_th_vi_ftm_cfg(int32_t cal_type, size_t data_size,
  6161. void *data)
  6162. {
  6163. int ret = 0;
  6164. struct audio_cal_type_sp_th_vi_ftm_cfg *cal_data = data;
  6165. if (this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL ||
  6166. cal_data == NULL ||
  6167. data_size != sizeof(*cal_data))
  6168. goto done;
  6169. pr_debug("%s: cal_type = %d\n", __func__, cal_type);
  6170. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  6171. memcpy(&this_afe.th_ftm_cfg, &cal_data->cal_info,
  6172. sizeof(this_afe.th_ftm_cfg));
  6173. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  6174. done:
  6175. return ret;
  6176. }
  6177. static int afe_set_cal_sp_ex_vi_ftm_cfg(int32_t cal_type, size_t data_size,
  6178. void *data)
  6179. {
  6180. int ret = 0;
  6181. struct audio_cal_type_sp_ex_vi_ftm_cfg *cal_data = data;
  6182. if (this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL] == NULL ||
  6183. cal_data == NULL ||
  6184. data_size != sizeof(*cal_data))
  6185. goto done;
  6186. pr_debug("%s: cal_type = %d\n", __func__, cal_type);
  6187. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  6188. memcpy(&this_afe.ex_ftm_cfg, &cal_data->cal_info,
  6189. sizeof(this_afe.ex_ftm_cfg));
  6190. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  6191. done:
  6192. return ret;
  6193. }
  6194. static int afe_set_cal_fb_spkr_prot(int32_t cal_type, size_t data_size,
  6195. void *data)
  6196. {
  6197. int ret = 0;
  6198. struct audio_cal_type_fb_spk_prot_cfg *cal_data = data;
  6199. pr_debug("%s:\n", __func__);
  6200. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL)
  6201. goto done;
  6202. if (cal_data == NULL)
  6203. goto done;
  6204. if (data_size != sizeof(*cal_data))
  6205. goto done;
  6206. if (cal_data->cal_info.mode == MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS)
  6207. __pm_wakeup_event(&wl.ws, jiffies_to_msecs(WAKELOCK_TIMEOUT));
  6208. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  6209. memcpy(&this_afe.prot_cfg, &cal_data->cal_info,
  6210. sizeof(this_afe.prot_cfg));
  6211. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  6212. done:
  6213. return ret;
  6214. }
  6215. static int afe_get_cal_sp_th_vi_ftm_param(int32_t cal_type, size_t data_size,
  6216. void *data)
  6217. {
  6218. int i, ret = 0;
  6219. struct audio_cal_type_sp_th_vi_param *cal_data = data;
  6220. struct afe_sp_th_vi_get_param th_vi;
  6221. pr_debug("%s: cal_type = %d\n", __func__, cal_type);
  6222. if (this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL] == NULL ||
  6223. cal_data == NULL ||
  6224. data_size != sizeof(*cal_data))
  6225. goto done;
  6226. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  6227. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  6228. cal_data->cal_info.status[i] = -EINVAL;
  6229. cal_data->cal_info.r_dc_q24[i] = -1;
  6230. cal_data->cal_info.temp_q22[i] = -1;
  6231. }
  6232. if (!afe_get_sp_th_vi_ftm_data(&th_vi)) {
  6233. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  6234. pr_debug("%s: ftm param status = %d\n",
  6235. __func__, th_vi.param.status[i]);
  6236. if (th_vi.param.status[i] == FBSP_IN_PROGRESS) {
  6237. cal_data->cal_info.status[i] = -EAGAIN;
  6238. } else if (th_vi.param.status[i] == FBSP_SUCCESS) {
  6239. cal_data->cal_info.status[i] = 0;
  6240. cal_data->cal_info.r_dc_q24[i] =
  6241. th_vi.param.dc_res_q24[i];
  6242. cal_data->cal_info.temp_q22[i] =
  6243. th_vi.param.temp_q22[i];
  6244. }
  6245. }
  6246. }
  6247. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_TH_VI_CAL]->lock);
  6248. done:
  6249. return ret;
  6250. }
  6251. static int afe_get_cal_sp_ex_vi_ftm_param(int32_t cal_type, size_t data_size,
  6252. void *data)
  6253. {
  6254. int i, ret = 0;
  6255. struct audio_cal_type_sp_ex_vi_param *cal_data = data;
  6256. struct afe_sp_ex_vi_get_param ex_vi;
  6257. pr_debug("%s: cal_type = %d\n", __func__, cal_type);
  6258. if (this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL] == NULL ||
  6259. cal_data == NULL ||
  6260. data_size != sizeof(*cal_data))
  6261. goto done;
  6262. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  6263. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  6264. cal_data->cal_info.status[i] = -EINVAL;
  6265. cal_data->cal_info.freq_q20[i] = -1;
  6266. cal_data->cal_info.resis_q24[i] = -1;
  6267. cal_data->cal_info.qmct_q24[i] = -1;
  6268. }
  6269. if (!afe_get_sp_ex_vi_ftm_data(&ex_vi)) {
  6270. for (i = 0; i < SP_V2_NUM_MAX_SPKRS; i++) {
  6271. pr_debug("%s: ftm param status = %d\n",
  6272. __func__, ex_vi.param.status[i]);
  6273. if (ex_vi.param.status[i] == FBSP_IN_PROGRESS) {
  6274. cal_data->cal_info.status[i] = -EAGAIN;
  6275. } else if (ex_vi.param.status[i] == FBSP_SUCCESS) {
  6276. cal_data->cal_info.status[i] = 0;
  6277. cal_data->cal_info.freq_q20[i] =
  6278. ex_vi.param.freq_q20[i];
  6279. cal_data->cal_info.resis_q24[i] =
  6280. ex_vi.param.resis_q24[i];
  6281. cal_data->cal_info.qmct_q24[i] =
  6282. ex_vi.param.qmct_q24[i];
  6283. }
  6284. }
  6285. }
  6286. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_EX_VI_CAL]->lock);
  6287. done:
  6288. return ret;
  6289. }
  6290. static int afe_get_cal_fb_spkr_prot(int32_t cal_type, size_t data_size,
  6291. void *data)
  6292. {
  6293. int ret = 0;
  6294. struct audio_cal_type_fb_spk_prot_status *cal_data = data;
  6295. struct afe_spkr_prot_get_vi_calib calib_resp;
  6296. pr_debug("%s:\n", __func__);
  6297. if (this_afe.cal_data[AFE_FB_SPKR_PROT_CAL] == NULL)
  6298. goto done;
  6299. if (cal_data == NULL)
  6300. goto done;
  6301. if (data_size != sizeof(*cal_data))
  6302. goto done;
  6303. mutex_lock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  6304. if (this_afe.prot_cfg.mode == MSM_SPKR_PROT_CALIBRATED) {
  6305. cal_data->cal_info.r0[SP_V2_SPKR_1] =
  6306. this_afe.prot_cfg.r0[SP_V2_SPKR_1];
  6307. cal_data->cal_info.r0[SP_V2_SPKR_2] =
  6308. this_afe.prot_cfg.r0[SP_V2_SPKR_2];
  6309. cal_data->cal_info.status = 0;
  6310. } else if (this_afe.prot_cfg.mode ==
  6311. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS) {
  6312. /*Call AFE to query the status*/
  6313. cal_data->cal_info.status = -EINVAL;
  6314. cal_data->cal_info.r0[SP_V2_SPKR_1] = -1;
  6315. cal_data->cal_info.r0[SP_V2_SPKR_2] = -1;
  6316. if (!afe_spk_prot_get_calib_data(&calib_resp)) {
  6317. if (calib_resp.res_cfg.th_vi_ca_state ==
  6318. FBSP_IN_PROGRESS)
  6319. cal_data->cal_info.status = -EAGAIN;
  6320. else if (calib_resp.res_cfg.th_vi_ca_state ==
  6321. FBSP_SUCCESS) {
  6322. cal_data->cal_info.status = 0;
  6323. cal_data->cal_info.r0[SP_V2_SPKR_1] =
  6324. calib_resp.res_cfg.r0_cali_q24[SP_V2_SPKR_1];
  6325. cal_data->cal_info.r0[SP_V2_SPKR_2] =
  6326. calib_resp.res_cfg.r0_cali_q24[SP_V2_SPKR_2];
  6327. }
  6328. }
  6329. if (!cal_data->cal_info.status) {
  6330. this_afe.prot_cfg.mode =
  6331. MSM_SPKR_PROT_CALIBRATED;
  6332. this_afe.prot_cfg.r0[SP_V2_SPKR_1] =
  6333. cal_data->cal_info.r0[SP_V2_SPKR_1];
  6334. this_afe.prot_cfg.r0[SP_V2_SPKR_2] =
  6335. cal_data->cal_info.r0[SP_V2_SPKR_2];
  6336. }
  6337. } else {
  6338. /*Indicates calibration data is invalid*/
  6339. cal_data->cal_info.status = -EINVAL;
  6340. cal_data->cal_info.r0[SP_V2_SPKR_1] = -1;
  6341. cal_data->cal_info.r0[SP_V2_SPKR_2] = -1;
  6342. }
  6343. mutex_unlock(&this_afe.cal_data[AFE_FB_SPKR_PROT_CAL]->lock);
  6344. __pm_relax(&wl.ws);
  6345. done:
  6346. return ret;
  6347. }
  6348. static int afe_map_cal_data(int32_t cal_type,
  6349. struct cal_block_data *cal_block)
  6350. {
  6351. int ret = 0;
  6352. int cal_index;
  6353. pr_debug("%s:\n", __func__);
  6354. cal_index = get_cal_type_index(cal_type);
  6355. if (cal_index < 0) {
  6356. pr_err("%s: could not get cal index %d!\n",
  6357. __func__, cal_index);
  6358. ret = -EINVAL;
  6359. goto done;
  6360. }
  6361. mutex_lock(&this_afe.afe_cmd_lock);
  6362. atomic_set(&this_afe.mem_map_cal_index, cal_index);
  6363. ret = afe_cmd_memory_map(cal_block->cal_data.paddr,
  6364. cal_block->map_data.map_size);
  6365. atomic_set(&this_afe.mem_map_cal_index, -1);
  6366. if (ret < 0) {
  6367. pr_err("%s: mmap did not work! size = %zd ret %d\n",
  6368. __func__,
  6369. cal_block->map_data.map_size, ret);
  6370. pr_debug("%s: mmap did not work! addr = 0x%pK, size = %zd\n",
  6371. __func__,
  6372. &cal_block->cal_data.paddr,
  6373. cal_block->map_data.map_size);
  6374. mutex_unlock(&this_afe.afe_cmd_lock);
  6375. goto done;
  6376. }
  6377. cal_block->map_data.q6map_handle = atomic_read(&this_afe.
  6378. mem_map_cal_handles[cal_index]);
  6379. mutex_unlock(&this_afe.afe_cmd_lock);
  6380. done:
  6381. return ret;
  6382. }
  6383. static int afe_unmap_cal_data(int32_t cal_type,
  6384. struct cal_block_data *cal_block)
  6385. {
  6386. int ret = 0;
  6387. int cal_index;
  6388. pr_debug("%s:\n", __func__);
  6389. cal_index = get_cal_type_index(cal_type);
  6390. if (cal_index < 0) {
  6391. pr_err("%s: could not get cal index %d!\n",
  6392. __func__, cal_index);
  6393. ret = -EINVAL;
  6394. goto done;
  6395. }
  6396. if (cal_block == NULL) {
  6397. pr_err("%s: Cal block is NULL!\n",
  6398. __func__);
  6399. goto done;
  6400. }
  6401. if (cal_block->map_data.q6map_handle == 0) {
  6402. pr_err("%s: Map handle is NULL, nothing to unmap\n",
  6403. __func__);
  6404. goto done;
  6405. }
  6406. atomic_set(&this_afe.mem_map_cal_handles[cal_index],
  6407. cal_block->map_data.q6map_handle);
  6408. atomic_set(&this_afe.mem_map_cal_index, cal_index);
  6409. ret = afe_cmd_memory_unmap_nowait(
  6410. cal_block->map_data.q6map_handle);
  6411. atomic_set(&this_afe.mem_map_cal_index, -1);
  6412. if (ret < 0) {
  6413. pr_err("%s: unmap did not work! cal_type %i ret %d\n",
  6414. __func__, cal_index, ret);
  6415. }
  6416. cal_block->map_data.q6map_handle = 0;
  6417. done:
  6418. return ret;
  6419. }
  6420. static void afe_delete_cal_data(void)
  6421. {
  6422. pr_debug("%s:\n", __func__);
  6423. cal_utils_destroy_cal_types(MAX_AFE_CAL_TYPES, this_afe.cal_data);
  6424. }
  6425. static int afe_init_cal_data(void)
  6426. {
  6427. int ret = 0;
  6428. struct cal_type_info cal_type_info[] = {
  6429. {{AFE_COMMON_RX_CAL_TYPE,
  6430. {afe_alloc_cal, afe_dealloc_cal, NULL,
  6431. afe_set_cal, NULL, NULL} },
  6432. {afe_map_cal_data, afe_unmap_cal_data,
  6433. cal_utils_match_buf_num} },
  6434. {{AFE_COMMON_TX_CAL_TYPE,
  6435. {afe_alloc_cal, afe_dealloc_cal, NULL,
  6436. afe_set_cal, NULL, NULL} },
  6437. {afe_map_cal_data, afe_unmap_cal_data,
  6438. cal_utils_match_buf_num} },
  6439. {{AFE_LSM_TX_CAL_TYPE,
  6440. {afe_alloc_cal, afe_dealloc_cal, NULL,
  6441. afe_set_cal, NULL, NULL} },
  6442. {afe_map_cal_data, afe_unmap_cal_data,
  6443. cal_utils_match_buf_num} },
  6444. {{AFE_AANC_CAL_TYPE,
  6445. {afe_alloc_cal, afe_dealloc_cal, NULL,
  6446. afe_set_cal, NULL, NULL} },
  6447. {afe_map_cal_data, afe_unmap_cal_data,
  6448. cal_utils_match_buf_num} },
  6449. {{AFE_FB_SPKR_PROT_CAL_TYPE,
  6450. {NULL, NULL, NULL, afe_set_cal_fb_spkr_prot,
  6451. afe_get_cal_fb_spkr_prot, NULL} },
  6452. {NULL, NULL, cal_utils_match_buf_num} },
  6453. {{AFE_HW_DELAY_CAL_TYPE,
  6454. {NULL, NULL, NULL,
  6455. afe_set_cal, NULL, NULL} },
  6456. {NULL, NULL, cal_utils_match_buf_num} },
  6457. {{AFE_SIDETONE_CAL_TYPE,
  6458. {NULL, NULL, NULL,
  6459. afe_set_cal, NULL, NULL} },
  6460. {NULL, NULL, cal_utils_match_buf_num} },
  6461. {{AFE_SIDETONE_IIR_CAL_TYPE,
  6462. {NULL, NULL, NULL,
  6463. afe_set_cal, NULL, NULL} },
  6464. {NULL, NULL, cal_utils_match_buf_num} },
  6465. {{AFE_TOPOLOGY_CAL_TYPE,
  6466. {NULL, NULL, NULL,
  6467. afe_set_cal, NULL, NULL} },
  6468. {NULL, NULL,
  6469. cal_utils_match_buf_num} },
  6470. {{AFE_LSM_TOPOLOGY_CAL_TYPE,
  6471. {NULL, NULL, NULL,
  6472. afe_set_cal, NULL, NULL} },
  6473. {NULL, NULL,
  6474. cal_utils_match_buf_num} },
  6475. {{AFE_CUST_TOPOLOGY_CAL_TYPE,
  6476. {afe_alloc_cal, afe_dealloc_cal, NULL,
  6477. afe_set_cal, NULL, NULL} },
  6478. {afe_map_cal_data, afe_unmap_cal_data,
  6479. cal_utils_match_buf_num} },
  6480. {{AFE_FB_SPKR_PROT_TH_VI_CAL_TYPE,
  6481. {NULL, NULL, NULL, afe_set_cal_sp_th_vi_ftm_cfg,
  6482. afe_get_cal_sp_th_vi_ftm_param, NULL} },
  6483. {NULL, NULL, cal_utils_match_buf_num} },
  6484. {{AFE_FB_SPKR_PROT_EX_VI_CAL_TYPE,
  6485. {NULL, NULL, NULL, afe_set_cal_sp_ex_vi_ftm_cfg,
  6486. afe_get_cal_sp_ex_vi_ftm_param, NULL} },
  6487. {NULL, NULL, cal_utils_match_buf_num} },
  6488. };
  6489. pr_debug("%s:\n", __func__);
  6490. ret = cal_utils_create_cal_types(MAX_AFE_CAL_TYPES, this_afe.cal_data,
  6491. cal_type_info);
  6492. if (ret < 0) {
  6493. pr_err("%s: could not create cal type! %d\n",
  6494. __func__, ret);
  6495. ret = -EINVAL;
  6496. goto err;
  6497. }
  6498. return ret;
  6499. err:
  6500. afe_delete_cal_data();
  6501. return ret;
  6502. }
  6503. int afe_map_rtac_block(struct rtac_cal_block_data *cal_block)
  6504. {
  6505. int result = 0;
  6506. pr_debug("%s:\n", __func__);
  6507. if (cal_block == NULL) {
  6508. pr_err("%s: cal_block is NULL!\n",
  6509. __func__);
  6510. result = -EINVAL;
  6511. goto done;
  6512. }
  6513. if (cal_block->cal_data.paddr == 0) {
  6514. pr_debug("%s: No address to map!\n",
  6515. __func__);
  6516. result = -EINVAL;
  6517. goto done;
  6518. }
  6519. if (cal_block->map_data.map_size == 0) {
  6520. pr_debug("%s: map size is 0!\n",
  6521. __func__);
  6522. result = -EINVAL;
  6523. goto done;
  6524. }
  6525. result = afe_cmd_memory_map(cal_block->cal_data.paddr,
  6526. cal_block->map_data.map_size);
  6527. if (result < 0) {
  6528. pr_err("%s: afe_cmd_memory_map failed for addr = 0x%pK, size = %d, err %d\n",
  6529. __func__, &cal_block->cal_data.paddr,
  6530. cal_block->map_data.map_size, result);
  6531. return result;
  6532. }
  6533. cal_block->map_data.map_handle = this_afe.mmap_handle;
  6534. done:
  6535. return result;
  6536. }
  6537. int afe_unmap_rtac_block(uint32_t *mem_map_handle)
  6538. {
  6539. int result = 0;
  6540. pr_debug("%s:\n", __func__);
  6541. if (mem_map_handle == NULL) {
  6542. pr_err("%s: Map handle is NULL, nothing to unmap\n",
  6543. __func__);
  6544. goto done;
  6545. }
  6546. if (*mem_map_handle == 0) {
  6547. pr_debug("%s: Map handle is 0, nothing to unmap\n",
  6548. __func__);
  6549. goto done;
  6550. }
  6551. result = afe_cmd_memory_unmap(*mem_map_handle);
  6552. if (result) {
  6553. pr_err("%s: AFE memory unmap failed %d, handle 0x%x\n",
  6554. __func__, result, *mem_map_handle);
  6555. goto done;
  6556. } else {
  6557. *mem_map_handle = 0;
  6558. }
  6559. done:
  6560. return result;
  6561. }
  6562. int __init afe_init(void)
  6563. {
  6564. int i = 0, ret;
  6565. atomic_set(&this_afe.state, 0);
  6566. atomic_set(&this_afe.status, 0);
  6567. atomic_set(&this_afe.mem_map_cal_index, -1);
  6568. this_afe.apr = NULL;
  6569. this_afe.dtmf_gen_rx_portid = -1;
  6570. this_afe.mmap_handle = 0;
  6571. this_afe.vi_tx_port = -1;
  6572. this_afe.vi_rx_port = -1;
  6573. this_afe.prot_cfg.mode = MSM_SPKR_PROT_DISABLED;
  6574. this_afe.th_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  6575. this_afe.ex_ftm_cfg.mode = MSM_SPKR_PROT_DISABLED;
  6576. mutex_init(&this_afe.afe_cmd_lock);
  6577. for (i = 0; i < AFE_MAX_PORTS; i++) {
  6578. this_afe.afe_cal_mode[i] = AFE_CAL_MODE_DEFAULT;
  6579. this_afe.afe_sample_rates[i] = 0;
  6580. this_afe.dev_acdb_id[i] = 0;
  6581. init_waitqueue_head(&this_afe.wait[i]);
  6582. }
  6583. wakeup_source_init(&wl.ws, "spkr-prot");
  6584. ret = afe_init_cal_data();
  6585. if (ret)
  6586. pr_err("%s: could not init cal data! %d\n", __func__, ret);
  6587. config_debug_fs_init();
  6588. return 0;
  6589. }
  6590. void afe_exit(void)
  6591. {
  6592. if (this_afe.apr) {
  6593. apr_reset(this_afe.apr);
  6594. atomic_set(&this_afe.state, 0);
  6595. this_afe.apr = NULL;
  6596. rtac_set_afe_handle(this_afe.apr);
  6597. }
  6598. afe_delete_cal_data();
  6599. config_debug_fs_exit();
  6600. mutex_destroy(&this_afe.afe_cmd_lock);
  6601. wakeup_source_trash(&wl.ws);
  6602. }